]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/arm/amlogic/aml8726/aml8726_clkmsr.c
MFH
[FreeBSD/FreeBSD.git] / sys / arm / amlogic / aml8726 / aml8726_clkmsr.c
1 /*-
2  * Copyright 2014-2015 John Wehle <john@feith.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  */
27
28 /*
29  * Amlogic aml8726 clock measurement driver.
30  *
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
35  * hardware).
36  */
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/bus.h>
44 #include <sys/kernel.h>
45 #include <sys/module.h>
46 #include <sys/malloc.h>
47 #include <sys/rman.h>
48 #include <sys/timetc.h>
49 #include <sys/timeet.h>
50
51 #include <machine/bus.h>
52 #include <machine/cpu.h>
53 #include <machine/fdt.h>
54
55 #include <dev/fdt/fdt_common.h>
56 #include <dev/ofw/ofw_bus.h>
57 #include <dev/ofw/ofw_bus_subr.h>
58
59 #include <arm/amlogic/aml8726/aml8726_soc.h>
60 #include <arm/amlogic/aml8726/aml8726_clkmsr.h>
61
62
63 static struct aml8726_clkmsr_clk {
64         const char *            name;
65         uint32_t                mux;
66 } aml8726_clkmsr_clks[] = {
67         { "clk81", 7 },
68 };
69
70 #define AML_CLKMSR_CLK81        0
71
72 #define AML_CLKMSR_NCLKS        (sizeof(aml8726_clkmsr_clks) \
73     / sizeof(aml8726_clkmsr_clks[0]))
74
75 struct aml8726_clkmsr_softc {
76         device_t                dev;
77         struct resource *       res[1];
78 };
79
80 static struct resource_spec aml8726_clkmsr_spec[] = {
81         { SYS_RES_MEMORY,       0,      RF_ACTIVE },
82         { -1, 0 }
83 };
84
85 /*
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.
89  */
90 #define AML_CLKMSR_DURATION             32
91
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
105
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))
110
111 static int
112 aml8726_clkmsr_clock_frequency(struct aml8726_clkmsr_softc *sc, unsigned clock)
113 {
114         uint32_t value;
115
116         if (clock >= AML_CLKMSR_NCLKS)
117                 return (0);
118
119         /*
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.
122          */
123
124         CSR_WRITE_4(sc, AML_CLKMSR_0_REG, 0);
125
126         CSR_BARRIER(sc, AML_CLKMSR_0_REG);
127
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);
133
134         CSR_BARRIER(sc, AML_CLKMSR_0_REG);
135
136         while ((CSR_READ_4(sc, AML_CLKMSR_0_REG) & AML_CLKMSR_0_BUSY) != 0)
137                 cpu_spinwait();
138
139         value &= ~AML_CLKMSR_0_MEASURE;
140         CSR_WRITE_4(sc, AML_CLKMSR_0_REG, value);
141
142         CSR_BARRIER(sc, AML_CLKMSR_0_REG);
143
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) /
146             AML_CLKMSR_DURATION;
147
148         return value;
149 }
150
151 static void
152 aml8726_clkmsr_fixup_clk81(struct aml8726_clkmsr_softc *sc, int freq)
153 {
154         pcell_t prop;
155         ssize_t len;
156         phandle_t clk_node;
157         phandle_t node;
158
159         node = ofw_bus_get_node(sc->dev);
160
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)
164                 return;
165
166         len = OF_getencprop(clk_node, "clock-frequency", &prop, sizeof(prop));
167         if ((len / sizeof(prop)) != 1 || prop != 0)
168                 return;
169
170         freq = cpu_to_fdt32(freq);
171
172         OF_setprop(clk_node, "clock-frequency", (void *)&freq, sizeof(freq));
173 }
174
175 static int
176 aml8726_clkmsr_probe(device_t dev)
177 {
178
179         if (!ofw_bus_status_okay(dev))
180                 return (ENXIO);
181
182         if (!ofw_bus_is_compatible(dev, "amlogic,aml8726-clkmsr"))
183                 return (ENXIO);
184
185         device_set_desc(dev, "Amlogic aml8726 clkmsr");
186
187         return (BUS_PROBE_DEFAULT);
188 }
189
190 static int
191 aml8726_clkmsr_attach(device_t dev)
192 {
193         struct aml8726_clkmsr_softc *sc = device_get_softc(dev);
194         int freq;
195
196         sc->dev = dev;
197
198         if (bus_alloc_resources(dev, aml8726_clkmsr_spec, sc->res)) {
199                 device_printf(dev, "can not allocate resources for device\n");
200                 return (ENXIO);
201         }
202
203         freq = aml8726_clkmsr_clock_frequency(sc, AML_CLKMSR_CLK81);
204         device_printf(sc->dev, "bus clock %u MHz\n", freq);
205
206         aml8726_clkmsr_fixup_clk81(sc, freq * 1000000);
207
208         return (0);
209 }
210
211 static int
212 aml8726_clkmsr_detach(device_t dev)
213 {
214         struct aml8726_clkmsr_softc *sc = device_get_softc(dev);
215
216         bus_release_resources(dev, aml8726_clkmsr_spec, sc->res);
217
218         return (0);
219 }
220
221
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),
227
228         DEVMETHOD_END
229 };
230
231 static driver_t aml8726_clkmsr_driver = {
232         "clkmsr",
233         aml8726_clkmsr_methods,
234         sizeof(struct aml8726_clkmsr_softc),
235 };
236
237 static devclass_t aml8726_clkmsr_devclass;
238
239 EARLY_DRIVER_MODULE(clkmsr, simplebus, aml8726_clkmsr_driver,
240     aml8726_clkmsr_devclass, 0, 0,  BUS_PASS_CPU + BUS_PASS_ORDER_EARLY);
241
242 int
243 aml8726_clkmsr_bus_frequency()
244 {
245         struct resource mem;
246         struct aml8726_clkmsr_softc sc;
247         phandle_t node;
248         u_long pbase, psize;
249         u_long start, size;
250         int freq;
251
252         KASSERT(aml8726_soc_hw_rev != AML_SOC_HW_REV_UNKNOWN,
253                 ("aml8726_soc_hw_rev isn't initialized"));
254
255         /*
256          * Try to access the clkmsr node directly i.e. through /aliases/.
257          */
258
259         if ((node = OF_finddevice("clkmsr")) != 0)
260                 if (fdt_is_compatible_strict(node, "amlogic,aml8726-clkmsr"))
261                          goto moveon;
262
263         /*
264          * Find the node the long way.
265          */
266         if ((node = OF_finddevice("/soc")) == 0)
267                 return (0);
268
269         if ((node = fdt_find_compatible(node,
270             "amlogic,aml8726-clkmsr", 1)) == 0)
271                 return (0);
272
273 moveon:
274         if (fdt_get_range(OF_parent(node), 0, &pbase, &psize) != 0
275             || fdt_regsize(node, &start, &size) != 0)
276                 return (0);
277
278         start += pbase;
279
280         memset(&mem, 0, sizeof(mem));
281
282         mem.r_bustag = fdtbus_bs_tag;
283
284         if (bus_space_map(mem.r_bustag, start, size, 0, &mem.r_bushandle) != 0)
285                 return (0);
286
287         /*
288          * Build an incomplete (however sufficient for the purpose
289          * of calling aml8726_clkmsr_clock_frequency) softc.
290          */
291
292         memset(&sc, 0, sizeof(sc));
293
294         sc.res[0] = &mem;
295
296         freq = aml8726_clkmsr_clock_frequency(&sc, AML_CLKMSR_CLK81) * 1000000;
297
298         bus_space_unmap(mem.r_bustag, mem.r_bushandle, size);
299
300         return (freq);
301 }