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 nitems(aml8726_clkmsr_clks)
74 struct aml8726_clkmsr_softc {
76 struct resource * res[1];
79 static struct resource_spec aml8726_clkmsr_spec[] = {
80 { SYS_RES_MEMORY, 0, RF_ACTIVE },
85 * Duration can range from 1uS to 65535 uS and should be chosen
86 * based on the expected frequency result so to maximize resolution
87 * and avoid overflowing the 16 bit result counter.
89 #define AML_CLKMSR_DURATION 32
91 #define AML_CLKMSR_DUTY_REG 0
92 #define AML_CLKMSR_0_REG 4
93 #define AML_CLKMSR_0_BUSY (1U << 31)
94 #define AML_CLKMSR_0_MUX_MASK (0x3f << 20)
95 #define AML_CLKMSR_0_MUX_SHIFT 20
96 #define AML_CLKMSR_0_MUX_EN (1 << 19)
97 #define AML_CLKMSR_0_MEASURE (1 << 16)
98 #define AML_CLKMSR_0_DURATION_MASK 0xffff
99 #define AML_CLKMSR_0_DURATION_SHIFT 0
100 #define AML_CLKMSR_1_REG 8
101 #define AML_CLKMSR_2_REG 12
102 #define AML_CLKMSR_2_RESULT_MASK 0xffff
103 #define AML_CLKMSR_2_RESULT_SHIFT 0
105 #define CSR_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], reg, (val))
106 #define CSR_READ_4(sc, reg) bus_read_4((sc)->res[0], reg)
107 #define CSR_BARRIER(sc, reg) bus_barrier((sc)->res[0], reg, 4, \
108 (BUS_SPACE_BARRIER_READ | BUS_SPACE_BARRIER_WRITE))
111 aml8726_clkmsr_clock_frequency(struct aml8726_clkmsr_softc *sc, unsigned clock)
115 if (clock >= AML_CLKMSR_NCLKS)
119 * Locking is not used as this is only expected to be called from
120 * FDT fixup (which occurs prior to driver initialization) or attach.
123 CSR_WRITE_4(sc, AML_CLKMSR_0_REG, 0);
125 CSR_BARRIER(sc, AML_CLKMSR_0_REG);
127 value = (aml8726_clkmsr_clks[clock].mux << AML_CLKMSR_0_MUX_SHIFT)
128 | ((AML_CLKMSR_DURATION - 1) << AML_CLKMSR_0_DURATION_SHIFT)
129 | AML_CLKMSR_0_MUX_EN
130 | AML_CLKMSR_0_MEASURE;
131 CSR_WRITE_4(sc, AML_CLKMSR_0_REG, value);
133 CSR_BARRIER(sc, AML_CLKMSR_0_REG);
135 while ((CSR_READ_4(sc, AML_CLKMSR_0_REG) & AML_CLKMSR_0_BUSY) != 0)
138 value &= ~AML_CLKMSR_0_MEASURE;
139 CSR_WRITE_4(sc, AML_CLKMSR_0_REG, value);
141 CSR_BARRIER(sc, AML_CLKMSR_0_REG);
143 value = (((CSR_READ_4(sc, AML_CLKMSR_2_REG) & AML_CLKMSR_2_RESULT_MASK)
144 >> AML_CLKMSR_2_RESULT_SHIFT) + AML_CLKMSR_DURATION / 2) /
151 aml8726_clkmsr_fixup_clk81(struct aml8726_clkmsr_softc *sc, int freq)
158 node = ofw_bus_get_node(sc->dev);
160 len = OF_getencprop(node, "clocks", &prop, sizeof(prop));
161 if ((len / sizeof(prop)) != 1 || prop == 0 ||
162 (clk_node = OF_node_from_xref(prop)) == 0)
165 len = OF_getencprop(clk_node, "clock-frequency", &prop, sizeof(prop));
166 if ((len / sizeof(prop)) != 1 || prop != 0)
169 freq = cpu_to_fdt32(freq);
171 OF_setprop(clk_node, "clock-frequency", (void *)&freq, sizeof(freq));
175 aml8726_clkmsr_probe(device_t dev)
178 if (!ofw_bus_status_okay(dev))
181 if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-clkmsr"))
184 device_set_desc(dev, "Amlogic aml8726 clkmsr");
186 return (BUS_PROBE_DEFAULT);
190 aml8726_clkmsr_attach(device_t dev)
192 struct aml8726_clkmsr_softc *sc = device_get_softc(dev);
197 if (bus_alloc_resources(dev, aml8726_clkmsr_spec, sc->res)) {
198 device_printf(dev, "can not allocate resources for device\n");
202 freq = aml8726_clkmsr_clock_frequency(sc, AML_CLKMSR_CLK81);
203 device_printf(sc->dev, "bus clock %u MHz\n", freq);
205 aml8726_clkmsr_fixup_clk81(sc, freq * 1000000);
211 aml8726_clkmsr_detach(device_t dev)
213 struct aml8726_clkmsr_softc *sc = device_get_softc(dev);
215 bus_release_resources(dev, aml8726_clkmsr_spec, sc->res);
221 static device_method_t aml8726_clkmsr_methods[] = {
222 /* Device interface */
223 DEVMETHOD(device_probe, aml8726_clkmsr_probe),
224 DEVMETHOD(device_attach, aml8726_clkmsr_attach),
225 DEVMETHOD(device_detach, aml8726_clkmsr_detach),
230 static driver_t aml8726_clkmsr_driver = {
232 aml8726_clkmsr_methods,
233 sizeof(struct aml8726_clkmsr_softc),
236 static devclass_t aml8726_clkmsr_devclass;
238 EARLY_DRIVER_MODULE(clkmsr, simplebus, aml8726_clkmsr_driver,
239 aml8726_clkmsr_devclass, 0, 0, BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
242 aml8726_clkmsr_bus_frequency()
245 struct aml8726_clkmsr_softc sc;
251 KASSERT(aml8726_soc_hw_rev != AML_SOC_HW_REV_UNKNOWN,
252 ("aml8726_soc_hw_rev isn't initialized"));
255 * Try to access the clkmsr node directly i.e. through /aliases/.
258 if ((node = OF_finddevice("clkmsr")) != -1)
259 if (fdt_is_compatible_strict(node, "amlogic,aml8726-clkmsr"))
263 * Find the node the long way.
265 if ((node = OF_finddevice("/soc")) == -1)
268 if ((node = fdt_find_compatible(node,
269 "amlogic,aml8726-clkmsr", 1)) == 0)
273 if (fdt_get_range(OF_parent(node), 0, &pbase, &psize) != 0
274 || fdt_regsize(node, &start, &size) != 0)
279 memset(&mem, 0, sizeof(mem));
281 mem.r_bustag = fdtbus_bs_tag;
283 if (bus_space_map(mem.r_bustag, start, size, 0, &mem.r_bushandle) != 0)
287 * Build an incomplete (however sufficient for the purpose
288 * of calling aml8726_clkmsr_clock_frequency) softc.
291 memset(&sc, 0, sizeof(sc));
295 freq = aml8726_clkmsr_clock_frequency(&sc, AML_CLKMSR_CLK81) * 1000000;
297 bus_space_unmap(mem.r_bustag, mem.r_bushandle, size);