clock-imx1.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. /*
  2. * Copyright (C) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  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 along
  14. * with this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/list.h>
  20. #include <linux/math64.h>
  21. #include <linux/err.h>
  22. #include <linux/clk.h>
  23. #include <linux/io.h>
  24. #include <linux/clkdev.h>
  25. #include <mach/clock.h>
  26. #include <mach/hardware.h>
  27. #include <mach/common.h>
  28. #define IO_ADDR_CCM(off) (MX1_IO_ADDRESS(MX1_CCM_BASE_ADDR + (off)))
  29. /* CCM register addresses */
  30. #define CCM_CSCR IO_ADDR_CCM(0x0)
  31. #define CCM_MPCTL0 IO_ADDR_CCM(0x4)
  32. #define CCM_SPCTL0 IO_ADDR_CCM(0xc)
  33. #define CCM_PCDR IO_ADDR_CCM(0x20)
  34. #define CCM_CSCR_CLKO_OFFSET 29
  35. #define CCM_CSCR_CLKO_MASK (0x7 << 29)
  36. #define CCM_CSCR_USB_OFFSET 26
  37. #define CCM_CSCR_USB_MASK (0x7 << 26)
  38. #define CCM_CSCR_OSC_EN_SHIFT 17
  39. #define CCM_CSCR_SYSTEM_SEL (1 << 16)
  40. #define CCM_CSCR_BCLK_OFFSET 10
  41. #define CCM_CSCR_BCLK_MASK (0xf << 10)
  42. #define CCM_CSCR_PRESC (1 << 15)
  43. #define CCM_PCDR_PCLK3_OFFSET 16
  44. #define CCM_PCDR_PCLK3_MASK (0x7f << 16)
  45. #define CCM_PCDR_PCLK2_OFFSET 4
  46. #define CCM_PCDR_PCLK2_MASK (0xf << 4)
  47. #define CCM_PCDR_PCLK1_OFFSET 0
  48. #define CCM_PCDR_PCLK1_MASK 0xf
  49. #define IO_ADDR_SCM(off) (MX1_IO_ADDRESS(MX1_SCM_BASE_ADDR + (off)))
  50. /* SCM register addresses */
  51. #define SCM_GCCR IO_ADDR_SCM(0xc)
  52. #define SCM_GCCR_DMA_CLK_EN_OFFSET 3
  53. #define SCM_GCCR_CSI_CLK_EN_OFFSET 2
  54. #define SCM_GCCR_MMA_CLK_EN_OFFSET 1
  55. #define SCM_GCCR_USBD_CLK_EN_OFFSET 0
  56. static int _clk_enable(struct clk *clk)
  57. {
  58. unsigned int reg;
  59. reg = __raw_readl(clk->enable_reg);
  60. reg |= 1 << clk->enable_shift;
  61. __raw_writel(reg, clk->enable_reg);
  62. return 0;
  63. }
  64. static void _clk_disable(struct clk *clk)
  65. {
  66. unsigned int reg;
  67. reg = __raw_readl(clk->enable_reg);
  68. reg &= ~(1 << clk->enable_shift);
  69. __raw_writel(reg, clk->enable_reg);
  70. }
  71. static int _clk_can_use_parent(const struct clk *clk_arr[], unsigned int size,
  72. struct clk *parent)
  73. {
  74. int i;
  75. for (i = 0; i < size; i++)
  76. if (parent == clk_arr[i])
  77. return i;
  78. return -EINVAL;
  79. }
  80. static unsigned long
  81. _clk_simple_round_rate(struct clk *clk, unsigned long rate, unsigned int limit)
  82. {
  83. int div;
  84. unsigned long parent_rate;
  85. parent_rate = clk_get_rate(clk->parent);
  86. div = parent_rate / rate;
  87. if (parent_rate % rate)
  88. div++;
  89. if (div > limit)
  90. div = limit;
  91. return parent_rate / div;
  92. }
  93. static unsigned long _clk_parent_round_rate(struct clk *clk, unsigned long rate)
  94. {
  95. return clk->parent->round_rate(clk->parent, rate);
  96. }
  97. static int _clk_parent_set_rate(struct clk *clk, unsigned long rate)
  98. {
  99. return clk->parent->set_rate(clk->parent, rate);
  100. }
  101. static unsigned long clk16m_get_rate(struct clk *clk)
  102. {
  103. return 16000000;
  104. }
  105. static struct clk clk16m = {
  106. .get_rate = clk16m_get_rate,
  107. .enable = _clk_enable,
  108. .enable_reg = CCM_CSCR,
  109. .enable_shift = CCM_CSCR_OSC_EN_SHIFT,
  110. .disable = _clk_disable,
  111. };
  112. /* in Hz */
  113. static unsigned long clk32_rate;
  114. static unsigned long clk32_get_rate(struct clk *clk)
  115. {
  116. return clk32_rate;
  117. }
  118. static struct clk clk32 = {
  119. .get_rate = clk32_get_rate,
  120. };
  121. static unsigned long clk32_premult_get_rate(struct clk *clk)
  122. {
  123. return clk_get_rate(clk->parent) * 512;
  124. }
  125. static struct clk clk32_premult = {
  126. .parent = &clk32,
  127. .get_rate = clk32_premult_get_rate,
  128. };
  129. static const struct clk *prem_clk_clocks[] = {
  130. &clk32_premult,
  131. &clk16m,
  132. };
  133. static int prem_clk_set_parent(struct clk *clk, struct clk *parent)
  134. {
  135. int i;
  136. unsigned int reg = __raw_readl(CCM_CSCR);
  137. i = _clk_can_use_parent(prem_clk_clocks, ARRAY_SIZE(prem_clk_clocks),
  138. parent);
  139. switch (i) {
  140. case 0:
  141. reg &= ~CCM_CSCR_SYSTEM_SEL;
  142. break;
  143. case 1:
  144. reg |= CCM_CSCR_SYSTEM_SEL;
  145. break;
  146. default:
  147. return i;
  148. }
  149. __raw_writel(reg, CCM_CSCR);
  150. return 0;
  151. }
  152. static struct clk prem_clk = {
  153. .set_parent = prem_clk_set_parent,
  154. };
  155. static unsigned long system_clk_get_rate(struct clk *clk)
  156. {
  157. return mxc_decode_pll(__raw_readl(CCM_SPCTL0),
  158. clk_get_rate(clk->parent));
  159. }
  160. static struct clk system_clk = {
  161. .parent = &prem_clk,
  162. .get_rate = system_clk_get_rate,
  163. };
  164. static unsigned long mcu_clk_get_rate(struct clk *clk)
  165. {
  166. return mxc_decode_pll(__raw_readl(CCM_MPCTL0),
  167. clk_get_rate(clk->parent));
  168. }
  169. static struct clk mcu_clk = {
  170. .parent = &clk32_premult,
  171. .get_rate = mcu_clk_get_rate,
  172. };
  173. static unsigned long fclk_get_rate(struct clk *clk)
  174. {
  175. unsigned long fclk = clk_get_rate(clk->parent);
  176. if (__raw_readl(CCM_CSCR) & CCM_CSCR_PRESC)
  177. fclk /= 2;
  178. return fclk;
  179. }
  180. static struct clk fclk = {
  181. .parent = &mcu_clk,
  182. .get_rate = fclk_get_rate,
  183. };
  184. /*
  185. * get hclk ( SDRAM, CSI, Memory Stick, I2C, DMA )
  186. */
  187. static unsigned long hclk_get_rate(struct clk *clk)
  188. {
  189. return clk_get_rate(clk->parent) / (((__raw_readl(CCM_CSCR) &
  190. CCM_CSCR_BCLK_MASK) >> CCM_CSCR_BCLK_OFFSET) + 1);
  191. }
  192. static unsigned long hclk_round_rate(struct clk *clk, unsigned long rate)
  193. {
  194. return _clk_simple_round_rate(clk, rate, 16);
  195. }
  196. static int hclk_set_rate(struct clk *clk, unsigned long rate)
  197. {
  198. unsigned int div;
  199. unsigned int reg;
  200. unsigned long parent_rate;
  201. parent_rate = clk_get_rate(clk->parent);
  202. div = parent_rate / rate;
  203. if (div > 16 || div < 1 || ((parent_rate / div) != rate))
  204. return -EINVAL;
  205. div--;
  206. reg = __raw_readl(CCM_CSCR);
  207. reg &= ~CCM_CSCR_BCLK_MASK;
  208. reg |= div << CCM_CSCR_BCLK_OFFSET;
  209. __raw_writel(reg, CCM_CSCR);
  210. return 0;
  211. }
  212. static struct clk hclk = {
  213. .parent = &system_clk,
  214. .get_rate = hclk_get_rate,
  215. .round_rate = hclk_round_rate,
  216. .set_rate = hclk_set_rate,
  217. };
  218. static unsigned long clk48m_get_rate(struct clk *clk)
  219. {
  220. return clk_get_rate(clk->parent) / (((__raw_readl(CCM_CSCR) &
  221. CCM_CSCR_USB_MASK) >> CCM_CSCR_USB_OFFSET) + 1);
  222. }
  223. static unsigned long clk48m_round_rate(struct clk *clk, unsigned long rate)
  224. {
  225. return _clk_simple_round_rate(clk, rate, 8);
  226. }
  227. static int clk48m_set_rate(struct clk *clk, unsigned long rate)
  228. {
  229. unsigned int div;
  230. unsigned int reg;
  231. unsigned long parent_rate;
  232. parent_rate = clk_get_rate(clk->parent);
  233. div = parent_rate / rate;
  234. if (div > 8 || div < 1 || ((parent_rate / div) != rate))
  235. return -EINVAL;
  236. div--;
  237. reg = __raw_readl(CCM_CSCR);
  238. reg &= ~CCM_CSCR_USB_MASK;
  239. reg |= div << CCM_CSCR_USB_OFFSET;
  240. __raw_writel(reg, CCM_CSCR);
  241. return 0;
  242. }
  243. static struct clk clk48m = {
  244. .parent = &system_clk,
  245. .get_rate = clk48m_get_rate,
  246. .round_rate = clk48m_round_rate,
  247. .set_rate = clk48m_set_rate,
  248. };
  249. /*
  250. * get peripheral clock 1 ( UART[12], Timer[12], PWM )
  251. */
  252. static unsigned long perclk1_get_rate(struct clk *clk)
  253. {
  254. return clk_get_rate(clk->parent) / (((__raw_readl(CCM_PCDR) &
  255. CCM_PCDR_PCLK1_MASK) >> CCM_PCDR_PCLK1_OFFSET) + 1);
  256. }
  257. static unsigned long perclk1_round_rate(struct clk *clk, unsigned long rate)
  258. {
  259. return _clk_simple_round_rate(clk, rate, 16);
  260. }
  261. static int perclk1_set_rate(struct clk *clk, unsigned long rate)
  262. {
  263. unsigned int div;
  264. unsigned int reg;
  265. unsigned long parent_rate;
  266. parent_rate = clk_get_rate(clk->parent);
  267. div = parent_rate / rate;
  268. if (div > 16 || div < 1 || ((parent_rate / div) != rate))
  269. return -EINVAL;
  270. div--;
  271. reg = __raw_readl(CCM_PCDR);
  272. reg &= ~CCM_PCDR_PCLK1_MASK;
  273. reg |= div << CCM_PCDR_PCLK1_OFFSET;
  274. __raw_writel(reg, CCM_PCDR);
  275. return 0;
  276. }
  277. /*
  278. * get peripheral clock 2 ( LCD, SD, SPI[12] )
  279. */
  280. static unsigned long perclk2_get_rate(struct clk *clk)
  281. {
  282. return clk_get_rate(clk->parent) / (((__raw_readl(CCM_PCDR) &
  283. CCM_PCDR_PCLK2_MASK) >> CCM_PCDR_PCLK2_OFFSET) + 1);
  284. }
  285. static unsigned long perclk2_round_rate(struct clk *clk, unsigned long rate)
  286. {
  287. return _clk_simple_round_rate(clk, rate, 16);
  288. }
  289. static int perclk2_set_rate(struct clk *clk, unsigned long rate)
  290. {
  291. unsigned int div;
  292. unsigned int reg;
  293. unsigned long parent_rate;
  294. parent_rate = clk_get_rate(clk->parent);
  295. div = parent_rate / rate;
  296. if (div > 16 || div < 1 || ((parent_rate / div) != rate))
  297. return -EINVAL;
  298. div--;
  299. reg = __raw_readl(CCM_PCDR);
  300. reg &= ~CCM_PCDR_PCLK2_MASK;
  301. reg |= div << CCM_PCDR_PCLK2_OFFSET;
  302. __raw_writel(reg, CCM_PCDR);
  303. return 0;
  304. }
  305. /*
  306. * get peripheral clock 3 ( SSI )
  307. */
  308. static unsigned long perclk3_get_rate(struct clk *clk)
  309. {
  310. return clk_get_rate(clk->parent) / (((__raw_readl(CCM_PCDR) &
  311. CCM_PCDR_PCLK3_MASK) >> CCM_PCDR_PCLK3_OFFSET) + 1);
  312. }
  313. static unsigned long perclk3_round_rate(struct clk *clk, unsigned long rate)
  314. {
  315. return _clk_simple_round_rate(clk, rate, 128);
  316. }
  317. static int perclk3_set_rate(struct clk *clk, unsigned long rate)
  318. {
  319. unsigned int div;
  320. unsigned int reg;
  321. unsigned long parent_rate;
  322. parent_rate = clk_get_rate(clk->parent);
  323. div = parent_rate / rate;
  324. if (div > 128 || div < 1 || ((parent_rate / div) != rate))
  325. return -EINVAL;
  326. div--;
  327. reg = __raw_readl(CCM_PCDR);
  328. reg &= ~CCM_PCDR_PCLK3_MASK;
  329. reg |= div << CCM_PCDR_PCLK3_OFFSET;
  330. __raw_writel(reg, CCM_PCDR);
  331. return 0;
  332. }
  333. static struct clk perclk[] = {
  334. {
  335. .id = 0,
  336. .parent = &system_clk,
  337. .get_rate = perclk1_get_rate,
  338. .round_rate = perclk1_round_rate,
  339. .set_rate = perclk1_set_rate,
  340. }, {
  341. .id = 1,
  342. .parent = &system_clk,
  343. .get_rate = perclk2_get_rate,
  344. .round_rate = perclk2_round_rate,
  345. .set_rate = perclk2_set_rate,
  346. }, {
  347. .id = 2,
  348. .parent = &system_clk,
  349. .get_rate = perclk3_get_rate,
  350. .round_rate = perclk3_round_rate,
  351. .set_rate = perclk3_set_rate,
  352. }
  353. };
  354. static const struct clk *clko_clocks[] = {
  355. &perclk[0],
  356. &hclk,
  357. &clk48m,
  358. &clk16m,
  359. &prem_clk,
  360. &fclk,
  361. };
  362. static int clko_set_parent(struct clk *clk, struct clk *parent)
  363. {
  364. int i;
  365. unsigned int reg;
  366. i = _clk_can_use_parent(clko_clocks, ARRAY_SIZE(clko_clocks), parent);
  367. if (i < 0)
  368. return i;
  369. reg = __raw_readl(CCM_CSCR) & ~CCM_CSCR_CLKO_MASK;
  370. reg |= i << CCM_CSCR_CLKO_OFFSET;
  371. __raw_writel(reg, CCM_CSCR);
  372. if (clko_clocks[i]->set_rate && clko_clocks[i]->round_rate) {
  373. clk->set_rate = _clk_parent_set_rate;
  374. clk->round_rate = _clk_parent_round_rate;
  375. } else {
  376. clk->set_rate = NULL;
  377. clk->round_rate = NULL;
  378. }
  379. return 0;
  380. }
  381. static struct clk clko_clk = {
  382. .set_parent = clko_set_parent,
  383. };
  384. static struct clk dma_clk = {
  385. .parent = &hclk,
  386. .round_rate = _clk_parent_round_rate,
  387. .set_rate = _clk_parent_set_rate,
  388. .enable = _clk_enable,
  389. .enable_reg = SCM_GCCR,
  390. .enable_shift = SCM_GCCR_DMA_CLK_EN_OFFSET,
  391. .disable = _clk_disable,
  392. };
  393. static struct clk csi_clk = {
  394. .parent = &hclk,
  395. .round_rate = _clk_parent_round_rate,
  396. .set_rate = _clk_parent_set_rate,
  397. .enable = _clk_enable,
  398. .enable_reg = SCM_GCCR,
  399. .enable_shift = SCM_GCCR_CSI_CLK_EN_OFFSET,
  400. .disable = _clk_disable,
  401. };
  402. static struct clk mma_clk = {
  403. .parent = &hclk,
  404. .round_rate = _clk_parent_round_rate,
  405. .set_rate = _clk_parent_set_rate,
  406. .enable = _clk_enable,
  407. .enable_reg = SCM_GCCR,
  408. .enable_shift = SCM_GCCR_MMA_CLK_EN_OFFSET,
  409. .disable = _clk_disable,
  410. };
  411. static struct clk usbd_clk = {
  412. .parent = &clk48m,
  413. .round_rate = _clk_parent_round_rate,
  414. .set_rate = _clk_parent_set_rate,
  415. .enable = _clk_enable,
  416. .enable_reg = SCM_GCCR,
  417. .enable_shift = SCM_GCCR_USBD_CLK_EN_OFFSET,
  418. .disable = _clk_disable,
  419. };
  420. static struct clk gpt_clk = {
  421. .parent = &perclk[0],
  422. .round_rate = _clk_parent_round_rate,
  423. .set_rate = _clk_parent_set_rate,
  424. };
  425. static struct clk uart_clk = {
  426. .parent = &perclk[0],
  427. .round_rate = _clk_parent_round_rate,
  428. .set_rate = _clk_parent_set_rate,
  429. };
  430. static struct clk i2c_clk = {
  431. .parent = &hclk,
  432. .round_rate = _clk_parent_round_rate,
  433. .set_rate = _clk_parent_set_rate,
  434. };
  435. static struct clk spi_clk = {
  436. .parent = &perclk[1],
  437. .round_rate = _clk_parent_round_rate,
  438. .set_rate = _clk_parent_set_rate,
  439. };
  440. static struct clk sdhc_clk = {
  441. .parent = &perclk[1],
  442. .round_rate = _clk_parent_round_rate,
  443. .set_rate = _clk_parent_set_rate,
  444. };
  445. static struct clk lcdc_clk = {
  446. .parent = &perclk[1],
  447. .round_rate = _clk_parent_round_rate,
  448. .set_rate = _clk_parent_set_rate,
  449. };
  450. static struct clk mshc_clk = {
  451. .parent = &hclk,
  452. .round_rate = _clk_parent_round_rate,
  453. .set_rate = _clk_parent_set_rate,
  454. };
  455. static struct clk ssi_clk = {
  456. .parent = &perclk[2],
  457. .round_rate = _clk_parent_round_rate,
  458. .set_rate = _clk_parent_set_rate,
  459. };
  460. static struct clk rtc_clk = {
  461. .parent = &clk32,
  462. };
  463. #define _REGISTER_CLOCK(d, n, c) \
  464. { \
  465. .dev_id = d, \
  466. .con_id = n, \
  467. .clk = &c, \
  468. },
  469. static struct clk_lookup lookups[] __initdata = {
  470. _REGISTER_CLOCK(NULL, "dma", dma_clk)
  471. _REGISTER_CLOCK("mx1-camera.0", NULL, csi_clk)
  472. _REGISTER_CLOCK(NULL, "mma", mma_clk)
  473. _REGISTER_CLOCK("imx_udc.0", NULL, usbd_clk)
  474. _REGISTER_CLOCK(NULL, "gpt", gpt_clk)
  475. _REGISTER_CLOCK("imx-uart.0", NULL, uart_clk)
  476. _REGISTER_CLOCK("imx-uart.1", NULL, uart_clk)
  477. _REGISTER_CLOCK("imx-uart.2", NULL, uart_clk)
  478. _REGISTER_CLOCK("imx-i2c.0", NULL, i2c_clk)
  479. _REGISTER_CLOCK("imx1-cspi.0", NULL, spi_clk)
  480. _REGISTER_CLOCK("imx1-cspi.1", NULL, spi_clk)
  481. _REGISTER_CLOCK("imx-mmc.0", NULL, sdhc_clk)
  482. _REGISTER_CLOCK("imx-fb.0", NULL, lcdc_clk)
  483. _REGISTER_CLOCK(NULL, "mshc", mshc_clk)
  484. _REGISTER_CLOCK(NULL, "ssi", ssi_clk)
  485. _REGISTER_CLOCK("mxc_rtc.0", NULL, rtc_clk)
  486. };
  487. int __init mx1_clocks_init(unsigned long fref)
  488. {
  489. unsigned int reg;
  490. /* disable clocks we are able to */
  491. __raw_writel(0, SCM_GCCR);
  492. clk32_rate = fref;
  493. reg = __raw_readl(CCM_CSCR);
  494. /* detect clock reference for system PLL */
  495. if (reg & CCM_CSCR_SYSTEM_SEL) {
  496. prem_clk.parent = &clk16m;
  497. } else {
  498. /* ensure that oscillator is disabled */
  499. reg &= ~(1 << CCM_CSCR_OSC_EN_SHIFT);
  500. __raw_writel(reg, CCM_CSCR);
  501. prem_clk.parent = &clk32_premult;
  502. }
  503. /* detect reference for CLKO */
  504. reg = (reg & CCM_CSCR_CLKO_MASK) >> CCM_CSCR_CLKO_OFFSET;
  505. clko_clk.parent = (struct clk *)clko_clocks[reg];
  506. clkdev_add_table(lookups, ARRAY_SIZE(lookups));
  507. clk_enable(&hclk);
  508. clk_enable(&fclk);
  509. mxc_timer_init(&gpt_clk, MX1_IO_ADDRESS(MX1_TIM1_BASE_ADDR),
  510. MX1_TIM1_INT);
  511. return 0;
  512. }