2 * Copyright (c) 2013-2014 Ruslan Bukin <br@bsdpad.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
28 * Vybrid Family Clock Controller Module (CCM)
29 * Chapter 10, Vybrid Reference Manual, Rev. 5, 07/2013
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
40 #include <sys/malloc.h>
42 #include <sys/timeet.h>
43 #include <sys/timetc.h>
44 #include <sys/watchdog.h>
46 #include <dev/fdt/fdt_common.h>
47 #include <dev/ofw/openfirm.h>
48 #include <dev/ofw/ofw_bus.h>
49 #include <dev/ofw/ofw_bus_subr.h>
51 #include <machine/bus.h>
52 #include <machine/fdt.h>
53 #include <machine/cpu.h>
54 #include <machine/intr.h>
56 #include <arm/freescale/vybrid/vf_common.h>
58 #define CCM_CCR 0x00 /* Control Register */
59 #define CCM_CSR 0x04 /* Status Register */
60 #define CCM_CCSR 0x08 /* Clock Switcher Register */
61 #define CCM_CACRR 0x0C /* ARM Clock Root Register */
62 #define CCM_CSCMR1 0x10 /* Serial Clock Multiplexer Register 1 */
63 #define CCM_CSCDR1 0x14 /* Serial Clock Divider Register 1 */
64 #define CCM_CSCDR2 0x18 /* Serial Clock Divider Register 2 */
65 #define CCM_CSCDR3 0x1C /* Serial Clock Divider Register 3 */
66 #define CCM_CSCMR2 0x20 /* Serial Clock Multiplexer Register 2 */
67 #define CCM_CTOR 0x28 /* Testing Observability Register */
68 #define CCM_CLPCR 0x2C /* Low Power Control Register */
69 #define CCM_CISR 0x30 /* Interrupt Status Register */
70 #define CCM_CIMR 0x34 /* Interrupt Mask Register */
71 #define CCM_CCOSR 0x38 /* Clock Output Source Register */
72 #define CCM_CGPR 0x3C /* General Purpose Register */
75 #define CCM_CCGR(n) (0x40 + (n * 0x04)) /* Clock Gating Register */
76 #define CCM_CMEOR(n) (0x70 + (n * 0x70)) /* Module Enable Override */
77 #define CCM_CCPGR(n) (0x90 + (n * 0x04)) /* Platform Clock Gating */
79 #define CCM_CPPDSR 0x88 /* PLL PFD Disable Status Register */
80 #define CCM_CCOWR 0x8C /* CORE Wakeup Register */
82 #define PLL3_PFD4_EN (1 << 31)
83 #define PLL3_PFD3_EN (1 << 30)
84 #define PLL3_PFD2_EN (1 << 29)
85 #define PLL3_PFD1_EN (1 << 28)
86 #define PLL2_PFD4_EN (1 << 15)
87 #define PLL2_PFD3_EN (1 << 14)
88 #define PLL2_PFD2_EN (1 << 13)
89 #define PLL2_PFD1_EN (1 << 12)
90 #define PLL1_PFD4_EN (1 << 11)
91 #define PLL1_PFD3_EN (1 << 10)
92 #define PLL1_PFD2_EN (1 << 9)
93 #define PLL1_PFD1_EN (1 << 8)
96 #define FIRC_EN (1 << 16)
97 #define FXOSC_EN (1 << 12)
98 #define FXOSC_RDY (1 << 5)
101 #define ENET_TS_EN (1 << 23)
102 #define RMII_CLK_EN (1 << 24)
103 #define SAI3_EN (1 << 19)
106 #define ESAI_EN (1 << 30)
107 #define ESDHC1_EN (1 << 29)
108 #define ESDHC0_EN (1 << 28)
109 #define NFC_EN (1 << 9)
110 #define ESDHC1_DIV_S 20
111 #define ESDHC1_DIV_M 0xf
112 #define ESDHC0_DIV_S 16
113 #define ESDHC0_DIV_M 0xf
116 #define DCU0_EN (1 << 19)
118 #define QSPI1_EN (1 << 12)
119 #define QSPI1_DIV (1 << 11)
120 #define QSPI1_X2_DIV (1 << 10)
121 #define QSPI1_X4_DIV_M 0x3
122 #define QSPI1_X4_DIV_S 8
124 #define QSPI0_EN (1 << 4)
125 #define QSPI0_DIV (1 << 3)
126 #define QSPI0_X2_DIV (1 << 2)
127 #define QSPI0_X4_DIV_M 0x3
128 #define QSPI0_X4_DIV_S 0
130 #define SAI3_DIV_SHIFT 12
131 #define SAI3_DIV_MASK 0xf
132 #define ESAI_DIV_SHIFT 24
133 #define ESAI_DIV_MASK 0xf
135 #define PLL4_CLK_DIV_SHIFT 6
136 #define PLL4_CLK_DIV_MASK 0x7
138 #define IPG_CLK_DIV_SHIFT 11
139 #define IPG_CLK_DIV_MASK 0x3
141 #define ESAI_CLK_SEL_SHIFT 20
142 #define ESAI_CLK_SEL_MASK 0x3
144 #define SAI3_CLK_SEL_SHIFT 6
145 #define SAI3_CLK_SEL_MASK 0x3
147 #define CKO1_EN (1 << 10)
148 #define CKO1_DIV_MASK 0xf
149 #define CKO1_DIV_SHIFT 6
150 #define CKO1_SEL_MASK 0x3f
151 #define CKO1_SEL_SHIFT 0
152 #define CKO1_PLL4_MAIN 0x6
153 #define CKO1_PLL4_DIVD 0x7
167 static struct clk ipg_clk = {
170 .div_mask = IPG_CLK_DIV_MASK,
171 .div_shift = IPG_CLK_DIV_SHIFT,
172 .div_val = 1, /* Divide by 2 */
180 PLL4 clock divider (before switching the clocks should be gated)
181 000 Divide by 1 (only if PLL frequency less than or equal to 650 MHz)
191 static struct clk pll4_clk = {
194 .div_mask = PLL4_CLK_DIV_MASK,
195 .div_shift = PLL4_CLK_DIV_SHIFT,
196 .div_val = 5, /* Divide by 12 */
203 static struct clk sai3_clk = {
205 .enable_reg = SAI3_EN,
206 .div_mask = SAI3_DIV_MASK,
207 .div_shift = SAI3_DIV_SHIFT,
209 .sel_reg = CCM_CSCMR1,
210 .sel_mask = SAI3_CLK_SEL_MASK,
211 .sel_shift = SAI3_CLK_SEL_SHIFT,
212 .sel_val = 0x3, /* Divided PLL4 main clock */
215 static struct clk cko1_clk = {
217 .enable_reg = CKO1_EN,
218 .div_mask = CKO1_DIV_MASK,
219 .div_shift = CKO1_DIV_SHIFT,
221 .sel_reg = CCM_CCOSR,
222 .sel_mask = CKO1_SEL_MASK,
223 .sel_shift = CKO1_SEL_SHIFT,
224 .sel_val = CKO1_PLL4_DIVD,
227 static struct clk esdhc0_clk = {
229 .enable_reg = ESDHC0_EN,
230 .div_mask = ESDHC0_DIV_M,
231 .div_shift = ESDHC0_DIV_S,
239 static struct clk esdhc1_clk = {
241 .enable_reg = ESDHC1_EN,
242 .div_mask = ESDHC1_DIV_M,
243 .div_shift = ESDHC1_DIV_S,
251 static struct clk qspi0_clk = {
253 .enable_reg = QSPI0_EN,
263 static struct clk dcu0_clk = {
265 .enable_reg = DCU0_EN,
267 .div_shift = 16, /* DCU0_DIV */
268 .div_val = 0, /* divide by 1 */
275 static struct clk enet_clk = {
277 .enable_reg = (ENET_TS_EN | RMII_CLK_EN),
287 static struct clk nand_clk = {
289 .enable_reg = NFC_EN,
300 Divider to generate ESAI clock
307 static struct clk esai_clk = {
309 .enable_reg = ESAI_EN,
310 .div_mask = ESAI_DIV_MASK,
311 .div_shift = ESAI_DIV_SHIFT,
312 .div_val = 3, /* Divide by 4 */
313 .sel_reg = CCM_CSCMR1,
314 .sel_mask = ESAI_CLK_SEL_MASK,
315 .sel_shift = ESAI_CLK_SEL_SHIFT,
316 .sel_val = 0x3, /* Divided PLL4 main clock */
324 static struct clock_entry clock_map[] = {
329 {"esdhc0", &esdhc0_clk},
330 {"esdhc1", &esdhc1_clk},
331 {"qspi0", &qspi0_clk},
340 struct resource *res[1];
342 bus_space_handle_t bsh;
346 static struct resource_spec ccm_spec[] = {
347 { SYS_RES_MEMORY, 0, RF_ACTIVE },
352 ccm_probe(device_t dev)
355 if (!ofw_bus_status_okay(dev))
358 if (!ofw_bus_is_compatible(dev, "fsl,mvf600-ccm"))
361 device_set_desc(dev, "Vybrid Family CCM Unit");
362 return (BUS_PROBE_DEFAULT);
366 set_clock(struct ccm_softc *sc, char *name)
372 for (i = 0; clock_map[i].name != NULL; i++) {
373 if (strcmp(clock_map[i].name, name) == 0) {
375 device_printf(sc->dev, "Configuring %s clk\n", name);
377 clk = clock_map[i].clk;
378 if (clk->sel_reg != 0) {
379 reg = READ4(sc, clk->sel_reg);
380 reg &= ~(clk->sel_mask << clk->sel_shift);
381 reg |= (clk->sel_val << clk->sel_shift);
382 WRITE4(sc, clk->sel_reg, reg);
385 reg = READ4(sc, clk->reg);
386 reg |= clk->enable_reg;
387 reg &= ~(clk->div_mask << clk->div_shift);
388 reg |= (clk->div_val << clk->div_shift);
389 WRITE4(sc, clk->reg, reg);
397 ccm_fdt_set(struct ccm_softc *sc)
399 phandle_t child, parent, root;
401 char *fdt_config, *name;
403 root = OF_finddevice("/");
407 /* Find 'clock_names' prop in the tree */
408 for (child = OF_child(parent); child != 0; child = OF_peer(child)) {
410 /* Find a 'leaf'. Start the search from this node. */
411 while (OF_child(child)) {
413 child = OF_child(child);
416 if (!fdt_is_enabled(child))
419 if ((len = OF_getproplen(child, "clock_names")) > 0) {
420 len = OF_getproplen(child, "clock_names");
421 OF_getprop_alloc(child, "clock_names", 1,
422 (void **)&fdt_config);
426 fdt_config += strlen(name) + 1;
427 len -= strlen(name) + 1;
432 if (OF_peer(child) == 0) {
433 /* No more siblings. */
435 parent = OF_parent(child);
443 ccm_attach(device_t dev)
445 struct ccm_softc *sc;
449 sc = device_get_softc(dev);
452 if (bus_alloc_resources(dev, ccm_spec, sc->res)) {
453 device_printf(dev, "could not allocate resources\n");
457 /* Memory interface */
458 sc->bst = rman_get_bustag(sc->res[0]);
459 sc->bsh = rman_get_bushandle(sc->res[0]);
461 /* Enable oscillator */
462 reg = READ4(sc, CCM_CCR);
463 reg |= (FIRC_EN | FXOSC_EN);
464 WRITE4(sc, CCM_CCR, reg);
467 for (i = 0; i < 10; i++) {
468 if (READ4(sc, CCM_CSR) & FXOSC_RDY) {
469 device_printf(sc->dev, "On board oscillator is ready.\n");
476 /* Clock is on during all modes, except stop mode. */
477 for (i = 0; i < CCM_CCGRN; i++) {
478 WRITE4(sc, CCM_CCGR(i), 0xffffffff);
481 /* Take and apply FDT clocks */
487 static device_method_t ccm_methods[] = {
488 DEVMETHOD(device_probe, ccm_probe),
489 DEVMETHOD(device_attach, ccm_attach),
493 static driver_t ccm_driver = {
496 sizeof(struct ccm_softc),
499 static devclass_t ccm_devclass;
501 DRIVER_MODULE(ccm, simplebus, ccm_driver, ccm_devclass, 0, 0);