clk-axi-clkgen.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * AXI clkgen driver
  4. *
  5. * Copyright 2012-2013 Analog Devices Inc.
  6. * Author: Lars-Peter Clausen <lars@metafoo.de>
  7. */
  8. #include <linux/adi-axi-common.h>
  9. #include <linux/bits.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/err.h>
  13. #include <linux/io.h>
  14. #include <linux/module.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/of.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/slab.h>
  19. #define AXI_CLKGEN_V2_REG_RESET 0x40
  20. #define AXI_CLKGEN_V2_REG_CLKSEL 0x44
  21. #define AXI_CLKGEN_V2_REG_DRP_CNTRL 0x70
  22. #define AXI_CLKGEN_V2_REG_DRP_STATUS 0x74
  23. #define AXI_CLKGEN_V2_RESET_MMCM_ENABLE BIT(1)
  24. #define AXI_CLKGEN_V2_RESET_ENABLE BIT(0)
  25. #define AXI_CLKGEN_V2_DRP_CNTRL_SEL BIT(29)
  26. #define AXI_CLKGEN_V2_DRP_CNTRL_READ BIT(28)
  27. #define AXI_CLKGEN_V2_DRP_STATUS_BUSY BIT(16)
  28. #define ADI_CLKGEN_REG_FPGA_VOLTAGE 0x0140
  29. #define ADI_CLKGEN_INFO_FPGA_VOLTAGE(val) ((val) & GENMASK(15, 0))
  30. #define MMCM_REG_CLKOUT5_2 0x07
  31. #define MMCM_REG_CLKOUT0_1 0x08
  32. #define MMCM_REG_CLKOUT0_2 0x09
  33. #define MMCM_REG_CLKOUT6_2 0x13
  34. #define MMCM_REG_CLK_FB1 0x14
  35. #define MMCM_REG_CLK_FB2 0x15
  36. #define MMCM_REG_CLK_DIV 0x16
  37. #define MMCM_REG_LOCK1 0x18
  38. #define MMCM_REG_LOCK2 0x19
  39. #define MMCM_REG_LOCK3 0x1a
  40. #define MMCM_REG_POWER 0x28
  41. #define MMCM_REG_FILTER1 0x4e
  42. #define MMCM_REG_FILTER2 0x4f
  43. #define MMCM_CLKOUT_NOCOUNT BIT(6)
  44. #define MMCM_CLK_DIV_DIVIDE BIT(11)
  45. #define MMCM_CLK_DIV_NOCOUNT BIT(12)
  46. struct axi_clkgen_limits {
  47. unsigned int fpfd_min;
  48. unsigned int fpfd_max;
  49. unsigned int fvco_min;
  50. unsigned int fvco_max;
  51. };
  52. struct axi_clkgen {
  53. void __iomem *base;
  54. struct clk_hw clk_hw;
  55. struct axi_clkgen_limits limits;
  56. };
  57. static uint32_t axi_clkgen_lookup_filter(unsigned int m)
  58. {
  59. switch (m) {
  60. case 0:
  61. return 0x01001990;
  62. case 1:
  63. return 0x01001190;
  64. case 2:
  65. return 0x01009890;
  66. case 3:
  67. return 0x01001890;
  68. case 4:
  69. return 0x01008890;
  70. case 5 ... 8:
  71. return 0x01009090;
  72. case 9 ... 11:
  73. return 0x01000890;
  74. case 12:
  75. return 0x08009090;
  76. case 13 ... 22:
  77. return 0x01001090;
  78. case 23 ... 36:
  79. return 0x01008090;
  80. case 37 ... 46:
  81. return 0x08001090;
  82. default:
  83. return 0x08008090;
  84. }
  85. }
  86. static const u32 axi_clkgen_lock_table[] = {
  87. 0x060603e8, 0x060603e8, 0x080803e8, 0x0b0b03e8,
  88. 0x0e0e03e8, 0x111103e8, 0x131303e8, 0x161603e8,
  89. 0x191903e8, 0x1c1c03e8, 0x1f1f0384, 0x1f1f0339,
  90. 0x1f1f02ee, 0x1f1f02bc, 0x1f1f028a, 0x1f1f0271,
  91. 0x1f1f023f, 0x1f1f0226, 0x1f1f020d, 0x1f1f01f4,
  92. 0x1f1f01db, 0x1f1f01c2, 0x1f1f01a9, 0x1f1f0190,
  93. 0x1f1f0190, 0x1f1f0177, 0x1f1f015e, 0x1f1f015e,
  94. 0x1f1f0145, 0x1f1f0145, 0x1f1f012c, 0x1f1f012c,
  95. 0x1f1f012c, 0x1f1f0113, 0x1f1f0113, 0x1f1f0113,
  96. };
  97. static u32 axi_clkgen_lookup_lock(unsigned int m)
  98. {
  99. if (m < ARRAY_SIZE(axi_clkgen_lock_table))
  100. return axi_clkgen_lock_table[m];
  101. return 0x1f1f00fa;
  102. }
  103. static const struct axi_clkgen_limits axi_clkgen_zynqmp_default_limits = {
  104. .fpfd_min = 10000,
  105. .fpfd_max = 450000,
  106. .fvco_min = 800000,
  107. .fvco_max = 1600000,
  108. };
  109. static const struct axi_clkgen_limits axi_clkgen_zynq_default_limits = {
  110. .fpfd_min = 10000,
  111. .fpfd_max = 450000,
  112. .fvco_min = 600000,
  113. .fvco_max = 1200000,
  114. };
  115. static void axi_clkgen_calc_params(const struct axi_clkgen_limits *limits,
  116. unsigned long fin, unsigned long fout,
  117. unsigned int *best_d, unsigned int *best_m,
  118. unsigned int *best_dout)
  119. {
  120. unsigned long d, d_min, d_max, _d_min, _d_max;
  121. unsigned long m, m_min, m_max;
  122. unsigned long f, dout, best_f, fvco;
  123. unsigned long fract_shift = 0;
  124. unsigned long fvco_min_fract, fvco_max_fract;
  125. fin /= 1000;
  126. fout /= 1000;
  127. best_f = ULONG_MAX;
  128. *best_d = 0;
  129. *best_m = 0;
  130. *best_dout = 0;
  131. d_min = max(DIV_ROUND_UP(fin, limits->fpfd_max), 1);
  132. d_max = min(fin / limits->fpfd_min, 80);
  133. again:
  134. fvco_min_fract = limits->fvco_min << fract_shift;
  135. fvco_max_fract = limits->fvco_max << fract_shift;
  136. m_min = max(DIV_ROUND_UP(fvco_min_fract, fin) * d_min, 1);
  137. m_max = min(fvco_max_fract * d_max / fin, 64 << fract_shift);
  138. for (m = m_min; m <= m_max; m++) {
  139. _d_min = max(d_min, DIV_ROUND_UP(fin * m, fvco_max_fract));
  140. _d_max = min(d_max, fin * m / fvco_min_fract);
  141. for (d = _d_min; d <= _d_max; d++) {
  142. fvco = fin * m / d;
  143. dout = DIV_ROUND_CLOSEST(fvco, fout);
  144. dout = clamp_t(unsigned long, dout, 1, 128 << fract_shift);
  145. f = fvco / dout;
  146. if (abs(f - fout) < abs(best_f - fout)) {
  147. best_f = f;
  148. *best_d = d;
  149. *best_m = m << (3 - fract_shift);
  150. *best_dout = dout << (3 - fract_shift);
  151. if (best_f == fout)
  152. return;
  153. }
  154. }
  155. }
  156. /* Let's see if we find a better setting in fractional mode */
  157. if (fract_shift == 0) {
  158. fract_shift = 3;
  159. goto again;
  160. }
  161. }
  162. struct axi_clkgen_div_params {
  163. unsigned int low;
  164. unsigned int high;
  165. unsigned int edge;
  166. unsigned int nocount;
  167. unsigned int frac_en;
  168. unsigned int frac;
  169. unsigned int frac_wf_f;
  170. unsigned int frac_wf_r;
  171. unsigned int frac_phase;
  172. };
  173. static void axi_clkgen_calc_clk_params(unsigned int divider,
  174. unsigned int frac_divider,
  175. struct axi_clkgen_div_params *params)
  176. {
  177. memset(params, 0x0, sizeof(*params));
  178. if (divider == 1) {
  179. params->nocount = 1;
  180. return;
  181. }
  182. if (frac_divider == 0) {
  183. params->high = divider / 2;
  184. params->edge = divider % 2;
  185. params->low = divider - params->high;
  186. } else {
  187. params->frac_en = 1;
  188. params->frac = frac_divider;
  189. params->high = divider / 2;
  190. params->edge = divider % 2;
  191. params->low = params->high;
  192. if (params->edge == 0) {
  193. params->high--;
  194. params->frac_wf_r = 1;
  195. }
  196. if (params->edge == 0 || frac_divider == 1)
  197. params->low--;
  198. if (((params->edge == 0) ^ (frac_divider == 1)) ||
  199. (divider == 2 && frac_divider == 1))
  200. params->frac_wf_f = 1;
  201. params->frac_phase = params->edge * 4 + frac_divider / 2;
  202. }
  203. }
  204. static void axi_clkgen_write(struct axi_clkgen *axi_clkgen,
  205. unsigned int reg, unsigned int val)
  206. {
  207. writel(val, axi_clkgen->base + reg);
  208. }
  209. static void axi_clkgen_read(struct axi_clkgen *axi_clkgen,
  210. unsigned int reg, unsigned int *val)
  211. {
  212. *val = readl(axi_clkgen->base + reg);
  213. }
  214. static int axi_clkgen_wait_non_busy(struct axi_clkgen *axi_clkgen)
  215. {
  216. unsigned int timeout = 10000;
  217. unsigned int val;
  218. do {
  219. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_STATUS, &val);
  220. } while ((val & AXI_CLKGEN_V2_DRP_STATUS_BUSY) && --timeout);
  221. if (val & AXI_CLKGEN_V2_DRP_STATUS_BUSY)
  222. return -EIO;
  223. return val & 0xffff;
  224. }
  225. static int axi_clkgen_mmcm_read(struct axi_clkgen *axi_clkgen,
  226. unsigned int reg, unsigned int *val)
  227. {
  228. unsigned int reg_val;
  229. int ret;
  230. ret = axi_clkgen_wait_non_busy(axi_clkgen);
  231. if (ret < 0)
  232. return ret;
  233. reg_val = AXI_CLKGEN_V2_DRP_CNTRL_SEL | AXI_CLKGEN_V2_DRP_CNTRL_READ;
  234. reg_val |= (reg << 16);
  235. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
  236. ret = axi_clkgen_wait_non_busy(axi_clkgen);
  237. if (ret < 0)
  238. return ret;
  239. *val = ret;
  240. return 0;
  241. }
  242. static int axi_clkgen_mmcm_write(struct axi_clkgen *axi_clkgen,
  243. unsigned int reg, unsigned int val,
  244. unsigned int mask)
  245. {
  246. unsigned int reg_val = 0;
  247. int ret;
  248. ret = axi_clkgen_wait_non_busy(axi_clkgen);
  249. if (ret < 0)
  250. return ret;
  251. if (mask != 0xffff) {
  252. axi_clkgen_mmcm_read(axi_clkgen, reg, &reg_val);
  253. reg_val &= ~mask;
  254. }
  255. reg_val |= AXI_CLKGEN_V2_DRP_CNTRL_SEL | (reg << 16) | (val & mask);
  256. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_DRP_CNTRL, reg_val);
  257. return 0;
  258. }
  259. static void axi_clkgen_mmcm_enable(struct axi_clkgen *axi_clkgen, bool enable)
  260. {
  261. unsigned int val = AXI_CLKGEN_V2_RESET_ENABLE;
  262. if (enable)
  263. val |= AXI_CLKGEN_V2_RESET_MMCM_ENABLE;
  264. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_RESET, val);
  265. }
  266. static struct axi_clkgen *clk_hw_to_axi_clkgen(struct clk_hw *clk_hw)
  267. {
  268. return container_of(clk_hw, struct axi_clkgen, clk_hw);
  269. }
  270. static void axi_clkgen_set_div(struct axi_clkgen *axi_clkgen,
  271. unsigned int reg1, unsigned int reg2,
  272. unsigned int reg3,
  273. struct axi_clkgen_div_params *params)
  274. {
  275. axi_clkgen_mmcm_write(axi_clkgen, reg1,
  276. (params->high << 6) | params->low, 0xefff);
  277. axi_clkgen_mmcm_write(axi_clkgen, reg2,
  278. (params->frac << 12) | (params->frac_en << 11) |
  279. (params->frac_wf_r << 10) | (params->edge << 7) |
  280. (params->nocount << 6), 0x7fff);
  281. if (reg3 != 0) {
  282. axi_clkgen_mmcm_write(axi_clkgen, reg3,
  283. (params->frac_phase << 11) | (params->frac_wf_f << 10),
  284. 0x3c00);
  285. }
  286. }
  287. static int axi_clkgen_set_rate(struct clk_hw *clk_hw, unsigned long rate,
  288. unsigned long parent_rate)
  289. {
  290. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  291. const struct axi_clkgen_limits *limits = &axi_clkgen->limits;
  292. unsigned int d, m, dout;
  293. struct axi_clkgen_div_params params;
  294. u32 power = 0, filter, lock;
  295. if (parent_rate == 0 || rate == 0)
  296. return -EINVAL;
  297. axi_clkgen_calc_params(limits, parent_rate, rate, &d, &m, &dout);
  298. if (d == 0 || dout == 0 || m == 0)
  299. return -EINVAL;
  300. if ((dout & 0x7) != 0 || (m & 0x7) != 0)
  301. power |= 0x9800;
  302. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_POWER, power, 0x9800);
  303. filter = axi_clkgen_lookup_filter(m - 1);
  304. lock = axi_clkgen_lookup_lock(m - 1);
  305. axi_clkgen_calc_clk_params(dout >> 3, dout & 0x7, &params);
  306. axi_clkgen_set_div(axi_clkgen, MMCM_REG_CLKOUT0_1, MMCM_REG_CLKOUT0_2,
  307. MMCM_REG_CLKOUT5_2, &params);
  308. axi_clkgen_calc_clk_params(d, 0, &params);
  309. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_CLK_DIV,
  310. (params.edge << 13) | (params.nocount << 12) |
  311. (params.high << 6) | params.low, 0x3fff);
  312. axi_clkgen_calc_clk_params(m >> 3, m & 0x7, &params);
  313. axi_clkgen_set_div(axi_clkgen, MMCM_REG_CLK_FB1, MMCM_REG_CLK_FB2,
  314. MMCM_REG_CLKOUT6_2, &params);
  315. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK1, lock & 0x3ff, 0x3ff);
  316. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK2,
  317. (((lock >> 16) & 0x1f) << 10) | 0x1, 0x7fff);
  318. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_LOCK3,
  319. (((lock >> 24) & 0x1f) << 10) | 0x3e9, 0x7fff);
  320. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER1, filter >> 16, 0x9900);
  321. axi_clkgen_mmcm_write(axi_clkgen, MMCM_REG_FILTER2, filter, 0x9900);
  322. return 0;
  323. }
  324. static int axi_clkgen_determine_rate(struct clk_hw *hw,
  325. struct clk_rate_request *req)
  326. {
  327. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(hw);
  328. const struct axi_clkgen_limits *limits = &axi_clkgen->limits;
  329. unsigned int d, m, dout;
  330. unsigned long long tmp;
  331. axi_clkgen_calc_params(limits, req->best_parent_rate, req->rate,
  332. &d, &m, &dout);
  333. if (d == 0 || dout == 0 || m == 0)
  334. return -EINVAL;
  335. tmp = (unsigned long long)req->best_parent_rate * m;
  336. tmp = DIV_ROUND_CLOSEST_ULL(tmp, dout * d);
  337. req->rate = min_t(unsigned long long, tmp, LONG_MAX);
  338. return 0;
  339. }
  340. static unsigned int axi_clkgen_get_div(struct axi_clkgen *axi_clkgen,
  341. unsigned int reg1, unsigned int reg2)
  342. {
  343. unsigned int val1, val2;
  344. unsigned int div;
  345. axi_clkgen_mmcm_read(axi_clkgen, reg2, &val2);
  346. if (val2 & MMCM_CLKOUT_NOCOUNT)
  347. return 8;
  348. axi_clkgen_mmcm_read(axi_clkgen, reg1, &val1);
  349. div = (val1 & 0x3f) + ((val1 >> 6) & 0x3f);
  350. div <<= 3;
  351. if (val2 & MMCM_CLK_DIV_DIVIDE) {
  352. if ((val2 & BIT(7)) && (val2 & 0x7000) != 0x1000)
  353. div += 8;
  354. else
  355. div += 16;
  356. div += (val2 >> 12) & 0x7;
  357. }
  358. return div;
  359. }
  360. static unsigned long axi_clkgen_recalc_rate(struct clk_hw *clk_hw,
  361. unsigned long parent_rate)
  362. {
  363. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  364. unsigned int d, m, dout;
  365. unsigned long long tmp;
  366. unsigned int val;
  367. dout = axi_clkgen_get_div(axi_clkgen, MMCM_REG_CLKOUT0_1,
  368. MMCM_REG_CLKOUT0_2);
  369. m = axi_clkgen_get_div(axi_clkgen, MMCM_REG_CLK_FB1,
  370. MMCM_REG_CLK_FB2);
  371. axi_clkgen_mmcm_read(axi_clkgen, MMCM_REG_CLK_DIV, &val);
  372. if (val & MMCM_CLK_DIV_NOCOUNT)
  373. d = 1;
  374. else
  375. d = (val & 0x3f) + ((val >> 6) & 0x3f);
  376. if (d == 0 || dout == 0)
  377. return 0;
  378. tmp = (unsigned long long)parent_rate * m;
  379. tmp = DIV_ROUND_CLOSEST_ULL(tmp, dout * d);
  380. return min_t(unsigned long long, tmp, ULONG_MAX);
  381. }
  382. static int axi_clkgen_enable(struct clk_hw *clk_hw)
  383. {
  384. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  385. axi_clkgen_mmcm_enable(axi_clkgen, true);
  386. return 0;
  387. }
  388. static void axi_clkgen_disable(struct clk_hw *clk_hw)
  389. {
  390. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  391. axi_clkgen_mmcm_enable(axi_clkgen, false);
  392. }
  393. static int axi_clkgen_set_parent(struct clk_hw *clk_hw, u8 index)
  394. {
  395. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  396. axi_clkgen_write(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, index);
  397. return 0;
  398. }
  399. static u8 axi_clkgen_get_parent(struct clk_hw *clk_hw)
  400. {
  401. struct axi_clkgen *axi_clkgen = clk_hw_to_axi_clkgen(clk_hw);
  402. unsigned int parent;
  403. axi_clkgen_read(axi_clkgen, AXI_CLKGEN_V2_REG_CLKSEL, &parent);
  404. return parent;
  405. }
  406. static int axi_clkgen_setup_limits(struct axi_clkgen *axi_clkgen,
  407. struct device *dev)
  408. {
  409. unsigned int tech, family, speed_grade, reg_value;
  410. axi_clkgen_read(axi_clkgen, ADI_AXI_REG_FPGA_INFO, &reg_value);
  411. tech = ADI_AXI_INFO_FPGA_TECH(reg_value);
  412. family = ADI_AXI_INFO_FPGA_FAMILY(reg_value);
  413. speed_grade = ADI_AXI_INFO_FPGA_SPEED_GRADE(reg_value);
  414. axi_clkgen->limits.fpfd_min = 10000;
  415. axi_clkgen->limits.fvco_min = 600000;
  416. switch (speed_grade) {
  417. case ADI_AXI_FPGA_SPEED_1 ... ADI_AXI_FPGA_SPEED_1LV:
  418. axi_clkgen->limits.fvco_max = 1200000;
  419. axi_clkgen->limits.fpfd_max = 450000;
  420. break;
  421. case ADI_AXI_FPGA_SPEED_2 ... ADI_AXI_FPGA_SPEED_2LV:
  422. axi_clkgen->limits.fvco_max = 1440000;
  423. axi_clkgen->limits.fpfd_max = 500000;
  424. if (family == ADI_AXI_FPGA_FAMILY_KINTEX || family == ADI_AXI_FPGA_FAMILY_ARTIX) {
  425. axi_clkgen_read(axi_clkgen, ADI_CLKGEN_REG_FPGA_VOLTAGE,
  426. &reg_value);
  427. if (ADI_CLKGEN_INFO_FPGA_VOLTAGE(reg_value) < 950) {
  428. axi_clkgen->limits.fvco_max = 1200000;
  429. axi_clkgen->limits.fpfd_max = 450000;
  430. }
  431. }
  432. break;
  433. case ADI_AXI_FPGA_SPEED_3:
  434. axi_clkgen->limits.fvco_max = 1600000;
  435. axi_clkgen->limits.fpfd_max = 550000;
  436. break;
  437. default:
  438. return dev_err_probe(dev, -ENODEV, "Unknown speed grade %d\n",
  439. speed_grade);
  440. }
  441. /* Overwrite vco limits for ultrascale+ */
  442. if (tech == ADI_AXI_FPGA_TECH_ULTRASCALE_PLUS) {
  443. axi_clkgen->limits.fvco_max = 1600000;
  444. axi_clkgen->limits.fvco_min = 800000;
  445. }
  446. return 0;
  447. }
  448. static const struct clk_ops axi_clkgen_ops = {
  449. .recalc_rate = axi_clkgen_recalc_rate,
  450. .determine_rate = axi_clkgen_determine_rate,
  451. .set_rate = axi_clkgen_set_rate,
  452. .enable = axi_clkgen_enable,
  453. .disable = axi_clkgen_disable,
  454. .set_parent = axi_clkgen_set_parent,
  455. .get_parent = axi_clkgen_get_parent,
  456. };
  457. static int axi_clkgen_probe(struct platform_device *pdev)
  458. {
  459. const struct axi_clkgen_limits *dflt_limits;
  460. struct axi_clkgen *axi_clkgen;
  461. unsigned int pcore_version;
  462. struct clk_init_data init;
  463. const char *parent_names[2];
  464. const char *clk_name;
  465. struct clk *axi_clk;
  466. unsigned int i;
  467. int ret;
  468. dflt_limits = device_get_match_data(&pdev->dev);
  469. if (!dflt_limits)
  470. return -ENODEV;
  471. axi_clkgen = devm_kzalloc(&pdev->dev, sizeof(*axi_clkgen), GFP_KERNEL);
  472. if (!axi_clkgen)
  473. return -ENOMEM;
  474. axi_clkgen->base = devm_platform_ioremap_resource(pdev, 0);
  475. if (IS_ERR(axi_clkgen->base))
  476. return PTR_ERR(axi_clkgen->base);
  477. init.num_parents = of_clk_get_parent_count(pdev->dev.of_node);
  478. axi_clk = devm_clk_get_enabled(&pdev->dev, "s_axi_aclk");
  479. if (!IS_ERR(axi_clk)) {
  480. if (init.num_parents < 2 || init.num_parents > 3)
  481. return -EINVAL;
  482. init.num_parents -= 1;
  483. } else {
  484. /*
  485. * Legacy... So that old DTs which do not have clock-names still
  486. * work. In this case we don't explicitly enable the AXI bus
  487. * clock.
  488. */
  489. if (PTR_ERR(axi_clk) != -ENOENT)
  490. return PTR_ERR(axi_clk);
  491. if (init.num_parents < 1 || init.num_parents > 2)
  492. return -EINVAL;
  493. }
  494. for (i = 0; i < init.num_parents; i++) {
  495. parent_names[i] = of_clk_get_parent_name(pdev->dev.of_node, i);
  496. if (!parent_names[i])
  497. return -EINVAL;
  498. }
  499. axi_clkgen_read(axi_clkgen, ADI_AXI_REG_VERSION, &pcore_version);
  500. if (ADI_AXI_PCORE_VER_MAJOR(pcore_version) > 0x04) {
  501. ret = axi_clkgen_setup_limits(axi_clkgen, &pdev->dev);
  502. if (ret)
  503. return ret;
  504. } else {
  505. memcpy(&axi_clkgen->limits, dflt_limits,
  506. sizeof(axi_clkgen->limits));
  507. }
  508. clk_name = pdev->dev.of_node->name;
  509. of_property_read_string(pdev->dev.of_node, "clock-output-names",
  510. &clk_name);
  511. init.name = clk_name;
  512. init.ops = &axi_clkgen_ops;
  513. init.flags = CLK_SET_RATE_GATE | CLK_SET_PARENT_GATE;
  514. init.parent_names = parent_names;
  515. axi_clkgen_mmcm_enable(axi_clkgen, false);
  516. axi_clkgen->clk_hw.init = &init;
  517. ret = devm_clk_hw_register(&pdev->dev, &axi_clkgen->clk_hw);
  518. if (ret)
  519. return ret;
  520. return devm_of_clk_add_hw_provider(&pdev->dev, of_clk_hw_simple_get,
  521. &axi_clkgen->clk_hw);
  522. }
  523. static const struct of_device_id axi_clkgen_ids[] = {
  524. {
  525. .compatible = "adi,zynqmp-axi-clkgen-2.00.a",
  526. .data = &axi_clkgen_zynqmp_default_limits,
  527. },
  528. {
  529. .compatible = "adi,axi-clkgen-2.00.a",
  530. .data = &axi_clkgen_zynq_default_limits,
  531. },
  532. { }
  533. };
  534. MODULE_DEVICE_TABLE(of, axi_clkgen_ids);
  535. static struct platform_driver axi_clkgen_driver = {
  536. .driver = {
  537. .name = "adi-axi-clkgen",
  538. .of_match_table = axi_clkgen_ids,
  539. },
  540. .probe = axi_clkgen_probe,
  541. };
  542. module_platform_driver(axi_clkgen_driver);
  543. MODULE_LICENSE("GPL v2");
  544. MODULE_AUTHOR("Lars-Peter Clausen <lars@metafoo.de>");
  545. MODULE_DESCRIPTION("Driver for the Analog Devices' AXI clkgen pcore clock generator");