clk-axi-clkgen.c 12 KB

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