Path: blob/main/sys/dev/clk/starfive/jh7110_clk_stg.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2024 Jari Sihvola <[email protected]>4*/56/* Clocks for STG group. PLL_OUT & SYS clocks must be registered first. */78#include <sys/param.h>9#include <sys/systm.h>10#include <sys/bus.h>11#include <sys/mutex.h>12#include <sys/kernel.h>13#include <sys/module.h>14#include <sys/resource.h>15#include <sys/rman.h>16#include <machine/bus.h>1718#include <dev/fdt/simplebus.h>19#include <dev/hwreset/hwreset.h>20#include <dev/ofw/ofw_bus.h>21#include <dev/ofw/ofw_bus_subr.h>2223#include <dev/clk/clk.h>24#include <dev/clk/starfive/jh7110_clk.h>2526#include <dt-bindings/clock/starfive,jh7110-crg.h>2728#include "clkdev_if.h"29#include "hwreset_if.h"3031static struct ofw_compat_data compat_data[] = {32{ "starfive,jh7110-stgcrg", 1 },33{ NULL, 0 }34};3536static struct resource_spec res_spec[] = {37{ SYS_RES_MEMORY, 0, RF_ACTIVE },38RESOURCE_SPEC_END39};4041/* parents */42static const char *e2_rtc_p[] = { "osc" };43static const char *e2_core_p[] = { "stg_axiahb" };44static const char *e2_dbg_p[] = { "stg_axiahb" };4546static const char *pcie_slv_main_p[] = { "stg_axiahb" };47static const char *pcie0_tl_p[] = { "stg_axiahb" };48static const char *pcie1_tl_p[] = { "stg_axiahb" };49static const char *pcie0_axi_mst0_p[] = { "stg_axiahb" };50static const char *pcie1_axi_mst0_p[] = { "stg_axiahb" };51static const char *pcie0_apb_p[] = { "apb_bus" };52static const char *pcie1_apb_p[] = { "apb_bus" };5354static const char *usb0_lpm_p[] = { "osc" };55static const char *usb0_stb_p[] = { "osc" };56static const char *usb0_apb_p[] = { "apb_bus" };57static const char *usb0_utmi_apb_p[] = { "apb_bus" };58static const char *usb0_axi_p[] = { "stg_axiahb" };59static const char *usb0_app_125_p[] = { "usb_125m" };60static const char *usb0_refclk_p[] = { "osc" };6162static const char *dma1p_axi_p[] = { "stg_axiahb" };63static const char *dma1p_ahb_p[] = { "stg_axiahb" };6465/* STG clocks */66static const struct jh7110_clk_def stg_clks[] = {67JH7110_GATE(JH7110_STGCLK_USB0_APB, "usb0_apb", usb0_apb_p),68JH7110_GATE(JH7110_STGCLK_USB0_UTMI_APB, "usb0_utmi_apb",69usb0_utmi_apb_p),70JH7110_GATE(JH7110_STGCLK_USB0_AXI, "usb0_axi", usb0_axi_p),71JH7110_GATEDIV(JH7110_STGCLK_USB0_LPM, "usb0_lpm", usb0_lpm_p, 2),72JH7110_GATEDIV(JH7110_STGCLK_USB0_STB, "usb0_stb", usb0_stb_p, 4),73JH7110_GATE(JH7110_STGCLK_USB0_APP_125, "usb0_app_125", usb0_app_125_p),74JH7110_DIV(JH7110_STGCLK_USB0_REFCLK, "usb0_refclk", usb0_refclk_p, 2),7576JH7110_GATE(JH7110_STGCLK_PCIE0_AXI_MST0, "pcie0_axi_mst0",77pcie0_axi_mst0_p),78JH7110_GATE(JH7110_STGCLK_PCIE0_APB, "pcie0_apb", pcie0_apb_p),79JH7110_GATE(JH7110_STGCLK_PCIE0_TL, "pcie0_tl", pcie0_tl_p),80JH7110_GATE(JH7110_STGCLK_PCIE1_AXI_MST0, "pcie1_axi_mst0",81pcie1_axi_mst0_p),8283JH7110_GATE(JH7110_STGCLK_PCIE1_APB, "pcie1_apb", pcie1_apb_p),84JH7110_GATE(JH7110_STGCLK_PCIE1_TL, "pcie1_tl", pcie1_tl_p),85JH7110_GATE(JH7110_STGCLK_PCIE_SLV_MAIN, "pcie_slv_main",86pcie_slv_main_p),8788JH7110_GATEDIV(JH7110_STGCLK_E2_RTC, "e2_rtc", e2_rtc_p, 24),89JH7110_GATE(JH7110_STGCLK_E2_CORE, "e2_core", e2_core_p),90JH7110_GATE(JH7110_STGCLK_E2_DBG, "e2_dbg", e2_dbg_p),9192JH7110_GATE(JH7110_STGCLK_DMA1P_AXI, "dma1p_axi", dma1p_axi_p),93JH7110_GATE(JH7110_STGCLK_DMA1P_AHB, "dma1p_ahb", dma1p_ahb_p),94};9596static int97jh7110_clk_stg_probe(device_t dev)98{99if (!ofw_bus_status_okay(dev))100return (ENXIO);101102if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)103return (ENXIO);104105device_set_desc(dev, "StarFive JH7110 STG clock generator");106107return (BUS_PROBE_DEFAULT);108}109110static int111jh7110_clk_stg_attach(device_t dev)112{113struct jh7110_clkgen_softc *sc;114int err;115116sc = device_get_softc(dev);117118sc->reset_status_offset = STGCRG_RESET_STATUS;119sc->reset_selector_offset = STGCRG_RESET_SELECTOR;120121mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);122123err = bus_alloc_resources(dev, res_spec, &sc->mem_res);124if (err != 0) {125device_printf(dev, "Couldn't allocate resources, error %d\n",126err);127return (ENXIO);128}129130sc->clkdom = clkdom_create(dev);131if (sc->clkdom == NULL) {132device_printf(dev, "Couldn't create clkdom, error %d\n", err);133return (ENXIO);134}135136for (int i = 0; i < nitems(stg_clks); i++) {137err = jh7110_clk_register(sc->clkdom, &stg_clks[i]);138if (err != 0) {139device_printf(dev,140"Couldn't register clk %s, error %d\n",141stg_clks[i].clkdef.name, err);142return (ENXIO);143}144}145146if (clkdom_finit(sc->clkdom) != 0)147panic("Cannot finalize clkdom initialization\n");148149if (bootverbose)150clkdom_dump(sc->clkdom);151152hwreset_register_ofw_provider(dev);153154return (0);155}156157static void158jh7110_clk_stg_device_lock(device_t dev)159{160struct jh7110_clkgen_softc *sc;161162sc = device_get_softc(dev);163mtx_lock(&sc->mtx);164}165166static void167jh7110_clk_stg_device_unlock(device_t dev)168{169struct jh7110_clkgen_softc *sc;170171sc = device_get_softc(dev);172mtx_unlock(&sc->mtx);173}174175static int176jh7110_clk_stg_detach(device_t dev)177{178/* Detach not supported */179return (EBUSY);180}181182static device_method_t jh7110_clk_stg_methods[] = {183/* Device interface */184DEVMETHOD(device_probe, jh7110_clk_stg_probe),185DEVMETHOD(device_attach, jh7110_clk_stg_attach),186DEVMETHOD(device_detach, jh7110_clk_stg_detach),187188/* clkdev interface */189DEVMETHOD(clkdev_device_lock, jh7110_clk_stg_device_lock),190DEVMETHOD(clkdev_device_unlock, jh7110_clk_stg_device_unlock),191192/* Reset interface */193DEVMETHOD(hwreset_assert, jh7110_reset_assert),194DEVMETHOD(hwreset_is_asserted, jh7110_reset_is_asserted),195196DEVMETHOD_END197};198199DEFINE_CLASS_0(jh7110_stg, jh7110_stg_driver, jh7110_clk_stg_methods,200sizeof(struct jh7110_clkgen_softc));201EARLY_DRIVER_MODULE(jh7110_stg, simplebus, jh7110_stg_driver, 0, 0,202BUS_PASS_BUS + BUS_PASS_ORDER_LATE + 1);203MODULE_VERSION(jh7110_stg, 1);204205206