xref: /rk3399_ARM-atf/drivers/ti/pd/ti_psc.c (revision a28114d66a6d43db4accef5fd5d6dab6c059e584)
1 /*
2  * Copyright (c) 2025-2026 Texas Instruments Incorporated - https://www.ti.com
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 /*
8  * Power/Sleep Controller (PSC) Driver
9  *
10  * This driver manages the PSC hardware including power domain state transitions,
11  * LPSC module clock gating, and dependency management between modules and power domains.
12  */
13 
14 #include <assert.h>
15 #include <errno.h>
16 #include <stddef.h>
17 
18 #include <common/debug.h>
19 #include <drivers/delay_timer.h>
20 #include <lib/mmio.h>
21 
22 #include <ti_clk.h>
23 #include <ti_device.h>
24 #include <ti_device_pm.h>
25 #include <ti_psc.h>
26 
27 #define PSC_PTCMD		0x120U
28 #define PSC_PTCMD_H		0x124U
29 #define PSC_PTSTAT		0x128U
30 #define PSC_PTSTAT_H		0x12CU
31 #define PSC_PDSTAT_BASE		0x200U
32 #define PSC_PDCTL_BASE		0x300U
33 #define PSC_MDSTAT_BASE		0x800U
34 #define PSC_MDCTL_BASE		0xa00U
35 
psc_reg(uint32_t base,uint32_t id)36 static inline uint32_t psc_reg(uint32_t base, uint32_t id)
37 {
38 	return base + (4U * id);
39 }
40 
41 #define MDSTAT_STATE_MASK		0x3fU
42 #define MDSTAT_BUSY_MASK		0x30U
43 #define MDSTAT_STATE_SWRSTDISABLE	0x00U
44 #define MDSTAT_STATE_SYNCRST		0x01U
45 #define MDSTAT_STATE_DISABLE		0x02U
46 #define MDSTAT_STATE_ENABLE		0x03U
47 
48 #define MDSTAT_MRSTDONE		BIT(11) /* Module reset done */
49 #define MDSTAT_LRSTDONE		BIT(9)	/* Local reset done */
50 
51 #define MDCTL_RESET_ISO		BIT(12)		/* Enable reset isolation */
52 #define MDCTL_LRST		BIT(8)		/* Assert local reset when 0 */
53 
54 #define PDSTAT_STATE_MASK		0x1fU
55 
56 #define PDCTL_STATE_MASK	BIT(0)
57 #define PDCTL_STATE_OFF		0U
58 #define PDCTL_STATE_ON		1U
59 
60 /* Timeout values for PSC state transitions */
61 #define PSC_TRANSITION_TIMEOUT_US	10000U	/* 10ms timeout */
62 #define PSC_TRANSITION_DELAY_US		100U	/* 100us delay between checks */
63 #define PSC_TRANSITION_RETRY_COUNT	(PSC_TRANSITION_TIMEOUT_US / PSC_TRANSITION_DELAY_US)
64 
65 /* Module state return values */
66 #define TI_LPSC_STATE_DISABLED		0U	/* Module is disabled */
67 #define TI_LPSC_STATE_ENABLED		1U	/* Module is enabled or in retention */
68 #define TI_LPSC_STATE_TRANSITIONING	2U	/* Module is transitioning */
69 
70 static struct ti_device *psc_devs;
71 
72 static void lpsc_module_get_internal(struct ti_device *dev,
73 				     struct ti_lpsc_module *module,
74 				     bool use, bool ret);
75 static void lpsc_module_put_internal(struct ti_device *dev,
76 				     struct ti_lpsc_module *module,
77 				     bool use, bool ret);
78 
psc_read(struct ti_device * dev,uint32_t reg)79 static uint32_t psc_read(struct ti_device *dev, uint32_t reg)
80 {
81 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
82 
83 	if (psc->base == 0U)
84 		return 0U;
85 
86 	return mmio_read_32(psc->base + reg);
87 }
88 
psc_write(struct ti_device * dev,uint32_t val,uint32_t reg)89 static void psc_write(struct ti_device *dev, uint32_t val, uint32_t reg)
90 {
91 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
92 
93 	if (psc->base == 0U)
94 		return;
95 
96 	mmio_write_32(psc->base + reg, val);
97 }
98 
ti_psc_pd_idx(struct ti_device * dev,const struct ti_psc_pd * pd)99 ti_pd_idx_t ti_psc_pd_idx(struct ti_device *dev, const struct ti_psc_pd *pd)
100 {
101 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
102 	ptrdiff_t diff = pd - psc->powerdomains;
103 
104 	return (ti_pd_idx_t) diff;
105 }
106 
psc_idx2pd(const struct ti_psc_drv_data * psc,ti_pd_idx_t id)107 static inline struct ti_psc_pd *psc_idx2pd(const struct ti_psc_drv_data *psc,
108 					   ti_pd_idx_t id)
109 {
110 	return &psc->powerdomains[id];
111 }
112 
get_psc_pd_data(struct ti_device * dev,struct ti_psc_pd * pd)113 static const struct ti_psc_pd_data *get_psc_pd_data(struct ti_device *dev,
114 						    struct ti_psc_pd *pd)
115 {
116 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
117 
118 	return &psc->pd_data[ti_psc_pd_idx(dev, pd)];
119 }
120 
ti_lpsc_module_idx(struct ti_device * dev,const struct ti_lpsc_module * module)121 ti_lpsc_idx_t ti_lpsc_module_idx(struct ti_device *dev, const struct ti_lpsc_module *module)
122 {
123 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
124 	ptrdiff_t diff = module - psc->modules;
125 
126 	return (ti_lpsc_idx_t) diff;
127 }
128 
psc_idx2mod(const struct ti_psc_drv_data * psc,ti_lpsc_idx_t id)129 static inline struct ti_lpsc_module *psc_idx2mod(const struct ti_psc_drv_data *psc,
130 						 ti_lpsc_idx_t id)
131 {
132 	return &psc->modules[id];
133 }
134 
ti_psc_pd_wait(struct ti_device * dev,struct ti_psc_pd * pd)135 void ti_psc_pd_wait(struct ti_device *dev, struct ti_psc_pd *pd)
136 {
137 	uint32_t psc_ptstat = PSC_PTSTAT;
138 	ti_pd_idx_t psc_idx    = ti_psc_pd_idx(dev, pd);
139 
140 	if (0U == (get_psc_pd_data(dev, pd)->flags & TI_PSC_PD_ALWAYSON)) {
141 		uint32_t retry_count = PSC_TRANSITION_RETRY_COUNT;
142 		/* power domain >= 32 uses PSC_PTSTAT_H register */
143 		if (psc_idx >= 32U) {
144 			psc_ptstat = PSC_PTSTAT_H;
145 		}
146 		while ((((psc_read(dev, psc_ptstat)) &
147 			 BIT(psc_idx % 32U)) != 0U) && (retry_count > 0U)) {
148 			udelay(PSC_TRANSITION_DELAY_US);
149 			retry_count--;
150 		}
151 		if (retry_count == 0U) {
152 			/* Directly convert to psc to get psc_idx */
153 			VERBOSE("PSC transition timeout: psc_id=%d pd_id=%d\n",
154 				(ti_to_psc_drv_data(ti_get_drv_data(dev)))->psc_idx,
155 				ti_psc_pd_idx(dev, pd));
156 		}
157 	}
158 }
159 
pd_initiate(struct ti_device * dev,struct ti_psc_pd * pd)160 static void pd_initiate(struct ti_device *dev, struct ti_psc_pd *pd)
161 {
162 	uint32_t psc_ptcmd = PSC_PTCMD;
163 	ti_pd_idx_t psc_idx   = ti_psc_pd_idx(dev, pd);
164 
165 	/* power domain >= 32 uses PSC_PTCMD_H register */
166 	if (psc_idx >= 32U) {
167 		psc_ptcmd = PSC_PTCMD_H;
168 	}
169 
170 	/* Note: This is a state machine reg */
171 	psc_write(dev, (uint32_t) BIT(psc_idx % 32U), psc_ptcmd);
172 }
173 
174 /**
175  * psc_pd_clk_get() - Enable clocks necessary for power domain
176  * @data: The const data for the power domain
177  *
178  * This function calls ti_clk_get on each of the clocks listed
179  * under clock_dep for a given power domain. Some power domains
180  * require certain clocks to be running while the power domain
181  * is in transition or on.
182  */
psc_pd_clk_get(const struct ti_psc_pd_data * data)183 static void psc_pd_clk_get(const struct ti_psc_pd_data *data)
184 {
185 	uint32_t i;
186 
187 	for (i = 0U; i < ARRAY_SIZE(data->clock_dep); i++) {
188 		struct ti_clk *clkp = ti_clk_lookup(data->clock_dep[i]);
189 
190 		if (clkp != NULL) {
191 			(void) ti_clk_get(clkp);
192 		}
193 	}
194 }
195 
ti_psc_pd_get(struct ti_device * dev,struct ti_psc_pd * pd)196 void ti_psc_pd_get(struct ti_device *dev, struct ti_psc_pd *pd)
197 {
198 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
199 	uint32_t idx = ti_psc_pd_idx(dev, pd);
200 	uint32_t pdctl;
201 
202 	VERBOSE("PD_GET: psc_id=%d pd_id=%d use_count=%d\n",
203 		psc->psc_idx, idx, pd->use_count);
204 
205 	if ((pd->use_count++) != 0U) {
206 		return;
207 	}
208 
209 	if ((psc->pd_data[idx].flags & TI_PSC_PD_ALWAYSON) != 0U) {
210 		return;
211 	}
212 
213 	/* Verify any previous transitions have completed */
214 	ti_psc_pd_wait(dev, pd);
215 
216 	if ((psc->pd_data[idx].flags & TI_PSC_PD_DEPENDS) != 0U) {
217 		ti_psc_pd_get(dev, psc_idx2pd(psc,
218 					      (ti_pd_idx_t) psc->pd_data[idx].depends));
219 	}
220 
221 	psc_pd_clk_get(&psc->pd_data[idx]);
222 
223 	pdctl = psc_read(dev, psc_reg(PSC_PDCTL_BASE, idx));
224 
225 	if ((pdctl & PDCTL_STATE_MASK) != PDCTL_STATE_ON) {
226 		/* Avoid redundant power-up transitions */
227 		pdctl &= ~PDCTL_STATE_MASK;
228 		pdctl |= PDCTL_STATE_ON;
229 
230 		/* Note: This is a state machine reg */
231 		psc_write(dev, pdctl, psc_reg(PSC_PDCTL_BASE, idx));
232 
233 		pd_initiate(dev, pd);
234 		ti_psc_pd_wait(dev, pd);
235 	}
236 
237 	psc->data->pds_enabled |= (uint32_t) BIT(idx);
238 }
239 
240 /**
241  * psc_pd_clk_put() - Disable clocks necessary for power domain
242  * @data: The const data for the power domain
243  *
244  * This function calls ti_clk_put on each of the clocks listed
245  * under clock_dep for a given power domain. Some power domains
246  * require certain clocks to be running while the power domain
247  * is in transition or on.
248  */
psc_pd_clk_put(const struct ti_psc_pd_data * data)249 static void psc_pd_clk_put(const struct ti_psc_pd_data *data)
250 {
251 	uint32_t i;
252 
253 	for (i = 0U; i < ARRAY_SIZE(data->clock_dep); i++) {
254 		struct ti_clk *clkp = ti_clk_lookup(data->clock_dep[i]);
255 
256 		if (clkp != NULL) {
257 			ti_clk_put(clkp);
258 		}
259 	}
260 }
261 
ti_psc_pd_put(struct ti_device * dev,struct ti_psc_pd * pd)262 void ti_psc_pd_put(struct ti_device *dev, struct ti_psc_pd *pd)
263 {
264 	uint32_t pdctl;
265 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
266 	uint32_t idx = ti_psc_pd_idx(dev, pd);
267 
268 	VERBOSE("PD_PUT: psc_id=%d pd_id=%d use_count=%d\n",
269 		psc->psc_idx, idx, pd->use_count);
270 
271 	if ((--pd->use_count) != 0U) {
272 		return;
273 	}
274 
275 	if ((psc->pd_data[idx].flags & TI_PSC_PD_ALWAYSON) != 0U) {
276 		return;
277 	}
278 
279 	/* Verify any previous transitions have completed */
280 	ti_psc_pd_wait(dev, pd);
281 
282 	pdctl = psc_read(dev, psc_reg(PSC_PDCTL_BASE, idx));
283 	if ((pdctl & PDCTL_STATE_MASK) != PDCTL_STATE_OFF) {
284 		/* Avoid redundant power-up transitions */
285 		pdctl &= ~PDCTL_STATE_MASK;
286 		pdctl |= PDCTL_STATE_OFF;
287 		/* Note: This is a state machine reg */
288 		psc_write(dev, pdctl, psc_reg(PSC_PDCTL_BASE, idx));
289 
290 		pd_initiate(dev, pd);
291 		ti_psc_pd_wait(dev, pd);
292 	}
293 
294 	psc_pd_clk_put(&psc->pd_data[idx]);
295 
296 	if ((psc->pd_data[idx].flags & TI_PSC_PD_DEPENDS) != 0U) {
297 		ti_psc_pd_put(dev, psc_idx2pd(psc,
298 					      (ti_pd_idx_t) psc->pd_data[idx].depends));
299 	}
300 
301 	psc->data->pds_enabled &= ~((uint32_t) BIT(idx));
302 }
303 
304 /**
305  * lpsc_module_sync_state() - Sync the hardware state of a module with it's software state.
306  * @dev: The device struct for this PSC.
307  * @module: The module data for this module.
308  * @domain_reset: True if syncing just prior to a domain reset,
309  *                in which case actual transitions are avoided.
310  *
311  * This function examines the current state of a given LPSC module and
312  * compares it with the last programmed state. If there have been changes it
313  * programs the hardware appropriately. It also enables/disables any
314  * dependencies as necessary.
315  */
lpsc_module_sync_state(struct ti_device * dev,struct ti_lpsc_module * module,bool domain_reset)316 static void lpsc_module_sync_state(struct ti_device *dev, struct ti_lpsc_module *module,
317 				   bool domain_reset)
318 {
319 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
320 	uint32_t mdctl;
321 	uint32_t idx = ti_lpsc_module_idx(dev, module);
322 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
323 	struct ti_psc_pd *pd = psc_idx2pd(psc, (ti_pd_idx_t) data->powerdomain);
324 	uint8_t state;		     /* Target module state */
325 	uint8_t old_state;	     /* Original module state */
326 	bool new_mrst_ret;     /* MRST induced retention */
327 	bool old_msrt_ret;
328 	bool old_pwr;	       /* Power domain should be enabled */
329 	bool old_ret;	       /* Retention of any kind */
330 	bool old_en;	       /* Enabled (clocks running) */
331 	bool old_rst;
332 	bool new_pwr;
333 	bool new_ret;
334 	bool new_en;
335 	bool new_rst;
336 	bool transition;       /* A state transition is necessary */
337 	bool get_en;	       /* Moving from disabled to enabled */
338 	bool get_ret;	       /* Moving from off to retention */
339 	bool get_pwr;	       /* Moving from power domain off to on */
340 	bool put_en;	       /* Moving from enabled to disabled */
341 	bool put_ret;	       /* Moving from retention to off */
342 	bool put_pwr;	       /* Moving from power domain on to off */
343 
344 	/*
345 	 * Determine target state based on usage counts and module reset:
346 	 *
347 	 * +---------+-----------+-------+-------------------------------+
348 	 * + Enabled | Retention | Reset | State			 |
349 	 * +=========+===========+=======+===============================+
350 	 * | No	     | No	 | NA	 | SwRstDisabled (may power off) |
351 	 * +---------+-----------+-------+-------------------------------+
352 	 * | No	     | Yes	 | No	 | Disabled			 |
353 	 * +---------+-----------+-------+-------------------------------+
354 	 * | No	     | Yes	 | Yes	 | SwRstDisabled (powered-on)	 |
355 	 * +---------+-----------+-------+-------------------------------+
356 	 * | Yes     | NA	 | No	 | Enabled			 |
357 	 * +---------+-----------+-------+-------------------------------+
358 	 * | Yes     | NA	 | Yes	 | SyncReset			 |
359 	 * +---------+-----------+-------+-------------------------------+
360 	 */
361 	new_mrst_ret = false;
362 	if ((module->use_count == 0U) && (module->ret_count == 0U)) {
363 		state = MDSTAT_STATE_SWRSTDISABLE;
364 	} else if (module->use_count == 0U) {
365 		/* Retention enabled, but module disabled */
366 		if (module->mrst_active == 0U) {
367 			state = MDSTAT_STATE_DISABLE;
368 		} else {
369 			new_mrst_ret = true;
370 			state = MDSTAT_STATE_SWRSTDISABLE;
371 		}
372 	} else {
373 		/* Module enabled (retention setting is don't care) */
374 		if (module->mrst_active == 0U) {
375 			state = MDSTAT_STATE_ENABLE;
376 		} else {
377 			state = MDSTAT_STATE_SYNCRST;
378 		}
379 	}
380 
381 	/* Promote target state based on disallowed states */
382 	if ((state == MDSTAT_STATE_SWRSTDISABLE) &&
383 	    ((data->flags & TI_LPSC_NO_MODULE_RESET) != 0U)) {
384 		state = MDSTAT_STATE_DISABLE;
385 		new_mrst_ret = false;
386 	}
387 	if ((state == MDSTAT_STATE_DISABLE) && ((data->flags & TI_LPSC_NO_CLOCK_GATING) != 0U)) {
388 		state = MDSTAT_STATE_ENABLE;
389 	}
390 	if ((state == MDSTAT_STATE_SYNCRST) && ((data->flags & TI_LPSC_NO_MODULE_RESET) != 0U)) {
391 		state = MDSTAT_STATE_ENABLE;
392 	}
393 
394 	/* Track transition of old state to new state */
395 	old_state = module->sw_state;
396 	module->sw_state = state;
397 	old_msrt_ret = module->sw_mrst_ret;
398 	module->sw_mrst_ret =  new_mrst_ret;
399 
400 	/* Previous setting of retention, enable, and reset */
401 	old_ret = old_state != MDSTAT_STATE_SWRSTDISABLE;
402 	old_pwr = old_ret || old_msrt_ret;
403 	old_en = (old_state == MDSTAT_STATE_SYNCRST) || (old_state == MDSTAT_STATE_ENABLE);
404 	old_rst = (old_state != MDSTAT_STATE_ENABLE) && (old_state != MDSTAT_STATE_DISABLE);
405 
406 	/* New setting of retention, enable, and reset */
407 	new_ret = state != MDSTAT_STATE_SWRSTDISABLE;
408 	new_pwr = new_ret || new_mrst_ret;
409 	new_en = (state == MDSTAT_STATE_SYNCRST) || (state == MDSTAT_STATE_ENABLE);
410 	new_rst = (state != MDSTAT_STATE_ENABLE) && (state != MDSTAT_STATE_DISABLE);
411 
412 	/* Are we transitioning from no retention/enable to retention/enable? */
413 	get_ret = !old_ret && new_ret;
414 	get_pwr = !old_pwr && new_pwr;
415 	get_en = !old_en && new_en;
416 
417 	/* Are we transitioning from retention/enable to no retention/enable? */
418 	put_ret = old_ret && !new_ret;
419 	put_pwr = old_pwr && !new_pwr;
420 	put_en = old_en && !new_en;
421 
422 	/* Make sure our parent LPSC is enabled as necessary */
423 	if ((get_en || get_ret) && ((data->flags & TI_LPSC_DEPENDS) != 0UL)) {
424 		const struct ti_psc_drv_data *depends_psc = psc;
425 		struct ti_device *depends_dev = dev;
426 
427 		if (data->depends_psc_idx != psc->psc_idx) {
428 			depends_dev = ti_psc_lookup((ti_psc_idx_t) data->depends_psc_idx);
429 			depends_psc = ti_to_psc_drv_data(ti_get_drv_data(depends_dev));
430 		}
431 		if (depends_dev == NULL) {
432 			VERBOSE("ACTION FAIL: PSC_INVALID_DEP_DATA dep_pd_id=%d pd_id=%d pos=1\n",
433 				psc->psc_idx, data->depends_psc_idx);
434 		} else {
435 			/*
436 			 * Moving from a clock stop state to a clock enabled
437 			 * state or from a retention disable to a retention
438 			 * enabled state, bump the reference count on our
439 			 * dependency.
440 			 */
441 			lpsc_module_get_internal(depends_dev,
442 						 psc_idx2mod(depends_psc, data->depends),
443 						 get_en, get_ret);
444 		}
445 	}
446 
447 	if (old_rst && !new_rst) {
448 		/* Coming out of reset, bump the context loss count. */
449 		module->loss_count++;
450 	}
451 
452 	if (get_pwr) {
453 		/* Make sure pd is on. */
454 		ti_psc_pd_get(dev, pd);
455 	}
456 
457 	mdctl = psc_read(dev, psc_reg(PSC_MDCTL_BASE, idx));
458 	transition = (mdctl & MDSTAT_STATE_MASK) != state;
459 	if (transition && (domain_reset == false)) {
460 		/* Verify any previous transitions have completed */
461 		ti_psc_pd_wait(dev, pd);
462 
463 		mdctl &= ~MDSTAT_STATE_MASK;
464 		mdctl |= (uint32_t) state;
465 		/* Note: This is a state machine reg */
466 		psc_write(dev, mdctl, psc_reg(PSC_MDCTL_BASE, idx));
467 	}
468 
469 	if (domain_reset == false) {
470 		if (put_pwr) {
471 			/* Module is ready for power down, drop ref count on pd */
472 			ti_psc_pd_put(dev, pd);
473 			if ((pd->use_count != 0U) && transition) {
474 				/*
475 				 * If ti_psc_pd_put has a use count of zero, it already
476 				 * initaited the transition, otherwise we need to
477 				 * do the transition ourselves.
478 				 */
479 				pd_initiate(dev, pd);
480 			}
481 		} else if (transition) {
482 			/*
483 			 * Initiate transition
484 			 */
485 			pd_initiate(dev, pd);
486 		}
487 	}
488 
489 	/* Allow our parent LPSC to be disabled as necessary */
490 	if ((put_en || put_ret) && ((data->flags & TI_LPSC_DEPENDS) != 0UL)) {
491 		const struct ti_psc_drv_data *depends_psc = psc;
492 		struct ti_device *depends_dev = dev;
493 
494 		if (data->depends_psc_idx != psc->psc_idx) {
495 			depends_dev = ti_psc_lookup((ti_psc_idx_t) data->depends_psc_idx);
496 			depends_psc = ti_to_psc_drv_data(ti_get_drv_data(depends_dev));
497 		}
498 		if (depends_dev == NULL) {
499 			VERBOSE("ACTION FAIL: PSC_INVALID_DEP_DATA dep_pd_id=%d pd_id=%d pos=2\n",
500 				psc->psc_idx, data->depends_psc_idx);
501 		} else if ((domain_reset == true) && (depends_dev == dev)) {
502 			/* Ignore self dependencies during domain reset */
503 		} else {
504 			/*
505 			 * Moving from a clock enabled state to a clock stop
506 			 * state or from a retention enable to a retention
507 			 * disabled state, drop the reference count on our
508 			 * dependency.
509 			 */
510 			lpsc_module_put_internal(depends_dev,
511 						 psc_idx2mod(depends_psc, data->depends),
512 						 put_en, put_ret);
513 		}
514 	}
515 }
516 
ti_lpsc_module_get_state(struct ti_device * dev,struct ti_lpsc_module * module)517 uint32_t ti_lpsc_module_get_state(struct ti_device *dev, struct ti_lpsc_module *module)
518 {
519 	uint32_t idx = ti_lpsc_module_idx(dev, module);
520 	uint8_t state;
521 	uint32_t ret;
522 
523 	state = (uint8_t)(psc_read(dev, psc_reg(PSC_MDSTAT_BASE, idx)) & MDSTAT_STATE_MASK);
524 
525 	if (state == MDSTAT_STATE_SWRSTDISABLE) {
526 		ret = TI_LPSC_STATE_DISABLED;
527 	} else if ((state == MDSTAT_STATE_DISABLE) ||
528 		   (state == MDSTAT_STATE_ENABLE) ||
529 		   (state == MDSTAT_STATE_SYNCRST)) {
530 		ret = TI_LPSC_STATE_ENABLED;
531 	} else {
532 		ret = TI_LPSC_STATE_TRANSITIONING;
533 	}
534 
535 	return ret;
536 }
537 
ti_lpsc_module_set_reset_iso(struct ti_device * dev,struct ti_lpsc_module * module,bool enable)538 void ti_lpsc_module_set_reset_iso(struct ti_device *dev, struct ti_lpsc_module *module,
539 				  bool enable)
540 {
541 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
542 	uint32_t idx = ti_lpsc_module_idx(dev, module);
543 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
544 	bool is_enabled;
545 	uint32_t mdctl;
546 
547 	if (0U == (data->flags & TI_LPSC_HAS_RESET_ISO)) {
548 		return;
549 	}
550 
551 	mdctl = psc_read(dev, psc_reg(PSC_MDCTL_BASE, idx));
552 	is_enabled = (mdctl & MDCTL_RESET_ISO) != 0U;
553 
554 	if (enable != is_enabled) {
555 		if (enable) {
556 			mdctl |= MDCTL_RESET_ISO;
557 		} else {
558 			mdctl &= ~MDCTL_RESET_ISO;
559 		}
560 		/* Note: This is a state machine reg */
561 		psc_write(dev, mdctl, psc_reg(PSC_MDCTL_BASE, idx));
562 	}
563 }
564 
ti_lpsc_module_get_reset_iso(struct ti_device * dev,struct ti_lpsc_module * module)565 bool ti_lpsc_module_get_reset_iso(struct ti_device *dev, struct ti_lpsc_module *module)
566 {
567 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
568 	uint32_t idx = ti_lpsc_module_idx(dev, module);
569 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
570 	bool ret;
571 
572 	if (0U == (data->flags & TI_LPSC_HAS_RESET_ISO)) {
573 		ret = false;
574 	} else {
575 		ret = (psc_read(dev, psc_reg(PSC_MDCTL_BASE, idx)) & MDCTL_RESET_ISO) != 0U;
576 	}
577 	return ret;
578 }
579 
580 /* Does not bump context loss count */
ti_lpsc_module_set_local_reset(struct ti_device * dev,struct ti_lpsc_module * module,bool enable)581 void ti_lpsc_module_set_local_reset(struct ti_device *dev, struct ti_lpsc_module *module,
582 				    bool enable)
583 {
584 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
585 	uint32_t idx = ti_lpsc_module_idx(dev, module);
586 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
587 	bool is_enabled;
588 	uint32_t mdctl;
589 
590 	if (0U == (data->flags & TI_LPSC_HAS_LOCAL_RESET)) {
591 		return;
592 	}
593 
594 	mdctl = psc_read(dev, (uint32_t) psc_reg(PSC_MDCTL_BASE, idx));
595 	is_enabled = (mdctl & MDCTL_LRST) == 0U;
596 
597 	if (enable != is_enabled) {
598 		VERBOSE("SET_LOCAL_RESET: psc_id=%d lpsc_id=%d enable=%d\n",
599 			psc->psc_idx, idx, ((enable == true) ? 1U : 0U));
600 
601 		if (enable) {
602 			mdctl &= ~MDCTL_LRST;
603 		} else {
604 			mdctl |= MDCTL_LRST;
605 		}
606 
607 		/* Note: This is a state machine reg */
608 		psc_write(dev, mdctl, psc_reg(PSC_MDCTL_BASE, idx));
609 	}
610 }
611 
ti_lpsc_module_set_module_reset(struct ti_device * dev,struct ti_lpsc_module * module,bool enable)612 void ti_lpsc_module_set_module_reset(struct ti_device *dev, struct ti_lpsc_module *module,
613 				     bool enable)
614 {
615 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
616 	uint32_t idx = ti_lpsc_module_idx(dev, module);
617 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
618 	bool is_enabled;
619 
620 	is_enabled = module->mrst_active != 0U;
621 
622 	if ((enable != is_enabled) && ((data->flags & TI_LPSC_NO_MODULE_RESET) == 0U)) {
623 		VERBOSE("SET_MODULE_RESET/SET_LOCAL_RESET: psc_id=%d lpsc_id=%d enable=%d\n",
624 			psc->psc_idx, idx, (enable ? 1U : 0U));
625 
626 		if (enable) {
627 			module->mrst_active = 1U;
628 		} else {
629 			module->mrst_active = 0U;
630 		}
631 
632 		lpsc_module_sync_state(dev, module, false);
633 		ti_lpsc_module_wait(dev, module);
634 	}
635 }
636 
ti_lpsc_module_get_local_reset(struct ti_device * dev,struct ti_lpsc_module * module)637 bool ti_lpsc_module_get_local_reset(struct ti_device *dev, struct ti_lpsc_module *module)
638 {
639 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
640 	uint32_t idx = ti_lpsc_module_idx(dev, module);
641 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
642 	bool ret;
643 
644 	if (0U == (data->flags & TI_LPSC_HAS_LOCAL_RESET)) {
645 		ret = false;
646 	} else {
647 		ret = (psc_read(dev, (uint32_t) psc_reg(PSC_MDCTL_BASE, idx)) & MDCTL_LRST) == 0U;
648 	}
649 	return ret;
650 }
651 
ti_lpsc_module_get_module_reset(struct ti_device * dev __unused,const struct ti_lpsc_module * module)652 bool ti_lpsc_module_get_module_reset(struct ti_device *dev __unused,
653 				     const struct ti_lpsc_module *module)
654 {
655 	return module->mrst_active == 1U;
656 }
657 
ti_lpsc_module_wait(struct ti_device * dev,struct ti_lpsc_module * module)658 void ti_lpsc_module_wait(struct ti_device *dev, struct ti_lpsc_module *module)
659 {
660 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
661 	uint32_t idx = ti_lpsc_module_idx(dev, module);
662 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
663 	struct ti_psc_pd *pd = psc_idx2pd(psc, (ti_pd_idx_t) data->powerdomain);
664 	uint32_t retry_count = PSC_TRANSITION_RETRY_COUNT;
665 
666 	ti_psc_pd_wait(dev, pd);
667 
668 	while ((((psc_read(dev, psc_reg(PSC_MDSTAT_BASE, idx))) & MDSTAT_BUSY_MASK) != 0U) &&
669 	       (retry_count > 0U)) {
670 		udelay(PSC_TRANSITION_DELAY_US);
671 		retry_count--;
672 	}
673 	if (retry_count == 0U) {
674 		VERBOSE("PSC module transition timeout: psc_id=%d lpsc_id=%d\n",
675 			psc->psc_idx, idx);
676 	}
677 
678 	/* Only wait for reset to complete if module is in use */
679 	if ((module->use_count) != 0U) {
680 		uint32_t mask = MDSTAT_MRSTDONE;
681 
682 		retry_count = PSC_TRANSITION_RETRY_COUNT;
683 
684 		if (!ti_lpsc_module_get_local_reset(dev, module)) {
685 			mask |= MDSTAT_LRSTDONE;
686 		}
687 		while ((0U == ((psc_read(dev, psc_reg(PSC_MDSTAT_BASE, idx))) & mask)) &&
688 		       (retry_count > 0U)) {
689 			udelay(PSC_TRANSITION_DELAY_US);
690 			retry_count--;
691 		}
692 		if (retry_count == 0U) {
693 			VERBOSE("PSC module reset done timeout: psc_id=%d lpsc_id=%d\n",
694 				psc->psc_idx, idx);
695 		}
696 	}
697 }
698 
lpsc_module_clk_get(struct ti_device * dev,struct ti_lpsc_module * mod)699 static void lpsc_module_clk_get(struct ti_device *dev, struct ti_lpsc_module *mod)
700 {
701 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
702 	uint32_t idx = ti_lpsc_module_idx(dev, mod);
703 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
704 
705 	/* Get clock dependency - currently only one clock supported */
706 	if (data->clock_dep[0] != 0U) {
707 		struct ti_clk *clkp = ti_clk_lookup(data->clock_dep[0]);
708 
709 		if (clkp != NULL) {
710 			(void) ti_clk_get(clkp);
711 		}
712 	}
713 }
714 
715 /**
716  * lpsc_module_clk_put() - Disable clocks necessary for LPSC module
717  * @dev: The device for this PSC.
718  * @mod: The const data for the LPSC module.
719  * @wait: True to wait for the module to complete transitioning
720  *        before disabling clocks. Set to false when called as
721  *        part of a domain reset.
722  *
723  * This function calls ti_clk_put on each of the clocks listed
724  * under clock_dep for a given LPSC module. Some modules
725  * require certain clocks to be running while the module
726  * is in transition or on.
727  */
lpsc_module_clk_put(struct ti_device * dev,struct ti_lpsc_module * mod,bool wait)728 static void lpsc_module_clk_put(struct ti_device *dev, struct ti_lpsc_module *mod,
729 				bool wait)
730 {
731 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
732 	uint32_t idx = ti_lpsc_module_idx(dev, mod);
733 	const struct ti_lpsc_module_data *data = &psc->mod_data[idx];
734 
735 	/* Put clock dependency - currently only one clock supported */
736 	if (data->clock_dep[0] != 0U) {
737 		struct ti_clk *clkp = ti_clk_lookup(data->clock_dep[0]);
738 
739 		if (clkp != NULL) {
740 			/*
741 			 * We have to wait for the transition to complete
742 			 * taking a clock away.
743 			 */
744 			if (wait) {
745 				ti_lpsc_module_wait(dev, mod);
746 			}
747 			ti_clk_put(clkp);
748 		}
749 	}
750 }
751 
lpsc_module_get_internal(struct ti_device * dev,struct ti_lpsc_module * module,bool use,bool ret)752 static void lpsc_module_get_internal(struct ti_device *dev,
753 				     struct ti_lpsc_module *module,
754 				     bool use, bool ret)
755 {
756 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
757 	bool modify = false;
758 
759 	if (use) {
760 		VERBOSE("MODULE_GET: psc_id=%d lpsc_id=%d use_count=%d\n",
761 			psc->psc_idx, ti_lpsc_module_idx(dev, module),
762 			module->use_count);
763 		module->use_count++;
764 		if (module->use_count == 1U) {
765 			lpsc_module_clk_get(dev, module);
766 			modify = true;
767 		}
768 	}
769 
770 	if (ret) {
771 		VERBOSE("RETENTION_GET: psc_id=%d lpsc_id=%d ret_count=%d\n",
772 			psc->psc_idx, ti_lpsc_module_idx(dev, module),
773 			module->ret_count);
774 		module->ret_count++;
775 		if (module->ret_count == 1U) {
776 			lpsc_module_clk_get(dev, module);
777 			modify = true;
778 		}
779 	}
780 
781 	if (modify) {
782 		lpsc_module_sync_state(dev, module, false);
783 		ti_lpsc_module_wait(dev, module);
784 	}
785 }
786 
lpsc_module_put_internal(struct ti_device * dev,struct ti_lpsc_module * module,bool use,bool ret)787 static void lpsc_module_put_internal(struct ti_device *dev,
788 				     struct ti_lpsc_module *module,
789 				     bool use, bool ret)
790 {
791 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
792 	bool modify = false;
793 
794 	if (use) {
795 		VERBOSE("MODULE_PUT: psc_id=%d lpsc_id=%d use_count=%d\n",
796 			psc->psc_idx, ti_lpsc_module_idx(dev, module),
797 			module->use_count);
798 		module->use_count--;
799 		if (module->use_count == 0U) {
800 			modify = true;
801 		}
802 	}
803 
804 	if (ret) {
805 		VERBOSE("RETENTION_PUT: psc_id=%d lpsc_id=%d ret_count=%d\n",
806 			psc->psc_idx, ti_lpsc_module_idx(dev, module),
807 			module->ret_count);
808 		module->ret_count--;
809 		if (module->ret_count == 0U) {
810 			modify = true;
811 		}
812 	}
813 
814 	if (modify) {
815 		lpsc_module_sync_state(dev, module, false);
816 		if ((module->use_count == 0U) && use) {
817 			lpsc_module_clk_put(dev, module, true);
818 		}
819 		if ((module->ret_count == 0U) && ret) {
820 			lpsc_module_clk_put(dev, module, true);
821 		}
822 	}
823 }
824 
ti_lpsc_module_get(struct ti_device * dev,struct ti_lpsc_module * module)825 void ti_lpsc_module_get(struct ti_device *dev, struct ti_lpsc_module *module)
826 {
827 	lpsc_module_get_internal(dev, module, true, false);
828 }
829 
ti_lpsc_module_put(struct ti_device * dev,struct ti_lpsc_module * module)830 void ti_lpsc_module_put(struct ti_device *dev, struct ti_lpsc_module *module)
831 {
832 	lpsc_module_put_internal(dev, module, true, false);
833 }
834 
ti_lpsc_module_ret_get(struct ti_device * dev,struct ti_lpsc_module * module)835 void ti_lpsc_module_ret_get(struct ti_device *dev, struct ti_lpsc_module *module)
836 {
837 	lpsc_module_get_internal(dev, module, false, true);
838 }
839 
ti_lpsc_module_ret_put(struct ti_device * dev,struct ti_lpsc_module * module)840 void ti_lpsc_module_ret_put(struct ti_device *dev, struct ti_lpsc_module *module)
841 {
842 	lpsc_module_put_internal(dev, module, false, true);
843 }
844 
845 /* Drop power up ref counts */
psc_pd_drop_pwr_up_ref(struct ti_device * dev)846 static void psc_pd_drop_pwr_up_ref(struct ti_device *dev)
847 {
848 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
849 	ti_pd_idx_t idx;
850 
851 	for (idx = 0U; idx < psc->pd_count; idx++) {
852 		struct ti_psc_pd *pd = psc_idx2pd(psc, idx);
853 
854 		if ((pd->pwr_up_enabled) != false) {
855 			pd->pwr_up_enabled = false;
856 			ti_psc_pd_put(dev, pd);
857 		}
858 	}
859 }
860 
ti_psc_lookup(ti_psc_idx_t id)861 struct ti_device *ti_psc_lookup(ti_psc_idx_t id)
862 {
863 	struct ti_device *dev;
864 
865 	dev = psc_devs;
866 	while (dev != NULL) {
867 		const struct ti_psc_drv_data *psc;
868 
869 		psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
870 
871 		if ((ti_psc_idx_t) psc->psc_idx == id) {
872 			break;
873 		}
874 
875 		dev = psc->data->next;
876 	}
877 
878 	return dev;
879 }
880 
ti_psc_lookup_pd(struct ti_device * dev,ti_pd_idx_t id)881 struct ti_psc_pd *ti_psc_lookup_pd(struct ti_device *dev, ti_pd_idx_t id)
882 {
883 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
884 	struct ti_psc_pd *pd = NULL;
885 
886 	if ((id < psc->pd_count) &&
887 	    ((psc->pd_data[id].flags & TI_PSC_PD_EXISTS) != 0U)) {
888 		pd = psc_idx2pd(psc, id);
889 	}
890 	return pd;
891 }
892 
ti_psc_lookup_lpsc(struct ti_device * dev,ti_pd_idx_t id)893 struct ti_lpsc_module *ti_psc_lookup_lpsc(struct ti_device *dev, ti_pd_idx_t id)
894 {
895 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
896 	struct ti_lpsc_module *mod = NULL;
897 
898 	if ((id < psc->module_count) &&
899 	    ((psc->mod_data[id].flags & TI_LPSC_MODULE_EXISTS) != 0U)) {
900 		mod = psc_idx2mod(psc, id);
901 	}
902 	return mod;
903 }
904 
905 /**
906  * psc_initialize_pds() - Initialize all the PSC powerdomains.
907  *
908  * This initializes the powerdomains by waiting for them to finish any
909  * active transitions, reading their state, and synchronizing it with the
910  * in memory state.
911  */
psc_initialize_pds(struct ti_device * dev)912 static void psc_initialize_pds(struct ti_device *dev)
913 {
914 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
915 	ti_pd_idx_t idx;
916 	struct ti_psc_pd *pd;
917 	uint8_t state;
918 
919 	/* First pass, find out which domains are on */
920 	for (idx = 0U; idx < psc->pd_count; idx++) {
921 		pd = psc_idx2pd(psc, idx);
922 
923 		if (0U == (psc->pd_data[idx].flags & TI_PSC_PD_EXISTS)) {
924 			continue;
925 		}
926 
927 		VERBOSE("PD_INIT: psc_id=%d lpsc_id=%d\n", psc->psc_idx, idx);
928 		ti_psc_pd_wait(dev, pd);
929 		state = (uint8_t) psc_read(dev, psc_reg(PSC_PDSTAT_BASE, (uint32_t) idx));
930 		state &= (uint8_t) PDSTAT_STATE_MASK;
931 
932 		/*
933 		 * Mark a PD as power up in use so we don't power everything
934 		 * off before PMMC startup is complete
935 		 */
936 		pd->pwr_up_enabled =  ((state == PDCTL_STATE_ON) ||
937 				       ((psc->pd_data[idx].flags & TI_PSC_PD_ALWAYSON) != 0U));
938 	}
939 
940 	/* Second pass, sync use count and impossible hardware states */
941 	for (idx = 0U; idx < psc->pd_count; idx++) {
942 		pd = psc_idx2pd(psc, idx);
943 
944 		if (pd->pwr_up_enabled != false) {
945 			ti_psc_pd_get(dev, pd);
946 		}
947 	}
948 }
949 
950 /**
951  * psc_uninitialize_pds() - Unititialize all the power domains of a PSC.
952  * @dev: The device associated with this PSC.
953  *
954  * This sets all the power domains in a PSC to a pre-initialized state
955  * in preparation for the reset of it's reset domain. Because power
956  * domains can only have dependencies on other domains within the same
957  * PSC, this just means ensuring that the clock references are dropped.
958  */
psc_uninitialize_pds(struct ti_device * dev)959 static void psc_uninitialize_pds(struct ti_device *dev)
960 {
961 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
962 	ti_pd_idx_t idx;
963 
964 	for (idx = 0U; idx < psc->pd_count; idx++) {
965 		struct ti_psc_pd *pd = psc_idx2pd(psc, idx);
966 
967 		if (((psc->pd_data[idx].flags & TI_PSC_PD_EXISTS) != 0U) &&
968 		    (pd->use_count != 0U) &&
969 		    ((psc->pd_data[idx].flags & TI_PSC_PD_ALWAYSON) == 0U)) {
970 			psc_pd_clk_put(&psc->pd_data[idx]);
971 		}
972 
973 		pd->use_count = 0U;
974 		pd->pwr_up_enabled = false;
975 	}
976 
977 	psc->data->pds_enabled = 0U;
978 }
979 
980 /* Drop power up ref counts */
ti_psc_drop_pwr_up_ref(void)981 void ti_psc_drop_pwr_up_ref(void)
982 {
983 	struct ti_device *dev = psc_devs;
984 	const struct ti_psc_drv_data *psc;
985 	ti_lpsc_idx_t idx;
986 
987 	while (dev != NULL) {
988 		psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
989 
990 		for (idx = 0U; idx < psc->module_count; idx++) {
991 			struct ti_lpsc_module *mod = psc_idx2mod(psc, idx);
992 
993 			if (mod->pwr_up_enabled != 0U) {
994 				mod->pwr_up_enabled = 0U;
995 				ti_lpsc_module_put(dev, mod);
996 			}
997 			if (mod->pwr_up_ret != 0U) {
998 				mod->pwr_up_ret = 0U;
999 				ti_lpsc_module_ret_put(dev, mod);
1000 			}
1001 		}
1002 		dev = psc->data->next;
1003 	}
1004 }
1005 
1006 /**
1007  * psc_initialize_modules() - Initialize all the PSC modules.
1008  *
1009  * This initializes the modules by waiting for them to finish any active
1010  * transitions and reading their state.
1011  */
psc_initialize_modules(struct ti_device * dev)1012 static void psc_initialize_modules(struct ti_device *dev)
1013 {
1014 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
1015 	ti_lpsc_idx_t idx;
1016 
1017 	/* First pass, find out which modules are enabled */
1018 	for (idx = 0U; idx < psc->module_count; idx++) {
1019 		struct ti_lpsc_module *mod = psc_idx2mod(psc, idx);
1020 		uint32_t val;
1021 		uint32_t retry_count;
1022 
1023 		if (0U == (psc->mod_data[idx].flags & TI_LPSC_MODULE_EXISTS)) {
1024 			continue;
1025 		}
1026 
1027 		retry_count = PSC_TRANSITION_RETRY_COUNT;
1028 		while ((((psc_read(dev, psc_reg(PSC_MDSTAT_BASE,
1029 						(uint32_t) idx))) &
1030 						MDSTAT_BUSY_MASK) != 0U) &&
1031 						(retry_count > 0U)) {
1032 			udelay(PSC_TRANSITION_DELAY_US);
1033 			retry_count--;
1034 		}
1035 		if (retry_count == 0U) {
1036 			VERBOSE("PSC module busy timeout: psc_id=%d lpsc_id=%d\n",
1037 				psc->psc_idx, idx);
1038 		}
1039 
1040 		val = psc_read(dev, psc_reg(PSC_MDSTAT_BASE, (uint32_t) idx));
1041 		val &= MDSTAT_STATE_MASK;
1042 
1043 		/* Ref count as if we are moving out of off state */
1044 		mod->sw_state = MDSTAT_STATE_SWRSTDISABLE;
1045 		mod->sw_mrst_ret = false;
1046 
1047 		if ((val == MDSTAT_STATE_ENABLE) || (val == MDSTAT_STATE_SYNCRST)) {
1048 			mod->pwr_up_enabled = 1U;
1049 			mod->pwr_up_ret = 1U;
1050 		} else if (val == MDSTAT_STATE_DISABLE) {
1051 			mod->pwr_up_enabled = 0U;
1052 			mod->pwr_up_ret = 1U;
1053 		} else if (val == MDSTAT_STATE_SWRSTDISABLE) {
1054 			mod->pwr_up_enabled = 0U;
1055 			mod->pwr_up_ret = 0U;
1056 		} else {
1057 			/* Invalid initial state, try turning everything on */
1058 			mod->pwr_up_ret = 1U;
1059 			mod->pwr_up_enabled = 1U;
1060 		}
1061 	}
1062 }
1063 
1064 /**
1065  * psc_initialize_modules_finish() - Finish initializing all the PSC modules.
1066  *
1067  * This finishes the initialization of modules by synchronizing their
1068  * state with the in memory state.
1069  */
psc_initialize_modules_finish(struct ti_device * dev)1070 static void psc_initialize_modules_finish(struct ti_device *dev)
1071 {
1072 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
1073 	ti_lpsc_idx_t idx;
1074 
1075 	/* Second pass, sync ref counts */
1076 	for (idx = 0U; idx < psc->module_count; idx++) {
1077 		struct ti_lpsc_module *mod = psc_idx2mod(psc, idx);
1078 
1079 		lpsc_module_get_internal(dev, mod, (mod->pwr_up_enabled != 0U),
1080 					 (mod->pwr_up_ret != 0U));
1081 	}
1082 
1083 	psc_pd_drop_pwr_up_ref(dev);
1084 }
1085 
1086 /**
1087  * psc_uninitialize_modules() - Unititialize all the LPSC modules of a PSC.
1088  * @dev: The device associated with this PSC.
1089  *
1090  * This sets all the LPSC modules in a PSC to a pre-initialized state
1091  * in preparation for the reset of it's reset domain. Because LPSC
1092  * modules can have dependencies on other domains, those dependencies
1093  * need to be droppe as appropriate. Any clock dependencies are also
1094  * dropped.
1095  */
psc_uninitialize_modules(struct ti_device * dev)1096 static void psc_uninitialize_modules(struct ti_device *dev)
1097 {
1098 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
1099 	ti_lpsc_idx_t idx;
1100 
1101 	/* First pass, find out which modules are enabled */
1102 	for (idx = 0U; idx < psc->module_count; idx++) {
1103 		struct ti_lpsc_module *mod = psc_idx2mod(psc, idx);
1104 
1105 		if ((psc->mod_data[idx].flags & TI_LPSC_MODULE_EXISTS) == 0U) {
1106 			continue;
1107 		}
1108 
1109 		if (mod->use_count != 0U) {
1110 			lpsc_module_clk_put(dev, mod, false);
1111 		}
1112 		if (mod->ret_count != 0U) {
1113 			lpsc_module_clk_put(dev, mod, false);
1114 		}
1115 
1116 		mod->use_count = 0U;
1117 		mod->ret_count = 0U;
1118 		mod->pwr_up_enabled = 0U;
1119 		mod->pwr_up_ret = 0U;
1120 
1121 		lpsc_module_sync_state(dev, mod, true);
1122 		mod->sw_state = MDSTAT_STATE_SWRSTDISABLE;
1123 		mod->sw_mrst_ret = false;
1124 	}
1125 }
1126 
1127 /**
1128  * psc_module_deps_ready() - Check if PSCs we depend on have completed their initial config
1129  *
1130  * PSCs can have domains that depend on domains in other PSCs. We break up
1131  * initialization into two stages because PSCs can have cross dependencies.
1132  * Once all PSCs in a cross dependency set have completed, each one can
1133  * move to the second stage of initialization. We use the -EAGAIN system
1134  * to manage this.
1135  *
1136  * Return: 0 on success, <0 on failure.
1137  */
psc_module_deps_ready(struct ti_device * dev)1138 static int32_t psc_module_deps_ready(struct ti_device *dev)
1139 {
1140 	const struct ti_psc_drv_data *psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
1141 	uint32_t idx;
1142 	ti_psc_idx_t id;
1143 
1144 	for (idx = 0; idx < psc->module_count; idx++) {
1145 		if (((psc->mod_data[idx].flags & TI_LPSC_DEPENDS) != 0UL) &&
1146 		    (psc->mod_data[idx].depends_psc_idx != psc->psc_idx)) {
1147 			id = (ti_psc_idx_t) psc->mod_data[idx].depends_psc_idx;
1148 			if (ti_psc_lookup(id) == NULL) {
1149 				return -EAGAIN;
1150 			}
1151 		}
1152 	}
1153 
1154 	return 0;
1155 }
1156 
psc_pre_init(struct ti_device * dev)1157 static int32_t psc_pre_init(struct ti_device *dev)
1158 {
1159 	const struct ti_drv_data *data;
1160 	const struct ti_psc_drv_data *psc;
1161 
1162 	data = ti_get_drv_data(dev);
1163 	psc = ti_to_psc_drv_data(data);
1164 
1165 	/* Only perform initial configuration if it has not been completed */
1166 	if (ti_psc_lookup((ti_psc_idx_t) psc->psc_idx) == NULL) {
1167 		if (psc->base == 0U) {
1168 			return -EINVAL;
1169 		}
1170 
1171 		psc_initialize_pds(dev);
1172 		psc_initialize_modules(dev);
1173 
1174 		/* This marks initial config as complete */
1175 		psc->data->next = psc_devs;
1176 		psc_devs = dev;
1177 	}
1178 
1179 	return 0;
1180 }
1181 
psc_post_init(struct ti_device * dev)1182 static int32_t psc_post_init(struct ti_device *dev)
1183 {
1184 	int32_t ret;
1185 
1186 	ret = psc_module_deps_ready(dev);
1187 
1188 	if (ret == 0) {
1189 		psc_initialize_modules_finish(dev);
1190 	}
1191 
1192 	return ret;
1193 }
1194 
1195 /**
1196  * psc_uninit() - Uninitialize this PSC.
1197  * @dev: The device associated with this PSC.
1198  *
1199  * Perform the steps necessary to bring this PSC back to a pre-init state.
1200  * This is performed before a reset domain reset so that the PSC can be
1201  * re-initialized after the reset is complete.
1202  */
psc_uninit(struct ti_device * dev)1203 static void psc_uninit(struct ti_device *dev)
1204 {
1205 	const struct ti_psc_drv_data *psc;
1206 	struct ti_device *curr_dev;
1207 	struct ti_device **last_dev;
1208 
1209 	psc = ti_to_psc_drv_data(ti_get_drv_data(dev));
1210 
1211 	psc_uninitialize_modules(dev);
1212 	psc_uninitialize_pds(dev);
1213 
1214 	/* Remove from list of uninitialized PSCs */
1215 	curr_dev = psc_devs;
1216 	last_dev = &psc_devs;
1217 	while ((curr_dev != NULL) && (curr_dev != dev)) {
1218 		const struct ti_psc_drv_data *curr_psc;
1219 
1220 		curr_psc = ti_to_psc_drv_data(ti_get_drv_data(curr_dev));
1221 		curr_dev = curr_psc->data->next;
1222 		last_dev = &curr_psc->data->next;
1223 	}
1224 	if (curr_dev == dev) {
1225 		*last_dev = psc->data->next;
1226 		psc->data->next = NULL;
1227 	}
1228 }
1229 
1230 const struct ti_drv psc_drv = {
1231 	.pre_init	= psc_pre_init,
1232 	.post_init	= psc_post_init,
1233 	.uninit		= psc_uninit,
1234 };
1235