1 // SPDX-License-Identifier: GPL-2.0+
3 * Texas Instruments power domain driver
5 * Copyright (C) 2020-2021 Texas Instruments Incorporated - http://www.ti.com/
6 * Tero Kristo <t-kristo@ti.com>
13 #include <power-domain-uclass.h>
16 #include <linux/iopoll.h>
18 #define PSC_PTCMD 0x120
19 #define PSC_PTSTAT 0x128
20 #define PSC_PDSTAT 0x200
21 #define PSC_PDCTL 0x300
22 #define PSC_MDSTAT 0x800
23 #define PSC_MDCTL 0xa00
25 #define PDCTL_STATE_MASK 0x1
26 #define PDCTL_STATE_OFF 0x0
27 #define PDCTL_STATE_ON 0x1
29 #define MDSTAT_STATE_MASK 0x3f
30 #define MDSTAT_BUSY_MASK 0x30
31 #define MDSTAT_STATE_SWRSTDISABLE 0x0
32 #define MDSTAT_STATE_ENABLE 0x3
34 #define LPSC_TIMEOUT 1000
35 #define PD_TIMEOUT 1000
37 static u32 psc_read(struct ti_psc *psc, u32 reg)
41 val = readl(psc->base + reg);
42 debug("%s: 0x%x from %p\n", __func__, val, psc->base + reg);
46 static void psc_write(u32 val, struct ti_psc *psc, u32 reg)
48 debug("%s: 0x%x to %p\n", __func__, val, psc->base + reg);
49 writel(val, psc->base + reg);
52 static u32 pd_read(struct ti_pd *pd, u32 reg)
54 return psc_read(pd->psc, reg + 4 * pd->id);
57 static void pd_write(u32 val, struct ti_pd *pd, u32 reg)
59 psc_write(val, pd->psc, reg + 4 * pd->id);
62 static u32 lpsc_read(struct ti_lpsc *lpsc, u32 reg)
64 return psc_read(lpsc->psc, reg + 4 * lpsc->id);
67 static void lpsc_write(u32 val, struct ti_lpsc *lpsc, u32 reg)
69 psc_write(val, lpsc->psc, reg + 4 * lpsc->id);
72 static const struct soc_attr ti_k3_soc_pd_data[] = {
73 #if IS_ENABLED(CONFIG_SOC_K3_J721E)
76 .data = &j721e_pd_platdata,
80 .data = &j7200_pd_platdata,
82 #elif CONFIG_SOC_K3_J721S2
85 .data = &j721s2_pd_platdata,
91 static int ti_power_domain_probe(struct udevice *dev)
93 struct ti_k3_pd_platdata *data = dev_get_priv(dev);
94 const struct soc_attr *soc_match_data;
95 const struct ti_k3_pd_platdata *pdata;
97 printf("%s(dev=%p)\n", __func__, dev);
102 soc_match_data = soc_device_match(ti_k3_soc_pd_data);
106 pdata = (const struct ti_k3_pd_platdata *)soc_match_data->data;
108 data->psc = pdata->psc;
109 data->pd = pdata->pd;
110 data->lpsc = pdata->lpsc;
111 data->devs = pdata->devs;
112 data->num_psc = pdata->num_psc;
113 data->num_pd = pdata->num_pd;
114 data->num_lpsc = pdata->num_lpsc;
115 data->num_devs = pdata->num_devs;
120 static int ti_pd_wait(struct ti_pd *pd)
125 ret = readl_poll_timeout(pd->psc->base + PSC_PTSTAT, ptstat,
126 !(ptstat & BIT(pd->id)), PD_TIMEOUT);
129 printf("%s: psc%d, pd%d failed to transition.\n", __func__,
130 pd->psc->id, pd->id);
135 static void ti_pd_transition(struct ti_pd *pd)
137 psc_write(BIT(pd->id), pd->psc, PSC_PTCMD);
140 u8 ti_pd_state(struct ti_pd *pd)
142 return pd_read(pd, PSC_PDCTL) & PDCTL_STATE_MASK;
145 static int ti_pd_get(struct ti_pd *pd)
152 if (pd->usecount > 1)
156 ret = ti_pd_get(pd->depend);
159 ti_pd_transition(pd->depend);
160 ret = ti_pd_wait(pd->depend);
165 pdctl = pd_read(pd, PSC_PDCTL);
167 if ((pdctl & PDCTL_STATE_MASK) == PDCTL_STATE_ON)
170 debug("%s: enabling psc:%d, pd:%d\n", __func__, pd->psc->id, pd->id);
172 pdctl &= ~PDCTL_STATE_MASK;
173 pdctl |= PDCTL_STATE_ON;
175 pd_write(pdctl, pd, PSC_PDCTL);
180 static int ti_pd_put(struct ti_pd *pd)
187 if (pd->usecount > 0)
190 pdctl = pd_read(pd, PSC_PDCTL);
191 if ((pdctl & PDCTL_STATE_MASK) == PDCTL_STATE_OFF)
194 pdctl &= ~PDCTL_STATE_MASK;
195 pdctl |= PDCTL_STATE_OFF;
197 debug("%s: disabling psc:%d, pd:%d\n", __func__, pd->psc->id, pd->id);
199 pd_write(pdctl, pd, PSC_PDCTL);
202 ti_pd_transition(pd);
203 ret = ti_pd_wait(pd);
207 ret = ti_pd_put(pd->depend);
210 ti_pd_transition(pd->depend);
211 ret = ti_pd_wait(pd->depend);
219 static int ti_lpsc_wait(struct ti_lpsc *lpsc)
224 ret = readl_poll_timeout(lpsc->psc->base + PSC_MDSTAT + lpsc->id * 4,
226 !(mdstat & MDSTAT_BUSY_MASK), LPSC_TIMEOUT);
229 printf("%s: module %d failed to transition.\n", __func__,
235 u8 lpsc_get_state(struct ti_lpsc *lpsc)
237 return lpsc_read(lpsc, PSC_MDCTL) & MDSTAT_STATE_MASK;
240 int ti_lpsc_transition(struct ti_lpsc *lpsc, u8 state)
242 struct ti_pd *psc_pd;
248 if (state == MDSTAT_STATE_ENABLE) {
250 if (lpsc->usecount > 1)
254 if (lpsc->usecount >= 1)
258 debug("%s: transitioning psc:%d, lpsc:%d to %x\n", __func__,
259 lpsc->psc->id, lpsc->id, state);
262 ti_lpsc_transition(lpsc->depend, state);
264 mdctl = lpsc_read(lpsc, PSC_MDCTL);
265 if ((mdctl & MDSTAT_STATE_MASK) == state)
268 if (state == MDSTAT_STATE_ENABLE)
273 mdctl &= ~MDSTAT_STATE_MASK;
276 lpsc_write(mdctl, lpsc, PSC_MDCTL);
278 ti_pd_transition(psc_pd);
279 ret = ti_pd_wait(psc_pd);
283 return ti_lpsc_wait(lpsc);
286 static int ti_power_domain_transition(struct power_domain *pd, u8 state)
288 struct ti_lpsc *lpsc = pd->priv;
290 return ti_lpsc_transition(lpsc, state);
293 static int ti_power_domain_on(struct power_domain *pd)
295 debug("%s(pd=%p, id=%lu)\n", __func__, pd, pd->id);
297 return ti_power_domain_transition(pd, MDSTAT_STATE_ENABLE);
300 static int ti_power_domain_off(struct power_domain *pd)
302 debug("%s(pd=%p, id=%lu)\n", __func__, pd, pd->id);
304 return ti_power_domain_transition(pd, MDSTAT_STATE_SWRSTDISABLE);
307 static struct ti_lpsc *lpsc_lookup(struct ti_k3_pd_platdata *data, int id)
311 for (idx = 0; idx < data->num_devs; idx++)
312 if (data->devs[idx].id == id)
313 return data->devs[idx].lpsc;
318 static int ti_power_domain_of_xlate(struct power_domain *pd,
319 struct ofnode_phandle_args *args)
321 struct ti_k3_pd_platdata *data = dev_get_priv(pd->dev);
322 struct ti_lpsc *lpsc;
324 debug("%s(power_domain=%p, id=%d)\n", __func__, pd, args->args[0]);
326 if (args->args_count < 1) {
327 printf("Invalid args_count: %d\n", args->args_count);
331 lpsc = lpsc_lookup(data, args->args[0]);
333 printf("%s: invalid dev-id: %d\n", __func__, args->args[0]);
343 static int ti_power_domain_request(struct power_domain *pd)
348 static int ti_power_domain_free(struct power_domain *pd)
353 static const struct udevice_id ti_power_domain_of_match[] = {
354 { .compatible = "ti,sci-pm-domain" },
358 static struct power_domain_ops ti_power_domain_ops = {
359 .on = ti_power_domain_on,
360 .off = ti_power_domain_off,
361 .of_xlate = ti_power_domain_of_xlate,
362 .request = ti_power_domain_request,
363 .rfree = ti_power_domain_free,
366 U_BOOT_DRIVER(ti_pm_domains) = {
367 .name = "ti-pm-domains",
368 .id = UCLASS_POWER_DOMAIN,
369 .of_match = ti_power_domain_of_match,
370 .probe = ti_power_domain_probe,
371 .priv_auto = sizeof(struct ti_k3_pd_platdata),
372 .ops = &ti_power_domain_ops,