2 * Copyright 2014-2015 John Wehle <john@feith.com>
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 AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, 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
29 * Amlogic aml8726 clock measurement driver.
31 * This allows various clock rates to be determine at runtime.
32 * The measurements are done once and are not expected to change
33 * (i.e. FDT fixup provides clk81 as bus-frequency to the MMC
34 * and UART drivers which use the value when programming the
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
41 #include <sys/param.h>
42 #include <sys/systm.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
48 #include <sys/timetc.h>
49 #include <sys/timeet.h>
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53 #include <machine/fdt.h>
55 #include <dev/fdt/fdt_common.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
59 #include <arm/amlogic/aml8726/aml8726_soc.h>
60 #include <arm/amlogic/aml8726/aml8726_clkmsr.h>
63 static struct aml8726_clkmsr_clk {
66 } aml8726_clkmsr_clks[] = {
70 #define AML_CLKMSR_CLK81 0
72 #define AML_CLKMSR_NCLKS (sizeof(aml8726_clkmsr_clks) \
73 / sizeof(aml8726_clkmsr_clks[0]))
75 struct aml8726_clkmsr_softc {
77 struct resource * res[1];
80 static struct resource_spec aml8726_clkmsr_spec[] = {
81 { SYS_RES_MEMORY, 0, RF_ACTIVE },
86 * Duration can range from 1uS to 65535 uS and should be chosen
87 * based on the expected frequency result so to maximize resolution
88 * and avoid overflowing the 16 bit result counter.
90 #define AML_CLKMSR_DURATION 32
92 #define AML_CLKMSR_DUTY_REG 0
93 #define AML_CLKMSR_0_REG 4
94 #define AML_CLKMSR_0_BUSY (1U << 31)
95 #define AML_CLKMSR_0_MUX_MASK (0x3f << 20)
96 #define AML_CLKMSR_0_MUX_SHIFT 20
97 #define AML_CLKMSR_0_MUX_EN (1 << 19)
98 #define AML_CLKMSR_0_MEASURE (1 << 16)
99 #define AML_CLKMSR_0_DURATION_MASK 0xffff
100 #define AML_CLKMSR_0_DURATION_SHIFT 0
101 #define AML_CLKMSR_1_REG 8
102 #define AML_CLKMSR_2_REG 12
103 #define AML_CLKMSR_2_RESULT_MASK 0xffff
104 #define AML_CLKMSR_2_RESULT_SHIFT 0
106 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
107 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
108 #define CSR_BARRIER(sc, reg) bus_barrier((sc)->res[0], reg, 4, \
109 (BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE))
112 aml8726_clkmsr_clock_frequency(struct aml8726_clkmsr_softc *sc, unsigned clock)
116 if (clock >= AML_CLKMSR_NCLKS)
120 * Locking is not used as this is only expected to be called from
121 * FDT fixup (which occurs prior to driver initialization) or attach.
124 CSR_WRITE_4(sc, AML_CLKMSR_0_REG, 0);
126 CSR_BARRIER(sc, AML_CLKMSR_0_REG);
128 value = (aml8726_clkmsr_clks[clock].mux << AML_CLKMSR_0_MUX_SHIFT)
129 | ((AML_CLKMSR_DURATION - 1) << AML_CLKMSR_0_DURATION_SHIFT)
130 | AML_CLKMSR_0_MUX_EN
131 | AML_CLKMSR_0_MEASURE;
132 CSR_WRITE_4(sc, AML_CLKMSR_0_REG, value);
134 CSR_BARRIER(sc, AML_CLKMSR_0_REG);
136 while ((CSR_READ_4(sc, AML_CLKMSR_0_REG) & AML_CLKMSR_0_BUSY) != 0)
139 value &= ~AML_CLKMSR_0_MEASURE;
140 CSR_WRITE_4(sc, AML_CLKMSR_0_REG, value);
142 CSR_BARRIER(sc, AML_CLKMSR_0_REG);
144 value = (((CSR_READ_4(sc, AML_CLKMSR_2_REG) & AML_CLKMSR_2_RESULT_MASK)
145 >> AML_CLKMSR_2_RESULT_SHIFT) + AML_CLKMSR_DURATION / 2) /
152 aml8726_clkmsr_fixup_clk81(struct aml8726_clkmsr_softc *sc, int freq)
159 node = ofw_bus_get_node(sc->dev);
161 len = OF_getencprop(node, "clocks", &prop, sizeof(prop));
162 if ((len / sizeof(prop)) != 1 || prop == 0 ||
163 (clk_node = OF_node_from_xref(prop)) == 0)
166 len = OF_getencprop(clk_node, "clock-frequency", &prop, sizeof(prop));
167 if ((len / sizeof(prop)) != 1 || prop != 0)
170 freq = cpu_to_fdt32(freq);
172 OF_setprop(clk_node, "clock-frequency", (void *)&freq, sizeof(freq));
176 aml8726_clkmsr_probe(device_t dev)
179 if (!ofw_bus_status_okay(dev))
182 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-clkmsr"))
185 device_set_desc(dev, "Amlogic aml8726 clkmsr");
187 return (BUS_PROBE_DEFAULT);
191 aml8726_clkmsr_attach(device_t dev)
193 struct aml8726_clkmsr_softc *sc = device_get_softc(dev);
198 if (bus_alloc_resources(dev, aml8726_clkmsr_spec, sc->res)) {
199 device_printf(dev, "can not allocate resources for device\n");
203 freq = aml8726_clkmsr_clock_frequency(sc, AML_CLKMSR_CLK81);
204 device_printf(sc->dev, "bus clock %u MHz\n", freq);
206 aml8726_clkmsr_fixup_clk81(sc, freq * 1000000);
212 aml8726_clkmsr_detach(device_t dev)
214 struct aml8726_clkmsr_softc *sc = device_get_softc(dev);
216 bus_release_resources(dev, aml8726_clkmsr_spec, sc->res);
222 static device_method_t aml8726_clkmsr_methods[] = {
223 /* Device interface */
224 DEVMETHOD(device_probe, aml8726_clkmsr_probe),
225 DEVMETHOD(device_attach, aml8726_clkmsr_attach),
226 DEVMETHOD(device_detach, aml8726_clkmsr_detach),
231 static driver_t aml8726_clkmsr_driver = {
233 aml8726_clkmsr_methods,
234 sizeof(struct aml8726_clkmsr_softc),
237 static devclass_t aml8726_clkmsr_devclass;
239 EARLY_DRIVER_MODULE(clkmsr, simplebus, aml8726_clkmsr_driver,
240 aml8726_clkmsr_devclass, 0, 0, BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
243 aml8726_clkmsr_bus_frequency()
246 struct aml8726_clkmsr_softc sc;
252 KASSERT(aml8726_soc_hw_rev != AML_SOC_HW_REV_UNKNOWN,
253 ("aml8726_soc_hw_rev isn't initialized"));
256 * Try to access the clkmsr node directly i.e. through /aliases/.
259 if ((node = OF_finddevice("clkmsr")) != 0)
260 if (fdt_is_compatible_strict(node, "amlogic,aml8726-clkmsr"))
264 * Find the node the long way.
266 if ((node = OF_finddevice("/soc")) == 0)
269 if ((node = fdt_find_compatible(node,
270 "amlogic,aml8726-clkmsr", 1)) == 0)
274 if (fdt_get_range(OF_parent(node), 0, &pbase, &psize) != 0
275 || fdt_regsize(node, &start, &size) != 0)
280 memset(&mem, 0, sizeof(mem));
282 mem.r_bustag = fdtbus_bs_tag;
284 if (bus_space_map(mem.r_bustag, start, size, 0, &mem.r_bushandle) != 0)
288 * Build an incomplete (however sufficient for the purpose
289 * of calling aml8726_clkmsr_clock_frequency) softc.
292 memset(&sc, 0, sizeof(sc));
296 freq = aml8726_clkmsr_clock_frequency(&sc, AML_CLKMSR_CLK81) * 1000000;
298 bus_space_unmap(mem.r_bustag, mem.r_bushandle, size);