divider.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703
  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. return ERR_PTR(-ENOMEM);
  283. init.name = name;
  284. init.ops = &ti_clk_divider_ops;
  285. init.flags = flags | CLK_IS_BASIC;
  286. init.parent_names = (parent_name ? &parent_name : NULL);
  287. init.num_parents = (parent_name ? 1 : 0);
  288. /* struct clk_divider assignments */
  289. memcpy(&div->reg, reg, sizeof(*reg));
  290. div->shift = shift;
  291. div->width = width;
  292. div->latch = latch;
  293. div->flags = clk_divider_flags;
  294. div->hw.init = &init;
  295. div->table = table;
  296. /* register the clock */
  297. clk = ti_clk_register(dev, &div->hw, name);
  298. if (IS_ERR(clk))
  299. kfree(div);
  300. return clk;
  301. }
  302. int ti_clk_parse_divider_data(int *div_table, int num_dividers, int max_div,
  303. u8 flags, u8 *width,
  304. const struct clk_div_table **table)
  305. {
  306. int valid_div = 0;
  307. u32 val;
  308. int div;
  309. int i;
  310. struct clk_div_table *tmp;
  311. if (!div_table) {
  312. if (flags & CLKF_INDEX_STARTS_AT_ONE)
  313. val = 1;
  314. else
  315. val = 0;
  316. div = 1;
  317. while (div < max_div) {
  318. if (flags & CLKF_INDEX_POWER_OF_TWO)
  319. div <<= 1;
  320. else
  321. div++;
  322. val++;
  323. }
  324. *width = fls(val);
  325. *table = NULL;
  326. return 0;
  327. }
  328. i = 0;
  329. while (!num_dividers || i < num_dividers) {
  330. if (div_table[i] == -1)
  331. break;
  332. if (div_table[i])
  333. valid_div++;
  334. i++;
  335. }
  336. num_dividers = i;
  337. tmp = kcalloc(valid_div + 1, sizeof(*tmp), GFP_KERNEL);
  338. if (!tmp)
  339. return -ENOMEM;
  340. valid_div = 0;
  341. *width = 0;
  342. for (i = 0; i < num_dividers; i++)
  343. if (div_table[i] > 0) {
  344. tmp[valid_div].div = div_table[i];
  345. tmp[valid_div].val = i;
  346. valid_div++;
  347. *width = i;
  348. }
  349. *width = fls(*width);
  350. *table = tmp;
  351. return 0;
  352. }
  353. static const struct clk_div_table *
  354. _get_div_table_from_setup(struct ti_clk_divider *setup, u8 *width)
  355. {
  356. const struct clk_div_table *table = NULL;
  357. ti_clk_parse_divider_data(setup->dividers, setup->num_dividers,
  358. setup->max_div, setup->flags, width,
  359. &table);
  360. return table;
  361. }
  362. struct clk_hw *ti_clk_build_component_div(struct ti_clk_divider *setup)
  363. {
  364. struct clk_omap_divider *div;
  365. struct clk_omap_reg *reg;
  366. if (!setup)
  367. return NULL;
  368. div = kzalloc(sizeof(*div), GFP_KERNEL);
  369. if (!div)
  370. return ERR_PTR(-ENOMEM);
  371. reg = (struct clk_omap_reg *)&div->reg;
  372. reg->index = setup->module;
  373. reg->offset = setup->reg;
  374. if (setup->flags & CLKF_INDEX_STARTS_AT_ONE)
  375. div->flags |= CLK_DIVIDER_ONE_BASED;
  376. if (setup->flags & CLKF_INDEX_POWER_OF_TWO)
  377. div->flags |= CLK_DIVIDER_POWER_OF_TWO;
  378. div->table = _get_div_table_from_setup(setup, &div->width);
  379. div->shift = setup->bit_shift;
  380. div->latch = -EINVAL;
  381. return &div->hw;
  382. }
  383. struct clk *ti_clk_register_divider(struct ti_clk *setup)
  384. {
  385. struct ti_clk_divider *div = setup->data;
  386. struct clk_omap_reg reg = {
  387. .index = div->module,
  388. .offset = div->reg,
  389. };
  390. u8 width;
  391. u32 flags = 0;
  392. u8 div_flags = 0;
  393. const struct clk_div_table *table;
  394. struct clk *clk;
  395. if (div->flags & CLKF_INDEX_STARTS_AT_ONE)
  396. div_flags |= CLK_DIVIDER_ONE_BASED;
  397. if (div->flags & CLKF_INDEX_POWER_OF_TWO)
  398. div_flags |= CLK_DIVIDER_POWER_OF_TWO;
  399. if (div->flags & CLKF_SET_RATE_PARENT)
  400. flags |= CLK_SET_RATE_PARENT;
  401. table = _get_div_table_from_setup(div, &width);
  402. if (IS_ERR(table))
  403. return (struct clk *)table;
  404. clk = _register_divider(NULL, setup->name, div->parent,
  405. flags, &reg, div->bit_shift,
  406. width, -EINVAL, div_flags, table);
  407. if (IS_ERR(clk))
  408. kfree(table);
  409. return clk;
  410. }
  411. static struct clk_div_table *
  412. __init ti_clk_get_div_table(struct device_node *node)
  413. {
  414. struct clk_div_table *table;
  415. const __be32 *divspec;
  416. u32 val;
  417. u32 num_div;
  418. u32 valid_div;
  419. int i;
  420. divspec = of_get_property(node, "ti,dividers", &num_div);
  421. if (!divspec)
  422. return NULL;
  423. num_div /= 4;
  424. valid_div = 0;
  425. /* Determine required size for divider table */
  426. for (i = 0; i < num_div; i++) {
  427. of_property_read_u32_index(node, "ti,dividers", i, &val);
  428. if (val)
  429. valid_div++;
  430. }
  431. if (!valid_div) {
  432. pr_err("no valid dividers for %pOFn table\n", node);
  433. return ERR_PTR(-EINVAL);
  434. }
  435. table = kcalloc(valid_div + 1, sizeof(*table), GFP_KERNEL);
  436. if (!table)
  437. return ERR_PTR(-ENOMEM);
  438. valid_div = 0;
  439. for (i = 0; i < num_div; i++) {
  440. of_property_read_u32_index(node, "ti,dividers", i, &val);
  441. if (val) {
  442. table[valid_div].div = val;
  443. table[valid_div].val = i;
  444. valid_div++;
  445. }
  446. }
  447. return table;
  448. }
  449. static int _get_divider_width(struct device_node *node,
  450. const struct clk_div_table *table,
  451. u8 flags)
  452. {
  453. u32 min_div;
  454. u32 max_div;
  455. u32 val = 0;
  456. u32 div;
  457. if (!table) {
  458. /* Clk divider table not provided, determine min/max divs */
  459. if (of_property_read_u32(node, "ti,min-div", &min_div))
  460. min_div = 1;
  461. if (of_property_read_u32(node, "ti,max-div", &max_div)) {
  462. pr_err("no max-div for %pOFn!\n", node);
  463. return -EINVAL;
  464. }
  465. /* Determine bit width for the field */
  466. if (flags & CLK_DIVIDER_ONE_BASED)
  467. val = 1;
  468. div = min_div;
  469. while (div < max_div) {
  470. if (flags & CLK_DIVIDER_POWER_OF_TWO)
  471. div <<= 1;
  472. else
  473. div++;
  474. val++;
  475. }
  476. } else {
  477. div = 0;
  478. while (table[div].div) {
  479. val = table[div].val;
  480. div++;
  481. }
  482. }
  483. return fls(val);
  484. }
  485. static int __init ti_clk_divider_populate(struct device_node *node,
  486. struct clk_omap_reg *reg, const struct clk_div_table **table,
  487. u32 *flags, u8 *div_flags, u8 *width, u8 *shift, s8 *latch)
  488. {
  489. u32 val;
  490. int ret;
  491. ret = ti_clk_get_reg_addr(node, 0, reg);
  492. if (ret)
  493. return ret;
  494. if (!of_property_read_u32(node, "ti,bit-shift", &val))
  495. *shift = val;
  496. else
  497. *shift = 0;
  498. if (latch) {
  499. if (!of_property_read_u32(node, "ti,latch-bit", &val))
  500. *latch = val;
  501. else
  502. *latch = -EINVAL;
  503. }
  504. *flags = 0;
  505. *div_flags = 0;
  506. if (of_property_read_bool(node, "ti,index-starts-at-one"))
  507. *div_flags |= CLK_DIVIDER_ONE_BASED;
  508. if (of_property_read_bool(node, "ti,index-power-of-two"))
  509. *div_flags |= CLK_DIVIDER_POWER_OF_TWO;
  510. if (of_property_read_bool(node, "ti,set-rate-parent"))
  511. *flags |= CLK_SET_RATE_PARENT;
  512. *table = ti_clk_get_div_table(node);
  513. if (IS_ERR(*table))
  514. return PTR_ERR(*table);
  515. *width = _get_divider_width(node, *table, *div_flags);
  516. return 0;
  517. }
  518. /**
  519. * of_ti_divider_clk_setup - Setup function for simple div rate clock
  520. * @node: device node for this clock
  521. *
  522. * Sets up a basic divider clock.
  523. */
  524. static void __init of_ti_divider_clk_setup(struct device_node *node)
  525. {
  526. struct clk *clk;
  527. const char *parent_name;
  528. struct clk_omap_reg reg;
  529. u8 clk_divider_flags = 0;
  530. u8 width = 0;
  531. u8 shift = 0;
  532. s8 latch = -EINVAL;
  533. const struct clk_div_table *table = NULL;
  534. u32 flags = 0;
  535. parent_name = of_clk_get_parent_name(node, 0);
  536. if (ti_clk_divider_populate(node, &reg, &table, &flags,
  537. &clk_divider_flags, &width, &shift, &latch))
  538. goto cleanup;
  539. clk = _register_divider(NULL, node->name, parent_name, flags, &reg,
  540. shift, width, latch, clk_divider_flags, table);
  541. if (!IS_ERR(clk)) {
  542. of_clk_add_provider(node, of_clk_src_simple_get, clk);
  543. of_ti_clk_autoidle_setup(node);
  544. return;
  545. }
  546. cleanup:
  547. kfree(table);
  548. }
  549. CLK_OF_DECLARE(divider_clk, "ti,divider-clock", of_ti_divider_clk_setup);
  550. static void __init of_ti_composite_divider_clk_setup(struct device_node *node)
  551. {
  552. struct clk_omap_divider *div;
  553. u32 val;
  554. div = kzalloc(sizeof(*div), GFP_KERNEL);
  555. if (!div)
  556. return;
  557. if (ti_clk_divider_populate(node, &div->reg, &div->table, &val,
  558. &div->flags, &div->width, &div->shift,
  559. NULL) < 0)
  560. goto cleanup;
  561. if (!ti_clk_add_component(node, &div->hw, CLK_COMPONENT_TYPE_DIVIDER))
  562. return;
  563. cleanup:
  564. kfree(div->table);
  565. kfree(div);
  566. }
  567. CLK_OF_DECLARE(ti_composite_divider_clk, "ti,composite-divider-clock",
  568. of_ti_composite_divider_clk_setup);