clk-pxa27x.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /*
  2. * Marvell PXA27x family clocks
  3. *
  4. * Copyright (C) 2014 Robert Jarzmik
  5. *
  6. * Heavily inspired from former arch/arm/mach-pxa/clock.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. */
  13. #include <linux/clk-provider.h>
  14. #include <mach/pxa2xx-regs.h>
  15. #include <linux/io.h>
  16. #include <linux/clk.h>
  17. #include <linux/clkdev.h>
  18. #include <linux/of.h>
  19. #include <dt-bindings/clock/pxa-clock.h>
  20. #include "clk-pxa.h"
  21. #define KHz 1000
  22. #define MHz (1000 * 1000)
  23. enum {
  24. PXA_CORE_13Mhz = 0,
  25. PXA_CORE_RUN,
  26. PXA_CORE_TURBO,
  27. };
  28. enum {
  29. PXA_BUS_13Mhz = 0,
  30. PXA_BUS_RUN,
  31. };
  32. enum {
  33. PXA_LCD_13Mhz = 0,
  34. PXA_LCD_RUN,
  35. };
  36. enum {
  37. PXA_MEM_13Mhz = 0,
  38. PXA_MEM_SYSTEM_BUS,
  39. PXA_MEM_RUN,
  40. };
  41. static const char * const get_freq_khz[] = {
  42. "core", "run", "cpll", "memory",
  43. "system_bus"
  44. };
  45. /*
  46. * Get the clock frequency as reflected by CCSR and the turbo flag.
  47. * We assume these values have been applied via a fcs.
  48. * If info is not 0 we also display the current settings.
  49. */
  50. unsigned int pxa27x_get_clk_frequency_khz(int info)
  51. {
  52. struct clk *clk;
  53. unsigned long clks[5];
  54. int i;
  55. for (i = 0; i < 5; i++) {
  56. clk = clk_get(NULL, get_freq_khz[i]);
  57. if (IS_ERR(clk)) {
  58. clks[i] = 0;
  59. } else {
  60. clks[i] = clk_get_rate(clk);
  61. clk_put(clk);
  62. }
  63. }
  64. if (info) {
  65. pr_info("Run Mode clock: %ld.%02ldMHz\n",
  66. clks[1] / 1000000, (clks[1] % 1000000) / 10000);
  67. pr_info("Turbo Mode clock: %ld.%02ldMHz\n",
  68. clks[2] / 1000000, (clks[2] % 1000000) / 10000);
  69. pr_info("Memory clock: %ld.%02ldMHz\n",
  70. clks[3] / 1000000, (clks[3] % 1000000) / 10000);
  71. pr_info("System bus clock: %ld.%02ldMHz\n",
  72. clks[4] / 1000000, (clks[4] % 1000000) / 10000);
  73. }
  74. return (unsigned int)clks[0] / KHz;
  75. }
  76. bool pxa27x_is_ppll_disabled(void)
  77. {
  78. unsigned long ccsr = CCSR;
  79. return ccsr & (1 << CCCR_PPDIS_BIT);
  80. }
  81. #define PXA27X_CKEN(dev_id, con_id, parents, mult_hp, div_hp, \
  82. bit, is_lp, flags) \
  83. PXA_CKEN(dev_id, con_id, bit, parents, 1, 1, mult_hp, div_hp, \
  84. is_lp, &CKEN, CKEN_ ## bit, flags)
  85. #define PXA27X_PBUS_CKEN(dev_id, con_id, bit, mult_hp, div_hp, delay) \
  86. PXA27X_CKEN(dev_id, con_id, pxa27x_pbus_parents, mult_hp, \
  87. div_hp, bit, pxa27x_is_ppll_disabled, 0)
  88. PARENTS(pxa27x_pbus) = { "osc_13mhz", "ppll_312mhz" };
  89. PARENTS(pxa27x_sbus) = { "system_bus", "system_bus" };
  90. PARENTS(pxa27x_32Mhz_bus) = { "osc_32_768khz", "osc_32_768khz" };
  91. PARENTS(pxa27x_lcd_bus) = { "lcd_base", "lcd_base" };
  92. PARENTS(pxa27x_membus) = { "lcd_base", "lcd_base" };
  93. #define PXA27X_CKEN_1RATE(dev_id, con_id, bit, parents, delay) \
  94. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  95. &CKEN, CKEN_ ## bit, 0)
  96. #define PXA27X_CKEN_1RATE_AO(dev_id, con_id, bit, parents, delay) \
  97. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  98. &CKEN, CKEN_ ## bit, CLK_IGNORE_UNUSED)
  99. static struct desc_clk_cken pxa27x_clocks[] __initdata = {
  100. PXA27X_PBUS_CKEN("pxa2xx-uart.0", NULL, FFUART, 2, 42, 1),
  101. PXA27X_PBUS_CKEN("pxa2xx-uart.1", NULL, BTUART, 2, 42, 1),
  102. PXA27X_PBUS_CKEN("pxa2xx-uart.2", NULL, STUART, 2, 42, 1),
  103. PXA27X_PBUS_CKEN("pxa2xx-i2s", NULL, I2S, 2, 51, 0),
  104. PXA27X_PBUS_CKEN("pxa2xx-i2c.0", NULL, I2C, 2, 19, 0),
  105. PXA27X_PBUS_CKEN("pxa27x-udc", NULL, USB, 2, 13, 5),
  106. PXA27X_PBUS_CKEN("pxa2xx-mci.0", NULL, MMC, 2, 32, 0),
  107. PXA27X_PBUS_CKEN("pxa2xx-ir", "FICPCLK", FICP, 2, 13, 0),
  108. PXA27X_PBUS_CKEN("pxa27x-ohci", NULL, USBHOST, 2, 13, 0),
  109. PXA27X_PBUS_CKEN("pxa2xx-i2c.1", NULL, PWRI2C, 1, 24, 0),
  110. PXA27X_PBUS_CKEN("pxa27x-ssp.0", NULL, SSP1, 1, 24, 0),
  111. PXA27X_PBUS_CKEN("pxa27x-ssp.1", NULL, SSP2, 1, 24, 0),
  112. PXA27X_PBUS_CKEN("pxa27x-ssp.2", NULL, SSP3, 1, 24, 0),
  113. PXA27X_PBUS_CKEN("pxa27x-pwm.0", NULL, PWM0, 1, 24, 0),
  114. PXA27X_PBUS_CKEN("pxa27x-pwm.1", NULL, PWM1, 1, 24, 0),
  115. PXA27X_PBUS_CKEN(NULL, "MSLCLK", MSL, 2, 13, 0),
  116. PXA27X_PBUS_CKEN(NULL, "USIMCLK", USIM, 2, 13, 0),
  117. PXA27X_PBUS_CKEN(NULL, "MSTKCLK", MEMSTK, 2, 32, 0),
  118. PXA27X_PBUS_CKEN(NULL, "AC97CLK", AC97, 1, 1, 0),
  119. PXA27X_PBUS_CKEN(NULL, "AC97CONFCLK", AC97CONF, 1, 1, 0),
  120. PXA27X_PBUS_CKEN(NULL, "OSTIMER0", OSTIMER, 1, 96, 0),
  121. PXA27X_CKEN_1RATE("pxa27x-keypad", NULL, KEYPAD,
  122. pxa27x_32Mhz_bus_parents, 0),
  123. PXA27X_CKEN_1RATE(NULL, "IMCLK", IM, pxa27x_sbus_parents, 0),
  124. PXA27X_CKEN_1RATE("pxa2xx-fb", NULL, LCD, pxa27x_lcd_bus_parents, 0),
  125. PXA27X_CKEN_1RATE("pxa27x-camera.0", NULL, CAMERA,
  126. pxa27x_lcd_bus_parents, 0),
  127. PXA27X_CKEN_1RATE_AO("pxa2xx-pcmcia", NULL, MEMC,
  128. pxa27x_membus_parents, 0),
  129. };
  130. static unsigned long clk_pxa27x_cpll_get_rate(struct clk_hw *hw,
  131. unsigned long parent_rate)
  132. {
  133. unsigned long clkcfg;
  134. unsigned int t, ht;
  135. unsigned int l, L, n2, N;
  136. unsigned long ccsr = CCSR;
  137. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  138. t = clkcfg & (1 << 0);
  139. ht = clkcfg & (1 << 2);
  140. l = ccsr & CCSR_L_MASK;
  141. n2 = (ccsr & CCSR_N2_MASK) >> CCSR_N2_SHIFT;
  142. L = l * parent_rate;
  143. N = (L * n2) / 2;
  144. return t ? N : L;
  145. }
  146. PARENTS(clk_pxa27x_cpll) = { "osc_13mhz" };
  147. RATE_RO_OPS(clk_pxa27x_cpll, "cpll");
  148. static unsigned long clk_pxa27x_lcd_base_get_rate(struct clk_hw *hw,
  149. unsigned long parent_rate)
  150. {
  151. unsigned int l, osc_forced;
  152. unsigned long ccsr = CCSR;
  153. unsigned long cccr = CCCR;
  154. l = ccsr & CCSR_L_MASK;
  155. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  156. if (osc_forced) {
  157. if (cccr & (1 << CCCR_LCD_26_BIT))
  158. return parent_rate * 2;
  159. else
  160. return parent_rate;
  161. }
  162. if (l <= 7)
  163. return parent_rate;
  164. if (l <= 16)
  165. return parent_rate / 2;
  166. return parent_rate / 4;
  167. }
  168. static u8 clk_pxa27x_lcd_base_get_parent(struct clk_hw *hw)
  169. {
  170. unsigned int osc_forced;
  171. unsigned long ccsr = CCSR;
  172. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  173. if (osc_forced)
  174. return PXA_LCD_13Mhz;
  175. else
  176. return PXA_LCD_RUN;
  177. }
  178. PARENTS(clk_pxa27x_lcd_base) = { "osc_13mhz", "run" };
  179. MUX_RO_RATE_RO_OPS(clk_pxa27x_lcd_base, "lcd_base");
  180. static void __init pxa27x_register_plls(void)
  181. {
  182. clk_register_fixed_rate(NULL, "osc_13mhz", NULL,
  183. CLK_GET_RATE_NOCACHE | CLK_IS_ROOT,
  184. 13 * MHz);
  185. clk_register_fixed_rate(NULL, "osc_32_768khz", NULL,
  186. CLK_GET_RATE_NOCACHE | CLK_IS_ROOT,
  187. 32768 * KHz);
  188. clk_register_fixed_rate(NULL, "clk_dummy", NULL, CLK_IS_ROOT, 0);
  189. clk_register_fixed_factor(NULL, "ppll_312mhz", "osc_13mhz", 0, 24, 1);
  190. }
  191. static unsigned long clk_pxa27x_core_get_rate(struct clk_hw *hw,
  192. unsigned long parent_rate)
  193. {
  194. unsigned long clkcfg;
  195. unsigned int t, ht, b, osc_forced;
  196. unsigned long ccsr = CCSR;
  197. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  198. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  199. t = clkcfg & (1 << 0);
  200. ht = clkcfg & (1 << 2);
  201. b = clkcfg & (1 << 3);
  202. if (osc_forced)
  203. return parent_rate;
  204. if (ht)
  205. return parent_rate / 2;
  206. else
  207. return parent_rate;
  208. }
  209. static u8 clk_pxa27x_core_get_parent(struct clk_hw *hw)
  210. {
  211. unsigned long clkcfg;
  212. unsigned int t, ht, b, osc_forced;
  213. unsigned long ccsr = CCSR;
  214. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  215. if (osc_forced)
  216. return PXA_CORE_13Mhz;
  217. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  218. t = clkcfg & (1 << 0);
  219. ht = clkcfg & (1 << 2);
  220. b = clkcfg & (1 << 3);
  221. if (ht || t)
  222. return PXA_CORE_TURBO;
  223. return PXA_CORE_RUN;
  224. }
  225. PARENTS(clk_pxa27x_core) = { "osc_13mhz", "run", "cpll" };
  226. MUX_RO_RATE_RO_OPS(clk_pxa27x_core, "core");
  227. static unsigned long clk_pxa27x_run_get_rate(struct clk_hw *hw,
  228. unsigned long parent_rate)
  229. {
  230. unsigned long ccsr = CCSR;
  231. unsigned int n2 = (ccsr & CCSR_N2_MASK) >> CCSR_N2_SHIFT;
  232. return (parent_rate / n2) * 2;
  233. }
  234. PARENTS(clk_pxa27x_run) = { "cpll" };
  235. RATE_RO_OPS(clk_pxa27x_run, "run");
  236. static void __init pxa27x_register_core(void)
  237. {
  238. clk_register_clk_pxa27x_cpll();
  239. clk_register_clk_pxa27x_run();
  240. clkdev_pxa_register(CLK_CORE, "core", NULL,
  241. clk_register_clk_pxa27x_core());
  242. }
  243. static unsigned long clk_pxa27x_system_bus_get_rate(struct clk_hw *hw,
  244. unsigned long parent_rate)
  245. {
  246. unsigned long clkcfg;
  247. unsigned int b, osc_forced;
  248. unsigned long ccsr = CCSR;
  249. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  250. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  251. b = clkcfg & (1 << 3);
  252. if (osc_forced)
  253. return parent_rate;
  254. if (b)
  255. return parent_rate / 2;
  256. else
  257. return parent_rate;
  258. }
  259. static u8 clk_pxa27x_system_bus_get_parent(struct clk_hw *hw)
  260. {
  261. unsigned int osc_forced;
  262. unsigned long ccsr = CCSR;
  263. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  264. if (osc_forced)
  265. return PXA_BUS_13Mhz;
  266. else
  267. return PXA_BUS_RUN;
  268. }
  269. PARENTS(clk_pxa27x_system_bus) = { "osc_13mhz", "run" };
  270. MUX_RO_RATE_RO_OPS(clk_pxa27x_system_bus, "system_bus");
  271. static unsigned long clk_pxa27x_memory_get_rate(struct clk_hw *hw,
  272. unsigned long parent_rate)
  273. {
  274. unsigned int a, l, osc_forced;
  275. unsigned long cccr = CCCR;
  276. unsigned long ccsr = CCSR;
  277. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  278. a = cccr & (1 << CCCR_A_BIT);
  279. l = ccsr & CCSR_L_MASK;
  280. if (osc_forced || a)
  281. return parent_rate;
  282. if (l <= 10)
  283. return parent_rate;
  284. if (l <= 20)
  285. return parent_rate / 2;
  286. return parent_rate / 4;
  287. }
  288. static u8 clk_pxa27x_memory_get_parent(struct clk_hw *hw)
  289. {
  290. unsigned int osc_forced, a;
  291. unsigned long cccr = CCCR;
  292. unsigned long ccsr = CCSR;
  293. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  294. a = cccr & (1 << CCCR_A_BIT);
  295. if (osc_forced)
  296. return PXA_MEM_13Mhz;
  297. if (a)
  298. return PXA_MEM_SYSTEM_BUS;
  299. else
  300. return PXA_MEM_RUN;
  301. }
  302. PARENTS(clk_pxa27x_memory) = { "osc_13mhz", "system_bus", "run" };
  303. MUX_RO_RATE_RO_OPS(clk_pxa27x_memory, "memory");
  304. #define DUMMY_CLK(_con_id, _dev_id, _parent) \
  305. { .con_id = _con_id, .dev_id = _dev_id, .parent = _parent }
  306. struct dummy_clk {
  307. const char *con_id;
  308. const char *dev_id;
  309. const char *parent;
  310. };
  311. static struct dummy_clk dummy_clks[] __initdata = {
  312. DUMMY_CLK(NULL, "pxa27x-gpio", "osc_32_768khz"),
  313. DUMMY_CLK(NULL, "sa1100-rtc", "osc_32_768khz"),
  314. DUMMY_CLK("UARTCLK", "pxa2xx-ir", "STUART"),
  315. };
  316. static void __init pxa27x_dummy_clocks_init(void)
  317. {
  318. struct clk *clk;
  319. struct dummy_clk *d;
  320. const char *name;
  321. int i;
  322. for (i = 0; i < ARRAY_SIZE(dummy_clks); i++) {
  323. d = &dummy_clks[i];
  324. name = d->dev_id ? d->dev_id : d->con_id;
  325. clk = clk_register_fixed_factor(NULL, name, d->parent, 0, 1, 1);
  326. clk_register_clkdev(clk, d->con_id, d->dev_id);
  327. }
  328. }
  329. static void __init pxa27x_base_clocks_init(void)
  330. {
  331. pxa27x_register_plls();
  332. pxa27x_register_core();
  333. clk_register_clk_pxa27x_system_bus();
  334. clk_register_clk_pxa27x_memory();
  335. clk_register_clk_pxa27x_lcd_base();
  336. }
  337. int __init pxa27x_clocks_init(void)
  338. {
  339. pxa27x_base_clocks_init();
  340. pxa27x_dummy_clocks_init();
  341. return clk_pxa_cken_init(pxa27x_clocks, ARRAY_SIZE(pxa27x_clocks));
  342. }
  343. static void __init pxa27x_dt_clocks_init(struct device_node *np)
  344. {
  345. pxa27x_clocks_init();
  346. clk_pxa_dt_common_init(np);
  347. }
  348. CLK_OF_DECLARE(pxa_clks, "marvell,pxa270-clocks", pxa27x_dt_clocks_init);