blob: aa118665911907fb4ed6eba3fdfb44b3cbdb6fdb [file] [log] [blame]
Neil Armstronga990c392019-10-11 15:12:19 +02001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (c) 2019 BayLibre, SAS
4 * Author: Neil Armstrong <narmstrong@baylibre.com>
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <power-domain-uclass.h>
10#include <regmap.h>
11#include <syscon.h>
12#include <reset.h>
13#include <clk.h>
14#include <dt-bindings/power/meson-g12a-power.h>
15#include <dt-bindings/power/meson-sm1-power.h>
Simon Glass61b29b82020-02-03 07:36:15 -070016#include <linux/err.h>
Neil Armstronga990c392019-10-11 15:12:19 +020017
18/* AO Offsets */
19
20#define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
21#define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
22
23/* HHI Offsets */
24
25#define HHI_MEM_PD_REG0 (0x40 << 2)
26#define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
27#define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
28#define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
29#define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
30#define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
31#define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
32#define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
33#define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
34
35struct meson_ee_pwrc;
36struct meson_ee_pwrc_domain;
37
38struct meson_ee_pwrc_mem_domain {
39 unsigned int reg;
40 unsigned int mask;
41};
42
43struct meson_ee_pwrc_top_domain {
44 unsigned int sleep_reg;
45 unsigned int sleep_mask;
46 unsigned int iso_reg;
47 unsigned int iso_mask;
48};
49
50struct meson_ee_pwrc_domain_desc {
51 char *name;
52 unsigned int reset_names_count;
53 unsigned int clk_names_count;
54 struct meson_ee_pwrc_top_domain *top_pd;
55 unsigned int mem_pd_count;
56 struct meson_ee_pwrc_mem_domain *mem_pd;
57 bool (*get_power)(struct power_domain *power_domain);
58};
59
60struct meson_ee_pwrc_domain_data {
61 unsigned int count;
62 struct meson_ee_pwrc_domain_desc *domains;
63};
64
65/* TOP Power Domains */
66
67static struct meson_ee_pwrc_top_domain g12a_pwrc_vpu = {
68 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
69 .sleep_mask = BIT(8),
70 .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
71 .iso_mask = BIT(9),
72};
73
74#define SM1_EE_PD(__bit) \
75 { \
76 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
77 .sleep_mask = BIT(__bit), \
78 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
79 .iso_mask = BIT(__bit), \
80 }
81
82static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
83static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
84static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
85static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
86static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
87
88/* Memory PD Domains */
89
90#define VPU_MEMPD(__reg) \
91 { __reg, GENMASK(1, 0) }, \
92 { __reg, GENMASK(3, 2) }, \
93 { __reg, GENMASK(5, 4) }, \
94 { __reg, GENMASK(7, 6) }, \
95 { __reg, GENMASK(9, 8) }, \
96 { __reg, GENMASK(11, 10) }, \
97 { __reg, GENMASK(13, 12) }, \
98 { __reg, GENMASK(15, 14) }, \
99 { __reg, GENMASK(17, 16) }, \
100 { __reg, GENMASK(19, 18) }, \
101 { __reg, GENMASK(21, 20) }, \
102 { __reg, GENMASK(23, 22) }, \
103 { __reg, GENMASK(25, 24) }, \
104 { __reg, GENMASK(27, 26) }, \
105 { __reg, GENMASK(29, 28) }, \
106 { __reg, GENMASK(31, 30) }
107
108#define VPU_HHI_MEMPD(__reg) \
109 { __reg, BIT(8) }, \
110 { __reg, BIT(9) }, \
111 { __reg, BIT(10) }, \
112 { __reg, BIT(11) }, \
113 { __reg, BIT(12) }, \
114 { __reg, BIT(13) }, \
115 { __reg, BIT(14) }, \
116 { __reg, BIT(15) }
117
118static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
119 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
120 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
121 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
122 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
123};
124
125static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
126 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
127};
128
129static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
130 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
131 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
132 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
133 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
134 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
135 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
136 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
137 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
138 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
139};
140
141static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
142 { HHI_NANOQ_MEM_PD_REG0, 0xff },
143 { HHI_NANOQ_MEM_PD_REG1, 0xff },
144};
145
146static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
147 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
148};
149
150static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
151 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
152};
153
154static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
155 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
156};
157
158static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
159 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
160 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
161 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
162 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
163 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
164 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
165 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
166 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
167 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
168 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
169 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
170 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
171 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
172};
173
174#define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
175 { \
176 .name = __name, \
177 .reset_names_count = __resets, \
178 .clk_names_count = __clks, \
179 .top_pd = __top_pd, \
180 .mem_pd_count = ARRAY_SIZE(__mem), \
181 .mem_pd = __mem, \
182 .get_power = __get_power, \
183 }
184
185#define TOP_PD(__name, __top_pd, __mem, __get_power) \
186 { \
187 .name = __name, \
188 .top_pd = __top_pd, \
189 .mem_pd_count = ARRAY_SIZE(__mem), \
190 .mem_pd = __mem, \
191 .get_power = __get_power, \
192 }
193
194#define MEM_PD(__name, __mem) \
195 TOP_PD(__name, NULL, __mem, NULL)
196
197static bool pwrc_ee_get_power(struct power_domain *power_domain);
198
199static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
200 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &g12a_pwrc_vpu, g12a_pwrc_mem_vpu,
201 pwrc_ee_get_power, 11, 2),
202 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
203};
204
205static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
206 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
207 pwrc_ee_get_power, 11, 2),
208 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
209 pwrc_ee_get_power),
210 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
211 pwrc_ee_get_power),
212 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
213 pwrc_ee_get_power),
214 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
215 pwrc_ee_get_power),
216 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
217 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
218};
219
220struct meson_ee_pwrc_priv {
221 struct regmap *regmap_ao;
222 struct regmap *regmap_hhi;
223 struct reset_ctl_bulk resets;
224 struct clk_bulk clks;
225 const struct meson_ee_pwrc_domain_data *data;
226};
227
228static bool pwrc_ee_get_power(struct power_domain *power_domain)
229{
230 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
231 struct meson_ee_pwrc_domain_desc *pwrc_domain;
232 u32 reg;
233
234 pwrc_domain = &priv->data->domains[power_domain->id];
235
236 regmap_read(priv->regmap_ao,
237 pwrc_domain->top_pd->sleep_reg, &reg);
238
239 return (reg & pwrc_domain->top_pd->sleep_mask);
240}
241
242static int meson_ee_pwrc_request(struct power_domain *power_domain)
243{
244 return 0;
245}
246
247static int meson_ee_pwrc_free(struct power_domain *power_domain)
248{
249 return 0;
250}
251
252static int meson_ee_pwrc_off(struct power_domain *power_domain)
253{
254 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
255 struct meson_ee_pwrc_domain_desc *pwrc_domain;
256 int i;
257
258 pwrc_domain = &priv->data->domains[power_domain->id];
259
260 if (pwrc_domain->top_pd)
261 regmap_update_bits(priv->regmap_ao,
262 pwrc_domain->top_pd->sleep_reg,
263 pwrc_domain->top_pd->sleep_mask,
264 pwrc_domain->top_pd->sleep_mask);
265 udelay(20);
266
267 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
268 regmap_update_bits(priv->regmap_hhi,
269 pwrc_domain->mem_pd[i].reg,
270 pwrc_domain->mem_pd[i].mask,
271 pwrc_domain->mem_pd[i].mask);
272
273 udelay(20);
274
275 if (pwrc_domain->top_pd)
276 regmap_update_bits(priv->regmap_ao,
277 pwrc_domain->top_pd->iso_reg,
278 pwrc_domain->top_pd->iso_mask,
279 pwrc_domain->top_pd->iso_mask);
280
281 if (pwrc_domain->clk_names_count) {
282 mdelay(20);
283 clk_disable_bulk(&priv->clks);
284 }
285
286 return 0;
287}
288
289static int meson_ee_pwrc_on(struct power_domain *power_domain)
290{
291 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
292 struct meson_ee_pwrc_domain_desc *pwrc_domain;
293 int i, ret;
294
295 pwrc_domain = &priv->data->domains[power_domain->id];
296
297 if (pwrc_domain->top_pd)
298 regmap_update_bits(priv->regmap_ao,
299 pwrc_domain->top_pd->sleep_reg,
300 pwrc_domain->top_pd->sleep_mask, 0);
301 udelay(20);
302
303 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
304 regmap_update_bits(priv->regmap_hhi,
305 pwrc_domain->mem_pd[i].reg,
306 pwrc_domain->mem_pd[i].mask, 0);
307
308 udelay(20);
309
310 if (pwrc_domain->reset_names_count) {
311 ret = reset_assert_bulk(&priv->resets);
312 if (ret)
313 return ret;
314 }
315
316 if (pwrc_domain->top_pd)
317 regmap_update_bits(priv->regmap_ao,
318 pwrc_domain->top_pd->iso_reg,
319 pwrc_domain->top_pd->iso_mask, 0);
320
321 if (pwrc_domain->reset_names_count) {
322 ret = reset_deassert_bulk(&priv->resets);
323 if (ret)
324 return ret;
325 }
326
327 if (pwrc_domain->clk_names_count)
328 return clk_enable_bulk(&priv->clks);
329
330 return 0;
331}
332
333static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
334 struct ofnode_phandle_args *args)
335{
336 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
337
338 /* #power-domain-cells is 1 */
339
340 if (args->args_count < 1) {
341 debug("Invalid args_count: %d\n", args->args_count);
342 return -EINVAL;
343 }
344
345 power_domain->id = args->args[0];
346
347 if (power_domain->id >= priv->data->count) {
348 debug("Invalid domain ID: %lu\n", power_domain->id);
349 return -EINVAL;
350 }
351
352 return 0;
353}
354
355struct power_domain_ops meson_ee_pwrc_ops = {
Simon Glass4f511882020-02-03 07:35:51 -0700356 .rfree = meson_ee_pwrc_free,
Neil Armstronga990c392019-10-11 15:12:19 +0200357 .off = meson_ee_pwrc_off,
358 .on = meson_ee_pwrc_on,
359 .request = meson_ee_pwrc_request,
360 .of_xlate = meson_ee_pwrc_of_xlate,
361};
362
363static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
364 .count = ARRAY_SIZE(g12a_pwrc_domains),
365 .domains = g12a_pwrc_domains,
366};
367
368static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
369 .count = ARRAY_SIZE(sm1_pwrc_domains),
370 .domains = sm1_pwrc_domains,
371};
372
373static const struct udevice_id meson_ee_pwrc_ids[] = {
374 {
375 .compatible = "amlogic,meson-g12a-pwrc",
376 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
377 },
378 {
379 .compatible = "amlogic,meson-sm1-pwrc",
380 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
381 },
382 { }
383};
384
385static int meson_ee_pwrc_probe(struct udevice *dev)
386{
387 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
388 u32 ao_phandle;
389 ofnode ao_node;
390 int ret;
391
392 priv->data = (void *)dev_get_driver_data(dev);
393 if (!priv->data)
394 return -EINVAL;
395
396 priv->regmap_hhi = syscon_node_to_regmap(dev_get_parent(dev)->node);
397 if (IS_ERR(priv->regmap_hhi))
398 return PTR_ERR(priv->regmap_hhi);
399
400 ret = ofnode_read_u32(dev->node, "amlogic,ao-sysctrl",
401 &ao_phandle);
402 if (ret)
403 return ret;
404
405 ao_node = ofnode_get_by_phandle(ao_phandle);
406 if (!ofnode_valid(ao_node))
407 return -EINVAL;
408
409 priv->regmap_ao = syscon_node_to_regmap(ao_node);
410 if (IS_ERR(priv->regmap_ao))
411 return PTR_ERR(priv->regmap_ao);
412
413 ret = reset_get_bulk(dev, &priv->resets);
414 if (ret)
415 return ret;
416
417 ret = clk_get_bulk(dev, &priv->clks);
418 if (ret)
419 return ret;
420
421 return 0;
422}
423
424U_BOOT_DRIVER(meson_ee_pwrc) = {
425 .name = "meson_ee_pwrc",
426 .id = UCLASS_POWER_DOMAIN,
427 .of_match = meson_ee_pwrc_ids,
428 .probe = meson_ee_pwrc_probe,
429 .ops = &meson_ee_pwrc_ops,
430 .priv_auto_alloc_size = sizeof(struct meson_ee_pwrc_priv),
431};