clk-pxa27x.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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 <mach/smemc.h>
  20. #include <dt-bindings/clock/pxa-clock.h>
  21. #include "clk-pxa.h"
  22. #define KHz 1000
  23. #define MHz (1000 * 1000)
  24. enum {
  25. PXA_CORE_13Mhz = 0,
  26. PXA_CORE_RUN,
  27. PXA_CORE_TURBO,
  28. };
  29. enum {
  30. PXA_BUS_13Mhz = 0,
  31. PXA_BUS_RUN,
  32. };
  33. enum {
  34. PXA_LCD_13Mhz = 0,
  35. PXA_LCD_RUN,
  36. };
  37. enum {
  38. PXA_MEM_13Mhz = 0,
  39. PXA_MEM_SYSTEM_BUS,
  40. PXA_MEM_RUN,
  41. };
  42. #define PXA27x_CLKCFG(B, HT, T) \
  43. (CLKCFG_FCS | \
  44. ((B) ? CLKCFG_FASTBUS : 0) | \
  45. ((HT) ? CLKCFG_HALFTURBO : 0) | \
  46. ((T) ? CLKCFG_TURBO : 0))
  47. #define PXA27x_CCCR(A, L, N2) (A << 25 | N2 << 7 | L)
  48. #define MDCNFG_DRAC2(mdcnfg) (((mdcnfg) >> 21) & 0x3)
  49. #define MDCNFG_DRAC0(mdcnfg) (((mdcnfg) >> 5) & 0x3)
  50. /* Define the refresh period in mSec for the SDRAM and the number of rows */
  51. #define SDRAM_TREF 64 /* standard 64ms SDRAM */
  52. static const char * const get_freq_khz[] = {
  53. "core", "run", "cpll", "memory",
  54. "system_bus"
  55. };
  56. static int get_sdram_rows(void)
  57. {
  58. static int sdram_rows;
  59. unsigned int drac2 = 0, drac0 = 0;
  60. u32 mdcnfg;
  61. if (sdram_rows)
  62. return sdram_rows;
  63. mdcnfg = readl_relaxed(MDCNFG);
  64. if (mdcnfg & (MDCNFG_DE2 | MDCNFG_DE3))
  65. drac2 = MDCNFG_DRAC2(mdcnfg);
  66. if (mdcnfg & (MDCNFG_DE0 | MDCNFG_DE1))
  67. drac0 = MDCNFG_DRAC0(mdcnfg);
  68. sdram_rows = 1 << (11 + max(drac0, drac2));
  69. return sdram_rows;
  70. }
  71. static u32 mdrefr_dri(unsigned int freq_khz)
  72. {
  73. u32 interval = freq_khz * SDRAM_TREF / get_sdram_rows();
  74. return (interval - 31) / 32;
  75. }
  76. /*
  77. * Get the clock frequency as reflected by CCSR and the turbo flag.
  78. * We assume these values have been applied via a fcs.
  79. * If info is not 0 we also display the current settings.
  80. */
  81. unsigned int pxa27x_get_clk_frequency_khz(int info)
  82. {
  83. struct clk *clk;
  84. unsigned long clks[5];
  85. int i;
  86. for (i = 0; i < 5; i++) {
  87. clk = clk_get(NULL, get_freq_khz[i]);
  88. if (IS_ERR(clk)) {
  89. clks[i] = 0;
  90. } else {
  91. clks[i] = clk_get_rate(clk);
  92. clk_put(clk);
  93. }
  94. }
  95. if (info) {
  96. pr_info("Run Mode clock: %ld.%02ldMHz\n",
  97. clks[1] / 1000000, (clks[1] % 1000000) / 10000);
  98. pr_info("Turbo Mode clock: %ld.%02ldMHz\n",
  99. clks[2] / 1000000, (clks[2] % 1000000) / 10000);
  100. pr_info("Memory clock: %ld.%02ldMHz\n",
  101. clks[3] / 1000000, (clks[3] % 1000000) / 10000);
  102. pr_info("System bus clock: %ld.%02ldMHz\n",
  103. clks[4] / 1000000, (clks[4] % 1000000) / 10000);
  104. }
  105. return (unsigned int)clks[0] / KHz;
  106. }
  107. bool pxa27x_is_ppll_disabled(void)
  108. {
  109. unsigned long ccsr = readl(CCSR);
  110. return ccsr & (1 << CCCR_PPDIS_BIT);
  111. }
  112. #define PXA27X_CKEN(dev_id, con_id, parents, mult_hp, div_hp, \
  113. bit, is_lp, flags) \
  114. PXA_CKEN(dev_id, con_id, bit, parents, 1, 1, mult_hp, div_hp, \
  115. is_lp, CKEN, CKEN_ ## bit, flags)
  116. #define PXA27X_PBUS_CKEN(dev_id, con_id, bit, mult_hp, div_hp, delay) \
  117. PXA27X_CKEN(dev_id, con_id, pxa27x_pbus_parents, mult_hp, \
  118. div_hp, bit, pxa27x_is_ppll_disabled, 0)
  119. PARENTS(pxa27x_pbus) = { "osc_13mhz", "ppll_312mhz" };
  120. PARENTS(pxa27x_sbus) = { "system_bus", "system_bus" };
  121. PARENTS(pxa27x_32Mhz_bus) = { "osc_32_768khz", "osc_32_768khz" };
  122. PARENTS(pxa27x_lcd_bus) = { "lcd_base", "lcd_base" };
  123. PARENTS(pxa27x_membus) = { "lcd_base", "lcd_base" };
  124. #define PXA27X_CKEN_1RATE(dev_id, con_id, bit, parents, delay) \
  125. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  126. CKEN, CKEN_ ## bit, 0)
  127. #define PXA27X_CKEN_1RATE_AO(dev_id, con_id, bit, parents, delay) \
  128. PXA_CKEN_1RATE(dev_id, con_id, bit, parents, \
  129. CKEN, CKEN_ ## bit, CLK_IGNORE_UNUSED)
  130. static struct desc_clk_cken pxa27x_clocks[] __initdata = {
  131. PXA27X_PBUS_CKEN("pxa2xx-uart.0", NULL, FFUART, 2, 42, 1),
  132. PXA27X_PBUS_CKEN("pxa2xx-uart.1", NULL, BTUART, 2, 42, 1),
  133. PXA27X_PBUS_CKEN("pxa2xx-uart.2", NULL, STUART, 2, 42, 1),
  134. PXA27X_PBUS_CKEN("pxa2xx-i2s", NULL, I2S, 2, 51, 0),
  135. PXA27X_PBUS_CKEN("pxa2xx-i2c.0", NULL, I2C, 2, 19, 0),
  136. PXA27X_PBUS_CKEN("pxa27x-udc", NULL, USB, 2, 13, 5),
  137. PXA27X_PBUS_CKEN("pxa2xx-mci.0", NULL, MMC, 2, 32, 0),
  138. PXA27X_PBUS_CKEN("pxa2xx-ir", "FICPCLK", FICP, 2, 13, 0),
  139. PXA27X_PBUS_CKEN("pxa27x-ohci", NULL, USBHOST, 2, 13, 0),
  140. PXA27X_PBUS_CKEN("pxa2xx-i2c.1", NULL, PWRI2C, 1, 24, 0),
  141. PXA27X_PBUS_CKEN("pxa27x-ssp.0", NULL, SSP1, 1, 24, 0),
  142. PXA27X_PBUS_CKEN("pxa27x-ssp.1", NULL, SSP2, 1, 24, 0),
  143. PXA27X_PBUS_CKEN("pxa27x-ssp.2", NULL, SSP3, 1, 24, 0),
  144. PXA27X_PBUS_CKEN("pxa27x-pwm.0", NULL, PWM0, 1, 24, 0),
  145. PXA27X_PBUS_CKEN("pxa27x-pwm.1", NULL, PWM1, 1, 24, 0),
  146. PXA27X_PBUS_CKEN(NULL, "MSLCLK", MSL, 2, 13, 0),
  147. PXA27X_PBUS_CKEN(NULL, "USIMCLK", USIM, 2, 13, 0),
  148. PXA27X_PBUS_CKEN(NULL, "MSTKCLK", MEMSTK, 2, 32, 0),
  149. PXA27X_PBUS_CKEN(NULL, "AC97CLK", AC97, 1, 1, 0),
  150. PXA27X_PBUS_CKEN(NULL, "AC97CONFCLK", AC97CONF, 1, 1, 0),
  151. PXA27X_PBUS_CKEN(NULL, "OSTIMER0", OSTIMER, 1, 96, 0),
  152. PXA27X_CKEN_1RATE("pxa27x-keypad", NULL, KEYPAD,
  153. pxa27x_32Mhz_bus_parents, 0),
  154. PXA27X_CKEN_1RATE(NULL, "IMCLK", IM, pxa27x_sbus_parents, 0),
  155. PXA27X_CKEN_1RATE("pxa2xx-fb", NULL, LCD, pxa27x_lcd_bus_parents, 0),
  156. PXA27X_CKEN_1RATE("pxa27x-camera.0", NULL, CAMERA,
  157. pxa27x_lcd_bus_parents, 0),
  158. PXA27X_CKEN_1RATE_AO("pxa2xx-pcmcia", NULL, MEMC,
  159. pxa27x_membus_parents, 0),
  160. };
  161. /*
  162. * PXA270 definitions
  163. *
  164. * For the PXA27x:
  165. * Control variables are A, L, 2N for CCCR; B, HT, T for CLKCFG.
  166. *
  167. * A = 0 => memory controller clock from table 3-7,
  168. * A = 1 => memory controller clock = system bus clock
  169. * Run mode frequency = 13 MHz * L
  170. * Turbo mode frequency = 13 MHz * L * N
  171. * System bus frequency = 13 MHz * L / (B + 1)
  172. *
  173. * In CCCR:
  174. * A = 1
  175. * L = 16 oscillator to run mode ratio
  176. * 2N = 6 2 * (turbo mode to run mode ratio)
  177. *
  178. * In CCLKCFG:
  179. * B = 1 Fast bus mode
  180. * HT = 0 Half-Turbo mode
  181. * T = 1 Turbo mode
  182. *
  183. * For now, just support some of the combinations in table 3-7 of
  184. * PXA27x Processor Family Developer's Manual to simplify frequency
  185. * change sequences.
  186. */
  187. static struct pxa2xx_freq pxa27x_freqs[] = {
  188. {104000000, 104000, PXA27x_CCCR(1, 8, 2), 0, PXA27x_CLKCFG(1, 0, 1) },
  189. {156000000, 104000, PXA27x_CCCR(1, 8, 3), 0, PXA27x_CLKCFG(1, 0, 1) },
  190. {208000000, 208000, PXA27x_CCCR(0, 16, 2), 1, PXA27x_CLKCFG(0, 0, 1) },
  191. {312000000, 208000, PXA27x_CCCR(1, 16, 3), 1, PXA27x_CLKCFG(1, 0, 1) },
  192. {416000000, 208000, PXA27x_CCCR(1, 16, 4), 1, PXA27x_CLKCFG(1, 0, 1) },
  193. {520000000, 208000, PXA27x_CCCR(1, 16, 5), 1, PXA27x_CLKCFG(1, 0, 1) },
  194. {624000000, 208000, PXA27x_CCCR(1, 16, 6), 1, PXA27x_CLKCFG(1, 0, 1) },
  195. };
  196. static unsigned long clk_pxa27x_cpll_get_rate(struct clk_hw *hw,
  197. unsigned long parent_rate)
  198. {
  199. unsigned long clkcfg;
  200. unsigned int t, ht;
  201. unsigned int l, L, n2, N;
  202. unsigned long ccsr = readl(CCSR);
  203. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  204. t = clkcfg & (1 << 0);
  205. ht = clkcfg & (1 << 2);
  206. l = ccsr & CCSR_L_MASK;
  207. n2 = (ccsr & CCSR_N2_MASK) >> CCSR_N2_SHIFT;
  208. L = l * parent_rate;
  209. N = (L * n2) / 2;
  210. return N;
  211. }
  212. static int clk_pxa27x_cpll_determine_rate(struct clk_hw *hw,
  213. struct clk_rate_request *req)
  214. {
  215. return pxa2xx_determine_rate(req, pxa27x_freqs,
  216. ARRAY_SIZE(pxa27x_freqs));
  217. }
  218. static int clk_pxa27x_cpll_set_rate(struct clk_hw *hw, unsigned long rate,
  219. unsigned long parent_rate)
  220. {
  221. int i;
  222. pr_debug("%s(rate=%lu parent_rate=%lu)\n", __func__, rate, parent_rate);
  223. for (i = 0; i < ARRAY_SIZE(pxa27x_freqs); i++)
  224. if (pxa27x_freqs[i].cpll == rate)
  225. break;
  226. if (i >= ARRAY_SIZE(pxa27x_freqs))
  227. return -EINVAL;
  228. pxa2xx_cpll_change(&pxa27x_freqs[i], mdrefr_dri, MDREFR, CCCR);
  229. return 0;
  230. }
  231. PARENTS(clk_pxa27x_cpll) = { "osc_13mhz" };
  232. RATE_OPS(clk_pxa27x_cpll, "cpll");
  233. static unsigned long clk_pxa27x_lcd_base_get_rate(struct clk_hw *hw,
  234. unsigned long parent_rate)
  235. {
  236. unsigned int l, osc_forced;
  237. unsigned long ccsr = readl(CCSR);
  238. unsigned long cccr = readl(CCCR);
  239. l = ccsr & CCSR_L_MASK;
  240. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  241. if (osc_forced) {
  242. if (cccr & (1 << CCCR_LCD_26_BIT))
  243. return parent_rate * 2;
  244. else
  245. return parent_rate;
  246. }
  247. if (l <= 7)
  248. return parent_rate;
  249. if (l <= 16)
  250. return parent_rate / 2;
  251. return parent_rate / 4;
  252. }
  253. static u8 clk_pxa27x_lcd_base_get_parent(struct clk_hw *hw)
  254. {
  255. unsigned int osc_forced;
  256. unsigned long ccsr = readl(CCSR);
  257. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  258. if (osc_forced)
  259. return PXA_LCD_13Mhz;
  260. else
  261. return PXA_LCD_RUN;
  262. }
  263. PARENTS(clk_pxa27x_lcd_base) = { "osc_13mhz", "run" };
  264. MUX_RO_RATE_RO_OPS(clk_pxa27x_lcd_base, "lcd_base");
  265. static void __init pxa27x_register_plls(void)
  266. {
  267. clk_register_fixed_rate(NULL, "osc_13mhz", NULL,
  268. CLK_GET_RATE_NOCACHE,
  269. 13 * MHz);
  270. clkdev_pxa_register(CLK_OSC32k768, "osc_32_768khz", NULL,
  271. clk_register_fixed_rate(NULL, "osc_32_768khz", NULL,
  272. CLK_GET_RATE_NOCACHE,
  273. 32768 * KHz));
  274. clk_register_fixed_rate(NULL, "clk_dummy", NULL, 0, 0);
  275. clk_register_fixed_factor(NULL, "ppll_312mhz", "osc_13mhz", 0, 24, 1);
  276. }
  277. static u8 clk_pxa27x_core_get_parent(struct clk_hw *hw)
  278. {
  279. unsigned long clkcfg;
  280. unsigned int t, ht, osc_forced;
  281. unsigned long ccsr = readl(CCSR);
  282. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  283. if (osc_forced)
  284. return PXA_CORE_13Mhz;
  285. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  286. t = clkcfg & (1 << 0);
  287. ht = clkcfg & (1 << 2);
  288. if (ht || t)
  289. return PXA_CORE_TURBO;
  290. return PXA_CORE_RUN;
  291. }
  292. static int clk_pxa27x_core_set_parent(struct clk_hw *hw, u8 index)
  293. {
  294. if (index > PXA_CORE_TURBO)
  295. return -EINVAL;
  296. pxa2xx_core_turbo_switch(index == PXA_CORE_TURBO);
  297. return 0;
  298. }
  299. static int clk_pxa27x_core_determine_rate(struct clk_hw *hw,
  300. struct clk_rate_request *req)
  301. {
  302. return __clk_mux_determine_rate(hw, req);
  303. }
  304. PARENTS(clk_pxa27x_core) = { "osc_13mhz", "run", "cpll" };
  305. MUX_OPS(clk_pxa27x_core, "core", CLK_SET_RATE_PARENT);
  306. static unsigned long clk_pxa27x_run_get_rate(struct clk_hw *hw,
  307. unsigned long parent_rate)
  308. {
  309. unsigned long ccsr = readl(CCSR);
  310. unsigned int n2 = (ccsr & CCSR_N2_MASK) >> CCSR_N2_SHIFT;
  311. return (parent_rate / n2) * 2;
  312. }
  313. PARENTS(clk_pxa27x_run) = { "cpll" };
  314. RATE_RO_OPS(clk_pxa27x_run, "run");
  315. static void __init pxa27x_register_core(void)
  316. {
  317. clkdev_pxa_register(CLK_NONE, "cpll", NULL,
  318. clk_register_clk_pxa27x_cpll());
  319. clkdev_pxa_register(CLK_NONE, "run", NULL,
  320. clk_register_clk_pxa27x_run());
  321. clkdev_pxa_register(CLK_CORE, "core", NULL,
  322. clk_register_clk_pxa27x_core());
  323. }
  324. static unsigned long clk_pxa27x_system_bus_get_rate(struct clk_hw *hw,
  325. unsigned long parent_rate)
  326. {
  327. unsigned long clkcfg;
  328. unsigned int b, osc_forced;
  329. unsigned long ccsr = readl(CCSR);
  330. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  331. asm("mrc\tp14, 0, %0, c6, c0, 0" : "=r" (clkcfg));
  332. b = clkcfg & (1 << 3);
  333. if (osc_forced)
  334. return parent_rate;
  335. if (b)
  336. return parent_rate;
  337. else
  338. return parent_rate / 2;
  339. }
  340. static u8 clk_pxa27x_system_bus_get_parent(struct clk_hw *hw)
  341. {
  342. unsigned int osc_forced;
  343. unsigned long ccsr = readl(CCSR);
  344. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  345. if (osc_forced)
  346. return PXA_BUS_13Mhz;
  347. else
  348. return PXA_BUS_RUN;
  349. }
  350. PARENTS(clk_pxa27x_system_bus) = { "osc_13mhz", "run" };
  351. MUX_RO_RATE_RO_OPS(clk_pxa27x_system_bus, "system_bus");
  352. static unsigned long clk_pxa27x_memory_get_rate(struct clk_hw *hw,
  353. unsigned long parent_rate)
  354. {
  355. unsigned int a, l, osc_forced;
  356. unsigned long cccr = readl(CCCR);
  357. unsigned long ccsr = readl(CCSR);
  358. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  359. a = cccr & (1 << CCCR_A_BIT);
  360. l = ccsr & CCSR_L_MASK;
  361. if (osc_forced || a)
  362. return parent_rate;
  363. if (l <= 10)
  364. return parent_rate;
  365. if (l <= 20)
  366. return parent_rate / 2;
  367. return parent_rate / 4;
  368. }
  369. static u8 clk_pxa27x_memory_get_parent(struct clk_hw *hw)
  370. {
  371. unsigned int osc_forced, a;
  372. unsigned long cccr = readl(CCCR);
  373. unsigned long ccsr = readl(CCSR);
  374. osc_forced = ccsr & (1 << CCCR_CPDIS_BIT);
  375. a = cccr & (1 << CCCR_A_BIT);
  376. if (osc_forced)
  377. return PXA_MEM_13Mhz;
  378. if (a)
  379. return PXA_MEM_SYSTEM_BUS;
  380. else
  381. return PXA_MEM_RUN;
  382. }
  383. PARENTS(clk_pxa27x_memory) = { "osc_13mhz", "system_bus", "run" };
  384. MUX_RO_RATE_RO_OPS(clk_pxa27x_memory, "memory");
  385. #define DUMMY_CLK(_con_id, _dev_id, _parent) \
  386. { .con_id = _con_id, .dev_id = _dev_id, .parent = _parent }
  387. struct dummy_clk {
  388. const char *con_id;
  389. const char *dev_id;
  390. const char *parent;
  391. };
  392. static struct dummy_clk dummy_clks[] __initdata = {
  393. DUMMY_CLK(NULL, "pxa27x-gpio", "osc_32_768khz"),
  394. DUMMY_CLK(NULL, "sa1100-rtc", "osc_32_768khz"),
  395. DUMMY_CLK("UARTCLK", "pxa2xx-ir", "STUART"),
  396. };
  397. static void __init pxa27x_dummy_clocks_init(void)
  398. {
  399. struct clk *clk;
  400. struct dummy_clk *d;
  401. const char *name;
  402. int i;
  403. for (i = 0; i < ARRAY_SIZE(dummy_clks); i++) {
  404. d = &dummy_clks[i];
  405. name = d->dev_id ? d->dev_id : d->con_id;
  406. clk = clk_register_fixed_factor(NULL, name, d->parent, 0, 1, 1);
  407. clk_register_clkdev(clk, d->con_id, d->dev_id);
  408. }
  409. }
  410. static void __init pxa27x_base_clocks_init(void)
  411. {
  412. pxa27x_register_plls();
  413. pxa27x_register_core();
  414. clkdev_pxa_register(CLK_NONE, "system_bus", NULL,
  415. clk_register_clk_pxa27x_system_bus());
  416. clkdev_pxa_register(CLK_NONE, "memory", NULL,
  417. clk_register_clk_pxa27x_memory());
  418. clk_register_clk_pxa27x_lcd_base();
  419. }
  420. int __init pxa27x_clocks_init(void)
  421. {
  422. pxa27x_base_clocks_init();
  423. pxa27x_dummy_clocks_init();
  424. return clk_pxa_cken_init(pxa27x_clocks, ARRAY_SIZE(pxa27x_clocks));
  425. }
  426. static void __init pxa27x_dt_clocks_init(struct device_node *np)
  427. {
  428. pxa27x_clocks_init();
  429. clk_pxa_dt_common_init(np);
  430. }
  431. CLK_OF_DECLARE(pxa_clks, "marvell,pxa270-clocks", pxa27x_dt_clocks_init);