clock.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright (C) 2015 Atmel Corporation
  3. * Wenyou Yang <wenyou.yang@atmel.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/io.h>
  9. #include <asm/arch/hardware.h>
  10. #include <asm/arch/at91_pmc.h>
  11. #define EN_UPLL_TIMEOUT 500
  12. void at91_periph_clk_enable(int id)
  13. {
  14. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  15. #ifdef CPU_HAS_PCR
  16. u32 regval;
  17. u32 div_value;
  18. if (id > AT91_PMC_PCR_PID_MASK)
  19. return;
  20. writel(id, &pmc->pcr);
  21. div_value = readl(&pmc->pcr) & AT91_PMC_PCR_DIV;
  22. regval = AT91_PMC_PCR_EN | AT91_PMC_PCR_CMD_WRITE | id | div_value;
  23. writel(regval, &pmc->pcr);
  24. #else
  25. writel(0x01 << id, &pmc->pcer);
  26. #endif
  27. }
  28. void at91_periph_clk_disable(int id)
  29. {
  30. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  31. #ifdef CPU_HAS_PCR
  32. u32 regval;
  33. if (id > AT91_PMC_PCR_PID_MASK)
  34. return;
  35. regval = AT91_PMC_PCR_CMD_WRITE | id;
  36. writel(regval, &pmc->pcr);
  37. #else
  38. writel(0x01 << id, &pmc->pcdr);
  39. #endif
  40. }
  41. void at91_system_clk_enable(int sys_clk)
  42. {
  43. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  44. writel(sys_clk, &pmc->scer);
  45. }
  46. void at91_system_clk_disable(int sys_clk)
  47. {
  48. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  49. writel(sys_clk, &pmc->scdr);
  50. }
  51. int at91_upll_clk_enable(void)
  52. {
  53. struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
  54. ulong start_time, tmp_time;
  55. if ((readl(&pmc->uckr) & AT91_PMC_UPLLEN) == AT91_PMC_UPLLEN)
  56. return 0;
  57. start_time = get_timer(0);
  58. writel(AT91_PMC_UPLLEN | AT91_PMC_BIASEN, &pmc->uckr);
  59. while ((readl(&pmc->sr) & AT91_PMC_LOCKU) != AT91_PMC_LOCKU) {
  60. tmp_time = get_timer(0);
  61. if ((tmp_time - start_time) > EN_UPLL_TIMEOUT) {
  62. printf("ERROR: failed to enable UPLL\n");
  63. return -1;
  64. }
  65. }
  66. return 0;
  67. }
  68. int at91_upll_clk_disable(void)
  69. {
  70. struct at91_pmc *pmc = (at91_pmc_t *)ATMEL_BASE_PMC;
  71. ulong start_time, tmp_time;
  72. start_time = get_timer(0);
  73. writel(readl(&pmc->uckr) & ~AT91_PMC_UPLLEN, &pmc->uckr);
  74. while ((readl(&pmc->sr) & AT91_PMC_LOCKU) == AT91_PMC_LOCKU) {
  75. tmp_time = get_timer(0);
  76. if ((tmp_time - start_time) > EN_UPLL_TIMEOUT) {
  77. printf("ERROR: failed to stop UPLL\n");
  78. return -1;
  79. }
  80. }
  81. return 0;
  82. }
  83. void at91_usb_clk_init(u32 value)
  84. {
  85. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  86. writel(value, &pmc->usb);
  87. }
  88. void at91_pllicpr_init(u32 icpr)
  89. {
  90. struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
  91. writel(icpr, &pmc->pllicpr);
  92. }