blob: dcf299548de1a81e9f2be0d6e83dd3e2b26067cf [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0
maxims@google.com2d5a2ad2017-01-18 13:44:56 -08002/*
3 * (C) Copyright 2016 Google, Inc
maxims@google.com2d5a2ad2017-01-18 13:44:56 -08004 */
5
6#include <common.h>
7#include <clk-uclass.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080011#include <asm/io.h>
12#include <asm/arch/scu_ast2500.h>
13#include <dm/lists.h>
Ryan Chen5e6c9f02020-08-31 14:03:03 +080014#include <dt-bindings/clock/aspeed-clock.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070016#include <linux/err.h>
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080017
maxims@google.com15016af2017-04-17 12:00:32 -070018/*
19 * MAC Clock Delay settings, taken from Aspeed SDK
20 */
21#define RGMII_TXCLK_ODLY 8
22#define RMII_RXCLK_IDLY 2
23
24/*
25 * TGMII Clock Duty constants, taken from Aspeed SDK
26 */
27#define RGMII2_TXCK_DUTY 0x66
28#define RGMII1_TXCK_DUTY 0x64
29
30#define D2PLL_DEFAULT_RATE (250 * 1000 * 1000)
31
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080032DECLARE_GLOBAL_DATA_PTR;
33
34/*
maxims@google.com15016af2017-04-17 12:00:32 -070035 * Clock divider/multiplier configuration struct.
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080036 * For H-PLL and M-PLL the formula is
37 * (Output Frequency) = CLKIN * ((M + 1) / (N + 1)) / (P + 1)
38 * M - Numerator
39 * N - Denumerator
40 * P - Post Divider
41 * They have the same layout in their control register.
maxims@google.com15016af2017-04-17 12:00:32 -070042 *
43 * D-PLL and D2-PLL have extra divider (OD + 1), which is not
44 * yet needed and ignored by clock configurations.
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080045 */
maxims@google.com15016af2017-04-17 12:00:32 -070046struct ast2500_div_config {
47 unsigned int num;
48 unsigned int denum;
49 unsigned int post_div;
50};
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080051
52/*
53 * Get the rate of the M-PLL clock from input clock frequency and
54 * the value of the M-PLL Parameter Register.
55 */
56static ulong ast2500_get_mpll_rate(ulong clkin, u32 mpll_reg)
57{
maxims@google.coma91f1d22017-04-17 12:00:33 -070058 const ulong num = (mpll_reg & SCU_MPLL_NUM_MASK) >> SCU_MPLL_NUM_SHIFT;
59 const ulong denum = (mpll_reg & SCU_MPLL_DENUM_MASK)
60 >> SCU_MPLL_DENUM_SHIFT;
61 const ulong post_div = (mpll_reg & SCU_MPLL_POST_MASK)
62 >> SCU_MPLL_POST_SHIFT;
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080063
maxims@google.comd0672172017-01-30 11:35:04 -080064 return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080065}
66
67/*
68 * Get the rate of the H-PLL clock from input clock frequency and
69 * the value of the H-PLL Parameter Register.
70 */
71static ulong ast2500_get_hpll_rate(ulong clkin, u32 hpll_reg)
72{
maxims@google.coma91f1d22017-04-17 12:00:33 -070073 const ulong num = (hpll_reg & SCU_HPLL_NUM_MASK) >> SCU_HPLL_NUM_SHIFT;
74 const ulong denum = (hpll_reg & SCU_HPLL_DENUM_MASK)
75 >> SCU_HPLL_DENUM_SHIFT;
76 const ulong post_div = (hpll_reg & SCU_HPLL_POST_MASK)
77 >> SCU_HPLL_POST_SHIFT;
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080078
maxims@google.comd0672172017-01-30 11:35:04 -080079 return (clkin * ((num + 1) / (denum + 1))) / (post_div + 1);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080080}
81
82static ulong ast2500_get_clkin(struct ast2500_scu *scu)
83{
84 return readl(&scu->hwstrap) & SCU_HWSTRAP_CLKIN_25MHZ
85 ? 25 * 1000 * 1000 : 24 * 1000 * 1000;
86}
87
88/**
89 * Get current rate or uart clock
90 *
91 * @scu SCU registers
92 * @uart_index UART index, 1-5
93 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010094 * Return: current setting for uart clock rate
maxims@google.com2d5a2ad2017-01-18 13:44:56 -080095 */
96static ulong ast2500_get_uart_clk_rate(struct ast2500_scu *scu, int uart_index)
97{
98 /*
99 * ast2500 datasheet is very confusing when it comes to UART clocks,
100 * especially when CLKIN = 25 MHz. The settings are in
101 * different registers and it is unclear how they interact.
102 *
103 * This has only been tested with default settings and CLKIN = 24 MHz.
104 */
105 ulong uart_clkin;
106
107 if (readl(&scu->misc_ctrl2) &
108 (1 << (uart_index - 1 + SCU_MISC2_UARTCLK_SHIFT)))
109 uart_clkin = 192 * 1000 * 1000;
110 else
111 uart_clkin = 24 * 1000 * 1000;
112
113 if (readl(&scu->misc_ctrl1) & SCU_MISC_UARTCLK_DIV13)
114 uart_clkin /= 13;
115
116 return uart_clkin;
117}
118
119static ulong ast2500_clk_get_rate(struct clk *clk)
120{
121 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
122 ulong clkin = ast2500_get_clkin(priv->scu);
123 ulong rate;
124
125 switch (clk->id) {
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800126 case ASPEED_CLK_HPLL:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800127 /*
128 * This ignores dynamic/static slowdown of ARMCLK and may
129 * be inaccurate.
130 */
131 rate = ast2500_get_hpll_rate(clkin,
132 readl(&priv->scu->h_pll_param));
133 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800134 case ASPEED_CLK_MPLL:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800135 rate = ast2500_get_mpll_rate(clkin,
136 readl(&priv->scu->m_pll_param));
137 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800138 case ASPEED_CLK_APB:
maxims@google.com995167b2017-04-17 12:00:29 -0700139 {
140 ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1)
maxims@google.coma91f1d22017-04-17 12:00:33 -0700141 & SCU_PCLK_DIV_MASK)
142 >> SCU_PCLK_DIV_SHIFT);
maxims@google.com995167b2017-04-17 12:00:29 -0700143 rate = ast2500_get_hpll_rate(clkin,
maxims@google.coma91f1d22017-04-17 12:00:33 -0700144 readl(&priv->
145 scu->h_pll_param));
maxims@google.com995167b2017-04-17 12:00:29 -0700146 rate = rate / apb_div;
147 }
148 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800149 case ASPEED_CLK_SDIO:
Eddie Jamesb7d76ac2019-08-15 14:29:37 -0500150 {
151 ulong apb_div = 4 + 4 * ((readl(&priv->scu->clk_sel1)
152 & SCU_SDCLK_DIV_MASK)
153 >> SCU_SDCLK_DIV_SHIFT);
154 rate = ast2500_get_hpll_rate(clkin,
155 readl(&priv->
156 scu->h_pll_param));
157 rate = rate / apb_div;
158 }
159 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800160 case ASPEED_CLK_GATE_UART1CLK:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800161 rate = ast2500_get_uart_clk_rate(priv->scu, 1);
162 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800163 case ASPEED_CLK_GATE_UART2CLK:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800164 rate = ast2500_get_uart_clk_rate(priv->scu, 2);
165 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800166 case ASPEED_CLK_GATE_UART3CLK:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800167 rate = ast2500_get_uart_clk_rate(priv->scu, 3);
168 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800169 case ASPEED_CLK_GATE_UART4CLK:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800170 rate = ast2500_get_uart_clk_rate(priv->scu, 4);
171 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800172 case ASPEED_CLK_GATE_UART5CLK:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800173 rate = ast2500_get_uart_clk_rate(priv->scu, 5);
174 break;
175 default:
Joel Stanley50ddb952022-06-23 18:35:30 +0930176 debug("%s: unknown clk %ld\n", __func__, clk->id);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800177 return -ENOENT;
178 }
179
180 return rate;
181}
182
Cédric Le Goaterd7f37892018-10-29 07:06:41 +0100183struct ast2500_clock_config {
184 ulong input_rate;
185 ulong rate;
186 struct ast2500_div_config cfg;
187};
188
189static const struct ast2500_clock_config ast2500_clock_config_defaults[] = {
190 { 24000000, 250000000, { .num = 124, .denum = 1, .post_div = 5 } },
191};
192
193static bool ast2500_get_clock_config_default(ulong input_rate,
194 ulong requested_rate,
195 struct ast2500_div_config *cfg)
196{
197 int i;
198
199 for (i = 0; i < ARRAY_SIZE(ast2500_clock_config_defaults); i++) {
200 const struct ast2500_clock_config *default_cfg =
201 &ast2500_clock_config_defaults[i];
202 if (default_cfg->input_rate == input_rate &&
203 default_cfg->rate == requested_rate) {
204 *cfg = default_cfg->cfg;
205 return true;
206 }
207 }
208
209 return false;
210}
211
maxims@google.com15016af2017-04-17 12:00:32 -0700212/*
213 * @input_rate - the rate of input clock in Hz
214 * @requested_rate - desired output rate in Hz
215 * @div - this is an IN/OUT parameter, at input all fields of the config
216 * need to be set to their maximum allowed values.
217 * The result (the best config we could find), would also be returned
218 * in this structure.
219 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100220 * Return: The clock rate, when the resulting div_config is used.
maxims@google.com15016af2017-04-17 12:00:32 -0700221 */
222static ulong ast2500_calc_clock_config(ulong input_rate, ulong requested_rate,
223 struct ast2500_div_config *cfg)
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800224{
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800225 /*
maxims@google.com15016af2017-04-17 12:00:32 -0700226 * The assumption is that kHz precision is good enough and
227 * also enough to avoid overflow when multiplying.
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800228 */
maxims@google.com15016af2017-04-17 12:00:32 -0700229 const ulong input_rate_khz = input_rate / 1000;
230 const ulong rate_khz = requested_rate / 1000;
231 const struct ast2500_div_config max_vals = *cfg;
232 struct ast2500_div_config it = { 0, 0, 0 };
233 ulong delta = rate_khz;
234 ulong new_rate_khz = 0;
235
Cédric Le Goaterd7f37892018-10-29 07:06:41 +0100236 /*
237 * Look for a well known frequency first.
238 */
239 if (ast2500_get_clock_config_default(input_rate, requested_rate, cfg))
240 return requested_rate;
241
maxims@google.com15016af2017-04-17 12:00:32 -0700242 for (; it.denum <= max_vals.denum; ++it.denum) {
243 for (it.post_div = 0; it.post_div <= max_vals.post_div;
244 ++it.post_div) {
245 it.num = (rate_khz * (it.post_div + 1) / input_rate_khz)
246 * (it.denum + 1);
247 if (it.num > max_vals.num)
248 continue;
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800249
maxims@google.com15016af2017-04-17 12:00:32 -0700250 new_rate_khz = (input_rate_khz
251 * ((it.num + 1) / (it.denum + 1)))
252 / (it.post_div + 1);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800253
254 /* Keep the rate below requested one. */
255 if (new_rate_khz > rate_khz)
256 continue;
257
258 if (new_rate_khz - rate_khz < delta) {
259 delta = new_rate_khz - rate_khz;
maxims@google.com15016af2017-04-17 12:00:32 -0700260 *cfg = it;
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800261 if (delta == 0)
maxims@google.com15016af2017-04-17 12:00:32 -0700262 return new_rate_khz * 1000;
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800263 }
264 }
265 }
266
maxims@google.com15016af2017-04-17 12:00:32 -0700267 return new_rate_khz * 1000;
268}
269
270static ulong ast2500_configure_ddr(struct ast2500_scu *scu, ulong rate)
271{
272 ulong clkin = ast2500_get_clkin(scu);
273 u32 mpll_reg;
274 struct ast2500_div_config div_cfg = {
maxims@google.coma91f1d22017-04-17 12:00:33 -0700275 .num = (SCU_MPLL_NUM_MASK >> SCU_MPLL_NUM_SHIFT),
276 .denum = (SCU_MPLL_DENUM_MASK >> SCU_MPLL_DENUM_SHIFT),
277 .post_div = (SCU_MPLL_POST_MASK >> SCU_MPLL_POST_SHIFT),
maxims@google.com15016af2017-04-17 12:00:32 -0700278 };
279
280 ast2500_calc_clock_config(clkin, rate, &div_cfg);
281
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800282 mpll_reg = readl(&scu->m_pll_param);
maxims@google.coma91f1d22017-04-17 12:00:33 -0700283 mpll_reg &= ~(SCU_MPLL_POST_MASK | SCU_MPLL_NUM_MASK
284 | SCU_MPLL_DENUM_MASK);
maxims@google.com15016af2017-04-17 12:00:32 -0700285 mpll_reg |= (div_cfg.post_div << SCU_MPLL_POST_SHIFT)
286 | (div_cfg.num << SCU_MPLL_NUM_SHIFT)
287 | (div_cfg.denum << SCU_MPLL_DENUM_SHIFT);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800288
maxims@google.comadea66c2017-04-17 12:00:23 -0700289 ast_scu_unlock(scu);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800290 writel(mpll_reg, &scu->m_pll_param);
maxims@google.comadea66c2017-04-17 12:00:23 -0700291 ast_scu_lock(scu);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800292
293 return ast2500_get_mpll_rate(clkin, mpll_reg);
294}
295
maxims@google.com15016af2017-04-17 12:00:32 -0700296static ulong ast2500_configure_mac(struct ast2500_scu *scu, int index)
297{
298 ulong clkin = ast2500_get_clkin(scu);
299 ulong hpll_rate = ast2500_get_hpll_rate(clkin,
300 readl(&scu->h_pll_param));
301 ulong required_rate;
302 u32 hwstrap;
303 u32 divisor;
304 u32 reset_bit;
305 u32 clkstop_bit;
306
307 /*
308 * According to data sheet, for 10/100 mode the MAC clock frequency
309 * should be at least 25MHz and for 1000 mode at least 100MHz
310 */
311 hwstrap = readl(&scu->hwstrap);
312 if (hwstrap & (SCU_HWSTRAP_MAC1_RGMII | SCU_HWSTRAP_MAC2_RGMII))
313 required_rate = 100 * 1000 * 1000;
314 else
315 required_rate = 25 * 1000 * 1000;
316
317 divisor = hpll_rate / required_rate;
318
319 if (divisor < 4) {
320 /* Clock can't run fast enough, but let's try anyway */
321 debug("MAC clock too slow\n");
322 divisor = 4;
323 } else if (divisor > 16) {
324 /* Can't slow down the clock enough, but let's try anyway */
325 debug("MAC clock too fast\n");
326 divisor = 16;
327 }
328
329 switch (index) {
330 case 1:
331 reset_bit = SCU_SYSRESET_MAC1;
332 clkstop_bit = SCU_CLKSTOP_MAC1;
333 break;
334 case 2:
335 reset_bit = SCU_SYSRESET_MAC2;
336 clkstop_bit = SCU_CLKSTOP_MAC2;
337 break;
338 default:
339 return -EINVAL;
340 }
341
342 ast_scu_unlock(scu);
343 clrsetbits_le32(&scu->clk_sel1, SCU_MACCLK_MASK,
344 ((divisor - 2) / 2) << SCU_MACCLK_SHIFT);
345
346 /*
347 * Disable MAC, start its clock and re-enable it.
348 * The procedure and the delays (100us & 10ms) are
349 * specified in the datasheet.
350 */
351 setbits_le32(&scu->sysreset_ctrl1, reset_bit);
352 udelay(100);
353 clrbits_le32(&scu->clk_stop_ctrl1, clkstop_bit);
354 mdelay(10);
355 clrbits_le32(&scu->sysreset_ctrl1, reset_bit);
356
357 writel((RGMII2_TXCK_DUTY << SCU_CLKDUTY_RGMII2TXCK_SHIFT)
358 | (RGMII1_TXCK_DUTY << SCU_CLKDUTY_RGMII1TXCK_SHIFT),
359 &scu->clk_duty_sel);
360
361 ast_scu_lock(scu);
362
363 return required_rate;
364}
365
366static ulong ast2500_configure_d2pll(struct ast2500_scu *scu, ulong rate)
367{
368 /*
369 * The values and the meaning of the next three
370 * parameters are undocumented. Taken from Aspeed SDK.
Cédric Le Goaterd7f37892018-10-29 07:06:41 +0100371 *
372 * TODO(clg@kaod.org): the SIP and SIC values depend on the
373 * Numerator value
maxims@google.com15016af2017-04-17 12:00:32 -0700374 */
375 const u32 d2_pll_ext_param = 0x2c;
376 const u32 d2_pll_sip = 0x11;
377 const u32 d2_pll_sic = 0x18;
378 u32 clk_delay_settings =
379 (RMII_RXCLK_IDLY << SCU_MICDS_MAC1RMII_RDLY_SHIFT)
380 | (RMII_RXCLK_IDLY << SCU_MICDS_MAC2RMII_RDLY_SHIFT)
381 | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC1RGMII_TXDLY_SHIFT)
382 | (RGMII_TXCLK_ODLY << SCU_MICDS_MAC2RGMII_TXDLY_SHIFT);
383 struct ast2500_div_config div_cfg = {
384 .num = SCU_D2PLL_NUM_MASK >> SCU_D2PLL_NUM_SHIFT,
385 .denum = SCU_D2PLL_DENUM_MASK >> SCU_D2PLL_DENUM_SHIFT,
386 .post_div = SCU_D2PLL_POST_MASK >> SCU_D2PLL_POST_SHIFT,
387 };
388 ulong clkin = ast2500_get_clkin(scu);
389 ulong new_rate;
390
391 ast_scu_unlock(scu);
392 writel((d2_pll_ext_param << SCU_D2PLL_EXT1_PARAM_SHIFT)
393 | SCU_D2PLL_EXT1_OFF
394 | SCU_D2PLL_EXT1_RESET, &scu->d2_pll_ext_param[0]);
395
396 /*
397 * Select USB2.0 port1 PHY clock as a clock source for GCRT.
398 * This would disconnect it from D2-PLL.
399 */
400 clrsetbits_le32(&scu->misc_ctrl1, SCU_MISC_D2PLL_OFF,
401 SCU_MISC_GCRT_USB20CLK);
402
403 new_rate = ast2500_calc_clock_config(clkin, rate, &div_cfg);
404 writel((d2_pll_sip << SCU_D2PLL_SIP_SHIFT)
405 | (d2_pll_sic << SCU_D2PLL_SIC_SHIFT)
406 | (div_cfg.num << SCU_D2PLL_NUM_SHIFT)
407 | (div_cfg.denum << SCU_D2PLL_DENUM_SHIFT)
408 | (div_cfg.post_div << SCU_D2PLL_POST_SHIFT),
409 &scu->d2_pll_param);
410
411 clrbits_le32(&scu->d2_pll_ext_param[0],
412 SCU_D2PLL_EXT1_OFF | SCU_D2PLL_EXT1_RESET);
413
414 clrsetbits_le32(&scu->misc_ctrl2,
415 SCU_MISC2_RGMII_HPLL | SCU_MISC2_RMII_MPLL
416 | SCU_MISC2_RGMII_CLKDIV_MASK |
417 SCU_MISC2_RMII_CLKDIV_MASK,
418 (4 << SCU_MISC2_RMII_CLKDIV_SHIFT));
419
420 writel(clk_delay_settings | SCU_MICDS_RGMIIPLL, &scu->mac_clk_delay);
421 writel(clk_delay_settings, &scu->mac_clk_delay_100M);
422 writel(clk_delay_settings, &scu->mac_clk_delay_10M);
423
424 ast_scu_lock(scu);
425
426 return new_rate;
427}
428
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800429static ulong ast2500_clk_set_rate(struct clk *clk, ulong rate)
430{
431 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
432
433 ulong new_rate;
434 switch (clk->id) {
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800435 case ASPEED_CLK_MPLL:
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800436 new_rate = ast2500_configure_ddr(priv->scu, rate);
437 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800438 case ASPEED_CLK_D2PLL:
maxims@google.com15016af2017-04-17 12:00:32 -0700439 new_rate = ast2500_configure_d2pll(priv->scu, rate);
440 break;
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800441 default:
Joel Stanley50ddb952022-06-23 18:35:30 +0930442 debug("%s: unknown clk %ld\n", __func__, clk->id);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800443 return -ENOENT;
444 }
445
446 return new_rate;
447}
448
maxims@google.com15016af2017-04-17 12:00:32 -0700449static int ast2500_clk_enable(struct clk *clk)
450{
451 struct ast2500_clk_priv *priv = dev_get_priv(clk->dev);
452
453 switch (clk->id) {
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800454 case ASPEED_CLK_SDIO:
Eddie Jamesb7d76ac2019-08-15 14:29:37 -0500455 if (readl(&priv->scu->clk_stop_ctrl1) & SCU_CLKSTOP_SDCLK) {
456 ast_scu_unlock(priv->scu);
457
458 setbits_le32(&priv->scu->sysreset_ctrl1,
459 SCU_SYSRESET_SDIO);
460 udelay(100);
461 clrbits_le32(&priv->scu->clk_stop_ctrl1,
462 SCU_CLKSTOP_SDCLK);
463 mdelay(10);
464 clrbits_le32(&priv->scu->sysreset_ctrl1,
465 SCU_SYSRESET_SDIO);
466
467 ast_scu_lock(priv->scu);
468 }
469 break;
maxims@google.com15016af2017-04-17 12:00:32 -0700470 /*
471 * For MAC clocks the clock rate is
472 * configured based on whether RGMII or RMII mode has been selected
473 * through hardware strapping.
474 */
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800475 case ASPEED_CLK_GATE_MAC1CLK:
maxims@google.com15016af2017-04-17 12:00:32 -0700476 ast2500_configure_mac(priv->scu, 1);
477 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800478 case ASPEED_CLK_GATE_MAC2CLK:
maxims@google.com15016af2017-04-17 12:00:32 -0700479 ast2500_configure_mac(priv->scu, 2);
480 break;
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800481 case ASPEED_CLK_D2PLL:
maxims@google.com15016af2017-04-17 12:00:32 -0700482 ast2500_configure_d2pll(priv->scu, D2PLL_DEFAULT_RATE);
Cédric Le Goater62b4bfd2018-10-29 07:06:37 +0100483 break;
maxims@google.com15016af2017-04-17 12:00:32 -0700484 default:
Joel Stanley50ddb952022-06-23 18:35:30 +0930485 debug("%s: unknown clk %ld\n", __func__, clk->id);
maxims@google.com15016af2017-04-17 12:00:32 -0700486 return -ENOENT;
487 }
488
489 return 0;
490}
491
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800492struct clk_ops ast2500_clk_ops = {
493 .get_rate = ast2500_clk_get_rate,
494 .set_rate = ast2500_clk_set_rate,
maxims@google.com15016af2017-04-17 12:00:32 -0700495 .enable = ast2500_clk_enable,
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800496};
497
Simon Glassaad29ae2020-12-03 16:55:21 -0700498static int ast2500_clk_of_to_plat(struct udevice *dev)
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800499{
500 struct ast2500_clk_priv *priv = dev_get_priv(dev);
501
Ryan Chen1e4a5c12020-08-31 14:03:04 +0800502 priv->scu = devfdt_get_addr_ptr(dev);
503 if (IS_ERR(priv->scu))
504 return PTR_ERR(priv->scu);
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800505
506 return 0;
507}
508
509static int ast2500_clk_bind(struct udevice *dev)
510{
511 int ret;
512
513 /* The reset driver does not have a device node, so bind it here */
514 ret = device_bind_driver(gd->dm_root, "ast_sysreset", "reset", &dev);
515 if (ret)
516 debug("Warning: No reset driver: ret=%d\n", ret);
517
518 return 0;
519}
520
521static const struct udevice_id ast2500_clk_ids[] = {
522 { .compatible = "aspeed,ast2500-scu" },
523 { }
524};
525
526U_BOOT_DRIVER(aspeed_ast2500_scu) = {
527 .name = "aspeed_ast2500_scu",
528 .id = UCLASS_CLK,
529 .of_match = ast2500_clk_ids,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700530 .priv_auto = sizeof(struct ast2500_clk_priv),
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800531 .ops = &ast2500_clk_ops,
532 .bind = ast2500_clk_bind,
Simon Glassaad29ae2020-12-03 16:55:21 -0700533 .of_to_plat = ast2500_clk_of_to_plat,
maxims@google.com2d5a2ad2017-01-18 13:44:56 -0800534};