pll-base-ld20.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (C) 2016 Socionext Inc.
  4. * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
  5. */
  6. #include <linux/bitfield.h>
  7. #include <linux/bitops.h>
  8. #include <linux/delay.h>
  9. #include <linux/kernel.h>
  10. #include <linux/errno.h>
  11. #include <linux/io.h>
  12. #include <linux/sizes.h>
  13. #include "pll.h"
  14. /* PLL type: SSC */
  15. #define SC_PLLCTRL_SSC_DK_MASK GENMASK(14, 0)
  16. #define SC_PLLCTRL_SSC_EN BIT(31)
  17. #define SC_PLLCTRL2_NRSTDS BIT(28)
  18. #define SC_PLLCTRL2_SSC_JK_MASK GENMASK(26, 0)
  19. #define SC_PLLCTRL3_REGI_MASK GENMASK(19, 16)
  20. /* PLL type: VPLL27 */
  21. #define SC_VPLL27CTRL_WP BIT(0)
  22. #define SC_VPLL27CTRL3_K_LD BIT(28)
  23. /* PLL type: DSPLL */
  24. #define SC_DSPLLCTRL2_K_LD BIT(28)
  25. int uniphier_ld20_sscpll_init(unsigned long reg_base, unsigned int freq,
  26. unsigned int ssc_rate, unsigned int divn)
  27. {
  28. void __iomem *base;
  29. u32 tmp;
  30. base = ioremap(reg_base, SZ_16);
  31. if (!base)
  32. return -ENOMEM;
  33. if (freq != UNIPHIER_PLL_FREQ_DEFAULT) {
  34. tmp = readl(base); /* SSCPLLCTRL */
  35. tmp &= ~SC_PLLCTRL_SSC_DK_MASK;
  36. tmp |= FIELD_PREP(SC_PLLCTRL_SSC_DK_MASK,
  37. DIV_ROUND_CLOSEST(487UL * freq * ssc_rate,
  38. divn * 512));
  39. writel(tmp, base);
  40. tmp = readl(base + 4);
  41. tmp &= ~SC_PLLCTRL2_SSC_JK_MASK;
  42. tmp |= FIELD_PREP(SC_PLLCTRL2_SSC_JK_MASK,
  43. DIV_ROUND_CLOSEST(21431887UL * freq,
  44. divn * 512));
  45. writel(tmp, base + 4);
  46. udelay(50);
  47. }
  48. tmp = readl(base + 4); /* SSCPLLCTRL2 */
  49. tmp |= SC_PLLCTRL2_NRSTDS;
  50. writel(tmp, base + 4);
  51. iounmap(base);
  52. return 0;
  53. }
  54. int uniphier_ld20_sscpll_ssc_en(unsigned long reg_base)
  55. {
  56. void __iomem *base;
  57. u32 tmp;
  58. base = ioremap(reg_base, SZ_16);
  59. if (!base)
  60. return -ENOMEM;
  61. tmp = readl(base); /* SSCPLLCTRL */
  62. tmp |= SC_PLLCTRL_SSC_EN;
  63. writel(tmp, base);
  64. iounmap(base);
  65. return 0;
  66. }
  67. int uniphier_ld20_sscpll_set_regi(unsigned long reg_base, unsigned regi)
  68. {
  69. void __iomem *base;
  70. u32 tmp;
  71. base = ioremap(reg_base, SZ_16);
  72. if (!base)
  73. return -ENOMEM;
  74. tmp = readl(base + 8); /* SSCPLLCTRL3 */
  75. tmp &= ~SC_PLLCTRL3_REGI_MASK;
  76. tmp |= FIELD_PREP(SC_PLLCTRL3_REGI_MASK, regi);
  77. writel(tmp, base + 8);
  78. iounmap(base);
  79. return 0;
  80. }
  81. int uniphier_ld20_vpll27_init(unsigned long reg_base)
  82. {
  83. void __iomem *base;
  84. u32 tmp;
  85. base = ioremap(reg_base, SZ_16);
  86. if (!base)
  87. return -ENOMEM;
  88. tmp = readl(base); /* VPLL27CTRL */
  89. tmp |= SC_VPLL27CTRL_WP; /* write protect off */
  90. writel(tmp, base);
  91. tmp = readl(base + 8); /* VPLL27CTRL3 */
  92. tmp |= SC_VPLL27CTRL3_K_LD;
  93. writel(tmp, base + 8);
  94. tmp = readl(base); /* VPLL27CTRL */
  95. tmp &= ~SC_VPLL27CTRL_WP; /* write protect on */
  96. writel(tmp, base);
  97. iounmap(base);
  98. return 0;
  99. }
  100. int uniphier_ld20_dspll_init(unsigned long reg_base)
  101. {
  102. void __iomem *base;
  103. u32 tmp;
  104. base = ioremap(reg_base, SZ_16);
  105. if (!base)
  106. return -ENOMEM;
  107. tmp = readl(base + 4); /* DSPLLCTRL2 */
  108. tmp |= SC_DSPLLCTRL2_K_LD;
  109. writel(tmp, base + 4);
  110. iounmap(base);
  111. return 0;
  112. }