clk-mod0.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2013 Emilio López
  4. *
  5. * Emilio López <emilio@elopez.com.ar>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/clk-provider.h>
  9. #include <linux/io.h>
  10. #include <linux/of_address.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include "clk-factors.h"
  14. /*
  15. * sun4i_a10_get_mod0_factors() - calculates m, n factors for MOD0-style clocks
  16. * MOD0 rate is calculated as follows
  17. * rate = (parent_rate >> p) / (m + 1);
  18. */
  19. static void sun4i_a10_get_mod0_factors(struct factors_request *req)
  20. {
  21. u8 div, calcm, calcp;
  22. /* These clocks can only divide, so we will never be able to achieve
  23. * frequencies higher than the parent frequency */
  24. if (req->rate > req->parent_rate)
  25. req->rate = req->parent_rate;
  26. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  27. if (div < 16)
  28. calcp = 0;
  29. else if (div / 2 < 16)
  30. calcp = 1;
  31. else if (div / 4 < 16)
  32. calcp = 2;
  33. else
  34. calcp = 3;
  35. calcm = DIV_ROUND_UP(div, 1 << calcp);
  36. req->rate = (req->parent_rate >> calcp) / calcm;
  37. req->m = calcm - 1;
  38. req->p = calcp;
  39. }
  40. /* user manual says "n" but it's really "p" */
  41. static const struct clk_factors_config sun4i_a10_mod0_config = {
  42. .mshift = 0,
  43. .mwidth = 4,
  44. .pshift = 16,
  45. .pwidth = 2,
  46. };
  47. static const struct factors_data sun4i_a10_mod0_data = {
  48. .enable = 31,
  49. .mux = 24,
  50. .muxmask = BIT(1) | BIT(0),
  51. .table = &sun4i_a10_mod0_config,
  52. .getter = sun4i_a10_get_mod0_factors,
  53. };
  54. static DEFINE_SPINLOCK(sun4i_a10_mod0_lock);
  55. static void __init sun4i_a10_mod0_setup(struct device_node *node)
  56. {
  57. void __iomem *reg;
  58. reg = of_iomap(node, 0);
  59. if (!reg) {
  60. /*
  61. * This happens with mod0 clk nodes instantiated through
  62. * mfd, as those do not have their resources assigned at
  63. * CLK_OF_DECLARE time yet, so do not print an error.
  64. */
  65. return;
  66. }
  67. sunxi_factors_register(node, &sun4i_a10_mod0_data,
  68. &sun4i_a10_mod0_lock, reg);
  69. }
  70. CLK_OF_DECLARE_DRIVER(sun4i_a10_mod0, "allwinner,sun4i-a10-mod0-clk",
  71. sun4i_a10_mod0_setup);
  72. static int sun4i_a10_mod0_clk_probe(struct platform_device *pdev)
  73. {
  74. struct device_node *np = pdev->dev.of_node;
  75. void __iomem *reg;
  76. if (!np)
  77. return -ENODEV;
  78. reg = devm_platform_ioremap_resource(pdev, 0);
  79. if (IS_ERR(reg))
  80. return PTR_ERR(reg);
  81. sunxi_factors_register(np, &sun4i_a10_mod0_data,
  82. &sun4i_a10_mod0_lock, reg);
  83. return 0;
  84. }
  85. static const struct of_device_id sun4i_a10_mod0_clk_dt_ids[] = {
  86. { .compatible = "allwinner,sun4i-a10-mod0-clk" },
  87. { /* sentinel */ }
  88. };
  89. static struct platform_driver sun4i_a10_mod0_clk_driver = {
  90. .driver = {
  91. .name = "sun4i-a10-mod0-clk",
  92. .of_match_table = sun4i_a10_mod0_clk_dt_ids,
  93. },
  94. .probe = sun4i_a10_mod0_clk_probe,
  95. };
  96. builtin_platform_driver(sun4i_a10_mod0_clk_driver);
  97. static const struct factors_data sun9i_a80_mod0_data __initconst = {
  98. .enable = 31,
  99. .mux = 24,
  100. .muxmask = BIT(3) | BIT(2) | BIT(1) | BIT(0),
  101. .table = &sun4i_a10_mod0_config,
  102. .getter = sun4i_a10_get_mod0_factors,
  103. };
  104. static void __init sun9i_a80_mod0_setup(struct device_node *node)
  105. {
  106. void __iomem *reg;
  107. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  108. if (IS_ERR(reg)) {
  109. pr_err("Could not get registers for mod0-clk: %pOFn\n",
  110. node);
  111. return;
  112. }
  113. sunxi_factors_register(node, &sun9i_a80_mod0_data,
  114. &sun4i_a10_mod0_lock, reg);
  115. }
  116. CLK_OF_DECLARE(sun9i_a80_mod0, "allwinner,sun9i-a80-mod0-clk", sun9i_a80_mod0_setup);
  117. static DEFINE_SPINLOCK(sun5i_a13_mbus_lock);
  118. static void __init sun5i_a13_mbus_setup(struct device_node *node)
  119. {
  120. void __iomem *reg;
  121. reg = of_iomap(node, 0);
  122. if (!reg) {
  123. pr_err("Could not get registers for a13-mbus-clk\n");
  124. return;
  125. }
  126. /* The MBUS clocks needs to be always enabled */
  127. sunxi_factors_register_critical(node, &sun4i_a10_mod0_data,
  128. &sun5i_a13_mbus_lock, reg);
  129. }
  130. CLK_OF_DECLARE(sun5i_a13_mbus, "allwinner,sun5i-a13-mbus-clk", sun5i_a13_mbus_setup);
  131. struct mmc_phase {
  132. struct clk_hw hw;
  133. u8 offset;
  134. void __iomem *reg;
  135. spinlock_t *lock;
  136. };
  137. #define to_mmc_phase(_hw) container_of(_hw, struct mmc_phase, hw)
  138. static int mmc_get_phase(struct clk_hw *hw)
  139. {
  140. struct clk *mmc, *mmc_parent, *clk = hw->clk;
  141. struct mmc_phase *phase = to_mmc_phase(hw);
  142. unsigned int mmc_rate, mmc_parent_rate;
  143. u16 step, mmc_div;
  144. u32 value;
  145. u8 delay;
  146. value = readl(phase->reg);
  147. delay = (value >> phase->offset) & 0x3;
  148. if (!delay)
  149. return 180;
  150. /* Get the main MMC clock */
  151. mmc = clk_get_parent(clk);
  152. if (!mmc)
  153. return -EINVAL;
  154. /* And its rate */
  155. mmc_rate = clk_get_rate(mmc);
  156. if (!mmc_rate)
  157. return -EINVAL;
  158. /* Now, get the MMC parent (most likely some PLL) */
  159. mmc_parent = clk_get_parent(mmc);
  160. if (!mmc_parent)
  161. return -EINVAL;
  162. /* And its rate */
  163. mmc_parent_rate = clk_get_rate(mmc_parent);
  164. if (!mmc_parent_rate)
  165. return -EINVAL;
  166. /* Get MMC clock divider */
  167. mmc_div = mmc_parent_rate / mmc_rate;
  168. step = DIV_ROUND_CLOSEST(360, mmc_div);
  169. return delay * step;
  170. }
  171. static int mmc_set_phase(struct clk_hw *hw, int degrees)
  172. {
  173. struct clk *mmc, *mmc_parent, *clk = hw->clk;
  174. struct mmc_phase *phase = to_mmc_phase(hw);
  175. unsigned int mmc_rate, mmc_parent_rate;
  176. unsigned long flags;
  177. u32 value;
  178. u8 delay;
  179. /* Get the main MMC clock */
  180. mmc = clk_get_parent(clk);
  181. if (!mmc)
  182. return -EINVAL;
  183. /* And its rate */
  184. mmc_rate = clk_get_rate(mmc);
  185. if (!mmc_rate)
  186. return -EINVAL;
  187. /* Now, get the MMC parent (most likely some PLL) */
  188. mmc_parent = clk_get_parent(mmc);
  189. if (!mmc_parent)
  190. return -EINVAL;
  191. /* And its rate */
  192. mmc_parent_rate = clk_get_rate(mmc_parent);
  193. if (!mmc_parent_rate)
  194. return -EINVAL;
  195. if (degrees != 180) {
  196. u16 step, mmc_div;
  197. /* Get MMC clock divider */
  198. mmc_div = mmc_parent_rate / mmc_rate;
  199. /*
  200. * We can only outphase the clocks by multiple of the
  201. * PLL's period.
  202. *
  203. * Since the MMC clock in only a divider, and the
  204. * formula to get the outphasing in degrees is deg =
  205. * 360 * delta / period
  206. *
  207. * If we simplify this formula, we can see that the
  208. * only thing that we're concerned about is the number
  209. * of period we want to outphase our clock from, and
  210. * the divider set by the MMC clock.
  211. */
  212. step = DIV_ROUND_CLOSEST(360, mmc_div);
  213. delay = DIV_ROUND_CLOSEST(degrees, step);
  214. } else {
  215. delay = 0;
  216. }
  217. spin_lock_irqsave(phase->lock, flags);
  218. value = readl(phase->reg);
  219. value &= ~GENMASK(phase->offset + 3, phase->offset);
  220. value |= delay << phase->offset;
  221. writel(value, phase->reg);
  222. spin_unlock_irqrestore(phase->lock, flags);
  223. return 0;
  224. }
  225. static const struct clk_ops mmc_clk_ops = {
  226. .get_phase = mmc_get_phase,
  227. .set_phase = mmc_set_phase,
  228. };
  229. /*
  230. * sunxi_mmc_setup - Common setup function for mmc module clocks
  231. *
  232. * The only difference between module clocks on different platforms is the
  233. * width of the mux register bits and the valid values, which are passed in
  234. * through struct factors_data. The phase clocks parts are identical.
  235. */
  236. static void __init sunxi_mmc_setup(struct device_node *node,
  237. const struct factors_data *data,
  238. spinlock_t *lock)
  239. {
  240. struct clk_onecell_data *clk_data;
  241. const char *parent;
  242. void __iomem *reg;
  243. int i;
  244. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  245. if (IS_ERR(reg)) {
  246. pr_err("Couldn't map the %pOFn clock registers\n", node);
  247. return;
  248. }
  249. clk_data = kmalloc_obj(*clk_data);
  250. if (!clk_data)
  251. return;
  252. clk_data->clks = kzalloc_objs(*clk_data->clks, 3);
  253. if (!clk_data->clks)
  254. goto err_free_data;
  255. clk_data->clk_num = 3;
  256. clk_data->clks[0] = sunxi_factors_register(node, data, lock, reg);
  257. if (!clk_data->clks[0])
  258. goto err_free_clks;
  259. parent = __clk_get_name(clk_data->clks[0]);
  260. for (i = 1; i < 3; i++) {
  261. struct clk_init_data init = {
  262. .num_parents = 1,
  263. .parent_names = &parent,
  264. .ops = &mmc_clk_ops,
  265. };
  266. struct mmc_phase *phase;
  267. phase = kmalloc_obj(*phase);
  268. if (!phase)
  269. continue;
  270. phase->hw.init = &init;
  271. phase->reg = reg;
  272. phase->lock = lock;
  273. if (i == 1)
  274. phase->offset = 8;
  275. else
  276. phase->offset = 20;
  277. if (of_property_read_string_index(node, "clock-output-names",
  278. i, &init.name))
  279. init.name = node->name;
  280. clk_data->clks[i] = clk_register(NULL, &phase->hw);
  281. if (IS_ERR(clk_data->clks[i])) {
  282. kfree(phase);
  283. continue;
  284. }
  285. }
  286. of_clk_add_provider(node, of_clk_src_onecell_get, clk_data);
  287. return;
  288. err_free_clks:
  289. kfree(clk_data->clks);
  290. err_free_data:
  291. kfree(clk_data);
  292. }
  293. static DEFINE_SPINLOCK(sun4i_a10_mmc_lock);
  294. static void __init sun4i_a10_mmc_setup(struct device_node *node)
  295. {
  296. sunxi_mmc_setup(node, &sun4i_a10_mod0_data, &sun4i_a10_mmc_lock);
  297. }
  298. CLK_OF_DECLARE(sun4i_a10_mmc, "allwinner,sun4i-a10-mmc-clk", sun4i_a10_mmc_setup);
  299. static DEFINE_SPINLOCK(sun9i_a80_mmc_lock);
  300. static void __init sun9i_a80_mmc_setup(struct device_node *node)
  301. {
  302. sunxi_mmc_setup(node, &sun9i_a80_mod0_data, &sun9i_a80_mmc_lock);
  303. }
  304. CLK_OF_DECLARE(sun9i_a80_mmc, "allwinner,sun9i-a80-mmc-clk", sun9i_a80_mmc_setup);