clk-sun9i-core.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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-provider.h>
  17. #include <linux/clkdev.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 PLL1
  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, u8 *k, u8 *m, u8 *p)
  32. {
  33. int div;
  34. /* Normalize value to a 6M multiple */
  35. div = DIV_ROUND_UP(*freq, 6000000);
  36. /* divs above 256 cannot be odd */
  37. if (div > 256)
  38. div = round_up(div, 2);
  39. /* divs above 512 must be a multiple of 4 */
  40. if (div > 512)
  41. div = round_up(div, 4);
  42. *freq = 6000000 * div;
  43. /* we were called to round the frequency, we can now return */
  44. if (n == NULL)
  45. return;
  46. /* p will be 1 for divs under 512 */
  47. if (div < 512)
  48. *p = 1;
  49. else
  50. *p = 0;
  51. /* m will be 1 if div is odd */
  52. if (div & 1)
  53. *m = 1;
  54. else
  55. *m = 0;
  56. /* calculate a suitable n based on m and p */
  57. *n = div / (*p + 1) / (*m + 1);
  58. }
  59. static struct clk_factors_config sun9i_a80_pll4_config = {
  60. .mshift = 18,
  61. .mwidth = 1,
  62. .nshift = 8,
  63. .nwidth = 8,
  64. .pshift = 16,
  65. .pwidth = 1,
  66. };
  67. static const struct factors_data sun9i_a80_pll4_data __initconst = {
  68. .enable = 31,
  69. .table = &sun9i_a80_pll4_config,
  70. .getter = sun9i_a80_get_pll4_factors,
  71. };
  72. static DEFINE_SPINLOCK(sun9i_a80_pll4_lock);
  73. static void __init sun9i_a80_pll4_setup(struct device_node *node)
  74. {
  75. sunxi_factors_register(node, &sun9i_a80_pll4_data, &sun9i_a80_pll4_lock);
  76. }
  77. CLK_OF_DECLARE(sun9i_a80_pll4, "allwinner,sun9i-a80-pll4-clk", sun9i_a80_pll4_setup);
  78. /**
  79. * sun9i_a80_get_gt_factors() - calculates m factor for GT
  80. * GT rate is calculated as follows
  81. * rate = parent_rate / (m + 1);
  82. */
  83. static void sun9i_a80_get_gt_factors(u32 *freq, u32 parent_rate,
  84. u8 *n, u8 *k, u8 *m, u8 *p)
  85. {
  86. u32 div;
  87. if (parent_rate < *freq)
  88. *freq = parent_rate;
  89. div = DIV_ROUND_UP(parent_rate, *freq);
  90. /* maximum divider is 4 */
  91. if (div > 4)
  92. div = 4;
  93. *freq = parent_rate / div;
  94. /* we were called to round the frequency, we can now return */
  95. if (!m)
  96. return;
  97. *m = div;
  98. }
  99. static struct clk_factors_config sun9i_a80_gt_config = {
  100. .mshift = 0,
  101. .mwidth = 2,
  102. };
  103. static const struct factors_data sun9i_a80_gt_data __initconst = {
  104. .mux = 24,
  105. .muxmask = BIT(1) | BIT(0),
  106. .table = &sun9i_a80_gt_config,
  107. .getter = sun9i_a80_get_gt_factors,
  108. };
  109. static DEFINE_SPINLOCK(sun9i_a80_gt_lock);
  110. static void __init sun9i_a80_gt_setup(struct device_node *node)
  111. {
  112. struct clk *gt = sunxi_factors_register(node, &sun9i_a80_gt_data,
  113. &sun9i_a80_gt_lock);
  114. /* The GT bus clock needs to be always enabled */
  115. __clk_get(gt);
  116. clk_prepare_enable(gt);
  117. }
  118. CLK_OF_DECLARE(sun9i_a80_gt, "allwinner,sun9i-a80-gt-clk", sun9i_a80_gt_setup);
  119. /**
  120. * sun9i_a80_get_ahb_factors() - calculates p factor for AHB0/1/2
  121. * AHB rate is calculated as follows
  122. * rate = parent_rate >> p;
  123. */
  124. static void sun9i_a80_get_ahb_factors(u32 *freq, u32 parent_rate,
  125. u8 *n, u8 *k, u8 *m, u8 *p)
  126. {
  127. u32 _p;
  128. if (parent_rate < *freq)
  129. *freq = parent_rate;
  130. _p = order_base_2(DIV_ROUND_UP(parent_rate, *freq));
  131. /* maximum p is 3 */
  132. if (_p > 3)
  133. _p = 3;
  134. *freq = parent_rate >> _p;
  135. /* we were called to round the frequency, we can now return */
  136. if (!p)
  137. return;
  138. *p = _p;
  139. }
  140. static struct clk_factors_config sun9i_a80_ahb_config = {
  141. .pshift = 0,
  142. .pwidth = 2,
  143. };
  144. static const struct factors_data sun9i_a80_ahb_data __initconst = {
  145. .mux = 24,
  146. .muxmask = BIT(1) | BIT(0),
  147. .table = &sun9i_a80_ahb_config,
  148. .getter = sun9i_a80_get_ahb_factors,
  149. };
  150. static DEFINE_SPINLOCK(sun9i_a80_ahb_lock);
  151. static void __init sun9i_a80_ahb_setup(struct device_node *node)
  152. {
  153. sunxi_factors_register(node, &sun9i_a80_ahb_data, &sun9i_a80_ahb_lock);
  154. }
  155. CLK_OF_DECLARE(sun9i_a80_ahb, "allwinner,sun9i-a80-ahb-clk", sun9i_a80_ahb_setup);
  156. static const struct factors_data sun9i_a80_apb0_data __initconst = {
  157. .mux = 24,
  158. .muxmask = BIT(0),
  159. .table = &sun9i_a80_ahb_config,
  160. .getter = sun9i_a80_get_ahb_factors,
  161. };
  162. static DEFINE_SPINLOCK(sun9i_a80_apb0_lock);
  163. static void __init sun9i_a80_apb0_setup(struct device_node *node)
  164. {
  165. sunxi_factors_register(node, &sun9i_a80_apb0_data, &sun9i_a80_apb0_lock);
  166. }
  167. CLK_OF_DECLARE(sun9i_a80_apb0, "allwinner,sun9i-a80-apb0-clk", sun9i_a80_apb0_setup);
  168. /**
  169. * sun9i_a80_get_apb1_factors() - calculates m, p factors for APB1
  170. * APB1 rate is calculated as follows
  171. * rate = (parent_rate >> p) / (m + 1);
  172. */
  173. static void sun9i_a80_get_apb1_factors(u32 *freq, u32 parent_rate,
  174. u8 *n, u8 *k, u8 *m, u8 *p)
  175. {
  176. u32 div;
  177. u8 calcm, calcp;
  178. if (parent_rate < *freq)
  179. *freq = parent_rate;
  180. div = DIV_ROUND_UP(parent_rate, *freq);
  181. /* Highest possible divider is 256 (p = 3, m = 31) */
  182. if (div > 256)
  183. div = 256;
  184. calcp = order_base_2(div);
  185. calcm = (parent_rate >> calcp) - 1;
  186. *freq = (parent_rate >> calcp) / (calcm + 1);
  187. /* we were called to round the frequency, we can now return */
  188. if (n == NULL)
  189. return;
  190. *m = calcm;
  191. *p = calcp;
  192. }
  193. static struct clk_factors_config sun9i_a80_apb1_config = {
  194. .mshift = 0,
  195. .mwidth = 5,
  196. .pshift = 16,
  197. .pwidth = 2,
  198. };
  199. static const struct factors_data sun9i_a80_apb1_data __initconst = {
  200. .mux = 24,
  201. .muxmask = BIT(0),
  202. .table = &sun9i_a80_apb1_config,
  203. .getter = sun9i_a80_get_apb1_factors,
  204. };
  205. static DEFINE_SPINLOCK(sun9i_a80_apb1_lock);
  206. static void __init sun9i_a80_apb1_setup(struct device_node *node)
  207. {
  208. sunxi_factors_register(node, &sun9i_a80_apb1_data, &sun9i_a80_apb1_lock);
  209. }
  210. CLK_OF_DECLARE(sun9i_a80_apb1, "allwinner,sun9i-a80-apb1-clk", sun9i_a80_apb1_setup);