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