blob: 513112c1146ca055d99c85eddd7de527b674c60f [file] [log] [blame]
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +05301// SPDX-License-Identifier: GPL-2.0+
2/*
Amit Singh Tomar8821be42020-04-19 19:28:30 +05303 * Common clock driver for Actions Semi SoCs.
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +05304 *
5 * Copyright (C) 2015 Actions Semi Co., Ltd.
6 * Copyright (C) 2018 Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
7 */
8
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +05309#include <dm.h>
Amit Singh Tomar8821be42020-04-19 19:28:30 +053010#include "clk_owl.h"
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053011#include <asm/io.h>
Amit Singh Tomar8821be42020-04-19 19:28:30 +053012#if defined(CONFIG_MACH_S900)
13#include <asm/arch-owl/regs_s900.h>
Amit Singh Tomar7c8e84c2020-04-19 19:28:28 +053014#include <dt-bindings/clock/actions,s900-cmu.h>
Amit Singh Tomar8821be42020-04-19 19:28:30 +053015#elif defined(CONFIG_MACH_S700)
16#include <asm/arch-owl/regs_s700.h>
17#include <dt-bindings/clock/actions,s700-cmu.h>
18#endif
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053021
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +053022#define CMU_DEVCLKEN0_SD0 BIT(22)
23
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053024void owl_clk_init(struct owl_clk_priv *priv)
25{
26 u32 bus_clk = 0, core_pll, dev_pll;
27
Amit Singh Tomar8821be42020-04-19 19:28:30 +053028#if defined(CONFIG_MACH_S900)
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053029 /* Enable ASSIST_PLL */
30 setbits_le32(priv->base + CMU_ASSISTPLL, BIT(0));
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053031 udelay(PLL_STABILITY_WAIT_US);
Amit Singh Tomar8821be42020-04-19 19:28:30 +053032#endif
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053033
34 /* Source HOSC to DEV_CLK */
35 clrbits_le32(priv->base + CMU_DEVPLL, CMU_DEVPLL_CLK);
36
37 /* Configure BUS_CLK */
38 bus_clk |= (CMU_PDBGDIV_DIV | CMU_PERDIV_DIV | CMU_NOCDIV_DIV |
39 CMU_DMMCLK_SRC | CMU_APBCLK_DIV | CMU_AHBCLK_DIV |
40 CMU_NOCCLK_SRC | CMU_CORECLK_HOSC);
41 writel(bus_clk, priv->base + CMU_BUSCLK);
42
43 udelay(PLL_STABILITY_WAIT_US);
44
45 /* Configure CORE_PLL */
46 core_pll = readl(priv->base + CMU_COREPLL);
47 core_pll |= (CMU_COREPLL_EN | CMU_COREPLL_HOSC_EN | CMU_COREPLL_OUT);
48 writel(core_pll, priv->base + CMU_COREPLL);
49
50 udelay(PLL_STABILITY_WAIT_US);
51
52 /* Configure DEV_PLL */
53 dev_pll = readl(priv->base + CMU_DEVPLL);
54 dev_pll |= (CMU_DEVPLL_EN | CMU_DEVPLL_OUT);
55 writel(dev_pll, priv->base + CMU_DEVPLL);
56
57 udelay(PLL_STABILITY_WAIT_US);
58
59 /* Source CORE_PLL for CORE_CLK */
60 clrsetbits_le32(priv->base + CMU_BUSCLK, CMU_CORECLK_MASK,
61 CMU_CORECLK_CPLL);
62
63 /* Source DEV_PLL for DEV_CLK */
64 setbits_le32(priv->base + CMU_DEVPLL, CMU_DEVPLL_CLK);
65
66 udelay(PLL_STABILITY_WAIT_US);
67}
68
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053069int owl_clk_enable(struct clk *clk)
70{
71 struct owl_clk_priv *priv = dev_get_priv(clk->dev);
Amit Singh Tomar8821be42020-04-19 19:28:30 +053072 enum owl_soc model = dev_get_driver_data(clk->dev);
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053073
74 switch (clk->id) {
Amit Singh Tomar7c8e84c2020-04-19 19:28:28 +053075 case CLK_UART5:
Amit Singh Tomar8821be42020-04-19 19:28:30 +053076 if (model != S900)
77 return -EINVAL;
78 /* Source HOSC for UART5 interface */
79 clrbits_le32(priv->base + CMU_UART5CLK, CMU_UARTCLK_SRC_DEVPLL);
80 /* Enable UART5 interface clock */
81 setbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART5);
82 break;
83 case CLK_UART3:
84 if (model != S700)
85 return -EINVAL;
86 /* Source HOSC for UART3 interface */
87 clrbits_le32(priv->base + CMU_UART3CLK, CMU_UARTCLK_SRC_DEVPLL);
88 /* Enable UART3 interface clock */
89 setbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART3);
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053090 break;
Amit Singh Tomarea4179e2020-05-09 19:55:09 +053091 case CLK_RMII_REF:
92 case CLK_ETHERNET:
93 setbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_ETH);
94 setbits_le32(priv->base + CMU_ETHERNETPLL, 5);
95 break;
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +053096 case CLK_SD0:
97 setbits_le32(priv->base + CMU_DEVCLKEN0, CMU_DEVCLKEN0_SD0);
98 break;
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +053099 default:
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530100 return -EINVAL;
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530101 }
102
103 return 0;
104}
105
106int owl_clk_disable(struct clk *clk)
107{
108 struct owl_clk_priv *priv = dev_get_priv(clk->dev);
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530109 enum owl_soc model = dev_get_driver_data(clk->dev);
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530110
111 switch (clk->id) {
Amit Singh Tomar7c8e84c2020-04-19 19:28:28 +0530112 case CLK_UART5:
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530113 if (model != S900)
114 return -EINVAL;
115 /* Disable UART5 interface clock */
116 clrbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART5);
117 break;
118 case CLK_UART3:
119 if (model != S700)
120 return -EINVAL;
121 /* Disable UART3 interface clock */
122 clrbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_UART3);
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530123 break;
Amit Singh Tomarea4179e2020-05-09 19:55:09 +0530124 case CLK_RMII_REF:
125 case CLK_ETHERNET:
126 clrbits_le32(priv->base + CMU_DEVCLKEN1, CMU_DEVCLKEN1_ETH);
127 break;
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +0530128 case CLK_SD0:
129 clrbits_le32(priv->base + CMU_DEVCLKEN0, CMU_DEVCLKEN0_SD0);
130 break;
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530131 default:
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530132 return -EINVAL;
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530133 }
134
135 return 0;
136}
137
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +0530138static ulong get_sd_parent_rate(struct owl_clk_priv *priv, u32 dev_index)
139{
140 ulong rate;
141 u32 reg;
142
143 reg = readl(priv->base + (CMU_SD0CLK + dev_index * 0x4));
144 /* Clock output of DEV/NAND_PLL
145 * Range: 48M ~ 756M
146 * Frequency= PLLCLK * 6
147 */
148 if (reg & 0x200)
149 rate = readl(priv->base + CMU_NANDPLL) & 0x7f;
150 else
151 rate = readl(priv->base + CMU_DEVPLL) & 0x7f;
152
153 rate *= 6000000;
154
155 return rate;
156}
157
158static ulong owl_get_sd_clk_rate(struct owl_clk_priv *priv, int sd_index)
159{
160 uint div, val;
161 ulong parent_rate = get_sd_parent_rate(priv, sd_index);
162
163 val = readl(priv->base + (CMU_SD0CLK + sd_index * 0x4));
164 div = (val & 0x1f) + 1;
165
166 return (parent_rate / div);
167}
168
169static ulong owl_set_sd_clk_rate(struct owl_clk_priv *priv, ulong rate,
170 int sd_index)
171{
172 uint div, val;
173 ulong parent_rate = get_sd_parent_rate(priv, sd_index);
174
175 if (rate == 0)
176 return rate;
177
178 div = (parent_rate / rate);
179
180 val = readl(priv->base + (CMU_SD0CLK + sd_index * 0x4));
181 /* Bits 4..0 is used to program div value and bit 8 to enable
182 * divide by 128 circuit
183 */
184 val &= ~0x11f;
185 if (div >= 128) {
186 div = div / 128;
187 val |= 0x100; /* enable divide by 128 circuit */
188 }
189 val |= ((div - 1) & 0x1f);
190 writel(val, priv->base + (CMU_SD0CLK + sd_index * 0x4));
191
192 return owl_get_sd_clk_rate(priv, 0);
193}
194
Amit Singh Tomar5433df92021-11-28 17:02:20 +0530195static ulong owl_clk_get_rate(struct clk *clk)
196{
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +0530197 struct owl_clk_priv *priv = dev_get_priv(clk->dev);
Amit Singh Tomar5433df92021-11-28 17:02:20 +0530198 ulong rate;
199
200 switch (clk->id) {
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +0530201 case CLK_SD0:
202 rate = owl_get_sd_clk_rate(priv, 0);
203 break;
Amit Singh Tomar5433df92021-11-28 17:02:20 +0530204 default:
205 return -ENOENT;
206 }
207
208 return rate;
209}
210
211static ulong owl_clk_set_rate(struct clk *clk, ulong rate)
212{
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +0530213 struct owl_clk_priv *priv = dev_get_priv(clk->dev);
Amit Singh Tomar5433df92021-11-28 17:02:20 +0530214 ulong new_rate;
215
216 switch (clk->id) {
Amit Singh Tomar1ec12dc2021-11-28 17:02:21 +0530217 case CLK_SD0:
218 new_rate = owl_set_sd_clk_rate(priv, rate, 0);
219 break;
Amit Singh Tomar5433df92021-11-28 17:02:20 +0530220 default:
221 return -ENOENT;
222 }
223
224 return new_rate;
225}
226
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530227static int owl_clk_probe(struct udevice *dev)
228{
229 struct owl_clk_priv *priv = dev_get_priv(dev);
230
231 priv->base = dev_read_addr(dev);
232 if (priv->base == FDT_ADDR_T_NONE)
233 return -EINVAL;
234
235 /* setup necessary clocks */
236 owl_clk_init(priv);
237
238 return 0;
239}
240
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530241static const struct clk_ops owl_clk_ops = {
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530242 .enable = owl_clk_enable,
243 .disable = owl_clk_disable,
Amit Singh Tomar5433df92021-11-28 17:02:20 +0530244 .get_rate = owl_clk_get_rate,
245 .set_rate = owl_clk_set_rate,
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530246};
247
248static const struct udevice_id owl_clk_ids[] = {
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530249#if defined(CONFIG_MACH_S900)
250 { .compatible = "actions,s900-cmu", .data = S900 },
251#elif defined(CONFIG_MACH_S700)
252 { .compatible = "actions,s700-cmu", .data = S700 },
253#endif
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530254 { }
255};
256
257U_BOOT_DRIVER(clk_owl) = {
Amit Singh Tomar8821be42020-04-19 19:28:30 +0530258 .name = "clk_owl",
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530259 .id = UCLASS_CLK,
260 .of_match = owl_clk_ids,
261 .ops = &owl_clk_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700262 .priv_auto = sizeof(struct owl_clk_priv),
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530263 .probe = owl_clk_probe,
Manivannan Sadhasivam91a85132018-06-14 23:38:35 +0530264};