clk-mod0.c 9.3 KB

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