clock.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  1. /*
  2. * linux/arch/arm/mach-omap1/clock.c
  3. *
  4. * Copyright (C) 2004 - 2005, 2009-2010 Nokia Corporation
  5. * Written by Tuukka Tikkanen <tuukka.tikkanen@elektrobit.com>
  6. *
  7. * Modified to use omap shared clock framework by
  8. * Tony Lindgren <tony@atomide.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/list.h>
  16. #include <linux/errno.h>
  17. #include <linux/err.h>
  18. #include <linux/clk.h>
  19. #include <linux/io.h>
  20. #include <linux/clkdev.h>
  21. #include <asm/mach-types.h>
  22. #include <plat/cpu.h>
  23. #include <plat/usb.h>
  24. #include <plat/clock.h>
  25. #include <plat/sram.h>
  26. #include <plat/clkdev_omap.h>
  27. #include "clock.h"
  28. #include "opp.h"
  29. __u32 arm_idlect1_mask;
  30. struct clk *api_ck_p, *ck_dpll1_p, *ck_ref_p;
  31. /*
  32. * Omap1 specific clock functions
  33. */
  34. unsigned long omap1_uart_recalc(struct clk *clk)
  35. {
  36. unsigned int val = __raw_readl(clk->enable_reg);
  37. return val & clk->enable_bit ? 48000000 : 12000000;
  38. }
  39. unsigned long omap1_sossi_recalc(struct clk *clk)
  40. {
  41. u32 div = omap_readl(MOD_CONF_CTRL_1);
  42. div = (div >> 17) & 0x7;
  43. div++;
  44. return clk->parent->rate / div;
  45. }
  46. static void omap1_clk_allow_idle(struct clk *clk)
  47. {
  48. struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
  49. if (!(clk->flags & CLOCK_IDLE_CONTROL))
  50. return;
  51. if (iclk->no_idle_count > 0 && !(--iclk->no_idle_count))
  52. arm_idlect1_mask |= 1 << iclk->idlect_shift;
  53. }
  54. static void omap1_clk_deny_idle(struct clk *clk)
  55. {
  56. struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
  57. if (!(clk->flags & CLOCK_IDLE_CONTROL))
  58. return;
  59. if (iclk->no_idle_count++ == 0)
  60. arm_idlect1_mask &= ~(1 << iclk->idlect_shift);
  61. }
  62. static __u16 verify_ckctl_value(__u16 newval)
  63. {
  64. /* This function checks for following limitations set
  65. * by the hardware (all conditions must be true):
  66. * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
  67. * ARM_CK >= TC_CK
  68. * DSP_CK >= TC_CK
  69. * DSPMMU_CK >= TC_CK
  70. *
  71. * In addition following rules are enforced:
  72. * LCD_CK <= TC_CK
  73. * ARMPER_CK <= TC_CK
  74. *
  75. * However, maximum frequencies are not checked for!
  76. */
  77. __u8 per_exp;
  78. __u8 lcd_exp;
  79. __u8 arm_exp;
  80. __u8 dsp_exp;
  81. __u8 tc_exp;
  82. __u8 dspmmu_exp;
  83. per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
  84. lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
  85. arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
  86. dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
  87. tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
  88. dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
  89. if (dspmmu_exp < dsp_exp)
  90. dspmmu_exp = dsp_exp;
  91. if (dspmmu_exp > dsp_exp+1)
  92. dspmmu_exp = dsp_exp+1;
  93. if (tc_exp < arm_exp)
  94. tc_exp = arm_exp;
  95. if (tc_exp < dspmmu_exp)
  96. tc_exp = dspmmu_exp;
  97. if (tc_exp > lcd_exp)
  98. lcd_exp = tc_exp;
  99. if (tc_exp > per_exp)
  100. per_exp = tc_exp;
  101. newval &= 0xf000;
  102. newval |= per_exp << CKCTL_PERDIV_OFFSET;
  103. newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
  104. newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
  105. newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
  106. newval |= tc_exp << CKCTL_TCDIV_OFFSET;
  107. newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
  108. return newval;
  109. }
  110. static int calc_dsor_exp(struct clk *clk, unsigned long rate)
  111. {
  112. /* Note: If target frequency is too low, this function will return 4,
  113. * which is invalid value. Caller must check for this value and act
  114. * accordingly.
  115. *
  116. * Note: This function does not check for following limitations set
  117. * by the hardware (all conditions must be true):
  118. * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
  119. * ARM_CK >= TC_CK
  120. * DSP_CK >= TC_CK
  121. * DSPMMU_CK >= TC_CK
  122. */
  123. unsigned long realrate;
  124. struct clk * parent;
  125. unsigned dsor_exp;
  126. parent = clk->parent;
  127. if (unlikely(parent == NULL))
  128. return -EIO;
  129. realrate = parent->rate;
  130. for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
  131. if (realrate <= rate)
  132. break;
  133. realrate /= 2;
  134. }
  135. return dsor_exp;
  136. }
  137. unsigned long omap1_ckctl_recalc(struct clk *clk)
  138. {
  139. /* Calculate divisor encoded as 2-bit exponent */
  140. int dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
  141. return clk->parent->rate / dsor;
  142. }
  143. unsigned long omap1_ckctl_recalc_dsp_domain(struct clk *clk)
  144. {
  145. int dsor;
  146. /* Calculate divisor encoded as 2-bit exponent
  147. *
  148. * The clock control bits are in DSP domain,
  149. * so api_ck is needed for access.
  150. * Note that DSP_CKCTL virt addr = phys addr, so
  151. * we must use __raw_readw() instead of omap_readw().
  152. */
  153. omap1_clk_enable(api_ck_p);
  154. dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset));
  155. omap1_clk_disable(api_ck_p);
  156. return clk->parent->rate / dsor;
  157. }
  158. /* MPU virtual clock functions */
  159. int omap1_select_table_rate(struct clk *clk, unsigned long rate)
  160. {
  161. /* Find the highest supported frequency <= rate and switch to it */
  162. struct mpu_rate * ptr;
  163. unsigned long dpll1_rate, ref_rate;
  164. dpll1_rate = ck_dpll1_p->rate;
  165. ref_rate = ck_ref_p->rate;
  166. for (ptr = omap1_rate_table; ptr->rate; ptr++) {
  167. if (ptr->xtal != ref_rate)
  168. continue;
  169. /* DPLL1 cannot be reprogrammed without risking system crash */
  170. if (likely(dpll1_rate != 0) && ptr->pll_rate != dpll1_rate)
  171. continue;
  172. /* Can check only after xtal frequency check */
  173. if (ptr->rate <= rate)
  174. break;
  175. }
  176. if (!ptr->rate)
  177. return -EINVAL;
  178. /*
  179. * In most cases we should not need to reprogram DPLL.
  180. * Reprogramming the DPLL is tricky, it must be done from SRAM.
  181. * (on 730, bit 13 must always be 1)
  182. */
  183. if (cpu_is_omap7xx())
  184. omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val | 0x2000);
  185. else
  186. omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val);
  187. /* XXX Do we need to recalculate the tree below DPLL1 at this point? */
  188. ck_dpll1_p->rate = ptr->pll_rate;
  189. return 0;
  190. }
  191. int omap1_clk_set_rate_dsp_domain(struct clk *clk, unsigned long rate)
  192. {
  193. int dsor_exp;
  194. u16 regval;
  195. dsor_exp = calc_dsor_exp(clk, rate);
  196. if (dsor_exp > 3)
  197. dsor_exp = -EINVAL;
  198. if (dsor_exp < 0)
  199. return dsor_exp;
  200. regval = __raw_readw(DSP_CKCTL);
  201. regval &= ~(3 << clk->rate_offset);
  202. regval |= dsor_exp << clk->rate_offset;
  203. __raw_writew(regval, DSP_CKCTL);
  204. clk->rate = clk->parent->rate / (1 << dsor_exp);
  205. return 0;
  206. }
  207. long omap1_clk_round_rate_ckctl_arm(struct clk *clk, unsigned long rate)
  208. {
  209. int dsor_exp = calc_dsor_exp(clk, rate);
  210. if (dsor_exp < 0)
  211. return dsor_exp;
  212. if (dsor_exp > 3)
  213. dsor_exp = 3;
  214. return clk->parent->rate / (1 << dsor_exp);
  215. }
  216. int omap1_clk_set_rate_ckctl_arm(struct clk *clk, unsigned long rate)
  217. {
  218. int dsor_exp;
  219. u16 regval;
  220. dsor_exp = calc_dsor_exp(clk, rate);
  221. if (dsor_exp > 3)
  222. dsor_exp = -EINVAL;
  223. if (dsor_exp < 0)
  224. return dsor_exp;
  225. regval = omap_readw(ARM_CKCTL);
  226. regval &= ~(3 << clk->rate_offset);
  227. regval |= dsor_exp << clk->rate_offset;
  228. regval = verify_ckctl_value(regval);
  229. omap_writew(regval, ARM_CKCTL);
  230. clk->rate = clk->parent->rate / (1 << dsor_exp);
  231. return 0;
  232. }
  233. long omap1_round_to_table_rate(struct clk *clk, unsigned long rate)
  234. {
  235. /* Find the highest supported frequency <= rate */
  236. struct mpu_rate * ptr;
  237. long highest_rate;
  238. unsigned long ref_rate;
  239. ref_rate = ck_ref_p->rate;
  240. highest_rate = -EINVAL;
  241. for (ptr = omap1_rate_table; ptr->rate; ptr++) {
  242. if (ptr->xtal != ref_rate)
  243. continue;
  244. highest_rate = ptr->rate;
  245. /* Can check only after xtal frequency check */
  246. if (ptr->rate <= rate)
  247. break;
  248. }
  249. return highest_rate;
  250. }
  251. static unsigned calc_ext_dsor(unsigned long rate)
  252. {
  253. unsigned dsor;
  254. /* MCLK and BCLK divisor selection is not linear:
  255. * freq = 96MHz / dsor
  256. *
  257. * RATIO_SEL range: dsor <-> RATIO_SEL
  258. * 0..6: (RATIO_SEL+2) <-> (dsor-2)
  259. * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
  260. * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
  261. * can not be used.
  262. */
  263. for (dsor = 2; dsor < 96; ++dsor) {
  264. if ((dsor & 1) && dsor > 8)
  265. continue;
  266. if (rate >= 96000000 / dsor)
  267. break;
  268. }
  269. return dsor;
  270. }
  271. /* XXX Only needed on 1510 */
  272. int omap1_set_uart_rate(struct clk *clk, unsigned long rate)
  273. {
  274. unsigned int val;
  275. val = __raw_readl(clk->enable_reg);
  276. if (rate == 12000000)
  277. val &= ~(1 << clk->enable_bit);
  278. else if (rate == 48000000)
  279. val |= (1 << clk->enable_bit);
  280. else
  281. return -EINVAL;
  282. __raw_writel(val, clk->enable_reg);
  283. clk->rate = rate;
  284. return 0;
  285. }
  286. /* External clock (MCLK & BCLK) functions */
  287. int omap1_set_ext_clk_rate(struct clk *clk, unsigned long rate)
  288. {
  289. unsigned dsor;
  290. __u16 ratio_bits;
  291. dsor = calc_ext_dsor(rate);
  292. clk->rate = 96000000 / dsor;
  293. if (dsor > 8)
  294. ratio_bits = ((dsor - 8) / 2 + 6) << 2;
  295. else
  296. ratio_bits = (dsor - 2) << 2;
  297. ratio_bits |= __raw_readw(clk->enable_reg) & ~0xfd;
  298. __raw_writew(ratio_bits, clk->enable_reg);
  299. return 0;
  300. }
  301. int omap1_set_sossi_rate(struct clk *clk, unsigned long rate)
  302. {
  303. u32 l;
  304. int div;
  305. unsigned long p_rate;
  306. p_rate = clk->parent->rate;
  307. /* Round towards slower frequency */
  308. div = (p_rate + rate - 1) / rate;
  309. div--;
  310. if (div < 0 || div > 7)
  311. return -EINVAL;
  312. l = omap_readl(MOD_CONF_CTRL_1);
  313. l &= ~(7 << 17);
  314. l |= div << 17;
  315. omap_writel(l, MOD_CONF_CTRL_1);
  316. clk->rate = p_rate / (div + 1);
  317. return 0;
  318. }
  319. long omap1_round_ext_clk_rate(struct clk *clk, unsigned long rate)
  320. {
  321. return 96000000 / calc_ext_dsor(rate);
  322. }
  323. void omap1_init_ext_clk(struct clk *clk)
  324. {
  325. unsigned dsor;
  326. __u16 ratio_bits;
  327. /* Determine current rate and ensure clock is based on 96MHz APLL */
  328. ratio_bits = __raw_readw(clk->enable_reg) & ~1;
  329. __raw_writew(ratio_bits, clk->enable_reg);
  330. ratio_bits = (ratio_bits & 0xfc) >> 2;
  331. if (ratio_bits > 6)
  332. dsor = (ratio_bits - 6) * 2 + 8;
  333. else
  334. dsor = ratio_bits + 2;
  335. clk-> rate = 96000000 / dsor;
  336. }
  337. int omap1_clk_enable(struct clk *clk)
  338. {
  339. int ret = 0;
  340. if (clk->usecount++ == 0) {
  341. if (clk->parent) {
  342. ret = omap1_clk_enable(clk->parent);
  343. if (ret)
  344. goto err;
  345. if (clk->flags & CLOCK_NO_IDLE_PARENT)
  346. omap1_clk_deny_idle(clk->parent);
  347. }
  348. ret = clk->ops->enable(clk);
  349. if (ret) {
  350. if (clk->parent)
  351. omap1_clk_disable(clk->parent);
  352. goto err;
  353. }
  354. }
  355. return ret;
  356. err:
  357. clk->usecount--;
  358. return ret;
  359. }
  360. void omap1_clk_disable(struct clk *clk)
  361. {
  362. if (clk->usecount > 0 && !(--clk->usecount)) {
  363. clk->ops->disable(clk);
  364. if (likely(clk->parent)) {
  365. omap1_clk_disable(clk->parent);
  366. if (clk->flags & CLOCK_NO_IDLE_PARENT)
  367. omap1_clk_allow_idle(clk->parent);
  368. }
  369. }
  370. }
  371. static int omap1_clk_enable_generic(struct clk *clk)
  372. {
  373. __u16 regval16;
  374. __u32 regval32;
  375. if (unlikely(clk->enable_reg == NULL)) {
  376. printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
  377. clk->name);
  378. return -EINVAL;
  379. }
  380. if (clk->flags & ENABLE_REG_32BIT) {
  381. regval32 = __raw_readl(clk->enable_reg);
  382. regval32 |= (1 << clk->enable_bit);
  383. __raw_writel(regval32, clk->enable_reg);
  384. } else {
  385. regval16 = __raw_readw(clk->enable_reg);
  386. regval16 |= (1 << clk->enable_bit);
  387. __raw_writew(regval16, clk->enable_reg);
  388. }
  389. return 0;
  390. }
  391. static void omap1_clk_disable_generic(struct clk *clk)
  392. {
  393. __u16 regval16;
  394. __u32 regval32;
  395. if (clk->enable_reg == NULL)
  396. return;
  397. if (clk->flags & ENABLE_REG_32BIT) {
  398. regval32 = __raw_readl(clk->enable_reg);
  399. regval32 &= ~(1 << clk->enable_bit);
  400. __raw_writel(regval32, clk->enable_reg);
  401. } else {
  402. regval16 = __raw_readw(clk->enable_reg);
  403. regval16 &= ~(1 << clk->enable_bit);
  404. __raw_writew(regval16, clk->enable_reg);
  405. }
  406. }
  407. const struct clkops clkops_generic = {
  408. .enable = omap1_clk_enable_generic,
  409. .disable = omap1_clk_disable_generic,
  410. };
  411. static int omap1_clk_enable_dsp_domain(struct clk *clk)
  412. {
  413. int retval;
  414. retval = omap1_clk_enable(api_ck_p);
  415. if (!retval) {
  416. retval = omap1_clk_enable_generic(clk);
  417. omap1_clk_disable(api_ck_p);
  418. }
  419. return retval;
  420. }
  421. static void omap1_clk_disable_dsp_domain(struct clk *clk)
  422. {
  423. if (omap1_clk_enable(api_ck_p) == 0) {
  424. omap1_clk_disable_generic(clk);
  425. omap1_clk_disable(api_ck_p);
  426. }
  427. }
  428. const struct clkops clkops_dspck = {
  429. .enable = omap1_clk_enable_dsp_domain,
  430. .disable = omap1_clk_disable_dsp_domain,
  431. };
  432. /* XXX SYSC register handling does not belong in the clock framework */
  433. static int omap1_clk_enable_uart_functional_16xx(struct clk *clk)
  434. {
  435. int ret;
  436. struct uart_clk *uclk;
  437. ret = omap1_clk_enable_generic(clk);
  438. if (ret == 0) {
  439. /* Set smart idle acknowledgement mode */
  440. uclk = (struct uart_clk *)clk;
  441. omap_writeb((omap_readb(uclk->sysc_addr) & ~0x10) | 8,
  442. uclk->sysc_addr);
  443. }
  444. return ret;
  445. }
  446. /* XXX SYSC register handling does not belong in the clock framework */
  447. static void omap1_clk_disable_uart_functional_16xx(struct clk *clk)
  448. {
  449. struct uart_clk *uclk;
  450. /* Set force idle acknowledgement mode */
  451. uclk = (struct uart_clk *)clk;
  452. omap_writeb((omap_readb(uclk->sysc_addr) & ~0x18), uclk->sysc_addr);
  453. omap1_clk_disable_generic(clk);
  454. }
  455. /* XXX SYSC register handling does not belong in the clock framework */
  456. const struct clkops clkops_uart_16xx = {
  457. .enable = omap1_clk_enable_uart_functional_16xx,
  458. .disable = omap1_clk_disable_uart_functional_16xx,
  459. };
  460. long omap1_clk_round_rate(struct clk *clk, unsigned long rate)
  461. {
  462. if (clk->round_rate != NULL)
  463. return clk->round_rate(clk, rate);
  464. return clk->rate;
  465. }
  466. int omap1_clk_set_rate(struct clk *clk, unsigned long rate)
  467. {
  468. int ret = -EINVAL;
  469. if (clk->set_rate)
  470. ret = clk->set_rate(clk, rate);
  471. return ret;
  472. }
  473. /*
  474. * Omap1 clock reset and init functions
  475. */
  476. #ifdef CONFIG_OMAP_RESET_CLOCKS
  477. void omap1_clk_disable_unused(struct clk *clk)
  478. {
  479. __u32 regval32;
  480. /* Clocks in the DSP domain need api_ck. Just assume bootloader
  481. * has not enabled any DSP clocks */
  482. if (clk->enable_reg == DSP_IDLECT2) {
  483. printk(KERN_INFO "Skipping reset check for DSP domain "
  484. "clock \"%s\"\n", clk->name);
  485. return;
  486. }
  487. /* Is the clock already disabled? */
  488. if (clk->flags & ENABLE_REG_32BIT)
  489. regval32 = __raw_readl(clk->enable_reg);
  490. else
  491. regval32 = __raw_readw(clk->enable_reg);
  492. if ((regval32 & (1 << clk->enable_bit)) == 0)
  493. return;
  494. printk(KERN_INFO "Disabling unused clock \"%s\"... ", clk->name);
  495. clk->ops->disable(clk);
  496. printk(" done\n");
  497. }
  498. #endif