Path: blob/main/sys/dev/clk/starfive/jh7110_clk_sys.c
39537 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright 2016 Michal Meloun <[email protected]>4* Copyright (c) 2020 Oskar Holmlund <[email protected]>5* Copyright (c) 2022 Mitchell Horne <[email protected]>6* Copyright (c) 2024 Jari Sihvola <[email protected]>7*/89/* Clocks for JH7110 SYS group. PLL driver must be attached before this. */1011#include <sys/param.h>12#include <sys/systm.h>13#include <sys/bus.h>14#include <sys/kernel.h>15#include <sys/module.h>16#include <sys/mutex.h>17#include <sys/resource.h>18#include <sys/rman.h>1920#include <machine/bus.h>2122#include <dev/fdt/simplebus.h>23#include <dev/ofw/ofw_bus.h>24#include <dev/ofw/ofw_bus_subr.h>2526#include <dev/clk/clk.h>27#include <dev/clk/starfive/jh7110_clk.h>28#include <dev/hwreset/hwreset.h>2930#include <dt-bindings/clock/starfive,jh7110-crg.h>3132#include "clkdev_if.h"33#include "hwreset_if.h"3435static struct ofw_compat_data compat_data[] = {36{ "starfive,jh7110-syscrg", 1 },37{ NULL, 0 }38};3940static struct resource_spec res_spec[] = {41{ SYS_RES_MEMORY, 0, RF_ACTIVE | RF_SHAREABLE },42RESOURCE_SPEC_END43};4445/* parents for non-pll SYS clocks */46static const char *cpu_root_p[] = { "osc", "pll0_out" };47static const char *cpu_core_p[] = { "cpu_root" };48static const char *cpu_bus_p[] = { "cpu_core" };49static const char *perh_root_p[] = { "pll0_out", "pll2_out" };50static const char *bus_root_p[] = { "osc", "pll2_out" };5152static const char *apb_bus_p[] = { "stg_axiahb" };53static const char *apb0_p[] = { "apb_bus" };54static const char *u0_sys_iomux_apb_p[] = { "apb_bus" };55static const char *stg_axiahb_p[] = { "axi_cfg0" };56static const char *ahb0_p[] = { "stg_axiahb" };57static const char *axi_cfg0_p[] = { "bus_root" };58static const char *nocstg_bus_p[] = { "bus_root" };59static const char *noc_bus_stg_axi_p[] = { "nocstg_bus" };6061static const char *u0_dw_uart_clk_apb_p[] = { "apb0" };62static const char *u0_dw_uart_clk_core_p[] = { "osc" };63static const char *u0_dw_sdio_clk_ahb_p[] = { "ahb0" };64static const char *u0_dw_sdio_clk_sdcard_p[] = { "axi_cfg0" };65static const char *u1_dw_uart_clk_apb_p[] = { "apb0" };66static const char *u1_dw_uart_clk_core_p[] = { "osc" };67static const char *u1_dw_sdio_clk_ahb_p[] = { "ahb0" };68static const char *u1_dw_sdio_clk_sdcard_p[] = { "axi_cfg0" };69static const char *usb_125m_p[] = { "pll0_out" };70static const char *u2_dw_uart_clk_apb_p[] = { "apb0" };71static const char *u2_dw_uart_clk_core_p[] = { "osc" };72static const char *u3_dw_uart_clk_apb_p[] = { "apb0" };73static const char *u3_dw_uart_clk_core_p[] = { "perh_root" };7475static const char *gmac_src_p[] = { "pll0_out" };76static const char *gmac_phy_p[] = { "gmac_src" };77static const char *gmac0_gtxclk_p[] = { "pll0_out" };78static const char *gmac0_ptp_p[] = { "gmac_src" };79static const char *gmac0_gtxc_p[] = { "gmac0_gtxclk" };80static const char *gmac1_gtxclk_p[] = { "pll0_out" };81static const char *gmac1_gtxc_p[] = { "gmac1_gtxclk" };82static const char *gmac1_rmii_rtx_p[] = { "gmac1_rmii_refin" };83static const char *gmac1_axi_p[] = { "stg_axiahb" };84static const char *gmac1_ahb_p[] = { "ahb0" };85static const char *gmac1_ptp_p[] = { "gmac_src" };86static const char *gmac1_tx_inv_p[] = { "gmac1_tx" };87static const char *gmac1_tx_p[] = { "gmac1_gtxclk", "gmac1_rmii_rtx" };88static const char *gmac1_rx_p[] = { "gmac1_rgmii_rxin", "gmac1_rmii_rtx" };89static const char *gmac1_rx_inv_p[] = { "gmac1_rx" };9091/* non-pll SYS clocks */92static const struct jh7110_clk_def sys_clks[] = {93JH7110_MUX(JH7110_SYSCLK_CPU_ROOT, "cpu_root", cpu_root_p),94JH7110_DIV(JH7110_SYSCLK_CPU_CORE, "cpu_core", cpu_core_p, 7),95JH7110_DIV(JH7110_SYSCLK_CPU_BUS, "cpu_bus", cpu_bus_p, 2),96JH7110_GATEDIV(JH7110_SYSCLK_PERH_ROOT, "perh_root", perh_root_p, 2),97JH7110_MUX(JH7110_SYSCLK_BUS_ROOT, "bus_root", bus_root_p),9899JH7110_GATE(JH7110_SYSCLK_APB0, "apb0", apb0_p),100JH7110_GATE(JH7110_SYSCLK_IOMUX_APB, "u0_sys_iomux_apb",101u0_sys_iomux_apb_p),102JH7110_GATE(JH7110_SYSCLK_UART0_APB, "u0_dw_uart_clk_apb",103u0_dw_uart_clk_apb_p),104JH7110_GATE(JH7110_SYSCLK_UART0_CORE, "u0_dw_uart_clk_core",105u0_dw_uart_clk_core_p),106JH7110_GATE(JH7110_SYSCLK_UART1_APB, "u1_dw_uart_clk_apb",107u1_dw_uart_clk_apb_p),108JH7110_GATE(JH7110_SYSCLK_UART1_CORE, "u1_dw_uart_clk_core",109u1_dw_uart_clk_core_p),110JH7110_GATE(JH7110_SYSCLK_UART2_APB, "u2_dw_uart_clk_apb",111u2_dw_uart_clk_apb_p),112JH7110_GATE(JH7110_SYSCLK_UART2_CORE, "u2_dw_uart_clk_core",113u2_dw_uart_clk_core_p),114JH7110_GATE(JH7110_SYSCLK_UART3_APB, "u3_dw_uart_clk_apb",115u3_dw_uart_clk_apb_p),116JH7110_GATE(JH7110_SYSCLK_UART3_CORE, "u3_dw_uart_clk_core",117u3_dw_uart_clk_core_p),118119JH7110_DIV(JH7110_SYSCLK_AXI_CFG0, "axi_cfg0", axi_cfg0_p, 3),120JH7110_DIV(JH7110_SYSCLK_STG_AXIAHB, "stg_axiahb", stg_axiahb_p, 2),121JH7110_DIV(JH7110_SYSCLK_NOCSTG_BUS, "nocstg_bus", nocstg_bus_p, 3),122JH7110_GATE(JH7110_SYSCLK_NOC_BUS_STG_AXI, "noc_bus_stg_axi",123noc_bus_stg_axi_p),124JH7110_GATE(JH7110_SYSCLK_AHB0, "ahb0", ahb0_p),125JH7110_DIV(JH7110_SYSCLK_APB_BUS, "apb_bus", apb_bus_p, 8),126127JH7110_GATE(JH7110_SYSCLK_SDIO0_AHB, "u0_dw_sdio_clk_ahb",128u0_dw_sdio_clk_ahb_p),129JH7110_GATE(JH7110_SYSCLK_SDIO1_AHB, "u1_dw_sdio_clk_ahb",130u1_dw_sdio_clk_ahb_p),131JH7110_GATEDIV(JH7110_SYSCLK_SDIO0_SDCARD, "u0_dw_sdio_clk_sdcard",132u0_dw_sdio_clk_sdcard_p, 15),133JH7110_GATEDIV(JH7110_SYSCLK_SDIO1_SDCARD, "u1_dw_sdio_clk_sdcard",134u1_dw_sdio_clk_sdcard_p, 15),135JH7110_DIV(JH7110_SYSCLK_USB_125M, "usb_125m", usb_125m_p, 15),136137JH7110_DIV(JH7110_SYSCLK_GMAC_SRC, "gmac_src", gmac_src_p, 7),138JH7110_GATEDIV(JH7110_SYSCLK_GMAC0_GTXCLK, "gmac0_gtxclk",139gmac0_gtxclk_p, 15),140JH7110_GATEDIV(JH7110_SYSCLK_GMAC0_PTP, "gmac0_ptp", gmac0_ptp_p, 31),141JH7110_GATEDIV(JH7110_SYSCLK_GMAC_PHY, "gmac_phy", gmac_phy_p, 31),142JH7110_GATE(JH7110_SYSCLK_GMAC0_GTXC, "gmac0_gtxc", gmac0_gtxc_p),143144JH7110_MUX(JH7110_SYSCLK_GMAC1_RX, "gmac1_rx", gmac1_rx_p),145JH7110_INV(JH7110_SYSCLK_GMAC1_RX_INV, "gmac1_rx_inv", gmac1_rx_inv_p),146JH7110_GATE(JH7110_SYSCLK_GMAC1_AHB, "gmac1_ahb", gmac1_ahb_p),147JH7110_DIV(JH7110_SYSCLK_GMAC1_GTXCLK, "gmac1_gtxclk",148gmac1_gtxclk_p, 15),149JH7110_GATEMUX(JH7110_SYSCLK_GMAC1_TX, "gmac1_tx", gmac1_tx_p),150JH7110_INV(JH7110_SYSCLK_GMAC1_TX_INV, "gmac1_tx_inv", gmac1_tx_inv_p),151JH7110_GATEDIV(JH7110_SYSCLK_GMAC1_PTP, "gmac1_ptp", gmac1_ptp_p, 31),152JH7110_GATE(JH7110_SYSCLK_GMAC1_AXI, "gmac1_axi", gmac1_axi_p),153JH7110_GATE(JH7110_SYSCLK_GMAC1_GTXC, "gmac1_gtxc", gmac1_gtxc_p),154JH7110_DIV(JH7110_SYSCLK_GMAC1_RMII_RTX, "gmac1_rmii_rtx",155gmac1_rmii_rtx_p, 30),156};157158static int159jh7110_clk_sys_probe(device_t dev)160{161if (!ofw_bus_status_okay(dev))162return (ENXIO);163164if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)165return (ENXIO);166167device_set_desc(dev, "StarFive JH7110 SYS clock generator");168169return (BUS_PROBE_DEFAULT);170}171172static int173jh7110_clk_sys_attach(device_t dev)174{175struct jh7110_clkgen_softc *sc;176int i, error;177178sc = device_get_softc(dev);179180sc->reset_status_offset = SYSCRG_RESET_STATUS;181sc->reset_selector_offset = SYSCRG_RESET_SELECTOR;182183mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);184185/* Allocate memory groups */186error = bus_alloc_resources(dev, res_spec, &sc->mem_res);187if (error != 0) {188device_printf(dev, "Couldn't allocate resources, error %d\n",189error);190return (ENXIO);191}192193/* Create clock domain */194sc->clkdom = clkdom_create(dev);195if (sc->clkdom == NULL) {196device_printf(dev, "Couldn't create clkdom\n");197return (ENXIO);198}199200/* Register clocks */201for (i = 0; i < nitems(sys_clks); i++) {202error = jh7110_clk_register(sc->clkdom, &sys_clks[i]);203if (error != 0) {204device_printf(dev, "Couldn't register clock %s: %d\n",205sys_clks[i].clkdef.name, error);206return (ENXIO);207}208}209210if (clkdom_finit(sc->clkdom) != 0)211panic("Cannot finalize clkdom initialization\n");212213if (bootverbose)214clkdom_dump(sc->clkdom);215216hwreset_register_ofw_provider(dev);217218return (0);219}220221static int222jh7110_clk_sys_detach(device_t dev)223{224/* Detach not supported */225return (EBUSY);226}227228static void229jh7110_clk_sys_device_lock(device_t dev)230{231struct jh7110_clkgen_softc *sc;232233sc = device_get_softc(dev);234mtx_lock(&sc->mtx);235}236237static void238jh7110_clk_sys_device_unlock(device_t dev)239{240struct jh7110_clkgen_softc *sc;241242sc = device_get_softc(dev);243mtx_unlock(&sc->mtx);244}245246static device_method_t jh7110_clk_sys_methods[] = {247/* Device interface */248DEVMETHOD(device_probe, jh7110_clk_sys_probe),249DEVMETHOD(device_attach, jh7110_clk_sys_attach),250DEVMETHOD(device_detach, jh7110_clk_sys_detach),251252/* clkdev interface */253DEVMETHOD(clkdev_device_lock, jh7110_clk_sys_device_lock),254DEVMETHOD(clkdev_device_unlock, jh7110_clk_sys_device_unlock),255256/* Reset interface */257DEVMETHOD(hwreset_assert, jh7110_reset_assert),258DEVMETHOD(hwreset_is_asserted, jh7110_reset_is_asserted),259260DEVMETHOD_END261};262263DEFINE_CLASS_0(jh7110_clk_sys, jh7110_clk_sys_driver, jh7110_clk_sys_methods,264sizeof(struct jh7110_clkgen_softc));265EARLY_DRIVER_MODULE(jh7110_clk_sys, simplebus, jh7110_clk_sys_driver, 0, 0,266BUS_PASS_BUS + BUS_PASS_ORDER_LATE);267MODULE_VERSION(jh7110_clk_sys, 1);268269270