clk-axi-clkgen.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/slab.h>
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/module.h>
  16. #include <linux/err.h>
  17. #define AXI_CLKGEN_V2_REG_RESET 0x40
  18. #define AXI_CLKGEN_V2_REG_CLKSEL 0x44
  19. #define AXI_CLKGEN_V2_REG_DRP_CNTRL 0x70
  20. #define AXI_CLKGEN_V2_REG_DRP_STATUS 0x74
  21. #define AXI_CLKGEN_V2_RESET_MMCM_ENABLE BIT(1)
  22. #define AXI_CLKGEN_V2_RESET_ENABLE BIT(0)
  23. #define AXI_CLKGEN_V2_DRP_CNTRL_SEL BIT(29)
  24. #define AXI_CLKGEN_V2_DRP_CNTRL_READ BIT(28)
  25. #define AXI_CLKGEN_V2_DRP_STATUS_BUSY BIT(16)
  26. #define MMCM_REG_CLKOUT0_1 0x08
  27. #define MMCM_REG_CLKOUT0_2 0x09
  28. #define MMCM_REG_CLK_FB1 0x14
  29. #define MMCM_REG_CLK_FB2 0x15
  30. #define MMCM_REG_CLK_DIV 0x16
  31. #define MMCM_REG_LOCK1 0x18
  32. #define MMCM_REG_LOCK2 0x19
  33. #define MMCM_REG_LOCK3 0x1a
  34. #define MMCM_REG_FILTER1 0x4e
  35. #define MMCM_REG_FILTER2 0x4f
  36. struct axi_clkgen {
  37. void __iomem *base;
  38. struct clk_hw clk_hw;
  39. };
  40. static uint32_t axi_clkgen_lookup_filter(unsigned int m)
  41. {
  42. switch (m) {
  43. case 0:
  44. return 0x01001990;
  45. case 1:
  46. return 0x01001190;
  47. case 2:
  48. return 0x01009890;
  49. case 3:
  50. return 0x01001890;
  51. case 4:
  52. return 0x01008890;
  53. case 5 ... 8:
  54. return 0x01009090;
  55. case 9 ... 11:
  56. return 0x01000890;
  57. case 12:
  58. return 0x08009090;
  59. case 13 ... 22:
  60. return 0x01001090;
  61. case 23 ... 36:
  62. return 0x01008090;
  63. case 37 ... 46:
  64. return 0x08001090;
  65. default:
  66. return 0x08008090;
  67. }
  68. }
  69. static const uint32_t axi_clkgen_lock_table[] = {
  70. 0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8,
  71. 0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8,
  72. 0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339,
  73. 0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271,
  74. 0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4,
  75. 0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190,
  76. 0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e,
  77. 0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c,
  78. 0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113,
  79. };
  80. static uint32_t axi_clkgen_lookup_lock(unsigned int m)
  81. {
  82. if (m < ARRAY_SIZE(axi_clkgen_lock_table))
  83. return axi_clkgen_lock_table[m];
  84. return 0x1f1f00fa;
  85. }
  86. static const unsigned int fpfd_min = 10000;
  87. static const unsigned int fpfd_max = 300000;
  88. static const unsigned int fvco_min = 600000;
  89. static const unsigned int fvco_max = 1200000;
  90. static void axi_clkgen_calc_params(unsigned long fin, unsigned long fout,
  91. unsigned int *best_d, unsigned int *best_m, unsigned int *best_dout)
  92. {
  93. unsigned long d, d_min, d_max, _d_min, _d_max;
  94. unsigned long m, m_min, m_max;
  95. unsigned long f, dout, best_f, fvco;
  96. fin /= 1000;
  97. fout /= 1000;
  98. best_f = ULONG_MAX;
  99. *best_d = 0;
  100. *best_m = 0;
  101. *best_dout = 0;
  102. d_min = max_t(unsigned long, DIV_ROUND_UP(fin, fpfd_max), 1);
  103. d_max = min_t(unsigned long, fin / fpfd_min, 80);
  104. m_min = max_t(unsigned long, DIV_ROUND_UP(fvco_min, fin) * d_min, 1);
  105. m_max = min_t(unsigned long, fvco_max * d_max / fin, 64);
  106. for (m = m_min; m <= m_max; m++) {
  107. _d_min = max(d_min, DIV_ROUND_UP(fin * m, fvco_max));
  108. _d_max = min(d_max, fin * m / fvco_min);
  109. for (d = _d_min; d <= _d_max; d++) {
  110. fvco = fin * m / d;
  111. dout = DIV_ROUND_CLOSEST(fvco, fout);
  112. dout = clamp_t(unsigned long, dout, 1, 128);
  113. f = fvco / dout;
  114. if (abs(f - fout) < abs(best_f - fout)) {
  115. best_f = f;
  116. *best_d = d;
  117. *best_m = m;
  118. *best_dout = dout;
  119. if (best_f == fout)
  120. return;
  121. }
  122. }
  123. }
  124. }
  125. static void axi_clkgen_calc_clk_params(unsigned int divider, unsigned int *low,
  126. unsigned int *high, unsigned int *edge, unsigned int *nocount)
  127. {
  128. if (divider == 1)
  129. *nocount = 1;
  130. else
  131. *nocount = 0;
  132. *high = divider / 2;
  133. *edge = divider % 2;
  134. *low = divider - *high;
  135. }
  136. static void axi_clkgen_write(struct axi_clkgen *axi_clkgen,
  137. unsigned int reg, unsigned int val)
  138. {
  139. writel(val, axi_clkgen->base + reg);
  140. }
  141. static void axi_clkgen_read(struct axi_clkgen *axi_clkgen,
  142. unsigned int reg, unsigned int *val)
  143. {
  144. *val = readl(axi_clkgen->base + reg);
  145. }
  146. static int axi_clkgen_wait_non_busy(struct axi_clkgen *axi_clkgen)
  147. {
  148. unsigned int timeout = 10000;
  149. unsigned int val;
  150. do {
  151. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_STATUS, &val);
  152. } while ((val & AXI_CLKGEN_V2_DRP_STATUS_BUSY) && --timeout);
  153. if (val & AXI_CLKGEN_V2_DRP_STATUS_BUSY)
  154. return -EIO;
  155. return val & 0xffff;
  156. }
  157. static int axi_clkgen_mmcm_read(struct axi_clkgen *axi_clkgen,
  158. unsigned int reg, unsigned int *val)
  159. {
  160. unsigned int reg_val;
  161. int ret;
  162. ret = axi_clkgen_wait_non_busy(axi_clkgen);
  163. if (ret < 0)
  164. return ret;
  165. reg_val = AXI_CLKGEN_V2_DRP_CNTRL_SEL | AXI_CLKGEN_V2_DRP_CNTRL_READ;
  166. reg_val |= (reg << 16);
  167. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
  168. ret = axi_clkgen_wait_non_busy(axi_clkgen);
  169. if (ret < 0)
  170. return ret;
  171. *val = ret;
  172. return 0;
  173. }
  174. static int axi_clkgen_mmcm_write(struct axi_clkgen *axi_clkgen,
  175. unsigned int reg, unsigned int val, unsigned int mask)
  176. {
  177. unsigned int reg_val = 0;
  178. int ret;
  179. ret = axi_clkgen_wait_non_busy(axi_clkgen);
  180. if (ret < 0)
  181. return ret;
  182. if (mask != 0xffff) {
  183. axi_clkgen_mmcm_read(axi_clkgen, reg, &reg_val);
  184. reg_val &= ~mask;
  185. }
  186. reg_val |= AXI_CLKGEN_V2_DRP_CNTRL_SEL | (reg << 16) | (val & mask);
  187. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
  188. return 0;
  189. }
  190. static void axi_clkgen_mmcm_enable(struct axi_clkgen *axi_clkgen,
  191. bool enable)
  192. {
  193. unsigned int val = AXI_CLKGEN_V2_RESET_ENABLE;
  194. if (enable)
  195. val |= AXI_CLKGEN_V2_RESET_MMCM_ENABLE;
  196. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_RESET, val);
  197. }
  198. static struct axi_clkgen *clk_hw_to_axi_clkgen(struct clk_hw *clk_hw)
  199. {
  200. return container_of(clk_hw, struct axi_clkgen, clk_hw);
  201. }
  202. static int axi_clkgen_set_rate(struct clk_hw *clk_hw,
  203. unsigned long rate, unsigned long parent_rate)
  204. {
  205. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  206. unsigned int d, m, dout;
  207. unsigned int nocount;
  208. unsigned int high;
  209. unsigned int edge;
  210. unsigned int low;
  211. uint32_t filter;
  212. uint32_t lock;
  213. if (parent_rate == 0 || rate == 0)
  214. return -EINVAL;
  215. axi_clkgen_calc_params(parent_rate, rate, &d, &m, &dout);
  216. if (d == 0 || dout == 0 || m == 0)
  217. return -EINVAL;
  218. filter = axi_clkgen_lookup_filter(m - 1);
  219. lock = axi_clkgen_lookup_lock(m - 1);
  220. axi_clkgen_calc_clk_params(dout, &low, &high, &edge, &nocount);
  221. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLKOUT0_1,
  222. (high << 6) | low, 0xefff);
  223. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLKOUT0_2,
  224. (edge << 7) | (nocount << 6), 0x03ff);
  225. axi_clkgen_calc_clk_params(d, &low, &high, &edge, &nocount);
  226. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_DIV,
  227. (edge << 13) | (nocount << 12) | (high << 6) | low, 0x3fff);
  228. axi_clkgen_calc_clk_params(m, &low, &high, &edge, &nocount);
  229. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_FB1,
  230. (high << 6) | low, 0xefff);
  231. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_FB2,
  232. (edge << 7) | (nocount << 6), 0x03ff);
  233. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK1, lock & 0x3ff, 0x3ff);
  234. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK2,
  235. (((lock >> 16) & 0x1f) << 10) | 0x1, 0x7fff);
  236. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK3,
  237. (((lock >> 24) & 0x1f) << 10) | 0x3e9, 0x7fff);
  238. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER1, filter >> 16, 0x9900);
  239. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER2, filter, 0x9900);
  240. return 0;
  241. }
  242. static long axi_clkgen_round_rate(struct clk_hw *hw, unsigned long rate,
  243. unsigned long *parent_rate)
  244. {
  245. unsigned int d, m, dout;
  246. axi_clkgen_calc_params(*parent_rate, rate, &d, &m, &dout);
  247. if (d == 0 || dout == 0 || m == 0)
  248. return -EINVAL;
  249. return *parent_rate / d * m / dout;
  250. }
  251. static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,
  252. unsigned long parent_rate)
  253. {
  254. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  255. unsigned int d, m, dout;
  256. unsigned int reg;
  257. unsigned long long tmp;
  258. axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLKOUT0_1, &reg);
  259. dout = (reg & 0x3f) + ((reg >> 6) & 0x3f);
  260. axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLK_DIV, &reg);
  261. d = (reg & 0x3f) + ((reg >> 6) & 0x3f);
  262. axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLK_FB1, &reg);
  263. m = (reg & 0x3f) + ((reg >> 6) & 0x3f);
  264. if (d == 0 || dout == 0)
  265. return 0;
  266. tmp = (unsigned long long)(parent_rate / d) * m;
  267. do_div(tmp, dout);
  268. return min_t(unsigned long long, tmp, ULONG_MAX);
  269. }
  270. static int axi_clkgen_enable(struct clk_hw *clk_hw)
  271. {
  272. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  273. axi_clkgen_mmcm_enable(axi_clkgen, true);
  274. return 0;
  275. }
  276. static void axi_clkgen_disable(struct clk_hw *clk_hw)
  277. {
  278. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  279. axi_clkgen_mmcm_enable(axi_clkgen, false);
  280. }
  281. static int axi_clkgen_set_parent(struct clk_hw *clk_hw, u8 index)
  282. {
  283. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  284. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, index);
  285. return 0;
  286. }
  287. static u8 axi_clkgen_get_parent(struct clk_hw *clk_hw)
  288. {
  289. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  290. unsigned int parent;
  291. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, &parent);
  292. return parent;
  293. }
  294. static const struct clk_ops axi_clkgen_ops = {
  295. .recalc_rate = axi_clkgen_recalc_rate,
  296. .round_rate = axi_clkgen_round_rate,
  297. .set_rate = axi_clkgen_set_rate,
  298. .enable = axi_clkgen_enable,
  299. .disable = axi_clkgen_disable,
  300. .set_parent = axi_clkgen_set_parent,
  301. .get_parent = axi_clkgen_get_parent,
  302. };
  303. static const struct of_device_id axi_clkgen_ids[] = {
  304. {
  305. .compatible = "adi,axi-clkgen-2.00.a",
  306. },
  307. { },
  308. };
  309. MODULE_DEVICE_TABLE(of, axi_clkgen_ids);
  310. static int axi_clkgen_probe(struct platform_device *pdev)
  311. {
  312. const struct of_device_id *id;
  313. struct axi_clkgen *axi_clkgen;
  314. struct clk_init_data init;
  315. const char *parent_names[2];
  316. const char *clk_name;
  317. struct resource *mem;
  318. unsigned int i;
  319. int ret;
  320. if (!pdev->dev.of_node)
  321. return -ENODEV;
  322. id = of_match_node(axi_clkgen_ids, pdev->dev.of_node);
  323. if (!id)
  324. return -ENODEV;
  325. axi_clkgen = devm_kzalloc(&pdev->dev, sizeof(*axi_clkgen), GFP_KERNEL);
  326. if (!axi_clkgen)
  327. return -ENOMEM;
  328. mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  329. axi_clkgen->base = devm_ioremap_resource(&pdev->dev, mem);
  330. if (IS_ERR(axi_clkgen->base))
  331. return PTR_ERR(axi_clkgen->base);
  332. init.num_parents = of_clk_get_parent_count(pdev->dev.of_node);
  333. if (init.num_parents < 1 || init.num_parents > 2)
  334. return -EINVAL;
  335. for (i = 0; i < init.num_parents; i++) {
  336. parent_names[i] = of_clk_get_parent_name(pdev->dev.of_node, i);
  337. if (!parent_names[i])
  338. return -EINVAL;
  339. }
  340. clk_name = pdev->dev.of_node->name;
  341. of_property_read_string(pdev->dev.of_node, "clock-output-names",
  342. &clk_name);
  343. init.name = clk_name;
  344. init.ops = &axi_clkgen_ops;
  345. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  346. init.parent_names = parent_names;
  347. axi_clkgen_mmcm_enable(axi_clkgen, false);
  348. axi_clkgen->clk_hw.init = &init;
  349. ret = devm_clk_hw_register(&pdev->dev, &axi_clkgen->clk_hw);
  350. if (ret)
  351. return ret;
  352. return of_clk_add_hw_provider(pdev->dev.of_node, of_clk_hw_simple_get,
  353. &axi_clkgen->clk_hw);
  354. }
  355. static int axi_clkgen_remove(struct platform_device *pdev)
  356. {
  357. of_clk_del_provider(pdev->dev.of_node);
  358. return 0;
  359. }
  360. static struct platform_driver axi_clkgen_driver = {
  361. .driver = {
  362. .name = "adi-axi-clkgen",
  363. .of_match_table = axi_clkgen_ids,
  364. },
  365. .probe = axi_clkgen_probe,
  366. .remove = axi_clkgen_remove,
  367. };
  368. module_platform_driver(axi_clkgen_driver);
  369. MODULE_LICENSE("GPL v2");
  370. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  371. MODULE_DESCRIPTION("Driver for the Analog Devices' AXI clkgen pcore clock generator");