xref: /openbmc/linux/drivers/clk/nxp/clk-lpc18xx-cgu.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
13bb16560SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2b04e0b8fSJoachim Eastwood /*
3b04e0b8fSJoachim Eastwood  * Clk driver for NXP LPC18xx/LPC43xx Clock Generation Unit (CGU)
4b04e0b8fSJoachim Eastwood  *
5b04e0b8fSJoachim Eastwood  * Copyright (C) 2015 Joachim Eastwood <manabian@gmail.com>
6b04e0b8fSJoachim Eastwood  */
7b04e0b8fSJoachim Eastwood 
8b04e0b8fSJoachim Eastwood #include <linux/clk-provider.h>
9b04e0b8fSJoachim Eastwood #include <linux/delay.h>
1062e59c4eSStephen Boyd #include <linux/io.h>
11b04e0b8fSJoachim Eastwood #include <linux/kernel.h>
12b04e0b8fSJoachim Eastwood #include <linux/of.h>
13b04e0b8fSJoachim Eastwood #include <linux/of_address.h>
14b04e0b8fSJoachim Eastwood 
15b04e0b8fSJoachim Eastwood #include <dt-bindings/clock/lpc18xx-cgu.h>
16b04e0b8fSJoachim Eastwood 
17b04e0b8fSJoachim Eastwood /* Clock Generation Unit (CGU) registers */
18b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_XTAL_OSC_CTRL	0x018
19b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0USB_STAT	0x01c
20b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0USB_CTRL	0x020
21b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0USB_MDIV	0x024
22b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0USB_NP_DIV	0x028
23b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0AUDIO_STAT	0x02c
24b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0AUDIO_CTRL	0x030
25b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0AUDIO_MDIV	0x034
26b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0AUDIO_NP_DIV	0x038
27b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL0AUDIO_FRAC	0x03c
28b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL1_STAT		0x040
29b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL1_CTRL		0x044
30b04e0b8fSJoachim Eastwood #define  LPC18XX_PLL1_CTRL_FBSEL	BIT(6)
31b04e0b8fSJoachim Eastwood #define  LPC18XX_PLL1_CTRL_DIRECT	BIT(7)
32b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_IDIV_CTRL(n)	(0x048 + (n) * sizeof(u32))
33b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_BASE_CLK(id)	(0x05c + (id) * sizeof(u32))
34b04e0b8fSJoachim Eastwood #define LPC18XX_CGU_PLL_CTRL_OFFSET	0x4
35b04e0b8fSJoachim Eastwood 
36b04e0b8fSJoachim Eastwood /* PLL0 bits common to both audio and USB PLL */
37b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_STAT_LOCK		BIT(0)
38b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_CTRL_PD		BIT(0)
39b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_CTRL_BYPASS	BIT(1)
40b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_CTRL_DIRECTI	BIT(2)
41b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_CTRL_DIRECTO	BIT(3)
42b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_CTRL_CLKEN		BIT(4)
43b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_MDIV_MDEC_MASK	0x1ffff
44b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_MDIV_SELP_SHIFT	17
45b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_MDIV_SELI_SHIFT	22
46b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_MSEL_MAX		BIT(15)
47b04e0b8fSJoachim Eastwood 
48b04e0b8fSJoachim Eastwood /* Register value that gives PLL0 post/pre dividers equal to 1 */
49b04e0b8fSJoachim Eastwood #define LPC18XX_PLL0_NP_DIVS_1		0x00302062
50b04e0b8fSJoachim Eastwood 
51b04e0b8fSJoachim Eastwood enum {
52b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32,
53b04e0b8fSJoachim Eastwood 	CLK_SRC_IRC,
54b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_RX_CLK,
55b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK,
56b04e0b8fSJoachim Eastwood 	CLK_SRC_GP_CLKIN,
57b04e0b8fSJoachim Eastwood 	CLK_SRC_RESERVED1,
58b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC,
59b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0USB,
60b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0AUDIO,
61b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL1,
62b04e0b8fSJoachim Eastwood 	CLK_SRC_RESERVED2,
63b04e0b8fSJoachim Eastwood 	CLK_SRC_RESERVED3,
64b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVA,
65b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVB,
66b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVC,
67b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVD,
68b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVE,
69b04e0b8fSJoachim Eastwood 	CLK_SRC_MAX
70b04e0b8fSJoachim Eastwood };
71b04e0b8fSJoachim Eastwood 
72b04e0b8fSJoachim Eastwood static const char *clk_src_names[CLK_SRC_MAX] = {
73b04e0b8fSJoachim Eastwood 	[CLK_SRC_OSC32]		= "osc32",
74b04e0b8fSJoachim Eastwood 	[CLK_SRC_IRC]		= "irc",
75b04e0b8fSJoachim Eastwood 	[CLK_SRC_ENET_RX_CLK]	= "enet_rx_clk",
76b04e0b8fSJoachim Eastwood 	[CLK_SRC_ENET_TX_CLK]	= "enet_tx_clk",
77b04e0b8fSJoachim Eastwood 	[CLK_SRC_GP_CLKIN]	= "gp_clkin",
78b04e0b8fSJoachim Eastwood 	[CLK_SRC_OSC]		= "osc",
79b04e0b8fSJoachim Eastwood 	[CLK_SRC_PLL0USB]	= "pll0usb",
80b04e0b8fSJoachim Eastwood 	[CLK_SRC_PLL0AUDIO]	= "pll0audio",
81b04e0b8fSJoachim Eastwood 	[CLK_SRC_PLL1]		= "pll1",
82b04e0b8fSJoachim Eastwood 	[CLK_SRC_IDIVA]		= "idiva",
83b04e0b8fSJoachim Eastwood 	[CLK_SRC_IDIVB]		= "idivb",
84b04e0b8fSJoachim Eastwood 	[CLK_SRC_IDIVC]		= "idivc",
85b04e0b8fSJoachim Eastwood 	[CLK_SRC_IDIVD]		= "idivd",
86b04e0b8fSJoachim Eastwood 	[CLK_SRC_IDIVE]		= "idive",
87b04e0b8fSJoachim Eastwood };
88b04e0b8fSJoachim Eastwood 
89b04e0b8fSJoachim Eastwood static const char *clk_base_names[BASE_CLK_MAX] = {
90b04e0b8fSJoachim Eastwood 	[BASE_SAFE_CLK]		= "base_safe_clk",
91b04e0b8fSJoachim Eastwood 	[BASE_USB0_CLK]		= "base_usb0_clk",
92b04e0b8fSJoachim Eastwood 	[BASE_PERIPH_CLK]	= "base_periph_clk",
93b04e0b8fSJoachim Eastwood 	[BASE_USB1_CLK]		= "base_usb1_clk",
94b04e0b8fSJoachim Eastwood 	[BASE_CPU_CLK]		= "base_cpu_clk",
95b04e0b8fSJoachim Eastwood 	[BASE_SPIFI_CLK]	= "base_spifi_clk",
96b04e0b8fSJoachim Eastwood 	[BASE_SPI_CLK]		= "base_spi_clk",
97b04e0b8fSJoachim Eastwood 	[BASE_PHY_RX_CLK]	= "base_phy_rx_clk",
98b04e0b8fSJoachim Eastwood 	[BASE_PHY_TX_CLK]	= "base_phy_tx_clk",
99b04e0b8fSJoachim Eastwood 	[BASE_APB1_CLK]		= "base_apb1_clk",
100b04e0b8fSJoachim Eastwood 	[BASE_APB3_CLK]		= "base_apb3_clk",
101b04e0b8fSJoachim Eastwood 	[BASE_LCD_CLK]		= "base_lcd_clk",
102b04e0b8fSJoachim Eastwood 	[BASE_ADCHS_CLK]	= "base_adchs_clk",
103b04e0b8fSJoachim Eastwood 	[BASE_SDIO_CLK]		= "base_sdio_clk",
104b04e0b8fSJoachim Eastwood 	[BASE_SSP0_CLK]		= "base_ssp0_clk",
105b04e0b8fSJoachim Eastwood 	[BASE_SSP1_CLK]		= "base_ssp1_clk",
106b04e0b8fSJoachim Eastwood 	[BASE_UART0_CLK]	= "base_uart0_clk",
107b04e0b8fSJoachim Eastwood 	[BASE_UART1_CLK]	= "base_uart1_clk",
108b04e0b8fSJoachim Eastwood 	[BASE_UART2_CLK]	= "base_uart2_clk",
109b04e0b8fSJoachim Eastwood 	[BASE_UART3_CLK]	= "base_uart3_clk",
110b04e0b8fSJoachim Eastwood 	[BASE_OUT_CLK]		= "base_out_clk",
111b04e0b8fSJoachim Eastwood 	[BASE_AUDIO_CLK]	= "base_audio_clk",
112b04e0b8fSJoachim Eastwood 	[BASE_CGU_OUT0_CLK]	= "base_cgu_out0_clk",
113b04e0b8fSJoachim Eastwood 	[BASE_CGU_OUT1_CLK]	= "base_cgu_out1_clk",
114b04e0b8fSJoachim Eastwood };
115b04e0b8fSJoachim Eastwood 
116b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_pll0_src_ids[] = {
117b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
118b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
119b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL1, CLK_SRC_IDIVA, CLK_SRC_IDIVB, CLK_SRC_IDIVC,
120b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVD, CLK_SRC_IDIVE,
121b04e0b8fSJoachim Eastwood };
122b04e0b8fSJoachim Eastwood 
123b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_pll1_src_ids[] = {
124b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
125b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
126b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0USB, CLK_SRC_PLL0AUDIO, CLK_SRC_IDIVA,
127b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVB, CLK_SRC_IDIVC, CLK_SRC_IDIVD, CLK_SRC_IDIVE,
128b04e0b8fSJoachim Eastwood };
129b04e0b8fSJoachim Eastwood 
130b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_idiva_src_ids[] = {
131b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
132b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
133b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0USB, CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1
134b04e0b8fSJoachim Eastwood };
135b04e0b8fSJoachim Eastwood 
136b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_idivbcde_src_ids[] = {
137b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
138b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
139b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1, CLK_SRC_IDIVA,
140b04e0b8fSJoachim Eastwood };
141b04e0b8fSJoachim Eastwood 
142b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_base_irc_src_ids[] = {CLK_SRC_IRC};
143b04e0b8fSJoachim Eastwood 
144b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_base_usb0_src_ids[] = {CLK_SRC_PLL0USB};
145b04e0b8fSJoachim Eastwood 
146b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_base_common_src_ids[] = {
147b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
148b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
149b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1, CLK_SRC_IDIVA,
150b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVB, CLK_SRC_IDIVC, CLK_SRC_IDIVD, CLK_SRC_IDIVE,
151b04e0b8fSJoachim Eastwood };
152b04e0b8fSJoachim Eastwood 
153b04e0b8fSJoachim Eastwood static u32 lpc18xx_cgu_base_all_src_ids[] = {
154b04e0b8fSJoachim Eastwood 	CLK_SRC_OSC32, CLK_SRC_IRC, CLK_SRC_ENET_RX_CLK,
155b04e0b8fSJoachim Eastwood 	CLK_SRC_ENET_TX_CLK, CLK_SRC_GP_CLKIN, CLK_SRC_OSC,
156b04e0b8fSJoachim Eastwood 	CLK_SRC_PLL0USB, CLK_SRC_PLL0AUDIO, CLK_SRC_PLL1,
157b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVA, CLK_SRC_IDIVB, CLK_SRC_IDIVC,
158b04e0b8fSJoachim Eastwood 	CLK_SRC_IDIVD, CLK_SRC_IDIVE,
159b04e0b8fSJoachim Eastwood };
160b04e0b8fSJoachim Eastwood 
161b04e0b8fSJoachim Eastwood struct lpc18xx_cgu_src_clk_div {
162b04e0b8fSJoachim Eastwood 	u8 clk_id;
163b04e0b8fSJoachim Eastwood 	u8 n_parents;
164b04e0b8fSJoachim Eastwood 	struct clk_divider	div;
165b04e0b8fSJoachim Eastwood 	struct clk_mux		mux;
166b04e0b8fSJoachim Eastwood 	struct clk_gate		gate;
167b04e0b8fSJoachim Eastwood };
168b04e0b8fSJoachim Eastwood 
169b04e0b8fSJoachim Eastwood #define LPC1XX_CGU_SRC_CLK_DIV(_id, _width, _table)	\
170b04e0b8fSJoachim Eastwood {							\
171b04e0b8fSJoachim Eastwood 	.clk_id = CLK_SRC_ ##_id,			\
172b04e0b8fSJoachim Eastwood 	.n_parents = ARRAY_SIZE(lpc18xx_cgu_ ##_table),	\
173b04e0b8fSJoachim Eastwood 	.div = {					\
174b04e0b8fSJoachim Eastwood 		.shift = 2,				\
175b04e0b8fSJoachim Eastwood 		.width = _width,			\
176b04e0b8fSJoachim Eastwood 	},						\
177b04e0b8fSJoachim Eastwood 	.mux = {					\
178b04e0b8fSJoachim Eastwood 		.mask = 0x1f,				\
179b04e0b8fSJoachim Eastwood 		.shift = 24,				\
180b04e0b8fSJoachim Eastwood 		.table = lpc18xx_cgu_ ##_table,		\
181b04e0b8fSJoachim Eastwood 	},						\
182b04e0b8fSJoachim Eastwood 	.gate = {					\
183b04e0b8fSJoachim Eastwood 		.bit_idx = 0,				\
184b04e0b8fSJoachim Eastwood 		.flags = CLK_GATE_SET_TO_DISABLE,	\
185b04e0b8fSJoachim Eastwood 	},						\
186b04e0b8fSJoachim Eastwood }
187b04e0b8fSJoachim Eastwood 
188b04e0b8fSJoachim Eastwood static struct lpc18xx_cgu_src_clk_div lpc18xx_cgu_src_clk_divs[] = {
189b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_SRC_CLK_DIV(IDIVA, 2, idiva_src_ids),
190b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_SRC_CLK_DIV(IDIVB, 4, idivbcde_src_ids),
191b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_SRC_CLK_DIV(IDIVC, 4, idivbcde_src_ids),
192b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_SRC_CLK_DIV(IDIVD, 4, idivbcde_src_ids),
193b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_SRC_CLK_DIV(IDIVE, 8, idivbcde_src_ids),
194b04e0b8fSJoachim Eastwood };
195b04e0b8fSJoachim Eastwood 
196b04e0b8fSJoachim Eastwood struct lpc18xx_cgu_base_clk {
197b04e0b8fSJoachim Eastwood 	u8 clk_id;
198b04e0b8fSJoachim Eastwood 	u8 n_parents;
199b04e0b8fSJoachim Eastwood 	struct clk_mux mux;
200b04e0b8fSJoachim Eastwood 	struct clk_gate gate;
201b04e0b8fSJoachim Eastwood };
202b04e0b8fSJoachim Eastwood 
203b04e0b8fSJoachim Eastwood #define LPC1XX_CGU_BASE_CLK(_id, _table, _flags)	\
204b04e0b8fSJoachim Eastwood {							\
205b04e0b8fSJoachim Eastwood 	.clk_id = BASE_ ##_id ##_CLK,			\
206b04e0b8fSJoachim Eastwood 	.n_parents = ARRAY_SIZE(lpc18xx_cgu_ ##_table),	\
207b04e0b8fSJoachim Eastwood 	.mux = {					\
208b04e0b8fSJoachim Eastwood 		.mask = 0x1f,				\
209b04e0b8fSJoachim Eastwood 		.shift = 24,				\
210b04e0b8fSJoachim Eastwood 		.table = lpc18xx_cgu_ ##_table,		\
211b04e0b8fSJoachim Eastwood 		.flags = _flags,			\
212b04e0b8fSJoachim Eastwood 	},						\
213b04e0b8fSJoachim Eastwood 	.gate = {					\
214b04e0b8fSJoachim Eastwood 		.bit_idx = 0,				\
215b04e0b8fSJoachim Eastwood 		.flags = CLK_GATE_SET_TO_DISABLE,	\
216b04e0b8fSJoachim Eastwood 	},						\
217b04e0b8fSJoachim Eastwood }
218b04e0b8fSJoachim Eastwood 
219b04e0b8fSJoachim Eastwood static struct lpc18xx_cgu_base_clk lpc18xx_cgu_base_clks[] = {
220b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(SAFE,	base_irc_src_ids, CLK_MUX_READ_ONLY),
221b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(USB0,	base_usb0_src_ids,   0),
222b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(PERIPH,	base_common_src_ids, 0),
223b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(USB1,	base_all_src_ids,    0),
224b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(CPU,	base_common_src_ids, 0),
225b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(SPIFI,	base_common_src_ids, 0),
226b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(SPI,	base_common_src_ids, 0),
227b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(PHY_RX,	base_common_src_ids, 0),
228b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(PHY_TX,	base_common_src_ids, 0),
229b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(APB1,	base_common_src_ids, 0),
230b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(APB3,	base_common_src_ids, 0),
231b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(LCD,	base_common_src_ids, 0),
232b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(ADCHS,	base_common_src_ids, 0),
233b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(SDIO,	base_common_src_ids, 0),
234b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(SSP0,	base_common_src_ids, 0),
235b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(SSP1,	base_common_src_ids, 0),
236b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(UART0,	base_common_src_ids, 0),
237b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(UART1,	base_common_src_ids, 0),
238b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(UART2,	base_common_src_ids, 0),
239b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(UART3,	base_common_src_ids, 0),
240b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(OUT,	base_all_src_ids,    0),
241b04e0b8fSJoachim Eastwood 	{ /* 21 reserved */ },
242b04e0b8fSJoachim Eastwood 	{ /* 22 reserved */ },
243b04e0b8fSJoachim Eastwood 	{ /* 23 reserved */ },
244b04e0b8fSJoachim Eastwood 	{ /* 24 reserved */ },
245b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(AUDIO,	base_common_src_ids, 0),
246b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(CGU_OUT0,	base_all_src_ids,    0),
247b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_BASE_CLK(CGU_OUT1,	base_all_src_ids,    0),
248b04e0b8fSJoachim Eastwood };
249b04e0b8fSJoachim Eastwood 
250b04e0b8fSJoachim Eastwood struct lpc18xx_pll {
251b04e0b8fSJoachim Eastwood 	struct		clk_hw hw;
252b04e0b8fSJoachim Eastwood 	void __iomem	*reg;
253b04e0b8fSJoachim Eastwood 	spinlock_t	*lock;
254b04e0b8fSJoachim Eastwood 	u8		flags;
255b04e0b8fSJoachim Eastwood };
256b04e0b8fSJoachim Eastwood 
257b04e0b8fSJoachim Eastwood #define to_lpc_pll(hw) container_of(hw, struct lpc18xx_pll, hw)
258b04e0b8fSJoachim Eastwood 
259b04e0b8fSJoachim Eastwood struct lpc18xx_cgu_pll_clk {
260b04e0b8fSJoachim Eastwood 	u8 clk_id;
261b04e0b8fSJoachim Eastwood 	u8 n_parents;
262b04e0b8fSJoachim Eastwood 	u8 reg_offset;
263b04e0b8fSJoachim Eastwood 	struct clk_mux mux;
264b04e0b8fSJoachim Eastwood 	struct clk_gate gate;
265b04e0b8fSJoachim Eastwood 	struct lpc18xx_pll pll;
266b04e0b8fSJoachim Eastwood 	const struct clk_ops *pll_ops;
267b04e0b8fSJoachim Eastwood };
268b04e0b8fSJoachim Eastwood 
269b04e0b8fSJoachim Eastwood #define LPC1XX_CGU_CLK_PLL(_id, _table, _pll_ops)	\
270b04e0b8fSJoachim Eastwood {							\
271b04e0b8fSJoachim Eastwood 	.clk_id = CLK_SRC_ ##_id,			\
272b04e0b8fSJoachim Eastwood 	.n_parents = ARRAY_SIZE(lpc18xx_cgu_ ##_table),	\
273b04e0b8fSJoachim Eastwood 	.reg_offset = LPC18XX_CGU_ ##_id ##_STAT,	\
274b04e0b8fSJoachim Eastwood 	.mux = {					\
275b04e0b8fSJoachim Eastwood 		.mask = 0x1f,				\
276b04e0b8fSJoachim Eastwood 		.shift = 24,				\
277b04e0b8fSJoachim Eastwood 		.table = lpc18xx_cgu_ ##_table,		\
278b04e0b8fSJoachim Eastwood 	},						\
279b04e0b8fSJoachim Eastwood 	.gate = {					\
280b04e0b8fSJoachim Eastwood 		.bit_idx = 0,				\
281b04e0b8fSJoachim Eastwood 		.flags = CLK_GATE_SET_TO_DISABLE,	\
282b04e0b8fSJoachim Eastwood 	},						\
283b04e0b8fSJoachim Eastwood 	.pll_ops = &lpc18xx_ ##_pll_ops,		\
284b04e0b8fSJoachim Eastwood }
285b04e0b8fSJoachim Eastwood 
286b04e0b8fSJoachim Eastwood /*
287b04e0b8fSJoachim Eastwood  * PLL0 uses a special register value encoding. The compute functions below
288b04e0b8fSJoachim Eastwood  * are taken or derived from the LPC1850 user manual (section 12.6.3.3).
289b04e0b8fSJoachim Eastwood  */
290b04e0b8fSJoachim Eastwood 
291b04e0b8fSJoachim Eastwood /* Compute PLL0 multiplier from decoded version */
lpc18xx_pll0_mdec2msel(u32 x)292b04e0b8fSJoachim Eastwood static u32 lpc18xx_pll0_mdec2msel(u32 x)
293b04e0b8fSJoachim Eastwood {
294b04e0b8fSJoachim Eastwood 	int i;
295b04e0b8fSJoachim Eastwood 
296b04e0b8fSJoachim Eastwood 	switch (x) {
297b04e0b8fSJoachim Eastwood 	case 0x18003: return 1;
298b04e0b8fSJoachim Eastwood 	case 0x10003: return 2;
299b04e0b8fSJoachim Eastwood 	default:
300b04e0b8fSJoachim Eastwood 		for (i = LPC18XX_PLL0_MSEL_MAX + 1; x != 0x4000 && i > 0; i--)
301b04e0b8fSJoachim Eastwood 			x = ((x ^ x >> 14) & 1) | (x << 1 & 0x7fff);
302b04e0b8fSJoachim Eastwood 		return i;
303b04e0b8fSJoachim Eastwood 	}
304b04e0b8fSJoachim Eastwood }
305b04e0b8fSJoachim Eastwood /* Compute PLL0 decoded multiplier from binary version */
lpc18xx_pll0_msel2mdec(u32 msel)306b04e0b8fSJoachim Eastwood static u32 lpc18xx_pll0_msel2mdec(u32 msel)
307b04e0b8fSJoachim Eastwood {
308b04e0b8fSJoachim Eastwood 	u32 i, x = 0x4000;
309b04e0b8fSJoachim Eastwood 
310b04e0b8fSJoachim Eastwood 	switch (msel) {
311b04e0b8fSJoachim Eastwood 	case 0: return 0;
312b04e0b8fSJoachim Eastwood 	case 1: return 0x18003;
313b04e0b8fSJoachim Eastwood 	case 2: return 0x10003;
314b04e0b8fSJoachim Eastwood 	default:
315b04e0b8fSJoachim Eastwood 		for (i = msel; i <= LPC18XX_PLL0_MSEL_MAX; i++)
316b04e0b8fSJoachim Eastwood 			x = ((x ^ x >> 1) & 1) << 14 | (x >> 1 & 0xffff);
317b04e0b8fSJoachim Eastwood 		return x;
318b04e0b8fSJoachim Eastwood 	}
319b04e0b8fSJoachim Eastwood }
320b04e0b8fSJoachim Eastwood 
321b04e0b8fSJoachim Eastwood /* Compute PLL0 bandwidth SELI reg from multiplier */
lpc18xx_pll0_msel2seli(u32 msel)322b04e0b8fSJoachim Eastwood static u32 lpc18xx_pll0_msel2seli(u32 msel)
323b04e0b8fSJoachim Eastwood {
324b04e0b8fSJoachim Eastwood 	u32 tmp;
325b04e0b8fSJoachim Eastwood 
326b04e0b8fSJoachim Eastwood 	if (msel > 16384) return 1;
327b04e0b8fSJoachim Eastwood 	if (msel >  8192) return 2;
328b04e0b8fSJoachim Eastwood 	if (msel >  2048) return 4;
329b04e0b8fSJoachim Eastwood 	if (msel >=  501) return 8;
330b04e0b8fSJoachim Eastwood 	if (msel >=   60) {
331b04e0b8fSJoachim Eastwood 		tmp = 1024 / (msel + 9);
332b04e0b8fSJoachim Eastwood 		return ((1024 == (tmp * (msel + 9))) == 0) ? tmp * 4 : (tmp + 1) * 4;
333b04e0b8fSJoachim Eastwood 	}
334b04e0b8fSJoachim Eastwood 
335b04e0b8fSJoachim Eastwood 	return (msel & 0x3c) + 4;
336b04e0b8fSJoachim Eastwood }
337b04e0b8fSJoachim Eastwood 
338b04e0b8fSJoachim Eastwood /* Compute PLL0 bandwidth SELP reg from multiplier */
lpc18xx_pll0_msel2selp(u32 msel)339b04e0b8fSJoachim Eastwood static u32 lpc18xx_pll0_msel2selp(u32 msel)
340b04e0b8fSJoachim Eastwood {
341b04e0b8fSJoachim Eastwood 	if (msel < 60)
342b04e0b8fSJoachim Eastwood 		return (msel >> 1) + 1;
343b04e0b8fSJoachim Eastwood 
344b04e0b8fSJoachim Eastwood 	return 31;
345b04e0b8fSJoachim Eastwood }
346b04e0b8fSJoachim Eastwood 
lpc18xx_pll0_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)347b04e0b8fSJoachim Eastwood static unsigned long lpc18xx_pll0_recalc_rate(struct clk_hw *hw,
348b04e0b8fSJoachim Eastwood 					      unsigned long parent_rate)
349b04e0b8fSJoachim Eastwood {
350b04e0b8fSJoachim Eastwood 	struct lpc18xx_pll *pll = to_lpc_pll(hw);
351b04e0b8fSJoachim Eastwood 	u32 ctrl, mdiv, msel, npdiv;
352b04e0b8fSJoachim Eastwood 
3535834fd75SJonas Gorski 	ctrl = readl(pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
3545834fd75SJonas Gorski 	mdiv = readl(pll->reg + LPC18XX_CGU_PLL0USB_MDIV);
3555834fd75SJonas Gorski 	npdiv = readl(pll->reg + LPC18XX_CGU_PLL0USB_NP_DIV);
356b04e0b8fSJoachim Eastwood 
357b04e0b8fSJoachim Eastwood 	if (ctrl & LPC18XX_PLL0_CTRL_BYPASS)
358b04e0b8fSJoachim Eastwood 		return parent_rate;
359b04e0b8fSJoachim Eastwood 
360b04e0b8fSJoachim Eastwood 	if (npdiv != LPC18XX_PLL0_NP_DIVS_1) {
361b04e0b8fSJoachim Eastwood 		pr_warn("%s: pre/post dividers not supported\n", __func__);
362b04e0b8fSJoachim Eastwood 		return 0;
363b04e0b8fSJoachim Eastwood 	}
364b04e0b8fSJoachim Eastwood 
365b04e0b8fSJoachim Eastwood 	msel = lpc18xx_pll0_mdec2msel(mdiv & LPC18XX_PLL0_MDIV_MDEC_MASK);
366b04e0b8fSJoachim Eastwood 	if (msel)
367b04e0b8fSJoachim Eastwood 		return 2 * msel * parent_rate;
368b04e0b8fSJoachim Eastwood 
369b04e0b8fSJoachim Eastwood 	pr_warn("%s: unable to calculate rate\n", __func__);
370b04e0b8fSJoachim Eastwood 
371b04e0b8fSJoachim Eastwood 	return 0;
372b04e0b8fSJoachim Eastwood }
373b04e0b8fSJoachim Eastwood 
lpc18xx_pll0_round_rate(struct clk_hw * hw,unsigned long rate,unsigned long * prate)374b04e0b8fSJoachim Eastwood static long lpc18xx_pll0_round_rate(struct clk_hw *hw, unsigned long rate,
375b04e0b8fSJoachim Eastwood 				    unsigned long *prate)
376b04e0b8fSJoachim Eastwood {
377b04e0b8fSJoachim Eastwood 	unsigned long m;
378b04e0b8fSJoachim Eastwood 
379b04e0b8fSJoachim Eastwood 	if (*prate < rate) {
380b04e0b8fSJoachim Eastwood 		pr_warn("%s: pll dividers not supported\n", __func__);
381b04e0b8fSJoachim Eastwood 		return -EINVAL;
382b04e0b8fSJoachim Eastwood 	}
383b04e0b8fSJoachim Eastwood 
384b04e0b8fSJoachim Eastwood 	m = DIV_ROUND_UP_ULL(*prate, rate * 2);
385b04e0b8fSJoachim Eastwood 	if (m <= 0 && m > LPC18XX_PLL0_MSEL_MAX) {
386b04e0b8fSJoachim Eastwood 		pr_warn("%s: unable to support rate %lu\n", __func__, rate);
387b04e0b8fSJoachim Eastwood 		return -EINVAL;
388b04e0b8fSJoachim Eastwood 	}
389b04e0b8fSJoachim Eastwood 
390b04e0b8fSJoachim Eastwood 	return 2 * *prate * m;
391b04e0b8fSJoachim Eastwood }
392b04e0b8fSJoachim Eastwood 
lpc18xx_pll0_set_rate(struct clk_hw * hw,unsigned long rate,unsigned long parent_rate)393b04e0b8fSJoachim Eastwood static int lpc18xx_pll0_set_rate(struct clk_hw *hw, unsigned long rate,
394b04e0b8fSJoachim Eastwood 				 unsigned long parent_rate)
395b04e0b8fSJoachim Eastwood {
396b04e0b8fSJoachim Eastwood 	struct lpc18xx_pll *pll = to_lpc_pll(hw);
397b04e0b8fSJoachim Eastwood 	u32 ctrl, stat, m;
398b04e0b8fSJoachim Eastwood 	int retry = 3;
399b04e0b8fSJoachim Eastwood 
400b04e0b8fSJoachim Eastwood 	if (parent_rate < rate) {
401b04e0b8fSJoachim Eastwood 		pr_warn("%s: pll dividers not supported\n", __func__);
402b04e0b8fSJoachim Eastwood 		return -EINVAL;
403b04e0b8fSJoachim Eastwood 	}
404b04e0b8fSJoachim Eastwood 
405b04e0b8fSJoachim Eastwood 	m = DIV_ROUND_UP_ULL(parent_rate, rate * 2);
406b04e0b8fSJoachim Eastwood 	if (m <= 0 && m > LPC18XX_PLL0_MSEL_MAX) {
407b04e0b8fSJoachim Eastwood 		pr_warn("%s: unable to support rate %lu\n", __func__, rate);
408b04e0b8fSJoachim Eastwood 		return -EINVAL;
409b04e0b8fSJoachim Eastwood 	}
410b04e0b8fSJoachim Eastwood 
411b04e0b8fSJoachim Eastwood 	m  = lpc18xx_pll0_msel2mdec(m);
412b04e0b8fSJoachim Eastwood 	m |= lpc18xx_pll0_msel2selp(m) << LPC18XX_PLL0_MDIV_SELP_SHIFT;
413b04e0b8fSJoachim Eastwood 	m |= lpc18xx_pll0_msel2seli(m) << LPC18XX_PLL0_MDIV_SELI_SHIFT;
414b04e0b8fSJoachim Eastwood 
415b04e0b8fSJoachim Eastwood 	/* Power down PLL, disable clk output and dividers */
4165834fd75SJonas Gorski 	ctrl = readl(pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
417b04e0b8fSJoachim Eastwood 	ctrl |= LPC18XX_PLL0_CTRL_PD;
418b04e0b8fSJoachim Eastwood 	ctrl &= ~(LPC18XX_PLL0_CTRL_BYPASS | LPC18XX_PLL0_CTRL_DIRECTI |
419b04e0b8fSJoachim Eastwood 		  LPC18XX_PLL0_CTRL_DIRECTO | LPC18XX_PLL0_CTRL_CLKEN);
4205834fd75SJonas Gorski 	writel(ctrl, pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
421b04e0b8fSJoachim Eastwood 
422b04e0b8fSJoachim Eastwood 	/* Configure new PLL settings */
4235834fd75SJonas Gorski 	writel(m, pll->reg + LPC18XX_CGU_PLL0USB_MDIV);
4245834fd75SJonas Gorski 	writel(LPC18XX_PLL0_NP_DIVS_1, pll->reg + LPC18XX_CGU_PLL0USB_NP_DIV);
425b04e0b8fSJoachim Eastwood 
426b04e0b8fSJoachim Eastwood 	/* Power up PLL and wait for lock */
427b04e0b8fSJoachim Eastwood 	ctrl &= ~LPC18XX_PLL0_CTRL_PD;
4285834fd75SJonas Gorski 	writel(ctrl, pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
429b04e0b8fSJoachim Eastwood 	do {
430b04e0b8fSJoachim Eastwood 		udelay(10);
4315834fd75SJonas Gorski 		stat = readl(pll->reg + LPC18XX_CGU_PLL0USB_STAT);
432b04e0b8fSJoachim Eastwood 		if (stat & LPC18XX_PLL0_STAT_LOCK) {
433b04e0b8fSJoachim Eastwood 			ctrl |= LPC18XX_PLL0_CTRL_CLKEN;
4345834fd75SJonas Gorski 			writel(ctrl, pll->reg + LPC18XX_CGU_PLL0USB_CTRL);
435b04e0b8fSJoachim Eastwood 
436b04e0b8fSJoachim Eastwood 			return 0;
437b04e0b8fSJoachim Eastwood 		}
438b04e0b8fSJoachim Eastwood 	} while (retry--);
439b04e0b8fSJoachim Eastwood 
440b04e0b8fSJoachim Eastwood 	pr_warn("%s: unable to lock pll\n", __func__);
441b04e0b8fSJoachim Eastwood 
442b04e0b8fSJoachim Eastwood 	return -EINVAL;
443b04e0b8fSJoachim Eastwood }
444b04e0b8fSJoachim Eastwood 
445b04e0b8fSJoachim Eastwood static const struct clk_ops lpc18xx_pll0_ops = {
446b04e0b8fSJoachim Eastwood 	.recalc_rate	= lpc18xx_pll0_recalc_rate,
447b04e0b8fSJoachim Eastwood 	.round_rate	= lpc18xx_pll0_round_rate,
448b04e0b8fSJoachim Eastwood 	.set_rate	= lpc18xx_pll0_set_rate,
449b04e0b8fSJoachim Eastwood };
450b04e0b8fSJoachim Eastwood 
lpc18xx_pll1_recalc_rate(struct clk_hw * hw,unsigned long parent_rate)451b04e0b8fSJoachim Eastwood static unsigned long lpc18xx_pll1_recalc_rate(struct clk_hw *hw,
452b04e0b8fSJoachim Eastwood 					      unsigned long parent_rate)
453b04e0b8fSJoachim Eastwood {
454b04e0b8fSJoachim Eastwood 	struct lpc18xx_pll *pll = to_lpc_pll(hw);
455b04e0b8fSJoachim Eastwood 	u16 msel, nsel, psel;
456b04e0b8fSJoachim Eastwood 	bool direct, fbsel;
457703da2aeSJonathan Neuschäfer 	u32 ctrl;
458b04e0b8fSJoachim Eastwood 
4595834fd75SJonas Gorski 	ctrl = readl(pll->reg + LPC18XX_CGU_PLL1_CTRL);
460b04e0b8fSJoachim Eastwood 
461b04e0b8fSJoachim Eastwood 	direct = (ctrl & LPC18XX_PLL1_CTRL_DIRECT) ? true : false;
462b04e0b8fSJoachim Eastwood 	fbsel = (ctrl & LPC18XX_PLL1_CTRL_FBSEL) ? true : false;
463b04e0b8fSJoachim Eastwood 
464b04e0b8fSJoachim Eastwood 	msel = ((ctrl >> 16) & 0xff) + 1;
465b04e0b8fSJoachim Eastwood 	nsel = ((ctrl >> 12) & 0x3) + 1;
466b04e0b8fSJoachim Eastwood 
467b04e0b8fSJoachim Eastwood 	if (direct || fbsel)
468b04e0b8fSJoachim Eastwood 		return msel * (parent_rate / nsel);
469b04e0b8fSJoachim Eastwood 
470b04e0b8fSJoachim Eastwood 	psel = (ctrl >>  8) & 0x3;
471b04e0b8fSJoachim Eastwood 	psel = 1 << psel;
472b04e0b8fSJoachim Eastwood 
473b04e0b8fSJoachim Eastwood 	return (msel / (2 * psel)) * (parent_rate / nsel);
474b04e0b8fSJoachim Eastwood }
475b04e0b8fSJoachim Eastwood 
476b04e0b8fSJoachim Eastwood static const struct clk_ops lpc18xx_pll1_ops = {
477b04e0b8fSJoachim Eastwood 	.recalc_rate = lpc18xx_pll1_recalc_rate,
478b04e0b8fSJoachim Eastwood };
479b04e0b8fSJoachim Eastwood 
lpc18xx_cgu_gate_enable(struct clk_hw * hw)480c23a5847SJoachim Eastwood static int lpc18xx_cgu_gate_enable(struct clk_hw *hw)
481c23a5847SJoachim Eastwood {
482c23a5847SJoachim Eastwood 	return clk_gate_ops.enable(hw);
483c23a5847SJoachim Eastwood }
484c23a5847SJoachim Eastwood 
lpc18xx_cgu_gate_disable(struct clk_hw * hw)485c23a5847SJoachim Eastwood static void lpc18xx_cgu_gate_disable(struct clk_hw *hw)
486c23a5847SJoachim Eastwood {
487c23a5847SJoachim Eastwood 	clk_gate_ops.disable(hw);
488c23a5847SJoachim Eastwood }
489c23a5847SJoachim Eastwood 
lpc18xx_cgu_gate_is_enabled(struct clk_hw * hw)490c23a5847SJoachim Eastwood static int lpc18xx_cgu_gate_is_enabled(struct clk_hw *hw)
491c23a5847SJoachim Eastwood {
492c23a5847SJoachim Eastwood 	const struct clk_hw *parent;
493c23a5847SJoachim Eastwood 
494c23a5847SJoachim Eastwood 	/*
495c23a5847SJoachim Eastwood 	 * The consumer of base clocks needs know if the
496c23a5847SJoachim Eastwood 	 * base clock is really enabled before it can be
497c23a5847SJoachim Eastwood 	 * accessed. It is therefore necessary to verify
498c23a5847SJoachim Eastwood 	 * this all the way up.
499c23a5847SJoachim Eastwood 	 */
500c23a5847SJoachim Eastwood 	parent = clk_hw_get_parent(hw);
501c23a5847SJoachim Eastwood 	if (!parent)
502c23a5847SJoachim Eastwood 		return 0;
503c23a5847SJoachim Eastwood 
504c23a5847SJoachim Eastwood 	if (!clk_hw_is_enabled(parent))
505c23a5847SJoachim Eastwood 		return 0;
506c23a5847SJoachim Eastwood 
507c23a5847SJoachim Eastwood 	return clk_gate_ops.is_enabled(hw);
508c23a5847SJoachim Eastwood }
509c23a5847SJoachim Eastwood 
510c23a5847SJoachim Eastwood static const struct clk_ops lpc18xx_gate_ops = {
511c23a5847SJoachim Eastwood 	.enable = lpc18xx_cgu_gate_enable,
512c23a5847SJoachim Eastwood 	.disable = lpc18xx_cgu_gate_disable,
513c23a5847SJoachim Eastwood 	.is_enabled = lpc18xx_cgu_gate_is_enabled,
514c23a5847SJoachim Eastwood };
515c23a5847SJoachim Eastwood 
516b04e0b8fSJoachim Eastwood static struct lpc18xx_cgu_pll_clk lpc18xx_cgu_src_clk_plls[] = {
517b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_CLK_PLL(PLL0USB,	pll0_src_ids, pll0_ops),
518b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_CLK_PLL(PLL0AUDIO,	pll0_src_ids, pll0_ops),
519b04e0b8fSJoachim Eastwood 	LPC1XX_CGU_CLK_PLL(PLL1,	pll1_src_ids, pll1_ops),
520b04e0b8fSJoachim Eastwood };
521b04e0b8fSJoachim Eastwood 
lpc18xx_fill_parent_names(const char ** parent,const u32 * id,int size)5222eb3b3f0SJonathan Neuschäfer static void lpc18xx_fill_parent_names(const char **parent, const u32 *id, int size)
523b04e0b8fSJoachim Eastwood {
524b04e0b8fSJoachim Eastwood 	int i;
525b04e0b8fSJoachim Eastwood 
526b04e0b8fSJoachim Eastwood 	for (i = 0; i < size; i++)
527b04e0b8fSJoachim Eastwood 		parent[i] = clk_src_names[id[i]];
528b04e0b8fSJoachim Eastwood }
529b04e0b8fSJoachim Eastwood 
lpc18xx_cgu_register_div(struct lpc18xx_cgu_src_clk_div * clk,void __iomem * base,int n)530b04e0b8fSJoachim Eastwood static struct clk *lpc18xx_cgu_register_div(struct lpc18xx_cgu_src_clk_div *clk,
531b04e0b8fSJoachim Eastwood 					    void __iomem *base, int n)
532b04e0b8fSJoachim Eastwood {
533b04e0b8fSJoachim Eastwood 	void __iomem *reg = base + LPC18XX_CGU_IDIV_CTRL(n);
534b04e0b8fSJoachim Eastwood 	const char *name = clk_src_names[clk->clk_id];
535b04e0b8fSJoachim Eastwood 	const char *parents[CLK_SRC_MAX];
536b04e0b8fSJoachim Eastwood 
537b04e0b8fSJoachim Eastwood 	clk->div.reg = reg;
538b04e0b8fSJoachim Eastwood 	clk->mux.reg = reg;
539b04e0b8fSJoachim Eastwood 	clk->gate.reg = reg;
540b04e0b8fSJoachim Eastwood 
541b04e0b8fSJoachim Eastwood 	lpc18xx_fill_parent_names(parents, clk->mux.table, clk->n_parents);
542b04e0b8fSJoachim Eastwood 
543b04e0b8fSJoachim Eastwood 	return clk_register_composite(NULL, name, parents, clk->n_parents,
544b04e0b8fSJoachim Eastwood 				      &clk->mux.hw, &clk_mux_ops,
545b04e0b8fSJoachim Eastwood 				      &clk->div.hw, &clk_divider_ops,
546c23a5847SJoachim Eastwood 				      &clk->gate.hw, &lpc18xx_gate_ops, 0);
547b04e0b8fSJoachim Eastwood }
548b04e0b8fSJoachim Eastwood 
549b04e0b8fSJoachim Eastwood 
lpc18xx_register_base_clk(struct lpc18xx_cgu_base_clk * clk,void __iomem * reg_base,int n)550b04e0b8fSJoachim Eastwood static struct clk *lpc18xx_register_base_clk(struct lpc18xx_cgu_base_clk *clk,
551b04e0b8fSJoachim Eastwood 					     void __iomem *reg_base, int n)
552b04e0b8fSJoachim Eastwood {
553b04e0b8fSJoachim Eastwood 	void __iomem *reg = reg_base + LPC18XX_CGU_BASE_CLK(n);
554b04e0b8fSJoachim Eastwood 	const char *name = clk_base_names[clk->clk_id];
555b04e0b8fSJoachim Eastwood 	const char *parents[CLK_SRC_MAX];
556b04e0b8fSJoachim Eastwood 
557b04e0b8fSJoachim Eastwood 	if (clk->n_parents == 0)
558b04e0b8fSJoachim Eastwood 		return ERR_PTR(-ENOENT);
559b04e0b8fSJoachim Eastwood 
560b04e0b8fSJoachim Eastwood 	clk->mux.reg = reg;
561b04e0b8fSJoachim Eastwood 	clk->gate.reg = reg;
562b04e0b8fSJoachim Eastwood 
563b04e0b8fSJoachim Eastwood 	lpc18xx_fill_parent_names(parents, clk->mux.table, clk->n_parents);
564b04e0b8fSJoachim Eastwood 
565b04e0b8fSJoachim Eastwood 	/* SAFE_CLK can not be turned off */
566b04e0b8fSJoachim Eastwood 	if (n == BASE_SAFE_CLK)
567b04e0b8fSJoachim Eastwood 		return clk_register_composite(NULL, name, parents, clk->n_parents,
568b04e0b8fSJoachim Eastwood 					      &clk->mux.hw, &clk_mux_ops,
569b04e0b8fSJoachim Eastwood 					      NULL, NULL, NULL, NULL, 0);
570b04e0b8fSJoachim Eastwood 
571b04e0b8fSJoachim Eastwood 	return clk_register_composite(NULL, name, parents, clk->n_parents,
572b04e0b8fSJoachim Eastwood 				      &clk->mux.hw, &clk_mux_ops,
573b04e0b8fSJoachim Eastwood 				      NULL,  NULL,
574c23a5847SJoachim Eastwood 				      &clk->gate.hw, &lpc18xx_gate_ops, 0);
575b04e0b8fSJoachim Eastwood }
576b04e0b8fSJoachim Eastwood 
577b04e0b8fSJoachim Eastwood 
lpc18xx_cgu_register_pll(struct lpc18xx_cgu_pll_clk * clk,void __iomem * base)578b04e0b8fSJoachim Eastwood static struct clk *lpc18xx_cgu_register_pll(struct lpc18xx_cgu_pll_clk *clk,
579b04e0b8fSJoachim Eastwood 					    void __iomem *base)
580b04e0b8fSJoachim Eastwood {
581b04e0b8fSJoachim Eastwood 	const char *name = clk_src_names[clk->clk_id];
582b04e0b8fSJoachim Eastwood 	const char *parents[CLK_SRC_MAX];
583b04e0b8fSJoachim Eastwood 
584b04e0b8fSJoachim Eastwood 	clk->pll.reg  = base;
585b04e0b8fSJoachim Eastwood 	clk->mux.reg  = base + clk->reg_offset + LPC18XX_CGU_PLL_CTRL_OFFSET;
586b04e0b8fSJoachim Eastwood 	clk->gate.reg = base + clk->reg_offset + LPC18XX_CGU_PLL_CTRL_OFFSET;
587b04e0b8fSJoachim Eastwood 
588b04e0b8fSJoachim Eastwood 	lpc18xx_fill_parent_names(parents, clk->mux.table, clk->n_parents);
589b04e0b8fSJoachim Eastwood 
590b04e0b8fSJoachim Eastwood 	return clk_register_composite(NULL, name, parents, clk->n_parents,
591b04e0b8fSJoachim Eastwood 				      &clk->mux.hw, &clk_mux_ops,
592b04e0b8fSJoachim Eastwood 				      &clk->pll.hw, clk->pll_ops,
593c23a5847SJoachim Eastwood 				      &clk->gate.hw, &lpc18xx_gate_ops, 0);
594b04e0b8fSJoachim Eastwood }
595b04e0b8fSJoachim Eastwood 
lpc18xx_cgu_register_source_clks(struct device_node * np,void __iomem * base)596b04e0b8fSJoachim Eastwood static void __init lpc18xx_cgu_register_source_clks(struct device_node *np,
597b04e0b8fSJoachim Eastwood 						    void __iomem *base)
598b04e0b8fSJoachim Eastwood {
599b04e0b8fSJoachim Eastwood 	const char *parents[CLK_SRC_MAX];
600b04e0b8fSJoachim Eastwood 	struct clk *clk;
601b04e0b8fSJoachim Eastwood 	int i;
602b04e0b8fSJoachim Eastwood 
603b04e0b8fSJoachim Eastwood 	/* Register the internal 12 MHz RC oscillator (IRC) */
604b04e0b8fSJoachim Eastwood 	clk = clk_register_fixed_rate(NULL, clk_src_names[CLK_SRC_IRC],
605615b34deSStephen Boyd 				      NULL, 0, 12000000);
606b04e0b8fSJoachim Eastwood 	if (IS_ERR(clk))
607b04e0b8fSJoachim Eastwood 		pr_warn("%s: failed to register irc clk\n", __func__);
608b04e0b8fSJoachim Eastwood 
609*7942ac9fSJulia Lawall 	/* Register crystal oscillator controller */
610b04e0b8fSJoachim Eastwood 	parents[0] = of_clk_get_parent_name(np, 0);
611b04e0b8fSJoachim Eastwood 	clk = clk_register_gate(NULL, clk_src_names[CLK_SRC_OSC], parents[0],
612b04e0b8fSJoachim Eastwood 				0, base + LPC18XX_CGU_XTAL_OSC_CTRL,
613b04e0b8fSJoachim Eastwood 				0, CLK_GATE_SET_TO_DISABLE, NULL);
614b04e0b8fSJoachim Eastwood 	if (IS_ERR(clk))
615b04e0b8fSJoachim Eastwood 		pr_warn("%s: failed to register osc clk\n", __func__);
616b04e0b8fSJoachim Eastwood 
617b04e0b8fSJoachim Eastwood 	/* Register all PLLs */
618b04e0b8fSJoachim Eastwood 	for (i = 0; i < ARRAY_SIZE(lpc18xx_cgu_src_clk_plls); i++) {
619b04e0b8fSJoachim Eastwood 		clk = lpc18xx_cgu_register_pll(&lpc18xx_cgu_src_clk_plls[i],
620b04e0b8fSJoachim Eastwood 						   base);
621b04e0b8fSJoachim Eastwood 		if (IS_ERR(clk))
622b04e0b8fSJoachim Eastwood 			pr_warn("%s: failed to register pll (%d)\n", __func__, i);
623b04e0b8fSJoachim Eastwood 	}
624b04e0b8fSJoachim Eastwood 
625b04e0b8fSJoachim Eastwood 	/* Register all clock dividers A-E */
626b04e0b8fSJoachim Eastwood 	for (i = 0; i < ARRAY_SIZE(lpc18xx_cgu_src_clk_divs); i++) {
627b04e0b8fSJoachim Eastwood 		clk = lpc18xx_cgu_register_div(&lpc18xx_cgu_src_clk_divs[i],
628b04e0b8fSJoachim Eastwood 					       base, i);
629b04e0b8fSJoachim Eastwood 		if (IS_ERR(clk))
630b04e0b8fSJoachim Eastwood 			pr_warn("%s: failed to register div %d\n", __func__, i);
631b04e0b8fSJoachim Eastwood 	}
632b04e0b8fSJoachim Eastwood }
633b04e0b8fSJoachim Eastwood 
634b04e0b8fSJoachim Eastwood static struct clk *clk_base[BASE_CLK_MAX];
635b04e0b8fSJoachim Eastwood static struct clk_onecell_data clk_base_data = {
636b04e0b8fSJoachim Eastwood 	.clks = clk_base,
637b04e0b8fSJoachim Eastwood 	.clk_num = BASE_CLK_MAX,
638b04e0b8fSJoachim Eastwood };
639b04e0b8fSJoachim Eastwood 
lpc18xx_cgu_register_base_clks(void __iomem * reg_base)640b04e0b8fSJoachim Eastwood static void __init lpc18xx_cgu_register_base_clks(void __iomem *reg_base)
641b04e0b8fSJoachim Eastwood {
642b04e0b8fSJoachim Eastwood 	int i;
643b04e0b8fSJoachim Eastwood 
644b04e0b8fSJoachim Eastwood 	for (i = BASE_SAFE_CLK; i < BASE_CLK_MAX; i++) {
645b04e0b8fSJoachim Eastwood 		clk_base[i] = lpc18xx_register_base_clk(&lpc18xx_cgu_base_clks[i],
646b04e0b8fSJoachim Eastwood 							reg_base, i);
647b04e0b8fSJoachim Eastwood 		if (IS_ERR(clk_base[i]) && PTR_ERR(clk_base[i]) != -ENOENT)
648b04e0b8fSJoachim Eastwood 			pr_warn("%s: register base clk %d failed\n", __func__, i);
649b04e0b8fSJoachim Eastwood 	}
650b04e0b8fSJoachim Eastwood }
651b04e0b8fSJoachim Eastwood 
lpc18xx_cgu_init(struct device_node * np)652b04e0b8fSJoachim Eastwood static void __init lpc18xx_cgu_init(struct device_node *np)
653b04e0b8fSJoachim Eastwood {
654b04e0b8fSJoachim Eastwood 	void __iomem *reg_base;
655b04e0b8fSJoachim Eastwood 
656b04e0b8fSJoachim Eastwood 	reg_base = of_iomap(np, 0);
657b04e0b8fSJoachim Eastwood 	if (!reg_base) {
658b04e0b8fSJoachim Eastwood 		pr_warn("%s: failed to map address range\n", __func__);
659b04e0b8fSJoachim Eastwood 		return;
660b04e0b8fSJoachim Eastwood 	}
661b04e0b8fSJoachim Eastwood 
662b04e0b8fSJoachim Eastwood 	lpc18xx_cgu_register_source_clks(np, reg_base);
663b04e0b8fSJoachim Eastwood 	lpc18xx_cgu_register_base_clks(reg_base);
664b04e0b8fSJoachim Eastwood 
665b04e0b8fSJoachim Eastwood 	of_clk_add_provider(np, of_clk_src_onecell_get, &clk_base_data);
666b04e0b8fSJoachim Eastwood }
667b04e0b8fSJoachim Eastwood CLK_OF_DECLARE(lpc18xx_cgu, "nxp,lpc1850-cgu", lpc18xx_cgu_init);
668