blob: 676fded8080ac57d3547fcf2b08c09a8d0e55f3b [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
7#include <common.h>
8#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Simon Glass9bc15642020-02-03 07:36:16 -070010#include <malloc.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020011#include <power-domain-uclass.h>
12#include <regmap.h>
13#include <syscon.h>
14#include <reset.h>
15#include <clk.h>
Neil Armstrong48373372020-09-30 11:55:50 +020016#include <dt-bindings/power/meson-axg-power.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020017#include <dt-bindings/power/meson-g12a-power.h>
Neil Armstrongba79d762020-09-30 11:52:49 +020018#include <dt-bindings/power/meson-gxbb-power.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020019#include <dt-bindings/power/meson-sm1-power.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060020#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060021#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070022#include <linux/err.h>
Neil Armstrong152b4532019-10-11 15:12:19 +020023
24/* AO Offsets */
25
26#define AO_RTI_GEN_PWR_SLEEP0 (0x3a << 2)
27#define AO_RTI_GEN_PWR_ISO0 (0x3b << 2)
28
29/* HHI Offsets */
30
31#define HHI_MEM_PD_REG0 (0x40 << 2)
32#define HHI_VPU_MEM_PD_REG0 (0x41 << 2)
33#define HHI_VPU_MEM_PD_REG1 (0x42 << 2)
34#define HHI_VPU_MEM_PD_REG3 (0x43 << 2)
35#define HHI_VPU_MEM_PD_REG4 (0x44 << 2)
36#define HHI_AUDIO_MEM_PD_REG0 (0x45 << 2)
37#define HHI_NANOQ_MEM_PD_REG0 (0x46 << 2)
38#define HHI_NANOQ_MEM_PD_REG1 (0x47 << 2)
39#define HHI_VPU_MEM_PD_REG2 (0x4d << 2)
40
41struct meson_ee_pwrc;
42struct meson_ee_pwrc_domain;
43
44struct meson_ee_pwrc_mem_domain {
45 unsigned int reg;
46 unsigned int mask;
47};
48
49struct meson_ee_pwrc_top_domain {
50 unsigned int sleep_reg;
51 unsigned int sleep_mask;
52 unsigned int iso_reg;
53 unsigned int iso_mask;
54};
55
56struct meson_ee_pwrc_domain_desc {
57 char *name;
58 unsigned int reset_names_count;
59 unsigned int clk_names_count;
60 struct meson_ee_pwrc_top_domain *top_pd;
61 unsigned int mem_pd_count;
62 struct meson_ee_pwrc_mem_domain *mem_pd;
63 bool (*get_power)(struct power_domain *power_domain);
64};
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
310 return 0;
311}
312
313static int meson_ee_pwrc_on(struct power_domain *power_domain)
314{
315 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
316 struct meson_ee_pwrc_domain_desc *pwrc_domain;
317 int i, ret;
318
319 pwrc_domain = &priv->data->domains[power_domain->id];
320
321 if (pwrc_domain->top_pd)
322 regmap_update_bits(priv->regmap_ao,
323 pwrc_domain->top_pd->sleep_reg,
324 pwrc_domain->top_pd->sleep_mask, 0);
325 udelay(20);
326
327 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
328 regmap_update_bits(priv->regmap_hhi,
329 pwrc_domain->mem_pd[i].reg,
330 pwrc_domain->mem_pd[i].mask, 0);
331
332 udelay(20);
333
334 if (pwrc_domain->reset_names_count) {
335 ret = reset_assert_bulk(&priv->resets);
336 if (ret)
337 return ret;
338 }
339
340 if (pwrc_domain->top_pd)
341 regmap_update_bits(priv->regmap_ao,
342 pwrc_domain->top_pd->iso_reg,
343 pwrc_domain->top_pd->iso_mask, 0);
344
345 if (pwrc_domain->reset_names_count) {
346 ret = reset_deassert_bulk(&priv->resets);
347 if (ret)
348 return ret;
349 }
350
351 if (pwrc_domain->clk_names_count)
352 return clk_enable_bulk(&priv->clks);
353
354 return 0;
355}
356
357static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
358 struct ofnode_phandle_args *args)
359{
360 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
361
362 /* #power-domain-cells is 1 */
363
364 if (args->args_count < 1) {
365 debug("Invalid args_count: %d\n", args->args_count);
366 return -EINVAL;
367 }
368
369 power_domain->id = args->args[0];
370
371 if (power_domain->id >= priv->data->count) {
372 debug("Invalid domain ID: %lu\n", power_domain->id);
373 return -EINVAL;
374 }
375
376 return 0;
377}
378
379struct power_domain_ops meson_ee_pwrc_ops = {
Neil Armstrong152b4532019-10-11 15:12:19 +0200380 .off = meson_ee_pwrc_off,
381 .on = meson_ee_pwrc_on,
Neil Armstrong152b4532019-10-11 15:12:19 +0200382 .of_xlate = meson_ee_pwrc_of_xlate,
383};
384
385static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
386 .count = ARRAY_SIZE(g12a_pwrc_domains),
387 .domains = g12a_pwrc_domains,
388};
389
Neil Armstrong48373372020-09-30 11:55:50 +0200390static struct meson_ee_pwrc_domain_data meson_ee_axg_pwrc_data = {
391 .count = ARRAY_SIZE(axg_pwrc_domains),
392 .domains = axg_pwrc_domains,
393};
394
Neil Armstrongba79d762020-09-30 11:52:49 +0200395static struct meson_ee_pwrc_domain_data meson_ee_gxbb_pwrc_data = {
396 .count = ARRAY_SIZE(gxbb_pwrc_domains),
397 .domains = gxbb_pwrc_domains,
398};
399
Neil Armstrong152b4532019-10-11 15:12:19 +0200400static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
401 .count = ARRAY_SIZE(sm1_pwrc_domains),
402 .domains = sm1_pwrc_domains,
403};
404
405static const struct udevice_id meson_ee_pwrc_ids[] = {
406 {
407 .compatible = "amlogic,meson-g12a-pwrc",
408 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
409 },
410 {
Neil Armstrongba79d762020-09-30 11:52:49 +0200411 .compatible = "amlogic,meson-gxbb-pwrc",
412 .data = (unsigned long)&meson_ee_gxbb_pwrc_data,
413 },
414 {
Neil Armstrong48373372020-09-30 11:55:50 +0200415 .compatible = "amlogic,meson-axg-pwrc",
416 .data = (unsigned long)&meson_ee_axg_pwrc_data,
417 },
418 {
Neil Armstrong152b4532019-10-11 15:12:19 +0200419 .compatible = "amlogic,meson-sm1-pwrc",
420 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
421 },
422 { }
423};
424
425static int meson_ee_pwrc_probe(struct udevice *dev)
426{
427 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
428 u32 ao_phandle;
429 ofnode ao_node;
430 int ret;
431
432 priv->data = (void *)dev_get_driver_data(dev);
433 if (!priv->data)
434 return -EINVAL;
435
Simon Glassa7ece582020-12-19 10:40:14 -0700436 priv->regmap_hhi = syscon_node_to_regmap(dev_ofnode(dev_get_parent(dev)));
Neil Armstrong152b4532019-10-11 15:12:19 +0200437 if (IS_ERR(priv->regmap_hhi))
438 return PTR_ERR(priv->regmap_hhi);
439
Simon Glassa7ece582020-12-19 10:40:14 -0700440 ret = ofnode_read_u32(dev_ofnode(dev), "amlogic,ao-sysctrl",
Neil Armstrong152b4532019-10-11 15:12:19 +0200441 &ao_phandle);
442 if (ret)
443 return ret;
444
445 ao_node = ofnode_get_by_phandle(ao_phandle);
446 if (!ofnode_valid(ao_node))
447 return -EINVAL;
448
449 priv->regmap_ao = syscon_node_to_regmap(ao_node);
450 if (IS_ERR(priv->regmap_ao))
451 return PTR_ERR(priv->regmap_ao);
452
453 ret = reset_get_bulk(dev, &priv->resets);
454 if (ret)
455 return ret;
456
457 ret = clk_get_bulk(dev, &priv->clks);
458 if (ret)
459 return ret;
460
461 return 0;
462}
463
464U_BOOT_DRIVER(meson_ee_pwrc) = {
465 .name = "meson_ee_pwrc",
466 .id = UCLASS_POWER_DOMAIN,
467 .of_match = meson_ee_pwrc_ids,
468 .probe = meson_ee_pwrc_probe,
469 .ops = &meson_ee_pwrc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700470 .priv_auto = sizeof(struct meson_ee_pwrc_priv),
Neil Armstrong152b4532019-10-11 15:12:19 +0200471};