blob: 077757efcb82094e7c94d8a8907f4c5f47bfaa7c [file] [log] [blame]
Lukasz Majewski1d7993d2019-06-24 15:50:45 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2019 DENX Software Engineering
4 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
5 */
6
7#include <common.h>
8#include <asm/io.h>
Giulio Benettiefadf792020-01-10 15:46:59 +01009#include <div64.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020010#include <malloc.h>
11#include <clk-uclass.h>
12#include <dm/device.h>
Simon Glass61b29b82020-02-03 07:36:15 -070013#include <dm/devres.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020014#include <dm/uclass.h>
15#include <clk.h>
16#include "clk.h"
Simon Glass61b29b82020-02-03 07:36:15 -070017#include <linux/err.h>
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020018
Giulio Benetti16faa592020-01-10 15:46:53 +010019#define UBOOT_DM_CLK_IMX_PLLV3_GENERIC "imx_clk_pllv3_generic"
Giulio Benettid0ceb932020-01-10 15:46:58 +010020#define UBOOT_DM_CLK_IMX_PLLV3_SYS "imx_clk_pllv3_sys"
Giulio Benetti16faa592020-01-10 15:46:53 +010021#define UBOOT_DM_CLK_IMX_PLLV3_USB "imx_clk_pllv3_usb"
Giulio Benettiefadf792020-01-10 15:46:59 +010022#define UBOOT_DM_CLK_IMX_PLLV3_AV "imx_clk_pllv3_av"
Lukasz Majewski8d540cc2020-02-24 14:55:25 +010023#define UBOOT_DM_CLK_IMX_PLLV3_ENET "imx_clk_pllv3_enet"
Giulio Benettiefadf792020-01-10 15:46:59 +010024
25#define PLL_NUM_OFFSET 0x10
26#define PLL_DENOM_OFFSET 0x20
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020027
Giulio Benettif4b70942020-01-10 15:46:55 +010028#define BM_PLL_POWER (0x1 << 12)
Giulio Benetti8cefbe92020-04-08 17:10:07 +020029#define BM_PLL_ENABLE (0x1 << 13)
Giulio Benetti9841fee2020-01-10 15:46:57 +010030#define BM_PLL_LOCK (0x1 << 31)
Giulio Benettif4b70942020-01-10 15:46:55 +010031
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020032struct clk_pllv3 {
33 struct clk clk;
34 void __iomem *base;
Giulio Benettif4b70942020-01-10 15:46:55 +010035 u32 power_bit;
36 bool powerup_set;
Giulio Benetti8cefbe92020-04-08 17:10:07 +020037 u32 enable_bit;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020038 u32 div_mask;
39 u32 div_shift;
Lukasz Majewski8d540cc2020-02-24 14:55:25 +010040 unsigned long ref_clock;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020041};
42
43#define to_clk_pllv3(_clk) container_of(_clk, struct clk_pllv3, clk)
44
Giulio Benetti16faa592020-01-10 15:46:53 +010045static ulong clk_pllv3_generic_get_rate(struct clk *clk)
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020046{
47 struct clk_pllv3 *pll = to_clk_pllv3(dev_get_clk_ptr(clk->dev));
48 unsigned long parent_rate = clk_get_parent_rate(clk);
49
50 u32 div = (readl(pll->base) >> pll->div_shift) & pll->div_mask;
51
52 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
53}
54
Giulio Benetti9841fee2020-01-10 15:46:57 +010055static ulong clk_pllv3_generic_set_rate(struct clk *clk, ulong rate)
56{
57 struct clk_pllv3 *pll = to_clk_pllv3(clk);
58 unsigned long parent_rate = clk_get_parent_rate(clk);
59 u32 val, div;
60
61 if (rate == parent_rate * 22)
62 div = 1;
63 else if (rate == parent_rate * 20)
64 div = 0;
65 else
66 return -EINVAL;
67
68 val = readl(pll->base);
69 val &= ~(pll->div_mask << pll->div_shift);
70 val |= (div << pll->div_shift);
71 writel(val, pll->base);
72
73 /* Wait for PLL to lock */
74 while (!(readl(pll->base) & BM_PLL_LOCK))
75 ;
76
77 return 0;
78}
79
Giulio Benettif4b70942020-01-10 15:46:55 +010080static int clk_pllv3_generic_enable(struct clk *clk)
81{
82 struct clk_pllv3 *pll = to_clk_pllv3(clk);
83 u32 val;
84
85 val = readl(pll->base);
86 if (pll->powerup_set)
87 val |= pll->power_bit;
88 else
89 val &= ~pll->power_bit;
Giulio Benetti8cefbe92020-04-08 17:10:07 +020090
91 val |= pll->enable_bit;
92
Giulio Benettif4b70942020-01-10 15:46:55 +010093 writel(val, pll->base);
94
95 return 0;
96}
97
Giulio Benetticbb20012020-01-10 15:46:56 +010098static int clk_pllv3_generic_disable(struct clk *clk)
99{
100 struct clk_pllv3 *pll = to_clk_pllv3(clk);
101 u32 val;
102
103 val = readl(pll->base);
104 if (pll->powerup_set)
105 val &= ~pll->power_bit;
106 else
107 val |= pll->power_bit;
Giulio Benetti8cefbe92020-04-08 17:10:07 +0200108
109 val &= ~pll->enable_bit;
110
Giulio Benetticbb20012020-01-10 15:46:56 +0100111 writel(val, pll->base);
112
113 return 0;
114}
115
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200116static const struct clk_ops clk_pllv3_generic_ops = {
Giulio Benetti16faa592020-01-10 15:46:53 +0100117 .get_rate = clk_pllv3_generic_get_rate,
Giulio Benettif4b70942020-01-10 15:46:55 +0100118 .enable = clk_pllv3_generic_enable,
Giulio Benetticbb20012020-01-10 15:46:56 +0100119 .disable = clk_pllv3_generic_disable,
Giulio Benetti9841fee2020-01-10 15:46:57 +0100120 .set_rate = clk_pllv3_generic_set_rate,
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200121};
122
Giulio Benettid0ceb932020-01-10 15:46:58 +0100123static ulong clk_pllv3_sys_get_rate(struct clk *clk)
124{
125 struct clk_pllv3 *pll = to_clk_pllv3(clk);
126 unsigned long parent_rate = clk_get_parent_rate(clk);
127 u32 div = readl(pll->base) & pll->div_mask;
128
129 return parent_rate * div / 2;
130}
131
132static ulong clk_pllv3_sys_set_rate(struct clk *clk, ulong rate)
133{
134 struct clk_pllv3 *pll = to_clk_pllv3(clk);
135 unsigned long parent_rate = clk_get_parent_rate(clk);
Giulio Benetti3391e772020-01-17 13:06:40 +0100136 unsigned long min_rate;
137 unsigned long max_rate;
Giulio Benettid0ceb932020-01-10 15:46:58 +0100138 u32 val, div;
139
Giulio Benetti3391e772020-01-17 13:06:40 +0100140 if (parent_rate == 0)
141 return -EINVAL;
142
143 min_rate = parent_rate * 54 / 2;
144 max_rate = parent_rate * 108 / 2;
145
Giulio Benettid0ceb932020-01-10 15:46:58 +0100146 if (rate < min_rate || rate > max_rate)
147 return -EINVAL;
148
149 div = rate * 2 / parent_rate;
150 val = readl(pll->base);
151 val &= ~pll->div_mask;
152 val |= div;
153 writel(val, pll->base);
154
155 /* Wait for PLL to lock */
156 while (!(readl(pll->base) & BM_PLL_LOCK))
157 ;
158
159 return 0;
160}
161
162static const struct clk_ops clk_pllv3_sys_ops = {
Wolfgang Denk0cf207e2021-09-27 17:42:39 +0200163 .enable = clk_pllv3_generic_enable,
Giulio Benettid0ceb932020-01-10 15:46:58 +0100164 .disable = clk_pllv3_generic_disable,
165 .get_rate = clk_pllv3_sys_get_rate,
166 .set_rate = clk_pllv3_sys_set_rate,
167};
168
Giulio Benettiefadf792020-01-10 15:46:59 +0100169static ulong clk_pllv3_av_get_rate(struct clk *clk)
170{
171 struct clk_pllv3 *pll = to_clk_pllv3(clk);
172 unsigned long parent_rate = clk_get_parent_rate(clk);
173 u32 mfn = readl(pll->base + PLL_NUM_OFFSET);
174 u32 mfd = readl(pll->base + PLL_DENOM_OFFSET);
175 u32 div = readl(pll->base) & pll->div_mask;
176 u64 temp64 = (u64)parent_rate;
177
Giulio Benettid37ecab2020-01-17 13:06:41 +0100178 if (mfd == 0)
179 return -EIO;
180
Giulio Benettiefadf792020-01-10 15:46:59 +0100181 temp64 *= mfn;
182 do_div(temp64, mfd);
183
184 return parent_rate * div + (unsigned long)temp64;
185}
186
187static ulong clk_pllv3_av_set_rate(struct clk *clk, ulong rate)
188{
189 struct clk_pllv3 *pll = to_clk_pllv3(clk);
190 unsigned long parent_rate = clk_get_parent_rate(clk);
Giulio Benetti041b06a2020-01-17 13:06:42 +0100191 unsigned long min_rate;
192 unsigned long max_rate;
Giulio Benettiefadf792020-01-10 15:46:59 +0100193 u32 val, div;
194 u32 mfn, mfd = 1000000;
195 u32 max_mfd = 0x3FFFFFFF;
196 u64 temp64;
197
Giulio Benetti041b06a2020-01-17 13:06:42 +0100198 if (parent_rate == 0)
199 return -EINVAL;
200
201 min_rate = parent_rate * 27;
202 max_rate = parent_rate * 54;
203
Giulio Benettiefadf792020-01-10 15:46:59 +0100204 if (rate < min_rate || rate > max_rate)
205 return -EINVAL;
206
207 if (parent_rate <= max_mfd)
208 mfd = parent_rate;
209
210 div = rate / parent_rate;
211 temp64 = (u64)(rate - div * parent_rate);
212 temp64 *= mfd;
213 do_div(temp64, parent_rate);
214 mfn = temp64;
215
216 val = readl(pll->base);
217 val &= ~pll->div_mask;
218 val |= div;
219 writel(val, pll->base);
220 writel(mfn, pll->base + PLL_NUM_OFFSET);
221 writel(mfd, pll->base + PLL_DENOM_OFFSET);
222
223 /* Wait for PLL to lock */
224 while (!(readl(pll->base) & BM_PLL_LOCK))
225 ;
226
227 return 0;
228}
229
230static const struct clk_ops clk_pllv3_av_ops = {
231 .enable = clk_pllv3_generic_enable,
232 .disable = clk_pllv3_generic_disable,
233 .get_rate = clk_pllv3_av_get_rate,
234 .set_rate = clk_pllv3_av_set_rate,
235};
236
Lukasz Majewski8d540cc2020-02-24 14:55:25 +0100237static ulong clk_pllv3_enet_get_rate(struct clk *clk)
238{
239 struct clk_pllv3 *pll = to_clk_pllv3(clk);
240
241 return pll->ref_clock;
242}
243
244static const struct clk_ops clk_pllv3_enet_ops = {
245 .enable = clk_pllv3_generic_enable,
246 .disable = clk_pllv3_generic_disable,
247 .get_rate = clk_pllv3_enet_get_rate,
248};
249
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200250struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
251 const char *parent_name, void __iomem *base,
252 u32 div_mask)
253{
254 struct clk_pllv3 *pll;
255 struct clk *clk;
256 char *drv_name;
257 int ret;
258
259 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
260 if (!pll)
261 return ERR_PTR(-ENOMEM);
262
Giulio Benettif4b70942020-01-10 15:46:55 +0100263 pll->power_bit = BM_PLL_POWER;
Giulio Benetti8cefbe92020-04-08 17:10:07 +0200264 pll->enable_bit = BM_PLL_ENABLE;
Giulio Benettif4b70942020-01-10 15:46:55 +0100265
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200266 switch (type) {
267 case IMX_PLLV3_GENERIC:
Giulio Benetti16faa592020-01-10 15:46:53 +0100268 drv_name = UBOOT_DM_CLK_IMX_PLLV3_GENERIC;
Giulio Benetti4abd8072020-01-10 15:46:54 +0100269 pll->div_shift = 0;
Giulio Benettif4b70942020-01-10 15:46:55 +0100270 pll->powerup_set = false;
Giulio Benetti16faa592020-01-10 15:46:53 +0100271 break;
Giulio Benettid0ceb932020-01-10 15:46:58 +0100272 case IMX_PLLV3_SYS:
273 drv_name = UBOOT_DM_CLK_IMX_PLLV3_SYS;
274 pll->div_shift = 0;
275 pll->powerup_set = false;
276 break;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200277 case IMX_PLLV3_USB:
Giulio Benetti16faa592020-01-10 15:46:53 +0100278 drv_name = UBOOT_DM_CLK_IMX_PLLV3_USB;
Giulio Benetti4abd8072020-01-10 15:46:54 +0100279 pll->div_shift = 1;
Giulio Benettif4b70942020-01-10 15:46:55 +0100280 pll->powerup_set = true;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200281 break;
Giulio Benettiefadf792020-01-10 15:46:59 +0100282 case IMX_PLLV3_AV:
283 drv_name = UBOOT_DM_CLK_IMX_PLLV3_AV;
284 pll->div_shift = 0;
285 pll->powerup_set = false;
286 break;
Lukasz Majewski8d540cc2020-02-24 14:55:25 +0100287 case IMX_PLLV3_ENET:
288 drv_name = UBOOT_DM_CLK_IMX_PLLV3_ENET;
289 pll->ref_clock = 500000000;
290 break;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200291 default:
292 kfree(pll);
Simon Glass9042bf62021-03-25 10:26:08 +1300293 return ERR_PTR(-EINVAL);
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200294 }
295
296 pll->base = base;
297 pll->div_mask = div_mask;
298 clk = &pll->clk;
299
300 ret = clk_register(clk, drv_name, name, parent_name);
301 if (ret) {
302 kfree(pll);
303 return ERR_PTR(ret);
304 }
305
306 return clk;
307}
308
309U_BOOT_DRIVER(clk_pllv3_generic) = {
Giulio Benetti16faa592020-01-10 15:46:53 +0100310 .name = UBOOT_DM_CLK_IMX_PLLV3_GENERIC,
311 .id = UCLASS_CLK,
312 .ops = &clk_pllv3_generic_ops,
313 .flags = DM_FLAG_PRE_RELOC,
314};
315
Giulio Benettid0ceb932020-01-10 15:46:58 +0100316U_BOOT_DRIVER(clk_pllv3_sys) = {
317 .name = UBOOT_DM_CLK_IMX_PLLV3_SYS,
318 .id = UCLASS_CLK,
319 .ops = &clk_pllv3_sys_ops,
320 .flags = DM_FLAG_PRE_RELOC,
321};
322
Giulio Benetti16faa592020-01-10 15:46:53 +0100323U_BOOT_DRIVER(clk_pllv3_usb) = {
324 .name = UBOOT_DM_CLK_IMX_PLLV3_USB,
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200325 .id = UCLASS_CLK,
326 .ops = &clk_pllv3_generic_ops,
327 .flags = DM_FLAG_PRE_RELOC,
328};
Giulio Benettiefadf792020-01-10 15:46:59 +0100329
330U_BOOT_DRIVER(clk_pllv3_av) = {
331 .name = UBOOT_DM_CLK_IMX_PLLV3_AV,
332 .id = UCLASS_CLK,
333 .ops = &clk_pllv3_av_ops,
334 .flags = DM_FLAG_PRE_RELOC,
335};
Lukasz Majewski8d540cc2020-02-24 14:55:25 +0100336
337U_BOOT_DRIVER(clk_pllv3_enet) = {
338 .name = UBOOT_DM_CLK_IMX_PLLV3_ENET,
339 .id = UCLASS_CLK,
340 .ops = &clk_pllv3_enet_ops,
341};