clk-pxa25x.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * Marvell PXA25x family clocks
  3. *
  4. * Copyright (C) 2014 Robert Jarzmik
  5. *
  6. * Heavily inspired from former arch/arm/mach-pxa/pxa25x.c.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; version 2 of the License.
  11. *
  12. * For non-devicetree platforms. Once pxa is fully converted to devicetree, this
  13. * should go away.
  14. */
  15. #include <linux/clk-provider.h>
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/io.h>
  19. #include <linux/of.h>
  20. #include <mach/pxa2xx-regs.h>
  21. #include <dt-bindings/clock/pxa-clock.h>
  22. #include "clk-pxa.h"
  23. #define KHz 1000
  24. #define MHz (1000 * 1000)
  25. enum {
  26. PXA_CORE_RUN = 0,
  27. PXA_CORE_TURBO,
  28. };
  29. /*
  30. * Various clock factors driven by the CCCR register.
  31. */
  32. /* Crystal Frequency to Memory Frequency Multiplier (L) */
  33. static unsigned char L_clk_mult[32] = { 0, 27, 32, 36, 40, 45, 0, };
  34. /* Memory Frequency to Run Mode Frequency Multiplier (M) */
  35. static unsigned char M_clk_mult[4] = { 0, 1, 2, 4 };
  36. /* Run Mode Frequency to Turbo Mode Frequency Multiplier (N) */
  37. /* Note: we store the value N * 2 here. */
  38. static unsigned char N2_clk_mult[8] = { 0, 0, 2, 3, 4, 0, 6, 0 };
  39. static const char * const get_freq_khz[] = {
  40. "core", "run", "cpll", "memory"
  41. };
  42. /*
  43. * Get the clock frequency as reflected by CCCR and the turbo flag.
  44. * We assume these values have been applied via a fcs.
  45. * If info is not 0 we also display the current settings.
  46. */
  47. unsigned int pxa25x_get_clk_frequency_khz(int info)
  48. {
  49. struct clk *clk;
  50. unsigned long clks[5];
  51. int i;
  52. for (i = 0; i < ARRAY_SIZE(get_freq_khz); i++) {
  53. clk = clk_get(NULL, get_freq_khz[i]);
  54. if (IS_ERR(clk)) {
  55. clks[i] = 0;
  56. } else {
  57. clks[i] = clk_get_rate(clk);
  58. clk_put(clk);
  59. }
  60. }
  61. if (info) {
  62. pr_info("Run Mode clock: %ld.%02ldMHz\n",
  63. clks[1] / 1000000, (clks[1] % 1000000) / 10000);
  64. pr_info("Turbo Mode clock: %ld.%02ldMHz\n",
  65. clks[2] / 1000000, (clks[2] % 1000000) / 10000);
  66. pr_info("Memory clock: %ld.%02ldMHz\n",
  67. clks[3] / 1000000, (clks[3] % 1000000) / 10000);
  68. }
  69. return (unsigned int)clks[0] / KHz;
  70. }
  71. static unsigned long clk_pxa25x_memory_get_rate(struct clk_hw *hw,
  72. unsigned long parent_rate)
  73. {
  74. unsigned long cccr = readl(CCCR);
  75. unsigned int m = M_clk_mult[(cccr >> 5) & 0x03];
  76. return parent_rate / m;
  77. }
  78. PARENTS(clk_pxa25x_memory) = { "run" };
  79. RATE_RO_OPS(clk_pxa25x_memory, "memory");
  80. PARENTS(pxa25x_pbus95) = { "ppll_95_85mhz", "ppll_95_85mhz" };
  81. PARENTS(pxa25x_pbus147) = { "ppll_147_46mhz", "ppll_147_46mhz" };
  82. PARENTS(pxa25x_osc3) = { "osc_3_6864mhz", "osc_3_6864mhz" };
  83. #define PXA25X_CKEN(dev_id, con_id, parents, mult, div, \
  84. bit, is_lp, flags) \
  85. PXA_CKEN(dev_id, con_id, bit, parents, mult, div, mult, div, \
  86. is_lp, CKEN, CKEN_ ## bit, flags)
  87. #define PXA25X_PBUS95_CKEN(dev_id, con_id, bit, mult_hp, div_hp, delay) \
  88. PXA25X_CKEN(dev_id, con_id, pxa25x_pbus95_parents, mult_hp, \
  89. div_hp, bit, NULL, 0)
  90. #define PXA25X_PBUS147_CKEN(dev_id, con_id, bit, mult_hp, div_hp, delay)\
  91. PXA25X_CKEN(dev_id, con_id, pxa25x_pbus147_parents, mult_hp, \
  92. div_hp, bit, NULL, 0)
  93. #define PXA25X_OSC3_CKEN(dev_id, con_id, bit, mult_hp, div_hp, delay) \
  94. PXA25X_CKEN(dev_id, con_id, pxa25x_osc3_parents, mult_hp, \
  95. div_hp, bit, NULL, 0)
  96. #define PXA25X_CKEN_1RATE(dev_id, con_id, bit, parents, delay) \
  97. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  98. CKEN, CKEN_ ## bit, 0)
  99. #define PXA25X_CKEN_1RATE_AO(dev_id, con_id, bit, parents, delay) \
  100. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  101. CKEN, CKEN_ ## bit, CLK_IGNORE_UNUSED)
  102. static struct desc_clk_cken pxa25x_clocks[] __initdata = {
  103. PXA25X_PBUS95_CKEN("pxa2xx-mci.0", NULL, MMC, 1, 5, 0),
  104. PXA25X_PBUS95_CKEN("pxa2xx-i2c.0", NULL, I2C, 1, 3, 0),
  105. PXA25X_PBUS95_CKEN("pxa2xx-ir", "FICPCLK", FICP, 1, 2, 0),
  106. PXA25X_PBUS95_CKEN("pxa25x-udc", NULL, USB, 1, 2, 5),
  107. PXA25X_PBUS147_CKEN("pxa2xx-uart.0", NULL, FFUART, 1, 10, 1),
  108. PXA25X_PBUS147_CKEN("pxa2xx-uart.1", NULL, BTUART, 1, 10, 1),
  109. PXA25X_PBUS147_CKEN("pxa2xx-uart.2", NULL, STUART, 1, 10, 1),
  110. PXA25X_PBUS147_CKEN("pxa2xx-uart.3", NULL, HWUART, 1, 10, 1),
  111. PXA25X_PBUS147_CKEN("pxa2xx-i2s", NULL, I2S, 1, 10, 0),
  112. PXA25X_PBUS147_CKEN(NULL, "AC97CLK", AC97, 1, 12, 0),
  113. PXA25X_OSC3_CKEN("pxa25x-ssp.0", NULL, SSP, 1, 1, 0),
  114. PXA25X_OSC3_CKEN("pxa25x-nssp.1", NULL, NSSP, 1, 1, 0),
  115. PXA25X_OSC3_CKEN("pxa25x-nssp.2", NULL, ASSP, 1, 1, 0),
  116. PXA25X_OSC3_CKEN("pxa25x-pwm.0", NULL, PWM0, 1, 1, 0),
  117. PXA25X_OSC3_CKEN("pxa25x-pwm.1", NULL, PWM1, 1, 1, 0),
  118. PXA25X_CKEN_1RATE("pxa2xx-fb", NULL, LCD, clk_pxa25x_memory_parents, 0),
  119. PXA25X_CKEN_1RATE_AO("pxa2xx-pcmcia", NULL, MEMC,
  120. clk_pxa25x_memory_parents, 0),
  121. };
  122. static u8 clk_pxa25x_core_get_parent(struct clk_hw *hw)
  123. {
  124. unsigned long clkcfg;
  125. unsigned int t;
  126. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  127. t = clkcfg & (1 << 0);
  128. if (t)
  129. return PXA_CORE_TURBO;
  130. return PXA_CORE_RUN;
  131. }
  132. static unsigned long clk_pxa25x_core_get_rate(struct clk_hw *hw,
  133. unsigned long parent_rate)
  134. {
  135. return parent_rate;
  136. }
  137. PARENTS(clk_pxa25x_core) = { "run", "cpll" };
  138. MUX_RO_RATE_RO_OPS(clk_pxa25x_core, "core");
  139. static unsigned long clk_pxa25x_run_get_rate(struct clk_hw *hw,
  140. unsigned long parent_rate)
  141. {
  142. unsigned long cccr = readl(CCCR);
  143. unsigned int n2 = N2_clk_mult[(cccr >> 7) & 0x07];
  144. return (parent_rate / n2) * 2;
  145. }
  146. PARENTS(clk_pxa25x_run) = { "cpll" };
  147. RATE_RO_OPS(clk_pxa25x_run, "run");
  148. static unsigned long clk_pxa25x_cpll_get_rate(struct clk_hw *hw,
  149. unsigned long parent_rate)
  150. {
  151. unsigned long clkcfg, cccr = readl(CCCR);
  152. unsigned int l, m, n2, t;
  153. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  154. t = clkcfg & (1 << 0);
  155. l = L_clk_mult[(cccr >> 0) & 0x1f];
  156. m = M_clk_mult[(cccr >> 5) & 0x03];
  157. n2 = N2_clk_mult[(cccr >> 7) & 0x07];
  158. if (t)
  159. return m * l * n2 * parent_rate / 2;
  160. return m * l * parent_rate;
  161. }
  162. PARENTS(clk_pxa25x_cpll) = { "osc_3_6864mhz" };
  163. RATE_RO_OPS(clk_pxa25x_cpll, "cpll");
  164. static void __init pxa25x_register_core(void)
  165. {
  166. clk_register_clk_pxa25x_cpll();
  167. clk_register_clk_pxa25x_run();
  168. clkdev_pxa_register(CLK_CORE, "core", NULL,
  169. clk_register_clk_pxa25x_core());
  170. }
  171. static void __init pxa25x_register_plls(void)
  172. {
  173. clk_register_fixed_rate(NULL, "osc_3_6864mhz", NULL,
  174. CLK_GET_RATE_NOCACHE, 3686400);
  175. clk_register_fixed_rate(NULL, "osc_32_768khz", NULL,
  176. CLK_GET_RATE_NOCACHE, 32768);
  177. clk_register_fixed_rate(NULL, "clk_dummy", NULL, 0, 0);
  178. clk_register_fixed_factor(NULL, "ppll_95_85mhz", "osc_3_6864mhz",
  179. 0, 26, 1);
  180. clk_register_fixed_factor(NULL, "ppll_147_46mhz", "osc_3_6864mhz",
  181. 0, 40, 1);
  182. }
  183. static void __init pxa25x_base_clocks_init(void)
  184. {
  185. pxa25x_register_plls();
  186. pxa25x_register_core();
  187. clk_register_clk_pxa25x_memory();
  188. }
  189. #define DUMMY_CLK(_con_id, _dev_id, _parent) \
  190. { .con_id = _con_id, .dev_id = _dev_id, .parent = _parent }
  191. struct dummy_clk {
  192. const char *con_id;
  193. const char *dev_id;
  194. const char *parent;
  195. };
  196. static struct dummy_clk dummy_clks[] __initdata = {
  197. DUMMY_CLK(NULL, "pxa25x-gpio", "osc_32_768khz"),
  198. DUMMY_CLK(NULL, "pxa26x-gpio", "osc_32_768khz"),
  199. DUMMY_CLK("GPIO11_CLK", NULL, "osc_3_6864mhz"),
  200. DUMMY_CLK("GPIO12_CLK", NULL, "osc_32_768khz"),
  201. DUMMY_CLK(NULL, "sa1100-rtc", "osc_32_768khz"),
  202. DUMMY_CLK("OSTIMER0", NULL, "osc_32_768khz"),
  203. DUMMY_CLK("UARTCLK", "pxa2xx-ir", "STUART"),
  204. };
  205. static void __init pxa25x_dummy_clocks_init(void)
  206. {
  207. struct clk *clk;
  208. struct dummy_clk *d;
  209. const char *name;
  210. int i;
  211. /*
  212. * All pinctrl logic has been wiped out of the clock driver, especially
  213. * for gpio11 and gpio12 outputs. Machine code should ensure proper pin
  214. * control (ie. pxa2xx_mfp_config() invocation).
  215. */
  216. for (i = 0; i < ARRAY_SIZE(dummy_clks); i++) {
  217. d = &dummy_clks[i];
  218. name = d->dev_id ? d->dev_id : d->con_id;
  219. clk = clk_register_fixed_factor(NULL, name, d->parent, 0, 1, 1);
  220. clk_register_clkdev(clk, d->con_id, d->dev_id);
  221. }
  222. }
  223. int __init pxa25x_clocks_init(void)
  224. {
  225. pxa25x_base_clocks_init();
  226. pxa25x_dummy_clocks_init();
  227. return clk_pxa_cken_init(pxa25x_clocks, ARRAY_SIZE(pxa25x_clocks));
  228. }
  229. static void __init pxa25x_dt_clocks_init(struct device_node *np)
  230. {
  231. pxa25x_clocks_init();
  232. clk_pxa_dt_common_init(np);
  233. }
  234. CLK_OF_DECLARE(pxa25x_clks, "marvell,pxa250-core-clocks",
  235. pxa25x_dt_clocks_init);