clk-sun9i-core.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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/clkdev.h>
  18. #include <linux/clk-provider.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/log2.h>
  22. #include "clk-factors.h"
  23. /**
  24. * sun9i_a80_get_pll4_factors() - calculates n, p, m factors for PLL4
  25. * PLL4 rate is calculated as follows
  26. * rate = (parent_rate * n >> p) / (m + 1);
  27. * parent_rate is always 24MHz
  28. *
  29. * p and m are named div1 and div2 in Allwinner's SDK
  30. */
  31. static void sun9i_a80_get_pll4_factors(struct factors_request *req)
  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(req->rate, 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. req->rate = ((24000000 * n) >> p) / (m + 1);
  54. req->n = n;
  55. req->m = m;
  56. req->p = p;
  57. }
  58. static const struct clk_factors_config sun9i_a80_pll4_config = {
  59. .mshift = 18,
  60. .mwidth = 1,
  61. .nshift = 8,
  62. .nwidth = 8,
  63. .pshift = 16,
  64. .pwidth = 1,
  65. };
  66. static const struct factors_data sun9i_a80_pll4_data __initconst = {
  67. .enable = 31,
  68. .table = &sun9i_a80_pll4_config,
  69. .getter = sun9i_a80_get_pll4_factors,
  70. };
  71. static DEFINE_SPINLOCK(sun9i_a80_pll4_lock);
  72. static void __init sun9i_a80_pll4_setup(struct device_node *node)
  73. {
  74. void __iomem *reg;
  75. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  76. if (IS_ERR(reg)) {
  77. pr_err("Could not get registers for a80-pll4-clk: %s\n",
  78. node->name);
  79. return;
  80. }
  81. sunxi_factors_register(node, &sun9i_a80_pll4_data,
  82. &sun9i_a80_pll4_lock, reg);
  83. }
  84. CLK_OF_DECLARE(sun9i_a80_pll4, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup);
  85. /**
  86. * sun9i_a80_get_gt_factors() - calculates m factor for GT
  87. * GT rate is calculated as follows
  88. * rate = parent_rate / (m + 1);
  89. */
  90. static void sun9i_a80_get_gt_factors(struct factors_request *req)
  91. {
  92. u32 div;
  93. if (req->parent_rate < req->rate)
  94. req->rate = req->parent_rate;
  95. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  96. /* maximum divider is 4 */
  97. if (div > 4)
  98. div = 4;
  99. req->rate = req->parent_rate / div;
  100. req->m = div;
  101. }
  102. static const struct clk_factors_config sun9i_a80_gt_config = {
  103. .mshift = 0,
  104. .mwidth = 2,
  105. };
  106. static const struct factors_data sun9i_a80_gt_data __initconst = {
  107. .mux = 24,
  108. .muxmask = BIT(1) | BIT(0),
  109. .table = &sun9i_a80_gt_config,
  110. .getter = sun9i_a80_get_gt_factors,
  111. };
  112. static DEFINE_SPINLOCK(sun9i_a80_gt_lock);
  113. static void __init sun9i_a80_gt_setup(struct device_node *node)
  114. {
  115. void __iomem *reg;
  116. struct clk *gt;
  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 a80-gt-clk: %s\n",
  120. node->name);
  121. return;
  122. }
  123. gt = sunxi_factors_register(node, &sun9i_a80_gt_data,
  124. &sun9i_a80_gt_lock, reg);
  125. /* The GT bus clock needs to be always enabled */
  126. __clk_get(gt);
  127. clk_prepare_enable(gt);
  128. }
  129. CLK_OF_DECLARE(sun9i_a80_gt, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup);
  130. /**
  131. * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
  132. * AHB rate is calculated as follows
  133. * rate = parent_rate >> p;
  134. */
  135. static void sun9i_a80_get_ahb_factors(struct factors_request *req)
  136. {
  137. u32 _p;
  138. if (req->parent_rate < req->rate)
  139. req->rate = req->parent_rate;
  140. _p = order_base_2(DIV_ROUND_UP(req->parent_rate, req->rate));
  141. /* maximum p is 3 */
  142. if (_p > 3)
  143. _p = 3;
  144. req->rate = req->parent_rate >> _p;
  145. req->p = _p;
  146. }
  147. static const struct clk_factors_config sun9i_a80_ahb_config = {
  148. .pshift = 0,
  149. .pwidth = 2,
  150. };
  151. static const struct factors_data sun9i_a80_ahb_data __initconst = {
  152. .mux = 24,
  153. .muxmask = BIT(1) | BIT(0),
  154. .table = &sun9i_a80_ahb_config,
  155. .getter = sun9i_a80_get_ahb_factors,
  156. };
  157. static DEFINE_SPINLOCK(sun9i_a80_ahb_lock);
  158. static void __init sun9i_a80_ahb_setup(struct device_node *node)
  159. {
  160. void __iomem *reg;
  161. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  162. if (IS_ERR(reg)) {
  163. pr_err("Could not get registers for a80-ahb-clk: %s\n",
  164. node->name);
  165. return;
  166. }
  167. sunxi_factors_register(node, &sun9i_a80_ahb_data,
  168. &sun9i_a80_ahb_lock, reg);
  169. }
  170. CLK_OF_DECLARE(sun9i_a80_ahb, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup);
  171. static const struct factors_data sun9i_a80_apb0_data __initconst = {
  172. .mux = 24,
  173. .muxmask = BIT(0),
  174. .table = &sun9i_a80_ahb_config,
  175. .getter = sun9i_a80_get_ahb_factors,
  176. };
  177. static DEFINE_SPINLOCK(sun9i_a80_apb0_lock);
  178. static void __init sun9i_a80_apb0_setup(struct device_node *node)
  179. {
  180. void __iomem *reg;
  181. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  182. if (IS_ERR(reg)) {
  183. pr_err("Could not get registers for a80-apb0-clk: %s\n",
  184. node->name);
  185. return;
  186. }
  187. sunxi_factors_register(node, &sun9i_a80_apb0_data,
  188. &sun9i_a80_apb0_lock, reg);
  189. }
  190. CLK_OF_DECLARE(sun9i_a80_apb0, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup);
  191. /**
  192. * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
  193. * APB1 rate is calculated as follows
  194. * rate = (parent_rate >> p) / (m + 1);
  195. */
  196. static void sun9i_a80_get_apb1_factors(struct factors_request *req)
  197. {
  198. u32 div;
  199. if (req->parent_rate < req->rate)
  200. req->rate = req->parent_rate;
  201. div = DIV_ROUND_UP(req->parent_rate, req->rate);
  202. /* Highest possible divider is 256 (p = 3, m = 31) */
  203. if (div > 256)
  204. div = 256;
  205. req->p = order_base_2(div);
  206. req->m = (req->parent_rate >> req->p) - 1;
  207. req->rate = (req->parent_rate >> req->p) / (req->m + 1);
  208. }
  209. static const struct clk_factors_config sun9i_a80_apb1_config = {
  210. .mshift = 0,
  211. .mwidth = 5,
  212. .pshift = 16,
  213. .pwidth = 2,
  214. };
  215. static const struct factors_data sun9i_a80_apb1_data __initconst = {
  216. .mux = 24,
  217. .muxmask = BIT(0),
  218. .table = &sun9i_a80_apb1_config,
  219. .getter = sun9i_a80_get_apb1_factors,
  220. };
  221. static DEFINE_SPINLOCK(sun9i_a80_apb1_lock);
  222. static void __init sun9i_a80_apb1_setup(struct device_node *node)
  223. {
  224. void __iomem *reg;
  225. reg = of_io_request_and_map(node, 0, of_node_full_name(node));
  226. if (IS_ERR(reg)) {
  227. pr_err("Could not get registers for a80-apb1-clk: %s\n",
  228. node->name);
  229. return;
  230. }
  231. sunxi_factors_register(node, &sun9i_a80_apb1_data,
  232. &sun9i_a80_apb1_lock, reg);
  233. }
  234. CLK_OF_DECLARE(sun9i_a80_apb1, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup);