clock.c 23 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * arch/arm/plat-spear/clock.c
  3. *
  4. * Clock framework for SPEAr platform
  5. *
  6. * Copyright (C) 2009 ST Microelectronics
  7. * Viresh Kumar<viresh.kumar@st.com>
  8. *
  9. * This file is licensed under the terms of the GNU General Public
  10. * License version 2. This program is licensed "as is" without any
  11. * warranty of any kind, whether express or implied.
  12. */
  13. #include <linux/bug.h>
  14. #include <linux/clk.h>
  15. #include <linux/debugfs.h>
  16. #include <linux/err.h>
  17. #include <linux/io.h>
  18. #include <linux/list.h>
  19. #include <linux/module.h>
  20. #include <linux/spinlock.h>
  21. #include <plat/clock.h>
  22. static DEFINE_SPINLOCK(clocks_lock);
  23. static LIST_HEAD(root_clks);
  24. #ifdef CONFIG_DEBUG_FS
  25. static LIST_HEAD(clocks);
  26. #endif
  27. static void propagate_rate(struct clk *, int on_init);
  28. #ifdef CONFIG_DEBUG_FS
  29. static int clk_debugfs_reparent(struct clk *);
  30. #endif
  31. static int generic_clk_enable(struct clk *clk)
  32. {
  33. unsigned int val;
  34. if (!clk->en_reg)
  35. return -EFAULT;
  36. val = readl(clk->en_reg);
  37. if (unlikely(clk->flags & RESET_TO_ENABLE))
  38. val &= ~(1 << clk->en_reg_bit);
  39. else
  40. val |= 1 << clk->en_reg_bit;
  41. writel(val, clk->en_reg);
  42. return 0;
  43. }
  44. static void generic_clk_disable(struct clk *clk)
  45. {
  46. unsigned int val;
  47. if (!clk->en_reg)
  48. return;
  49. val = readl(clk->en_reg);
  50. if (unlikely(clk->flags & RESET_TO_ENABLE))
  51. val |= 1 << clk->en_reg_bit;
  52. else
  53. val &= ~(1 << clk->en_reg_bit);
  54. writel(val, clk->en_reg);
  55. }
  56. /* generic clk ops */
  57. static struct clkops generic_clkops = {
  58. .enable = generic_clk_enable,
  59. .disable = generic_clk_disable,
  60. };
  61. /* returns current programmed clocks clock info structure */
  62. static struct pclk_info *pclk_info_get(struct clk *clk)
  63. {
  64. unsigned int val, i;
  65. struct pclk_info *info = NULL;
  66. val = (readl(clk->pclk_sel->pclk_sel_reg) >> clk->pclk_sel_shift)
  67. & clk->pclk_sel->pclk_sel_mask;
  68. for (i = 0; i < clk->pclk_sel->pclk_count; i++) {
  69. if (clk->pclk_sel->pclk_info[i].pclk_val == val)
  70. info = &clk->pclk_sel->pclk_info[i];
  71. }
  72. return info;
  73. }
  74. /*
  75. * Set Update pclk, and pclk_info of clk and add clock sibling node to current
  76. * parents children list
  77. */
  78. static void clk_reparent(struct clk *clk, struct pclk_info *pclk_info)
  79. {
  80. unsigned long flags;
  81. spin_lock_irqsave(&clocks_lock, flags);
  82. list_del(&clk->sibling);
  83. list_add(&clk->sibling, &pclk_info->pclk->children);
  84. clk->pclk = pclk_info->pclk;
  85. spin_unlock_irqrestore(&clocks_lock, flags);
  86. #ifdef CONFIG_DEBUG_FS
  87. clk_debugfs_reparent(clk);
  88. #endif
  89. }
  90. static void do_clk_disable(struct clk *clk)
  91. {
  92. if (!clk)
  93. return;
  94. if (!clk->usage_count) {
  95. WARN_ON(1);
  96. return;
  97. }
  98. clk->usage_count--;
  99. if (clk->usage_count == 0) {
  100. /*
  101. * Surely, there are no active childrens or direct users
  102. * of this clock
  103. */
  104. if (clk->pclk)
  105. do_clk_disable(clk->pclk);
  106. if (clk->ops && clk->ops->disable)
  107. clk->ops->disable(clk);
  108. }
  109. }
  110. static int do_clk_enable(struct clk *clk)
  111. {
  112. int ret = 0;
  113. if (!clk)
  114. return -EFAULT;
  115. if (clk->usage_count == 0) {
  116. if (clk->pclk) {
  117. ret = do_clk_enable(clk->pclk);
  118. if (ret)
  119. goto err;
  120. }
  121. if (clk->ops && clk->ops->enable) {
  122. ret = clk->ops->enable(clk);
  123. if (ret) {
  124. if (clk->pclk)
  125. do_clk_disable(clk->pclk);
  126. goto err;
  127. }
  128. }
  129. /*
  130. * Since the clock is going to be used for the first
  131. * time please reclac
  132. */
  133. if (clk->recalc) {
  134. ret = clk->recalc(clk);
  135. if (ret)
  136. goto err;
  137. }
  138. }
  139. clk->usage_count++;
  140. err:
  141. return ret;
  142. }
  143. /*
  144. * clk_enable - inform the system when the clock source should be running.
  145. * @clk: clock source
  146. *
  147. * If the clock can not be enabled/disabled, this should return success.
  148. *
  149. * Returns success (0) or negative errno.
  150. */
  151. int clk_enable(struct clk *clk)
  152. {
  153. unsigned long flags;
  154. int ret = 0;
  155. spin_lock_irqsave(&clocks_lock, flags);
  156. ret = do_clk_enable(clk);
  157. spin_unlock_irqrestore(&clocks_lock, flags);
  158. return ret;
  159. }
  160. EXPORT_SYMBOL(clk_enable);
  161. /*
  162. * clk_disable - inform the system when the clock source is no longer required.
  163. * @clk: clock source
  164. *
  165. * Inform the system that a clock source is no longer required by
  166. * a driver and may be shut down.
  167. *
  168. * Implementation detail: if the clock source is shared between
  169. * multiple drivers, clk_enable() calls must be balanced by the
  170. * same number of clk_disable() calls for the clock source to be
  171. * disabled.
  172. */
  173. void clk_disable(struct clk *clk)
  174. {
  175. unsigned long flags;
  176. spin_lock_irqsave(&clocks_lock, flags);
  177. do_clk_disable(clk);
  178. spin_unlock_irqrestore(&clocks_lock, flags);
  179. }
  180. EXPORT_SYMBOL(clk_disable);
  181. /**
  182. * clk_get_rate - obtain the current clock rate (in Hz) for a clock source.
  183. * This is only valid once the clock source has been enabled.
  184. * @clk: clock source
  185. */
  186. unsigned long clk_get_rate(struct clk *clk)
  187. {
  188. unsigned long flags, rate;
  189. spin_lock_irqsave(&clocks_lock, flags);
  190. rate = clk->rate;
  191. spin_unlock_irqrestore(&clocks_lock, flags);
  192. return rate;
  193. }
  194. EXPORT_SYMBOL(clk_get_rate);
  195. /**
  196. * clk_set_parent - set the parent clock source for this clock
  197. * @clk: clock source
  198. * @parent: parent clock source
  199. *
  200. * Returns success (0) or negative errno.
  201. */
  202. int clk_set_parent(struct clk *clk, struct clk *parent)
  203. {
  204. int i, found = 0, val = 0;
  205. unsigned long flags;
  206. if (!clk || !parent)
  207. return -EFAULT;
  208. if (clk->pclk == parent)
  209. return 0;
  210. if (!clk->pclk_sel)
  211. return -EPERM;
  212. /* check if requested parent is in clk parent list */
  213. for (i = 0; i < clk->pclk_sel->pclk_count; i++) {
  214. if (clk->pclk_sel->pclk_info[i].pclk == parent) {
  215. found = 1;
  216. break;
  217. }
  218. }
  219. if (!found)
  220. return -EINVAL;
  221. spin_lock_irqsave(&clocks_lock, flags);
  222. /* reflect parent change in hardware */
  223. val = readl(clk->pclk_sel->pclk_sel_reg);
  224. val &= ~(clk->pclk_sel->pclk_sel_mask << clk->pclk_sel_shift);
  225. val |= clk->pclk_sel->pclk_info[i].pclk_val << clk->pclk_sel_shift;
  226. writel(val, clk->pclk_sel->pclk_sel_reg);
  227. spin_unlock_irqrestore(&clocks_lock, flags);
  228. /* reflect parent change in software */
  229. clk_reparent(clk, &clk->pclk_sel->pclk_info[i]);
  230. propagate_rate(clk, 0);
  231. return 0;
  232. }
  233. EXPORT_SYMBOL(clk_set_parent);
  234. /**
  235. * clk_set_rate - set the clock rate for a clock source
  236. * @clk: clock source
  237. * @rate: desired clock rate in Hz
  238. *
  239. * Returns success (0) or negative errno.
  240. */
  241. int clk_set_rate(struct clk *clk, unsigned long rate)
  242. {
  243. unsigned long flags;
  244. int ret = -EINVAL;
  245. if (!clk || !rate)
  246. return -EFAULT;
  247. if (clk->set_rate) {
  248. spin_lock_irqsave(&clocks_lock, flags);
  249. ret = clk->set_rate(clk, rate);
  250. if (!ret)
  251. /* if successful -> propagate */
  252. propagate_rate(clk, 0);
  253. spin_unlock_irqrestore(&clocks_lock, flags);
  254. } else if (clk->pclk) {
  255. u32 mult = clk->div_factor ? clk->div_factor : 1;
  256. ret = clk_set_rate(clk->pclk, mult * rate);
  257. }
  258. return ret;
  259. }
  260. EXPORT_SYMBOL(clk_set_rate);
  261. /* registers clock in platform clock framework */
  262. void clk_register(struct clk_lookup *cl)
  263. {
  264. struct clk *clk;
  265. unsigned long flags;
  266. if (!cl || !cl->clk)
  267. return;
  268. clk = cl->clk;
  269. spin_lock_irqsave(&clocks_lock, flags);
  270. INIT_LIST_HEAD(&clk->children);
  271. if (clk->flags & ALWAYS_ENABLED)
  272. clk->ops = NULL;
  273. else if (!clk->ops)
  274. clk->ops = &generic_clkops;
  275. /* root clock don't have any parents */
  276. if (!clk->pclk && !clk->pclk_sel) {
  277. list_add(&clk->sibling, &root_clks);
  278. } else if (clk->pclk && !clk->pclk_sel) {
  279. /* add clocks with only one parent to parent's children list */
  280. list_add(&clk->sibling, &clk->pclk->children);
  281. } else {
  282. /* clocks with more than one parent */
  283. struct pclk_info *pclk_info;
  284. pclk_info = pclk_info_get(clk);
  285. if (!pclk_info) {
  286. pr_err("CLKDEV: invalid pclk info of clk with"
  287. " %s dev_id and %s con_id\n",
  288. cl->dev_id, cl->con_id);
  289. } else {
  290. clk->pclk = pclk_info->pclk;
  291. list_add(&clk->sibling, &pclk_info->pclk->children);
  292. }
  293. }
  294. spin_unlock_irqrestore(&clocks_lock, flags);
  295. /* debugfs specific */
  296. #ifdef CONFIG_DEBUG_FS
  297. list_add(&clk->node, &clocks);
  298. clk->cl = cl;
  299. #endif
  300. /* add clock to arm clockdev framework */
  301. clkdev_add(cl);
  302. }
  303. /**
  304. * propagate_rate - recalculate and propagate all clocks to children
  305. * @pclk: parent clock required to be propogated
  306. * @on_init: flag for enabling clocks which are ENABLED_ON_INIT.
  307. *
  308. * Recalculates all children clocks
  309. */
  310. void propagate_rate(struct clk *pclk, int on_init)
  311. {
  312. struct clk *clk, *_temp;
  313. int ret = 0;
  314. list_for_each_entry_safe(clk, _temp, &pclk->children, sibling) {
  315. if (clk->recalc) {
  316. ret = clk->recalc(clk);
  317. /*
  318. * recalc will return error if clk out is not programmed
  319. * In this case configure default rate.
  320. */
  321. if (ret && clk->set_rate)
  322. clk->set_rate(clk, 0);
  323. }
  324. propagate_rate(clk, on_init);
  325. if (!on_init)
  326. continue;
  327. /* Enable clks enabled on init, in software view */
  328. if (clk->flags & ENABLED_ON_INIT)
  329. do_clk_enable(clk);
  330. }
  331. }
  332. /**
  333. * round_rate_index - return closest programmable rate index in rate_config tbl
  334. * @clk: ptr to clock structure
  335. * @drate: desired rate
  336. * @rate: final rate will be returned in this variable only.
  337. *
  338. * Finds index in rate_config for highest clk rate which is less than
  339. * requested rate. If there is no clk rate lesser than requested rate then
  340. * -EINVAL is returned. This routine assumes that rate_config is written
  341. * in incrementing order of clk rates.
  342. * If drate passed is zero then default rate is programmed.
  343. */
  344. static int
  345. round_rate_index(struct clk *clk, unsigned long drate, unsigned long *rate)
  346. {
  347. unsigned long tmp = 0, prev_rate = 0;
  348. int index;
  349. if (!clk->calc_rate)
  350. return -EFAULT;
  351. if (!drate)
  352. return -EINVAL;
  353. /*
  354. * This loops ends on two conditions:
  355. * - as soon as clk is found with rate greater than requested rate.
  356. * - if all clks in rate_config are smaller than requested rate.
  357. */
  358. for (index = 0; index < clk->rate_config.count; index++) {
  359. prev_rate = tmp;
  360. tmp = clk->calc_rate(clk, index);
  361. if (drate < tmp) {
  362. index--;
  363. break;
  364. }
  365. }
  366. /* return if can't find suitable clock */
  367. if (index < 0) {
  368. index = -EINVAL;
  369. *rate = 0;
  370. } else if (index == clk->rate_config.count) {
  371. /* program with highest clk rate possible */
  372. index = clk->rate_config.count - 1;
  373. *rate = tmp;
  374. } else
  375. *rate = prev_rate;
  376. return index;
  377. }
  378. /**
  379. * clk_round_rate - adjust a rate to the exact rate a clock can provide
  380. * @clk: clock source
  381. * @rate: desired clock rate in Hz
  382. *
  383. * Returns rounded clock rate in Hz, or negative errno.
  384. */
  385. long clk_round_rate(struct clk *clk, unsigned long drate)
  386. {
  387. long rate = 0;
  388. int index;
  389. /*
  390. * propagate call to parent who supports calc_rate. Similar approach is
  391. * used in clk_set_rate.
  392. */
  393. if (!clk->calc_rate) {
  394. u32 mult;
  395. if (!clk->pclk)
  396. return clk->rate;
  397. mult = clk->div_factor ? clk->div_factor : 1;
  398. return clk_round_rate(clk->pclk, mult * drate) / mult;
  399. }
  400. index = round_rate_index(clk, drate, &rate);
  401. if (index >= 0)
  402. return rate;
  403. else
  404. return index;
  405. }
  406. EXPORT_SYMBOL(clk_round_rate);
  407. /*All below functions are called with lock held */
  408. /*
  409. * Calculates pll clk rate for specific value of mode, m, n and p
  410. *
  411. * In normal mode
  412. * rate = (2 * M[15:8] * Fin)/(N * 2^P)
  413. *
  414. * In Dithered mode
  415. * rate = (2 * M[15:0] * Fin)/(256 * N * 2^P)
  416. */
  417. unsigned long pll_calc_rate(struct clk *clk, int index)
  418. {
  419. unsigned long rate = clk->pclk->rate;
  420. struct pll_rate_tbl *tbls = clk->rate_config.tbls;
  421. unsigned int mode;
  422. mode = tbls[index].mode ? 256 : 1;
  423. return (((2 * rate / 10000) * tbls[index].m) /
  424. (mode * tbls[index].n * (1 << tbls[index].p))) * 10000;
  425. }
  426. /*
  427. * calculates current programmed rate of pll1
  428. *
  429. * In normal mode
  430. * rate = (2 * M[15:8] * Fin)/(N * 2^P)
  431. *
  432. * In Dithered mode
  433. * rate = (2 * M[15:0] * Fin)/(256 * N * 2^P)
  434. */
  435. int pll_clk_recalc(struct clk *clk)
  436. {
  437. struct pll_clk_config *config = clk->private_data;
  438. unsigned int num = 2, den = 0, val, mode = 0;
  439. mode = (readl(config->mode_reg) >> config->masks->mode_shift) &
  440. config->masks->mode_mask;
  441. val = readl(config->cfg_reg);
  442. /* calculate denominator */
  443. den = (val >> config->masks->div_p_shift) & config->masks->div_p_mask;
  444. den = 1 << den;
  445. den *= (val >> config->masks->div_n_shift) & config->masks->div_n_mask;
  446. /* calculate numerator & denominator */
  447. if (!mode) {
  448. /* Normal mode */
  449. num *= (val >> config->masks->norm_fdbk_m_shift) &
  450. config->masks->norm_fdbk_m_mask;
  451. } else {
  452. /* Dithered mode */
  453. num *= (val >> config->masks->dith_fdbk_m_shift) &
  454. config->masks->dith_fdbk_m_mask;
  455. den *= 256;
  456. }
  457. if (!den)
  458. return -EINVAL;
  459. clk->rate = (((clk->pclk->rate/10000) * num) / den) * 10000;
  460. return 0;
  461. }
  462. /*
  463. * Configures new clock rate of pll
  464. */
  465. int pll_clk_set_rate(struct clk *clk, unsigned long desired_rate)
  466. {
  467. struct pll_rate_tbl *tbls = clk->rate_config.tbls;
  468. struct pll_clk_config *config = clk->private_data;
  469. unsigned long val, rate;
  470. int i;
  471. i = round_rate_index(clk, desired_rate, &rate);
  472. if (i < 0)
  473. return i;
  474. val = readl(config->mode_reg) &
  475. ~(config->masks->mode_mask << config->masks->mode_shift);
  476. val |= (tbls[i].mode & config->masks->mode_mask) <<
  477. config->masks->mode_shift;
  478. writel(val, config->mode_reg);
  479. val = readl(config->cfg_reg) &
  480. ~(config->masks->div_p_mask << config->masks->div_p_shift);
  481. val |= (tbls[i].p & config->masks->div_p_mask) <<
  482. config->masks->div_p_shift;
  483. val &= ~(config->masks->div_n_mask << config->masks->div_n_shift);
  484. val |= (tbls[i].n & config->masks->div_n_mask) <<
  485. config->masks->div_n_shift;
  486. val &= ~(config->masks->dith_fdbk_m_mask <<
  487. config->masks->dith_fdbk_m_shift);
  488. if (tbls[i].mode)
  489. val |= (tbls[i].m & config->masks->dith_fdbk_m_mask) <<
  490. config->masks->dith_fdbk_m_shift;
  491. else
  492. val |= (tbls[i].m & config->masks->norm_fdbk_m_mask) <<
  493. config->masks->norm_fdbk_m_shift;
  494. writel(val, config->cfg_reg);
  495. clk->rate = rate;
  496. return 0;
  497. }
  498. /*
  499. * Calculates ahb, apb clk rate for specific value of div
  500. */
  501. unsigned long bus_calc_rate(struct clk *clk, int index)
  502. {
  503. unsigned long rate = clk->pclk->rate;
  504. struct bus_rate_tbl *tbls = clk->rate_config.tbls;
  505. return rate / (tbls[index].div + 1);
  506. }
  507. /* calculates current programmed rate of ahb or apb bus */
  508. int bus_clk_recalc(struct clk *clk)
  509. {
  510. struct bus_clk_config *config = clk->private_data;
  511. unsigned int div;
  512. div = ((readl(config->reg) >> config->masks->shift) &
  513. config->masks->mask) + 1;
  514. if (!div)
  515. return -EINVAL;
  516. clk->rate = (unsigned long)clk->pclk->rate / div;
  517. return 0;
  518. }
  519. /* Configures new clock rate of AHB OR APB bus */
  520. int bus_clk_set_rate(struct clk *clk, unsigned long desired_rate)
  521. {
  522. struct bus_rate_tbl *tbls = clk->rate_config.tbls;
  523. struct bus_clk_config *config = clk->private_data;
  524. unsigned long val, rate;
  525. int i;
  526. i = round_rate_index(clk, desired_rate, &rate);
  527. if (i < 0)
  528. return i;
  529. val = readl(config->reg) &
  530. ~(config->masks->mask << config->masks->shift);
  531. val |= (tbls[i].div & config->masks->mask) << config->masks->shift;
  532. writel(val, config->reg);
  533. clk->rate = rate;
  534. return 0;
  535. }
  536. /*
  537. * gives rate for different values of eq, x and y
  538. *
  539. * Fout from synthesizer can be given from two equations:
  540. * Fout1 = (Fin * X/Y)/2 EQ1
  541. * Fout2 = Fin * X/Y EQ2
  542. */
  543. unsigned long aux_calc_rate(struct clk *clk, int index)
  544. {
  545. unsigned long rate = clk->pclk->rate;
  546. struct aux_rate_tbl *tbls = clk->rate_config.tbls;
  547. u8 eq = tbls[index].eq ? 1 : 2;
  548. return (((rate/10000) * tbls[index].xscale) /
  549. (tbls[index].yscale * eq)) * 10000;
  550. }
  551. /*
  552. * calculates current programmed rate of auxiliary synthesizers
  553. * used by: UART, FIRDA
  554. *
  555. * Fout from synthesizer can be given from two equations:
  556. * Fout1 = (Fin * X/Y)/2
  557. * Fout2 = Fin * X/Y
  558. *
  559. * Selection of eqn 1 or 2 is programmed in register
  560. */
  561. int aux_clk_recalc(struct clk *clk)
  562. {
  563. struct aux_clk_config *config = clk->private_data;
  564. unsigned int num = 1, den = 1, val, eqn;
  565. val = readl(config->synth_reg);
  566. eqn = (val >> config->masks->eq_sel_shift) &
  567. config->masks->eq_sel_mask;
  568. if (eqn == config->masks->eq1_mask)
  569. den *= 2;
  570. /* calculate numerator */
  571. num = (val >> config->masks->xscale_sel_shift) &
  572. config->masks->xscale_sel_mask;
  573. /* calculate denominator */
  574. den *= (val >> config->masks->yscale_sel_shift) &
  575. config->masks->yscale_sel_mask;
  576. if (!den)
  577. return -EINVAL;
  578. clk->rate = (((clk->pclk->rate/10000) * num) / den) * 10000;
  579. return 0;
  580. }
  581. /* Configures new clock rate of auxiliary synthesizers used by: UART, FIRDA*/
  582. int aux_clk_set_rate(struct clk *clk, unsigned long desired_rate)
  583. {
  584. struct aux_rate_tbl *tbls = clk->rate_config.tbls;
  585. struct aux_clk_config *config = clk->private_data;
  586. unsigned long val, rate;
  587. int i;
  588. i = round_rate_index(clk, desired_rate, &rate);
  589. if (i < 0)
  590. return i;
  591. val = readl(config->synth_reg) &
  592. ~(config->masks->eq_sel_mask << config->masks->eq_sel_shift);
  593. val |= (tbls[i].eq & config->masks->eq_sel_mask) <<
  594. config->masks->eq_sel_shift;
  595. val &= ~(config->masks->xscale_sel_mask <<
  596. config->masks->xscale_sel_shift);
  597. val |= (tbls[i].xscale & config->masks->xscale_sel_mask) <<
  598. config->masks->xscale_sel_shift;
  599. val &= ~(config->masks->yscale_sel_mask <<
  600. config->masks->yscale_sel_shift);
  601. val |= (tbls[i].yscale & config->masks->yscale_sel_mask) <<
  602. config->masks->yscale_sel_shift;
  603. writel(val, config->synth_reg);
  604. clk->rate = rate;
  605. return 0;
  606. }
  607. /*
  608. * Calculates gpt clk rate for different values of mscale and nscale
  609. *
  610. * Fout= Fin/((2 ^ (N+1)) * (M+1))
  611. */
  612. unsigned long gpt_calc_rate(struct clk *clk, int index)
  613. {
  614. unsigned long rate = clk->pclk->rate;
  615. struct gpt_rate_tbl *tbls = clk->rate_config.tbls;
  616. return rate / ((1 << (tbls[index].nscale + 1)) *
  617. (tbls[index].mscale + 1));
  618. }
  619. /*
  620. * calculates current programmed rate of gpt synthesizers
  621. * Fout from synthesizer can be given from below equations:
  622. * Fout= Fin/((2 ^ (N+1)) * (M+1))
  623. */
  624. int gpt_clk_recalc(struct clk *clk)
  625. {
  626. struct gpt_clk_config *config = clk->private_data;
  627. unsigned int div = 1, val;
  628. val = readl(config->synth_reg);
  629. div += (val >> config->masks->mscale_sel_shift) &
  630. config->masks->mscale_sel_mask;
  631. div *= 1 << (((val >> config->masks->nscale_sel_shift) &
  632. config->masks->nscale_sel_mask) + 1);
  633. if (!div)
  634. return -EINVAL;
  635. clk->rate = (unsigned long)clk->pclk->rate / div;
  636. return 0;
  637. }
  638. /* Configures new clock rate of gptiliary synthesizers used by: UART, FIRDA*/
  639. int gpt_clk_set_rate(struct clk *clk, unsigned long desired_rate)
  640. {
  641. struct gpt_rate_tbl *tbls = clk->rate_config.tbls;
  642. struct gpt_clk_config *config = clk->private_data;
  643. unsigned long val, rate;
  644. int i;
  645. i = round_rate_index(clk, desired_rate, &rate);
  646. if (i < 0)
  647. return i;
  648. val = readl(config->synth_reg) & ~(config->masks->mscale_sel_mask <<
  649. config->masks->mscale_sel_shift);
  650. val |= (tbls[i].mscale & config->masks->mscale_sel_mask) <<
  651. config->masks->mscale_sel_shift;
  652. val &= ~(config->masks->nscale_sel_mask <<
  653. config->masks->nscale_sel_shift);
  654. val |= (tbls[i].nscale & config->masks->nscale_sel_mask) <<
  655. config->masks->nscale_sel_shift;
  656. writel(val, config->synth_reg);
  657. clk->rate = rate;
  658. return 0;
  659. }
  660. /*
  661. * Calculates clcd clk rate for different values of div
  662. *
  663. * Fout from synthesizer can be given from below equation:
  664. * Fout= Fin/2*div (division factor)
  665. * div is 17 bits:-
  666. * 0-13 (fractional part)
  667. * 14-16 (integer part)
  668. * To calculate Fout we left shift val by 14 bits and divide Fin by
  669. * complete div (including fractional part) and then right shift the
  670. * result by 14 places.
  671. */
  672. unsigned long clcd_calc_rate(struct clk *clk, int index)
  673. {
  674. unsigned long rate = clk->pclk->rate;
  675. struct clcd_rate_tbl *tbls = clk->rate_config.tbls;
  676. rate /= 1000;
  677. rate <<= 12;
  678. rate /= (2 * tbls[index].div);
  679. rate >>= 12;
  680. rate *= 1000;
  681. return rate;
  682. }
  683. /*
  684. * calculates current programmed rate of clcd synthesizer
  685. * Fout from synthesizer can be given from below equation:
  686. * Fout= Fin/2*div (division factor)
  687. * div is 17 bits:-
  688. * 0-13 (fractional part)
  689. * 14-16 (integer part)
  690. * To calculate Fout we left shift val by 14 bits and divide Fin by
  691. * complete div (including fractional part) and then right shift the
  692. * result by 14 places.
  693. */
  694. int clcd_clk_recalc(struct clk *clk)
  695. {
  696. struct clcd_clk_config *config = clk->private_data;
  697. unsigned int div = 1;
  698. unsigned long prate;
  699. unsigned int val;
  700. val = readl(config->synth_reg);
  701. div = (val >> config->masks->div_factor_shift) &
  702. config->masks->div_factor_mask;
  703. if (!div)
  704. return -EINVAL;
  705. prate = clk->pclk->rate / 1000; /* first level division, make it KHz */
  706. clk->rate = (((unsigned long)prate << 12) / (2 * div)) >> 12;
  707. clk->rate *= 1000;
  708. return 0;
  709. }
  710. /* Configures new clock rate of auxiliary synthesizers used by: UART, FIRDA*/
  711. int clcd_clk_set_rate(struct clk *clk, unsigned long desired_rate)
  712. {
  713. struct clcd_rate_tbl *tbls = clk->rate_config.tbls;
  714. struct clcd_clk_config *config = clk->private_data;
  715. unsigned long val, rate;
  716. int i;
  717. i = round_rate_index(clk, desired_rate, &rate);
  718. if (i < 0)
  719. return i;
  720. val = readl(config->synth_reg) & ~(config->masks->div_factor_mask <<
  721. config->masks->div_factor_shift);
  722. val |= (tbls[i].div & config->masks->div_factor_mask) <<
  723. config->masks->div_factor_shift;
  724. writel(val, config->synth_reg);
  725. clk->rate = rate;
  726. return 0;
  727. }
  728. /*
  729. * Used for clocks that always have value as the parent clock divided by a
  730. * fixed divisor
  731. */
  732. int follow_parent(struct clk *clk)
  733. {
  734. unsigned int div_factor = (clk->div_factor < 1) ? 1 : clk->div_factor;
  735. clk->rate = clk->pclk->rate/div_factor;
  736. return 0;
  737. }
  738. /**
  739. * recalc_root_clocks - recalculate and propagate all root clocks
  740. *
  741. * Recalculates all root clocks (clocks with no parent), which if the
  742. * clock's .recalc is set correctly, should also propagate their rates.
  743. */
  744. void recalc_root_clocks(void)
  745. {
  746. struct clk *pclk;
  747. unsigned long flags;
  748. int ret = 0;
  749. spin_lock_irqsave(&clocks_lock, flags);
  750. list_for_each_entry(pclk, &root_clks, sibling) {
  751. if (pclk->recalc) {
  752. ret = pclk->recalc(pclk);
  753. /*
  754. * recalc will return error if clk out is not programmed
  755. * In this case configure default clock.
  756. */
  757. if (ret && pclk->set_rate)
  758. pclk->set_rate(pclk, 0);
  759. }
  760. propagate_rate(pclk, 1);
  761. /* Enable clks enabled on init, in software view */
  762. if (pclk->flags & ENABLED_ON_INIT)
  763. do_clk_enable(pclk);
  764. }
  765. spin_unlock_irqrestore(&clocks_lock, flags);
  766. }
  767. void __init clk_init(void)
  768. {
  769. recalc_root_clocks();
  770. }
  771. #ifdef CONFIG_DEBUG_FS
  772. /*
  773. * debugfs support to trace clock tree hierarchy and attributes
  774. */
  775. static struct dentry *clk_debugfs_root;
  776. static int clk_debugfs_register_one(struct clk *c)
  777. {
  778. int err;
  779. struct dentry *d;
  780. struct clk *pa = c->pclk;
  781. char s[255];
  782. char *p = s;
  783. if (c) {
  784. if (c->cl->con_id)
  785. p += sprintf(p, "%s", c->cl->con_id);
  786. if (c->cl->dev_id)
  787. p += sprintf(p, "%s", c->cl->dev_id);
  788. }
  789. d = debugfs_create_dir(s, pa ? pa->dent : clk_debugfs_root);
  790. if (!d)
  791. return -ENOMEM;
  792. c->dent = d;
  793. d = debugfs_create_u32("usage_count", S_IRUGO, c->dent,
  794. (u32 *)&c->usage_count);
  795. if (!d) {
  796. err = -ENOMEM;
  797. goto err_out;
  798. }
  799. d = debugfs_create_u32("rate", S_IRUGO, c->dent, (u32 *)&c->rate);
  800. if (!d) {
  801. err = -ENOMEM;
  802. goto err_out;
  803. }
  804. d = debugfs_create_x32("flags", S_IRUGO, c->dent, (u32 *)&c->flags);
  805. if (!d) {
  806. err = -ENOMEM;
  807. goto err_out;
  808. }
  809. return 0;
  810. err_out:
  811. debugfs_remove_recursive(c->dent);
  812. return err;
  813. }
  814. static int clk_debugfs_register(struct clk *c)
  815. {
  816. int err;
  817. struct clk *pa = c->pclk;
  818. if (pa && !pa->dent) {
  819. err = clk_debugfs_register(pa);
  820. if (err)
  821. return err;
  822. }
  823. if (!c->dent) {
  824. err = clk_debugfs_register_one(c);
  825. if (err)
  826. return err;
  827. }
  828. return 0;
  829. }
  830. static int __init clk_debugfs_init(void)
  831. {
  832. struct clk *c;
  833. struct dentry *d;
  834. int err;
  835. d = debugfs_create_dir("clock", NULL);
  836. if (!d)
  837. return -ENOMEM;
  838. clk_debugfs_root = d;
  839. list_for_each_entry(c, &clocks, node) {
  840. err = clk_debugfs_register(c);
  841. if (err)
  842. goto err_out;
  843. }
  844. return 0;
  845. err_out:
  846. debugfs_remove_recursive(clk_debugfs_root);
  847. return err;
  848. }
  849. late_initcall(clk_debugfs_init);
  850. static int clk_debugfs_reparent(struct clk *c)
  851. {
  852. debugfs_remove(c->dent);
  853. return clk_debugfs_register_one(c);
  854. }
  855. #endif /* CONFIG_DEBUG_FS */