clk-mod0.c 9.2 KB

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