clk-mod0.c 9.3 KB

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