clk-corediv.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * MVEBU Core divider clock
  3. *
  4. * Copyright (C) 2013 Marvell
  5. *
  6. * Ezequiel Garcia <ezequiel.garcia@free-electrons.com>
  7. *
  8. * This file is licensed under the terms of the GNU General Public
  9. * License version 2. This program is licensed "as is" without any
  10. * warranty of any kind, whether express or implied.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/clk-provider.h>
  14. #include <linux/of_address.h>
  15. #include <linux/slab.h>
  16. #include <linux/delay.h>
  17. #include "common.h"
  18. #define CORE_CLK_DIV_RATIO_MASK 0xff
  19. /*
  20. * This structure describes the hardware details (bit offset and mask)
  21. * to configure one particular core divider clock. Those hardware
  22. * details may differ from one SoC to another. This structure is
  23. * therefore typically instantiated statically to describe the
  24. * hardware details.
  25. */
  26. struct clk_corediv_desc {
  27. unsigned int mask;
  28. unsigned int offset;
  29. unsigned int fieldbit;
  30. };
  31. /*
  32. * This structure describes the hardware details to configure the core
  33. * divider clocks on a given SoC. Amongst others, it points to the
  34. * array of core divider clock descriptors for this SoC, as well as
  35. * the corresponding operations to manipulate them.
  36. */
  37. struct clk_corediv_soc_desc {
  38. const struct clk_corediv_desc *descs;
  39. unsigned int ndescs;
  40. const struct clk_ops ops;
  41. u32 ratio_reload;
  42. u32 enable_bit_offset;
  43. u32 ratio_offset;
  44. };
  45. /*
  46. * This structure represents one core divider clock for the clock
  47. * framework, and is dynamically allocated for each core divider clock
  48. * existing in the current SoC.
  49. */
  50. struct clk_corediv {
  51. struct clk_hw hw;
  52. void __iomem *reg;
  53. const struct clk_corediv_desc *desc;
  54. const struct clk_corediv_soc_desc *soc_desc;
  55. spinlock_t lock;
  56. };
  57. static struct clk_onecell_data clk_data;
  58. /*
  59. * Description of the core divider clocks available. For now, we
  60. * support only NAND, and it is available at the same register
  61. * locations regardless of the SoC.
  62. */
  63. static const struct clk_corediv_desc mvebu_corediv_desc[] = {
  64. { .mask = 0x3f, .offset = 8, .fieldbit = 1 }, /* NAND clock */
  65. };
  66. #define to_corediv_clk(p) container_of(p, struct clk_corediv, hw)
  67. static int clk_corediv_is_enabled(struct clk_hw *hwclk)
  68. {
  69. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  70. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  71. const struct clk_corediv_desc *desc = corediv->desc;
  72. u32 enable_mask = BIT(desc->fieldbit) << soc_desc->enable_bit_offset;
  73. return !!(readl(corediv->reg) & enable_mask);
  74. }
  75. static int clk_corediv_enable(struct clk_hw *hwclk)
  76. {
  77. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  78. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  79. const struct clk_corediv_desc *desc = corediv->desc;
  80. unsigned long flags = 0;
  81. u32 reg;
  82. spin_lock_irqsave(&corediv->lock, flags);
  83. reg = readl(corediv->reg);
  84. reg |= (BIT(desc->fieldbit) << soc_desc->enable_bit_offset);
  85. writel(reg, corediv->reg);
  86. spin_unlock_irqrestore(&corediv->lock, flags);
  87. return 0;
  88. }
  89. static void clk_corediv_disable(struct clk_hw *hwclk)
  90. {
  91. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  92. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  93. const struct clk_corediv_desc *desc = corediv->desc;
  94. unsigned long flags = 0;
  95. u32 reg;
  96. spin_lock_irqsave(&corediv->lock, flags);
  97. reg = readl(corediv->reg);
  98. reg &= ~(BIT(desc->fieldbit) << soc_desc->enable_bit_offset);
  99. writel(reg, corediv->reg);
  100. spin_unlock_irqrestore(&corediv->lock, flags);
  101. }
  102. static unsigned long clk_corediv_recalc_rate(struct clk_hw *hwclk,
  103. unsigned long parent_rate)
  104. {
  105. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  106. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  107. const struct clk_corediv_desc *desc = corediv->desc;
  108. u32 reg, div;
  109. reg = readl(corediv->reg + soc_desc->ratio_offset);
  110. div = (reg >> desc->offset) & desc->mask;
  111. return parent_rate / div;
  112. }
  113. static long clk_corediv_round_rate(struct clk_hw *hwclk, unsigned long rate,
  114. unsigned long *parent_rate)
  115. {
  116. /* Valid ratio are 1:4, 1:5, 1:6 and 1:8 */
  117. u32 div;
  118. div = *parent_rate / rate;
  119. if (div < 4)
  120. div = 4;
  121. else if (div > 6)
  122. div = 8;
  123. return *parent_rate / div;
  124. }
  125. static int clk_corediv_set_rate(struct clk_hw *hwclk, unsigned long rate,
  126. unsigned long parent_rate)
  127. {
  128. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  129. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  130. const struct clk_corediv_desc *desc = corediv->desc;
  131. unsigned long flags = 0;
  132. u32 reg, div;
  133. div = parent_rate / rate;
  134. spin_lock_irqsave(&corediv->lock, flags);
  135. /* Write new divider to the divider ratio register */
  136. reg = readl(corediv->reg + soc_desc->ratio_offset);
  137. reg &= ~(desc->mask << desc->offset);
  138. reg |= (div & desc->mask) << desc->offset;
  139. writel(reg, corediv->reg + soc_desc->ratio_offset);
  140. /* Set reload-force for this clock */
  141. reg = readl(corediv->reg) | BIT(desc->fieldbit);
  142. writel(reg, corediv->reg);
  143. /* Now trigger the clock update */
  144. reg = readl(corediv->reg) | soc_desc->ratio_reload;
  145. writel(reg, corediv->reg);
  146. /*
  147. * Wait for clocks to settle down, and then clear all the
  148. * ratios request and the reload request.
  149. */
  150. udelay(1000);
  151. reg &= ~(CORE_CLK_DIV_RATIO_MASK | soc_desc->ratio_reload);
  152. writel(reg, corediv->reg);
  153. udelay(1000);
  154. spin_unlock_irqrestore(&corediv->lock, flags);
  155. return 0;
  156. }
  157. static const struct clk_corediv_soc_desc armada370_corediv_soc = {
  158. .descs = mvebu_corediv_desc,
  159. .ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  160. .ops = {
  161. .enable = clk_corediv_enable,
  162. .disable = clk_corediv_disable,
  163. .is_enabled = clk_corediv_is_enabled,
  164. .recalc_rate = clk_corediv_recalc_rate,
  165. .round_rate = clk_corediv_round_rate,
  166. .set_rate = clk_corediv_set_rate,
  167. },
  168. .ratio_reload = BIT(8),
  169. .enable_bit_offset = 24,
  170. .ratio_offset = 0x8,
  171. };
  172. static const struct clk_corediv_soc_desc armada380_corediv_soc = {
  173. .descs = mvebu_corediv_desc,
  174. .ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  175. .ops = {
  176. .enable = clk_corediv_enable,
  177. .disable = clk_corediv_disable,
  178. .is_enabled = clk_corediv_is_enabled,
  179. .recalc_rate = clk_corediv_recalc_rate,
  180. .round_rate = clk_corediv_round_rate,
  181. .set_rate = clk_corediv_set_rate,
  182. },
  183. .ratio_reload = BIT(8),
  184. .enable_bit_offset = 16,
  185. .ratio_offset = 0x4,
  186. };
  187. static const struct clk_corediv_soc_desc armada375_corediv_soc = {
  188. .descs = mvebu_corediv_desc,
  189. .ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  190. .ops = {
  191. .recalc_rate = clk_corediv_recalc_rate,
  192. .round_rate = clk_corediv_round_rate,
  193. .set_rate = clk_corediv_set_rate,
  194. },
  195. .ratio_reload = BIT(8),
  196. .ratio_offset = 0x4,
  197. };
  198. static void __init
  199. mvebu_corediv_clk_init(struct device_node *node,
  200. const struct clk_corediv_soc_desc *soc_desc)
  201. {
  202. struct clk_init_data init;
  203. struct clk_corediv *corediv;
  204. struct clk **clks;
  205. void __iomem *base;
  206. const char *parent_name;
  207. const char *clk_name;
  208. int i;
  209. base = of_iomap(node, 0);
  210. if (WARN_ON(!base))
  211. return;
  212. parent_name = of_clk_get_parent_name(node, 0);
  213. clk_data.clk_num = soc_desc->ndescs;
  214. /* clks holds the clock array */
  215. clks = kcalloc(clk_data.clk_num, sizeof(struct clk *),
  216. GFP_KERNEL);
  217. if (WARN_ON(!clks))
  218. goto err_unmap;
  219. /* corediv holds the clock specific array */
  220. corediv = kcalloc(clk_data.clk_num, sizeof(struct clk_corediv),
  221. GFP_KERNEL);
  222. if (WARN_ON(!corediv))
  223. goto err_free_clks;
  224. spin_lock_init(&corediv->lock);
  225. for (i = 0; i < clk_data.clk_num; i++) {
  226. of_property_read_string_index(node, "clock-output-names",
  227. i, &clk_name);
  228. init.num_parents = 1;
  229. init.parent_names = &parent_name;
  230. init.name = clk_name;
  231. init.ops = &soc_desc->ops;
  232. init.flags = 0;
  233. corediv[i].soc_desc = soc_desc;
  234. corediv[i].desc = soc_desc->descs + i;
  235. corediv[i].reg = base;
  236. corediv[i].hw.init = &init;
  237. clks[i] = clk_register(NULL, &corediv[i].hw);
  238. WARN_ON(IS_ERR(clks[i]));
  239. }
  240. clk_data.clks = clks;
  241. of_clk_add_provider(node, of_clk_src_onecell_get, &clk_data);
  242. return;
  243. err_free_clks:
  244. kfree(clks);
  245. err_unmap:
  246. iounmap(base);
  247. }
  248. static void __init armada370_corediv_clk_init(struct device_node *node)
  249. {
  250. return mvebu_corediv_clk_init(node, &armada370_corediv_soc);
  251. }
  252. CLK_OF_DECLARE(armada370_corediv_clk, "marvell,armada-370-corediv-clock",
  253. armada370_corediv_clk_init);
  254. static void __init armada375_corediv_clk_init(struct device_node *node)
  255. {
  256. return mvebu_corediv_clk_init(node, &armada375_corediv_soc);
  257. }
  258. CLK_OF_DECLARE(armada375_corediv_clk, "marvell,armada-375-corediv-clock",
  259. armada375_corediv_clk_init);
  260. static void __init armada380_corediv_clk_init(struct device_node *node)
  261. {
  262. return mvebu_corediv_clk_init(node, &armada380_corediv_soc);
  263. }
  264. CLK_OF_DECLARE(armada380_corediv_clk, "marvell,armada-380-corediv-clock",
  265. armada380_corediv_clk_init);