1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (C) 2019 Texas Instruments Incorporated - http://www.ti.com/
6 * Tero Kristo <t-kristo@ti.com>
9 #include <linux/kernel.h>
10 #include <linux/clk.h>
11 #include <linux/device.h>
13 #include <linux/iopoll.h>
14 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/pm_clock.h>
19 #include <linux/pm_domain.h>
20 #include <linux/reset-controller.h>
21 #include <linux/delay.h>
23 #include <linux/platform_data/ti-prm.h>
25 enum omap_prm_domain_mode {
28 OMAP_PRMD_ON_INACTIVE,
32 struct omap_prm_domain_map {
33 unsigned int usable_modes; /* Mask of hardware supported modes */
34 unsigned long statechange:1; /* Optional low-power state change */
35 unsigned long logicretstate:1; /* Optional logic off mode */
38 struct omap_prm_domain {
41 struct generic_pm_domain pd;
44 const struct omap_prm_domain_map *cap;
46 unsigned int uses_pm_clk:1;
54 struct omap_prm_data {
57 const char *clkdm_name;
60 const struct omap_prm_domain_map *dmap;
63 const struct omap_rst_map *rstmap;
68 const struct omap_prm_data *data;
70 struct omap_prm_domain *prmd;
73 struct omap_reset_data {
74 struct reset_controller_dev rcdev;
78 struct clockdomain *clkdm;
82 #define genpd_to_prm_domain(gpd) container_of(gpd, struct omap_prm_domain, pd)
83 #define to_omap_reset_data(p) container_of((p), struct omap_reset_data, rcdev)
85 #define OMAP_MAX_RESETS 8
86 #define OMAP_RESET_MAX_WAIT 10000
88 #define OMAP_PRM_HAS_RSTCTRL BIT(0)
89 #define OMAP_PRM_HAS_RSTST BIT(1)
90 #define OMAP_PRM_HAS_NO_CLKDM BIT(2)
91 #define OMAP_PRM_RET_WHEN_IDLE BIT(3)
93 #define OMAP_PRM_HAS_RESETS (OMAP_PRM_HAS_RSTCTRL | OMAP_PRM_HAS_RSTST)
95 #define PRM_STATE_MAX_WAIT 10000
96 #define PRM_LOGICRETSTATE BIT(2)
97 #define PRM_LOWPOWERSTATECHANGE BIT(4)
98 #define PRM_POWERSTATE_MASK OMAP_PRMD_ON_ACTIVE
100 #define PRM_ST_INTRANSITION BIT(20)
102 static const struct omap_prm_domain_map omap_prm_all = {
103 .usable_modes = BIT(OMAP_PRMD_ON_ACTIVE) | BIT(OMAP_PRMD_ON_INACTIVE) |
104 BIT(OMAP_PRMD_RETENTION) | BIT(OMAP_PRMD_OFF),
109 static const struct omap_prm_domain_map omap_prm_noinact = {
110 .usable_modes = BIT(OMAP_PRMD_ON_ACTIVE) | BIT(OMAP_PRMD_RETENTION) |
116 static const struct omap_prm_domain_map omap_prm_nooff = {
117 .usable_modes = BIT(OMAP_PRMD_ON_ACTIVE) | BIT(OMAP_PRMD_ON_INACTIVE) |
118 BIT(OMAP_PRMD_RETENTION),
123 static const struct omap_prm_domain_map omap_prm_onoff_noauto = {
124 .usable_modes = BIT(OMAP_PRMD_ON_ACTIVE) | BIT(OMAP_PRMD_OFF),
128 static const struct omap_prm_domain_map omap_prm_alwon = {
129 .usable_modes = BIT(OMAP_PRMD_ON_ACTIVE),
132 static const struct omap_prm_domain_map omap_prm_reton = {
133 .usable_modes = BIT(OMAP_PRMD_ON_ACTIVE) | BIT(OMAP_PRMD_RETENTION),
138 static const struct omap_rst_map rst_map_0[] = {
139 { .rst = 0, .st = 0 },
143 static const struct omap_rst_map rst_map_01[] = {
144 { .rst = 0, .st = 0 },
145 { .rst = 1, .st = 1 },
149 static const struct omap_rst_map rst_map_012[] = {
150 { .rst = 0, .st = 0 },
151 { .rst = 1, .st = 1 },
152 { .rst = 2, .st = 2 },
156 static const struct omap_prm_data omap4_prm_data[] = {
158 .name = "mpu", .base = 0x4a306300,
159 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton,
162 .name = "tesla", .base = 0x4a306400,
163 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
164 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
167 .name = "abe", .base = 0x4a306500,
168 .pwrstctrl = 0, .pwrstst = 0x4, .dmap = &omap_prm_all,
171 .name = "always_on_core", .base = 0x4a306600,
172 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
175 .name = "core", .base = 0x4a306700,
176 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton,
177 .rstctrl = 0x210, .rstst = 0x214, .clkdm_name = "ducati",
178 .rstmap = rst_map_012,
179 .flags = OMAP_PRM_RET_WHEN_IDLE,
182 .name = "ivahd", .base = 0x4a306f00,
183 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
184 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_012
187 .name = "cam", .base = 0x4a307000,
188 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
191 .name = "dss", .base = 0x4a307100,
192 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact
195 .name = "gfx", .base = 0x4a307200,
196 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
199 .name = "l3init", .base = 0x4a307300,
200 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton
203 .name = "l4per", .base = 0x4a307400,
204 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton,
205 .flags = OMAP_PRM_RET_WHEN_IDLE,
208 .name = "cefuse", .base = 0x4a307600,
209 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
212 .name = "wkup", .base = 0x4a307700,
213 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon
216 .name = "emu", .base = 0x4a307900,
217 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
220 .name = "device", .base = 0x4a307b00,
221 .rstctrl = 0x0, .rstst = 0x4, .rstmap = rst_map_01,
222 .flags = OMAP_PRM_HAS_RSTCTRL | OMAP_PRM_HAS_NO_CLKDM
227 static const struct omap_prm_data omap5_prm_data[] = {
229 .name = "mpu", .base = 0x4ae06300,
230 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton,
233 .name = "dsp", .base = 0x4ae06400,
234 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
235 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
238 .name = "abe", .base = 0x4ae06500,
239 .pwrstctrl = 0, .pwrstst = 0x4, .dmap = &omap_prm_nooff,
242 .name = "coreaon", .base = 0x4ae06600,
243 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon
246 .name = "core", .base = 0x4ae06700,
247 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton,
248 .rstctrl = 0x210, .rstst = 0x214, .clkdm_name = "ipu",
249 .rstmap = rst_map_012
252 .name = "iva", .base = 0x4ae07200,
253 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
254 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_012
257 .name = "cam", .base = 0x4ae07300,
258 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
261 .name = "dss", .base = 0x4ae07400,
262 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact
265 .name = "gpu", .base = 0x4ae07500,
266 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
269 .name = "l3init", .base = 0x4ae07600,
270 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton
273 .name = "custefuse", .base = 0x4ae07700,
274 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
277 .name = "wkupaon", .base = 0x4ae07800,
278 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon
281 .name = "emu", .base = 0x4ae07a00,
282 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto
285 .name = "device", .base = 0x4ae07c00,
286 .rstctrl = 0x0, .rstst = 0x4, .rstmap = rst_map_01,
287 .flags = OMAP_PRM_HAS_RSTCTRL | OMAP_PRM_HAS_NO_CLKDM
292 static const struct omap_prm_data dra7_prm_data[] = {
294 .name = "mpu", .base = 0x4ae06300,
295 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_reton,
298 .name = "dsp1", .base = 0x4ae06400,
299 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
300 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01,
303 .name = "ipu", .base = 0x4ae06500,
304 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
305 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_012,
309 .name = "coreaon", .base = 0x4ae06628,
310 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
313 .name = "core", .base = 0x4ae06700,
314 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
315 .rstctrl = 0x210, .rstst = 0x214, .rstmap = rst_map_012,
319 .name = "iva", .base = 0x4ae06f00,
320 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
321 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_012,
324 .name = "cam", .base = 0x4ae07000,
325 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
328 .name = "dss", .base = 0x4ae07100,
329 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
332 .name = "gpu", .base = 0x4ae07200,
333 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
336 .name = "l3init", .base = 0x4ae07300,
337 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
338 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01,
342 .name = "l4per", .base = 0x4ae07400,
343 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
346 .name = "custefuse", .base = 0x4ae07600,
347 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
350 .name = "wkupaon", .base = 0x4ae07724,
351 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
354 .name = "emu", .base = 0x4ae07900,
355 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
358 .name = "dsp2", .base = 0x4ae07b00,
359 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
360 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
363 .name = "eve1", .base = 0x4ae07b40,
364 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
365 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
368 .name = "eve2", .base = 0x4ae07b80,
369 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
370 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
373 .name = "eve3", .base = 0x4ae07bc0,
374 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
375 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
378 .name = "eve4", .base = 0x4ae07c00,
379 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
380 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_01
383 .name = "rtc", .base = 0x4ae07c60,
384 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
387 .name = "vpe", .base = 0x4ae07c80,
388 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
393 static const struct omap_rst_map am3_per_rst_map[] = {
398 static const struct omap_rst_map am3_wkup_rst_map[] = {
399 { .rst = 3, .st = 5 },
403 static const struct omap_prm_data am3_prm_data[] = {
405 .name = "per", .base = 0x44e00c00,
406 .pwrstctrl = 0xc, .pwrstst = 0x8, .dmap = &omap_prm_noinact,
407 .rstctrl = 0x0, .rstmap = am3_per_rst_map,
408 .flags = OMAP_PRM_HAS_RSTCTRL, .clkdm_name = "pruss_ocp"
411 .name = "wkup", .base = 0x44e00d00,
412 .pwrstctrl = 0x4, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
413 .rstctrl = 0x0, .rstst = 0xc, .rstmap = am3_wkup_rst_map,
414 .flags = OMAP_PRM_HAS_RSTCTRL | OMAP_PRM_HAS_NO_CLKDM
417 .name = "mpu", .base = 0x44e00e00,
418 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
421 .name = "device", .base = 0x44e00f00,
422 .rstctrl = 0x0, .rstst = 0x8, .rstmap = rst_map_01,
423 .flags = OMAP_PRM_HAS_RSTCTRL | OMAP_PRM_HAS_NO_CLKDM
426 .name = "rtc", .base = 0x44e01000,
427 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
430 .name = "gfx", .base = 0x44e01100,
431 .pwrstctrl = 0, .pwrstst = 0x10, .dmap = &omap_prm_noinact,
432 .rstctrl = 0x4, .rstst = 0x14, .rstmap = rst_map_0, .clkdm_name = "gfx_l3",
435 .name = "cefuse", .base = 0x44e01200,
436 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
441 static const struct omap_rst_map am4_per_rst_map[] = {
442 { .rst = 1, .st = 0 },
446 static const struct omap_rst_map am4_device_rst_map[] = {
447 { .rst = 0, .st = 1 },
448 { .rst = 1, .st = 0 },
452 static const struct omap_prm_data am4_prm_data[] = {
454 .name = "mpu", .base = 0x44df0300,
455 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
458 .name = "gfx", .base = 0x44df0400,
459 .pwrstctrl = 0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
460 .rstctrl = 0x10, .rstst = 0x14, .rstmap = rst_map_0, .clkdm_name = "gfx_l3",
463 .name = "rtc", .base = 0x44df0500,
464 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
467 .name = "tamper", .base = 0x44df0600,
468 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
471 .name = "cefuse", .base = 0x44df0700,
472 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_onoff_noauto,
475 .name = "per", .base = 0x44df0800,
476 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_noinact,
477 .rstctrl = 0x10, .rstst = 0x14, .rstmap = am4_per_rst_map,
478 .clkdm_name = "pruss_ocp"
481 .name = "wkup", .base = 0x44df2000,
482 .pwrstctrl = 0x0, .pwrstst = 0x4, .dmap = &omap_prm_alwon,
483 .rstctrl = 0x10, .rstst = 0x14, .rstmap = am3_wkup_rst_map,
484 .flags = OMAP_PRM_HAS_NO_CLKDM
487 .name = "device", .base = 0x44df4000,
488 .rstctrl = 0x0, .rstst = 0x4, .rstmap = am4_device_rst_map,
489 .flags = OMAP_PRM_HAS_RSTCTRL | OMAP_PRM_HAS_NO_CLKDM
494 static const struct of_device_id omap_prm_id_table[] = {
495 { .compatible = "ti,omap4-prm-inst", .data = omap4_prm_data },
496 { .compatible = "ti,omap5-prm-inst", .data = omap5_prm_data },
497 { .compatible = "ti,dra7-prm-inst", .data = dra7_prm_data },
498 { .compatible = "ti,am3-prm-inst", .data = am3_prm_data },
499 { .compatible = "ti,am4-prm-inst", .data = am4_prm_data },
504 static void omap_prm_domain_show_state(struct omap_prm_domain *prmd,
507 dev_dbg(prmd->dev, "%s %s: %08x/%08x\n",
509 readl_relaxed(prmd->prm->base + prmd->pwrstctrl),
510 readl_relaxed(prmd->prm->base + prmd->pwrstst));
513 static inline void omap_prm_domain_show_state(struct omap_prm_domain *prmd,
519 static int omap_prm_domain_power_on(struct generic_pm_domain *domain)
521 struct omap_prm_domain *prmd;
525 prmd = genpd_to_prm_domain(domain);
529 omap_prm_domain_show_state(prmd, "on: previous state");
531 if (prmd->pwrstctrl_saved)
532 v = prmd->pwrstctrl_saved;
534 v = readl_relaxed(prmd->prm->base + prmd->pwrstctrl);
536 if (prmd->prm->data->flags & OMAP_PRM_RET_WHEN_IDLE)
537 mode = OMAP_PRMD_RETENTION;
539 mode = OMAP_PRMD_ON_ACTIVE;
541 writel_relaxed((v & ~PRM_POWERSTATE_MASK) | mode,
542 prmd->prm->base + prmd->pwrstctrl);
544 /* wait for the transition bit to get cleared */
545 ret = readl_relaxed_poll_timeout(prmd->prm->base + prmd->pwrstst,
546 v, !(v & PRM_ST_INTRANSITION), 1,
549 dev_err(prmd->dev, "%s: %s timed out\n",
550 prmd->pd.name, __func__);
552 omap_prm_domain_show_state(prmd, "on: new state");
557 /* No need to check for holes in the mask for the lowest mode */
558 static int omap_prm_domain_find_lowest(struct omap_prm_domain *prmd)
560 return __ffs(prmd->cap->usable_modes);
563 static int omap_prm_domain_power_off(struct generic_pm_domain *domain)
565 struct omap_prm_domain *prmd;
569 prmd = genpd_to_prm_domain(domain);
573 omap_prm_domain_show_state(prmd, "off: previous state");
575 v = readl_relaxed(prmd->prm->base + prmd->pwrstctrl);
576 prmd->pwrstctrl_saved = v;
578 v &= ~PRM_POWERSTATE_MASK;
579 v |= omap_prm_domain_find_lowest(prmd);
581 if (prmd->cap->statechange)
582 v |= PRM_LOWPOWERSTATECHANGE;
583 if (prmd->cap->logicretstate)
584 v &= ~PRM_LOGICRETSTATE;
586 v |= PRM_LOGICRETSTATE;
588 writel_relaxed(v, prmd->prm->base + prmd->pwrstctrl);
590 /* wait for the transition bit to get cleared */
591 ret = readl_relaxed_poll_timeout(prmd->prm->base + prmd->pwrstst,
592 v, !(v & PRM_ST_INTRANSITION), 1,
595 dev_warn(prmd->dev, "%s: %s timed out\n",
596 __func__, prmd->pd.name);
598 omap_prm_domain_show_state(prmd, "off: new state");
604 * Note that ti-sysc already manages the module clocks separately so
605 * no need to manage those. Interconnect instances need clocks managed
608 static int omap_prm_domain_attach_clock(struct device *dev,
609 struct omap_prm_domain *prmd)
611 struct device_node *np = dev->of_node;
614 if (!of_device_is_compatible(np, "simple-pm-bus"))
617 if (!of_property_read_bool(np, "clocks"))
620 error = pm_clk_create(dev);
624 error = of_pm_clk_add_clks(dev);
630 prmd->uses_pm_clk = 1;
635 static int omap_prm_domain_attach_dev(struct generic_pm_domain *domain,
638 struct generic_pm_domain_data *genpd_data;
639 struct of_phandle_args pd_args;
640 struct omap_prm_domain *prmd;
641 struct device_node *np;
644 prmd = genpd_to_prm_domain(domain);
647 ret = of_parse_phandle_with_args(np, "power-domains",
648 "#power-domain-cells", 0, &pd_args);
652 if (pd_args.args_count != 0)
653 dev_warn(dev, "%s: unusupported #power-domain-cells: %i\n",
654 prmd->pd.name, pd_args.args_count);
656 genpd_data = dev_gpd_data(dev);
657 genpd_data->data = NULL;
659 ret = omap_prm_domain_attach_clock(dev, prmd);
666 static void omap_prm_domain_detach_dev(struct generic_pm_domain *domain,
669 struct generic_pm_domain_data *genpd_data;
670 struct omap_prm_domain *prmd;
672 prmd = genpd_to_prm_domain(domain);
673 if (prmd->uses_pm_clk)
675 genpd_data = dev_gpd_data(dev);
676 genpd_data->data = NULL;
679 static int omap_prm_domain_init(struct device *dev, struct omap_prm *prm)
681 struct omap_prm_domain *prmd;
682 struct device_node *np = dev->of_node;
683 const struct omap_prm_data *data;
687 if (!of_find_property(dev->of_node, "#power-domain-cells", NULL))
690 of_node_put(dev->of_node);
692 prmd = devm_kzalloc(dev, sizeof(*prmd), GFP_KERNEL);
697 name = devm_kasprintf(dev, GFP_KERNEL, "prm_%s",
702 prmd->cap = prmd->prm->data->dmap;
703 prmd->pwrstctrl = prmd->prm->data->pwrstctrl;
704 prmd->pwrstst = prmd->prm->data->pwrstst;
706 prmd->pd.name = name;
707 prmd->pd.power_on = omap_prm_domain_power_on;
708 prmd->pd.power_off = omap_prm_domain_power_off;
709 prmd->pd.attach_dev = omap_prm_domain_attach_dev;
710 prmd->pd.detach_dev = omap_prm_domain_detach_dev;
711 prmd->pd.flags = GENPD_FLAG_PM_CLK;
713 pm_genpd_init(&prmd->pd, NULL, true);
714 error = of_genpd_add_provider_simple(np, &prmd->pd);
716 pm_genpd_remove(&prmd->pd);
723 static bool _is_valid_reset(struct omap_reset_data *reset, unsigned long id)
725 if (reset->mask & BIT(id))
731 static int omap_reset_get_st_bit(struct omap_reset_data *reset,
734 const struct omap_rst_map *map = reset->prm->data->rstmap;
736 while (map->rst >= 0) {
746 static int omap_reset_status(struct reset_controller_dev *rcdev,
749 struct omap_reset_data *reset = to_omap_reset_data(rcdev);
751 int st_bit = omap_reset_get_st_bit(reset, id);
752 bool has_rstst = reset->prm->data->rstst ||
753 (reset->prm->data->flags & OMAP_PRM_HAS_RSTST);
755 /* Check if we have rstst */
759 /* Check if hw reset line is asserted */
760 v = readl_relaxed(reset->prm->base + reset->prm->data->rstctrl);
765 * Check reset status, high value means reset sequence has been
766 * completed successfully so we can return 0 here (reset deasserted)
768 v = readl_relaxed(reset->prm->base + reset->prm->data->rstst);
775 static int omap_reset_assert(struct reset_controller_dev *rcdev,
778 struct omap_reset_data *reset = to_omap_reset_data(rcdev);
782 /* assert the reset control line */
783 spin_lock_irqsave(&reset->lock, flags);
784 v = readl_relaxed(reset->prm->base + reset->prm->data->rstctrl);
786 writel_relaxed(v, reset->prm->base + reset->prm->data->rstctrl);
787 spin_unlock_irqrestore(&reset->lock, flags);
792 static int omap_reset_deassert(struct reset_controller_dev *rcdev,
795 struct omap_reset_data *reset = to_omap_reset_data(rcdev);
800 struct ti_prm_platform_data *pdata = dev_get_platdata(reset->dev);
803 /* Nothing to do if the reset is already deasserted */
804 if (!omap_reset_status(rcdev, id))
807 has_rstst = reset->prm->data->rstst ||
808 (reset->prm->data->flags & OMAP_PRM_HAS_RSTST);
811 st_bit = omap_reset_get_st_bit(reset, id);
813 /* Clear the reset status by writing 1 to the status bit */
815 writel_relaxed(v, reset->prm->base + reset->prm->data->rstst);
819 pdata->clkdm_deny_idle(reset->clkdm);
821 /* de-assert the reset control line */
822 spin_lock_irqsave(&reset->lock, flags);
823 v = readl_relaxed(reset->prm->base + reset->prm->data->rstctrl);
825 writel_relaxed(v, reset->prm->base + reset->prm->data->rstctrl);
826 spin_unlock_irqrestore(&reset->lock, flags);
828 /* wait for the reset bit to clear */
829 ret = readl_relaxed_poll_timeout_atomic(reset->prm->base +
830 reset->prm->data->rstctrl,
831 v, !(v & BIT(id)), 1,
832 OMAP_RESET_MAX_WAIT);
834 pr_err("%s: timedout waiting for %s:%lu\n", __func__,
835 reset->prm->data->name, id);
837 /* wait for the status to be set */
839 ret = readl_relaxed_poll_timeout_atomic(reset->prm->base +
840 reset->prm->data->rstst,
841 v, v & BIT(st_bit), 1,
842 OMAP_RESET_MAX_WAIT);
844 pr_err("%s: timedout waiting for %s:%lu\n", __func__,
845 reset->prm->data->name, id);
849 pdata->clkdm_allow_idle(reset->clkdm);
854 static const struct reset_control_ops omap_reset_ops = {
855 .assert = omap_reset_assert,
856 .deassert = omap_reset_deassert,
857 .status = omap_reset_status,
860 static int omap_prm_reset_xlate(struct reset_controller_dev *rcdev,
861 const struct of_phandle_args *reset_spec)
863 struct omap_reset_data *reset = to_omap_reset_data(rcdev);
865 if (!_is_valid_reset(reset, reset_spec->args[0]))
868 return reset_spec->args[0];
871 static int omap_prm_reset_init(struct platform_device *pdev,
872 struct omap_prm *prm)
874 struct omap_reset_data *reset;
875 const struct omap_rst_map *map;
876 struct ti_prm_platform_data *pdata = dev_get_platdata(&pdev->dev);
881 * Check if we have controllable resets. If either rstctrl is non-zero
882 * or OMAP_PRM_HAS_RSTCTRL flag is set, we have reset control register
885 if (!prm->data->rstctrl && !(prm->data->flags & OMAP_PRM_HAS_RSTCTRL))
888 /* Check if we have the pdata callbacks in place */
889 if (!pdata || !pdata->clkdm_lookup || !pdata->clkdm_deny_idle ||
890 !pdata->clkdm_allow_idle)
893 map = prm->data->rstmap;
897 reset = devm_kzalloc(&pdev->dev, sizeof(*reset), GFP_KERNEL);
901 reset->rcdev.owner = THIS_MODULE;
902 reset->rcdev.ops = &omap_reset_ops;
903 reset->rcdev.of_node = pdev->dev.of_node;
904 reset->rcdev.nr_resets = OMAP_MAX_RESETS;
905 reset->rcdev.of_xlate = omap_prm_reset_xlate;
906 reset->rcdev.of_reset_n_cells = 1;
907 reset->dev = &pdev->dev;
908 spin_lock_init(&reset->lock);
912 sprintf(buf, "%s_clkdm", prm->data->clkdm_name ? prm->data->clkdm_name :
915 if (!(prm->data->flags & OMAP_PRM_HAS_NO_CLKDM)) {
916 reset->clkdm = pdata->clkdm_lookup(buf);
921 while (map->rst >= 0) {
922 reset->mask |= BIT(map->rst);
926 /* Quirk handling to assert rst_map_012 bits on reset and avoid errors */
927 if (prm->data->rstmap == rst_map_012) {
928 v = readl_relaxed(reset->prm->base + reset->prm->data->rstctrl);
929 if ((v & reset->mask) != reset->mask) {
930 dev_dbg(&pdev->dev, "Asserting all resets: %08x\n", v);
931 writel_relaxed(reset->mask, reset->prm->base +
932 reset->prm->data->rstctrl);
936 return devm_reset_controller_register(&pdev->dev, &reset->rcdev);
939 static int omap_prm_probe(struct platform_device *pdev)
941 struct resource *res;
942 const struct omap_prm_data *data;
943 struct omap_prm *prm;
944 const struct of_device_id *match;
947 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
951 match = of_match_device(omap_prm_id_table, &pdev->dev);
955 prm = devm_kzalloc(&pdev->dev, sizeof(*prm), GFP_KERNEL);
961 while (data->base != res->start) {
969 prm->base = devm_ioremap_resource(&pdev->dev, res);
970 if (IS_ERR(prm->base))
971 return PTR_ERR(prm->base);
973 ret = omap_prm_domain_init(&pdev->dev, prm);
977 ret = omap_prm_reset_init(pdev, prm);
984 of_genpd_del_provider(pdev->dev.of_node);
985 pm_genpd_remove(&prm->prmd->pd);
990 static struct platform_driver omap_prm_driver = {
991 .probe = omap_prm_probe,
993 .name = KBUILD_MODNAME,
994 .of_match_table = omap_prm_id_table,
997 builtin_platform_driver(omap_prm_driver);