clock.c 21 KB

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