2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright 2020 Michal Meloun <mmel@FreeBSD.org>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <sys/param.h>
32 #include <sys/systm.h>
34 #include <sys/kernel.h>
36 #include <sys/module.h>
37 #include <sys/malloc.h>
40 #include <sys/mutex.h>
42 #include <machine/bus.h>
43 #include <machine/cpu.h>
45 #include <dev/extres/clk/clk_div.h>
46 #include <dev/extres/clk/clk_fixed.h>
47 #include <dev/extres/clk/clk_gate.h>
48 #include <dev/extres/clk/clk_mux.h>
49 #include <dev/extres/hwreset/hwreset.h>
50 #include <dev/ofw/openfirm.h>
51 #include <dev/ofw/ofw_bus.h>
52 #include <dev/ofw/ofw_bus_subr.h>
54 #include <dt-bindings/clock/tegra210-car.h>
56 #include "clkdev_if.h"
57 #include "hwreset_if.h"
58 #include "tegra210_car.h"
60 static struct ofw_compat_data compat_data[] = {
61 {"nvidia,tegra210-car", 1},
65 #define PLIST(x) static const char *x[]
67 /* Pure multiplexer. */
68 #define MUX(_id, cname, plists, o, s, w) \
71 .clkdef.name = cname, \
72 .clkdef.parent_names = plists, \
73 .clkdef.parent_cnt = nitems(plists), \
74 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
80 /* Fractional divider (7.1). */
81 #define DIV7_1(_id, cname, plist, o, s) \
84 .clkdef.name = cname, \
85 .clkdef.parent_names = (const char *[]){plist}, \
86 .clkdef.parent_cnt = 1, \
87 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
95 /* Integer divider. */
96 #define DIV(_id, cname, plist, o, s, w, f) \
99 .clkdef.name = cname, \
100 .clkdef.parent_names = (const char *[]){plist}, \
101 .clkdef.parent_cnt = 1, \
102 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
109 /* Gate in PLL block. */
110 #define GATE_PLL(_id, cname, plist, o, s) \
113 .clkdef.name = cname, \
114 .clkdef.parent_names = (const char *[]){plist}, \
115 .clkdef.parent_cnt = 1, \
116 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
125 #define GATE(_id, cname, plist, o, s) \
128 .clkdef.name = cname, \
129 .clkdef.parent_names = (const char *[]){plist}, \
130 .clkdef.parent_cnt = 1, \
131 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
140 #define GATE_INV(_id, cname, plist, o, s) \
143 .clkdef.name = cname, \
144 .clkdef.parent_names = (const char *[]){plist}, \
145 .clkdef.parent_cnt = 1, \
146 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
154 /* Fixed rate clock. */
155 #define FRATE(_id, cname, _freq) \
158 .clkdef.name = cname, \
159 .clkdef.parent_names = NULL, \
160 .clkdef.parent_cnt = 0, \
161 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
165 /* Fixed rate multipier/divider. */
166 #define FACT(_id, cname, pname, _mult, _div) \
169 .clkdef.name = cname, \
170 .clkdef.parent_names = (const char *[]){pname}, \
171 .clkdef.parent_cnt = 1, \
172 .clkdef.flags = CLK_NODE_STATIC_STRINGS, \
177 static uint32_t osc_freqs[16] = {
188 PLIST(mux_xusb_hs) = {"xusb_ss_div2", "pllU_60", "pc_xusb_ss" };
189 PLIST(mux_xusb_ssp) = {"xusb_ss", "osc_div_clk"};
192 /* Clocks adjusted online. */
193 static struct clk_fixed_def fixed_osc =
194 FRATE(TEGRA210_CLK_CLK_M, "osc", 38400000);
195 static struct clk_fixed_def fixed_clk_m =
196 FACT(0, "clk_m", "osc", 1, 1);
197 static struct clk_fixed_def fixed_osc_div =
198 FACT(0, "osc_div_clk", "osc", 1, 1);
200 static struct clk_fixed_def tegra210_fixed_clks[] = {
202 FRATE(0, "bogus", 1),
203 FRATE(0, "clk_s", 32768),
206 FRATE(0, "vimclk_sync", 1),
207 FRATE(0, "i2s1_sync", 1),
208 FRATE(0, "i2s2_sync", 1),
209 FRATE(0, "i2s3_sync", 1),
210 FRATE(0, "i2s4_sync", 1),
211 FRATE(0, "i2s5_sync", 1),
212 FRATE(0, "spdif_in_sync", 1),
215 FACT(TEGRA210_CLK_XUSB_SS_DIV2, "xusb_ss_div2", "xusb_ss", 1, 2),
218 FACT(0, "sor_safe_div", "pllP_out0", 1, 17),
219 FACT(0, "dpaux_div", "sor_safe", 1, 17),
220 FACT(0, "dpaux1_div", "sor_safe", 1, 17),
222 /* Not Yet Implemented */
223 FRATE(0, "audio", 10000000),
224 FRATE(0, "audio0", 10000000),
225 FRATE(0, "audio1", 10000000),
226 FRATE(0, "audio2", 10000000),
227 FRATE(0, "audio3", 10000000),
228 FRATE(0, "audio4", 10000000),
229 FRATE(0, "ext_vimclk", 10000000),
230 FRATE(0, "audiod1", 10000000),
231 FRATE(0, "audiod2", 10000000),
232 FRATE(0, "audiod3", 10000000),
233 FRATE(0, "dfllCPU_out", 10000000),
238 static struct clk_mux_def tegra210_mux_clks[] = {
240 MUX(TEGRA210_CLK_XUSB_HS_SRC, "xusb_hs", mux_xusb_hs, CLK_SOURCE_XUSB_SS, 25, 2),
241 MUX(0, "xusb_ssp", mux_xusb_ssp, CLK_SOURCE_XUSB_SS, 24, 1),
246 static struct clk_gate_def tegra210_gate_clks[] = {
247 /* Base peripheral clocks. */
248 GATE_INV(TEGRA210_CLK_HCLK, "hclk", "hclk_div", CLK_SYSTEM_RATE, 7),
249 GATE_INV(TEGRA210_CLK_PCLK, "pclk", "pclk_div", CLK_SYSTEM_RATE, 3),
250 GATE(TEGRA210_CLK_CML0, "cml0", "pllE_out0", PLLE_AUX, 0),
251 GATE(TEGRA210_CLK_CML1, "cml1", "pllE_out0", PLLE_AUX, 1),
252 GATE(0, "pllD_dsi_csi", "pllD_out0", PLLD_MISC, 21),
253 GATE(0, "pllP_hsio", "pllP_out0", PLLP_MISC1, 29),
254 GATE(0, "pllP_xusb", "pllP_hsio", PLLP_MISC1, 28),
257 static struct clk_div_def tegra210_div_clks[] = {
258 /* Base peripheral clocks. */
259 DIV(0, "hclk_div", "sclk", CLK_SYSTEM_RATE, 4, 2, 0),
260 DIV(0, "pclk_div", "hclk", CLK_SYSTEM_RATE, 0, 2, 0),
263 /* Initial setup table. */
264 static struct tegra210_init_item clk_init_table[] = {
265 /* clock, partent, frequency, enable */
266 {"uarta", "pllP_out0", 408000000, 0},
267 {"uartb", "pllP_out0", 408000000, 0},
268 {"uartc", "pllP_out0", 408000000, 0},
269 {"uartd", "pllP_out0", 408000000, 0},
270 {"pllA", NULL, 564480000, 1},
271 {"pllA_out0", NULL, 11289600, 1},
272 {"extperiph1", "pllA_out0", 0, 1},
273 {"i2s1", "pllA_out0", 11289600, 0},
274 {"i2s2", "pllA_out0", 11289600, 0},
275 {"i2s3", "pllA_out0", 11289600, 0},
276 {"i2s4", "pllA_out0", 11289600, 0},
277 {"i2s5", "pllA_out0", 11289600, 0},
278 {"host1x", "pllP_out0", 136000000, 1},
279 {"sclk", "pllP_out2", 102000000, 1},
280 {"dvfs_soc", "pllP_out0", 51000000, 1},
281 {"dvfs_ref", "pllP_out0", 51000000, 1},
282 {"spi4", "pllP_out0", 12000000, 1},
283 {"pllREFE", NULL, 672000000, 0},
285 {"xusb", NULL, 0, 1},
286 {"xusb_ss", "pllU_480", 120000000, 0},
287 {"pc_xusb_fs", "pllU_48", 48000000, 0},
288 {"xusb_hs", "pc_xusb_ss", 120000000, 0},
289 {"xusb_ssp", "xusb_ss", 120000000, 0},
290 {"pc_xusb_falcon", "pllP_xusb", 204000000, 0},
291 {"pc_xusb_core_host", "pllP_xusb", 102000000, 0},
292 {"pc_xusb_core_dev", "pllP_xusb", 102000000, 0},
294 {"sata", "pllP_out0", 104000000, 0},
295 {"sata_oob", "pllP_out0", 204000000, 0},
297 {"mselect", NULL, 0, 1},
298 {"csite", NULL, 0, 1},
300 {"dbgapb", NULL, 0, 1 },
301 {"tsensor", "clk_m", 400000, 0},
302 {"i2c1", "pllP_out0", 0, 0},
303 {"i2c2", "pllP_out0", 0, 0},
304 {"i2c3", "pllP_out0", 0, 0},
305 {"i2c4", "pllP_out0", 0, 0},
306 {"i2c5", "pllP_out0", 0, 0},
307 {"i2c6", "pllP_out0", 0, 0},
309 {"pllDP_out0", NULL, 270000000, 0},
310 {"soc_therm", "pllP_out0", 51000000, 0},
311 {"cclk_g", NULL, 0, 1},
312 {"pllU_out1", NULL, 48000000, 1},
313 {"pllU_out2", NULL, 60000000, 1},
314 {"pllC4", NULL, 1000000000, 1},
315 {"pllC4_out0", NULL, 1000000000, 1},
319 init_divs(struct tegra210_car_softc *sc, struct clk_div_def *clks, int nclks)
323 for (i = 0; i < nclks; i++) {
324 rv = clknode_div_register(sc->clkdom, clks + i);
326 panic("clk_div_register failed");
331 init_gates(struct tegra210_car_softc *sc, struct clk_gate_def *clks, int nclks)
336 for (i = 0; i < nclks; i++) {
337 rv = clknode_gate_register(sc->clkdom, clks + i);
339 panic("clk_gate_register failed");
344 init_muxes(struct tegra210_car_softc *sc, struct clk_mux_def *clks, int nclks)
349 for (i = 0; i < nclks; i++) {
350 rv = clknode_mux_register(sc->clkdom, clks + i);
352 panic("clk_mux_register failed");
357 init_fixeds(struct tegra210_car_softc *sc, struct clk_fixed_def *clks,
364 CLKDEV_READ_4(sc->dev, OSC_CTRL, &val);
365 osc_idx = OSC_CTRL_OSC_FREQ_GET(val);
366 fixed_osc.freq = osc_freqs[osc_idx];
367 if (fixed_osc.freq == 0)
368 panic("Undefined input frequency");
369 rv = clknode_fixed_register(sc->clkdom, &fixed_osc);
371 panic("clk_fixed_register failed");
373 fixed_osc_div.div = 1 << OSC_CTRL_PLL_REF_DIV_GET(val);
374 rv = clknode_fixed_register(sc->clkdom, &fixed_osc_div);
376 panic("clk_fixed_register failed");
378 CLKDEV_READ_4(sc->dev, SPARE_REG0, &val);
379 fixed_clk_m.div = SPARE_REG0_MDIV_GET(val) + 1;
380 rv = clknode_fixed_register(sc->clkdom, &fixed_clk_m);
382 panic("clk_fixed_register failed");
384 for (i = 0; i < nclks; i++) {
385 rv = clknode_fixed_register(sc->clkdom, clks + i);
387 panic("clk_fixed_register failed");
392 postinit_clock(struct tegra210_car_softc *sc)
395 struct tegra210_init_item *tbl;
396 struct clknode *clknode;
399 for (i = 0; i < nitems(clk_init_table); i++) {
400 tbl = &clk_init_table[i];
402 clknode = clknode_find_by_name(tbl->name);
403 if (clknode == NULL) {
404 device_printf(sc->dev, "Cannot find clock %s\n",
408 if (tbl->parent != NULL) {
409 rv = clknode_set_parent_by_name(clknode, tbl->parent);
411 device_printf(sc->dev,
412 "Cannot set parent for %s (to %s): %d\n",
413 tbl->name, tbl->parent, rv);
417 if (tbl->frequency != 0) {
418 rv = clknode_set_freq(clknode, tbl->frequency, 0 , 9999);
420 device_printf(sc->dev,
421 "Cannot set frequency for %s: %d\n",
426 if (tbl->enable!= 0) {
427 rv = clknode_enable(clknode);
429 device_printf(sc->dev,
430 "Cannot enable %s: %d\n", tbl->name, rv);
438 register_clocks(device_t dev)
440 struct tegra210_car_softc *sc;
442 sc = device_get_softc(dev);
443 sc->clkdom = clkdom_create(dev);
444 if (sc->clkdom == NULL)
445 panic("clkdom == NULL");
447 init_fixeds(sc, tegra210_fixed_clks, nitems(tegra210_fixed_clks));
448 tegra210_init_plls(sc);
449 init_muxes(sc, tegra210_mux_clks, nitems(tegra210_mux_clks));
450 init_divs(sc, tegra210_div_clks, nitems(tegra210_div_clks));
451 init_gates(sc, tegra210_gate_clks, nitems(tegra210_gate_clks));
452 tegra210_periph_clock(sc);
453 tegra210_super_mux_clock(sc);
454 clkdom_finit(sc->clkdom);
455 clkdom_xlock(sc->clkdom);
457 clkdom_unlock(sc->clkdom);
459 clkdom_dump(sc->clkdom);
463 tegra210_car_clkdev_read_4(device_t dev, bus_addr_t addr, uint32_t *val)
465 struct tegra210_car_softc *sc;
467 sc = device_get_softc(dev);
468 *val = bus_read_4(sc->mem_res, addr);
473 tegra210_car_clkdev_write_4(device_t dev, bus_addr_t addr, uint32_t val)
475 struct tegra210_car_softc *sc;
477 sc = device_get_softc(dev);
478 bus_write_4(sc->mem_res, addr, val);
483 tegra210_car_clkdev_modify_4(device_t dev, bus_addr_t addr, uint32_t clear_mask,
486 struct tegra210_car_softc *sc;
489 sc = device_get_softc(dev);
490 reg = bus_read_4(sc->mem_res, addr);
493 bus_write_4(sc->mem_res, addr, reg);
498 tegra210_car_clkdev_device_lock(device_t dev)
500 struct tegra210_car_softc *sc;
502 sc = device_get_softc(dev);
507 tegra210_car_clkdev_device_unlock(device_t dev)
509 struct tegra210_car_softc *sc;
511 sc = device_get_softc(dev);
512 mtx_unlock(&sc->mtx);
516 tegra210_car_detach(device_t dev)
519 device_printf(dev, "Error: Clock driver cannot be detached\n");
524 tegra210_car_probe(device_t dev)
527 if (!ofw_bus_status_okay(dev))
530 if (ofw_bus_search_compatible(dev, compat_data)->ocd_data != 0) {
531 device_set_desc(dev, "Tegra Clock Driver");
532 return (BUS_PROBE_DEFAULT);
539 tegra210_car_attach(device_t dev)
541 struct tegra210_car_softc *sc = device_get_softc(dev);
546 mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF);
547 sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
549 /* Resource setup. */
551 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
554 device_printf(dev, "cannot allocate memory resource\n");
559 register_clocks(dev);
560 hwreset_register_ofw_provider(dev);
565 bus_release_resource(dev, SYS_RES_MEMORY, 0, sc->mem_res);
571 tegra210_car_hwreset_assert(device_t dev, intptr_t id, bool value)
573 struct tegra210_car_softc *sc = device_get_softc(dev);
575 return (tegra210_hwreset_by_idx(sc, id, value));
578 static device_method_t tegra210_car_methods[] = {
579 /* Device interface */
580 DEVMETHOD(device_probe, tegra210_car_probe),
581 DEVMETHOD(device_attach, tegra210_car_attach),
582 DEVMETHOD(device_detach, tegra210_car_detach),
584 /* Clkdev interface*/
585 DEVMETHOD(clkdev_read_4, tegra210_car_clkdev_read_4),
586 DEVMETHOD(clkdev_write_4, tegra210_car_clkdev_write_4),
587 DEVMETHOD(clkdev_modify_4, tegra210_car_clkdev_modify_4),
588 DEVMETHOD(clkdev_device_lock, tegra210_car_clkdev_device_lock),
589 DEVMETHOD(clkdev_device_unlock, tegra210_car_clkdev_device_unlock),
591 /* Reset interface */
592 DEVMETHOD(hwreset_assert, tegra210_car_hwreset_assert),
597 static devclass_t tegra210_car_devclass;
598 static DEFINE_CLASS_0(car, tegra210_car_driver, tegra210_car_methods,
599 sizeof(struct tegra210_car_softc));
600 EARLY_DRIVER_MODULE(tegra210_car, simplebus, tegra210_car_driver,
601 tegra210_car_devclass, NULL, NULL, BUS_PASS_TIMER);