clock-imx35.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. /*
  2. * Copyright (C) 2009 by Sascha Hauer, Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  16. * MA 02110-1301, USA.
  17. */
  18. #include <linux/kernel.h>
  19. #include <linux/init.h>
  20. #include <linux/list.h>
  21. #include <linux/clk.h>
  22. #include <linux/io.h>
  23. #include <asm/clkdev.h>
  24. #include <mach/clock.h>
  25. #include <mach/hardware.h>
  26. #include <mach/common.h>
  27. #define CCM_BASE IO_ADDRESS(CCM_BASE_ADDR)
  28. #define CCM_CCMR 0x00
  29. #define CCM_PDR0 0x04
  30. #define CCM_PDR1 0x08
  31. #define CCM_PDR2 0x0C
  32. #define CCM_PDR3 0x10
  33. #define CCM_PDR4 0x14
  34. #define CCM_RCSR 0x18
  35. #define CCM_MPCTL 0x1C
  36. #define CCM_PPCTL 0x20
  37. #define CCM_ACMR 0x24
  38. #define CCM_COSR 0x28
  39. #define CCM_CGR0 0x2C
  40. #define CCM_CGR1 0x30
  41. #define CCM_CGR2 0x34
  42. #define CCM_CGR3 0x38
  43. #ifdef HAVE_SET_RATE_SUPPORT
  44. static void calc_dividers(u32 div, u32 *pre, u32 *post, u32 maxpost)
  45. {
  46. u32 min_pre, temp_pre, old_err, err;
  47. min_pre = (div - 1) / maxpost + 1;
  48. old_err = 8;
  49. for (temp_pre = 8; temp_pre >= min_pre; temp_pre--) {
  50. if (div > (temp_pre * maxpost))
  51. break;
  52. if (div < (temp_pre * temp_pre))
  53. continue;
  54. err = div % temp_pre;
  55. if (err == 0) {
  56. *pre = temp_pre;
  57. break;
  58. }
  59. err = temp_pre - err;
  60. if (err < old_err) {
  61. old_err = err;
  62. *pre = temp_pre;
  63. }
  64. }
  65. *post = (div + *pre - 1) / *pre;
  66. }
  67. /* get the best values for a 3-bit divider combined with a 6-bit divider */
  68. static void calc_dividers_3_6(u32 div, u32 *pre, u32 *post)
  69. {
  70. if (div >= 512) {
  71. *pre = 8;
  72. *post = 64;
  73. } else if (div >= 64) {
  74. calc_dividers(div, pre, post, 64);
  75. } else if (div <= 8) {
  76. *pre = div;
  77. *post = 1;
  78. } else {
  79. *pre = 1;
  80. *post = div;
  81. }
  82. }
  83. /* get the best values for two cascaded 3-bit dividers */
  84. static void calc_dividers_3_3(u32 div, u32 *pre, u32 *post)
  85. {
  86. if (div >= 64) {
  87. *pre = *post = 8;
  88. } else if (div > 8) {
  89. calc_dividers(div, pre, post, 8);
  90. } else {
  91. *pre = 1;
  92. *post = div;
  93. }
  94. }
  95. #endif
  96. static unsigned long get_rate_mpll(void)
  97. {
  98. ulong mpctl = __raw_readl(CCM_BASE + CCM_MPCTL);
  99. return mxc_decode_pll(mpctl, 24000000);
  100. }
  101. static unsigned long get_rate_ppll(void)
  102. {
  103. ulong ppctl = __raw_readl(CCM_BASE + CCM_PPCTL);
  104. return mxc_decode_pll(ppctl, 24000000);
  105. }
  106. struct arm_ahb_div {
  107. unsigned char arm, ahb, sel;
  108. };
  109. static struct arm_ahb_div clk_consumer[] = {
  110. { .arm = 1, .ahb = 4, .sel = 0},
  111. { .arm = 1, .ahb = 3, .sel = 1},
  112. { .arm = 2, .ahb = 2, .sel = 0},
  113. { .arm = 0, .ahb = 0, .sel = 0},
  114. { .arm = 0, .ahb = 0, .sel = 0},
  115. { .arm = 0, .ahb = 0, .sel = 0},
  116. { .arm = 4, .ahb = 1, .sel = 0},
  117. { .arm = 1, .ahb = 5, .sel = 0},
  118. { .arm = 1, .ahb = 8, .sel = 0},
  119. { .arm = 1, .ahb = 6, .sel = 1},
  120. { .arm = 2, .ahb = 4, .sel = 0},
  121. { .arm = 0, .ahb = 0, .sel = 0},
  122. { .arm = 0, .ahb = 0, .sel = 0},
  123. { .arm = 0, .ahb = 0, .sel = 0},
  124. { .arm = 4, .ahb = 2, .sel = 0},
  125. { .arm = 0, .ahb = 0, .sel = 0},
  126. };
  127. static struct arm_ahb_div clk_automotive[] = {
  128. { .arm = 1, .ahb = 3, .sel = 0},
  129. { .arm = 1, .ahb = 2, .sel = 1},
  130. { .arm = 2, .ahb = 1, .sel = 1},
  131. { .arm = 0, .ahb = 0, .sel = 0},
  132. { .arm = 1, .ahb = 6, .sel = 0},
  133. { .arm = 1, .ahb = 4, .sel = 1},
  134. { .arm = 2, .ahb = 2, .sel = 1},
  135. { .arm = 0, .ahb = 0, .sel = 0},
  136. };
  137. static unsigned long get_rate_arm(void)
  138. {
  139. unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
  140. struct arm_ahb_div *aad;
  141. unsigned long fref = get_rate_mpll();
  142. if (pdr0 & 1) {
  143. /* consumer path */
  144. aad = &clk_consumer[(pdr0 >> 16) & 0xf];
  145. if (aad->sel)
  146. fref = fref * 2 / 3;
  147. } else {
  148. /* auto path */
  149. aad = &clk_automotive[(pdr0 >> 9) & 0x7];
  150. if (aad->sel)
  151. fref = fref * 3 / 4;
  152. }
  153. return fref / aad->arm;
  154. }
  155. static unsigned long get_rate_ahb(struct clk *clk)
  156. {
  157. unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
  158. struct arm_ahb_div *aad;
  159. unsigned long fref = get_rate_mpll();
  160. if (pdr0 & 1)
  161. /* consumer path */
  162. aad = &clk_consumer[(pdr0 >> 16) & 0xf];
  163. else
  164. /* auto path */
  165. aad = &clk_automotive[(pdr0 >> 9) & 0x7];
  166. return fref / aad->ahb;
  167. }
  168. static unsigned long get_rate_ipg(struct clk *clk)
  169. {
  170. return get_rate_ahb(NULL) >> 1;
  171. }
  172. static unsigned long get_3_3_div(unsigned long in)
  173. {
  174. return (((in >> 3) & 0x7) + 1) * ((in & 0x7) + 1);
  175. }
  176. static unsigned long get_rate_uart(struct clk *clk)
  177. {
  178. unsigned long pdr3 = __raw_readl(CCM_BASE + CCM_PDR3);
  179. unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
  180. unsigned long div = get_3_3_div(pdr4 >> 10);
  181. if (pdr3 & (1 << 14))
  182. return get_rate_arm() / div;
  183. else
  184. return get_rate_ppll() / div;
  185. }
  186. static unsigned long get_rate_sdhc(struct clk *clk)
  187. {
  188. unsigned long pdr3 = __raw_readl(CCM_BASE + CCM_PDR3);
  189. unsigned long div, rate;
  190. if (pdr3 & (1 << 6))
  191. rate = get_rate_arm();
  192. else
  193. rate = get_rate_ppll();
  194. switch (clk->id) {
  195. default:
  196. case 0:
  197. div = pdr3 & 0x3f;
  198. break;
  199. case 1:
  200. div = (pdr3 >> 8) & 0x3f;
  201. break;
  202. case 2:
  203. div = (pdr3 >> 16) & 0x3f;
  204. break;
  205. }
  206. return rate / get_3_3_div(div);
  207. }
  208. static unsigned long get_rate_mshc(struct clk *clk)
  209. {
  210. unsigned long pdr1 = __raw_readl(CCM_BASE + CCM_PDR1);
  211. unsigned long div1, div2, rate;
  212. if (pdr1 & (1 << 7))
  213. rate = get_rate_arm();
  214. else
  215. rate = get_rate_ppll();
  216. div1 = (pdr1 >> 29) & 0x7;
  217. div2 = (pdr1 >> 22) & 0x3f;
  218. return rate / ((div1 + 1) * (div2 + 1));
  219. }
  220. static unsigned long get_rate_ssi(struct clk *clk)
  221. {
  222. unsigned long pdr2 = __raw_readl(CCM_BASE + CCM_PDR2);
  223. unsigned long div1, div2, rate;
  224. if (pdr2 & (1 << 6))
  225. rate = get_rate_arm();
  226. else
  227. rate = get_rate_ppll();
  228. switch (clk->id) {
  229. default:
  230. case 0:
  231. div1 = pdr2 & 0x3f;
  232. div2 = (pdr2 >> 24) & 0x7;
  233. break;
  234. case 1:
  235. div1 = (pdr2 >> 8) & 0x3f;
  236. div2 = (pdr2 >> 27) & 0x7;
  237. break;
  238. }
  239. return rate / ((div1 + 1) * (div2 + 1));
  240. }
  241. static unsigned long get_rate_csi(struct clk *clk)
  242. {
  243. unsigned long pdr2 = __raw_readl(CCM_BASE + CCM_PDR2);
  244. unsigned long rate;
  245. if (pdr2 & (1 << 7))
  246. rate = get_rate_arm();
  247. else
  248. rate = get_rate_ppll();
  249. return rate / get_3_3_div((pdr2 >> 16) & 0x3f);
  250. }
  251. static unsigned long get_rate_ipg_per(struct clk *clk)
  252. {
  253. unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
  254. unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
  255. unsigned long div1, div2;
  256. if (pdr0 & (1 << 26)) {
  257. div1 = (pdr4 >> 19) & 0x7;
  258. div2 = (pdr4 >> 16) & 0x7;
  259. return get_rate_arm() / ((div1 + 1) * (div2 + 1));
  260. } else {
  261. div1 = (pdr0 >> 12) & 0x7;
  262. return get_rate_ahb(NULL) / div1;
  263. }
  264. }
  265. static int clk_cgr_enable(struct clk *clk)
  266. {
  267. u32 reg;
  268. reg = __raw_readl(clk->enable_reg);
  269. reg |= 3 << clk->enable_shift;
  270. __raw_writel(reg, clk->enable_reg);
  271. return 0;
  272. }
  273. static void clk_cgr_disable(struct clk *clk)
  274. {
  275. u32 reg;
  276. reg = __raw_readl(clk->enable_reg);
  277. reg &= ~(3 << clk->enable_shift);
  278. __raw_writel(reg, clk->enable_reg);
  279. }
  280. #define DEFINE_CLOCK(name, i, er, es, gr, sr) \
  281. static struct clk name = { \
  282. .id = i, \
  283. .enable_reg = CCM_BASE + er, \
  284. .enable_shift = es, \
  285. .get_rate = gr, \
  286. .set_rate = sr, \
  287. .enable = clk_cgr_enable, \
  288. .disable = clk_cgr_disable, \
  289. }
  290. DEFINE_CLOCK(asrc_clk, 0, CCM_CGR0, 0, NULL, NULL);
  291. DEFINE_CLOCK(ata_clk, 0, CCM_CGR0, 2, get_rate_ipg, NULL);
  292. DEFINE_CLOCK(audmux_clk, 0, CCM_CGR0, 4, NULL, NULL);
  293. DEFINE_CLOCK(can1_clk, 0, CCM_CGR0, 6, get_rate_ipg, NULL);
  294. DEFINE_CLOCK(can2_clk, 1, CCM_CGR0, 8, get_rate_ipg, NULL);
  295. DEFINE_CLOCK(cspi1_clk, 0, CCM_CGR0, 10, get_rate_ipg, NULL);
  296. DEFINE_CLOCK(cspi2_clk, 1, CCM_CGR0, 12, get_rate_ipg, NULL);
  297. DEFINE_CLOCK(ect_clk, 0, CCM_CGR0, 14, get_rate_ipg, NULL);
  298. DEFINE_CLOCK(edio_clk, 0, CCM_CGR0, 16, NULL, NULL);
  299. DEFINE_CLOCK(emi_clk, 0, CCM_CGR0, 18, get_rate_ipg, NULL);
  300. DEFINE_CLOCK(epit1_clk, 0, CCM_CGR0, 20, get_rate_ipg_per, NULL);
  301. DEFINE_CLOCK(epit2_clk, 1, CCM_CGR0, 22, get_rate_ipg_per, NULL);
  302. DEFINE_CLOCK(esai_clk, 0, CCM_CGR0, 24, NULL, NULL);
  303. DEFINE_CLOCK(esdhc1_clk, 0, CCM_CGR0, 26, get_rate_sdhc, NULL);
  304. DEFINE_CLOCK(esdhc2_clk, 1, CCM_CGR0, 28, get_rate_sdhc, NULL);
  305. DEFINE_CLOCK(esdhc3_clk, 2, CCM_CGR0, 30, get_rate_sdhc, NULL);
  306. DEFINE_CLOCK(fec_clk, 0, CCM_CGR1, 0, get_rate_ipg, NULL);
  307. DEFINE_CLOCK(gpio1_clk, 0, CCM_CGR1, 2, NULL, NULL);
  308. DEFINE_CLOCK(gpio2_clk, 1, CCM_CGR1, 4, NULL, NULL);
  309. DEFINE_CLOCK(gpio3_clk, 2, CCM_CGR1, 6, NULL, NULL);
  310. DEFINE_CLOCK(gpt_clk, 0, CCM_CGR1, 8, get_rate_ipg, NULL);
  311. DEFINE_CLOCK(i2c1_clk, 0, CCM_CGR1, 10, get_rate_ipg_per, NULL);
  312. DEFINE_CLOCK(i2c2_clk, 1, CCM_CGR1, 12, get_rate_ipg_per, NULL);
  313. DEFINE_CLOCK(i2c3_clk, 2, CCM_CGR1, 14, get_rate_ipg_per, NULL);
  314. DEFINE_CLOCK(iomuxc_clk, 0, CCM_CGR1, 16, NULL, NULL);
  315. DEFINE_CLOCK(ipu_clk, 0, CCM_CGR1, 18, NULL, NULL);
  316. DEFINE_CLOCK(kpp_clk, 0, CCM_CGR1, 20, get_rate_ipg, NULL);
  317. DEFINE_CLOCK(mlb_clk, 0, CCM_CGR1, 22, get_rate_ahb, NULL);
  318. DEFINE_CLOCK(mshc_clk, 0, CCM_CGR1, 24, get_rate_mshc, NULL);
  319. DEFINE_CLOCK(owire_clk, 0, CCM_CGR1, 26, get_rate_ipg_per, NULL);
  320. DEFINE_CLOCK(pwm_clk, 0, CCM_CGR1, 28, get_rate_ipg_per, NULL);
  321. DEFINE_CLOCK(rngc_clk, 0, CCM_CGR1, 30, get_rate_ipg, NULL);
  322. DEFINE_CLOCK(rtc_clk, 0, CCM_CGR2, 0, get_rate_ipg, NULL);
  323. DEFINE_CLOCK(rtic_clk, 0, CCM_CGR2, 2, get_rate_ahb, NULL);
  324. DEFINE_CLOCK(scc_clk, 0, CCM_CGR2, 4, get_rate_ipg, NULL);
  325. DEFINE_CLOCK(sdma_clk, 0, CCM_CGR2, 6, NULL, NULL);
  326. DEFINE_CLOCK(spba_clk, 0, CCM_CGR2, 8, get_rate_ipg, NULL);
  327. DEFINE_CLOCK(spdif_clk, 0, CCM_CGR2, 10, NULL, NULL);
  328. DEFINE_CLOCK(ssi1_clk, 0, CCM_CGR2, 12, get_rate_ssi, NULL);
  329. DEFINE_CLOCK(ssi2_clk, 1, CCM_CGR2, 14, get_rate_ssi, NULL);
  330. DEFINE_CLOCK(uart1_clk, 0, CCM_CGR2, 16, get_rate_uart, NULL);
  331. DEFINE_CLOCK(uart2_clk, 1, CCM_CGR2, 18, get_rate_uart, NULL);
  332. DEFINE_CLOCK(uart3_clk, 2, CCM_CGR2, 20, get_rate_uart, NULL);
  333. DEFINE_CLOCK(usbotg_clk, 0, CCM_CGR2, 22, NULL, NULL);
  334. DEFINE_CLOCK(wdog_clk, 0, CCM_CGR2, 24, NULL, NULL);
  335. DEFINE_CLOCK(max_clk, 0, CCM_CGR2, 26, NULL, NULL);
  336. DEFINE_CLOCK(admux_clk, 0, CCM_CGR2, 30, NULL, NULL);
  337. DEFINE_CLOCK(csi_clk, 0, CCM_CGR3, 0, get_rate_csi, NULL);
  338. DEFINE_CLOCK(iim_clk, 0, CCM_CGR3, 2, NULL, NULL);
  339. DEFINE_CLOCK(gpu2d_clk, 0, CCM_CGR3, 4, NULL, NULL);
  340. #define _REGISTER_CLOCK(d, n, c) \
  341. { \
  342. .dev_id = d, \
  343. .con_id = n, \
  344. .clk = &c, \
  345. },
  346. static struct clk_lookup lookups[] __initdata = {
  347. _REGISTER_CLOCK(NULL, "asrc", asrc_clk)
  348. _REGISTER_CLOCK(NULL, "ata", ata_clk)
  349. _REGISTER_CLOCK(NULL, "audmux", audmux_clk)
  350. _REGISTER_CLOCK(NULL, "can", can1_clk)
  351. _REGISTER_CLOCK(NULL, "can", can2_clk)
  352. _REGISTER_CLOCK("spi_imx.0", NULL, cspi1_clk)
  353. _REGISTER_CLOCK("spi_imx.1", NULL, cspi2_clk)
  354. _REGISTER_CLOCK(NULL, "ect", ect_clk)
  355. _REGISTER_CLOCK(NULL, "edio", edio_clk)
  356. _REGISTER_CLOCK(NULL, "emi", emi_clk)
  357. _REGISTER_CLOCK(NULL, "epit", epit1_clk)
  358. _REGISTER_CLOCK(NULL, "epit", epit2_clk)
  359. _REGISTER_CLOCK(NULL, "esai", esai_clk)
  360. _REGISTER_CLOCK(NULL, "sdhc", esdhc1_clk)
  361. _REGISTER_CLOCK(NULL, "sdhc", esdhc2_clk)
  362. _REGISTER_CLOCK(NULL, "sdhc", esdhc3_clk)
  363. _REGISTER_CLOCK("fec.0", NULL, fec_clk)
  364. _REGISTER_CLOCK(NULL, "gpio", gpio1_clk)
  365. _REGISTER_CLOCK(NULL, "gpio", gpio2_clk)
  366. _REGISTER_CLOCK(NULL, "gpio", gpio3_clk)
  367. _REGISTER_CLOCK("gpt.0", NULL, gpt_clk)
  368. _REGISTER_CLOCK("imx-i2c.0", NULL, i2c1_clk)
  369. _REGISTER_CLOCK("imx-i2c.1", NULL, i2c2_clk)
  370. _REGISTER_CLOCK("imx-i2c.2", NULL, i2c3_clk)
  371. _REGISTER_CLOCK(NULL, "iomuxc", iomuxc_clk)
  372. _REGISTER_CLOCK(NULL, "ipu", ipu_clk)
  373. _REGISTER_CLOCK(NULL, "kpp", kpp_clk)
  374. _REGISTER_CLOCK(NULL, "mlb", mlb_clk)
  375. _REGISTER_CLOCK(NULL, "mshc", mshc_clk)
  376. _REGISTER_CLOCK("mxc_w1", NULL, owire_clk)
  377. _REGISTER_CLOCK(NULL, "pwm", pwm_clk)
  378. _REGISTER_CLOCK(NULL, "rngc", rngc_clk)
  379. _REGISTER_CLOCK(NULL, "rtc", rtc_clk)
  380. _REGISTER_CLOCK(NULL, "rtic", rtic_clk)
  381. _REGISTER_CLOCK(NULL, "scc", scc_clk)
  382. _REGISTER_CLOCK(NULL, "sdma", sdma_clk)
  383. _REGISTER_CLOCK(NULL, "spba", spba_clk)
  384. _REGISTER_CLOCK(NULL, "spdif", spdif_clk)
  385. _REGISTER_CLOCK(NULL, "ssi", ssi1_clk)
  386. _REGISTER_CLOCK(NULL, "ssi", ssi2_clk)
  387. _REGISTER_CLOCK("imx-uart.0", NULL, uart1_clk)
  388. _REGISTER_CLOCK("imx-uart.1", NULL, uart2_clk)
  389. _REGISTER_CLOCK("imx-uart.2", NULL, uart3_clk)
  390. _REGISTER_CLOCK(NULL, "usbotg", usbotg_clk)
  391. _REGISTER_CLOCK("mxc_wdt.0", NULL, wdog_clk)
  392. _REGISTER_CLOCK(NULL, "max", max_clk)
  393. _REGISTER_CLOCK(NULL, "admux", admux_clk)
  394. _REGISTER_CLOCK(NULL, "csi", csi_clk)
  395. _REGISTER_CLOCK(NULL, "iim", iim_clk)
  396. _REGISTER_CLOCK(NULL, "gpu2d", gpu2d_clk)
  397. };
  398. int __init mx35_clocks_init()
  399. {
  400. int i;
  401. unsigned int ll = 0;
  402. mxc_set_cpu_type(MXC_CPU_MX35);
  403. #ifdef CONFIG_DEBUG_LL_CONSOLE
  404. ll = (3 << 16);
  405. #endif
  406. for (i = 0; i < ARRAY_SIZE(lookups); i++)
  407. clkdev_add(&lookups[i]);
  408. /* Turn off all clocks except the ones we need to survive, namely:
  409. * EMI, GPIO1/2/3, GPT, IOMUX, MAX and eventually uart
  410. */
  411. __raw_writel((3 << 18), CCM_BASE + CCM_CGR0);
  412. __raw_writel((3 << 2) | (3 << 4) | (3 << 6) | (3 << 8) | (3 << 16),
  413. CCM_BASE + CCM_CGR1);
  414. __raw_writel((3 << 26) | ll, CCM_BASE + CCM_CGR2);
  415. __raw_writel(0, CCM_BASE + CCM_CGR3);
  416. mxc_timer_init(&gpt_clk);
  417. return 0;
  418. }