1 // SPDX-License-Identifier: GPL-2.0
3 * Nuvoton NPCM7xx Clock Generator
4 * All the clocks are initialized by the bootloader, so this driver allow only
5 * reading of current settings directly from the hardware.
7 * Copyright (C) 2018 Nuvoton Technologies tali.perry@nuvoton.com
10 #include <linux/module.h>
11 #include <linux/clk-provider.h>
13 #include <linux/kernel.h>
15 #include <linux/of_address.h>
16 #include <linux/slab.h>
17 #include <linux/err.h>
18 #include <linux/bitfield.h>
20 #include <dt-bindings/clock/nuvoton,npcm7xx-clock.h>
22 struct npcm7xx_clk_pll {
28 #define to_npcm7xx_clk_pll(_hw) container_of(_hw, struct npcm7xx_clk_pll, hw)
30 #define PLLCON_LOKI BIT(31)
31 #define PLLCON_LOKS BIT(30)
32 #define PLLCON_FBDV GENMASK(27, 16)
33 #define PLLCON_OTDV2 GENMASK(15, 13)
34 #define PLLCON_PWDEN BIT(12)
35 #define PLLCON_OTDV1 GENMASK(10, 8)
36 #define PLLCON_INDV GENMASK(5, 0)
38 static unsigned long npcm7xx_clk_pll_recalc_rate(struct clk_hw *hw,
39 unsigned long parent_rate)
41 struct npcm7xx_clk_pll *pll = to_npcm7xx_clk_pll(hw);
42 unsigned long fbdv, indv, otdv1, otdv2;
46 if (parent_rate == 0) {
47 pr_err("%s: parent rate is zero", __func__);
51 val = readl_relaxed(pll->pllcon);
53 indv = FIELD_GET(PLLCON_INDV, val);
54 fbdv = FIELD_GET(PLLCON_FBDV, val);
55 otdv1 = FIELD_GET(PLLCON_OTDV1, val);
56 otdv2 = FIELD_GET(PLLCON_OTDV2, val);
58 ret = (u64)parent_rate * fbdv;
59 do_div(ret, indv * otdv1 * otdv2);
64 static const struct clk_ops npcm7xx_clk_pll_ops = {
65 .recalc_rate = npcm7xx_clk_pll_recalc_rate,
68 static struct clk_hw *
69 npcm7xx_clk_register_pll(void __iomem *pllcon, const char *name,
70 const char *parent_name, unsigned long flags)
72 struct npcm7xx_clk_pll *pll;
73 struct clk_init_data init;
77 pll = kzalloc(sizeof(*pll), GFP_KERNEL);
79 return ERR_PTR(-ENOMEM);
81 pr_debug("%s reg, name=%s, p=%s\n", __func__, name, parent_name);
84 init.ops = &npcm7xx_clk_pll_ops;
85 init.parent_names = &parent_name;
94 ret = clk_hw_register(NULL, hw);
103 #define NPCM7XX_CLKEN1 (0x00)
104 #define NPCM7XX_CLKEN2 (0x28)
105 #define NPCM7XX_CLKEN3 (0x30)
106 #define NPCM7XX_CLKSEL (0x04)
107 #define NPCM7XX_CLKDIV1 (0x08)
108 #define NPCM7XX_CLKDIV2 (0x2C)
109 #define NPCM7XX_CLKDIV3 (0x58)
110 #define NPCM7XX_PLLCON0 (0x0C)
111 #define NPCM7XX_PLLCON1 (0x10)
112 #define NPCM7XX_PLLCON2 (0x54)
113 #define NPCM7XX_SWRSTR (0x14)
114 #define NPCM7XX_IRQWAKECON (0x18)
115 #define NPCM7XX_IRQWAKEFLAG (0x1C)
116 #define NPCM7XX_IPSRST1 (0x20)
117 #define NPCM7XX_IPSRST2 (0x24)
118 #define NPCM7XX_IPSRST3 (0x34)
119 #define NPCM7XX_WD0RCR (0x38)
120 #define NPCM7XX_WD1RCR (0x3C)
121 #define NPCM7XX_WD2RCR (0x40)
122 #define NPCM7XX_SWRSTC1 (0x44)
123 #define NPCM7XX_SWRSTC2 (0x48)
124 #define NPCM7XX_SWRSTC3 (0x4C)
125 #define NPCM7XX_SWRSTC4 (0x50)
126 #define NPCM7XX_CORSTC (0x5C)
127 #define NPCM7XX_PLLCONG (0x60)
128 #define NPCM7XX_AHBCKFI (0x64)
129 #define NPCM7XX_SECCNT (0x68)
130 #define NPCM7XX_CNTR25M (0x6C)
132 struct npcm7xx_clk_gate_data {
136 const char *parent_name;
139 * If this clock is exported via DT, set onecell_idx to constant
140 * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
141 * this specific clock. Otherwise, set to -1.
146 struct npcm7xx_clk_mux_data {
151 const char * const *parent_names;
155 * If this clock is exported via DT, set onecell_idx to constant
156 * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
157 * this specific clock. Otherwise, set to -1.
163 struct npcm7xx_clk_div_fixed_data {
167 const char *parent_name;
168 u8 clk_divider_flags;
170 * If this clock is exported via DT, set onecell_idx to constant
171 * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
172 * this specific clock. Otherwise, set to -1.
178 struct npcm7xx_clk_div_data {
183 const char *parent_name;
184 u8 clk_divider_flags;
187 * If this clock is exported via DT, set onecell_idx to constant
188 * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
189 * this specific clock. Otherwise, set to -1.
194 struct npcm7xx_clk_pll_data {
197 const char *parent_name;
200 * If this clock is exported via DT, set onecell_idx to constant
201 * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
202 * this specific clock. Otherwise, set to -1.
208 * Single copy of strings used to refer to clocks within this driver indexed by
211 #define NPCM7XX_CLK_S_REFCLK "refclk"
212 #define NPCM7XX_CLK_S_SYSBYPCK "sysbypck"
213 #define NPCM7XX_CLK_S_MCBYPCK "mcbypck"
214 #define NPCM7XX_CLK_S_GFXBYPCK "gfxbypck"
215 #define NPCM7XX_CLK_S_PLL0 "pll0"
216 #define NPCM7XX_CLK_S_PLL1 "pll1"
217 #define NPCM7XX_CLK_S_PLL1_DIV2 "pll1_div2"
218 #define NPCM7XX_CLK_S_PLL2 "pll2"
219 #define NPCM7XX_CLK_S_PLL_GFX "pll_gfx"
220 #define NPCM7XX_CLK_S_PLL2_DIV2 "pll2_div2"
221 #define NPCM7XX_CLK_S_PIX_MUX "gfx_pixel"
222 #define NPCM7XX_CLK_S_GPRFSEL_MUX "gprfsel_mux"
223 #define NPCM7XX_CLK_S_MC_MUX "mc_phy"
224 #define NPCM7XX_CLK_S_CPU_MUX "cpu" /*AKA system clock.*/
225 #define NPCM7XX_CLK_S_MC "mc"
226 #define NPCM7XX_CLK_S_AXI "axi" /*AKA CLK2*/
227 #define NPCM7XX_CLK_S_AHB "ahb" /*AKA CLK4*/
228 #define NPCM7XX_CLK_S_CLKOUT_MUX "clkout_mux"
229 #define NPCM7XX_CLK_S_UART_MUX "uart_mux"
230 #define NPCM7XX_CLK_S_TIM_MUX "timer_mux"
231 #define NPCM7XX_CLK_S_SD_MUX "sd_mux"
232 #define NPCM7XX_CLK_S_GFXM_MUX "gfxm_mux"
233 #define NPCM7XX_CLK_S_SU_MUX "serial_usb_mux"
234 #define NPCM7XX_CLK_S_DVC_MUX "dvc_mux"
235 #define NPCM7XX_CLK_S_GFX_MUX "gfx_mux"
236 #define NPCM7XX_CLK_S_GFX_PIXEL "gfx_pixel"
237 #define NPCM7XX_CLK_S_SPI0 "spi0"
238 #define NPCM7XX_CLK_S_SPI3 "spi3"
239 #define NPCM7XX_CLK_S_SPIX "spix"
240 #define NPCM7XX_CLK_S_APB1 "apb1"
241 #define NPCM7XX_CLK_S_APB2 "apb2"
242 #define NPCM7XX_CLK_S_APB3 "apb3"
243 #define NPCM7XX_CLK_S_APB4 "apb4"
244 #define NPCM7XX_CLK_S_APB5 "apb5"
245 #define NPCM7XX_CLK_S_TOCK "tock"
246 #define NPCM7XX_CLK_S_CLKOUT "clkout"
247 #define NPCM7XX_CLK_S_UART "uart"
248 #define NPCM7XX_CLK_S_TIMER "timer"
249 #define NPCM7XX_CLK_S_MMC "mmc"
250 #define NPCM7XX_CLK_S_SDHC "sdhc"
251 #define NPCM7XX_CLK_S_ADC "adc"
252 #define NPCM7XX_CLK_S_GFX "gfx0_gfx1_mem"
253 #define NPCM7XX_CLK_S_USBIF "serial_usbif"
254 #define NPCM7XX_CLK_S_USB_HOST "usb_host"
255 #define NPCM7XX_CLK_S_USB_BRIDGE "usb_bridge"
256 #define NPCM7XX_CLK_S_PCI "pci"
258 static u32 pll_mux_table[] = {0, 1, 2, 3};
259 static const char * const pll_mux_parents[] __initconst = {
261 NPCM7XX_CLK_S_PLL1_DIV2,
262 NPCM7XX_CLK_S_REFCLK,
263 NPCM7XX_CLK_S_PLL2_DIV2,
266 static u32 cpuck_mux_table[] = {0, 1, 2, 3};
267 static const char * const cpuck_mux_parents[] __initconst = {
269 NPCM7XX_CLK_S_PLL1_DIV2,
270 NPCM7XX_CLK_S_REFCLK,
271 NPCM7XX_CLK_S_SYSBYPCK,
274 static u32 pixcksel_mux_table[] = {0, 2};
275 static const char * const pixcksel_mux_parents[] __initconst = {
276 NPCM7XX_CLK_S_PLL_GFX,
277 NPCM7XX_CLK_S_REFCLK,
280 static u32 sucksel_mux_table[] = {2, 3};
281 static const char * const sucksel_mux_parents[] __initconst = {
282 NPCM7XX_CLK_S_REFCLK,
283 NPCM7XX_CLK_S_PLL2_DIV2,
286 static u32 mccksel_mux_table[] = {0, 2, 3};
287 static const char * const mccksel_mux_parents[] __initconst = {
288 NPCM7XX_CLK_S_PLL1_DIV2,
289 NPCM7XX_CLK_S_REFCLK,
290 NPCM7XX_CLK_S_MCBYPCK,
293 static u32 clkoutsel_mux_table[] = {0, 1, 2, 3, 4};
294 static const char * const clkoutsel_mux_parents[] __initconst = {
296 NPCM7XX_CLK_S_PLL1_DIV2,
297 NPCM7XX_CLK_S_REFCLK,
298 NPCM7XX_CLK_S_PLL_GFX, // divided by 2
299 NPCM7XX_CLK_S_PLL2_DIV2,
302 static u32 gfxmsel_mux_table[] = {2, 3};
303 static const char * const gfxmsel_mux_parents[] __initconst = {
304 NPCM7XX_CLK_S_REFCLK,
305 NPCM7XX_CLK_S_PLL2_DIV2,
308 static u32 dvcssel_mux_table[] = {2, 3};
309 static const char * const dvcssel_mux_parents[] __initconst = {
310 NPCM7XX_CLK_S_REFCLK,
314 static const struct npcm7xx_clk_pll_data npcm7xx_plls[] __initconst = {
315 {NPCM7XX_PLLCON0, NPCM7XX_CLK_S_PLL0, NPCM7XX_CLK_S_REFCLK, 0, -1},
317 {NPCM7XX_PLLCON1, NPCM7XX_CLK_S_PLL1,
318 NPCM7XX_CLK_S_REFCLK, 0, -1},
320 {NPCM7XX_PLLCON2, NPCM7XX_CLK_S_PLL2,
321 NPCM7XX_CLK_S_REFCLK, 0, -1},
323 {NPCM7XX_PLLCONG, NPCM7XX_CLK_S_PLL_GFX,
324 NPCM7XX_CLK_S_REFCLK, 0, -1},
327 static const struct npcm7xx_clk_mux_data npcm7xx_muxes[] __initconst = {
328 {0, GENMASK(1, 0), cpuck_mux_table, NPCM7XX_CLK_S_CPU_MUX,
329 cpuck_mux_parents, ARRAY_SIZE(cpuck_mux_parents), CLK_IS_CRITICAL,
332 {4, GENMASK(1, 0), pixcksel_mux_table, NPCM7XX_CLK_S_PIX_MUX,
333 pixcksel_mux_parents, ARRAY_SIZE(pixcksel_mux_parents), 0,
334 NPCM7XX_CLK_GFX_PIXEL},
336 {6, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_SD_MUX,
337 pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
339 {8, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_UART_MUX,
340 pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
342 {10, GENMASK(1, 0), sucksel_mux_table, NPCM7XX_CLK_S_SU_MUX,
343 sucksel_mux_parents, ARRAY_SIZE(sucksel_mux_parents), 0, -1},
345 {12, GENMASK(1, 0), mccksel_mux_table, NPCM7XX_CLK_S_MC_MUX,
346 mccksel_mux_parents, ARRAY_SIZE(mccksel_mux_parents), 0, -1},
348 {14, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_TIM_MUX,
349 pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
351 {16, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_GFX_MUX,
352 pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
354 {18, GENMASK(2, 0), clkoutsel_mux_table, NPCM7XX_CLK_S_CLKOUT_MUX,
355 clkoutsel_mux_parents, ARRAY_SIZE(clkoutsel_mux_parents), 0, -1},
357 {21, GENMASK(1, 0), gfxmsel_mux_table, NPCM7XX_CLK_S_GFXM_MUX,
358 gfxmsel_mux_parents, ARRAY_SIZE(gfxmsel_mux_parents), 0, -1},
360 {23, GENMASK(1, 0), dvcssel_mux_table, NPCM7XX_CLK_S_DVC_MUX,
361 dvcssel_mux_parents, ARRAY_SIZE(dvcssel_mux_parents), 0, -1},
364 /* configurable dividers: */
365 static const struct npcm7xx_clk_div_data npcm7xx_divs[] __initconst = {
366 {NPCM7XX_CLKDIV1, 28, 3, NPCM7XX_CLK_S_ADC,
367 NPCM7XX_CLK_S_TIMER, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_ADC},
369 {NPCM7XX_CLKDIV1, 26, 2, NPCM7XX_CLK_S_AHB,
370 NPCM7XX_CLK_S_AXI, 0, CLK_IS_CRITICAL, NPCM7XX_CLK_AHB},
372 {NPCM7XX_CLKDIV1, 21, 5, NPCM7XX_CLK_S_TIMER,
373 NPCM7XX_CLK_S_TIM_MUX, 0, 0, NPCM7XX_CLK_TIMER},
375 {NPCM7XX_CLKDIV1, 16, 5, NPCM7XX_CLK_S_UART,
376 NPCM7XX_CLK_S_UART_MUX, 0, 0, NPCM7XX_CLK_UART},
378 {NPCM7XX_CLKDIV1, 11, 5, NPCM7XX_CLK_S_MMC,
379 NPCM7XX_CLK_S_SD_MUX, 0, 0, NPCM7XX_CLK_MMC},
381 {NPCM7XX_CLKDIV1, 6, 5, NPCM7XX_CLK_S_SPI3,
382 NPCM7XX_CLK_S_AHB, 0, 0, NPCM7XX_CLK_SPI3},
384 {NPCM7XX_CLKDIV1, 2, 4, NPCM7XX_CLK_S_PCI,
385 NPCM7XX_CLK_S_GFX_MUX, 0, 0, NPCM7XX_CLK_PCI},
387 {NPCM7XX_CLKDIV1, 0, 1, NPCM7XX_CLK_S_AXI,
388 NPCM7XX_CLK_S_CPU_MUX, CLK_DIVIDER_POWER_OF_TWO, CLK_IS_CRITICAL,
389 NPCM7XX_CLK_AXI},/*0 CLK2DIV*/
391 {NPCM7XX_CLKDIV2, 30, 2, NPCM7XX_CLK_S_APB4,
392 NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB4},
394 {NPCM7XX_CLKDIV2, 28, 2, NPCM7XX_CLK_S_APB3,
395 NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB3},
397 {NPCM7XX_CLKDIV2, 26, 2, NPCM7XX_CLK_S_APB2,
398 NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB2},
400 {NPCM7XX_CLKDIV2, 24, 2, NPCM7XX_CLK_S_APB1,
401 NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB1},
403 {NPCM7XX_CLKDIV2, 22, 2, NPCM7XX_CLK_S_APB5,
404 NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB5},
406 {NPCM7XX_CLKDIV2, 16, 5, NPCM7XX_CLK_S_CLKOUT,
407 NPCM7XX_CLK_S_CLKOUT_MUX, 0, 0, NPCM7XX_CLK_CLKOUT},
409 {NPCM7XX_CLKDIV2, 13, 3, NPCM7XX_CLK_S_GFX,
410 NPCM7XX_CLK_S_GFX_MUX, 0, 0, NPCM7XX_CLK_GFX},
412 {NPCM7XX_CLKDIV2, 8, 5, NPCM7XX_CLK_S_USB_BRIDGE,
413 NPCM7XX_CLK_S_SU_MUX, 0, 0, NPCM7XX_CLK_SU},
415 {NPCM7XX_CLKDIV2, 4, 4, NPCM7XX_CLK_S_USB_HOST,
416 NPCM7XX_CLK_S_SU_MUX, 0, 0, NPCM7XX_CLK_SU48},
418 {NPCM7XX_CLKDIV2, 0, 4, NPCM7XX_CLK_S_SDHC,
419 NPCM7XX_CLK_S_SD_MUX, 0, 0, NPCM7XX_CLK_SDHC}
422 {NPCM7XX_CLKDIV3, 6, 5, NPCM7XX_CLK_S_SPI0,
423 NPCM7XX_CLK_S_AHB, 0, 0, NPCM7XX_CLK_SPI0},
425 {NPCM7XX_CLKDIV3, 1, 5, NPCM7XX_CLK_S_SPIX,
426 NPCM7XX_CLK_S_AHB, 0, 0, NPCM7XX_CLK_SPIX},
431 static DEFINE_SPINLOCK(npcm7xx_clk_lock);
433 static void __init npcm7xx_clk_init(struct device_node *clk_np)
435 struct clk_hw_onecell_data *npcm7xx_clk_data;
436 void __iomem *clk_base;
442 ret = of_address_to_resource(clk_np, 0, &res);
444 pr_err("%pOFn: failed to get resource, ret %d\n", clk_np,
449 clk_base = ioremap(res.start, resource_size(&res));
451 goto npcm7xx_init_error;
453 npcm7xx_clk_data = kzalloc(struct_size(npcm7xx_clk_data, hws,
454 NPCM7XX_NUM_CLOCKS), GFP_KERNEL);
455 if (!npcm7xx_clk_data)
456 goto npcm7xx_init_np_err;
458 npcm7xx_clk_data->num = NPCM7XX_NUM_CLOCKS;
460 for (i = 0; i < NPCM7XX_NUM_CLOCKS; i++)
461 npcm7xx_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
464 for (i = 0; i < ARRAY_SIZE(npcm7xx_plls); i++) {
465 const struct npcm7xx_clk_pll_data *pll_data = &npcm7xx_plls[i];
467 hw = npcm7xx_clk_register_pll(clk_base + pll_data->reg,
468 pll_data->name, pll_data->parent_name, pll_data->flags);
470 pr_err("npcm7xx_clk: Can't register pll\n");
471 goto npcm7xx_init_fail;
474 if (pll_data->onecell_idx >= 0)
475 npcm7xx_clk_data->hws[pll_data->onecell_idx] = hw;
478 /* Register fixed dividers */
479 hw = clk_hw_register_fixed_factor(NULL, NPCM7XX_CLK_S_PLL1_DIV2,
480 NPCM7XX_CLK_S_PLL1, 0, 1, 2);
482 pr_err("npcm7xx_clk: Can't register fixed div\n");
483 goto npcm7xx_init_fail;
486 hw = clk_hw_register_fixed_factor(NULL, NPCM7XX_CLK_S_PLL2_DIV2,
487 NPCM7XX_CLK_S_PLL2, 0, 1, 2);
489 pr_err("npcm7xx_clk: Can't register div2\n");
490 goto npcm7xx_init_fail;
494 for (i = 0; i < ARRAY_SIZE(npcm7xx_muxes); i++) {
495 const struct npcm7xx_clk_mux_data *mux_data = &npcm7xx_muxes[i];
497 hw = clk_hw_register_mux_table(NULL,
499 mux_data->parent_names, mux_data->num_parents,
500 mux_data->flags, clk_base + NPCM7XX_CLKSEL,
501 mux_data->shift, mux_data->mask, 0,
502 mux_data->table, &npcm7xx_clk_lock);
505 pr_err("npcm7xx_clk: Can't register mux\n");
506 goto npcm7xx_init_fail;
509 if (mux_data->onecell_idx >= 0)
510 npcm7xx_clk_data->hws[mux_data->onecell_idx] = hw;
513 /* Register clock dividers specified in npcm7xx_divs */
514 for (i = 0; i < ARRAY_SIZE(npcm7xx_divs); i++) {
515 const struct npcm7xx_clk_div_data *div_data = &npcm7xx_divs[i];
517 hw = clk_hw_register_divider(NULL, div_data->name,
518 div_data->parent_name,
520 clk_base + div_data->reg,
521 div_data->shift, div_data->width,
522 div_data->clk_divider_flags, &npcm7xx_clk_lock);
524 pr_err("npcm7xx_clk: Can't register div table\n");
525 goto npcm7xx_init_fail;
528 if (div_data->onecell_idx >= 0)
529 npcm7xx_clk_data->hws[div_data->onecell_idx] = hw;
532 ret = of_clk_add_hw_provider(clk_np, of_clk_hw_onecell_get,
535 pr_err("failed to add DT provider: %d\n", ret);
542 kfree(npcm7xx_clk_data->hws);
548 CLK_OF_DECLARE(npcm7xx_clk_init, "nuvoton,npcm750-clk", npcm7xx_clk_init);