clk-axi-clkgen.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * AXI clkgen driver
  3. *
  4. * Copyright 2012-2013 Analog Devices Inc.
  5. * Author: Lars-Peter Clausen <lars@metafoo.de>
  6. *
  7. * Licensed under the GPL-2.
  8. *
  9. */
  10. #include <linux/platform_device.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/clk.h>
  13. #include <linux/slab.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/module.h>
  17. #include <linux/err.h>
  18. #define AXI_CLKGEN_REG_UPDATE_ENABLE 0x04
  19. #define AXI_CLKGEN_REG_CLK_OUT1 0x08
  20. #define AXI_CLKGEN_REG_CLK_OUT2 0x0c
  21. #define AXI_CLKGEN_REG_CLK_DIV 0x10
  22. #define AXI_CLKGEN_REG_CLK_FB1 0x14
  23. #define AXI_CLKGEN_REG_CLK_FB2 0x18
  24. #define AXI_CLKGEN_REG_LOCK1 0x1c
  25. #define AXI_CLKGEN_REG_LOCK2 0x20
  26. #define AXI_CLKGEN_REG_LOCK3 0x24
  27. #define AXI_CLKGEN_REG_FILTER1 0x28
  28. #define AXI_CLKGEN_REG_FILTER2 0x2c
  29. struct axi_clkgen {
  30. void __iomem *base;
  31. struct clk_hw clk_hw;
  32. };
  33. static uint32_t axi_clkgen_lookup_filter(unsigned int m)
  34. {
  35. switch (m) {
  36. case 0:
  37. return 0x01001990;
  38. case 1:
  39. return 0x01001190;
  40. case 2:
  41. return 0x01009890;
  42. case 3:
  43. return 0x01001890;
  44. case 4:
  45. return 0x01008890;
  46. case 5 ... 8:
  47. return 0x01009090;
  48. case 9 ... 11:
  49. return 0x01000890;
  50. case 12:
  51. return 0x08009090;
  52. case 13 ... 22:
  53. return 0x01001090;
  54. case 23 ... 36:
  55. return 0x01008090;
  56. case 37 ... 46:
  57. return 0x08001090;
  58. default:
  59. return 0x08008090;
  60. }
  61. }
  62. static const uint32_t axi_clkgen_lock_table[] = {
  63. 0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8,
  64. 0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8,
  65. 0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339,
  66. 0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271,
  67. 0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4,
  68. 0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190,
  69. 0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e,
  70. 0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c,
  71. 0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113,
  72. };
  73. static uint32_t axi_clkgen_lookup_lock(unsigned int m)
  74. {
  75. if (m < ARRAY_SIZE(axi_clkgen_lock_table))
  76. return axi_clkgen_lock_table[m];
  77. return 0x1f1f00fa;
  78. }
  79. static const unsigned int fpfd_min = 10000;
  80. static const unsigned int fpfd_max = 300000;
  81. static const unsigned int fvco_min = 600000;
  82. static const unsigned int fvco_max = 1200000;
  83. static void axi_clkgen_calc_params(unsigned long fin, unsigned long fout,
  84. unsigned int *best_d, unsigned int *best_m, unsigned int *best_dout)
  85. {
  86. unsigned long d, d_min, d_max, _d_min, _d_max;
  87. unsigned long m, m_min, m_max;
  88. unsigned long f, dout, best_f, fvco;
  89. fin /= 1000;
  90. fout /= 1000;
  91. best_f = ULONG_MAX;
  92. *best_d = 0;
  93. *best_m = 0;
  94. *best_dout = 0;
  95. d_min = max_t(unsigned long, DIV_ROUND_UP(fin, fpfd_max), 1);
  96. d_max = min_t(unsigned long, fin / fpfd_min, 80);
  97. m_min = max_t(unsigned long, DIV_ROUND_UP(fvco_min, fin) * d_min, 1);
  98. m_max = min_t(unsigned long, fvco_max * d_max / fin, 64);
  99. for (m = m_min; m <= m_max; m++) {
  100. _d_min = max(d_min, DIV_ROUND_UP(fin * m, fvco_max));
  101. _d_max = min(d_max, fin * m / fvco_min);
  102. for (d = _d_min; d <= _d_max; d++) {
  103. fvco = fin * m / d;
  104. dout = DIV_ROUND_CLOSEST(fvco, fout);
  105. dout = clamp_t(unsigned long, dout, 1, 128);
  106. f = fvco / dout;
  107. if (abs(f - fout) < abs(best_f - fout)) {
  108. best_f = f;
  109. *best_d = d;
  110. *best_m = m;
  111. *best_dout = dout;
  112. if (best_f == fout)
  113. return;
  114. }
  115. }
  116. }
  117. }
  118. static void axi_clkgen_calc_clk_params(unsigned int divider, unsigned int *low,
  119. unsigned int *high, unsigned int *edge, unsigned int *nocount)
  120. {
  121. if (divider == 1)
  122. *nocount = 1;
  123. else
  124. *nocount = 0;
  125. *high = divider / 2;
  126. *edge = divider % 2;
  127. *low = divider - *high;
  128. }
  129. static void axi_clkgen_write(struct axi_clkgen *axi_clkgen,
  130. unsigned int reg, unsigned int val)
  131. {
  132. writel(val, axi_clkgen->base + reg);
  133. }
  134. static void axi_clkgen_read(struct axi_clkgen *axi_clkgen,
  135. unsigned int reg, unsigned int *val)
  136. {
  137. *val = readl(axi_clkgen->base + reg);
  138. }
  139. static struct axi_clkgen *clk_hw_to_axi_clkgen(struct clk_hw *clk_hw)
  140. {
  141. return container_of(clk_hw, struct axi_clkgen, clk_hw);
  142. }
  143. static int axi_clkgen_set_rate(struct clk_hw *clk_hw,
  144. unsigned long rate, unsigned long parent_rate)
  145. {
  146. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  147. unsigned int d, m, dout;
  148. unsigned int nocount;
  149. unsigned int high;
  150. unsigned int edge;
  151. unsigned int low;
  152. uint32_t filter;
  153. uint32_t lock;
  154. if (parent_rate == 0 || rate == 0)
  155. return -EINVAL;
  156. axi_clkgen_calc_params(parent_rate, rate, &d, &m, &dout);
  157. if (d == 0 || dout == 0 || m == 0)
  158. return -EINVAL;
  159. filter = axi_clkgen_lookup_filter(m - 1);
  160. lock = axi_clkgen_lookup_lock(m - 1);
  161. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_UPDATE_ENABLE, 0);
  162. axi_clkgen_calc_clk_params(dout, &low, &high, &edge, &nocount);
  163. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_CLK_OUT1,
  164. (high << 6) | low);
  165. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_CLK_OUT2,
  166. (edge << 7) | (nocount << 6));
  167. axi_clkgen_calc_clk_params(d, &low, &high, &edge, &nocount);
  168. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_CLK_DIV,
  169. (edge << 13) | (nocount << 12) | (high << 6) | low);
  170. axi_clkgen_calc_clk_params(m, &low, &high, &edge, &nocount);
  171. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_CLK_FB1,
  172. (high << 6) | low);
  173. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_CLK_FB2,
  174. (edge << 7) | (nocount << 6));
  175. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_LOCK1, lock & 0x3ff);
  176. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_LOCK2,
  177. (((lock >> 16) & 0x1f) << 10) | 0x1);
  178. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_LOCK3,
  179. (((lock >> 24) & 0x1f) << 10) | 0x3e9);
  180. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_FILTER1, filter >> 16);
  181. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_FILTER2, filter);
  182. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_REG_UPDATE_ENABLE, 1);
  183. return 0;
  184. }
  185. static long axi_clkgen_round_rate(struct clk_hw *hw, unsigned long rate,
  186. unsigned long *parent_rate)
  187. {
  188. unsigned int d, m, dout;
  189. axi_clkgen_calc_params(*parent_rate, rate, &d, &m, &dout);
  190. if (d == 0 || dout == 0 || m == 0)
  191. return -EINVAL;
  192. return *parent_rate / d * m / dout;
  193. }
  194. static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,
  195. unsigned long parent_rate)
  196. {
  197. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  198. unsigned int d, m, dout;
  199. unsigned int reg;
  200. unsigned long long tmp;
  201. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_REG_CLK_OUT1, &reg);
  202. dout = (reg & 0x3f) + ((reg >> 6) & 0x3f);
  203. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_REG_CLK_DIV, &reg);
  204. d = (reg & 0x3f) + ((reg >> 6) & 0x3f);
  205. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_REG_CLK_FB1, &reg);
  206. m = (reg & 0x3f) + ((reg >> 6) & 0x3f);
  207. if (d == 0 || dout == 0)
  208. return 0;
  209. tmp = (unsigned long long)(parent_rate / d) * m;
  210. do_div(tmp, dout);
  211. if (tmp > ULONG_MAX)
  212. return ULONG_MAX;
  213. return tmp;
  214. }
  215. static const struct clk_ops axi_clkgen_ops = {
  216. .recalc_rate = axi_clkgen_recalc_rate,
  217. .round_rate = axi_clkgen_round_rate,
  218. .set_rate = axi_clkgen_set_rate,
  219. };
  220. static int axi_clkgen_probe(struct platform_device *pdev)
  221. {
  222. struct axi_clkgen *axi_clkgen;
  223. struct clk_init_data init;
  224. const char *parent_name;
  225. const char *clk_name;
  226. struct resource *mem;
  227. struct clk *clk;
  228. axi_clkgen = devm_kzalloc(&pdev->dev, sizeof(*axi_clkgen), GFP_KERNEL);
  229. if (!axi_clkgen)
  230. return -ENOMEM;
  231. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  232. axi_clkgen->base = devm_ioremap_resource(&pdev->dev, mem);
  233. if (IS_ERR(axi_clkgen->base))
  234. return PTR_ERR(axi_clkgen->base);
  235. parent_name = of_clk_get_parent_name(pdev->dev.of_node, 0);
  236. if (!parent_name)
  237. return -EINVAL;
  238. clk_name = pdev->dev.of_node->name;
  239. of_property_read_string(pdev->dev.of_node, "clock-output-names",
  240. &clk_name);
  241. init.name = clk_name;
  242. init.ops = &axi_clkgen_ops;
  243. init.flags = 0;
  244. init.parent_names = &parent_name;
  245. init.num_parents = 1;
  246. axi_clkgen->clk_hw.init = &init;
  247. clk = devm_clk_register(&pdev->dev, &axi_clkgen->clk_hw);
  248. if (IS_ERR(clk))
  249. return PTR_ERR(clk);
  250. return of_clk_add_provider(pdev->dev.of_node, of_clk_src_simple_get,
  251. clk);
  252. }
  253. static int axi_clkgen_remove(struct platform_device *pdev)
  254. {
  255. of_clk_del_provider(pdev->dev.of_node);
  256. return 0;
  257. }
  258. static const struct of_device_id axi_clkgen_ids[] = {
  259. { .compatible = "adi,axi-clkgen-1.00.a" },
  260. { },
  261. };
  262. MODULE_DEVICE_TABLE(of, axi_clkgen_ids);
  263. static struct platform_driver axi_clkgen_driver = {
  264. .driver = {
  265. .name = "adi-axi-clkgen",
  266. .owner = THIS_MODULE,
  267. .of_match_table = axi_clkgen_ids,
  268. },
  269. .probe = axi_clkgen_probe,
  270. .remove = axi_clkgen_remove,
  271. };
  272. module_platform_driver(axi_clkgen_driver);
  273. MODULE_LICENSE("GPL v2");
  274. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  275. MODULE_DESCRIPTION("Driver for the Analog Devices' AXI clkgen pcore clock generator");