2 * Copyright (c) 2016 Jared McNeill <jmcneill@invisible.ca>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * 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 CAUSED
21 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
22 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <machine/bus.h>
44 #include <dev/ofw/ofw_bus.h>
45 #include <dev/ofw/ofw_bus_subr.h>
46 #include <dev/ofw/ofw_subr.h>
48 #include <dev/extres/clk/clk_mux.h>
50 #define A10_CPU_CLK_SRC_SEL_WIDTH 2
51 #define A10_CPU_CLK_SRC_SEL_SHIFT 16
53 #define A83T_Cx_CLK_SRC_SEL_WIDTH 1
54 #define A83T_C0_CLK_SRC_SEL_SHIFT 12
55 #define A83T_C1_CLK_SRC_SEL_SHIFT 28
57 struct aw_cpuclk_config {
62 static struct aw_cpuclk_config a10_config = {
63 .width = A10_CPU_CLK_SRC_SEL_WIDTH,
64 .shift = A10_CPU_CLK_SRC_SEL_SHIFT,
67 static struct aw_cpuclk_config a83t_c0_config = {
68 .width = A83T_Cx_CLK_SRC_SEL_WIDTH,
69 .shift = A83T_C0_CLK_SRC_SEL_SHIFT,
72 static struct aw_cpuclk_config a83t_c1_config = {
73 .width = A83T_Cx_CLK_SRC_SEL_WIDTH,
74 .shift = A83T_C1_CLK_SRC_SEL_SHIFT,
77 static struct ofw_compat_data compat_data[] = {
78 { "allwinner,sun4i-a10-cpu-clk", (uintptr_t)&a10_config },
79 { "allwinner,sun8i-a83t-c0cpu-clk", (uintptr_t)&a83t_c0_config },
80 { "allwinner,sun8i-a83t-c1cpu-clk", (uintptr_t)&a83t_c1_config },
81 { NULL, (uintptr_t)NULL }
84 #define CPUCLK_CONF(d) \
85 (void *)ofw_bus_search_compatible((d), compat_data)->ocd_data
88 aw_cpuclk_probe(device_t dev)
90 if (!ofw_bus_status_okay(dev))
93 if (CPUCLK_CONF(dev) == NULL)
96 device_set_desc(dev, "Allwinner CPU Clock");
97 return (BUS_PROBE_DEFAULT);
101 aw_cpuclk_attach(device_t dev)
103 struct clk_mux_def def;
104 struct clkdom *clkdom;
105 struct aw_cpuclk_config *conf;
109 int error, ncells, i;
112 node = ofw_bus_get_node(dev);
113 conf = CPUCLK_CONF(dev);
115 if (ofw_reg_to_paddr(node, 0, &paddr, &psize, NULL) != 0) {
116 device_printf(dev, "cannot parse 'reg' property\n");
120 error = ofw_bus_parse_xref_list_get_length(node, "clocks",
121 "#clock-cells", &ncells);
123 device_printf(dev, "cannot get clock count\n");
127 clkdom = clkdom_create(dev);
129 memset(&def, 0, sizeof(def));
131 def.clkdef.parent_names = malloc(sizeof(char *) * ncells, M_OFWPROP,
133 for (i = 0; i < ncells; i++) {
134 error = clk_get_by_ofw_index(dev, 0, i, &clk);
136 device_printf(dev, "cannot get clock %d\n", i);
139 def.clkdef.parent_names[i] = clk_get_name(clk);
142 def.clkdef.parent_cnt = ncells;
144 def.shift = conf->shift;
145 def.width = conf->width;
147 error = clk_parse_ofw_clk_name(dev, node, &def.clkdef.name);
149 device_printf(dev, "cannot parse clock name\n");
154 error = clknode_mux_register(clkdom, &def);
156 device_printf(dev, "cannot register mux clock\n");
161 if (clkdom_finit(clkdom) != 0) {
162 device_printf(dev, "cannot finalize clkdom initialization\n");
167 OF_prop_free(__DECONST(char *, def.clkdef.parent_names));
168 OF_prop_free(__DECONST(char *, def.clkdef.name));
176 OF_prop_free(__DECONST(char *, def.clkdef.name));
180 static device_method_t aw_cpuclk_methods[] = {
181 /* Device interface */
182 DEVMETHOD(device_probe, aw_cpuclk_probe),
183 DEVMETHOD(device_attach, aw_cpuclk_attach),
188 static driver_t aw_cpuclk_driver = {
194 static devclass_t aw_cpuclk_devclass;
196 EARLY_DRIVER_MODULE(aw_cpuclk, simplebus, aw_cpuclk_driver,
197 aw_cpuclk_devclass, 0, 0, BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);