1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SDIO clock driver for RP1
5 * Copyright (C) 2023 Raspberry Pi Ltd.
10 #include <linux/clk-provider.h>
11 #include <linux/module.h>
12 #include <linux/platform_device.h>
15 #define MODE 0x00000000
16 #define MODE_BITS 0x70030000
17 #define MODE_RESET 0x00000000
18 // Field : MODE_STEPS_PER_CYCLE
19 #define MODE_STEPS_PER_CYCLE_RESET 0x0
20 #define MODE_STEPS_PER_CYCLE_BITS 0x70000000
21 #define MODE_STEPS_PER_CYCLE_MSB 30
22 #define MODE_STEPS_PER_CYCLE_LSB 28
23 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_20 0x0
24 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_10 0x1
25 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_16 0x2
26 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_8 0x3
27 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_12 0x4
28 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_6 0x5
29 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_5 0x6
30 #define MODE_STEPS_PER_CYCLE_VALUE_STEPS_4 0x7
31 // Field : MODE_SRC_SEL
32 #define MODE_SRC_SEL_RESET 0x0
33 #define MODE_SRC_SEL_BITS 0x00030000
34 #define MODE_SRC_SEL_MSB 17
35 #define MODE_SRC_SEL_LSB 16
36 #define MODE_SRC_SEL_VALUE_STOP 0x0
37 #define MODE_SRC_SEL_VALUE_CLK_ALT_SRC 0x1
38 #define MODE_SRC_SEL_VALUE_PLL_SYS_VCO 0x2
39 #define MODE_SRC_SEL_VALUE_PLL_SYS_VCO_AGAIN 0x3
41 #define FROMIP 0x00000004
42 #define FROMIP_BITS 0x0f9713ff
43 #define FROMIP_RESET 0x00000000
44 // Field : FROMIP_TUNING_CCLK_SEL
45 #define FROMIP_TUNING_CCLK_SEL_RESET 0x0
46 #define FROMIP_TUNING_CCLK_SEL_BITS 0x0f000000
47 #define FROMIP_TUNING_CCLK_SEL_MSB 27
48 #define FROMIP_TUNING_CCLK_SEL_LSB 24
49 // Field : FROMIP_TUNING_CCLK_UPDATE
50 #define FROMIP_TUNING_CCLK_UPDATE_RESET 0x0
51 #define FROMIP_TUNING_CCLK_UPDATE_BITS 0x00800000
52 #define FROMIP_TUNING_CCLK_UPDATE_MSB 23
53 #define FROMIP_TUNING_CCLK_UPDATE_LSB 23
54 // Field : FROMIP_SAMPLE_CCLK_SEL
55 #define FROMIP_SAMPLE_CCLK_SEL_RESET 0x0
56 #define FROMIP_SAMPLE_CCLK_SEL_BITS 0x00100000
57 #define FROMIP_SAMPLE_CCLK_SEL_MSB 20
58 #define FROMIP_SAMPLE_CCLK_SEL_LSB 20
59 // Field : FROMIP_CLK2CARD_ON
60 #define FROMIP_CLK2CARD_ON_RESET 0x0
61 #define FROMIP_CLK2CARD_ON_BITS 0x00040000
62 #define FROMIP_CLK2CARD_ON_MSB 18
63 #define FROMIP_CLK2CARD_ON_LSB 18
64 // Field : FROMIP_CARD_CLK_STABLE
65 #define FROMIP_CARD_CLK_STABLE_RESET 0x0
66 #define FROMIP_CARD_CLK_STABLE_BITS 0x00020000
67 #define FROMIP_CARD_CLK_STABLE_MSB 17
68 #define FROMIP_CARD_CLK_STABLE_LSB 17
69 // Field : FROMIP_CARD_CLK_EN
70 #define FROMIP_CARD_CLK_EN_RESET 0x0
71 #define FROMIP_CARD_CLK_EN_BITS 0x00010000
72 #define FROMIP_CARD_CLK_EN_MSB 16
73 #define FROMIP_CARD_CLK_EN_LSB 16
74 // Field : FROMIP_CLK_GEN_SEL
75 #define FROMIP_CLK_GEN_SEL_RESET 0x0
76 #define FROMIP_CLK_GEN_SEL_BITS 0x00001000
77 #define FROMIP_CLK_GEN_SEL_MSB 12
78 #define FROMIP_CLK_GEN_SEL_LSB 12
79 // Field : FROMIP_FREQ_SEL
80 #define FROMIP_FREQ_SEL_RESET 0x000
81 #define FROMIP_FREQ_SEL_BITS 0x000003ff
82 #define FROMIP_FREQ_SEL_MSB 9
83 #define FROMIP_FREQ_SEL_LSB 0
85 #define LOCAL 0x00000008
86 #define LOCAL_BITS 0x1f9713ff
87 #define LOCAL_RESET 0x00000000
88 // Field : LOCAL_TUNING_CCLK_SEL
89 #define LOCAL_TUNING_CCLK_SEL_RESET 0x00
90 #define LOCAL_TUNING_CCLK_SEL_BITS 0x1f000000
91 #define LOCAL_TUNING_CCLK_SEL_MSB 28
92 #define LOCAL_TUNING_CCLK_SEL_LSB 24
93 // Field : LOCAL_TUNING_CCLK_UPDATE
94 #define LOCAL_TUNING_CCLK_UPDATE_RESET 0x0
95 #define LOCAL_TUNING_CCLK_UPDATE_BITS 0x00800000
96 #define LOCAL_TUNING_CCLK_UPDATE_MSB 23
97 #define LOCAL_TUNING_CCLK_UPDATE_LSB 23
98 // Field : LOCAL_SAMPLE_CCLK_SEL
99 #define LOCAL_SAMPLE_CCLK_SEL_RESET 0x0
100 #define LOCAL_SAMPLE_CCLK_SEL_BITS 0x00100000
101 #define LOCAL_SAMPLE_CCLK_SEL_MSB 20
102 #define LOCAL_SAMPLE_CCLK_SEL_LSB 20
103 // Field : LOCAL_CLK2CARD_ON
104 #define LOCAL_CLK2CARD_ON_RESET 0x0
105 #define LOCAL_CLK2CARD_ON_BITS 0x00040000
106 #define LOCAL_CLK2CARD_ON_MSB 18
107 #define LOCAL_CLK2CARD_ON_LSB 18
108 // Field : LOCAL_CARD_CLK_STABLE
109 #define LOCAL_CARD_CLK_STABLE_RESET 0x0
110 #define LOCAL_CARD_CLK_STABLE_BITS 0x00020000
111 #define LOCAL_CARD_CLK_STABLE_MSB 17
112 #define LOCAL_CARD_CLK_STABLE_LSB 17
113 // Field : LOCAL_CARD_CLK_EN
114 #define LOCAL_CARD_CLK_EN_RESET 0x0
115 #define LOCAL_CARD_CLK_EN_BITS 0x00010000
116 #define LOCAL_CARD_CLK_EN_MSB 16
117 #define LOCAL_CARD_CLK_EN_LSB 16
118 // Field : LOCAL_CLK_GEN_SEL
119 #define LOCAL_CLK_GEN_SEL_RESET 0x0
120 #define LOCAL_CLK_GEN_SEL_BITS 0x00001000
121 #define LOCAL_CLK_GEN_SEL_MSB 12
122 #define LOCAL_CLK_GEN_SEL_LSB 12
123 #define LOCAL_CLK_GEN_SEL_VALUE_PROGCLOCKMODE 0x0
124 #define LOCAL_CLK_GEN_SEL_VALUE_DIVCLOCKMODE 0x1
125 // Field : LOCAL_FREQ_SEL
126 #define LOCAL_FREQ_SEL_RESET 0x000
127 #define LOCAL_FREQ_SEL_BITS 0x000003ff
128 #define LOCAL_FREQ_SEL_MSB 9
129 #define LOCAL_FREQ_SEL_LSB 0
130 // Register : USE_LOCAL
131 #define USE_LOCAL 0x0000000c
132 #define USE_LOCAL_BITS 0x01951001
133 #define USE_LOCAL_RESET 0x00000000
134 // Field : USE_LOCAL_TUNING_CCLK_SEL
135 #define USE_LOCAL_TUNING_CCLK_SEL_RESET 0x0
136 #define USE_LOCAL_TUNING_CCLK_SEL_BITS 0x01000000
137 #define USE_LOCAL_TUNING_CCLK_SEL_MSB 24
138 #define USE_LOCAL_TUNING_CCLK_SEL_LSB 24
139 // Field : USE_LOCAL_TUNING_CCLK_UPDATE
140 #define USE_LOCAL_TUNING_CCLK_UPDATE_RESET 0x0
141 #define USE_LOCAL_TUNING_CCLK_UPDATE_BITS 0x00800000
142 #define USE_LOCAL_TUNING_CCLK_UPDATE_MSB 23
143 #define USE_LOCAL_TUNING_CCLK_UPDATE_LSB 23
144 // Field : USE_LOCAL_SAMPLE_CCLK_SEL
145 #define USE_LOCAL_SAMPLE_CCLK_SEL_RESET 0x0
146 #define USE_LOCAL_SAMPLE_CCLK_SEL_BITS 0x00100000
147 #define USE_LOCAL_SAMPLE_CCLK_SEL_MSB 20
148 #define USE_LOCAL_SAMPLE_CCLK_SEL_LSB 20
149 // Field : USE_LOCAL_CLK2CARD_ON
150 #define USE_LOCAL_CLK2CARD_ON_RESET 0x0
151 #define USE_LOCAL_CLK2CARD_ON_BITS 0x00040000
152 #define USE_LOCAL_CLK2CARD_ON_MSB 18
153 #define USE_LOCAL_CLK2CARD_ON_LSB 18
154 // Field : USE_LOCAL_CARD_CLK_EN
155 #define USE_LOCAL_CARD_CLK_EN_RESET 0x0
156 #define USE_LOCAL_CARD_CLK_EN_BITS 0x00010000
157 #define USE_LOCAL_CARD_CLK_EN_MSB 16
158 #define USE_LOCAL_CARD_CLK_EN_LSB 16
159 // Field : USE_LOCAL_CLK_GEN_SEL
160 #define USE_LOCAL_CLK_GEN_SEL_RESET 0x0
161 #define USE_LOCAL_CLK_GEN_SEL_BITS 0x00001000
162 #define USE_LOCAL_CLK_GEN_SEL_MSB 12
163 #define USE_LOCAL_CLK_GEN_SEL_LSB 12
164 // Field : USE_LOCAL_FREQ_SEL
165 #define USE_LOCAL_FREQ_SEL_RESET 0x0
166 #define USE_LOCAL_FREQ_SEL_BITS 0x00000001
167 #define USE_LOCAL_FREQ_SEL_MSB 0
168 #define USE_LOCAL_FREQ_SEL_LSB 0
169 // Register : SD_DELAY
170 #define SD_DELAY 0x00000010
171 #define SD_DELAY_BITS 0x0000001f
172 #define SD_DELAY_RESET 0x00000000
173 // Field : SD_DELAY_STEPS
174 #define SD_DELAY_STEPS_RESET 0x00
175 #define SD_DELAY_STEPS_BITS 0x0000001f
176 #define SD_DELAY_STEPS_MSB 4
177 #define SD_DELAY_STEPS_LSB 0
178 // Register : RX_DELAY
179 #define RX_DELAY 0x00000014
180 #define RX_DELAY_BITS 0x19f3331f
181 #define RX_DELAY_RESET 0x00000000
182 // Field : RX_DELAY_BYPASS
183 #define RX_DELAY_BYPASS_RESET 0x0
184 #define RX_DELAY_BYPASS_BITS 0x10000000
185 #define RX_DELAY_BYPASS_MSB 28
186 #define RX_DELAY_BYPASS_LSB 28
187 // Field : RX_DELAY_FAIL_ACTUAL
188 #define RX_DELAY_FAIL_ACTUAL_RESET 0x0
189 #define RX_DELAY_FAIL_ACTUAL_BITS 0x08000000
190 #define RX_DELAY_FAIL_ACTUAL_MSB 27
191 #define RX_DELAY_FAIL_ACTUAL_LSB 27
192 // Field : RX_DELAY_ACTUAL
193 #define RX_DELAY_ACTUAL_RESET 0x00
194 #define RX_DELAY_ACTUAL_BITS 0x01f00000
195 #define RX_DELAY_ACTUAL_MSB 24
196 #define RX_DELAY_ACTUAL_LSB 20
197 // Field : RX_DELAY_OFFSET
198 #define RX_DELAY_OFFSET_RESET 0x0
199 #define RX_DELAY_OFFSET_BITS 0x00030000
200 #define RX_DELAY_OFFSET_MSB 17
201 #define RX_DELAY_OFFSET_LSB 16
202 // Field : RX_DELAY_OVERFLOW
203 #define RX_DELAY_OVERFLOW_RESET 0x0
204 #define RX_DELAY_OVERFLOW_BITS 0x00003000
205 #define RX_DELAY_OVERFLOW_MSB 13
206 #define RX_DELAY_OVERFLOW_LSB 12
207 #define RX_DELAY_OVERFLOW_VALUE_ALLOW 0x0
208 #define RX_DELAY_OVERFLOW_VALUE_CLAMP 0x1
209 #define RX_DELAY_OVERFLOW_VALUE_FAIL 0x2
210 // Field : RX_DELAY_MAP
211 #define RX_DELAY_MAP_RESET 0x0
212 #define RX_DELAY_MAP_BITS 0x00000300
213 #define RX_DELAY_MAP_MSB 9
214 #define RX_DELAY_MAP_LSB 8
215 #define RX_DELAY_MAP_VALUE_DIRECT 0x0
216 #define RX_DELAY_MAP_VALUE 0x1
217 #define RX_DELAY_MAP_VALUE_STRETCH 0x2
218 // Field : RX_DELAY_FIXED
219 #define RX_DELAY_FIXED_RESET 0x00
220 #define RX_DELAY_FIXED_BITS 0x0000001f
221 #define RX_DELAY_FIXED_MSB 4
222 #define RX_DELAY_FIXED_LSB 0
224 #define NDIV 0x00000018
225 #define NDIV_BITS 0x1fff0000
226 #define NDIV_RESET 0x00110000
228 #define NDIV_DIVB_RESET 0x001
229 #define NDIV_DIVB_BITS 0x1ff00000
230 #define NDIV_DIVB_MSB 28
231 #define NDIV_DIVB_LSB 20
233 #define NDIV_DIVA_RESET 0x1
234 #define NDIV_DIVA_BITS 0x000f0000
235 #define NDIV_DIVA_MSB 19
236 #define NDIV_DIVA_LSB 16
238 #define CS 0x0000001c
239 #define CS_BITS 0x00111101
240 #define CS_RESET 0x00000001
241 // Field : CS_RX_DEL_UPDATED
242 #define CS_RX_DEL_UPDATED_RESET 0x0
243 #define CS_RX_DEL_UPDATED_BITS 0x00100000
244 #define CS_RX_DEL_UPDATED_MSB 20
245 #define CS_RX_DEL_UPDATED_LSB 20
246 // Field : CS_RX_CLK_RUNNING
247 #define CS_RX_CLK_RUNNING_RESET 0x0
248 #define CS_RX_CLK_RUNNING_BITS 0x00010000
249 #define CS_RX_CLK_RUNNING_MSB 16
250 #define CS_RX_CLK_RUNNING_LSB 16
251 // Field : CS_SD_CLK_RUNNING
252 #define CS_SD_CLK_RUNNING_RESET 0x0
253 #define CS_SD_CLK_RUNNING_BITS 0x00001000
254 #define CS_SD_CLK_RUNNING_MSB 12
255 #define CS_SD_CLK_RUNNING_LSB 12
256 // Field : CS_TX_CLK_RUNNING
257 #define CS_TX_CLK_RUNNING_RESET 0x0
258 #define CS_TX_CLK_RUNNING_BITS 0x00000100
259 #define CS_TX_CLK_RUNNING_MSB 8
260 #define CS_TX_CLK_RUNNING_LSB 8
262 #define CS_RESET_RESET 0x1
263 #define CS_RESET_BITS 0x00000001
264 #define CS_RESET_MSB 0
265 #define CS_RESET_LSB 0
267 #define FPGA_SRC_RATE 400000000
269 /* Base number of steps to delay in relation to tx clk.
270 * The relationship of the 3 clocks are as follows:
271 * tx_clk: This clock is provided to the controller. Data is sent out
272 * to the pads using this clock.
273 * sd_clk: This clock is sent out to the card.
274 * rx_clk: This clock is used to sample the data coming back from the card.
275 * This may need to be several steps ahead of the tx_clk. The default rx delay
276 * is used as a base delay, and can be further adjusted by the sd host
277 * controller during the tuning process if using a DDR50 or faster SD card
280 * PRJY-1813 - the default SD clock delay needs to be set to ~60% of the total
281 * number of steps to meet tISU (>6ns) and tIH (>2ns) in high-speed mode.
282 * On FPGA this means delay SDCLK by 5, and sample RX with a delay of 6.
284 #define DEFAULT_RX_DELAY 6
285 #define DEFAULT_SD_DELAY 5
287 struct rp1_sdio_clkgen {
290 /* Source clock. Either PLL VCO or fixed freq on FPGA */
292 /* Desired base frequency. Max freq card can go */
293 struct clk *base_clk;
298 /* Starting value of local register before changing freq */
302 static inline void clkgen_write(struct rp1_sdio_clkgen *clkgen, u32 reg, u32 val)
304 dev_dbg(clkgen->dev, "%s: write reg 0x%x: 0x%x\n", __func__, reg, val);
305 writel(val, clkgen->regs + reg);
308 static inline u32 clkgen_read(struct rp1_sdio_clkgen *clkgen, u32 reg)
310 u32 val = readl(clkgen->regs + reg);
312 dev_dbg(clkgen->dev, "%s: read reg 0x%x: 0x%x\n", __func__, reg, val);
316 static int get_steps(unsigned int steps)
321 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_4;
323 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_5;
325 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_6;
327 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_8;
328 else if (steps == 10)
329 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_10;
330 else if (steps == 12)
331 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_12;
332 else if (steps == 16)
333 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_16;
334 else if (steps == 20)
335 ret = MODE_STEPS_PER_CYCLE_VALUE_STEPS_20;
339 static int rp1_sdio_clk_init(struct rp1_sdio_clkgen *clkgen)
341 unsigned long src_rate = clk_get_rate(clkgen->src_clk);
342 unsigned long base_rate = clk_get_rate(clkgen->base_clk);
343 unsigned int steps = src_rate / base_rate;
347 dev_dbg(clkgen->dev, "init: src_rate %lu, base_rate %lu, steps %d\n",
348 src_rate, base_rate, steps);
350 /* Assert reset while we set up clkgen */
351 clkgen_write(clkgen, CS, CS_RESET_BITS);
353 /* Pick clock source */
354 if (src_rate == FPGA_SRC_RATE) {
356 reg |= MODE_SRC_SEL_VALUE_CLK_ALT_SRC << MODE_SRC_SEL_LSB;
358 /* Assume we are using PLL SYS VCO */
359 reg |= MODE_SRC_SEL_VALUE_PLL_SYS_VCO << MODE_SRC_SEL_LSB;
362 /* How many delay steps are available in one cycle for this source */
363 steps_value = get_steps(steps);
364 if (steps_value < 0) {
365 dev_err(clkgen->dev, "Invalid step value: %d\n", steps);
368 reg |= steps_value << MODE_STEPS_PER_CYCLE_LSB;
370 /* Mode register is done now*/
371 clkgen_write(clkgen, MODE, reg);
373 /* Now set delay mode */
374 /* Clamp value if out of range rx delay is used */
375 reg = RX_DELAY_OVERFLOW_VALUE_CLAMP << RX_DELAY_OVERFLOW_LSB;
376 /* SD tuning bus goes from 0x0 to 0xf but we don't necessarily have that
377 * many steps available depending on the source so map 0x0 -> 0xf to one
380 reg |= RX_DELAY_MAP_VALUE_STRETCH << RX_DELAY_MAP_LSB;
382 /* Default RX delay */
383 dev_dbg(clkgen->dev, "default rx delay %d\n", DEFAULT_RX_DELAY);
384 reg |= (DEFAULT_RX_DELAY & RX_DELAY_FIXED_BITS) << RX_DELAY_FIXED_LSB;
385 clkgen_write(clkgen, RX_DELAY, reg);
387 /* Default SD delay */
388 dev_dbg(clkgen->dev, "default sd delay %d\n", DEFAULT_SD_DELAY);
389 reg = (DEFAULT_SD_DELAY & SD_DELAY_STEPS_BITS) << SD_DELAY_STEPS_LSB;
390 clkgen_write(clkgen, SD_DELAY, reg);
392 /* We select freq, we turn on tx clock, we turn on sd clk,
393 * we pick clock generator mode
395 reg = USE_LOCAL_FREQ_SEL_BITS | USE_LOCAL_CARD_CLK_EN_BITS |
396 USE_LOCAL_CLK2CARD_ON_BITS | USE_LOCAL_CLK_GEN_SEL_BITS;
397 clkgen_write(clkgen, USE_LOCAL, reg);
399 /* Deassert reset. Reset bit is only writable bit of CS
400 * reg so fine to write a 0.
402 clkgen_write(clkgen, CS, 0);
408 (CS_TX_CLK_RUNNING_BITS | CS_RX_CLK_RUNNING_BITS | \
409 CS_SD_CLK_RUNNING_BITS)
410 static int rp1_sdio_clk_is_prepared(struct clk_hw *hw)
412 struct rp1_sdio_clkgen *clkgen =
413 container_of(hw, struct rp1_sdio_clkgen, hw);
416 dev_dbg(clkgen->dev, "is_prepared\n");
417 status = clkgen_read(clkgen, CS);
418 return ((status & RUNNING) == RUNNING);
421 /* Can define an additional divider if an sd card isn't working at full speed */
422 /* #define SLOWDOWN 3 */
424 static unsigned long rp1_sdio_clk_get_rate(struct clk_hw *hw,
425 unsigned long parent_rate)
427 /* Get the current rate */
428 struct rp1_sdio_clkgen *clkgen =
429 container_of(hw, struct rp1_sdio_clkgen, hw);
430 unsigned long actual_rate = 0;
436 tmp = clkgen_read(clkgen, LOCAL);
437 if ((tmp & LOCAL_CLK2CARD_ON_BITS) == 0) {
438 dev_dbg(clkgen->dev, "get_rate 0\n");
442 tmp = clkgen_read(clkgen, NDIV);
443 ndiv_diva = (tmp & NDIV_DIVA_BITS) >> NDIV_DIVA_LSB;
444 ndiv_divb = (tmp & NDIV_DIVB_BITS) >> NDIV_DIVB_LSB;
445 div = ndiv_diva * ndiv_divb;
446 actual_rate = (clk_get_rate(clkgen->base_clk) / div);
449 actual_rate *= SLOWDOWN;
452 dev_dbg(clkgen->dev, "get_rate. ndiv_diva %d, ndiv_divb %d = %lu\n",
453 ndiv_diva, ndiv_divb, actual_rate);
458 static int rp1_sdio_clk_set_rate(struct clk_hw *hw, unsigned long rate,
459 unsigned long parent_rate)
461 struct rp1_sdio_clkgen *clkgen =
462 container_of(hw, struct rp1_sdio_clkgen, hw);
466 dev_dbg(clkgen->dev, "set_rate %lu\n", rate);
469 /* Keep tx clock running */
470 clkgen_write(clkgen, LOCAL, LOCAL_CARD_CLK_EN_BITS);
478 div = (clk_get_rate(clkgen->base_clk) / rate) - 1;
479 reg = LOCAL_CLK_GEN_SEL_BITS | LOCAL_CARD_CLK_EN_BITS |
480 LOCAL_CLK2CARD_ON_BITS | (div << LOCAL_FREQ_SEL_LSB);
481 clkgen_write(clkgen, LOCAL, reg);
486 #define MAX_NDIV (256 * 8)
487 static int rp1_sdio_clk_determine_rate(struct clk_hw *hw,
488 struct clk_rate_request *req)
491 struct rp1_sdio_clkgen *clkgen =
492 container_of(hw, struct rp1_sdio_clkgen, hw);
493 unsigned long base_rate = clk_get_rate(clkgen->base_clk);
496 /* What is the actual rate I can get if I request xyz */
498 div = min((u32)(base_rate / req->rate), (u32)MAX_NDIV);
499 rate = base_rate / div;
501 dev_dbg(clkgen->dev, "determine_rate %lu: %lu / %d = %lu\n",
502 req->rate, base_rate, div, rate);
505 dev_dbg(clkgen->dev, "determine_rate %lu: %lu\n", req->rate,
512 static const struct clk_ops rp1_sdio_clk_ops = {
513 .is_prepared = rp1_sdio_clk_is_prepared,
514 .recalc_rate = rp1_sdio_clk_get_rate,
515 .set_rate = rp1_sdio_clk_set_rate,
516 .determine_rate = rp1_sdio_clk_determine_rate,
519 static int rp1_sdio_clk_probe(struct platform_device *pdev)
521 struct device_node *node = pdev->dev.of_node;
522 struct rp1_sdio_clkgen *clkgen;
524 struct clk_init_data init = {};
527 clkgen = devm_kzalloc(&pdev->dev, sizeof(*clkgen), GFP_KERNEL);
530 platform_set_drvdata(pdev, clkgen);
532 clkgen->dev = &pdev->dev;
535 clkgen->src_clk = devm_clk_get(&pdev->dev, "src");
536 if (IS_ERR(clkgen->src_clk)) {
537 int err = PTR_ERR(clkgen->src_clk);
539 dev_err(&pdev->dev, "failed to get src clk: %d\n", err);
543 /* Desired maximum output freq (i.e. base freq) */
544 clkgen->base_clk = devm_clk_get(&pdev->dev, "base");
545 if (IS_ERR(clkgen->base_clk)) {
546 int err = PTR_ERR(clkgen->base_clk);
548 dev_err(&pdev->dev, "failed to get base clk: %d\n", err);
552 regs = devm_platform_ioremap_resource(pdev, 0);
554 return PTR_ERR(regs);
556 init.name = node->name;
557 init.ops = &rp1_sdio_clk_ops;
558 init.flags = CLK_GET_RATE_NOCACHE;
560 clkgen->hw.init = &init;
563 dev_info(&pdev->dev, "loaded %s\n", init.name);
565 ret = devm_clk_hw_register(&pdev->dev, &clkgen->hw);
569 ret = of_clk_add_hw_provider(node, of_clk_hw_simple_get, &clkgen->hw);
573 ret = rp1_sdio_clk_init(clkgen);
577 static int rp1_sdio_clk_remove(struct platform_device *pdev)
582 static const struct of_device_id rp1_sdio_clk_dt_ids[] = {
583 { .compatible = "raspberrypi,rp1-sdio-clk", },
586 MODULE_DEVICE_TABLE(of, rp1_sdio_clk_dt_ids);
588 static struct platform_driver rp1_sdio_clk_driver = {
589 .probe = rp1_sdio_clk_probe,
590 .remove = rp1_sdio_clk_remove,
592 .name = "rp1-sdio-clk",
593 .of_match_table = rp1_sdio_clk_dt_ids,
596 module_platform_driver(rp1_sdio_clk_driver);
598 MODULE_AUTHOR("Liam Fraser <liam@raspberrypi.com>");
599 MODULE_DESCRIPTION("RP1 SDIO clock driver");
600 MODULE_LICENSE("GPL");