blob: 4d9f3bba644c432e87c55bd56349a7c079fd5cc2 [file] [log] [blame]
Neil Armstrong152b4532019-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
Neil Armstrong152b4532019-10-11 15:12:19 +02007#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -07009#include <malloc.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020010#include <power-domain-uclass.h>
11#include <regmap.h>
12#include <syscon.h>
13#include <reset.h>
14#include <clk.h>
Neil Armstrong48373372020-09-30 11:55:50 +020015#include <dt-bindings/power/meson-axg-power.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020016#include <dt-bindings/power/meson-g12a-power.h>
Neil Armstrongba79d762020-09-30 11:52:49 +020017#include <dt-bindings/power/meson-gxbb-power.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020018#include <dt-bindings/power/meson-sm1-power.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060019#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060020#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070021#include <linux/err.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020022
23/* AO Offsets */
24
25#define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
26#define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
27
28/* HHI Offsets */
29
30#define HHI_MEM_PD_REG0 (0x40 << 2)
31#define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
32#define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
33#define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
34#define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
35#define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
36#define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
37#define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
38#define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
39
40struct meson_ee_pwrc;
41struct meson_ee_pwrc_domain;
42
43struct meson_ee_pwrc_mem_domain {
44 unsigned int reg;
45 unsigned int mask;
46};
47
48struct meson_ee_pwrc_top_domain {
49 unsigned int sleep_reg;
50 unsigned int sleep_mask;
51 unsigned int iso_reg;
52 unsigned int iso_mask;
53};
54
55struct meson_ee_pwrc_domain_desc {
56 char *name;
57 unsigned int reset_names_count;
58 unsigned int clk_names_count;
59 struct meson_ee_pwrc_top_domain *top_pd;
60 unsigned int mem_pd_count;
61 struct meson_ee_pwrc_mem_domain *mem_pd;
62 bool (*get_power)(struct power_domain *power_domain);
Neil Armstronge8e3f1d2024-10-09 11:15:21 +020063 bool enabled;
Neil Armstrong152b4532019-10-11 15:12:19 +020064};
65
66struct meson_ee_pwrc_domain_data {
67 unsigned int count;
68 struct meson_ee_pwrc_domain_desc *domains;
69};
70
71/* TOP Power Domains */
72
Neil Armstrongba79d762020-09-30 11:52:49 +020073static struct meson_ee_pwrc_top_domain gx_pwrc_vpu = {
Neil Armstrong152b4532019-10-11 15:12:19 +020074 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
75 .sleep_mask = BIT(8),
76 .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
77 .iso_mask = BIT(9),
78};
79
80#define SM1_EE_PD(__bit) \
81 { \
82 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
83 .sleep_mask = BIT(__bit), \
84 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
85 .iso_mask = BIT(__bit), \
86 }
87
88static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
89static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
90static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
91static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
92static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
93
94/* Memory PD Domains */
95
96#define VPU_MEMPD(__reg) \
97 { __reg, GENMASK(1, 0) }, \
98 { __reg, GENMASK(3, 2) }, \
99 { __reg, GENMASK(5, 4) }, \
100 { __reg, GENMASK(7, 6) }, \
101 { __reg, GENMASK(9, 8) }, \
102 { __reg, GENMASK(11, 10) }, \
103 { __reg, GENMASK(13, 12) }, \
104 { __reg, GENMASK(15, 14) }, \
105 { __reg, GENMASK(17, 16) }, \
106 { __reg, GENMASK(19, 18) }, \
107 { __reg, GENMASK(21, 20) }, \
108 { __reg, GENMASK(23, 22) }, \
109 { __reg, GENMASK(25, 24) }, \
110 { __reg, GENMASK(27, 26) }, \
111 { __reg, GENMASK(29, 28) }, \
112 { __reg, GENMASK(31, 30) }
113
114#define VPU_HHI_MEMPD(__reg) \
115 { __reg, BIT(8) }, \
116 { __reg, BIT(9) }, \
117 { __reg, BIT(10) }, \
118 { __reg, BIT(11) }, \
119 { __reg, BIT(12) }, \
120 { __reg, BIT(13) }, \
121 { __reg, BIT(14) }, \
122 { __reg, BIT(15) }
123
124static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
125 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
126 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
127 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
128 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
129};
130
Neil Armstrong48373372020-09-30 11:55:50 +0200131static struct meson_ee_pwrc_mem_domain axg_pwrc_mem_vpu[] = {
132 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
133 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
134};
135
Neil Armstrongba79d762020-09-30 11:52:49 +0200136static struct meson_ee_pwrc_mem_domain gxbb_pwrc_mem_vpu[] = {
137 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
138 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
139 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
140};
141
Neil Armstrong152b4532019-10-11 15:12:19 +0200142static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
143 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
144};
145
146static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
147 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
148 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
149 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
150 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
151 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
152 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
153 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
154 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
155 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
156};
157
158static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
159 { HHI_NANOQ_MEM_PD_REG0, 0xff },
160 { HHI_NANOQ_MEM_PD_REG1, 0xff },
161};
162
163static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
164 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
165};
166
167static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
168 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
169};
170
171static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
172 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
173};
174
Neil Armstrong48373372020-09-30 11:55:50 +0200175static struct meson_ee_pwrc_mem_domain axg_pwrc_mem_audio[] = {
176 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
177};
178
Neil Armstrong152b4532019-10-11 15:12:19 +0200179static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
180 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
181 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
182 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
183 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
184 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
185 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
186 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
187 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
188 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
189 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
190 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
191 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
192 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
193};
194
195#define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
196 { \
197 .name = __name, \
198 .reset_names_count = __resets, \
199 .clk_names_count = __clks, \
200 .top_pd = __top_pd, \
201 .mem_pd_count = ARRAY_SIZE(__mem), \
202 .mem_pd = __mem, \
203 .get_power = __get_power, \
204 }
205
206#define TOP_PD(__name, __top_pd, __mem, __get_power) \
207 { \
208 .name = __name, \
209 .top_pd = __top_pd, \
210 .mem_pd_count = ARRAY_SIZE(__mem), \
211 .mem_pd = __mem, \
212 .get_power = __get_power, \
213 }
214
215#define MEM_PD(__name, __mem) \
216 TOP_PD(__name, NULL, __mem, NULL)
217
218static bool pwrc_ee_get_power(struct power_domain *power_domain);
219
220static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
Neil Armstrongba79d762020-09-30 11:52:49 +0200221 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, g12a_pwrc_mem_vpu,
Neil Armstrong152b4532019-10-11 15:12:19 +0200222 pwrc_ee_get_power, 11, 2),
223 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
224};
225
Neil Armstrong48373372020-09-30 11:55:50 +0200226static struct meson_ee_pwrc_domain_desc axg_pwrc_domains[] = {
227 [PWRC_AXG_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, axg_pwrc_mem_vpu,
228 pwrc_ee_get_power, 5, 2),
229 [PWRC_AXG_ETHERNET_MEM_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
230 [PWRC_AXG_AUDIO_ID] = MEM_PD("AUDIO", axg_pwrc_mem_audio),
231};
232
Neil Armstrongba79d762020-09-30 11:52:49 +0200233static struct meson_ee_pwrc_domain_desc gxbb_pwrc_domains[] = {
234 [PWRC_GXBB_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, gxbb_pwrc_mem_vpu,
235 pwrc_ee_get_power, 12, 2),
236 [PWRC_GXBB_ETHERNET_MEM_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
237};
238
Neil Armstrong152b4532019-10-11 15:12:19 +0200239static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
240 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
241 pwrc_ee_get_power, 11, 2),
242 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
243 pwrc_ee_get_power),
244 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
245 pwrc_ee_get_power),
246 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
247 pwrc_ee_get_power),
248 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
249 pwrc_ee_get_power),
250 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
251 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
252};
253
254struct meson_ee_pwrc_priv {
255 struct regmap *regmap_ao;
256 struct regmap *regmap_hhi;
257 struct reset_ctl_bulk resets;
258 struct clk_bulk clks;
259 const struct meson_ee_pwrc_domain_data *data;
260};
261
262static bool pwrc_ee_get_power(struct power_domain *power_domain)
263{
264 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
265 struct meson_ee_pwrc_domain_desc *pwrc_domain;
266 u32 reg;
267
268 pwrc_domain = &priv->data->domains[power_domain->id];
269
270 regmap_read(priv->regmap_ao,
271 pwrc_domain->top_pd->sleep_reg, &reg);
272
273 return (reg & pwrc_domain->top_pd->sleep_mask);
274}
275
Neil Armstrong152b4532019-10-11 15:12:19 +0200276static int meson_ee_pwrc_off(struct power_domain *power_domain)
277{
278 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
279 struct meson_ee_pwrc_domain_desc *pwrc_domain;
280 int i;
281
282 pwrc_domain = &priv->data->domains[power_domain->id];
283
284 if (pwrc_domain->top_pd)
285 regmap_update_bits(priv->regmap_ao,
286 pwrc_domain->top_pd->sleep_reg,
287 pwrc_domain->top_pd->sleep_mask,
288 pwrc_domain->top_pd->sleep_mask);
289 udelay(20);
290
291 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
292 regmap_update_bits(priv->regmap_hhi,
293 pwrc_domain->mem_pd[i].reg,
294 pwrc_domain->mem_pd[i].mask,
295 pwrc_domain->mem_pd[i].mask);
296
297 udelay(20);
298
299 if (pwrc_domain->top_pd)
300 regmap_update_bits(priv->regmap_ao,
301 pwrc_domain->top_pd->iso_reg,
302 pwrc_domain->top_pd->iso_mask,
303 pwrc_domain->top_pd->iso_mask);
304
305 if (pwrc_domain->clk_names_count) {
306 mdelay(20);
307 clk_disable_bulk(&priv->clks);
308 }
309
Neil Armstronge8e3f1d2024-10-09 11:15:21 +0200310 pwrc_domain->enabled = false;
311
Neil Armstrong152b4532019-10-11 15:12:19 +0200312 return 0;
313}
314
315static int meson_ee_pwrc_on(struct power_domain *power_domain)
316{
317 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
318 struct meson_ee_pwrc_domain_desc *pwrc_domain;
319 int i, ret;
320
321 pwrc_domain = &priv->data->domains[power_domain->id];
322
Neil Armstronge8e3f1d2024-10-09 11:15:21 +0200323 if (pwrc_domain->enabled)
324 return 0;
325
Neil Armstrong152b4532019-10-11 15:12:19 +0200326 if (pwrc_domain->top_pd)
327 regmap_update_bits(priv->regmap_ao,
328 pwrc_domain->top_pd->sleep_reg,
329 pwrc_domain->top_pd->sleep_mask, 0);
330 udelay(20);
331
332 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
333 regmap_update_bits(priv->regmap_hhi,
334 pwrc_domain->mem_pd[i].reg,
335 pwrc_domain->mem_pd[i].mask, 0);
336
337 udelay(20);
338
339 if (pwrc_domain->reset_names_count) {
340 ret = reset_assert_bulk(&priv->resets);
341 if (ret)
342 return ret;
343 }
344
345 if (pwrc_domain->top_pd)
346 regmap_update_bits(priv->regmap_ao,
347 pwrc_domain->top_pd->iso_reg,
348 pwrc_domain->top_pd->iso_mask, 0);
349
350 if (pwrc_domain->reset_names_count) {
351 ret = reset_deassert_bulk(&priv->resets);
352 if (ret)
353 return ret;
354 }
355
Neil Armstronge8e3f1d2024-10-09 11:15:21 +0200356 if (pwrc_domain->clk_names_count) {
357 ret = clk_enable_bulk(&priv->clks);
358 if (ret)
359 return ret;
360 }
361
362 pwrc_domain->enabled = true;
Neil Armstrong152b4532019-10-11 15:12:19 +0200363
364 return 0;
365}
366
367static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
368 struct ofnode_phandle_args *args)
369{
370 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
371
372 /* #power-domain-cells is 1 */
373
374 if (args->args_count < 1) {
375 debug("Invalid args_count: %d\n", args->args_count);
376 return -EINVAL;
377 }
378
379 power_domain->id = args->args[0];
380
381 if (power_domain->id >= priv->data->count) {
382 debug("Invalid domain ID: %lu\n", power_domain->id);
383 return -EINVAL;
384 }
385
386 return 0;
387}
388
389struct power_domain_ops meson_ee_pwrc_ops = {
Neil Armstrong152b4532019-10-11 15:12:19 +0200390 .off = meson_ee_pwrc_off,
391 .on = meson_ee_pwrc_on,
Neil Armstrong152b4532019-10-11 15:12:19 +0200392 .of_xlate = meson_ee_pwrc_of_xlate,
393};
394
395static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
396 .count = ARRAY_SIZE(g12a_pwrc_domains),
397 .domains = g12a_pwrc_domains,
398};
399
Neil Armstrong48373372020-09-30 11:55:50 +0200400static struct meson_ee_pwrc_domain_data meson_ee_axg_pwrc_data = {
401 .count = ARRAY_SIZE(axg_pwrc_domains),
402 .domains = axg_pwrc_domains,
403};
404
Neil Armstrongba79d762020-09-30 11:52:49 +0200405static struct meson_ee_pwrc_domain_data meson_ee_gxbb_pwrc_data = {
406 .count = ARRAY_SIZE(gxbb_pwrc_domains),
407 .domains = gxbb_pwrc_domains,
408};
409
Neil Armstrong152b4532019-10-11 15:12:19 +0200410static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
411 .count = ARRAY_SIZE(sm1_pwrc_domains),
412 .domains = sm1_pwrc_domains,
413};
414
415static const struct udevice_id meson_ee_pwrc_ids[] = {
416 {
417 .compatible = "amlogic,meson-g12a-pwrc",
418 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
419 },
420 {
Neil Armstrongba79d762020-09-30 11:52:49 +0200421 .compatible = "amlogic,meson-gxbb-pwrc",
422 .data = (unsigned long)&meson_ee_gxbb_pwrc_data,
423 },
424 {
Neil Armstrong48373372020-09-30 11:55:50 +0200425 .compatible = "amlogic,meson-axg-pwrc",
426 .data = (unsigned long)&meson_ee_axg_pwrc_data,
427 },
428 {
Neil Armstrong152b4532019-10-11 15:12:19 +0200429 .compatible = "amlogic,meson-sm1-pwrc",
430 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
431 },
432 { }
433};
434
435static int meson_ee_pwrc_probe(struct udevice *dev)
436{
437 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
438 u32 ao_phandle;
439 ofnode ao_node;
440 int ret;
441
442 priv->data = (void *)dev_get_driver_data(dev);
443 if (!priv->data)
444 return -EINVAL;
445
Simon Glassa7ece582020-12-19 10:40:14 -0700446 priv->regmap_hhi = syscon_node_to_regmap(dev_ofnode(dev_get_parent(dev)));
Neil Armstrong152b4532019-10-11 15:12:19 +0200447 if (IS_ERR(priv->regmap_hhi))
448 return PTR_ERR(priv->regmap_hhi);
449
Simon Glassa7ece582020-12-19 10:40:14 -0700450 ret = ofnode_read_u32(dev_ofnode(dev), "amlogic,ao-sysctrl",
Neil Armstrong152b4532019-10-11 15:12:19 +0200451 &ao_phandle);
452 if (ret)
453 return ret;
454
455 ao_node = ofnode_get_by_phandle(ao_phandle);
456 if (!ofnode_valid(ao_node))
457 return -EINVAL;
458
459 priv->regmap_ao = syscon_node_to_regmap(ao_node);
460 if (IS_ERR(priv->regmap_ao))
461 return PTR_ERR(priv->regmap_ao);
462
463 ret = reset_get_bulk(dev, &priv->resets);
464 if (ret)
465 return ret;
466
467 ret = clk_get_bulk(dev, &priv->clks);
468 if (ret)
469 return ret;
470
471 return 0;
472}
473
474U_BOOT_DRIVER(meson_ee_pwrc) = {
475 .name = "meson_ee_pwrc",
476 .id = UCLASS_POWER_DOMAIN,
477 .of_match = meson_ee_pwrc_ids,
478 .probe = meson_ee_pwrc_probe,
479 .ops = &meson_ee_pwrc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700480 .priv_auto = sizeof(struct meson_ee_pwrc_priv),
Neil Armstrong152b4532019-10-11 15:12:19 +0200481};