clock.c 14 KB

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