clk-clps711x.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Cirrus Logic CLPS711X CLK driver
  3. *
  4. * Copyright (C) 2014 Alexander Shiyan <[email protected]>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/clk-provider.h>
  12. #include <linux/clkdev.h>
  13. #include <linux/io.h>
  14. #include <linux/ioport.h>
  15. #include <linux/of_address.h>
  16. #include <linux/slab.h>
  17. #include <linux/mfd/syscon/clps711x.h>
  18. #include <dt-bindings/clock/clps711x-clock.h>
  19. #define CLPS711X_SYSCON1 (0x0100)
  20. #define CLPS711X_SYSCON2 (0x1100)
  21. #define CLPS711X_SYSFLG2 (CLPS711X_SYSCON2 + SYSFLG_OFFSET)
  22. #define CLPS711X_PLLR (0xa5a8)
  23. #define CLPS711X_EXT_FREQ (13000000)
  24. #define CLPS711X_OSC_FREQ (3686400)
  25. static const struct clk_div_table spi_div_table[] = {
  26. { .val = 0, .div = 32, },
  27. { .val = 1, .div = 8, },
  28. { .val = 2, .div = 2, },
  29. { .val = 3, .div = 1, },
  30. };
  31. static const struct clk_div_table timer_div_table[] = {
  32. { .val = 0, .div = 256, },
  33. { .val = 1, .div = 1, },
  34. };
  35. struct clps711x_clk {
  36. spinlock_t lock;
  37. struct clk_hw_onecell_data clk_data;
  38. };
  39. static struct clps711x_clk * __init _clps711x_clk_init(void __iomem *base,
  40. u32 fref)
  41. {
  42. u32 tmp, f_cpu, f_pll, f_bus, f_tim, f_pwm, f_spi;
  43. struct clps711x_clk *clps711x_clk;
  44. unsigned i;
  45. if (!base)
  46. return ERR_PTR(-ENOMEM);
  47. clps711x_clk = kzalloc(sizeof(*clps711x_clk) +
  48. sizeof(*clps711x_clk->clk_data.hws) * CLPS711X_CLK_MAX,
  49. GFP_KERNEL);
  50. if (!clps711x_clk)
  51. return ERR_PTR(-ENOMEM);
  52. spin_lock_init(&clps711x_clk->lock);
  53. /* Read PLL multiplier value and sanity check */
  54. tmp = readl(base + CLPS711X_PLLR) >> 24;
  55. if (((tmp >= 10) && (tmp <= 50)) || !fref)
  56. f_pll = DIV_ROUND_UP(CLPS711X_OSC_FREQ * tmp, 2);
  57. else
  58. f_pll = fref;
  59. tmp = readl(base + CLPS711X_SYSFLG2);
  60. if (tmp & SYSFLG2_CKMODE) {
  61. f_cpu = CLPS711X_EXT_FREQ;
  62. f_bus = CLPS711X_EXT_FREQ;
  63. f_spi = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 96);
  64. f_pll = 0;
  65. f_pwm = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 128);
  66. } else {
  67. f_cpu = f_pll;
  68. if (f_cpu > 36864000)
  69. f_bus = DIV_ROUND_UP(f_cpu, 2);
  70. else
  71. f_bus = 36864000 / 2;
  72. f_spi = DIV_ROUND_CLOSEST(f_cpu, 576);
  73. f_pwm = DIV_ROUND_CLOSEST(f_cpu, 768);
  74. }
  75. if (tmp & SYSFLG2_CKMODE) {
  76. if (readl(base + CLPS711X_SYSCON2) & SYSCON2_OSTB)
  77. f_tim = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 26);
  78. else
  79. f_tim = DIV_ROUND_CLOSEST(CLPS711X_EXT_FREQ, 24);
  80. } else
  81. f_tim = DIV_ROUND_CLOSEST(f_cpu, 144);
  82. tmp = readl(base + CLPS711X_SYSCON1);
  83. /* Timer1 in free running mode.
  84. * Counter will wrap around to 0xffff when it underflows
  85. * and will continue to count down.
  86. */
  87. tmp &= ~(SYSCON1_TC1M | SYSCON1_TC1S);
  88. /* Timer2 in prescale mode.
  89. * Value writen is automatically re-loaded when
  90. * the counter underflows.
  91. */
  92. tmp |= SYSCON1_TC2M | SYSCON1_TC2S;
  93. writel(tmp, base + CLPS711X_SYSCON1);
  94. clps711x_clk->clk_data.hws[CLPS711X_CLK_DUMMY] =
  95. clk_hw_register_fixed_rate(NULL, "dummy", NULL, 0, 0);
  96. clps711x_clk->clk_data.hws[CLPS711X_CLK_CPU] =
  97. clk_hw_register_fixed_rate(NULL, "cpu", NULL, 0, f_cpu);
  98. clps711x_clk->clk_data.hws[CLPS711X_CLK_BUS] =
  99. clk_hw_register_fixed_rate(NULL, "bus", NULL, 0, f_bus);
  100. clps711x_clk->clk_data.hws[CLPS711X_CLK_PLL] =
  101. clk_hw_register_fixed_rate(NULL, "pll", NULL, 0, f_pll);
  102. clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMERREF] =
  103. clk_hw_register_fixed_rate(NULL, "timer_ref", NULL, 0, f_tim);
  104. clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER1] =
  105. clk_hw_register_divider_table(NULL, "timer1", "timer_ref", 0,
  106. base + CLPS711X_SYSCON1, 5, 1, 0,
  107. timer_div_table, &clps711x_clk->lock);
  108. clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER2] =
  109. clk_hw_register_divider_table(NULL, "timer2", "timer_ref", 0,
  110. base + CLPS711X_SYSCON1, 7, 1, 0,
  111. timer_div_table, &clps711x_clk->lock);
  112. clps711x_clk->clk_data.hws[CLPS711X_CLK_PWM] =
  113. clk_hw_register_fixed_rate(NULL, "pwm", NULL, 0, f_pwm);
  114. clps711x_clk->clk_data.hws[CLPS711X_CLK_SPIREF] =
  115. clk_hw_register_fixed_rate(NULL, "spi_ref", NULL, 0, f_spi);
  116. clps711x_clk->clk_data.hws[CLPS711X_CLK_SPI] =
  117. clk_hw_register_divider_table(NULL, "spi", "spi_ref", 0,
  118. base + CLPS711X_SYSCON1, 16, 2, 0,
  119. spi_div_table, &clps711x_clk->lock);
  120. clps711x_clk->clk_data.hws[CLPS711X_CLK_UART] =
  121. clk_hw_register_fixed_factor(NULL, "uart", "bus", 0, 1, 10);
  122. clps711x_clk->clk_data.hws[CLPS711X_CLK_TICK] =
  123. clk_hw_register_fixed_rate(NULL, "tick", NULL, 0, 64);
  124. for (i = 0; i < CLPS711X_CLK_MAX; i++)
  125. if (IS_ERR(clps711x_clk->clk_data.hws[i]))
  126. pr_err("clk %i: register failed with %ld\n",
  127. i, PTR_ERR(clps711x_clk->clk_data.hws[i]));
  128. return clps711x_clk;
  129. }
  130. void __init clps711x_clk_init(void __iomem *base)
  131. {
  132. struct clps711x_clk *clps711x_clk;
  133. clps711x_clk = _clps711x_clk_init(base, 73728000);
  134. BUG_ON(IS_ERR(clps711x_clk));
  135. /* Clocksource */
  136. clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER1],
  137. NULL, "clps711x-timer.0");
  138. clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_TIMER2],
  139. NULL, "clps711x-timer.1");
  140. /* Drivers */
  141. clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_PWM],
  142. NULL, "clps711x-pwm");
  143. clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_UART],
  144. NULL, "clps711x-uart.0");
  145. clk_hw_register_clkdev(clps711x_clk->clk_data.hws[CLPS711X_CLK_UART],
  146. NULL, "clps711x-uart.1");
  147. }
  148. #ifdef CONFIG_OF
  149. static void __init clps711x_clk_init_dt(struct device_node *np)
  150. {
  151. void __iomem *base = of_iomap(np, 0);
  152. struct clps711x_clk *clps711x_clk;
  153. u32 fref = 0;
  154. WARN_ON(of_property_read_u32(np, "startup-frequency", &fref));
  155. clps711x_clk = _clps711x_clk_init(base, fref);
  156. BUG_ON(IS_ERR(clps711x_clk));
  157. clps711x_clk->clk_data.num = CLPS711X_CLK_MAX;
  158. of_clk_add_hw_provider(np, of_clk_hw_onecell_get,
  159. &clps711x_clk->clk_data);
  160. }
  161. CLK_OF_DECLARE(clps711x, "cirrus,ep7209-clk", clps711x_clk_init_dt);
  162. #endif