clk-utmi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Atmel Corporation
  4. * Wenyou.Yang <wenyou.yang@atmel.com>
  5. */
  6. #include <common.h>
  7. #include <clk-uclass.h>
  8. #include <dm.h>
  9. #include <syscon.h>
  10. #include <linux/io.h>
  11. #include <mach/at91_pmc.h>
  12. #include <mach/sama5_sfr.h>
  13. #include "pmc.h"
  14. /*
  15. * The purpose of this clock is to generate a 480 MHz signal. A different
  16. * rate can't be configured.
  17. */
  18. #define UTMI_RATE 480000000
  19. static int utmi_clk_enable(struct clk *clk)
  20. {
  21. struct pmc_platdata *plat = dev_get_platdata(clk->dev);
  22. struct at91_pmc *pmc = plat->reg_base;
  23. struct clk clk_dev;
  24. ulong clk_rate;
  25. u32 utmi_ref_clk_freq;
  26. u32 tmp;
  27. int err;
  28. if (readl(&pmc->sr) & AT91_PMC_LOCKU)
  29. return 0;
  30. /*
  31. * If mainck rate is different from 12 MHz, we have to configure the
  32. * FREQ field of the SFR_UTMICKTRIM register to generate properly
  33. * the utmi clock.
  34. */
  35. err = clk_get_by_index(clk->dev, 0, &clk_dev);
  36. if (err)
  37. return -EINVAL;
  38. clk_rate = clk_get_rate(&clk_dev);
  39. switch (clk_rate) {
  40. case 12000000:
  41. utmi_ref_clk_freq = 0;
  42. break;
  43. case 16000000:
  44. utmi_ref_clk_freq = 1;
  45. break;
  46. case 24000000:
  47. utmi_ref_clk_freq = 2;
  48. break;
  49. /*
  50. * Not supported on SAMA5D2 but it's not an issue since MAINCK
  51. * maximum value is 24 MHz.
  52. */
  53. case 48000000:
  54. utmi_ref_clk_freq = 3;
  55. break;
  56. default:
  57. printf("UTMICK: unsupported mainck rate\n");
  58. return -EINVAL;
  59. }
  60. if (plat->regmap_sfr) {
  61. err = regmap_read(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, &tmp);
  62. if (err)
  63. return -EINVAL;
  64. tmp &= ~AT91_UTMICKTRIM_FREQ;
  65. tmp |= utmi_ref_clk_freq;
  66. err = regmap_write(plat->regmap_sfr, AT91_SFR_UTMICKTRIM, tmp);
  67. if (err)
  68. return -EINVAL;
  69. } else if (utmi_ref_clk_freq) {
  70. printf("UTMICK: sfr node required\n");
  71. return -EINVAL;
  72. }
  73. tmp = readl(&pmc->uckr);
  74. tmp |= AT91_PMC_UPLLEN |
  75. AT91_PMC_UPLLCOUNT |
  76. AT91_PMC_BIASEN;
  77. writel(tmp, &pmc->uckr);
  78. while (!(readl(&pmc->sr) & AT91_PMC_LOCKU))
  79. ;
  80. return 0;
  81. }
  82. static ulong utmi_clk_get_rate(struct clk *clk)
  83. {
  84. /* UTMI clk rate is fixed. */
  85. return UTMI_RATE;
  86. }
  87. static struct clk_ops utmi_clk_ops = {
  88. .enable = utmi_clk_enable,
  89. .get_rate = utmi_clk_get_rate,
  90. };
  91. static int utmi_clk_ofdata_to_platdata(struct udevice *dev)
  92. {
  93. struct pmc_platdata *plat = dev_get_platdata(dev);
  94. struct udevice *syscon;
  95. uclass_get_device_by_phandle(UCLASS_SYSCON, dev,
  96. "regmap-sfr", &syscon);
  97. if (syscon)
  98. plat->regmap_sfr = syscon_get_regmap(syscon);
  99. return 0;
  100. }
  101. static int utmi_clk_probe(struct udevice *dev)
  102. {
  103. return at91_pmc_core_probe(dev);
  104. }
  105. static const struct udevice_id utmi_clk_match[] = {
  106. { .compatible = "atmel,at91sam9x5-clk-utmi" },
  107. {}
  108. };
  109. U_BOOT_DRIVER(at91sam9x5_utmi_clk) = {
  110. .name = "at91sam9x5-utmi-clk",
  111. .id = UCLASS_CLK,
  112. .of_match = utmi_clk_match,
  113. .probe = utmi_clk_probe,
  114. .ofdata_to_platdata = utmi_clk_ofdata_to_platdata,
  115. .platdata_auto_alloc_size = sizeof(struct pmc_platdata),
  116. .ops = &utmi_clk_ops,
  117. };