clk-corediv.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. static const struct clk_corediv_desc mv98dx3236_corediv_desc[] = {
  67. { .mask = 0x0f, .offset = 6, .fieldbit = 27 }, /* NAND clock */
  68. };
  69. #define to_corediv_clk(p) container_of(p, struct clk_corediv, hw)
  70. static int clk_corediv_is_enabled(struct clk_hw *hwclk)
  71. {
  72. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  73. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  74. const struct clk_corediv_desc *desc = corediv->desc;
  75. u32 enable_mask = BIT(desc->fieldbit) << soc_desc->enable_bit_offset;
  76. return !!(readl(corediv->reg) & enable_mask);
  77. }
  78. static int clk_corediv_enable(struct clk_hw *hwclk)
  79. {
  80. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  81. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  82. const struct clk_corediv_desc *desc = corediv->desc;
  83. unsigned long flags = 0;
  84. u32 reg;
  85. spin_lock_irqsave(&corediv->lock, flags);
  86. reg = readl(corediv->reg);
  87. reg |= (BIT(desc->fieldbit) << soc_desc->enable_bit_offset);
  88. writel(reg, corediv->reg);
  89. spin_unlock_irqrestore(&corediv->lock, flags);
  90. return 0;
  91. }
  92. static void clk_corediv_disable(struct clk_hw *hwclk)
  93. {
  94. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  95. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  96. const struct clk_corediv_desc *desc = corediv->desc;
  97. unsigned long flags = 0;
  98. u32 reg;
  99. spin_lock_irqsave(&corediv->lock, flags);
  100. reg = readl(corediv->reg);
  101. reg &= ~(BIT(desc->fieldbit) << soc_desc->enable_bit_offset);
  102. writel(reg, corediv->reg);
  103. spin_unlock_irqrestore(&corediv->lock, flags);
  104. }
  105. static unsigned long clk_corediv_recalc_rate(struct clk_hw *hwclk,
  106. unsigned long parent_rate)
  107. {
  108. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  109. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  110. const struct clk_corediv_desc *desc = corediv->desc;
  111. u32 reg, div;
  112. reg = readl(corediv->reg + soc_desc->ratio_offset);
  113. div = (reg >> desc->offset) & desc->mask;
  114. return parent_rate / div;
  115. }
  116. static long clk_corediv_round_rate(struct clk_hw *hwclk, unsigned long rate,
  117. unsigned long *parent_rate)
  118. {
  119. /* Valid ratio are 1:4, 1:5, 1:6 and 1:8 */
  120. u32 div;
  121. div = *parent_rate / rate;
  122. if (div < 4)
  123. div = 4;
  124. else if (div > 6)
  125. div = 8;
  126. return *parent_rate / div;
  127. }
  128. static int clk_corediv_set_rate(struct clk_hw *hwclk, unsigned long rate,
  129. unsigned long parent_rate)
  130. {
  131. struct clk_corediv *corediv = to_corediv_clk(hwclk);
  132. const struct clk_corediv_soc_desc *soc_desc = corediv->soc_desc;
  133. const struct clk_corediv_desc *desc = corediv->desc;
  134. unsigned long flags = 0;
  135. u32 reg, div;
  136. div = parent_rate / rate;
  137. spin_lock_irqsave(&corediv->lock, flags);
  138. /* Write new divider to the divider ratio register */
  139. reg = readl(corediv->reg + soc_desc->ratio_offset);
  140. reg &= ~(desc->mask << desc->offset);
  141. reg |= (div & desc->mask) << desc->offset;
  142. writel(reg, corediv->reg + soc_desc->ratio_offset);
  143. /* Set reload-force for this clock */
  144. reg = readl(corediv->reg) | BIT(desc->fieldbit);
  145. writel(reg, corediv->reg);
  146. /* Now trigger the clock update */
  147. reg = readl(corediv->reg) | soc_desc->ratio_reload;
  148. writel(reg, corediv->reg);
  149. /*
  150. * Wait for clocks to settle down, and then clear all the
  151. * ratios request and the reload request.
  152. */
  153. udelay(1000);
  154. reg &= ~(CORE_CLK_DIV_RATIO_MASK | soc_desc->ratio_reload);
  155. writel(reg, corediv->reg);
  156. udelay(1000);
  157. spin_unlock_irqrestore(&corediv->lock, flags);
  158. return 0;
  159. }
  160. static const struct clk_corediv_soc_desc armada370_corediv_soc = {
  161. .descs = mvebu_corediv_desc,
  162. .ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  163. .ops = {
  164. .enable = clk_corediv_enable,
  165. .disable = clk_corediv_disable,
  166. .is_enabled = clk_corediv_is_enabled,
  167. .recalc_rate = clk_corediv_recalc_rate,
  168. .round_rate = clk_corediv_round_rate,
  169. .set_rate = clk_corediv_set_rate,
  170. },
  171. .ratio_reload = BIT(8),
  172. .enable_bit_offset = 24,
  173. .ratio_offset = 0x8,
  174. };
  175. static const struct clk_corediv_soc_desc armada380_corediv_soc = {
  176. .descs = mvebu_corediv_desc,
  177. .ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  178. .ops = {
  179. .enable = clk_corediv_enable,
  180. .disable = clk_corediv_disable,
  181. .is_enabled = clk_corediv_is_enabled,
  182. .recalc_rate = clk_corediv_recalc_rate,
  183. .round_rate = clk_corediv_round_rate,
  184. .set_rate = clk_corediv_set_rate,
  185. },
  186. .ratio_reload = BIT(8),
  187. .enable_bit_offset = 16,
  188. .ratio_offset = 0x4,
  189. };
  190. static const struct clk_corediv_soc_desc armada375_corediv_soc = {
  191. .descs = mvebu_corediv_desc,
  192. .ndescs = ARRAY_SIZE(mvebu_corediv_desc),
  193. .ops = {
  194. .recalc_rate = clk_corediv_recalc_rate,
  195. .round_rate = clk_corediv_round_rate,
  196. .set_rate = clk_corediv_set_rate,
  197. },
  198. .ratio_reload = BIT(8),
  199. .ratio_offset = 0x4,
  200. };
  201. static const struct clk_corediv_soc_desc mv98dx3236_corediv_soc = {
  202. .descs = mv98dx3236_corediv_desc,
  203. .ndescs = ARRAY_SIZE(mv98dx3236_corediv_desc),
  204. .ops = {
  205. .recalc_rate = clk_corediv_recalc_rate,
  206. .round_rate = clk_corediv_round_rate,
  207. .set_rate = clk_corediv_set_rate,
  208. },
  209. .ratio_reload = BIT(10),
  210. .ratio_offset = 0x8,
  211. };
  212. static void __init
  213. mvebu_corediv_clk_init(struct device_node *node,
  214. const struct clk_corediv_soc_desc *soc_desc)
  215. {
  216. struct clk_init_data init;
  217. struct clk_corediv *corediv;
  218. struct clk **clks;
  219. void __iomem *base;
  220. const char *parent_name;
  221. const char *clk_name;
  222. int i;
  223. base = of_iomap(node, 0);
  224. if (WARN_ON(!base))
  225. return;
  226. parent_name = of_clk_get_parent_name(node, 0);
  227. clk_data.clk_num = soc_desc->ndescs;
  228. /* clks holds the clock array */
  229. clks = kcalloc(clk_data.clk_num, sizeof(struct clk *),
  230. GFP_KERNEL);
  231. if (WARN_ON(!clks))
  232. goto err_unmap;
  233. /* corediv holds the clock specific array */
  234. corediv = kcalloc(clk_data.clk_num, sizeof(struct clk_corediv),
  235. GFP_KERNEL);
  236. if (WARN_ON(!corediv))
  237. goto err_free_clks;
  238. spin_lock_init(&corediv->lock);
  239. for (i = 0; i < clk_data.clk_num; i++) {
  240. of_property_read_string_index(node, "clock-output-names",
  241. i, &clk_name);
  242. init.num_parents = 1;
  243. init.parent_names = &parent_name;
  244. init.name = clk_name;
  245. init.ops = &soc_desc->ops;
  246. init.flags = 0;
  247. corediv[i].soc_desc = soc_desc;
  248. corediv[i].desc = soc_desc->descs + i;
  249. corediv[i].reg = base;
  250. corediv[i].hw.init = &init;
  251. clks[i] = clk_register(NULL, &corediv[i].hw);
  252. WARN_ON(IS_ERR(clks[i]));
  253. }
  254. clk_data.clks = clks;
  255. of_clk_add_provider(node, of_clk_src_onecell_get, &clk_data);
  256. return;
  257. err_free_clks:
  258. kfree(clks);
  259. err_unmap:
  260. iounmap(base);
  261. }
  262. static void __init armada370_corediv_clk_init(struct device_node *node)
  263. {
  264. return mvebu_corediv_clk_init(node, &armada370_corediv_soc);
  265. }
  266. CLK_OF_DECLARE(armada370_corediv_clk, "marvell,armada-370-corediv-clock",
  267. armada370_corediv_clk_init);
  268. static void __init armada375_corediv_clk_init(struct device_node *node)
  269. {
  270. return mvebu_corediv_clk_init(node, &armada375_corediv_soc);
  271. }
  272. CLK_OF_DECLARE(armada375_corediv_clk, "marvell,armada-375-corediv-clock",
  273. armada375_corediv_clk_init);
  274. static void __init armada380_corediv_clk_init(struct device_node *node)
  275. {
  276. return mvebu_corediv_clk_init(node, &armada380_corediv_soc);
  277. }
  278. CLK_OF_DECLARE(armada380_corediv_clk, "marvell,armada-380-corediv-clock",
  279. armada380_corediv_clk_init);
  280. static void __init mv98dx3236_corediv_clk_init(struct device_node *node)
  281. {
  282. return mvebu_corediv_clk_init(node, &mv98dx3236_corediv_soc);
  283. }
  284. CLK_OF_DECLARE(mv98dx3236_corediv_clk, "marvell,mv98dx3236-corediv-clock",
  285. mv98dx3236_corediv_clk_init);