clock-imx35.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 unsigned long get_rate_arm(void)
  128. {
  129. unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
  130. struct arm_ahb_div *aad;
  131. unsigned long fref = get_rate_mpll();
  132. aad = &clk_consumer[(pdr0 >> 16) & 0xf];
  133. if (aad->sel)
  134. fref = fref * 2 / 3;
  135. return fref / aad->arm;
  136. }
  137. static unsigned long get_rate_ahb(struct clk *clk)
  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. aad = &clk_consumer[(pdr0 >> 16) & 0xf];
  143. return fref / aad->ahb;
  144. }
  145. static unsigned long get_rate_ipg(struct clk *clk)
  146. {
  147. return get_rate_ahb(NULL) >> 1;
  148. }
  149. static unsigned long get_3_3_div(unsigned long in)
  150. {
  151. return (((in >> 3) & 0x7) + 1) * ((in & 0x7) + 1);
  152. }
  153. static unsigned long get_rate_uart(struct clk *clk)
  154. {
  155. unsigned long pdr3 = __raw_readl(CCM_BASE + CCM_PDR3);
  156. unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
  157. unsigned long div = get_3_3_div(pdr4 >> 10);
  158. if (pdr3 & (1 << 14))
  159. return get_rate_arm() / div;
  160. else
  161. return get_rate_ppll() / div;
  162. }
  163. static unsigned long get_rate_sdhc(struct clk *clk)
  164. {
  165. unsigned long pdr3 = __raw_readl(CCM_BASE + CCM_PDR3);
  166. unsigned long div, rate;
  167. if (pdr3 & (1 << 6))
  168. rate = get_rate_arm();
  169. else
  170. rate = get_rate_ppll();
  171. switch (clk->id) {
  172. default:
  173. case 0:
  174. div = pdr3 & 0x3f;
  175. break;
  176. case 1:
  177. div = (pdr3 >> 8) & 0x3f;
  178. break;
  179. case 2:
  180. div = (pdr3 >> 16) & 0x3f;
  181. break;
  182. }
  183. return rate / get_3_3_div(div);
  184. }
  185. static unsigned long get_rate_mshc(struct clk *clk)
  186. {
  187. unsigned long pdr1 = __raw_readl(CCM_BASE + CCM_PDR1);
  188. unsigned long div1, div2, rate;
  189. if (pdr1 & (1 << 7))
  190. rate = get_rate_arm();
  191. else
  192. rate = get_rate_ppll();
  193. div1 = (pdr1 >> 29) & 0x7;
  194. div2 = (pdr1 >> 22) & 0x3f;
  195. return rate / ((div1 + 1) * (div2 + 1));
  196. }
  197. static unsigned long get_rate_ssi(struct clk *clk)
  198. {
  199. unsigned long pdr2 = __raw_readl(CCM_BASE + CCM_PDR2);
  200. unsigned long div1, div2, rate;
  201. if (pdr2 & (1 << 6))
  202. rate = get_rate_arm();
  203. else
  204. rate = get_rate_ppll();
  205. switch (clk->id) {
  206. default:
  207. case 0:
  208. div1 = pdr2 & 0x3f;
  209. div2 = (pdr2 >> 24) & 0x7;
  210. break;
  211. case 1:
  212. div1 = (pdr2 >> 8) & 0x3f;
  213. div2 = (pdr2 >> 27) & 0x7;
  214. break;
  215. }
  216. return rate / ((div1 + 1) * (div2 + 1));
  217. }
  218. static unsigned long get_rate_csi(struct clk *clk)
  219. {
  220. unsigned long pdr2 = __raw_readl(CCM_BASE + CCM_PDR2);
  221. unsigned long rate;
  222. if (pdr2 & (1 << 7))
  223. rate = get_rate_arm();
  224. else
  225. rate = get_rate_ppll();
  226. return rate / get_3_3_div((pdr2 >> 16) & 0x3f);
  227. }
  228. static unsigned long get_rate_otg(struct clk *clk)
  229. {
  230. unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
  231. unsigned long rate;
  232. if (pdr4 & (1 << 9))
  233. rate = get_rate_arm();
  234. else
  235. rate = get_rate_ppll();
  236. return rate / get_3_3_div((pdr4 >> 22) & 0x3f);
  237. }
  238. static unsigned long get_rate_ipg_per(struct clk *clk)
  239. {
  240. unsigned long pdr0 = __raw_readl(CCM_BASE + CCM_PDR0);
  241. unsigned long pdr4 = __raw_readl(CCM_BASE + CCM_PDR4);
  242. unsigned long div1, div2;
  243. if (pdr0 & (1 << 26)) {
  244. div1 = (pdr4 >> 19) & 0x7;
  245. div2 = (pdr4 >> 16) & 0x7;
  246. return get_rate_arm() / ((div1 + 1) * (div2 + 1));
  247. } else {
  248. div1 = (pdr0 >> 12) & 0x7;
  249. return get_rate_ahb(NULL) / div1;
  250. }
  251. }
  252. static int clk_cgr_enable(struct clk *clk)
  253. {
  254. u32 reg;
  255. reg = __raw_readl(clk->enable_reg);
  256. reg |= 3 << clk->enable_shift;
  257. __raw_writel(reg, clk->enable_reg);
  258. return 0;
  259. }
  260. static void clk_cgr_disable(struct clk *clk)
  261. {
  262. u32 reg;
  263. reg = __raw_readl(clk->enable_reg);
  264. reg &= ~(3 << clk->enable_shift);
  265. __raw_writel(reg, clk->enable_reg);
  266. }
  267. #define DEFINE_CLOCK(name, i, er, es, gr, sr) \
  268. static struct clk name = { \
  269. .id = i, \
  270. .enable_reg = CCM_BASE + er, \
  271. .enable_shift = es, \
  272. .get_rate = gr, \
  273. .set_rate = sr, \
  274. .enable = clk_cgr_enable, \
  275. .disable = clk_cgr_disable, \
  276. }
  277. DEFINE_CLOCK(asrc_clk, 0, CCM_CGR0, 0, NULL, NULL);
  278. DEFINE_CLOCK(ata_clk, 0, CCM_CGR0, 2, get_rate_ipg, NULL);
  279. DEFINE_CLOCK(audmux_clk, 0, CCM_CGR0, 4, NULL, NULL);
  280. DEFINE_CLOCK(can1_clk, 0, CCM_CGR0, 6, get_rate_ipg, NULL);
  281. DEFINE_CLOCK(can2_clk, 1, CCM_CGR0, 8, get_rate_ipg, NULL);
  282. DEFINE_CLOCK(cspi1_clk, 0, CCM_CGR0, 10, get_rate_ipg, NULL);
  283. DEFINE_CLOCK(cspi2_clk, 1, CCM_CGR0, 12, get_rate_ipg, NULL);
  284. DEFINE_CLOCK(ect_clk, 0, CCM_CGR0, 14, get_rate_ipg, NULL);
  285. DEFINE_CLOCK(edio_clk, 0, CCM_CGR0, 16, NULL, NULL);
  286. DEFINE_CLOCK(emi_clk, 0, CCM_CGR0, 18, get_rate_ipg, NULL);
  287. DEFINE_CLOCK(epit1_clk, 0, CCM_CGR0, 20, get_rate_ipg_per, NULL);
  288. DEFINE_CLOCK(epit2_clk, 1, CCM_CGR0, 22, get_rate_ipg_per, NULL);
  289. DEFINE_CLOCK(esai_clk, 0, CCM_CGR0, 24, NULL, NULL);
  290. DEFINE_CLOCK(esdhc1_clk, 0, CCM_CGR0, 26, get_rate_sdhc, NULL);
  291. DEFINE_CLOCK(esdhc2_clk, 1, CCM_CGR0, 28, get_rate_sdhc, NULL);
  292. DEFINE_CLOCK(esdhc3_clk, 2, CCM_CGR0, 30, get_rate_sdhc, NULL);
  293. DEFINE_CLOCK(fec_clk, 0, CCM_CGR1, 0, get_rate_ipg, NULL);
  294. DEFINE_CLOCK(gpio1_clk, 0, CCM_CGR1, 2, NULL, NULL);
  295. DEFINE_CLOCK(gpio2_clk, 1, CCM_CGR1, 4, NULL, NULL);
  296. DEFINE_CLOCK(gpio3_clk, 2, CCM_CGR1, 6, NULL, NULL);
  297. DEFINE_CLOCK(gpt_clk, 0, CCM_CGR1, 8, get_rate_ipg, NULL);
  298. DEFINE_CLOCK(i2c1_clk, 0, CCM_CGR1, 10, get_rate_ipg_per, NULL);
  299. DEFINE_CLOCK(i2c2_clk, 1, CCM_CGR1, 12, get_rate_ipg_per, NULL);
  300. DEFINE_CLOCK(i2c3_clk, 2, CCM_CGR1, 14, get_rate_ipg_per, NULL);
  301. DEFINE_CLOCK(iomuxc_clk, 0, CCM_CGR1, 16, NULL, NULL);
  302. DEFINE_CLOCK(ipu_clk, 0, CCM_CGR1, 18, NULL, NULL);
  303. DEFINE_CLOCK(kpp_clk, 0, CCM_CGR1, 20, get_rate_ipg, NULL);
  304. DEFINE_CLOCK(mlb_clk, 0, CCM_CGR1, 22, get_rate_ahb, NULL);
  305. DEFINE_CLOCK(mshc_clk, 0, CCM_CGR1, 24, get_rate_mshc, NULL);
  306. DEFINE_CLOCK(owire_clk, 0, CCM_CGR1, 26, get_rate_ipg_per, NULL);
  307. DEFINE_CLOCK(pwm_clk, 0, CCM_CGR1, 28, get_rate_ipg_per, NULL);
  308. DEFINE_CLOCK(rngc_clk, 0, CCM_CGR1, 30, get_rate_ipg, NULL);
  309. DEFINE_CLOCK(rtc_clk, 0, CCM_CGR2, 0, get_rate_ipg, NULL);
  310. DEFINE_CLOCK(rtic_clk, 0, CCM_CGR2, 2, get_rate_ahb, NULL);
  311. DEFINE_CLOCK(scc_clk, 0, CCM_CGR2, 4, get_rate_ipg, NULL);
  312. DEFINE_CLOCK(sdma_clk, 0, CCM_CGR2, 6, NULL, NULL);
  313. DEFINE_CLOCK(spba_clk, 0, CCM_CGR2, 8, get_rate_ipg, NULL);
  314. DEFINE_CLOCK(spdif_clk, 0, CCM_CGR2, 10, NULL, NULL);
  315. DEFINE_CLOCK(ssi1_clk, 0, CCM_CGR2, 12, get_rate_ssi, NULL);
  316. DEFINE_CLOCK(ssi2_clk, 1, CCM_CGR2, 14, get_rate_ssi, NULL);
  317. DEFINE_CLOCK(uart1_clk, 0, CCM_CGR2, 16, get_rate_uart, NULL);
  318. DEFINE_CLOCK(uart2_clk, 1, CCM_CGR2, 18, get_rate_uart, NULL);
  319. DEFINE_CLOCK(uart3_clk, 2, CCM_CGR2, 20, get_rate_uart, NULL);
  320. DEFINE_CLOCK(usbotg_clk, 0, CCM_CGR2, 22, get_rate_otg, NULL);
  321. DEFINE_CLOCK(wdog_clk, 0, CCM_CGR2, 24, NULL, NULL);
  322. DEFINE_CLOCK(max_clk, 0, CCM_CGR2, 26, NULL, NULL);
  323. DEFINE_CLOCK(admux_clk, 0, CCM_CGR2, 30, NULL, NULL);
  324. DEFINE_CLOCK(csi_clk, 0, CCM_CGR3, 0, get_rate_csi, NULL);
  325. DEFINE_CLOCK(iim_clk, 0, CCM_CGR3, 2, NULL, NULL);
  326. DEFINE_CLOCK(gpu2d_clk, 0, CCM_CGR3, 4, NULL, NULL);
  327. #define _REGISTER_CLOCK(d, n, c) \
  328. { \
  329. .dev_id = d, \
  330. .con_id = n, \
  331. .clk = &c, \
  332. },
  333. static struct clk_lookup lookups[] = {
  334. _REGISTER_CLOCK(NULL, "asrc", asrc_clk)
  335. _REGISTER_CLOCK(NULL, "ata", ata_clk)
  336. _REGISTER_CLOCK(NULL, "audmux", audmux_clk)
  337. _REGISTER_CLOCK(NULL, "can", can1_clk)
  338. _REGISTER_CLOCK(NULL, "can", can2_clk)
  339. _REGISTER_CLOCK("spi_imx.0", NULL, cspi1_clk)
  340. _REGISTER_CLOCK("spi_imx.1", NULL, cspi2_clk)
  341. _REGISTER_CLOCK(NULL, "ect", ect_clk)
  342. _REGISTER_CLOCK(NULL, "edio", edio_clk)
  343. _REGISTER_CLOCK(NULL, "emi", emi_clk)
  344. _REGISTER_CLOCK(NULL, "epit", epit1_clk)
  345. _REGISTER_CLOCK(NULL, "epit", epit2_clk)
  346. _REGISTER_CLOCK(NULL, "esai", esai_clk)
  347. _REGISTER_CLOCK(NULL, "sdhc", esdhc1_clk)
  348. _REGISTER_CLOCK(NULL, "sdhc", esdhc2_clk)
  349. _REGISTER_CLOCK(NULL, "sdhc", esdhc3_clk)
  350. _REGISTER_CLOCK("fec.0", NULL, fec_clk)
  351. _REGISTER_CLOCK(NULL, "gpio", gpio1_clk)
  352. _REGISTER_CLOCK(NULL, "gpio", gpio2_clk)
  353. _REGISTER_CLOCK(NULL, "gpio", gpio3_clk)
  354. _REGISTER_CLOCK("gpt.0", NULL, gpt_clk)
  355. _REGISTER_CLOCK("imx-i2c.0", NULL, i2c1_clk)
  356. _REGISTER_CLOCK("imx-i2c.1", NULL, i2c2_clk)
  357. _REGISTER_CLOCK("imx-i2c.2", NULL, i2c3_clk)
  358. _REGISTER_CLOCK(NULL, "iomuxc", iomuxc_clk)
  359. _REGISTER_CLOCK("ipu-core", NULL, ipu_clk)
  360. _REGISTER_CLOCK("mx3_sdc_fb", NULL, ipu_clk)
  361. _REGISTER_CLOCK(NULL, "kpp", kpp_clk)
  362. _REGISTER_CLOCK(NULL, "mlb", mlb_clk)
  363. _REGISTER_CLOCK(NULL, "mshc", mshc_clk)
  364. _REGISTER_CLOCK("mxc_w1", NULL, owire_clk)
  365. _REGISTER_CLOCK(NULL, "pwm", pwm_clk)
  366. _REGISTER_CLOCK(NULL, "rngc", rngc_clk)
  367. _REGISTER_CLOCK(NULL, "rtc", rtc_clk)
  368. _REGISTER_CLOCK(NULL, "rtic", rtic_clk)
  369. _REGISTER_CLOCK(NULL, "scc", scc_clk)
  370. _REGISTER_CLOCK(NULL, "sdma", sdma_clk)
  371. _REGISTER_CLOCK(NULL, "spba", spba_clk)
  372. _REGISTER_CLOCK(NULL, "spdif", spdif_clk)
  373. _REGISTER_CLOCK(NULL, "ssi", ssi1_clk)
  374. _REGISTER_CLOCK(NULL, "ssi", ssi2_clk)
  375. _REGISTER_CLOCK("imx-uart.0", NULL, uart1_clk)
  376. _REGISTER_CLOCK("imx-uart.1", NULL, uart2_clk)
  377. _REGISTER_CLOCK("imx-uart.2", NULL, uart3_clk)
  378. _REGISTER_CLOCK("mxc-ehci.0", "usb", usbotg_clk)
  379. _REGISTER_CLOCK("mxc-ehci.1", "usb", usbotg_clk)
  380. _REGISTER_CLOCK("mxc-ehci.2", "usb", usbotg_clk)
  381. _REGISTER_CLOCK("fsl-usb2-udc", "usb", usbotg_clk)
  382. _REGISTER_CLOCK("imx-wdt.0", NULL, wdog_clk)
  383. _REGISTER_CLOCK(NULL, "max", max_clk)
  384. _REGISTER_CLOCK(NULL, "admux", admux_clk)
  385. _REGISTER_CLOCK(NULL, "csi", csi_clk)
  386. _REGISTER_CLOCK(NULL, "iim", iim_clk)
  387. _REGISTER_CLOCK(NULL, "gpu2d", gpu2d_clk)
  388. };
  389. int __init mx35_clocks_init()
  390. {
  391. int i;
  392. unsigned int ll = 0;
  393. #ifdef CONFIG_DEBUG_LL_CONSOLE
  394. ll = (3 << 16);
  395. #endif
  396. for (i = 0; i < ARRAY_SIZE(lookups); i++)
  397. clkdev_add(&lookups[i]);
  398. /* Turn off all clocks except the ones we need to survive, namely:
  399. * EMI, GPIO1/2/3, GPT, IOMUX, MAX and eventually uart
  400. */
  401. __raw_writel((3 << 18), CCM_BASE + CCM_CGR0);
  402. __raw_writel((3 << 2) | (3 << 4) | (3 << 6) | (3 << 8) | (3 << 16),
  403. CCM_BASE + CCM_CGR1);
  404. __raw_writel((3 << 26) | ll, CCM_BASE + CCM_CGR2);
  405. __raw_writel(0, CCM_BASE + CCM_CGR3);
  406. mxc_timer_init(&gpt_clk, IO_ADDRESS(GPT1_BASE_ADDR), MXC_INT_GPT);
  407. return 0;
  408. }