1d3d04f6cSJoel Stanley // SPDX-License-Identifier: GPL-2.0-or-later
2d3d04f6cSJoel Stanley // Copyright IBM Corp
3d3d04f6cSJoel Stanley // Copyright ASPEED Technology
4d3d04f6cSJoel Stanley
5d3d04f6cSJoel Stanley #define pr_fmt(fmt) "clk-ast2600: " fmt
6d3d04f6cSJoel Stanley
7d3d04f6cSJoel Stanley #include <linux/mfd/syscon.h>
8a96cbb14SRob Herring #include <linux/mod_devicetable.h>
9d3d04f6cSJoel Stanley #include <linux/of_address.h>
10d3d04f6cSJoel Stanley #include <linux/platform_device.h>
11d3d04f6cSJoel Stanley #include <linux/regmap.h>
12d3d04f6cSJoel Stanley #include <linux/slab.h>
13d3d04f6cSJoel Stanley
14d3d04f6cSJoel Stanley #include <dt-bindings/clock/ast2600-clock.h>
15d3d04f6cSJoel Stanley
16d3d04f6cSJoel Stanley #include "clk-aspeed.h"
17d3d04f6cSJoel Stanley
18e9d23014SJeremy Kerr /*
19e9d23014SJeremy Kerr * This includes the gates (configured from aspeed_g6_gates), plus the
20e9d23014SJeremy Kerr * explicitly-configured clocks (ASPEED_CLK_HPLL and up).
21e9d23014SJeremy Kerr */
22e9d23014SJeremy Kerr #define ASPEED_G6_NUM_CLKS 72
23d3d04f6cSJoel Stanley
246286ce1eSRyan Chen #define ASPEED_G6_SILICON_REV 0x014
256286ce1eSRyan Chen #define CHIP_REVISION_ID GENMASK(23, 16)
26d3d04f6cSJoel Stanley
27d3d04f6cSJoel Stanley #define ASPEED_G6_RESET_CTRL 0x040
28d3d04f6cSJoel Stanley #define ASPEED_G6_RESET_CTRL2 0x050
29d3d04f6cSJoel Stanley
30d3d04f6cSJoel Stanley #define ASPEED_G6_CLK_STOP_CTRL 0x080
31d3d04f6cSJoel Stanley #define ASPEED_G6_CLK_STOP_CTRL2 0x090
32d3d04f6cSJoel Stanley
33d3d04f6cSJoel Stanley #define ASPEED_G6_MISC_CTRL 0x0C0
34d3d04f6cSJoel Stanley #define UART_DIV13_EN BIT(12)
35d3d04f6cSJoel Stanley
36d3d04f6cSJoel Stanley #define ASPEED_G6_CLK_SELECTION1 0x300
37d3d04f6cSJoel Stanley #define ASPEED_G6_CLK_SELECTION2 0x304
38d3d04f6cSJoel Stanley #define ASPEED_G6_CLK_SELECTION4 0x310
39e9d23014SJeremy Kerr #define ASPEED_G6_CLK_SELECTION5 0x314
40e9d23014SJeremy Kerr #define I3C_CLK_SELECTION_SHIFT 31
41e9d23014SJeremy Kerr #define I3C_CLK_SELECTION BIT(31)
42e9d23014SJeremy Kerr #define I3C_CLK_SELECT_HCLK (0 << I3C_CLK_SELECTION_SHIFT)
43e9d23014SJeremy Kerr #define I3C_CLK_SELECT_APLL_DIV (1 << I3C_CLK_SELECTION_SHIFT)
44e9d23014SJeremy Kerr #define APLL_DIV_SELECTION_SHIFT 28
45e9d23014SJeremy Kerr #define APLL_DIV_SELECTION GENMASK(30, 28)
46e9d23014SJeremy Kerr #define APLL_DIV_2 (0b001 << APLL_DIV_SELECTION_SHIFT)
47e9d23014SJeremy Kerr #define APLL_DIV_3 (0b010 << APLL_DIV_SELECTION_SHIFT)
48e9d23014SJeremy Kerr #define APLL_DIV_4 (0b011 << APLL_DIV_SELECTION_SHIFT)
49e9d23014SJeremy Kerr #define APLL_DIV_5 (0b100 << APLL_DIV_SELECTION_SHIFT)
50e9d23014SJeremy Kerr #define APLL_DIV_6 (0b101 << APLL_DIV_SELECTION_SHIFT)
51e9d23014SJeremy Kerr #define APLL_DIV_7 (0b110 << APLL_DIV_SELECTION_SHIFT)
52e9d23014SJeremy Kerr #define APLL_DIV_8 (0b111 << APLL_DIV_SELECTION_SHIFT)
53d3d04f6cSJoel Stanley
54d3d04f6cSJoel Stanley #define ASPEED_HPLL_PARAM 0x200
55d3d04f6cSJoel Stanley #define ASPEED_APLL_PARAM 0x210
56d3d04f6cSJoel Stanley #define ASPEED_MPLL_PARAM 0x220
57d3d04f6cSJoel Stanley #define ASPEED_EPLL_PARAM 0x240
58d3d04f6cSJoel Stanley #define ASPEED_DPLL_PARAM 0x260
59d3d04f6cSJoel Stanley
60d3d04f6cSJoel Stanley #define ASPEED_G6_STRAP1 0x500
61d3d04f6cSJoel Stanley
623696eebdSAndrew Jeffery #define ASPEED_MAC12_CLK_DLY 0x340
633696eebdSAndrew Jeffery #define ASPEED_MAC34_CLK_DLY 0x350
643696eebdSAndrew Jeffery
65d3d04f6cSJoel Stanley /* Globally visible clocks */
66d3d04f6cSJoel Stanley static DEFINE_SPINLOCK(aspeed_g6_clk_lock);
67d3d04f6cSJoel Stanley
68d3d04f6cSJoel Stanley /* Keeps track of all clocks */
69d3d04f6cSJoel Stanley static struct clk_hw_onecell_data *aspeed_g6_clk_data;
70d3d04f6cSJoel Stanley
71d3d04f6cSJoel Stanley static void __iomem *scu_g6_base;
72f45c5b1cSJoel Stanley /* AST2600 revision: A0, A1, A2, etc */
73f45c5b1cSJoel Stanley static u8 soc_rev;
74d3d04f6cSJoel Stanley
75d3d04f6cSJoel Stanley /*
761ef491e2SJeremy Kerr * The majority of the clocks in the system are gates paired with a reset
771ef491e2SJeremy Kerr * controller that holds the IP in reset; this is represented by the @reset_idx
781ef491e2SJeremy Kerr * member of entries here.
791ef491e2SJeremy Kerr *
801ef491e2SJeremy Kerr * This borrows from clk_hw_register_gate, but registers two 'gates', one
811ef491e2SJeremy Kerr * to control the clock enable register and the other to control the reset
821ef491e2SJeremy Kerr * IP. This allows us to enforce the ordering:
831ef491e2SJeremy Kerr *
841ef491e2SJeremy Kerr * 1. Place IP in reset
851ef491e2SJeremy Kerr * 2. Enable clock
861ef491e2SJeremy Kerr * 3. Delay
871ef491e2SJeremy Kerr * 4. Release reset
881ef491e2SJeremy Kerr *
891ef491e2SJeremy Kerr * Consequently, if reset_idx is set, reset control is implicit: the clock
901ef491e2SJeremy Kerr * consumer does not need its own reset handling, as enabling the clock will
911ef491e2SJeremy Kerr * also deassert reset.
921ef491e2SJeremy Kerr *
931ef491e2SJeremy Kerr * There are some gates that do not have an associated reset; these are
941ef491e2SJeremy Kerr * handled by using -1 as the index for the reset, and the consumer must
951ef491e2SJeremy Kerr * explictly assert/deassert reset lines as required.
961ef491e2SJeremy Kerr *
97d3d04f6cSJoel Stanley * Clocks marked with CLK_IS_CRITICAL:
98d3d04f6cSJoel Stanley *
99d3d04f6cSJoel Stanley * ref0 and ref1 are essential for the SoC to operate
100d3d04f6cSJoel Stanley * mpll is required if SDRAM is used
101d3d04f6cSJoel Stanley */
102d3d04f6cSJoel Stanley static const struct aspeed_gate_data aspeed_g6_gates[] = {
103d3d04f6cSJoel Stanley /* clk rst name parent flags */
104d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_MCLK] = { 0, -1, "mclk-gate", "mpll", CLK_IS_CRITICAL }, /* SDRAM */
1053536169fSJae Hyun Yoo [ASPEED_CLK_GATE_ECLK] = { 1, 6, "eclk-gate", "eclk", 0 }, /* Video Engine */
106d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_GCLK] = { 2, 7, "gclk-gate", NULL, 0 }, /* 2D engine */
107d3d04f6cSJoel Stanley /* vclk parent - dclk/d1clk/hclk/mclk */
1083536169fSJae Hyun Yoo [ASPEED_CLK_GATE_VCLK] = { 3, -1, "vclk-gate", NULL, 0 }, /* Video Capture */
109*4c8f2e32SJae Hyun Yoo [ASPEED_CLK_GATE_BCLK] = { 4, 8, "bclk-gate", "bclk", CLK_IS_CRITICAL }, /* PCIe/PCI */
110d3d04f6cSJoel Stanley /* From dpll */
111d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_DCLK] = { 5, -1, "dclk-gate", NULL, CLK_IS_CRITICAL }, /* DAC */
112d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_REF0CLK] = { 6, -1, "ref0clk-gate", "clkin", CLK_IS_CRITICAL },
113d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_USBPORT2CLK] = { 7, 3, "usb-port2-gate", NULL, 0 }, /* USB2.0 Host port 2 */
114d3d04f6cSJoel Stanley /* Reserved 8 */
115d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_USBUHCICLK] = { 9, 15, "usb-uhci-gate", NULL, 0 }, /* USB1.1 (requires port 2 enabled) */
116d3d04f6cSJoel Stanley /* From dpll/epll/40mhz usb p1 phy/gpioc6/dp phy pll */
117d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_D1CLK] = { 10, 13, "d1clk-gate", "d1clk", 0 }, /* GFX CRT */
118d3d04f6cSJoel Stanley /* Reserved 11/12 */
119d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_YCLK] = { 13, 4, "yclk-gate", NULL, 0 }, /* HAC */
120d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_USBPORT1CLK] = { 14, 14, "usb-port1-gate", NULL, 0 }, /* USB2 hub/USB2 host port 1/USB1.1 dev */
121d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART5CLK] = { 15, -1, "uart5clk-gate", "uart", 0 }, /* UART5 */
122d3d04f6cSJoel Stanley /* Reserved 16/19 */
123d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_MAC1CLK] = { 20, 11, "mac1clk-gate", "mac12", 0 }, /* MAC1 */
124d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_MAC2CLK] = { 21, 12, "mac2clk-gate", "mac12", 0 }, /* MAC2 */
125d3d04f6cSJoel Stanley /* Reserved 22/23 */
126d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_RSACLK] = { 24, 4, "rsaclk-gate", NULL, 0 }, /* HAC */
127d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_RVASCLK] = { 25, 9, "rvasclk-gate", NULL, 0 }, /* RVAS */
128d3d04f6cSJoel Stanley /* Reserved 26 */
129d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_EMMCCLK] = { 27, 16, "emmcclk-gate", NULL, 0 }, /* For card clk */
130d3d04f6cSJoel Stanley /* Reserved 28/29/30 */
131d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_LCLK] = { 32, 32, "lclk-gate", NULL, 0 }, /* LPC */
132d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_ESPICLK] = { 33, -1, "espiclk-gate", NULL, 0 }, /* eSPI */
133d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_REF1CLK] = { 34, -1, "ref1clk-gate", "clkin", CLK_IS_CRITICAL },
134d3d04f6cSJoel Stanley /* Reserved 35 */
135d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_SDCLK] = { 36, 56, "sdclk-gate", NULL, 0 }, /* SDIO/SD */
136d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_LHCCLK] = { 37, -1, "lhclk-gate", "lhclk", 0 }, /* LPC master/LPC+ */
137d3d04f6cSJoel Stanley /* Reserved 38 RSA: no longer used */
138d3d04f6cSJoel Stanley /* Reserved 39 */
139e9d23014SJeremy Kerr [ASPEED_CLK_GATE_I3C0CLK] = { 40, 40, "i3c0clk-gate", "i3cclk", 0 }, /* I3C0 */
140e9d23014SJeremy Kerr [ASPEED_CLK_GATE_I3C1CLK] = { 41, 41, "i3c1clk-gate", "i3cclk", 0 }, /* I3C1 */
141e9d23014SJeremy Kerr [ASPEED_CLK_GATE_I3C2CLK] = { 42, 42, "i3c2clk-gate", "i3cclk", 0 }, /* I3C2 */
142e9d23014SJeremy Kerr [ASPEED_CLK_GATE_I3C3CLK] = { 43, 43, "i3c3clk-gate", "i3cclk", 0 }, /* I3C3 */
143e9d23014SJeremy Kerr [ASPEED_CLK_GATE_I3C4CLK] = { 44, 44, "i3c4clk-gate", "i3cclk", 0 }, /* I3C4 */
144e9d23014SJeremy Kerr [ASPEED_CLK_GATE_I3C5CLK] = { 45, 45, "i3c5clk-gate", "i3cclk", 0 }, /* I3C5 */
145e9d23014SJeremy Kerr /* Reserved: 46 & 47 */
146d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART1CLK] = { 48, -1, "uart1clk-gate", "uart", 0 }, /* UART1 */
147d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART2CLK] = { 49, -1, "uart2clk-gate", "uart", 0 }, /* UART2 */
148d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART3CLK] = { 50, -1, "uart3clk-gate", "uart", 0 }, /* UART3 */
149d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART4CLK] = { 51, -1, "uart4clk-gate", "uart", 0 }, /* UART4 */
150d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_MAC3CLK] = { 52, 52, "mac3clk-gate", "mac34", 0 }, /* MAC3 */
151d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_MAC4CLK] = { 53, 53, "mac4clk-gate", "mac34", 0 }, /* MAC4 */
152d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART6CLK] = { 54, -1, "uart6clk-gate", "uartx", 0 }, /* UART6 */
153d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART7CLK] = { 55, -1, "uart7clk-gate", "uartx", 0 }, /* UART7 */
154d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART8CLK] = { 56, -1, "uart8clk-gate", "uartx", 0 }, /* UART8 */
155d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART9CLK] = { 57, -1, "uart9clk-gate", "uartx", 0 }, /* UART9 */
156d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART10CLK] = { 58, -1, "uart10clk-gate", "uartx", 0 }, /* UART10 */
157d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART11CLK] = { 59, -1, "uart11clk-gate", "uartx", 0 }, /* UART11 */
158d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART12CLK] = { 60, -1, "uart12clk-gate", "uartx", 0 }, /* UART12 */
159d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_UART13CLK] = { 61, -1, "uart13clk-gate", "uartx", 0 }, /* UART13 */
160d3d04f6cSJoel Stanley [ASPEED_CLK_GATE_FSICLK] = { 62, 59, "fsiclk-gate", NULL, 0 }, /* FSI */
161d3d04f6cSJoel Stanley };
162d3d04f6cSJoel Stanley
163d3d04f6cSJoel Stanley static const struct clk_div_table ast2600_eclk_div_table[] = {
164d3d04f6cSJoel Stanley { 0x0, 2 },
165d3d04f6cSJoel Stanley { 0x1, 2 },
166d3d04f6cSJoel Stanley { 0x2, 3 },
167d3d04f6cSJoel Stanley { 0x3, 4 },
168d3d04f6cSJoel Stanley { 0x4, 5 },
169d3d04f6cSJoel Stanley { 0x5, 6 },
170d3d04f6cSJoel Stanley { 0x6, 7 },
171d3d04f6cSJoel Stanley { 0x7, 8 },
172d3d04f6cSJoel Stanley { 0 }
173d3d04f6cSJoel Stanley };
174d3d04f6cSJoel Stanley
175c2407ab3SEddie James static const struct clk_div_table ast2600_emmc_extclk_div_table[] = {
176c2407ab3SEddie James { 0x0, 2 },
177c2407ab3SEddie James { 0x1, 4 },
178c2407ab3SEddie James { 0x2, 6 },
179c2407ab3SEddie James { 0x3, 8 },
180c2407ab3SEddie James { 0x4, 10 },
181c2407ab3SEddie James { 0x5, 12 },
182c2407ab3SEddie James { 0x6, 14 },
183c2407ab3SEddie James { 0x7, 16 },
184c2407ab3SEddie James { 0 }
185c2407ab3SEddie James };
186c2407ab3SEddie James
187d3d04f6cSJoel Stanley static const struct clk_div_table ast2600_mac_div_table[] = {
188d3d04f6cSJoel Stanley { 0x0, 4 },
189d3d04f6cSJoel Stanley { 0x1, 4 },
190d3d04f6cSJoel Stanley { 0x2, 6 },
191d3d04f6cSJoel Stanley { 0x3, 8 },
192d3d04f6cSJoel Stanley { 0x4, 10 },
193d3d04f6cSJoel Stanley { 0x5, 12 },
194d3d04f6cSJoel Stanley { 0x6, 14 },
195d3d04f6cSJoel Stanley { 0x7, 16 },
196d3d04f6cSJoel Stanley { 0 }
197d3d04f6cSJoel Stanley };
198d3d04f6cSJoel Stanley
199d3d04f6cSJoel Stanley static const struct clk_div_table ast2600_div_table[] = {
200d3d04f6cSJoel Stanley { 0x0, 4 },
201d3d04f6cSJoel Stanley { 0x1, 8 },
202d3d04f6cSJoel Stanley { 0x2, 12 },
203d3d04f6cSJoel Stanley { 0x3, 16 },
204d3d04f6cSJoel Stanley { 0x4, 20 },
205d3d04f6cSJoel Stanley { 0x5, 24 },
206d3d04f6cSJoel Stanley { 0x6, 28 },
207d3d04f6cSJoel Stanley { 0x7, 32 },
208d3d04f6cSJoel Stanley { 0 }
209d3d04f6cSJoel Stanley };
210d3d04f6cSJoel Stanley
211d3d04f6cSJoel Stanley /* For hpll/dpll/epll/mpll */
ast2600_calc_pll(const char * name,u32 val)212d3d04f6cSJoel Stanley static struct clk_hw *ast2600_calc_pll(const char *name, u32 val)
213d3d04f6cSJoel Stanley {
214d3d04f6cSJoel Stanley unsigned int mult, div;
215d3d04f6cSJoel Stanley
216d3d04f6cSJoel Stanley if (val & BIT(24)) {
217d3d04f6cSJoel Stanley /* Pass through mode */
218d3d04f6cSJoel Stanley mult = div = 1;
219d3d04f6cSJoel Stanley } else {
220d3d04f6cSJoel Stanley /* F = 25Mhz * [(M + 2) / (n + 1)] / (p + 1) */
221d3d04f6cSJoel Stanley u32 m = val & 0x1fff;
222d3d04f6cSJoel Stanley u32 n = (val >> 13) & 0x3f;
223d3d04f6cSJoel Stanley u32 p = (val >> 19) & 0xf;
224d3d04f6cSJoel Stanley mult = (m + 1) / (n + 1);
225d3d04f6cSJoel Stanley div = (p + 1);
226d3d04f6cSJoel Stanley }
227d3d04f6cSJoel Stanley return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
228d3d04f6cSJoel Stanley mult, div);
229d3d04f6cSJoel Stanley };
230d3d04f6cSJoel Stanley
ast2600_calc_apll(const char * name,u32 val)231d3d04f6cSJoel Stanley static struct clk_hw *ast2600_calc_apll(const char *name, u32 val)
232d3d04f6cSJoel Stanley {
233d3d04f6cSJoel Stanley unsigned int mult, div;
234d3d04f6cSJoel Stanley
235f45c5b1cSJoel Stanley if (soc_rev >= 2) {
2366286ce1eSRyan Chen if (val & BIT(24)) {
2376286ce1eSRyan Chen /* Pass through mode */
2386286ce1eSRyan Chen mult = div = 1;
2396286ce1eSRyan Chen } else {
2406286ce1eSRyan Chen /* F = 25Mhz * [(m + 1) / (n + 1)] / (p + 1) */
2416286ce1eSRyan Chen u32 m = val & 0x1fff;
2426286ce1eSRyan Chen u32 n = (val >> 13) & 0x3f;
2436286ce1eSRyan Chen u32 p = (val >> 19) & 0xf;
2446286ce1eSRyan Chen
2456286ce1eSRyan Chen mult = (m + 1);
2466286ce1eSRyan Chen div = (n + 1) * (p + 1);
2476286ce1eSRyan Chen }
2486286ce1eSRyan Chen } else {
249d3d04f6cSJoel Stanley if (val & BIT(20)) {
250d3d04f6cSJoel Stanley /* Pass through mode */
251d3d04f6cSJoel Stanley mult = div = 1;
252d3d04f6cSJoel Stanley } else {
253d3d04f6cSJoel Stanley /* F = 25Mhz * (2-od) * [(m + 2) / (n + 1)] */
254d3d04f6cSJoel Stanley u32 m = (val >> 5) & 0x3f;
255d3d04f6cSJoel Stanley u32 od = (val >> 4) & 0x1;
256d3d04f6cSJoel Stanley u32 n = val & 0xf;
257d3d04f6cSJoel Stanley
258d3d04f6cSJoel Stanley mult = (2 - od) * (m + 2);
259d3d04f6cSJoel Stanley div = n + 1;
260d3d04f6cSJoel Stanley }
2616286ce1eSRyan Chen }
262d3d04f6cSJoel Stanley return clk_hw_register_fixed_factor(NULL, name, "clkin", 0,
263d3d04f6cSJoel Stanley mult, div);
264d3d04f6cSJoel Stanley };
265d3d04f6cSJoel Stanley
get_bit(u8 idx)266d3d04f6cSJoel Stanley static u32 get_bit(u8 idx)
267d3d04f6cSJoel Stanley {
268d3d04f6cSJoel Stanley return BIT(idx % 32);
269d3d04f6cSJoel Stanley }
270d3d04f6cSJoel Stanley
get_reset_reg(struct aspeed_clk_gate * gate)271d3d04f6cSJoel Stanley static u32 get_reset_reg(struct aspeed_clk_gate *gate)
272d3d04f6cSJoel Stanley {
273d3d04f6cSJoel Stanley if (gate->reset_idx < 32)
274d3d04f6cSJoel Stanley return ASPEED_G6_RESET_CTRL;
275d3d04f6cSJoel Stanley
276d3d04f6cSJoel Stanley return ASPEED_G6_RESET_CTRL2;
277d3d04f6cSJoel Stanley }
278d3d04f6cSJoel Stanley
get_clock_reg(struct aspeed_clk_gate * gate)279d3d04f6cSJoel Stanley static u32 get_clock_reg(struct aspeed_clk_gate *gate)
280d3d04f6cSJoel Stanley {
281d3d04f6cSJoel Stanley if (gate->clock_idx < 32)
282d3d04f6cSJoel Stanley return ASPEED_G6_CLK_STOP_CTRL;
283d3d04f6cSJoel Stanley
284d3d04f6cSJoel Stanley return ASPEED_G6_CLK_STOP_CTRL2;
285d3d04f6cSJoel Stanley }
286d3d04f6cSJoel Stanley
aspeed_g6_clk_is_enabled(struct clk_hw * hw)287d3d04f6cSJoel Stanley static int aspeed_g6_clk_is_enabled(struct clk_hw *hw)
288d3d04f6cSJoel Stanley {
289d3d04f6cSJoel Stanley struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
290d3d04f6cSJoel Stanley u32 clk = get_bit(gate->clock_idx);
291d3d04f6cSJoel Stanley u32 rst = get_bit(gate->reset_idx);
292d3d04f6cSJoel Stanley u32 reg;
293d3d04f6cSJoel Stanley u32 enval;
294d3d04f6cSJoel Stanley
295d3d04f6cSJoel Stanley /*
296d3d04f6cSJoel Stanley * If the IP is in reset, treat the clock as not enabled,
297d3d04f6cSJoel Stanley * this happens with some clocks such as the USB one when
298d3d04f6cSJoel Stanley * coming from cold reset. Without this, aspeed_clk_enable()
299d3d04f6cSJoel Stanley * will fail to lift the reset.
300d3d04f6cSJoel Stanley */
301d3d04f6cSJoel Stanley if (gate->reset_idx >= 0) {
302d3d04f6cSJoel Stanley regmap_read(gate->map, get_reset_reg(gate), ®);
303d3d04f6cSJoel Stanley
304d3d04f6cSJoel Stanley if (reg & rst)
305d3d04f6cSJoel Stanley return 0;
306d3d04f6cSJoel Stanley }
307d3d04f6cSJoel Stanley
308d3d04f6cSJoel Stanley regmap_read(gate->map, get_clock_reg(gate), ®);
309d3d04f6cSJoel Stanley
310d3d04f6cSJoel Stanley enval = (gate->flags & CLK_GATE_SET_TO_DISABLE) ? 0 : clk;
311d3d04f6cSJoel Stanley
312d3d04f6cSJoel Stanley return ((reg & clk) == enval) ? 1 : 0;
313d3d04f6cSJoel Stanley }
314d3d04f6cSJoel Stanley
aspeed_g6_clk_enable(struct clk_hw * hw)315d3d04f6cSJoel Stanley static int aspeed_g6_clk_enable(struct clk_hw *hw)
316d3d04f6cSJoel Stanley {
317d3d04f6cSJoel Stanley struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
318d3d04f6cSJoel Stanley unsigned long flags;
319d3d04f6cSJoel Stanley u32 clk = get_bit(gate->clock_idx);
320d3d04f6cSJoel Stanley u32 rst = get_bit(gate->reset_idx);
321d3d04f6cSJoel Stanley
322d3d04f6cSJoel Stanley spin_lock_irqsave(gate->lock, flags);
323d3d04f6cSJoel Stanley
324d3d04f6cSJoel Stanley if (aspeed_g6_clk_is_enabled(hw)) {
325d3d04f6cSJoel Stanley spin_unlock_irqrestore(gate->lock, flags);
326d3d04f6cSJoel Stanley return 0;
327d3d04f6cSJoel Stanley }
328d3d04f6cSJoel Stanley
329d3d04f6cSJoel Stanley if (gate->reset_idx >= 0) {
330d3d04f6cSJoel Stanley /* Put IP in reset */
331d3d04f6cSJoel Stanley regmap_write(gate->map, get_reset_reg(gate), rst);
332d3d04f6cSJoel Stanley /* Delay 100us */
333d3d04f6cSJoel Stanley udelay(100);
334d3d04f6cSJoel Stanley }
335d3d04f6cSJoel Stanley
336d3d04f6cSJoel Stanley /* Enable clock */
337d3d04f6cSJoel Stanley if (gate->flags & CLK_GATE_SET_TO_DISABLE) {
338427400fcSJoel Stanley /* Clock is clear to enable, so use set to clear register */
339d3d04f6cSJoel Stanley regmap_write(gate->map, get_clock_reg(gate) + 0x04, clk);
340427400fcSJoel Stanley } else {
341427400fcSJoel Stanley /* Clock is set to enable, so use write to set register */
342427400fcSJoel Stanley regmap_write(gate->map, get_clock_reg(gate), clk);
343d3d04f6cSJoel Stanley }
344d3d04f6cSJoel Stanley
345d3d04f6cSJoel Stanley if (gate->reset_idx >= 0) {
346d3d04f6cSJoel Stanley /* A delay of 10ms is specified by the ASPEED docs */
347d3d04f6cSJoel Stanley mdelay(10);
348d3d04f6cSJoel Stanley /* Take IP out of reset */
349d3d04f6cSJoel Stanley regmap_write(gate->map, get_reset_reg(gate) + 0x4, rst);
350d3d04f6cSJoel Stanley }
351d3d04f6cSJoel Stanley
352d3d04f6cSJoel Stanley spin_unlock_irqrestore(gate->lock, flags);
353d3d04f6cSJoel Stanley
354d3d04f6cSJoel Stanley return 0;
355d3d04f6cSJoel Stanley }
356d3d04f6cSJoel Stanley
aspeed_g6_clk_disable(struct clk_hw * hw)357d3d04f6cSJoel Stanley static void aspeed_g6_clk_disable(struct clk_hw *hw)
358d3d04f6cSJoel Stanley {
359d3d04f6cSJoel Stanley struct aspeed_clk_gate *gate = to_aspeed_clk_gate(hw);
360d3d04f6cSJoel Stanley unsigned long flags;
361d3d04f6cSJoel Stanley u32 clk = get_bit(gate->clock_idx);
362d3d04f6cSJoel Stanley
363d3d04f6cSJoel Stanley spin_lock_irqsave(gate->lock, flags);
364d3d04f6cSJoel Stanley
365d3d04f6cSJoel Stanley if (gate->flags & CLK_GATE_SET_TO_DISABLE) {
366d3d04f6cSJoel Stanley regmap_write(gate->map, get_clock_reg(gate), clk);
367d3d04f6cSJoel Stanley } else {
368d3d04f6cSJoel Stanley /* Use set to clear register */
369d3d04f6cSJoel Stanley regmap_write(gate->map, get_clock_reg(gate) + 0x4, clk);
370d3d04f6cSJoel Stanley }
371d3d04f6cSJoel Stanley
372d3d04f6cSJoel Stanley spin_unlock_irqrestore(gate->lock, flags);
373d3d04f6cSJoel Stanley }
374d3d04f6cSJoel Stanley
375d3d04f6cSJoel Stanley static const struct clk_ops aspeed_g6_clk_gate_ops = {
376d3d04f6cSJoel Stanley .enable = aspeed_g6_clk_enable,
377d3d04f6cSJoel Stanley .disable = aspeed_g6_clk_disable,
378d3d04f6cSJoel Stanley .is_enabled = aspeed_g6_clk_is_enabled,
379d3d04f6cSJoel Stanley };
380d3d04f6cSJoel Stanley
aspeed_g6_reset_deassert(struct reset_controller_dev * rcdev,unsigned long id)381d3d04f6cSJoel Stanley static int aspeed_g6_reset_deassert(struct reset_controller_dev *rcdev,
382d3d04f6cSJoel Stanley unsigned long id)
383d3d04f6cSJoel Stanley {
384d3d04f6cSJoel Stanley struct aspeed_reset *ar = to_aspeed_reset(rcdev);
385d3d04f6cSJoel Stanley u32 rst = get_bit(id);
386d3d04f6cSJoel Stanley u32 reg = id >= 32 ? ASPEED_G6_RESET_CTRL2 : ASPEED_G6_RESET_CTRL;
387d3d04f6cSJoel Stanley
388d3d04f6cSJoel Stanley /* Use set to clear register */
389d3d04f6cSJoel Stanley return regmap_write(ar->map, reg + 0x04, rst);
390d3d04f6cSJoel Stanley }
391d3d04f6cSJoel Stanley
aspeed_g6_reset_assert(struct reset_controller_dev * rcdev,unsigned long id)392d3d04f6cSJoel Stanley static int aspeed_g6_reset_assert(struct reset_controller_dev *rcdev,
393d3d04f6cSJoel Stanley unsigned long id)
394d3d04f6cSJoel Stanley {
395d3d04f6cSJoel Stanley struct aspeed_reset *ar = to_aspeed_reset(rcdev);
396d3d04f6cSJoel Stanley u32 rst = get_bit(id);
397d3d04f6cSJoel Stanley u32 reg = id >= 32 ? ASPEED_G6_RESET_CTRL2 : ASPEED_G6_RESET_CTRL;
398d3d04f6cSJoel Stanley
399d3d04f6cSJoel Stanley return regmap_write(ar->map, reg, rst);
400d3d04f6cSJoel Stanley }
401d3d04f6cSJoel Stanley
aspeed_g6_reset_status(struct reset_controller_dev * rcdev,unsigned long id)402d3d04f6cSJoel Stanley static int aspeed_g6_reset_status(struct reset_controller_dev *rcdev,
403d3d04f6cSJoel Stanley unsigned long id)
404d3d04f6cSJoel Stanley {
405d3d04f6cSJoel Stanley struct aspeed_reset *ar = to_aspeed_reset(rcdev);
406d3d04f6cSJoel Stanley int ret;
407d3d04f6cSJoel Stanley u32 val;
408d3d04f6cSJoel Stanley u32 rst = get_bit(id);
409d3d04f6cSJoel Stanley u32 reg = id >= 32 ? ASPEED_G6_RESET_CTRL2 : ASPEED_G6_RESET_CTRL;
410d3d04f6cSJoel Stanley
411d3d04f6cSJoel Stanley ret = regmap_read(ar->map, reg, &val);
412d3d04f6cSJoel Stanley if (ret)
413d3d04f6cSJoel Stanley return ret;
414d3d04f6cSJoel Stanley
415d3d04f6cSJoel Stanley return !!(val & rst);
416d3d04f6cSJoel Stanley }
417d3d04f6cSJoel Stanley
418d3d04f6cSJoel Stanley static const struct reset_control_ops aspeed_g6_reset_ops = {
419d3d04f6cSJoel Stanley .assert = aspeed_g6_reset_assert,
420d3d04f6cSJoel Stanley .deassert = aspeed_g6_reset_deassert,
421d3d04f6cSJoel Stanley .status = aspeed_g6_reset_status,
422d3d04f6cSJoel Stanley };
423d3d04f6cSJoel Stanley
aspeed_g6_clk_hw_register_gate(struct device * dev,const char * name,const char * parent_name,unsigned long flags,struct regmap * map,u8 clock_idx,u8 reset_idx,u8 clk_gate_flags,spinlock_t * lock)424d3d04f6cSJoel Stanley static struct clk_hw *aspeed_g6_clk_hw_register_gate(struct device *dev,
425d3d04f6cSJoel Stanley const char *name, const char *parent_name, unsigned long flags,
426d3d04f6cSJoel Stanley struct regmap *map, u8 clock_idx, u8 reset_idx,
427d3d04f6cSJoel Stanley u8 clk_gate_flags, spinlock_t *lock)
428d3d04f6cSJoel Stanley {
429d3d04f6cSJoel Stanley struct aspeed_clk_gate *gate;
430d3d04f6cSJoel Stanley struct clk_init_data init;
431d3d04f6cSJoel Stanley struct clk_hw *hw;
432d3d04f6cSJoel Stanley int ret;
433d3d04f6cSJoel Stanley
434d3d04f6cSJoel Stanley gate = kzalloc(sizeof(*gate), GFP_KERNEL);
435d3d04f6cSJoel Stanley if (!gate)
436d3d04f6cSJoel Stanley return ERR_PTR(-ENOMEM);
437d3d04f6cSJoel Stanley
438d3d04f6cSJoel Stanley init.name = name;
439d3d04f6cSJoel Stanley init.ops = &aspeed_g6_clk_gate_ops;
440d3d04f6cSJoel Stanley init.flags = flags;
441d3d04f6cSJoel Stanley init.parent_names = parent_name ? &parent_name : NULL;
442d3d04f6cSJoel Stanley init.num_parents = parent_name ? 1 : 0;
443d3d04f6cSJoel Stanley
444d3d04f6cSJoel Stanley gate->map = map;
445d3d04f6cSJoel Stanley gate->clock_idx = clock_idx;
446d3d04f6cSJoel Stanley gate->reset_idx = reset_idx;
447d3d04f6cSJoel Stanley gate->flags = clk_gate_flags;
448d3d04f6cSJoel Stanley gate->lock = lock;
449d3d04f6cSJoel Stanley gate->hw.init = &init;
450d3d04f6cSJoel Stanley
451d3d04f6cSJoel Stanley hw = &gate->hw;
452d3d04f6cSJoel Stanley ret = clk_hw_register(dev, hw);
453d3d04f6cSJoel Stanley if (ret) {
454d3d04f6cSJoel Stanley kfree(gate);
455d3d04f6cSJoel Stanley hw = ERR_PTR(ret);
456d3d04f6cSJoel Stanley }
457d3d04f6cSJoel Stanley
458d3d04f6cSJoel Stanley return hw;
459d3d04f6cSJoel Stanley }
460d3d04f6cSJoel Stanley
461c2407ab3SEddie James static const char *const emmc_extclk_parent_names[] = {
462c2407ab3SEddie James "emmc_extclk_hpll_in",
463c2407ab3SEddie James "mpll",
464c2407ab3SEddie James };
465c2407ab3SEddie James
466d3d04f6cSJoel Stanley static const char * const vclk_parent_names[] = {
467d3d04f6cSJoel Stanley "dpll",
468d3d04f6cSJoel Stanley "d1pll",
469d3d04f6cSJoel Stanley "hclk",
470d3d04f6cSJoel Stanley "mclk",
471d3d04f6cSJoel Stanley };
472d3d04f6cSJoel Stanley
473d3d04f6cSJoel Stanley static const char * const d1clk_parent_names[] = {
474d3d04f6cSJoel Stanley "dpll",
475d3d04f6cSJoel Stanley "epll",
476d3d04f6cSJoel Stanley "usb-phy-40m",
477d3d04f6cSJoel Stanley "gpioc6_clkin",
478d3d04f6cSJoel Stanley "dp_phy_pll",
479d3d04f6cSJoel Stanley };
480d3d04f6cSJoel Stanley
aspeed_g6_clk_probe(struct platform_device * pdev)481d3d04f6cSJoel Stanley static int aspeed_g6_clk_probe(struct platform_device *pdev)
482d3d04f6cSJoel Stanley {
483d3d04f6cSJoel Stanley struct device *dev = &pdev->dev;
484d3d04f6cSJoel Stanley struct aspeed_reset *ar;
485d3d04f6cSJoel Stanley struct regmap *map;
486d3d04f6cSJoel Stanley struct clk_hw *hw;
487d3d04f6cSJoel Stanley u32 val, rate;
488d3d04f6cSJoel Stanley int i, ret;
489d3d04f6cSJoel Stanley
490d3d04f6cSJoel Stanley map = syscon_node_to_regmap(dev->of_node);
491d3d04f6cSJoel Stanley if (IS_ERR(map)) {
492d3d04f6cSJoel Stanley dev_err(dev, "no syscon regmap\n");
493d3d04f6cSJoel Stanley return PTR_ERR(map);
494d3d04f6cSJoel Stanley }
495d3d04f6cSJoel Stanley
496d3d04f6cSJoel Stanley ar = devm_kzalloc(dev, sizeof(*ar), GFP_KERNEL);
497d3d04f6cSJoel Stanley if (!ar)
498d3d04f6cSJoel Stanley return -ENOMEM;
499d3d04f6cSJoel Stanley
500d3d04f6cSJoel Stanley ar->map = map;
501d3d04f6cSJoel Stanley
502d3d04f6cSJoel Stanley ar->rcdev.owner = THIS_MODULE;
503d3d04f6cSJoel Stanley ar->rcdev.nr_resets = 64;
504d3d04f6cSJoel Stanley ar->rcdev.ops = &aspeed_g6_reset_ops;
505d3d04f6cSJoel Stanley ar->rcdev.of_node = dev->of_node;
506d3d04f6cSJoel Stanley
507d3d04f6cSJoel Stanley ret = devm_reset_controller_register(dev, &ar->rcdev);
508d3d04f6cSJoel Stanley if (ret) {
509d3d04f6cSJoel Stanley dev_err(dev, "could not register reset controller\n");
510d3d04f6cSJoel Stanley return ret;
511d3d04f6cSJoel Stanley }
512d3d04f6cSJoel Stanley
513d3d04f6cSJoel Stanley /* UART clock div13 setting */
514d3d04f6cSJoel Stanley regmap_read(map, ASPEED_G6_MISC_CTRL, &val);
515d3d04f6cSJoel Stanley if (val & UART_DIV13_EN)
516d3d04f6cSJoel Stanley rate = 24000000 / 13;
517d3d04f6cSJoel Stanley else
518d3d04f6cSJoel Stanley rate = 24000000;
519d3d04f6cSJoel Stanley hw = clk_hw_register_fixed_rate(dev, "uart", NULL, 0, rate);
520d3d04f6cSJoel Stanley if (IS_ERR(hw))
521d3d04f6cSJoel Stanley return PTR_ERR(hw);
522d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_UART] = hw;
523d3d04f6cSJoel Stanley
524d3d04f6cSJoel Stanley /* UART6~13 clock div13 setting */
525d3d04f6cSJoel Stanley regmap_read(map, 0x80, &val);
526d3d04f6cSJoel Stanley if (val & BIT(31))
527d3d04f6cSJoel Stanley rate = 24000000 / 13;
528d3d04f6cSJoel Stanley else
529d3d04f6cSJoel Stanley rate = 24000000;
530d3d04f6cSJoel Stanley hw = clk_hw_register_fixed_rate(dev, "uartx", NULL, 0, rate);
531d3d04f6cSJoel Stanley if (IS_ERR(hw))
532d3d04f6cSJoel Stanley return PTR_ERR(hw);
533d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_UARTX] = hw;
534d3d04f6cSJoel Stanley
535c2407ab3SEddie James /* EMMC ext clock */
536c2407ab3SEddie James hw = clk_hw_register_fixed_factor(dev, "emmc_extclk_hpll_in", "hpll",
537c2407ab3SEddie James 0, 1, 2);
538d3d04f6cSJoel Stanley if (IS_ERR(hw))
539d3d04f6cSJoel Stanley return PTR_ERR(hw);
540c2407ab3SEddie James
541c2407ab3SEddie James hw = clk_hw_register_mux(dev, "emmc_extclk_mux",
542c2407ab3SEddie James emmc_extclk_parent_names,
543c2407ab3SEddie James ARRAY_SIZE(emmc_extclk_parent_names), 0,
544c2407ab3SEddie James scu_g6_base + ASPEED_G6_CLK_SELECTION1, 11, 1,
545c2407ab3SEddie James 0, &aspeed_g6_clk_lock);
546c2407ab3SEddie James if (IS_ERR(hw))
547c2407ab3SEddie James return PTR_ERR(hw);
548c2407ab3SEddie James
549c2407ab3SEddie James hw = clk_hw_register_gate(dev, "emmc_extclk_gate", "emmc_extclk_mux",
550c2407ab3SEddie James 0, scu_g6_base + ASPEED_G6_CLK_SELECTION1,
551c2407ab3SEddie James 15, 0, &aspeed_g6_clk_lock);
552c2407ab3SEddie James if (IS_ERR(hw))
553c2407ab3SEddie James return PTR_ERR(hw);
554c2407ab3SEddie James
555c2407ab3SEddie James hw = clk_hw_register_divider_table(dev, "emmc_extclk",
556c2407ab3SEddie James "emmc_extclk_gate", 0,
557c2407ab3SEddie James scu_g6_base +
558c2407ab3SEddie James ASPEED_G6_CLK_SELECTION1, 12,
559c2407ab3SEddie James 3, 0, ast2600_emmc_extclk_div_table,
560d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
561d3d04f6cSJoel Stanley if (IS_ERR(hw))
562d3d04f6cSJoel Stanley return PTR_ERR(hw);
563d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_EMMC] = hw;
564d3d04f6cSJoel Stanley
565d3d04f6cSJoel Stanley /* SD/SDIO clock divider and gate */
566d3d04f6cSJoel Stanley hw = clk_hw_register_gate(dev, "sd_extclk_gate", "hpll", 0,
567d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION4, 31, 0,
568d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
569d3d04f6cSJoel Stanley if (IS_ERR(hw))
570d3d04f6cSJoel Stanley return PTR_ERR(hw);
571d3d04f6cSJoel Stanley hw = clk_hw_register_divider_table(dev, "sd_extclk", "sd_extclk_gate",
572d3d04f6cSJoel Stanley 0, scu_g6_base + ASPEED_G6_CLK_SELECTION4, 28, 3, 0,
573d3d04f6cSJoel Stanley ast2600_div_table,
574d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
575d3d04f6cSJoel Stanley if (IS_ERR(hw))
576d3d04f6cSJoel Stanley return PTR_ERR(hw);
577d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_SDIO] = hw;
578d3d04f6cSJoel Stanley
5793696eebdSAndrew Jeffery /* MAC1/2 RMII 50MHz RCLK */
5803696eebdSAndrew Jeffery hw = clk_hw_register_fixed_rate(dev, "mac12rclk", "hpll", 0, 50000000);
5813696eebdSAndrew Jeffery if (IS_ERR(hw))
5823696eebdSAndrew Jeffery return PTR_ERR(hw);
5833696eebdSAndrew Jeffery
584d3d04f6cSJoel Stanley /* MAC1/2 AHB bus clock divider */
585d3d04f6cSJoel Stanley hw = clk_hw_register_divider_table(dev, "mac12", "hpll", 0,
586d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION1, 16, 3, 0,
587d3d04f6cSJoel Stanley ast2600_mac_div_table,
588d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
589d3d04f6cSJoel Stanley if (IS_ERR(hw))
590d3d04f6cSJoel Stanley return PTR_ERR(hw);
591d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_MAC12] = hw;
592d3d04f6cSJoel Stanley
5933696eebdSAndrew Jeffery /* RMII1 50MHz (RCLK) output enable */
5943696eebdSAndrew Jeffery hw = clk_hw_register_gate(dev, "mac1rclk", "mac12rclk", 0,
5953696eebdSAndrew Jeffery scu_g6_base + ASPEED_MAC12_CLK_DLY, 29, 0,
5963696eebdSAndrew Jeffery &aspeed_g6_clk_lock);
5973696eebdSAndrew Jeffery if (IS_ERR(hw))
5983696eebdSAndrew Jeffery return PTR_ERR(hw);
5993696eebdSAndrew Jeffery aspeed_g6_clk_data->hws[ASPEED_CLK_MAC1RCLK] = hw;
6003696eebdSAndrew Jeffery
6013696eebdSAndrew Jeffery /* RMII2 50MHz (RCLK) output enable */
6023696eebdSAndrew Jeffery hw = clk_hw_register_gate(dev, "mac2rclk", "mac12rclk", 0,
6033696eebdSAndrew Jeffery scu_g6_base + ASPEED_MAC12_CLK_DLY, 30, 0,
6043696eebdSAndrew Jeffery &aspeed_g6_clk_lock);
6053696eebdSAndrew Jeffery if (IS_ERR(hw))
6063696eebdSAndrew Jeffery return PTR_ERR(hw);
6073696eebdSAndrew Jeffery aspeed_g6_clk_data->hws[ASPEED_CLK_MAC2RCLK] = hw;
6083696eebdSAndrew Jeffery
6093696eebdSAndrew Jeffery /* MAC1/2 RMII 50MHz RCLK */
6103696eebdSAndrew Jeffery hw = clk_hw_register_fixed_rate(dev, "mac34rclk", "hclk", 0, 50000000);
6113696eebdSAndrew Jeffery if (IS_ERR(hw))
6123696eebdSAndrew Jeffery return PTR_ERR(hw);
6133696eebdSAndrew Jeffery
614d3d04f6cSJoel Stanley /* MAC3/4 AHB bus clock divider */
615d3d04f6cSJoel Stanley hw = clk_hw_register_divider_table(dev, "mac34", "hpll", 0,
616d3d04f6cSJoel Stanley scu_g6_base + 0x310, 24, 3, 0,
617d3d04f6cSJoel Stanley ast2600_mac_div_table,
618d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
619d3d04f6cSJoel Stanley if (IS_ERR(hw))
620d3d04f6cSJoel Stanley return PTR_ERR(hw);
621d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_MAC34] = hw;
622d3d04f6cSJoel Stanley
6233696eebdSAndrew Jeffery /* RMII3 50MHz (RCLK) output enable */
6243696eebdSAndrew Jeffery hw = clk_hw_register_gate(dev, "mac3rclk", "mac34rclk", 0,
6253696eebdSAndrew Jeffery scu_g6_base + ASPEED_MAC34_CLK_DLY, 29, 0,
6263696eebdSAndrew Jeffery &aspeed_g6_clk_lock);
6273696eebdSAndrew Jeffery if (IS_ERR(hw))
6283696eebdSAndrew Jeffery return PTR_ERR(hw);
6293696eebdSAndrew Jeffery aspeed_g6_clk_data->hws[ASPEED_CLK_MAC3RCLK] = hw;
6303696eebdSAndrew Jeffery
6313696eebdSAndrew Jeffery /* RMII4 50MHz (RCLK) output enable */
6323696eebdSAndrew Jeffery hw = clk_hw_register_gate(dev, "mac4rclk", "mac34rclk", 0,
6333696eebdSAndrew Jeffery scu_g6_base + ASPEED_MAC34_CLK_DLY, 30, 0,
6343696eebdSAndrew Jeffery &aspeed_g6_clk_lock);
6353696eebdSAndrew Jeffery if (IS_ERR(hw))
6363696eebdSAndrew Jeffery return PTR_ERR(hw);
6373696eebdSAndrew Jeffery aspeed_g6_clk_data->hws[ASPEED_CLK_MAC4RCLK] = hw;
6383696eebdSAndrew Jeffery
639d3d04f6cSJoel Stanley /* LPC Host (LHCLK) clock divider */
640d3d04f6cSJoel Stanley hw = clk_hw_register_divider_table(dev, "lhclk", "hpll", 0,
641d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION1, 20, 3, 0,
642d3d04f6cSJoel Stanley ast2600_div_table,
643d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
644d3d04f6cSJoel Stanley if (IS_ERR(hw))
645d3d04f6cSJoel Stanley return PTR_ERR(hw);
646d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_LHCLK] = hw;
647d3d04f6cSJoel Stanley
648d3d04f6cSJoel Stanley /* gfx d1clk : use dp clk */
649d3d04f6cSJoel Stanley regmap_update_bits(map, ASPEED_G6_CLK_SELECTION1, GENMASK(10, 8), BIT(10));
650d3d04f6cSJoel Stanley /* SoC Display clock selection */
651d3d04f6cSJoel Stanley hw = clk_hw_register_mux(dev, "d1clk", d1clk_parent_names,
652d3d04f6cSJoel Stanley ARRAY_SIZE(d1clk_parent_names), 0,
653d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION1, 8, 3, 0,
654d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
655d3d04f6cSJoel Stanley if (IS_ERR(hw))
656d3d04f6cSJoel Stanley return PTR_ERR(hw);
657d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_D1CLK] = hw;
658d3d04f6cSJoel Stanley
659d3d04f6cSJoel Stanley /* d1 clk div 0x308[17:15] x [14:12] - 8,7,6,5,4,3,2,1 */
660d3d04f6cSJoel Stanley regmap_write(map, 0x308, 0x12000); /* 3x3 = 9 */
661d3d04f6cSJoel Stanley
662d3d04f6cSJoel Stanley /* P-Bus (BCLK) clock divider */
663b8c1dc9cSJoel Stanley hw = clk_hw_register_divider_table(dev, "bclk", "epll", 0,
664d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION1, 20, 3, 0,
665d3d04f6cSJoel Stanley ast2600_div_table,
666d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
667d3d04f6cSJoel Stanley if (IS_ERR(hw))
668d3d04f6cSJoel Stanley return PTR_ERR(hw);
669d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_BCLK] = hw;
670d3d04f6cSJoel Stanley
671d3d04f6cSJoel Stanley /* Video Capture clock selection */
672d3d04f6cSJoel Stanley hw = clk_hw_register_mux(dev, "vclk", vclk_parent_names,
673d3d04f6cSJoel Stanley ARRAY_SIZE(vclk_parent_names), 0,
674d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION2, 12, 3, 0,
675d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
676d3d04f6cSJoel Stanley if (IS_ERR(hw))
677d3d04f6cSJoel Stanley return PTR_ERR(hw);
678d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_VCLK] = hw;
679d3d04f6cSJoel Stanley
680d3d04f6cSJoel Stanley /* Video Engine clock divider */
681d3d04f6cSJoel Stanley hw = clk_hw_register_divider_table(dev, "eclk", NULL, 0,
682d3d04f6cSJoel Stanley scu_g6_base + ASPEED_G6_CLK_SELECTION1, 28, 3, 0,
683d3d04f6cSJoel Stanley ast2600_eclk_div_table,
684d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
685d3d04f6cSJoel Stanley if (IS_ERR(hw))
686d3d04f6cSJoel Stanley return PTR_ERR(hw);
687d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_ECLK] = hw;
688d3d04f6cSJoel Stanley
689d3d04f6cSJoel Stanley for (i = 0; i < ARRAY_SIZE(aspeed_g6_gates); i++) {
690d3d04f6cSJoel Stanley const struct aspeed_gate_data *gd = &aspeed_g6_gates[i];
691d3d04f6cSJoel Stanley u32 gate_flags;
692d3d04f6cSJoel Stanley
693f0dd78deSJeremy Kerr if (!gd->name)
694f0dd78deSJeremy Kerr continue;
695f0dd78deSJeremy Kerr
696d3d04f6cSJoel Stanley /*
697d3d04f6cSJoel Stanley * Special case: the USB port 1 clock (bit 14) is always
698d3d04f6cSJoel Stanley * working the opposite way from the other ones.
699d3d04f6cSJoel Stanley */
700d3d04f6cSJoel Stanley gate_flags = (gd->clock_idx == 14) ? 0 : CLK_GATE_SET_TO_DISABLE;
701d3d04f6cSJoel Stanley hw = aspeed_g6_clk_hw_register_gate(dev,
702d3d04f6cSJoel Stanley gd->name,
703d3d04f6cSJoel Stanley gd->parent_name,
704d3d04f6cSJoel Stanley gd->flags,
705d3d04f6cSJoel Stanley map,
706d3d04f6cSJoel Stanley gd->clock_idx,
707d3d04f6cSJoel Stanley gd->reset_idx,
708d3d04f6cSJoel Stanley gate_flags,
709d3d04f6cSJoel Stanley &aspeed_g6_clk_lock);
710d3d04f6cSJoel Stanley if (IS_ERR(hw))
711d3d04f6cSJoel Stanley return PTR_ERR(hw);
712d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[i] = hw;
713d3d04f6cSJoel Stanley }
714d3d04f6cSJoel Stanley
715d3d04f6cSJoel Stanley return 0;
716d3d04f6cSJoel Stanley };
717d3d04f6cSJoel Stanley
718d3d04f6cSJoel Stanley static const struct of_device_id aspeed_g6_clk_dt_ids[] = {
719d3d04f6cSJoel Stanley { .compatible = "aspeed,ast2600-scu" },
720d3d04f6cSJoel Stanley { }
721d3d04f6cSJoel Stanley };
722d3d04f6cSJoel Stanley
723d3d04f6cSJoel Stanley static struct platform_driver aspeed_g6_clk_driver = {
724d3d04f6cSJoel Stanley .probe = aspeed_g6_clk_probe,
725d3d04f6cSJoel Stanley .driver = {
726d3d04f6cSJoel Stanley .name = "ast2600-clk",
727d3d04f6cSJoel Stanley .of_match_table = aspeed_g6_clk_dt_ids,
728d3d04f6cSJoel Stanley .suppress_bind_attrs = true,
729d3d04f6cSJoel Stanley },
730d3d04f6cSJoel Stanley };
731d3d04f6cSJoel Stanley builtin_platform_driver(aspeed_g6_clk_driver);
732d3d04f6cSJoel Stanley
733d3d04f6cSJoel Stanley static const u32 ast2600_a0_axi_ahb_div_table[] = {
734d3d04f6cSJoel Stanley 2, 2, 3, 5,
735d3d04f6cSJoel Stanley };
736d3d04f6cSJoel Stanley
7372d491066SEddie James static const u32 ast2600_a1_axi_ahb_div0_tbl[] = {
7382d491066SEddie James 3, 2, 3, 4,
7392d491066SEddie James };
7402d491066SEddie James
7412d491066SEddie James static const u32 ast2600_a1_axi_ahb_div1_tbl[] = {
7422d491066SEddie James 3, 4, 6, 8,
7432d491066SEddie James };
7442d491066SEddie James
7452d491066SEddie James static const u32 ast2600_a1_axi_ahb200_tbl[] = {
7462d491066SEddie James 3, 4, 3, 4, 2, 2, 2, 2,
747d3d04f6cSJoel Stanley };
748d3d04f6cSJoel Stanley
aspeed_g6_cc(struct regmap * map)749d3d04f6cSJoel Stanley static void __init aspeed_g6_cc(struct regmap *map)
750d3d04f6cSJoel Stanley {
751d3d04f6cSJoel Stanley struct clk_hw *hw;
752f45c5b1cSJoel Stanley u32 val, div, divbits, axi_div, ahb_div;
753d3d04f6cSJoel Stanley
754d3d04f6cSJoel Stanley clk_hw_register_fixed_rate(NULL, "clkin", NULL, 0, 25000000);
755d3d04f6cSJoel Stanley
756d3d04f6cSJoel Stanley /*
757d3d04f6cSJoel Stanley * High-speed PLL clock derived from the crystal. This the CPU clock,
758d3d04f6cSJoel Stanley * and we assume that it is enabled
759d3d04f6cSJoel Stanley */
760d3d04f6cSJoel Stanley regmap_read(map, ASPEED_HPLL_PARAM, &val);
761d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_HPLL] = ast2600_calc_pll("hpll", val);
762d3d04f6cSJoel Stanley
763d3d04f6cSJoel Stanley regmap_read(map, ASPEED_MPLL_PARAM, &val);
764d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_MPLL] = ast2600_calc_pll("mpll", val);
765d3d04f6cSJoel Stanley
766d3d04f6cSJoel Stanley regmap_read(map, ASPEED_DPLL_PARAM, &val);
767d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_DPLL] = ast2600_calc_pll("dpll", val);
768d3d04f6cSJoel Stanley
769d3d04f6cSJoel Stanley regmap_read(map, ASPEED_EPLL_PARAM, &val);
770d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_EPLL] = ast2600_calc_pll("epll", val);
771d3d04f6cSJoel Stanley
772d3d04f6cSJoel Stanley regmap_read(map, ASPEED_APLL_PARAM, &val);
773d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_APLL] = ast2600_calc_apll("apll", val);
774d3d04f6cSJoel Stanley
775d3d04f6cSJoel Stanley /* Strap bits 12:11 define the AXI/AHB clock frequency ratio (aka HCLK)*/
776d3d04f6cSJoel Stanley regmap_read(map, ASPEED_G6_STRAP1, &val);
777d3d04f6cSJoel Stanley if (val & BIT(16))
778d3d04f6cSJoel Stanley axi_div = 1;
779d3d04f6cSJoel Stanley else
780d3d04f6cSJoel Stanley axi_div = 2;
781d3d04f6cSJoel Stanley
7822d491066SEddie James divbits = (val >> 11) & 0x3;
783f45c5b1cSJoel Stanley if (soc_rev >= 1) {
7842d491066SEddie James if (!divbits) {
7852d491066SEddie James ahb_div = ast2600_a1_axi_ahb200_tbl[(val >> 8) & 0x3];
7862d491066SEddie James if (val & BIT(16))
7872d491066SEddie James ahb_div *= 2;
7882d491066SEddie James } else {
7892d491066SEddie James if (val & BIT(16))
7902d491066SEddie James ahb_div = ast2600_a1_axi_ahb_div1_tbl[divbits];
791d3d04f6cSJoel Stanley else
7922d491066SEddie James ahb_div = ast2600_a1_axi_ahb_div0_tbl[divbits];
7932d491066SEddie James }
7942d491066SEddie James } else {
795d3d04f6cSJoel Stanley ahb_div = ast2600_a0_axi_ahb_div_table[(val >> 11) & 0x3];
7962d491066SEddie James }
797d3d04f6cSJoel Stanley
798d3d04f6cSJoel Stanley hw = clk_hw_register_fixed_factor(NULL, "ahb", "hpll", 0, 1, axi_div * ahb_div);
799d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_AHB] = hw;
800d3d04f6cSJoel Stanley
801d3d04f6cSJoel Stanley regmap_read(map, ASPEED_G6_CLK_SELECTION1, &val);
802d3d04f6cSJoel Stanley val = (val >> 23) & 0x7;
803d3d04f6cSJoel Stanley div = 4 * (val + 1);
804d3d04f6cSJoel Stanley hw = clk_hw_register_fixed_factor(NULL, "apb1", "hpll", 0, 1, div);
805d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_APB1] = hw;
806d3d04f6cSJoel Stanley
807d3d04f6cSJoel Stanley regmap_read(map, ASPEED_G6_CLK_SELECTION4, &val);
808d3d04f6cSJoel Stanley val = (val >> 9) & 0x7;
809d3d04f6cSJoel Stanley div = 2 * (val + 1);
810d3d04f6cSJoel Stanley hw = clk_hw_register_fixed_factor(NULL, "apb2", "ahb", 0, 1, div);
811d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_APB2] = hw;
812d3d04f6cSJoel Stanley
813d3d04f6cSJoel Stanley /* USB 2.0 port1 phy 40MHz clock */
814d3d04f6cSJoel Stanley hw = clk_hw_register_fixed_rate(NULL, "usb-phy-40m", NULL, 0, 40000000);
815d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[ASPEED_CLK_USBPHY_40M] = hw;
816e9d23014SJeremy Kerr
817e9d23014SJeremy Kerr /* i3c clock: source from apll, divide by 8 */
818e9d23014SJeremy Kerr regmap_update_bits(map, ASPEED_G6_CLK_SELECTION5,
819e9d23014SJeremy Kerr I3C_CLK_SELECTION | APLL_DIV_SELECTION,
820e9d23014SJeremy Kerr I3C_CLK_SELECT_APLL_DIV | APLL_DIV_8);
821e9d23014SJeremy Kerr
822e9d23014SJeremy Kerr hw = clk_hw_register_fixed_factor(NULL, "i3cclk", "apll", 0, 1, 8);
823e9d23014SJeremy Kerr aspeed_g6_clk_data->hws[ASPEED_CLK_I3C] = hw;
824d3d04f6cSJoel Stanley };
825d3d04f6cSJoel Stanley
aspeed_g6_cc_init(struct device_node * np)826d3d04f6cSJoel Stanley static void __init aspeed_g6_cc_init(struct device_node *np)
827d3d04f6cSJoel Stanley {
828d3d04f6cSJoel Stanley struct regmap *map;
829d3d04f6cSJoel Stanley int ret;
830d3d04f6cSJoel Stanley int i;
831d3d04f6cSJoel Stanley
832d3d04f6cSJoel Stanley scu_g6_base = of_iomap(np, 0);
833d3d04f6cSJoel Stanley if (!scu_g6_base)
834d3d04f6cSJoel Stanley return;
835d3d04f6cSJoel Stanley
836f45c5b1cSJoel Stanley soc_rev = (readl(scu_g6_base + ASPEED_G6_SILICON_REV) & CHIP_REVISION_ID) >> 16;
837f45c5b1cSJoel Stanley
838d3d04f6cSJoel Stanley aspeed_g6_clk_data = kzalloc(struct_size(aspeed_g6_clk_data, hws,
839d3d04f6cSJoel Stanley ASPEED_G6_NUM_CLKS), GFP_KERNEL);
840d3d04f6cSJoel Stanley if (!aspeed_g6_clk_data)
841d3d04f6cSJoel Stanley return;
842f316cdffSKees Cook aspeed_g6_clk_data->num = ASPEED_G6_NUM_CLKS;
843d3d04f6cSJoel Stanley
844d3d04f6cSJoel Stanley /*
845d3d04f6cSJoel Stanley * This way all clocks fetched before the platform device probes,
846d3d04f6cSJoel Stanley * except those we assign here for early use, will be deferred.
847d3d04f6cSJoel Stanley */
848d3d04f6cSJoel Stanley for (i = 0; i < ASPEED_G6_NUM_CLKS; i++)
849d3d04f6cSJoel Stanley aspeed_g6_clk_data->hws[i] = ERR_PTR(-EPROBE_DEFER);
850d3d04f6cSJoel Stanley
851d3d04f6cSJoel Stanley /*
852d3d04f6cSJoel Stanley * We check that the regmap works on this very first access,
853d3d04f6cSJoel Stanley * but as this is an MMIO-backed regmap, subsequent regmap
854d3d04f6cSJoel Stanley * access is not going to fail and we skip error checks from
855d3d04f6cSJoel Stanley * this point.
856d3d04f6cSJoel Stanley */
857d3d04f6cSJoel Stanley map = syscon_node_to_regmap(np);
858d3d04f6cSJoel Stanley if (IS_ERR(map)) {
859d3d04f6cSJoel Stanley pr_err("no syscon regmap\n");
860d3d04f6cSJoel Stanley return;
861d3d04f6cSJoel Stanley }
862d3d04f6cSJoel Stanley
863d3d04f6cSJoel Stanley aspeed_g6_cc(map);
864d3d04f6cSJoel Stanley ret = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, aspeed_g6_clk_data);
865d3d04f6cSJoel Stanley if (ret)
866d3d04f6cSJoel Stanley pr_err("failed to add DT provider: %d\n", ret);
867d3d04f6cSJoel Stanley };
868d3d04f6cSJoel Stanley CLK_OF_DECLARE_DRIVER(aspeed_cc_g6, "aspeed,ast2600-scu", aspeed_g6_cc_init);
869