blob: 20e9f32b3819b3ce067eb07009d8845778544df1 [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);
63};
64
65struct meson_ee_pwrc_domain_data {
66 unsigned int count;
67 struct meson_ee_pwrc_domain_desc *domains;
68};
69
70/* TOP Power Domains */
71
Neil Armstrongba79d762020-09-30 11:52:49 +020072static struct meson_ee_pwrc_top_domain gx_pwrc_vpu = {
Neil Armstrong152b4532019-10-11 15:12:19 +020073 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0,
74 .sleep_mask = BIT(8),
75 .iso_reg = AO_RTI_GEN_PWR_SLEEP0,
76 .iso_mask = BIT(9),
77};
78
79#define SM1_EE_PD(__bit) \
80 { \
81 .sleep_reg = AO_RTI_GEN_PWR_SLEEP0, \
82 .sleep_mask = BIT(__bit), \
83 .iso_reg = AO_RTI_GEN_PWR_ISO0, \
84 .iso_mask = BIT(__bit), \
85 }
86
87static struct meson_ee_pwrc_top_domain sm1_pwrc_vpu = SM1_EE_PD(8);
88static struct meson_ee_pwrc_top_domain sm1_pwrc_nna = SM1_EE_PD(16);
89static struct meson_ee_pwrc_top_domain sm1_pwrc_usb = SM1_EE_PD(17);
90static struct meson_ee_pwrc_top_domain sm1_pwrc_pci = SM1_EE_PD(18);
91static struct meson_ee_pwrc_top_domain sm1_pwrc_ge2d = SM1_EE_PD(19);
92
93/* Memory PD Domains */
94
95#define VPU_MEMPD(__reg) \
96 { __reg, GENMASK(1, 0) }, \
97 { __reg, GENMASK(3, 2) }, \
98 { __reg, GENMASK(5, 4) }, \
99 { __reg, GENMASK(7, 6) }, \
100 { __reg, GENMASK(9, 8) }, \
101 { __reg, GENMASK(11, 10) }, \
102 { __reg, GENMASK(13, 12) }, \
103 { __reg, GENMASK(15, 14) }, \
104 { __reg, GENMASK(17, 16) }, \
105 { __reg, GENMASK(19, 18) }, \
106 { __reg, GENMASK(21, 20) }, \
107 { __reg, GENMASK(23, 22) }, \
108 { __reg, GENMASK(25, 24) }, \
109 { __reg, GENMASK(27, 26) }, \
110 { __reg, GENMASK(29, 28) }, \
111 { __reg, GENMASK(31, 30) }
112
113#define VPU_HHI_MEMPD(__reg) \
114 { __reg, BIT(8) }, \
115 { __reg, BIT(9) }, \
116 { __reg, BIT(10) }, \
117 { __reg, BIT(11) }, \
118 { __reg, BIT(12) }, \
119 { __reg, BIT(13) }, \
120 { __reg, BIT(14) }, \
121 { __reg, BIT(15) }
122
123static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_vpu[] = {
124 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
125 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
126 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
127 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
128};
129
Neil Armstrong48373372020-09-30 11:55:50 +0200130static struct meson_ee_pwrc_mem_domain axg_pwrc_mem_vpu[] = {
131 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
132 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
133};
134
Neil Armstrongba79d762020-09-30 11:52:49 +0200135static struct meson_ee_pwrc_mem_domain gxbb_pwrc_mem_vpu[] = {
136 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
137 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
138 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
139};
140
Neil Armstrong152b4532019-10-11 15:12:19 +0200141static struct meson_ee_pwrc_mem_domain g12a_pwrc_mem_eth[] = {
142 { HHI_MEM_PD_REG0, GENMASK(3, 2) },
143};
144
145static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_vpu[] = {
146 VPU_MEMPD(HHI_VPU_MEM_PD_REG0),
147 VPU_MEMPD(HHI_VPU_MEM_PD_REG1),
148 VPU_MEMPD(HHI_VPU_MEM_PD_REG2),
149 VPU_MEMPD(HHI_VPU_MEM_PD_REG3),
150 { HHI_VPU_MEM_PD_REG4, GENMASK(1, 0) },
151 { HHI_VPU_MEM_PD_REG4, GENMASK(3, 2) },
152 { HHI_VPU_MEM_PD_REG4, GENMASK(5, 4) },
153 { HHI_VPU_MEM_PD_REG4, GENMASK(7, 6) },
154 VPU_HHI_MEMPD(HHI_MEM_PD_REG0),
155};
156
157static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_nna[] = {
158 { HHI_NANOQ_MEM_PD_REG0, 0xff },
159 { HHI_NANOQ_MEM_PD_REG1, 0xff },
160};
161
162static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_usb[] = {
163 { HHI_MEM_PD_REG0, GENMASK(31, 30) },
164};
165
166static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_pcie[] = {
167 { HHI_MEM_PD_REG0, GENMASK(29, 26) },
168};
169
170static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_ge2d[] = {
171 { HHI_MEM_PD_REG0, GENMASK(25, 18) },
172};
173
Neil Armstrong48373372020-09-30 11:55:50 +0200174static struct meson_ee_pwrc_mem_domain axg_pwrc_mem_audio[] = {
175 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
176};
177
Neil Armstrong152b4532019-10-11 15:12:19 +0200178static struct meson_ee_pwrc_mem_domain sm1_pwrc_mem_audio[] = {
179 { HHI_MEM_PD_REG0, GENMASK(5, 4) },
180 { HHI_AUDIO_MEM_PD_REG0, GENMASK(1, 0) },
181 { HHI_AUDIO_MEM_PD_REG0, GENMASK(3, 2) },
182 { HHI_AUDIO_MEM_PD_REG0, GENMASK(5, 4) },
183 { HHI_AUDIO_MEM_PD_REG0, GENMASK(7, 6) },
184 { HHI_AUDIO_MEM_PD_REG0, GENMASK(13, 12) },
185 { HHI_AUDIO_MEM_PD_REG0, GENMASK(15, 14) },
186 { HHI_AUDIO_MEM_PD_REG0, GENMASK(17, 16) },
187 { HHI_AUDIO_MEM_PD_REG0, GENMASK(19, 18) },
188 { HHI_AUDIO_MEM_PD_REG0, GENMASK(21, 20) },
189 { HHI_AUDIO_MEM_PD_REG0, GENMASK(23, 22) },
190 { HHI_AUDIO_MEM_PD_REG0, GENMASK(25, 24) },
191 { HHI_AUDIO_MEM_PD_REG0, GENMASK(27, 26) },
192};
193
194#define VPU_PD(__name, __top_pd, __mem, __get_power, __resets, __clks) \
195 { \
196 .name = __name, \
197 .reset_names_count = __resets, \
198 .clk_names_count = __clks, \
199 .top_pd = __top_pd, \
200 .mem_pd_count = ARRAY_SIZE(__mem), \
201 .mem_pd = __mem, \
202 .get_power = __get_power, \
203 }
204
205#define TOP_PD(__name, __top_pd, __mem, __get_power) \
206 { \
207 .name = __name, \
208 .top_pd = __top_pd, \
209 .mem_pd_count = ARRAY_SIZE(__mem), \
210 .mem_pd = __mem, \
211 .get_power = __get_power, \
212 }
213
214#define MEM_PD(__name, __mem) \
215 TOP_PD(__name, NULL, __mem, NULL)
216
217static bool pwrc_ee_get_power(struct power_domain *power_domain);
218
219static struct meson_ee_pwrc_domain_desc g12a_pwrc_domains[] = {
Neil Armstrongba79d762020-09-30 11:52:49 +0200220 [PWRC_G12A_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, g12a_pwrc_mem_vpu,
Neil Armstrong152b4532019-10-11 15:12:19 +0200221 pwrc_ee_get_power, 11, 2),
222 [PWRC_G12A_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
223};
224
Neil Armstrong48373372020-09-30 11:55:50 +0200225static struct meson_ee_pwrc_domain_desc axg_pwrc_domains[] = {
226 [PWRC_AXG_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, axg_pwrc_mem_vpu,
227 pwrc_ee_get_power, 5, 2),
228 [PWRC_AXG_ETHERNET_MEM_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
229 [PWRC_AXG_AUDIO_ID] = MEM_PD("AUDIO", axg_pwrc_mem_audio),
230};
231
Neil Armstrongba79d762020-09-30 11:52:49 +0200232static struct meson_ee_pwrc_domain_desc gxbb_pwrc_domains[] = {
233 [PWRC_GXBB_VPU_ID] = VPU_PD("VPU", &gx_pwrc_vpu, gxbb_pwrc_mem_vpu,
234 pwrc_ee_get_power, 12, 2),
235 [PWRC_GXBB_ETHERNET_MEM_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
236};
237
Neil Armstrong152b4532019-10-11 15:12:19 +0200238static struct meson_ee_pwrc_domain_desc sm1_pwrc_domains[] = {
239 [PWRC_SM1_VPU_ID] = VPU_PD("VPU", &sm1_pwrc_vpu, sm1_pwrc_mem_vpu,
240 pwrc_ee_get_power, 11, 2),
241 [PWRC_SM1_NNA_ID] = TOP_PD("NNA", &sm1_pwrc_nna, sm1_pwrc_mem_nna,
242 pwrc_ee_get_power),
243 [PWRC_SM1_USB_ID] = TOP_PD("USB", &sm1_pwrc_usb, sm1_pwrc_mem_usb,
244 pwrc_ee_get_power),
245 [PWRC_SM1_PCIE_ID] = TOP_PD("PCI", &sm1_pwrc_pci, sm1_pwrc_mem_pcie,
246 pwrc_ee_get_power),
247 [PWRC_SM1_GE2D_ID] = TOP_PD("GE2D", &sm1_pwrc_ge2d, sm1_pwrc_mem_ge2d,
248 pwrc_ee_get_power),
249 [PWRC_SM1_AUDIO_ID] = MEM_PD("AUDIO", sm1_pwrc_mem_audio),
250 [PWRC_SM1_ETH_ID] = MEM_PD("ETH", g12a_pwrc_mem_eth),
251};
252
253struct meson_ee_pwrc_priv {
254 struct regmap *regmap_ao;
255 struct regmap *regmap_hhi;
256 struct reset_ctl_bulk resets;
257 struct clk_bulk clks;
258 const struct meson_ee_pwrc_domain_data *data;
259};
260
261static bool pwrc_ee_get_power(struct power_domain *power_domain)
262{
263 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
264 struct meson_ee_pwrc_domain_desc *pwrc_domain;
265 u32 reg;
266
267 pwrc_domain = &priv->data->domains[power_domain->id];
268
269 regmap_read(priv->regmap_ao,
270 pwrc_domain->top_pd->sleep_reg, &reg);
271
272 return (reg & pwrc_domain->top_pd->sleep_mask);
273}
274
Neil Armstrong152b4532019-10-11 15:12:19 +0200275static int meson_ee_pwrc_off(struct power_domain *power_domain)
276{
277 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
278 struct meson_ee_pwrc_domain_desc *pwrc_domain;
279 int i;
280
281 pwrc_domain = &priv->data->domains[power_domain->id];
282
283 if (pwrc_domain->top_pd)
284 regmap_update_bits(priv->regmap_ao,
285 pwrc_domain->top_pd->sleep_reg,
286 pwrc_domain->top_pd->sleep_mask,
287 pwrc_domain->top_pd->sleep_mask);
288 udelay(20);
289
290 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
291 regmap_update_bits(priv->regmap_hhi,
292 pwrc_domain->mem_pd[i].reg,
293 pwrc_domain->mem_pd[i].mask,
294 pwrc_domain->mem_pd[i].mask);
295
296 udelay(20);
297
298 if (pwrc_domain->top_pd)
299 regmap_update_bits(priv->regmap_ao,
300 pwrc_domain->top_pd->iso_reg,
301 pwrc_domain->top_pd->iso_mask,
302 pwrc_domain->top_pd->iso_mask);
303
304 if (pwrc_domain->clk_names_count) {
305 mdelay(20);
306 clk_disable_bulk(&priv->clks);
307 }
308
309 return 0;
310}
311
312static int meson_ee_pwrc_on(struct power_domain *power_domain)
313{
314 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
315 struct meson_ee_pwrc_domain_desc *pwrc_domain;
316 int i, ret;
317
318 pwrc_domain = &priv->data->domains[power_domain->id];
319
320 if (pwrc_domain->top_pd)
321 regmap_update_bits(priv->regmap_ao,
322 pwrc_domain->top_pd->sleep_reg,
323 pwrc_domain->top_pd->sleep_mask, 0);
324 udelay(20);
325
326 for (i = 0 ; i < pwrc_domain->mem_pd_count ; ++i)
327 regmap_update_bits(priv->regmap_hhi,
328 pwrc_domain->mem_pd[i].reg,
329 pwrc_domain->mem_pd[i].mask, 0);
330
331 udelay(20);
332
333 if (pwrc_domain->reset_names_count) {
334 ret = reset_assert_bulk(&priv->resets);
335 if (ret)
336 return ret;
337 }
338
339 if (pwrc_domain->top_pd)
340 regmap_update_bits(priv->regmap_ao,
341 pwrc_domain->top_pd->iso_reg,
342 pwrc_domain->top_pd->iso_mask, 0);
343
344 if (pwrc_domain->reset_names_count) {
345 ret = reset_deassert_bulk(&priv->resets);
346 if (ret)
347 return ret;
348 }
349
350 if (pwrc_domain->clk_names_count)
351 return clk_enable_bulk(&priv->clks);
352
353 return 0;
354}
355
356static int meson_ee_pwrc_of_xlate(struct power_domain *power_domain,
357 struct ofnode_phandle_args *args)
358{
359 struct meson_ee_pwrc_priv *priv = dev_get_priv(power_domain->dev);
360
361 /* #power-domain-cells is 1 */
362
363 if (args->args_count < 1) {
364 debug("Invalid args_count: %d\n", args->args_count);
365 return -EINVAL;
366 }
367
368 power_domain->id = args->args[0];
369
370 if (power_domain->id >= priv->data->count) {
371 debug("Invalid domain ID: %lu\n", power_domain->id);
372 return -EINVAL;
373 }
374
375 return 0;
376}
377
378struct power_domain_ops meson_ee_pwrc_ops = {
Neil Armstrong152b4532019-10-11 15:12:19 +0200379 .off = meson_ee_pwrc_off,
380 .on = meson_ee_pwrc_on,
Neil Armstrong152b4532019-10-11 15:12:19 +0200381 .of_xlate = meson_ee_pwrc_of_xlate,
382};
383
384static struct meson_ee_pwrc_domain_data meson_ee_g12a_pwrc_data = {
385 .count = ARRAY_SIZE(g12a_pwrc_domains),
386 .domains = g12a_pwrc_domains,
387};
388
Neil Armstrong48373372020-09-30 11:55:50 +0200389static struct meson_ee_pwrc_domain_data meson_ee_axg_pwrc_data = {
390 .count = ARRAY_SIZE(axg_pwrc_domains),
391 .domains = axg_pwrc_domains,
392};
393
Neil Armstrongba79d762020-09-30 11:52:49 +0200394static struct meson_ee_pwrc_domain_data meson_ee_gxbb_pwrc_data = {
395 .count = ARRAY_SIZE(gxbb_pwrc_domains),
396 .domains = gxbb_pwrc_domains,
397};
398
Neil Armstrong152b4532019-10-11 15:12:19 +0200399static struct meson_ee_pwrc_domain_data meson_ee_sm1_pwrc_data = {
400 .count = ARRAY_SIZE(sm1_pwrc_domains),
401 .domains = sm1_pwrc_domains,
402};
403
404static const struct udevice_id meson_ee_pwrc_ids[] = {
405 {
406 .compatible = "amlogic,meson-g12a-pwrc",
407 .data = (unsigned long)&meson_ee_g12a_pwrc_data,
408 },
409 {
Neil Armstrongba79d762020-09-30 11:52:49 +0200410 .compatible = "amlogic,meson-gxbb-pwrc",
411 .data = (unsigned long)&meson_ee_gxbb_pwrc_data,
412 },
413 {
Neil Armstrong48373372020-09-30 11:55:50 +0200414 .compatible = "amlogic,meson-axg-pwrc",
415 .data = (unsigned long)&meson_ee_axg_pwrc_data,
416 },
417 {
Neil Armstrong152b4532019-10-11 15:12:19 +0200418 .compatible = "amlogic,meson-sm1-pwrc",
419 .data = (unsigned long)&meson_ee_sm1_pwrc_data,
420 },
421 { }
422};
423
424static int meson_ee_pwrc_probe(struct udevice *dev)
425{
426 struct meson_ee_pwrc_priv *priv = dev_get_priv(dev);
427 u32 ao_phandle;
428 ofnode ao_node;
429 int ret;
430
431 priv->data = (void *)dev_get_driver_data(dev);
432 if (!priv->data)
433 return -EINVAL;
434
Simon Glassa7ece582020-12-19 10:40:14 -0700435 priv->regmap_hhi = syscon_node_to_regmap(dev_ofnode(dev_get_parent(dev)));
Neil Armstrong152b4532019-10-11 15:12:19 +0200436 if (IS_ERR(priv->regmap_hhi))
437 return PTR_ERR(priv->regmap_hhi);
438
Simon Glassa7ece582020-12-19 10:40:14 -0700439 ret = ofnode_read_u32(dev_ofnode(dev), "amlogic,ao-sysctrl",
Neil Armstrong152b4532019-10-11 15:12:19 +0200440 &ao_phandle);
441 if (ret)
442 return ret;
443
444 ao_node = ofnode_get_by_phandle(ao_phandle);
445 if (!ofnode_valid(ao_node))
446 return -EINVAL;
447
448 priv->regmap_ao = syscon_node_to_regmap(ao_node);
449 if (IS_ERR(priv->regmap_ao))
450 return PTR_ERR(priv->regmap_ao);
451
452 ret = reset_get_bulk(dev, &priv->resets);
453 if (ret)
454 return ret;
455
456 ret = clk_get_bulk(dev, &priv->clks);
457 if (ret)
458 return ret;
459
460 return 0;
461}
462
463U_BOOT_DRIVER(meson_ee_pwrc) = {
464 .name = "meson_ee_pwrc",
465 .id = UCLASS_POWER_DOMAIN,
466 .of_match = meson_ee_pwrc_ids,
467 .probe = meson_ee_pwrc_probe,
468 .ops = &meson_ee_pwrc_ops,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700469 .priv_auto = sizeof(struct meson_ee_pwrc_priv),
Neil Armstrong152b4532019-10-11 15:12:19 +0200470};