clk-sun9i-core.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * Copyright 2014 Chen-Yu Tsai
  3. *
  4. * Chen-Yu Tsai <wens@csie.org>
  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.h>
  19. #include <linux/of_address.h>
  20. #include <linux/log2.h>
  21. #include "clk-factors.h"
  22. /**
  23. * sun9i_a80_get_pll4_factors() - calculates n, p, m factors for PLL4
  24. * PLL4 rate is calculated as follows
  25. * rate = (parent_rate * n >> p) / (m + 1);
  26. * parent_rate is always 24MHz
  27. *
  28. * p and m are named div1 and div2 in Allwinner's SDK
  29. */
  30. static void sun9i_a80_get_pll4_factors(u32 *freq, u32 parent_rate,
  31. u8 *n_ret, u8 *k, u8 *m_ret, u8 *p_ret)
  32. {
  33. int n;
  34. int m = 1;
  35. int p = 1;
  36. /* Normalize value to a 6 MHz multiple (24 MHz / 4) */
  37. n = DIV_ROUND_UP(*freq, 6000000);
  38. /* If n is too large switch to steps of 12 MHz */
  39. if (n > 255) {
  40. m = 0;
  41. n = (n + 1) / 2;
  42. }
  43. /* If n is still too large switch to steps of 24 MHz */
  44. if (n > 255) {
  45. p = 0;
  46. n = (n + 1) / 2;
  47. }
  48. /* n must be between 12 and 255 */
  49. if (n > 255)
  50. n = 255;
  51. else if (n < 12)
  52. n = 12;
  53. *freq = ((24000000 * n) >> p) / (m + 1);
  54. /* we were called to round the frequency, we can now return */
  55. if (n_ret == NULL)
  56. return;
  57. *n_ret = n;
  58. *m_ret = m;
  59. *p_ret = p;
  60. }
  61. static struct clk_factors_config sun9i_a80_pll4_config = {
  62. .mshift = 18,
  63. .mwidth = 1,
  64. .nshift = 8,
  65. .nwidth = 8,
  66. .pshift = 16,
  67. .pwidth = 1,
  68. };
  69. static const struct factors_data sun9i_a80_pll4_data __initconst = {
  70. .enable = 31,
  71. .table = &sun9i_a80_pll4_config,
  72. .getter = sun9i_a80_get_pll4_factors,
  73. };
  74. static DEFINE_SPINLOCK(sun9i_a80_pll4_lock);
  75. static void __init sun9i_a80_pll4_setup(struct device_node *node)
  76. {
  77. void __iomem *reg;
  78. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  79. if (IS_ERR(reg)) {
  80. pr_err("Could not get registers for a80-pll4-clk: %s\n",
  81. node->name);
  82. return;
  83. }
  84. sunxi_factors_register(node, &sun9i_a80_pll4_data,
  85. &sun9i_a80_pll4_lock, reg);
  86. }
  87. CLK_OF_DECLARE(sun9i_a80_pll4, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup);
  88. /**
  89. * sun9i_a80_get_gt_factors() - calculates m factor for GT
  90. * GT rate is calculated as follows
  91. * rate = parent_rate / (m + 1);
  92. */
  93. static void sun9i_a80_get_gt_factors(u32 *freq, u32 parent_rate,
  94. u8 *n, u8 *k, u8 *m, u8 *p)
  95. {
  96. u32 div;
  97. if (parent_rate < *freq)
  98. *freq = parent_rate;
  99. div = DIV_ROUND_UP(parent_rate, *freq);
  100. /* maximum divider is 4 */
  101. if (div > 4)
  102. div = 4;
  103. *freq = parent_rate / div;
  104. /* we were called to round the frequency, we can now return */
  105. if (!m)
  106. return;
  107. *m = div;
  108. }
  109. static struct clk_factors_config sun9i_a80_gt_config = {
  110. .mshift = 0,
  111. .mwidth = 2,
  112. };
  113. static const struct factors_data sun9i_a80_gt_data __initconst = {
  114. .mux = 24,
  115. .muxmask = BIT(1) | BIT(0),
  116. .table = &sun9i_a80_gt_config,
  117. .getter = sun9i_a80_get_gt_factors,
  118. };
  119. static DEFINE_SPINLOCK(sun9i_a80_gt_lock);
  120. static void __init sun9i_a80_gt_setup(struct device_node *node)
  121. {
  122. void __iomem *reg;
  123. struct clk *gt;
  124. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  125. if (IS_ERR(reg)) {
  126. pr_err("Could not get registers for a80-gt-clk: %s\n",
  127. node->name);
  128. return;
  129. }
  130. gt = sunxi_factors_register(node, &sun9i_a80_gt_data,
  131. &sun9i_a80_gt_lock, reg);
  132. /* The GT bus clock needs to be always enabled */
  133. __clk_get(gt);
  134. clk_prepare_enable(gt);
  135. }
  136. CLK_OF_DECLARE(sun9i_a80_gt, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup);
  137. /**
  138. * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
  139. * AHB rate is calculated as follows
  140. * rate = parent_rate >> p;
  141. */
  142. static void sun9i_a80_get_ahb_factors(u32 *freq, u32 parent_rate,
  143. u8 *n, u8 *k, u8 *m, u8 *p)
  144. {
  145. u32 _p;
  146. if (parent_rate < *freq)
  147. *freq = parent_rate;
  148. _p = order_base_2(DIV_ROUND_UP(parent_rate, *freq));
  149. /* maximum p is 3 */
  150. if (_p > 3)
  151. _p = 3;
  152. *freq = parent_rate >> _p;
  153. /* we were called to round the frequency, we can now return */
  154. if (!p)
  155. return;
  156. *p = _p;
  157. }
  158. static struct clk_factors_config sun9i_a80_ahb_config = {
  159. .pshift = 0,
  160. .pwidth = 2,
  161. };
  162. static const struct factors_data sun9i_a80_ahb_data __initconst = {
  163. .mux = 24,
  164. .muxmask = BIT(1) | BIT(0),
  165. .table = &sun9i_a80_ahb_config,
  166. .getter = sun9i_a80_get_ahb_factors,
  167. };
  168. static DEFINE_SPINLOCK(sun9i_a80_ahb_lock);
  169. static void __init sun9i_a80_ahb_setup(struct device_node *node)
  170. {
  171. void __iomem *reg;
  172. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  173. if (IS_ERR(reg)) {
  174. pr_err("Could not get registers for a80-ahb-clk: %s\n",
  175. node->name);
  176. return;
  177. }
  178. sunxi_factors_register(node, &sun9i_a80_ahb_data,
  179. &sun9i_a80_ahb_lock, reg);
  180. }
  181. CLK_OF_DECLARE(sun9i_a80_ahb, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup);
  182. static const struct factors_data sun9i_a80_apb0_data __initconst = {
  183. .mux = 24,
  184. .muxmask = BIT(0),
  185. .table = &sun9i_a80_ahb_config,
  186. .getter = sun9i_a80_get_ahb_factors,
  187. };
  188. static DEFINE_SPINLOCK(sun9i_a80_apb0_lock);
  189. static void __init sun9i_a80_apb0_setup(struct device_node *node)
  190. {
  191. void __iomem *reg;
  192. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  193. if (IS_ERR(reg)) {
  194. pr_err("Could not get registers for a80-apb0-clk: %s\n",
  195. node->name);
  196. return;
  197. }
  198. sunxi_factors_register(node, &sun9i_a80_apb0_data,
  199. &sun9i_a80_apb0_lock, reg);
  200. }
  201. CLK_OF_DECLARE(sun9i_a80_apb0, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup);
  202. /**
  203. * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
  204. * APB1 rate is calculated as follows
  205. * rate = (parent_rate >> p) / (m + 1);
  206. */
  207. static void sun9i_a80_get_apb1_factors(u32 *freq, u32 parent_rate,
  208. u8 *n, u8 *k, u8 *m, u8 *p)
  209. {
  210. u32 div;
  211. u8 calcm, calcp;
  212. if (parent_rate < *freq)
  213. *freq = parent_rate;
  214. div = DIV_ROUND_UP(parent_rate, *freq);
  215. /* Highest possible divider is 256 (p = 3, m = 31) */
  216. if (div > 256)
  217. div = 256;
  218. calcp = order_base_2(div);
  219. calcm = (parent_rate >> calcp) - 1;
  220. *freq = (parent_rate >> calcp) / (calcm + 1);
  221. /* we were called to round the frequency, we can now return */
  222. if (n == NULL)
  223. return;
  224. *m = calcm;
  225. *p = calcp;
  226. }
  227. static struct clk_factors_config sun9i_a80_apb1_config = {
  228. .mshift = 0,
  229. .mwidth = 5,
  230. .pshift = 16,
  231. .pwidth = 2,
  232. };
  233. static const struct factors_data sun9i_a80_apb1_data __initconst = {
  234. .mux = 24,
  235. .muxmask = BIT(0),
  236. .table = &sun9i_a80_apb1_config,
  237. .getter = sun9i_a80_get_apb1_factors,
  238. };
  239. static DEFINE_SPINLOCK(sun9i_a80_apb1_lock);
  240. static void __init sun9i_a80_apb1_setup(struct device_node *node)
  241. {
  242. void __iomem *reg;
  243. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  244. if (IS_ERR(reg)) {
  245. pr_err("Could not get registers for a80-apb1-clk: %s\n",
  246. node->name);
  247. return;
  248. }
  249. sunxi_factors_register(node, &sun9i_a80_apb1_data,
  250. &sun9i_a80_apb1_lock, reg);
  251. }
  252. CLK_OF_DECLARE(sun9i_a80_apb1, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup);