1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * R9A06G032 clock driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2018 Renesas Electronics Europe Limited
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Michel Pollet <michel.pollet@bp.renesas.com>, <buserror@gmail.com>
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/clk.h>
11*4882a593Smuzhiyun #include <linux/clk-provider.h>
12*4882a593Smuzhiyun #include <linux/delay.h>
13*4882a593Smuzhiyun #include <linux/init.h>
14*4882a593Smuzhiyun #include <linux/io.h>
15*4882a593Smuzhiyun #include <linux/kernel.h>
16*4882a593Smuzhiyun #include <linux/math64.h>
17*4882a593Smuzhiyun #include <linux/of.h>
18*4882a593Smuzhiyun #include <linux/of_address.h>
19*4882a593Smuzhiyun #include <linux/platform_device.h>
20*4882a593Smuzhiyun #include <linux/pm_clock.h>
21*4882a593Smuzhiyun #include <linux/pm_domain.h>
22*4882a593Smuzhiyun #include <linux/slab.h>
23*4882a593Smuzhiyun #include <linux/spinlock.h>
24*4882a593Smuzhiyun #include <dt-bindings/clock/r9a06g032-sysctrl.h>
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun struct r9a06g032_gate {
27*4882a593Smuzhiyun u16 gate, reset, ready, midle,
28*4882a593Smuzhiyun scon, mirack, mistat;
29*4882a593Smuzhiyun };
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* This is used to describe a clock for instantiation */
32*4882a593Smuzhiyun struct r9a06g032_clkdesc {
33*4882a593Smuzhiyun const char *name;
34*4882a593Smuzhiyun uint32_t managed: 1;
35*4882a593Smuzhiyun uint32_t type: 3;
36*4882a593Smuzhiyun uint32_t index: 8;
37*4882a593Smuzhiyun uint32_t source : 8; /* source index + 1 (0 == none) */
38*4882a593Smuzhiyun /* these are used to populate the bitsel struct */
39*4882a593Smuzhiyun union {
40*4882a593Smuzhiyun struct r9a06g032_gate gate;
41*4882a593Smuzhiyun /* for dividers */
42*4882a593Smuzhiyun struct {
43*4882a593Smuzhiyun unsigned int div_min : 10, div_max : 10, reg: 10;
44*4882a593Smuzhiyun u16 div_table[4];
45*4882a593Smuzhiyun };
46*4882a593Smuzhiyun /* For fixed-factor ones */
47*4882a593Smuzhiyun struct {
48*4882a593Smuzhiyun u16 div, mul;
49*4882a593Smuzhiyun };
50*4882a593Smuzhiyun unsigned int factor;
51*4882a593Smuzhiyun unsigned int frequency;
52*4882a593Smuzhiyun /* for dual gate */
53*4882a593Smuzhiyun struct {
54*4882a593Smuzhiyun uint16_t group : 1, index: 3;
55*4882a593Smuzhiyun u16 sel, g1, r1, g2, r2;
56*4882a593Smuzhiyun } dual;
57*4882a593Smuzhiyun };
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun #define I_GATE(_clk, _rst, _rdy, _midle, _scon, _mirack, _mistat) \
61*4882a593Smuzhiyun { .gate = _clk, .reset = _rst, \
62*4882a593Smuzhiyun .ready = _rdy, .midle = _midle, \
63*4882a593Smuzhiyun .scon = _scon, .mirack = _mirack, .mistat = _mistat }
64*4882a593Smuzhiyun #define D_GATE(_idx, _n, _src, ...) \
65*4882a593Smuzhiyun { .type = K_GATE, .index = R9A06G032_##_idx, \
66*4882a593Smuzhiyun .source = 1 + R9A06G032_##_src, .name = _n, \
67*4882a593Smuzhiyun .gate = I_GATE(__VA_ARGS__) }
68*4882a593Smuzhiyun #define D_MODULE(_idx, _n, _src, ...) \
69*4882a593Smuzhiyun { .type = K_GATE, .index = R9A06G032_##_idx, \
70*4882a593Smuzhiyun .source = 1 + R9A06G032_##_src, .name = _n, \
71*4882a593Smuzhiyun .managed = 1, .gate = I_GATE(__VA_ARGS__) }
72*4882a593Smuzhiyun #define D_ROOT(_idx, _n, _mul, _div) \
73*4882a593Smuzhiyun { .type = K_FFC, .index = R9A06G032_##_idx, .name = _n, \
74*4882a593Smuzhiyun .div = _div, .mul = _mul }
75*4882a593Smuzhiyun #define D_FFC(_idx, _n, _src, _div) \
76*4882a593Smuzhiyun { .type = K_FFC, .index = R9A06G032_##_idx, \
77*4882a593Smuzhiyun .source = 1 + R9A06G032_##_src, .name = _n, \
78*4882a593Smuzhiyun .div = _div, .mul = 1}
79*4882a593Smuzhiyun #define D_DIV(_idx, _n, _src, _reg, _min, _max, ...) \
80*4882a593Smuzhiyun { .type = K_DIV, .index = R9A06G032_##_idx, \
81*4882a593Smuzhiyun .source = 1 + R9A06G032_##_src, .name = _n, \
82*4882a593Smuzhiyun .reg = _reg, .div_min = _min, .div_max = _max, \
83*4882a593Smuzhiyun .div_table = { __VA_ARGS__ } }
84*4882a593Smuzhiyun #define D_UGATE(_idx, _n, _src, _g, _gi, _g1, _r1, _g2, _r2) \
85*4882a593Smuzhiyun { .type = K_DUALGATE, .index = R9A06G032_##_idx, \
86*4882a593Smuzhiyun .source = 1 + R9A06G032_##_src, .name = _n, \
87*4882a593Smuzhiyun .dual = { .group = _g, .index = _gi, \
88*4882a593Smuzhiyun .g1 = _g1, .r1 = _r1, .g2 = _g2, .r2 = _r2 }, }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun enum { K_GATE = 0, K_FFC, K_DIV, K_BITSEL, K_DUALGATE };
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun /* Internal clock IDs */
93*4882a593Smuzhiyun #define R9A06G032_CLKOUT 0
94*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D10 2
95*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D16 3
96*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D160 4
97*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D1OR2 5
98*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D20 6
99*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D40 7
100*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D5 8
101*4882a593Smuzhiyun #define R9A06G032_CLKOUT_D8 9
102*4882a593Smuzhiyun #define R9A06G032_DIV_ADC 10
103*4882a593Smuzhiyun #define R9A06G032_DIV_I2C 11
104*4882a593Smuzhiyun #define R9A06G032_DIV_NAND 12
105*4882a593Smuzhiyun #define R9A06G032_DIV_P1_PG 13
106*4882a593Smuzhiyun #define R9A06G032_DIV_P2_PG 14
107*4882a593Smuzhiyun #define R9A06G032_DIV_P3_PG 15
108*4882a593Smuzhiyun #define R9A06G032_DIV_P4_PG 16
109*4882a593Smuzhiyun #define R9A06G032_DIV_P5_PG 17
110*4882a593Smuzhiyun #define R9A06G032_DIV_P6_PG 18
111*4882a593Smuzhiyun #define R9A06G032_DIV_QSPI0 19
112*4882a593Smuzhiyun #define R9A06G032_DIV_QSPI1 20
113*4882a593Smuzhiyun #define R9A06G032_DIV_REF_SYNC 21
114*4882a593Smuzhiyun #define R9A06G032_DIV_SDIO0 22
115*4882a593Smuzhiyun #define R9A06G032_DIV_SDIO1 23
116*4882a593Smuzhiyun #define R9A06G032_DIV_SWITCH 24
117*4882a593Smuzhiyun #define R9A06G032_DIV_UART 25
118*4882a593Smuzhiyun #define R9A06G032_DIV_MOTOR 64
119*4882a593Smuzhiyun #define R9A06G032_CLK_DDRPHY_PLLCLK_D4 78
120*4882a593Smuzhiyun #define R9A06G032_CLK_ECAT100_D4 79
121*4882a593Smuzhiyun #define R9A06G032_CLK_HSR100_D2 80
122*4882a593Smuzhiyun #define R9A06G032_CLK_REF_SYNC_D4 81
123*4882a593Smuzhiyun #define R9A06G032_CLK_REF_SYNC_D8 82
124*4882a593Smuzhiyun #define R9A06G032_CLK_SERCOS100_D2 83
125*4882a593Smuzhiyun #define R9A06G032_DIV_CA7 84
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun #define R9A06G032_UART_GROUP_012 154
128*4882a593Smuzhiyun #define R9A06G032_UART_GROUP_34567 155
129*4882a593Smuzhiyun
130*4882a593Smuzhiyun #define R9A06G032_CLOCK_COUNT (R9A06G032_UART_GROUP_34567 + 1)
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun static const struct r9a06g032_clkdesc r9a06g032_clocks[] = {
133*4882a593Smuzhiyun D_ROOT(CLKOUT, "clkout", 25, 1),
134*4882a593Smuzhiyun D_ROOT(CLK_PLL_USB, "clk_pll_usb", 12, 10),
135*4882a593Smuzhiyun D_FFC(CLKOUT_D10, "clkout_d10", CLKOUT, 10),
136*4882a593Smuzhiyun D_FFC(CLKOUT_D16, "clkout_d16", CLKOUT, 16),
137*4882a593Smuzhiyun D_FFC(CLKOUT_D160, "clkout_d160", CLKOUT, 160),
138*4882a593Smuzhiyun D_DIV(CLKOUT_D1OR2, "clkout_d1or2", CLKOUT, 0, 1, 2),
139*4882a593Smuzhiyun D_FFC(CLKOUT_D20, "clkout_d20", CLKOUT, 20),
140*4882a593Smuzhiyun D_FFC(CLKOUT_D40, "clkout_d40", CLKOUT, 40),
141*4882a593Smuzhiyun D_FFC(CLKOUT_D5, "clkout_d5", CLKOUT, 5),
142*4882a593Smuzhiyun D_FFC(CLKOUT_D8, "clkout_d8", CLKOUT, 8),
143*4882a593Smuzhiyun D_DIV(DIV_ADC, "div_adc", CLKOUT, 77, 50, 250),
144*4882a593Smuzhiyun D_DIV(DIV_I2C, "div_i2c", CLKOUT, 78, 12, 16),
145*4882a593Smuzhiyun D_DIV(DIV_NAND, "div_nand", CLKOUT, 82, 12, 32),
146*4882a593Smuzhiyun D_DIV(DIV_P1_PG, "div_p1_pg", CLKOUT, 68, 12, 200),
147*4882a593Smuzhiyun D_DIV(DIV_P2_PG, "div_p2_pg", CLKOUT, 62, 12, 128),
148*4882a593Smuzhiyun D_DIV(DIV_P3_PG, "div_p3_pg", CLKOUT, 64, 8, 128),
149*4882a593Smuzhiyun D_DIV(DIV_P4_PG, "div_p4_pg", CLKOUT, 66, 8, 128),
150*4882a593Smuzhiyun D_DIV(DIV_P5_PG, "div_p5_pg", CLKOUT, 71, 10, 40),
151*4882a593Smuzhiyun D_DIV(DIV_P6_PG, "div_p6_pg", CLKOUT, 18, 12, 64),
152*4882a593Smuzhiyun D_DIV(DIV_QSPI0, "div_qspi0", CLKOUT, 73, 3, 7),
153*4882a593Smuzhiyun D_DIV(DIV_QSPI1, "div_qspi1", CLKOUT, 25, 3, 7),
154*4882a593Smuzhiyun D_DIV(DIV_REF_SYNC, "div_ref_sync", CLKOUT, 56, 2, 16, 2, 4, 8, 16),
155*4882a593Smuzhiyun D_DIV(DIV_SDIO0, "div_sdio0", CLKOUT, 74, 20, 128),
156*4882a593Smuzhiyun D_DIV(DIV_SDIO1, "div_sdio1", CLKOUT, 75, 20, 128),
157*4882a593Smuzhiyun D_DIV(DIV_SWITCH, "div_switch", CLKOUT, 37, 5, 40),
158*4882a593Smuzhiyun D_DIV(DIV_UART, "div_uart", CLKOUT, 79, 12, 128),
159*4882a593Smuzhiyun D_GATE(CLK_25_PG4, "clk_25_pg4", CLKOUT_D40, 0x749, 0x74a, 0x74b, 0, 0xae3, 0, 0),
160*4882a593Smuzhiyun D_GATE(CLK_25_PG5, "clk_25_pg5", CLKOUT_D40, 0x74c, 0x74d, 0x74e, 0, 0xae4, 0, 0),
161*4882a593Smuzhiyun D_GATE(CLK_25_PG6, "clk_25_pg6", CLKOUT_D40, 0x74f, 0x750, 0x751, 0, 0xae5, 0, 0),
162*4882a593Smuzhiyun D_GATE(CLK_25_PG7, "clk_25_pg7", CLKOUT_D40, 0x752, 0x753, 0x754, 0, 0xae6, 0, 0),
163*4882a593Smuzhiyun D_GATE(CLK_25_PG8, "clk_25_pg8", CLKOUT_D40, 0x755, 0x756, 0x757, 0, 0xae7, 0, 0),
164*4882a593Smuzhiyun D_GATE(CLK_ADC, "clk_adc", DIV_ADC, 0x1ea, 0x1eb, 0, 0, 0, 0, 0),
165*4882a593Smuzhiyun D_GATE(CLK_ECAT100, "clk_ecat100", CLKOUT_D10, 0x405, 0, 0, 0, 0, 0, 0),
166*4882a593Smuzhiyun D_GATE(CLK_HSR100, "clk_hsr100", CLKOUT_D10, 0x483, 0, 0, 0, 0, 0, 0),
167*4882a593Smuzhiyun D_GATE(CLK_I2C0, "clk_i2c0", DIV_I2C, 0x1e6, 0x1e7, 0, 0, 0, 0, 0),
168*4882a593Smuzhiyun D_GATE(CLK_I2C1, "clk_i2c1", DIV_I2C, 0x1e8, 0x1e9, 0, 0, 0, 0, 0),
169*4882a593Smuzhiyun D_GATE(CLK_MII_REF, "clk_mii_ref", CLKOUT_D40, 0x342, 0, 0, 0, 0, 0, 0),
170*4882a593Smuzhiyun D_GATE(CLK_NAND, "clk_nand", DIV_NAND, 0x284, 0x285, 0, 0, 0, 0, 0),
171*4882a593Smuzhiyun D_GATE(CLK_NOUSBP2_PG6, "clk_nousbp2_pg6", DIV_P2_PG, 0x774, 0x775, 0, 0, 0, 0, 0),
172*4882a593Smuzhiyun D_GATE(CLK_P1_PG2, "clk_p1_pg2", DIV_P1_PG, 0x862, 0x863, 0, 0, 0, 0, 0),
173*4882a593Smuzhiyun D_GATE(CLK_P1_PG3, "clk_p1_pg3", DIV_P1_PG, 0x864, 0x865, 0, 0, 0, 0, 0),
174*4882a593Smuzhiyun D_GATE(CLK_P1_PG4, "clk_p1_pg4", DIV_P1_PG, 0x866, 0x867, 0, 0, 0, 0, 0),
175*4882a593Smuzhiyun D_GATE(CLK_P4_PG3, "clk_p4_pg3", DIV_P4_PG, 0x824, 0x825, 0, 0, 0, 0, 0),
176*4882a593Smuzhiyun D_GATE(CLK_P4_PG4, "clk_p4_pg4", DIV_P4_PG, 0x826, 0x827, 0, 0, 0, 0, 0),
177*4882a593Smuzhiyun D_GATE(CLK_P6_PG1, "clk_p6_pg1", DIV_P6_PG, 0x8a0, 0x8a1, 0x8a2, 0, 0xb60, 0, 0),
178*4882a593Smuzhiyun D_GATE(CLK_P6_PG2, "clk_p6_pg2", DIV_P6_PG, 0x8a3, 0x8a4, 0x8a5, 0, 0xb61, 0, 0),
179*4882a593Smuzhiyun D_GATE(CLK_P6_PG3, "clk_p6_pg3", DIV_P6_PG, 0x8a6, 0x8a7, 0x8a8, 0, 0xb62, 0, 0),
180*4882a593Smuzhiyun D_GATE(CLK_P6_PG4, "clk_p6_pg4", DIV_P6_PG, 0x8a9, 0x8aa, 0x8ab, 0, 0xb63, 0, 0),
181*4882a593Smuzhiyun D_MODULE(CLK_PCI_USB, "clk_pci_usb", CLKOUT_D40, 0xe6, 0, 0, 0, 0, 0, 0),
182*4882a593Smuzhiyun D_GATE(CLK_QSPI0, "clk_qspi0", DIV_QSPI0, 0x2a4, 0x2a5, 0, 0, 0, 0, 0),
183*4882a593Smuzhiyun D_GATE(CLK_QSPI1, "clk_qspi1", DIV_QSPI1, 0x484, 0x485, 0, 0, 0, 0, 0),
184*4882a593Smuzhiyun D_GATE(CLK_RGMII_REF, "clk_rgmii_ref", CLKOUT_D8, 0x340, 0, 0, 0, 0, 0, 0),
185*4882a593Smuzhiyun D_GATE(CLK_RMII_REF, "clk_rmii_ref", CLKOUT_D20, 0x341, 0, 0, 0, 0, 0, 0),
186*4882a593Smuzhiyun D_GATE(CLK_SDIO0, "clk_sdio0", DIV_SDIO0, 0x64, 0, 0, 0, 0, 0, 0),
187*4882a593Smuzhiyun D_GATE(CLK_SDIO1, "clk_sdio1", DIV_SDIO1, 0x644, 0, 0, 0, 0, 0, 0),
188*4882a593Smuzhiyun D_GATE(CLK_SERCOS100, "clk_sercos100", CLKOUT_D10, 0x425, 0, 0, 0, 0, 0, 0),
189*4882a593Smuzhiyun D_GATE(CLK_SLCD, "clk_slcd", DIV_P1_PG, 0x860, 0x861, 0, 0, 0, 0, 0),
190*4882a593Smuzhiyun D_GATE(CLK_SPI0, "clk_spi0", DIV_P3_PG, 0x7e0, 0x7e1, 0, 0, 0, 0, 0),
191*4882a593Smuzhiyun D_GATE(CLK_SPI1, "clk_spi1", DIV_P3_PG, 0x7e2, 0x7e3, 0, 0, 0, 0, 0),
192*4882a593Smuzhiyun D_GATE(CLK_SPI2, "clk_spi2", DIV_P3_PG, 0x7e4, 0x7e5, 0, 0, 0, 0, 0),
193*4882a593Smuzhiyun D_GATE(CLK_SPI3, "clk_spi3", DIV_P3_PG, 0x7e6, 0x7e7, 0, 0, 0, 0, 0),
194*4882a593Smuzhiyun D_GATE(CLK_SPI4, "clk_spi4", DIV_P4_PG, 0x820, 0x821, 0, 0, 0, 0, 0),
195*4882a593Smuzhiyun D_GATE(CLK_SPI5, "clk_spi5", DIV_P4_PG, 0x822, 0x823, 0, 0, 0, 0, 0),
196*4882a593Smuzhiyun D_GATE(CLK_SWITCH, "clk_switch", DIV_SWITCH, 0x982, 0x983, 0, 0, 0, 0, 0),
197*4882a593Smuzhiyun D_DIV(DIV_MOTOR, "div_motor", CLKOUT_D5, 84, 2, 8),
198*4882a593Smuzhiyun D_MODULE(HCLK_ECAT125, "hclk_ecat125", CLKOUT_D8, 0x400, 0x401, 0, 0x402, 0, 0x440, 0x441),
199*4882a593Smuzhiyun D_MODULE(HCLK_PINCONFIG, "hclk_pinconfig", CLKOUT_D40, 0x740, 0x741, 0x742, 0, 0xae0, 0, 0),
200*4882a593Smuzhiyun D_MODULE(HCLK_SERCOS, "hclk_sercos", CLKOUT_D10, 0x420, 0x422, 0, 0x421, 0, 0x460, 0x461),
201*4882a593Smuzhiyun D_MODULE(HCLK_SGPIO2, "hclk_sgpio2", DIV_P5_PG, 0x8c3, 0x8c4, 0x8c5, 0, 0xb41, 0, 0),
202*4882a593Smuzhiyun D_MODULE(HCLK_SGPIO3, "hclk_sgpio3", DIV_P5_PG, 0x8c6, 0x8c7, 0x8c8, 0, 0xb42, 0, 0),
203*4882a593Smuzhiyun D_MODULE(HCLK_SGPIO4, "hclk_sgpio4", DIV_P5_PG, 0x8c9, 0x8ca, 0x8cb, 0, 0xb43, 0, 0),
204*4882a593Smuzhiyun D_MODULE(HCLK_TIMER0, "hclk_timer0", CLKOUT_D40, 0x743, 0x744, 0x745, 0, 0xae1, 0, 0),
205*4882a593Smuzhiyun D_MODULE(HCLK_TIMER1, "hclk_timer1", CLKOUT_D40, 0x746, 0x747, 0x748, 0, 0xae2, 0, 0),
206*4882a593Smuzhiyun D_MODULE(HCLK_USBF, "hclk_usbf", CLKOUT_D8, 0xe3, 0, 0, 0xe4, 0, 0x102, 0x103),
207*4882a593Smuzhiyun D_MODULE(HCLK_USBH, "hclk_usbh", CLKOUT_D8, 0xe0, 0xe1, 0, 0xe2, 0, 0x100, 0x101),
208*4882a593Smuzhiyun D_MODULE(HCLK_USBPM, "hclk_usbpm", CLKOUT_D8, 0xe5, 0, 0, 0, 0, 0, 0),
209*4882a593Smuzhiyun D_GATE(CLK_48_PG_F, "clk_48_pg_f", CLK_48, 0x78c, 0x78d, 0, 0x78e, 0, 0xb04, 0xb05),
210*4882a593Smuzhiyun D_GATE(CLK_48_PG4, "clk_48_pg4", CLK_48, 0x789, 0x78a, 0x78b, 0, 0xb03, 0, 0),
211*4882a593Smuzhiyun D_FFC(CLK_DDRPHY_PLLCLK_D4, "clk_ddrphy_pllclk_d4", CLK_DDRPHY_PLLCLK, 4),
212*4882a593Smuzhiyun D_FFC(CLK_ECAT100_D4, "clk_ecat100_d4", CLK_ECAT100, 4),
213*4882a593Smuzhiyun D_FFC(CLK_HSR100_D2, "clk_hsr100_d2", CLK_HSR100, 2),
214*4882a593Smuzhiyun D_FFC(CLK_REF_SYNC_D4, "clk_ref_sync_d4", CLK_REF_SYNC, 4),
215*4882a593Smuzhiyun D_FFC(CLK_REF_SYNC_D8, "clk_ref_sync_d8", CLK_REF_SYNC, 8),
216*4882a593Smuzhiyun D_FFC(CLK_SERCOS100_D2, "clk_sercos100_d2", CLK_SERCOS100, 2),
217*4882a593Smuzhiyun D_DIV(DIV_CA7, "div_ca7", CLK_REF_SYNC, 57, 1, 4, 1, 2, 4),
218*4882a593Smuzhiyun D_MODULE(HCLK_CAN0, "hclk_can0", CLK_48, 0x783, 0x784, 0x785, 0, 0xb01, 0, 0),
219*4882a593Smuzhiyun D_MODULE(HCLK_CAN1, "hclk_can1", CLK_48, 0x786, 0x787, 0x788, 0, 0xb02, 0, 0),
220*4882a593Smuzhiyun D_MODULE(HCLK_DELTASIGMA, "hclk_deltasigma", DIV_MOTOR, 0x1ef, 0x1f0, 0x1f1, 0, 0, 0, 0),
221*4882a593Smuzhiyun D_MODULE(HCLK_PWMPTO, "hclk_pwmpto", DIV_MOTOR, 0x1ec, 0x1ed, 0x1ee, 0, 0, 0, 0),
222*4882a593Smuzhiyun D_MODULE(HCLK_RSV, "hclk_rsv", CLK_48, 0x780, 0x781, 0x782, 0, 0xb00, 0, 0),
223*4882a593Smuzhiyun D_MODULE(HCLK_SGPIO0, "hclk_sgpio0", DIV_MOTOR, 0x1e0, 0x1e1, 0x1e2, 0, 0, 0, 0),
224*4882a593Smuzhiyun D_MODULE(HCLK_SGPIO1, "hclk_sgpio1", DIV_MOTOR, 0x1e3, 0x1e4, 0x1e5, 0, 0, 0, 0),
225*4882a593Smuzhiyun D_DIV(RTOS_MDC, "rtos_mdc", CLK_REF_SYNC, 100, 80, 640, 80, 160, 320, 640),
226*4882a593Smuzhiyun D_GATE(CLK_CM3, "clk_cm3", CLK_REF_SYNC_D4, 0xba0, 0xba1, 0, 0xba2, 0, 0xbc0, 0xbc1),
227*4882a593Smuzhiyun D_GATE(CLK_DDRC, "clk_ddrc", CLK_DDRPHY_PLLCLK_D4, 0x323, 0x324, 0, 0, 0, 0, 0),
228*4882a593Smuzhiyun D_GATE(CLK_ECAT25, "clk_ecat25", CLK_ECAT100_D4, 0x403, 0x404, 0, 0, 0, 0, 0),
229*4882a593Smuzhiyun D_GATE(CLK_HSR50, "clk_hsr50", CLK_HSR100_D2, 0x484, 0x485, 0, 0, 0, 0, 0),
230*4882a593Smuzhiyun D_GATE(CLK_HW_RTOS, "clk_hw_rtos", CLK_REF_SYNC_D4, 0xc60, 0xc61, 0, 0, 0, 0, 0),
231*4882a593Smuzhiyun D_GATE(CLK_SERCOS50, "clk_sercos50", CLK_SERCOS100_D2, 0x424, 0x423, 0, 0, 0, 0, 0),
232*4882a593Smuzhiyun D_MODULE(HCLK_ADC, "hclk_adc", CLK_REF_SYNC_D8, 0x1af, 0x1b0, 0x1b1, 0, 0, 0, 0),
233*4882a593Smuzhiyun D_MODULE(HCLK_CM3, "hclk_cm3", CLK_REF_SYNC_D4, 0xc20, 0xc21, 0xc22, 0, 0, 0, 0),
234*4882a593Smuzhiyun D_MODULE(HCLK_CRYPTO_EIP150, "hclk_crypto_eip150", CLK_REF_SYNC_D4, 0x123, 0x124, 0x125, 0, 0x142, 0, 0),
235*4882a593Smuzhiyun D_MODULE(HCLK_CRYPTO_EIP93, "hclk_crypto_eip93", CLK_REF_SYNC_D4, 0x120, 0x121, 0, 0x122, 0, 0x140, 0x141),
236*4882a593Smuzhiyun D_MODULE(HCLK_DDRC, "hclk_ddrc", CLK_REF_SYNC_D4, 0x320, 0x322, 0, 0x321, 0, 0x3a0, 0x3a1),
237*4882a593Smuzhiyun D_MODULE(HCLK_DMA0, "hclk_dma0", CLK_REF_SYNC_D4, 0x260, 0x261, 0x262, 0x263, 0x2c0, 0x2c1, 0x2c2),
238*4882a593Smuzhiyun D_MODULE(HCLK_DMA1, "hclk_dma1", CLK_REF_SYNC_D4, 0x264, 0x265, 0x266, 0x267, 0x2c3, 0x2c4, 0x2c5),
239*4882a593Smuzhiyun D_MODULE(HCLK_GMAC0, "hclk_gmac0", CLK_REF_SYNC_D4, 0x360, 0x361, 0x362, 0x363, 0x3c0, 0x3c1, 0x3c2),
240*4882a593Smuzhiyun D_MODULE(HCLK_GMAC1, "hclk_gmac1", CLK_REF_SYNC_D4, 0x380, 0x381, 0x382, 0x383, 0x3e0, 0x3e1, 0x3e2),
241*4882a593Smuzhiyun D_MODULE(HCLK_GPIO0, "hclk_gpio0", CLK_REF_SYNC_D4, 0x212, 0x213, 0x214, 0, 0, 0, 0),
242*4882a593Smuzhiyun D_MODULE(HCLK_GPIO1, "hclk_gpio1", CLK_REF_SYNC_D4, 0x215, 0x216, 0x217, 0, 0, 0, 0),
243*4882a593Smuzhiyun D_MODULE(HCLK_GPIO2, "hclk_gpio2", CLK_REF_SYNC_D4, 0x229, 0x22a, 0x22b, 0, 0, 0, 0),
244*4882a593Smuzhiyun D_MODULE(HCLK_HSR, "hclk_hsr", CLK_HSR100_D2, 0x480, 0x482, 0, 0x481, 0, 0x4c0, 0x4c1),
245*4882a593Smuzhiyun D_MODULE(HCLK_I2C0, "hclk_i2c0", CLK_REF_SYNC_D8, 0x1a9, 0x1aa, 0x1ab, 0, 0, 0, 0),
246*4882a593Smuzhiyun D_MODULE(HCLK_I2C1, "hclk_i2c1", CLK_REF_SYNC_D8, 0x1ac, 0x1ad, 0x1ae, 0, 0, 0, 0),
247*4882a593Smuzhiyun D_MODULE(HCLK_LCD, "hclk_lcd", CLK_REF_SYNC_D4, 0x7a0, 0x7a1, 0x7a2, 0, 0xb20, 0, 0),
248*4882a593Smuzhiyun D_MODULE(HCLK_MSEBI_M, "hclk_msebi_m", CLK_REF_SYNC_D4, 0x164, 0x165, 0x166, 0, 0x183, 0, 0),
249*4882a593Smuzhiyun D_MODULE(HCLK_MSEBI_S, "hclk_msebi_s", CLK_REF_SYNC_D4, 0x160, 0x161, 0x162, 0x163, 0x180, 0x181, 0x182),
250*4882a593Smuzhiyun D_MODULE(HCLK_NAND, "hclk_nand", CLK_REF_SYNC_D4, 0x280, 0x281, 0x282, 0x283, 0x2e0, 0x2e1, 0x2e2),
251*4882a593Smuzhiyun D_MODULE(HCLK_PG_I, "hclk_pg_i", CLK_REF_SYNC_D4, 0x7ac, 0x7ad, 0, 0x7ae, 0, 0xb24, 0xb25),
252*4882a593Smuzhiyun D_MODULE(HCLK_PG19, "hclk_pg19", CLK_REF_SYNC_D4, 0x22c, 0x22d, 0x22e, 0, 0, 0, 0),
253*4882a593Smuzhiyun D_MODULE(HCLK_PG20, "hclk_pg20", CLK_REF_SYNC_D4, 0x22f, 0x230, 0x231, 0, 0, 0, 0),
254*4882a593Smuzhiyun D_MODULE(HCLK_PG3, "hclk_pg3", CLK_REF_SYNC_D4, 0x7a6, 0x7a7, 0x7a8, 0, 0xb22, 0, 0),
255*4882a593Smuzhiyun D_MODULE(HCLK_PG4, "hclk_pg4", CLK_REF_SYNC_D4, 0x7a9, 0x7aa, 0x7ab, 0, 0xb23, 0, 0),
256*4882a593Smuzhiyun D_MODULE(HCLK_QSPI0, "hclk_qspi0", CLK_REF_SYNC_D4, 0x2a0, 0x2a1, 0x2a2, 0x2a3, 0x300, 0x301, 0x302),
257*4882a593Smuzhiyun D_MODULE(HCLK_QSPI1, "hclk_qspi1", CLK_REF_SYNC_D4, 0x480, 0x481, 0x482, 0x483, 0x4c0, 0x4c1, 0x4c2),
258*4882a593Smuzhiyun D_MODULE(HCLK_ROM, "hclk_rom", CLK_REF_SYNC_D4, 0xaa0, 0xaa1, 0xaa2, 0, 0xb80, 0, 0),
259*4882a593Smuzhiyun D_MODULE(HCLK_RTC, "hclk_rtc", CLK_REF_SYNC_D8, 0xa00, 0, 0, 0, 0, 0, 0),
260*4882a593Smuzhiyun D_MODULE(HCLK_SDIO0, "hclk_sdio0", CLK_REF_SYNC_D4, 0x60, 0x61, 0x62, 0x63, 0x80, 0x81, 0x82),
261*4882a593Smuzhiyun D_MODULE(HCLK_SDIO1, "hclk_sdio1", CLK_REF_SYNC_D4, 0x640, 0x641, 0x642, 0x643, 0x660, 0x661, 0x662),
262*4882a593Smuzhiyun D_MODULE(HCLK_SEMAP, "hclk_semap", CLK_REF_SYNC_D4, 0x7a3, 0x7a4, 0x7a5, 0, 0xb21, 0, 0),
263*4882a593Smuzhiyun D_MODULE(HCLK_SPI0, "hclk_spi0", CLK_REF_SYNC_D4, 0x200, 0x201, 0x202, 0, 0, 0, 0),
264*4882a593Smuzhiyun D_MODULE(HCLK_SPI1, "hclk_spi1", CLK_REF_SYNC_D4, 0x203, 0x204, 0x205, 0, 0, 0, 0),
265*4882a593Smuzhiyun D_MODULE(HCLK_SPI2, "hclk_spi2", CLK_REF_SYNC_D4, 0x206, 0x207, 0x208, 0, 0, 0, 0),
266*4882a593Smuzhiyun D_MODULE(HCLK_SPI3, "hclk_spi3", CLK_REF_SYNC_D4, 0x209, 0x20a, 0x20b, 0, 0, 0, 0),
267*4882a593Smuzhiyun D_MODULE(HCLK_SPI4, "hclk_spi4", CLK_REF_SYNC_D4, 0x20c, 0x20d, 0x20e, 0, 0, 0, 0),
268*4882a593Smuzhiyun D_MODULE(HCLK_SPI5, "hclk_spi5", CLK_REF_SYNC_D4, 0x20f, 0x210, 0x211, 0, 0, 0, 0),
269*4882a593Smuzhiyun D_MODULE(HCLK_SWITCH, "hclk_switch", CLK_REF_SYNC_D4, 0x980, 0, 0x981, 0, 0, 0, 0),
270*4882a593Smuzhiyun D_MODULE(HCLK_SWITCH_RG, "hclk_switch_rg", CLK_REF_SYNC_D4, 0xc40, 0xc41, 0xc42, 0, 0, 0, 0),
271*4882a593Smuzhiyun D_MODULE(HCLK_UART0, "hclk_uart0", CLK_REF_SYNC_D8, 0x1a0, 0x1a1, 0x1a2, 0, 0, 0, 0),
272*4882a593Smuzhiyun D_MODULE(HCLK_UART1, "hclk_uart1", CLK_REF_SYNC_D8, 0x1a3, 0x1a4, 0x1a5, 0, 0, 0, 0),
273*4882a593Smuzhiyun D_MODULE(HCLK_UART2, "hclk_uart2", CLK_REF_SYNC_D8, 0x1a6, 0x1a7, 0x1a8, 0, 0, 0, 0),
274*4882a593Smuzhiyun D_MODULE(HCLK_UART3, "hclk_uart3", CLK_REF_SYNC_D4, 0x218, 0x219, 0x21a, 0, 0, 0, 0),
275*4882a593Smuzhiyun D_MODULE(HCLK_UART4, "hclk_uart4", CLK_REF_SYNC_D4, 0x21b, 0x21c, 0x21d, 0, 0, 0, 0),
276*4882a593Smuzhiyun D_MODULE(HCLK_UART5, "hclk_uart5", CLK_REF_SYNC_D4, 0x220, 0x221, 0x222, 0, 0, 0, 0),
277*4882a593Smuzhiyun D_MODULE(HCLK_UART6, "hclk_uart6", CLK_REF_SYNC_D4, 0x223, 0x224, 0x225, 0, 0, 0, 0),
278*4882a593Smuzhiyun D_MODULE(HCLK_UART7, "hclk_uart7", CLK_REF_SYNC_D4, 0x226, 0x227, 0x228, 0, 0, 0, 0),
279*4882a593Smuzhiyun /*
280*4882a593Smuzhiyun * These are not hardware clocks, but are needed to handle the special
281*4882a593Smuzhiyun * case where we have a 'selector bit' that doesn't just change the
282*4882a593Smuzhiyun * parent for a clock, but also the gate it's suposed to use.
283*4882a593Smuzhiyun */
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun .index = R9A06G032_UART_GROUP_012,
286*4882a593Smuzhiyun .name = "uart_group_012",
287*4882a593Smuzhiyun .type = K_BITSEL,
288*4882a593Smuzhiyun .source = 1 + R9A06G032_DIV_UART,
289*4882a593Smuzhiyun /* R9A06G032_SYSCTRL_REG_PWRCTRL_PG0_0 */
290*4882a593Smuzhiyun .dual.sel = ((0x34 / 4) << 5) | 30,
291*4882a593Smuzhiyun .dual.group = 0,
292*4882a593Smuzhiyun },
293*4882a593Smuzhiyun {
294*4882a593Smuzhiyun .index = R9A06G032_UART_GROUP_34567,
295*4882a593Smuzhiyun .name = "uart_group_34567",
296*4882a593Smuzhiyun .type = K_BITSEL,
297*4882a593Smuzhiyun .source = 1 + R9A06G032_DIV_P2_PG,
298*4882a593Smuzhiyun /* R9A06G032_SYSCTRL_REG_PWRCTRL_PG1_PR2 */
299*4882a593Smuzhiyun .dual.sel = ((0xec / 4) << 5) | 24,
300*4882a593Smuzhiyun .dual.group = 1,
301*4882a593Smuzhiyun },
302*4882a593Smuzhiyun D_UGATE(CLK_UART0, "clk_uart0", UART_GROUP_012, 0, 0, 0x1b2, 0x1b3, 0x1b4, 0x1b5),
303*4882a593Smuzhiyun D_UGATE(CLK_UART1, "clk_uart1", UART_GROUP_012, 0, 1, 0x1b6, 0x1b7, 0x1b8, 0x1b9),
304*4882a593Smuzhiyun D_UGATE(CLK_UART2, "clk_uart2", UART_GROUP_012, 0, 2, 0x1ba, 0x1bb, 0x1bc, 0x1bd),
305*4882a593Smuzhiyun D_UGATE(CLK_UART3, "clk_uart3", UART_GROUP_34567, 1, 0, 0x760, 0x761, 0x762, 0x763),
306*4882a593Smuzhiyun D_UGATE(CLK_UART4, "clk_uart4", UART_GROUP_34567, 1, 1, 0x764, 0x765, 0x766, 0x767),
307*4882a593Smuzhiyun D_UGATE(CLK_UART5, "clk_uart5", UART_GROUP_34567, 1, 2, 0x768, 0x769, 0x76a, 0x76b),
308*4882a593Smuzhiyun D_UGATE(CLK_UART6, "clk_uart6", UART_GROUP_34567, 1, 3, 0x76c, 0x76d, 0x76e, 0x76f),
309*4882a593Smuzhiyun D_UGATE(CLK_UART7, "clk_uart7", UART_GROUP_34567, 1, 4, 0x770, 0x771, 0x772, 0x773),
310*4882a593Smuzhiyun };
311*4882a593Smuzhiyun
312*4882a593Smuzhiyun struct r9a06g032_priv {
313*4882a593Smuzhiyun struct clk_onecell_data data;
314*4882a593Smuzhiyun spinlock_t lock; /* protects concurent access to gates */
315*4882a593Smuzhiyun void __iomem *reg;
316*4882a593Smuzhiyun };
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun /* register/bit pairs are encoded as an uint16_t */
319*4882a593Smuzhiyun static void
clk_rdesc_set(struct r9a06g032_priv * clocks,u16 one,unsigned int on)320*4882a593Smuzhiyun clk_rdesc_set(struct r9a06g032_priv *clocks,
321*4882a593Smuzhiyun u16 one, unsigned int on)
322*4882a593Smuzhiyun {
323*4882a593Smuzhiyun u32 __iomem *reg = clocks->reg + (4 * (one >> 5));
324*4882a593Smuzhiyun u32 val = readl(reg);
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun val = (val & ~(1U << (one & 0x1f))) | ((!!on) << (one & 0x1f));
327*4882a593Smuzhiyun writel(val, reg);
328*4882a593Smuzhiyun }
329*4882a593Smuzhiyun
330*4882a593Smuzhiyun static int
clk_rdesc_get(struct r9a06g032_priv * clocks,uint16_t one)331*4882a593Smuzhiyun clk_rdesc_get(struct r9a06g032_priv *clocks,
332*4882a593Smuzhiyun uint16_t one)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun u32 __iomem *reg = clocks->reg + (4 * (one >> 5));
335*4882a593Smuzhiyun u32 val = readl(reg);
336*4882a593Smuzhiyun
337*4882a593Smuzhiyun return !!(val & (1U << (one & 0x1f)));
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun
340*4882a593Smuzhiyun /*
341*4882a593Smuzhiyun * This implements the R9A06G032 clock gate 'driver'. We cannot use the system's
342*4882a593Smuzhiyun * clock gate framework as the gates on the R9A06G032 have a special enabling
343*4882a593Smuzhiyun * sequence, therefore we use this little proxy.
344*4882a593Smuzhiyun */
345*4882a593Smuzhiyun struct r9a06g032_clk_gate {
346*4882a593Smuzhiyun struct clk_hw hw;
347*4882a593Smuzhiyun struct r9a06g032_priv *clocks;
348*4882a593Smuzhiyun u16 index;
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun struct r9a06g032_gate gate;
351*4882a593Smuzhiyun };
352*4882a593Smuzhiyun
353*4882a593Smuzhiyun #define to_r9a06g032_gate(_hw) container_of(_hw, struct r9a06g032_clk_gate, hw)
354*4882a593Smuzhiyun
create_add_module_clock(struct of_phandle_args * clkspec,struct device * dev)355*4882a593Smuzhiyun static int create_add_module_clock(struct of_phandle_args *clkspec,
356*4882a593Smuzhiyun struct device *dev)
357*4882a593Smuzhiyun {
358*4882a593Smuzhiyun struct clk *clk;
359*4882a593Smuzhiyun int error;
360*4882a593Smuzhiyun
361*4882a593Smuzhiyun clk = of_clk_get_from_provider(clkspec);
362*4882a593Smuzhiyun if (IS_ERR(clk))
363*4882a593Smuzhiyun return PTR_ERR(clk);
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun error = pm_clk_create(dev);
366*4882a593Smuzhiyun if (error) {
367*4882a593Smuzhiyun clk_put(clk);
368*4882a593Smuzhiyun return error;
369*4882a593Smuzhiyun }
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun error = pm_clk_add_clk(dev, clk);
372*4882a593Smuzhiyun if (error) {
373*4882a593Smuzhiyun pm_clk_destroy(dev);
374*4882a593Smuzhiyun clk_put(clk);
375*4882a593Smuzhiyun }
376*4882a593Smuzhiyun
377*4882a593Smuzhiyun return error;
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
r9a06g032_attach_dev(struct generic_pm_domain * pd,struct device * dev)380*4882a593Smuzhiyun static int r9a06g032_attach_dev(struct generic_pm_domain *pd,
381*4882a593Smuzhiyun struct device *dev)
382*4882a593Smuzhiyun {
383*4882a593Smuzhiyun struct device_node *np = dev->of_node;
384*4882a593Smuzhiyun struct of_phandle_args clkspec;
385*4882a593Smuzhiyun int i = 0;
386*4882a593Smuzhiyun int error;
387*4882a593Smuzhiyun int index;
388*4882a593Smuzhiyun
389*4882a593Smuzhiyun while (!of_parse_phandle_with_args(np, "clocks", "#clock-cells", i,
390*4882a593Smuzhiyun &clkspec)) {
391*4882a593Smuzhiyun if (clkspec.np != pd->dev.of_node)
392*4882a593Smuzhiyun continue;
393*4882a593Smuzhiyun
394*4882a593Smuzhiyun index = clkspec.args[0];
395*4882a593Smuzhiyun if (index < R9A06G032_CLOCK_COUNT &&
396*4882a593Smuzhiyun r9a06g032_clocks[index].managed) {
397*4882a593Smuzhiyun error = create_add_module_clock(&clkspec, dev);
398*4882a593Smuzhiyun of_node_put(clkspec.np);
399*4882a593Smuzhiyun if (error)
400*4882a593Smuzhiyun return error;
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun i++;
403*4882a593Smuzhiyun }
404*4882a593Smuzhiyun
405*4882a593Smuzhiyun return 0;
406*4882a593Smuzhiyun }
407*4882a593Smuzhiyun
r9a06g032_detach_dev(struct generic_pm_domain * unused,struct device * dev)408*4882a593Smuzhiyun static void r9a06g032_detach_dev(struct generic_pm_domain *unused, struct device *dev)
409*4882a593Smuzhiyun {
410*4882a593Smuzhiyun if (!pm_clk_no_clocks(dev))
411*4882a593Smuzhiyun pm_clk_destroy(dev);
412*4882a593Smuzhiyun }
413*4882a593Smuzhiyun
r9a06g032_add_clk_domain(struct device * dev)414*4882a593Smuzhiyun static int r9a06g032_add_clk_domain(struct device *dev)
415*4882a593Smuzhiyun {
416*4882a593Smuzhiyun struct device_node *np = dev->of_node;
417*4882a593Smuzhiyun struct generic_pm_domain *pd;
418*4882a593Smuzhiyun
419*4882a593Smuzhiyun pd = devm_kzalloc(dev, sizeof(*pd), GFP_KERNEL);
420*4882a593Smuzhiyun if (!pd)
421*4882a593Smuzhiyun return -ENOMEM;
422*4882a593Smuzhiyun
423*4882a593Smuzhiyun pd->name = np->name;
424*4882a593Smuzhiyun pd->flags = GENPD_FLAG_PM_CLK | GENPD_FLAG_ALWAYS_ON |
425*4882a593Smuzhiyun GENPD_FLAG_ACTIVE_WAKEUP;
426*4882a593Smuzhiyun pd->attach_dev = r9a06g032_attach_dev;
427*4882a593Smuzhiyun pd->detach_dev = r9a06g032_detach_dev;
428*4882a593Smuzhiyun pm_genpd_init(pd, &pm_domain_always_on_gov, false);
429*4882a593Smuzhiyun
430*4882a593Smuzhiyun of_genpd_add_provider_simple(np, pd);
431*4882a593Smuzhiyun return 0;
432*4882a593Smuzhiyun }
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun static void
r9a06g032_clk_gate_set(struct r9a06g032_priv * clocks,struct r9a06g032_gate * g,int on)435*4882a593Smuzhiyun r9a06g032_clk_gate_set(struct r9a06g032_priv *clocks,
436*4882a593Smuzhiyun struct r9a06g032_gate *g, int on)
437*4882a593Smuzhiyun {
438*4882a593Smuzhiyun unsigned long flags;
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun WARN_ON(!g->gate);
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun spin_lock_irqsave(&clocks->lock, flags);
443*4882a593Smuzhiyun clk_rdesc_set(clocks, g->gate, on);
444*4882a593Smuzhiyun /* De-assert reset */
445*4882a593Smuzhiyun if (g->reset)
446*4882a593Smuzhiyun clk_rdesc_set(clocks, g->reset, 1);
447*4882a593Smuzhiyun spin_unlock_irqrestore(&clocks->lock, flags);
448*4882a593Smuzhiyun
449*4882a593Smuzhiyun /* Hardware manual recommends 5us delay after enabling clock & reset */
450*4882a593Smuzhiyun udelay(5);
451*4882a593Smuzhiyun
452*4882a593Smuzhiyun /* If the peripheral is memory mapped (i.e. an AXI slave), there is an
453*4882a593Smuzhiyun * associated SLVRDY bit in the System Controller that needs to be set
454*4882a593Smuzhiyun * so that the FlexWAY bus fabric passes on the read/write requests.
455*4882a593Smuzhiyun */
456*4882a593Smuzhiyun if (g->ready || g->midle) {
457*4882a593Smuzhiyun spin_lock_irqsave(&clocks->lock, flags);
458*4882a593Smuzhiyun if (g->ready)
459*4882a593Smuzhiyun clk_rdesc_set(clocks, g->ready, on);
460*4882a593Smuzhiyun /* Clear 'Master Idle Request' bit */
461*4882a593Smuzhiyun if (g->midle)
462*4882a593Smuzhiyun clk_rdesc_set(clocks, g->midle, !on);
463*4882a593Smuzhiyun spin_unlock_irqrestore(&clocks->lock, flags);
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun /* Note: We don't wait for FlexWAY Socket Connection signal */
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
r9a06g032_clk_gate_enable(struct clk_hw * hw)468*4882a593Smuzhiyun static int r9a06g032_clk_gate_enable(struct clk_hw *hw)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun struct r9a06g032_clk_gate *g = to_r9a06g032_gate(hw);
471*4882a593Smuzhiyun
472*4882a593Smuzhiyun r9a06g032_clk_gate_set(g->clocks, &g->gate, 1);
473*4882a593Smuzhiyun return 0;
474*4882a593Smuzhiyun }
475*4882a593Smuzhiyun
r9a06g032_clk_gate_disable(struct clk_hw * hw)476*4882a593Smuzhiyun static void r9a06g032_clk_gate_disable(struct clk_hw *hw)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun struct r9a06g032_clk_gate *g = to_r9a06g032_gate(hw);
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun r9a06g032_clk_gate_set(g->clocks, &g->gate, 0);
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
r9a06g032_clk_gate_is_enabled(struct clk_hw * hw)483*4882a593Smuzhiyun static int r9a06g032_clk_gate_is_enabled(struct clk_hw *hw)
484*4882a593Smuzhiyun {
485*4882a593Smuzhiyun struct r9a06g032_clk_gate *g = to_r9a06g032_gate(hw);
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun /* if clock is in reset, the gate might be on, and still not 'be' on */
488*4882a593Smuzhiyun if (g->gate.reset && !clk_rdesc_get(g->clocks, g->gate.reset))
489*4882a593Smuzhiyun return 0;
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun return clk_rdesc_get(g->clocks, g->gate.gate);
492*4882a593Smuzhiyun }
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun static const struct clk_ops r9a06g032_clk_gate_ops = {
495*4882a593Smuzhiyun .enable = r9a06g032_clk_gate_enable,
496*4882a593Smuzhiyun .disable = r9a06g032_clk_gate_disable,
497*4882a593Smuzhiyun .is_enabled = r9a06g032_clk_gate_is_enabled,
498*4882a593Smuzhiyun };
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun static struct clk *
r9a06g032_register_gate(struct r9a06g032_priv * clocks,const char * parent_name,const struct r9a06g032_clkdesc * desc)501*4882a593Smuzhiyun r9a06g032_register_gate(struct r9a06g032_priv *clocks,
502*4882a593Smuzhiyun const char *parent_name,
503*4882a593Smuzhiyun const struct r9a06g032_clkdesc *desc)
504*4882a593Smuzhiyun {
505*4882a593Smuzhiyun struct clk *clk;
506*4882a593Smuzhiyun struct r9a06g032_clk_gate *g;
507*4882a593Smuzhiyun struct clk_init_data init;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun g = kzalloc(sizeof(*g), GFP_KERNEL);
510*4882a593Smuzhiyun if (!g)
511*4882a593Smuzhiyun return NULL;
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun init.name = desc->name;
514*4882a593Smuzhiyun init.ops = &r9a06g032_clk_gate_ops;
515*4882a593Smuzhiyun init.flags = CLK_SET_RATE_PARENT;
516*4882a593Smuzhiyun init.parent_names = parent_name ? &parent_name : NULL;
517*4882a593Smuzhiyun init.num_parents = parent_name ? 1 : 0;
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun g->clocks = clocks;
520*4882a593Smuzhiyun g->index = desc->index;
521*4882a593Smuzhiyun g->gate = desc->gate;
522*4882a593Smuzhiyun g->hw.init = &init;
523*4882a593Smuzhiyun
524*4882a593Smuzhiyun /*
525*4882a593Smuzhiyun * important here, some clocks are already in use by the CM3, we
526*4882a593Smuzhiyun * have to assume they are not Linux's to play with and try to disable
527*4882a593Smuzhiyun * at the end of the boot!
528*4882a593Smuzhiyun */
529*4882a593Smuzhiyun if (r9a06g032_clk_gate_is_enabled(&g->hw)) {
530*4882a593Smuzhiyun init.flags |= CLK_IS_CRITICAL;
531*4882a593Smuzhiyun pr_debug("%s was enabled, making read-only\n", desc->name);
532*4882a593Smuzhiyun }
533*4882a593Smuzhiyun
534*4882a593Smuzhiyun clk = clk_register(NULL, &g->hw);
535*4882a593Smuzhiyun if (IS_ERR(clk)) {
536*4882a593Smuzhiyun kfree(g);
537*4882a593Smuzhiyun return NULL;
538*4882a593Smuzhiyun }
539*4882a593Smuzhiyun return clk;
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun struct r9a06g032_clk_div {
543*4882a593Smuzhiyun struct clk_hw hw;
544*4882a593Smuzhiyun struct r9a06g032_priv *clocks;
545*4882a593Smuzhiyun u16 index;
546*4882a593Smuzhiyun u16 reg;
547*4882a593Smuzhiyun u16 min, max;
548*4882a593Smuzhiyun u8 table_size;
549*4882a593Smuzhiyun u16 table[8]; /* we know there are no more than 8 */
550*4882a593Smuzhiyun };
551*4882a593Smuzhiyun
552*4882a593Smuzhiyun #define to_r9a06g032_div(_hw) \
553*4882a593Smuzhiyun container_of(_hw, struct r9a06g032_clk_div, hw)
554*4882a593Smuzhiyun
555*4882a593Smuzhiyun static unsigned long
r9a06g032_div_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)556*4882a593Smuzhiyun r9a06g032_div_recalc_rate(struct clk_hw *hw,
557*4882a593Smuzhiyun unsigned long parent_rate)
558*4882a593Smuzhiyun {
559*4882a593Smuzhiyun struct r9a06g032_clk_div *clk = to_r9a06g032_div(hw);
560*4882a593Smuzhiyun u32 __iomem *reg = clk->clocks->reg + (4 * clk->reg);
561*4882a593Smuzhiyun u32 div = readl(reg);
562*4882a593Smuzhiyun
563*4882a593Smuzhiyun if (div < clk->min)
564*4882a593Smuzhiyun div = clk->min;
565*4882a593Smuzhiyun else if (div > clk->max)
566*4882a593Smuzhiyun div = clk->max;
567*4882a593Smuzhiyun return DIV_ROUND_UP(parent_rate, div);
568*4882a593Smuzhiyun }
569*4882a593Smuzhiyun
570*4882a593Smuzhiyun /*
571*4882a593Smuzhiyun * Attempts to find a value that is in range of min,max,
572*4882a593Smuzhiyun * and if a table of set dividers was specified for this
573*4882a593Smuzhiyun * register, try to find the fixed divider that is the closest
574*4882a593Smuzhiyun * to the target frequency
575*4882a593Smuzhiyun */
576*4882a593Smuzhiyun static long
r9a06g032_div_clamp_div(struct r9a06g032_clk_div * clk,unsigned long rate,unsigned long prate)577*4882a593Smuzhiyun r9a06g032_div_clamp_div(struct r9a06g032_clk_div *clk,
578*4882a593Smuzhiyun unsigned long rate, unsigned long prate)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun /* + 1 to cope with rates that have the remainder dropped */
581*4882a593Smuzhiyun u32 div = DIV_ROUND_UP(prate, rate + 1);
582*4882a593Smuzhiyun int i;
583*4882a593Smuzhiyun
584*4882a593Smuzhiyun if (div <= clk->min)
585*4882a593Smuzhiyun return clk->min;
586*4882a593Smuzhiyun if (div >= clk->max)
587*4882a593Smuzhiyun return clk->max;
588*4882a593Smuzhiyun
589*4882a593Smuzhiyun for (i = 0; clk->table_size && i < clk->table_size - 1; i++) {
590*4882a593Smuzhiyun if (div >= clk->table[i] && div <= clk->table[i + 1]) {
591*4882a593Smuzhiyun unsigned long m = rate -
592*4882a593Smuzhiyun DIV_ROUND_UP(prate, clk->table[i]);
593*4882a593Smuzhiyun unsigned long p =
594*4882a593Smuzhiyun DIV_ROUND_UP(prate, clk->table[i + 1]) -
595*4882a593Smuzhiyun rate;
596*4882a593Smuzhiyun /*
597*4882a593Smuzhiyun * select the divider that generates
598*4882a593Smuzhiyun * the value closest to the ideal frequency
599*4882a593Smuzhiyun */
600*4882a593Smuzhiyun div = p >= m ? clk->table[i] : clk->table[i + 1];
601*4882a593Smuzhiyun return div;
602*4882a593Smuzhiyun }
603*4882a593Smuzhiyun }
604*4882a593Smuzhiyun return div;
605*4882a593Smuzhiyun }
606*4882a593Smuzhiyun
607*4882a593Smuzhiyun static long
r9a06g032_div_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)608*4882a593Smuzhiyun r9a06g032_div_round_rate(struct clk_hw *hw,
609*4882a593Smuzhiyun unsigned long rate, unsigned long *prate)
610*4882a593Smuzhiyun {
611*4882a593Smuzhiyun struct r9a06g032_clk_div *clk = to_r9a06g032_div(hw);
612*4882a593Smuzhiyun u32 div = DIV_ROUND_UP(*prate, rate);
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun pr_devel("%s %pC %ld (prate %ld) (wanted div %u)\n", __func__,
615*4882a593Smuzhiyun hw->clk, rate, *prate, div);
616*4882a593Smuzhiyun pr_devel(" min %d (%ld) max %d (%ld)\n",
617*4882a593Smuzhiyun clk->min, DIV_ROUND_UP(*prate, clk->min),
618*4882a593Smuzhiyun clk->max, DIV_ROUND_UP(*prate, clk->max));
619*4882a593Smuzhiyun
620*4882a593Smuzhiyun div = r9a06g032_div_clamp_div(clk, rate, *prate);
621*4882a593Smuzhiyun /*
622*4882a593Smuzhiyun * this is a hack. Currently the serial driver asks for a clock rate
623*4882a593Smuzhiyun * that is 16 times the baud rate -- and that is wildly outside the
624*4882a593Smuzhiyun * range of the UART divider, somehow there is no provision for that
625*4882a593Smuzhiyun * case of 'let the divider as is if outside range'.
626*4882a593Smuzhiyun * The serial driver *shouldn't* play with these clocks anyway, there's
627*4882a593Smuzhiyun * several uarts attached to this divider, and changing this impacts
628*4882a593Smuzhiyun * everyone.
629*4882a593Smuzhiyun */
630*4882a593Smuzhiyun if (clk->index == R9A06G032_DIV_UART ||
631*4882a593Smuzhiyun clk->index == R9A06G032_DIV_P2_PG) {
632*4882a593Smuzhiyun pr_devel("%s div uart hack!\n", __func__);
633*4882a593Smuzhiyun return clk_get_rate(hw->clk);
634*4882a593Smuzhiyun }
635*4882a593Smuzhiyun pr_devel("%s %pC %ld / %u = %ld\n", __func__, hw->clk,
636*4882a593Smuzhiyun *prate, div, DIV_ROUND_UP(*prate, div));
637*4882a593Smuzhiyun return DIV_ROUND_UP(*prate, div);
638*4882a593Smuzhiyun }
639*4882a593Smuzhiyun
640*4882a593Smuzhiyun static int
r9a06g032_div_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)641*4882a593Smuzhiyun r9a06g032_div_set_rate(struct clk_hw *hw,
642*4882a593Smuzhiyun unsigned long rate, unsigned long parent_rate)
643*4882a593Smuzhiyun {
644*4882a593Smuzhiyun struct r9a06g032_clk_div *clk = to_r9a06g032_div(hw);
645*4882a593Smuzhiyun /* + 1 to cope with rates that have the remainder dropped */
646*4882a593Smuzhiyun u32 div = DIV_ROUND_UP(parent_rate, rate + 1);
647*4882a593Smuzhiyun u32 __iomem *reg = clk->clocks->reg + (4 * clk->reg);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun pr_devel("%s %pC rate %ld parent %ld div %d\n", __func__, hw->clk,
650*4882a593Smuzhiyun rate, parent_rate, div);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun /*
653*4882a593Smuzhiyun * Need to write the bit 31 with the divider value to
654*4882a593Smuzhiyun * latch it. Technically we should wait until it has been
655*4882a593Smuzhiyun * cleared too.
656*4882a593Smuzhiyun * TODO: Find whether this callback is sleepable, in case
657*4882a593Smuzhiyun * the hardware /does/ require some sort of spinloop here.
658*4882a593Smuzhiyun */
659*4882a593Smuzhiyun writel(div | BIT(31), reg);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun return 0;
662*4882a593Smuzhiyun }
663*4882a593Smuzhiyun
664*4882a593Smuzhiyun static const struct clk_ops r9a06g032_clk_div_ops = {
665*4882a593Smuzhiyun .recalc_rate = r9a06g032_div_recalc_rate,
666*4882a593Smuzhiyun .round_rate = r9a06g032_div_round_rate,
667*4882a593Smuzhiyun .set_rate = r9a06g032_div_set_rate,
668*4882a593Smuzhiyun };
669*4882a593Smuzhiyun
670*4882a593Smuzhiyun static struct clk *
r9a06g032_register_div(struct r9a06g032_priv * clocks,const char * parent_name,const struct r9a06g032_clkdesc * desc)671*4882a593Smuzhiyun r9a06g032_register_div(struct r9a06g032_priv *clocks,
672*4882a593Smuzhiyun const char *parent_name,
673*4882a593Smuzhiyun const struct r9a06g032_clkdesc *desc)
674*4882a593Smuzhiyun {
675*4882a593Smuzhiyun struct r9a06g032_clk_div *div;
676*4882a593Smuzhiyun struct clk *clk;
677*4882a593Smuzhiyun struct clk_init_data init;
678*4882a593Smuzhiyun unsigned int i;
679*4882a593Smuzhiyun
680*4882a593Smuzhiyun div = kzalloc(sizeof(*div), GFP_KERNEL);
681*4882a593Smuzhiyun if (!div)
682*4882a593Smuzhiyun return NULL;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun init.name = desc->name;
685*4882a593Smuzhiyun init.ops = &r9a06g032_clk_div_ops;
686*4882a593Smuzhiyun init.flags = CLK_SET_RATE_PARENT;
687*4882a593Smuzhiyun init.parent_names = parent_name ? &parent_name : NULL;
688*4882a593Smuzhiyun init.num_parents = parent_name ? 1 : 0;
689*4882a593Smuzhiyun
690*4882a593Smuzhiyun div->clocks = clocks;
691*4882a593Smuzhiyun div->index = desc->index;
692*4882a593Smuzhiyun div->reg = desc->reg;
693*4882a593Smuzhiyun div->hw.init = &init;
694*4882a593Smuzhiyun div->min = desc->div_min;
695*4882a593Smuzhiyun div->max = desc->div_max;
696*4882a593Smuzhiyun /* populate (optional) divider table fixed values */
697*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(div->table) &&
698*4882a593Smuzhiyun i < ARRAY_SIZE(desc->div_table) && desc->div_table[i]; i++) {
699*4882a593Smuzhiyun div->table[div->table_size++] = desc->div_table[i];
700*4882a593Smuzhiyun }
701*4882a593Smuzhiyun
702*4882a593Smuzhiyun clk = clk_register(NULL, &div->hw);
703*4882a593Smuzhiyun if (IS_ERR(clk)) {
704*4882a593Smuzhiyun kfree(div);
705*4882a593Smuzhiyun return NULL;
706*4882a593Smuzhiyun }
707*4882a593Smuzhiyun return clk;
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun /*
711*4882a593Smuzhiyun * This clock provider handles the case of the R9A06G032 where you have
712*4882a593Smuzhiyun * peripherals that have two potential clock source and two gates, one for
713*4882a593Smuzhiyun * each of the clock source - the used clock source (for all sub clocks)
714*4882a593Smuzhiyun * is selected by a single bit.
715*4882a593Smuzhiyun * That single bit affects all sub-clocks, and therefore needs to change the
716*4882a593Smuzhiyun * active gate (and turn the others off) and force a recalculation of the rates.
717*4882a593Smuzhiyun *
718*4882a593Smuzhiyun * This implements two clock providers, one 'bitselect' that
719*4882a593Smuzhiyun * handles the switch between both parents, and another 'dualgate'
720*4882a593Smuzhiyun * that knows which gate to poke at, depending on the parent's bit position.
721*4882a593Smuzhiyun */
722*4882a593Smuzhiyun struct r9a06g032_clk_bitsel {
723*4882a593Smuzhiyun struct clk_hw hw;
724*4882a593Smuzhiyun struct r9a06g032_priv *clocks;
725*4882a593Smuzhiyun u16 index;
726*4882a593Smuzhiyun u16 selector; /* selector register + bit */
727*4882a593Smuzhiyun };
728*4882a593Smuzhiyun
729*4882a593Smuzhiyun #define to_clk_bitselect(_hw) \
730*4882a593Smuzhiyun container_of(_hw, struct r9a06g032_clk_bitsel, hw)
731*4882a593Smuzhiyun
r9a06g032_clk_mux_get_parent(struct clk_hw * hw)732*4882a593Smuzhiyun static u8 r9a06g032_clk_mux_get_parent(struct clk_hw *hw)
733*4882a593Smuzhiyun {
734*4882a593Smuzhiyun struct r9a06g032_clk_bitsel *set = to_clk_bitselect(hw);
735*4882a593Smuzhiyun
736*4882a593Smuzhiyun return clk_rdesc_get(set->clocks, set->selector);
737*4882a593Smuzhiyun }
738*4882a593Smuzhiyun
r9a06g032_clk_mux_set_parent(struct clk_hw * hw,u8 index)739*4882a593Smuzhiyun static int r9a06g032_clk_mux_set_parent(struct clk_hw *hw, u8 index)
740*4882a593Smuzhiyun {
741*4882a593Smuzhiyun struct r9a06g032_clk_bitsel *set = to_clk_bitselect(hw);
742*4882a593Smuzhiyun
743*4882a593Smuzhiyun /* a single bit in the register selects one of two parent clocks */
744*4882a593Smuzhiyun clk_rdesc_set(set->clocks, set->selector, !!index);
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun return 0;
747*4882a593Smuzhiyun }
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun static const struct clk_ops clk_bitselect_ops = {
750*4882a593Smuzhiyun .get_parent = r9a06g032_clk_mux_get_parent,
751*4882a593Smuzhiyun .set_parent = r9a06g032_clk_mux_set_parent,
752*4882a593Smuzhiyun };
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun static struct clk *
r9a06g032_register_bitsel(struct r9a06g032_priv * clocks,const char * parent_name,const struct r9a06g032_clkdesc * desc)755*4882a593Smuzhiyun r9a06g032_register_bitsel(struct r9a06g032_priv *clocks,
756*4882a593Smuzhiyun const char *parent_name,
757*4882a593Smuzhiyun const struct r9a06g032_clkdesc *desc)
758*4882a593Smuzhiyun {
759*4882a593Smuzhiyun struct clk *clk;
760*4882a593Smuzhiyun struct r9a06g032_clk_bitsel *g;
761*4882a593Smuzhiyun struct clk_init_data init;
762*4882a593Smuzhiyun const char *names[2];
763*4882a593Smuzhiyun
764*4882a593Smuzhiyun /* allocate the gate */
765*4882a593Smuzhiyun g = kzalloc(sizeof(*g), GFP_KERNEL);
766*4882a593Smuzhiyun if (!g)
767*4882a593Smuzhiyun return NULL;
768*4882a593Smuzhiyun
769*4882a593Smuzhiyun names[0] = parent_name;
770*4882a593Smuzhiyun names[1] = "clk_pll_usb";
771*4882a593Smuzhiyun
772*4882a593Smuzhiyun init.name = desc->name;
773*4882a593Smuzhiyun init.ops = &clk_bitselect_ops;
774*4882a593Smuzhiyun init.flags = CLK_SET_RATE_PARENT;
775*4882a593Smuzhiyun init.parent_names = names;
776*4882a593Smuzhiyun init.num_parents = 2;
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun g->clocks = clocks;
779*4882a593Smuzhiyun g->index = desc->index;
780*4882a593Smuzhiyun g->selector = desc->dual.sel;
781*4882a593Smuzhiyun g->hw.init = &init;
782*4882a593Smuzhiyun
783*4882a593Smuzhiyun clk = clk_register(NULL, &g->hw);
784*4882a593Smuzhiyun if (IS_ERR(clk)) {
785*4882a593Smuzhiyun kfree(g);
786*4882a593Smuzhiyun return NULL;
787*4882a593Smuzhiyun }
788*4882a593Smuzhiyun return clk;
789*4882a593Smuzhiyun }
790*4882a593Smuzhiyun
791*4882a593Smuzhiyun struct r9a06g032_clk_dualgate {
792*4882a593Smuzhiyun struct clk_hw hw;
793*4882a593Smuzhiyun struct r9a06g032_priv *clocks;
794*4882a593Smuzhiyun u16 index;
795*4882a593Smuzhiyun u16 selector; /* selector register + bit */
796*4882a593Smuzhiyun struct r9a06g032_gate gate[2];
797*4882a593Smuzhiyun };
798*4882a593Smuzhiyun
799*4882a593Smuzhiyun #define to_clk_dualgate(_hw) \
800*4882a593Smuzhiyun container_of(_hw, struct r9a06g032_clk_dualgate, hw)
801*4882a593Smuzhiyun
802*4882a593Smuzhiyun static int
r9a06g032_clk_dualgate_setenable(struct r9a06g032_clk_dualgate * g,int enable)803*4882a593Smuzhiyun r9a06g032_clk_dualgate_setenable(struct r9a06g032_clk_dualgate *g, int enable)
804*4882a593Smuzhiyun {
805*4882a593Smuzhiyun u8 sel_bit = clk_rdesc_get(g->clocks, g->selector);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun /* we always turn off the 'other' gate, regardless */
808*4882a593Smuzhiyun r9a06g032_clk_gate_set(g->clocks, &g->gate[!sel_bit], 0);
809*4882a593Smuzhiyun r9a06g032_clk_gate_set(g->clocks, &g->gate[sel_bit], enable);
810*4882a593Smuzhiyun
811*4882a593Smuzhiyun return 0;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
r9a06g032_clk_dualgate_enable(struct clk_hw * hw)814*4882a593Smuzhiyun static int r9a06g032_clk_dualgate_enable(struct clk_hw *hw)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun struct r9a06g032_clk_dualgate *gate = to_clk_dualgate(hw);
817*4882a593Smuzhiyun
818*4882a593Smuzhiyun r9a06g032_clk_dualgate_setenable(gate, 1);
819*4882a593Smuzhiyun
820*4882a593Smuzhiyun return 0;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun
r9a06g032_clk_dualgate_disable(struct clk_hw * hw)823*4882a593Smuzhiyun static void r9a06g032_clk_dualgate_disable(struct clk_hw *hw)
824*4882a593Smuzhiyun {
825*4882a593Smuzhiyun struct r9a06g032_clk_dualgate *gate = to_clk_dualgate(hw);
826*4882a593Smuzhiyun
827*4882a593Smuzhiyun r9a06g032_clk_dualgate_setenable(gate, 0);
828*4882a593Smuzhiyun }
829*4882a593Smuzhiyun
r9a06g032_clk_dualgate_is_enabled(struct clk_hw * hw)830*4882a593Smuzhiyun static int r9a06g032_clk_dualgate_is_enabled(struct clk_hw *hw)
831*4882a593Smuzhiyun {
832*4882a593Smuzhiyun struct r9a06g032_clk_dualgate *g = to_clk_dualgate(hw);
833*4882a593Smuzhiyun u8 sel_bit = clk_rdesc_get(g->clocks, g->selector);
834*4882a593Smuzhiyun
835*4882a593Smuzhiyun return clk_rdesc_get(g->clocks, g->gate[sel_bit].gate);
836*4882a593Smuzhiyun }
837*4882a593Smuzhiyun
838*4882a593Smuzhiyun static const struct clk_ops r9a06g032_clk_dualgate_ops = {
839*4882a593Smuzhiyun .enable = r9a06g032_clk_dualgate_enable,
840*4882a593Smuzhiyun .disable = r9a06g032_clk_dualgate_disable,
841*4882a593Smuzhiyun .is_enabled = r9a06g032_clk_dualgate_is_enabled,
842*4882a593Smuzhiyun };
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun static struct clk *
r9a06g032_register_dualgate(struct r9a06g032_priv * clocks,const char * parent_name,const struct r9a06g032_clkdesc * desc,uint16_t sel)845*4882a593Smuzhiyun r9a06g032_register_dualgate(struct r9a06g032_priv *clocks,
846*4882a593Smuzhiyun const char *parent_name,
847*4882a593Smuzhiyun const struct r9a06g032_clkdesc *desc,
848*4882a593Smuzhiyun uint16_t sel)
849*4882a593Smuzhiyun {
850*4882a593Smuzhiyun struct r9a06g032_clk_dualgate *g;
851*4882a593Smuzhiyun struct clk *clk;
852*4882a593Smuzhiyun struct clk_init_data init;
853*4882a593Smuzhiyun
854*4882a593Smuzhiyun /* allocate the gate */
855*4882a593Smuzhiyun g = kzalloc(sizeof(*g), GFP_KERNEL);
856*4882a593Smuzhiyun if (!g)
857*4882a593Smuzhiyun return NULL;
858*4882a593Smuzhiyun g->clocks = clocks;
859*4882a593Smuzhiyun g->index = desc->index;
860*4882a593Smuzhiyun g->selector = sel;
861*4882a593Smuzhiyun g->gate[0].gate = desc->dual.g1;
862*4882a593Smuzhiyun g->gate[0].reset = desc->dual.r1;
863*4882a593Smuzhiyun g->gate[1].gate = desc->dual.g2;
864*4882a593Smuzhiyun g->gate[1].reset = desc->dual.r2;
865*4882a593Smuzhiyun
866*4882a593Smuzhiyun init.name = desc->name;
867*4882a593Smuzhiyun init.ops = &r9a06g032_clk_dualgate_ops;
868*4882a593Smuzhiyun init.flags = CLK_SET_RATE_PARENT;
869*4882a593Smuzhiyun init.parent_names = &parent_name;
870*4882a593Smuzhiyun init.num_parents = 1;
871*4882a593Smuzhiyun g->hw.init = &init;
872*4882a593Smuzhiyun /*
873*4882a593Smuzhiyun * important here, some clocks are already in use by the CM3, we
874*4882a593Smuzhiyun * have to assume they are not Linux's to play with and try to disable
875*4882a593Smuzhiyun * at the end of the boot!
876*4882a593Smuzhiyun */
877*4882a593Smuzhiyun if (r9a06g032_clk_dualgate_is_enabled(&g->hw)) {
878*4882a593Smuzhiyun init.flags |= CLK_IS_CRITICAL;
879*4882a593Smuzhiyun pr_debug("%s was enabled, making read-only\n", desc->name);
880*4882a593Smuzhiyun }
881*4882a593Smuzhiyun
882*4882a593Smuzhiyun clk = clk_register(NULL, &g->hw);
883*4882a593Smuzhiyun if (IS_ERR(clk)) {
884*4882a593Smuzhiyun kfree(g);
885*4882a593Smuzhiyun return NULL;
886*4882a593Smuzhiyun }
887*4882a593Smuzhiyun return clk;
888*4882a593Smuzhiyun }
889*4882a593Smuzhiyun
r9a06g032_clocks_del_clk_provider(void * data)890*4882a593Smuzhiyun static void r9a06g032_clocks_del_clk_provider(void *data)
891*4882a593Smuzhiyun {
892*4882a593Smuzhiyun of_clk_del_provider(data);
893*4882a593Smuzhiyun }
894*4882a593Smuzhiyun
r9a06g032_clocks_probe(struct platform_device * pdev)895*4882a593Smuzhiyun static int __init r9a06g032_clocks_probe(struct platform_device *pdev)
896*4882a593Smuzhiyun {
897*4882a593Smuzhiyun struct device *dev = &pdev->dev;
898*4882a593Smuzhiyun struct device_node *np = dev->of_node;
899*4882a593Smuzhiyun struct r9a06g032_priv *clocks;
900*4882a593Smuzhiyun struct clk **clks;
901*4882a593Smuzhiyun struct clk *mclk;
902*4882a593Smuzhiyun unsigned int i;
903*4882a593Smuzhiyun u16 uart_group_sel[2];
904*4882a593Smuzhiyun int error;
905*4882a593Smuzhiyun
906*4882a593Smuzhiyun clocks = devm_kzalloc(dev, sizeof(*clocks), GFP_KERNEL);
907*4882a593Smuzhiyun clks = devm_kcalloc(dev, R9A06G032_CLOCK_COUNT, sizeof(struct clk *),
908*4882a593Smuzhiyun GFP_KERNEL);
909*4882a593Smuzhiyun if (!clocks || !clks)
910*4882a593Smuzhiyun return -ENOMEM;
911*4882a593Smuzhiyun
912*4882a593Smuzhiyun spin_lock_init(&clocks->lock);
913*4882a593Smuzhiyun
914*4882a593Smuzhiyun clocks->data.clks = clks;
915*4882a593Smuzhiyun clocks->data.clk_num = R9A06G032_CLOCK_COUNT;
916*4882a593Smuzhiyun
917*4882a593Smuzhiyun mclk = devm_clk_get(dev, "mclk");
918*4882a593Smuzhiyun if (IS_ERR(mclk))
919*4882a593Smuzhiyun return PTR_ERR(mclk);
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun clocks->reg = of_iomap(np, 0);
922*4882a593Smuzhiyun if (WARN_ON(!clocks->reg))
923*4882a593Smuzhiyun return -ENOMEM;
924*4882a593Smuzhiyun for (i = 0; i < ARRAY_SIZE(r9a06g032_clocks); ++i) {
925*4882a593Smuzhiyun const struct r9a06g032_clkdesc *d = &r9a06g032_clocks[i];
926*4882a593Smuzhiyun const char *parent_name = d->source ?
927*4882a593Smuzhiyun __clk_get_name(clocks->data.clks[d->source - 1]) :
928*4882a593Smuzhiyun __clk_get_name(mclk);
929*4882a593Smuzhiyun struct clk *clk = NULL;
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun switch (d->type) {
932*4882a593Smuzhiyun case K_FFC:
933*4882a593Smuzhiyun clk = clk_register_fixed_factor(NULL, d->name,
934*4882a593Smuzhiyun parent_name, 0,
935*4882a593Smuzhiyun d->mul, d->div);
936*4882a593Smuzhiyun break;
937*4882a593Smuzhiyun case K_GATE:
938*4882a593Smuzhiyun clk = r9a06g032_register_gate(clocks, parent_name, d);
939*4882a593Smuzhiyun break;
940*4882a593Smuzhiyun case K_DIV:
941*4882a593Smuzhiyun clk = r9a06g032_register_div(clocks, parent_name, d);
942*4882a593Smuzhiyun break;
943*4882a593Smuzhiyun case K_BITSEL:
944*4882a593Smuzhiyun /* keep that selector register around */
945*4882a593Smuzhiyun uart_group_sel[d->dual.group] = d->dual.sel;
946*4882a593Smuzhiyun clk = r9a06g032_register_bitsel(clocks, parent_name, d);
947*4882a593Smuzhiyun break;
948*4882a593Smuzhiyun case K_DUALGATE:
949*4882a593Smuzhiyun clk = r9a06g032_register_dualgate(clocks, parent_name,
950*4882a593Smuzhiyun d,
951*4882a593Smuzhiyun uart_group_sel[d->dual.group]);
952*4882a593Smuzhiyun break;
953*4882a593Smuzhiyun }
954*4882a593Smuzhiyun clocks->data.clks[d->index] = clk;
955*4882a593Smuzhiyun }
956*4882a593Smuzhiyun error = of_clk_add_provider(np, of_clk_src_onecell_get, &clocks->data);
957*4882a593Smuzhiyun if (error)
958*4882a593Smuzhiyun return error;
959*4882a593Smuzhiyun
960*4882a593Smuzhiyun error = devm_add_action_or_reset(dev,
961*4882a593Smuzhiyun r9a06g032_clocks_del_clk_provider, np);
962*4882a593Smuzhiyun if (error)
963*4882a593Smuzhiyun return error;
964*4882a593Smuzhiyun
965*4882a593Smuzhiyun return r9a06g032_add_clk_domain(dev);
966*4882a593Smuzhiyun }
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun static const struct of_device_id r9a06g032_match[] = {
969*4882a593Smuzhiyun { .compatible = "renesas,r9a06g032-sysctrl" },
970*4882a593Smuzhiyun { }
971*4882a593Smuzhiyun };
972*4882a593Smuzhiyun
973*4882a593Smuzhiyun static struct platform_driver r9a06g032_clock_driver = {
974*4882a593Smuzhiyun .driver = {
975*4882a593Smuzhiyun .name = "renesas,r9a06g032-sysctrl",
976*4882a593Smuzhiyun .of_match_table = r9a06g032_match,
977*4882a593Smuzhiyun },
978*4882a593Smuzhiyun };
979*4882a593Smuzhiyun
r9a06g032_clocks_init(void)980*4882a593Smuzhiyun static int __init r9a06g032_clocks_init(void)
981*4882a593Smuzhiyun {
982*4882a593Smuzhiyun return platform_driver_probe(&r9a06g032_clock_driver,
983*4882a593Smuzhiyun r9a06g032_clocks_probe);
984*4882a593Smuzhiyun }
985*4882a593Smuzhiyun
986*4882a593Smuzhiyun subsys_initcall(r9a06g032_clocks_init);
987