1 // SPDX-License-Identifier: GPL-2.0+
3 * Compatible code for non CCF AT91 platforms.
5 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries
7 * Author: Claudiu Beznea <claudiu.beznea@microchip.com>
10 #include <clk-uclass.h>
14 #include <mach/at91_pmc.h>
15 #include <mach/at91_sfr.h>
21 DECLARE_GLOBAL_DATA_PTR;
24 struct at91_pmc *reg_base;
25 struct regmap *regmap_sfr;
28 static const struct udevice_id at91_pmc_match[] = {
29 { .compatible = "atmel,at91rm9200-pmc" },
30 { .compatible = "atmel,at91sam9260-pmc" },
31 { .compatible = "atmel,at91sam9g45-pmc" },
32 { .compatible = "atmel,at91sam9n12-pmc" },
33 { .compatible = "atmel,at91sam9x5-pmc" },
34 { .compatible = "atmel,sama5d3-pmc" },
35 { .compatible = "atmel,sama5d2-pmc" },
39 U_BOOT_DRIVER(at91_pmc) = {
41 .id = UCLASS_SIMPLE_BUS,
42 .of_match = at91_pmc_match,
45 static int at91_pmc_core_probe(struct udevice *dev)
47 struct pmc_platdata *plat = dev_get_platdata(dev);
49 dev = dev_get_parent(dev);
51 plat->reg_base = dev_read_addr_ptr(dev);
57 * at91_clk_sub_device_bind() - for the at91 clock driver
58 * Recursively bind its children as clk devices.
60 * @return: 0 on success, or negative error code on failure
62 int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
64 const void *fdt = gd->fdt_blob;
65 int offset = dev_of_offset(dev);
66 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
70 for (offset = fdt_first_subnode(fdt, offset);
72 offset = fdt_next_subnode(fdt, offset)) {
74 !ofnode_pre_reloc(offset_to_ofnode(offset)))
77 * If this node has "compatible" property, this is not
78 * a clock sub-node, but a normal device. skip.
80 fdt_get_property(fdt, offset, "compatible", &ret);
84 if (ret != -FDT_ERR_NOTFOUND)
87 name = fdt_get_name(fdt, offset, NULL);
90 ret = device_bind_driver_to_node(dev, drv_name, name,
91 offset_to_ofnode(offset), NULL);
99 int at91_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
103 if (args->args_count) {
104 debug("Invalid args_count: %d\n", args->args_count);
108 periph = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(clk->dev), "reg",
118 int at91_clk_probe(struct udevice *dev)
120 struct udevice *dev_periph_container, *dev_pmc;
121 struct pmc_platdata *plat = dev_get_platdata(dev);
123 dev_periph_container = dev_get_parent(dev);
124 dev_pmc = dev_get_parent(dev_periph_container);
126 plat->reg_base = dev_read_addr_ptr(dev_pmc);
131 /* SCKC specific code. */
132 static const struct udevice_id at91_sckc_match[] = {
133 { .compatible = "atmel,at91sam9x5-sckc" },
137 U_BOOT_DRIVER(at91_sckc) = {
139 .id = UCLASS_SIMPLE_BUS,
140 .of_match = at91_sckc_match,
143 /* Slow clock specific code. */
144 static int at91_slow_clk_enable(struct clk *clk)
149 static ulong at91_slow_clk_get_rate(struct clk *clk)
151 return CONFIG_SYS_AT91_SLOW_CLOCK;
154 static struct clk_ops at91_slow_clk_ops = {
155 .enable = at91_slow_clk_enable,
156 .get_rate = at91_slow_clk_get_rate,
159 static const struct udevice_id at91_slow_clk_match[] = {
160 { .compatible = "atmel,at91sam9x5-clk-slow" },
164 U_BOOT_DRIVER(at91_slow_clk) = {
165 .name = "at91-slow-clk",
167 .of_match = at91_slow_clk_match,
168 .ops = &at91_slow_clk_ops,
171 /* Master clock specific code. */
172 static ulong at91_master_clk_get_rate(struct clk *clk)
174 return gd->arch.mck_rate_hz;
177 static struct clk_ops at91_master_clk_ops = {
178 .get_rate = at91_master_clk_get_rate,
181 static const struct udevice_id at91_master_clk_match[] = {
182 { .compatible = "atmel,at91rm9200-clk-master" },
183 { .compatible = "atmel,at91sam9x5-clk-master" },
187 U_BOOT_DRIVER(at91_master_clk) = {
188 .name = "at91-master-clk",
190 .of_match = at91_master_clk_match,
191 .ops = &at91_master_clk_ops,
194 /* Main osc clock specific code. */
195 static int main_osc_clk_enable(struct clk *clk)
197 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
198 struct at91_pmc *pmc = plat->reg_base;
200 if (readl(&pmc->sr) & AT91_PMC_MOSCSELS)
206 static ulong main_osc_clk_get_rate(struct clk *clk)
208 return gd->arch.main_clk_rate_hz;
211 static struct clk_ops main_osc_clk_ops = {
212 .enable = main_osc_clk_enable,
213 .get_rate = main_osc_clk_get_rate,
216 static int main_osc_clk_probe(struct udevice *dev)
218 return at91_pmc_core_probe(dev);
221 static const struct udevice_id main_osc_clk_match[] = {
222 { .compatible = "atmel,at91sam9x5-clk-main" },
226 U_BOOT_DRIVER(at91sam9x5_main_osc_clk) = {
227 .name = "at91sam9x5-main-osc-clk",
229 .of_match = main_osc_clk_match,
230 .probe = main_osc_clk_probe,
231 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
232 .ops = &main_osc_clk_ops,
235 /* PLLA clock specific code. */
236 static int plla_clk_enable(struct clk *clk)
238 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
239 struct at91_pmc *pmc = plat->reg_base;
241 if (readl(&pmc->sr) & AT91_PMC_LOCKA)
247 static ulong plla_clk_get_rate(struct clk *clk)
249 return gd->arch.plla_rate_hz;
252 static struct clk_ops plla_clk_ops = {
253 .enable = plla_clk_enable,
254 .get_rate = plla_clk_get_rate,
257 static int plla_clk_probe(struct udevice *dev)
259 return at91_pmc_core_probe(dev);
262 static const struct udevice_id plla_clk_match[] = {
263 { .compatible = "atmel,sama5d3-clk-pll" },
267 U_BOOT_DRIVER(at91_plla_clk) = {
268 .name = "at91-plla-clk",
270 .of_match = plla_clk_match,
271 .probe = plla_clk_probe,
272 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
273 .ops = &plla_clk_ops,
276 /* PLLA DIV clock specific code. */
277 static int at91_plladiv_clk_enable(struct clk *clk)
282 static ulong at91_plladiv_clk_get_rate(struct clk *clk)
284 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
285 struct at91_pmc *pmc = plat->reg_base;
290 ret = clk_get_by_index(clk->dev, 0, &source);
294 clk_rate = clk_get_rate(&source);
295 if (readl(&pmc->mckr) & AT91_PMC_MCKR_PLLADIV_2)
301 static ulong at91_plladiv_clk_set_rate(struct clk *clk, ulong rate)
303 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
304 struct at91_pmc *pmc = plat->reg_base;
309 ret = clk_get_by_index(clk->dev, 0, &source);
313 parent_rate = clk_get_rate(&source);
314 if ((parent_rate != rate) && ((parent_rate) / 2 != rate))
317 if (parent_rate != rate) {
318 writel((readl(&pmc->mckr) | AT91_PMC_MCKR_PLLADIV_2),
325 static struct clk_ops at91_plladiv_clk_ops = {
326 .enable = at91_plladiv_clk_enable,
327 .get_rate = at91_plladiv_clk_get_rate,
328 .set_rate = at91_plladiv_clk_set_rate,
331 static int at91_plladiv_clk_probe(struct udevice *dev)
333 return at91_pmc_core_probe(dev);
336 static const struct udevice_id at91_plladiv_clk_match[] = {
337 { .compatible = "atmel,at91sam9x5-clk-plldiv" },
341 U_BOOT_DRIVER(at91_plladiv_clk) = {
342 .name = "at91-plladiv-clk",
344 .of_match = at91_plladiv_clk_match,
345 .probe = at91_plladiv_clk_probe,
346 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
347 .ops = &at91_plladiv_clk_ops,
350 /* System clock specific code. */
351 #define SYSTEM_MAX_ID 31
354 * at91_system_clk_bind() - for the system clock driver
355 * Recursively bind its children as clk devices.
357 * @return: 0 on success, or negative error code on failure
359 static int at91_system_clk_bind(struct udevice *dev)
361 return at91_clk_sub_device_bind(dev, "system-clk");
364 static const struct udevice_id at91_system_clk_match[] = {
365 { .compatible = "atmel,at91rm9200-clk-system" },
369 U_BOOT_DRIVER(at91_system_clk) = {
370 .name = "at91-system-clk",
372 .of_match = at91_system_clk_match,
373 .bind = at91_system_clk_bind,
376 static inline int is_pck(int id)
378 return (id >= 8) && (id <= 15);
381 static ulong system_clk_get_rate(struct clk *clk)
386 ret = clk_get_by_index(clk->dev, 0, &clk_dev);
390 return clk_get_rate(&clk_dev);
393 static ulong system_clk_set_rate(struct clk *clk, ulong rate)
398 ret = clk_get_by_index(clk->dev, 0, &clk_dev);
402 return clk_set_rate(&clk_dev, rate);
405 static int system_clk_enable(struct clk *clk)
407 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
408 struct at91_pmc *pmc = plat->reg_base;
411 if (clk->id > SYSTEM_MAX_ID)
416 writel(mask, &pmc->scer);
419 * For the programmable clocks the Ready status in the PMC
420 * status register should be checked after enabling.
421 * For other clocks this is unnecessary.
423 if (!is_pck(clk->id))
426 while (!(readl(&pmc->sr) & mask))
432 static struct clk_ops system_clk_ops = {
433 .of_xlate = at91_clk_of_xlate,
434 .get_rate = system_clk_get_rate,
435 .set_rate = system_clk_set_rate,
436 .enable = system_clk_enable,
439 U_BOOT_DRIVER(system_clk) = {
440 .name = "system-clk",
442 .probe = at91_clk_probe,
443 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
444 .ops = &system_clk_ops,
447 /* Peripheral clock specific code. */
448 #define PERIPHERAL_ID_MIN 2
449 #define PERIPHERAL_ID_MAX 31
450 #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
452 enum periph_clk_type {
453 CLK_PERIPH_AT91RM9200 = 0,
454 CLK_PERIPH_AT91SAM9X5,
458 * sam9x5_periph_clk_bind() - for the periph clock driver
459 * Recursively bind its children as clk devices.
461 * @return: 0 on success, or negative error code on failure
463 static int sam9x5_periph_clk_bind(struct udevice *dev)
465 return at91_clk_sub_device_bind(dev, "periph-clk");
468 static const struct udevice_id sam9x5_periph_clk_match[] = {
470 .compatible = "atmel,at91rm9200-clk-peripheral",
471 .data = CLK_PERIPH_AT91RM9200,
474 .compatible = "atmel,at91sam9x5-clk-peripheral",
475 .data = CLK_PERIPH_AT91SAM9X5,
480 U_BOOT_DRIVER(sam9x5_periph_clk) = {
481 .name = "sam9x5-periph-clk",
483 .of_match = sam9x5_periph_clk_match,
484 .bind = sam9x5_periph_clk_bind,
487 static int periph_clk_enable(struct clk *clk)
489 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
490 struct at91_pmc *pmc = plat->reg_base;
491 enum periph_clk_type clk_type;
494 if (clk->id < PERIPHERAL_ID_MIN)
497 clk_type = dev_get_driver_data(dev_get_parent(clk->dev));
498 if (clk_type == CLK_PERIPH_AT91RM9200) {
500 if (clk->id > PERIPHERAL_ID_MAX)
503 setbits_le32(addr, PERIPHERAL_MASK(clk->id));
505 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
506 setbits_le32(&pmc->pcr,
507 AT91_PMC_PCR_CMD_WRITE | AT91_PMC_PCR_EN);
513 static ulong periph_get_rate(struct clk *clk)
520 dev = dev_get_parent(clk->dev);
522 ret = clk_get_by_index(dev, 0, &clk_dev);
526 clk_rate = clk_get_rate(&clk_dev);
533 static struct clk_ops periph_clk_ops = {
534 .of_xlate = at91_clk_of_xlate,
535 .enable = periph_clk_enable,
536 .get_rate = periph_get_rate,
539 U_BOOT_DRIVER(clk_periph) = {
540 .name = "periph-clk",
542 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
543 .probe = at91_clk_probe,
544 .ops = &periph_clk_ops,
547 /* UTMI clock specific code. */
548 #ifdef CONFIG_AT91_UTMI
551 * The purpose of this clock is to generate a 480 MHz signal. A different
552 * rate can't be configured.
554 #define UTMI_RATE 480000000
556 static int utmi_clk_enable(struct clk *clk)
558 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
559 struct at91_pmc *pmc = plat->reg_base;
562 u32 utmi_ref_clk_freq;
565 int timeout = 2000000;
567 if (readl(&pmc->sr) & AT91_PMC_LOCKU)
571 * If mainck rate is different from 12 MHz, we have to configure the
572 * FREQ field of the SFR_UTMICKTRIM register to generate properly
575 err = clk_get_by_index(clk->dev, 0, &clk_dev);
579 clk_rate = clk_get_rate(&clk_dev);
582 utmi_ref_clk_freq = 0;
585 utmi_ref_clk_freq = 1;
588 utmi_ref_clk_freq = 2;
591 * Not supported on SAMA5D2 but it's not an issue since MAINCK
592 * maximum value is 24 MHz.
595 utmi_ref_clk_freq = 3;
598 printf("UTMICK: unsupported mainck rate\n");
602 if (plat->regmap_sfr) {
603 err = regmap_read(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, &tmp);
607 tmp &= ~AT91_UTMICKTRIM_FREQ;
608 tmp |= utmi_ref_clk_freq;
609 err = regmap_write(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, tmp);
612 } else if (utmi_ref_clk_freq) {
613 printf("UTMICK: sfr node required\n");
617 tmp = readl(&pmc->uckr);
618 tmp |= AT91_PMC_UPLLEN |
621 writel(tmp, &pmc->uckr);
623 while ((--timeout) && !(readl(&pmc->sr) & AT91_PMC_LOCKU))
626 printf("UTMICK: timeout waiting for UPLL lock\n");
633 static ulong utmi_clk_get_rate(struct clk *clk)
635 /* UTMI clk rate is fixed. */
639 static struct clk_ops utmi_clk_ops = {
640 .enable = utmi_clk_enable,
641 .get_rate = utmi_clk_get_rate,
644 static int utmi_clk_ofdata_to_platdata(struct udevice *dev)
646 struct pmc_platdata *plat = dev_get_platdata(dev);
647 struct udevice *syscon;
649 uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
650 "regmap-sfr", &syscon);
653 plat->regmap_sfr = syscon_get_regmap(syscon);
658 static int utmi_clk_probe(struct udevice *dev)
660 return at91_pmc_core_probe(dev);
663 static const struct udevice_id utmi_clk_match[] = {
664 { .compatible = "atmel,at91sam9x5-clk-utmi" },
668 U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
669 .name = "at91sam9x5-utmi-clk",
671 .of_match = utmi_clk_match,
672 .probe = utmi_clk_probe,
673 .ofdata_to_platdata = utmi_clk_ofdata_to_platdata,
674 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
675 .ops = &utmi_clk_ops,
678 #endif /* CONFIG_AT91_UTMI */
680 /* H32MX clock specific code. */
681 #ifdef CONFIG_AT91_H32MX
683 #define H32MX_MAX_FREQ 90000000
685 static ulong sama5d4_h32mx_clk_get_rate(struct clk *clk)
687 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
688 struct at91_pmc *pmc = plat->reg_base;
689 ulong rate = gd->arch.mck_rate_hz;
691 if (readl(&pmc->mckr) & AT91_PMC_MCKR_H32MXDIV)
694 if (rate > H32MX_MAX_FREQ)
695 dev_dbg(clk->dev, "H32MX clock is too fast\n");
700 static struct clk_ops sama5d4_h32mx_clk_ops = {
701 .get_rate = sama5d4_h32mx_clk_get_rate,
704 static int sama5d4_h32mx_clk_probe(struct udevice *dev)
706 return at91_pmc_core_probe(dev);
709 static const struct udevice_id sama5d4_h32mx_clk_match[] = {
710 { .compatible = "atmel,sama5d4-clk-h32mx" },
714 U_BOOT_DRIVER(sama5d4_h32mx_clk) = {
715 .name = "sama5d4-h32mx-clk",
717 .of_match = sama5d4_h32mx_clk_match,
718 .probe = sama5d4_h32mx_clk_probe,
719 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
720 .ops = &sama5d4_h32mx_clk_ops,
723 #endif /* CONFIG_AT91_H32MX */
725 /* Generic clock specific code. */
726 #ifdef CONFIG_AT91_GENERIC_CLK
728 #define GENERATED_SOURCE_MAX 6
729 #define GENERATED_MAX_DIV 255
732 * generated_clk_bind() - for the generated clock driver
733 * Recursively bind its children as clk devices.
735 * @return: 0 on success, or negative error code on failure
737 static int generated_clk_bind(struct udevice *dev)
739 return at91_clk_sub_device_bind(dev, "generic-clk");
742 static const struct udevice_id generated_clk_match[] = {
743 { .compatible = "atmel,sama5d2-clk-generated" },
747 U_BOOT_DRIVER(generated_clk) = {
748 .name = "generated-clk",
750 .of_match = generated_clk_match,
751 .bind = generated_clk_bind,
754 struct generic_clk_priv {
758 static ulong generic_clk_get_rate(struct clk *clk)
760 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
761 struct at91_pmc *pmc = plat->reg_base;
765 u8 clock_source, parent_index;
768 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
769 tmp = readl(&pmc->pcr);
770 clock_source = (tmp >> AT91_PMC_PCR_GCKCSS_OFFSET) &
771 AT91_PMC_PCR_GCKCSS_MASK;
772 gckdiv = (tmp >> AT91_PMC_PCR_GCKDIV_OFFSET) & AT91_PMC_PCR_GCKDIV_MASK;
774 parent_index = clock_source - 1;
775 ret = clk_get_by_index(dev_get_parent(clk->dev), parent_index, &parent);
779 clk_rate = clk_get_rate(&parent) / (gckdiv + 1);
786 static ulong generic_clk_set_rate(struct clk *clk, ulong rate)
788 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
789 struct at91_pmc *pmc = plat->reg_base;
790 struct generic_clk_priv *priv = dev_get_priv(clk->dev);
791 struct clk parent, best_parent;
792 ulong tmp_rate, best_rate = rate, parent_rate;
793 int tmp_diff, best_diff = -1;
794 u32 div, best_div = 0;
795 u8 best_parent_index, best_clock_source = 0;
800 for (i = 0; i < priv->num_parents; i++) {
801 ret = clk_get_by_index(dev_get_parent(clk->dev), i, &parent);
805 parent_rate = clk_get_rate(&parent);
806 if (IS_ERR_VALUE(parent_rate))
809 for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
810 tmp_rate = DIV_ROUND_CLOSEST(parent_rate, div);
811 tmp_diff = abs(rate - tmp_rate);
813 if (best_diff < 0 || best_diff > tmp_diff) {
814 best_rate = tmp_rate;
815 best_diff = tmp_diff;
818 best_parent = parent;
819 best_parent_index = i;
820 best_clock_source = best_parent_index + 1;
823 if (!best_diff || tmp_rate < rate)
831 debug("GCK: best parent: %s, best_rate = %ld, best_div = %d\n",
832 best_parent.dev->name, best_rate, best_div);
834 ret = clk_enable(&best_parent);
838 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
839 tmp = readl(&pmc->pcr);
840 tmp &= ~(AT91_PMC_PCR_GCKDIV | AT91_PMC_PCR_GCKCSS);
841 tmp |= AT91_PMC_PCR_GCKCSS_(best_clock_source) |
842 AT91_PMC_PCR_CMD_WRITE |
843 AT91_PMC_PCR_GCKDIV_(best_div) |
845 writel(tmp, &pmc->pcr);
847 while (!(readl(&pmc->sr) & AT91_PMC_GCKRDY))
853 static struct clk_ops generic_clk_ops = {
854 .of_xlate = at91_clk_of_xlate,
855 .get_rate = generic_clk_get_rate,
856 .set_rate = generic_clk_set_rate,
859 static int generic_clk_ofdata_to_platdata(struct udevice *dev)
861 struct generic_clk_priv *priv = dev_get_priv(dev);
862 u32 cells[GENERATED_SOURCE_MAX];
865 num_parents = fdtdec_get_int_array_count(gd->fdt_blob,
866 dev_of_offset(dev_get_parent(dev)), "clocks", cells,
867 GENERATED_SOURCE_MAX);
872 priv->num_parents = num_parents;
877 U_BOOT_DRIVER(generic_clk) = {
878 .name = "generic-clk",
880 .probe = at91_clk_probe,
881 .ofdata_to_platdata = generic_clk_ofdata_to_platdata,
882 .priv_auto_alloc_size = sizeof(struct generic_clk_priv),
883 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
884 .ops = &generic_clk_ops,
887 #endif /* CONFIG_AT91_GENERIC_CLK */
889 /* USB clock specific code. */
890 #ifdef CONFIG_AT91_USB_CLK
892 #define AT91_USB_CLK_SOURCE_MAX 2
893 #define AT91_USB_CLK_MAX_DIV 15
895 struct at91_usb_clk_priv {
899 static ulong at91_usb_clk_get_rate(struct clk *clk)
901 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
902 struct at91_pmc *pmc = plat->reg_base;
908 tmp = readl(&pmc->pcr);
909 source_index = (tmp >> AT91_PMC_USB_USBS_OFFSET) &
910 AT91_PMC_USB_USBS_MASK;
911 usbdiv = (tmp >> AT91_PMC_USB_DIV_OFFSET) & AT91_PMC_USB_DIV_MASK;
913 ret = clk_get_by_index(clk->dev, source_index, &source);
917 return clk_get_rate(&source) / (usbdiv + 1);
920 static ulong at91_usb_clk_set_rate(struct clk *clk, ulong rate)
922 struct pmc_platdata *plat = dev_get_platdata(clk->dev);
923 struct at91_pmc *pmc = plat->reg_base;
924 struct at91_usb_clk_priv *priv = dev_get_priv(clk->dev);
925 struct clk source, best_source;
926 ulong tmp_rate, best_rate = rate, source_rate;
927 int tmp_diff, best_diff = -1;
928 u32 div, best_div = 0;
929 u8 best_source_index = 0;
934 for (i = 0; i < priv->num_clksource; i++) {
935 ret = clk_get_by_index(clk->dev, i, &source);
939 source_rate = clk_get_rate(&source);
940 if (IS_ERR_VALUE(source_rate))
943 for (div = 1; div < AT91_USB_CLK_MAX_DIV + 2; div++) {
944 tmp_rate = DIV_ROUND_CLOSEST(source_rate, div);
945 tmp_diff = abs(rate - tmp_rate);
947 if (best_diff < 0 || best_diff > tmp_diff) {
948 best_rate = tmp_rate;
949 best_diff = tmp_diff;
952 best_source = source;
953 best_source_index = i;
956 if (!best_diff || tmp_rate < rate)
964 debug("AT91 USB: best sourc: %s, best_rate = %ld, best_div = %d\n",
965 best_source.dev->name, best_rate, best_div);
967 ret = clk_enable(&best_source);
971 tmp = AT91_PMC_USB_USBS_(best_source_index) |
972 AT91_PMC_USB_DIV_(best_div);
973 writel(tmp, &pmc->usb);
978 static struct clk_ops at91_usb_clk_ops = {
979 .get_rate = at91_usb_clk_get_rate,
980 .set_rate = at91_usb_clk_set_rate,
983 static int at91_usb_clk_ofdata_to_platdata(struct udevice *dev)
985 struct at91_usb_clk_priv *priv = dev_get_priv(dev);
986 u32 cells[AT91_USB_CLK_SOURCE_MAX];
989 num_clksource = fdtdec_get_int_array_count(gd->fdt_blob,
992 AT91_USB_CLK_SOURCE_MAX);
997 priv->num_clksource = num_clksource;
1002 static int at91_usb_clk_probe(struct udevice *dev)
1004 return at91_pmc_core_probe(dev);
1007 static const struct udevice_id at91_usb_clk_match[] = {
1008 { .compatible = "atmel,at91sam9x5-clk-usb" },
1012 U_BOOT_DRIVER(at91_usb_clk) = {
1013 .name = "at91-usb-clk",
1015 .of_match = at91_usb_clk_match,
1016 .probe = at91_usb_clk_probe,
1017 .ofdata_to_platdata = at91_usb_clk_ofdata_to_platdata,
1018 .priv_auto_alloc_size = sizeof(struct at91_usb_clk_priv),
1019 .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
1020 .ops = &at91_usb_clk_ops,
1023 #endif /* CONFIG_AT91_USB_CLK */