clk-sun9i-core.c 6.7 KB

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