1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
8 #include <clk-uclass.h>
11 #include <dt-bindings/clk/mpc83xx-clk.h>
12 #include <asm/arch/soc.h>
14 #include "mpc83xx_clk.h"
16 DECLARE_GLOBAL_DATA_PTR;
19 * struct mpc83xx_clk_priv - Private data structure for the MPC83xx clock
21 * @speed: Array containing the speed values of all system clocks (initialized
22 * once, then only read back)
24 struct mpc83xx_clk_priv {
25 u32 speed[MPC83XX_CLK_COUNT];
29 * is_clk_valid() - Check if clock ID is valid for given clock device
30 * @clk: The clock device for which to check a clock ID
31 * @id: The clock ID to check
33 * Return: true if clock ID is valid for clock device, false if not
35 static inline bool is_clk_valid(struct udevice *clk, int id)
37 ulong type = dev_get_driver_data(clk);
42 case MPC83XX_CLK_MEM_SEC:
43 return type == SOC_MPC8360;
45 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
46 case MPC83XX_CLK_I2C1:
49 return type == SOC_MPC8315;
50 case MPC83XX_CLK_SDHC:
51 return mpc83xx_has_sdhc(type);
52 case MPC83XX_CLK_TSEC1:
53 case MPC83XX_CLK_TSEC2:
54 return mpc83xx_has_tsec(type);
55 case MPC83XX_CLK_USBDR:
56 return type == SOC_MPC8360;
57 case MPC83XX_CLK_USBMPH:
58 return type == SOC_MPC8349;
59 case MPC83XX_CLK_PCIEXP1:
60 return mpc83xx_has_pcie1(type);
61 case MPC83XX_CLK_PCIEXP2:
62 return mpc83xx_has_pcie2(type);
63 case MPC83XX_CLK_SATA:
64 return mpc83xx_has_sata(type);
65 case MPC83XX_CLK_DMAC:
66 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
68 return mpc83xx_has_pci(type);
71 case MPC83XX_CLK_I2C2:
72 return mpc83xx_has_second_i2c(type);
75 return mpc83xx_has_quicc_engine(type) && (type != SOC_MPC8309);
76 case MPC83XX_CLK_LCLK:
77 case MPC83XX_CLK_LBIU:
78 case MPC83XX_CLK_CORE:
86 * init_single_clk() - Initialize a clock with a given ID
87 * @dev: The clock device for which to initialize the clock
90 * The clock speed is read from the hardware's registers, and stored in the
91 * private data structure of the driver. From there it is only retrieved, and
94 * Return: 0 if OK, -ve on error
96 static int init_single_clk(struct udevice *dev, int clk)
98 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
99 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
100 ulong type = dev_get_driver_data(dev);
101 struct clk_mode mode;
103 u32 csb_clk = get_csb_clk(im);
106 ret = retrieve_mode(clk, type, &mode);
108 debug("%s: Could not retrieve mode for clk %d (ret = %d)\n",
109 dev->name, clk, ret);
113 if (mode.type == TYPE_INVALID) {
114 debug("%s: clock %d invalid\n", dev->name, clk);
118 if (mode.type == TYPE_SCCR_STANDARD) {
119 mask = GENMASK(31 - mode.low, 31 - mode.high);
121 switch (sccr_field(im, mask)) {
123 priv->speed[clk] = 0;
126 priv->speed[clk] = csb_clk;
129 priv->speed[clk] = csb_clk / 2;
132 priv->speed[clk] = csb_clk / 3;
135 priv->speed[clk] = 0;
141 if (mode.type == TYPE_SPMR_DIRECT_MULTIPLY) {
142 mask = GENMASK(31 - mode.low, 31 - mode.high);
144 priv->speed[clk] = csb_clk * (1 + sccr_field(im, mask));
148 if (clk == MPC83XX_CLK_CSB || clk == MPC83XX_CLK_I2C2) {
149 priv->speed[clk] = csb_clk; /* i2c-2 clk is equal to csb clk */
153 if (clk == MPC83XX_CLK_QE || clk == MPC83XX_CLK_BRG) {
154 u32 pci_sync_in = get_pci_sync_in(im);
155 u32 qepmf = spmr_field(im, SPMR_CEPMF);
156 u32 qepdf = spmr_field(im, SPMR_CEPDF);
157 u32 qe_clk = (pci_sync_in * qepmf) / (1 + qepdf);
159 if (clk == MPC83XX_CLK_QE)
160 priv->speed[clk] = qe_clk;
162 priv->speed[clk] = qe_clk / 2;
167 if (clk == MPC83XX_CLK_LCLK || clk == MPC83XX_CLK_LBIU) {
168 u32 lbiu_clk = csb_clk *
169 (1 + spmr_field(im, SPMR_LBIUCM));
170 u32 clkdiv = lcrr_field(im, LCRR_CLKDIV);
172 if (clk == MPC83XX_CLK_LBIU)
173 priv->speed[clk] = lbiu_clk;
179 priv->speed[clk] = lbiu_clk / clkdiv;
183 priv->speed[clk] = 0;
189 if (clk == MPC83XX_CLK_CORE) {
190 u8 corepll = spmr_field(im, SPMR_COREPLL);
191 u32 corecnf_tab_index = ((corepll & 0x1F) << 2) |
192 ((corepll & 0x60) >> 5);
194 if (corecnf_tab_index > (ARRAY_SIZE(corecnf_tab))) {
195 debug("%s: Core configuration index %02x too high; possible wrong value",
196 dev->name, corecnf_tab_index);
200 switch (corecnf_tab[corecnf_tab_index].core_csb_ratio) {
203 priv->speed[clk] = csb_clk;
206 priv->speed[clk] = (3 * csb_clk) / 2;
209 priv->speed[clk] = 2 * csb_clk;
212 priv->speed[clk] = (5 * csb_clk) / 2;
215 priv->speed[clk] = 3 * csb_clk;
218 /* unknown core to csb ratio */
219 priv->speed[clk] = 0;
225 /* Unknown clk value -> error */
226 debug("%s: clock %d invalid\n", dev->name, clk);
231 * init_all_clks() - Initialize all clocks of a clock device
232 * @dev: The clock device whose clocks should be initialized
234 * Return: 0 if OK, -ve on error
236 static inline int init_all_clks(struct udevice *dev)
240 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
243 if (!is_clk_valid(dev, i))
246 ret = init_single_clk(dev, i);
248 debug("%s: Failed to initialize %s clock\n",
249 dev->name, names[i]);
257 static int mpc83xx_clk_request(struct clk *clock)
259 /* Reject requests of clocks that are not available */
260 if (is_clk_valid(clock->dev, clock->id))
266 static ulong mpc83xx_clk_get_rate(struct clk *clk)
268 struct mpc83xx_clk_priv *priv = dev_get_priv(clk->dev);
270 if (clk->id >= MPC83XX_CLK_COUNT) {
271 debug("%s: clock index %lu invalid\n", __func__, clk->id);
275 return priv->speed[clk->id];
278 static int mpc83xx_clk_enable(struct clk *clk)
280 /* MPC83xx clocks are always enabled */
286 /* Empty implementation to keep the prototype in common.h happy */
290 int get_serial_clock(void)
292 struct mpc83xx_clk_priv *priv;
296 ret = uclass_first_device_err(UCLASS_CLK, &clk);
298 debug("%s: Could not get clock device\n", __func__);
302 priv = dev_get_priv(clk);
304 return priv->speed[MPC83XX_CLK_CSB];
307 const struct clk_ops mpc83xx_clk_ops = {
308 .request = mpc83xx_clk_request,
309 .get_rate = mpc83xx_clk_get_rate,
310 .enable = mpc83xx_clk_enable,
313 static const struct udevice_id mpc83xx_clk_match[] = {
314 { .compatible = "fsl,mpc8308-clk", .data = SOC_MPC8308 },
315 { .compatible = "fsl,mpc8309-clk", .data = SOC_MPC8309 },
316 { .compatible = "fsl,mpc8313-clk", .data = SOC_MPC8313 },
317 { .compatible = "fsl,mpc8315-clk", .data = SOC_MPC8315 },
318 { .compatible = "fsl,mpc832x-clk", .data = SOC_MPC832X },
319 { .compatible = "fsl,mpc8349-clk", .data = SOC_MPC8349 },
320 { .compatible = "fsl,mpc8360-clk", .data = SOC_MPC8360 },
321 { .compatible = "fsl,mpc8379-clk", .data = SOC_MPC8379 },
325 static int mpc83xx_clk_probe(struct udevice *dev)
327 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
331 ret = init_all_clks(dev);
333 debug("%s: Could not initialize all clocks (ret = %d)\n",
338 type = dev_get_driver_data(dev);
340 if (mpc83xx_has_sdhc(type))
341 gd->arch.sdhc_clk = priv->speed[MPC83XX_CLK_SDHC];
343 gd->arch.core_clk = priv->speed[MPC83XX_CLK_CORE];
344 gd->arch.i2c1_clk = priv->speed[MPC83XX_CLK_I2C1];
345 if (mpc83xx_has_second_i2c(type))
346 gd->arch.i2c2_clk = priv->speed[MPC83XX_CLK_I2C2];
348 gd->mem_clk = priv->speed[MPC83XX_CLK_MEM];
350 if (mpc83xx_has_pci(type))
351 gd->pci_clk = priv->speed[MPC83XX_CLK_PCI];
353 gd->cpu_clk = priv->speed[MPC83XX_CLK_CORE];
354 gd->bus_clk = priv->speed[MPC83XX_CLK_CSB];
359 static int mpc83xx_clk_bind(struct udevice *dev)
362 struct udevice *sys_child;
365 * Since there is no corresponding device tree entry, and since the
366 * clock driver has to be present in either case, bind the sysreset
369 ret = device_bind_driver(dev, "mpc83xx_sysreset", "sysreset",
372 debug("%s: No sysreset driver: ret=%d\n",
378 U_BOOT_DRIVER(mpc83xx_clk) = {
379 .name = "mpc83xx_clk",
381 .of_match = mpc83xx_clk_match,
382 .ops = &mpc83xx_clk_ops,
383 .probe = mpc83xx_clk_probe,
384 .priv_auto_alloc_size = sizeof(struct mpc83xx_clk_priv),
385 .bind = mpc83xx_clk_bind,
388 static int do_clocks(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
394 struct mpc83xx_clk_priv *priv;
396 ret = uclass_first_device_err(UCLASS_CLK, &clk);
398 debug("%s: Could not get clock device\n", __func__);
402 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
403 if (!is_clk_valid(clk, i))
406 priv = dev_get_priv(clk);
408 printf("%s = %s MHz\n", names[i], strmhz(buf, priv->speed[i]));
414 U_BOOT_CMD(clocks, 1, 1, do_clocks,
415 "display values of SoC's clocks",