clk-mod0.c 9.3 KB

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