blob: d2f61236fee1bfeff3796be7cf6add640a9de77c [file] [log] [blame]
Ralph Siemsena0dfc712023-05-12 21:36:51 -04001// SPDX-License-Identifier: GPL-2.0
2/*
3 * R9A06G032 clock driver
4 *
5 * Copyright (C) 2018 Renesas Electronics Europe Limited
6 *
7 * Michel Pollet <michel.pollet@bp.renesas.com>, <buserror@gmail.com>
8 */
9
10#include <common.h>
11#include <clk-uclass.h>
12#include <dm.h>
13#include <dm/device_compat.h>
14#include <regmap.h>
15#include <syscon.h>
16#include <linux/bitops.h>
17#include <linux/clk-provider.h>
18#include <linux/delay.h>
19#include <asm/io.h>
20
21#include <dt-bindings/clock/r9a06g032-sysctrl.h>
22
23/**
24 * struct regbit - describe one bit in a register
25 * @reg: offset of register relative to base address,
26 * expressed in units of 32-bit words (not bytes),
27 * @bit: which bit (0 to 31) in the register
28 *
29 * This structure is used to compactly encode the location
30 * of a single bit in a register. Five bits are needed to
31 * encode the bit number. With uint16_t data type, this
32 * leaves 11 bits to encode a register offset up to 2047.
33 *
34 * Since registers are aligned on 32-bit boundaries, the
35 * offset will be specified in 32-bit words rather than bytes.
36 * This allows encoding an offset up to 0x1FFC (8188) bytes.
37 *
38 * Helper macro RB() takes care of converting the register
39 * offset from bytes to 32-bit words.
40 */
41struct regbit {
42 u16 reg:11;
43 u16 bit:5;
44};
45
46#define RB(_reg, _bit) ((struct regbit) { \
47 .reg = (_reg) / 4, \
48 .bit = (_bit) \
49})
50
51/**
52 * struct r9a06g032_gate - clock-related control bits
53 * @gate: clock enable/disable
54 * @reset: clock module reset (active low)
55 * @ready: enables NoC forwarding of read/write requests to device,
56 * (eg. device is ready to handle read/write requests)
57 * @midle: request to idle the NoC interconnect
58 *
59 * Each of these fields describes a single bit in a register,
60 * which controls some aspect of clock gating. The @gate field
61 * is mandatory, this one enables/disables the clock. The
62 * other fields are optional, with zero indicating "not used".
63 *
64 * In most cases there is a @reset bit which needs to be
65 * de-asserted to bring the module out of reset.
66 *
67 * Modules may also need to signal when the are @ready to
68 * handle requests (read/writes) from the NoC interconnect.
69 *
70 * Similarly, the @midle bit is used to idle the master.
71 */
72struct r9a06g032_gate {
73 struct regbit gate, reset, ready, midle;
74 /* Unused fields omitted to save space */
75 /* struct regbit scon, mirack, mistat */;
76};
77
78enum gate_type {
79 K_GATE = 0, /* gate which enable/disable */
80 K_FFC, /* fixed factor clock */
81 K_DIV, /* divisor */
82 K_BITSEL, /* special for UARTs */
83 K_DUALGATE /* special for UARTs */
84};
85
86/**
87 * struct r9a06g032_clkdesc - describe a single clock
88 * @name: string describing this clock
89 * @managed: boolean indicating if this clock should be
90 * started/stopped as part of power management
91 * (not used in u-boot)
92 * @type: see enum @gate_type
93 * @index: the ID of this clock element
94 * @source: the ID+1 of the parent clock element.
95 * Root clock uses ID of ~0 (PARENT_ID);
96 * @gate: clock enable/disable
97 * @div_min: smallest permitted clock divider
98 * @div_max: largest permitted clock divider
99 * @reg: clock divider register offset, in 32-bit words
100 * @div_table: optional list of fixed clock divider values;
101 * must be in ascending order, zero for unused
102 * @div: divisor for fixed-factor clock
103 * @mul: multiplier for fixed-factor clock
104 * @group: UART group, 0=UART0/1/2, 1=UART3/4/5/6/7
105 * @sel: select either g1/r1 or g2/r2 as clock source
106 * @g1: 1st source gate (clock enable/disable)
107 * @r1: 1st source reset (module reset)
108 * @g2: 2nd source gate (clock enable/disable)
109 * @r2: 2nd source reset (module reset)
110 *
111 * Describes a single element in the clock tree hierarchy.
112 * As there are quite a large number of clock elements, this
113 * structure is packed tightly to conserve space.
114 */
115struct r9a06g032_clkdesc {
116 const char *name;
117 uint32_t managed:1;
118 enum gate_type type:3;
119 uint32_t index:8;
120 uint32_t source:8; /* source index + 1 (0 == none) */
121 union {
122 /* type = K_GATE */
123 struct r9a06g032_gate gate;
124 /* type = K_DIV */
125 struct {
126 unsigned int div_min:10, div_max:10, reg:10;
127 u16 div_table[4];
128 };
129 /* type = K_FFC */
130 struct {
131 u16 div, mul;
132 };
133 /* type = K_DUALGATE */
134 struct {
135 uint16_t group:1;
136 struct regbit sel, g1, r1, g2, r2;
137 } dual;
138 };
139};
140
141/*
142 * The last three arguments are not currently used,
143 * but are kept in the r9a06g032_clocks table below.
144 */
145#define I_GATE(_clk, _rst, _rdy, _midle, _scon, _mirack, _mistat) { \
146 .gate = _clk, \
147 .reset = _rst, \
148 .ready = _rdy, \
149 .midle = _midle, \
150 /* .scon = _scon, */ \
151 /* .mirack = _mirack, */ \
152 /* .mistat = _mistat */ \
153}
154#define D_GATE(_idx, _n, _src, ...) { \
155 .type = K_GATE, \
156 .index = R9A06G032_##_idx, \
157 .source = 1 + R9A06G032_##_src, \
158 .name = _n, \
159 .gate = I_GATE(__VA_ARGS__) \
160}
161#define D_MODULE(_idx, _n, _src, ...) { \
162 .type = K_GATE, \
163 .index = R9A06G032_##_idx, \
164 .source = 1 + R9A06G032_##_src, \
165 .name = _n, \
166 .managed = 1, \
167 .gate = I_GATE(__VA_ARGS__) \
168}
169#define D_ROOT(_idx, _n, _mul, _div) { \
170 .type = K_FFC, \
171 .index = R9A06G032_##_idx, \
172 .name = _n, \
173 .div = _div, \
174 .mul = _mul \
175}
176#define D_FFC(_idx, _n, _src, _div) { \
177 .type = K_FFC, \
178 .index = R9A06G032_##_idx, \
179 .source = 1 + R9A06G032_##_src, \
180 .name = _n, \
181 .div = _div, \
182 .mul = 1 \
183}
184#define D_DIV(_idx, _n, _src, _reg, _min, _max, ...) { \
185 .type = K_DIV, \
186 .index = R9A06G032_##_idx, \
187 .source = 1 + R9A06G032_##_src, \
188 .name = _n, \
189 .reg = _reg, \
190 .div_min = _min, \
191 .div_max = _max, \
192 .div_table = { __VA_ARGS__ } \
193}
194#define D_UGATE(_idx, _n, _src, _g, _g1, _r1, _g2, _r2) { \
195 .type = K_DUALGATE, \
196 .index = R9A06G032_##_idx, \
197 .source = 1 + R9A06G032_##_src, \
198 .name = _n, \
199 .dual = { \
200 .group = _g, \
201 .g1 = _g1, \
202 .r1 = _r1, \
203 .g2 = _g2, \
204 .r2 = _r2 \
205 }, \
206}
207
208/* Internal clock IDs */
209#define R9A06G032_CLKOUT 0
210#define R9A06G032_CLKOUT_D10 2
211#define R9A06G032_CLKOUT_D16 3
212#define R9A06G032_CLKOUT_D160 4
213#define R9A06G032_CLKOUT_D1OR2 5
214#define R9A06G032_CLKOUT_D20 6
215#define R9A06G032_CLKOUT_D40 7
216#define R9A06G032_CLKOUT_D5 8
217#define R9A06G032_CLKOUT_D8 9
218#define R9A06G032_DIV_ADC 10
219#define R9A06G032_DIV_I2C 11
220#define R9A06G032_DIV_NAND 12
221#define R9A06G032_DIV_P1_PG 13
222#define R9A06G032_DIV_P2_PG 14
223#define R9A06G032_DIV_P3_PG 15
224#define R9A06G032_DIV_P4_PG 16
225#define R9A06G032_DIV_P5_PG 17
226#define R9A06G032_DIV_P6_PG 18
227#define R9A06G032_DIV_QSPI0 19
228#define R9A06G032_DIV_QSPI1 20
229#define R9A06G032_DIV_REF_SYNC 21
230#define R9A06G032_DIV_SDIO0 22
231#define R9A06G032_DIV_SDIO1 23
232#define R9A06G032_DIV_SWITCH 24
233#define R9A06G032_DIV_UART 25
234#define R9A06G032_DIV_MOTOR 64
235#define R9A06G032_CLK_DDRPHY_PLLCLK_D4 78
236#define R9A06G032_CLK_ECAT100_D4 79
237#define R9A06G032_CLK_HSR100_D2 80
238#define R9A06G032_CLK_REF_SYNC_D4 81
239#define R9A06G032_CLK_REF_SYNC_D8 82
240#define R9A06G032_CLK_SERCOS100_D2 83
241#define R9A06G032_DIV_CA7 84
242
243#define R9A06G032_UART_GROUP_012 154
244#define R9A06G032_UART_GROUP_34567 155
245
246#define R9A06G032_CLOCK_COUNT (R9A06G032_UART_GROUP_34567 + 1)
247
248static const struct r9a06g032_clkdesc r9a06g032_clocks[] = {
249 D_ROOT(CLKOUT, "clkout", 25, 1),
250 D_ROOT(CLK_PLL_USB, "clk_pll_usb", 12, 10),
251 D_FFC(CLKOUT_D10, "clkout_d10", CLKOUT, 10),
252 D_FFC(CLKOUT_D16, "clkout_d16", CLKOUT, 16),
253 D_FFC(CLKOUT_D160, "clkout_d160", CLKOUT, 160),
254 D_DIV(CLKOUT_D1OR2, "clkout_d1or2", CLKOUT, 0, 1, 2),
255 D_FFC(CLKOUT_D20, "clkout_d20", CLKOUT, 20),
256 D_FFC(CLKOUT_D40, "clkout_d40", CLKOUT, 40),
257 D_FFC(CLKOUT_D5, "clkout_d5", CLKOUT, 5),
258 D_FFC(CLKOUT_D8, "clkout_d8", CLKOUT, 8),
259 D_DIV(DIV_ADC, "div_adc", CLKOUT, 77, 50, 250),
260 D_DIV(DIV_I2C, "div_i2c", CLKOUT, 78, 12, 16),
261 D_DIV(DIV_NAND, "div_nand", CLKOUT, 82, 12, 32),
262 D_DIV(DIV_P1_PG, "div_p1_pg", CLKOUT, 68, 12, 200),
263 D_DIV(DIV_P2_PG, "div_p2_pg", CLKOUT, 62, 12, 128),
264 D_DIV(DIV_P3_PG, "div_p3_pg", CLKOUT, 64, 8, 128),
265 D_DIV(DIV_P4_PG, "div_p4_pg", CLKOUT, 66, 8, 128),
266 D_DIV(DIV_P5_PG, "div_p5_pg", CLKOUT, 71, 10, 40),
267 D_DIV(DIV_P6_PG, "div_p6_pg", CLKOUT, 18, 12, 64),
268 D_DIV(DIV_QSPI0, "div_qspi0", CLKOUT, 73, 3, 7),
269 D_DIV(DIV_QSPI1, "div_qspi1", CLKOUT, 25, 3, 7),
270 D_DIV(DIV_REF_SYNC, "div_ref_sync", CLKOUT, 56, 2, 16, 2, 4, 8, 16),
271 D_DIV(DIV_SDIO0, "div_sdio0", CLKOUT, 74, 20, 128),
272 D_DIV(DIV_SDIO1, "div_sdio1", CLKOUT, 75, 20, 128),
273 D_DIV(DIV_SWITCH, "div_switch", CLKOUT, 37, 5, 40),
274 D_DIV(DIV_UART, "div_uart", CLKOUT, 79, 12, 128),
275 D_GATE(CLK_25_PG4, "clk_25_pg4", CLKOUT_D40, RB(0xe8, 9),
276 RB(0xe8, 10), RB(0xe8, 11), RB(0x00, 0),
277 RB(0x15c, 3), RB(0x00, 0), RB(0x00, 0)),
278 D_GATE(CLK_25_PG5, "clk_25_pg5", CLKOUT_D40, RB(0xe8, 12),
279 RB(0xe8, 13), RB(0xe8, 14), RB(0x00, 0),
280 RB(0x15c, 4), RB(0x00, 0), RB(0x00, 0)),
281 D_GATE(CLK_25_PG6, "clk_25_pg6", CLKOUT_D40, RB(0xe8, 15),
282 RB(0xe8, 16), RB(0xe8, 17), RB(0x00, 0),
283 RB(0x15c, 5), RB(0x00, 0), RB(0x00, 0)),
284 D_GATE(CLK_25_PG7, "clk_25_pg7", CLKOUT_D40, RB(0xe8, 18),
285 RB(0xe8, 19), RB(0xe8, 20), RB(0x00, 0),
286 RB(0x15c, 6), RB(0x00, 0), RB(0x00, 0)),
287 D_GATE(CLK_25_PG8, "clk_25_pg8", CLKOUT_D40, RB(0xe8, 21),
288 RB(0xe8, 22), RB(0xe8, 23), RB(0x00, 0),
289 RB(0x15c, 7), RB(0x00, 0), RB(0x00, 0)),
290 D_GATE(CLK_ADC, "clk_adc", DIV_ADC, RB(0x3c, 10),
291 RB(0x3c, 11), RB(0x00, 0), RB(0x00, 0),
292 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
293 D_GATE(CLK_ECAT100, "clk_ecat100", CLKOUT_D10, RB(0x80, 5),
294 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
295 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
296 D_GATE(CLK_HSR100, "clk_hsr100", CLKOUT_D10, RB(0x90, 3),
297 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
298 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
299 D_GATE(CLK_I2C0, "clk_i2c0", DIV_I2C, RB(0x3c, 6),
300 RB(0x3c, 7), RB(0x00, 0), RB(0x00, 0),
301 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
302 D_GATE(CLK_I2C1, "clk_i2c1", DIV_I2C, RB(0x3c, 8),
303 RB(0x3c, 9), RB(0x00, 0), RB(0x00, 0),
304 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
305 D_GATE(CLK_MII_REF, "clk_mii_ref", CLKOUT_D40, RB(0x68, 2),
306 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
307 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
308 D_GATE(CLK_NAND, "clk_nand", DIV_NAND, RB(0x50, 4),
309 RB(0x50, 5), RB(0x00, 0), RB(0x00, 0),
310 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
311 D_GATE(CLK_NOUSBP2_PG6, "clk_nousbp2_pg6", DIV_P2_PG, RB(0xec, 20),
312 RB(0xec, 21), RB(0x00, 0), RB(0x00, 0),
313 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
314 D_GATE(CLK_P1_PG2, "clk_p1_pg2", DIV_P1_PG, RB(0x10c, 2),
315 RB(0x10c, 3), RB(0x00, 0), RB(0x00, 0),
316 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
317 D_GATE(CLK_P1_PG3, "clk_p1_pg3", DIV_P1_PG, RB(0x10c, 4),
318 RB(0x10c, 5), RB(0x00, 0), RB(0x00, 0),
319 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
320 D_GATE(CLK_P1_PG4, "clk_p1_pg4", DIV_P1_PG, RB(0x10c, 6),
321 RB(0x10c, 7), RB(0x00, 0), RB(0x00, 0),
322 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
323 D_GATE(CLK_P4_PG3, "clk_p4_pg3", DIV_P4_PG, RB(0x104, 4),
324 RB(0x104, 5), RB(0x00, 0), RB(0x00, 0),
325 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
326 D_GATE(CLK_P4_PG4, "clk_p4_pg4", DIV_P4_PG, RB(0x104, 6),
327 RB(0x104, 7), RB(0x00, 0), RB(0x00, 0),
328 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
329 D_GATE(CLK_P6_PG1, "clk_p6_pg1", DIV_P6_PG, RB(0x114, 0),
330 RB(0x114, 1), RB(0x114, 2), RB(0x00, 0),
331 RB(0x16c, 0), RB(0x00, 0), RB(0x00, 0)),
332 D_GATE(CLK_P6_PG2, "clk_p6_pg2", DIV_P6_PG, RB(0x114, 3),
333 RB(0x114, 4), RB(0x114, 5), RB(0x00, 0),
334 RB(0x16c, 1), RB(0x00, 0), RB(0x00, 0)),
335 D_GATE(CLK_P6_PG3, "clk_p6_pg3", DIV_P6_PG, RB(0x114, 6),
336 RB(0x114, 7), RB(0x114, 8), RB(0x00, 0),
337 RB(0x16c, 2), RB(0x00, 0), RB(0x00, 0)),
338 D_GATE(CLK_P6_PG4, "clk_p6_pg4", DIV_P6_PG, RB(0x114, 9),
339 RB(0x114, 10), RB(0x114, 11), RB(0x00, 0),
340 RB(0x16c, 3), RB(0x00, 0), RB(0x00, 0)),
341 D_MODULE(CLK_PCI_USB, "clk_pci_usb", CLKOUT_D40, RB(0x1c, 6),
342 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
343 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
344 D_GATE(CLK_QSPI0, "clk_qspi0", DIV_QSPI0, RB(0x54, 4),
345 RB(0x54, 5), RB(0x00, 0), RB(0x00, 0),
346 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
347 D_GATE(CLK_QSPI1, "clk_qspi1", DIV_QSPI1, RB(0x90, 4),
348 RB(0x90, 5), RB(0x00, 0), RB(0x00, 0),
349 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
350 D_GATE(CLK_RGMII_REF, "clk_rgmii_ref", CLKOUT_D8, RB(0x68, 0),
351 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
352 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
353 D_GATE(CLK_RMII_REF, "clk_rmii_ref", CLKOUT_D20, RB(0x68, 1),
354 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
355 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
356 D_GATE(CLK_SDIO0, "clk_sdio0", DIV_SDIO0, RB(0x0c, 4),
357 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
358 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
359 D_GATE(CLK_SDIO1, "clk_sdio1", DIV_SDIO1, RB(0xc8, 4),
360 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
361 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
362 D_GATE(CLK_SERCOS100, "clk_sercos100", CLKOUT_D10, RB(0x84, 5),
363 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
364 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
365 D_GATE(CLK_SLCD, "clk_slcd", DIV_P1_PG, RB(0x10c, 0),
366 RB(0x10c, 1), RB(0x00, 0), RB(0x00, 0),
367 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
368 D_GATE(CLK_SPI0, "clk_spi0", DIV_P3_PG, RB(0xfc, 0),
369 RB(0xfc, 1), RB(0x00, 0), RB(0x00, 0),
370 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
371 D_GATE(CLK_SPI1, "clk_spi1", DIV_P3_PG, RB(0xfc, 2),
372 RB(0xfc, 3), RB(0x00, 0), RB(0x00, 0),
373 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
374 D_GATE(CLK_SPI2, "clk_spi2", DIV_P3_PG, RB(0xfc, 4),
375 RB(0xfc, 5), RB(0x00, 0), RB(0x00, 0),
376 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
377 D_GATE(CLK_SPI3, "clk_spi3", DIV_P3_PG, RB(0xfc, 6),
378 RB(0xfc, 7), RB(0x00, 0), RB(0x00, 0),
379 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
380 D_GATE(CLK_SPI4, "clk_spi4", DIV_P4_PG, RB(0x104, 0),
381 RB(0x104, 1), RB(0x00, 0), RB(0x00, 0),
382 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
383 D_GATE(CLK_SPI5, "clk_spi5", DIV_P4_PG, RB(0x104, 2),
384 RB(0x104, 3), RB(0x00, 0), RB(0x00, 0),
385 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
386 D_GATE(CLK_SWITCH, "clk_switch", DIV_SWITCH, RB(0x130, 2),
387 RB(0x130, 3), RB(0x00, 0), RB(0x00, 0),
388 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
389 D_DIV(DIV_MOTOR, "div_motor", CLKOUT_D5, 84, 2, 8),
390 D_MODULE(HCLK_ECAT125, "hclk_ecat125", CLKOUT_D8, RB(0x80, 0),
391 RB(0x80, 1), RB(0x00, 0), RB(0x80, 2),
392 RB(0x00, 0), RB(0x88, 0), RB(0x88, 1)),
393 D_MODULE(HCLK_PINCONFIG, "hclk_pinconfig", CLKOUT_D40, RB(0xe8, 0),
394 RB(0xe8, 1), RB(0xe8, 2), RB(0x00, 0),
395 RB(0x15c, 0), RB(0x00, 0), RB(0x00, 0)),
396 D_MODULE(HCLK_SERCOS, "hclk_sercos", CLKOUT_D10, RB(0x84, 0),
397 RB(0x84, 2), RB(0x00, 0), RB(0x84, 1),
398 RB(0x00, 0), RB(0x8c, 0), RB(0x8c, 1)),
399 D_MODULE(HCLK_SGPIO2, "hclk_sgpio2", DIV_P5_PG, RB(0x118, 3),
400 RB(0x118, 4), RB(0x118, 5), RB(0x00, 0),
401 RB(0x168, 1), RB(0x00, 0), RB(0x00, 0)),
402 D_MODULE(HCLK_SGPIO3, "hclk_sgpio3", DIV_P5_PG, RB(0x118, 6),
403 RB(0x118, 7), RB(0x118, 8), RB(0x00, 0),
404 RB(0x168, 2), RB(0x00, 0), RB(0x00, 0)),
405 D_MODULE(HCLK_SGPIO4, "hclk_sgpio4", DIV_P5_PG, RB(0x118, 9),
406 RB(0x118, 10), RB(0x118, 11), RB(0x00, 0),
407 RB(0x168, 3), RB(0x00, 0), RB(0x00, 0)),
408 D_MODULE(HCLK_TIMER0, "hclk_timer0", CLKOUT_D40, RB(0xe8, 3),
409 RB(0xe8, 4), RB(0xe8, 5), RB(0x00, 0),
410 RB(0x15c, 1), RB(0x00, 0), RB(0x00, 0)),
411 D_MODULE(HCLK_TIMER1, "hclk_timer1", CLKOUT_D40, RB(0xe8, 6),
412 RB(0xe8, 7), RB(0xe8, 8), RB(0x00, 0),
413 RB(0x15c, 2), RB(0x00, 0), RB(0x00, 0)),
414 D_MODULE(HCLK_USBF, "hclk_usbf", CLKOUT_D8, RB(0x1c, 3),
415 RB(0x00, 0), RB(0x00, 0), RB(0x1c, 4),
416 RB(0x00, 0), RB(0x20, 2), RB(0x20, 3)),
417 D_MODULE(HCLK_USBH, "hclk_usbh", CLKOUT_D8, RB(0x1c, 0),
418 RB(0x1c, 1), RB(0x00, 0), RB(0x1c, 2),
419 RB(0x00, 0), RB(0x20, 0), RB(0x20, 1)),
420 D_MODULE(HCLK_USBPM, "hclk_usbpm", CLKOUT_D8, RB(0x1c, 5),
421 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0),
422 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
423 D_GATE(CLK_48_PG_F, "clk_48_pg_f", CLK_48, RB(0xf0, 12),
424 RB(0xf0, 13), RB(0x00, 0), RB(0xf0, 14),
425 RB(0x00, 0), RB(0x160, 4), RB(0x160, 5)),
426 D_GATE(CLK_48_PG4, "clk_48_pg4", CLK_48, RB(0xf0, 9),
427 RB(0xf0, 10), RB(0xf0, 11), RB(0x00, 0),
428 RB(0x160, 3), RB(0x00, 0), RB(0x00, 0)),
429 D_FFC(CLK_DDRPHY_PLLCLK_D4, "clk_ddrphy_pllclk_d4", CLK_DDRPHY_PLLCLK, 4),
430 D_FFC(CLK_ECAT100_D4, "clk_ecat100_d4", CLK_ECAT100, 4),
431 D_FFC(CLK_HSR100_D2, "clk_hsr100_d2", CLK_HSR100, 2),
432 D_FFC(CLK_REF_SYNC_D4, "clk_ref_sync_d4", CLK_REF_SYNC, 4),
433 D_FFC(CLK_REF_SYNC_D8, "clk_ref_sync_d8", CLK_REF_SYNC, 8),
434 D_FFC(CLK_SERCOS100_D2, "clk_sercos100_d2", CLK_SERCOS100, 2),
435 D_DIV(DIV_CA7, "div_ca7", CLK_REF_SYNC, 57, 1, 4, 1, 2, 4),
436 D_MODULE(HCLK_CAN0, "hclk_can0", CLK_48, RB(0xf0, 3),
437 RB(0xf0, 4), RB(0xf0, 5), RB(0x00, 0),
438 RB(0x160, 1), RB(0x00, 0), RB(0x00, 0)),
439 D_MODULE(HCLK_CAN1, "hclk_can1", CLK_48, RB(0xf0, 6),
440 RB(0xf0, 7), RB(0xf0, 8), RB(0x00, 0),
441 RB(0x160, 2), RB(0x00, 0), RB(0x00, 0)),
442 D_MODULE(HCLK_DELTASIGMA, "hclk_deltasigma", DIV_MOTOR, RB(0x3c, 15),
443 RB(0x3c, 16), RB(0x3c, 17), RB(0x00, 0),
444 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
445 D_MODULE(HCLK_PWMPTO, "hclk_pwmpto", DIV_MOTOR, RB(0x3c, 12),
446 RB(0x3c, 13), RB(0x3c, 14), RB(0x00, 0),
447 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
448 D_MODULE(HCLK_RSV, "hclk_rsv", CLK_48, RB(0xf0, 0),
449 RB(0xf0, 1), RB(0xf0, 2), RB(0x00, 0),
450 RB(0x160, 0), RB(0x00, 0), RB(0x00, 0)),
451 D_MODULE(HCLK_SGPIO0, "hclk_sgpio0", DIV_MOTOR, RB(0x3c, 0),
452 RB(0x3c, 1), RB(0x3c, 2), RB(0x00, 0),
453 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
454 D_MODULE(HCLK_SGPIO1, "hclk_sgpio1", DIV_MOTOR, RB(0x3c, 3),
455 RB(0x3c, 4), RB(0x3c, 5), RB(0x00, 0),
456 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
457 D_DIV(RTOS_MDC, "rtos_mdc", CLK_REF_SYNC, 100, 80, 640, 80, 160, 320, 640),
458 D_GATE(CLK_CM3, "clk_cm3", CLK_REF_SYNC_D4, RB(0x174, 0),
459 RB(0x174, 1), RB(0x00, 0), RB(0x174, 2),
460 RB(0x00, 0), RB(0x178, 0), RB(0x178, 1)),
461 D_GATE(CLK_DDRC, "clk_ddrc", CLK_DDRPHY_PLLCLK_D4, RB(0x64, 3),
462 RB(0x64, 4), RB(0x00, 0), RB(0x00, 0),
463 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
464 D_GATE(CLK_ECAT25, "clk_ecat25", CLK_ECAT100_D4, RB(0x80, 3),
465 RB(0x80, 4), RB(0x00, 0), RB(0x00, 0),
466 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
467 D_GATE(CLK_HSR50, "clk_hsr50", CLK_HSR100_D2, RB(0x90, 4),
468 RB(0x90, 5), RB(0x00, 0), RB(0x00, 0),
469 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
470 D_GATE(CLK_HW_RTOS, "clk_hw_rtos", CLK_REF_SYNC_D4, RB(0x18c, 0),
471 RB(0x18c, 1), RB(0x00, 0), RB(0x00, 0),
472 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
473 D_GATE(CLK_SERCOS50, "clk_sercos50", CLK_SERCOS100_D2, RB(0x84, 4),
474 RB(0x84, 3), RB(0x00, 0), RB(0x00, 0),
475 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
476 D_MODULE(HCLK_ADC, "hclk_adc", CLK_REF_SYNC_D8, RB(0x34, 15),
477 RB(0x34, 16), RB(0x34, 17), RB(0x00, 0),
478 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
479 D_MODULE(HCLK_CM3, "hclk_cm3", CLK_REF_SYNC_D4, RB(0x184, 0),
480 RB(0x184, 1), RB(0x184, 2), RB(0x00, 0),
481 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
482 D_MODULE(HCLK_CRYPTO_EIP150, "hclk_crypto_eip150", CLK_REF_SYNC_D4, RB(0x24, 3),
483 RB(0x24, 4), RB(0x24, 5), RB(0x00, 0),
484 RB(0x28, 2), RB(0x00, 0), RB(0x00, 0)),
485 D_MODULE(HCLK_CRYPTO_EIP93, "hclk_crypto_eip93", CLK_REF_SYNC_D4, RB(0x24, 0),
486 RB(0x24, 1), RB(0x00, 0), RB(0x24, 2),
487 RB(0x00, 0), RB(0x28, 0), RB(0x28, 1)),
488 D_MODULE(HCLK_DDRC, "hclk_ddrc", CLK_REF_SYNC_D4, RB(0x64, 0),
489 RB(0x64, 2), RB(0x00, 0), RB(0x64, 1),
490 RB(0x00, 0), RB(0x74, 0), RB(0x74, 1)),
491 D_MODULE(HCLK_DMA0, "hclk_dma0", CLK_REF_SYNC_D4, RB(0x4c, 0),
492 RB(0x4c, 1), RB(0x4c, 2), RB(0x4c, 3),
493 RB(0x58, 0), RB(0x58, 1), RB(0x58, 2)),
494 D_MODULE(HCLK_DMA1, "hclk_dma1", CLK_REF_SYNC_D4, RB(0x4c, 4),
495 RB(0x4c, 5), RB(0x4c, 6), RB(0x4c, 7),
496 RB(0x58, 3), RB(0x58, 4), RB(0x58, 5)),
497 D_MODULE(HCLK_GMAC0, "hclk_gmac0", CLK_REF_SYNC_D4, RB(0x6c, 0),
498 RB(0x6c, 1), RB(0x6c, 2), RB(0x6c, 3),
499 RB(0x78, 0), RB(0x78, 1), RB(0x78, 2)),
500 D_MODULE(HCLK_GMAC1, "hclk_gmac1", CLK_REF_SYNC_D4, RB(0x70, 0),
501 RB(0x70, 1), RB(0x70, 2), RB(0x70, 3),
502 RB(0x7c, 0), RB(0x7c, 1), RB(0x7c, 2)),
503 D_MODULE(HCLK_GPIO0, "hclk_gpio0", CLK_REF_SYNC_D4, RB(0x40, 18),
504 RB(0x40, 19), RB(0x40, 20), RB(0x00, 0),
505 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
506 D_MODULE(HCLK_GPIO1, "hclk_gpio1", CLK_REF_SYNC_D4, RB(0x40, 21),
507 RB(0x40, 22), RB(0x40, 23), RB(0x00, 0),
508 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
509 D_MODULE(HCLK_GPIO2, "hclk_gpio2", CLK_REF_SYNC_D4, RB(0x44, 9),
510 RB(0x44, 10), RB(0x44, 11), RB(0x00, 0),
511 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
512 D_MODULE(HCLK_HSR, "hclk_hsr", CLK_HSR100_D2, RB(0x90, 0),
513 RB(0x90, 2), RB(0x00, 0), RB(0x90, 1),
514 RB(0x00, 0), RB(0x98, 0), RB(0x98, 1)),
515 D_MODULE(HCLK_I2C0, "hclk_i2c0", CLK_REF_SYNC_D8, RB(0x34, 9),
516 RB(0x34, 10), RB(0x34, 11), RB(0x00, 0),
517 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
518 D_MODULE(HCLK_I2C1, "hclk_i2c1", CLK_REF_SYNC_D8, RB(0x34, 12),
519 RB(0x34, 13), RB(0x34, 14), RB(0x00, 0),
520 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
521 D_MODULE(HCLK_LCD, "hclk_lcd", CLK_REF_SYNC_D4, RB(0xf4, 0),
522 RB(0xf4, 1), RB(0xf4, 2), RB(0x00, 0),
523 RB(0x164, 0), RB(0x00, 0), RB(0x00, 0)),
524 D_MODULE(HCLK_MSEBI_M, "hclk_msebi_m", CLK_REF_SYNC_D4, RB(0x2c, 4),
525 RB(0x2c, 5), RB(0x2c, 6), RB(0x00, 0),
526 RB(0x30, 3), RB(0x00, 0), RB(0x00, 0)),
527 D_MODULE(HCLK_MSEBI_S, "hclk_msebi_s", CLK_REF_SYNC_D4, RB(0x2c, 0),
528 RB(0x2c, 1), RB(0x2c, 2), RB(0x2c, 3),
529 RB(0x30, 0), RB(0x30, 1), RB(0x30, 2)),
530 D_MODULE(HCLK_NAND, "hclk_nand", CLK_REF_SYNC_D4, RB(0x50, 0),
531 RB(0x50, 1), RB(0x50, 2), RB(0x50, 3),
532 RB(0x5c, 0), RB(0x5c, 1), RB(0x5c, 2)),
533 D_MODULE(HCLK_PG_I, "hclk_pg_i", CLK_REF_SYNC_D4, RB(0xf4, 12),
534 RB(0xf4, 13), RB(0x00, 0), RB(0xf4, 14),
535 RB(0x00, 0), RB(0x164, 4), RB(0x164, 5)),
536 D_MODULE(HCLK_PG19, "hclk_pg19", CLK_REF_SYNC_D4, RB(0x44, 12),
537 RB(0x44, 13), RB(0x44, 14), RB(0x00, 0),
538 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
539 D_MODULE(HCLK_PG20, "hclk_pg20", CLK_REF_SYNC_D4, RB(0x44, 15),
540 RB(0x44, 16), RB(0x44, 17), RB(0x00, 0),
541 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
542 D_MODULE(HCLK_PG3, "hclk_pg3", CLK_REF_SYNC_D4, RB(0xf4, 6),
543 RB(0xf4, 7), RB(0xf4, 8), RB(0x00, 0),
544 RB(0x164, 2), RB(0x00, 0), RB(0x00, 0)),
545 D_MODULE(HCLK_PG4, "hclk_pg4", CLK_REF_SYNC_D4, RB(0xf4, 9),
546 RB(0xf4, 10), RB(0xf4, 11), RB(0x00, 0),
547 RB(0x164, 3), RB(0x00, 0), RB(0x00, 0)),
548 D_MODULE(HCLK_QSPI0, "hclk_qspi0", CLK_REF_SYNC_D4, RB(0x54, 0),
549 RB(0x54, 1), RB(0x54, 2), RB(0x54, 3),
550 RB(0x60, 0), RB(0x60, 1), RB(0x60, 2)),
551 D_MODULE(HCLK_QSPI1, "hclk_qspi1", CLK_REF_SYNC_D4, RB(0x90, 0),
552 RB(0x90, 1), RB(0x90, 2), RB(0x90, 3),
553 RB(0x98, 0), RB(0x98, 1), RB(0x98, 2)),
554 D_MODULE(HCLK_ROM, "hclk_rom", CLK_REF_SYNC_D4, RB(0x154, 0),
555 RB(0x154, 1), RB(0x154, 2), RB(0x00, 0),
556 RB(0x170, 0), RB(0x00, 0), RB(0x00, 0)),
557 D_MODULE(HCLK_RTC, "hclk_rtc", CLK_REF_SYNC_D8, RB(0x140, 0),
558 RB(0x140, 3), RB(0x00, 0), RB(0x140, 2),
559 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
560 D_MODULE(HCLK_SDIO0, "hclk_sdio0", CLK_REF_SYNC_D4, RB(0x0c, 0),
561 RB(0x0c, 1), RB(0x0c, 2), RB(0x0c, 3),
562 RB(0x10, 0), RB(0x10, 1), RB(0x10, 2)),
563 D_MODULE(HCLK_SDIO1, "hclk_sdio1", CLK_REF_SYNC_D4, RB(0xc8, 0),
564 RB(0xc8, 1), RB(0xc8, 2), RB(0xc8, 3),
565 RB(0xcc, 0), RB(0xcc, 1), RB(0xcc, 2)),
566 D_MODULE(HCLK_SEMAP, "hclk_semap", CLK_REF_SYNC_D4, RB(0xf4, 3),
567 RB(0xf4, 4), RB(0xf4, 5), RB(0x00, 0),
568 RB(0x164, 1), RB(0x00, 0), RB(0x00, 0)),
569 D_MODULE(HCLK_SPI0, "hclk_spi0", CLK_REF_SYNC_D4, RB(0x40, 0),
570 RB(0x40, 1), RB(0x40, 2), RB(0x00, 0),
571 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
572 D_MODULE(HCLK_SPI1, "hclk_spi1", CLK_REF_SYNC_D4, RB(0x40, 3),
573 RB(0x40, 4), RB(0x40, 5), RB(0x00, 0),
574 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
575 D_MODULE(HCLK_SPI2, "hclk_spi2", CLK_REF_SYNC_D4, RB(0x40, 6),
576 RB(0x40, 7), RB(0x40, 8), RB(0x00, 0),
577 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
578 D_MODULE(HCLK_SPI3, "hclk_spi3", CLK_REF_SYNC_D4, RB(0x40, 9),
579 RB(0x40, 10), RB(0x40, 11), RB(0x00, 0),
580 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
581 D_MODULE(HCLK_SPI4, "hclk_spi4", CLK_REF_SYNC_D4, RB(0x40, 12),
582 RB(0x40, 13), RB(0x40, 14), RB(0x00, 0),
583 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
584 D_MODULE(HCLK_SPI5, "hclk_spi5", CLK_REF_SYNC_D4, RB(0x40, 15),
585 RB(0x40, 16), RB(0x40, 17), RB(0x00, 0),
586 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
587 D_MODULE(HCLK_SWITCH, "hclk_switch", CLK_REF_SYNC_D4, RB(0x130, 0),
588 RB(0x00, 0), RB(0x130, 1), RB(0x00, 0),
589 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
590 D_MODULE(HCLK_SWITCH_RG, "hclk_switch_rg", CLK_REF_SYNC_D4, RB(0x188, 0),
591 RB(0x188, 1), RB(0x188, 2), RB(0x00, 0),
592 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
593 D_MODULE(HCLK_UART0, "hclk_uart0", CLK_REF_SYNC_D8, RB(0x34, 0),
594 RB(0x34, 1), RB(0x34, 2), RB(0x00, 0),
595 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
596 D_MODULE(HCLK_UART1, "hclk_uart1", CLK_REF_SYNC_D8, RB(0x34, 3),
597 RB(0x34, 4), RB(0x34, 5), RB(0x00, 0),
598 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
599 D_MODULE(HCLK_UART2, "hclk_uart2", CLK_REF_SYNC_D8, RB(0x34, 6),
600 RB(0x34, 7), RB(0x34, 8), RB(0x00, 0),
601 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
602 D_MODULE(HCLK_UART3, "hclk_uart3", CLK_REF_SYNC_D4, RB(0x40, 24),
603 RB(0x40, 25), RB(0x40, 26), RB(0x00, 0),
604 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
605 D_MODULE(HCLK_UART4, "hclk_uart4", CLK_REF_SYNC_D4, RB(0x40, 27),
606 RB(0x40, 28), RB(0x40, 29), RB(0x00, 0),
607 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
608 D_MODULE(HCLK_UART5, "hclk_uart5", CLK_REF_SYNC_D4, RB(0x44, 0),
609 RB(0x44, 1), RB(0x44, 2), RB(0x00, 0),
610 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
611 D_MODULE(HCLK_UART6, "hclk_uart6", CLK_REF_SYNC_D4, RB(0x44, 3),
612 RB(0x44, 4), RB(0x44, 5), RB(0x00, 0),
613 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
614 D_MODULE(HCLK_UART7, "hclk_uart7", CLK_REF_SYNC_D4, RB(0x44, 6),
615 RB(0x44, 7), RB(0x44, 8), RB(0x00, 0),
616 RB(0x00, 0), RB(0x00, 0), RB(0x00, 0)),
617 /*
618 * These are not hardware clocks, but are needed to handle the special
619 * case where we have a 'selector bit' that doesn't just change the
620 * parent for a clock, but also the gate it's supposed to use.
621 */
622 {
623 .index = R9A06G032_UART_GROUP_012,
624 .name = "uart_group_012",
625 .type = K_BITSEL,
626 .source = 1 + R9A06G032_DIV_UART,
627 /* R9A06G032_SYSCTRL_REG_PWRCTRL_PG0_0 */
628 .dual.sel = RB(0x34, 30),
629 .dual.group = 0,
630 },
631 {
632 .index = R9A06G032_UART_GROUP_34567,
633 .name = "uart_group_34567",
634 .type = K_BITSEL,
635 .source = 1 + R9A06G032_DIV_P2_PG,
636 /* R9A06G032_SYSCTRL_REG_PWRCTRL_PG1_PR2 */
637 .dual.sel = RB(0xec, 24),
638 .dual.group = 1,
639 },
640 D_UGATE(CLK_UART0, "clk_uart0", UART_GROUP_012, 0,
641 RB(0x34, 18), RB(0x34, 19), RB(0x34, 20), RB(0x34, 21)),
642 D_UGATE(CLK_UART1, "clk_uart1", UART_GROUP_012, 0,
643 RB(0x34, 22), RB(0x34, 23), RB(0x34, 24), RB(0x34, 25)),
644 D_UGATE(CLK_UART2, "clk_uart2", UART_GROUP_012, 0,
645 RB(0x34, 26), RB(0x34, 27), RB(0x34, 28), RB(0x34, 29)),
646 D_UGATE(CLK_UART3, "clk_uart3", UART_GROUP_34567, 1,
647 RB(0xec, 0), RB(0xec, 1), RB(0xec, 2), RB(0xec, 3)),
648 D_UGATE(CLK_UART4, "clk_uart4", UART_GROUP_34567, 1,
649 RB(0xec, 4), RB(0xec, 5), RB(0xec, 6), RB(0xec, 7)),
650 D_UGATE(CLK_UART5, "clk_uart5", UART_GROUP_34567, 1,
651 RB(0xec, 8), RB(0xec, 9), RB(0xec, 10), RB(0xec, 11)),
652 D_UGATE(CLK_UART6, "clk_uart6", UART_GROUP_34567, 1,
653 RB(0xec, 12), RB(0xec, 13), RB(0xec, 14), RB(0xec, 15)),
654 D_UGATE(CLK_UART7, "clk_uart7", UART_GROUP_34567, 1,
655 RB(0xec, 16), RB(0xec, 17), RB(0xec, 18), RB(0xec, 19)),
656};
657
658struct r9a06g032_priv {
659 struct regmap *regmap;
660 struct clk mclk;
661};
662
663static const struct r9a06g032_clkdesc *r9a06g032_clk_get(struct clk *clk)
664{
665 const unsigned long clkid = clk->id & 0xffff;
666 int i;
667
668 for (i = 0; i < ARRAY_SIZE(r9a06g032_clocks); i++) {
669 if (r9a06g032_clocks[i].index == clkid)
670 return &r9a06g032_clocks[i];
671 }
672
673 return NULL;
674}
675
676#define PARENT_ID (~0)
677
678static int r9a06g032_clk_get_parent(struct clk *clk, struct clk *parent)
679{
680 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
681
682 if (!desc)
683 return -ENOENT;
684
685 if (desc->source)
686 parent->id = desc->source - 1;
687 else
688 parent->id = PARENT_ID; /* Top-level clock */
689
690 parent->dev = clk->dev;
691
692 return 0;
693}
694
695static ulong r9a06g032_clk_get_parent_rate(struct clk *clk)
696{
697 struct clk parent;
698 unsigned long parent_rate;
699 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
700
701 if (r9a06g032_clk_get_parent(clk, &parent)) {
702 dev_dbg(clk->dev, "Failed to get parent clock for id=%lu\b", clk->id);
703 return 0;
704 }
705
706 if (parent.id == PARENT_ID)
707 parent_rate = clk_get_rate(&clocks->mclk);
708 else
709 parent_rate = clk_get_rate(&parent);
710
711 if (!parent_rate)
712 dev_dbg(clk->dev, "%s: parent_rate is zero\n", __func__);
713
714 return parent_rate;
715}
716
717/* register/bit pairs are encoded as an uint16_t */
718static void clk_rdesc_set(struct r9a06g032_priv *clocks,
719 struct regbit rb, unsigned int on)
720{
721 uint reg = rb.reg * 4;
722 uint bit = rb.bit;
723
724 if (!reg && !bit)
725 return;
726
727 uint mask = BIT(bit);
728 uint val = (!!on) << bit;
729
730 regmap_update_bits(clocks->regmap, reg, mask, val);
731}
732
733static int clk_rdesc_get(struct r9a06g032_priv *clocks,
734 struct regbit rb)
735{
736 uint reg = rb.reg * 4;
737 uint bit = rb.bit;
738 u32 val = 0;
739
740 regmap_read(clocks->regmap, reg, &val);
741
742 return !!(val & BIT(bit));
743}
744
745/*
746 * Cheating a little bit here: leverage the existing code to control the
747 * per-clock reset. It should really be handled by a reset controller instead.
748 */
749void clk_rzn1_reset_state(struct clk *clk, int on)
750{
751 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
752 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
753 const struct r9a06g032_gate *g;
754
755 assert(desc);
756 assert(desc->type == K_GATE);
757 g = &desc->gate;
758
759 clk_rdesc_set(clocks, g->reset, on);
760}
761
762/*
763 * This implements the R9A06G032 clock gate 'driver'. We cannot use the system's
764 * clock gate framework as the gates on the R9A06G032 have a special enabling
765 * sequence, therefore we use this little proxy.
766 */
767static int r9a06g032_clk_gate_set(struct clk *clk, int on)
768{
769 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
770 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
771 const struct r9a06g032_gate *g;
772
773 assert(desc);
774 assert(desc->type == K_GATE);
775 g = &desc->gate;
776
777 /* Enable or disable the clock */
778 clk_rdesc_set(clocks, g->gate, on);
779
780 /* De-assert reset */
781 clk_rdesc_set(clocks, g->reset, 1);
782
783 /* Hardware manual recommends 5us delay after enabling clock & reset */
784 udelay(5);
785
786 /* If the peripheral is memory mapped (i.e. an AXI slave), there is an
787 * associated SLVRDY bit in the System Controller that needs to be set
788 * so that the FlexWAY bus fabric passes on the read/write requests.
789 */
790 clk_rdesc_set(clocks, g->ready, on);
791
792 /* Clear 'Master Idle Request' bit */
793 clk_rdesc_set(clocks, g->midle, !on);
794
795 /* Note: We don't wait for FlexWAY Socket Connection signal */
796
797 return 0;
798}
799
800static int r9a06g032_clk_gate_enable(struct clk *clk)
801{
802 return r9a06g032_clk_gate_set(clk, 1);
803}
804
805static int r9a06g032_clk_gate_disable(struct clk *clk)
806{
807 return r9a06g032_clk_gate_set(clk, 0);
808}
809
810/*
811 * Fixed factor clock
812 */
813static ulong r9a06g032_ffc_get_rate(struct clk *clk)
814{
815 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
816 unsigned long parent_rate = r9a06g032_clk_get_parent_rate(clk);
817 unsigned long long rate;
818
819 rate = (unsigned long long)parent_rate * desc->mul;
820 rate = DIV_ROUND_UP(rate, desc->div);
821
822 return (ulong)rate;
823}
824
825/*
826 * This implements R9A06G032 clock divider 'driver'. This differs from the
827 * standard clk_divider because the set_rate method must also set b[31] to
828 * trigger the hardware rate change. In theory it should also wait for this
829 * bit to clear.
830 */
831static ulong r9a06g032_div_get_rate(struct clk *clk)
832{
833 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
834 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
835 unsigned long parent_rate = r9a06g032_clk_get_parent_rate(clk);
836 u32 div = 0;
837
838 regmap_read(clocks->regmap, 4 * desc->reg, &div);
839
840 if (div < desc->div_min)
841 div = desc->div_min;
842 else if (div > desc->div_max)
843 div = desc->div_max;
844 return DIV_ROUND_UP(parent_rate, div);
845}
846
847static ulong r9a06g032_div_set_rate(struct clk *clk, ulong rate)
848{
849 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
850 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
851 unsigned long parent_rate = r9a06g032_clk_get_parent_rate(clk);
852 size_t i;
853
854 /* + 1 to cope with rates that have the remainder dropped */
855 u32 div = DIV_ROUND_UP(parent_rate, rate + 1);
856
857 /* Clamp to allowable range */
858 if (div < desc->div_min)
859 div = desc->div_min;
860 else if (div > desc->div_max)
861 div = desc->div_max;
862
863 /* Limit to allowable divisors */
864 for (i = 0; i < ARRAY_SIZE(desc->div_table) - 2; i++) {
865 u16 div_m = desc->div_table[i];
866 u16 div_p = desc->div_table[i + 1];
867
868 if (!div_m || !div_p)
869 continue;
870
871 if (div >= div_m && div <= div_p) {
872 /*
873 * select the divider that generates
874 * the value closest to ideal frequency
875 */
876 u32 m = rate - DIV_ROUND_UP(parent_rate, div_m);
877 u32 p = DIV_ROUND_UP(parent_rate, div_p) - rate;
878
879 div = p >= m ? div_m : div_p;
880 }
881 }
882
883 dev_dbg(clk->dev, "%s clkid %lu rate %ld parent %ld div %d\n",
884 __func__, clk->id, rate, parent_rate, div);
885
886 /*
887 * Need to write the bit 31 with the divider value to
888 * latch it. Technically we should wait until it has been
889 * cleared too.
890 * TODO: Find whether this callback is sleepable, in case
891 * the hardware /does/ require some sort of spinloop here.
892 */
893 regmap_write(clocks->regmap, 4 * desc->reg, div | BIT(31));
894
895 return 0;
896}
897
898/*
899 * Dual gate. This handles toggling the approprate clock/reset bits,
900 * which depends on the mux setting above.
901 */
902static int r9a06g032_clk_dualgate_setenable(struct r9a06g032_priv *clocks,
903 const struct r9a06g032_clkdesc *desc,
904 int enable)
905{
906 u8 sel_bit = clk_rdesc_get(clocks, desc->dual.sel);
907 struct regbit gate[2] = { desc->dual.g1, desc->dual.g2 };
908 struct regbit reset[2] = { desc->dual.r1, desc->dual.r2 };
909
910 /* we always turn off the 'other' gate, regardless */
911 clk_rdesc_set(clocks, gate[!sel_bit], 0);
912 clk_rdesc_set(clocks, reset[!sel_bit], 1);
913
914 /* set the gate as requested */
915 clk_rdesc_set(clocks, gate[sel_bit], enable);
916 clk_rdesc_set(clocks, reset[sel_bit], 1);
917
918 return 0;
919}
920
921static int r9a06g032_clk_dualgate_enable(struct clk *clk)
922{
923 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
924 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
925
926 return r9a06g032_clk_dualgate_setenable(clocks, desc, 1);
927}
928
929static int r9a06g032_clk_dualgate_disable(struct clk *clk)
930{
931 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
932 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
933
934 return r9a06g032_clk_dualgate_setenable(clocks, desc, 0);
935}
936
937static int r9a06g032_clk_dualgate_is_enabled(struct clk *clk)
938{
939 struct r9a06g032_priv *clocks = dev_get_priv(clk->dev);
940 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
941 u8 sel_bit = clk_rdesc_get(clocks, desc->dual.sel);
942 struct regbit gate[2] = { desc->dual.g1, desc->dual.g2 };
943
944 return clk_rdesc_get(clocks, gate[sel_bit]);
945}
946
947/*
948 * Main clock driver
949 */
950static int r9a06g032_clk_enable(struct clk *clk)
951{
952 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
953
954 switch (desc->type) {
955 case K_GATE:
956 return r9a06g032_clk_gate_enable(clk);
957 case K_DUALGATE:
958 return r9a06g032_clk_dualgate_enable(clk);
959 default:
960 dev_dbg(clk->dev, "ERROR: unhandled type=%d\n", desc->type);
961 break;
962 }
963
964 return 0;
965}
966
967static int r9a06g032_clk_disable(struct clk *clk)
968{
969 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
970
971 switch (desc->type) {
972 case K_GATE:
973 return r9a06g032_clk_gate_disable(clk);
974 case K_DUALGATE:
975 return r9a06g032_clk_dualgate_disable(clk);
976 default:
977 dev_dbg(clk->dev, "ERROR: unhandled type=%d\n", desc->type);
978 break;
979 }
980
981 return 0;
982}
983
984static ulong r9a06g032_clk_get_rate(struct clk *clk)
985{
986 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
987 ulong ret = 0;
988
989 assert(desc);
990
991 switch (desc->type) {
992 case K_FFC:
993 ret = r9a06g032_ffc_get_rate(clk);
994 break;
995 case K_GATE:
996 ret = r9a06g032_clk_get_parent_rate(clk);
997 break;
998 case K_DIV:
999 ret = r9a06g032_div_get_rate(clk);
1000 break;
1001 case K_BITSEL:
1002 /*
1003 * Look at the mux to determine parent.
1004 * 0 means it is coming from UART DIV (group 012 or 34567)
1005 * 1 means it is coming from USB_PLL (fixed at 48MHz)
1006 */
1007 if (r9a06g032_clk_dualgate_is_enabled(clk)) {
1008 struct clk usb_clk = { .id = R9A06G032_CLK_PLL_USB };
1009
1010 ret = r9a06g032_clk_get_parent_rate(&usb_clk);
1011 } else {
1012 ret = r9a06g032_clk_get_parent_rate(clk);
1013 }
1014 break;
1015 case K_DUALGATE:
1016 ret = r9a06g032_clk_get_parent_rate(clk);
1017 break;
1018 }
1019
1020 return ret;
1021}
1022
1023static ulong r9a06g032_clk_set_rate(struct clk *clk, ulong rate)
1024{
1025 const struct r9a06g032_clkdesc *desc = r9a06g032_clk_get(clk);
1026 ulong ret = 0;
1027
1028 assert(desc);
1029
1030 switch (desc->type) {
1031 case K_DIV:
1032 ret = r9a06g032_div_set_rate(clk, rate);
1033 break;
1034 default:
1035 dev_dbg(clk->dev, "ERROR: not implemented for %d\n", desc->type);
1036 };
1037
1038 return ret;
1039}
1040
1041static int r9a06g032_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
1042{
1043 if (args->args_count != 1) {
1044 dev_dbg(clk->dev, "Invalid args_count: %d\n", args->args_count);
1045 return -EINVAL;
1046 }
1047
1048 clk->id = args->args[0];
1049
1050 return 0;
1051}
1052
1053static const struct clk_ops r9a06g032_clk_ops = {
1054 .enable = r9a06g032_clk_enable,
1055 .disable = r9a06g032_clk_disable,
1056 .get_rate = r9a06g032_clk_get_rate,
1057 .set_rate = r9a06g032_clk_set_rate,
1058 .of_xlate = r9a06g032_clk_of_xlate,
1059};
1060
1061/* Reset Enable Register */
1062#define RZN1_SYSCTRL_REG_RSTEN 288 /* 0x120*/
1063#define RZN1_SYSCTRL_REG_RSTEN_MRESET_EN BIT(0)
1064#define RZN1_SYSCTRL_REG_RSTEN_WDA7RST_CA7_0_EN BIT(1)
1065#define RZN1_SYSCTRL_REG_RSTEN_WDA7RST_CA7_1_EN BIT(2)
1066#define RZN1_SYSCTRL_REG_RSTEN_WDM3RST_EN BIT(3)
1067#define RZN1_SYSCTRL_REG_RSTEN_CM3LOCKUPRST_EN BIT(4)
1068#define RZN1_SYSCTRL_REG_RSTEN_CM3SYSRESET_EN BIT(5)
1069#define RZN1_SYSCTRL_REG_RSTEN_SWRST_EN BIT(6)
1070
1071static int r9a06g032_clk_probe(struct udevice *dev)
1072{
1073 struct r9a06g032_priv *priv = dev_get_priv(dev);
1074
1075 priv->regmap = syscon_regmap_lookup_by_phandle(dev, "regmap");
1076 if (IS_ERR(priv->regmap)) {
1077 dev_dbg(dev, "unable to find regmap\n");
1078 return PTR_ERR(priv->regmap);
1079 }
1080
1081 /* Enable S/W reset */
1082 regmap_write(priv->regmap, RZN1_SYSCTRL_REG_RSTEN,
1083 RZN1_SYSCTRL_REG_RSTEN_MRESET_EN |
1084 RZN1_SYSCTRL_REG_RSTEN_SWRST_EN);
1085
1086 /* Get master clock */
1087 return clk_get_by_name(dev, "mclk", &priv->mclk);
1088}
1089
1090static const struct udevice_id r9a06g032_clk_ids[] = {
1091 { .compatible = "renesas,r9a06g032-sysctrl" },
1092 { }
1093};
1094
1095U_BOOT_DRIVER(clk_r9a06g032) = {
1096 .name = "clk_r9a06g032",
1097 .id = UCLASS_CLK,
1098 .of_match = r9a06g032_clk_ids,
1099 .priv_auto = sizeof(struct r9a06g032_priv),
1100 .ops = &r9a06g032_clk_ops,
1101 .probe = &r9a06g032_clk_probe,
1102 .flags = DM_FLAG_PRE_RELOC,
1103};