1 // SPDX-License-Identifier: GPL-2.0+
7 #include <clk-uclass.h>
8 #include <clock_legacy.h>
13 #include <asm/global_data.h>
15 #include <dt-bindings/clk/mpc83xx-clk.h>
16 #include <asm/arch/soc.h>
17 #include <linux/bitops.h>
19 #include "mpc83xx_clk.h"
21 DECLARE_GLOBAL_DATA_PTR;
24 * struct mpc83xx_clk_priv - Private data structure for the MPC83xx clock
26 * @speed: Array containing the speed values of all system clocks (initialized
27 * once, then only read back)
29 struct mpc83xx_clk_priv {
30 u32 speed[MPC83XX_CLK_COUNT];
34 * is_clk_valid() - Check if clock ID is valid for given clock device
35 * @clk: The clock device for which to check a clock ID
36 * @id: The clock ID to check
38 * Return: true if clock ID is valid for clock device, false if not
40 static inline bool is_clk_valid(struct udevice *clk, int id)
42 ulong type = dev_get_driver_data(clk);
47 case MPC83XX_CLK_MEM_SEC:
48 return type == SOC_MPC8360;
50 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
51 case MPC83XX_CLK_I2C1:
54 return type == SOC_MPC8315;
55 case MPC83XX_CLK_SDHC:
56 return mpc83xx_has_sdhc(type);
57 case MPC83XX_CLK_TSEC1:
58 case MPC83XX_CLK_TSEC2:
59 return mpc83xx_has_tsec(type);
60 case MPC83XX_CLK_USBDR:
61 return type == SOC_MPC8360;
62 case MPC83XX_CLK_USBMPH:
63 return type == SOC_MPC8349;
64 case MPC83XX_CLK_PCIEXP1:
65 return mpc83xx_has_pcie1(type);
66 case MPC83XX_CLK_PCIEXP2:
67 return mpc83xx_has_pcie2(type);
68 case MPC83XX_CLK_SATA:
69 return mpc83xx_has_sata(type);
70 case MPC83XX_CLK_DMAC:
71 return (type == SOC_MPC8308) || (type == SOC_MPC8309);
74 * FIXME: implement proper support for this.
76 return 0 && mpc83xx_has_pci(type);
79 case MPC83XX_CLK_I2C2:
80 return mpc83xx_has_second_i2c(type);
83 return mpc83xx_has_quicc_engine(type) && (type != SOC_MPC8309);
84 case MPC83XX_CLK_LCLK:
85 case MPC83XX_CLK_LBIU:
86 case MPC83XX_CLK_CORE:
94 * init_single_clk() - Initialize a clock with a given ID
95 * @dev: The clock device for which to initialize the clock
98 * The clock speed is read from the hardware's registers, and stored in the
99 * private data structure of the driver. From there it is only retrieved, and
102 * Return: 0 if OK, -ve on error
104 static int init_single_clk(struct udevice *dev, int clk)
106 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
107 immap_t *im = (immap_t *)CONFIG_SYS_IMMR;
108 ulong type = dev_get_driver_data(dev);
109 struct clk_mode mode;
111 u32 csb_clk = get_csb_clk(im);
114 ret = retrieve_mode(clk, type, &mode);
116 debug("%s: Could not retrieve mode for clk %d (ret = %d)\n",
117 dev->name, clk, ret);
121 if (mode.type == TYPE_INVALID) {
122 debug("%s: clock %d invalid\n", dev->name, clk);
126 if (mode.type == TYPE_SCCR_STANDARD) {
127 mask = GENMASK(31 - mode.low, 31 - mode.high);
129 switch (sccr_field(im, mask)) {
131 priv->speed[clk] = 0;
134 priv->speed[clk] = csb_clk;
137 priv->speed[clk] = csb_clk / 2;
140 priv->speed[clk] = csb_clk / 3;
143 priv->speed[clk] = 0;
149 if (mode.type == TYPE_SPMR_DIRECT_MULTIPLY) {
150 mask = GENMASK(31 - mode.low, 31 - mode.high);
152 priv->speed[clk] = csb_clk * (1 + sccr_field(im, mask));
156 if (clk == MPC83XX_CLK_CSB || clk == MPC83XX_CLK_I2C2) {
157 priv->speed[clk] = csb_clk; /* i2c-2 clk is equal to csb clk */
161 if (clk == MPC83XX_CLK_QE || clk == MPC83XX_CLK_BRG) {
162 u32 pci_sync_in = get_pci_sync_in(im);
163 u32 qepmf = spmr_field(im, SPMR_CEPMF);
164 u32 qepdf = spmr_field(im, SPMR_CEPDF);
165 u32 qe_clk = (pci_sync_in * qepmf) / (1 + qepdf);
167 if (clk == MPC83XX_CLK_QE)
168 priv->speed[clk] = qe_clk;
170 priv->speed[clk] = qe_clk / 2;
175 if (clk == MPC83XX_CLK_LCLK || clk == MPC83XX_CLK_LBIU) {
176 u32 lbiu_clk = csb_clk *
177 (1 + spmr_field(im, SPMR_LBIUCM));
178 u32 clkdiv = lcrr_field(im, LCRR_CLKDIV);
180 if (clk == MPC83XX_CLK_LBIU)
181 priv->speed[clk] = lbiu_clk;
187 priv->speed[clk] = lbiu_clk / clkdiv;
191 priv->speed[clk] = 0;
197 if (clk == MPC83XX_CLK_CORE) {
198 u8 corepll = spmr_field(im, SPMR_COREPLL);
199 u32 corecnf_tab_index = ((corepll & 0x1F) << 2) |
200 ((corepll & 0x60) >> 5);
202 if (corecnf_tab_index > (ARRAY_SIZE(corecnf_tab))) {
203 debug("%s: Core configuration index %02x too high; possible wrong value",
204 dev->name, corecnf_tab_index);
208 switch (corecnf_tab[corecnf_tab_index].core_csb_ratio) {
211 priv->speed[clk] = csb_clk;
214 priv->speed[clk] = (3 * csb_clk) / 2;
217 priv->speed[clk] = 2 * csb_clk;
220 priv->speed[clk] = (5 * csb_clk) / 2;
223 priv->speed[clk] = 3 * csb_clk;
226 /* unknown core to csb ratio */
227 priv->speed[clk] = 0;
233 /* Unknown clk value -> error */
234 debug("%s: clock %d invalid\n", dev->name, clk);
239 * init_all_clks() - Initialize all clocks of a clock device
240 * @dev: The clock device whose clocks should be initialized
242 * Return: 0 if OK, -ve on error
244 static inline int init_all_clks(struct udevice *dev)
248 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
251 if (!is_clk_valid(dev, i))
254 ret = init_single_clk(dev, i);
256 debug("%s: Failed to initialize %s clock\n",
257 dev->name, names[i]);
265 static int mpc83xx_clk_request(struct clk *clock)
267 /* Reject requests of clocks that are not available */
268 if (is_clk_valid(clock->dev, clock->id))
274 static ulong mpc83xx_clk_get_rate(struct clk *clk)
276 struct mpc83xx_clk_priv *priv = dev_get_priv(clk->dev);
278 if (clk->id >= MPC83XX_CLK_COUNT) {
279 debug("%s: clock index %lu invalid\n", __func__, clk->id);
283 return priv->speed[clk->id];
286 static int mpc83xx_clk_enable(struct clk *clk)
288 /* MPC83xx clocks are always enabled */
294 /* Empty implementation to keep the prototype in common.h happy */
298 int get_serial_clock(void)
300 struct mpc83xx_clk_priv *priv;
304 ret = uclass_first_device_err(UCLASS_CLK, &clk);
306 debug("%s: Could not get clock device\n", __func__);
310 priv = dev_get_priv(clk);
312 return priv->speed[MPC83XX_CLK_CSB];
315 const struct clk_ops mpc83xx_clk_ops = {
316 .request = mpc83xx_clk_request,
317 .get_rate = mpc83xx_clk_get_rate,
318 .enable = mpc83xx_clk_enable,
321 static const struct udevice_id mpc83xx_clk_match[] = {
322 { .compatible = "fsl,mpc8308-clk", .data = SOC_MPC8308 },
323 { .compatible = "fsl,mpc8309-clk", .data = SOC_MPC8309 },
324 { .compatible = "fsl,mpc8313-clk", .data = SOC_MPC8313 },
325 { .compatible = "fsl,mpc8315-clk", .data = SOC_MPC8315 },
326 { .compatible = "fsl,mpc832x-clk", .data = SOC_MPC832X },
327 { .compatible = "fsl,mpc8349-clk", .data = SOC_MPC8349 },
328 { .compatible = "fsl,mpc8360-clk", .data = SOC_MPC8360 },
329 { .compatible = "fsl,mpc8379-clk", .data = SOC_MPC8379 },
333 static int mpc83xx_clk_probe(struct udevice *dev)
335 struct mpc83xx_clk_priv *priv = dev_get_priv(dev);
339 ret = init_all_clks(dev);
341 debug("%s: Could not initialize all clocks (ret = %d)\n",
346 type = dev_get_driver_data(dev);
348 #ifdef CONFIG_FSL_ESDHC
349 if (mpc83xx_has_sdhc(type))
350 gd->arch.sdhc_clk = priv->speed[MPC83XX_CLK_SDHC];
353 gd->arch.core_clk = priv->speed[MPC83XX_CLK_CORE];
354 gd->arch.i2c1_clk = priv->speed[MPC83XX_CLK_I2C1];
355 if (mpc83xx_has_second_i2c(type))
356 gd->arch.i2c2_clk = priv->speed[MPC83XX_CLK_I2C2];
358 gd->mem_clk = priv->speed[MPC83XX_CLK_MEM];
360 if (mpc83xx_has_pci(type))
361 gd->arch.pci_clk = priv->speed[MPC83XX_CLK_PCI];
363 gd->cpu_clk = priv->speed[MPC83XX_CLK_CORE];
364 gd->bus_clk = priv->speed[MPC83XX_CLK_CSB];
367 gd->arch.qe_clk = priv->speed[MPC83XX_CLK_QE];
368 gd->arch.brg_clk = priv->speed[MPC83XX_CLK_BRG];
374 static int mpc83xx_clk_bind(struct udevice *dev)
377 struct udevice *sys_child;
380 * Since there is no corresponding device tree entry, and since the
381 * clock driver has to be present in either case, bind the sysreset
384 ret = device_bind_driver(dev, "mpc83xx_sysreset", "sysreset",
387 debug("%s: No sysreset driver: ret=%d\n",
393 U_BOOT_DRIVER(mpc83xx_clk) = {
394 .name = "mpc83xx_clk",
396 .of_match = mpc83xx_clk_match,
397 .ops = &mpc83xx_clk_ops,
398 .probe = mpc83xx_clk_probe,
399 .priv_auto = sizeof(struct mpc83xx_clk_priv),
400 .bind = mpc83xx_clk_bind,
403 static int do_clocks(struct cmd_tbl *cmdtp, int flag, int argc,
410 struct mpc83xx_clk_priv *priv;
412 ret = uclass_first_device_err(UCLASS_CLK, &clk);
414 debug("%s: Could not get clock device\n", __func__);
418 for (i = 0; i < MPC83XX_CLK_COUNT; i++) {
419 if (!is_clk_valid(clk, i))
422 priv = dev_get_priv(clk);
424 printf("%s = %s MHz\n", names[i], strmhz(buf, priv->speed[i]));
430 U_BOOT_CMD(clocks, 1, 1, do_clocks,
431 "display values of SoC's clocks",