blob: 7da9643820a81967ec5caff88dd0a3645b7ec873 [file] [log] [blame]
Dhaval Shahcee81132017-12-21 10:33:06 -08001// 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/clk.h>
10#include <linux/device.h>
11#include <linux/errno.h>
12#include <linux/io.h>
Michael Tretter30b79eb2020-11-09 14:48:17 +010013#include <linux/mfd/syscon.h>
14#include <linux/mfd/syscon/xlnx-vcu.h>
Dhaval Shahcee81132017-12-21 10:33:06 -080015#include <linux/module.h>
16#include <linux/of_platform.h>
17#include <linux/platform_device.h>
Michael Tretter30b79eb2020-11-09 14:48:17 +010018#include <linux/regmap.h>
Dhaval Shahcee81132017-12-21 10:33:06 -080019
20/* vcu slcr registers, bitmask and shift */
21#define VCU_PLL_CTRL 0x24
22#define VCU_PLL_CTRL_RESET_MASK 0x01
23#define VCU_PLL_CTRL_RESET_SHIFT 0
24#define VCU_PLL_CTRL_BYPASS_MASK 0x01
25#define VCU_PLL_CTRL_BYPASS_SHIFT 3
26#define VCU_PLL_CTRL_FBDIV_MASK 0x7f
27#define VCU_PLL_CTRL_FBDIV_SHIFT 8
28#define VCU_PLL_CTRL_POR_IN_MASK 0x01
29#define VCU_PLL_CTRL_POR_IN_SHIFT 1
30#define VCU_PLL_CTRL_PWR_POR_MASK 0x01
31#define VCU_PLL_CTRL_PWR_POR_SHIFT 2
32#define VCU_PLL_CTRL_CLKOUTDIV_MASK 0x03
33#define VCU_PLL_CTRL_CLKOUTDIV_SHIFT 16
34#define VCU_PLL_CTRL_DEFAULT 0
35#define VCU_PLL_DIV2 2
36
37#define VCU_PLL_CFG 0x28
38#define VCU_PLL_CFG_RES_MASK 0x0f
39#define VCU_PLL_CFG_RES_SHIFT 0
40#define VCU_PLL_CFG_CP_MASK 0x0f
41#define VCU_PLL_CFG_CP_SHIFT 5
42#define VCU_PLL_CFG_LFHF_MASK 0x03
43#define VCU_PLL_CFG_LFHF_SHIFT 10
44#define VCU_PLL_CFG_LOCK_CNT_MASK 0x03ff
45#define VCU_PLL_CFG_LOCK_CNT_SHIFT 13
46#define VCU_PLL_CFG_LOCK_DLY_MASK 0x7f
47#define VCU_PLL_CFG_LOCK_DLY_SHIFT 25
48#define VCU_ENC_CORE_CTRL 0x30
49#define VCU_ENC_MCU_CTRL 0x34
50#define VCU_DEC_CORE_CTRL 0x38
51#define VCU_DEC_MCU_CTRL 0x3c
52#define VCU_PLL_DIVISOR_MASK 0x3f
53#define VCU_PLL_DIVISOR_SHIFT 4
54#define VCU_SRCSEL_MASK 0x01
55#define VCU_SRCSEL_SHIFT 0
56#define VCU_SRCSEL_PLL 1
57
58#define VCU_PLL_STATUS 0x60
59#define VCU_PLL_STATUS_LOCK_STATUS_MASK 0x01
60
61#define MHZ 1000000
62#define FVCO_MIN (1500U * MHZ)
63#define FVCO_MAX (3000U * MHZ)
64#define DIVISOR_MIN 0
65#define DIVISOR_MAX 63
66#define FRAC 100
67#define LIMIT (10 * MHZ)
68
69/**
70 * struct xvcu_device - Xilinx VCU init device structure
71 * @dev: Platform device
72 * @pll_ref: pll ref clock source
73 * @aclk: axi clock source
74 * @logicore_reg_ba: logicore reg base address
75 * @vcu_slcr_ba: vcu_slcr Register base address
Dhaval Shahcee81132017-12-21 10:33:06 -080076 */
77struct xvcu_device {
78 struct device *dev;
79 struct clk *pll_ref;
80 struct clk *aclk;
Michael Tretter30b79eb2020-11-09 14:48:17 +010081 struct regmap *logicore_reg_ba;
Dhaval Shahcee81132017-12-21 10:33:06 -080082 void __iomem *vcu_slcr_ba;
Dhaval Shahcee81132017-12-21 10:33:06 -080083};
84
Michael Tretter30b79eb2020-11-09 14:48:17 +010085static struct regmap_config vcu_settings_regmap_config = {
86 .name = "regmap",
87 .reg_bits = 32,
88 .val_bits = 32,
89 .reg_stride = 4,
90 .max_register = 0xfff,
91 .cache_type = REGCACHE_NONE,
92};
93
Dhaval Shahcee81132017-12-21 10:33:06 -080094/**
95 * struct xvcu_pll_cfg - Helper data
96 * @fbdiv: The integer portion of the feedback divider to the PLL
97 * @cp: PLL charge pump control
98 * @res: PLL loop filter resistor control
99 * @lfhf: PLL loop filter high frequency capacitor control
100 * @lock_dly: Lock circuit configuration settings for lock windowsize
101 * @lock_cnt: Lock circuit counter setting
102 */
103struct xvcu_pll_cfg {
104 u32 fbdiv;
105 u32 cp;
106 u32 res;
107 u32 lfhf;
108 u32 lock_dly;
109 u32 lock_cnt;
110};
111
112static const struct xvcu_pll_cfg xvcu_pll_cfg[] = {
113 { 25, 3, 10, 3, 63, 1000 },
114 { 26, 3, 10, 3, 63, 1000 },
115 { 27, 4, 6, 3, 63, 1000 },
116 { 28, 4, 6, 3, 63, 1000 },
117 { 29, 4, 6, 3, 63, 1000 },
118 { 30, 4, 6, 3, 63, 1000 },
119 { 31, 6, 1, 3, 63, 1000 },
120 { 32, 6, 1, 3, 63, 1000 },
121 { 33, 4, 10, 3, 63, 1000 },
122 { 34, 5, 6, 3, 63, 1000 },
123 { 35, 5, 6, 3, 63, 1000 },
124 { 36, 5, 6, 3, 63, 1000 },
125 { 37, 5, 6, 3, 63, 1000 },
126 { 38, 5, 6, 3, 63, 975 },
127 { 39, 3, 12, 3, 63, 950 },
128 { 40, 3, 12, 3, 63, 925 },
129 { 41, 3, 12, 3, 63, 900 },
130 { 42, 3, 12, 3, 63, 875 },
131 { 43, 3, 12, 3, 63, 850 },
132 { 44, 3, 12, 3, 63, 850 },
133 { 45, 3, 12, 3, 63, 825 },
134 { 46, 3, 12, 3, 63, 800 },
135 { 47, 3, 12, 3, 63, 775 },
136 { 48, 3, 12, 3, 63, 775 },
137 { 49, 3, 12, 3, 63, 750 },
138 { 50, 3, 12, 3, 63, 750 },
139 { 51, 3, 2, 3, 63, 725 },
140 { 52, 3, 2, 3, 63, 700 },
141 { 53, 3, 2, 3, 63, 700 },
142 { 54, 3, 2, 3, 63, 675 },
143 { 55, 3, 2, 3, 63, 675 },
144 { 56, 3, 2, 3, 63, 650 },
145 { 57, 3, 2, 3, 63, 650 },
146 { 58, 3, 2, 3, 63, 625 },
147 { 59, 3, 2, 3, 63, 625 },
148 { 60, 3, 2, 3, 63, 625 },
149 { 61, 3, 2, 3, 63, 600 },
150 { 62, 3, 2, 3, 63, 600 },
151 { 63, 3, 2, 3, 63, 600 },
152 { 64, 3, 2, 3, 63, 600 },
153 { 65, 3, 2, 3, 63, 600 },
154 { 66, 3, 2, 3, 63, 600 },
155 { 67, 3, 2, 3, 63, 600 },
156 { 68, 3, 2, 3, 63, 600 },
157 { 69, 3, 2, 3, 63, 600 },
158 { 70, 3, 2, 3, 63, 600 },
159 { 71, 3, 2, 3, 63, 600 },
160 { 72, 3, 2, 3, 63, 600 },
161 { 73, 3, 2, 3, 63, 600 },
162 { 74, 3, 2, 3, 63, 600 },
163 { 75, 3, 2, 3, 63, 600 },
164 { 76, 3, 2, 3, 63, 600 },
165 { 77, 3, 2, 3, 63, 600 },
166 { 78, 3, 2, 3, 63, 600 },
167 { 79, 3, 2, 3, 63, 600 },
168 { 80, 3, 2, 3, 63, 600 },
169 { 81, 3, 2, 3, 63, 600 },
170 { 82, 3, 2, 3, 63, 600 },
171 { 83, 4, 2, 3, 63, 600 },
172 { 84, 4, 2, 3, 63, 600 },
173 { 85, 4, 2, 3, 63, 600 },
174 { 86, 4, 2, 3, 63, 600 },
175 { 87, 4, 2, 3, 63, 600 },
176 { 88, 4, 2, 3, 63, 600 },
177 { 89, 4, 2, 3, 63, 600 },
178 { 90, 4, 2, 3, 63, 600 },
179 { 91, 4, 2, 3, 63, 600 },
180 { 92, 4, 2, 3, 63, 600 },
181 { 93, 4, 2, 3, 63, 600 },
182 { 94, 4, 2, 3, 63, 600 },
183 { 95, 4, 2, 3, 63, 600 },
184 { 96, 4, 2, 3, 63, 600 },
185 { 97, 4, 2, 3, 63, 600 },
186 { 98, 4, 2, 3, 63, 600 },
187 { 99, 4, 2, 3, 63, 600 },
188 { 100, 4, 2, 3, 63, 600 },
189 { 101, 4, 2, 3, 63, 600 },
190 { 102, 4, 2, 3, 63, 600 },
191 { 103, 5, 2, 3, 63, 600 },
192 { 104, 5, 2, 3, 63, 600 },
193 { 105, 5, 2, 3, 63, 600 },
194 { 106, 5, 2, 3, 63, 600 },
195 { 107, 3, 4, 3, 63, 600 },
196 { 108, 3, 4, 3, 63, 600 },
197 { 109, 3, 4, 3, 63, 600 },
198 { 110, 3, 4, 3, 63, 600 },
199 { 111, 3, 4, 3, 63, 600 },
200 { 112, 3, 4, 3, 63, 600 },
201 { 113, 3, 4, 3, 63, 600 },
202 { 114, 3, 4, 3, 63, 600 },
203 { 115, 3, 4, 3, 63, 600 },
204 { 116, 3, 4, 3, 63, 600 },
205 { 117, 3, 4, 3, 63, 600 },
206 { 118, 3, 4, 3, 63, 600 },
207 { 119, 3, 4, 3, 63, 600 },
208 { 120, 3, 4, 3, 63, 600 },
209 { 121, 3, 4, 3, 63, 600 },
210 { 122, 3, 4, 3, 63, 600 },
211 { 123, 3, 4, 3, 63, 600 },
212 { 124, 3, 4, 3, 63, 600 },
213 { 125, 3, 4, 3, 63, 600 },
214};
215
216/**
217 * xvcu_read - Read from the VCU register space
218 * @iomem: vcu reg space base address
219 * @offset: vcu reg offset from base
220 *
221 * Return: Returns 32bit value from VCU register specified
222 *
223 */
224static inline u32 xvcu_read(void __iomem *iomem, u32 offset)
225{
226 return ioread32(iomem + offset);
227}
228
229/**
230 * xvcu_write - Write to the VCU register space
231 * @iomem: vcu reg space base address
232 * @offset: vcu reg offset from base
233 * @value: Value to write
234 */
235static inline void xvcu_write(void __iomem *iomem, u32 offset, u32 value)
236{
237 iowrite32(value, iomem + offset);
238}
239
240/**
241 * xvcu_write_field_reg - Write to the vcu reg field
242 * @iomem: vcu reg space base address
243 * @offset: vcu reg offset from base
244 * @field: vcu reg field to write to
245 * @mask: vcu reg mask
246 * @shift: vcu reg number of bits to shift the bitfield
247 */
248static void xvcu_write_field_reg(void __iomem *iomem, int offset,
249 u32 field, u32 mask, int shift)
250{
251 u32 val = xvcu_read(iomem, offset);
252
253 val &= ~(mask << shift);
254 val |= (field & mask) << shift;
255
256 xvcu_write(iomem, offset, val);
257}
258
259/**
260 * xvcu_set_vcu_pll_info - Set the VCU PLL info
261 * @xvcu: Pointer to the xvcu_device structure
262 *
263 * Programming the VCU PLL based on the user configuration
264 * (ref clock freq, core clock freq, mcu clock freq).
265 * Core clock frequency has higher priority than mcu clock frequency
266 * Errors in following cases
267 * - When mcu or clock clock get from logicoreIP is 0
268 * - When VCU PLL DIV related bits value other than 1
269 * - When proper data not found for given data
270 * - When sis570_1 clocksource related operation failed
271 *
272 * Return: Returns status, either success or error+reason
273 */
274static int xvcu_set_vcu_pll_info(struct xvcu_device *xvcu)
275{
276 u32 refclk, coreclk, mcuclk, inte, deci;
277 u32 divisor_mcu, divisor_core, fvco;
278 u32 clkoutdiv, vcu_pll_ctrl, pll_clk;
279 u32 cfg_val, mod, ctrl;
280 int ret, i;
281 const struct xvcu_pll_cfg *found = NULL;
282
Michael Tretter30b79eb2020-11-09 14:48:17 +0100283 regmap_read(xvcu->logicore_reg_ba, VCU_PLL_CLK, &inte);
284 regmap_read(xvcu->logicore_reg_ba, VCU_PLL_CLK_DEC, &deci);
285 regmap_read(xvcu->logicore_reg_ba, VCU_CORE_CLK, &coreclk);
286 coreclk *= MHZ;
287 regmap_read(xvcu->logicore_reg_ba, VCU_MCU_CLK, &mcuclk);
288 mcuclk *= MHZ;
Dhaval Shahcee81132017-12-21 10:33:06 -0800289 if (!mcuclk || !coreclk) {
290 dev_err(xvcu->dev, "Invalid mcu and core clock data\n");
291 return -EINVAL;
292 }
293
294 refclk = (inte * MHZ) + (deci * (MHZ / FRAC));
295 dev_dbg(xvcu->dev, "Ref clock from logicoreIP is %uHz\n", refclk);
296 dev_dbg(xvcu->dev, "Core clock from logicoreIP is %uHz\n", coreclk);
297 dev_dbg(xvcu->dev, "Mcu clock from logicoreIP is %uHz\n", mcuclk);
298
299 clk_disable_unprepare(xvcu->pll_ref);
300 ret = clk_set_rate(xvcu->pll_ref, refclk);
301 if (ret)
302 dev_warn(xvcu->dev, "failed to set logicoreIP refclk rate\n");
303
304 ret = clk_prepare_enable(xvcu->pll_ref);
305 if (ret) {
306 dev_err(xvcu->dev, "failed to enable pll_ref clock source\n");
307 return ret;
308 }
309
310 refclk = clk_get_rate(xvcu->pll_ref);
311
312 /*
313 * The divide-by-2 should be always enabled (==1)
314 * to meet the timing in the design.
315 * Otherwise, it's an error
316 */
317 vcu_pll_ctrl = xvcu_read(xvcu->vcu_slcr_ba, VCU_PLL_CTRL);
318 clkoutdiv = vcu_pll_ctrl >> VCU_PLL_CTRL_CLKOUTDIV_SHIFT;
Gustavo A. R. Silva2a7157b2018-01-15 13:15:28 -0600319 clkoutdiv = clkoutdiv & VCU_PLL_CTRL_CLKOUTDIV_MASK;
Dhaval Shahcee81132017-12-21 10:33:06 -0800320 if (clkoutdiv != 1) {
321 dev_err(xvcu->dev, "clkoutdiv value is invalid\n");
322 return -EINVAL;
323 }
324
325 for (i = ARRAY_SIZE(xvcu_pll_cfg) - 1; i >= 0; i--) {
326 const struct xvcu_pll_cfg *cfg = &xvcu_pll_cfg[i];
327
328 fvco = cfg->fbdiv * refclk;
329 if (fvco >= FVCO_MIN && fvco <= FVCO_MAX) {
330 pll_clk = fvco / VCU_PLL_DIV2;
331 if (fvco % VCU_PLL_DIV2 != 0)
332 pll_clk++;
333 mod = pll_clk % coreclk;
334 if (mod < LIMIT) {
335 divisor_core = pll_clk / coreclk;
336 } else if (coreclk - mod < LIMIT) {
337 divisor_core = pll_clk / coreclk;
338 divisor_core++;
339 } else {
340 continue;
341 }
342 if (divisor_core >= DIVISOR_MIN &&
343 divisor_core <= DIVISOR_MAX) {
344 found = cfg;
345 divisor_mcu = pll_clk / mcuclk;
346 mod = pll_clk % mcuclk;
347 if (mcuclk - mod < LIMIT)
348 divisor_mcu++;
349 break;
350 }
351 }
352 }
353
354 if (!found) {
355 dev_err(xvcu->dev, "Invalid clock combination.\n");
356 return -EINVAL;
357 }
358
Michael Tretterd387dfc2021-01-21 08:16:47 +0100359 coreclk = pll_clk / divisor_core;
Dhaval Shahcee81132017-12-21 10:33:06 -0800360 mcuclk = pll_clk / divisor_mcu;
361 dev_dbg(xvcu->dev, "Actual Ref clock freq is %uHz\n", refclk);
Michael Tretterd387dfc2021-01-21 08:16:47 +0100362 dev_dbg(xvcu->dev, "Actual Core clock freq is %uHz\n", coreclk);
Dhaval Shahcee81132017-12-21 10:33:06 -0800363 dev_dbg(xvcu->dev, "Actual Mcu clock freq is %uHz\n", mcuclk);
364
365 vcu_pll_ctrl &= ~(VCU_PLL_CTRL_FBDIV_MASK << VCU_PLL_CTRL_FBDIV_SHIFT);
366 vcu_pll_ctrl |= (found->fbdiv & VCU_PLL_CTRL_FBDIV_MASK) <<
367 VCU_PLL_CTRL_FBDIV_SHIFT;
368 vcu_pll_ctrl &= ~(VCU_PLL_CTRL_POR_IN_MASK <<
369 VCU_PLL_CTRL_POR_IN_SHIFT);
370 vcu_pll_ctrl |= (VCU_PLL_CTRL_DEFAULT & VCU_PLL_CTRL_POR_IN_MASK) <<
371 VCU_PLL_CTRL_POR_IN_SHIFT;
372 vcu_pll_ctrl &= ~(VCU_PLL_CTRL_PWR_POR_MASK <<
373 VCU_PLL_CTRL_PWR_POR_SHIFT);
374 vcu_pll_ctrl |= (VCU_PLL_CTRL_DEFAULT & VCU_PLL_CTRL_PWR_POR_MASK) <<
375 VCU_PLL_CTRL_PWR_POR_SHIFT;
376 xvcu_write(xvcu->vcu_slcr_ba, VCU_PLL_CTRL, vcu_pll_ctrl);
377
378 /* Set divisor for the core and mcu clock */
379 ctrl = xvcu_read(xvcu->vcu_slcr_ba, VCU_ENC_CORE_CTRL);
380 ctrl &= ~(VCU_PLL_DIVISOR_MASK << VCU_PLL_DIVISOR_SHIFT);
381 ctrl |= (divisor_core & VCU_PLL_DIVISOR_MASK) <<
382 VCU_PLL_DIVISOR_SHIFT;
383 ctrl &= ~(VCU_SRCSEL_MASK << VCU_SRCSEL_SHIFT);
384 ctrl |= (VCU_SRCSEL_PLL & VCU_SRCSEL_MASK) << VCU_SRCSEL_SHIFT;
385 xvcu_write(xvcu->vcu_slcr_ba, VCU_ENC_CORE_CTRL, ctrl);
386
387 ctrl = xvcu_read(xvcu->vcu_slcr_ba, VCU_DEC_CORE_CTRL);
388 ctrl &= ~(VCU_PLL_DIVISOR_MASK << VCU_PLL_DIVISOR_SHIFT);
389 ctrl |= (divisor_core & VCU_PLL_DIVISOR_MASK) <<
390 VCU_PLL_DIVISOR_SHIFT;
391 ctrl &= ~(VCU_SRCSEL_MASK << VCU_SRCSEL_SHIFT);
392 ctrl |= (VCU_SRCSEL_PLL & VCU_SRCSEL_MASK) << VCU_SRCSEL_SHIFT;
393 xvcu_write(xvcu->vcu_slcr_ba, VCU_DEC_CORE_CTRL, ctrl);
394
395 ctrl = xvcu_read(xvcu->vcu_slcr_ba, VCU_ENC_MCU_CTRL);
396 ctrl &= ~(VCU_PLL_DIVISOR_MASK << VCU_PLL_DIVISOR_SHIFT);
397 ctrl |= (divisor_mcu & VCU_PLL_DIVISOR_MASK) << VCU_PLL_DIVISOR_SHIFT;
398 ctrl &= ~(VCU_SRCSEL_MASK << VCU_SRCSEL_SHIFT);
399 ctrl |= (VCU_SRCSEL_PLL & VCU_SRCSEL_MASK) << VCU_SRCSEL_SHIFT;
400 xvcu_write(xvcu->vcu_slcr_ba, VCU_ENC_MCU_CTRL, ctrl);
401
402 ctrl = xvcu_read(xvcu->vcu_slcr_ba, VCU_DEC_MCU_CTRL);
403 ctrl &= ~(VCU_PLL_DIVISOR_MASK << VCU_PLL_DIVISOR_SHIFT);
404 ctrl |= (divisor_mcu & VCU_PLL_DIVISOR_MASK) << VCU_PLL_DIVISOR_SHIFT;
405 ctrl &= ~(VCU_SRCSEL_MASK << VCU_SRCSEL_SHIFT);
406 ctrl |= (VCU_SRCSEL_PLL & VCU_SRCSEL_MASK) << VCU_SRCSEL_SHIFT;
407 xvcu_write(xvcu->vcu_slcr_ba, VCU_DEC_MCU_CTRL, ctrl);
408
409 /* Set RES, CP, LFHF, LOCK_CNT and LOCK_DLY cfg values */
410 cfg_val = (found->res << VCU_PLL_CFG_RES_SHIFT) |
411 (found->cp << VCU_PLL_CFG_CP_SHIFT) |
412 (found->lfhf << VCU_PLL_CFG_LFHF_SHIFT) |
413 (found->lock_cnt << VCU_PLL_CFG_LOCK_CNT_SHIFT) |
414 (found->lock_dly << VCU_PLL_CFG_LOCK_DLY_SHIFT);
415 xvcu_write(xvcu->vcu_slcr_ba, VCU_PLL_CFG, cfg_val);
416
417 return 0;
418}
419
420/**
421 * xvcu_set_pll - PLL init sequence
422 * @xvcu: Pointer to the xvcu_device structure
423 *
424 * Call the api to set the PLL info and once that is done then
425 * init the PLL sequence to make the PLL stable.
426 *
427 * Return: Returns status, either success or error+reason
428 */
429static int xvcu_set_pll(struct xvcu_device *xvcu)
430{
431 u32 lock_status;
432 unsigned long timeout;
433 int ret;
434
435 ret = xvcu_set_vcu_pll_info(xvcu);
436 if (ret) {
437 dev_err(xvcu->dev, "failed to set pll info\n");
438 return ret;
439 }
440
441 xvcu_write_field_reg(xvcu->vcu_slcr_ba, VCU_PLL_CTRL,
442 1, VCU_PLL_CTRL_BYPASS_MASK,
443 VCU_PLL_CTRL_BYPASS_SHIFT);
444 xvcu_write_field_reg(xvcu->vcu_slcr_ba, VCU_PLL_CTRL,
445 1, VCU_PLL_CTRL_RESET_MASK,
446 VCU_PLL_CTRL_RESET_SHIFT);
447 xvcu_write_field_reg(xvcu->vcu_slcr_ba, VCU_PLL_CTRL,
448 0, VCU_PLL_CTRL_RESET_MASK,
449 VCU_PLL_CTRL_RESET_SHIFT);
450 /*
451 * Defined the timeout for the max time to wait the
452 * PLL_STATUS to be locked.
453 */
454 timeout = jiffies + msecs_to_jiffies(2000);
455 do {
456 lock_status = xvcu_read(xvcu->vcu_slcr_ba, VCU_PLL_STATUS);
457 if (lock_status & VCU_PLL_STATUS_LOCK_STATUS_MASK) {
458 xvcu_write_field_reg(xvcu->vcu_slcr_ba, VCU_PLL_CTRL,
459 0, VCU_PLL_CTRL_BYPASS_MASK,
460 VCU_PLL_CTRL_BYPASS_SHIFT);
461 return 0;
462 }
463 } while (!time_after(jiffies, timeout));
464
465 /* PLL is not locked even after the timeout of the 2sec */
466 dev_err(xvcu->dev, "PLL is not locked\n");
467 return -ETIMEDOUT;
468}
469
470/**
471 * xvcu_probe - Probe existence of the logicoreIP
472 * and initialize PLL
473 *
474 * @pdev: Pointer to the platform_device structure
475 *
476 * Return: Returns 0 on success
477 * Negative error code otherwise
478 */
479static int xvcu_probe(struct platform_device *pdev)
480{
481 struct resource *res;
482 struct xvcu_device *xvcu;
Michael Tretter30b79eb2020-11-09 14:48:17 +0100483 void __iomem *regs;
Dhaval Shahcee81132017-12-21 10:33:06 -0800484 int ret;
485
486 xvcu = devm_kzalloc(&pdev->dev, sizeof(*xvcu), GFP_KERNEL);
487 if (!xvcu)
488 return -ENOMEM;
489
490 xvcu->dev = &pdev->dev;
491 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "vcu_slcr");
492 if (!res) {
493 dev_err(&pdev->dev, "get vcu_slcr memory resource failed.\n");
494 return -ENODEV;
495 }
496
Christoph Hellwig4bdc0d62020-01-06 09:43:50 +0100497 xvcu->vcu_slcr_ba = devm_ioremap(&pdev->dev, res->start,
Dhaval Shahcee81132017-12-21 10:33:06 -0800498 resource_size(res));
499 if (!xvcu->vcu_slcr_ba) {
500 dev_err(&pdev->dev, "vcu_slcr register mapping failed.\n");
501 return -ENOMEM;
502 }
503
Michael Tretter30b79eb2020-11-09 14:48:17 +0100504 xvcu->logicore_reg_ba =
505 syscon_regmap_lookup_by_compatible("xlnx,vcu-settings");
506 if (IS_ERR(xvcu->logicore_reg_ba)) {
507 dev_info(&pdev->dev,
508 "could not find xlnx,vcu-settings: trying direct register access\n");
Dhaval Shahcee81132017-12-21 10:33:06 -0800509
Michael Tretter30b79eb2020-11-09 14:48:17 +0100510 res = platform_get_resource_byname(pdev,
511 IORESOURCE_MEM, "logicore");
512 if (!res) {
513 dev_err(&pdev->dev, "get logicore memory resource failed.\n");
514 return -ENODEV;
515 }
516
517 regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
518 if (!regs) {
519 dev_err(&pdev->dev, "logicore register mapping failed.\n");
520 return -ENOMEM;
521 }
522
523 xvcu->logicore_reg_ba =
524 devm_regmap_init_mmio(&pdev->dev, regs,
525 &vcu_settings_regmap_config);
526 if (IS_ERR(xvcu->logicore_reg_ba)) {
527 dev_err(&pdev->dev, "failed to init regmap\n");
528 return PTR_ERR(xvcu->logicore_reg_ba);
529 }
Dhaval Shahcee81132017-12-21 10:33:06 -0800530 }
531
532 xvcu->aclk = devm_clk_get(&pdev->dev, "aclk");
533 if (IS_ERR(xvcu->aclk)) {
534 dev_err(&pdev->dev, "Could not get aclk clock\n");
535 return PTR_ERR(xvcu->aclk);
536 }
537
538 xvcu->pll_ref = devm_clk_get(&pdev->dev, "pll_ref");
539 if (IS_ERR(xvcu->pll_ref)) {
540 dev_err(&pdev->dev, "Could not get pll_ref clock\n");
541 return PTR_ERR(xvcu->pll_ref);
542 }
543
544 ret = clk_prepare_enable(xvcu->aclk);
545 if (ret) {
546 dev_err(&pdev->dev, "aclk clock enable failed\n");
547 return ret;
548 }
549
550 ret = clk_prepare_enable(xvcu->pll_ref);
551 if (ret) {
552 dev_err(&pdev->dev, "pll_ref clock enable failed\n");
553 goto error_aclk;
554 }
555
556 /*
557 * Do the Gasket isolation and put the VCU out of reset
558 * Bit 0 : Gasket isolation
559 * Bit 1 : put VCU out of reset
560 */
Michael Tretter30b79eb2020-11-09 14:48:17 +0100561 regmap_write(xvcu->logicore_reg_ba, VCU_GASKET_INIT, VCU_GASKET_VALUE);
Dhaval Shahcee81132017-12-21 10:33:06 -0800562
563 /* Do the PLL Settings based on the ref clk,core and mcu clk freq */
564 ret = xvcu_set_pll(xvcu);
565 if (ret) {
566 dev_err(&pdev->dev, "Failed to set the pll\n");
567 goto error_pll_ref;
568 }
569
570 dev_set_drvdata(&pdev->dev, xvcu);
571
Dhaval Shahcee81132017-12-21 10:33:06 -0800572 return 0;
573
574error_pll_ref:
575 clk_disable_unprepare(xvcu->pll_ref);
576error_aclk:
577 clk_disable_unprepare(xvcu->aclk);
578 return ret;
579}
580
581/**
582 * xvcu_remove - Insert gasket isolation
583 * and disable the clock
584 * @pdev: Pointer to the platform_device structure
585 *
586 * Return: Returns 0 on success
587 * Negative error code otherwise
588 */
589static int xvcu_remove(struct platform_device *pdev)
590{
591 struct xvcu_device *xvcu;
592
593 xvcu = platform_get_drvdata(pdev);
594 if (!xvcu)
595 return -ENODEV;
596
597 /* Add the the Gasket isolation and put the VCU in reset. */
Michael Tretter30b79eb2020-11-09 14:48:17 +0100598 regmap_write(xvcu->logicore_reg_ba, VCU_GASKET_INIT, 0);
Dhaval Shahcee81132017-12-21 10:33:06 -0800599
600 clk_disable_unprepare(xvcu->pll_ref);
601 clk_disable_unprepare(xvcu->aclk);
602
603 return 0;
604}
605
606static const struct of_device_id xvcu_of_id_table[] = {
607 { .compatible = "xlnx,vcu" },
608 { .compatible = "xlnx,vcu-logicoreip-1.0" },
609 { }
610};
611MODULE_DEVICE_TABLE(of, xvcu_of_id_table);
612
613static struct platform_driver xvcu_driver = {
614 .driver = {
615 .name = "xilinx-vcu",
616 .of_match_table = xvcu_of_id_table,
617 },
618 .probe = xvcu_probe,
619 .remove = xvcu_remove,
620};
621
622module_platform_driver(xvcu_driver);
623
624MODULE_AUTHOR("Dhaval Shah <dshah@xilinx.com>");
625MODULE_DESCRIPTION("Xilinx VCU init Driver");
626MODULE_LICENSE("GPL v2");