clk-corediv.c 9.1 KB

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