blob: 24b5b020753a9e4a66a5d3db7c8f7ad8bee0b928 [file] [log] [blame]
Boris BREZILLONc84a61d2013-10-17 18:55:41 +02001/*
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
28struct 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
36struct 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
45static 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 return parent_rate / (usbdiv + 1);
56}
57
58static long at91sam9x5_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
59 unsigned long *parent_rate)
60{
61 unsigned long div;
62 unsigned long bestrate;
63 unsigned long tmp;
64
65 if (rate >= *parent_rate)
66 return *parent_rate;
67
68 div = *parent_rate / rate;
69 if (div >= SAM9X5_USB_MAX_DIV)
70 return *parent_rate / (SAM9X5_USB_MAX_DIV + 1);
71
72 bestrate = *parent_rate / div;
73 tmp = *parent_rate / (div + 1);
74 if (bestrate - rate > rate - tmp)
75 bestrate = tmp;
76
77 return bestrate;
78}
79
80static int at91sam9x5_clk_usb_set_parent(struct clk_hw *hw, u8 index)
81{
82 u32 tmp;
83 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
84 struct at91_pmc *pmc = usb->pmc;
85
86 if (index > 1)
87 return -EINVAL;
88 tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS;
89 if (index)
90 tmp |= AT91_PMC_USBS;
91 pmc_write(pmc, AT91_PMC_USB, tmp);
92 return 0;
93}
94
95static u8 at91sam9x5_clk_usb_get_parent(struct clk_hw *hw)
96{
97 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
98 struct at91_pmc *pmc = usb->pmc;
99
100 return pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS;
101}
102
103static int at91sam9x5_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
104 unsigned long parent_rate)
105{
106 u32 tmp;
107 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
108 struct at91_pmc *pmc = usb->pmc;
109 unsigned long div = parent_rate / rate;
110
111 if (parent_rate % rate || div < 1 || div >= SAM9X5_USB_MAX_DIV)
112 return -EINVAL;
113
114 tmp = pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_OHCIUSBDIV;
115 tmp |= (div - 1) << SAM9X5_USB_DIV_SHIFT;
116 pmc_write(pmc, AT91_PMC_USB, tmp);
117
118 return 0;
119}
120
121static const struct clk_ops at91sam9x5_usb_ops = {
122 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
123 .round_rate = at91sam9x5_clk_usb_round_rate,
124 .get_parent = at91sam9x5_clk_usb_get_parent,
125 .set_parent = at91sam9x5_clk_usb_set_parent,
126 .set_rate = at91sam9x5_clk_usb_set_rate,
127};
128
129static int at91sam9n12_clk_usb_enable(struct clk_hw *hw)
130{
131 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
132 struct at91_pmc *pmc = usb->pmc;
133
134 pmc_write(pmc, AT91_PMC_USB,
135 pmc_read(pmc, AT91_PMC_USB) | AT91_PMC_USBS);
136 return 0;
137}
138
139static void at91sam9n12_clk_usb_disable(struct clk_hw *hw)
140{
141 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
142 struct at91_pmc *pmc = usb->pmc;
143
144 pmc_write(pmc, AT91_PMC_USB,
145 pmc_read(pmc, AT91_PMC_USB) & ~AT91_PMC_USBS);
146}
147
148static int at91sam9n12_clk_usb_is_enabled(struct clk_hw *hw)
149{
150 struct at91sam9x5_clk_usb *usb = to_at91sam9x5_clk_usb(hw);
151 struct at91_pmc *pmc = usb->pmc;
152
153 return !!(pmc_read(pmc, AT91_PMC_USB) & AT91_PMC_USBS);
154}
155
156static const struct clk_ops at91sam9n12_usb_ops = {
157 .enable = at91sam9n12_clk_usb_enable,
158 .disable = at91sam9n12_clk_usb_disable,
159 .is_enabled = at91sam9n12_clk_usb_is_enabled,
160 .recalc_rate = at91sam9x5_clk_usb_recalc_rate,
161 .round_rate = at91sam9x5_clk_usb_round_rate,
162 .set_rate = at91sam9x5_clk_usb_set_rate,
163};
164
165static struct clk * __init
166at91sam9x5_clk_register_usb(struct at91_pmc *pmc, const char *name,
167 const char **parent_names, u8 num_parents)
168{
169 struct at91sam9x5_clk_usb *usb;
170 struct clk *clk = NULL;
171 struct clk_init_data init;
172
173 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
174 if (!usb)
175 return ERR_PTR(-ENOMEM);
176
177 init.name = name;
178 init.ops = &at91sam9x5_usb_ops;
179 init.parent_names = parent_names;
180 init.num_parents = num_parents;
181 init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
182
183 usb->hw.init = &init;
184 usb->pmc = pmc;
185
186 clk = clk_register(NULL, &usb->hw);
187 if (IS_ERR(clk))
188 kfree(usb);
189
190 return clk;
191}
192
193static struct clk * __init
194at91sam9n12_clk_register_usb(struct at91_pmc *pmc, const char *name,
195 const char *parent_name)
196{
197 struct at91sam9x5_clk_usb *usb;
198 struct clk *clk = NULL;
199 struct clk_init_data init;
200
201 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
202 if (!usb)
203 return ERR_PTR(-ENOMEM);
204
205 init.name = name;
206 init.ops = &at91sam9n12_usb_ops;
207 init.parent_names = &parent_name;
208 init.num_parents = 1;
209 init.flags = CLK_SET_RATE_GATE;
210
211 usb->hw.init = &init;
212 usb->pmc = pmc;
213
214 clk = clk_register(NULL, &usb->hw);
215 if (IS_ERR(clk))
216 kfree(usb);
217
218 return clk;
219}
220
221static unsigned long at91rm9200_clk_usb_recalc_rate(struct clk_hw *hw,
222 unsigned long parent_rate)
223{
224 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
225 struct at91_pmc *pmc = usb->pmc;
226 u32 tmp;
227 u8 usbdiv;
228
229 tmp = pmc_read(pmc, AT91_CKGR_PLLBR);
230 usbdiv = (tmp & AT91_PMC_USBDIV) >> RM9200_USB_DIV_SHIFT;
231 if (usb->divisors[usbdiv])
232 return parent_rate / usb->divisors[usbdiv];
233
234 return 0;
235}
236
237static long at91rm9200_clk_usb_round_rate(struct clk_hw *hw, unsigned long rate,
238 unsigned long *parent_rate)
239{
240 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
Boris BREZILLON13a60732014-09-02 09:50:17 +0200241 struct clk *parent = __clk_get_parent(hw->clk);
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200242 unsigned long bestrate = 0;
243 int bestdiff = -1;
244 unsigned long tmprate;
245 int tmpdiff;
246 int i = 0;
247
Boris BREZILLON13a60732014-09-02 09:50:17 +0200248 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
249 unsigned long tmp_parent_rate;
250
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200251 if (!usb->divisors[i])
252 continue;
Boris BREZILLON13a60732014-09-02 09:50:17 +0200253
254 tmp_parent_rate = rate * usb->divisors[i];
255 tmp_parent_rate = __clk_round_rate(parent, tmp_parent_rate);
256 tmprate = tmp_parent_rate / usb->divisors[i];
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200257 if (tmprate < rate)
258 tmpdiff = rate - tmprate;
259 else
260 tmpdiff = tmprate - rate;
261
262 if (bestdiff < 0 || bestdiff > tmpdiff) {
263 bestrate = tmprate;
264 bestdiff = tmpdiff;
Boris BREZILLON13a60732014-09-02 09:50:17 +0200265 *parent_rate = tmp_parent_rate;
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200266 }
267
268 if (!bestdiff)
269 break;
270 }
271
272 return bestrate;
273}
274
275static int at91rm9200_clk_usb_set_rate(struct clk_hw *hw, unsigned long rate,
276 unsigned long parent_rate)
277{
278 u32 tmp;
279 int i;
280 struct at91rm9200_clk_usb *usb = to_at91rm9200_clk_usb(hw);
281 struct at91_pmc *pmc = usb->pmc;
Boris BREZILLON16eeaec2014-09-02 09:50:18 +0200282 unsigned long div;
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200283
Boris BREZILLON16eeaec2014-09-02 09:50:18 +0200284 if (!rate || parent_rate % rate)
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200285 return -EINVAL;
Boris BREZILLON16eeaec2014-09-02 09:50:18 +0200286
287 div = parent_rate / rate;
288
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200289 for (i = 0; i < RM9200_USB_DIV_TAB_SIZE; i++) {
290 if (usb->divisors[i] == div) {
291 tmp = pmc_read(pmc, AT91_CKGR_PLLBR) &
292 ~AT91_PMC_USBDIV;
293 tmp |= i << RM9200_USB_DIV_SHIFT;
294 pmc_write(pmc, AT91_CKGR_PLLBR, tmp);
295 return 0;
296 }
297 }
298
299 return -EINVAL;
300}
301
302static const struct clk_ops at91rm9200_usb_ops = {
303 .recalc_rate = at91rm9200_clk_usb_recalc_rate,
304 .round_rate = at91rm9200_clk_usb_round_rate,
305 .set_rate = at91rm9200_clk_usb_set_rate,
306};
307
308static struct clk * __init
309at91rm9200_clk_register_usb(struct at91_pmc *pmc, const char *name,
310 const char *parent_name, const u32 *divisors)
311{
312 struct at91rm9200_clk_usb *usb;
313 struct clk *clk = NULL;
314 struct clk_init_data init;
315
316 usb = kzalloc(sizeof(*usb), GFP_KERNEL);
317 if (!usb)
318 return ERR_PTR(-ENOMEM);
319
320 init.name = name;
321 init.ops = &at91rm9200_usb_ops;
322 init.parent_names = &parent_name;
323 init.num_parents = 1;
Boris BREZILLON13a60732014-09-02 09:50:17 +0200324 init.flags = CLK_SET_RATE_PARENT;
Boris BREZILLONc84a61d2013-10-17 18:55:41 +0200325
326 usb->hw.init = &init;
327 usb->pmc = pmc;
328 memcpy(usb->divisors, divisors, sizeof(usb->divisors));
329
330 clk = clk_register(NULL, &usb->hw);
331 if (IS_ERR(clk))
332 kfree(usb);
333
334 return clk;
335}
336
337void __init of_at91sam9x5_clk_usb_setup(struct device_node *np,
338 struct at91_pmc *pmc)
339{
340 struct clk *clk;
341 int i;
342 int num_parents;
343 const char *parent_names[USB_SOURCE_MAX];
344 const char *name = np->name;
345
346 num_parents = of_count_phandle_with_args(np, "clocks", "#clock-cells");
347 if (num_parents <= 0 || num_parents > USB_SOURCE_MAX)
348 return;
349
350 for (i = 0; i < num_parents; i++) {
351 parent_names[i] = of_clk_get_parent_name(np, i);
352 if (!parent_names[i])
353 return;
354 }
355
356 of_property_read_string(np, "clock-output-names", &name);
357
358 clk = at91sam9x5_clk_register_usb(pmc, name, parent_names, num_parents);
359 if (IS_ERR(clk))
360 return;
361
362 of_clk_add_provider(np, of_clk_src_simple_get, clk);
363}
364
365void __init of_at91sam9n12_clk_usb_setup(struct device_node *np,
366 struct at91_pmc *pmc)
367{
368 struct clk *clk;
369 const char *parent_name;
370 const char *name = np->name;
371
372 parent_name = of_clk_get_parent_name(np, 0);
373 if (!parent_name)
374 return;
375
376 of_property_read_string(np, "clock-output-names", &name);
377
378 clk = at91sam9n12_clk_register_usb(pmc, name, parent_name);
379 if (IS_ERR(clk))
380 return;
381
382 of_clk_add_provider(np, of_clk_src_simple_get, clk);
383}
384
385void __init of_at91rm9200_clk_usb_setup(struct device_node *np,
386 struct at91_pmc *pmc)
387{
388 struct clk *clk;
389 const char *parent_name;
390 const char *name = np->name;
391 u32 divisors[4] = {0, 0, 0, 0};
392
393 parent_name = of_clk_get_parent_name(np, 0);
394 if (!parent_name)
395 return;
396
397 of_property_read_u32_array(np, "atmel,clk-divisors", divisors, 4);
398 if (!divisors[0])
399 return;
400
401 of_property_read_string(np, "clock-output-names", &name);
402
403 clk = at91rm9200_clk_register_usb(pmc, name, parent_name, divisors);
404 if (IS_ERR(clk))
405 return;
406
407 of_clk_add_provider(np, of_clk_src_simple_get, clk);
408}