blob: 4b5ec49139c227f062cffa71fa6155c0c124f97b [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;
Manuel Lauss69e4e632015-02-18 11:01:56 +0100130 if (alchemy_get_cputype() < ALCHEMY_CPU_AU1300)
131 t &= 0x3f;
Manuel Lauss47440222014-07-23 16:36:48 +0200132 t *= parent_rate;
133 }
134
135 return t;
136}
137
Manuel Lauss45a848f2015-01-29 16:06:43 +0100138void __init alchemy_set_lpj(void)
139{
140 preset_lpj = alchemy_clk_cpu_recalc(NULL, ALCHEMY_ROOTCLK_RATE);
141 preset_lpj /= 2 * HZ;
142}
143
Manuel Lauss47440222014-07-23 16:36:48 +0200144static struct clk_ops alchemy_clkops_cpu = {
145 .recalc_rate = alchemy_clk_cpu_recalc,
146};
147
148static struct clk __init *alchemy_clk_setup_cpu(const char *parent_name,
149 int ctype)
150{
151 struct clk_init_data id;
152 struct clk_hw *h;
153
154 h = kzalloc(sizeof(*h), GFP_KERNEL);
155 if (!h)
156 return ERR_PTR(-ENOMEM);
157
158 id.name = ALCHEMY_CPU_CLK;
159 id.parent_names = &parent_name;
160 id.num_parents = 1;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200161 id.flags = CLK_IS_BASIC;
Manuel Lauss47440222014-07-23 16:36:48 +0200162 id.ops = &alchemy_clkops_cpu;
163 h->init = &id;
164
165 return clk_register(NULL, h);
166}
167
168/* AUXPLLs ************************************************************/
169
170struct alchemy_auxpll_clk {
171 struct clk_hw hw;
172 unsigned long reg; /* au1300 has also AUXPLL2 */
173 int maxmult; /* max multiplier */
174};
175#define to_auxpll_clk(x) container_of(x, struct alchemy_auxpll_clk, hw)
176
177static unsigned long alchemy_clk_aux_recalc(struct clk_hw *hw,
178 unsigned long parent_rate)
179{
180 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
181
182 return (alchemy_rdsys(a->reg) & 0xff) * parent_rate;
183}
184
185static int alchemy_clk_aux_setr(struct clk_hw *hw,
186 unsigned long rate,
187 unsigned long parent_rate)
188{
189 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
190 unsigned long d = rate;
191
192 if (rate)
193 d /= parent_rate;
194 else
195 d = 0;
196
197 /* minimum is 84MHz, max is 756-1032 depending on variant */
198 if (((d < 7) && (d != 0)) || (d > a->maxmult))
199 return -EINVAL;
200
201 alchemy_wrsys(d, a->reg);
202 return 0;
203}
204
205static long alchemy_clk_aux_roundr(struct clk_hw *hw,
206 unsigned long rate,
207 unsigned long *parent_rate)
208{
209 struct alchemy_auxpll_clk *a = to_auxpll_clk(hw);
210 unsigned long mult;
211
212 if (!rate || !*parent_rate)
213 return 0;
214
215 mult = rate / (*parent_rate);
216
217 if (mult && (mult < 7))
218 mult = 7;
219 if (mult > a->maxmult)
220 mult = a->maxmult;
221
222 return (*parent_rate) * mult;
223}
224
225static struct clk_ops alchemy_clkops_aux = {
226 .recalc_rate = alchemy_clk_aux_recalc,
227 .set_rate = alchemy_clk_aux_setr,
228 .round_rate = alchemy_clk_aux_roundr,
229};
230
231static struct clk __init *alchemy_clk_setup_aux(const char *parent_name,
232 char *name, int maxmult,
233 unsigned long reg)
234{
235 struct clk_init_data id;
236 struct clk *c;
237 struct alchemy_auxpll_clk *a;
238
239 a = kzalloc(sizeof(*a), GFP_KERNEL);
240 if (!a)
241 return ERR_PTR(-ENOMEM);
242
243 id.name = name;
244 id.parent_names = &parent_name;
245 id.num_parents = 1;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200246 id.flags = CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200247 id.ops = &alchemy_clkops_aux;
248
249 a->reg = reg;
250 a->maxmult = maxmult;
251 a->hw.init = &id;
252
253 c = clk_register(NULL, &a->hw);
254 if (!IS_ERR(c))
255 clk_register_clkdev(c, name, NULL);
256 else
257 kfree(a);
258
259 return c;
260}
261
262/* sysbus_clk *********************************************************/
263
264static struct clk __init *alchemy_clk_setup_sysbus(const char *pn)
265{
266 unsigned long v = (alchemy_rdsys(AU1000_SYS_POWERCTRL) & 3) + 2;
267 struct clk *c;
268
269 c = clk_register_fixed_factor(NULL, ALCHEMY_SYSBUS_CLK,
270 pn, 0, 1, v);
271 if (!IS_ERR(c))
272 clk_register_clkdev(c, ALCHEMY_SYSBUS_CLK, NULL);
273 return c;
274}
275
276/* Peripheral Clock ***************************************************/
277
278static struct clk __init *alchemy_clk_setup_periph(const char *pn)
279{
280 /* Peripheral clock runs at half the rate of sysbus clk */
281 struct clk *c;
282
283 c = clk_register_fixed_factor(NULL, ALCHEMY_PERIPH_CLK,
284 pn, 0, 1, 2);
285 if (!IS_ERR(c))
286 clk_register_clkdev(c, ALCHEMY_PERIPH_CLK, NULL);
287 return c;
288}
289
290/* mem clock **********************************************************/
291
292static struct clk __init *alchemy_clk_setup_mem(const char *pn, int ct)
293{
294 void __iomem *addr = IOMEM(AU1000_MEM_PHYS_ADDR);
295 unsigned long v;
296 struct clk *c;
297 int div;
298
299 switch (ct) {
300 case ALCHEMY_CPU_AU1550:
301 case ALCHEMY_CPU_AU1200:
302 v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
303 div = (v & (1 << 15)) ? 1 : 2;
304 break;
305 case ALCHEMY_CPU_AU1300:
306 v = __raw_readl(addr + AU1550_MEM_SDCONFIGB);
307 div = (v & (1 << 31)) ? 1 : 2;
308 break;
309 case ALCHEMY_CPU_AU1000:
310 case ALCHEMY_CPU_AU1500:
311 case ALCHEMY_CPU_AU1100:
312 default:
313 div = 2;
314 break;
315 }
316
317 c = clk_register_fixed_factor(NULL, ALCHEMY_MEM_CLK, pn,
318 0, 1, div);
319 if (!IS_ERR(c))
320 clk_register_clkdev(c, ALCHEMY_MEM_CLK, NULL);
321 return c;
322}
323
324/* lrclk: external synchronous static bus clock ***********************/
325
Manuel Lauss51f105d32015-01-29 16:06:42 +0100326static struct clk __init *alchemy_clk_setup_lrclk(const char *pn, int t)
Manuel Lauss47440222014-07-23 16:36:48 +0200327{
Manuel Lauss51f105d32015-01-29 16:06:42 +0100328 /* Au1000, Au1500: MEM_STCFG0[11]: If bit is set, lrclk=pclk/5,
329 * otherwise lrclk=pclk/4.
330 * All other variants: MEM_STCFG0[15:13] = divisor.
Manuel Lauss47440222014-07-23 16:36:48 +0200331 * L/RCLK = periph_clk / (divisor + 1)
332 * On Au1000, Au1500, Au1100 it's called LCLK,
333 * on later models it's called RCLK, but it's the same thing.
334 */
335 struct clk *c;
Manuel Lauss51f105d32015-01-29 16:06:42 +0100336 unsigned long v = alchemy_rdsmem(AU1000_MEM_STCFG0);
Manuel Lauss47440222014-07-23 16:36:48 +0200337
Manuel Lauss51f105d32015-01-29 16:06:42 +0100338 switch (t) {
339 case ALCHEMY_CPU_AU1000:
340 case ALCHEMY_CPU_AU1500:
341 v = 4 + ((v >> 11) & 1);
342 break;
343 default: /* all other models */
344 v = ((v >> 13) & 7) + 1;
345 }
Manuel Lauss47440222014-07-23 16:36:48 +0200346 c = clk_register_fixed_factor(NULL, ALCHEMY_LR_CLK,
347 pn, 0, 1, v);
348 if (!IS_ERR(c))
349 clk_register_clkdev(c, ALCHEMY_LR_CLK, NULL);
350 return c;
351}
352
353/* Clock dividers and muxes *******************************************/
354
355/* data for fgen and csrc mux-dividers */
356struct alchemy_fgcs_clk {
357 struct clk_hw hw;
358 spinlock_t *reglock; /* register lock */
359 unsigned long reg; /* SYS_FREQCTRL0/1 */
360 int shift; /* offset in register */
361 int parent; /* parent before disable [Au1300] */
362 int isen; /* is it enabled? */
363 int *dt; /* dividertable for csrc */
364};
365#define to_fgcs_clk(x) container_of(x, struct alchemy_fgcs_clk, hw)
366
367static long alchemy_calc_div(unsigned long rate, unsigned long prate,
368 int scale, int maxdiv, unsigned long *rv)
369{
370 long div1, div2;
371
372 div1 = prate / rate;
373 if ((prate / div1) > rate)
374 div1++;
375
376 if (scale == 2) { /* only div-by-multiple-of-2 possible */
377 if (div1 & 1)
378 div1++; /* stay <=prate */
379 }
380
381 div2 = (div1 / scale) - 1; /* value to write to register */
382
383 if (div2 > maxdiv)
384 div2 = maxdiv;
385 if (rv)
386 *rv = div2;
387
388 div1 = ((div2 + 1) * scale);
389 return div1;
390}
391
392static long alchemy_clk_fgcs_detr(struct clk_hw *hw, unsigned long rate,
393 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100394 struct clk_hw **best_parent_clk,
Manuel Lauss47440222014-07-23 16:36:48 +0200395 int scale, int maxdiv)
396{
397 struct clk *pc, *bpc, *free;
398 long tdv, tpr, pr, nr, br, bpr, diff, lastdiff;
399 int j;
400
401 lastdiff = INT_MAX;
402 bpr = 0;
403 bpc = NULL;
404 br = -EINVAL;
405 free = NULL;
406
407 /* look at the rates each enabled parent supplies and select
408 * the one that gets closest to but not over the requested rate.
409 */
410 for (j = 0; j < 7; j++) {
411 pc = clk_get_parent_by_index(hw->clk, j);
412 if (!pc)
413 break;
414
415 /* if this parent is currently unused, remember it.
Tomeu Vizoso24c71c82014-10-20 15:40:01 +0200416 * XXX: we would actually want clk_has_active_children()
417 * but this is a good-enough approximation for now.
Manuel Lauss47440222014-07-23 16:36:48 +0200418 */
Tomeu Vizoso24c71c82014-10-20 15:40:01 +0200419 if (!__clk_is_prepared(pc)) {
Manuel Lauss47440222014-07-23 16:36:48 +0200420 if (!free)
421 free = pc;
422 }
423
424 pr = clk_get_rate(pc);
425 if (pr < rate)
426 continue;
427
428 /* what can hardware actually provide */
429 tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
430 nr = pr / tdv;
431 diff = rate - nr;
432 if (nr > rate)
433 continue;
434
435 if (diff < lastdiff) {
436 lastdiff = diff;
437 bpr = pr;
438 bpc = pc;
439 br = nr;
440 }
441 if (diff == 0)
442 break;
443 }
444
445 /* if we couldn't get the exact rate we wanted from the enabled
446 * parents, maybe we can tell an available disabled/inactive one
447 * to give us a rate we can divide down to the requested rate.
448 */
449 if (lastdiff && free) {
450 for (j = (maxdiv == 4) ? 1 : scale; j <= maxdiv; j += scale) {
451 tpr = rate * j;
452 if (tpr < 0)
453 break;
454 pr = clk_round_rate(free, tpr);
455
456 tdv = alchemy_calc_div(rate, pr, scale, maxdiv, NULL);
457 nr = pr / tdv;
458 diff = rate - nr;
459 if (nr > rate)
460 continue;
461 if (diff < lastdiff) {
462 lastdiff = diff;
463 bpr = pr;
464 bpc = free;
465 br = nr;
466 }
467 if (diff == 0)
468 break;
469 }
470 }
471
472 *best_parent_rate = bpr;
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100473 *best_parent_clk = __clk_get_hw(bpc);
Manuel Lauss47440222014-07-23 16:36:48 +0200474 return br;
475}
476
477static int alchemy_clk_fgv1_en(struct clk_hw *hw)
478{
479 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
480 unsigned long v, flags;
481
482 spin_lock_irqsave(c->reglock, flags);
483 v = alchemy_rdsys(c->reg);
484 v |= (1 << 1) << c->shift;
485 alchemy_wrsys(v, c->reg);
486 spin_unlock_irqrestore(c->reglock, flags);
487
488 return 0;
489}
490
491static int alchemy_clk_fgv1_isen(struct clk_hw *hw)
492{
493 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
494 unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 1);
495
496 return v & 1;
497}
498
499static void alchemy_clk_fgv1_dis(struct clk_hw *hw)
500{
501 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
502 unsigned long v, flags;
503
504 spin_lock_irqsave(c->reglock, flags);
505 v = alchemy_rdsys(c->reg);
506 v &= ~((1 << 1) << c->shift);
507 alchemy_wrsys(v, c->reg);
508 spin_unlock_irqrestore(c->reglock, flags);
509}
510
511static int alchemy_clk_fgv1_setp(struct clk_hw *hw, u8 index)
512{
513 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
514 unsigned long v, flags;
515
516 spin_lock_irqsave(c->reglock, flags);
517 v = alchemy_rdsys(c->reg);
518 if (index)
519 v |= (1 << c->shift);
520 else
521 v &= ~(1 << c->shift);
522 alchemy_wrsys(v, c->reg);
523 spin_unlock_irqrestore(c->reglock, flags);
524
525 return 0;
526}
527
528static u8 alchemy_clk_fgv1_getp(struct clk_hw *hw)
529{
530 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
531
532 return (alchemy_rdsys(c->reg) >> c->shift) & 1;
533}
534
535static int alchemy_clk_fgv1_setr(struct clk_hw *hw, unsigned long rate,
536 unsigned long parent_rate)
537{
538 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
539 unsigned long div, v, flags, ret;
540 int sh = c->shift + 2;
541
542 if (!rate || !parent_rate || rate > (parent_rate / 2))
543 return -EINVAL;
544 ret = alchemy_calc_div(rate, parent_rate, 2, 512, &div);
545 spin_lock_irqsave(c->reglock, flags);
546 v = alchemy_rdsys(c->reg);
547 v &= ~(0xff << sh);
548 v |= div << sh;
549 alchemy_wrsys(v, c->reg);
550 spin_unlock_irqrestore(c->reglock, flags);
551
552 return 0;
553}
554
555static unsigned long alchemy_clk_fgv1_recalc(struct clk_hw *hw,
556 unsigned long parent_rate)
557{
558 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
559 unsigned long v = alchemy_rdsys(c->reg) >> (c->shift + 2);
560
561 v = ((v & 0xff) + 1) * 2;
562 return parent_rate / v;
563}
564
565static long alchemy_clk_fgv1_detr(struct clk_hw *hw, unsigned long rate,
566 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100567 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200568{
569 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
570 best_parent_clk, 2, 512);
571}
572
573/* Au1000, Au1100, Au15x0, Au12x0 */
574static struct clk_ops alchemy_clkops_fgenv1 = {
575 .recalc_rate = alchemy_clk_fgv1_recalc,
576 .determine_rate = alchemy_clk_fgv1_detr,
577 .set_rate = alchemy_clk_fgv1_setr,
578 .set_parent = alchemy_clk_fgv1_setp,
579 .get_parent = alchemy_clk_fgv1_getp,
580 .enable = alchemy_clk_fgv1_en,
581 .disable = alchemy_clk_fgv1_dis,
582 .is_enabled = alchemy_clk_fgv1_isen,
583};
584
585static void __alchemy_clk_fgv2_en(struct alchemy_fgcs_clk *c)
586{
587 unsigned long v = alchemy_rdsys(c->reg);
588
589 v &= ~(3 << c->shift);
590 v |= (c->parent & 3) << c->shift;
591 alchemy_wrsys(v, c->reg);
592 c->isen = 1;
593}
594
595static int alchemy_clk_fgv2_en(struct clk_hw *hw)
596{
597 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
598 unsigned long flags;
599
600 /* enable by setting the previous parent clock */
601 spin_lock_irqsave(c->reglock, flags);
602 __alchemy_clk_fgv2_en(c);
603 spin_unlock_irqrestore(c->reglock, flags);
604
605 return 0;
606}
607
608static int alchemy_clk_fgv2_isen(struct clk_hw *hw)
609{
610 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
611
612 return ((alchemy_rdsys(c->reg) >> c->shift) & 3) != 0;
613}
614
615static void alchemy_clk_fgv2_dis(struct clk_hw *hw)
616{
617 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
618 unsigned long v, flags;
619
620 spin_lock_irqsave(c->reglock, flags);
621 v = alchemy_rdsys(c->reg);
622 v &= ~(3 << c->shift); /* set input mux to "disabled" state */
623 alchemy_wrsys(v, c->reg);
624 c->isen = 0;
625 spin_unlock_irqrestore(c->reglock, flags);
626}
627
628static int alchemy_clk_fgv2_setp(struct clk_hw *hw, u8 index)
629{
630 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
631 unsigned long flags;
632
633 spin_lock_irqsave(c->reglock, flags);
634 c->parent = index + 1; /* value to write to register */
635 if (c->isen)
636 __alchemy_clk_fgv2_en(c);
637 spin_unlock_irqrestore(c->reglock, flags);
638
639 return 0;
640}
641
642static u8 alchemy_clk_fgv2_getp(struct clk_hw *hw)
643{
644 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
645 unsigned long flags, v;
646
647 spin_lock_irqsave(c->reglock, flags);
648 v = c->parent - 1;
649 spin_unlock_irqrestore(c->reglock, flags);
650 return v;
651}
652
653/* fg0-2 and fg4-6 share a "scale"-bit. With this bit cleared, the
654 * dividers behave exactly as on previous models (dividers are multiples
655 * of 2); with the bit set, dividers are multiples of 1, halving their
656 * range, but making them also much more flexible.
657 */
658static int alchemy_clk_fgv2_setr(struct clk_hw *hw, unsigned long rate,
659 unsigned long parent_rate)
660{
661 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
662 int sh = c->shift + 2;
663 unsigned long div, v, flags, ret;
664
665 if (!rate || !parent_rate || rate > parent_rate)
666 return -EINVAL;
667
668 v = alchemy_rdsys(c->reg) & (1 << 30); /* test "scale" bit */
669 ret = alchemy_calc_div(rate, parent_rate, v ? 1 : 2,
670 v ? 256 : 512, &div);
671
672 spin_lock_irqsave(c->reglock, flags);
673 v = alchemy_rdsys(c->reg);
674 v &= ~(0xff << sh);
675 v |= (div & 0xff) << sh;
676 alchemy_wrsys(v, c->reg);
677 spin_unlock_irqrestore(c->reglock, flags);
678
679 return 0;
680}
681
682static unsigned long alchemy_clk_fgv2_recalc(struct clk_hw *hw,
683 unsigned long parent_rate)
684{
685 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
686 int sh = c->shift + 2;
687 unsigned long v, t;
688
689 v = alchemy_rdsys(c->reg);
690 t = parent_rate / (((v >> sh) & 0xff) + 1);
691 if ((v & (1 << 30)) == 0) /* test scale bit */
692 t /= 2;
693
694 return t;
695}
696
697static long alchemy_clk_fgv2_detr(struct clk_hw *hw, unsigned long rate,
698 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100699 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200700{
701 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
702 int scale, maxdiv;
703
704 if (alchemy_rdsys(c->reg) & (1 << 30)) {
705 scale = 1;
706 maxdiv = 256;
707 } else {
708 scale = 2;
709 maxdiv = 512;
710 }
711
712 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
713 best_parent_clk, scale, maxdiv);
714}
715
716/* Au1300 larger input mux, no separate disable bit, flexible divider */
717static struct clk_ops alchemy_clkops_fgenv2 = {
718 .recalc_rate = alchemy_clk_fgv2_recalc,
719 .determine_rate = alchemy_clk_fgv2_detr,
720 .set_rate = alchemy_clk_fgv2_setr,
721 .set_parent = alchemy_clk_fgv2_setp,
722 .get_parent = alchemy_clk_fgv2_getp,
723 .enable = alchemy_clk_fgv2_en,
724 .disable = alchemy_clk_fgv2_dis,
725 .is_enabled = alchemy_clk_fgv2_isen,
726};
727
728static const char * const alchemy_clk_fgv1_parents[] = {
729 ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
730};
731
732static const char * const alchemy_clk_fgv2_parents[] = {
733 ALCHEMY_AUXPLL2_CLK, ALCHEMY_CPU_CLK, ALCHEMY_AUXPLL_CLK
734};
735
736static const char * const alchemy_clk_fgen_names[] = {
737 ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
738 ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK };
739
740static int __init alchemy_clk_init_fgens(int ctype)
741{
742 struct clk *c;
743 struct clk_init_data id;
744 struct alchemy_fgcs_clk *a;
745 unsigned long v;
746 int i, ret;
747
748 switch (ctype) {
749 case ALCHEMY_CPU_AU1000...ALCHEMY_CPU_AU1200:
750 id.ops = &alchemy_clkops_fgenv1;
751 id.parent_names = (const char **)alchemy_clk_fgv1_parents;
752 id.num_parents = 2;
753 break;
754 case ALCHEMY_CPU_AU1300:
755 id.ops = &alchemy_clkops_fgenv2;
756 id.parent_names = (const char **)alchemy_clk_fgv2_parents;
757 id.num_parents = 3;
758 break;
759 default:
760 return -ENODEV;
761 }
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200762 id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200763
764 a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
765 if (!a)
766 return -ENOMEM;
767
768 spin_lock_init(&alchemy_clk_fg0_lock);
769 spin_lock_init(&alchemy_clk_fg1_lock);
770 ret = 0;
771 for (i = 0; i < 6; i++) {
772 id.name = alchemy_clk_fgen_names[i];
773 a->shift = 10 * (i < 3 ? i : i - 3);
774 if (i > 2) {
775 a->reg = AU1000_SYS_FREQCTRL1;
776 a->reglock = &alchemy_clk_fg1_lock;
777 } else {
778 a->reg = AU1000_SYS_FREQCTRL0;
779 a->reglock = &alchemy_clk_fg0_lock;
780 }
781
782 /* default to first parent if bootloader has set
783 * the mux to disabled state.
784 */
785 if (ctype == ALCHEMY_CPU_AU1300) {
786 v = alchemy_rdsys(a->reg);
787 a->parent = (v >> a->shift) & 3;
788 if (!a->parent) {
789 a->parent = 1;
790 a->isen = 0;
791 } else
792 a->isen = 1;
793 }
794
795 a->hw.init = &id;
796 c = clk_register(NULL, &a->hw);
797 if (IS_ERR(c))
798 ret++;
799 else
800 clk_register_clkdev(c, id.name, NULL);
801 a++;
802 }
803
804 return ret;
805}
806
807/* internal sources muxes *********************************************/
808
809static int alchemy_clk_csrc_isen(struct clk_hw *hw)
810{
811 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
812 unsigned long v = alchemy_rdsys(c->reg);
813
814 return (((v >> c->shift) >> 2) & 7) != 0;
815}
816
817static void __alchemy_clk_csrc_en(struct alchemy_fgcs_clk *c)
818{
819 unsigned long v = alchemy_rdsys(c->reg);
820
821 v &= ~((7 << 2) << c->shift);
822 v |= ((c->parent & 7) << 2) << c->shift;
823 alchemy_wrsys(v, c->reg);
824 c->isen = 1;
825}
826
827static int alchemy_clk_csrc_en(struct clk_hw *hw)
828{
829 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
830 unsigned long flags;
831
832 /* enable by setting the previous parent clock */
833 spin_lock_irqsave(c->reglock, flags);
834 __alchemy_clk_csrc_en(c);
835 spin_unlock_irqrestore(c->reglock, flags);
836
837 return 0;
838}
839
840static void alchemy_clk_csrc_dis(struct clk_hw *hw)
841{
842 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
843 unsigned long v, flags;
844
845 spin_lock_irqsave(c->reglock, flags);
846 v = alchemy_rdsys(c->reg);
847 v &= ~((3 << 2) << c->shift); /* mux to "disabled" state */
848 alchemy_wrsys(v, c->reg);
849 c->isen = 0;
850 spin_unlock_irqrestore(c->reglock, flags);
851}
852
853static int alchemy_clk_csrc_setp(struct clk_hw *hw, u8 index)
854{
855 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
856 unsigned long flags;
857
858 spin_lock_irqsave(c->reglock, flags);
859 c->parent = index + 1; /* value to write to register */
860 if (c->isen)
861 __alchemy_clk_csrc_en(c);
862 spin_unlock_irqrestore(c->reglock, flags);
863
864 return 0;
865}
866
867static u8 alchemy_clk_csrc_getp(struct clk_hw *hw)
868{
869 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
870
871 return c->parent - 1;
872}
873
874static unsigned long alchemy_clk_csrc_recalc(struct clk_hw *hw,
875 unsigned long parent_rate)
876{
877 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
878 unsigned long v = (alchemy_rdsys(c->reg) >> c->shift) & 3;
879
880 return parent_rate / c->dt[v];
881}
882
883static int alchemy_clk_csrc_setr(struct clk_hw *hw, unsigned long rate,
884 unsigned long parent_rate)
885{
886 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
887 unsigned long d, v, flags;
888 int i;
889
890 if (!rate || !parent_rate || rate > parent_rate)
891 return -EINVAL;
892
893 d = (parent_rate + (rate / 2)) / rate;
894 if (d > 4)
895 return -EINVAL;
896 if ((d == 3) && (c->dt[2] != 3))
897 d = 4;
898
899 for (i = 0; i < 4; i++)
900 if (c->dt[i] == d)
901 break;
902
903 if (i >= 4)
904 return -EINVAL; /* oops */
905
906 spin_lock_irqsave(c->reglock, flags);
907 v = alchemy_rdsys(c->reg);
908 v &= ~(3 << c->shift);
909 v |= (i & 3) << c->shift;
910 alchemy_wrsys(v, c->reg);
911 spin_unlock_irqrestore(c->reglock, flags);
912
913 return 0;
914}
915
916static long alchemy_clk_csrc_detr(struct clk_hw *hw, unsigned long rate,
917 unsigned long *best_parent_rate,
Tomeu Vizoso646cafc2014-12-02 08:54:22 +0100918 struct clk_hw **best_parent_clk)
Manuel Lauss47440222014-07-23 16:36:48 +0200919{
920 struct alchemy_fgcs_clk *c = to_fgcs_clk(hw);
921 int scale = c->dt[2] == 3 ? 1 : 2; /* au1300 check */
922
923 return alchemy_clk_fgcs_detr(hw, rate, best_parent_rate,
924 best_parent_clk, scale, 4);
925}
926
927static struct clk_ops alchemy_clkops_csrc = {
928 .recalc_rate = alchemy_clk_csrc_recalc,
929 .determine_rate = alchemy_clk_csrc_detr,
930 .set_rate = alchemy_clk_csrc_setr,
931 .set_parent = alchemy_clk_csrc_setp,
932 .get_parent = alchemy_clk_csrc_getp,
933 .enable = alchemy_clk_csrc_en,
934 .disable = alchemy_clk_csrc_dis,
935 .is_enabled = alchemy_clk_csrc_isen,
936};
937
938static const char * const alchemy_clk_csrc_parents[] = {
939 /* disabled at index 0 */ ALCHEMY_AUXPLL_CLK,
940 ALCHEMY_FG0_CLK, ALCHEMY_FG1_CLK, ALCHEMY_FG2_CLK,
941 ALCHEMY_FG3_CLK, ALCHEMY_FG4_CLK, ALCHEMY_FG5_CLK
942};
943
944/* divider tables */
945static int alchemy_csrc_dt1[] = { 1, 4, 1, 2 }; /* rest */
946static int alchemy_csrc_dt2[] = { 1, 4, 3, 2 }; /* Au1300 */
947
948static int __init alchemy_clk_setup_imux(int ctype)
949{
950 struct alchemy_fgcs_clk *a;
951 const char * const *names;
952 struct clk_init_data id;
953 unsigned long v;
954 int i, ret, *dt;
955 struct clk *c;
956
957 id.ops = &alchemy_clkops_csrc;
958 id.parent_names = (const char **)alchemy_clk_csrc_parents;
959 id.num_parents = 7;
Manuel Lauss72e1e2a2014-07-23 16:36:57 +0200960 id.flags = CLK_SET_RATE_PARENT | CLK_GET_RATE_NOCACHE;
Manuel Lauss47440222014-07-23 16:36:48 +0200961
962 dt = alchemy_csrc_dt1;
963 switch (ctype) {
964 case ALCHEMY_CPU_AU1000:
965 names = alchemy_au1000_intclknames;
966 break;
967 case ALCHEMY_CPU_AU1500:
968 names = alchemy_au1500_intclknames;
969 break;
970 case ALCHEMY_CPU_AU1100:
971 names = alchemy_au1100_intclknames;
972 break;
973 case ALCHEMY_CPU_AU1550:
974 names = alchemy_au1550_intclknames;
975 break;
976 case ALCHEMY_CPU_AU1200:
977 names = alchemy_au1200_intclknames;
978 break;
979 case ALCHEMY_CPU_AU1300:
980 dt = alchemy_csrc_dt2;
981 names = alchemy_au1300_intclknames;
982 break;
983 default:
984 return -ENODEV;
985 }
986
987 a = kzalloc((sizeof(*a)) * 6, GFP_KERNEL);
988 if (!a)
989 return -ENOMEM;
990
991 spin_lock_init(&alchemy_clk_csrc_lock);
992 ret = 0;
993
994 for (i = 0; i < 6; i++) {
995 id.name = names[i];
996 if (!id.name)
997 goto next;
998
999 a->shift = i * 5;
1000 a->reg = AU1000_SYS_CLKSRC;
1001 a->reglock = &alchemy_clk_csrc_lock;
1002 a->dt = dt;
1003
1004 /* default to first parent clock if mux is initially
1005 * set to disabled state.
1006 */
1007 v = alchemy_rdsys(a->reg);
1008 a->parent = ((v >> a->shift) >> 2) & 7;
1009 if (!a->parent) {
1010 a->parent = 1;
1011 a->isen = 0;
1012 } else
1013 a->isen = 1;
1014
1015 a->hw.init = &id;
1016 c = clk_register(NULL, &a->hw);
1017 if (IS_ERR(c))
1018 ret++;
1019 else
1020 clk_register_clkdev(c, id.name, NULL);
1021next:
1022 a++;
1023 }
1024
1025 return ret;
1026}
1027
1028
1029/**********************************************************************/
1030
1031
1032#define ERRCK(x) \
1033 if (IS_ERR(x)) { \
1034 ret = PTR_ERR(x); \
1035 goto out; \
1036 }
1037
1038static int __init alchemy_clk_init(void)
1039{
1040 int ctype = alchemy_get_cputype(), ret, i;
1041 struct clk_aliastable *t = alchemy_clk_aliases;
1042 struct clk *c;
1043
1044 /* Root of the Alchemy clock tree: external 12MHz crystal osc */
1045 c = clk_register_fixed_rate(NULL, ALCHEMY_ROOT_CLK, NULL,
1046 CLK_IS_ROOT,
1047 ALCHEMY_ROOTCLK_RATE);
1048 ERRCK(c)
1049
1050 /* CPU core clock */
1051 c = alchemy_clk_setup_cpu(ALCHEMY_ROOT_CLK, ctype);
1052 ERRCK(c)
1053
1054 /* AUXPLLs: max 1GHz on Au1300, 748MHz on older models */
1055 i = (ctype == ALCHEMY_CPU_AU1300) ? 84 : 63;
1056 c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK, ALCHEMY_AUXPLL_CLK,
1057 i, AU1000_SYS_AUXPLL);
1058 ERRCK(c)
1059
1060 if (ctype == ALCHEMY_CPU_AU1300) {
1061 c = alchemy_clk_setup_aux(ALCHEMY_ROOT_CLK,
1062 ALCHEMY_AUXPLL2_CLK, i,
1063 AU1300_SYS_AUXPLL2);
1064 ERRCK(c)
1065 }
1066
1067 /* sysbus clock: cpu core clock divided by 2, 3 or 4 */
1068 c = alchemy_clk_setup_sysbus(ALCHEMY_CPU_CLK);
1069 ERRCK(c)
1070
1071 /* peripheral clock: runs at half rate of sysbus clk */
1072 c = alchemy_clk_setup_periph(ALCHEMY_SYSBUS_CLK);
1073 ERRCK(c)
1074
1075 /* SDR/DDR memory clock */
1076 c = alchemy_clk_setup_mem(ALCHEMY_SYSBUS_CLK, ctype);
1077 ERRCK(c)
1078
1079 /* L/RCLK: external static bus clock for synchronous mode */
Manuel Lauss51f105d32015-01-29 16:06:42 +01001080 c = alchemy_clk_setup_lrclk(ALCHEMY_PERIPH_CLK, ctype);
Manuel Lauss47440222014-07-23 16:36:48 +02001081 ERRCK(c)
1082
1083 /* Frequency dividers 0-5 */
1084 ret = alchemy_clk_init_fgens(ctype);
1085 if (ret) {
1086 ret = -ENODEV;
1087 goto out;
1088 }
1089
1090 /* diving muxes for internal sources */
1091 ret = alchemy_clk_setup_imux(ctype);
1092 if (ret) {
1093 ret = -ENODEV;
1094 goto out;
1095 }
1096
1097 /* set up aliases drivers might look for */
1098 while (t->base) {
1099 if (t->cputype == ctype)
1100 clk_add_alias(t->alias, NULL, t->base, NULL);
1101 t++;
1102 }
1103
1104 pr_info("Alchemy clocktree installed\n");
1105 return 0;
1106
1107out:
1108 return ret;
1109}
1110postcore_initcall(alchemy_clk_init);