xlnx_vcu.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Xilinx VCU Init
  4. *
  5. * Copyright (C) 2016 - 2017 Xilinx, Inc.
  6. *
  7. * Contacts Dhaval Shah <dshah@xilinx.com>
  8. */
  9. #include <linux/bitfield.h>
  10. #include <linux/clk.h>
  11. #include <linux/clk-provider.h>
  12. #include <linux/device.h>
  13. #include <linux/errno.h>
  14. #include <linux/io.h>
  15. #include <linux/mfd/syscon.h>
  16. #include <linux/mfd/syscon/xlnx-vcu.h>
  17. #include <linux/module.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/regmap.h>
  21. #include <dt-bindings/clock/xlnx-vcu.h>
  22. #define VCU_PLL_CTRL 0x24
  23. #define VCU_PLL_CTRL_RESET BIT(0)
  24. #define VCU_PLL_CTRL_POR_IN BIT(1)
  25. #define VCU_PLL_CTRL_PWR_POR BIT(2)
  26. #define VCU_PLL_CTRL_BYPASS BIT(3)
  27. #define VCU_PLL_CTRL_FBDIV GENMASK(14, 8)
  28. #define VCU_PLL_CTRL_CLKOUTDIV GENMASK(18, 16)
  29. #define VCU_PLL_CFG 0x28
  30. #define VCU_PLL_CFG_RES GENMASK(3, 0)
  31. #define VCU_PLL_CFG_CP GENMASK(8, 5)
  32. #define VCU_PLL_CFG_LFHF GENMASK(12, 10)
  33. #define VCU_PLL_CFG_LOCK_CNT GENMASK(22, 13)
  34. #define VCU_PLL_CFG_LOCK_DLY GENMASK(31, 25)
  35. #define VCU_ENC_CORE_CTRL 0x30
  36. #define VCU_ENC_MCU_CTRL 0x34
  37. #define VCU_DEC_CORE_CTRL 0x38
  38. #define VCU_DEC_MCU_CTRL 0x3c
  39. #define VCU_PLL_STATUS 0x60
  40. #define VCU_PLL_STATUS_LOCK_STATUS BIT(0)
  41. #define MHZ 1000000
  42. #define FVCO_MIN (1500U * MHZ)
  43. #define FVCO_MAX (3000U * MHZ)
  44. /**
  45. * struct xvcu_device - Xilinx VCU init device structure
  46. * @dev: Platform device
  47. * @pll_ref: pll ref clock source
  48. * @aclk: axi clock source
  49. * @logicore_reg_ba: logicore reg base address
  50. * @vcu_slcr_ba: vcu_slcr Register base address
  51. * @pll: handle for the VCU PLL
  52. * @pll_post: handle for the VCU PLL post divider
  53. * @clk_data: clocks provided by the vcu clock provider
  54. */
  55. struct xvcu_device {
  56. struct device *dev;
  57. struct clk *pll_ref;
  58. struct clk *aclk;
  59. struct regmap *logicore_reg_ba;
  60. void __iomem *vcu_slcr_ba;
  61. struct clk_hw *pll;
  62. struct clk_hw *pll_post;
  63. struct clk_hw_onecell_data *clk_data;
  64. };
  65. static const struct regmap_config vcu_settings_regmap_config = {
  66. .name = "regmap",
  67. .reg_bits = 32,
  68. .val_bits = 32,
  69. .reg_stride = 4,
  70. .max_register = 0xfff,
  71. .cache_type = REGCACHE_NONE,
  72. };
  73. /**
  74. * struct xvcu_pll_cfg - Helper data
  75. * @fbdiv: The integer portion of the feedback divider to the PLL
  76. * @cp: PLL charge pump control
  77. * @res: PLL loop filter resistor control
  78. * @lfhf: PLL loop filter high frequency capacitor control
  79. * @lock_dly: Lock circuit configuration settings for lock windowsize
  80. * @lock_cnt: Lock circuit counter setting
  81. */
  82. struct xvcu_pll_cfg {
  83. u32 fbdiv;
  84. u32 cp;
  85. u32 res;
  86. u32 lfhf;
  87. u32 lock_dly;
  88. u32 lock_cnt;
  89. };
  90. static const struct xvcu_pll_cfg xvcu_pll_cfg[] = {
  91. { 25, 3, 10, 3, 63, 1000 },
  92. { 26, 3, 10, 3, 63, 1000 },
  93. { 27, 4, 6, 3, 63, 1000 },
  94. { 28, 4, 6, 3, 63, 1000 },
  95. { 29, 4, 6, 3, 63, 1000 },
  96. { 30, 4, 6, 3, 63, 1000 },
  97. { 31, 6, 1, 3, 63, 1000 },
  98. { 32, 6, 1, 3, 63, 1000 },
  99. { 33, 4, 10, 3, 63, 1000 },
  100. { 34, 5, 6, 3, 63, 1000 },
  101. { 35, 5, 6, 3, 63, 1000 },
  102. { 36, 5, 6, 3, 63, 1000 },
  103. { 37, 5, 6, 3, 63, 1000 },
  104. { 38, 5, 6, 3, 63, 975 },
  105. { 39, 3, 12, 3, 63, 950 },
  106. { 40, 3, 12, 3, 63, 925 },
  107. { 41, 3, 12, 3, 63, 900 },
  108. { 42, 3, 12, 3, 63, 875 },
  109. { 43, 3, 12, 3, 63, 850 },
  110. { 44, 3, 12, 3, 63, 850 },
  111. { 45, 3, 12, 3, 63, 825 },
  112. { 46, 3, 12, 3, 63, 800 },
  113. { 47, 3, 12, 3, 63, 775 },
  114. { 48, 3, 12, 3, 63, 775 },
  115. { 49, 3, 12, 3, 63, 750 },
  116. { 50, 3, 12, 3, 63, 750 },
  117. { 51, 3, 2, 3, 63, 725 },
  118. { 52, 3, 2, 3, 63, 700 },
  119. { 53, 3, 2, 3, 63, 700 },
  120. { 54, 3, 2, 3, 63, 675 },
  121. { 55, 3, 2, 3, 63, 675 },
  122. { 56, 3, 2, 3, 63, 650 },
  123. { 57, 3, 2, 3, 63, 650 },
  124. { 58, 3, 2, 3, 63, 625 },
  125. { 59, 3, 2, 3, 63, 625 },
  126. { 60, 3, 2, 3, 63, 625 },
  127. { 61, 3, 2, 3, 63, 600 },
  128. { 62, 3, 2, 3, 63, 600 },
  129. { 63, 3, 2, 3, 63, 600 },
  130. { 64, 3, 2, 3, 63, 600 },
  131. { 65, 3, 2, 3, 63, 600 },
  132. { 66, 3, 2, 3, 63, 600 },
  133. { 67, 3, 2, 3, 63, 600 },
  134. { 68, 3, 2, 3, 63, 600 },
  135. { 69, 3, 2, 3, 63, 600 },
  136. { 70, 3, 2, 3, 63, 600 },
  137. { 71, 3, 2, 3, 63, 600 },
  138. { 72, 3, 2, 3, 63, 600 },
  139. { 73, 3, 2, 3, 63, 600 },
  140. { 74, 3, 2, 3, 63, 600 },
  141. { 75, 3, 2, 3, 63, 600 },
  142. { 76, 3, 2, 3, 63, 600 },
  143. { 77, 3, 2, 3, 63, 600 },
  144. { 78, 3, 2, 3, 63, 600 },
  145. { 79, 3, 2, 3, 63, 600 },
  146. { 80, 3, 2, 3, 63, 600 },
  147. { 81, 3, 2, 3, 63, 600 },
  148. { 82, 3, 2, 3, 63, 600 },
  149. { 83, 4, 2, 3, 63, 600 },
  150. { 84, 4, 2, 3, 63, 600 },
  151. { 85, 4, 2, 3, 63, 600 },
  152. { 86, 4, 2, 3, 63, 600 },
  153. { 87, 4, 2, 3, 63, 600 },
  154. { 88, 4, 2, 3, 63, 600 },
  155. { 89, 4, 2, 3, 63, 600 },
  156. { 90, 4, 2, 3, 63, 600 },
  157. { 91, 4, 2, 3, 63, 600 },
  158. { 92, 4, 2, 3, 63, 600 },
  159. { 93, 4, 2, 3, 63, 600 },
  160. { 94, 4, 2, 3, 63, 600 },
  161. { 95, 4, 2, 3, 63, 600 },
  162. { 96, 4, 2, 3, 63, 600 },
  163. { 97, 4, 2, 3, 63, 600 },
  164. { 98, 4, 2, 3, 63, 600 },
  165. { 99, 4, 2, 3, 63, 600 },
  166. { 100, 4, 2, 3, 63, 600 },
  167. { 101, 4, 2, 3, 63, 600 },
  168. { 102, 4, 2, 3, 63, 600 },
  169. { 103, 5, 2, 3, 63, 600 },
  170. { 104, 5, 2, 3, 63, 600 },
  171. { 105, 5, 2, 3, 63, 600 },
  172. { 106, 5, 2, 3, 63, 600 },
  173. { 107, 3, 4, 3, 63, 600 },
  174. { 108, 3, 4, 3, 63, 600 },
  175. { 109, 3, 4, 3, 63, 600 },
  176. { 110, 3, 4, 3, 63, 600 },
  177. { 111, 3, 4, 3, 63, 600 },
  178. { 112, 3, 4, 3, 63, 600 },
  179. { 113, 3, 4, 3, 63, 600 },
  180. { 114, 3, 4, 3, 63, 600 },
  181. { 115, 3, 4, 3, 63, 600 },
  182. { 116, 3, 4, 3, 63, 600 },
  183. { 117, 3, 4, 3, 63, 600 },
  184. { 118, 3, 4, 3, 63, 600 },
  185. { 119, 3, 4, 3, 63, 600 },
  186. { 120, 3, 4, 3, 63, 600 },
  187. { 121, 3, 4, 3, 63, 600 },
  188. { 122, 3, 4, 3, 63, 600 },
  189. { 123, 3, 4, 3, 63, 600 },
  190. { 124, 3, 4, 3, 63, 600 },
  191. { 125, 3, 4, 3, 63, 600 },
  192. };
  193. /**
  194. * xvcu_read - Read from the VCU register space
  195. * @iomem: vcu reg space base address
  196. * @offset: vcu reg offset from base
  197. *
  198. * Return: Returns 32bit value from VCU register specified
  199. *
  200. */
  201. static inline u32 xvcu_read(void __iomem *iomem, u32 offset)
  202. {
  203. return ioread32(iomem + offset);
  204. }
  205. /**
  206. * xvcu_write - Write to the VCU register space
  207. * @iomem: vcu reg space base address
  208. * @offset: vcu reg offset from base
  209. * @value: Value to write
  210. */
  211. static inline void xvcu_write(void __iomem *iomem, u32 offset, u32 value)
  212. {
  213. iowrite32(value, iomem + offset);
  214. }
  215. #define to_vcu_pll(_hw) container_of(_hw, struct vcu_pll, hw)
  216. struct vcu_pll {
  217. struct clk_hw hw;
  218. void __iomem *reg_base;
  219. unsigned long fvco_min;
  220. unsigned long fvco_max;
  221. };
  222. static int xvcu_pll_wait_for_lock(struct vcu_pll *pll)
  223. {
  224. void __iomem *base = pll->reg_base;
  225. unsigned long timeout;
  226. u32 lock_status;
  227. timeout = jiffies + msecs_to_jiffies(2000);
  228. do {
  229. lock_status = xvcu_read(base, VCU_PLL_STATUS);
  230. if (lock_status & VCU_PLL_STATUS_LOCK_STATUS)
  231. return 0;
  232. } while (!time_after(jiffies, timeout));
  233. return -ETIMEDOUT;
  234. }
  235. static struct clk_hw *xvcu_register_pll_post(struct device *dev,
  236. const char *name,
  237. const struct clk_hw *parent_hw,
  238. void __iomem *reg_base)
  239. {
  240. u32 div;
  241. u32 vcu_pll_ctrl;
  242. /*
  243. * The output divider of the PLL must be set to 1/2 to meet the
  244. * timing in the design.
  245. */
  246. vcu_pll_ctrl = xvcu_read(reg_base, VCU_PLL_CTRL);
  247. div = FIELD_GET(VCU_PLL_CTRL_CLKOUTDIV, vcu_pll_ctrl);
  248. if (div != 1)
  249. return ERR_PTR(-EINVAL);
  250. return clk_hw_register_fixed_factor(dev, "vcu_pll_post",
  251. clk_hw_get_name(parent_hw),
  252. CLK_SET_RATE_PARENT, 1, 2);
  253. }
  254. static const struct xvcu_pll_cfg *xvcu_find_cfg(int div)
  255. {
  256. const struct xvcu_pll_cfg *cfg = NULL;
  257. unsigned int i;
  258. for (i = 0; i < ARRAY_SIZE(xvcu_pll_cfg) - 1; i++)
  259. if (xvcu_pll_cfg[i].fbdiv == div)
  260. cfg = &xvcu_pll_cfg[i];
  261. return cfg;
  262. }
  263. static int xvcu_pll_set_div(struct vcu_pll *pll, int div)
  264. {
  265. void __iomem *base = pll->reg_base;
  266. const struct xvcu_pll_cfg *cfg = NULL;
  267. u32 vcu_pll_ctrl;
  268. u32 cfg_val;
  269. cfg = xvcu_find_cfg(div);
  270. if (!cfg)
  271. return -EINVAL;
  272. vcu_pll_ctrl = xvcu_read(base, VCU_PLL_CTRL);
  273. vcu_pll_ctrl &= ~VCU_PLL_CTRL_FBDIV;
  274. vcu_pll_ctrl |= FIELD_PREP(VCU_PLL_CTRL_FBDIV, cfg->fbdiv);
  275. xvcu_write(base, VCU_PLL_CTRL, vcu_pll_ctrl);
  276. cfg_val = FIELD_PREP(VCU_PLL_CFG_RES, cfg->res) |
  277. FIELD_PREP(VCU_PLL_CFG_CP, cfg->cp) |
  278. FIELD_PREP(VCU_PLL_CFG_LFHF, cfg->lfhf) |
  279. FIELD_PREP(VCU_PLL_CFG_LOCK_CNT, cfg->lock_cnt) |
  280. FIELD_PREP(VCU_PLL_CFG_LOCK_DLY, cfg->lock_dly);
  281. xvcu_write(base, VCU_PLL_CFG, cfg_val);
  282. return 0;
  283. }
  284. static long xvcu_pll_round_rate(struct clk_hw *hw,
  285. unsigned long rate, unsigned long *parent_rate)
  286. {
  287. struct vcu_pll *pll = to_vcu_pll(hw);
  288. unsigned int feedback_div;
  289. rate = clamp_t(unsigned long, rate, pll->fvco_min, pll->fvco_max);
  290. feedback_div = DIV_ROUND_CLOSEST_ULL(rate, *parent_rate);
  291. feedback_div = clamp_t(unsigned int, feedback_div, 25, 125);
  292. return *parent_rate * feedback_div;
  293. }
  294. static unsigned long xvcu_pll_recalc_rate(struct clk_hw *hw,
  295. unsigned long parent_rate)
  296. {
  297. struct vcu_pll *pll = to_vcu_pll(hw);
  298. void __iomem *base = pll->reg_base;
  299. unsigned int div;
  300. u32 vcu_pll_ctrl;
  301. vcu_pll_ctrl = xvcu_read(base, VCU_PLL_CTRL);
  302. div = FIELD_GET(VCU_PLL_CTRL_FBDIV, vcu_pll_ctrl);
  303. return div * parent_rate;
  304. }
  305. static int xvcu_pll_set_rate(struct clk_hw *hw,
  306. unsigned long rate, unsigned long parent_rate)
  307. {
  308. struct vcu_pll *pll = to_vcu_pll(hw);
  309. return xvcu_pll_set_div(pll, rate / parent_rate);
  310. }
  311. static int xvcu_pll_enable(struct clk_hw *hw)
  312. {
  313. struct vcu_pll *pll = to_vcu_pll(hw);
  314. void __iomem *base = pll->reg_base;
  315. u32 vcu_pll_ctrl;
  316. int ret;
  317. vcu_pll_ctrl = xvcu_read(base, VCU_PLL_CTRL);
  318. vcu_pll_ctrl |= VCU_PLL_CTRL_BYPASS;
  319. xvcu_write(base, VCU_PLL_CTRL, vcu_pll_ctrl);
  320. vcu_pll_ctrl = xvcu_read(base, VCU_PLL_CTRL);
  321. vcu_pll_ctrl &= ~VCU_PLL_CTRL_POR_IN;
  322. vcu_pll_ctrl &= ~VCU_PLL_CTRL_PWR_POR;
  323. vcu_pll_ctrl &= ~VCU_PLL_CTRL_RESET;
  324. xvcu_write(base, VCU_PLL_CTRL, vcu_pll_ctrl);
  325. ret = xvcu_pll_wait_for_lock(pll);
  326. if (ret) {
  327. pr_err("VCU PLL is not locked\n");
  328. goto err;
  329. }
  330. vcu_pll_ctrl = xvcu_read(base, VCU_PLL_CTRL);
  331. vcu_pll_ctrl &= ~VCU_PLL_CTRL_BYPASS;
  332. xvcu_write(base, VCU_PLL_CTRL, vcu_pll_ctrl);
  333. err:
  334. return ret;
  335. }
  336. static void xvcu_pll_disable(struct clk_hw *hw)
  337. {
  338. struct vcu_pll *pll = to_vcu_pll(hw);
  339. void __iomem *base = pll->reg_base;
  340. u32 vcu_pll_ctrl;
  341. vcu_pll_ctrl = xvcu_read(base, VCU_PLL_CTRL);
  342. vcu_pll_ctrl |= VCU_PLL_CTRL_POR_IN;
  343. vcu_pll_ctrl |= VCU_PLL_CTRL_PWR_POR;
  344. vcu_pll_ctrl |= VCU_PLL_CTRL_RESET;
  345. xvcu_write(base, VCU_PLL_CTRL, vcu_pll_ctrl);
  346. }
  347. static const struct clk_ops vcu_pll_ops = {
  348. .enable = xvcu_pll_enable,
  349. .disable = xvcu_pll_disable,
  350. .round_rate = xvcu_pll_round_rate,
  351. .recalc_rate = xvcu_pll_recalc_rate,
  352. .set_rate = xvcu_pll_set_rate,
  353. };
  354. static struct clk_hw *xvcu_register_pll(struct device *dev,
  355. void __iomem *reg_base,
  356. const char *name, const char *parent,
  357. unsigned long flags)
  358. {
  359. struct vcu_pll *pll;
  360. struct clk_hw *hw;
  361. struct clk_init_data init;
  362. int ret;
  363. init.name = name;
  364. init.parent_names = &parent;
  365. init.ops = &vcu_pll_ops;
  366. init.num_parents = 1;
  367. init.flags = flags;
  368. pll = devm_kmalloc(dev, sizeof(*pll), GFP_KERNEL);
  369. if (!pll)
  370. return ERR_PTR(-ENOMEM);
  371. pll->hw.init = &init;
  372. pll->reg_base = reg_base;
  373. pll->fvco_min = FVCO_MIN;
  374. pll->fvco_max = FVCO_MAX;
  375. hw = &pll->hw;
  376. ret = devm_clk_hw_register(dev, hw);
  377. if (ret)
  378. return ERR_PTR(ret);
  379. clk_hw_set_rate_range(hw, pll->fvco_min, pll->fvco_max);
  380. return hw;
  381. }
  382. static struct clk_hw *xvcu_clk_hw_register_leaf(struct device *dev,
  383. const char *name,
  384. const struct clk_parent_data *parent_data,
  385. u8 num_parents,
  386. void __iomem *reg)
  387. {
  388. u8 mux_flags = CLK_MUX_ROUND_CLOSEST;
  389. u8 divider_flags = CLK_DIVIDER_ONE_BASED | CLK_DIVIDER_ALLOW_ZERO |
  390. CLK_DIVIDER_ROUND_CLOSEST;
  391. struct clk_hw *mux = NULL;
  392. struct clk_hw *divider = NULL;
  393. struct clk_hw *gate = NULL;
  394. char *name_mux;
  395. char *name_div;
  396. int err;
  397. /* Protect register shared by clocks */
  398. spinlock_t *lock;
  399. lock = devm_kzalloc(dev, sizeof(*lock), GFP_KERNEL);
  400. if (!lock)
  401. return ERR_PTR(-ENOMEM);
  402. spin_lock_init(lock);
  403. name_mux = devm_kasprintf(dev, GFP_KERNEL, "%s%s", name, "_mux");
  404. if (!name_mux)
  405. return ERR_PTR(-ENOMEM);
  406. mux = clk_hw_register_mux_parent_data(dev, name_mux,
  407. parent_data, num_parents,
  408. CLK_SET_RATE_PARENT,
  409. reg, 0, 1, mux_flags, lock);
  410. if (IS_ERR(mux))
  411. return mux;
  412. name_div = devm_kasprintf(dev, GFP_KERNEL, "%s%s", name, "_div");
  413. if (!name_div) {
  414. err = -ENOMEM;
  415. goto unregister_mux;
  416. }
  417. divider = clk_hw_register_divider_parent_hw(dev, name_div, mux,
  418. CLK_SET_RATE_PARENT,
  419. reg, 4, 6, divider_flags,
  420. lock);
  421. if (IS_ERR(divider)) {
  422. err = PTR_ERR(divider);
  423. goto unregister_mux;
  424. }
  425. gate = clk_hw_register_gate_parent_hw(dev, name, divider,
  426. CLK_SET_RATE_PARENT, reg, 12, 0,
  427. lock);
  428. if (IS_ERR(gate)) {
  429. err = PTR_ERR(gate);
  430. goto unregister_divider;
  431. }
  432. return gate;
  433. unregister_divider:
  434. clk_hw_unregister_divider(divider);
  435. unregister_mux:
  436. clk_hw_unregister_mux(mux);
  437. return ERR_PTR(err);
  438. }
  439. static void xvcu_clk_hw_unregister_leaf(struct clk_hw *hw)
  440. {
  441. struct clk_hw *gate = hw;
  442. struct clk_hw *divider;
  443. struct clk_hw *mux;
  444. if (!gate)
  445. return;
  446. divider = clk_hw_get_parent(gate);
  447. clk_hw_unregister_gate(gate);
  448. if (!divider)
  449. return;
  450. mux = clk_hw_get_parent(divider);
  451. clk_hw_unregister_mux(mux);
  452. if (!divider)
  453. return;
  454. clk_hw_unregister_divider(divider);
  455. }
  456. static int xvcu_register_clock_provider(struct xvcu_device *xvcu)
  457. {
  458. struct device *dev = xvcu->dev;
  459. struct clk_parent_data parent_data[2] = { 0 };
  460. struct clk_hw_onecell_data *data;
  461. struct clk_hw **hws;
  462. struct clk_hw *hw;
  463. void __iomem *reg_base = xvcu->vcu_slcr_ba;
  464. data = devm_kzalloc(dev, struct_size(data, hws, CLK_XVCU_NUM_CLOCKS), GFP_KERNEL);
  465. if (!data)
  466. return -ENOMEM;
  467. data->num = CLK_XVCU_NUM_CLOCKS;
  468. hws = data->hws;
  469. xvcu->clk_data = data;
  470. hw = xvcu_register_pll(dev, reg_base,
  471. "vcu_pll", __clk_get_name(xvcu->pll_ref),
  472. CLK_SET_RATE_NO_REPARENT | CLK_OPS_PARENT_ENABLE);
  473. if (IS_ERR(hw))
  474. return PTR_ERR(hw);
  475. xvcu->pll = hw;
  476. hw = xvcu_register_pll_post(dev, "vcu_pll_post", xvcu->pll, reg_base);
  477. if (IS_ERR(hw))
  478. return PTR_ERR(hw);
  479. xvcu->pll_post = hw;
  480. parent_data[0].fw_name = "pll_ref";
  481. parent_data[1].hw = xvcu->pll_post;
  482. hws[CLK_XVCU_ENC_CORE] =
  483. xvcu_clk_hw_register_leaf(dev, "venc_core_clk",
  484. parent_data,
  485. ARRAY_SIZE(parent_data),
  486. reg_base + VCU_ENC_CORE_CTRL);
  487. hws[CLK_XVCU_ENC_MCU] =
  488. xvcu_clk_hw_register_leaf(dev, "venc_mcu_clk",
  489. parent_data,
  490. ARRAY_SIZE(parent_data),
  491. reg_base + VCU_ENC_MCU_CTRL);
  492. hws[CLK_XVCU_DEC_CORE] =
  493. xvcu_clk_hw_register_leaf(dev, "vdec_core_clk",
  494. parent_data,
  495. ARRAY_SIZE(parent_data),
  496. reg_base + VCU_DEC_CORE_CTRL);
  497. hws[CLK_XVCU_DEC_MCU] =
  498. xvcu_clk_hw_register_leaf(dev, "vdec_mcu_clk",
  499. parent_data,
  500. ARRAY_SIZE(parent_data),
  501. reg_base + VCU_DEC_MCU_CTRL);
  502. return devm_of_clk_add_hw_provider(dev, of_clk_hw_onecell_get, data);
  503. }
  504. static void xvcu_unregister_clock_provider(struct xvcu_device *xvcu)
  505. {
  506. struct clk_hw_onecell_data *data = xvcu->clk_data;
  507. struct clk_hw **hws = data->hws;
  508. if (!IS_ERR_OR_NULL(hws[CLK_XVCU_DEC_MCU]))
  509. xvcu_clk_hw_unregister_leaf(hws[CLK_XVCU_DEC_MCU]);
  510. if (!IS_ERR_OR_NULL(hws[CLK_XVCU_DEC_CORE]))
  511. xvcu_clk_hw_unregister_leaf(hws[CLK_XVCU_DEC_CORE]);
  512. if (!IS_ERR_OR_NULL(hws[CLK_XVCU_ENC_MCU]))
  513. xvcu_clk_hw_unregister_leaf(hws[CLK_XVCU_ENC_MCU]);
  514. if (!IS_ERR_OR_NULL(hws[CLK_XVCU_ENC_CORE]))
  515. xvcu_clk_hw_unregister_leaf(hws[CLK_XVCU_ENC_CORE]);
  516. clk_hw_unregister_fixed_factor(xvcu->pll_post);
  517. }
  518. /**
  519. * xvcu_probe - Probe existence of the logicoreIP
  520. * and initialize PLL
  521. *
  522. * @pdev: Pointer to the platform_device structure
  523. *
  524. * Return: Returns 0 on success
  525. * Negative error code otherwise
  526. */
  527. static int xvcu_probe(struct platform_device *pdev)
  528. {
  529. struct resource *res;
  530. struct xvcu_device *xvcu;
  531. void __iomem *regs;
  532. int ret;
  533. xvcu = devm_kzalloc(&pdev->dev, sizeof(*xvcu), GFP_KERNEL);
  534. if (!xvcu)
  535. return -ENOMEM;
  536. xvcu->dev = &pdev->dev;
  537. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vcu_slcr");
  538. if (!res) {
  539. dev_err(&pdev->dev, "get vcu_slcr memory resource failed.\n");
  540. return -ENODEV;
  541. }
  542. xvcu->vcu_slcr_ba = devm_ioremap(&pdev->dev, res->start,
  543. resource_size(res));
  544. if (!xvcu->vcu_slcr_ba) {
  545. dev_err(&pdev->dev, "vcu_slcr register mapping failed.\n");
  546. return -ENOMEM;
  547. }
  548. xvcu->logicore_reg_ba =
  549. syscon_regmap_lookup_by_compatible("xlnx,vcu-settings");
  550. if (IS_ERR(xvcu->logicore_reg_ba)) {
  551. dev_info(&pdev->dev,
  552. "could not find xlnx,vcu-settings: trying direct register access\n");
  553. res = platform_get_resource_byname(pdev,
  554. IORESOURCE_MEM, "logicore");
  555. if (!res) {
  556. dev_err(&pdev->dev, "get logicore memory resource failed.\n");
  557. return -ENODEV;
  558. }
  559. regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
  560. if (!regs) {
  561. dev_err(&pdev->dev, "logicore register mapping failed.\n");
  562. return -ENOMEM;
  563. }
  564. xvcu->logicore_reg_ba =
  565. devm_regmap_init_mmio(&pdev->dev, regs,
  566. &vcu_settings_regmap_config);
  567. if (IS_ERR(xvcu->logicore_reg_ba)) {
  568. dev_err(&pdev->dev, "failed to init regmap\n");
  569. return PTR_ERR(xvcu->logicore_reg_ba);
  570. }
  571. }
  572. xvcu->aclk = devm_clk_get(&pdev->dev, "aclk");
  573. if (IS_ERR(xvcu->aclk)) {
  574. dev_err(&pdev->dev, "Could not get aclk clock\n");
  575. return PTR_ERR(xvcu->aclk);
  576. }
  577. xvcu->pll_ref = devm_clk_get(&pdev->dev, "pll_ref");
  578. if (IS_ERR(xvcu->pll_ref)) {
  579. dev_err(&pdev->dev, "Could not get pll_ref clock\n");
  580. return PTR_ERR(xvcu->pll_ref);
  581. }
  582. ret = clk_prepare_enable(xvcu->aclk);
  583. if (ret) {
  584. dev_err(&pdev->dev, "aclk clock enable failed\n");
  585. return ret;
  586. }
  587. /*
  588. * Do the Gasket isolation and put the VCU out of reset
  589. * Bit 0 : Gasket isolation
  590. * Bit 1 : put VCU out of reset
  591. */
  592. regmap_write(xvcu->logicore_reg_ba, VCU_GASKET_INIT, VCU_GASKET_VALUE);
  593. ret = xvcu_register_clock_provider(xvcu);
  594. if (ret) {
  595. dev_err(&pdev->dev, "failed to register clock provider\n");
  596. goto error_clk_provider;
  597. }
  598. dev_set_drvdata(&pdev->dev, xvcu);
  599. return 0;
  600. error_clk_provider:
  601. xvcu_unregister_clock_provider(xvcu);
  602. clk_disable_unprepare(xvcu->aclk);
  603. return ret;
  604. }
  605. /**
  606. * xvcu_remove - Insert gasket isolation
  607. * and disable the clock
  608. * @pdev: Pointer to the platform_device structure
  609. *
  610. * Return: Returns 0 on success
  611. * Negative error code otherwise
  612. */
  613. static void xvcu_remove(struct platform_device *pdev)
  614. {
  615. struct xvcu_device *xvcu;
  616. xvcu = platform_get_drvdata(pdev);
  617. xvcu_unregister_clock_provider(xvcu);
  618. /* Add the Gasket isolation and put the VCU in reset. */
  619. regmap_write(xvcu->logicore_reg_ba, VCU_GASKET_INIT, 0);
  620. clk_disable_unprepare(xvcu->aclk);
  621. }
  622. static const struct of_device_id xvcu_of_id_table[] = {
  623. { .compatible = "xlnx,vcu" },
  624. { .compatible = "xlnx,vcu-logicoreip-1.0" },
  625. { }
  626. };
  627. MODULE_DEVICE_TABLE(of, xvcu_of_id_table);
  628. static struct platform_driver xvcu_driver = {
  629. .driver = {
  630. .name = "xilinx-vcu",
  631. .of_match_table = xvcu_of_id_table,
  632. },
  633. .probe = xvcu_probe,
  634. .remove = xvcu_remove,
  635. };
  636. module_platform_driver(xvcu_driver);
  637. MODULE_AUTHOR("Dhaval Shah <dshah@xilinx.com>");
  638. MODULE_DESCRIPTION("Xilinx VCU init Driver");
  639. MODULE_LICENSE("GPL v2");