blob: ade73375ede6a9a6b2d042b37916b957a10794b3 [file] [log] [blame]
Manuel Lauss47440222014-07-23 16:36:48 +02001/*
2 * Alchemy clocks.
3 *
4 * Exposes all configurable internal clock sources to the clk framework.
5 *
6 * We have:
7 * - Root source, usually 12MHz supplied by an external crystal
8 * - 3 PLLs which generate multiples of root rate [AUX, CPU, AUX2]
9 *
10 * Dividers:
11 * - 6 clock dividers with:
12 * * selectable source [one of the PLLs],
13 * * output divided between [2 .. 512 in steps of 2] (!Au1300)
14 * or [1 .. 256 in steps of 1] (Au1300),
15 * * can be enabled individually.
16 *
17 * - up to 6 "internal" (fixed) consumers which:
18 * * take either AUXPLL or one of the above 6 dividers as input,
19 * * divide this input by 1, 2, or 4 (and 3 on Au1300).
20 * * can be disabled separately.
21 *
22 * Misc clocks:
23 * - sysbus clock: CPU core clock (CPUPLL) divided by 2, 3 or 4.
24 * depends on board design and should be set by bootloader, read-only.
25 * - peripheral clock: half the rate of sysbus clock, source for a lot
26 * of peripheral blocks, read-only.
27 * - memory clock: clk rate to main memory chips, depends on board
28 * design and is read-only,
29 * - lrclk: the static bus clock signal for synchronous operation.
30 * depends on board design, must be set by bootloader,
31 * but may be required to correctly configure devices attached to
32 * the static bus. The Au1000/1500/1100 manuals call it LCLK, on
33 * later models it's called RCLK.
34 */
35
36#include <linux/init.h>
37#include <linux/io.h>
38#include <linux/clk-provider.h>
39#include <linux/clkdev.h>
Manuel Lauss47440222014-07-23 16:36:48 +020040#include <linux/slab.h>
41#include <linux/spinlock.h>
42#include <linux/types.h>
43#include <asm/mach-au1x00/au1000.h>
44
45/* Base clock: 12MHz is the default in all databooks, and I haven't
46 * found any board yet which uses a different rate.
47 */
48#define ALCHEMY_ROOTCLK_RATE 12000000
49
50/*
51 * the internal sources which can be driven by the PLLs and dividers.
52 * Names taken from the databooks, refer to them for more information,
53 * especially which ones are share a clock line.
54 */
55static const char * const alchemy_au1300_intclknames[] = {
56 "lcd_intclk", "gpemgp_clk", "maempe_clk", "maebsa_clk",
57 "EXTCLK0", "EXTCLK1"
58};
59
60static const char * const alchemy_au1200_intclknames[] = {
61 "lcd_intclk", NULL, NULL, NULL, "EXTCLK0", "EXTCLK1"
62};
63
64static const char * const alchemy_au1550_intclknames[] = {
65 "usb_clk", "psc0_intclk", "psc1_intclk", "pci_clko",
66 "EXTCLK0", "EXTCLK1"
67};
68
69static const char * const alchemy_au1100_intclknames[] = {
70 "usb_clk", "lcd_intclk", NULL, "i2s_clk", "EXTCLK0", "EXTCLK1"
71};
72
73static const char * const alchemy_au1500_intclknames[] = {
74 NULL, "usbd_clk", "usbh_clk", "pci_clko", "EXTCLK0", "EXTCLK1"
75};
76
77static const char * const alchemy_au1000_intclknames[] = {
78 "irda_clk", "usbd_clk", "usbh_clk", "i2s_clk", "EXTCLK0",
79 "EXTCLK1"
80};
81
82/* aliases for a few on-chip sources which are either shared
83 * or have gone through name changes.
84 */
85static struct clk_aliastable {
86 char *alias;
87 char *base;
88 int cputype;
89} alchemy_clk_aliases[] __initdata = {
90 { "usbh_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
91 { "usbd_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
92 { "irda_clk", "usb_clk", ALCHEMY_CPU_AU1100 },
93 { "usbh_clk", "usb_clk", ALCHEMY_CPU_AU1550 },
94 { "usbd_clk", "usb_clk", ALCHEMY_CPU_AU1550 },
95 { "psc2_intclk", "usb_clk", ALCHEMY_CPU_AU1550 },
96 { "psc3_intclk", "EXTCLK0", ALCHEMY_CPU_AU1550 },
97 { "psc0_intclk", "EXTCLK0", ALCHEMY_CPU_AU1200 },
98 { "psc1_intclk", "EXTCLK1", ALCHEMY_CPU_AU1200 },
99 { "psc0_intclk", "EXTCLK0", ALCHEMY_CPU_AU1300 },
100 { "psc2_intclk", "EXTCLK0", ALCHEMY_CPU_AU1300 },
101 { "psc1_intclk", "EXTCLK1", ALCHEMY_CPU_AU1300 },
102 { "psc3_intclk", "EXTCLK1", ALCHEMY_CPU_AU1300 },
103
104 { NULL, NULL, 0 },
105};
106
107#define IOMEM(x) ((void __iomem *)(KSEG1ADDR(CPHYSADDR(x))))
108
109/* access locks to SYS_FREQCTRL0/1 and SYS_CLKSRC registers */
110static spinlock_t alchemy_clk_fg0_lock;
111static spinlock_t alchemy_clk_fg1_lock;
112static spinlock_t alchemy_clk_csrc_lock;
113
114/* CPU Core clock *****************************************************/
115
116static unsigned long alchemy_clk_cpu_recalc(struct clk_hw *hw,
117 unsigned long parent_rate)
118{
119 unsigned long t;
120
121 /*
122 * On early Au1000, sys_cpupll was write-only. Since these
123 * silicon versions of Au1000 are not sold, we don't bend
124 * over backwards trying to determine the frequency.
125 */
126 if (unlikely(au1xxx_cpu_has_pll_wo()))
127 t = 396000000;
128 else {
129 t = alchemy_rdsys(AU1000_SYS_CPUPLL) & 0x7f;
130 t *= parent_rate;
131 }
132
133 return t;
134}
135
136static struct clk_ops alchemy_clkops_cpu = {
137 .recalc_rate = alchemy_clk_cpu_recalc,
138};
139
140static struct clk __init *alchemy_clk_setup_cpu(const char *parent_name,
141 int ctype)
142{
143 struct clk_init_data id;
144 struct clk_hw *h;
145
146 h = kzalloc(sizeof(*h), GFP_KERNEL);
147 if (!h)
148 return ERR_PTR(-ENOMEM);
149
150 id.name = ALCHEMY_CPU_CLK;
151 id.parent_names = &parent_name;
152 id.num_parents = 1;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200153 id.flags = CLK_IS_BASIC;
Manuel Lauss47440222014-07-23 16:36:48 +0200154 id.ops = &alchemy_clkops_cpu;
155 h->init = &id;
156
157 return clk_register(NULL, h);
158}
159
160/* AUXPLLs ************************************************************/
161
162struct alchemy_auxpll_clk {
163 struct clk_hw hw;
164 unsigned long reg; /* au1300 has also AUXPLL2 */
165 int maxmult; /* max multiplier */
166};
167#define to_auxpll_clk(x) container_of(x, struct alchemy_auxpll_clk, hw)
168
169static unsigned long alchemy_clk_aux_recalc(struct clk_hw *hw,
170 unsigned long parent_rate)
171{
172 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
173
174 return (alchemy_rdsys(a->reg) & 0xff) * parent_rate;
175}
176
177static int alchemy_clk_aux_setr(struct clk_hw *hw,
178 unsigned long rate,
179 unsigned long parent_rate)
180{
181 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
182 unsigned long d = rate;
183
184 if (rate)
185 d /= parent_rate;
186 else
187 d = 0;
188
189 /* minimum is 84MHz, max is 756-1032 depending on variant */
190 if (((d < 7) && (d != 0)) || (d > a->maxmult))
191 return -EINVAL;
192
193 alchemy_wrsys(d, a->reg);
194 return 0;
195}
196
197static long alchemy_clk_aux_roundr(struct clk_hw *hw,
198 unsigned long rate,
199 unsigned long *parent_rate)
200{
201 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
202 unsigned long mult;
203
204 if (!rate || !*parent_rate)
205 return 0;
206
207 mult = rate / (*parent_rate);
208
209 if (mult && (mult < 7))
210 mult = 7;
211 if (mult > a->maxmult)
212 mult = a->maxmult;
213
214 return (*parent_rate) * mult;
215}
216
217static struct clk_ops alchemy_clkops_aux = {
218 .recalc_rate = alchemy_clk_aux_recalc,
219 .set_rate = alchemy_clk_aux_setr,
220 .round_rate = alchemy_clk_aux_roundr,
221};
222
223static struct clk __init *alchemy_clk_setup_aux(const char *parent_name,
224 char *name, int maxmult,
225 unsigned long reg)
226{
227 struct clk_init_data id;
228 struct clk *c;
229 struct alchemy_auxpll_clk *a;
230
231 a = kzalloc(sizeof(*a), GFP_KERNEL);
232 if (!a)
233 return ERR_PTR(-ENOMEM);
234
235 id.name = name;
236 id.parent_names = &parent_name;
237 id.num_parents = 1;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200238 id.flags = CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200239 id.ops = &alchemy_clkops_aux;
240
241 a->reg = reg;
242 a->maxmult = maxmult;
243 a->hw.init = &id;
244
245 c = clk_register(NULL, &a->hw);
246 if (!IS_ERR(c))
247 clk_register_clkdev(c, name, NULL);
248 else
249 kfree(a);
250
251 return c;
252}
253
254/* sysbus_clk *********************************************************/
255
256static struct clk __init *alchemy_clk_setup_sysbus(const char *pn)
257{
258 unsigned long v = (alchemy_rdsys(AU1000_SYS_POWERCTRL) & 3) + 2;
259 struct clk *c;
260
261 c = clk_register_fixed_factor(NULL, ALCHEMY_SYSBUS_CLK,
262 pn, 0, 1, v);
263 if (!IS_ERR(c))
264 clk_register_clkdev(c, ALCHEMY_SYSBUS_CLK, NULL);
265 return c;
266}
267
268/* Peripheral Clock ***************************************************/
269
270static struct clk __init *alchemy_clk_setup_periph(const char *pn)
271{
272 /* Peripheral clock runs at half the rate of sysbus clk */
273 struct clk *c;
274
275 c = clk_register_fixed_factor(NULL, ALCHEMY_PERIPH_CLK,
276 pn, 0, 1, 2);
277 if (!IS_ERR(c))
278 clk_register_clkdev(c, ALCHEMY_PERIPH_CLK, NULL);
279 return c;
280}
281
282/* mem clock **********************************************************/
283
284static struct clk __init *alchemy_clk_setup_mem(const char *pn, int ct)
285{
286 void __iomem *addr = IOMEM(AU1000_MEM_PHYS_ADDR);
287 unsigned long v;
288 struct clk *c;
289 int div;
290
291 switch (ct) {
292 case ALCHEMY_CPU_AU1550:
293 case ALCHEMY_CPU_AU1200:
294 v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
295 div = (v & (1 << 15)) ? 1 : 2;
296 break;
297 case ALCHEMY_CPU_AU1300:
298 v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
299 div = (v & (1 << 31)) ? 1 : 2;
300 break;
301 case ALCHEMY_CPU_AU1000:
302 case ALCHEMY_CPU_AU1500:
303 case ALCHEMY_CPU_AU1100:
304 default:
305 div = 2;
306 break;
307 }
308
309 c = clk_register_fixed_factor(NULL, ALCHEMY_MEM_CLK, pn,
310 0, 1, div);
311 if (!IS_ERR(c))
312 clk_register_clkdev(c, ALCHEMY_MEM_CLK, NULL);
313 return c;
314}
315
316/* lrclk: external synchronous static bus clock ***********************/
317
Manuel Lauss51f105d32015-01-29 16:06:42 +0100318static struct clk __init *alchemy_clk_setup_lrclk(const char *pn, int t)
Manuel Lauss47440222014-07-23 16:36:48 +0200319{
Manuel Lauss51f105d32015-01-29 16:06:42 +0100320 /* Au1000, Au1500: MEM_STCFG0[11]: If bit is set, lrclk=pclk/5,
321 * otherwise lrclk=pclk/4.
322 * All other variants: MEM_STCFG0[15:13] = divisor.
Manuel Lauss47440222014-07-23 16:36:48 +0200323 * L/RCLK = periph_clk / (divisor + 1)
324 * On Au1000, Au1500, Au1100 it's called LCLK,
325 * on later models it's called RCLK, but it's the same thing.
326 */
327 struct clk *c;
Manuel Lauss51f105d32015-01-29 16:06:42 +0100328 unsigned long v = alchemy_rdsmem(AU1000_MEM_STCFG0);
Manuel Lauss47440222014-07-23 16:36:48 +0200329
Manuel Lauss51f105d32015-01-29 16:06:42 +0100330 switch (t) {
331 case ALCHEMY_CPU_AU1000:
332 case ALCHEMY_CPU_AU1500:
333 v = 4 + ((v >> 11) & 1);
334 break;
335 default: /* all other models */
336 v = ((v >> 13) & 7) + 1;
337 }
Manuel Lauss47440222014-07-23 16:36:48 +0200338 c = clk_register_fixed_factor(NULL, ALCHEMY_LR_CLK,
339 pn, 0, 1, v);
340 if (!IS_ERR(c))
341 clk_register_clkdev(c, ALCHEMY_LR_CLK, NULL);
342 return c;
343}
344
345/* Clock dividers and muxes *******************************************/
346
347/* data for fgen and csrc mux-dividers */
348struct alchemy_fgcs_clk {
349 struct clk_hw hw;
350 spinlock_t *reglock; /* register lock */
351 unsigned long reg; /* SYS_FREQCTRL0/1 */
352 int shift; /* offset in register */
353 int parent; /* parent before disable [Au1300] */
354 int isen; /* is it enabled? */
355 int *dt; /* dividertable for csrc */
356};
357#define to_fgcs_clk(x) container_of(x, struct alchemy_fgcs_clk, hw)
358
359static long alchemy_calc_div(unsigned long rate, unsigned long prate,
360 int scale, int maxdiv, unsigned long *rv)
361{
362 long div1, div2;
363
364 div1 = prate / rate;
365 if ((prate / div1) > rate)
366 div1++;
367
368 if (scale == 2) { /* only div-by-multiple-of-2 possible */
369 if (div1 & 1)
370 div1++; /* stay <=prate */
371 }
372
373 div2 = (div1 / scale) - 1; /* value to write to register */
374
375 if (div2 > maxdiv)
376 div2 = maxdiv;
377 if (rv)
378 *rv = div2;
379
380 div1 = ((div2 + 1) * scale);
381 return div1;
382}
383
384static long alchemy_clk_fgcs_detr(struct clk_hw *hw, unsigned long rate,
385 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100386 struct clk_hw **best_parent_clk,
Manuel Lauss47440222014-07-23 16:36:48 +0200387 int scale, int maxdiv)
388{
389 struct clk *pc, *bpc, *free;
390 long tdv, tpr, pr, nr, br, bpr, diff, lastdiff;
391 int j;
392
393 lastdiff = INT_MAX;
394 bpr = 0;
395 bpc = NULL;
396 br = -EINVAL;
397 free = NULL;
398
399 /* look at the rates each enabled parent supplies and select
400 * the one that gets closest to but not over the requested rate.
401 */
402 for (j = 0; j < 7; j++) {
403 pc = clk_get_parent_by_index(hw->clk, j);
404 if (!pc)
405 break;
406
407 /* if this parent is currently unused, remember it.
Tomeu Vizoso24c71c82014-10-20 15:40:01 +0200408 * XXX: we would actually want clk_has_active_children()
409 * but this is a good-enough approximation for now.
Manuel Lauss47440222014-07-23 16:36:48 +0200410 */
Tomeu Vizoso24c71c82014-10-20 15:40:01 +0200411 if (!__clk_is_prepared(pc)) {
Manuel Lauss47440222014-07-23 16:36:48 +0200412 if (!free)
413 free = pc;
414 }
415
416 pr = clk_get_rate(pc);
417 if (pr < rate)
418 continue;
419
420 /* what can hardware actually provide */
421 tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
422 nr = pr / tdv;
423 diff = rate - nr;
424 if (nr > rate)
425 continue;
426
427 if (diff < lastdiff) {
428 lastdiff = diff;
429 bpr = pr;
430 bpc = pc;
431 br = nr;
432 }
433 if (diff == 0)
434 break;
435 }
436
437 /* if we couldn't get the exact rate we wanted from the enabled
438 * parents, maybe we can tell an available disabled/inactive one
439 * to give us a rate we can divide down to the requested rate.
440 */
441 if (lastdiff && free) {
442 for (j = (maxdiv == 4) ? 1 : scale; j <= maxdiv; j += scale) {
443 tpr = rate * j;
444 if (tpr < 0)
445 break;
446 pr = clk_round_rate(free, tpr);
447
448 tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
449 nr = pr / tdv;
450 diff = rate - nr;
451 if (nr > rate)
452 continue;
453 if (diff < lastdiff) {
454 lastdiff = diff;
455 bpr = pr;
456 bpc = free;
457 br = nr;
458 }
459 if (diff == 0)
460 break;
461 }
462 }
463
464 *best_parent_rate = bpr;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100465 *best_parent_clk = __clk_get_hw(bpc);
Manuel Lauss47440222014-07-23 16:36:48 +0200466 return br;
467}
468
469static int alchemy_clk_fgv1_en(struct clk_hw *hw)
470{
471 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
472 unsigned long v, flags;
473
474 spin_lock_irqsave(c->reglock, flags);
475 v = alchemy_rdsys(c->reg);
476 v |= (1 << 1) << c->shift;
477 alchemy_wrsys(v, c->reg);
478 spin_unlock_irqrestore(c->reglock, flags);
479
480 return 0;
481}
482
483static int alchemy_clk_fgv1_isen(struct clk_hw *hw)
484{
485 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
486 unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 1);
487
488 return v & 1;
489}
490
491static void alchemy_clk_fgv1_dis(struct clk_hw *hw)
492{
493 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
494 unsigned long v, flags;
495
496 spin_lock_irqsave(c->reglock, flags);
497 v = alchemy_rdsys(c->reg);
498 v &= ~((1 << 1) << c->shift);
499 alchemy_wrsys(v, c->reg);
500 spin_unlock_irqrestore(c->reglock, flags);
501}
502
503static int alchemy_clk_fgv1_setp(struct clk_hw *hw, u8 index)
504{
505 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
506 unsigned long v, flags;
507
508 spin_lock_irqsave(c->reglock, flags);
509 v = alchemy_rdsys(c->reg);
510 if (index)
511 v |= (1 << c->shift);
512 else
513 v &= ~(1 << c->shift);
514 alchemy_wrsys(v, c->reg);
515 spin_unlock_irqrestore(c->reglock, flags);
516
517 return 0;
518}
519
520static u8 alchemy_clk_fgv1_getp(struct clk_hw *hw)
521{
522 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
523
524 return (alchemy_rdsys(c->reg) >> c->shift) & 1;
525}
526
527static int alchemy_clk_fgv1_setr(struct clk_hw *hw, unsigned long rate,
528 unsigned long parent_rate)
529{
530 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
531 unsigned long div, v, flags, ret;
532 int sh = c->shift + 2;
533
534 if (!rate || !parent_rate || rate > (parent_rate / 2))
535 return -EINVAL;
536 ret = alchemy_calc_div(rate, parent_rate, 2, 512, &div);
537 spin_lock_irqsave(c->reglock, flags);
538 v = alchemy_rdsys(c->reg);
539 v &= ~(0xff << sh);
540 v |= div << sh;
541 alchemy_wrsys(v, c->reg);
542 spin_unlock_irqrestore(c->reglock, flags);
543
544 return 0;
545}
546
547static unsigned long alchemy_clk_fgv1_recalc(struct clk_hw *hw,
548 unsigned long parent_rate)
549{
550 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
551 unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 2);
552
553 v = ((v & 0xff) + 1) * 2;
554 return parent_rate / v;
555}
556
557static long alchemy_clk_fgv1_detr(struct clk_hw *hw, unsigned long rate,
558 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100559 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200560{
561 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
562 best_parent_clk, 2, 512);
563}
564
565/* Au1000, Au1100, Au15x0, Au12x0 */
566static struct clk_ops alchemy_clkops_fgenv1 = {
567 .recalc_rate = alchemy_clk_fgv1_recalc,
568 .determine_rate = alchemy_clk_fgv1_detr,
569 .set_rate = alchemy_clk_fgv1_setr,
570 .set_parent = alchemy_clk_fgv1_setp,
571 .get_parent = alchemy_clk_fgv1_getp,
572 .enable = alchemy_clk_fgv1_en,
573 .disable = alchemy_clk_fgv1_dis,
574 .is_enabled = alchemy_clk_fgv1_isen,
575};
576
577static void __alchemy_clk_fgv2_en(struct alchemy_fgcs_clk *c)
578{
579 unsigned long v = alchemy_rdsys(c->reg);
580
581 v &= ~(3 << c->shift);
582 v |= (c->parent & 3) << c->shift;
583 alchemy_wrsys(v, c->reg);
584 c->isen = 1;
585}
586
587static int alchemy_clk_fgv2_en(struct clk_hw *hw)
588{
589 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
590 unsigned long flags;
591
592 /* enable by setting the previous parent clock */
593 spin_lock_irqsave(c->reglock, flags);
594 __alchemy_clk_fgv2_en(c);
595 spin_unlock_irqrestore(c->reglock, flags);
596
597 return 0;
598}
599
600static int alchemy_clk_fgv2_isen(struct clk_hw *hw)
601{
602 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
603
604 return ((alchemy_rdsys(c->reg) >> c->shift) & 3) != 0;
605}
606
607static void alchemy_clk_fgv2_dis(struct clk_hw *hw)
608{
609 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
610 unsigned long v, flags;
611
612 spin_lock_irqsave(c->reglock, flags);
613 v = alchemy_rdsys(c->reg);
614 v &= ~(3 << c->shift); /* set input mux to "disabled" state */
615 alchemy_wrsys(v, c->reg);
616 c->isen = 0;
617 spin_unlock_irqrestore(c->reglock, flags);
618}
619
620static int alchemy_clk_fgv2_setp(struct clk_hw *hw, u8 index)
621{
622 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
623 unsigned long flags;
624
625 spin_lock_irqsave(c->reglock, flags);
626 c->parent = index + 1; /* value to write to register */
627 if (c->isen)
628 __alchemy_clk_fgv2_en(c);
629 spin_unlock_irqrestore(c->reglock, flags);
630
631 return 0;
632}
633
634static u8 alchemy_clk_fgv2_getp(struct clk_hw *hw)
635{
636 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
637 unsigned long flags, v;
638
639 spin_lock_irqsave(c->reglock, flags);
640 v = c->parent - 1;
641 spin_unlock_irqrestore(c->reglock, flags);
642 return v;
643}
644
645/* fg0-2 and fg4-6 share a "scale"-bit. With this bit cleared, the
646 * dividers behave exactly as on previous models (dividers are multiples
647 * of 2); with the bit set, dividers are multiples of 1, halving their
648 * range, but making them also much more flexible.
649 */
650static int alchemy_clk_fgv2_setr(struct clk_hw *hw, unsigned long rate,
651 unsigned long parent_rate)
652{
653 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
654 int sh = c->shift + 2;
655 unsigned long div, v, flags, ret;
656
657 if (!rate || !parent_rate || rate > parent_rate)
658 return -EINVAL;
659
660 v = alchemy_rdsys(c->reg) & (1 << 30); /* test "scale" bit */
661 ret = alchemy_calc_div(rate, parent_rate, v ? 1 : 2,
662 v ? 256 : 512, &div);
663
664 spin_lock_irqsave(c->reglock, flags);
665 v = alchemy_rdsys(c->reg);
666 v &= ~(0xff << sh);
667 v |= (div & 0xff) << sh;
668 alchemy_wrsys(v, c->reg);
669 spin_unlock_irqrestore(c->reglock, flags);
670
671 return 0;
672}
673
674static unsigned long alchemy_clk_fgv2_recalc(struct clk_hw *hw,
675 unsigned long parent_rate)
676{
677 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
678 int sh = c->shift + 2;
679 unsigned long v, t;
680
681 v = alchemy_rdsys(c->reg);
682 t = parent_rate / (((v >> sh) & 0xff) + 1);
683 if ((v & (1 << 30)) == 0) /* test scale bit */
684 t /= 2;
685
686 return t;
687}
688
689static long alchemy_clk_fgv2_detr(struct clk_hw *hw, unsigned long rate,
690 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100691 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200692{
693 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
694 int scale, maxdiv;
695
696 if (alchemy_rdsys(c->reg) & (1 << 30)) {
697 scale = 1;
698 maxdiv = 256;
699 } else {
700 scale = 2;
701 maxdiv = 512;
702 }
703
704 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
705 best_parent_clk, scale, maxdiv);
706}
707
708/* Au1300 larger input mux, no separate disable bit, flexible divider */
709static struct clk_ops alchemy_clkops_fgenv2 = {
710 .recalc_rate = alchemy_clk_fgv2_recalc,
711 .determine_rate = alchemy_clk_fgv2_detr,
712 .set_rate = alchemy_clk_fgv2_setr,
713 .set_parent = alchemy_clk_fgv2_setp,
714 .get_parent = alchemy_clk_fgv2_getp,
715 .enable = alchemy_clk_fgv2_en,
716 .disable = alchemy_clk_fgv2_dis,
717 .is_enabled = alchemy_clk_fgv2_isen,
718};
719
720static const char * const alchemy_clk_fgv1_parents[] = {
721 ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
722};
723
724static const char * const alchemy_clk_fgv2_parents[] = {
725 ALCHEMY_AUXPLL2_CLK, ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
726};
727
728static const char * const alchemy_clk_fgen_names[] = {
729 ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
730 ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK };
731
732static int __init alchemy_clk_init_fgens(int ctype)
733{
734 struct clk *c;
735 struct clk_init_data id;
736 struct alchemy_fgcs_clk *a;
737 unsigned long v;
738 int i, ret;
739
740 switch (ctype) {
741 case ALCHEMY_CPU_AU1000...ALCHEMY_CPU_AU1200:
742 id.ops = &alchemy_clkops_fgenv1;
743 id.parent_names = (const char **)alchemy_clk_fgv1_parents;
744 id.num_parents = 2;
745 break;
746 case ALCHEMY_CPU_AU1300:
747 id.ops = &alchemy_clkops_fgenv2;
748 id.parent_names = (const char **)alchemy_clk_fgv2_parents;
749 id.num_parents = 3;
750 break;
751 default:
752 return -ENODEV;
753 }
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200754 id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200755
756 a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
757 if (!a)
758 return -ENOMEM;
759
760 spin_lock_init(&alchemy_clk_fg0_lock);
761 spin_lock_init(&alchemy_clk_fg1_lock);
762 ret = 0;
763 for (i = 0; i < 6; i++) {
764 id.name = alchemy_clk_fgen_names[i];
765 a->shift = 10 * (i < 3 ? i : i - 3);
766 if (i > 2) {
767 a->reg = AU1000_SYS_FREQCTRL1;
768 a->reglock = &alchemy_clk_fg1_lock;
769 } else {
770 a->reg = AU1000_SYS_FREQCTRL0;
771 a->reglock = &alchemy_clk_fg0_lock;
772 }
773
774 /* default to first parent if bootloader has set
775 * the mux to disabled state.
776 */
777 if (ctype == ALCHEMY_CPU_AU1300) {
778 v = alchemy_rdsys(a->reg);
779 a->parent = (v >> a->shift) & 3;
780 if (!a->parent) {
781 a->parent = 1;
782 a->isen = 0;
783 } else
784 a->isen = 1;
785 }
786
787 a->hw.init = &id;
788 c = clk_register(NULL, &a->hw);
789 if (IS_ERR(c))
790 ret++;
791 else
792 clk_register_clkdev(c, id.name, NULL);
793 a++;
794 }
795
796 return ret;
797}
798
799/* internal sources muxes *********************************************/
800
801static int alchemy_clk_csrc_isen(struct clk_hw *hw)
802{
803 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
804 unsigned long v = alchemy_rdsys(c->reg);
805
806 return (((v >> c->shift) >> 2) & 7) != 0;
807}
808
809static void __alchemy_clk_csrc_en(struct alchemy_fgcs_clk *c)
810{
811 unsigned long v = alchemy_rdsys(c->reg);
812
813 v &= ~((7 << 2) << c->shift);
814 v |= ((c->parent & 7) << 2) << c->shift;
815 alchemy_wrsys(v, c->reg);
816 c->isen = 1;
817}
818
819static int alchemy_clk_csrc_en(struct clk_hw *hw)
820{
821 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
822 unsigned long flags;
823
824 /* enable by setting the previous parent clock */
825 spin_lock_irqsave(c->reglock, flags);
826 __alchemy_clk_csrc_en(c);
827 spin_unlock_irqrestore(c->reglock, flags);
828
829 return 0;
830}
831
832static void alchemy_clk_csrc_dis(struct clk_hw *hw)
833{
834 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
835 unsigned long v, flags;
836
837 spin_lock_irqsave(c->reglock, flags);
838 v = alchemy_rdsys(c->reg);
839 v &= ~((3 << 2) << c->shift); /* mux to "disabled" state */
840 alchemy_wrsys(v, c->reg);
841 c->isen = 0;
842 spin_unlock_irqrestore(c->reglock, flags);
843}
844
845static int alchemy_clk_csrc_setp(struct clk_hw *hw, u8 index)
846{
847 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
848 unsigned long flags;
849
850 spin_lock_irqsave(c->reglock, flags);
851 c->parent = index + 1; /* value to write to register */
852 if (c->isen)
853 __alchemy_clk_csrc_en(c);
854 spin_unlock_irqrestore(c->reglock, flags);
855
856 return 0;
857}
858
859static u8 alchemy_clk_csrc_getp(struct clk_hw *hw)
860{
861 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
862
863 return c->parent - 1;
864}
865
866static unsigned long alchemy_clk_csrc_recalc(struct clk_hw *hw,
867 unsigned long parent_rate)
868{
869 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
870 unsigned long v = (alchemy_rdsys(c->reg) >> c->shift) & 3;
871
872 return parent_rate / c->dt[v];
873}
874
875static int alchemy_clk_csrc_setr(struct clk_hw *hw, unsigned long rate,
876 unsigned long parent_rate)
877{
878 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
879 unsigned long d, v, flags;
880 int i;
881
882 if (!rate || !parent_rate || rate > parent_rate)
883 return -EINVAL;
884
885 d = (parent_rate + (rate / 2)) / rate;
886 if (d > 4)
887 return -EINVAL;
888 if ((d == 3) && (c->dt[2] != 3))
889 d = 4;
890
891 for (i = 0; i < 4; i++)
892 if (c->dt[i] == d)
893 break;
894
895 if (i >= 4)
896 return -EINVAL; /* oops */
897
898 spin_lock_irqsave(c->reglock, flags);
899 v = alchemy_rdsys(c->reg);
900 v &= ~(3 << c->shift);
901 v |= (i & 3) << c->shift;
902 alchemy_wrsys(v, c->reg);
903 spin_unlock_irqrestore(c->reglock, flags);
904
905 return 0;
906}
907
908static long alchemy_clk_csrc_detr(struct clk_hw *hw, unsigned long rate,
909 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100910 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200911{
912 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
913 int scale = c->dt[2] == 3 ? 1 : 2; /* au1300 check */
914
915 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
916 best_parent_clk, scale, 4);
917}
918
919static struct clk_ops alchemy_clkops_csrc = {
920 .recalc_rate = alchemy_clk_csrc_recalc,
921 .determine_rate = alchemy_clk_csrc_detr,
922 .set_rate = alchemy_clk_csrc_setr,
923 .set_parent = alchemy_clk_csrc_setp,
924 .get_parent = alchemy_clk_csrc_getp,
925 .enable = alchemy_clk_csrc_en,
926 .disable = alchemy_clk_csrc_dis,
927 .is_enabled = alchemy_clk_csrc_isen,
928};
929
930static const char * const alchemy_clk_csrc_parents[] = {
931 /* disabled at index 0 */ ALCHEMY_AUXPLL_CLK,
932 ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
933 ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK
934};
935
936/* divider tables */
937static int alchemy_csrc_dt1[] = { 1, 4, 1, 2 }; /* rest */
938static int alchemy_csrc_dt2[] = { 1, 4, 3, 2 }; /* Au1300 */
939
940static int __init alchemy_clk_setup_imux(int ctype)
941{
942 struct alchemy_fgcs_clk *a;
943 const char * const *names;
944 struct clk_init_data id;
945 unsigned long v;
946 int i, ret, *dt;
947 struct clk *c;
948
949 id.ops = &alchemy_clkops_csrc;
950 id.parent_names = (const char **)alchemy_clk_csrc_parents;
951 id.num_parents = 7;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200952 id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200953
954 dt = alchemy_csrc_dt1;
955 switch (ctype) {
956 case ALCHEMY_CPU_AU1000:
957 names = alchemy_au1000_intclknames;
958 break;
959 case ALCHEMY_CPU_AU1500:
960 names = alchemy_au1500_intclknames;
961 break;
962 case ALCHEMY_CPU_AU1100:
963 names = alchemy_au1100_intclknames;
964 break;
965 case ALCHEMY_CPU_AU1550:
966 names = alchemy_au1550_intclknames;
967 break;
968 case ALCHEMY_CPU_AU1200:
969 names = alchemy_au1200_intclknames;
970 break;
971 case ALCHEMY_CPU_AU1300:
972 dt = alchemy_csrc_dt2;
973 names = alchemy_au1300_intclknames;
974 break;
975 default:
976 return -ENODEV;
977 }
978
979 a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
980 if (!a)
981 return -ENOMEM;
982
983 spin_lock_init(&alchemy_clk_csrc_lock);
984 ret = 0;
985
986 for (i = 0; i < 6; i++) {
987 id.name = names[i];
988 if (!id.name)
989 goto next;
990
991 a->shift = i * 5;
992 a->reg = AU1000_SYS_CLKSRC;
993 a->reglock = &alchemy_clk_csrc_lock;
994 a->dt = dt;
995
996 /* default to first parent clock if mux is initially
997 * set to disabled state.
998 */
999 v = alchemy_rdsys(a->reg);
1000 a->parent = ((v >> a->shift) >> 2) & 7;
1001 if (!a->parent) {
1002 a->parent = 1;
1003 a->isen = 0;
1004 } else
1005 a->isen = 1;
1006
1007 a->hw.init = &id;
1008 c = clk_register(NULL, &a->hw);
1009 if (IS_ERR(c))
1010 ret++;
1011 else
1012 clk_register_clkdev(c, id.name, NULL);
1013next:
1014 a++;
1015 }
1016
1017 return ret;
1018}
1019
1020
1021/**********************************************************************/
1022
1023
1024#define ERRCK(x) \
1025 if (IS_ERR(x)) { \
1026 ret = PTR_ERR(x); \
1027 goto out; \
1028 }
1029
1030static int __init alchemy_clk_init(void)
1031{
1032 int ctype = alchemy_get_cputype(), ret, i;
1033 struct clk_aliastable *t = alchemy_clk_aliases;
1034 struct clk *c;
1035
1036 /* Root of the Alchemy clock tree: external 12MHz crystal osc */
1037 c = clk_register_fixed_rate(NULL, ALCHEMY_ROOT_CLK, NULL,
1038 CLK_IS_ROOT,
1039 ALCHEMY_ROOTCLK_RATE);
1040 ERRCK(c)
1041
1042 /* CPU core clock */
1043 c = alchemy_clk_setup_cpu(ALCHEMY_ROOT_CLK, ctype);
1044 ERRCK(c)
1045
1046 /* AUXPLLs: max 1GHz on Au1300, 748MHz on older models */
1047 i = (ctype == ALCHEMY_CPU_AU1300) ? 84 : 63;
1048 c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK, ALCHEMY_AUXPLL_CLK,
1049 i, AU1000_SYS_AUXPLL);
1050 ERRCK(c)
1051
1052 if (ctype == ALCHEMY_CPU_AU1300) {
1053 c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK,
1054 ALCHEMY_AUXPLL2_CLK, i,
1055 AU1300_SYS_AUXPLL2);
1056 ERRCK(c)
1057 }
1058
1059 /* sysbus clock: cpu core clock divided by 2, 3 or 4 */
1060 c = alchemy_clk_setup_sysbus(ALCHEMY_CPU_CLK);
1061 ERRCK(c)
1062
1063 /* peripheral clock: runs at half rate of sysbus clk */
1064 c = alchemy_clk_setup_periph(ALCHEMY_SYSBUS_CLK);
1065 ERRCK(c)
1066
1067 /* SDR/DDR memory clock */
1068 c = alchemy_clk_setup_mem(ALCHEMY_SYSBUS_CLK, ctype);
1069 ERRCK(c)
1070
1071 /* L/RCLK: external static bus clock for synchronous mode */
Manuel Lauss51f105d32015-01-29 16:06:42 +01001072 c = alchemy_clk_setup_lrclk(ALCHEMY_PERIPH_CLK, ctype);
Manuel Lauss47440222014-07-23 16:36:48 +02001073 ERRCK(c)
1074
1075 /* Frequency dividers 0-5 */
1076 ret = alchemy_clk_init_fgens(ctype);
1077 if (ret) {
1078 ret = -ENODEV;
1079 goto out;
1080 }
1081
1082 /* diving muxes for internal sources */
1083 ret = alchemy_clk_setup_imux(ctype);
1084 if (ret) {
1085 ret = -ENODEV;
1086 goto out;
1087 }
1088
1089 /* set up aliases drivers might look for */
1090 while (t->base) {
1091 if (t->cputype == ctype)
1092 clk_add_alias(t->alias, NULL, t->base, NULL);
1093 t++;
1094 }
1095
1096 pr_info("Alchemy clocktree installed\n");
1097 return 0;
1098
1099out:
1100 return ret;
1101}
1102postcore_initcall(alchemy_clk_init);