blob: 9563285674b33593f77567f92e9547d89eab9047 [file] [log] [blame]
Claudiu Beznea653bcce2020-09-07 17:46:39 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Compatible code for non CCF AT91 platforms.
4 *
5 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
6 *
7 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
8 */
9#include <common.h>
10#include <clk-uclass.h>
11#include <dm.h>
Tom Rini68963972020-10-15 21:44:43 -040012#include <dm/device_compat.h>
Claudiu Beznea653bcce2020-09-07 17:46:39 +030013#include <dm/lists.h>
14#include <dm/util.h>
15#include <mach/at91_pmc.h>
16#include <mach/at91_sfr.h>
17#include <regmap.h>
18#include <syscon.h>
19
20#include "pmc.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
24struct pmc_platdata {
25 struct at91_pmc *reg_base;
26 struct regmap *regmap_sfr;
27};
28
29static const struct udevice_id at91_pmc_match[] = {
30 { .compatible = "atmel,at91rm9200-pmc" },
31 { .compatible = "atmel,at91sam9260-pmc" },
32 { .compatible = "atmel,at91sam9g45-pmc" },
33 { .compatible = "atmel,at91sam9n12-pmc" },
34 { .compatible = "atmel,at91sam9x5-pmc" },
35 { .compatible = "atmel,sama5d3-pmc" },
36 { .compatible = "atmel,sama5d2-pmc" },
37 {}
38};
39
40U_BOOT_DRIVER(at91_pmc) = {
41 .name = "at91-pmc",
42 .id = UCLASS_SIMPLE_BUS,
43 .of_match = at91_pmc_match,
44};
45
46static int at91_pmc_core_probe(struct udevice *dev)
47{
48 struct pmc_platdata *plat = dev_get_platdata(dev);
49
50 dev = dev_get_parent(dev);
51
52 plat->reg_base = dev_read_addr_ptr(dev);
53
54 return 0;
55}
56
57/**
58 * at91_clk_sub_device_bind() - for the at91 clock driver
59 * Recursively bind its children as clk devices.
60 *
61 * @return: 0 on success, or negative error code on failure
62 */
63int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
64{
65 const void *fdt = gd->fdt_blob;
66 int offset = dev_of_offset(dev);
67 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
68 const char *name;
69 int ret;
70
71 for (offset = fdt_first_subnode(fdt, offset);
72 offset > 0;
73 offset = fdt_next_subnode(fdt, offset)) {
74 if (pre_reloc_only &&
75 !ofnode_pre_reloc(offset_to_ofnode(offset)))
76 continue;
77 /*
78 * If this node has "compatible" property, this is not
79 * a clock sub-node, but a normal device. skip.
80 */
81 fdt_get_property(fdt, offset, "compatible", &ret);
82 if (ret >= 0)
83 continue;
84
85 if (ret != -FDT_ERR_NOTFOUND)
86 return ret;
87
88 name = fdt_get_name(fdt, offset, NULL);
89 if (!name)
90 return -EINVAL;
91 ret = device_bind_driver_to_node(dev, drv_name, name,
92 offset_to_ofnode(offset), NULL);
93 if (ret)
94 return ret;
95 }
96
97 return 0;
98}
99
100int at91_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
101{
102 int periph;
103
104 if (args->args_count) {
105 debug("Invalid args_count: %d\n", args->args_count);
106 return -EINVAL;
107 }
108
109 periph = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(clk->dev), "reg",
110 -1);
111 if (periph < 0)
112 return -EINVAL;
113
114 clk->id = periph;
115
116 return 0;
117}
118
119int at91_clk_probe(struct udevice *dev)
120{
121 struct udevice *dev_periph_container, *dev_pmc;
122 struct pmc_platdata *plat = dev_get_platdata(dev);
123
124 dev_periph_container = dev_get_parent(dev);
125 dev_pmc = dev_get_parent(dev_periph_container);
126
127 plat->reg_base = dev_read_addr_ptr(dev_pmc);
128
129 return 0;
130}
131
132/* SCKC specific code. */
133static const struct udevice_id at91_sckc_match[] = {
134 { .compatible = "atmel,at91sam9x5-sckc" },
135 {}
136};
137
138U_BOOT_DRIVER(at91_sckc) = {
139 .name = "at91-sckc",
140 .id = UCLASS_SIMPLE_BUS,
141 .of_match = at91_sckc_match,
142};
143
144/* Slow clock specific code. */
145static int at91_slow_clk_enable(struct clk *clk)
146{
147 return 0;
148}
149
150static ulong at91_slow_clk_get_rate(struct clk *clk)
151{
152 return CONFIG_SYS_AT91_SLOW_CLOCK;
153}
154
155static struct clk_ops at91_slow_clk_ops = {
156 .enable = at91_slow_clk_enable,
157 .get_rate = at91_slow_clk_get_rate,
158};
159
160static const struct udevice_id at91_slow_clk_match[] = {
161 { .compatible = "atmel,at91sam9x5-clk-slow" },
162 {}
163};
164
165U_BOOT_DRIVER(at91_slow_clk) = {
166 .name = "at91-slow-clk",
167 .id = UCLASS_CLK,
168 .of_match = at91_slow_clk_match,
169 .ops = &at91_slow_clk_ops,
170};
171
172/* Master clock specific code. */
173static ulong at91_master_clk_get_rate(struct clk *clk)
174{
175 return gd->arch.mck_rate_hz;
176}
177
178static struct clk_ops at91_master_clk_ops = {
179 .get_rate = at91_master_clk_get_rate,
180};
181
182static const struct udevice_id at91_master_clk_match[] = {
183 { .compatible = "atmel,at91rm9200-clk-master" },
184 { .compatible = "atmel,at91sam9x5-clk-master" },
185 {}
186};
187
188U_BOOT_DRIVER(at91_master_clk) = {
189 .name = "at91-master-clk",
190 .id = UCLASS_CLK,
191 .of_match = at91_master_clk_match,
192 .ops = &at91_master_clk_ops,
193};
194
195/* Main osc clock specific code. */
196static int main_osc_clk_enable(struct clk *clk)
197{
198 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
199 struct at91_pmc *pmc = plat->reg_base;
200
201 if (readl(&pmc->sr) & AT91_PMC_MOSCSELS)
202 return 0;
203
204 return -EINVAL;
205}
206
207static ulong main_osc_clk_get_rate(struct clk *clk)
208{
209 return gd->arch.main_clk_rate_hz;
210}
211
212static struct clk_ops main_osc_clk_ops = {
213 .enable = main_osc_clk_enable,
214 .get_rate = main_osc_clk_get_rate,
215};
216
217static int main_osc_clk_probe(struct udevice *dev)
218{
219 return at91_pmc_core_probe(dev);
220}
221
222static const struct udevice_id main_osc_clk_match[] = {
223 { .compatible = "atmel,at91sam9x5-clk-main" },
224 {}
225};
226
227U_BOOT_DRIVER(at91sam9x5_main_osc_clk) = {
228 .name = "at91sam9x5-main-osc-clk",
229 .id = UCLASS_CLK,
230 .of_match = main_osc_clk_match,
231 .probe = main_osc_clk_probe,
232 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
233 .ops = &main_osc_clk_ops,
234};
235
236/* PLLA clock specific code. */
237static int plla_clk_enable(struct clk *clk)
238{
239 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
240 struct at91_pmc *pmc = plat->reg_base;
241
242 if (readl(&pmc->sr) & AT91_PMC_LOCKA)
243 return 0;
244
245 return -EINVAL;
246}
247
248static ulong plla_clk_get_rate(struct clk *clk)
249{
250 return gd->arch.plla_rate_hz;
251}
252
253static struct clk_ops plla_clk_ops = {
254 .enable = plla_clk_enable,
255 .get_rate = plla_clk_get_rate,
256};
257
258static int plla_clk_probe(struct udevice *dev)
259{
260 return at91_pmc_core_probe(dev);
261}
262
263static const struct udevice_id plla_clk_match[] = {
264 { .compatible = "atmel,sama5d3-clk-pll" },
265 {}
266};
267
268U_BOOT_DRIVER(at91_plla_clk) = {
269 .name = "at91-plla-clk",
270 .id = UCLASS_CLK,
271 .of_match = plla_clk_match,
272 .probe = plla_clk_probe,
273 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
274 .ops = &plla_clk_ops,
275};
276
277/* PLLA DIV clock specific code. */
278static int at91_plladiv_clk_enable(struct clk *clk)
279{
280 return 0;
281}
282
283static ulong at91_plladiv_clk_get_rate(struct clk *clk)
284{
285 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
286 struct at91_pmc *pmc = plat->reg_base;
287 struct clk source;
288 ulong clk_rate;
289 int ret;
290
291 ret = clk_get_by_index(clk->dev, 0, &source);
292 if (ret)
293 return -EINVAL;
294
295 clk_rate = clk_get_rate(&source);
296 if (readl(&pmc->mckr) & AT91_PMC_MCKR_PLLADIV_2)
297 clk_rate /= 2;
298
299 return clk_rate;
300}
301
302static ulong at91_plladiv_clk_set_rate(struct clk *clk, ulong rate)
303{
304 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
305 struct at91_pmc *pmc = plat->reg_base;
306 struct clk source;
307 ulong parent_rate;
308 int ret;
309
310 ret = clk_get_by_index(clk->dev, 0, &source);
311 if (ret)
312 return -EINVAL;
313
314 parent_rate = clk_get_rate(&source);
315 if ((parent_rate != rate) && ((parent_rate) / 2 != rate))
316 return -EINVAL;
317
318 if (parent_rate != rate) {
319 writel((readl(&pmc->mckr) | AT91_PMC_MCKR_PLLADIV_2),
320 &pmc->mckr);
321 }
322
323 return 0;
324}
325
326static struct clk_ops at91_plladiv_clk_ops = {
327 .enable = at91_plladiv_clk_enable,
328 .get_rate = at91_plladiv_clk_get_rate,
329 .set_rate = at91_plladiv_clk_set_rate,
330};
331
332static int at91_plladiv_clk_probe(struct udevice *dev)
333{
334 return at91_pmc_core_probe(dev);
335}
336
337static const struct udevice_id at91_plladiv_clk_match[] = {
338 { .compatible = "atmel,at91sam9x5-clk-plldiv" },
339 {}
340};
341
342U_BOOT_DRIVER(at91_plladiv_clk) = {
343 .name = "at91-plladiv-clk",
344 .id = UCLASS_CLK,
345 .of_match = at91_plladiv_clk_match,
346 .probe = at91_plladiv_clk_probe,
347 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
348 .ops = &at91_plladiv_clk_ops,
349};
350
351/* System clock specific code. */
352#define SYSTEM_MAX_ID 31
353
354/**
355 * at91_system_clk_bind() - for the system clock driver
356 * Recursively bind its children as clk devices.
357 *
358 * @return: 0 on success, or negative error code on failure
359 */
360static int at91_system_clk_bind(struct udevice *dev)
361{
362 return at91_clk_sub_device_bind(dev, "system-clk");
363}
364
365static const struct udevice_id at91_system_clk_match[] = {
366 { .compatible = "atmel,at91rm9200-clk-system" },
367 {}
368};
369
370U_BOOT_DRIVER(at91_system_clk) = {
371 .name = "at91-system-clk",
372 .id = UCLASS_MISC,
373 .of_match = at91_system_clk_match,
374 .bind = at91_system_clk_bind,
375};
376
377static inline int is_pck(int id)
378{
379 return (id >= 8) && (id <= 15);
380}
381
382static ulong system_clk_get_rate(struct clk *clk)
383{
384 struct clk clk_dev;
385 int ret;
386
387 ret = clk_get_by_index(clk->dev, 0, &clk_dev);
388 if (ret)
389 return -EINVAL;
390
391 return clk_get_rate(&clk_dev);
392}
393
394static ulong system_clk_set_rate(struct clk *clk, ulong rate)
395{
396 struct clk clk_dev;
397 int ret;
398
399 ret = clk_get_by_index(clk->dev, 0, &clk_dev);
400 if (ret)
401 return -EINVAL;
402
403 return clk_set_rate(&clk_dev, rate);
404}
405
406static int system_clk_enable(struct clk *clk)
407{
408 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
409 struct at91_pmc *pmc = plat->reg_base;
410 u32 mask;
411
412 if (clk->id > SYSTEM_MAX_ID)
413 return -EINVAL;
414
415 mask = BIT(clk->id);
416
417 writel(mask, &pmc->scer);
418
419 /**
420 * For the programmable clocks the Ready status in the PMC
421 * status register should be checked after enabling.
422 * For other clocks this is unnecessary.
423 */
424 if (!is_pck(clk->id))
425 return 0;
426
427 while (!(readl(&pmc->sr) & mask))
428 ;
429
430 return 0;
431}
432
433static struct clk_ops system_clk_ops = {
434 .of_xlate = at91_clk_of_xlate,
435 .get_rate = system_clk_get_rate,
436 .set_rate = system_clk_set_rate,
437 .enable = system_clk_enable,
438};
439
440U_BOOT_DRIVER(system_clk) = {
441 .name = "system-clk",
442 .id = UCLASS_CLK,
443 .probe = at91_clk_probe,
444 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
445 .ops = &system_clk_ops,
446};
447
448/* Peripheral clock specific code. */
449#define PERIPHERAL_ID_MIN 2
450#define PERIPHERAL_ID_MAX 31
451#define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
452
453enum periph_clk_type {
454 CLK_PERIPH_AT91RM9200 = 0,
455 CLK_PERIPH_AT91SAM9X5,
456};
457
458/**
459 * sam9x5_periph_clk_bind() - for the periph clock driver
460 * Recursively bind its children as clk devices.
461 *
462 * @return: 0 on success, or negative error code on failure
463 */
464static int sam9x5_periph_clk_bind(struct udevice *dev)
465{
466 return at91_clk_sub_device_bind(dev, "periph-clk");
467}
468
469static const struct udevice_id sam9x5_periph_clk_match[] = {
470 {
471 .compatible = "atmel,at91rm9200-clk-peripheral",
472 .data = CLK_PERIPH_AT91RM9200,
473 },
474 {
475 .compatible = "atmel,at91sam9x5-clk-peripheral",
476 .data = CLK_PERIPH_AT91SAM9X5,
477 },
478 {}
479};
480
481U_BOOT_DRIVER(sam9x5_periph_clk) = {
482 .name = "sam9x5-periph-clk",
483 .id = UCLASS_MISC,
484 .of_match = sam9x5_periph_clk_match,
485 .bind = sam9x5_periph_clk_bind,
486};
487
488static int periph_clk_enable(struct clk *clk)
489{
490 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
491 struct at91_pmc *pmc = plat->reg_base;
492 enum periph_clk_type clk_type;
493 void *addr;
494
495 if (clk->id < PERIPHERAL_ID_MIN)
496 return -1;
497
498 clk_type = dev_get_driver_data(dev_get_parent(clk->dev));
499 if (clk_type == CLK_PERIPH_AT91RM9200) {
500 addr = &pmc->pcer;
501 if (clk->id > PERIPHERAL_ID_MAX)
502 addr = &pmc->pcer1;
503
504 setbits_le32(addr, PERIPHERAL_MASK(clk->id));
505 } else {
506 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
507 setbits_le32(&pmc->pcr,
508 AT91_PMC_PCR_CMD_WRITE | AT91_PMC_PCR_EN);
509 }
510
511 return 0;
512}
513
514static ulong periph_get_rate(struct clk *clk)
515{
516 struct udevice *dev;
517 struct clk clk_dev;
518 ulong clk_rate;
519 int ret;
520
521 dev = dev_get_parent(clk->dev);
522
523 ret = clk_get_by_index(dev, 0, &clk_dev);
524 if (ret)
525 return ret;
526
527 clk_rate = clk_get_rate(&clk_dev);
528
529 clk_free(&clk_dev);
530
531 return clk_rate;
532}
533
534static struct clk_ops periph_clk_ops = {
535 .of_xlate = at91_clk_of_xlate,
536 .enable = periph_clk_enable,
537 .get_rate = periph_get_rate,
538};
539
540U_BOOT_DRIVER(clk_periph) = {
541 .name = "periph-clk",
542 .id = UCLASS_CLK,
543 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
544 .probe = at91_clk_probe,
545 .ops = &periph_clk_ops,
546};
547
548/* UTMI clock specific code. */
549#ifdef CONFIG_AT91_UTMI
550
551/*
552 * The purpose of this clock is to generate a 480 MHz signal. A different
553 * rate can't be configured.
554 */
555#define UTMI_RATE 480000000
556
557static int utmi_clk_enable(struct clk *clk)
558{
559 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
560 struct at91_pmc *pmc = plat->reg_base;
561 struct clk clk_dev;
562 ulong clk_rate;
563 u32 utmi_ref_clk_freq;
564 u32 tmp;
565 int err;
566 int timeout = 2000000;
567
568 if (readl(&pmc->sr) & AT91_PMC_LOCKU)
569 return 0;
570
571 /*
572 * If mainck rate is different from 12 MHz, we have to configure the
573 * FREQ field of the SFR_UTMICKTRIM register to generate properly
574 * the utmi clock.
575 */
576 err = clk_get_by_index(clk->dev, 0, &clk_dev);
577 if (err)
578 return -EINVAL;
579
580 clk_rate = clk_get_rate(&clk_dev);
581 switch (clk_rate) {
582 case 12000000:
583 utmi_ref_clk_freq = 0;
584 break;
585 case 16000000:
586 utmi_ref_clk_freq = 1;
587 break;
588 case 24000000:
589 utmi_ref_clk_freq = 2;
590 break;
591 /*
592 * Not supported on SAMA5D2 but it's not an issue since MAINCK
593 * maximum value is 24 MHz.
594 */
595 case 48000000:
596 utmi_ref_clk_freq = 3;
597 break;
598 default:
599 printf("UTMICK: unsupported mainck rate\n");
600 return -EINVAL;
601 }
602
603 if (plat->regmap_sfr) {
604 err = regmap_read(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, &tmp);
605 if (err)
606 return -EINVAL;
607
608 tmp &= ~AT91_UTMICKTRIM_FREQ;
609 tmp |= utmi_ref_clk_freq;
610 err = regmap_write(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, tmp);
611 if (err)
612 return -EINVAL;
613 } else if (utmi_ref_clk_freq) {
614 printf("UTMICK: sfr node required\n");
615 return -EINVAL;
616 }
617
618 tmp = readl(&pmc->uckr);
619 tmp |= AT91_PMC_UPLLEN |
620 AT91_PMC_UPLLCOUNT |
621 AT91_PMC_BIASEN;
622 writel(tmp, &pmc->uckr);
623
624 while ((--timeout) && !(readl(&pmc->sr) & AT91_PMC_LOCKU))
625 ;
626 if (!timeout) {
627 printf("UTMICK: timeout waiting for UPLL lock\n");
628 return -ETIMEDOUT;
629 }
630
631 return 0;
632}
633
634static ulong utmi_clk_get_rate(struct clk *clk)
635{
636 /* UTMI clk rate is fixed. */
637 return UTMI_RATE;
638}
639
640static struct clk_ops utmi_clk_ops = {
641 .enable = utmi_clk_enable,
642 .get_rate = utmi_clk_get_rate,
643};
644
645static int utmi_clk_ofdata_to_platdata(struct udevice *dev)
646{
647 struct pmc_platdata *plat = dev_get_platdata(dev);
648 struct udevice *syscon;
649
650 uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
651 "regmap-sfr", &syscon);
652
653 if (syscon)
654 plat->regmap_sfr = syscon_get_regmap(syscon);
655
656 return 0;
657}
658
659static int utmi_clk_probe(struct udevice *dev)
660{
661 return at91_pmc_core_probe(dev);
662}
663
664static const struct udevice_id utmi_clk_match[] = {
665 { .compatible = "atmel,at91sam9x5-clk-utmi" },
666 {}
667};
668
669U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
670 .name = "at91sam9x5-utmi-clk",
671 .id = UCLASS_CLK,
672 .of_match = utmi_clk_match,
673 .probe = utmi_clk_probe,
674 .ofdata_to_platdata = utmi_clk_ofdata_to_platdata,
675 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
676 .ops = &utmi_clk_ops,
677};
678
679#endif /* CONFIG_AT91_UTMI */
680
681/* H32MX clock specific code. */
682#ifdef CONFIG_AT91_H32MX
683
684#define H32MX_MAX_FREQ 90000000
685
686static ulong sama5d4_h32mx_clk_get_rate(struct clk *clk)
687{
688 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
689 struct at91_pmc *pmc = plat->reg_base;
690 ulong rate = gd->arch.mck_rate_hz;
691
692 if (readl(&pmc->mckr) & AT91_PMC_MCKR_H32MXDIV)
693 rate /= 2;
694
695 if (rate > H32MX_MAX_FREQ)
696 dev_dbg(clk->dev, "H32MX clock is too fast\n");
697
698 return rate;
699}
700
701static struct clk_ops sama5d4_h32mx_clk_ops = {
702 .get_rate = sama5d4_h32mx_clk_get_rate,
703};
704
705static int sama5d4_h32mx_clk_probe(struct udevice *dev)
706{
707 return at91_pmc_core_probe(dev);
708}
709
710static const struct udevice_id sama5d4_h32mx_clk_match[] = {
711 { .compatible = "atmel,sama5d4-clk-h32mx" },
712 {}
713};
714
715U_BOOT_DRIVER(sama5d4_h32mx_clk) = {
716 .name = "sama5d4-h32mx-clk",
717 .id = UCLASS_CLK,
718 .of_match = sama5d4_h32mx_clk_match,
719 .probe = sama5d4_h32mx_clk_probe,
720 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
721 .ops = &sama5d4_h32mx_clk_ops,
722};
723
724#endif /* CONFIG_AT91_H32MX */
725
726/* Generic clock specific code. */
727#ifdef CONFIG_AT91_GENERIC_CLK
728
729#define GENERATED_SOURCE_MAX 6
730#define GENERATED_MAX_DIV 255
731
732/**
733 * generated_clk_bind() - for the generated clock driver
734 * Recursively bind its children as clk devices.
735 *
736 * @return: 0 on success, or negative error code on failure
737 */
738static int generated_clk_bind(struct udevice *dev)
739{
740 return at91_clk_sub_device_bind(dev, "generic-clk");
741}
742
743static const struct udevice_id generated_clk_match[] = {
744 { .compatible = "atmel,sama5d2-clk-generated" },
745 {}
746};
747
748U_BOOT_DRIVER(generated_clk) = {
749 .name = "generated-clk",
750 .id = UCLASS_MISC,
751 .of_match = generated_clk_match,
752 .bind = generated_clk_bind,
753};
754
755struct generic_clk_priv {
756 u32 num_parents;
757};
758
759static ulong generic_clk_get_rate(struct clk *clk)
760{
761 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
762 struct at91_pmc *pmc = plat->reg_base;
763 struct clk parent;
764 ulong clk_rate;
765 u32 tmp, gckdiv;
766 u8 clock_source, parent_index;
767 int ret;
768
769 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
770 tmp = readl(&pmc->pcr);
771 clock_source = (tmp >> AT91_PMC_PCR_GCKCSS_OFFSET) &
772 AT91_PMC_PCR_GCKCSS_MASK;
773 gckdiv = (tmp >> AT91_PMC_PCR_GCKDIV_OFFSET) & AT91_PMC_PCR_GCKDIV_MASK;
774
775 parent_index = clock_source - 1;
776 ret = clk_get_by_index(dev_get_parent(clk->dev), parent_index, &parent);
777 if (ret)
778 return 0;
779
780 clk_rate = clk_get_rate(&parent) / (gckdiv + 1);
781
782 clk_free(&parent);
783
784 return clk_rate;
785}
786
787static ulong generic_clk_set_rate(struct clk *clk, ulong rate)
788{
789 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
790 struct at91_pmc *pmc = plat->reg_base;
791 struct generic_clk_priv *priv = dev_get_priv(clk->dev);
792 struct clk parent, best_parent;
793 ulong tmp_rate, best_rate = rate, parent_rate;
794 int tmp_diff, best_diff = -1;
795 u32 div, best_div = 0;
796 u8 best_parent_index, best_clock_source = 0;
797 u8 i;
798 u32 tmp;
799 int ret;
800
801 for (i = 0; i < priv->num_parents; i++) {
802 ret = clk_get_by_index(dev_get_parent(clk->dev), i, &parent);
803 if (ret)
804 return ret;
805
806 parent_rate = clk_get_rate(&parent);
807 if (IS_ERR_VALUE(parent_rate))
808 return parent_rate;
809
810 for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
811 tmp_rate = DIV_ROUND_CLOSEST(parent_rate, div);
812 tmp_diff = abs(rate - tmp_rate);
813
814 if (best_diff < 0 || best_diff > tmp_diff) {
815 best_rate = tmp_rate;
816 best_diff = tmp_diff;
817
818 best_div = div - 1;
819 best_parent = parent;
820 best_parent_index = i;
821 best_clock_source = best_parent_index + 1;
822 }
823
824 if (!best_diff || tmp_rate < rate)
825 break;
826 }
827
828 if (!best_diff)
829 break;
830 }
831
832 debug("GCK: best parent: %s, best_rate = %ld, best_div = %d\n",
833 best_parent.dev->name, best_rate, best_div);
834
835 ret = clk_enable(&best_parent);
836 if (ret)
837 return ret;
838
839 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
840 tmp = readl(&pmc->pcr);
841 tmp &= ~(AT91_PMC_PCR_GCKDIV | AT91_PMC_PCR_GCKCSS);
842 tmp |= AT91_PMC_PCR_GCKCSS_(best_clock_source) |
843 AT91_PMC_PCR_CMD_WRITE |
844 AT91_PMC_PCR_GCKDIV_(best_div) |
845 AT91_PMC_PCR_GCKEN;
846 writel(tmp, &pmc->pcr);
847
848 while (!(readl(&pmc->sr) & AT91_PMC_GCKRDY))
849 ;
850
851 return 0;
852}
853
854static struct clk_ops generic_clk_ops = {
855 .of_xlate = at91_clk_of_xlate,
856 .get_rate = generic_clk_get_rate,
857 .set_rate = generic_clk_set_rate,
858};
859
860static int generic_clk_ofdata_to_platdata(struct udevice *dev)
861{
862 struct generic_clk_priv *priv = dev_get_priv(dev);
863 u32 cells[GENERATED_SOURCE_MAX];
864 u32 num_parents;
865
866 num_parents = fdtdec_get_int_array_count(gd->fdt_blob,
867 dev_of_offset(dev_get_parent(dev)), "clocks", cells,
868 GENERATED_SOURCE_MAX);
869
870 if (!num_parents)
871 return -1;
872
873 priv->num_parents = num_parents;
874
875 return 0;
876}
877
878U_BOOT_DRIVER(generic_clk) = {
879 .name = "generic-clk",
880 .id = UCLASS_CLK,
881 .probe = at91_clk_probe,
882 .ofdata_to_platdata = generic_clk_ofdata_to_platdata,
883 .priv_auto_alloc_size = sizeof(struct generic_clk_priv),
884 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
885 .ops = &generic_clk_ops,
886};
887
888#endif /* CONFIG_AT91_GENERIC_CLK */
889
890/* USB clock specific code. */
891#ifdef CONFIG_AT91_USB_CLK
892
893#define AT91_USB_CLK_SOURCE_MAX 2
894#define AT91_USB_CLK_MAX_DIV 15
895
896struct at91_usb_clk_priv {
897 u32 num_clksource;
898};
899
900static ulong at91_usb_clk_get_rate(struct clk *clk)
901{
902 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
903 struct at91_pmc *pmc = plat->reg_base;
904 struct clk source;
905 u32 tmp, usbdiv;
906 u8 source_index;
907 int ret;
908
909 tmp = readl(&pmc->pcr);
910 source_index = (tmp >> AT91_PMC_USB_USBS_OFFSET) &
911 AT91_PMC_USB_USBS_MASK;
912 usbdiv = (tmp >> AT91_PMC_USB_DIV_OFFSET) & AT91_PMC_USB_DIV_MASK;
913
914 ret = clk_get_by_index(clk->dev, source_index, &source);
915 if (ret)
916 return 0;
917
918 return clk_get_rate(&source) / (usbdiv + 1);
919}
920
921static ulong at91_usb_clk_set_rate(struct clk *clk, ulong rate)
922{
923 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
924 struct at91_pmc *pmc = plat->reg_base;
925 struct at91_usb_clk_priv *priv = dev_get_priv(clk->dev);
926 struct clk source, best_source;
927 ulong tmp_rate, best_rate = rate, source_rate;
928 int tmp_diff, best_diff = -1;
929 u32 div, best_div = 0;
930 u8 best_source_index = 0;
931 u8 i;
932 u32 tmp;
933 int ret;
934
935 for (i = 0; i < priv->num_clksource; i++) {
936 ret = clk_get_by_index(clk->dev, i, &source);
937 if (ret)
938 return ret;
939
940 source_rate = clk_get_rate(&source);
941 if (IS_ERR_VALUE(source_rate))
942 return source_rate;
943
944 for (div = 1; div < AT91_USB_CLK_MAX_DIV + 2; div++) {
945 tmp_rate = DIV_ROUND_CLOSEST(source_rate, div);
946 tmp_diff = abs(rate - tmp_rate);
947
948 if (best_diff < 0 || best_diff > tmp_diff) {
949 best_rate = tmp_rate;
950 best_diff = tmp_diff;
951
952 best_div = div - 1;
953 best_source = source;
954 best_source_index = i;
955 }
956
957 if (!best_diff || tmp_rate < rate)
958 break;
959 }
960
961 if (!best_diff)
962 break;
963 }
964
965 debug("AT91 USB: best sourc: %s, best_rate = %ld, best_div = %d\n",
966 best_source.dev->name, best_rate, best_div);
967
968 ret = clk_enable(&best_source);
969 if (ret)
970 return ret;
971
972 tmp = AT91_PMC_USB_USBS_(best_source_index) |
973 AT91_PMC_USB_DIV_(best_div);
974 writel(tmp, &pmc->usb);
975
976 return 0;
977}
978
979static struct clk_ops at91_usb_clk_ops = {
980 .get_rate = at91_usb_clk_get_rate,
981 .set_rate = at91_usb_clk_set_rate,
982};
983
984static int at91_usb_clk_ofdata_to_platdata(struct udevice *dev)
985{
986 struct at91_usb_clk_priv *priv = dev_get_priv(dev);
987 u32 cells[AT91_USB_CLK_SOURCE_MAX];
988 u32 num_clksource;
989
990 num_clksource = fdtdec_get_int_array_count(gd->fdt_blob,
991 dev_of_offset(dev),
992 "clocks", cells,
993 AT91_USB_CLK_SOURCE_MAX);
994
995 if (!num_clksource)
996 return -1;
997
998 priv->num_clksource = num_clksource;
999
1000 return 0;
1001}
1002
1003static int at91_usb_clk_probe(struct udevice *dev)
1004{
1005 return at91_pmc_core_probe(dev);
1006}
1007
1008static const struct udevice_id at91_usb_clk_match[] = {
1009 { .compatible = "atmel,at91sam9x5-clk-usb" },
1010 {}
1011};
1012
1013U_BOOT_DRIVER(at91_usb_clk) = {
1014 .name = "at91-usb-clk",
1015 .id = UCLASS_CLK,
1016 .of_match = at91_usb_clk_match,
1017 .probe = at91_usb_clk_probe,
1018 .ofdata_to_platdata = at91_usb_clk_ofdata_to_platdata,
1019 .priv_auto_alloc_size = sizeof(struct at91_usb_clk_priv),
1020 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
1021 .ops = &at91_usb_clk_ops,
1022};
1023
1024#endif /* CONFIG_AT91_USB_CLK */