1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (c) 2020 Collabora Ltd.
4 */
5 #include <linux/arm-smccc.h>
6 #include <linux/clk.h>
7 #include <linux/clk-provider.h>
8 #include <linux/init.h>
9 #include <linux/io.h>
10 #include <linux/iopoll.h>
11 #include <linux/mfd/syscon.h>
12 #include <linux/of.h>
13 #include <linux/of_clk.h>
14 #include <linux/platform_device.h>
15 #include <linux/pm_domain.h>
16 #include <linux/regmap.h>
17 #include <linux/regulator/consumer.h>
18 #include <linux/soc/mediatek/infracfg.h>
19 #include <linux/soc/mediatek/mtk_sip_svc.h>
20
21 #include "mt6735-pm-domains.h"
22 #include "mt6795-pm-domains.h"
23 #include "mt6858-pm-domains.h"
24 #include "mt6893-pm-domains.h"
25 #include "mt8167-pm-domains.h"
26 #include "mt8173-pm-domains.h"
27 #include "mt8183-pm-domains.h"
28 #include "mt8186-pm-domains.h"
29 #include "mt8188-pm-domains.h"
30 #include "mt8189-pm-domains.h"
31 #include "mt8192-pm-domains.h"
32 #include "mt8195-pm-domains.h"
33 #include "mt8196-pm-domains.h"
34 #include "mt8365-pm-domains.h"
35
36 #define MTK_POLL_DELAY_US 10
37 #define MTK_POLL_TIMEOUT USEC_PER_SEC
38
39 #define MTK_HWV_POLL_DELAY_US 5
40 #define MTK_HWV_POLL_TIMEOUT (300 * USEC_PER_MSEC)
41
42 #define MTK_HWV_PREPARE_DELAY_US 1
43 #define MTK_HWV_PREPARE_TIMEOUT (3 * USEC_PER_MSEC)
44
45 #define PWR_RST_B_BIT BIT(0)
46 #define PWR_ISO_BIT BIT(1)
47 #define PWR_ON_BIT BIT(2)
48 #define PWR_ON_2ND_BIT BIT(3)
49 #define PWR_CLK_DIS_BIT BIT(4)
50 #define PWR_SRAM_CLKISO_BIT BIT(5)
51 #define PWR_SRAM_ISOINT_B_BIT BIT(6)
52
53 #define PWR_RTFF_SAVE BIT(24)
54 #define PWR_RTFF_NRESTORE BIT(25)
55 #define PWR_RTFF_CLK_DIS BIT(26)
56 #define PWR_RTFF_SAVE_FLAG BIT(27)
57 #define PWR_RTFF_UFS_CLK_DIS BIT(28)
58
59 #define MTK_SIP_KERNEL_HWCCF_CONTROL MTK_SIP_SMC_CMD(0x540)
60
61 /* Secure MTCMOS commands for modem subsystem */
62 #define MTK_MD_MTCMOS_ENABLE 18
63 #define MTK_MD_MTCMOS_DISABLE 19
64
65 struct scpsys_domain {
66 struct generic_pm_domain genpd;
67 const struct scpsys_domain_data *data;
68 const struct scpsys_hwv_domain_data *hwv_data;
69 struct scpsys *scpsys;
70 int num_clks;
71 struct clk_bulk_data *clks;
72 int num_subsys_clks;
73 struct clk_bulk_data *subsys_clks;
74 struct regulator *supply;
75 };
76
77 struct scpsys {
78 struct device *dev;
79 struct regmap *base;
80 const struct scpsys_soc_data *soc_data;
81 u8 bus_prot_index[BUS_PROT_BLOCK_COUNT];
82 struct regmap **bus_prot;
83 struct genpd_onecell_data pd_data;
84 struct generic_pm_domain *domains[];
85 };
86
87 #define to_scpsys_domain(gpd) container_of(gpd, struct scpsys_domain, genpd)
88
scpsys_domain_is_on(struct scpsys_domain * pd)89 static bool scpsys_domain_is_on(struct scpsys_domain *pd)
90 {
91 struct scpsys *scpsys = pd->scpsys;
92 u32 mask = pd->data->sta_mask;
93 u32 status, status2, mask2;
94
95 mask2 = pd->data->sta2nd_mask ? pd->data->sta2nd_mask : mask;
96
97 regmap_read(scpsys->base, pd->data->pwr_sta_offs, &status);
98 status &= mask;
99
100 regmap_read(scpsys->base, pd->data->pwr_sta2nd_offs, &status2);
101 status2 &= mask2;
102
103 /* A domain is on when both status bits are set. */
104 return status && status2;
105 }
106
scpsys_hwv_domain_is_disable_done(struct scpsys_domain * pd)107 static bool scpsys_hwv_domain_is_disable_done(struct scpsys_domain *pd)
108 {
109 const struct scpsys_hwv_domain_data *hwv = pd->hwv_data;
110 u32 regs[2] = { hwv->done, hwv->clr_sta };
111 u32 val[2];
112 u32 mask = BIT(hwv->setclr_bit);
113
114 regmap_multi_reg_read(pd->scpsys->base, regs, val, 2);
115
116 /* Disable is done when the bit is set in DONE, cleared in CLR_STA */
117 return (val[0] & mask) && !(val[1] & mask);
118 }
119
scpsys_hwv_domain_is_enable_done(struct scpsys_domain * pd)120 static bool scpsys_hwv_domain_is_enable_done(struct scpsys_domain *pd)
121 {
122 const struct scpsys_hwv_domain_data *hwv = pd->hwv_data;
123 u32 regs[3] = { hwv->done, hwv->en, hwv->set_sta };
124 u32 val[3];
125 u32 mask = BIT(hwv->setclr_bit);
126
127 regmap_multi_reg_read(pd->scpsys->base, regs, val, 3);
128
129 /* Enable is done when the bit is set in DONE and EN, cleared in SET_STA */
130 return (val[0] & mask) && (val[1] & mask) && !(val[2] & mask);
131 }
132
scpsys_sec_infra_power_on(bool on)133 static int scpsys_sec_infra_power_on(bool on)
134 {
135 struct arm_smccc_res res;
136 unsigned long cmd = on ? 1 : 0;
137
138 arm_smccc_smc(MTK_SIP_KERNEL_HWCCF_CONTROL, cmd, 0, 0, 0, 0, 0, 0, &res);
139 return res.a0;
140 }
141
scpsys_sram_enable(struct scpsys_domain * pd)142 static int scpsys_sram_enable(struct scpsys_domain *pd)
143 {
144 u32 expected_ack, pdn_ack = pd->data->sram_pdn_ack_bits;
145 struct scpsys *scpsys = pd->scpsys;
146 unsigned int tmp;
147 int ret;
148
149 if (MTK_SCPD_CAPS(pd, MTK_SCPD_SRAM_PDN_INVERTED)) {
150 regmap_set_bits(scpsys->base, pd->data->ctl_offs, pd->data->sram_pdn_bits);
151 expected_ack = pdn_ack;
152 } else {
153 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, pd->data->sram_pdn_bits);
154 expected_ack = 0;
155 }
156
157 /* Either wait until SRAM_PDN_ACK all 1 or 0 */
158 ret = regmap_read_poll_timeout(scpsys->base, pd->data->ctl_offs, tmp,
159 (tmp & pdn_ack) == expected_ack,
160 MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
161 if (ret < 0)
162 return ret;
163
164 if (MTK_SCPD_CAPS(pd, MTK_SCPD_SRAM_ISO)) {
165 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_SRAM_ISOINT_B_BIT);
166 udelay(1);
167 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_SRAM_CLKISO_BIT);
168 }
169
170 return 0;
171 }
172
scpsys_sram_disable(struct scpsys_domain * pd)173 static int scpsys_sram_disable(struct scpsys_domain *pd)
174 {
175 u32 expected_ack, pdn_ack = pd->data->sram_pdn_ack_bits;
176 struct scpsys *scpsys = pd->scpsys;
177 unsigned int tmp;
178
179 if (MTK_SCPD_CAPS(pd, MTK_SCPD_SRAM_ISO)) {
180 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_SRAM_CLKISO_BIT);
181 udelay(1);
182 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_SRAM_ISOINT_B_BIT);
183 }
184
185 if (MTK_SCPD_CAPS(pd, MTK_SCPD_SRAM_PDN_INVERTED)) {
186 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, pd->data->sram_pdn_bits);
187 expected_ack = 0;
188 } else {
189 regmap_set_bits(scpsys->base, pd->data->ctl_offs, pd->data->sram_pdn_bits);
190 expected_ack = pdn_ack;
191 }
192
193 /* Either wait until SRAM_PDN_ACK all 1 or 0 */
194 return regmap_read_poll_timeout(scpsys->base, pd->data->ctl_offs, tmp,
195 (tmp & pdn_ack) == expected_ack,
196 MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
197 }
198
scpsys_bus_protect_get_regmap(struct scpsys_domain * pd,const struct scpsys_bus_prot_data * bpd)199 static struct regmap *scpsys_bus_protect_get_regmap(struct scpsys_domain *pd,
200 const struct scpsys_bus_prot_data *bpd)
201 {
202 struct scpsys *scpsys = pd->scpsys;
203 unsigned short block_idx = scpsys->bus_prot_index[bpd->bus_prot_block];
204
205 return scpsys->bus_prot[block_idx];
206 }
207
scpsys_bus_protect_get_sta_regmap(struct scpsys_domain * pd,const struct scpsys_bus_prot_data * bpd)208 static struct regmap *scpsys_bus_protect_get_sta_regmap(struct scpsys_domain *pd,
209 const struct scpsys_bus_prot_data *bpd)
210 {
211 struct scpsys *scpsys = pd->scpsys;
212 int block_idx = scpsys->bus_prot_index[bpd->bus_prot_sta_block];
213
214 return scpsys->bus_prot[block_idx];
215 }
216
scpsys_bus_protect_clear(struct scpsys_domain * pd,const struct scpsys_bus_prot_data * bpd)217 static int scpsys_bus_protect_clear(struct scpsys_domain *pd,
218 const struct scpsys_bus_prot_data *bpd)
219 {
220 struct regmap *sta_regmap = scpsys_bus_protect_get_sta_regmap(pd, bpd);
221 struct regmap *regmap = scpsys_bus_protect_get_regmap(pd, bpd);
222 u32 sta_mask = bpd->bus_prot_sta_mask;
223 u32 expected_ack;
224 u32 val;
225
226 expected_ack = (bpd->bus_prot_sta_block == BUS_PROT_BLOCK_INFRA_NAO ? sta_mask : 0);
227
228 if (bpd->flags & BUS_PROT_REG_UPDATE)
229 regmap_clear_bits(regmap, bpd->bus_prot_clr, bpd->bus_prot_set_clr_mask);
230 else
231 regmap_write(regmap, bpd->bus_prot_clr, bpd->bus_prot_set_clr_mask);
232
233 if (bpd->flags & BUS_PROT_IGNORE_CLR_ACK)
234 return 0;
235
236 return regmap_read_poll_timeout(sta_regmap, bpd->bus_prot_sta,
237 val, (val & sta_mask) == expected_ack,
238 MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
239 }
240
scpsys_bus_protect_set(struct scpsys_domain * pd,const struct scpsys_bus_prot_data * bpd)241 static int scpsys_bus_protect_set(struct scpsys_domain *pd,
242 const struct scpsys_bus_prot_data *bpd)
243 {
244 struct regmap *sta_regmap = scpsys_bus_protect_get_sta_regmap(pd, bpd);
245 struct regmap *regmap = scpsys_bus_protect_get_regmap(pd, bpd);
246 u32 sta_mask = bpd->bus_prot_sta_mask;
247 u32 val;
248
249 if (bpd->flags & BUS_PROT_REG_UPDATE)
250 regmap_set_bits(regmap, bpd->bus_prot_set, bpd->bus_prot_set_clr_mask);
251 else
252 regmap_write(regmap, bpd->bus_prot_set, bpd->bus_prot_set_clr_mask);
253
254 return regmap_read_poll_timeout(sta_regmap, bpd->bus_prot_sta,
255 val, (val & sta_mask) == sta_mask,
256 MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
257 }
258
scpsys_bus_protect_enable(struct scpsys_domain * pd,u8 flags)259 static int scpsys_bus_protect_enable(struct scpsys_domain *pd, u8 flags)
260 {
261 for (int i = 0; i < SPM_MAX_BUS_PROT_DATA; i++) {
262 const struct scpsys_bus_prot_data *bpd = &pd->data->bp_cfg[i];
263 int ret;
264
265 if (!bpd->bus_prot_set_clr_mask)
266 break;
267
268 if ((bpd->flags & BUS_PROT_IGNORE_SUBCLK) !=
269 (flags & BUS_PROT_IGNORE_SUBCLK))
270 continue;
271
272 if (bpd->flags & BUS_PROT_INVERTED)
273 ret = scpsys_bus_protect_clear(pd, bpd);
274 else
275 ret = scpsys_bus_protect_set(pd, bpd);
276 if (ret)
277 return ret;
278 }
279
280 return 0;
281 }
282
scpsys_bus_protect_disable(struct scpsys_domain * pd,u8 flags)283 static int scpsys_bus_protect_disable(struct scpsys_domain *pd, u8 flags)
284 {
285 for (int i = SPM_MAX_BUS_PROT_DATA - 1; i >= 0; i--) {
286 const struct scpsys_bus_prot_data *bpd = &pd->data->bp_cfg[i];
287 int ret;
288
289 if (!bpd->bus_prot_set_clr_mask)
290 continue;
291
292 if ((bpd->flags & BUS_PROT_IGNORE_SUBCLK) !=
293 (flags & BUS_PROT_IGNORE_SUBCLK))
294 continue;
295
296 if (bpd->flags & BUS_PROT_INVERTED)
297 ret = scpsys_bus_protect_set(pd, bpd);
298 else
299 ret = scpsys_bus_protect_clear(pd, bpd);
300 if (ret)
301 return ret;
302 }
303
304 return 0;
305 }
306
scpsys_regulator_enable(struct regulator * supply)307 static int scpsys_regulator_enable(struct regulator *supply)
308 {
309 return supply ? regulator_enable(supply) : 0;
310 }
311
scpsys_regulator_disable(struct regulator * supply)312 static int scpsys_regulator_disable(struct regulator *supply)
313 {
314 return supply ? regulator_disable(supply) : 0;
315 }
316
scpsys_hwv_power_on(struct generic_pm_domain * genpd)317 static int scpsys_hwv_power_on(struct generic_pm_domain *genpd)
318 {
319 struct scpsys_domain *pd = container_of(genpd, struct scpsys_domain, genpd);
320 const struct scpsys_hwv_domain_data *hwv = pd->hwv_data;
321 struct scpsys *scpsys = pd->scpsys;
322 u32 val;
323 int ret;
324
325 if (MTK_SCPD_CAPS(pd, MTK_SCPD_INFRA_PWR_CTL)) {
326 ret = scpsys_sec_infra_power_on(true);
327 if (ret)
328 return ret;
329 }
330
331 ret = scpsys_regulator_enable(pd->supply);
332 if (ret)
333 goto err_infra;
334
335 ret = clk_bulk_prepare_enable(pd->num_clks, pd->clks);
336 if (ret)
337 goto err_reg;
338
339 /* For HWV the subsys clocks refer to the HWV low power subsystem */
340 ret = clk_bulk_prepare_enable(pd->num_subsys_clks, pd->subsys_clks);
341 if (ret)
342 goto err_disable_clks;
343
344 /* Make sure the HW Voter is idle and able to accept commands */
345 ret = regmap_read_poll_timeout_atomic(scpsys->base, hwv->done, val,
346 val & BIT(hwv->setclr_bit),
347 MTK_HWV_POLL_DELAY_US,
348 MTK_HWV_POLL_TIMEOUT);
349 if (ret) {
350 dev_err(scpsys->dev, "Failed to power on: HW Voter busy.\n");
351 goto err_disable_subsys_clks;
352 }
353
354 /*
355 * Instruct the HWV to power on the MTCMOS (power domain): after that,
356 * the same bit will be unset immediately by the hardware.
357 */
358 regmap_write(scpsys->base, hwv->set, BIT(hwv->setclr_bit));
359
360 /*
361 * Wait until the HWV sets the bit again, signalling that its internal
362 * state machine was started and it now processing the vote command.
363 */
364 ret = regmap_read_poll_timeout_atomic(scpsys->base, hwv->set, val,
365 val & BIT(hwv->setclr_bit),
366 MTK_HWV_PREPARE_DELAY_US,
367 MTK_HWV_PREPARE_TIMEOUT);
368 if (ret) {
369 dev_err(scpsys->dev, "Failed to power on: HW Voter not starting.\n");
370 goto err_disable_subsys_clks;
371 }
372
373 /* Wait for ACK, signalling that the MTCMOS was enabled */
374 ret = readx_poll_timeout_atomic(scpsys_hwv_domain_is_enable_done, pd, val, val,
375 MTK_HWV_POLL_DELAY_US, MTK_HWV_POLL_TIMEOUT);
376 if (ret) {
377 dev_err(scpsys->dev, "Failed to power on: HW Voter ACK timeout.\n");
378 goto err_disable_subsys_clks;
379 }
380
381 /* It's done! Disable the HWV low power subsystem clocks */
382 clk_bulk_disable_unprepare(pd->num_subsys_clks, pd->subsys_clks);
383
384 if (MTK_SCPD_CAPS(pd, MTK_SCPD_INFRA_PWR_CTL))
385 scpsys_sec_infra_power_on(false);
386
387 return 0;
388
389 err_disable_subsys_clks:
390 clk_bulk_disable_unprepare(pd->num_subsys_clks, pd->subsys_clks);
391 err_disable_clks:
392 clk_bulk_disable_unprepare(pd->num_clks, pd->clks);
393 err_reg:
394 scpsys_regulator_disable(pd->supply);
395 err_infra:
396 if (MTK_SCPD_CAPS(pd, MTK_SCPD_INFRA_PWR_CTL))
397 scpsys_sec_infra_power_on(false);
398 return ret;
399 };
400
scpsys_hwv_power_off_internal(struct scpsys_domain * pd)401 static int scpsys_hwv_power_off_internal(struct scpsys_domain *pd)
402 {
403 const struct scpsys_hwv_domain_data *hwv = pd->hwv_data;
404 struct scpsys *scpsys = pd->scpsys;
405 u32 val;
406 int ret;
407
408 if (MTK_SCPD_CAPS(pd, MTK_SCPD_INFRA_PWR_CTL)) {
409 ret = scpsys_sec_infra_power_on(true);
410 if (ret)
411 return ret;
412 }
413
414 ret = clk_bulk_prepare_enable(pd->num_subsys_clks, pd->subsys_clks);
415 if (ret)
416 goto err_infra;
417
418 /* Make sure the HW Voter is idle and able to accept commands */
419 ret = regmap_read_poll_timeout_atomic(scpsys->base, hwv->done, val,
420 val & BIT(hwv->setclr_bit),
421 MTK_HWV_POLL_DELAY_US,
422 MTK_HWV_POLL_TIMEOUT);
423 if (ret)
424 goto err_disable_subsys_clks;
425
426
427 /*
428 * Instruct the HWV to power off the MTCMOS (power domain): differently
429 * from poweron, the bit will be kept set.
430 */
431 regmap_write(scpsys->base, hwv->clr, BIT(hwv->setclr_bit));
432
433 /*
434 * Wait until the HWV clears the bit, signalling that its internal
435 * state machine was started and it now processing the clear command.
436 */
437 ret = regmap_read_poll_timeout_atomic(scpsys->base, hwv->clr, val,
438 !(val & BIT(hwv->setclr_bit)),
439 MTK_HWV_PREPARE_DELAY_US,
440 MTK_HWV_PREPARE_TIMEOUT);
441 if (ret)
442 goto err_disable_subsys_clks;
443
444 /* Poweroff needs 100us for the HW to stabilize */
445 udelay(100);
446
447 /* Wait for ACK, signalling that the MTCMOS was disabled */
448 ret = readx_poll_timeout_atomic(scpsys_hwv_domain_is_disable_done, pd, val, val,
449 MTK_HWV_POLL_DELAY_US, MTK_HWV_POLL_TIMEOUT);
450 if (ret)
451 goto err_disable_subsys_clks;
452
453 clk_bulk_disable_unprepare(pd->num_subsys_clks, pd->subsys_clks);
454 clk_bulk_disable_unprepare(pd->num_clks, pd->clks);
455
456 scpsys_regulator_disable(pd->supply);
457
458 if (MTK_SCPD_CAPS(pd, MTK_SCPD_INFRA_PWR_CTL))
459 scpsys_sec_infra_power_on(false);
460
461 return 0;
462
463 err_disable_subsys_clks:
464 clk_bulk_disable_unprepare(pd->num_subsys_clks, pd->subsys_clks);
465 err_infra:
466 if (MTK_SCPD_CAPS(pd, MTK_SCPD_INFRA_PWR_CTL))
467 scpsys_sec_infra_power_on(false);
468 return ret;
469 };
470
scpsys_hwv_power_off(struct generic_pm_domain * genpd)471 static int scpsys_hwv_power_off(struct generic_pm_domain *genpd)
472 {
473 struct scpsys_domain *pd = container_of(genpd, struct scpsys_domain, genpd);
474
475 return scpsys_hwv_power_off_internal(pd);
476 }
477
scpsys_ctl_pwrseq_on(struct scpsys_domain * pd)478 static int scpsys_ctl_pwrseq_on(struct scpsys_domain *pd)
479 {
480 struct scpsys *scpsys = pd->scpsys;
481 bool do_rtff_nrestore, tmp;
482 int ret;
483
484 /* subsys power on */
485 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_ON_BIT);
486 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_ON_2ND_BIT);
487
488 /* wait until PWR_ACK = 1 */
489 ret = readx_poll_timeout(scpsys_domain_is_on, pd, tmp, tmp, MTK_POLL_DELAY_US,
490 MTK_POLL_TIMEOUT);
491 if (ret < 0)
492 return ret;
493
494 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_CLK_DIS_BIT);
495 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_ISO_BIT);
496 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
497
498 /*
499 * RTFF HW state may be modified by secure world or remote processors.
500 *
501 * With the only exception of STOR_UFS, which always needs save/restore,
502 * check if this power domain's RTFF is already on before trying to do
503 * the NRESTORE procedure, otherwise the system will lock up.
504 */
505 switch (pd->data->rtff_type) {
506 case SCPSYS_RTFF_TYPE_GENERIC:
507 case SCPSYS_RTFF_TYPE_PCIE_PHY:
508 {
509 u32 ctl_status;
510
511 regmap_read(scpsys->base, pd->data->ctl_offs, &ctl_status);
512 do_rtff_nrestore = ctl_status & PWR_RTFF_SAVE_FLAG;
513 break;
514 }
515 case SCPSYS_RTFF_TYPE_STOR_UFS:
516 /* STOR_UFS always needs NRESTORE */
517 do_rtff_nrestore = true;
518 break;
519 default:
520 do_rtff_nrestore = false;
521 break;
522 }
523
524 /* Return early if RTFF NRESTORE shall not be done */
525 if (!do_rtff_nrestore)
526 return 0;
527
528 switch (pd->data->rtff_type) {
529 case SCPSYS_RTFF_TYPE_GENERIC:
530 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE_FLAG);
531 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_CLK_DIS);
532 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_NRESTORE);
533 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_NRESTORE);
534 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_CLK_DIS);
535 break;
536 case SCPSYS_RTFF_TYPE_PCIE_PHY:
537 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE_FLAG);
538 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_NRESTORE);
539 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_NRESTORE);
540 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_CLK_DIS);
541 break;
542 case SCPSYS_RTFF_TYPE_STOR_UFS:
543 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_UFS_CLK_DIS);
544 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_NRESTORE);
545 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_NRESTORE);
546 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_UFS_CLK_DIS);
547 break;
548 default:
549 break;
550 }
551
552 return 0;
553 }
554
scpsys_ctl_pwrseq_off(struct scpsys_domain * pd)555 static int scpsys_ctl_pwrseq_off(struct scpsys_domain *pd)
556 {
557 struct scpsys *scpsys = pd->scpsys;
558 bool tmp;
559 int ret;
560
561 switch (pd->data->rtff_type) {
562 case SCPSYS_RTFF_TYPE_GENERIC:
563 case SCPSYS_RTFF_TYPE_PCIE_PHY:
564 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_CLK_DIS);
565 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE);
566 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE);
567 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_CLK_DIS);
568 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE_FLAG);
569 break;
570 case SCPSYS_RTFF_TYPE_STOR_UFS:
571 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_UFS_CLK_DIS);
572 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE);
573 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_SAVE);
574 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RTFF_UFS_CLK_DIS);
575 break;
576 default:
577 break;
578 }
579
580 /* subsys power off */
581 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_ISO_BIT);
582
583 /* Wait for RTFF HW to sync buck isolation state if this is PCIe PHY RTFF */
584 if (pd->data->rtff_type == SCPSYS_RTFF_TYPE_PCIE_PHY)
585 udelay(1);
586
587 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_CLK_DIS_BIT);
588 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
589 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_ON_2ND_BIT);
590 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_ON_BIT);
591
592 /* wait until PWR_ACK = 0 */
593 ret = readx_poll_timeout(scpsys_domain_is_on, pd, tmp, !tmp, MTK_POLL_DELAY_US,
594 MTK_POLL_TIMEOUT);
595 if (ret < 0)
596 return ret;
597
598 return 0;
599 }
600
scpsys_simple_pwrseq_on(struct scpsys_domain * pd)601 static int scpsys_simple_pwrseq_on(struct scpsys_domain *pd)
602 {
603 struct scpsys *scpsys = pd->scpsys;
604
605 /* Enable subsys clock input and trigger power domain reset state */
606 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_CLK_DIS_BIT);
607 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
608
609 /* Wait for the hardware to stabilize */
610 udelay(1);
611
612 /* Get out of reset: set power on */
613 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
614
615 return 0;
616 }
617
scpsys_simple_pwrseq_off(struct scpsys_domain * pd)618 static int scpsys_simple_pwrseq_off(struct scpsys_domain *pd)
619 {
620 struct scpsys *scpsys = pd->scpsys;
621
622 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
623 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_CLK_DIS_BIT);
624
625 return 0;
626 }
627
scpsys_modem_pwrseq_on(struct scpsys_domain * pd)628 static int scpsys_modem_pwrseq_on(struct scpsys_domain *pd)
629 {
630 struct scpsys *scpsys = pd->scpsys;
631 bool tmp;
632 int ret;
633
634 if (!MTK_SCPD_CAPS(pd, MTK_SCPD_SKIP_RESET_B))
635 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
636
637 regmap_set_bits(scpsys->base, pd->data->ctl_offs, PWR_ON_BIT);
638
639 /* wait until PWR_ACK = 1 */
640 ret = readx_poll_timeout(scpsys_domain_is_on, pd, tmp, tmp, MTK_POLL_DELAY_US,
641 MTK_POLL_TIMEOUT);
642 if (ret < 0)
643 return ret;
644
645 return 0;
646 }
647
scpsys_modem_pwrseq_off(struct scpsys_domain * pd)648 static int scpsys_modem_pwrseq_off(struct scpsys_domain *pd)
649 {
650 struct scpsys *scpsys = pd->scpsys;
651 bool tmp;
652 int ret;
653
654 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_ON_BIT);
655
656 if (!MTK_SCPD_CAPS(pd, MTK_SCPD_SKIP_RESET_B))
657 regmap_clear_bits(scpsys->base, pd->data->ctl_offs, PWR_RST_B_BIT);
658
659 /* wait until PWR_ACK = 0 */
660 ret = readx_poll_timeout(scpsys_domain_is_on, pd, tmp, !tmp, MTK_POLL_DELAY_US,
661 MTK_POLL_TIMEOUT);
662 if (ret < 0)
663 return ret;
664
665 return 0;
666 }
667
scpsys_modem_sec_poll(unsigned long cmd)668 static bool scpsys_modem_sec_poll(unsigned long cmd)
669 {
670 struct arm_smccc_res res;
671
672 arm_smccc_smc(MTK_SIP_KERNEL_CCCI_CONTROL, cmd, 1, 0, 0, 0, 0, 0, &res);
673
674 return res.a0 == 0;
675 }
676
scpsys_modem_sec_power_on(bool on)677 static int scpsys_modem_sec_power_on(bool on)
678 {
679 struct arm_smccc_res res;
680 unsigned long cmd = on ? MTK_MD_MTCMOS_ENABLE : MTK_MD_MTCMOS_DISABLE;
681 bool tmp;
682 int ret;
683
684 arm_smccc_smc(MTK_SIP_KERNEL_CCCI_CONTROL, cmd, 0, 0, 0, 0, 0, 0, &res);
685 if (res.a0 == 0)
686 return 0;
687
688 ret = readx_poll_timeout(scpsys_modem_sec_poll, cmd, tmp, tmp,
689 MTK_POLL_DELAY_US, MTK_POLL_TIMEOUT);
690 if (ret < 0)
691 return ret;
692
693 return 0;
694 }
695
scpsys_power_on(struct generic_pm_domain * genpd)696 static int scpsys_power_on(struct generic_pm_domain *genpd)
697 {
698 struct scpsys_domain *pd = container_of(genpd, struct scpsys_domain, genpd);
699 struct scpsys *scpsys = pd->scpsys;
700 int ret;
701
702 ret = scpsys_regulator_enable(pd->supply);
703 if (ret)
704 return ret;
705
706 ret = clk_bulk_prepare_enable(pd->num_clks, pd->clks);
707 if (ret)
708 goto err_reg;
709
710 if (pd->data->ext_buck_iso_offs && MTK_SCPD_CAPS(pd, MTK_SCPD_EXT_BUCK_ISO))
711 regmap_clear_bits(scpsys->base, pd->data->ext_buck_iso_offs,
712 pd->data->ext_buck_iso_mask);
713
714 if (MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_SECURE_PWRSEQ))
715 ret = scpsys_modem_sec_power_on(true);
716 else if (MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_PWRSEQ))
717 ret = scpsys_modem_pwrseq_on(pd);
718 else if (MTK_SCPD_CAPS(pd, MTK_SCPD_SIMPLE_PWRSEQ))
719 ret = scpsys_simple_pwrseq_on(pd);
720 else
721 ret = scpsys_ctl_pwrseq_on(pd);
722
723 if (ret)
724 goto err_pwr_ack;
725
726 /*
727 * In MT8189 mminfra power domain, the bus protect policy separates
728 * into two parts, one is set before subsys clocks enabled, and another
729 * need to enable after subsys clocks enable.
730 */
731 ret = scpsys_bus_protect_disable(pd, BUS_PROT_IGNORE_SUBCLK);
732 if (ret < 0)
733 goto err_pwr_ack;
734
735 /*
736 * In few Mediatek platforms(e.g. MT6779), the bus protect policy is
737 * stricter, which leads to bus protect release must be prior to bus
738 * access.
739 */
740 if (!MTK_SCPD_CAPS(pd, MTK_SCPD_STRICT_BUS_PROTECTION)) {
741 ret = clk_bulk_prepare_enable(pd->num_subsys_clks,
742 pd->subsys_clks);
743 if (ret)
744 goto err_pwr_ack;
745 }
746
747 if (!MTK_SCPD_CAPS(pd, MTK_SCPD_SIMPLE_PWRSEQ) &&
748 !MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_SECURE_PWRSEQ)) {
749 ret = scpsys_sram_enable(pd);
750 if (ret < 0)
751 goto err_disable_subsys_clks;
752 }
753
754 ret = scpsys_bus_protect_disable(pd, 0);
755 if (ret < 0)
756 goto err_disable_sram;
757
758 if (MTK_SCPD_CAPS(pd, MTK_SCPD_STRICT_BUS_PROTECTION)) {
759 ret = clk_bulk_prepare_enable(pd->num_subsys_clks,
760 pd->subsys_clks);
761 if (ret)
762 goto err_enable_bus_protect;
763 }
764
765 return 0;
766
767 err_enable_bus_protect:
768 scpsys_bus_protect_enable(pd, 0);
769 err_disable_sram:
770 if (!MTK_SCPD_CAPS(pd, MTK_SCPD_SIMPLE_PWRSEQ) &&
771 !MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_SECURE_PWRSEQ))
772 scpsys_sram_disable(pd);
773 err_disable_subsys_clks:
774 if (!MTK_SCPD_CAPS(pd, MTK_SCPD_STRICT_BUS_PROTECTION))
775 clk_bulk_disable_unprepare(pd->num_subsys_clks,
776 pd->subsys_clks);
777 err_pwr_ack:
778 clk_bulk_disable_unprepare(pd->num_clks, pd->clks);
779 err_reg:
780 scpsys_regulator_disable(pd->supply);
781 return ret;
782 }
783
scpsys_power_off_internal(struct scpsys_domain * pd)784 static int scpsys_power_off_internal(struct scpsys_domain *pd)
785 {
786 struct scpsys *scpsys = pd->scpsys;
787 int ret;
788
789 ret = scpsys_bus_protect_enable(pd, 0);
790 if (ret < 0)
791 return ret;
792
793 if (MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_SECURE_PWRSEQ)) {
794 ret = scpsys_modem_sec_power_on(false);
795 if (ret)
796 return ret;
797 } else if (!MTK_SCPD_CAPS(pd, MTK_SCPD_SIMPLE_PWRSEQ)) {
798 ret = scpsys_sram_disable(pd);
799 if (ret < 0)
800 return ret;
801 }
802
803 if (pd->data->ext_buck_iso_offs && MTK_SCPD_CAPS(pd, MTK_SCPD_EXT_BUCK_ISO))
804 regmap_set_bits(scpsys->base, pd->data->ext_buck_iso_offs,
805 pd->data->ext_buck_iso_mask);
806
807 clk_bulk_disable_unprepare(pd->num_subsys_clks, pd->subsys_clks);
808
809 ret = scpsys_bus_protect_enable(pd, BUS_PROT_IGNORE_SUBCLK);
810 if (ret < 0)
811 return ret;
812
813 if (MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_PWRSEQ))
814 ret = scpsys_modem_pwrseq_off(pd);
815 else if (MTK_SCPD_CAPS(pd, MTK_SCPD_SIMPLE_PWRSEQ))
816 ret = scpsys_simple_pwrseq_off(pd);
817 else if (!MTK_SCPD_CAPS(pd, MTK_SCPD_MODEM_SECURE_PWRSEQ))
818 ret = scpsys_ctl_pwrseq_off(pd);
819
820 if (ret < 0) {
821 /* Re-enable clocks so that next power off doesn't break the refcount */
822 int r = clk_bulk_prepare_enable(pd->num_subsys_clks, pd->subsys_clks);
823
824 if (r)
825 dev_warn(scpsys->dev, "Could not re-enable clocks: %d\n", r);
826
827 return ret;
828 }
829
830 clk_bulk_disable_unprepare(pd->num_clks, pd->clks);
831
832 scpsys_regulator_disable(pd->supply);
833
834 return 0;
835 }
836
scpsys_power_off(struct generic_pm_domain * genpd)837 static int scpsys_power_off(struct generic_pm_domain *genpd)
838 {
839 struct scpsys_domain *pd = container_of(genpd, struct scpsys_domain, genpd);
840
841 return scpsys_power_off_internal(pd);
842 }
843
844 static struct
scpsys_add_one_domain(struct scpsys * scpsys,struct device_node * node,u8 * domains_idx,u8 * num_domains)845 generic_pm_domain *scpsys_add_one_domain(struct scpsys *scpsys, struct device_node *node,
846 u8 *domains_idx, u8 *num_domains)
847 {
848 const struct scpsys_domain_data *domain_data;
849 const struct scpsys_hwv_domain_data *hwv_domain_data;
850 struct scpsys_domain *pd;
851 struct property *prop;
852 const char *clk_name;
853 int i, ret, num_clks;
854 struct clk *clk;
855 int clk_ind = 0;
856 u32 id;
857
858 ret = of_property_read_u32(node, "reg", &id);
859 if (ret) {
860 dev_err(scpsys->dev, "%pOF: failed to retrieve domain id from reg: %d\n",
861 node, ret);
862 return ERR_PTR(-EINVAL);
863 }
864
865 switch (scpsys->soc_data->type) {
866 case SCPSYS_MTCMOS_TYPE_DIRECT_CTL:
867 if (id >= scpsys->soc_data->num_domains) {
868 dev_err(scpsys->dev, "%pOF: invalid domain id %d\n", node, id);
869 return ERR_PTR(-EINVAL);
870 }
871
872 domain_data = &scpsys->soc_data->domains_data[id];
873 hwv_domain_data = NULL;
874
875 if (domain_data->sta_mask == 0) {
876 dev_err(scpsys->dev, "%pOF: undefined domain id %d\n", node, id);
877 return ERR_PTR(-EINVAL);
878 }
879
880 break;
881 case SCPSYS_MTCMOS_TYPE_HW_VOTER:
882 if (id >= scpsys->soc_data->num_hwv_domains) {
883 dev_err(scpsys->dev, "%pOF: invalid HWV domain id %d\n", node, id);
884 return ERR_PTR(-EINVAL);
885 }
886
887 domain_data = NULL;
888 hwv_domain_data = &scpsys->soc_data->hwv_domains_data[id];
889
890 break;
891 default:
892 return ERR_PTR(-EINVAL);
893 }
894
895 pd = devm_kzalloc(scpsys->dev, sizeof(*pd), GFP_KERNEL);
896 if (!pd)
897 return ERR_PTR(-ENOMEM);
898
899 pd->data = domain_data;
900 pd->hwv_data = hwv_domain_data;
901 pd->scpsys = scpsys;
902
903 if (MTK_SCPD_CAPS(pd, MTK_SCPD_DOMAIN_SUPPLY)) {
904 pd->supply = devm_of_regulator_get_optional(scpsys->dev, node, "domain");
905 if (IS_ERR(pd->supply))
906 return dev_err_cast_probe(scpsys->dev, pd->supply,
907 "%pOF: failed to get power supply.\n",
908 node);
909 }
910
911 num_clks = of_clk_get_parent_count(node);
912 if (num_clks > 0) {
913 /* Calculate number of subsys_clks */
914 of_property_for_each_string(node, "clock-names", prop, clk_name) {
915 char *subsys;
916
917 subsys = strchr(clk_name, '-');
918 if (subsys)
919 pd->num_subsys_clks++;
920 else
921 pd->num_clks++;
922 }
923
924 pd->clks = devm_kcalloc(scpsys->dev, pd->num_clks, sizeof(*pd->clks), GFP_KERNEL);
925 if (!pd->clks)
926 return ERR_PTR(-ENOMEM);
927
928 pd->subsys_clks = devm_kcalloc(scpsys->dev, pd->num_subsys_clks,
929 sizeof(*pd->subsys_clks), GFP_KERNEL);
930 if (!pd->subsys_clks)
931 return ERR_PTR(-ENOMEM);
932
933 }
934
935 for (i = 0; i < pd->num_clks; i++) {
936 clk = of_clk_get(node, i);
937 if (IS_ERR(clk)) {
938 ret = PTR_ERR(clk);
939 dev_err_probe(scpsys->dev, ret,
940 "%pOF: failed to get clk at index %d\n", node, i);
941 goto err_put_clocks;
942 }
943
944 pd->clks[clk_ind++].clk = clk;
945 }
946
947 for (i = 0; i < pd->num_subsys_clks; i++) {
948 clk = of_clk_get(node, i + clk_ind);
949 if (IS_ERR(clk)) {
950 ret = PTR_ERR(clk);
951 dev_err_probe(scpsys->dev, ret,
952 "%pOF: failed to get clk at index %d\n", node,
953 i + clk_ind);
954 goto err_put_subsys_clocks;
955 }
956
957 pd->subsys_clks[i].clk = clk;
958 }
959
960 if (scpsys->domains[id]) {
961 ret = -EINVAL;
962 dev_err(scpsys->dev,
963 "power domain with id %d already exists, check your device-tree\n", id);
964 goto err_put_subsys_clocks;
965 }
966
967 if (pd->data && pd->data->name)
968 pd->genpd.name = pd->data->name;
969 else if (pd->hwv_data && pd->hwv_data->name)
970 pd->genpd.name = pd->hwv_data->name;
971 else
972 pd->genpd.name = node->name;
973
974 if (scpsys->soc_data->type == SCPSYS_MTCMOS_TYPE_DIRECT_CTL) {
975 pd->genpd.power_off = scpsys_power_off;
976 pd->genpd.power_on = scpsys_power_on;
977 } else {
978 pd->genpd.power_off = scpsys_hwv_power_off;
979 pd->genpd.power_on = scpsys_hwv_power_on;
980
981 /* HW-Voter code can be invoked in atomic context */
982 pd->genpd.flags |= GENPD_FLAG_IRQ_SAFE;
983 }
984
985 /*
986 * Initially turn on all domains to make the domains usable
987 * with !CONFIG_PM and to get the hardware in sync with the
988 * software. The unused domains will be switched off during
989 * late_init time.
990 */
991 if (MTK_SCPD_CAPS(pd, MTK_SCPD_KEEP_DEFAULT_OFF)) {
992 bool domain_is_on;
993
994 if (scpsys->soc_data->type == SCPSYS_MTCMOS_TYPE_HW_VOTER)
995 domain_is_on = scpsys_hwv_domain_is_enable_done(pd);
996 else
997 domain_is_on = scpsys_domain_is_on(pd);
998
999 if (domain_is_on)
1000 dev_warn(scpsys->dev,
1001 "%pOF: A default off power domain has been ON\n", node);
1002 } else {
1003 ret = pd->genpd.power_on(&pd->genpd);
1004 if (ret < 0) {
1005 dev_err(scpsys->dev, "%pOF: failed to power on domain: %d\n", node, ret);
1006 goto err_put_subsys_clocks;
1007 }
1008
1009 if (MTK_SCPD_CAPS(pd, MTK_SCPD_ALWAYS_ON))
1010 pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
1011 }
1012
1013 if (MTK_SCPD_CAPS(pd, MTK_SCPD_ACTIVE_WAKEUP))
1014 pd->genpd.flags |= GENPD_FLAG_ACTIVE_WAKEUP;
1015
1016 if (MTK_SCPD_CAPS(pd, MTK_SCPD_KEEP_DEFAULT_OFF))
1017 pm_genpd_init(&pd->genpd, NULL, true);
1018 else
1019 pm_genpd_init(&pd->genpd, NULL, false);
1020
1021 domains_idx[(*num_domains)++] = (u8) id;
1022 scpsys->domains[id] = &pd->genpd;
1023
1024 return scpsys->pd_data.domains[id];
1025
1026 err_put_subsys_clocks:
1027 clk_bulk_put(pd->num_subsys_clks, pd->subsys_clks);
1028 err_put_clocks:
1029 clk_bulk_put(pd->num_clks, pd->clks);
1030 return ERR_PTR(ret);
1031 }
1032
scpsys_add_subdomain(struct scpsys * scpsys,struct device_node * parent,u8 * domains_idx,u8 * num_domains)1033 static int scpsys_add_subdomain(struct scpsys *scpsys, struct device_node *parent,
1034 u8 *domains_idx, u8 *num_domains)
1035 {
1036 struct generic_pm_domain *child_pd, *parent_pd;
1037 struct device_node *child;
1038 int ret;
1039
1040 for_each_child_of_node(parent, child) {
1041 u32 id;
1042
1043 ret = of_property_read_u32(parent, "reg", &id);
1044 if (ret) {
1045 dev_err(scpsys->dev, "%pOF: failed to get parent domain id\n", child);
1046 goto err_put_node;
1047 }
1048
1049 if (!scpsys->pd_data.domains[id]) {
1050 ret = -EINVAL;
1051 dev_err(scpsys->dev, "power domain with id %d does not exist\n", id);
1052 goto err_put_node;
1053 }
1054
1055 parent_pd = scpsys->pd_data.domains[id];
1056
1057 child_pd = scpsys_add_one_domain(scpsys, child, domains_idx, num_domains);
1058 if (IS_ERR(child_pd)) {
1059 ret = PTR_ERR(child_pd);
1060 dev_err_probe(scpsys->dev, ret, "%pOF: failed to get child domain id\n",
1061 child);
1062 goto err_put_node;
1063 }
1064
1065 /* recursive call to add all subdomains */
1066 ret = scpsys_add_subdomain(scpsys, child, domains_idx, num_domains);
1067 if (ret)
1068 goto err_put_node;
1069
1070 ret = pm_genpd_add_subdomain(parent_pd, child_pd);
1071 if (ret) {
1072 dev_err(scpsys->dev, "failed to add %s subdomain to parent %s\n",
1073 child_pd->name, parent_pd->name);
1074 goto err_put_node;
1075 } else {
1076 dev_dbg(scpsys->dev, "%s add subdomain: %s\n", parent_pd->name,
1077 child_pd->name);
1078 }
1079 }
1080
1081 return 0;
1082
1083 err_put_node:
1084 of_node_put(child);
1085 return ret;
1086 }
1087
scpsys_remove_one_domain(struct scpsys_domain * pd)1088 static void scpsys_remove_one_domain(struct scpsys_domain *pd)
1089 {
1090 struct scpsys *scpsys = pd->scpsys;
1091 int ret;
1092
1093 /*
1094 * We're in the error cleanup already, so we only complain,
1095 * but won't emit another error on top of the original one.
1096 */
1097 ret = pm_genpd_remove(&pd->genpd);
1098 if (ret < 0)
1099 dev_err(pd->scpsys->dev,
1100 "failed to remove domain '%s' : %d - state may be inconsistent\n",
1101 pd->genpd.name, ret);
1102
1103 if (scpsys->soc_data->type == SCPSYS_MTCMOS_TYPE_HW_VOTER) {
1104 if (scpsys_hwv_domain_is_enable_done(pd))
1105 scpsys_hwv_power_off_internal(pd);
1106 } else {
1107 if (scpsys_domain_is_on(pd) || MTK_SCPD_CAPS(pd, MTK_SCPD_SIMPLE_PWRSEQ))
1108 scpsys_power_off_internal(pd);
1109 }
1110
1111 clk_bulk_put(pd->num_clks, pd->clks);
1112 clk_bulk_put(pd->num_subsys_clks, pd->subsys_clks);
1113 }
1114
scpsys_domain_cleanup(struct scpsys * scpsys,u8 * domains_idx,u8 num_probed)1115 static void scpsys_domain_cleanup(struct scpsys *scpsys, u8 *domains_idx, u8 num_probed)
1116 {
1117 struct generic_pm_domain *genpd;
1118 struct scpsys_domain *pd;
1119 int i;
1120
1121 for (i = num_probed - 1; i >= 0; i--) {
1122 u8 pd_idx = domains_idx[i];
1123
1124 genpd = scpsys->pd_data.domains[pd_idx];
1125 if (genpd) {
1126 pd = to_scpsys_domain(genpd);
1127 scpsys_remove_one_domain(pd);
1128 }
1129 }
1130 }
1131
scpsys_get_bus_protection_legacy(struct device * dev,struct scpsys * scpsys)1132 static int scpsys_get_bus_protection_legacy(struct device *dev, struct scpsys *scpsys)
1133 {
1134 const u8 bp_blocks[3] = {
1135 BUS_PROT_BLOCK_INFRA, BUS_PROT_BLOCK_SMI, BUS_PROT_BLOCK_INFRA_NAO
1136 };
1137 struct device_node *np = dev->of_node;
1138 struct device_node *node, *smi_np;
1139 int num_regmaps = 0, i, j;
1140 struct regmap *regmap[3];
1141 int ret = 0;
1142
1143 /*
1144 * Legacy code retrieves a maximum of three bus protection handles:
1145 * some may be optional, or may not be, so the array of bp blocks
1146 * that is normally passed in as platform data must be dynamically
1147 * built in this case.
1148 *
1149 * Here, try to retrieve all of the regmaps that the legacy code
1150 * supported and then count the number of the ones that are present,
1151 * this makes it then possible to allocate the array of bus_prot
1152 * regmaps and convert all to the new style handling.
1153 */
1154 of_node_get(np);
1155 node = of_find_node_with_property(np, "mediatek,infracfg");
1156 if (node) {
1157 regmap[0] = syscon_regmap_lookup_by_phandle(node, "mediatek,infracfg");
1158 num_regmaps++;
1159 if (IS_ERR(regmap[0])) {
1160 ret = dev_err_probe(dev, PTR_ERR(regmap[0]),
1161 "%pOF: failed to get infracfg regmap\n",
1162 node);
1163 of_node_put(node);
1164 return ret;
1165 }
1166 of_node_put(node);
1167 } else {
1168 regmap[0] = NULL;
1169 }
1170
1171 of_node_get(np);
1172 node = of_find_node_with_property(np, "mediatek,smi");
1173 if (node) {
1174 smi_np = of_parse_phandle(node, "mediatek,smi", 0);
1175 if (!smi_np) {
1176 of_node_put(node);
1177 return -ENODEV;
1178 }
1179
1180 regmap[1] = device_node_to_regmap(smi_np);
1181 num_regmaps++;
1182 of_node_put(smi_np);
1183 if (IS_ERR(regmap[1])) {
1184 ret = dev_err_probe(dev, PTR_ERR(regmap[1]),
1185 "%pOF: failed to get SMI regmap\n",
1186 node);
1187 of_node_put(node);
1188 return ret;
1189 }
1190 of_node_put(node);
1191 } else {
1192 regmap[1] = NULL;
1193 }
1194
1195 of_node_get(np);
1196 node = of_find_node_with_property(np, "mediatek,infracfg-nao");
1197 if (node) {
1198 regmap[2] = syscon_regmap_lookup_by_phandle(node, "mediatek,infracfg-nao");
1199 num_regmaps++;
1200 if (IS_ERR(regmap[2])) {
1201 ret = dev_err_probe(dev, PTR_ERR(regmap[2]),
1202 "%pOF: failed to get infracfg regmap\n",
1203 node);
1204 of_node_put(node);
1205 return ret;
1206 }
1207 of_node_put(node);
1208 } else {
1209 regmap[2] = NULL;
1210 }
1211
1212 /* If no access controllers are needed, don't allocate and don't fail */
1213 if (num_regmaps == 0) {
1214 scpsys->bus_prot = NULL;
1215 return 0;
1216 }
1217
1218 scpsys->bus_prot = devm_kmalloc_array(dev, num_regmaps,
1219 sizeof(*scpsys->bus_prot), GFP_KERNEL);
1220 if (!scpsys->bus_prot)
1221 return -ENOMEM;
1222
1223 for (i = 0, j = 0; i < ARRAY_SIZE(bp_blocks); i++) {
1224 enum scpsys_bus_prot_block bp_type;
1225
1226 if (!regmap[i])
1227 continue;
1228
1229 bp_type = bp_blocks[i];
1230 scpsys->bus_prot_index[bp_type] = j;
1231 scpsys->bus_prot[j] = regmap[i];
1232
1233 j++;
1234 }
1235
1236 return 0;
1237 }
1238
scpsys_get_bus_protection(struct device * dev,struct scpsys * scpsys)1239 static int scpsys_get_bus_protection(struct device *dev, struct scpsys *scpsys)
1240 {
1241 const struct scpsys_soc_data *soc = scpsys->soc_data;
1242 struct device_node *np = dev->of_node;
1243 int i, num_handles;
1244
1245 num_handles = of_count_phandle_with_args(np, "access-controllers", NULL);
1246 if (num_handles < 0 || num_handles != soc->num_bus_prot_blocks)
1247 return dev_err_probe(dev, -EINVAL,
1248 "Cannot get access controllers: expected %u, got %d\n",
1249 soc->num_bus_prot_blocks, num_handles);
1250
1251 scpsys->bus_prot = devm_kmalloc_array(dev, soc->num_bus_prot_blocks,
1252 sizeof(*scpsys->bus_prot), GFP_KERNEL);
1253 if (!scpsys->bus_prot)
1254 return -ENOMEM;
1255
1256 for (i = 0; i < soc->num_bus_prot_blocks; i++) {
1257 enum scpsys_bus_prot_block bp_type;
1258 struct device_node *node;
1259
1260 node = of_parse_phandle(np, "access-controllers", i);
1261 if (!node)
1262 return -EINVAL;
1263
1264 /*
1265 * Index the bus protection regmaps so that we don't have to
1266 * find the right one by type with a loop at every execution
1267 * of power sequence(s).
1268 */
1269 bp_type = soc->bus_prot_blocks[i];
1270 scpsys->bus_prot_index[bp_type] = i;
1271
1272 scpsys->bus_prot[i] = device_node_to_regmap(node);
1273 of_node_put(node);
1274 if (IS_ERR_OR_NULL(scpsys->bus_prot[i]))
1275 return dev_err_probe(dev, scpsys->bus_prot[i] ?
1276 PTR_ERR(scpsys->bus_prot[i]) : -ENXIO,
1277 "Cannot get regmap for access controller %d\n", i);
1278 }
1279
1280 return 0;
1281 }
1282
1283 static const struct of_device_id scpsys_of_match[] = {
1284 {
1285 .compatible = "mediatek,mt6735-power-controller",
1286 .data = &mt6735_scpsys_data,
1287 },
1288 {
1289 .compatible = "mediatek,mt6795-power-controller",
1290 .data = &mt6795_scpsys_data,
1291 },
1292 {
1293 .compatible = "mediatek,mt6858-power-controller",
1294 .data = &mt6858_scpsys_data,
1295 },
1296 {
1297 .compatible = "mediatek,mt6893-power-controller",
1298 .data = &mt6893_scpsys_data,
1299 },
1300 {
1301 .compatible = "mediatek,mt8167-power-controller",
1302 .data = &mt8167_scpsys_data,
1303 },
1304 {
1305 .compatible = "mediatek,mt8173-power-controller",
1306 .data = &mt8173_scpsys_data,
1307 },
1308 {
1309 .compatible = "mediatek,mt8183-power-controller",
1310 .data = &mt8183_scpsys_data,
1311 },
1312 {
1313 .compatible = "mediatek,mt8186-power-controller",
1314 .data = &mt8186_scpsys_data,
1315 },
1316 {
1317 .compatible = "mediatek,mt8188-power-controller",
1318 .data = &mt8188_scpsys_data,
1319 },
1320 {
1321 .compatible = "mediatek,mt8189-power-controller",
1322 .data = &mt8189_scpsys_data,
1323 },
1324 {
1325 .compatible = "mediatek,mt8192-power-controller",
1326 .data = &mt8192_scpsys_data,
1327 },
1328 {
1329 .compatible = "mediatek,mt8195-power-controller",
1330 .data = &mt8195_scpsys_data,
1331 },
1332 {
1333 .compatible = "mediatek,mt8196-power-controller",
1334 .data = &mt8196_scpsys_data,
1335 },
1336 {
1337 .compatible = "mediatek,mt8196-hfrp-power-controller",
1338 .data = &mt8196_hfrpsys_data,
1339 },
1340 {
1341 .compatible = "mediatek,mt8196-hwv-hfrp-power-controller",
1342 .data = &mt8196_hfrpsys_hwv_data,
1343 },
1344 {
1345 .compatible = "mediatek,mt8196-hwv-scp-power-controller",
1346 .data = &mt8196_scpsys_hwv_data,
1347 },
1348 {
1349 .compatible = "mediatek,mt8365-power-controller",
1350 .data = &mt8365_scpsys_data,
1351 },
1352 { }
1353 };
1354
scpsys_probe(struct platform_device * pdev)1355 static int scpsys_probe(struct platform_device *pdev)
1356 {
1357 struct device *dev = &pdev->dev;
1358 struct device_node *np = dev->of_node;
1359 const struct scpsys_soc_data *soc;
1360 struct device *parent;
1361 struct scpsys *scpsys;
1362 int num_domains, ret;
1363 u8 num_added_pds = 0;
1364 u8 *added_pds_idx;
1365
1366 soc = of_device_get_match_data(&pdev->dev);
1367 if (!soc) {
1368 dev_err(&pdev->dev, "no power controller data\n");
1369 return -EINVAL;
1370 }
1371
1372 num_domains = soc->num_domains + soc->num_hwv_domains;
1373
1374 scpsys = devm_kzalloc(dev, struct_size(scpsys, domains, num_domains), GFP_KERNEL);
1375 if (!scpsys)
1376 return -ENOMEM;
1377
1378 /*
1379 * Temporarily store the IDs of the power domains that are added as in
1380 * case of a probe deferral this can be used to correctly cleanup all
1381 * of what was added before.
1382 *
1383 * Note that this array is used only in the probe function and must be
1384 * freed at the end, regardless of whether all of the power domains were
1385 * probed successfully or any failure happened.
1386 */
1387 added_pds_idx = devm_kmalloc_array(dev, num_domains, sizeof(*added_pds_idx), GFP_KERNEL);
1388 if (!added_pds_idx)
1389 return -ENOMEM;
1390
1391 scpsys->dev = dev;
1392 scpsys->soc_data = soc;
1393
1394 scpsys->pd_data.domains = scpsys->domains;
1395 scpsys->pd_data.num_domains = num_domains;
1396
1397 parent = dev->parent;
1398 if (!parent) {
1399 dev_err(dev, "no parent for syscon devices\n");
1400 return -ENODEV;
1401 }
1402
1403 scpsys->base = syscon_node_to_regmap(parent->of_node);
1404 if (IS_ERR(scpsys->base)) {
1405 dev_err(dev, "no regmap available\n");
1406 return PTR_ERR(scpsys->base);
1407 }
1408
1409 if (of_find_property(np, "access-controllers", NULL))
1410 ret = scpsys_get_bus_protection(dev, scpsys);
1411 else
1412 ret = scpsys_get_bus_protection_legacy(dev, scpsys);
1413
1414 if (ret)
1415 return ret;
1416
1417 ret = -ENODEV;
1418 for_each_available_child_of_node_scoped(np, node) {
1419 struct generic_pm_domain *domain;
1420
1421 domain = scpsys_add_one_domain(scpsys, node,
1422 added_pds_idx, &num_added_pds);
1423 if (IS_ERR(domain)) {
1424 ret = PTR_ERR(domain);
1425 goto err_cleanup_domains;
1426 }
1427
1428 ret = scpsys_add_subdomain(scpsys, node,
1429 added_pds_idx, &num_added_pds);
1430 if (ret)
1431 goto err_cleanup_domains;
1432 }
1433
1434 if (ret) {
1435 dev_dbg(dev, "no power domains present\n");
1436 return ret;
1437 }
1438
1439 ret = of_genpd_add_provider_onecell(np, &scpsys->pd_data);
1440 if (ret) {
1441 dev_err(dev, "failed to add provider: %d\n", ret);
1442 goto err_cleanup_domains;
1443 }
1444
1445 devm_kfree(dev, added_pds_idx);
1446 return 0;
1447
1448 err_cleanup_domains:
1449 scpsys_domain_cleanup(scpsys, added_pds_idx, num_added_pds);
1450 return ret;
1451 }
1452
1453 static struct platform_driver scpsys_pm_domain_driver = {
1454 .probe = scpsys_probe,
1455 .driver = {
1456 .name = "mtk-power-controller",
1457 .suppress_bind_attrs = true,
1458 .of_match_table = scpsys_of_match,
1459 },
1460 };
1461 builtin_platform_driver(scpsys_pm_domain_driver);
1462