clk-pxa3xx.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Marvell PXA3xxx family clocks
  3. *
  4. * Copyright (C) 2014 Robert Jarzmik
  5. *
  6. * Heavily inspired from former arch/arm/mach-pxa/pxa3xx.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/io.h>
  16. #include <linux/clk.h>
  17. #include <linux/clk-provider.h>
  18. #include <linux/clkdev.h>
  19. #include <linux/of.h>
  20. #include <mach/smemc.h>
  21. #include <mach/pxa3xx-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_60Mhz = 0,
  28. PXA_CORE_RUN,
  29. PXA_CORE_TURBO,
  30. };
  31. enum {
  32. PXA_BUS_60Mhz = 0,
  33. PXA_BUS_HSS,
  34. };
  35. /* crystal frequency to HSIO bus frequency multiplier (HSS) */
  36. static unsigned char hss_mult[4] = { 8, 12, 16, 24 };
  37. /* crystal frequency to static memory controller multiplier (SMCFS) */
  38. static unsigned int smcfs_mult[8] = { 6, 0, 8, 0, 0, 16, };
  39. static unsigned int df_clkdiv[4] = { 1, 2, 4, 1 };
  40. static const char * const get_freq_khz[] = {
  41. "core", "ring_osc_60mhz", "run", "cpll", "system_bus"
  42. };
  43. /*
  44. * Get the clock frequency as reflected by ACSR 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 pxa3xx_get_clk_frequency_khz(int info)
  49. {
  50. struct clk *clk;
  51. unsigned long clks[5];
  52. int i;
  53. for (i = 0; i < 5; 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("RO Mode clock: %ld.%02ldMHz\n",
  64. clks[1] / 1000000, (clks[0] % 1000000) / 10000);
  65. pr_info("Run Mode clock: %ld.%02ldMHz\n",
  66. clks[2] / 1000000, (clks[1] % 1000000) / 10000);
  67. pr_info("Turbo Mode clock: %ld.%02ldMHz\n",
  68. clks[3] / 1000000, (clks[2] % 1000000) / 10000);
  69. pr_info("System bus clock: %ld.%02ldMHz\n",
  70. clks[4] / 1000000, (clks[4] % 1000000) / 10000);
  71. }
  72. return (unsigned int)clks[0] / KHz;
  73. }
  74. static unsigned long clk_pxa3xx_ac97_get_rate(struct clk_hw *hw,
  75. unsigned long parent_rate)
  76. {
  77. unsigned long ac97_div, rate;
  78. ac97_div = AC97_DIV;
  79. /* This may loose precision for some rates but won't for the
  80. * standard 24.576MHz.
  81. */
  82. rate = parent_rate / 2;
  83. rate /= ((ac97_div >> 12) & 0x7fff);
  84. rate *= (ac97_div & 0xfff);
  85. return rate;
  86. }
  87. PARENTS(clk_pxa3xx_ac97) = { "spll_624mhz" };
  88. RATE_RO_OPS(clk_pxa3xx_ac97, "ac97");
  89. static unsigned long clk_pxa3xx_smemc_get_rate(struct clk_hw *hw,
  90. unsigned long parent_rate)
  91. {
  92. unsigned long acsr = ACSR;
  93. unsigned long memclkcfg = __raw_readl(MEMCLKCFG);
  94. return (parent_rate / 48) * smcfs_mult[(acsr >> 23) & 0x7] /
  95. df_clkdiv[(memclkcfg >> 16) & 0x3];
  96. }
  97. PARENTS(clk_pxa3xx_smemc) = { "spll_624mhz" };
  98. RATE_RO_OPS(clk_pxa3xx_smemc, "smemc");
  99. static bool pxa3xx_is_ring_osc_forced(void)
  100. {
  101. unsigned long acsr = ACSR;
  102. return acsr & ACCR_D0CS;
  103. }
  104. PARENTS(pxa3xx_pbus) = { "ring_osc_60mhz", "spll_624mhz" };
  105. PARENTS(pxa3xx_32Khz_bus) = { "osc_32_768khz", "osc_32_768khz" };
  106. PARENTS(pxa3xx_13MHz_bus) = { "osc_13mhz", "osc_13mhz" };
  107. PARENTS(pxa3xx_ac97_bus) = { "ring_osc_60mhz", "ac97" };
  108. PARENTS(pxa3xx_sbus) = { "ring_osc_60mhz", "system_bus" };
  109. PARENTS(pxa3xx_smemcbus) = { "ring_osc_60mhz", "smemc" };
  110. #define CKEN_AB(bit) ((CKEN_ ## bit > 31) ? &CKENB : &CKENA)
  111. #define PXA3XX_CKEN(dev_id, con_id, parents, mult_lp, div_lp, mult_hp, \
  112. div_hp, bit, is_lp, flags) \
  113. PXA_CKEN(dev_id, con_id, bit, parents, mult_lp, div_lp, \
  114. mult_hp, div_hp, is_lp, CKEN_AB(bit), \
  115. (CKEN_ ## bit % 32), flags)
  116. #define PXA3XX_PBUS_CKEN(dev_id, con_id, bit, mult_lp, div_lp, \
  117. mult_hp, div_hp, delay) \
  118. PXA3XX_CKEN(dev_id, con_id, pxa3xx_pbus_parents, mult_lp, \
  119. div_lp, mult_hp, div_hp, bit, pxa3xx_is_ring_osc_forced, 0)
  120. #define PXA3XX_CKEN_1RATE(dev_id, con_id, bit, parents) \
  121. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  122. CKEN_AB(bit), (CKEN_ ## bit % 32), 0)
  123. static struct desc_clk_cken pxa3xx_clocks[] __initdata = {
  124. PXA3XX_PBUS_CKEN("pxa2xx-uart.0", NULL, FFUART, 1, 4, 1, 42, 1),
  125. PXA3XX_PBUS_CKEN("pxa2xx-uart.1", NULL, BTUART, 1, 4, 1, 42, 1),
  126. PXA3XX_PBUS_CKEN("pxa2xx-uart.2", NULL, STUART, 1, 4, 1, 42, 1),
  127. PXA3XX_PBUS_CKEN("pxa2xx-i2c.0", NULL, I2C, 2, 5, 1, 19, 0),
  128. PXA3XX_PBUS_CKEN("pxa27x-udc", NULL, UDC, 1, 4, 1, 13, 5),
  129. PXA3XX_PBUS_CKEN("pxa27x-ohci", NULL, USBH, 1, 4, 1, 13, 0),
  130. PXA3XX_PBUS_CKEN("pxa3xx-u2d", NULL, USB2, 1, 4, 1, 13, 0),
  131. PXA3XX_PBUS_CKEN("pxa27x-pwm.0", NULL, PWM0, 1, 6, 1, 48, 0),
  132. PXA3XX_PBUS_CKEN("pxa27x-pwm.1", NULL, PWM1, 1, 6, 1, 48, 0),
  133. PXA3XX_PBUS_CKEN("pxa2xx-mci.0", NULL, MMC1, 1, 4, 1, 24, 0),
  134. PXA3XX_PBUS_CKEN("pxa2xx-mci.1", NULL, MMC2, 1, 4, 1, 24, 0),
  135. PXA3XX_PBUS_CKEN("pxa2xx-mci.2", NULL, MMC3, 1, 4, 1, 24, 0),
  136. PXA3XX_CKEN_1RATE("pxa27x-keypad", NULL, KEYPAD,
  137. pxa3xx_32Khz_bus_parents),
  138. PXA3XX_CKEN_1RATE("pxa3xx-ssp.0", NULL, SSP1, pxa3xx_13MHz_bus_parents),
  139. PXA3XX_CKEN_1RATE("pxa3xx-ssp.1", NULL, SSP2, pxa3xx_13MHz_bus_parents),
  140. PXA3XX_CKEN_1RATE("pxa3xx-ssp.2", NULL, SSP3, pxa3xx_13MHz_bus_parents),
  141. PXA3XX_CKEN_1RATE("pxa3xx-ssp.3", NULL, SSP4, pxa3xx_13MHz_bus_parents),
  142. PXA3XX_CKEN(NULL, "AC97CLK", pxa3xx_ac97_bus_parents, 1, 4, 1, 1, AC97,
  143. pxa3xx_is_ring_osc_forced, 0),
  144. PXA3XX_CKEN(NULL, "CAMCLK", pxa3xx_sbus_parents, 1, 2, 1, 1, CAMERA,
  145. pxa3xx_is_ring_osc_forced, 0),
  146. PXA3XX_CKEN("pxa2xx-fb", NULL, pxa3xx_sbus_parents, 1, 1, 1, 1, LCD,
  147. pxa3xx_is_ring_osc_forced, 0),
  148. PXA3XX_CKEN("pxa2xx-pcmcia", NULL, pxa3xx_smemcbus_parents, 1, 4,
  149. 1, 1, SMC, pxa3xx_is_ring_osc_forced, CLK_IGNORE_UNUSED),
  150. };
  151. static struct desc_clk_cken pxa300_310_clocks[] __initdata = {
  152. PXA3XX_PBUS_CKEN("pxa3xx-gcu", NULL, PXA300_GCU, 1, 1, 1, 1, 0),
  153. PXA3XX_PBUS_CKEN("pxa3xx-nand", NULL, NAND, 1, 2, 1, 4, 0),
  154. PXA3XX_CKEN_1RATE("pxa3xx-gpio", NULL, GPIO, pxa3xx_13MHz_bus_parents),
  155. };
  156. static struct desc_clk_cken pxa320_clocks[] __initdata = {
  157. PXA3XX_PBUS_CKEN("pxa3xx-nand", NULL, NAND, 1, 2, 1, 6, 0),
  158. PXA3XX_PBUS_CKEN("pxa3xx-gcu", NULL, PXA320_GCU, 1, 1, 1, 1, 0),
  159. PXA3XX_CKEN_1RATE("pxa3xx-gpio", NULL, GPIO, pxa3xx_13MHz_bus_parents),
  160. };
  161. static struct desc_clk_cken pxa93x_clocks[] __initdata = {
  162. PXA3XX_PBUS_CKEN("pxa3xx-gcu", NULL, PXA300_GCU, 1, 1, 1, 1, 0),
  163. PXA3XX_PBUS_CKEN("pxa3xx-nand", NULL, NAND, 1, 2, 1, 4, 0),
  164. PXA3XX_CKEN_1RATE("pxa93x-gpio", NULL, GPIO, pxa3xx_13MHz_bus_parents),
  165. };
  166. static unsigned long clk_pxa3xx_system_bus_get_rate(struct clk_hw *hw,
  167. unsigned long parent_rate)
  168. {
  169. unsigned long acsr = ACSR;
  170. unsigned int hss = (acsr >> 14) & 0x3;
  171. if (pxa3xx_is_ring_osc_forced())
  172. return parent_rate;
  173. return parent_rate / 48 * hss_mult[hss];
  174. }
  175. static u8 clk_pxa3xx_system_bus_get_parent(struct clk_hw *hw)
  176. {
  177. if (pxa3xx_is_ring_osc_forced())
  178. return PXA_BUS_60Mhz;
  179. else
  180. return PXA_BUS_HSS;
  181. }
  182. PARENTS(clk_pxa3xx_system_bus) = { "ring_osc_60mhz", "spll_624mhz" };
  183. MUX_RO_RATE_RO_OPS(clk_pxa3xx_system_bus, "system_bus");
  184. static unsigned long clk_pxa3xx_core_get_rate(struct clk_hw *hw,
  185. unsigned long parent_rate)
  186. {
  187. return parent_rate;
  188. }
  189. static u8 clk_pxa3xx_core_get_parent(struct clk_hw *hw)
  190. {
  191. unsigned long xclkcfg;
  192. unsigned int t;
  193. if (pxa3xx_is_ring_osc_forced())
  194. return PXA_CORE_60Mhz;
  195. /* Read XCLKCFG register turbo bit */
  196. __asm__ __volatile__("mrc\tp14, 0, %0, c6, c0, 0" : "=r"(xclkcfg));
  197. t = xclkcfg & 0x1;
  198. if (t)
  199. return PXA_CORE_TURBO;
  200. return PXA_CORE_RUN;
  201. }
  202. PARENTS(clk_pxa3xx_core) = { "ring_osc_60mhz", "run", "cpll" };
  203. MUX_RO_RATE_RO_OPS(clk_pxa3xx_core, "core");
  204. static unsigned long clk_pxa3xx_run_get_rate(struct clk_hw *hw,
  205. unsigned long parent_rate)
  206. {
  207. unsigned long acsr = ACSR;
  208. unsigned int xn = (acsr & ACCR_XN_MASK) >> 8;
  209. unsigned int t, xclkcfg;
  210. /* Read XCLKCFG register turbo bit */
  211. __asm__ __volatile__("mrc\tp14, 0, %0, c6, c0, 0" : "=r"(xclkcfg));
  212. t = xclkcfg & 0x1;
  213. return t ? (parent_rate / xn) * 2 : parent_rate;
  214. }
  215. PARENTS(clk_pxa3xx_run) = { "cpll" };
  216. RATE_RO_OPS(clk_pxa3xx_run, "run");
  217. static unsigned long clk_pxa3xx_cpll_get_rate(struct clk_hw *hw,
  218. unsigned long parent_rate)
  219. {
  220. unsigned long acsr = ACSR;
  221. unsigned int xn = (acsr & ACCR_XN_MASK) >> 8;
  222. unsigned int xl = acsr & ACCR_XL_MASK;
  223. unsigned int t, xclkcfg;
  224. /* Read XCLKCFG register turbo bit */
  225. __asm__ __volatile__("mrc\tp14, 0, %0, c6, c0, 0" : "=r"(xclkcfg));
  226. t = xclkcfg & 0x1;
  227. pr_info("RJK: parent_rate=%lu, xl=%u, xn=%u\n", parent_rate, xl, xn);
  228. return t ? parent_rate * xl * xn : parent_rate * xl;
  229. }
  230. PARENTS(clk_pxa3xx_cpll) = { "osc_13mhz" };
  231. RATE_RO_OPS(clk_pxa3xx_cpll, "cpll");
  232. static void __init pxa3xx_register_core(void)
  233. {
  234. clk_register_clk_pxa3xx_cpll();
  235. clk_register_clk_pxa3xx_run();
  236. clkdev_pxa_register(CLK_CORE, "core", NULL,
  237. clk_register_clk_pxa3xx_core());
  238. }
  239. static void __init pxa3xx_register_plls(void)
  240. {
  241. clk_register_fixed_rate(NULL, "osc_13mhz", NULL,
  242. CLK_GET_RATE_NOCACHE | CLK_IS_ROOT,
  243. 13 * MHz);
  244. clk_register_fixed_rate(NULL, "osc_32_768khz", NULL,
  245. CLK_GET_RATE_NOCACHE | CLK_IS_ROOT,
  246. 32768);
  247. clk_register_fixed_rate(NULL, "ring_osc_120mhz", NULL,
  248. CLK_GET_RATE_NOCACHE | CLK_IS_ROOT,
  249. 120 * MHz);
  250. clk_register_fixed_rate(NULL, "clk_dummy", NULL, CLK_IS_ROOT, 0);
  251. clk_register_fixed_factor(NULL, "spll_624mhz", "osc_13mhz", 0, 48, 1);
  252. clk_register_fixed_factor(NULL, "ring_osc_60mhz", "ring_osc_120mhz",
  253. 0, 1, 2);
  254. }
  255. #define DUMMY_CLK(_con_id, _dev_id, _parent) \
  256. { .con_id = _con_id, .dev_id = _dev_id, .parent = _parent }
  257. struct dummy_clk {
  258. const char *con_id;
  259. const char *dev_id;
  260. const char *parent;
  261. };
  262. static struct dummy_clk dummy_clks[] __initdata = {
  263. DUMMY_CLK(NULL, "pxa93x-gpio", "osc_13mhz"),
  264. DUMMY_CLK(NULL, "sa1100-rtc", "osc_32_768khz"),
  265. DUMMY_CLK("UARTCLK", "pxa2xx-ir", "STUART"),
  266. DUMMY_CLK(NULL, "pxa3xx-pwri2c.1", "osc_13mhz"),
  267. };
  268. static void __init pxa3xx_dummy_clocks_init(void)
  269. {
  270. struct clk *clk;
  271. struct dummy_clk *d;
  272. const char *name;
  273. int i;
  274. for (i = 0; i < ARRAY_SIZE(dummy_clks); i++) {
  275. d = &dummy_clks[i];
  276. name = d->dev_id ? d->dev_id : d->con_id;
  277. clk = clk_register_fixed_factor(NULL, name, d->parent, 0, 1, 1);
  278. clk_register_clkdev(clk, d->con_id, d->dev_id);
  279. }
  280. }
  281. static void __init pxa3xx_base_clocks_init(void)
  282. {
  283. pxa3xx_register_plls();
  284. pxa3xx_register_core();
  285. clk_register_clk_pxa3xx_system_bus();
  286. clk_register_clk_pxa3xx_ac97();
  287. clk_register_clk_pxa3xx_smemc();
  288. clk_register_gate(NULL, "CLK_POUT", "osc_13mhz", 0,
  289. (void __iomem *)&OSCC, 11, 0, NULL);
  290. clkdev_pxa_register(CLK_OSTIMER, "OSTIMER0", NULL,
  291. clk_register_fixed_factor(NULL, "os-timer0",
  292. "osc_13mhz", 0, 1, 4));
  293. }
  294. int __init pxa3xx_clocks_init(void)
  295. {
  296. int ret;
  297. pxa3xx_base_clocks_init();
  298. pxa3xx_dummy_clocks_init();
  299. ret = clk_pxa_cken_init(pxa3xx_clocks, ARRAY_SIZE(pxa3xx_clocks));
  300. if (ret)
  301. return ret;
  302. if (cpu_is_pxa320())
  303. return clk_pxa_cken_init(pxa320_clocks,
  304. ARRAY_SIZE(pxa320_clocks));
  305. if (cpu_is_pxa300() || cpu_is_pxa310())
  306. return clk_pxa_cken_init(pxa300_310_clocks,
  307. ARRAY_SIZE(pxa300_310_clocks));
  308. return clk_pxa_cken_init(pxa93x_clocks, ARRAY_SIZE(pxa93x_clocks));
  309. }
  310. static void __init pxa3xx_dt_clocks_init(struct device_node *np)
  311. {
  312. pxa3xx_clocks_init();
  313. clk_pxa_dt_common_init(np);
  314. }
  315. CLK_OF_DECLARE(pxa_clks, "marvell,pxa300-clocks", pxa3xx_dt_clocks_init);