clk-pxa25x.c 8.2 KB

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