Path: blob/main/sys/dev/clk/allwinner/ccu_sun8i_r.c
39536 views
/*-1* SPDX-License-Identifier: BSD-2-Clause2*3* Copyright (c) 2017,2018 Emmanuel Vadot <[email protected]>4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR15* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES16* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.17* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,18* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,19* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;20* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED21* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,22* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627#include <sys/param.h>28#include <sys/systm.h>29#include <sys/bus.h>30#include <sys/rman.h>31#include <sys/kernel.h>32#include <sys/module.h>33#include <machine/bus.h>3435#include <dev/fdt/simplebus.h>3637#include <dev/ofw/ofw_bus.h>38#include <dev/ofw/ofw_bus_subr.h>3940#if defined(__aarch64__)41#include "opt_soc.h"42#endif4344#include <dev/clk/clk_div.h>45#include <dev/clk/clk_fixed.h>46#include <dev/clk/clk_mux.h>4748#include <dev/clk/allwinner/aw_ccung.h>4950#include <dt-bindings/clock/sun8i-r-ccu.h>51#include <dt-bindings/reset/sun8i-r-ccu.h>5253/* Non-exported clocks */54#define CLK_AHB0 155#define CLK_APB0 25657static struct aw_ccung_reset ccu_sun8i_r_resets[] = {58CCU_RESET(RST_APB0_IR, 0xb0, 1)59CCU_RESET(RST_APB0_TIMER, 0xb0, 2)60CCU_RESET(RST_APB0_RSB, 0xb0, 3)61CCU_RESET(RST_APB0_UART, 0xb0, 4)62CCU_RESET(RST_APB0_I2C, 0xb0, 6)63};6465static struct aw_ccung_gate ccu_sun8i_r_gates[] = {66CCU_GATE(CLK_APB0_PIO, "apb0-pio", "apb0", 0x28, 0)67CCU_GATE(CLK_APB0_IR, "apb0-ir", "apb0", 0x28, 1)68CCU_GATE(CLK_APB0_TIMER, "apb0-timer", "apb0", 0x28, 2)69CCU_GATE(CLK_APB0_RSB, "apb0-rsb", "apb0", 0x28, 3)70CCU_GATE(CLK_APB0_UART, "apb0-uart", "apb0", 0x28, 4)71CCU_GATE(CLK_APB0_I2C, "apb0-i2c", "apb0", 0x28, 6)72CCU_GATE(CLK_APB0_TWD, "apb0-twd", "apb0", 0x28, 7)73};7475static const char *ar100_parents[] = {"osc32k", "osc24M", "pll_periph0", "iosc"};76static const char *a83t_ar100_parents[] = {"osc16M-d512", "osc24M", "pll_periph", "osc16M"};77PREDIV_CLK(ar100_clk, CLK_AR100, /* id */78"ar100", ar100_parents, /* name, parents */790x00, /* offset */8016, 2, /* mux */814, 2, 0, AW_CLK_FACTOR_POWER_OF_TWO, /* div */828, 5, 0, AW_CLK_FACTOR_HAS_COND, /* prediv */8316, 2, 2); /* prediv condition */84PREDIV_CLK(a83t_ar100_clk, CLK_AR100, /* id */85"ar100", a83t_ar100_parents, /* name, parents */860x00, /* offset */8716, 2, /* mux */884, 2, 0, AW_CLK_FACTOR_POWER_OF_TWO, /* div */898, 5, 0, AW_CLK_FACTOR_HAS_COND, /* prediv */9016, 2, 2); /* prediv condition */9192static const char *ahb0_parents[] = {"ar100"};93FIXED_CLK(ahb0_clk,94CLK_AHB0, /* id */95"ahb0", /* name */96ahb0_parents, /* parent */970, /* freq */981, /* mult */991, /* div */1000); /* flags */101102static const char *apb0_parents[] = {"ahb0"};103DIV_CLK(apb0_clk,104CLK_APB0, /* id */105"apb0", apb0_parents, /* name, parents */1060x0c, /* offset */1070, 2, /* shift, width */1080, NULL); /* flags, div table */109110static const char *r_ccu_ir_parents[] = {"osc32k", "osc24M"};111NM_CLK(r_ccu_ir_clk,112CLK_IR, /* id */113"ir", r_ccu_ir_parents, /* names, parents */1140x54, /* offset */1150, 4, 0, 0, /* N factor */11616, 2, 0, 0, /* M factor */11724, 2, /* mux */11831, /* gate */119AW_CLK_HAS_MUX | AW_CLK_REPARENT | AW_CLK_HAS_GATE);/* flags */120121static const char *a83t_ir_parents[] = {"osc16M", "osc24M"};122static struct aw_clk_nm_def a83t_ir_clk = {123.clkdef = {124.id = CLK_IR,125.name = "ir",126.parent_names = a83t_ir_parents,127.parent_cnt = nitems(a83t_ir_parents),128},129.offset = 0x54,130.n = {.shift = 0, .width = 4, .flags = AW_CLK_FACTOR_POWER_OF_TWO, },131.m = {.shift = 16, .width = 2},132.prediv = {133.cond_shift = 24,134.cond_width = 2,135.cond_value = 0,136.value = 16137},138.mux_shift = 24,139.mux_width = 2,140.flags = AW_CLK_HAS_MUX | AW_CLK_HAS_PREDIV,141};142143static struct aw_ccung_clk clks[] = {144{ .type = AW_CLK_PREDIV_MUX, .clk.prediv_mux = &ar100_clk},145{ .type = AW_CLK_DIV, .clk.div = &apb0_clk},146{ .type = AW_CLK_FIXED, .clk.fixed = &ahb0_clk},147{ .type = AW_CLK_NM, .clk.nm = &r_ccu_ir_clk},148};149150static struct aw_ccung_clk a83t_clks[] = {151{ .type = AW_CLK_PREDIV_MUX, .clk.prediv_mux = &a83t_ar100_clk},152{ .type = AW_CLK_DIV, .clk.div = &apb0_clk},153{ .type = AW_CLK_FIXED, .clk.fixed = &ahb0_clk},154{ .type = AW_CLK_NM, .clk.nm = &a83t_ir_clk},155};156157static struct ofw_compat_data compat_data[] = {158#if defined(SOC_ALLWINNER_H3) || defined(SOC_ALLWINNER_H5)159{ "allwinner,sun8i-h3-r-ccu", 1 },160#endif161#if defined(SOC_ALLWINNER_A64)162{ "allwinner,sun50i-a64-r-ccu", 1 },163#endif164{ NULL, 0},165};166167static int168ccu_sun8i_r_probe(device_t dev)169{170171if (!ofw_bus_status_okay(dev))172return (ENXIO);173174if (ofw_bus_search_compatible(dev, compat_data)->ocd_data == 0)175return (ENXIO);176177device_set_desc(dev, "Allwinner SUN8I_R Clock Control Unit NG");178return (BUS_PROBE_DEFAULT);179}180181static int182ccu_sun8i_r_attach(device_t dev)183{184struct aw_ccung_softc *sc;185186sc = device_get_softc(dev);187188sc->resets = ccu_sun8i_r_resets;189sc->nresets = nitems(ccu_sun8i_r_resets);190sc->gates = ccu_sun8i_r_gates;191sc->ngates = nitems(ccu_sun8i_r_gates);192sc->clks = clks;193sc->nclks = nitems(clks);194195return (aw_ccung_attach(dev));196}197198static device_method_t ccu_sun8i_r_methods[] = {199/* Device interface */200DEVMETHOD(device_probe, ccu_sun8i_r_probe),201DEVMETHOD(device_attach, ccu_sun8i_r_attach),202203DEVMETHOD_END204};205206DEFINE_CLASS_1(ccu_sun8i_r, ccu_sun8i_r_driver, ccu_sun8i_r_methods,207sizeof(struct aw_ccung_softc), aw_ccung_driver);208209EARLY_DRIVER_MODULE(ccu_sun8i_r, simplebus, ccu_sun8i_r_driver, 0, 0,210BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);211212static int213ccu_a83t_r_probe(device_t dev)214{215216if (!ofw_bus_status_okay(dev))217return (ENXIO);218219if (!ofw_bus_is_compatible(dev, "allwinner,sun8i-a83t-r-ccu"))220return (ENXIO);221222device_set_desc(dev, "Allwinner A83T_R Clock Control Unit NG");223return (BUS_PROBE_DEFAULT);224}225226static int227ccu_a83t_r_attach(device_t dev)228{229struct aw_ccung_softc *sc;230231sc = device_get_softc(dev);232233sc->resets = ccu_sun8i_r_resets;234sc->nresets = nitems(ccu_sun8i_r_resets);235sc->gates = ccu_sun8i_r_gates;236sc->ngates = nitems(ccu_sun8i_r_gates);237sc->clks = a83t_clks;238sc->nclks = nitems(a83t_clks);239240return (aw_ccung_attach(dev));241}242243static device_method_t ccu_a83t_r_methods[] = {244/* Device interface */245DEVMETHOD(device_probe, ccu_a83t_r_probe),246DEVMETHOD(device_attach, ccu_a83t_r_attach),247248DEVMETHOD_END249};250251DEFINE_CLASS_1(ccu_a83t_r, ccu_a83t_r_driver, ccu_a83t_r_methods,252sizeof(struct aw_ccung_softc), aw_ccung_driver);253254EARLY_DRIVER_MODULE(ccu_a83t_r, simplebus, ccu_a83t_r_driver, 0, 0,255BUS_PASS_RESOURCE + BUS_PASS_ORDER_MIDDLE);256257258