1fcfd1436STali Perry // SPDX-License-Identifier: GPL-2.0
2fcfd1436STali Perry /*
3fcfd1436STali Perry * Nuvoton NPCM7xx Clock Generator
4fcfd1436STali Perry * All the clocks are initialized by the bootloader, so this driver allow only
5fcfd1436STali Perry * reading of current settings directly from the hardware.
6fcfd1436STali Perry *
7fcfd1436STali Perry * Copyright (C) 2018 Nuvoton Technologies tali.perry@nuvoton.com
8fcfd1436STali Perry */
9fcfd1436STali Perry
10fcfd1436STali Perry #include <linux/module.h>
11fcfd1436STali Perry #include <linux/clk-provider.h>
12fcfd1436STali Perry #include <linux/io.h>
13fcfd1436STali Perry #include <linux/kernel.h>
14fcfd1436STali Perry #include <linux/of.h>
15fcfd1436STali Perry #include <linux/of_address.h>
16fcfd1436STali Perry #include <linux/slab.h>
17fcfd1436STali Perry #include <linux/err.h>
18fcfd1436STali Perry #include <linux/bitfield.h>
19fcfd1436STali Perry
20fcfd1436STali Perry #include <dt-bindings/clock/nuvoton,npcm7xx-clock.h>
21fcfd1436STali Perry
22fcfd1436STali Perry struct npcm7xx_clk_pll {
23fcfd1436STali Perry struct clk_hw hw;
24fcfd1436STali Perry void __iomem *pllcon;
25fcfd1436STali Perry u8 flags;
26fcfd1436STali Perry };
27fcfd1436STali Perry
28fcfd1436STali Perry #define to_npcm7xx_clk_pll(_hw) container_of(_hw, struct npcm7xx_clk_pll, hw)
29fcfd1436STali Perry
30fcfd1436STali Perry #define PLLCON_LOKI BIT(31)
31fcfd1436STali Perry #define PLLCON_LOKS BIT(30)
32fcfd1436STali Perry #define PLLCON_FBDV GENMASK(27, 16)
33fcfd1436STali Perry #define PLLCON_OTDV2 GENMASK(15, 13)
34fcfd1436STali Perry #define PLLCON_PWDEN BIT(12)
35fcfd1436STali Perry #define PLLCON_OTDV1 GENMASK(10, 8)
36fcfd1436STali Perry #define PLLCON_INDV GENMASK(5, 0)
37fcfd1436STali Perry
npcm7xx_clk_pll_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)38fcfd1436STali Perry static unsigned long npcm7xx_clk_pll_recalc_rate(struct clk_hw *hw,
39fcfd1436STali Perry unsigned long parent_rate)
40fcfd1436STali Perry {
41fcfd1436STali Perry struct npcm7xx_clk_pll *pll = to_npcm7xx_clk_pll(hw);
42fcfd1436STali Perry unsigned long fbdv, indv, otdv1, otdv2;
43fcfd1436STali Perry unsigned int val;
44fcfd1436STali Perry u64 ret;
45fcfd1436STali Perry
46fcfd1436STali Perry if (parent_rate == 0) {
47fcfd1436STali Perry pr_err("%s: parent rate is zero", __func__);
48fcfd1436STali Perry return 0;
49fcfd1436STali Perry }
50fcfd1436STali Perry
51fcfd1436STali Perry val = readl_relaxed(pll->pllcon);
52fcfd1436STali Perry
53fcfd1436STali Perry indv = FIELD_GET(PLLCON_INDV, val);
54fcfd1436STali Perry fbdv = FIELD_GET(PLLCON_FBDV, val);
55fcfd1436STali Perry otdv1 = FIELD_GET(PLLCON_OTDV1, val);
56fcfd1436STali Perry otdv2 = FIELD_GET(PLLCON_OTDV2, val);
57fcfd1436STali Perry
58fcfd1436STali Perry ret = (u64)parent_rate * fbdv;
59fcfd1436STali Perry do_div(ret, indv * otdv1 * otdv2);
60fcfd1436STali Perry
61fcfd1436STali Perry return ret;
62fcfd1436STali Perry }
63fcfd1436STali Perry
64fcfd1436STali Perry static const struct clk_ops npcm7xx_clk_pll_ops = {
65fcfd1436STali Perry .recalc_rate = npcm7xx_clk_pll_recalc_rate,
66fcfd1436STali Perry };
67fcfd1436STali Perry
68fcfd1436STali Perry static struct clk_hw *
npcm7xx_clk_register_pll(void __iomem * pllcon,const char * name,const char * parent_name,unsigned long flags)69fcfd1436STali Perry npcm7xx_clk_register_pll(void __iomem *pllcon, const char *name,
70fcfd1436STali Perry const char *parent_name, unsigned long flags)
71fcfd1436STali Perry {
72fcfd1436STali Perry struct npcm7xx_clk_pll *pll;
73fcfd1436STali Perry struct clk_init_data init;
74fcfd1436STali Perry struct clk_hw *hw;
75fcfd1436STali Perry int ret;
76fcfd1436STali Perry
77fcfd1436STali Perry pll = kzalloc(sizeof(*pll), GFP_KERNEL);
78fcfd1436STali Perry if (!pll)
79fcfd1436STali Perry return ERR_PTR(-ENOMEM);
80fcfd1436STali Perry
81fcfd1436STali Perry pr_debug("%s reg, name=%s, p=%s\n", __func__, name, parent_name);
82fcfd1436STali Perry
83fcfd1436STali Perry init.name = name;
84fcfd1436STali Perry init.ops = &npcm7xx_clk_pll_ops;
85fcfd1436STali Perry init.parent_names = &parent_name;
86fcfd1436STali Perry init.num_parents = 1;
87fcfd1436STali Perry init.flags = flags;
88fcfd1436STali Perry
89fcfd1436STali Perry pll->pllcon = pllcon;
90fcfd1436STali Perry pll->hw.init = &init;
91fcfd1436STali Perry
92fcfd1436STali Perry hw = &pll->hw;
93fcfd1436STali Perry
94fcfd1436STali Perry ret = clk_hw_register(NULL, hw);
95fcfd1436STali Perry if (ret) {
96fcfd1436STali Perry kfree(pll);
97fcfd1436STali Perry hw = ERR_PTR(ret);
98fcfd1436STali Perry }
99fcfd1436STali Perry
100fcfd1436STali Perry return hw;
101fcfd1436STali Perry }
102fcfd1436STali Perry
103fcfd1436STali Perry #define NPCM7XX_CLKEN1 (0x00)
104fcfd1436STali Perry #define NPCM7XX_CLKEN2 (0x28)
105fcfd1436STali Perry #define NPCM7XX_CLKEN3 (0x30)
106fcfd1436STali Perry #define NPCM7XX_CLKSEL (0x04)
107fcfd1436STali Perry #define NPCM7XX_CLKDIV1 (0x08)
108fcfd1436STali Perry #define NPCM7XX_CLKDIV2 (0x2C)
109fcfd1436STali Perry #define NPCM7XX_CLKDIV3 (0x58)
110fcfd1436STali Perry #define NPCM7XX_PLLCON0 (0x0C)
111fcfd1436STali Perry #define NPCM7XX_PLLCON1 (0x10)
112fcfd1436STali Perry #define NPCM7XX_PLLCON2 (0x54)
113fcfd1436STali Perry #define NPCM7XX_SWRSTR (0x14)
114fcfd1436STali Perry #define NPCM7XX_IRQWAKECON (0x18)
115fcfd1436STali Perry #define NPCM7XX_IRQWAKEFLAG (0x1C)
116fcfd1436STali Perry #define NPCM7XX_IPSRST1 (0x20)
117fcfd1436STali Perry #define NPCM7XX_IPSRST2 (0x24)
118fcfd1436STali Perry #define NPCM7XX_IPSRST3 (0x34)
119fcfd1436STali Perry #define NPCM7XX_WD0RCR (0x38)
120fcfd1436STali Perry #define NPCM7XX_WD1RCR (0x3C)
121fcfd1436STali Perry #define NPCM7XX_WD2RCR (0x40)
122fcfd1436STali Perry #define NPCM7XX_SWRSTC1 (0x44)
123fcfd1436STali Perry #define NPCM7XX_SWRSTC2 (0x48)
124fcfd1436STali Perry #define NPCM7XX_SWRSTC3 (0x4C)
125fcfd1436STali Perry #define NPCM7XX_SWRSTC4 (0x50)
126fcfd1436STali Perry #define NPCM7XX_CORSTC (0x5C)
127fcfd1436STali Perry #define NPCM7XX_PLLCONG (0x60)
128fcfd1436STali Perry #define NPCM7XX_AHBCKFI (0x64)
129fcfd1436STali Perry #define NPCM7XX_SECCNT (0x68)
130fcfd1436STali Perry #define NPCM7XX_CNTR25M (0x6C)
131fcfd1436STali Perry
132fcfd1436STali Perry struct npcm7xx_clk_mux_data {
133fcfd1436STali Perry u8 shift;
134fcfd1436STali Perry u8 mask;
135fcfd1436STali Perry u32 *table;
136fcfd1436STali Perry const char *name;
137fcfd1436STali Perry const char * const *parent_names;
138fcfd1436STali Perry u8 num_parents;
139fcfd1436STali Perry unsigned long flags;
140fcfd1436STali Perry /*
141fcfd1436STali Perry * If this clock is exported via DT, set onecell_idx to constant
142fcfd1436STali Perry * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
143fcfd1436STali Perry * this specific clock. Otherwise, set to -1.
144fcfd1436STali Perry */
145fcfd1436STali Perry int onecell_idx;
146fcfd1436STali Perry
147fcfd1436STali Perry };
148fcfd1436STali Perry
149fcfd1436STali Perry struct npcm7xx_clk_div_data {
150fcfd1436STali Perry u32 reg;
151fcfd1436STali Perry u8 shift;
152fcfd1436STali Perry u8 width;
153fcfd1436STali Perry const char *name;
154fcfd1436STali Perry const char *parent_name;
155fcfd1436STali Perry u8 clk_divider_flags;
156fcfd1436STali Perry unsigned long flags;
157fcfd1436STali Perry /*
158fcfd1436STali Perry * If this clock is exported via DT, set onecell_idx to constant
159fcfd1436STali Perry * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
160fcfd1436STali Perry * this specific clock. Otherwise, set to -1.
161fcfd1436STali Perry */
162fcfd1436STali Perry int onecell_idx;
163fcfd1436STali Perry };
164fcfd1436STali Perry
165fcfd1436STali Perry struct npcm7xx_clk_pll_data {
166fcfd1436STali Perry u32 reg;
167fcfd1436STali Perry const char *name;
168fcfd1436STali Perry const char *parent_name;
169fcfd1436STali Perry unsigned long flags;
170fcfd1436STali Perry /*
171fcfd1436STali Perry * If this clock is exported via DT, set onecell_idx to constant
172fcfd1436STali Perry * defined in include/dt-bindings/clock/nuvoton, NPCM7XX-clock.h for
173fcfd1436STali Perry * this specific clock. Otherwise, set to -1.
174fcfd1436STali Perry */
175fcfd1436STali Perry int onecell_idx;
176fcfd1436STali Perry };
177fcfd1436STali Perry
178fcfd1436STali Perry /*
179fcfd1436STali Perry * Single copy of strings used to refer to clocks within this driver indexed by
180fcfd1436STali Perry * above enum.
181fcfd1436STali Perry */
182fcfd1436STali Perry #define NPCM7XX_CLK_S_REFCLK "refclk"
183fcfd1436STali Perry #define NPCM7XX_CLK_S_SYSBYPCK "sysbypck"
184fcfd1436STali Perry #define NPCM7XX_CLK_S_MCBYPCK "mcbypck"
185fcfd1436STali Perry #define NPCM7XX_CLK_S_GFXBYPCK "gfxbypck"
186fcfd1436STali Perry #define NPCM7XX_CLK_S_PLL0 "pll0"
187fcfd1436STali Perry #define NPCM7XX_CLK_S_PLL1 "pll1"
188fcfd1436STali Perry #define NPCM7XX_CLK_S_PLL1_DIV2 "pll1_div2"
189fcfd1436STali Perry #define NPCM7XX_CLK_S_PLL2 "pll2"
190fcfd1436STali Perry #define NPCM7XX_CLK_S_PLL_GFX "pll_gfx"
191fcfd1436STali Perry #define NPCM7XX_CLK_S_PLL2_DIV2 "pll2_div2"
192fcfd1436STali Perry #define NPCM7XX_CLK_S_PIX_MUX "gfx_pixel"
193fcfd1436STali Perry #define NPCM7XX_CLK_S_GPRFSEL_MUX "gprfsel_mux"
194fcfd1436STali Perry #define NPCM7XX_CLK_S_MC_MUX "mc_phy"
195fcfd1436STali Perry #define NPCM7XX_CLK_S_CPU_MUX "cpu" /*AKA system clock.*/
196fcfd1436STali Perry #define NPCM7XX_CLK_S_MC "mc"
197fcfd1436STali Perry #define NPCM7XX_CLK_S_AXI "axi" /*AKA CLK2*/
198fcfd1436STali Perry #define NPCM7XX_CLK_S_AHB "ahb" /*AKA CLK4*/
199fcfd1436STali Perry #define NPCM7XX_CLK_S_CLKOUT_MUX "clkout_mux"
200fcfd1436STali Perry #define NPCM7XX_CLK_S_UART_MUX "uart_mux"
201fcfd1436STali Perry #define NPCM7XX_CLK_S_TIM_MUX "timer_mux"
202fcfd1436STali Perry #define NPCM7XX_CLK_S_SD_MUX "sd_mux"
203fcfd1436STali Perry #define NPCM7XX_CLK_S_GFXM_MUX "gfxm_mux"
204fcfd1436STali Perry #define NPCM7XX_CLK_S_SU_MUX "serial_usb_mux"
205fcfd1436STali Perry #define NPCM7XX_CLK_S_DVC_MUX "dvc_mux"
206fcfd1436STali Perry #define NPCM7XX_CLK_S_GFX_MUX "gfx_mux"
207fcfd1436STali Perry #define NPCM7XX_CLK_S_GFX_PIXEL "gfx_pixel"
208fcfd1436STali Perry #define NPCM7XX_CLK_S_SPI0 "spi0"
209fcfd1436STali Perry #define NPCM7XX_CLK_S_SPI3 "spi3"
210fcfd1436STali Perry #define NPCM7XX_CLK_S_SPIX "spix"
211fcfd1436STali Perry #define NPCM7XX_CLK_S_APB1 "apb1"
212fcfd1436STali Perry #define NPCM7XX_CLK_S_APB2 "apb2"
213fcfd1436STali Perry #define NPCM7XX_CLK_S_APB3 "apb3"
214fcfd1436STali Perry #define NPCM7XX_CLK_S_APB4 "apb4"
215fcfd1436STali Perry #define NPCM7XX_CLK_S_APB5 "apb5"
216fcfd1436STali Perry #define NPCM7XX_CLK_S_TOCK "tock"
217fcfd1436STali Perry #define NPCM7XX_CLK_S_CLKOUT "clkout"
218fcfd1436STali Perry #define NPCM7XX_CLK_S_UART "uart"
219fcfd1436STali Perry #define NPCM7XX_CLK_S_TIMER "timer"
220fcfd1436STali Perry #define NPCM7XX_CLK_S_MMC "mmc"
221fcfd1436STali Perry #define NPCM7XX_CLK_S_SDHC "sdhc"
222fcfd1436STali Perry #define NPCM7XX_CLK_S_ADC "adc"
223fcfd1436STali Perry #define NPCM7XX_CLK_S_GFX "gfx0_gfx1_mem"
224fcfd1436STali Perry #define NPCM7XX_CLK_S_USBIF "serial_usbif"
225fcfd1436STali Perry #define NPCM7XX_CLK_S_USB_HOST "usb_host"
226fcfd1436STali Perry #define NPCM7XX_CLK_S_USB_BRIDGE "usb_bridge"
227fcfd1436STali Perry #define NPCM7XX_CLK_S_PCI "pci"
228fcfd1436STali Perry
229fcfd1436STali Perry static u32 pll_mux_table[] = {0, 1, 2, 3};
230fcfd1436STali Perry static const char * const pll_mux_parents[] __initconst = {
231fcfd1436STali Perry NPCM7XX_CLK_S_PLL0,
232fcfd1436STali Perry NPCM7XX_CLK_S_PLL1_DIV2,
233fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
234fcfd1436STali Perry NPCM7XX_CLK_S_PLL2_DIV2,
235fcfd1436STali Perry };
236fcfd1436STali Perry
237fcfd1436STali Perry static u32 cpuck_mux_table[] = {0, 1, 2, 3};
238fcfd1436STali Perry static const char * const cpuck_mux_parents[] __initconst = {
239fcfd1436STali Perry NPCM7XX_CLK_S_PLL0,
240fcfd1436STali Perry NPCM7XX_CLK_S_PLL1_DIV2,
241fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
242fcfd1436STali Perry NPCM7XX_CLK_S_SYSBYPCK,
243fcfd1436STali Perry };
244fcfd1436STali Perry
245fcfd1436STali Perry static u32 pixcksel_mux_table[] = {0, 2};
246fcfd1436STali Perry static const char * const pixcksel_mux_parents[] __initconst = {
247fcfd1436STali Perry NPCM7XX_CLK_S_PLL_GFX,
248fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
249fcfd1436STali Perry };
250fcfd1436STali Perry
251fcfd1436STali Perry static u32 sucksel_mux_table[] = {2, 3};
252fcfd1436STali Perry static const char * const sucksel_mux_parents[] __initconst = {
253fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
254fcfd1436STali Perry NPCM7XX_CLK_S_PLL2_DIV2,
255fcfd1436STali Perry };
256fcfd1436STali Perry
257fcfd1436STali Perry static u32 mccksel_mux_table[] = {0, 2, 3};
258fcfd1436STali Perry static const char * const mccksel_mux_parents[] __initconst = {
259fcfd1436STali Perry NPCM7XX_CLK_S_PLL1_DIV2,
260fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
261fcfd1436STali Perry NPCM7XX_CLK_S_MCBYPCK,
262fcfd1436STali Perry };
263fcfd1436STali Perry
264fcfd1436STali Perry static u32 clkoutsel_mux_table[] = {0, 1, 2, 3, 4};
265fcfd1436STali Perry static const char * const clkoutsel_mux_parents[] __initconst = {
266fcfd1436STali Perry NPCM7XX_CLK_S_PLL0,
267fcfd1436STali Perry NPCM7XX_CLK_S_PLL1_DIV2,
268fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
269fcfd1436STali Perry NPCM7XX_CLK_S_PLL_GFX, // divided by 2
270fcfd1436STali Perry NPCM7XX_CLK_S_PLL2_DIV2,
271fcfd1436STali Perry };
272fcfd1436STali Perry
273fcfd1436STali Perry static u32 gfxmsel_mux_table[] = {2, 3};
274fcfd1436STali Perry static const char * const gfxmsel_mux_parents[] __initconst = {
275fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
276fcfd1436STali Perry NPCM7XX_CLK_S_PLL2_DIV2,
277fcfd1436STali Perry };
278fcfd1436STali Perry
279fcfd1436STali Perry static u32 dvcssel_mux_table[] = {2, 3};
280fcfd1436STali Perry static const char * const dvcssel_mux_parents[] __initconst = {
281fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK,
282fcfd1436STali Perry NPCM7XX_CLK_S_PLL2,
283fcfd1436STali Perry };
284fcfd1436STali Perry
285fcfd1436STali Perry static const struct npcm7xx_clk_pll_data npcm7xx_plls[] __initconst = {
286fcfd1436STali Perry {NPCM7XX_PLLCON0, NPCM7XX_CLK_S_PLL0, NPCM7XX_CLK_S_REFCLK, 0, -1},
287fcfd1436STali Perry
288fcfd1436STali Perry {NPCM7XX_PLLCON1, NPCM7XX_CLK_S_PLL1,
289fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK, 0, -1},
290fcfd1436STali Perry
291fcfd1436STali Perry {NPCM7XX_PLLCON2, NPCM7XX_CLK_S_PLL2,
292fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK, 0, -1},
293fcfd1436STali Perry
294fcfd1436STali Perry {NPCM7XX_PLLCONG, NPCM7XX_CLK_S_PLL_GFX,
295fcfd1436STali Perry NPCM7XX_CLK_S_REFCLK, 0, -1},
296fcfd1436STali Perry };
297fcfd1436STali Perry
298fcfd1436STali Perry static const struct npcm7xx_clk_mux_data npcm7xx_muxes[] __initconst = {
299fcfd1436STali Perry {0, GENMASK(1, 0), cpuck_mux_table, NPCM7XX_CLK_S_CPU_MUX,
300fcfd1436STali Perry cpuck_mux_parents, ARRAY_SIZE(cpuck_mux_parents), CLK_IS_CRITICAL,
301fcfd1436STali Perry NPCM7XX_CLK_CPU},
302fcfd1436STali Perry
303fcfd1436STali Perry {4, GENMASK(1, 0), pixcksel_mux_table, NPCM7XX_CLK_S_PIX_MUX,
304fcfd1436STali Perry pixcksel_mux_parents, ARRAY_SIZE(pixcksel_mux_parents), 0,
305fcfd1436STali Perry NPCM7XX_CLK_GFX_PIXEL},
306fcfd1436STali Perry
307fcfd1436STali Perry {6, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_SD_MUX,
308fcfd1436STali Perry pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
309fcfd1436STali Perry
310fcfd1436STali Perry {8, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_UART_MUX,
311fcfd1436STali Perry pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
312fcfd1436STali Perry
313fcfd1436STali Perry {10, GENMASK(1, 0), sucksel_mux_table, NPCM7XX_CLK_S_SU_MUX,
314fcfd1436STali Perry sucksel_mux_parents, ARRAY_SIZE(sucksel_mux_parents), 0, -1},
315fcfd1436STali Perry
316fcfd1436STali Perry {12, GENMASK(1, 0), mccksel_mux_table, NPCM7XX_CLK_S_MC_MUX,
317fcfd1436STali Perry mccksel_mux_parents, ARRAY_SIZE(mccksel_mux_parents), 0, -1},
318fcfd1436STali Perry
319fcfd1436STali Perry {14, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_TIM_MUX,
320fcfd1436STali Perry pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
321fcfd1436STali Perry
322fcfd1436STali Perry {16, GENMASK(1, 0), pll_mux_table, NPCM7XX_CLK_S_GFX_MUX,
323fcfd1436STali Perry pll_mux_parents, ARRAY_SIZE(pll_mux_parents), 0, -1},
324fcfd1436STali Perry
325fcfd1436STali Perry {18, GENMASK(2, 0), clkoutsel_mux_table, NPCM7XX_CLK_S_CLKOUT_MUX,
326fcfd1436STali Perry clkoutsel_mux_parents, ARRAY_SIZE(clkoutsel_mux_parents), 0, -1},
327fcfd1436STali Perry
328fcfd1436STali Perry {21, GENMASK(1, 0), gfxmsel_mux_table, NPCM7XX_CLK_S_GFXM_MUX,
329fcfd1436STali Perry gfxmsel_mux_parents, ARRAY_SIZE(gfxmsel_mux_parents), 0, -1},
330fcfd1436STali Perry
331fcfd1436STali Perry {23, GENMASK(1, 0), dvcssel_mux_table, NPCM7XX_CLK_S_DVC_MUX,
332fcfd1436STali Perry dvcssel_mux_parents, ARRAY_SIZE(dvcssel_mux_parents), 0, -1},
333fcfd1436STali Perry };
334fcfd1436STali Perry
335fcfd1436STali Perry /* configurable dividers: */
336fcfd1436STali Perry static const struct npcm7xx_clk_div_data npcm7xx_divs[] __initconst = {
337fcfd1436STali Perry {NPCM7XX_CLKDIV1, 28, 3, NPCM7XX_CLK_S_ADC,
338fcfd1436STali Perry NPCM7XX_CLK_S_TIMER, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_ADC},
339fcfd1436STali Perry /*30-28 ADCCKDIV*/
340fcfd1436STali Perry {NPCM7XX_CLKDIV1, 26, 2, NPCM7XX_CLK_S_AHB,
341fcfd1436STali Perry NPCM7XX_CLK_S_AXI, 0, CLK_IS_CRITICAL, NPCM7XX_CLK_AHB},
342fcfd1436STali Perry /*27-26 CLK4DIV*/
343fcfd1436STali Perry {NPCM7XX_CLKDIV1, 21, 5, NPCM7XX_CLK_S_TIMER,
344fcfd1436STali Perry NPCM7XX_CLK_S_TIM_MUX, 0, 0, NPCM7XX_CLK_TIMER},
345fcfd1436STali Perry /*25-21 TIMCKDIV*/
346fcfd1436STali Perry {NPCM7XX_CLKDIV1, 16, 5, NPCM7XX_CLK_S_UART,
347fcfd1436STali Perry NPCM7XX_CLK_S_UART_MUX, 0, 0, NPCM7XX_CLK_UART},
348fcfd1436STali Perry /*20-16 UARTDIV*/
349fcfd1436STali Perry {NPCM7XX_CLKDIV1, 11, 5, NPCM7XX_CLK_S_MMC,
350fcfd1436STali Perry NPCM7XX_CLK_S_SD_MUX, 0, 0, NPCM7XX_CLK_MMC},
351fcfd1436STali Perry /*15-11 MMCCKDIV*/
352fcfd1436STali Perry {NPCM7XX_CLKDIV1, 6, 5, NPCM7XX_CLK_S_SPI3,
353fcfd1436STali Perry NPCM7XX_CLK_S_AHB, 0, 0, NPCM7XX_CLK_SPI3},
354fcfd1436STali Perry /*10-6 AHB3CKDIV*/
355fcfd1436STali Perry {NPCM7XX_CLKDIV1, 2, 4, NPCM7XX_CLK_S_PCI,
356fcfd1436STali Perry NPCM7XX_CLK_S_GFX_MUX, 0, 0, NPCM7XX_CLK_PCI},
357fcfd1436STali Perry /*5-2 PCICKDIV*/
358fcfd1436STali Perry {NPCM7XX_CLKDIV1, 0, 1, NPCM7XX_CLK_S_AXI,
359fcfd1436STali Perry NPCM7XX_CLK_S_CPU_MUX, CLK_DIVIDER_POWER_OF_TWO, CLK_IS_CRITICAL,
360fcfd1436STali Perry NPCM7XX_CLK_AXI},/*0 CLK2DIV*/
361fcfd1436STali Perry
362fcfd1436STali Perry {NPCM7XX_CLKDIV2, 30, 2, NPCM7XX_CLK_S_APB4,
363fcfd1436STali Perry NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB4},
364fcfd1436STali Perry /*31-30 APB4CKDIV*/
365fcfd1436STali Perry {NPCM7XX_CLKDIV2, 28, 2, NPCM7XX_CLK_S_APB3,
366fcfd1436STali Perry NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB3},
367fcfd1436STali Perry /*29-28 APB3CKDIV*/
368fcfd1436STali Perry {NPCM7XX_CLKDIV2, 26, 2, NPCM7XX_CLK_S_APB2,
369fcfd1436STali Perry NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB2},
370fcfd1436STali Perry /*27-26 APB2CKDIV*/
371fcfd1436STali Perry {NPCM7XX_CLKDIV2, 24, 2, NPCM7XX_CLK_S_APB1,
372fcfd1436STali Perry NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB1},
373fcfd1436STali Perry /*25-24 APB1CKDIV*/
374fcfd1436STali Perry {NPCM7XX_CLKDIV2, 22, 2, NPCM7XX_CLK_S_APB5,
375fcfd1436STali Perry NPCM7XX_CLK_S_AHB, CLK_DIVIDER_POWER_OF_TWO, 0, NPCM7XX_CLK_APB5},
376fcfd1436STali Perry /*23-22 APB5CKDIV*/
377fcfd1436STali Perry {NPCM7XX_CLKDIV2, 16, 5, NPCM7XX_CLK_S_CLKOUT,
378fcfd1436STali Perry NPCM7XX_CLK_S_CLKOUT_MUX, 0, 0, NPCM7XX_CLK_CLKOUT},
379fcfd1436STali Perry /*20-16 CLKOUTDIV*/
380fcfd1436STali Perry {NPCM7XX_CLKDIV2, 13, 3, NPCM7XX_CLK_S_GFX,
381fcfd1436STali Perry NPCM7XX_CLK_S_GFX_MUX, 0, 0, NPCM7XX_CLK_GFX},
382fcfd1436STali Perry /*15-13 GFXCKDIV*/
383fcfd1436STali Perry {NPCM7XX_CLKDIV2, 8, 5, NPCM7XX_CLK_S_USB_BRIDGE,
384fcfd1436STali Perry NPCM7XX_CLK_S_SU_MUX, 0, 0, NPCM7XX_CLK_SU},
385fcfd1436STali Perry /*12-8 SUCKDIV*/
386fcfd1436STali Perry {NPCM7XX_CLKDIV2, 4, 4, NPCM7XX_CLK_S_USB_HOST,
387fcfd1436STali Perry NPCM7XX_CLK_S_SU_MUX, 0, 0, NPCM7XX_CLK_SU48},
388fcfd1436STali Perry /*7-4 SU48CKDIV*/
389fcfd1436STali Perry {NPCM7XX_CLKDIV2, 0, 4, NPCM7XX_CLK_S_SDHC,
390fcfd1436STali Perry NPCM7XX_CLK_S_SD_MUX, 0, 0, NPCM7XX_CLK_SDHC}
391fcfd1436STali Perry ,/*3-0 SD1CKDIV*/
392fcfd1436STali Perry
393fcfd1436STali Perry {NPCM7XX_CLKDIV3, 6, 5, NPCM7XX_CLK_S_SPI0,
394fcfd1436STali Perry NPCM7XX_CLK_S_AHB, 0, 0, NPCM7XX_CLK_SPI0},
395fcfd1436STali Perry /*10-6 SPI0CKDV*/
396fcfd1436STali Perry {NPCM7XX_CLKDIV3, 1, 5, NPCM7XX_CLK_S_SPIX,
397fcfd1436STali Perry NPCM7XX_CLK_S_AHB, 0, 0, NPCM7XX_CLK_SPIX},
398fcfd1436STali Perry /*5-1 SPIXCKDV*/
399fcfd1436STali Perry
400fcfd1436STali Perry };
401fcfd1436STali Perry
402fcfd1436STali Perry static DEFINE_SPINLOCK(npcm7xx_clk_lock);
403fcfd1436STali Perry
npcm7xx_clk_init(struct device_node * clk_np)404fcfd1436STali Perry static void __init npcm7xx_clk_init(struct device_node *clk_np)
405fcfd1436STali Perry {
406fcfd1436STali Perry struct clk_hw_onecell_data *npcm7xx_clk_data;
407fcfd1436STali Perry void __iomem *clk_base;
408fcfd1436STali Perry struct resource res;
409fcfd1436STali Perry struct clk_hw *hw;
410fcfd1436STali Perry int ret;
411fcfd1436STali Perry int i;
412fcfd1436STali Perry
413fcfd1436STali Perry ret = of_address_to_resource(clk_np, 0, &res);
414fcfd1436STali Perry if (ret) {
415e665f029SRob Herring pr_err("%pOFn: failed to get resource, ret %d\n", clk_np,
416fcfd1436STali Perry ret);
417fcfd1436STali Perry return;
418fcfd1436STali Perry }
419fcfd1436STali Perry
420fcfd1436STali Perry clk_base = ioremap(res.start, resource_size(&res));
4211646337bSWei Yongjun if (!clk_base)
422fcfd1436STali Perry goto npcm7xx_init_error;
423fcfd1436STali Perry
424450b6b9bSGustavo A. R. Silva npcm7xx_clk_data = kzalloc(struct_size(npcm7xx_clk_data, hws,
425450b6b9bSGustavo A. R. Silva NPCM7XX_NUM_CLOCKS), GFP_KERNEL);
426fcfd1436STali Perry if (!npcm7xx_clk_data)
427fcfd1436STali Perry goto npcm7xx_init_np_err;
428fcfd1436STali Perry
429fcfd1436STali Perry npcm7xx_clk_data->num = NPCM7XX_NUM_CLOCKS;
430fcfd1436STali Perry
431fcfd1436STali Perry for (i = 0; i < NPCM7XX_NUM_CLOCKS; i++)
432fcfd1436STali Perry npcm7xx_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
433fcfd1436STali Perry
434fcfd1436STali Perry /* Register plls */
435fcfd1436STali Perry for (i = 0; i < ARRAY_SIZE(npcm7xx_plls); i++) {
436fcfd1436STali Perry const struct npcm7xx_clk_pll_data *pll_data = &npcm7xx_plls[i];
437fcfd1436STali Perry
438fcfd1436STali Perry hw = npcm7xx_clk_register_pll(clk_base + pll_data->reg,
439fcfd1436STali Perry pll_data->name, pll_data->parent_name, pll_data->flags);
440fcfd1436STali Perry if (IS_ERR(hw)) {
441fcfd1436STali Perry pr_err("npcm7xx_clk: Can't register pll\n");
442fcfd1436STali Perry goto npcm7xx_init_fail;
443fcfd1436STali Perry }
444fcfd1436STali Perry
445fcfd1436STali Perry if (pll_data->onecell_idx >= 0)
446fcfd1436STali Perry npcm7xx_clk_data->hws[pll_data->onecell_idx] = hw;
447fcfd1436STali Perry }
448fcfd1436STali Perry
449fcfd1436STali Perry /* Register fixed dividers */
450fcfd1436STali Perry hw = clk_hw_register_fixed_factor(NULL, NPCM7XX_CLK_S_PLL1_DIV2,
451fcfd1436STali Perry NPCM7XX_CLK_S_PLL1, 0, 1, 2);
452fcfd1436STali Perry if (IS_ERR(hw)) {
453fcfd1436STali Perry pr_err("npcm7xx_clk: Can't register fixed div\n");
454fcfd1436STali Perry goto npcm7xx_init_fail;
455fcfd1436STali Perry }
456fcfd1436STali Perry
457fcfd1436STali Perry hw = clk_hw_register_fixed_factor(NULL, NPCM7XX_CLK_S_PLL2_DIV2,
458fcfd1436STali Perry NPCM7XX_CLK_S_PLL2, 0, 1, 2);
459fcfd1436STali Perry if (IS_ERR(hw)) {
460fcfd1436STali Perry pr_err("npcm7xx_clk: Can't register div2\n");
461fcfd1436STali Perry goto npcm7xx_init_fail;
462fcfd1436STali Perry }
463fcfd1436STali Perry
464fcfd1436STali Perry /* Register muxes */
465fcfd1436STali Perry for (i = 0; i < ARRAY_SIZE(npcm7xx_muxes); i++) {
466fcfd1436STali Perry const struct npcm7xx_clk_mux_data *mux_data = &npcm7xx_muxes[i];
467fcfd1436STali Perry
468fcfd1436STali Perry hw = clk_hw_register_mux_table(NULL,
469fcfd1436STali Perry mux_data->name,
470fcfd1436STali Perry mux_data->parent_names, mux_data->num_parents,
471fcfd1436STali Perry mux_data->flags, clk_base + NPCM7XX_CLKSEL,
472fcfd1436STali Perry mux_data->shift, mux_data->mask, 0,
473fcfd1436STali Perry mux_data->table, &npcm7xx_clk_lock);
474fcfd1436STali Perry
475fcfd1436STali Perry if (IS_ERR(hw)) {
476fcfd1436STali Perry pr_err("npcm7xx_clk: Can't register mux\n");
477fcfd1436STali Perry goto npcm7xx_init_fail;
478fcfd1436STali Perry }
479fcfd1436STali Perry
480fcfd1436STali Perry if (mux_data->onecell_idx >= 0)
481fcfd1436STali Perry npcm7xx_clk_data->hws[mux_data->onecell_idx] = hw;
482fcfd1436STali Perry }
483fcfd1436STali Perry
484fcfd1436STali Perry /* Register clock dividers specified in npcm7xx_divs */
485fcfd1436STali Perry for (i = 0; i < ARRAY_SIZE(npcm7xx_divs); i++) {
486fcfd1436STali Perry const struct npcm7xx_clk_div_data *div_data = &npcm7xx_divs[i];
487fcfd1436STali Perry
488fcfd1436STali Perry hw = clk_hw_register_divider(NULL, div_data->name,
489fcfd1436STali Perry div_data->parent_name,
490fcfd1436STali Perry div_data->flags,
491fcfd1436STali Perry clk_base + div_data->reg,
492fcfd1436STali Perry div_data->shift, div_data->width,
493fcfd1436STali Perry div_data->clk_divider_flags, &npcm7xx_clk_lock);
494fcfd1436STali Perry if (IS_ERR(hw)) {
495fcfd1436STali Perry pr_err("npcm7xx_clk: Can't register div table\n");
496fcfd1436STali Perry goto npcm7xx_init_fail;
497fcfd1436STali Perry }
498fcfd1436STali Perry
499fcfd1436STali Perry if (div_data->onecell_idx >= 0)
500fcfd1436STali Perry npcm7xx_clk_data->hws[div_data->onecell_idx] = hw;
501fcfd1436STali Perry }
502fcfd1436STali Perry
503fcfd1436STali Perry ret = of_clk_add_hw_provider(clk_np, of_clk_hw_onecell_get,
504fcfd1436STali Perry npcm7xx_clk_data);
505fcfd1436STali Perry if (ret)
506fcfd1436STali Perry pr_err("failed to add DT provider: %d\n", ret);
507fcfd1436STali Perry
508fcfd1436STali Perry of_node_put(clk_np);
509fcfd1436STali Perry
510fcfd1436STali Perry return;
511fcfd1436STali Perry
512fcfd1436STali Perry npcm7xx_init_fail:
513*db81fa4aSJonathan Neuschäfer kfree(npcm7xx_clk_data);
514fcfd1436STali Perry npcm7xx_init_np_err:
515fcfd1436STali Perry iounmap(clk_base);
516fcfd1436STali Perry npcm7xx_init_error:
517fcfd1436STali Perry of_node_put(clk_np);
518fcfd1436STali Perry }
519fcfd1436STali Perry CLK_OF_DECLARE(npcm7xx_clk_init, "nuvoton,npcm750-clk", npcm7xx_clk_init);
520