blob: 69c6207e49a74198204a0308633696a0b5b0fcc1 [file] [log] [blame]
Mario Six07d538d2018-08-06 10:23:36 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2017
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
5 */
6
7#include <common.h>
8#include <clk-uclass.h>
9#include <dm.h>
Simon Glass2189d5f2019-11-14 12:57:20 -070010#include <vsprintf.h>
Mario Six07d538d2018-08-06 10:23:36 +020011#include <dm/lists.h>
12#include <dt-bindings/clk/mpc83xx-clk.h>
13#include <asm/arch/soc.h>
14
15#include "mpc83xx_clk.h"
16
17DECLARE_GLOBAL_DATA_PTR;
18
19/**
20 * struct mpc83xx_clk_priv - Private data structure for the MPC83xx clock
21 * driver
22 * @speed: Array containing the speed values of all system clocks (initialized
23 * once, then only read back)
24 */
25struct mpc83xx_clk_priv {
26 u32 speed[MPC83XX_CLK_COUNT];
27};
28
29/**
30 * is_clk_valid() - Check if clock ID is valid for given clock device
31 * @clk: The clock device for which to check a clock ID
32 * @id: The clock ID to check
33 *
34 * Return: true if clock ID is valid for clock device, false if not
35 */
36static inline bool is_clk_valid(struct udevice *clk, int id)
37{
38 ulong type = dev_get_driver_data(clk);
39
40 switch (id) {
41 case MPC83XX_CLK_MEM:
42 return true;
43 case MPC83XX_CLK_MEM_SEC:
44 return type == SOC_MPC8360;
45 case MPC83XX_CLK_ENC:
46 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
47 case MPC83XX_CLK_I2C1:
48 return true;
49 case MPC83XX_CLK_TDM:
50 return type == SOC_MPC8315;
51 case MPC83XX_CLK_SDHC:
52 return mpc83xx_has_sdhc(type);
53 case MPC83XX_CLK_TSEC1:
54 case MPC83XX_CLK_TSEC2:
55 return mpc83xx_has_tsec(type);
56 case MPC83XX_CLK_USBDR:
57 return type == SOC_MPC8360;
58 case MPC83XX_CLK_USBMPH:
59 return type == SOC_MPC8349;
60 case MPC83XX_CLK_PCIEXP1:
61 return mpc83xx_has_pcie1(type);
62 case MPC83XX_CLK_PCIEXP2:
63 return mpc83xx_has_pcie2(type);
64 case MPC83XX_CLK_SATA:
65 return mpc83xx_has_sata(type);
66 case MPC83XX_CLK_DMAC:
67 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
68 case MPC83XX_CLK_PCI:
Rasmus Villemoesfddf8762019-12-19 09:46:08 +000069 /*
70 * FIXME: implement proper support for this.
71 */
72 return 0 && mpc83xx_has_pci(type);
Mario Six07d538d2018-08-06 10:23:36 +020073 case MPC83XX_CLK_CSB:
74 return true;
75 case MPC83XX_CLK_I2C2:
76 return mpc83xx_has_second_i2c(type);
77 case MPC83XX_CLK_QE:
78 case MPC83XX_CLK_BRG:
79 return mpc83xx_has_quicc_engine(type) && (type != SOC_MPC8309);
80 case MPC83XX_CLK_LCLK:
81 case MPC83XX_CLK_LBIU:
82 case MPC83XX_CLK_CORE:
83 return true;
84 }
85
86 return false;
87}
88
89/**
90 * init_single_clk() - Initialize a clock with a given ID
91 * @dev: The clock device for which to initialize the clock
92 * @clk: The clock ID
93 *
94 * The clock speed is read from the hardware's registers, and stored in the
95 * private data structure of the driver. From there it is only retrieved, and
96 * not set.
97 *
98 * Return: 0 if OK, -ve on error
99 */
100static int init_single_clk(struct udevice *dev, int clk)
101{
102 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
103 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
104 ulong type = dev_get_driver_data(dev);
105 struct clk_mode mode;
106 ulong mask;
107 u32 csb_clk = get_csb_clk(im);
108 int ret;
109
110 ret = retrieve_mode(clk, type, &mode);
111 if (ret) {
112 debug("%s: Could not retrieve mode for clk %d (ret = %d)\n",
113 dev->name, clk, ret);
114 return ret;
115 }
116
117 if (mode.type == TYPE_INVALID) {
118 debug("%s: clock %d invalid\n", dev->name, clk);
119 return -EINVAL;
120 }
121
122 if (mode.type == TYPE_SCCR_STANDARD) {
123 mask = GENMASK(31 - mode.low, 31 - mode.high);
124
125 switch (sccr_field(im, mask)) {
126 case 0:
127 priv->speed[clk] = 0;
128 break;
129 case 1:
130 priv->speed[clk] = csb_clk;
131 break;
132 case 2:
133 priv->speed[clk] = csb_clk / 2;
134 break;
135 case 3:
136 priv->speed[clk] = csb_clk / 3;
137 break;
138 default:
139 priv->speed[clk] = 0;
140 }
141
142 return 0;
143 }
144
145 if (mode.type == TYPE_SPMR_DIRECT_MULTIPLY) {
146 mask = GENMASK(31 - mode.low, 31 - mode.high);
147
148 priv->speed[clk] = csb_clk * (1 + sccr_field(im, mask));
149 return 0;
150 }
151
152 if (clk == MPC83XX_CLK_CSB || clk == MPC83XX_CLK_I2C2) {
153 priv->speed[clk] = csb_clk; /* i2c-2 clk is equal to csb clk */
154 return 0;
155 }
156
157 if (clk == MPC83XX_CLK_QE || clk == MPC83XX_CLK_BRG) {
158 u32 pci_sync_in = get_pci_sync_in(im);
159 u32 qepmf = spmr_field(im, SPMR_CEPMF);
160 u32 qepdf = spmr_field(im, SPMR_CEPDF);
161 u32 qe_clk = (pci_sync_in * qepmf) / (1 + qepdf);
162
163 if (clk == MPC83XX_CLK_QE)
164 priv->speed[clk] = qe_clk;
165 else
166 priv->speed[clk] = qe_clk / 2;
167
168 return 0;
169 }
170
171 if (clk == MPC83XX_CLK_LCLK || clk == MPC83XX_CLK_LBIU) {
172 u32 lbiu_clk = csb_clk *
173 (1 + spmr_field(im, SPMR_LBIUCM));
174 u32 clkdiv = lcrr_field(im, LCRR_CLKDIV);
175
176 if (clk == MPC83XX_CLK_LBIU)
177 priv->speed[clk] = lbiu_clk;
178
179 switch (clkdiv) {
180 case 2:
181 case 4:
182 case 8:
183 priv->speed[clk] = lbiu_clk / clkdiv;
184 break;
185 default:
186 /* unknown lcrr */
187 priv->speed[clk] = 0;
188 }
189
190 return 0;
191 }
192
193 if (clk == MPC83XX_CLK_CORE) {
194 u8 corepll = spmr_field(im, SPMR_COREPLL);
195 u32 corecnf_tab_index = ((corepll & 0x1F) << 2) |
196 ((corepll & 0x60) >> 5);
197
198 if (corecnf_tab_index > (ARRAY_SIZE(corecnf_tab))) {
199 debug("%s: Core configuration index %02x too high; possible wrong value",
200 dev->name, corecnf_tab_index);
201 return -EINVAL;
202 }
203
204 switch (corecnf_tab[corecnf_tab_index].core_csb_ratio) {
205 case RAT_BYP:
206 case RAT_1_TO_1:
207 priv->speed[clk] = csb_clk;
208 break;
209 case RAT_1_5_TO_1:
210 priv->speed[clk] = (3 * csb_clk) / 2;
211 break;
212 case RAT_2_TO_1:
213 priv->speed[clk] = 2 * csb_clk;
214 break;
215 case RAT_2_5_TO_1:
216 priv->speed[clk] = (5 * csb_clk) / 2;
217 break;
218 case RAT_3_TO_1:
219 priv->speed[clk] = 3 * csb_clk;
220 break;
221 default:
222 /* unknown core to csb ratio */
223 priv->speed[clk] = 0;
224 }
225
226 return 0;
227 }
228
229 /* Unknown clk value -> error */
230 debug("%s: clock %d invalid\n", dev->name, clk);
231 return -EINVAL;
232}
233
234/**
235 * init_all_clks() - Initialize all clocks of a clock device
236 * @dev: The clock device whose clocks should be initialized
237 *
238 * Return: 0 if OK, -ve on error
239 */
240static inline int init_all_clks(struct udevice *dev)
241{
242 int i;
243
244 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
245 int ret;
246
247 if (!is_clk_valid(dev, i))
248 continue;
249
250 ret = init_single_clk(dev, i);
251 if (ret) {
252 debug("%s: Failed to initialize %s clock\n",
253 dev->name, names[i]);
254 return ret;
255 }
256 }
257
258 return 0;
259}
260
261static int mpc83xx_clk_request(struct clk *clock)
262{
263 /* Reject requests of clocks that are not available */
264 if (is_clk_valid(clock->dev, clock->id))
265 return 0;
266 else
267 return -ENODEV;
268}
269
270static ulong mpc83xx_clk_get_rate(struct clk *clk)
271{
272 struct mpc83xx_clk_priv *priv = dev_get_priv(clk->dev);
273
274 if (clk->id >= MPC83XX_CLK_COUNT) {
275 debug("%s: clock index %lu invalid\n", __func__, clk->id);
276 return 0;
277 }
278
279 return priv->speed[clk->id];
280}
281
Mario Six487bb2b2019-01-28 09:40:36 +0100282static int mpc83xx_clk_enable(struct clk *clk)
283{
284 /* MPC83xx clocks are always enabled */
285 return 0;
286}
287
Mario Six07d538d2018-08-06 10:23:36 +0200288int get_clocks(void)
289{
290 /* Empty implementation to keep the prototype in common.h happy */
291 return 0;
292}
293
294int get_serial_clock(void)
295{
296 struct mpc83xx_clk_priv *priv;
297 struct udevice *clk;
298 int ret;
299
300 ret = uclass_first_device_err(UCLASS_CLK, &clk);
301 if (ret) {
302 debug("%s: Could not get clock device\n", __func__);
303 return ret;
304 }
305
306 priv = dev_get_priv(clk);
307
308 return priv->speed[MPC83XX_CLK_CSB];
309}
310
311const struct clk_ops mpc83xx_clk_ops = {
312 .request = mpc83xx_clk_request,
313 .get_rate = mpc83xx_clk_get_rate,
Mario Six487bb2b2019-01-28 09:40:36 +0100314 .enable = mpc83xx_clk_enable,
Mario Six07d538d2018-08-06 10:23:36 +0200315};
316
317static const struct udevice_id mpc83xx_clk_match[] = {
318 { .compatible = "fsl,mpc8308-clk", .data = SOC_MPC8308 },
319 { .compatible = "fsl,mpc8309-clk", .data = SOC_MPC8309 },
320 { .compatible = "fsl,mpc8313-clk", .data = SOC_MPC8313 },
321 { .compatible = "fsl,mpc8315-clk", .data = SOC_MPC8315 },
322 { .compatible = "fsl,mpc832x-clk", .data = SOC_MPC832X },
323 { .compatible = "fsl,mpc8349-clk", .data = SOC_MPC8349 },
324 { .compatible = "fsl,mpc8360-clk", .data = SOC_MPC8360 },
325 { .compatible = "fsl,mpc8379-clk", .data = SOC_MPC8379 },
326 { /* sentinel */ }
327};
328
329static int mpc83xx_clk_probe(struct udevice *dev)
330{
331 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
332 ulong type;
333 int ret;
334
335 ret = init_all_clks(dev);
336 if (ret) {
337 debug("%s: Could not initialize all clocks (ret = %d)\n",
338 dev->name, ret);
339 return ret;
340 }
341
342 type = dev_get_driver_data(dev);
343
344 if (mpc83xx_has_sdhc(type))
345 gd->arch.sdhc_clk = priv->speed[MPC83XX_CLK_SDHC];
346
347 gd->arch.core_clk = priv->speed[MPC83XX_CLK_CORE];
348 gd->arch.i2c1_clk = priv->speed[MPC83XX_CLK_I2C1];
349 if (mpc83xx_has_second_i2c(type))
350 gd->arch.i2c2_clk = priv->speed[MPC83XX_CLK_I2C2];
351
352 gd->mem_clk = priv->speed[MPC83XX_CLK_MEM];
353
354 if (mpc83xx_has_pci(type))
355 gd->pci_clk = priv->speed[MPC83XX_CLK_PCI];
356
357 gd->cpu_clk = priv->speed[MPC83XX_CLK_CORE];
358 gd->bus_clk = priv->speed[MPC83XX_CLK_CSB];
359
360 return 0;
361}
362
363static int mpc83xx_clk_bind(struct udevice *dev)
364{
365 int ret;
366 struct udevice *sys_child;
367
368 /*
369 * Since there is no corresponding device tree entry, and since the
370 * clock driver has to be present in either case, bind the sysreset
371 * driver here.
372 */
373 ret = device_bind_driver(dev, "mpc83xx_sysreset", "sysreset",
374 &sys_child);
375 if (ret)
376 debug("%s: No sysreset driver: ret=%d\n",
377 dev->name, ret);
378
379 return 0;
380}
381
382U_BOOT_DRIVER(mpc83xx_clk) = {
383 .name = "mpc83xx_clk",
384 .id = UCLASS_CLK,
385 .of_match = mpc83xx_clk_match,
386 .ops = &mpc83xx_clk_ops,
387 .probe = mpc83xx_clk_probe,
388 .priv_auto_alloc_size = sizeof(struct mpc83xx_clk_priv),
389 .bind = mpc83xx_clk_bind,
390};
391
392static int do_clocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
393{
394 int i;
395 char buf[32];
396 struct udevice *clk;
397 int ret;
398 struct mpc83xx_clk_priv *priv;
399
400 ret = uclass_first_device_err(UCLASS_CLK, &clk);
401 if (ret) {
402 debug("%s: Could not get clock device\n", __func__);
403 return ret;
404 }
405
406 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
407 if (!is_clk_valid(clk, i))
408 continue;
409
410 priv = dev_get_priv(clk);
411
412 printf("%s = %s MHz\n", names[i], strmhz(buf, priv->speed[i]));
413 }
414
415 return 0;
416}
417
418U_BOOT_CMD(clocks, 1, 1, do_clocks,
419 "display values of SoC's clocks",
420 ""
421);