clk-mod0.c 9.3 KB

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