divider.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710
  1. /*
  2. * TI Divider Clock
  3. *
  4. * Copyright (C) 2013 Texas Instruments, Inc.
  5. *
  6. * Tero Kristo <t-kristo@ti.com>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed "as is" WITHOUT ANY WARRANTY of any
  13. * kind, whether express or implied; without even the implied warranty
  14. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk-provider.h>
  18. #include <linux/slab.h>
  19. #include <linux/err.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/clk/ti.h>
  23. #include "clock.h"
  24. #undef pr_fmt
  25. #define pr_fmt(fmt) "%s: " fmt, __func__
  26. #define div_mask(d) ((1 << ((d)->width)) - 1)
  27. static unsigned int _get_table_maxdiv(const struct clk_div_table *table)
  28. {
  29. unsigned int maxdiv = 0;
  30. const struct clk_div_table *clkt;
  31. for (clkt = table; clkt->div; clkt++)
  32. if (clkt->div > maxdiv)
  33. maxdiv = clkt->div;
  34. return maxdiv;
  35. }
  36. static unsigned int _get_maxdiv(struct clk_omap_divider *divider)
  37. {
  38. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  39. return div_mask(divider);
  40. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  41. return 1 << div_mask(divider);
  42. if (divider->table)
  43. return _get_table_maxdiv(divider->table);
  44. return div_mask(divider) + 1;
  45. }
  46. static unsigned int _get_table_div(const struct clk_div_table *table,
  47. unsigned int val)
  48. {
  49. const struct clk_div_table *clkt;
  50. for (clkt = table; clkt->div; clkt++)
  51. if (clkt->val == val)
  52. return clkt->div;
  53. return 0;
  54. }
  55. static unsigned int _get_div(struct clk_omap_divider *divider, unsigned int val)
  56. {
  57. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  58. return val;
  59. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  60. return 1 << val;
  61. if (divider->table)
  62. return _get_table_div(divider->table, val);
  63. return val + 1;
  64. }
  65. static unsigned int _get_table_val(const struct clk_div_table *table,
  66. unsigned int div)
  67. {
  68. const struct clk_div_table *clkt;
  69. for (clkt = table; clkt->div; clkt++)
  70. if (clkt->div == div)
  71. return clkt->val;
  72. return 0;
  73. }
  74. static unsigned int _get_val(struct clk_omap_divider *divider, u8 div)
  75. {
  76. if (divider->flags & CLK_DIVIDER_ONE_BASED)
  77. return div;
  78. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  79. return __ffs(div);
  80. if (divider->table)
  81. return _get_table_val(divider->table, div);
  82. return div - 1;
  83. }
  84. static unsigned long ti_clk_divider_recalc_rate(struct clk_hw *hw,
  85. unsigned long parent_rate)
  86. {
  87. struct clk_omap_divider *divider = to_clk_omap_divider(hw);
  88. unsigned int div, val;
  89. val = ti_clk_ll_ops->clk_readl(&divider->reg) >> divider->shift;
  90. val &= div_mask(divider);
  91. div = _get_div(divider, val);
  92. if (!div) {
  93. WARN(!(divider->flags & CLK_DIVIDER_ALLOW_ZERO),
  94. "%s: Zero divisor and CLK_DIVIDER_ALLOW_ZERO not set\n",
  95. clk_hw_get_name(hw));
  96. return parent_rate;
  97. }
  98. return DIV_ROUND_UP(parent_rate, div);
  99. }
  100. /*
  101. * The reverse of DIV_ROUND_UP: The maximum number which
  102. * divided by m is r
  103. */
  104. #define MULT_ROUND_UP(r, m) ((r) * (m) + (m) - 1)
  105. static bool _is_valid_table_div(const struct clk_div_table *table,
  106. unsigned int div)
  107. {
  108. const struct clk_div_table *clkt;
  109. for (clkt = table; clkt->div; clkt++)
  110. if (clkt->div == div)
  111. return true;
  112. return false;
  113. }
  114. static bool _is_valid_div(struct clk_omap_divider *divider, unsigned int div)
  115. {
  116. if (divider->flags & CLK_DIVIDER_POWER_OF_TWO)
  117. return is_power_of_2(div);
  118. if (divider->table)
  119. return _is_valid_table_div(divider->table, div);
  120. return true;
  121. }
  122. static int _div_round_up(const struct clk_div_table *table,
  123. unsigned long parent_rate, unsigned long rate)
  124. {
  125. const struct clk_div_table *clkt;
  126. int up = INT_MAX;
  127. int div = DIV_ROUND_UP_ULL((u64)parent_rate, rate);
  128. for (clkt = table; clkt->div; clkt++) {
  129. if (clkt->div == div)
  130. return clkt->div;
  131. else if (clkt->div < div)
  132. continue;
  133. if ((clkt->div - div) < (up - div))
  134. up = clkt->div;
  135. }
  136. return up;
  137. }
  138. static int _div_round(const struct clk_div_table *table,
  139. unsigned long parent_rate, unsigned long rate)
  140. {
  141. if (!table)
  142. return DIV_ROUND_UP(parent_rate, rate);
  143. return _div_round_up(table, parent_rate, rate);
  144. }
  145. static int ti_clk_divider_bestdiv(struct clk_hw *hw, unsigned long rate,
  146. unsigned long *best_parent_rate)
  147. {
  148. struct clk_omap_divider *divider = to_clk_omap_divider(hw);
  149. int i, bestdiv = 0;
  150. unsigned long parent_rate, best = 0, now, maxdiv;
  151. unsigned long parent_rate_saved = *best_parent_rate;
  152. if (!rate)
  153. rate = 1;
  154. maxdiv = _get_maxdiv(divider);
  155. if (!(clk_hw_get_flags(hw) & CLK_SET_RATE_PARENT)) {
  156. parent_rate = *best_parent_rate;
  157. bestdiv = _div_round(divider->table, parent_rate, rate);
  158. bestdiv = bestdiv == 0 ? 1 : bestdiv;
  159. bestdiv = bestdiv > maxdiv ? maxdiv : bestdiv;
  160. return bestdiv;
  161. }
  162. /*
  163. * The maximum divider we can use without overflowing
  164. * unsigned long in rate * i below
  165. */
  166. maxdiv = min(ULONG_MAX / rate, maxdiv);
  167. for (i = 1; i <= maxdiv; i++) {
  168. if (!_is_valid_div(divider, i))
  169. continue;
  170. if (rate * i == parent_rate_saved) {
  171. /*
  172. * It's the most ideal case if the requested rate can be
  173. * divided from parent clock without needing to change
  174. * parent rate, so return the divider immediately.
  175. */
  176. *best_parent_rate = parent_rate_saved;
  177. return i;
  178. }
  179. parent_rate = clk_hw_round_rate(clk_hw_get_parent(hw),
  180. MULT_ROUND_UP(rate, i));
  181. now = DIV_ROUND_UP(parent_rate, i);
  182. if (now <= rate && now > best) {
  183. bestdiv = i;
  184. best = now;
  185. *best_parent_rate = parent_rate;
  186. }
  187. }
  188. if (!bestdiv) {
  189. bestdiv = _get_maxdiv(divider);
  190. *best_parent_rate =
  191. clk_hw_round_rate(clk_hw_get_parent(hw), 1);
  192. }
  193. return bestdiv;
  194. }
  195. static long ti_clk_divider_round_rate(struct clk_hw *hw, unsigned long rate,
  196. unsigned long *prate)
  197. {
  198. int div;
  199. div = ti_clk_divider_bestdiv(hw, rate, prate);
  200. return DIV_ROUND_UP(*prate, div);
  201. }
  202. static int ti_clk_divider_set_rate(struct clk_hw *hw, unsigned long rate,
  203. unsigned long parent_rate)
  204. {
  205. struct clk_omap_divider *divider;
  206. unsigned int div, value;
  207. u32 val;
  208. if (!hw || !rate)
  209. return -EINVAL;
  210. divider = to_clk_omap_divider(hw);
  211. div = DIV_ROUND_UP(parent_rate, rate);
  212. value = _get_val(divider, div);
  213. if (value > div_mask(divider))
  214. value = div_mask(divider);
  215. if (divider->flags & CLK_DIVIDER_HIWORD_MASK) {
  216. val = div_mask(divider) << (divider->shift + 16);
  217. } else {
  218. val = ti_clk_ll_ops->clk_readl(&divider->reg);
  219. val &= ~(div_mask(divider) << divider->shift);
  220. }
  221. val |= value << divider->shift;
  222. ti_clk_ll_ops->clk_writel(val, &divider->reg);
  223. ti_clk_latch(&divider->reg, divider->latch);
  224. return 0;
  225. }
  226. /**
  227. * clk_divider_save_context - Save the divider value
  228. * @hw: pointer struct clk_hw
  229. *
  230. * Save the divider value
  231. */
  232. static int clk_divider_save_context(struct clk_hw *hw)
  233. {
  234. struct clk_omap_divider *divider = to_clk_omap_divider(hw);
  235. u32 val;
  236. val = ti_clk_ll_ops->clk_readl(&divider->reg) >> divider->shift;
  237. divider->context = val & div_mask(divider);
  238. return 0;
  239. }
  240. /**
  241. * clk_divider_restore_context - restore the saved the divider value
  242. * @hw: pointer struct clk_hw
  243. *
  244. * Restore the saved the divider value
  245. */
  246. static void clk_divider_restore_context(struct clk_hw *hw)
  247. {
  248. struct clk_omap_divider *divider = to_clk_omap_divider(hw);
  249. u32 val;
  250. val = ti_clk_ll_ops->clk_readl(&divider->reg);
  251. val &= ~(div_mask(divider) << divider->shift);
  252. val |= divider->context << divider->shift;
  253. ti_clk_ll_ops->clk_writel(val, &divider->reg);
  254. }
  255. const struct clk_ops ti_clk_divider_ops = {
  256. .recalc_rate = ti_clk_divider_recalc_rate,
  257. .round_rate = ti_clk_divider_round_rate,
  258. .set_rate = ti_clk_divider_set_rate,
  259. .save_context = clk_divider_save_context,
  260. .restore_context = clk_divider_restore_context,
  261. };
  262. static struct clk *_register_divider(struct device *dev, const char *name,
  263. const char *parent_name,
  264. unsigned long flags,
  265. struct clk_omap_reg *reg,
  266. u8 shift, u8 width, s8 latch,
  267. u8 clk_divider_flags,
  268. const struct clk_div_table *table)
  269. {
  270. struct clk_omap_divider *div;
  271. struct clk *clk;
  272. struct clk_init_data init;
  273. if (clk_divider_flags & CLK_DIVIDER_HIWORD_MASK) {
  274. if (width + shift > 16) {
  275. pr_warn("divider value exceeds LOWORD field\n");
  276. return ERR_PTR(-EINVAL);
  277. }
  278. }
  279. /* allocate the divider */
  280. div = kzalloc(sizeof(*div), GFP_KERNEL);
  281. if (!div) {
  282. pr_err("%s: could not allocate divider clk\n", __func__);
  283. return ERR_PTR(-ENOMEM);
  284. }
  285. init.name = name;
  286. init.ops = &ti_clk_divider_ops;
  287. init.flags = flags | CLK_IS_BASIC;
  288. init.parent_names = (parent_name ? &parent_name : NULL);
  289. init.num_parents = (parent_name ? 1 : 0);
  290. /* struct clk_divider assignments */
  291. memcpy(&div->reg, reg, sizeof(*reg));
  292. div->shift = shift;
  293. div->width = width;
  294. div->latch = latch;
  295. div->flags = clk_divider_flags;
  296. div->hw.init = &init;
  297. div->table = table;
  298. /* register the clock */
  299. clk = ti_clk_register(dev, &div->hw, name);
  300. if (IS_ERR(clk))
  301. kfree(div);
  302. return clk;
  303. }
  304. int ti_clk_parse_divider_data(int *div_table, int num_dividers, int max_div,
  305. u8 flags, u8 *width,
  306. const struct clk_div_table **table)
  307. {
  308. int valid_div = 0;
  309. u32 val;
  310. int div;
  311. int i;
  312. struct clk_div_table *tmp;
  313. if (!div_table) {
  314. if (flags & CLKF_INDEX_STARTS_AT_ONE)
  315. val = 1;
  316. else
  317. val = 0;
  318. div = 1;
  319. while (div < max_div) {
  320. if (flags & CLKF_INDEX_POWER_OF_TWO)
  321. div <<= 1;
  322. else
  323. div++;
  324. val++;
  325. }
  326. *width = fls(val);
  327. *table = NULL;
  328. return 0;
  329. }
  330. i = 0;
  331. while (!num_dividers || i < num_dividers) {
  332. if (div_table[i] == -1)
  333. break;
  334. if (div_table[i])
  335. valid_div++;
  336. i++;
  337. }
  338. num_dividers = i;
  339. tmp = kzalloc(sizeof(*tmp) * (valid_div + 1), GFP_KERNEL);
  340. if (!tmp)
  341. return -ENOMEM;
  342. valid_div = 0;
  343. *width = 0;
  344. for (i = 0; i < num_dividers; i++)
  345. if (div_table[i] > 0) {
  346. tmp[valid_div].div = div_table[i];
  347. tmp[valid_div].val = i;
  348. valid_div++;
  349. *width = i;
  350. }
  351. *width = fls(*width);
  352. *table = tmp;
  353. return 0;
  354. }
  355. static const struct clk_div_table *
  356. _get_div_table_from_setup(struct ti_clk_divider *setup, u8 *width)
  357. {
  358. const struct clk_div_table *table = NULL;
  359. ti_clk_parse_divider_data(setup->dividers, setup->num_dividers,
  360. setup->max_div, setup->flags, width,
  361. &table);
  362. return table;
  363. }
  364. struct clk_hw *ti_clk_build_component_div(struct ti_clk_divider *setup)
  365. {
  366. struct clk_omap_divider *div;
  367. struct clk_omap_reg *reg;
  368. if (!setup)
  369. return NULL;
  370. div = kzalloc(sizeof(*div), GFP_KERNEL);
  371. if (!div)
  372. return ERR_PTR(-ENOMEM);
  373. reg = (struct clk_omap_reg *)&div->reg;
  374. reg->index = setup->module;
  375. reg->offset = setup->reg;
  376. if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
  377. div->flags |= CLK_DIVIDER_ONE_BASED;
  378. if (setup->flags & CLKF_INDEX_POWER_OF_TWO)
  379. div->flags |= CLK_DIVIDER_POWER_OF_TWO;
  380. div->table = _get_div_table_from_setup(setup, &div->width);
  381. div->shift = setup->bit_shift;
  382. div->latch = -EINVAL;
  383. return &div->hw;
  384. }
  385. struct clk *ti_clk_register_divider(struct ti_clk *setup)
  386. {
  387. struct ti_clk_divider *div;
  388. struct clk_omap_reg *reg_setup;
  389. u32 reg;
  390. u8 width;
  391. u32 flags = 0;
  392. u8 div_flags = 0;
  393. const struct clk_div_table *table;
  394. struct clk *clk;
  395. div = setup->data;
  396. reg_setup = (struct clk_omap_reg *)&reg;
  397. reg_setup->index = div->module;
  398. reg_setup->offset = div->reg;
  399. if (div->flags & CLKF_INDEX_STARTS_AT_ONE)
  400. div_flags |= CLK_DIVIDER_ONE_BASED;
  401. if (div->flags & CLKF_INDEX_POWER_OF_TWO)
  402. div_flags |= CLK_DIVIDER_POWER_OF_TWO;
  403. if (div->flags & CLKF_SET_RATE_PARENT)
  404. flags |= CLK_SET_RATE_PARENT;
  405. table = _get_div_table_from_setup(div, &width);
  406. if (IS_ERR(table))
  407. return (struct clk *)table;
  408. clk = _register_divider(NULL, setup->name, div->parent,
  409. flags, (void __iomem *)reg, div->bit_shift,
  410. width, -EINVAL, div_flags, table);
  411. if (IS_ERR(clk))
  412. kfree(table);
  413. return clk;
  414. }
  415. static struct clk_div_table *
  416. __init ti_clk_get_div_table(struct device_node *node)
  417. {
  418. struct clk_div_table *table;
  419. const __be32 *divspec;
  420. u32 val;
  421. u32 num_div;
  422. u32 valid_div;
  423. int i;
  424. divspec = of_get_property(node, "ti,dividers", &num_div);
  425. if (!divspec)
  426. return NULL;
  427. num_div /= 4;
  428. valid_div = 0;
  429. /* Determine required size for divider table */
  430. for (i = 0; i < num_div; i++) {
  431. of_property_read_u32_index(node, "ti,dividers", i, &val);
  432. if (val)
  433. valid_div++;
  434. }
  435. if (!valid_div) {
  436. pr_err("no valid dividers for %s table\n", node->name);
  437. return ERR_PTR(-EINVAL);
  438. }
  439. table = kzalloc(sizeof(*table) * (valid_div + 1), GFP_KERNEL);
  440. if (!table)
  441. return ERR_PTR(-ENOMEM);
  442. valid_div = 0;
  443. for (i = 0; i < num_div; i++) {
  444. of_property_read_u32_index(node, "ti,dividers", i, &val);
  445. if (val) {
  446. table[valid_div].div = val;
  447. table[valid_div].val = i;
  448. valid_div++;
  449. }
  450. }
  451. return table;
  452. }
  453. static int _get_divider_width(struct device_node *node,
  454. const struct clk_div_table *table,
  455. u8 flags)
  456. {
  457. u32 min_div;
  458. u32 max_div;
  459. u32 val = 0;
  460. u32 div;
  461. if (!table) {
  462. /* Clk divider table not provided, determine min/max divs */
  463. if (of_property_read_u32(node, "ti,min-div", &min_div))
  464. min_div = 1;
  465. if (of_property_read_u32(node, "ti,max-div", &max_div)) {
  466. pr_err("no max-div for %s!\n", node->name);
  467. return -EINVAL;
  468. }
  469. /* Determine bit width for the field */
  470. if (flags & CLK_DIVIDER_ONE_BASED)
  471. val = 1;
  472. div = min_div;
  473. while (div < max_div) {
  474. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  475. div <<= 1;
  476. else
  477. div++;
  478. val++;
  479. }
  480. } else {
  481. div = 0;
  482. while (table[div].div) {
  483. val = table[div].val;
  484. div++;
  485. }
  486. }
  487. return fls(val);
  488. }
  489. static int __init ti_clk_divider_populate(struct device_node *node,
  490. struct clk_omap_reg *reg, const struct clk_div_table **table,
  491. u32 *flags, u8 *div_flags, u8 *width, u8 *shift, s8 *latch)
  492. {
  493. u32 val;
  494. int ret;
  495. ret = ti_clk_get_reg_addr(node, 0, reg);
  496. if (ret)
  497. return ret;
  498. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  499. *shift = val;
  500. else
  501. *shift = 0;
  502. if (latch) {
  503. if (!of_property_read_u32(node, "ti,latch-bit", &val))
  504. *latch = val;
  505. else
  506. *latch = -EINVAL;
  507. }
  508. *flags = 0;
  509. *div_flags = 0;
  510. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  511. *div_flags |= CLK_DIVIDER_ONE_BASED;
  512. if (of_property_read_bool(node, "ti,index-power-of-two"))
  513. *div_flags |= CLK_DIVIDER_POWER_OF_TWO;
  514. if (of_property_read_bool(node, "ti,set-rate-parent"))
  515. *flags |= CLK_SET_RATE_PARENT;
  516. *table = ti_clk_get_div_table(node);
  517. if (IS_ERR(*table))
  518. return PTR_ERR(*table);
  519. *width = _get_divider_width(node, *table, *div_flags);
  520. return 0;
  521. }
  522. /**
  523. * of_ti_divider_clk_setup - Setup function for simple div rate clock
  524. * @node: device node for this clock
  525. *
  526. * Sets up a basic divider clock.
  527. */
  528. static void __init of_ti_divider_clk_setup(struct device_node *node)
  529. {
  530. struct clk *clk;
  531. const char *parent_name;
  532. struct clk_omap_reg reg;
  533. u8 clk_divider_flags = 0;
  534. u8 width = 0;
  535. u8 shift = 0;
  536. s8 latch = -EINVAL;
  537. const struct clk_div_table *table = NULL;
  538. u32 flags = 0;
  539. parent_name = of_clk_get_parent_name(node, 0);
  540. if (ti_clk_divider_populate(node, &reg, &table, &flags,
  541. &clk_divider_flags, &width, &shift, &latch))
  542. goto cleanup;
  543. clk = _register_divider(NULL, node->name, parent_name, flags, &reg,
  544. shift, width, latch, clk_divider_flags, table);
  545. if (!IS_ERR(clk)) {
  546. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  547. of_ti_clk_autoidle_setup(node);
  548. return;
  549. }
  550. cleanup:
  551. kfree(table);
  552. }
  553. CLK_OF_DECLARE(divider_clk, "ti,divider-clock", of_ti_divider_clk_setup);
  554. static void __init of_ti_composite_divider_clk_setup(struct device_node *node)
  555. {
  556. struct clk_omap_divider *div;
  557. u32 val;
  558. div = kzalloc(sizeof(*div), GFP_KERNEL);
  559. if (!div)
  560. return;
  561. if (ti_clk_divider_populate(node, &div->reg, &div->table, &val,
  562. &div->flags, &div->width, &div->shift,
  563. NULL) < 0)
  564. goto cleanup;
  565. if (!ti_clk_add_component(node, &div->hw, CLK_COMPONENT_TYPE_DIVIDER))
  566. return;
  567. cleanup:
  568. kfree(div->table);
  569. kfree(div);
  570. }
  571. CLK_OF_DECLARE(ti_composite_divider_clk, "ti,composite-divider-clock",
  572. of_ti_composite_divider_clk_setup);