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
10 #include <clk-uclass.h>
12 #include <asm/global_data.h>
13 #include <dm/device_compat.h>
16 #include <mach/at91_pmc.h>
17 #include <mach/at91_sfr.h>
23 DECLARE_GLOBAL_DATA_PTR;
26 struct at91_pmc *reg_base;
27 struct regmap *regmap_sfr;
30 static const struct udevice_id at91_pmc_match[] = {
31 { .compatible = "atmel,at91rm9200-pmc" },
32 { .compatible = "atmel,at91sam9260-pmc" },
33 { .compatible = "atmel,at91sam9g45-pmc" },
34 { .compatible = "atmel,at91sam9n12-pmc" },
35 { .compatible = "atmel,at91sam9x5-pmc" },
36 { .compatible = "atmel,sama5d3-pmc" },
37 { .compatible = "atmel,sama5d2-pmc" },
41 U_BOOT_DRIVER(at91_pmc) = {
43 .id = UCLASS_SIMPLE_BUS,
44 .of_match = at91_pmc_match,
47 static int at91_pmc_core_probe(struct udevice *dev)
49 struct pmc_plat *plat = dev_get_plat(dev);
51 dev = dev_get_parent(dev);
53 plat->reg_base = dev_read_addr_ptr(dev);
59 * at91_clk_sub_device_bind() - for the at91 clock driver
60 * Recursively bind its children as clk devices.
62 * @return: 0 on success, or negative error code on failure
64 int at91_clk_sub_device_bind(struct udevice *dev, const char *drv_name)
66 ofnode parent = dev_ofnode(dev);
68 bool pre_reloc_only = !(gd->flags & GD_FLG_RELOC);
72 ofnode_for_each_subnode(node, parent) {
73 if (pre_reloc_only && !ofnode_pre_reloc(node))
76 * If this node has "compatible" property, this is not
77 * a clock sub-node, but a normal device. skip.
79 if (ofnode_read_prop(node, "compatible", NULL))
82 if (ret != -FDT_ERR_NOTFOUND)
85 name = ofnode_get_name(node);
88 ret = device_bind_driver_to_node(dev, drv_name, name, node,
97 int at91_clk_of_xlate(struct clk *clk, struct ofnode_phandle_args *args)
101 if (args->args_count) {
102 debug("Invalid args_count: %d\n", args->args_count);
106 periph = fdtdec_get_uint(gd->fdt_blob, dev_of_offset(clk->dev), "reg",
116 int at91_clk_probe(struct udevice *dev)
118 struct udevice *dev_periph_container, *dev_pmc;
119 struct pmc_plat *plat = dev_get_plat(dev);
121 dev_periph_container = dev_get_parent(dev);
122 dev_pmc = dev_get_parent(dev_periph_container);
124 plat->reg_base = dev_read_addr_ptr(dev_pmc);
129 /* SCKC specific code. */
130 static const struct udevice_id at91_sckc_match[] = {
131 { .compatible = "atmel,at91sam9x5-sckc" },
135 U_BOOT_DRIVER(at91_sckc) = {
137 .id = UCLASS_SIMPLE_BUS,
138 .of_match = at91_sckc_match,
141 /* Slow clock specific code. */
142 static int at91_slow_clk_enable(struct clk *clk)
147 static ulong at91_slow_clk_get_rate(struct clk *clk)
149 return CONFIG_SYS_AT91_SLOW_CLOCK;
152 static struct clk_ops at91_slow_clk_ops = {
153 .enable = at91_slow_clk_enable,
154 .get_rate = at91_slow_clk_get_rate,
157 static const struct udevice_id at91_slow_clk_match[] = {
158 { .compatible = "atmel,at91sam9x5-clk-slow" },
162 U_BOOT_DRIVER(at91_slow_clk) = {
163 .name = "at91-slow-clk",
165 .of_match = at91_slow_clk_match,
166 .ops = &at91_slow_clk_ops,
169 /* Master clock specific code. */
170 static ulong at91_master_clk_get_rate(struct clk *clk)
172 return gd->arch.mck_rate_hz;
175 static struct clk_ops at91_master_clk_ops = {
176 .get_rate = at91_master_clk_get_rate,
179 static const struct udevice_id at91_master_clk_match[] = {
180 { .compatible = "atmel,at91rm9200-clk-master" },
181 { .compatible = "atmel,at91sam9x5-clk-master" },
185 U_BOOT_DRIVER(at91_master_clk) = {
186 .name = "at91-master-clk",
188 .of_match = at91_master_clk_match,
189 .ops = &at91_master_clk_ops,
192 /* Main osc clock specific code. */
193 static int main_osc_clk_enable(struct clk *clk)
195 struct pmc_plat *plat = dev_get_plat(clk->dev);
196 struct at91_pmc *pmc = plat->reg_base;
198 if (readl(&pmc->sr) & AT91_PMC_MOSCSELS)
204 static ulong main_osc_clk_get_rate(struct clk *clk)
206 return gd->arch.main_clk_rate_hz;
209 static struct clk_ops main_osc_clk_ops = {
210 .enable = main_osc_clk_enable,
211 .get_rate = main_osc_clk_get_rate,
214 static int main_osc_clk_probe(struct udevice *dev)
216 return at91_pmc_core_probe(dev);
219 static const struct udevice_id main_osc_clk_match[] = {
220 { .compatible = "atmel,at91sam9x5-clk-main" },
224 U_BOOT_DRIVER(at91sam9x5_main_osc_clk) = {
225 .name = "at91sam9x5-main-osc-clk",
227 .of_match = main_osc_clk_match,
228 .probe = main_osc_clk_probe,
229 .plat_auto = sizeof(struct pmc_plat),
230 .ops = &main_osc_clk_ops,
233 /* PLLA clock specific code. */
234 static int plla_clk_enable(struct clk *clk)
236 struct pmc_plat *plat = dev_get_plat(clk->dev);
237 struct at91_pmc *pmc = plat->reg_base;
239 if (readl(&pmc->sr) & AT91_PMC_LOCKA)
245 static ulong plla_clk_get_rate(struct clk *clk)
247 return gd->arch.plla_rate_hz;
250 static struct clk_ops plla_clk_ops = {
251 .enable = plla_clk_enable,
252 .get_rate = plla_clk_get_rate,
255 static int plla_clk_probe(struct udevice *dev)
257 return at91_pmc_core_probe(dev);
260 static const struct udevice_id plla_clk_match[] = {
261 { .compatible = "atmel,sama5d3-clk-pll" },
265 U_BOOT_DRIVER(at91_plla_clk) = {
266 .name = "at91-plla-clk",
268 .of_match = plla_clk_match,
269 .probe = plla_clk_probe,
270 .plat_auto = sizeof(struct pmc_plat),
271 .ops = &plla_clk_ops,
274 /* PLLA DIV clock specific code. */
275 static int at91_plladiv_clk_enable(struct clk *clk)
280 static ulong at91_plladiv_clk_get_rate(struct clk *clk)
282 struct pmc_plat *plat = dev_get_plat(clk->dev);
283 struct at91_pmc *pmc = plat->reg_base;
288 ret = clk_get_by_index(clk->dev, 0, &source);
292 clk_rate = clk_get_rate(&source);
293 if (readl(&pmc->mckr) & AT91_PMC_MCKR_PLLADIV_2)
299 static ulong at91_plladiv_clk_set_rate(struct clk *clk, ulong rate)
301 struct pmc_plat *plat = dev_get_plat(clk->dev);
302 struct at91_pmc *pmc = plat->reg_base;
307 ret = clk_get_by_index(clk->dev, 0, &source);
311 parent_rate = clk_get_rate(&source);
312 if ((parent_rate != rate) && ((parent_rate) / 2 != rate))
315 if (parent_rate != rate) {
316 writel((readl(&pmc->mckr) | AT91_PMC_MCKR_PLLADIV_2),
323 static struct clk_ops at91_plladiv_clk_ops = {
324 .enable = at91_plladiv_clk_enable,
325 .get_rate = at91_plladiv_clk_get_rate,
326 .set_rate = at91_plladiv_clk_set_rate,
329 static int at91_plladiv_clk_probe(struct udevice *dev)
331 return at91_pmc_core_probe(dev);
334 static const struct udevice_id at91_plladiv_clk_match[] = {
335 { .compatible = "atmel,at91sam9x5-clk-plldiv" },
339 U_BOOT_DRIVER(at91_plladiv_clk) = {
340 .name = "at91-plladiv-clk",
342 .of_match = at91_plladiv_clk_match,
343 .probe = at91_plladiv_clk_probe,
344 .plat_auto = sizeof(struct pmc_plat),
345 .ops = &at91_plladiv_clk_ops,
348 /* System clock specific code. */
349 #define SYSTEM_MAX_ID 31
352 * at91_system_clk_bind() - for the system clock driver
353 * Recursively bind its children as clk devices.
355 * @return: 0 on success, or negative error code on failure
357 static int at91_system_clk_bind(struct udevice *dev)
359 return at91_clk_sub_device_bind(dev, "system-clk");
362 static const struct udevice_id at91_system_clk_match[] = {
363 { .compatible = "atmel,at91rm9200-clk-system" },
367 U_BOOT_DRIVER(at91_system_clk) = {
368 .name = "at91-system-clk",
370 .of_match = at91_system_clk_match,
371 .bind = at91_system_clk_bind,
374 static inline int is_pck(int id)
376 return (id >= 8) && (id <= 15);
379 static ulong system_clk_get_rate(struct clk *clk)
384 ret = clk_get_by_index(clk->dev, 0, &clk_dev);
388 return clk_get_rate(&clk_dev);
391 static ulong system_clk_set_rate(struct clk *clk, ulong rate)
396 ret = clk_get_by_index(clk->dev, 0, &clk_dev);
400 return clk_set_rate(&clk_dev, rate);
403 static int system_clk_enable(struct clk *clk)
405 struct pmc_plat *plat = dev_get_plat(clk->dev);
406 struct at91_pmc *pmc = plat->reg_base;
409 if (clk->id > SYSTEM_MAX_ID)
414 writel(mask, &pmc->scer);
417 * For the programmable clocks the Ready status in the PMC
418 * status register should be checked after enabling.
419 * For other clocks this is unnecessary.
421 if (!is_pck(clk->id))
424 while (!(readl(&pmc->sr) & mask))
430 static struct clk_ops system_clk_ops = {
431 .of_xlate = at91_clk_of_xlate,
432 .get_rate = system_clk_get_rate,
433 .set_rate = system_clk_set_rate,
434 .enable = system_clk_enable,
437 U_BOOT_DRIVER(system_clk) = {
438 .name = "system-clk",
440 .probe = at91_clk_probe,
441 .plat_auto = sizeof(struct pmc_plat),
442 .ops = &system_clk_ops,
445 /* Peripheral clock specific code. */
446 #define PERIPHERAL_ID_MIN 2
447 #define PERIPHERAL_ID_MAX 31
448 #define PERIPHERAL_MASK(id) (1 << ((id) & PERIPHERAL_ID_MAX))
450 enum periph_clk_type {
451 CLK_PERIPH_AT91RM9200 = 0,
452 CLK_PERIPH_AT91SAM9X5,
456 * sam9x5_periph_clk_bind() - for the periph clock driver
457 * Recursively bind its children as clk devices.
459 * @return: 0 on success, or negative error code on failure
461 static int sam9x5_periph_clk_bind(struct udevice *dev)
463 return at91_clk_sub_device_bind(dev, "periph-clk");
466 static const struct udevice_id sam9x5_periph_clk_match[] = {
468 .compatible = "atmel,at91rm9200-clk-peripheral",
469 .data = CLK_PERIPH_AT91RM9200,
472 .compatible = "atmel,at91sam9x5-clk-peripheral",
473 .data = CLK_PERIPH_AT91SAM9X5,
478 U_BOOT_DRIVER(sam9x5_periph_clk) = {
479 .name = "sam9x5-periph-clk",
481 .of_match = sam9x5_periph_clk_match,
482 .bind = sam9x5_periph_clk_bind,
485 static int periph_clk_enable(struct clk *clk)
487 struct pmc_plat *plat = dev_get_plat(clk->dev);
488 struct at91_pmc *pmc = plat->reg_base;
489 enum periph_clk_type clk_type;
492 if (clk->id < PERIPHERAL_ID_MIN)
495 clk_type = dev_get_driver_data(dev_get_parent(clk->dev));
496 if (clk_type == CLK_PERIPH_AT91RM9200) {
498 if (clk->id > PERIPHERAL_ID_MAX)
501 setbits_le32(addr, PERIPHERAL_MASK(clk->id));
503 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
504 setbits_le32(&pmc->pcr,
505 AT91_PMC_PCR_CMD_WRITE | AT91_PMC_PCR_EN);
511 static ulong periph_get_rate(struct clk *clk)
518 dev = dev_get_parent(clk->dev);
520 ret = clk_get_by_index(dev, 0, &clk_dev);
524 clk_rate = clk_get_rate(&clk_dev);
531 static struct clk_ops periph_clk_ops = {
532 .of_xlate = at91_clk_of_xlate,
533 .enable = periph_clk_enable,
534 .get_rate = periph_get_rate,
537 U_BOOT_DRIVER(clk_periph) = {
538 .name = "periph-clk",
540 .plat_auto = sizeof(struct pmc_plat),
541 .probe = at91_clk_probe,
542 .ops = &periph_clk_ops,
545 /* UTMI clock specific code. */
546 #ifdef CONFIG_AT91_UTMI
549 * The purpose of this clock is to generate a 480 MHz signal. A different
550 * rate can't be configured.
552 #define UTMI_RATE 480000000
554 static int utmi_clk_enable(struct clk *clk)
556 struct pmc_plat *plat = dev_get_plat(clk->dev);
557 struct at91_pmc *pmc = plat->reg_base;
560 u32 utmi_ref_clk_freq;
563 int timeout = 2000000;
565 if (readl(&pmc->sr) & AT91_PMC_LOCKU)
569 * If mainck rate is different from 12 MHz, we have to configure the
570 * FREQ field of the SFR_UTMICKTRIM register to generate properly
573 err = clk_get_by_index(clk->dev, 0, &clk_dev);
577 clk_rate = clk_get_rate(&clk_dev);
580 utmi_ref_clk_freq = 0;
583 utmi_ref_clk_freq = 1;
586 utmi_ref_clk_freq = 2;
589 * Not supported on SAMA5D2 but it's not an issue since MAINCK
590 * maximum value is 24 MHz.
593 utmi_ref_clk_freq = 3;
596 printf("UTMICK: unsupported mainck rate\n");
600 if (plat->regmap_sfr) {
601 err = regmap_read(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, &tmp);
605 tmp &= ~AT91_UTMICKTRIM_FREQ;
606 tmp |= utmi_ref_clk_freq;
607 err = regmap_write(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, tmp);
610 } else if (utmi_ref_clk_freq) {
611 printf("UTMICK: sfr node required\n");
615 tmp = readl(&pmc->uckr);
616 tmp |= AT91_PMC_UPLLEN |
619 writel(tmp, &pmc->uckr);
621 while ((--timeout) && !(readl(&pmc->sr) & AT91_PMC_LOCKU))
624 printf("UTMICK: timeout waiting for UPLL lock\n");
631 static ulong utmi_clk_get_rate(struct clk *clk)
633 /* UTMI clk rate is fixed. */
637 static struct clk_ops utmi_clk_ops = {
638 .enable = utmi_clk_enable,
639 .get_rate = utmi_clk_get_rate,
642 static int utmi_clk_of_to_plat(struct udevice *dev)
644 struct pmc_plat *plat = dev_get_plat(dev);
645 struct udevice *syscon;
647 uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
648 "regmap-sfr", &syscon);
651 plat->regmap_sfr = syscon_get_regmap(syscon);
656 static int utmi_clk_probe(struct udevice *dev)
658 return at91_pmc_core_probe(dev);
661 static const struct udevice_id utmi_clk_match[] = {
662 { .compatible = "atmel,at91sam9x5-clk-utmi" },
666 U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
667 .name = "at91sam9x5-utmi-clk",
669 .of_match = utmi_clk_match,
670 .probe = utmi_clk_probe,
671 .of_to_plat = utmi_clk_of_to_plat,
672 .plat_auto = sizeof(struct pmc_plat),
673 .ops = &utmi_clk_ops,
676 #endif /* CONFIG_AT91_UTMI */
678 /* H32MX clock specific code. */
679 #ifdef CONFIG_AT91_H32MX
681 #define H32MX_MAX_FREQ 90000000
683 static ulong sama5d4_h32mx_clk_get_rate(struct clk *clk)
685 struct pmc_plat *plat = dev_get_plat(clk->dev);
686 struct at91_pmc *pmc = plat->reg_base;
687 ulong rate = gd->arch.mck_rate_hz;
689 if (readl(&pmc->mckr) & AT91_PMC_MCKR_H32MXDIV)
692 if (rate > H32MX_MAX_FREQ)
693 dev_dbg(clk->dev, "H32MX clock is too fast\n");
698 static struct clk_ops sama5d4_h32mx_clk_ops = {
699 .get_rate = sama5d4_h32mx_clk_get_rate,
702 static int sama5d4_h32mx_clk_probe(struct udevice *dev)
704 return at91_pmc_core_probe(dev);
707 static const struct udevice_id sama5d4_h32mx_clk_match[] = {
708 { .compatible = "atmel,sama5d4-clk-h32mx" },
712 U_BOOT_DRIVER(sama5d4_h32mx_clk) = {
713 .name = "sama5d4-h32mx-clk",
715 .of_match = sama5d4_h32mx_clk_match,
716 .probe = sama5d4_h32mx_clk_probe,
717 .plat_auto = sizeof(struct pmc_plat),
718 .ops = &sama5d4_h32mx_clk_ops,
721 #endif /* CONFIG_AT91_H32MX */
723 /* Generic clock specific code. */
724 #ifdef CONFIG_AT91_GENERIC_CLK
726 #define GENERATED_SOURCE_MAX 6
727 #define GENERATED_MAX_DIV 255
730 * generated_clk_bind() - for the generated clock driver
731 * Recursively bind its children as clk devices.
733 * @return: 0 on success, or negative error code on failure
735 static int generated_clk_bind(struct udevice *dev)
737 return at91_clk_sub_device_bind(dev, "generic-clk");
740 static const struct udevice_id generated_clk_match[] = {
741 { .compatible = "atmel,sama5d2-clk-generated" },
745 U_BOOT_DRIVER(generated_clk) = {
746 .name = "generated-clk",
748 .of_match = generated_clk_match,
749 .bind = generated_clk_bind,
752 struct generic_clk_priv {
756 static ulong generic_clk_get_rate(struct clk *clk)
758 struct pmc_plat *plat = dev_get_plat(clk->dev);
759 struct at91_pmc *pmc = plat->reg_base;
763 u8 clock_source, parent_index;
766 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
767 tmp = readl(&pmc->pcr);
768 clock_source = (tmp >> AT91_PMC_PCR_GCKCSS_OFFSET) &
769 AT91_PMC_PCR_GCKCSS_MASK;
770 gckdiv = (tmp >> AT91_PMC_PCR_GCKDIV_OFFSET) & AT91_PMC_PCR_GCKDIV_MASK;
772 parent_index = clock_source - 1;
773 ret = clk_get_by_index(dev_get_parent(clk->dev), parent_index, &parent);
777 clk_rate = clk_get_rate(&parent) / (gckdiv + 1);
784 static ulong generic_clk_set_rate(struct clk *clk, ulong rate)
786 struct pmc_plat *plat = dev_get_plat(clk->dev);
787 struct at91_pmc *pmc = plat->reg_base;
788 struct generic_clk_priv *priv = dev_get_priv(clk->dev);
789 struct clk parent, best_parent;
790 ulong tmp_rate, best_rate = rate, parent_rate;
791 int tmp_diff, best_diff = -1;
792 u32 div, best_div = 0;
793 u8 best_parent_index, best_clock_source = 0;
798 for (i = 0; i < priv->num_parents; i++) {
799 ret = clk_get_by_index(dev_get_parent(clk->dev), i, &parent);
803 parent_rate = clk_get_rate(&parent);
804 if (IS_ERR_VALUE(parent_rate))
807 for (div = 1; div < GENERATED_MAX_DIV + 2; div++) {
808 tmp_rate = DIV_ROUND_CLOSEST(parent_rate, div);
809 tmp_diff = abs(rate - tmp_rate);
811 if (best_diff < 0 || best_diff > tmp_diff) {
812 best_rate = tmp_rate;
813 best_diff = tmp_diff;
816 best_parent = parent;
817 best_parent_index = i;
818 best_clock_source = best_parent_index + 1;
821 if (!best_diff || tmp_rate < rate)
829 debug("GCK: best parent: %s, best_rate = %ld, best_div = %d\n",
830 best_parent.dev->name, best_rate, best_div);
832 ret = clk_enable(&best_parent);
836 writel(clk->id & AT91_PMC_PCR_PID_MASK, &pmc->pcr);
837 tmp = readl(&pmc->pcr);
838 tmp &= ~(AT91_PMC_PCR_GCKDIV | AT91_PMC_PCR_GCKCSS);
839 tmp |= AT91_PMC_PCR_GCKCSS_(best_clock_source) |
840 AT91_PMC_PCR_CMD_WRITE |
841 AT91_PMC_PCR_GCKDIV_(best_div) |
843 writel(tmp, &pmc->pcr);
845 while (!(readl(&pmc->sr) & AT91_PMC_GCKRDY))
851 static struct clk_ops generic_clk_ops = {
852 .of_xlate = at91_clk_of_xlate,
853 .get_rate = generic_clk_get_rate,
854 .set_rate = generic_clk_set_rate,
857 static int generic_clk_of_to_plat(struct udevice *dev)
859 struct generic_clk_priv *priv = dev_get_priv(dev);
860 u32 cells[GENERATED_SOURCE_MAX];
863 num_parents = fdtdec_get_int_array_count(gd->fdt_blob,
864 dev_of_offset(dev_get_parent(dev)), "clocks", cells,
865 GENERATED_SOURCE_MAX);
870 priv->num_parents = num_parents;
875 U_BOOT_DRIVER(generic_clk) = {
876 .name = "generic-clk",
878 .probe = at91_clk_probe,
879 .of_to_plat = generic_clk_of_to_plat,
880 .priv_auto = sizeof(struct generic_clk_priv),
881 .plat_auto = sizeof(struct pmc_plat),
882 .ops = &generic_clk_ops,
885 #endif /* CONFIG_AT91_GENERIC_CLK */
887 /* USB clock specific code. */
888 #ifdef CONFIG_AT91_USB_CLK
890 #define AT91_USB_CLK_SOURCE_MAX 2
891 #define AT91_USB_CLK_MAX_DIV 15
893 struct at91_usb_clk_priv {
897 static ulong at91_usb_clk_get_rate(struct clk *clk)
899 struct pmc_plat *plat = dev_get_plat(clk->dev);
900 struct at91_pmc *pmc = plat->reg_base;
906 tmp = readl(&pmc->pcr);
907 source_index = (tmp >> AT91_PMC_USB_USBS_OFFSET) &
908 AT91_PMC_USB_USBS_MASK;
909 usbdiv = (tmp >> AT91_PMC_USB_DIV_OFFSET) & AT91_PMC_USB_DIV_MASK;
911 ret = clk_get_by_index(clk->dev, source_index, &source);
915 return clk_get_rate(&source) / (usbdiv + 1);
918 static ulong at91_usb_clk_set_rate(struct clk *clk, ulong rate)
920 struct pmc_plat *plat = dev_get_plat(clk->dev);
921 struct at91_pmc *pmc = plat->reg_base;
922 struct at91_usb_clk_priv *priv = dev_get_priv(clk->dev);
923 struct clk source, best_source;
924 ulong tmp_rate, best_rate = rate, source_rate;
925 int tmp_diff, best_diff = -1;
926 u32 div, best_div = 0;
927 u8 best_source_index = 0;
932 for (i = 0; i < priv->num_clksource; i++) {
933 ret = clk_get_by_index(clk->dev, i, &source);
937 source_rate = clk_get_rate(&source);
938 if (IS_ERR_VALUE(source_rate))
941 for (div = 1; div < AT91_USB_CLK_MAX_DIV + 2; div++) {
942 tmp_rate = DIV_ROUND_CLOSEST(source_rate, div);
943 tmp_diff = abs(rate - tmp_rate);
945 if (best_diff < 0 || best_diff > tmp_diff) {
946 best_rate = tmp_rate;
947 best_diff = tmp_diff;
950 best_source = source;
951 best_source_index = i;
954 if (!best_diff || tmp_rate < rate)
962 debug("AT91 USB: best sourc: %s, best_rate = %ld, best_div = %d\n",
963 best_source.dev->name, best_rate, best_div);
965 ret = clk_enable(&best_source);
969 tmp = AT91_PMC_USB_USBS_(best_source_index) |
970 AT91_PMC_USB_DIV_(best_div);
971 writel(tmp, &pmc->usb);
976 static struct clk_ops at91_usb_clk_ops = {
977 .get_rate = at91_usb_clk_get_rate,
978 .set_rate = at91_usb_clk_set_rate,
981 static int at91_usb_clk_of_to_plat(struct udevice *dev)
983 struct at91_usb_clk_priv *priv = dev_get_priv(dev);
984 u32 cells[AT91_USB_CLK_SOURCE_MAX];
987 num_clksource = fdtdec_get_int_array_count(gd->fdt_blob,
990 AT91_USB_CLK_SOURCE_MAX);
995 priv->num_clksource = num_clksource;
1000 static int at91_usb_clk_probe(struct udevice *dev)
1002 return at91_pmc_core_probe(dev);
1005 static const struct udevice_id at91_usb_clk_match[] = {
1006 { .compatible = "atmel,at91sam9x5-clk-usb" },
1010 U_BOOT_DRIVER(at91_usb_clk) = {
1011 .name = "at91-usb-clk",
1013 .of_match = at91_usb_clk_match,
1014 .probe = at91_usb_clk_probe,
1015 .of_to_plat = at91_usb_clk_of_to_plat,
1016 .priv_auto = sizeof(struct at91_usb_clk_priv),
1017 .plat_auto = sizeof(struct pmc_plat),
1018 .ops = &at91_usb_clk_ops,
1021 #endif /* CONFIG_AT91_USB_CLK */