clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Copyright (C) 2007,2008 Freescale Semiconductor, Inc. All rights reserved.
  3. *
  4. * Author: John Rigby <jrigby@freescale.com>
  5. *
  6. * Implements the clk api defined in include/linux/clk.h
  7. *
  8. * Original based on linux/arch/arm/mach-integrator/clock.c
  9. *
  10. * Copyright (C) 2004 ARM Limited.
  11. * Written by Deep Blue Solutions Limited.
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License version 2 as
  15. * published by the Free Software Foundation.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/list.h>
  19. #include <linux/errno.h>
  20. #include <linux/err.h>
  21. #include <linux/string.h>
  22. #include <linux/clk.h>
  23. #include <linux/mutex.h>
  24. #include <linux/io.h>
  25. #include <linux/of_platform.h>
  26. #include <asm/mpc5xxx.h>
  27. #include <asm/clk_interface.h>
  28. #undef CLK_DEBUG
  29. static int clocks_initialized;
  30. #define CLK_HAS_RATE 0x1 /* has rate in MHz */
  31. #define CLK_HAS_CTRL 0x2 /* has control reg and bit */
  32. struct clk {
  33. struct list_head node;
  34. char name[32];
  35. int flags;
  36. struct device *dev;
  37. unsigned long rate;
  38. struct module *owner;
  39. void (*calc) (struct clk *);
  40. struct clk *parent;
  41. int reg, bit; /* CLK_HAS_CTRL */
  42. int div_shift; /* only used by generic_div_clk_calc */
  43. };
  44. static LIST_HEAD(clocks);
  45. static DEFINE_MUTEX(clocks_mutex);
  46. static struct clk *mpc5121_clk_get(struct device *dev, const char *id)
  47. {
  48. struct clk *p, *clk = ERR_PTR(-ENOENT);
  49. int dev_match = 0;
  50. int id_match = 0;
  51. if (dev == NULL || id == NULL)
  52. return NULL;
  53. mutex_lock(&clocks_mutex);
  54. list_for_each_entry(p, &clocks, node) {
  55. if (dev == p->dev)
  56. dev_match++;
  57. if (strcmp(id, p->name) == 0)
  58. id_match++;
  59. if ((dev_match || id_match) && try_module_get(p->owner)) {
  60. clk = p;
  61. break;
  62. }
  63. }
  64. mutex_unlock(&clocks_mutex);
  65. return clk;
  66. }
  67. #ifdef CLK_DEBUG
  68. static void dump_clocks(void)
  69. {
  70. struct clk *p;
  71. mutex_lock(&clocks_mutex);
  72. printk(KERN_INFO "CLOCKS:\n");
  73. list_for_each_entry(p, &clocks, node) {
  74. pr_info(" %s=%ld", p->name, p->rate);
  75. if (p->parent)
  76. pr_cont(" %s=%ld", p->parent->name,
  77. p->parent->rate);
  78. if (p->flags & CLK_HAS_CTRL)
  79. pr_cont(" reg/bit=%d/%d", p->reg, p->bit);
  80. pr_cont("\n");
  81. }
  82. mutex_unlock(&clocks_mutex);
  83. }
  84. #define DEBUG_CLK_DUMP() dump_clocks()
  85. #else
  86. #define DEBUG_CLK_DUMP()
  87. #endif
  88. static void mpc5121_clk_put(struct clk *clk)
  89. {
  90. module_put(clk->owner);
  91. }
  92. #define NRPSC 12
  93. struct mpc512x_clockctl {
  94. u32 spmr; /* System PLL Mode Reg */
  95. u32 sccr[2]; /* System Clk Ctrl Reg 1 & 2 */
  96. u32 scfr1; /* System Clk Freq Reg 1 */
  97. u32 scfr2; /* System Clk Freq Reg 2 */
  98. u32 reserved;
  99. u32 bcr; /* Bread Crumb Reg */
  100. u32 pccr[NRPSC]; /* PSC Clk Ctrl Reg 0-11 */
  101. u32 spccr; /* SPDIF Clk Ctrl Reg */
  102. u32 cccr; /* CFM Clk Ctrl Reg */
  103. u32 dccr; /* DIU Clk Cnfg Reg */
  104. };
  105. struct mpc512x_clockctl __iomem *clockctl;
  106. static int mpc5121_clk_enable(struct clk *clk)
  107. {
  108. unsigned int mask;
  109. if (clk->flags & CLK_HAS_CTRL) {
  110. mask = in_be32(&clockctl->sccr[clk->reg]);
  111. mask |= 1 << clk->bit;
  112. out_be32(&clockctl->sccr[clk->reg], mask);
  113. }
  114. return 0;
  115. }
  116. static void mpc5121_clk_disable(struct clk *clk)
  117. {
  118. unsigned int mask;
  119. if (clk->flags & CLK_HAS_CTRL) {
  120. mask = in_be32(&clockctl->sccr[clk->reg]);
  121. mask &= ~(1 << clk->bit);
  122. out_be32(&clockctl->sccr[clk->reg], mask);
  123. }
  124. }
  125. static unsigned long mpc5121_clk_get_rate(struct clk *clk)
  126. {
  127. if (clk->flags & CLK_HAS_RATE)
  128. return clk->rate;
  129. else
  130. return 0;
  131. }
  132. static long mpc5121_clk_round_rate(struct clk *clk, unsigned long rate)
  133. {
  134. return rate;
  135. }
  136. static int mpc5121_clk_set_rate(struct clk *clk, unsigned long rate)
  137. {
  138. return 0;
  139. }
  140. static int clk_register(struct clk *clk)
  141. {
  142. mutex_lock(&clocks_mutex);
  143. list_add(&clk->node, &clocks);
  144. mutex_unlock(&clocks_mutex);
  145. return 0;
  146. }
  147. static unsigned long spmf_mult(void)
  148. {
  149. /*
  150. * Convert spmf to multiplier
  151. */
  152. static int spmf_to_mult[] = {
  153. 68, 1, 12, 16,
  154. 20, 24, 28, 32,
  155. 36, 40, 44, 48,
  156. 52, 56, 60, 64
  157. };
  158. int spmf = (clockctl->spmr >> 24) & 0xf;
  159. return spmf_to_mult[spmf];
  160. }
  161. static unsigned long sysdiv_div_x_2(void)
  162. {
  163. /*
  164. * Convert sysdiv to divisor x 2
  165. * Some divisors have fractional parts so
  166. * multiply by 2 then divide by this value
  167. */
  168. static int sysdiv_to_div_x_2[] = {
  169. 4, 5, 6, 7,
  170. 8, 9, 10, 14,
  171. 12, 16, 18, 22,
  172. 20, 24, 26, 30,
  173. 28, 32, 34, 38,
  174. 36, 40, 42, 46,
  175. 44, 48, 50, 54,
  176. 52, 56, 58, 62,
  177. 60, 64, 66,
  178. };
  179. int sysdiv = (clockctl->scfr2 >> 26) & 0x3f;
  180. return sysdiv_to_div_x_2[sysdiv];
  181. }
  182. static unsigned long ref_to_sys(unsigned long rate)
  183. {
  184. rate *= spmf_mult();
  185. rate *= 2;
  186. rate /= sysdiv_div_x_2();
  187. return rate;
  188. }
  189. static unsigned long sys_to_ref(unsigned long rate)
  190. {
  191. rate *= sysdiv_div_x_2();
  192. rate /= 2;
  193. rate /= spmf_mult();
  194. return rate;
  195. }
  196. static long ips_to_ref(unsigned long rate)
  197. {
  198. int ips_div = (clockctl->scfr1 >> 23) & 0x7;
  199. rate *= ips_div; /* csb_clk = ips_clk * ips_div */
  200. rate *= 2; /* sys_clk = csb_clk * 2 */
  201. return sys_to_ref(rate);
  202. }
  203. static unsigned long devtree_getfreq(char *clockname)
  204. {
  205. struct device_node *np;
  206. const unsigned int *prop;
  207. unsigned int val = 0;
  208. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-immr");
  209. if (np) {
  210. prop = of_get_property(np, clockname, NULL);
  211. if (prop)
  212. val = *prop;
  213. of_node_put(np);
  214. }
  215. return val;
  216. }
  217. static void ref_clk_calc(struct clk *clk)
  218. {
  219. unsigned long rate;
  220. rate = devtree_getfreq("bus-frequency");
  221. if (rate == 0) {
  222. printk(KERN_ERR "No bus-frequency in dev tree\n");
  223. clk->rate = 0;
  224. return;
  225. }
  226. clk->rate = ips_to_ref(rate);
  227. }
  228. static struct clk ref_clk = {
  229. .name = "ref_clk",
  230. .calc = ref_clk_calc,
  231. };
  232. static void sys_clk_calc(struct clk *clk)
  233. {
  234. clk->rate = ref_to_sys(ref_clk.rate);
  235. }
  236. static struct clk sys_clk = {
  237. .name = "sys_clk",
  238. .calc = sys_clk_calc,
  239. };
  240. static void diu_clk_calc(struct clk *clk)
  241. {
  242. int diudiv_x_2 = clockctl->scfr1 & 0xff;
  243. unsigned long rate;
  244. rate = sys_clk.rate;
  245. rate *= 2;
  246. rate /= diudiv_x_2;
  247. clk->rate = rate;
  248. }
  249. static void viu_clk_calc(struct clk *clk)
  250. {
  251. unsigned long rate;
  252. rate = sys_clk.rate;
  253. rate /= 2;
  254. clk->rate = rate;
  255. }
  256. static void half_clk_calc(struct clk *clk)
  257. {
  258. clk->rate = clk->parent->rate / 2;
  259. }
  260. static void generic_div_clk_calc(struct clk *clk)
  261. {
  262. int div = (clockctl->scfr1 >> clk->div_shift) & 0x7;
  263. clk->rate = clk->parent->rate / div;
  264. }
  265. static void unity_clk_calc(struct clk *clk)
  266. {
  267. clk->rate = clk->parent->rate;
  268. }
  269. static struct clk csb_clk = {
  270. .name = "csb_clk",
  271. .calc = half_clk_calc,
  272. .parent = &sys_clk,
  273. };
  274. static void e300_clk_calc(struct clk *clk)
  275. {
  276. int spmf = (clockctl->spmr >> 16) & 0xf;
  277. int ratex2 = clk->parent->rate * spmf;
  278. clk->rate = ratex2 / 2;
  279. }
  280. static struct clk e300_clk = {
  281. .name = "e300_clk",
  282. .calc = e300_clk_calc,
  283. .parent = &csb_clk,
  284. };
  285. static struct clk ips_clk = {
  286. .name = "ips_clk",
  287. .calc = generic_div_clk_calc,
  288. .parent = &csb_clk,
  289. .div_shift = 23,
  290. };
  291. /*
  292. * Clocks controlled by SCCR1 (.reg = 0)
  293. */
  294. static struct clk lpc_clk = {
  295. .name = "lpc_clk",
  296. .flags = CLK_HAS_CTRL,
  297. .reg = 0,
  298. .bit = 30,
  299. .calc = generic_div_clk_calc,
  300. .parent = &ips_clk,
  301. .div_shift = 11,
  302. };
  303. static struct clk nfc_clk = {
  304. .name = "nfc_clk",
  305. .flags = CLK_HAS_CTRL,
  306. .reg = 0,
  307. .bit = 29,
  308. .calc = generic_div_clk_calc,
  309. .parent = &ips_clk,
  310. .div_shift = 8,
  311. };
  312. static struct clk pata_clk = {
  313. .name = "pata_clk",
  314. .flags = CLK_HAS_CTRL,
  315. .reg = 0,
  316. .bit = 28,
  317. .calc = unity_clk_calc,
  318. .parent = &ips_clk,
  319. };
  320. /*
  321. * PSC clocks (bits 27 - 16)
  322. * are setup elsewhere
  323. */
  324. static struct clk sata_clk = {
  325. .name = "sata_clk",
  326. .flags = CLK_HAS_CTRL,
  327. .reg = 0,
  328. .bit = 14,
  329. .calc = unity_clk_calc,
  330. .parent = &ips_clk,
  331. };
  332. static struct clk fec_clk = {
  333. .name = "fec_clk",
  334. .flags = CLK_HAS_CTRL,
  335. .reg = 0,
  336. .bit = 13,
  337. .calc = unity_clk_calc,
  338. .parent = &ips_clk,
  339. };
  340. static struct clk pci_clk = {
  341. .name = "pci_clk",
  342. .flags = CLK_HAS_CTRL,
  343. .reg = 0,
  344. .bit = 11,
  345. .calc = generic_div_clk_calc,
  346. .parent = &csb_clk,
  347. .div_shift = 20,
  348. };
  349. /*
  350. * Clocks controlled by SCCR2 (.reg = 1)
  351. */
  352. static struct clk diu_clk = {
  353. .name = "diu_clk",
  354. .flags = CLK_HAS_CTRL,
  355. .reg = 1,
  356. .bit = 31,
  357. .calc = diu_clk_calc,
  358. };
  359. static struct clk viu_clk = {
  360. .name = "viu_clk",
  361. .flags = CLK_HAS_CTRL,
  362. .reg = 1,
  363. .bit = 18,
  364. .calc = viu_clk_calc,
  365. };
  366. static struct clk axe_clk = {
  367. .name = "axe_clk",
  368. .flags = CLK_HAS_CTRL,
  369. .reg = 1,
  370. .bit = 30,
  371. .calc = unity_clk_calc,
  372. .parent = &csb_clk,
  373. };
  374. static struct clk usb1_clk = {
  375. .name = "usb1_clk",
  376. .flags = CLK_HAS_CTRL,
  377. .reg = 1,
  378. .bit = 28,
  379. .calc = unity_clk_calc,
  380. .parent = &csb_clk,
  381. };
  382. static struct clk usb2_clk = {
  383. .name = "usb2_clk",
  384. .flags = CLK_HAS_CTRL,
  385. .reg = 1,
  386. .bit = 27,
  387. .calc = unity_clk_calc,
  388. .parent = &csb_clk,
  389. };
  390. static struct clk i2c_clk = {
  391. .name = "i2c_clk",
  392. .flags = CLK_HAS_CTRL,
  393. .reg = 1,
  394. .bit = 26,
  395. .calc = unity_clk_calc,
  396. .parent = &ips_clk,
  397. };
  398. static struct clk mscan_clk = {
  399. .name = "mscan_clk",
  400. .flags = CLK_HAS_CTRL,
  401. .reg = 1,
  402. .bit = 25,
  403. .calc = unity_clk_calc,
  404. .parent = &ips_clk,
  405. };
  406. static struct clk sdhc_clk = {
  407. .name = "sdhc_clk",
  408. .flags = CLK_HAS_CTRL,
  409. .reg = 1,
  410. .bit = 24,
  411. .calc = unity_clk_calc,
  412. .parent = &ips_clk,
  413. };
  414. static struct clk mbx_bus_clk = {
  415. .name = "mbx_bus_clk",
  416. .flags = CLK_HAS_CTRL,
  417. .reg = 1,
  418. .bit = 22,
  419. .calc = half_clk_calc,
  420. .parent = &csb_clk,
  421. };
  422. static struct clk mbx_clk = {
  423. .name = "mbx_clk",
  424. .flags = CLK_HAS_CTRL,
  425. .reg = 1,
  426. .bit = 21,
  427. .calc = unity_clk_calc,
  428. .parent = &csb_clk,
  429. };
  430. static struct clk mbx_3d_clk = {
  431. .name = "mbx_3d_clk",
  432. .flags = CLK_HAS_CTRL,
  433. .reg = 1,
  434. .bit = 20,
  435. .calc = generic_div_clk_calc,
  436. .parent = &mbx_bus_clk,
  437. .div_shift = 14,
  438. };
  439. static void psc_mclk_in_calc(struct clk *clk)
  440. {
  441. clk->rate = devtree_getfreq("psc_mclk_in");
  442. if (!clk->rate)
  443. clk->rate = 25000000;
  444. }
  445. static struct clk psc_mclk_in = {
  446. .name = "psc_mclk_in",
  447. .calc = psc_mclk_in_calc,
  448. };
  449. static struct clk spdif_txclk = {
  450. .name = "spdif_txclk",
  451. .flags = CLK_HAS_CTRL,
  452. .reg = 1,
  453. .bit = 23,
  454. };
  455. static struct clk spdif_rxclk = {
  456. .name = "spdif_rxclk",
  457. .flags = CLK_HAS_CTRL,
  458. .reg = 1,
  459. .bit = 23,
  460. };
  461. static void ac97_clk_calc(struct clk *clk)
  462. {
  463. /* ac97 bit clock is always 24.567 MHz */
  464. clk->rate = 24567000;
  465. }
  466. static struct clk ac97_clk = {
  467. .name = "ac97_clk_in",
  468. .calc = ac97_clk_calc,
  469. };
  470. struct clk *rate_clks[] = {
  471. &ref_clk,
  472. &sys_clk,
  473. &diu_clk,
  474. &viu_clk,
  475. &csb_clk,
  476. &e300_clk,
  477. &ips_clk,
  478. &fec_clk,
  479. &sata_clk,
  480. &pata_clk,
  481. &nfc_clk,
  482. &lpc_clk,
  483. &mbx_bus_clk,
  484. &mbx_clk,
  485. &mbx_3d_clk,
  486. &axe_clk,
  487. &usb1_clk,
  488. &usb2_clk,
  489. &i2c_clk,
  490. &mscan_clk,
  491. &sdhc_clk,
  492. &pci_clk,
  493. &psc_mclk_in,
  494. &spdif_txclk,
  495. &spdif_rxclk,
  496. &ac97_clk,
  497. NULL
  498. };
  499. static void rate_clk_init(struct clk *clk)
  500. {
  501. if (clk->calc) {
  502. clk->calc(clk);
  503. clk->flags |= CLK_HAS_RATE;
  504. clk_register(clk);
  505. } else {
  506. printk(KERN_WARNING
  507. "Could not initialize clk %s without a calc routine\n",
  508. clk->name);
  509. }
  510. }
  511. static void rate_clks_init(void)
  512. {
  513. struct clk **cpp, *clk;
  514. cpp = rate_clks;
  515. while ((clk = *cpp++))
  516. rate_clk_init(clk);
  517. }
  518. /*
  519. * There are two clk enable registers with 32 enable bits each
  520. * psc clocks and device clocks are all stored in dev_clks
  521. */
  522. struct clk dev_clks[2][32];
  523. /*
  524. * Given a psc number return the dev_clk
  525. * associated with it
  526. */
  527. static struct clk *psc_dev_clk(int pscnum)
  528. {
  529. int reg, bit;
  530. struct clk *clk;
  531. reg = 0;
  532. bit = 27 - pscnum;
  533. clk = &dev_clks[reg][bit];
  534. clk->reg = 0;
  535. clk->bit = bit;
  536. return clk;
  537. }
  538. /*
  539. * PSC clock rate calculation
  540. */
  541. static void psc_calc_rate(struct clk *clk, int pscnum, struct device_node *np)
  542. {
  543. unsigned long mclk_src = sys_clk.rate;
  544. unsigned long mclk_div;
  545. /*
  546. * Can only change value of mclk divider
  547. * when the divider is disabled.
  548. *
  549. * Zero is not a valid divider so minimum
  550. * divider is 1
  551. *
  552. * disable/set divider/enable
  553. */
  554. out_be32(&clockctl->pccr[pscnum], 0);
  555. out_be32(&clockctl->pccr[pscnum], 0x00020000);
  556. out_be32(&clockctl->pccr[pscnum], 0x00030000);
  557. if (clockctl->pccr[pscnum] & 0x80) {
  558. clk->rate = spdif_rxclk.rate;
  559. return;
  560. }
  561. switch ((clockctl->pccr[pscnum] >> 14) & 0x3) {
  562. case 0:
  563. mclk_src = sys_clk.rate;
  564. break;
  565. case 1:
  566. mclk_src = ref_clk.rate;
  567. break;
  568. case 2:
  569. mclk_src = psc_mclk_in.rate;
  570. break;
  571. case 3:
  572. mclk_src = spdif_txclk.rate;
  573. break;
  574. }
  575. mclk_div = ((clockctl->pccr[pscnum] >> 17) & 0x7fff) + 1;
  576. clk->rate = mclk_src / mclk_div;
  577. }
  578. /*
  579. * Find all psc nodes in device tree and assign a clock
  580. * with name "psc%d_mclk" and dev pointing at the device
  581. * returned from of_find_device_by_node
  582. */
  583. static void psc_clks_init(void)
  584. {
  585. struct device_node *np;
  586. const u32 *cell_index;
  587. struct platform_device *ofdev;
  588. for_each_compatible_node(np, NULL, "fsl,mpc5121-psc") {
  589. cell_index = of_get_property(np, "cell-index", NULL);
  590. if (cell_index) {
  591. int pscnum = *cell_index;
  592. struct clk *clk = psc_dev_clk(pscnum);
  593. clk->flags = CLK_HAS_RATE | CLK_HAS_CTRL;
  594. ofdev = of_find_device_by_node(np);
  595. clk->dev = &ofdev->dev;
  596. /*
  597. * AC97 is special rate clock does
  598. * not go through normal path
  599. */
  600. if (strcmp("ac97", np->name) == 0)
  601. clk->rate = ac97_clk.rate;
  602. else
  603. psc_calc_rate(clk, pscnum, np);
  604. sprintf(clk->name, "psc%d_mclk", pscnum);
  605. clk_register(clk);
  606. clk_enable(clk);
  607. }
  608. }
  609. }
  610. static struct clk_interface mpc5121_clk_functions = {
  611. .clk_get = mpc5121_clk_get,
  612. .clk_enable = mpc5121_clk_enable,
  613. .clk_disable = mpc5121_clk_disable,
  614. .clk_get_rate = mpc5121_clk_get_rate,
  615. .clk_put = mpc5121_clk_put,
  616. .clk_round_rate = mpc5121_clk_round_rate,
  617. .clk_set_rate = mpc5121_clk_set_rate,
  618. .clk_set_parent = NULL,
  619. .clk_get_parent = NULL,
  620. };
  621. int __init mpc5121_clk_init(void)
  622. {
  623. struct device_node *np;
  624. np = of_find_compatible_node(NULL, NULL, "fsl,mpc5121-clock");
  625. if (np) {
  626. clockctl = of_iomap(np, 0);
  627. of_node_put(np);
  628. }
  629. if (!clockctl) {
  630. printk(KERN_ERR "Could not map clock control registers\n");
  631. return 0;
  632. }
  633. rate_clks_init();
  634. psc_clks_init();
  635. /* leave clockctl mapped forever */
  636. /*iounmap(clockctl); */
  637. DEBUG_CLK_DUMP();
  638. clocks_initialized++;
  639. clk_functions = mpc5121_clk_functions;
  640. return 0;
  641. }