blob: 4637b9fdf15562b8263a0e3e908be75c67bca0db [file] [log] [blame]
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +03001/*
2 * Synopsys HSDK SDP CGU clock driver
3 *
4 * Copyright (C) 2017 Synopsys
5 * Author: Eugeniy Paltsev <Eugeniy.Paltsev@synopsys.com>
6 *
7 * This file is licensed under the terms of the GNU General Public
8 * License version 2. This program is licensed "as is" without any
9 * warranty of any kind, whether express or implied.
10 */
11
12#include <common.h>
13#include <clk-uclass.h>
14#include <div64.h>
15#include <dm.h>
16#include <linux/io.h>
17
18/*
19 * Synopsys ARC HSDK clock tree.
20 *
21 * ------------------
22 * | 33.33 MHz xtal |
23 * ------------------
24 * |
25 * | -----------
26 * |-->| ARC PLL |
27 * | -----------
28 * | |
29 * | |-->|CGU_ARC_IDIV|----------->
30 * | |-->|CREG_CORE_IF_DIV|------->
31 * |
32 * | --------------
33 * |-->| SYSTEM PLL |
34 * | --------------
35 * | |
36 * | |-->|CGU_SYS_IDIV_APB|------->
37 * | |-->|CGU_SYS_IDIV_AXI|------->
38 * | |-->|CGU_SYS_IDIV_*|--------->
39 * | |-->|CGU_SYS_IDIV_EBI_REF|--->
40 * |
41 * | --------------
42 * |-->| TUNNEL PLL |
43 * | --------------
44 * | |
Eugeniy Paltsev74514242018-01-16 20:44:25 +030045 * | |-->|CGU_TUN_IDIV_TUN|----------->
46 * | |-->|CGU_TUN_IDIV_ROM|----------->
47 * | |-->|CGU_TUN_IDIV_PWM|----------->
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +030048 * |
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +030049 * | -----------
50 * |-->| DDR PLL |
51 * -----------
52 * |
53 * |---------------------------->
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +030054 *
55 * ------------------
56 * | 27.00 MHz xtal |
57 * ------------------
58 * |
59 * | ------------
60 * |-->| HDMI PLL |
61 * ------------
62 * |
63 * |-->|CGU_HDMI_IDIV_APB|------>
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +030064 */
65
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +030066#define CGU_ARC_IDIV 0x080
Eugeniy Paltsev74514242018-01-16 20:44:25 +030067#define CGU_TUN_IDIV_TUN 0x380
68#define CGU_TUN_IDIV_ROM 0x390
69#define CGU_TUN_IDIV_PWM 0x3A0
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +030070#define CGU_HDMI_IDIV_APB 0x480
71#define CGU_SYS_IDIV_APB 0x180
72#define CGU_SYS_IDIV_AXI 0x190
73#define CGU_SYS_IDIV_ETH 0x1A0
74#define CGU_SYS_IDIV_USB 0x1B0
75#define CGU_SYS_IDIV_SDIO 0x1C0
76#define CGU_SYS_IDIV_HDMI 0x1D0
77#define CGU_SYS_IDIV_GFX_CORE 0x1E0
78#define CGU_SYS_IDIV_GFX_DMA 0x1F0
79#define CGU_SYS_IDIV_GFX_CFG 0x200
80#define CGU_SYS_IDIV_DMAC_CORE 0x210
81#define CGU_SYS_IDIV_DMAC_CFG 0x220
82#define CGU_SYS_IDIV_SDIO_REF 0x230
83#define CGU_SYS_IDIV_SPI_REF 0x240
84#define CGU_SYS_IDIV_I2C_REF 0x250
85#define CGU_SYS_IDIV_UART_REF 0x260
86#define CGU_SYS_IDIV_EBI_REF 0x270
87
88#define CGU_IDIV_MASK 0xFF /* All idiv have 8 significant bits */
89
90#define CGU_ARC_PLL 0x0
91#define CGU_SYS_PLL 0x10
92#define CGU_DDR_PLL 0x20
93#define CGU_TUN_PLL 0x30
94#define CGU_HDMI_PLL 0x40
95
96#define CGU_PLL_CTRL 0x000 /* ARC PLL control register */
97#define CGU_PLL_STATUS 0x004 /* ARC PLL status register */
98#define CGU_PLL_FMEAS 0x008 /* ARC PLL frequency measurement register */
99#define CGU_PLL_MON 0x00C /* ARC PLL monitor register */
100
101#define CGU_PLL_CTRL_ODIV_SHIFT 2
102#define CGU_PLL_CTRL_IDIV_SHIFT 4
103#define CGU_PLL_CTRL_FBDIV_SHIFT 9
104#define CGU_PLL_CTRL_BAND_SHIFT 20
105
106#define CGU_PLL_CTRL_ODIV_MASK GENMASK(3, CGU_PLL_CTRL_ODIV_SHIFT)
107#define CGU_PLL_CTRL_IDIV_MASK GENMASK(8, CGU_PLL_CTRL_IDIV_SHIFT)
108#define CGU_PLL_CTRL_FBDIV_MASK GENMASK(15, CGU_PLL_CTRL_FBDIV_SHIFT)
109
110#define CGU_PLL_CTRL_PD BIT(0)
111#define CGU_PLL_CTRL_BYPASS BIT(1)
112
113#define CGU_PLL_STATUS_LOCK BIT(0)
114#define CGU_PLL_STATUS_ERR BIT(1)
115
116#define HSDK_PLL_MAX_LOCK_TIME 100 /* 100 us */
117
118#define CREG_CORE_IF_DIV 0x000 /* ARC CORE interface divider */
119#define CORE_IF_CLK_THRESHOLD_HZ 500000000
120#define CREG_CORE_IF_CLK_DIV_1 0x0
121#define CREG_CORE_IF_CLK_DIV_2 0x1
122
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300123#define MIN_PLL_RATE 100000000 /* 100 MHz */
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300124#define PARENT_RATE_33 33333333 /* fixed clock - xtal */
125#define PARENT_RATE_27 27000000 /* fixed clock - xtal */
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300126#define CGU_MAX_CLOCKS 26
127
128#define CGU_SYS_CLOCKS 16
129#define MAX_AXI_CLOCKS 4
130
131#define CGU_TUN_CLOCKS 3
132#define MAX_TUN_CLOCKS 6
133
134struct hsdk_tun_idiv_cfg {
135 u32 oft;
136 u8 val[MAX_TUN_CLOCKS];
137};
138
139struct hsdk_tun_clk_cfg {
140 const u32 clk_rate[MAX_TUN_CLOCKS];
141 const u32 pll_rate[MAX_TUN_CLOCKS];
142 const struct hsdk_tun_idiv_cfg idiv[CGU_TUN_CLOCKS];
143};
144
145static const struct hsdk_tun_clk_cfg tun_clk_cfg = {
146 { 25000000, 50000000, 75000000, 100000000, 125000000, 150000000 },
147 { 600000000, 600000000, 600000000, 600000000, 700000000, 600000000 }, {
148 { CGU_TUN_IDIV_TUN, { 24, 12, 8, 6, 6, 4 } },
149 { CGU_TUN_IDIV_ROM, { 4, 4, 4, 4, 5, 4 } },
150 { CGU_TUN_IDIV_PWM, { 8, 8, 8, 8, 10, 8 } }
151 }
152};
153
154struct hsdk_sys_idiv_cfg {
155 u32 oft;
156 u8 val[MAX_AXI_CLOCKS];
157};
158
159struct hsdk_axi_clk_cfg {
160 const u32 clk_rate[MAX_AXI_CLOCKS];
161 const u32 pll_rate[MAX_AXI_CLOCKS];
162 const struct hsdk_sys_idiv_cfg idiv[CGU_SYS_CLOCKS];
163};
164
165static const struct hsdk_axi_clk_cfg axi_clk_cfg = {
166 { 200000000, 400000000, 600000000, 800000000 },
167 { 800000000, 800000000, 600000000, 800000000 }, {
168 { CGU_SYS_IDIV_APB, { 4, 4, 3, 4 } }, /* APB */
169 { CGU_SYS_IDIV_AXI, { 4, 2, 1, 1 } }, /* AXI */
170 { CGU_SYS_IDIV_ETH, { 2, 2, 2, 2 } }, /* ETH */
171 { CGU_SYS_IDIV_USB, { 2, 2, 2, 2 } }, /* USB */
172 { CGU_SYS_IDIV_SDIO, { 2, 2, 2, 2 } }, /* SDIO */
173 { CGU_SYS_IDIV_HDMI, { 2, 2, 2, 2 } }, /* HDMI */
174 { CGU_SYS_IDIV_GFX_CORE, { 1, 1, 1, 1 } }, /* GPU-CORE */
175 { CGU_SYS_IDIV_GFX_DMA, { 2, 2, 2, 2 } }, /* GPU-DMA */
176 { CGU_SYS_IDIV_GFX_CFG, { 4, 4, 3, 4 } }, /* GPU-CFG */
177 { CGU_SYS_IDIV_DMAC_CORE,{ 2, 2, 2, 2 } }, /* DMAC-CORE */
178 { CGU_SYS_IDIV_DMAC_CFG, { 4, 4, 3, 4 } }, /* DMAC-CFG */
179 { CGU_SYS_IDIV_SDIO_REF, { 8, 8, 6, 8 } }, /* SDIO-REF */
180 { CGU_SYS_IDIV_SPI_REF, { 24, 24, 18, 24 } }, /* SPI-REF */
181 { CGU_SYS_IDIV_I2C_REF, { 4, 4, 3, 4 } }, /* I2C-REF */
182 { CGU_SYS_IDIV_UART_REF, { 24, 24, 18, 24 } }, /* UART-REF */
183 { CGU_SYS_IDIV_EBI_REF, { 16, 16, 12, 16 } } /* EBI-REF */
184 }
185};
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300186
187struct hsdk_pll_cfg {
188 u32 rate;
189 u32 idiv;
190 u32 fbdiv;
191 u32 odiv;
192 u32 band;
193};
194
195static const struct hsdk_pll_cfg asdt_pll_cfg[] = {
196 { 100000000, 0, 11, 3, 0 },
197 { 125000000, 0, 14, 3, 0 },
198 { 133000000, 0, 15, 3, 0 },
199 { 150000000, 0, 17, 3, 0 },
200 { 200000000, 1, 47, 3, 0 },
201 { 233000000, 1, 27, 2, 0 },
202 { 300000000, 1, 35, 2, 0 },
203 { 333000000, 1, 39, 2, 0 },
204 { 400000000, 1, 47, 2, 0 },
205 { 500000000, 0, 14, 1, 0 },
206 { 600000000, 0, 17, 1, 0 },
207 { 700000000, 0, 20, 1, 0 },
208 { 800000000, 0, 23, 1, 0 },
209 { 900000000, 1, 26, 0, 0 },
210 { 1000000000, 1, 29, 0, 0 },
211 { 1100000000, 1, 32, 0, 0 },
212 { 1200000000, 1, 35, 0, 0 },
213 { 1300000000, 1, 38, 0, 0 },
214 { 1400000000, 1, 41, 0, 0 },
215 { 1500000000, 1, 44, 0, 0 },
216 { 1600000000, 1, 47, 0, 0 },
217 {}
218};
219
220static const struct hsdk_pll_cfg hdmi_pll_cfg[] = {
221 { 297000000, 0, 21, 2, 0 },
222 { 540000000, 0, 19, 1, 0 },
223 { 594000000, 0, 21, 1, 0 },
224 {}
225};
226
227struct hsdk_cgu_clk {
228 /* CGU block register */
229 void __iomem *cgu_regs;
230 /* CREG block register */
231 void __iomem *creg_regs;
232
233 /* PLLs registers */
234 void __iomem *regs;
235 /* PLLs special registers */
236 void __iomem *spec_regs;
237 /* PLLs devdata */
238 const struct hsdk_pll_devdata *pll_devdata;
239
240 /* Dividers registers */
241 void __iomem *idiv_regs;
242};
243
244struct hsdk_pll_devdata {
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300245 const u32 parent_rate;
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300246 const struct hsdk_pll_cfg *pll_cfg;
247 int (*update_rate)(struct hsdk_cgu_clk *clk, unsigned long rate,
248 const struct hsdk_pll_cfg *cfg);
249};
250
251static int hsdk_pll_core_update_rate(struct hsdk_cgu_clk *, unsigned long,
252 const struct hsdk_pll_cfg *);
253static int hsdk_pll_comm_update_rate(struct hsdk_cgu_clk *, unsigned long,
254 const struct hsdk_pll_cfg *);
255
256static const struct hsdk_pll_devdata core_pll_dat = {
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300257 .parent_rate = PARENT_RATE_33,
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300258 .pll_cfg = asdt_pll_cfg,
259 .update_rate = hsdk_pll_core_update_rate,
260};
261
262static const struct hsdk_pll_devdata sdt_pll_dat = {
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300263 .parent_rate = PARENT_RATE_33,
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300264 .pll_cfg = asdt_pll_cfg,
265 .update_rate = hsdk_pll_comm_update_rate,
266};
267
268static const struct hsdk_pll_devdata hdmi_pll_dat = {
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300269 .parent_rate = PARENT_RATE_27,
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300270 .pll_cfg = hdmi_pll_cfg,
271 .update_rate = hsdk_pll_comm_update_rate,
272};
273
274static ulong idiv_set(struct clk *, ulong);
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300275static ulong cpu_clk_set(struct clk *, ulong);
276static ulong axi_clk_set(struct clk *, ulong);
277static ulong tun_clk_set(struct clk *, ulong);
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300278static ulong idiv_get(struct clk *);
279static int idiv_off(struct clk *);
280static ulong pll_set(struct clk *, ulong);
281static ulong pll_get(struct clk *);
282
283struct hsdk_cgu_clock_map {
284 u32 cgu_pll_oft;
285 u32 creg_div_oft;
286 u32 cgu_div_oft;
287 const struct hsdk_pll_devdata *pll_devdata;
288 ulong (*get_rate)(struct clk *clk);
289 ulong (*set_rate)(struct clk *clk, ulong rate);
290 int (*disable)(struct clk *clk);
291};
292
293static const struct hsdk_cgu_clock_map clock_map[] = {
294 { CGU_ARC_PLL, 0, 0, &core_pll_dat, pll_get, pll_set, NULL },
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300295 { CGU_ARC_PLL, 0, CGU_ARC_IDIV, &core_pll_dat, idiv_get, cpu_clk_set, idiv_off },
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300296 { CGU_DDR_PLL, 0, 0, &sdt_pll_dat, pll_get, pll_set, NULL },
297 { CGU_SYS_PLL, 0, 0, &sdt_pll_dat, pll_get, pll_set, NULL },
298 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_APB, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300299 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_AXI, &sdt_pll_dat, idiv_get, axi_clk_set, idiv_off },
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300300 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_ETH, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
301 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_USB, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
302 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_SDIO, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
303 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_HDMI, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
304 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_GFX_CORE, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
305 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_GFX_DMA, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
306 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_GFX_CFG, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
307 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_DMAC_CORE, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
308 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_DMAC_CFG, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
309 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_SDIO_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
310 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_SPI_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
311 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_I2C_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
312 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_UART_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
313 { CGU_SYS_PLL, 0, CGU_SYS_IDIV_EBI_REF, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
314 { CGU_TUN_PLL, 0, 0, &sdt_pll_dat, pll_get, pll_set, NULL },
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300315 { CGU_TUN_PLL, 0, CGU_TUN_IDIV_TUN, &sdt_pll_dat, idiv_get, tun_clk_set, idiv_off },
316 { CGU_TUN_PLL, 0, CGU_TUN_IDIV_ROM, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
317 { CGU_TUN_PLL, 0, CGU_TUN_IDIV_PWM, &sdt_pll_dat, idiv_get, idiv_set, idiv_off },
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300318 { CGU_HDMI_PLL, 0, 0, &hdmi_pll_dat, pll_get, pll_set, NULL },
319 { CGU_HDMI_PLL, 0, CGU_HDMI_IDIV_APB, &hdmi_pll_dat, idiv_get, idiv_set, idiv_off }
320};
321
322static inline void hsdk_idiv_write(struct hsdk_cgu_clk *clk, u32 val)
323{
324 iowrite32(val, clk->idiv_regs);
325}
326
327static inline u32 hsdk_idiv_read(struct hsdk_cgu_clk *clk)
328{
329 return ioread32(clk->idiv_regs);
330}
331
332static inline void hsdk_pll_write(struct hsdk_cgu_clk *clk, u32 reg, u32 val)
333{
334 iowrite32(val, clk->regs + reg);
335}
336
337static inline u32 hsdk_pll_read(struct hsdk_cgu_clk *clk, u32 reg)
338{
339 return ioread32(clk->regs + reg);
340}
341
342static inline void hsdk_pll_spcwrite(struct hsdk_cgu_clk *clk, u32 reg, u32 val)
343{
344 iowrite32(val, clk->spec_regs + reg);
345}
346
347static inline u32 hsdk_pll_spcread(struct hsdk_cgu_clk *clk, u32 reg)
348{
349 return ioread32(clk->spec_regs + reg);
350}
351
352static inline void hsdk_pll_set_cfg(struct hsdk_cgu_clk *clk,
353 const struct hsdk_pll_cfg *cfg)
354{
355 u32 val = 0;
356
357 /* Powerdown and Bypass bits should be cleared */
358 val |= cfg->idiv << CGU_PLL_CTRL_IDIV_SHIFT;
359 val |= cfg->fbdiv << CGU_PLL_CTRL_FBDIV_SHIFT;
360 val |= cfg->odiv << CGU_PLL_CTRL_ODIV_SHIFT;
361 val |= cfg->band << CGU_PLL_CTRL_BAND_SHIFT;
362
363 pr_debug("write configurarion: %#x\n", val);
364
365 hsdk_pll_write(clk, CGU_PLL_CTRL, val);
366}
367
368static inline bool hsdk_pll_is_locked(struct hsdk_cgu_clk *clk)
369{
370 return !!(hsdk_pll_read(clk, CGU_PLL_STATUS) & CGU_PLL_STATUS_LOCK);
371}
372
373static inline bool hsdk_pll_is_err(struct hsdk_cgu_clk *clk)
374{
375 return !!(hsdk_pll_read(clk, CGU_PLL_STATUS) & CGU_PLL_STATUS_ERR);
376}
377
378static ulong pll_get(struct clk *sclk)
379{
380 u32 val;
381 u64 rate;
382 u32 idiv, fbdiv, odiv;
383 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300384 u32 parent_rate = clk->pll_devdata->parent_rate;
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300385
386 val = hsdk_pll_read(clk, CGU_PLL_CTRL);
387
388 pr_debug("current configurarion: %#x\n", val);
389
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300390 /* Check if PLL is bypassed */
391 if (val & CGU_PLL_CTRL_BYPASS)
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300392 return parent_rate;
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300393
Eugeniy Paltsev9d70c9c2020-01-29 14:08:29 +0300394 /* Check if PLL is disabled */
395 if (val & CGU_PLL_CTRL_PD)
396 return 0;
397
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300398 /* input divider = reg.idiv + 1 */
399 idiv = 1 + ((val & CGU_PLL_CTRL_IDIV_MASK) >> CGU_PLL_CTRL_IDIV_SHIFT);
400 /* fb divider = 2*(reg.fbdiv + 1) */
401 fbdiv = 2 * (1 + ((val & CGU_PLL_CTRL_FBDIV_MASK) >> CGU_PLL_CTRL_FBDIV_SHIFT));
402 /* output divider = 2^(reg.odiv) */
403 odiv = 1 << ((val & CGU_PLL_CTRL_ODIV_MASK) >> CGU_PLL_CTRL_ODIV_SHIFT);
404
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300405 rate = (u64)parent_rate * fbdiv;
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300406 do_div(rate, idiv * odiv);
407
408 return rate;
409}
410
411static unsigned long hsdk_pll_round_rate(struct clk *sclk, unsigned long rate)
412{
413 int i;
414 unsigned long best_rate;
415 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
416 const struct hsdk_pll_cfg *pll_cfg = clk->pll_devdata->pll_cfg;
417
418 if (pll_cfg[0].rate == 0)
419 return -EINVAL;
420
421 best_rate = pll_cfg[0].rate;
422
423 for (i = 1; pll_cfg[i].rate != 0; i++) {
424 if (abs(rate - pll_cfg[i].rate) < abs(rate - best_rate))
425 best_rate = pll_cfg[i].rate;
426 }
427
428 pr_debug("chosen best rate: %lu\n", best_rate);
429
430 return best_rate;
431}
432
433static int hsdk_pll_comm_update_rate(struct hsdk_cgu_clk *clk,
434 unsigned long rate,
435 const struct hsdk_pll_cfg *cfg)
436{
437 hsdk_pll_set_cfg(clk, cfg);
438
439 /*
440 * Wait until CGU relocks and check error status.
441 * If after timeout CGU is unlocked yet return error.
442 */
443 udelay(HSDK_PLL_MAX_LOCK_TIME);
444 if (!hsdk_pll_is_locked(clk))
445 return -ETIMEDOUT;
446
447 if (hsdk_pll_is_err(clk))
448 return -EINVAL;
449
450 return 0;
451}
452
453static int hsdk_pll_core_update_rate(struct hsdk_cgu_clk *clk,
454 unsigned long rate,
455 const struct hsdk_pll_cfg *cfg)
456{
457 /*
458 * When core clock exceeds 500MHz, the divider for the interface
459 * clock must be programmed to div-by-2.
460 */
461 if (rate > CORE_IF_CLK_THRESHOLD_HZ)
462 hsdk_pll_spcwrite(clk, CREG_CORE_IF_DIV, CREG_CORE_IF_CLK_DIV_2);
463
464 hsdk_pll_set_cfg(clk, cfg);
465
466 /*
467 * Wait until CGU relocks and check error status.
468 * If after timeout CGU is unlocked yet return error.
469 */
470 udelay(HSDK_PLL_MAX_LOCK_TIME);
471 if (!hsdk_pll_is_locked(clk))
472 return -ETIMEDOUT;
473
474 if (hsdk_pll_is_err(clk))
475 return -EINVAL;
476
477 /*
478 * Program divider to div-by-1 if we succesfuly set core clock below
479 * 500MHz threshold.
480 */
481 if (rate <= CORE_IF_CLK_THRESHOLD_HZ)
482 hsdk_pll_spcwrite(clk, CREG_CORE_IF_DIV, CREG_CORE_IF_CLK_DIV_1);
483
484 return 0;
485}
486
487static ulong pll_set(struct clk *sclk, ulong rate)
488{
489 int i;
490 unsigned long best_rate;
491 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
492 const struct hsdk_pll_cfg *pll_cfg = clk->pll_devdata->pll_cfg;
493
494 best_rate = hsdk_pll_round_rate(sclk, rate);
495
496 for (i = 0; pll_cfg[i].rate != 0; i++) {
497 if (pll_cfg[i].rate == best_rate) {
498 return clk->pll_devdata->update_rate(clk, best_rate,
499 &pll_cfg[i]);
500 }
501 }
502
Eugeniy Paltsev5872fc62020-01-29 14:08:30 +0300503 pr_err("invalid rate=%ld Hz, parent_rate=%d Hz\n", best_rate,
504 clk->pll_devdata->parent_rate);
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300505
506 return -EINVAL;
507}
508
509static int idiv_off(struct clk *sclk)
510{
511 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
512
513 hsdk_idiv_write(clk, 0);
514
515 return 0;
516}
517
518static ulong idiv_get(struct clk *sclk)
519{
520 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
521 ulong parent_rate = pll_get(sclk);
522 u32 div_factor = hsdk_idiv_read(clk);
523
524 div_factor &= CGU_IDIV_MASK;
525
526 pr_debug("current configurarion: %#x (%d)\n", div_factor, div_factor);
527
528 if (div_factor == 0)
529 return 0;
530
531 return parent_rate / div_factor;
532}
533
Eugeniy Paltsev74514242018-01-16 20:44:25 +0300534/* Special behavior: wen we set this clock we set both idiv and pll */
535static ulong cpu_clk_set(struct clk *sclk, ulong rate)
536{
537 ulong ret;
538
539 ret = pll_set(sclk, rate);
540 idiv_set(sclk, rate);
541
542 return ret;
543}
544
545/* Special behavior: wen we set this clock we set both idiv and pll and all pll dividers */
546static ulong axi_clk_set(struct clk *sclk, ulong rate)
547{
548 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
549 ulong pll_rate;
550 int i, freq_idx = -1;
551 ulong ret = 0;
552
553 pll_rate = pll_get(sclk);
554
555 for (i = 0; i < MAX_AXI_CLOCKS; i++) {
556 if (axi_clk_cfg.clk_rate[i] == rate) {
557 freq_idx = i;
558 break;
559 }
560 }
561
562 if (freq_idx < 0) {
563 pr_err("axi clk: invalid rate=%ld Hz\n", rate);
564 return -EINVAL;
565 }
566
567 /* configure PLL before dividers */
568 if (axi_clk_cfg.pll_rate[freq_idx] < pll_rate)
569 ret = pll_set(sclk, axi_clk_cfg.pll_rate[freq_idx]);
570
571 /* configure SYS dividers */
572 for (i = 0; i < CGU_SYS_CLOCKS; i++) {
573 clk->idiv_regs = clk->cgu_regs + axi_clk_cfg.idiv[i].oft;
574 hsdk_idiv_write(clk, axi_clk_cfg.idiv[i].val[freq_idx]);
575 }
576
577 /* configure PLL after dividers */
578 if (axi_clk_cfg.pll_rate[freq_idx] >= pll_rate)
579 ret = pll_set(sclk, axi_clk_cfg.pll_rate[freq_idx]);
580
581 return ret;
582}
583
584static ulong tun_clk_set(struct clk *sclk, ulong rate)
585{
586 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
587 ulong pll_rate;
588 int i, freq_idx = -1;
589 ulong ret = 0;
590
591 pll_rate = pll_get(sclk);
592
593 for (i = 0; i < MAX_TUN_CLOCKS; i++) {
594 if (tun_clk_cfg.clk_rate[i] == rate) {
595 freq_idx = i;
596 break;
597 }
598 }
599
600 if (freq_idx < 0) {
601 pr_err("tun clk: invalid rate=%ld Hz\n", rate);
602 return -EINVAL;
603 }
604
605 /* configure PLL before dividers */
606 if (tun_clk_cfg.pll_rate[freq_idx] < pll_rate)
607 ret = pll_set(sclk, tun_clk_cfg.pll_rate[freq_idx]);
608
609 /* configure SYS dividers */
610 for (i = 0; i < CGU_TUN_CLOCKS; i++) {
611 clk->idiv_regs = clk->cgu_regs + tun_clk_cfg.idiv[i].oft;
612 hsdk_idiv_write(clk, tun_clk_cfg.idiv[i].val[freq_idx]);
613 }
614
615 /* configure PLL after dividers */
616 if (tun_clk_cfg.pll_rate[freq_idx] >= pll_rate)
617 ret = pll_set(sclk, tun_clk_cfg.pll_rate[freq_idx]);
618
619 return ret;
620}
621
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300622static ulong idiv_set(struct clk *sclk, ulong rate)
623{
624 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
625 ulong parent_rate = pll_get(sclk);
626 u32 div_factor;
627
628 div_factor = parent_rate / rate;
629 if (abs(rate - parent_rate / (div_factor + 1)) <=
630 abs(rate - parent_rate / div_factor)) {
631 div_factor += 1;
632 }
633
634 if (div_factor & ~CGU_IDIV_MASK) {
Eugeniy Paltsev7d7d9f22018-01-16 20:44:27 +0300635 pr_err("invalid rate=%ld Hz, parent_rate=%ld Hz, div=%d: max divider valie is%d\n",
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300636 rate, parent_rate, div_factor, CGU_IDIV_MASK);
637
638 div_factor = CGU_IDIV_MASK;
639 }
640
641 if (div_factor == 0) {
Eugeniy Paltsev7d7d9f22018-01-16 20:44:27 +0300642 pr_err("invalid rate=%ld Hz, parent_rate=%ld Hz, div=%d: min divider valie is 1\n",
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300643 rate, parent_rate, div_factor);
644
645 div_factor = 1;
646 }
647
648 hsdk_idiv_write(clk, div_factor);
649
650 return 0;
651}
652
653static int hsdk_prepare_clock_tree_branch(struct clk *sclk)
654{
655 struct hsdk_cgu_clk *clk = dev_get_priv(sclk->dev);
656
657 if (sclk->id >= CGU_MAX_CLOCKS)
658 return -EINVAL;
659
660 clk->pll_devdata = clock_map[sclk->id].pll_devdata;
661 clk->regs = clk->cgu_regs + clock_map[sclk->id].cgu_pll_oft;
662 clk->spec_regs = clk->creg_regs + clock_map[sclk->id].creg_div_oft;
663 clk->idiv_regs = clk->cgu_regs + clock_map[sclk->id].cgu_div_oft;
664
665 return 0;
666}
667
668static ulong hsdk_cgu_get_rate(struct clk *sclk)
669{
670 if (hsdk_prepare_clock_tree_branch(sclk))
671 return -EINVAL;
672
673 return clock_map[sclk->id].get_rate(sclk);
674}
675
676static ulong hsdk_cgu_set_rate(struct clk *sclk, ulong rate)
677{
678 if (hsdk_prepare_clock_tree_branch(sclk))
679 return -EINVAL;
680
681 return clock_map[sclk->id].set_rate(sclk, rate);
682}
683
684static int hsdk_cgu_disable(struct clk *sclk)
685{
686 if (hsdk_prepare_clock_tree_branch(sclk))
687 return -EINVAL;
688
689 if (clock_map[sclk->id].disable)
690 return clock_map[sclk->id].disable(sclk);
691
692 return -ENOTSUPP;
693}
694
695static const struct clk_ops hsdk_cgu_ops = {
696 .set_rate = hsdk_cgu_set_rate,
697 .get_rate = hsdk_cgu_get_rate,
698 .disable = hsdk_cgu_disable,
699};
700
701static int hsdk_cgu_clk_probe(struct udevice *dev)
702{
703 struct hsdk_cgu_clk *pll_clk = dev_get_priv(dev);
704
705 BUILD_BUG_ON(ARRAY_SIZE(clock_map) != CGU_MAX_CLOCKS);
706
707 pll_clk->cgu_regs = (void __iomem *)devfdt_get_addr_index(dev, 0);
708 if (!pll_clk->cgu_regs)
709 return -EINVAL;
710
711 pll_clk->creg_regs = (void __iomem *)devfdt_get_addr_index(dev, 1);
712 if (!pll_clk->creg_regs)
713 return -EINVAL;
714
715 return 0;
716}
717
718static const struct udevice_id hsdk_cgu_clk_id[] = {
719 { .compatible = "snps,hsdk-cgu-clock" },
720 { }
721};
722
723U_BOOT_DRIVER(hsdk_cgu_clk) = {
724 .name = "hsdk-cgu-clk",
725 .id = UCLASS_CLK,
726 .of_match = hsdk_cgu_clk_id,
727 .probe = hsdk_cgu_clk_probe,
Eugeniy Paltseva5a238f2018-01-16 20:44:26 +0300728 .priv_auto_alloc_size = sizeof(struct hsdk_cgu_clk),
Eugeniy Paltsev7e1fb092017-12-10 21:20:08 +0300729 .ops = &hsdk_cgu_ops,
730};