blob: b4a9d587e1b191650796d32c94a90c7d4dc94096 [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"
23
24#define PLL_NUM_OFFSET 0x10
25#define PLL_DENOM_OFFSET 0x20
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020026
Giulio Benettif4b70942020-01-10 15:46:55 +010027#define BM_PLL_POWER (0x1 << 12)
Giulio Benetti8cefbe92020-04-08 17:10:07 +020028#define BM_PLL_ENABLE (0x1 << 13)
Giulio Benetti9841fee2020-01-10 15:46:57 +010029#define BM_PLL_LOCK (0x1 << 31)
Giulio Benettif4b70942020-01-10 15:46:55 +010030
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020031struct clk_pllv3 {
32 struct clk clk;
33 void __iomem *base;
Giulio Benettif4b70942020-01-10 15:46:55 +010034 u32 power_bit;
35 bool powerup_set;
Giulio Benetti8cefbe92020-04-08 17:10:07 +020036 u32 enable_bit;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020037 u32 div_mask;
38 u32 div_shift;
39};
40
41#define to_clk_pllv3(_clk) container_of(_clk, struct clk_pllv3, clk)
42
Giulio Benetti16faa592020-01-10 15:46:53 +010043static ulong clk_pllv3_generic_get_rate(struct clk *clk)
Lukasz Majewski1d7993d2019-06-24 15:50:45 +020044{
45 struct clk_pllv3 *pll = to_clk_pllv3(dev_get_clk_ptr(clk->dev));
46 unsigned long parent_rate = clk_get_parent_rate(clk);
47
48 u32 div = (readl(pll->base) >> pll->div_shift) & pll->div_mask;
49
50 return (div == 1) ? parent_rate * 22 : parent_rate * 20;
51}
52
Giulio Benetti9841fee2020-01-10 15:46:57 +010053static ulong clk_pllv3_generic_set_rate(struct clk *clk, ulong rate)
54{
55 struct clk_pllv3 *pll = to_clk_pllv3(clk);
56 unsigned long parent_rate = clk_get_parent_rate(clk);
57 u32 val, div;
58
59 if (rate == parent_rate * 22)
60 div = 1;
61 else if (rate == parent_rate * 20)
62 div = 0;
63 else
64 return -EINVAL;
65
66 val = readl(pll->base);
67 val &= ~(pll->div_mask << pll->div_shift);
68 val |= (div << pll->div_shift);
69 writel(val, pll->base);
70
71 /* Wait for PLL to lock */
72 while (!(readl(pll->base) & BM_PLL_LOCK))
73 ;
74
75 return 0;
76}
77
Giulio Benettif4b70942020-01-10 15:46:55 +010078static int clk_pllv3_generic_enable(struct clk *clk)
79{
80 struct clk_pllv3 *pll = to_clk_pllv3(clk);
81 u32 val;
82
83 val = readl(pll->base);
84 if (pll->powerup_set)
85 val |= pll->power_bit;
86 else
87 val &= ~pll->power_bit;
Giulio Benetti8cefbe92020-04-08 17:10:07 +020088
89 val |= pll->enable_bit;
90
Giulio Benettif4b70942020-01-10 15:46:55 +010091 writel(val, pll->base);
92
93 return 0;
94}
95
Giulio Benetticbb20012020-01-10 15:46:56 +010096static int clk_pllv3_generic_disable(struct clk *clk)
97{
98 struct clk_pllv3 *pll = to_clk_pllv3(clk);
99 u32 val;
100
101 val = readl(pll->base);
102 if (pll->powerup_set)
103 val &= ~pll->power_bit;
104 else
105 val |= pll->power_bit;
Giulio Benetti8cefbe92020-04-08 17:10:07 +0200106
107 val &= ~pll->enable_bit;
108
Giulio Benetticbb20012020-01-10 15:46:56 +0100109 writel(val, pll->base);
110
111 return 0;
112}
113
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200114static const struct clk_ops clk_pllv3_generic_ops = {
Giulio Benetti16faa592020-01-10 15:46:53 +0100115 .get_rate = clk_pllv3_generic_get_rate,
Giulio Benettif4b70942020-01-10 15:46:55 +0100116 .enable = clk_pllv3_generic_enable,
Giulio Benetticbb20012020-01-10 15:46:56 +0100117 .disable = clk_pllv3_generic_disable,
Giulio Benetti9841fee2020-01-10 15:46:57 +0100118 .set_rate = clk_pllv3_generic_set_rate,
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200119};
120
Giulio Benettid0ceb932020-01-10 15:46:58 +0100121static ulong clk_pllv3_sys_get_rate(struct clk *clk)
122{
123 struct clk_pllv3 *pll = to_clk_pllv3(clk);
124 unsigned long parent_rate = clk_get_parent_rate(clk);
125 u32 div = readl(pll->base) & pll->div_mask;
126
127 return parent_rate * div / 2;
128}
129
130static ulong clk_pllv3_sys_set_rate(struct clk *clk, ulong rate)
131{
132 struct clk_pllv3 *pll = to_clk_pllv3(clk);
133 unsigned long parent_rate = clk_get_parent_rate(clk);
Giulio Benetti3391e772020-01-17 13:06:40 +0100134 unsigned long min_rate;
135 unsigned long max_rate;
Giulio Benettid0ceb932020-01-10 15:46:58 +0100136 u32 val, div;
137
Giulio Benetti3391e772020-01-17 13:06:40 +0100138 if (parent_rate == 0)
139 return -EINVAL;
140
141 min_rate = parent_rate * 54 / 2;
142 max_rate = parent_rate * 108 / 2;
143
Giulio Benettid0ceb932020-01-10 15:46:58 +0100144 if (rate < min_rate || rate > max_rate)
145 return -EINVAL;
146
147 div = rate * 2 / parent_rate;
148 val = readl(pll->base);
149 val &= ~pll->div_mask;
150 val |= div;
151 writel(val, pll->base);
152
153 /* Wait for PLL to lock */
154 while (!(readl(pll->base) & BM_PLL_LOCK))
155 ;
156
157 return 0;
158}
159
160static const struct clk_ops clk_pllv3_sys_ops = {
161 .enable = clk_pllv3_generic_enable,
162 .disable = clk_pllv3_generic_disable,
163 .get_rate = clk_pllv3_sys_get_rate,
164 .set_rate = clk_pllv3_sys_set_rate,
165};
166
Giulio Benettiefadf792020-01-10 15:46:59 +0100167static ulong clk_pllv3_av_get_rate(struct clk *clk)
168{
169 struct clk_pllv3 *pll = to_clk_pllv3(clk);
170 unsigned long parent_rate = clk_get_parent_rate(clk);
171 u32 mfn = readl(pll->base + PLL_NUM_OFFSET);
172 u32 mfd = readl(pll->base + PLL_DENOM_OFFSET);
173 u32 div = readl(pll->base) & pll->div_mask;
174 u64 temp64 = (u64)parent_rate;
175
Giulio Benettid37ecab2020-01-17 13:06:41 +0100176 if (mfd == 0)
177 return -EIO;
178
Giulio Benettiefadf792020-01-10 15:46:59 +0100179 temp64 *= mfn;
180 do_div(temp64, mfd);
181
182 return parent_rate * div + (unsigned long)temp64;
183}
184
185static ulong clk_pllv3_av_set_rate(struct clk *clk, ulong rate)
186{
187 struct clk_pllv3 *pll = to_clk_pllv3(clk);
188 unsigned long parent_rate = clk_get_parent_rate(clk);
Giulio Benetti041b06a2020-01-17 13:06:42 +0100189 unsigned long min_rate;
190 unsigned long max_rate;
Giulio Benettiefadf792020-01-10 15:46:59 +0100191 u32 val, div;
192 u32 mfn, mfd = 1000000;
193 u32 max_mfd = 0x3FFFFFFF;
194 u64 temp64;
195
Giulio Benetti041b06a2020-01-17 13:06:42 +0100196 if (parent_rate == 0)
197 return -EINVAL;
198
199 min_rate = parent_rate * 27;
200 max_rate = parent_rate * 54;
201
Giulio Benettiefadf792020-01-10 15:46:59 +0100202 if (rate < min_rate || rate > max_rate)
203 return -EINVAL;
204
205 if (parent_rate <= max_mfd)
206 mfd = parent_rate;
207
208 div = rate / parent_rate;
209 temp64 = (u64)(rate - div * parent_rate);
210 temp64 *= mfd;
211 do_div(temp64, parent_rate);
212 mfn = temp64;
213
214 val = readl(pll->base);
215 val &= ~pll->div_mask;
216 val |= div;
217 writel(val, pll->base);
218 writel(mfn, pll->base + PLL_NUM_OFFSET);
219 writel(mfd, pll->base + PLL_DENOM_OFFSET);
220
221 /* Wait for PLL to lock */
222 while (!(readl(pll->base) & BM_PLL_LOCK))
223 ;
224
225 return 0;
226}
227
228static const struct clk_ops clk_pllv3_av_ops = {
229 .enable = clk_pllv3_generic_enable,
230 .disable = clk_pllv3_generic_disable,
231 .get_rate = clk_pllv3_av_get_rate,
232 .set_rate = clk_pllv3_av_set_rate,
233};
234
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200235struct clk *imx_clk_pllv3(enum imx_pllv3_type type, const char *name,
236 const char *parent_name, void __iomem *base,
237 u32 div_mask)
238{
239 struct clk_pllv3 *pll;
240 struct clk *clk;
241 char *drv_name;
242 int ret;
243
244 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
245 if (!pll)
246 return ERR_PTR(-ENOMEM);
247
Giulio Benettif4b70942020-01-10 15:46:55 +0100248 pll->power_bit = BM_PLL_POWER;
Giulio Benetti8cefbe92020-04-08 17:10:07 +0200249 pll->enable_bit = BM_PLL_ENABLE;
Giulio Benettif4b70942020-01-10 15:46:55 +0100250
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200251 switch (type) {
252 case IMX_PLLV3_GENERIC:
Giulio Benetti16faa592020-01-10 15:46:53 +0100253 drv_name = UBOOT_DM_CLK_IMX_PLLV3_GENERIC;
Giulio Benetti4abd8072020-01-10 15:46:54 +0100254 pll->div_shift = 0;
Giulio Benettif4b70942020-01-10 15:46:55 +0100255 pll->powerup_set = false;
Giulio Benetti16faa592020-01-10 15:46:53 +0100256 break;
Giulio Benettid0ceb932020-01-10 15:46:58 +0100257 case IMX_PLLV3_SYS:
258 drv_name = UBOOT_DM_CLK_IMX_PLLV3_SYS;
259 pll->div_shift = 0;
260 pll->powerup_set = false;
261 break;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200262 case IMX_PLLV3_USB:
Giulio Benetti16faa592020-01-10 15:46:53 +0100263 drv_name = UBOOT_DM_CLK_IMX_PLLV3_USB;
Giulio Benetti4abd8072020-01-10 15:46:54 +0100264 pll->div_shift = 1;
Giulio Benettif4b70942020-01-10 15:46:55 +0100265 pll->powerup_set = true;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200266 break;
Giulio Benettiefadf792020-01-10 15:46:59 +0100267 case IMX_PLLV3_AV:
268 drv_name = UBOOT_DM_CLK_IMX_PLLV3_AV;
269 pll->div_shift = 0;
270 pll->powerup_set = false;
271 break;
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200272 default:
273 kfree(pll);
274 return ERR_PTR(-ENOTSUPP);
275 }
276
277 pll->base = base;
278 pll->div_mask = div_mask;
279 clk = &pll->clk;
280
281 ret = clk_register(clk, drv_name, name, parent_name);
282 if (ret) {
283 kfree(pll);
284 return ERR_PTR(ret);
285 }
286
287 return clk;
288}
289
290U_BOOT_DRIVER(clk_pllv3_generic) = {
Giulio Benetti16faa592020-01-10 15:46:53 +0100291 .name = UBOOT_DM_CLK_IMX_PLLV3_GENERIC,
292 .id = UCLASS_CLK,
293 .ops = &clk_pllv3_generic_ops,
294 .flags = DM_FLAG_PRE_RELOC,
295};
296
Giulio Benettid0ceb932020-01-10 15:46:58 +0100297U_BOOT_DRIVER(clk_pllv3_sys) = {
298 .name = UBOOT_DM_CLK_IMX_PLLV3_SYS,
299 .id = UCLASS_CLK,
300 .ops = &clk_pllv3_sys_ops,
301 .flags = DM_FLAG_PRE_RELOC,
302};
303
Giulio Benetti16faa592020-01-10 15:46:53 +0100304U_BOOT_DRIVER(clk_pllv3_usb) = {
305 .name = UBOOT_DM_CLK_IMX_PLLV3_USB,
Lukasz Majewski1d7993d2019-06-24 15:50:45 +0200306 .id = UCLASS_CLK,
307 .ops = &clk_pllv3_generic_ops,
308 .flags = DM_FLAG_PRE_RELOC,
309};
Giulio Benettiefadf792020-01-10 15:46:59 +0100310
311U_BOOT_DRIVER(clk_pllv3_av) = {
312 .name = UBOOT_DM_CLK_IMX_PLLV3_AV,
313 .id = UCLASS_CLK,
314 .ops = &clk_pllv3_av_ops,
315 .flags = DM_FLAG_PRE_RELOC,
316};