clock.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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/export.h>
  16. #include <linux/list.h>
  17. #include <linux/errno.h>
  18. #include <linux/err.h>
  19. #include <linux/io.h>
  20. #include <linux/clk.h>
  21. #include <linux/clkdev.h>
  22. #include <asm/mach-types.h>
  23. #include <mach/hardware.h>
  24. #include "soc.h"
  25. #include "iomap.h"
  26. #include "clock.h"
  27. #include "opp.h"
  28. #include "sram.h"
  29. __u32 arm_idlect1_mask;
  30. struct clk *api_ck_p, *ck_dpll1_p, *ck_ref_p;
  31. static LIST_HEAD(clocks);
  32. static DEFINE_MUTEX(clocks_mutex);
  33. static DEFINE_SPINLOCK(clockfw_lock);
  34. /*
  35. * Omap1 specific clock functions
  36. */
  37. unsigned long omap1_uart_recalc(struct clk *clk)
  38. {
  39. unsigned int val = __raw_readl(clk->enable_reg);
  40. return val & clk->enable_bit ? 48000000 : 12000000;
  41. }
  42. unsigned long omap1_sossi_recalc(struct clk *clk)
  43. {
  44. u32 div = omap_readl(MOD_CONF_CTRL_1);
  45. div = (div >> 17) & 0x7;
  46. div++;
  47. return clk->parent->rate / div;
  48. }
  49. static void omap1_clk_allow_idle(struct clk *clk)
  50. {
  51. struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
  52. if (!(clk->flags & CLOCK_IDLE_CONTROL))
  53. return;
  54. if (iclk->no_idle_count > 0 && !(--iclk->no_idle_count))
  55. arm_idlect1_mask |= 1 << iclk->idlect_shift;
  56. }
  57. static void omap1_clk_deny_idle(struct clk *clk)
  58. {
  59. struct arm_idlect1_clk * iclk = (struct arm_idlect1_clk *)clk;
  60. if (!(clk->flags & CLOCK_IDLE_CONTROL))
  61. return;
  62. if (iclk->no_idle_count++ == 0)
  63. arm_idlect1_mask &= ~(1 << iclk->idlect_shift);
  64. }
  65. static __u16 verify_ckctl_value(__u16 newval)
  66. {
  67. /* This function checks for following limitations set
  68. * by the hardware (all conditions must be true):
  69. * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
  70. * ARM_CK >= TC_CK
  71. * DSP_CK >= TC_CK
  72. * DSPMMU_CK >= TC_CK
  73. *
  74. * In addition following rules are enforced:
  75. * LCD_CK <= TC_CK
  76. * ARMPER_CK <= TC_CK
  77. *
  78. * However, maximum frequencies are not checked for!
  79. */
  80. __u8 per_exp;
  81. __u8 lcd_exp;
  82. __u8 arm_exp;
  83. __u8 dsp_exp;
  84. __u8 tc_exp;
  85. __u8 dspmmu_exp;
  86. per_exp = (newval >> CKCTL_PERDIV_OFFSET) & 3;
  87. lcd_exp = (newval >> CKCTL_LCDDIV_OFFSET) & 3;
  88. arm_exp = (newval >> CKCTL_ARMDIV_OFFSET) & 3;
  89. dsp_exp = (newval >> CKCTL_DSPDIV_OFFSET) & 3;
  90. tc_exp = (newval >> CKCTL_TCDIV_OFFSET) & 3;
  91. dspmmu_exp = (newval >> CKCTL_DSPMMUDIV_OFFSET) & 3;
  92. if (dspmmu_exp < dsp_exp)
  93. dspmmu_exp = dsp_exp;
  94. if (dspmmu_exp > dsp_exp+1)
  95. dspmmu_exp = dsp_exp+1;
  96. if (tc_exp < arm_exp)
  97. tc_exp = arm_exp;
  98. if (tc_exp < dspmmu_exp)
  99. tc_exp = dspmmu_exp;
  100. if (tc_exp > lcd_exp)
  101. lcd_exp = tc_exp;
  102. if (tc_exp > per_exp)
  103. per_exp = tc_exp;
  104. newval &= 0xf000;
  105. newval |= per_exp << CKCTL_PERDIV_OFFSET;
  106. newval |= lcd_exp << CKCTL_LCDDIV_OFFSET;
  107. newval |= arm_exp << CKCTL_ARMDIV_OFFSET;
  108. newval |= dsp_exp << CKCTL_DSPDIV_OFFSET;
  109. newval |= tc_exp << CKCTL_TCDIV_OFFSET;
  110. newval |= dspmmu_exp << CKCTL_DSPMMUDIV_OFFSET;
  111. return newval;
  112. }
  113. static int calc_dsor_exp(struct clk *clk, unsigned long rate)
  114. {
  115. /* Note: If target frequency is too low, this function will return 4,
  116. * which is invalid value. Caller must check for this value and act
  117. * accordingly.
  118. *
  119. * Note: This function does not check for following limitations set
  120. * by the hardware (all conditions must be true):
  121. * DSPMMU_CK == DSP_CK or DSPMMU_CK == DSP_CK/2
  122. * ARM_CK >= TC_CK
  123. * DSP_CK >= TC_CK
  124. * DSPMMU_CK >= TC_CK
  125. */
  126. unsigned long realrate;
  127. struct clk * parent;
  128. unsigned dsor_exp;
  129. parent = clk->parent;
  130. if (unlikely(parent == NULL))
  131. return -EIO;
  132. realrate = parent->rate;
  133. for (dsor_exp=0; dsor_exp<4; dsor_exp++) {
  134. if (realrate <= rate)
  135. break;
  136. realrate /= 2;
  137. }
  138. return dsor_exp;
  139. }
  140. unsigned long omap1_ckctl_recalc(struct clk *clk)
  141. {
  142. /* Calculate divisor encoded as 2-bit exponent */
  143. int dsor = 1 << (3 & (omap_readw(ARM_CKCTL) >> clk->rate_offset));
  144. return clk->parent->rate / dsor;
  145. }
  146. unsigned long omap1_ckctl_recalc_dsp_domain(struct clk *clk)
  147. {
  148. int dsor;
  149. /* Calculate divisor encoded as 2-bit exponent
  150. *
  151. * The clock control bits are in DSP domain,
  152. * so api_ck is needed for access.
  153. * Note that DSP_CKCTL virt addr = phys addr, so
  154. * we must use __raw_readw() instead of omap_readw().
  155. */
  156. omap1_clk_enable(api_ck_p);
  157. dsor = 1 << (3 & (__raw_readw(DSP_CKCTL) >> clk->rate_offset));
  158. omap1_clk_disable(api_ck_p);
  159. return clk->parent->rate / dsor;
  160. }
  161. /* MPU virtual clock functions */
  162. int omap1_select_table_rate(struct clk *clk, unsigned long rate)
  163. {
  164. /* Find the highest supported frequency <= rate and switch to it */
  165. struct mpu_rate * ptr;
  166. unsigned long ref_rate;
  167. ref_rate = ck_ref_p->rate;
  168. for (ptr = omap1_rate_table; ptr->rate; ptr++) {
  169. if (!(ptr->flags & cpu_mask))
  170. continue;
  171. if (ptr->xtal != ref_rate)
  172. continue;
  173. /* Can check only after xtal frequency check */
  174. if (ptr->rate <= rate)
  175. break;
  176. }
  177. if (!ptr->rate)
  178. return -EINVAL;
  179. /*
  180. * In most cases we should not need to reprogram DPLL.
  181. * Reprogramming the DPLL is tricky, it must be done from SRAM.
  182. */
  183. omap_sram_reprogram_clock(ptr->dpllctl_val, ptr->ckctl_val);
  184. /* XXX Do we need to recalculate the tree below DPLL1 at this point? */
  185. ck_dpll1_p->rate = ptr->pll_rate;
  186. return 0;
  187. }
  188. int omap1_clk_set_rate_dsp_domain(struct clk *clk, unsigned long rate)
  189. {
  190. int dsor_exp;
  191. u16 regval;
  192. dsor_exp = calc_dsor_exp(clk, rate);
  193. if (dsor_exp > 3)
  194. dsor_exp = -EINVAL;
  195. if (dsor_exp < 0)
  196. return dsor_exp;
  197. regval = __raw_readw(DSP_CKCTL);
  198. regval &= ~(3 << clk->rate_offset);
  199. regval |= dsor_exp << clk->rate_offset;
  200. __raw_writew(regval, DSP_CKCTL);
  201. clk->rate = clk->parent->rate / (1 << dsor_exp);
  202. return 0;
  203. }
  204. long omap1_clk_round_rate_ckctl_arm(struct clk *clk, unsigned long rate)
  205. {
  206. int dsor_exp = calc_dsor_exp(clk, rate);
  207. if (dsor_exp < 0)
  208. return dsor_exp;
  209. if (dsor_exp > 3)
  210. dsor_exp = 3;
  211. return clk->parent->rate / (1 << dsor_exp);
  212. }
  213. int omap1_clk_set_rate_ckctl_arm(struct clk *clk, unsigned long rate)
  214. {
  215. int dsor_exp;
  216. u16 regval;
  217. dsor_exp = calc_dsor_exp(clk, rate);
  218. if (dsor_exp > 3)
  219. dsor_exp = -EINVAL;
  220. if (dsor_exp < 0)
  221. return dsor_exp;
  222. regval = omap_readw(ARM_CKCTL);
  223. regval &= ~(3 << clk->rate_offset);
  224. regval |= dsor_exp << clk->rate_offset;
  225. regval = verify_ckctl_value(regval);
  226. omap_writew(regval, ARM_CKCTL);
  227. clk->rate = clk->parent->rate / (1 << dsor_exp);
  228. return 0;
  229. }
  230. long omap1_round_to_table_rate(struct clk *clk, unsigned long rate)
  231. {
  232. /* Find the highest supported frequency <= rate */
  233. struct mpu_rate * ptr;
  234. long highest_rate;
  235. unsigned long ref_rate;
  236. ref_rate = ck_ref_p->rate;
  237. highest_rate = -EINVAL;
  238. for (ptr = omap1_rate_table; ptr->rate; ptr++) {
  239. if (!(ptr->flags & cpu_mask))
  240. continue;
  241. if (ptr->xtal != ref_rate)
  242. continue;
  243. highest_rate = ptr->rate;
  244. /* Can check only after xtal frequency check */
  245. if (ptr->rate <= rate)
  246. break;
  247. }
  248. return highest_rate;
  249. }
  250. static unsigned calc_ext_dsor(unsigned long rate)
  251. {
  252. unsigned dsor;
  253. /* MCLK and BCLK divisor selection is not linear:
  254. * freq = 96MHz / dsor
  255. *
  256. * RATIO_SEL range: dsor <-> RATIO_SEL
  257. * 0..6: (RATIO_SEL+2) <-> (dsor-2)
  258. * 6..48: (8+(RATIO_SEL-6)*2) <-> ((dsor-8)/2+6)
  259. * Minimum dsor is 2 and maximum is 96. Odd divisors starting from 9
  260. * can not be used.
  261. */
  262. for (dsor = 2; dsor < 96; ++dsor) {
  263. if ((dsor & 1) && dsor > 8)
  264. continue;
  265. if (rate >= 96000000 / dsor)
  266. break;
  267. }
  268. return dsor;
  269. }
  270. /* XXX Only needed on 1510 */
  271. int omap1_set_uart_rate(struct clk *clk, unsigned long rate)
  272. {
  273. unsigned int val;
  274. val = __raw_readl(clk->enable_reg);
  275. if (rate == 12000000)
  276. val &= ~(1 << clk->enable_bit);
  277. else if (rate == 48000000)
  278. val |= (1 << clk->enable_bit);
  279. else
  280. return -EINVAL;
  281. __raw_writel(val, clk->enable_reg);
  282. clk->rate = rate;
  283. return 0;
  284. }
  285. /* External clock (MCLK & BCLK) functions */
  286. int omap1_set_ext_clk_rate(struct clk *clk, unsigned long rate)
  287. {
  288. unsigned dsor;
  289. __u16 ratio_bits;
  290. dsor = calc_ext_dsor(rate);
  291. clk->rate = 96000000 / dsor;
  292. if (dsor > 8)
  293. ratio_bits = ((dsor - 8) / 2 + 6) << 2;
  294. else
  295. ratio_bits = (dsor - 2) << 2;
  296. ratio_bits |= __raw_readw(clk->enable_reg) & ~0xfd;
  297. __raw_writew(ratio_bits, clk->enable_reg);
  298. return 0;
  299. }
  300. int omap1_set_sossi_rate(struct clk *clk, unsigned long rate)
  301. {
  302. u32 l;
  303. int div;
  304. unsigned long p_rate;
  305. p_rate = clk->parent->rate;
  306. /* Round towards slower frequency */
  307. div = (p_rate + rate - 1) / rate;
  308. div--;
  309. if (div < 0 || div > 7)
  310. return -EINVAL;
  311. l = omap_readl(MOD_CONF_CTRL_1);
  312. l &= ~(7 << 17);
  313. l |= div << 17;
  314. omap_writel(l, MOD_CONF_CTRL_1);
  315. clk->rate = p_rate / (div + 1);
  316. return 0;
  317. }
  318. long omap1_round_ext_clk_rate(struct clk *clk, unsigned long rate)
  319. {
  320. return 96000000 / calc_ext_dsor(rate);
  321. }
  322. void omap1_init_ext_clk(struct clk *clk)
  323. {
  324. unsigned dsor;
  325. __u16 ratio_bits;
  326. /* Determine current rate and ensure clock is based on 96MHz APLL */
  327. ratio_bits = __raw_readw(clk->enable_reg) & ~1;
  328. __raw_writew(ratio_bits, clk->enable_reg);
  329. ratio_bits = (ratio_bits & 0xfc) >> 2;
  330. if (ratio_bits > 6)
  331. dsor = (ratio_bits - 6) * 2 + 8;
  332. else
  333. dsor = ratio_bits + 2;
  334. clk-> rate = 96000000 / dsor;
  335. }
  336. int omap1_clk_enable(struct clk *clk)
  337. {
  338. int ret = 0;
  339. if (clk->usecount++ == 0) {
  340. if (clk->parent) {
  341. ret = omap1_clk_enable(clk->parent);
  342. if (ret)
  343. goto err;
  344. if (clk->flags & CLOCK_NO_IDLE_PARENT)
  345. omap1_clk_deny_idle(clk->parent);
  346. }
  347. ret = clk->ops->enable(clk);
  348. if (ret) {
  349. if (clk->parent)
  350. omap1_clk_disable(clk->parent);
  351. goto err;
  352. }
  353. }
  354. return ret;
  355. err:
  356. clk->usecount--;
  357. return ret;
  358. }
  359. void omap1_clk_disable(struct clk *clk)
  360. {
  361. if (clk->usecount > 0 && !(--clk->usecount)) {
  362. clk->ops->disable(clk);
  363. if (likely(clk->parent)) {
  364. omap1_clk_disable(clk->parent);
  365. if (clk->flags & CLOCK_NO_IDLE_PARENT)
  366. omap1_clk_allow_idle(clk->parent);
  367. }
  368. }
  369. }
  370. static int omap1_clk_enable_generic(struct clk *clk)
  371. {
  372. __u16 regval16;
  373. __u32 regval32;
  374. if (unlikely(clk->enable_reg == NULL)) {
  375. printk(KERN_ERR "clock.c: Enable for %s without enable code\n",
  376. clk->name);
  377. return -EINVAL;
  378. }
  379. if (clk->flags & ENABLE_REG_32BIT) {
  380. regval32 = __raw_readl(clk->enable_reg);
  381. regval32 |= (1 << clk->enable_bit);
  382. __raw_writel(regval32, clk->enable_reg);
  383. } else {
  384. regval16 = __raw_readw(clk->enable_reg);
  385. regval16 |= (1 << clk->enable_bit);
  386. __raw_writew(regval16, clk->enable_reg);
  387. }
  388. return 0;
  389. }
  390. static void omap1_clk_disable_generic(struct clk *clk)
  391. {
  392. __u16 regval16;
  393. __u32 regval32;
  394. if (clk->enable_reg == NULL)
  395. return;
  396. if (clk->flags & ENABLE_REG_32BIT) {
  397. regval32 = __raw_readl(clk->enable_reg);
  398. regval32 &= ~(1 << clk->enable_bit);
  399. __raw_writel(regval32, clk->enable_reg);
  400. } else {
  401. regval16 = __raw_readw(clk->enable_reg);
  402. regval16 &= ~(1 << clk->enable_bit);
  403. __raw_writew(regval16, clk->enable_reg);
  404. }
  405. }
  406. const struct clkops clkops_generic = {
  407. .enable = omap1_clk_enable_generic,
  408. .disable = omap1_clk_disable_generic,
  409. };
  410. static int omap1_clk_enable_dsp_domain(struct clk *clk)
  411. {
  412. int retval;
  413. retval = omap1_clk_enable(api_ck_p);
  414. if (!retval) {
  415. retval = omap1_clk_enable_generic(clk);
  416. omap1_clk_disable(api_ck_p);
  417. }
  418. return retval;
  419. }
  420. static void omap1_clk_disable_dsp_domain(struct clk *clk)
  421. {
  422. if (omap1_clk_enable(api_ck_p) == 0) {
  423. omap1_clk_disable_generic(clk);
  424. omap1_clk_disable(api_ck_p);
  425. }
  426. }
  427. const struct clkops clkops_dspck = {
  428. .enable = omap1_clk_enable_dsp_domain,
  429. .disable = omap1_clk_disable_dsp_domain,
  430. };
  431. /* XXX SYSC register handling does not belong in the clock framework */
  432. static int omap1_clk_enable_uart_functional_16xx(struct clk *clk)
  433. {
  434. int ret;
  435. struct uart_clk *uclk;
  436. ret = omap1_clk_enable_generic(clk);
  437. if (ret == 0) {
  438. /* Set smart idle acknowledgement mode */
  439. uclk = (struct uart_clk *)clk;
  440. omap_writeb((omap_readb(uclk->sysc_addr) & ~0x10) | 8,
  441. uclk->sysc_addr);
  442. }
  443. return ret;
  444. }
  445. /* XXX SYSC register handling does not belong in the clock framework */
  446. static void omap1_clk_disable_uart_functional_16xx(struct clk *clk)
  447. {
  448. struct uart_clk *uclk;
  449. /* Set force idle acknowledgement mode */
  450. uclk = (struct uart_clk *)clk;
  451. omap_writeb((omap_readb(uclk->sysc_addr) & ~0x18), uclk->sysc_addr);
  452. omap1_clk_disable_generic(clk);
  453. }
  454. /* XXX SYSC register handling does not belong in the clock framework */
  455. const struct clkops clkops_uart_16xx = {
  456. .enable = omap1_clk_enable_uart_functional_16xx,
  457. .disable = omap1_clk_disable_uart_functional_16xx,
  458. };
  459. long omap1_clk_round_rate(struct clk *clk, unsigned long rate)
  460. {
  461. if (clk->round_rate != NULL)
  462. return clk->round_rate(clk, rate);
  463. return clk->rate;
  464. }
  465. int omap1_clk_set_rate(struct clk *clk, unsigned long rate)
  466. {
  467. int ret = -EINVAL;
  468. if (clk->set_rate)
  469. ret = clk->set_rate(clk, rate);
  470. return ret;
  471. }
  472. /*
  473. * Omap1 clock reset and init functions
  474. */
  475. #ifdef CONFIG_OMAP_RESET_CLOCKS
  476. void omap1_clk_disable_unused(struct clk *clk)
  477. {
  478. __u32 regval32;
  479. /* Clocks in the DSP domain need api_ck. Just assume bootloader
  480. * has not enabled any DSP clocks */
  481. if (clk->enable_reg == DSP_IDLECT2) {
  482. pr_info("Skipping reset check for DSP domain clock \"%s\"\n",
  483. clk->name);
  484. return;
  485. }
  486. /* Is the clock already disabled? */
  487. if (clk->flags & ENABLE_REG_32BIT)
  488. regval32 = __raw_readl(clk->enable_reg);
  489. else
  490. regval32 = __raw_readw(clk->enable_reg);
  491. if ((regval32 & (1 << clk->enable_bit)) == 0)
  492. return;
  493. printk(KERN_INFO "Disabling unused clock \"%s\"... ", clk->name);
  494. clk->ops->disable(clk);
  495. printk(" done\n");
  496. }
  497. #endif
  498. int clk_enable(struct clk *clk)
  499. {
  500. unsigned long flags;
  501. int ret;
  502. if (clk == NULL || IS_ERR(clk))
  503. return -EINVAL;
  504. spin_lock_irqsave(&clockfw_lock, flags);
  505. ret = omap1_clk_enable(clk);
  506. spin_unlock_irqrestore(&clockfw_lock, flags);
  507. return ret;
  508. }
  509. EXPORT_SYMBOL(clk_enable);
  510. void clk_disable(struct clk *clk)
  511. {
  512. unsigned long flags;
  513. if (clk == NULL || IS_ERR(clk))
  514. return;
  515. spin_lock_irqsave(&clockfw_lock, flags);
  516. if (clk->usecount == 0) {
  517. pr_err("Trying disable clock %s with 0 usecount\n",
  518. clk->name);
  519. WARN_ON(1);
  520. goto out;
  521. }
  522. omap1_clk_disable(clk);
  523. out:
  524. spin_unlock_irqrestore(&clockfw_lock, flags);
  525. }
  526. EXPORT_SYMBOL(clk_disable);
  527. unsigned long clk_get_rate(struct clk *clk)
  528. {
  529. unsigned long flags;
  530. unsigned long ret;
  531. if (clk == NULL || IS_ERR(clk))
  532. return 0;
  533. spin_lock_irqsave(&clockfw_lock, flags);
  534. ret = clk->rate;
  535. spin_unlock_irqrestore(&clockfw_lock, flags);
  536. return ret;
  537. }
  538. EXPORT_SYMBOL(clk_get_rate);
  539. /*
  540. * Optional clock functions defined in include/linux/clk.h
  541. */
  542. long clk_round_rate(struct clk *clk, unsigned long rate)
  543. {
  544. unsigned long flags;
  545. long ret;
  546. if (clk == NULL || IS_ERR(clk))
  547. return 0;
  548. spin_lock_irqsave(&clockfw_lock, flags);
  549. ret = omap1_clk_round_rate(clk, rate);
  550. spin_unlock_irqrestore(&clockfw_lock, flags);
  551. return ret;
  552. }
  553. EXPORT_SYMBOL(clk_round_rate);
  554. int clk_set_rate(struct clk *clk, unsigned long rate)
  555. {
  556. unsigned long flags;
  557. int ret = -EINVAL;
  558. if (clk == NULL || IS_ERR(clk))
  559. return ret;
  560. spin_lock_irqsave(&clockfw_lock, flags);
  561. ret = omap1_clk_set_rate(clk, rate);
  562. if (ret == 0)
  563. propagate_rate(clk);
  564. spin_unlock_irqrestore(&clockfw_lock, flags);
  565. return ret;
  566. }
  567. EXPORT_SYMBOL(clk_set_rate);
  568. int clk_set_parent(struct clk *clk, struct clk *parent)
  569. {
  570. WARN_ONCE(1, "clk_set_parent() not implemented for OMAP1\n");
  571. return -EINVAL;
  572. }
  573. EXPORT_SYMBOL(clk_set_parent);
  574. struct clk *clk_get_parent(struct clk *clk)
  575. {
  576. return clk->parent;
  577. }
  578. EXPORT_SYMBOL(clk_get_parent);
  579. /*
  580. * OMAP specific clock functions shared between omap1 and omap2
  581. */
  582. /* Used for clocks that always have same value as the parent clock */
  583. unsigned long followparent_recalc(struct clk *clk)
  584. {
  585. return clk->parent->rate;
  586. }
  587. /*
  588. * Used for clocks that have the same value as the parent clock,
  589. * divided by some factor
  590. */
  591. unsigned long omap_fixed_divisor_recalc(struct clk *clk)
  592. {
  593. WARN_ON(!clk->fixed_div);
  594. return clk->parent->rate / clk->fixed_div;
  595. }
  596. void clk_reparent(struct clk *child, struct clk *parent)
  597. {
  598. list_del_init(&child->sibling);
  599. if (parent)
  600. list_add(&child->sibling, &parent->children);
  601. child->parent = parent;
  602. /* now do the debugfs renaming to reattach the child
  603. to the proper parent */
  604. }
  605. /* Propagate rate to children */
  606. void propagate_rate(struct clk *tclk)
  607. {
  608. struct clk *clkp;
  609. list_for_each_entry(clkp, &tclk->children, sibling) {
  610. if (clkp->recalc)
  611. clkp->rate = clkp->recalc(clkp);
  612. propagate_rate(clkp);
  613. }
  614. }
  615. static LIST_HEAD(root_clks);
  616. /**
  617. * recalculate_root_clocks - recalculate and propagate all root clocks
  618. *
  619. * Recalculates all root clocks (clocks with no parent), which if the
  620. * clock's .recalc is set correctly, should also propagate their rates.
  621. * Called at init.
  622. */
  623. void recalculate_root_clocks(void)
  624. {
  625. struct clk *clkp;
  626. list_for_each_entry(clkp, &root_clks, sibling) {
  627. if (clkp->recalc)
  628. clkp->rate = clkp->recalc(clkp);
  629. propagate_rate(clkp);
  630. }
  631. }
  632. /**
  633. * clk_preinit - initialize any fields in the struct clk before clk init
  634. * @clk: struct clk * to initialize
  635. *
  636. * Initialize any struct clk fields needed before normal clk initialization
  637. * can run. No return value.
  638. */
  639. void clk_preinit(struct clk *clk)
  640. {
  641. INIT_LIST_HEAD(&clk->children);
  642. }
  643. int clk_register(struct clk *clk)
  644. {
  645. if (clk == NULL || IS_ERR(clk))
  646. return -EINVAL;
  647. /*
  648. * trap out already registered clocks
  649. */
  650. if (clk->node.next || clk->node.prev)
  651. return 0;
  652. mutex_lock(&clocks_mutex);
  653. if (clk->parent)
  654. list_add(&clk->sibling, &clk->parent->children);
  655. else
  656. list_add(&clk->sibling, &root_clks);
  657. list_add(&clk->node, &clocks);
  658. if (clk->init)
  659. clk->init(clk);
  660. mutex_unlock(&clocks_mutex);
  661. return 0;
  662. }
  663. EXPORT_SYMBOL(clk_register);
  664. void clk_unregister(struct clk *clk)
  665. {
  666. if (clk == NULL || IS_ERR(clk))
  667. return;
  668. mutex_lock(&clocks_mutex);
  669. list_del(&clk->sibling);
  670. list_del(&clk->node);
  671. mutex_unlock(&clocks_mutex);
  672. }
  673. EXPORT_SYMBOL(clk_unregister);
  674. void clk_enable_init_clocks(void)
  675. {
  676. struct clk *clkp;
  677. list_for_each_entry(clkp, &clocks, node)
  678. if (clkp->flags & ENABLE_ON_INIT)
  679. clk_enable(clkp);
  680. }
  681. /**
  682. * omap_clk_get_by_name - locate OMAP struct clk by its name
  683. * @name: name of the struct clk to locate
  684. *
  685. * Locate an OMAP struct clk by its name. Assumes that struct clk
  686. * names are unique. Returns NULL if not found or a pointer to the
  687. * struct clk if found.
  688. */
  689. struct clk *omap_clk_get_by_name(const char *name)
  690. {
  691. struct clk *c;
  692. struct clk *ret = NULL;
  693. mutex_lock(&clocks_mutex);
  694. list_for_each_entry(c, &clocks, node) {
  695. if (!strcmp(c->name, name)) {
  696. ret = c;
  697. break;
  698. }
  699. }
  700. mutex_unlock(&clocks_mutex);
  701. return ret;
  702. }
  703. int omap_clk_enable_autoidle_all(void)
  704. {
  705. struct clk *c;
  706. unsigned long flags;
  707. spin_lock_irqsave(&clockfw_lock, flags);
  708. list_for_each_entry(c, &clocks, node)
  709. if (c->ops->allow_idle)
  710. c->ops->allow_idle(c);
  711. spin_unlock_irqrestore(&clockfw_lock, flags);
  712. return 0;
  713. }
  714. int omap_clk_disable_autoidle_all(void)
  715. {
  716. struct clk *c;
  717. unsigned long flags;
  718. spin_lock_irqsave(&clockfw_lock, flags);
  719. list_for_each_entry(c, &clocks, node)
  720. if (c->ops->deny_idle)
  721. c->ops->deny_idle(c);
  722. spin_unlock_irqrestore(&clockfw_lock, flags);
  723. return 0;
  724. }
  725. /*
  726. * Low level helpers
  727. */
  728. static int clkll_enable_null(struct clk *clk)
  729. {
  730. return 0;
  731. }
  732. static void clkll_disable_null(struct clk *clk)
  733. {
  734. }
  735. const struct clkops clkops_null = {
  736. .enable = clkll_enable_null,
  737. .disable = clkll_disable_null,
  738. };
  739. /*
  740. * Dummy clock
  741. *
  742. * Used for clock aliases that are needed on some OMAPs, but not others
  743. */
  744. struct clk dummy_ck = {
  745. .name = "dummy",
  746. .ops = &clkops_null,
  747. };
  748. /*
  749. *
  750. */
  751. #ifdef CONFIG_OMAP_RESET_CLOCKS
  752. /*
  753. * Disable any unused clocks left on by the bootloader
  754. */
  755. static int __init clk_disable_unused(void)
  756. {
  757. struct clk *ck;
  758. unsigned long flags;
  759. pr_info("clock: disabling unused clocks to save power\n");
  760. spin_lock_irqsave(&clockfw_lock, flags);
  761. list_for_each_entry(ck, &clocks, node) {
  762. if (ck->ops == &clkops_null)
  763. continue;
  764. if (ck->usecount > 0 || !ck->enable_reg)
  765. continue;
  766. omap1_clk_disable_unused(ck);
  767. }
  768. spin_unlock_irqrestore(&clockfw_lock, flags);
  769. return 0;
  770. }
  771. late_initcall(clk_disable_unused);
  772. late_initcall(omap_clk_enable_autoidle_all);
  773. #endif
  774. #if defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS)
  775. /*
  776. * debugfs support to trace clock tree hierarchy and attributes
  777. */
  778. #include <linux/debugfs.h>
  779. #include <linux/seq_file.h>
  780. static struct dentry *clk_debugfs_root;
  781. static int clk_dbg_show_summary(struct seq_file *s, void *unused)
  782. {
  783. struct clk *c;
  784. struct clk *pa;
  785. mutex_lock(&clocks_mutex);
  786. seq_printf(s, "%-30s %-30s %-10s %s\n",
  787. "clock-name", "parent-name", "rate", "use-count");
  788. list_for_each_entry(c, &clocks, node) {
  789. pa = c->parent;
  790. seq_printf(s, "%-30s %-30s %-10lu %d\n",
  791. c->name, pa ? pa->name : "none", c->rate,
  792. c->usecount);
  793. }
  794. mutex_unlock(&clocks_mutex);
  795. return 0;
  796. }
  797. static int clk_dbg_open(struct inode *inode, struct file *file)
  798. {
  799. return single_open(file, clk_dbg_show_summary, inode->i_private);
  800. }
  801. static const struct file_operations debug_clock_fops = {
  802. .open = clk_dbg_open,
  803. .read = seq_read,
  804. .llseek = seq_lseek,
  805. .release = single_release,
  806. };
  807. static int clk_debugfs_register_one(struct clk *c)
  808. {
  809. int err;
  810. struct dentry *d;
  811. struct clk *pa = c->parent;
  812. d = debugfs_create_dir(c->name, pa ? pa->dent : clk_debugfs_root);
  813. if (!d)
  814. return -ENOMEM;
  815. c->dent = d;
  816. d = debugfs_create_u8("usecount", S_IRUGO, c->dent, &c->usecount);
  817. if (!d) {
  818. err = -ENOMEM;
  819. goto err_out;
  820. }
  821. d = debugfs_create_ulong("rate", S_IRUGO, c->dent, &c->rate);
  822. if (!d) {
  823. err = -ENOMEM;
  824. goto err_out;
  825. }
  826. d = debugfs_create_x8("flags", S_IRUGO, c->dent, &c->flags);
  827. if (!d) {
  828. err = -ENOMEM;
  829. goto err_out;
  830. }
  831. return 0;
  832. err_out:
  833. debugfs_remove_recursive(c->dent);
  834. return err;
  835. }
  836. static int clk_debugfs_register(struct clk *c)
  837. {
  838. int err;
  839. struct clk *pa = c->parent;
  840. if (pa && !pa->dent) {
  841. err = clk_debugfs_register(pa);
  842. if (err)
  843. return err;
  844. }
  845. if (!c->dent) {
  846. err = clk_debugfs_register_one(c);
  847. if (err)
  848. return err;
  849. }
  850. return 0;
  851. }
  852. static int __init clk_debugfs_init(void)
  853. {
  854. struct clk *c;
  855. struct dentry *d;
  856. int err;
  857. d = debugfs_create_dir("clock", NULL);
  858. if (!d)
  859. return -ENOMEM;
  860. clk_debugfs_root = d;
  861. list_for_each_entry(c, &clocks, node) {
  862. err = clk_debugfs_register(c);
  863. if (err)
  864. goto err_out;
  865. }
  866. d = debugfs_create_file("summary", S_IRUGO,
  867. d, NULL, &debug_clock_fops);
  868. if (!d)
  869. return -ENOMEM;
  870. return 0;
  871. err_out:
  872. debugfs_remove_recursive(clk_debugfs_root);
  873. return err;
  874. }
  875. late_initcall(clk_debugfs_init);
  876. #endif /* defined(CONFIG_PM_DEBUG) && defined(CONFIG_DEBUG_FS) */