1 // SPDX-License-Identifier: GPL-2.0+
4 * Mario Six, Guntermann & Drunck GmbH, mario.six@gdsys.cc
8 #include <clk-uclass.h>
9 #include <clock_legacy.h>
14 #include <dt-bindings/clk/mpc83xx-clk.h>
15 #include <asm/arch/soc.h>
17 #include "mpc83xx_clk.h"
19 DECLARE_GLOBAL_DATA_PTR;
22 * struct mpc83xx_clk_priv - Private data structure for the MPC83xx clock
24 * @speed: Array containing the speed values of all system clocks (initialized
25 * once, then only read back)
27 struct mpc83xx_clk_priv {
28 u32 speed[MPC83XX_CLK_COUNT];
32 * is_clk_valid() - Check if clock ID is valid for given clock device
33 * @clk: The clock device for which to check a clock ID
34 * @id: The clock ID to check
36 * Return: true if clock ID is valid for clock device, false if not
38 static inline bool is_clk_valid(struct udevice *clk, int id)
40 ulong type = dev_get_driver_data(clk);
45 case MPC83XX_CLK_MEM_SEC:
46 return type == SOC_MPC8360;
48 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
49 case MPC83XX_CLK_I2C1:
52 return type == SOC_MPC8315;
53 case MPC83XX_CLK_SDHC:
54 return mpc83xx_has_sdhc(type);
55 case MPC83XX_CLK_TSEC1:
56 case MPC83XX_CLK_TSEC2:
57 return mpc83xx_has_tsec(type);
58 case MPC83XX_CLK_USBDR:
59 return type == SOC_MPC8360;
60 case MPC83XX_CLK_USBMPH:
61 return type == SOC_MPC8349;
62 case MPC83XX_CLK_PCIEXP1:
63 return mpc83xx_has_pcie1(type);
64 case MPC83XX_CLK_PCIEXP2:
65 return mpc83xx_has_pcie2(type);
66 case MPC83XX_CLK_SATA:
67 return mpc83xx_has_sata(type);
68 case MPC83XX_CLK_DMAC:
69 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
72 * FIXME: implement proper support for this.
74 return 0 && mpc83xx_has_pci(type);
77 case MPC83XX_CLK_I2C2:
78 return mpc83xx_has_second_i2c(type);
81 return mpc83xx_has_quicc_engine(type) && (type != SOC_MPC8309);
82 case MPC83XX_CLK_LCLK:
83 case MPC83XX_CLK_LBIU:
84 case MPC83XX_CLK_CORE:
92 * init_single_clk() - Initialize a clock with a given ID
93 * @dev: The clock device for which to initialize the clock
96 * The clock speed is read from the hardware's registers, and stored in the
97 * private data structure of the driver. From there it is only retrieved, and
100 * Return: 0 if OK, -ve on error
102 static int init_single_clk(struct udevice *dev, int clk)
104 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
105 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
106 ulong type = dev_get_driver_data(dev);
107 struct clk_mode mode;
109 u32 csb_clk = get_csb_clk(im);
112 ret = retrieve_mode(clk, type, &mode);
114 debug("%s: Could not retrieve mode for clk %d (ret = %d)\n",
115 dev->name, clk, ret);
119 if (mode.type == TYPE_INVALID) {
120 debug("%s: clock %d invalid\n", dev->name, clk);
124 if (mode.type == TYPE_SCCR_STANDARD) {
125 mask = GENMASK(31 - mode.low, 31 - mode.high);
127 switch (sccr_field(im, mask)) {
129 priv->speed[clk] = 0;
132 priv->speed[clk] = csb_clk;
135 priv->speed[clk] = csb_clk / 2;
138 priv->speed[clk] = csb_clk / 3;
141 priv->speed[clk] = 0;
147 if (mode.type == TYPE_SPMR_DIRECT_MULTIPLY) {
148 mask = GENMASK(31 - mode.low, 31 - mode.high);
150 priv->speed[clk] = csb_clk * (1 + sccr_field(im, mask));
154 if (clk == MPC83XX_CLK_CSB || clk == MPC83XX_CLK_I2C2) {
155 priv->speed[clk] = csb_clk; /* i2c-2 clk is equal to csb clk */
159 if (clk == MPC83XX_CLK_QE || clk == MPC83XX_CLK_BRG) {
160 u32 pci_sync_in = get_pci_sync_in(im);
161 u32 qepmf = spmr_field(im, SPMR_CEPMF);
162 u32 qepdf = spmr_field(im, SPMR_CEPDF);
163 u32 qe_clk = (pci_sync_in * qepmf) / (1 + qepdf);
165 if (clk == MPC83XX_CLK_QE)
166 priv->speed[clk] = qe_clk;
168 priv->speed[clk] = qe_clk / 2;
173 if (clk == MPC83XX_CLK_LCLK || clk == MPC83XX_CLK_LBIU) {
174 u32 lbiu_clk = csb_clk *
175 (1 + spmr_field(im, SPMR_LBIUCM));
176 u32 clkdiv = lcrr_field(im, LCRR_CLKDIV);
178 if (clk == MPC83XX_CLK_LBIU)
179 priv->speed[clk] = lbiu_clk;
185 priv->speed[clk] = lbiu_clk / clkdiv;
189 priv->speed[clk] = 0;
195 if (clk == MPC83XX_CLK_CORE) {
196 u8 corepll = spmr_field(im, SPMR_COREPLL);
197 u32 corecnf_tab_index = ((corepll & 0x1F) << 2) |
198 ((corepll & 0x60) >> 5);
200 if (corecnf_tab_index > (ARRAY_SIZE(corecnf_tab))) {
201 debug("%s: Core configuration index %02x too high; possible wrong value",
202 dev->name, corecnf_tab_index);
206 switch (corecnf_tab[corecnf_tab_index].core_csb_ratio) {
209 priv->speed[clk] = csb_clk;
212 priv->speed[clk] = (3 * csb_clk) / 2;
215 priv->speed[clk] = 2 * csb_clk;
218 priv->speed[clk] = (5 * csb_clk) / 2;
221 priv->speed[clk] = 3 * csb_clk;
224 /* unknown core to csb ratio */
225 priv->speed[clk] = 0;
231 /* Unknown clk value -> error */
232 debug("%s: clock %d invalid\n", dev->name, clk);
237 * init_all_clks() - Initialize all clocks of a clock device
238 * @dev: The clock device whose clocks should be initialized
240 * Return: 0 if OK, -ve on error
242 static inline int init_all_clks(struct udevice *dev)
246 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
249 if (!is_clk_valid(dev, i))
252 ret = init_single_clk(dev, i);
254 debug("%s: Failed to initialize %s clock\n",
255 dev->name, names[i]);
263 static int mpc83xx_clk_request(struct clk *clock)
265 /* Reject requests of clocks that are not available */
266 if (is_clk_valid(clock->dev, clock->id))
272 static ulong mpc83xx_clk_get_rate(struct clk *clk)
274 struct mpc83xx_clk_priv *priv = dev_get_priv(clk->dev);
276 if (clk->id >= MPC83XX_CLK_COUNT) {
277 debug("%s: clock index %lu invalid\n", __func__, clk->id);
281 return priv->speed[clk->id];
284 static int mpc83xx_clk_enable(struct clk *clk)
286 /* MPC83xx clocks are always enabled */
292 /* Empty implementation to keep the prototype in common.h happy */
296 int get_serial_clock(void)
298 struct mpc83xx_clk_priv *priv;
302 ret = uclass_first_device_err(UCLASS_CLK, &clk);
304 debug("%s: Could not get clock device\n", __func__);
308 priv = dev_get_priv(clk);
310 return priv->speed[MPC83XX_CLK_CSB];
313 const struct clk_ops mpc83xx_clk_ops = {
314 .request = mpc83xx_clk_request,
315 .get_rate = mpc83xx_clk_get_rate,
316 .enable = mpc83xx_clk_enable,
319 static const struct udevice_id mpc83xx_clk_match[] = {
320 { .compatible = "fsl,mpc8308-clk", .data = SOC_MPC8308 },
321 { .compatible = "fsl,mpc8309-clk", .data = SOC_MPC8309 },
322 { .compatible = "fsl,mpc8313-clk", .data = SOC_MPC8313 },
323 { .compatible = "fsl,mpc8315-clk", .data = SOC_MPC8315 },
324 { .compatible = "fsl,mpc832x-clk", .data = SOC_MPC832X },
325 { .compatible = "fsl,mpc8349-clk", .data = SOC_MPC8349 },
326 { .compatible = "fsl,mpc8360-clk", .data = SOC_MPC8360 },
327 { .compatible = "fsl,mpc8379-clk", .data = SOC_MPC8379 },
331 static int mpc83xx_clk_probe(struct udevice *dev)
333 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
337 ret = init_all_clks(dev);
339 debug("%s: Could not initialize all clocks (ret = %d)\n",
344 type = dev_get_driver_data(dev);
346 if (mpc83xx_has_sdhc(type))
347 gd->arch.sdhc_clk = priv->speed[MPC83XX_CLK_SDHC];
349 gd->arch.core_clk = priv->speed[MPC83XX_CLK_CORE];
350 gd->arch.i2c1_clk = priv->speed[MPC83XX_CLK_I2C1];
351 if (mpc83xx_has_second_i2c(type))
352 gd->arch.i2c2_clk = priv->speed[MPC83XX_CLK_I2C2];
354 gd->mem_clk = priv->speed[MPC83XX_CLK_MEM];
356 if (mpc83xx_has_pci(type))
357 gd->pci_clk = priv->speed[MPC83XX_CLK_PCI];
359 gd->cpu_clk = priv->speed[MPC83XX_CLK_CORE];
360 gd->bus_clk = priv->speed[MPC83XX_CLK_CSB];
365 static int mpc83xx_clk_bind(struct udevice *dev)
368 struct udevice *sys_child;
371 * Since there is no corresponding device tree entry, and since the
372 * clock driver has to be present in either case, bind the sysreset
375 ret = device_bind_driver(dev, "mpc83xx_sysreset", "sysreset",
378 debug("%s: No sysreset driver: ret=%d\n",
384 U_BOOT_DRIVER(mpc83xx_clk) = {
385 .name = "mpc83xx_clk",
387 .of_match = mpc83xx_clk_match,
388 .ops = &mpc83xx_clk_ops,
389 .probe = mpc83xx_clk_probe,
390 .priv_auto_alloc_size = sizeof(struct mpc83xx_clk_priv),
391 .bind = mpc83xx_clk_bind,
394 static int do_clocks(struct cmd_tbl *cmdtp, int flag, int argc,
401 struct mpc83xx_clk_priv *priv;
403 ret = uclass_first_device_err(UCLASS_CLK, &clk);
405 debug("%s: Could not get clock device\n", __func__);
409 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
410 if (!is_clk_valid(clk, i))
413 priv = dev_get_priv(clk);
415 printf("%s = %s MHz\n", names[i], strmhz(buf, priv->speed[i]));
421 U_BOOT_CMD(clocks, 1, 1, do_clocks,
422 "display values of SoC's clocks",