blob: 1dc05a4a7e8de9cf44619ecf8ac6bf237d7aca7e [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (c) 2018 MediaTek
4 * Author: Yong Wu <yong.wu@mediatek.com>
5 *
6 * Based on drivers/soc/mediatek/mtk-scpsys.c
7 *
8 * All the mtcmos enable/disable code is from DE. More closer to DE.
9 */
10#include <linux/io.h>
11#include <linux/iopoll.h>
12#include <linux/mfd/syscon.h>
13#include <linux/of_device.h>
14#include <linux/platform_device.h>
15#include <linux/pm_domain.h>
16
17#include <dt-bindings/power/mt2731-power.h>
18
19static void __iomem *spm_base; /* spm */
20static void __iomem *infra_base; /* infra_ao */
21
22#define SPM_BASE spm_base
23#define INFRACFG_BASE infra_base
24
25#define spm_read(addr) readl(addr)
26#define spm_write(addr, val) writel((val), (addr))
27
28typedef int (*mtcmos_ops)(struct device *dev, int state);
29
30/* SPM Register. should be auto-gened. */
31#define POWERON_CONFIG_EN (SPM_BASE + 0x0000)
32#define PWR_STATUS (SPM_BASE + 0x0160)
33#define PWR_STATUS_2ND (SPM_BASE + 0x0164)
34#define MD1_PWR_CON (SPM_BASE + 0x0318)
35#define MD1_SRAM_ISOINT_B (SPM_BASE + 0x0320)
36#define MD_EXT_BUCK_ISO_CON (SPM_BASE + 0x03B0)
37#define MD_EXTRA_PWR_CON (SPM_BASE + 0x03F0)
38#define SPM_PROJECT_CODE 0xb16
39#define SPM_REGWR_CFG_KEY (SPM_PROJECT_CODE << 16)
40
41
42/* INFRASYS Register */
43#define INFRA_TOPAXI_PROTECTEN (INFRACFG_BASE + 0x0220)
44#define INFRA_TOPAXI_PROTECTEN_STA1 (INFRACFG_BASE + 0x0228)
45#define INFRA_TOPAXI_PROTECTEN_1 (INFRACFG_BASE + 0x0250)
46#define INFRA_TOPAXI_PROTECTEN_STA1_1 (INFRACFG_BASE + 0x0258)
47#define INFRA_TOPAXI_PROTECTEN_SET (INFRACFG_BASE + 0x02A0)
48#define INFRA_TOPAXI_PROTECTEN_CLR (INFRACFG_BASE + 0x02A4)
49#define INFRA_TOPAXI_PROTECTEN_1_SET (INFRACFG_BASE + 0x02A8)
50#define INFRA_TOPAXI_PROTECTEN_1_CLR (INFRACFG_BASE + 0x02AC)
51#define VDNR_CON (INFRACFG_BASE + 0x71C)
52
53#define STA_POWER_DOWN 0
54#define STA_POWER_ON 1
55
56#define SRAM_ISOINT_B (1U << 6)
57#define SRAM_CKISO (1U << 5)
58#define PWR_CLK_DIS (1U << 4)
59#define PWR_ON_2ND (1U << 3)
60#define PWR_ON (1U << 2)
61#define PWR_ISO (1U << 1)
62#define PWR_RST_B (1U << 0)
63
64/* Kernel readl_poll wait-ack support timeout. */
65#define READL_POLL_SUPPORT 1
66
67/**************************************
68 * for NON-CPU MTCMOS
69 **************************************/
70/* Define MTCMOS Power Status Mask */
71#define MD1_PWR_STA_MASK (0x1 << 0)
72
73/* Define MTCMOS Bus Protect Mask */
74#define MD1_PROT_STEP1_0_MASK ((0x1 << 6) \
75 |(0x1 << 7))
76#define MD1_PROT_STEP1_0_ACK_MASK ((0x1 << 6) \
77 |(0x1 << 7))
78#define MD1_PROT_STEP2_0_MASK ((0x1 << 3) \
79 |(0x1 << 4))
80#define MD1_PROT_STEP2_0_ACK_MASK ((0x1 << 3) \
81 |(0x1 << 4))
82static int spm_mtcmos_ctrl_md1(struct device *dev, int state)
83{
84 int err = 0, tmp, ret;
85
86 /* TINFO="enable SPM register control" */
87 spm_write(POWERON_CONFIG_EN, (SPM_PROJECT_CODE << 16) | (0x1 << 0));
88
89 if (state == STA_POWER_DOWN) {
90 /* TINFO="Start to turn off MD1" */
91 /* TINFO="Switch to original protect control path" */
92 spm_write(VDNR_CON, spm_read(VDNR_CON) | 0x7F);
93 /* TINFO="Set bus protect - step1 : 0" */
94 spm_write(INFRA_TOPAXI_PROTECTEN_SET, MD1_PROT_STEP1_0_MASK);
95#ifndef IGNORE_MTCMOS_CHECK
96 #if READL_POLL_SUPPORT
97 ret = readl_poll_timeout_atomic(
98 INFRA_TOPAXI_PROTECTEN_STA1, tmp,
99 (tmp & MD1_PROT_STEP1_0_ACK_MASK) ==
100 MD1_PROT_STEP1_0_ACK_MASK /* out */,
101 10, 100000);
102 if (ret) {
103 dev_warn(dev, "MD1 step1_0 ack fail\n");
104 return ret;
105 }
106 #else
107 while ((spm_read(INFRA_TOPAXI_PROTECTEN_STA1) &
108 MD1_PROT_STEP1_0_ACK_MASK) !=
109 MD1_PROT_STEP1_0_ACK_MASK) {
110 }
111 #endif
112#endif
113 /* TINFO="Set bus protect - step2 : 0" */
114 spm_write(INFRA_TOPAXI_PROTECTEN_SET, MD1_PROT_STEP2_0_MASK);
115#ifndef IGNORE_MTCMOS_CHECK
116 #if READL_POLL_SUPPORT
117 ret = readl_poll_timeout_atomic(
118 INFRA_TOPAXI_PROTECTEN_STA1, tmp,
119 (tmp & MD1_PROT_STEP2_0_ACK_MASK) ==
120 MD1_PROT_STEP2_0_ACK_MASK,
121 10, 100000);
122 if (ret) {
123 dev_warn(dev, "MD1 step2_0 ack fail\n");
124 return ret;
125 }
126 #else
127 while ((spm_read(INFRA_TOPAXI_PROTECTEN_STA1) &
128 MD1_PROT_STEP2_0_ACK_MASK) !=
129 MD1_PROT_STEP2_0_ACK_MASK) {
130 }
131 #endif
132#endif
133 /* TINFO="MD_EXTRA_PWR_CON[0]=1"*/
134 spm_write(MD_EXTRA_PWR_CON,
135 spm_read(MD_EXTRA_PWR_CON) | (0x1 << 0));
136 /* TINFO="Set PWR_CLK_DIS = 1" */
137 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_CLK_DIS);
138 /* TINFO="Set PWR_ISO = 1" */
139 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_ISO);
140 /* TINFO="MD_SRAM_ISO_CON[0]=0"*/
141 spm_write(MD1_SRAM_ISOINT_B,
142 spm_read(MD1_SRAM_ISOINT_B) & ~(0x1 << 0));
143 /* TINFO="Set SRAM_PDN = 1" */
144 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | (0x1 << 8));
145#ifndef IGNORE_MTCMOS_CHECK
146#endif
147 /* TINFO="Set PWR_ON = 0" */
148 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_ON);
149 /* TINFO="Set PWR_ON_2ND = 0" */
150 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_ON_2ND);
151#ifndef IGNORE_MTCMOS_CHECK
152 #if READL_POLL_SUPPORT
153 ret = readl_poll_timeout_atomic(
154 PWR_STATUS, tmp,
155 !(tmp & MD1_PWR_STA_MASK),
156 10, 100000);
157 if (ret) {
158 dev_warn(dev, "PWR STATUS MASK fail\n");
159 return ret;
160 }
161 #else
162 /* TINFO="Wait until MD1_PWR_STA_MASK = 0" */
163 while (spm_read(PWR_STATUS) & MD1_PWR_STA_MASK) {
164 /* No logic between pwr_on and pwr_ack.
165 * Print SRAM / MTCMOS control and PWR_ACK for debug.
166 */
167 }
168 #endif
169#endif
170 /* TINFO="MD_EXT_BUCK_ISO_CON[0]=1"*/
171 spm_write(MD_EXT_BUCK_ISO_CON,
172 spm_read(MD_EXT_BUCK_ISO_CON) | (0x1 << 0));
173 /* TINFO="MD_EXT_BUCK_ISO_CON[1]=1"*/
174 spm_write(MD_EXT_BUCK_ISO_CON,
175 spm_read(MD_EXT_BUCK_ISO_CON) | (0x1 << 1));
176 /* TINFO="MD_EXT_BUCK_ISO_CON[2]=1"*/
177 spm_write(MD_EXT_BUCK_ISO_CON,
178 spm_read(MD_EXT_BUCK_ISO_CON) | (0x1 << 2));
179 /* TINFO="MD_EXT_BUCK_ISO_CON[3]=1"*/
180 spm_write(MD_EXT_BUCK_ISO_CON,
181 spm_read(MD_EXT_BUCK_ISO_CON) | (0x1 << 3));
182 /* TINFO="Set PWR_RST_B = 0" */
183 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_RST_B);
184 /* TINFO="Finish to turn off MD1" */
185 } else { /* STA_POWER_ON */
186 /* TINFO="Start to turn on MD1" */
187 /* TINFO="Set PWR_RST_B = 0" */
188 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_RST_B);
189 /* TINFO="Set PWR_ON = 1" */
190 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_ON);
191 /* TINFO="Set PWR_ON_2ND = 1" */
192 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_ON_2ND);
193#ifndef IGNORE_MTCMOS_CHECK
194 #if READL_POLL_SUPPORT
195 ret = readl_poll_timeout_atomic(
196 PWR_STATUS, tmp,
197 (tmp & MD1_PWR_STA_MASK) == MD1_PWR_STA_MASK,
198 10, 100000);
199 if (ret) {
200 dev_warn(dev, "PWRON STS1 ON MASK fail\n");
201 return ret;
202 }
203 ret = readl_poll_timeout_atomic(
204 PWR_STATUS_2ND, tmp,
205 (tmp & MD1_PWR_STA_MASK) == MD1_PWR_STA_MASK,
206 10, 100000);
207 if (ret) {
208 dev_warn(dev, "PWRON STS2 ON MASK fail\n");
209 return ret;
210 }
211 #else
212 /* TINFO="Wait until PWR_STATUS = 1 and PWR_STATUS_2ND = 1" */
213 while (((spm_read(PWR_STATUS) & MD1_PWR_STA_MASK) !=
214 MD1_PWR_STA_MASK) ||
215 ((spm_read(PWR_STATUS_2ND) & MD1_PWR_STA_MASK) !=
216 MD1_PWR_STA_MASK)) {
217 /* No logic between pwr_on and pwr_ack.
218 * Print SRAM / MTCMOS control and PWR_ACK for
219 * debug.
220 */
221 }
222 #endif
223#endif
224 /* TINFO="Set SRAM_PDN = 0" */
225 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~(0x1 << 8));
226 /* TINFO="MD_SRAM_ISO_CON[0]=1"*/
227 spm_write(MD1_SRAM_ISOINT_B,
228 spm_read(MD1_SRAM_ISOINT_B) | (0x1 << 0));
229
230 /* TINFO="Set PWR_CLK_DIS = 0" */
231 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_CLK_DIS);
232 #if READL_POLL_SUPPORT
233 ret = readl_poll_timeout_atomic(
234 MD1_PWR_CON, tmp,
235 (tmp & PWR_CLK_DIS) != PWR_CLK_DIS,
236 10, 100000);
237 if (ret) {
238 dev_warn(dev, "PWRON CLK DIS fail\n");
239 return ret;
240 }
241 #else
242 while ((spm_read(MD1_PWR_CON) & PWR_CLK_DIS) == PWR_CLK_DIS)
243 ;
244 #endif
245 /* TINFO="Set PWR_CLK_DIS = 1" */
246 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_CLK_DIS);
247
248 /* TINFO="Set PWR_ISO = 0" */
249 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_ISO);
250
251 /* TINFO="Set PWR_RST_B = 1" */
252 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_RST_B);
253
254 /* TINFO="Set PWR_CLK_DIS = 0" */
255 spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) & ~PWR_CLK_DIS);
256 /* TINFO="Set PWR_RST_B = 1" */
257 /* spm_write(MD1_PWR_CON, spm_read(MD1_PWR_CON) | PWR_RST_B); */
258 /* TINFO="MD_EXT_BUCK_ISO_CON[0]=0"*/
259 spm_write(MD_EXT_BUCK_ISO_CON,
260 spm_read(MD_EXT_BUCK_ISO_CON) & ~(0x1 << 0));
261 /* TINFO="MD_EXT_BUCK_ISO_CON[1]=0"*/
262 spm_write(MD_EXT_BUCK_ISO_CON,
263 spm_read(MD_EXT_BUCK_ISO_CON) & ~(0x1 << 1));
264 /* TINFO="MD_EXT_BUCK_ISO_CON[2]=0"*/
265 spm_write(MD_EXT_BUCK_ISO_CON,
266 spm_read(MD_EXT_BUCK_ISO_CON) & ~(0x1 << 2));
267 /* TINFO="MD_EXT_BUCK_ISO_CON[3]=0"*/
268 spm_write(MD_EXT_BUCK_ISO_CON,
269 spm_read(MD_EXT_BUCK_ISO_CON) & ~(0x1 << 3));
270 /* TINFO="MD_EXTRA_PWR_CON[0]=0"*/
271 spm_write(MD_EXTRA_PWR_CON,
272 spm_read(MD_EXTRA_PWR_CON) & ~(0x1 << 0));
273 /* TINFO="Switch to original protect control path" */
274 spm_write(VDNR_CON, spm_read(VDNR_CON) | 0x7F);
275 /* TINFO="Release bus protect - step2 : 0" */
276 spm_write(INFRA_TOPAXI_PROTECTEN_CLR, MD1_PROT_STEP2_0_MASK);
277 /* TINFO="Release bus protect - step1 : 0" */
278 spm_write(INFRA_TOPAXI_PROTECTEN_CLR, MD1_PROT_STEP1_0_MASK);
279 /* TINFO="Finish to turn on MD1" */
280
281 /* From DE, the 93MD need a delay after power-on...*/
282 udelay(250);
283 }
284 return err;
285}
286
287struct scp_domain_data {
288 const char *name;
289 mtcmos_ops mtcmos_enable_func;
290 bool active_wakeup;
291};
292
293struct scp {
294 struct scp_domain *domains;
295 struct genpd_onecell_data pd_data;
296 struct device *dev;
297};
298
299struct scp_domain {
300 struct generic_pm_domain genpd;
301 struct scp *scp;
302 const struct scp_domain_data *data;
303};
304
305struct scp_soc_data {
306 const struct scp_domain_data *domains;
307 int num_domains;
308};
309
310static const struct scp_domain_data scp_domain_data_mt2731[] = {
311 [MT2731_POWER_DOMAIN_MD] = {
312 .name = "MD",
313 .mtcmos_enable_func = spm_mtcmos_ctrl_md1,
314 .active_wakeup = true,
315 }
316};
317
318static const struct scp_soc_data mt2731_data = {
319 .domains = scp_domain_data_mt2731,
320 .num_domains = ARRAY_SIZE(scp_domain_data_mt2731),
321};
322
323static int scpsys_power_on(struct generic_pm_domain *genpd)
324{
325 struct scp_domain *scpd = container_of(genpd, struct scp_domain, genpd);
326 const struct scp_domain_data *data = scpd->data;
327 int ret;
328
329 ret = data->mtcmos_enable_func(scpd->scp->dev, STA_POWER_ON);
330
331 pr_info("mtcmos(%s) power on ret:%d.\n", data->name, ret);
332 return ret;
333}
334
335static int scpsys_power_off(struct generic_pm_domain *genpd)
336{
337 struct scp_domain *scpd = container_of(genpd, struct scp_domain, genpd);
338 const struct scp_domain_data *data = scpd->data;
339 int ret;
340
341 ret = data->mtcmos_enable_func(scpd->scp->dev, STA_POWER_DOWN);
342
343 pr_info("mtcmos(%s) power off ret:%d\n", data->name, ret);
344 return 0;
345}
346
347static bool scpsys_active_wakeup(struct device *dev)
348{
349 struct generic_pm_domain *genpd;
350 struct scp_domain *scpd;
351
352 genpd = pd_to_genpd(dev->pm_domain);
353 scpd = container_of(genpd, struct scp_domain, genpd);
354
355 return scpd->data->active_wakeup;
356}
357
358static struct scp *init_scp(struct platform_device *pdev,
359 const struct scp_domain_data *scp_domain_data,
360 int num)
361{
362 struct genpd_onecell_data *pd_data;
363 int i;
364 struct scp *scp;
365
366 scp = devm_kzalloc(&pdev->dev, sizeof(*scp), GFP_KERNEL);
367 if (!scp)
368 return ERR_PTR(-ENOMEM);
369
370 scp->dev = &pdev->dev;
371 scp->domains = devm_kcalloc(&pdev->dev,
372 num, sizeof(*scp->domains), GFP_KERNEL);
373 if (!scp->domains)
374 return ERR_PTR(-ENOMEM);
375
376 pd_data = &scp->pd_data;
377
378 pd_data->domains = devm_kcalloc(&pdev->dev,
379 num, sizeof(*pd_data->domains), GFP_KERNEL);
380 if (!pd_data->domains)
381 return ERR_PTR(-ENOMEM);
382
383 pd_data->num_domains = num;
384
385 for (i = 0; i < num; i++) {
386 struct scp_domain *scpd = &scp->domains[i];
387 struct generic_pm_domain *genpd = &scpd->genpd;
388 const struct scp_domain_data *data = &scp_domain_data[i];
389
390 pd_data->domains[i] = genpd;
391 scpd->scp = scp;
392
393 scpd->data = data;
394
395 genpd->name = data->name;
396 genpd->power_off = scpsys_power_off;
397 genpd->power_on = scpsys_power_on;
398 genpd->dev_ops.active_wakeup = scpsys_active_wakeup;
399 }
400 return scp;
401}
402
403static void mtk_register_power_domains(struct platform_device *pdev,
404 struct scp *scp, int num)
405{
406 struct genpd_onecell_data *pd_data;
407 int i, ret;
408
409 for (i = 0; i < num; i++) {
410 struct scp_domain *scpd = &scp->domains[i];
411 struct generic_pm_domain *genpd = &scpd->genpd;
412
413 /*
414 * Initially turn on all domains to make the domains usable
415 * with !CONFIG_PM and to get the hardware in sync with the
416 * software. The unused domains will be switched off during
417 * late_init time.
418 */
419 genpd->power_on(genpd);
420
421 pm_genpd_init(genpd, NULL, false);
422 }
423
424 /*
425 * We are not allowed to fail here since there is no way to unregister
426 * a power domain. Once registered above we have to keep the domains
427 * valid.
428 */
429 pd_data = &scp->pd_data;
430
431 ret = of_genpd_add_provider_onecell(pdev->dev.of_node, pd_data);
432 if (ret)
433 dev_err(&pdev->dev, "Failed to add OF provider: %d\n", ret);
434}
435
436static int scpsys_probe(struct platform_device *pdev)
437{
438 struct device *dev = &pdev->dev;
439 struct resource *res;
440 const struct scp_soc_data *soc;
441 struct scp *scp;
442
443 soc = of_device_get_match_data(dev);
444
445 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
446 spm_base = devm_ioremap_resource(dev, res);
447 if (IS_ERR(spm_base))
448 return PTR_ERR(spm_base);
449
450 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
451 infra_base = devm_ioremap_resource(dev, res);
452 if (IS_ERR(infra_base))
453 return PTR_ERR(infra_base);
454
455 scp = init_scp(pdev, soc->domains, soc->num_domains);
456 if (IS_ERR(scp)) {
457 dev_err(dev, "scp init fail(%lx).\n", PTR_ERR(scp));
458 return PTR_ERR(scp);
459 }
460
461 mtk_register_power_domains(pdev, scp, soc->num_domains);
462
463 dev_dbg(dev, "probe done. %p-%p.nr %d\n",
464 spm_base, infra_base, soc->num_domains);
465 return 0;
466}
467
468static const struct of_device_id of_scpsys_match_tbl[] = {
469 {
470 .compatible = "mediatek,mt2731-scpsys",
471 .data = &mt2731_data,
472 }, {
473 /* sentinel */
474 }
475};
476
477static struct platform_driver scpsys_drv = {
478 .probe = scpsys_probe,
479 .driver = {
480 .name = "mtk-scpsys-variant",
481 .suppress_bind_attrs = true,
482 .owner = THIS_MODULE,
483 .of_match_table = of_match_ptr(of_scpsys_match_tbl),
484 },
485};
486builtin_platform_driver(scpsys_drv);