1 /*
2 * Copyright (C) 2013 Boris BREZILLON <b.brezillon@overkiz.com>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 */
10
11 #include <linux/clk-provider.h>
12 #include <linux/clkdev.h>
13 #include <linux/clk/at91_pmc.h>
14 #include <linux/of.h>
15 #include <linux/of_address.h>
16 #include <linux/io.h>
17
18 #include "pmc.h"
19
20 #define USB_SOURCE_MAX 2
21
22 #define SAM9X5_USB_DIV_SHIFT 8
23 #define SAM9X5_USB_MAX_DIV 0xf
24
25 #define RM9200_USB_DIV_SHIFT 28
26 #define RM9200_USB_DIV_TAB_SIZE 4
27
28 struct at91sam9x5_clk_usb {
29 struct clk_hw hw;
30 struct at91_pmc *pmc;
31 };
32
33 #define to_at91sam9x5_clk_usb(hw) \
34 container_of(hw, struct at91sam9x5_clk_usb, hw)
35
36 struct at91rm9200_clk_usb {
37 struct clk_hw hw;
38 struct at91_pmc *pmc;
39 u32 divisors[4];
40 };
41
42 #define to_at91rm9200_clk_usb(hw) \
43 container_of(hw, struct at91rm9200_clk_usb, hw)
44
at91sam9x5_clk_usb_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)45 static unsigned long at91sam9x5_clk_usb_recalc_rate(struct clk_hw *hw,
46 unsigned long parent_rate)
47 {
48 u32 tmp;
49 u8 usbdiv;
50 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
51 struct at91_pmc *pmc = usb->pmc;
52
53 tmp = pmc_read(pmc, AT91_PMC_USB);
54 usbdiv = (tmp & AT91_PMC_OHCIUSBDIV) >> SAM9X5_USB_DIV_SHIFT;
55
56 return DIV_ROUND_CLOSEST(parent_rate, (usbdiv + 1));
57 }
58
at91sam9x5_clk_usb_determine_rate(struct clk_hw * hw,unsigned long rate,unsigned long * best_parent_rate,struct clk ** best_parent_hw)59 static long at91sam9x5_clk_usb_determine_rate(struct clk_hw *hw,
60 unsigned long rate,
61 unsigned long *best_parent_rate,
62 struct clk **best_parent_hw)
63 {
64 struct clk *parent = NULL;
65 long best_rate = -EINVAL;
66 unsigned long tmp_rate;
67 int best_diff = -1;
68 int tmp_diff;
69 int i;
70
71 for (i = 0; i < __clk_get_num_parents(hw->clk); i++) {
72 int div;
73
74 parent = clk_get_parent_by_index(hw->clk, i);
75 if (!parent)
76 continue;
77
78 for (div = 1; div < SAM9X5_USB_MAX_DIV + 2; div++) {
79 unsigned long tmp_parent_rate;
80
81 tmp_parent_rate = rate * div;
82 tmp_parent_rate = __clk_round_rate(parent,
83 tmp_parent_rate);
84 tmp_rate = DIV_ROUND_CLOSEST(tmp_parent_rate, div);
85 if (tmp_rate < rate)
86 tmp_diff = rate - tmp_rate;
87 else
88 tmp_diff = tmp_rate - rate;
89
90 if (best_diff < 0 || best_diff > tmp_diff) {
91 best_rate = tmp_rate;
92 best_diff = tmp_diff;
93 *best_parent_rate = tmp_parent_rate;
94 *best_parent_hw = parent;
95 }
96
97 if (!best_diff || tmp_rate < rate)
98 break;
99 }
100
101 if (!best_diff)
102 break;
103 }
104
105 return best_rate;
106 }
107
at91sam9x5_clk_usb_set_parent(struct clk_hw * hw,u8 index)108 static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
109 {
110 u32 tmp;
111 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
112 struct at91_pmc *pmc = usb->pmc;
113
114 if (index > 1)
115 return -EINVAL;
116 tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
117 if (index)
118 tmp |= AT91_PMC_USBS;
119 pmc_write(pmc, AT91_PMC_USB, tmp);
120 return 0;
121 }
122
at91sam9x5_clk_usb_get_parent(struct clk_hw * hw)123 static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
124 {
125 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
126 struct at91_pmc *pmc = usb->pmc;
127
128 return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
129 }
130
at91sam9x5_clk_usb_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)131 static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
132 unsigned long parent_rate)
133 {
134 u32 tmp;
135 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
136 struct at91_pmc *pmc = usb->pmc;
137 unsigned long div;
138
139 if (!rate)
140 return -EINVAL;
141
142 div = DIV_ROUND_CLOSEST(parent_rate, rate);
143 if (div > SAM9X5_USB_MAX_DIV + 1 || !div)
144 return -EINVAL;
145
146 tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
147 tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
148 pmc_write(pmc, AT91_PMC_USB, tmp);
149
150 return 0;
151 }
152
153 static const struct clk_ops at91sam9x5_usb_ops = {
154 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
155 .determine_rate = at91sam9x5_clk_usb_determine_rate,
156 .get_parent = at91sam9x5_clk_usb_get_parent,
157 .set_parent = at91sam9x5_clk_usb_set_parent,
158 .set_rate = at91sam9x5_clk_usb_set_rate,
159 };
160
at91sam9n12_clk_usb_enable(struct clk_hw * hw)161 static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
162 {
163 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
164 struct at91_pmc *pmc = usb->pmc;
165
166 pmc_write(pmc, AT91_PMC_USB,
167 pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
168 return 0;
169 }
170
at91sam9n12_clk_usb_disable(struct clk_hw * hw)171 static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
172 {
173 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
174 struct at91_pmc *pmc = usb->pmc;
175
176 pmc_write(pmc, AT91_PMC_USB,
177 pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
178 }
179
at91sam9n12_clk_usb_is_enabled(struct clk_hw * hw)180 static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
181 {
182 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
183 struct at91_pmc *pmc = usb->pmc;
184
185 return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
186 }
187
188 static const struct clk_ops at91sam9n12_usb_ops = {
189 .enable = at91sam9n12_clk_usb_enable,
190 .disable = at91sam9n12_clk_usb_disable,
191 .is_enabled = at91sam9n12_clk_usb_is_enabled,
192 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
193 .determine_rate = at91sam9x5_clk_usb_determine_rate,
194 .set_rate = at91sam9x5_clk_usb_set_rate,
195 };
196
197 static struct clk * __init
at91sam9x5_clk_register_usb(struct at91_pmc * pmc,const char * name,const char ** parent_names,u8 num_parents)198 at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
199 const char **parent_names, u8 num_parents)
200 {
201 struct at91sam9x5_clk_usb *usb;
202 struct clk *clk = NULL;
203 struct clk_init_data init;
204
205 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
206 if (!usb)
207 return ERR_PTR(-ENOMEM);
208
209 init.name = name;
210 init.ops = &at91sam9x5_usb_ops;
211 init.parent_names = parent_names;
212 init.num_parents = num_parents;
213 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE |
214 CLK_SET_RATE_PARENT;
215
216 usb->hw.init = &init;
217 usb->pmc = pmc;
218
219 clk = clk_register(NULL, &usb->hw);
220 if (IS_ERR(clk))
221 kfree(usb);
222
223 return clk;
224 }
225
226 static struct clk * __init
at91sam9n12_clk_register_usb(struct at91_pmc * pmc,const char * name,const char * parent_name)227 at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
228 const char *parent_name)
229 {
230 struct at91sam9x5_clk_usb *usb;
231 struct clk *clk = NULL;
232 struct clk_init_data init;
233
234 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
235 if (!usb)
236 return ERR_PTR(-ENOMEM);
237
238 init.name = name;
239 init.ops = &at91sam9n12_usb_ops;
240 init.parent_names = &parent_name;
241 init.num_parents = 1;
242 init.flags = CLK_SET_RATE_GATE | CLK_SET_RATE_PARENT;
243
244 usb->hw.init = &init;
245 usb->pmc = pmc;
246
247 clk = clk_register(NULL, &usb->hw);
248 if (IS_ERR(clk))
249 kfree(usb);
250
251 return clk;
252 }
253
at91rm9200_clk_usb_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)254 static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
255 unsigned long parent_rate)
256 {
257 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
258 struct at91_pmc *pmc = usb->pmc;
259 u32 tmp;
260 u8 usbdiv;
261
262 tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
263 usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
264 if (usb->divisors[usbdiv])
265 return parent_rate / usb->divisors[usbdiv];
266
267 return 0;
268 }
269
at91rm9200_clk_usb_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * parent_rate)270 static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
271 unsigned long *parent_rate)
272 {
273 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
274 struct clk *parent = __clk_get_parent(hw->clk);
275 unsigned long bestrate = 0;
276 int bestdiff = -1;
277 unsigned long tmprate;
278 int tmpdiff;
279 int i = 0;
280
281 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
282 unsigned long tmp_parent_rate;
283
284 if (!usb->divisors[i])
285 continue;
286
287 tmp_parent_rate = rate * usb->divisors[i];
288 tmp_parent_rate = __clk_round_rate(parent, tmp_parent_rate);
289 tmprate = DIV_ROUND_CLOSEST(tmp_parent_rate, usb->divisors[i]);
290 if (tmprate < rate)
291 tmpdiff = rate - tmprate;
292 else
293 tmpdiff = tmprate - rate;
294
295 if (bestdiff < 0 || bestdiff > tmpdiff) {
296 bestrate = tmprate;
297 bestdiff = tmpdiff;
298 *parent_rate = tmp_parent_rate;
299 }
300
301 if (!bestdiff)
302 break;
303 }
304
305 return bestrate;
306 }
307
at91rm9200_clk_usb_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)308 static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
309 unsigned long parent_rate)
310 {
311 u32 tmp;
312 int i;
313 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
314 struct at91_pmc *pmc = usb->pmc;
315 unsigned long div;
316
317 if (!rate)
318 return -EINVAL;
319
320 div = DIV_ROUND_CLOSEST(parent_rate, rate);
321
322 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
323 if (usb->divisors[i] == div) {
324 tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
325 ~AT91_PMC_USBDIV;
326 tmp |= i << RM9200_USB_DIV_SHIFT;
327 pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
328 return 0;
329 }
330 }
331
332 return -EINVAL;
333 }
334
335 static const struct clk_ops at91rm9200_usb_ops = {
336 .recalc_rate = at91rm9200_clk_usb_recalc_rate,
337 .round_rate = at91rm9200_clk_usb_round_rate,
338 .set_rate = at91rm9200_clk_usb_set_rate,
339 };
340
341 static struct clk * __init
at91rm9200_clk_register_usb(struct at91_pmc * pmc,const char * name,const char * parent_name,const u32 * divisors)342 at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
343 const char *parent_name, const u32 *divisors)
344 {
345 struct at91rm9200_clk_usb *usb;
346 struct clk *clk = NULL;
347 struct clk_init_data init;
348
349 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
350 if (!usb)
351 return ERR_PTR(-ENOMEM);
352
353 init.name = name;
354 init.ops = &at91rm9200_usb_ops;
355 init.parent_names = &parent_name;
356 init.num_parents = 1;
357 init.flags = CLK_SET_RATE_PARENT;
358
359 usb->hw.init = &init;
360 usb->pmc = pmc;
361 memcpy(usb->divisors, divisors, sizeof(usb->divisors));
362
363 clk = clk_register(NULL, &usb->hw);
364 if (IS_ERR(clk))
365 kfree(usb);
366
367 return clk;
368 }
369
of_at91sam9x5_clk_usb_setup(struct device_node * np,struct at91_pmc * pmc)370 void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
371 struct at91_pmc *pmc)
372 {
373 struct clk *clk;
374 int i;
375 int num_parents;
376 const char *parent_names[USB_SOURCE_MAX];
377 const char *name = np->name;
378
379 num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
380 if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
381 return;
382
383 for (i = 0; i < num_parents; i++) {
384 parent_names[i] = of_clk_get_parent_name(np, i);
385 if (!parent_names[i])
386 return;
387 }
388
389 of_property_read_string(np, "clock-output-names", &name);
390
391 clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
392 if (IS_ERR(clk))
393 return;
394
395 of_clk_add_provider(np, of_clk_src_simple_get, clk);
396 }
397
of_at91sam9n12_clk_usb_setup(struct device_node * np,struct at91_pmc * pmc)398 void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
399 struct at91_pmc *pmc)
400 {
401 struct clk *clk;
402 const char *parent_name;
403 const char *name = np->name;
404
405 parent_name = of_clk_get_parent_name(np, 0);
406 if (!parent_name)
407 return;
408
409 of_property_read_string(np, "clock-output-names", &name);
410
411 clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
412 if (IS_ERR(clk))
413 return;
414
415 of_clk_add_provider(np, of_clk_src_simple_get, clk);
416 }
417
of_at91rm9200_clk_usb_setup(struct device_node * np,struct at91_pmc * pmc)418 void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
419 struct at91_pmc *pmc)
420 {
421 struct clk *clk;
422 const char *parent_name;
423 const char *name = np->name;
424 u32 divisors[4] = {0, 0, 0, 0};
425
426 parent_name = of_clk_get_parent_name(np, 0);
427 if (!parent_name)
428 return;
429
430 of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
431 if (!divisors[0])
432 return;
433
434 of_property_read_string(np, "clock-output-names", &name);
435
436 clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
437 if (IS_ERR(clk))
438 return;
439
440 of_clk_add_provider(np, of_clk_src_simple_get, clk);
441 }
442