xref: /OK3568_Linux_fs/kernel/drivers/remoteproc/omap_remoteproc.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * OMAP Remote Processor driver
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (C) 2011-2020 Texas Instruments Incorporated - http://www.ti.com/
6*4882a593Smuzhiyun  * Copyright (C) 2011 Google, Inc.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  * Ohad Ben-Cohen <ohad@wizery.com>
9*4882a593Smuzhiyun  * Brian Swetland <swetland@google.com>
10*4882a593Smuzhiyun  * Fernando Guzman Lugo <fernando.lugo@ti.com>
11*4882a593Smuzhiyun  * Mark Grosen <mgrosen@ti.com>
12*4882a593Smuzhiyun  * Suman Anna <s-anna@ti.com>
13*4882a593Smuzhiyun  * Hari Kanigeri <h-kanigeri2@ti.com>
14*4882a593Smuzhiyun  */
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #include <linux/kernel.h>
17*4882a593Smuzhiyun #include <linux/module.h>
18*4882a593Smuzhiyun #include <linux/clk.h>
19*4882a593Smuzhiyun #include <linux/clk/ti.h>
20*4882a593Smuzhiyun #include <linux/err.h>
21*4882a593Smuzhiyun #include <linux/io.h>
22*4882a593Smuzhiyun #include <linux/of_device.h>
23*4882a593Smuzhiyun #include <linux/of_reserved_mem.h>
24*4882a593Smuzhiyun #include <linux/platform_device.h>
25*4882a593Smuzhiyun #include <linux/pm_runtime.h>
26*4882a593Smuzhiyun #include <linux/dma-mapping.h>
27*4882a593Smuzhiyun #include <linux/interrupt.h>
28*4882a593Smuzhiyun #include <linux/remoteproc.h>
29*4882a593Smuzhiyun #include <linux/mailbox_client.h>
30*4882a593Smuzhiyun #include <linux/omap-iommu.h>
31*4882a593Smuzhiyun #include <linux/omap-mailbox.h>
32*4882a593Smuzhiyun #include <linux/regmap.h>
33*4882a593Smuzhiyun #include <linux/mfd/syscon.h>
34*4882a593Smuzhiyun #include <linux/reset.h>
35*4882a593Smuzhiyun #include <clocksource/timer-ti-dm.h>
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun #include <linux/platform_data/dmtimer-omap.h>
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun #include "omap_remoteproc.h"
40*4882a593Smuzhiyun #include "remoteproc_internal.h"
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun /* default auto-suspend delay (ms) */
43*4882a593Smuzhiyun #define DEFAULT_AUTOSUSPEND_DELAY		10000
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun /**
46*4882a593Smuzhiyun  * struct omap_rproc_boot_data - boot data structure for the DSP omap rprocs
47*4882a593Smuzhiyun  * @syscon: regmap handle for the system control configuration module
48*4882a593Smuzhiyun  * @boot_reg: boot register offset within the @syscon regmap
49*4882a593Smuzhiyun  * @boot_reg_shift: bit-field shift required for the boot address value in
50*4882a593Smuzhiyun  *		    @boot_reg
51*4882a593Smuzhiyun  */
52*4882a593Smuzhiyun struct omap_rproc_boot_data {
53*4882a593Smuzhiyun 	struct regmap *syscon;
54*4882a593Smuzhiyun 	unsigned int boot_reg;
55*4882a593Smuzhiyun 	unsigned int boot_reg_shift;
56*4882a593Smuzhiyun };
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun /**
59*4882a593Smuzhiyun  * struct omap_rproc_mem - internal memory structure
60*4882a593Smuzhiyun  * @cpu_addr: MPU virtual address of the memory region
61*4882a593Smuzhiyun  * @bus_addr: bus address used to access the memory region
62*4882a593Smuzhiyun  * @dev_addr: device address of the memory region from DSP view
63*4882a593Smuzhiyun  * @size: size of the memory region
64*4882a593Smuzhiyun  */
65*4882a593Smuzhiyun struct omap_rproc_mem {
66*4882a593Smuzhiyun 	void __iomem *cpu_addr;
67*4882a593Smuzhiyun 	phys_addr_t bus_addr;
68*4882a593Smuzhiyun 	u32 dev_addr;
69*4882a593Smuzhiyun 	size_t size;
70*4882a593Smuzhiyun };
71*4882a593Smuzhiyun 
72*4882a593Smuzhiyun /**
73*4882a593Smuzhiyun  * struct omap_rproc_timer - data structure for a timer used by a omap rproc
74*4882a593Smuzhiyun  * @odt: timer pointer
75*4882a593Smuzhiyun  * @timer_ops: OMAP dmtimer ops for @odt timer
76*4882a593Smuzhiyun  * @irq: timer irq
77*4882a593Smuzhiyun  */
78*4882a593Smuzhiyun struct omap_rproc_timer {
79*4882a593Smuzhiyun 	struct omap_dm_timer *odt;
80*4882a593Smuzhiyun 	const struct omap_dm_timer_ops *timer_ops;
81*4882a593Smuzhiyun 	int irq;
82*4882a593Smuzhiyun };
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun /**
85*4882a593Smuzhiyun  * struct omap_rproc - omap remote processor state
86*4882a593Smuzhiyun  * @mbox: mailbox channel handle
87*4882a593Smuzhiyun  * @client: mailbox client to request the mailbox channel
88*4882a593Smuzhiyun  * @boot_data: boot data structure for setting processor boot address
89*4882a593Smuzhiyun  * @mem: internal memory regions data
90*4882a593Smuzhiyun  * @num_mems: number of internal memory regions
91*4882a593Smuzhiyun  * @num_timers: number of rproc timer(s)
92*4882a593Smuzhiyun  * @num_wd_timers: number of rproc watchdog timers
93*4882a593Smuzhiyun  * @timers: timer(s) info used by rproc
94*4882a593Smuzhiyun  * @autosuspend_delay: auto-suspend delay value to be used for runtime pm
95*4882a593Smuzhiyun  * @need_resume: if true a resume is needed in the system resume callback
96*4882a593Smuzhiyun  * @rproc: rproc handle
97*4882a593Smuzhiyun  * @reset: reset handle
98*4882a593Smuzhiyun  * @pm_comp: completion primitive to sync for suspend response
99*4882a593Smuzhiyun  * @fck: functional clock for the remoteproc
100*4882a593Smuzhiyun  * @suspend_acked: state machine flag to store the suspend request ack
101*4882a593Smuzhiyun  */
102*4882a593Smuzhiyun struct omap_rproc {
103*4882a593Smuzhiyun 	struct mbox_chan *mbox;
104*4882a593Smuzhiyun 	struct mbox_client client;
105*4882a593Smuzhiyun 	struct omap_rproc_boot_data *boot_data;
106*4882a593Smuzhiyun 	struct omap_rproc_mem *mem;
107*4882a593Smuzhiyun 	int num_mems;
108*4882a593Smuzhiyun 	int num_timers;
109*4882a593Smuzhiyun 	int num_wd_timers;
110*4882a593Smuzhiyun 	struct omap_rproc_timer *timers;
111*4882a593Smuzhiyun 	int autosuspend_delay;
112*4882a593Smuzhiyun 	bool need_resume;
113*4882a593Smuzhiyun 	struct rproc *rproc;
114*4882a593Smuzhiyun 	struct reset_control *reset;
115*4882a593Smuzhiyun 	struct completion pm_comp;
116*4882a593Smuzhiyun 	struct clk *fck;
117*4882a593Smuzhiyun 	bool suspend_acked;
118*4882a593Smuzhiyun };
119*4882a593Smuzhiyun 
120*4882a593Smuzhiyun /**
121*4882a593Smuzhiyun  * struct omap_rproc_mem_data - memory definitions for an omap remote processor
122*4882a593Smuzhiyun  * @name: name for this memory entry
123*4882a593Smuzhiyun  * @dev_addr: device address for the memory entry
124*4882a593Smuzhiyun  */
125*4882a593Smuzhiyun struct omap_rproc_mem_data {
126*4882a593Smuzhiyun 	const char *name;
127*4882a593Smuzhiyun 	const u32 dev_addr;
128*4882a593Smuzhiyun };
129*4882a593Smuzhiyun 
130*4882a593Smuzhiyun /**
131*4882a593Smuzhiyun  * struct omap_rproc_dev_data - device data for the omap remote processor
132*4882a593Smuzhiyun  * @device_name: device name of the remote processor
133*4882a593Smuzhiyun  * @mems: memory definitions for this remote processor
134*4882a593Smuzhiyun  */
135*4882a593Smuzhiyun struct omap_rproc_dev_data {
136*4882a593Smuzhiyun 	const char *device_name;
137*4882a593Smuzhiyun 	const struct omap_rproc_mem_data *mems;
138*4882a593Smuzhiyun };
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun  * omap_rproc_request_timer() - request a timer for a remoteproc
142*4882a593Smuzhiyun  * @dev: device requesting the timer
143*4882a593Smuzhiyun  * @np: device node pointer to the desired timer
144*4882a593Smuzhiyun  * @timer: handle to a struct omap_rproc_timer to return the timer handle
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * This helper function is used primarily to request a timer associated with
147*4882a593Smuzhiyun  * a remoteproc. The returned handle is stored in the .odt field of the
148*4882a593Smuzhiyun  * @timer structure passed in, and is used to invoke other timer specific
149*4882a593Smuzhiyun  * ops (like starting a timer either during device initialization or during
150*4882a593Smuzhiyun  * a resume operation, or for stopping/freeing a timer).
151*4882a593Smuzhiyun  *
152*4882a593Smuzhiyun  * Return: 0 on success, otherwise an appropriate failure
153*4882a593Smuzhiyun  */
omap_rproc_request_timer(struct device * dev,struct device_node * np,struct omap_rproc_timer * timer)154*4882a593Smuzhiyun static int omap_rproc_request_timer(struct device *dev, struct device_node *np,
155*4882a593Smuzhiyun 				    struct omap_rproc_timer *timer)
156*4882a593Smuzhiyun {
157*4882a593Smuzhiyun 	int ret;
158*4882a593Smuzhiyun 
159*4882a593Smuzhiyun 	timer->odt = timer->timer_ops->request_by_node(np);
160*4882a593Smuzhiyun 	if (!timer->odt) {
161*4882a593Smuzhiyun 		dev_err(dev, "request for timer node %p failed\n", np);
162*4882a593Smuzhiyun 		return -EBUSY;
163*4882a593Smuzhiyun 	}
164*4882a593Smuzhiyun 
165*4882a593Smuzhiyun 	ret = timer->timer_ops->set_source(timer->odt, OMAP_TIMER_SRC_SYS_CLK);
166*4882a593Smuzhiyun 	if (ret) {
167*4882a593Smuzhiyun 		dev_err(dev, "error setting OMAP_TIMER_SRC_SYS_CLK as source for timer node %p\n",
168*4882a593Smuzhiyun 			np);
169*4882a593Smuzhiyun 		timer->timer_ops->free(timer->odt);
170*4882a593Smuzhiyun 		return ret;
171*4882a593Smuzhiyun 	}
172*4882a593Smuzhiyun 
173*4882a593Smuzhiyun 	/* clean counter, remoteproc code will set the value */
174*4882a593Smuzhiyun 	timer->timer_ops->set_load(timer->odt, 0);
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	return 0;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun  * omap_rproc_start_timer() - start a timer for a remoteproc
181*4882a593Smuzhiyun  * @timer: handle to a OMAP rproc timer
182*4882a593Smuzhiyun  *
183*4882a593Smuzhiyun  * This helper function is used to start a timer associated with a remoteproc,
184*4882a593Smuzhiyun  * obtained using the request_timer ops. The helper function needs to be
185*4882a593Smuzhiyun  * invoked by the driver to start the timer (during device initialization)
186*4882a593Smuzhiyun  * or to just resume the timer.
187*4882a593Smuzhiyun  *
188*4882a593Smuzhiyun  * Return: 0 on success, otherwise a failure as returned by DMTimer ops
189*4882a593Smuzhiyun  */
omap_rproc_start_timer(struct omap_rproc_timer * timer)190*4882a593Smuzhiyun static inline int omap_rproc_start_timer(struct omap_rproc_timer *timer)
191*4882a593Smuzhiyun {
192*4882a593Smuzhiyun 	return timer->timer_ops->start(timer->odt);
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun 
195*4882a593Smuzhiyun /**
196*4882a593Smuzhiyun  * omap_rproc_stop_timer() - stop a timer for a remoteproc
197*4882a593Smuzhiyun  * @timer: handle to a OMAP rproc timer
198*4882a593Smuzhiyun  *
199*4882a593Smuzhiyun  * This helper function is used to disable a timer associated with a
200*4882a593Smuzhiyun  * remoteproc, and needs to be called either during a device shutdown
201*4882a593Smuzhiyun  * or suspend operation. The separate helper function allows the driver
202*4882a593Smuzhiyun  * to just stop a timer without having to release the timer during a
203*4882a593Smuzhiyun  * suspend operation.
204*4882a593Smuzhiyun  *
205*4882a593Smuzhiyun  * Return: 0 on success, otherwise a failure as returned by DMTimer ops
206*4882a593Smuzhiyun  */
omap_rproc_stop_timer(struct omap_rproc_timer * timer)207*4882a593Smuzhiyun static inline int omap_rproc_stop_timer(struct omap_rproc_timer *timer)
208*4882a593Smuzhiyun {
209*4882a593Smuzhiyun 	return timer->timer_ops->stop(timer->odt);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun 
212*4882a593Smuzhiyun /**
213*4882a593Smuzhiyun  * omap_rproc_release_timer() - release a timer for a remoteproc
214*4882a593Smuzhiyun  * @timer: handle to a OMAP rproc timer
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * This helper function is used primarily to release a timer associated
217*4882a593Smuzhiyun  * with a remoteproc. The dmtimer will be available for other clients to
218*4882a593Smuzhiyun  * use once released.
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * Return: 0 on success, otherwise a failure as returned by DMTimer ops
221*4882a593Smuzhiyun  */
omap_rproc_release_timer(struct omap_rproc_timer * timer)222*4882a593Smuzhiyun static inline int omap_rproc_release_timer(struct omap_rproc_timer *timer)
223*4882a593Smuzhiyun {
224*4882a593Smuzhiyun 	return timer->timer_ops->free(timer->odt);
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun 
227*4882a593Smuzhiyun /**
228*4882a593Smuzhiyun  * omap_rproc_get_timer_irq() - get the irq for a timer
229*4882a593Smuzhiyun  * @timer: handle to a OMAP rproc timer
230*4882a593Smuzhiyun  *
231*4882a593Smuzhiyun  * This function is used to get the irq associated with a watchdog timer. The
232*4882a593Smuzhiyun  * function is called by the OMAP remoteproc driver to register a interrupt
233*4882a593Smuzhiyun  * handler to handle watchdog events on the remote processor.
234*4882a593Smuzhiyun  *
235*4882a593Smuzhiyun  * Return: irq id on success, otherwise a failure as returned by DMTimer ops
236*4882a593Smuzhiyun  */
omap_rproc_get_timer_irq(struct omap_rproc_timer * timer)237*4882a593Smuzhiyun static inline int omap_rproc_get_timer_irq(struct omap_rproc_timer *timer)
238*4882a593Smuzhiyun {
239*4882a593Smuzhiyun 	return timer->timer_ops->get_irq(timer->odt);
240*4882a593Smuzhiyun }
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun /**
243*4882a593Smuzhiyun  * omap_rproc_ack_timer_irq() - acknowledge a timer irq
244*4882a593Smuzhiyun  * @timer: handle to a OMAP rproc timer
245*4882a593Smuzhiyun  *
246*4882a593Smuzhiyun  * This function is used to clear the irq associated with a watchdog timer. The
247*4882a593Smuzhiyun  * The function is called by the OMAP remoteproc upon a watchdog event on the
248*4882a593Smuzhiyun  * remote processor to clear the interrupt status of the watchdog timer.
249*4882a593Smuzhiyun  */
omap_rproc_ack_timer_irq(struct omap_rproc_timer * timer)250*4882a593Smuzhiyun static inline void omap_rproc_ack_timer_irq(struct omap_rproc_timer *timer)
251*4882a593Smuzhiyun {
252*4882a593Smuzhiyun 	timer->timer_ops->write_status(timer->odt, OMAP_TIMER_INT_OVERFLOW);
253*4882a593Smuzhiyun }
254*4882a593Smuzhiyun 
255*4882a593Smuzhiyun /**
256*4882a593Smuzhiyun  * omap_rproc_watchdog_isr() - Watchdog ISR handler for remoteproc device
257*4882a593Smuzhiyun  * @irq: IRQ number associated with a watchdog timer
258*4882a593Smuzhiyun  * @data: IRQ handler data
259*4882a593Smuzhiyun  *
260*4882a593Smuzhiyun  * This ISR routine executes the required necessary low-level code to
261*4882a593Smuzhiyun  * acknowledge a watchdog timer interrupt. There can be multiple watchdog
262*4882a593Smuzhiyun  * timers associated with a rproc (like IPUs which have 2 watchdog timers,
263*4882a593Smuzhiyun  * one per Cortex M3/M4 core), so a lookup has to be performed to identify
264*4882a593Smuzhiyun  * the timer to acknowledge its interrupt.
265*4882a593Smuzhiyun  *
266*4882a593Smuzhiyun  * The function also invokes rproc_report_crash to report the watchdog event
267*4882a593Smuzhiyun  * to the remoteproc driver core, to trigger a recovery.
268*4882a593Smuzhiyun  *
269*4882a593Smuzhiyun  * Return: IRQ_HANDLED on success, otherwise IRQ_NONE
270*4882a593Smuzhiyun  */
omap_rproc_watchdog_isr(int irq,void * data)271*4882a593Smuzhiyun static irqreturn_t omap_rproc_watchdog_isr(int irq, void *data)
272*4882a593Smuzhiyun {
273*4882a593Smuzhiyun 	struct rproc *rproc = data;
274*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
275*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
276*4882a593Smuzhiyun 	struct omap_rproc_timer *timers = oproc->timers;
277*4882a593Smuzhiyun 	struct omap_rproc_timer *wd_timer = NULL;
278*4882a593Smuzhiyun 	int num_timers = oproc->num_timers + oproc->num_wd_timers;
279*4882a593Smuzhiyun 	int i;
280*4882a593Smuzhiyun 
281*4882a593Smuzhiyun 	for (i = oproc->num_timers; i < num_timers; i++) {
282*4882a593Smuzhiyun 		if (timers[i].irq > 0 && irq == timers[i].irq) {
283*4882a593Smuzhiyun 			wd_timer = &timers[i];
284*4882a593Smuzhiyun 			break;
285*4882a593Smuzhiyun 		}
286*4882a593Smuzhiyun 	}
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	if (!wd_timer) {
289*4882a593Smuzhiyun 		dev_err(dev, "invalid timer\n");
290*4882a593Smuzhiyun 		return IRQ_NONE;
291*4882a593Smuzhiyun 	}
292*4882a593Smuzhiyun 
293*4882a593Smuzhiyun 	omap_rproc_ack_timer_irq(wd_timer);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	rproc_report_crash(rproc, RPROC_WATCHDOG);
296*4882a593Smuzhiyun 
297*4882a593Smuzhiyun 	return IRQ_HANDLED;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun 
300*4882a593Smuzhiyun /**
301*4882a593Smuzhiyun  * omap_rproc_enable_timers() - enable the timers for a remoteproc
302*4882a593Smuzhiyun  * @rproc: handle of a remote processor
303*4882a593Smuzhiyun  * @configure: boolean flag used to acquire and configure the timer handle
304*4882a593Smuzhiyun  *
305*4882a593Smuzhiyun  * This function is used primarily to enable the timers associated with
306*4882a593Smuzhiyun  * a remoteproc. The configure flag is provided to allow the driver to
307*4882a593Smuzhiyun  * to either acquire and start a timer (during device initialization) or
308*4882a593Smuzhiyun  * to just start a timer (during a resume operation).
309*4882a593Smuzhiyun  *
310*4882a593Smuzhiyun  * Return: 0 on success, otherwise an appropriate failure
311*4882a593Smuzhiyun  */
omap_rproc_enable_timers(struct rproc * rproc,bool configure)312*4882a593Smuzhiyun static int omap_rproc_enable_timers(struct rproc *rproc, bool configure)
313*4882a593Smuzhiyun {
314*4882a593Smuzhiyun 	int i;
315*4882a593Smuzhiyun 	int ret = 0;
316*4882a593Smuzhiyun 	struct platform_device *tpdev;
317*4882a593Smuzhiyun 	struct dmtimer_platform_data *tpdata;
318*4882a593Smuzhiyun 	const struct omap_dm_timer_ops *timer_ops;
319*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
320*4882a593Smuzhiyun 	struct omap_rproc_timer *timers = oproc->timers;
321*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
322*4882a593Smuzhiyun 	struct device_node *np = NULL;
323*4882a593Smuzhiyun 	int num_timers = oproc->num_timers + oproc->num_wd_timers;
324*4882a593Smuzhiyun 
325*4882a593Smuzhiyun 	if (!num_timers)
326*4882a593Smuzhiyun 		return 0;
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (!configure)
329*4882a593Smuzhiyun 		goto start_timers;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	for (i = 0; i < num_timers; i++) {
332*4882a593Smuzhiyun 		if (i < oproc->num_timers)
333*4882a593Smuzhiyun 			np = of_parse_phandle(dev->of_node, "ti,timers", i);
334*4882a593Smuzhiyun 		else
335*4882a593Smuzhiyun 			np = of_parse_phandle(dev->of_node,
336*4882a593Smuzhiyun 					      "ti,watchdog-timers",
337*4882a593Smuzhiyun 					      (i - oproc->num_timers));
338*4882a593Smuzhiyun 		if (!np) {
339*4882a593Smuzhiyun 			ret = -ENXIO;
340*4882a593Smuzhiyun 			dev_err(dev, "device node lookup for timer at index %d failed: %d\n",
341*4882a593Smuzhiyun 				i < oproc->num_timers ? i :
342*4882a593Smuzhiyun 				i - oproc->num_timers, ret);
343*4882a593Smuzhiyun 			goto free_timers;
344*4882a593Smuzhiyun 		}
345*4882a593Smuzhiyun 
346*4882a593Smuzhiyun 		tpdev = of_find_device_by_node(np);
347*4882a593Smuzhiyun 		if (!tpdev) {
348*4882a593Smuzhiyun 			ret = -ENODEV;
349*4882a593Smuzhiyun 			dev_err(dev, "could not get timer platform device\n");
350*4882a593Smuzhiyun 			goto put_node;
351*4882a593Smuzhiyun 		}
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 		tpdata = dev_get_platdata(&tpdev->dev);
354*4882a593Smuzhiyun 		put_device(&tpdev->dev);
355*4882a593Smuzhiyun 		if (!tpdata) {
356*4882a593Smuzhiyun 			ret = -EINVAL;
357*4882a593Smuzhiyun 			dev_err(dev, "dmtimer pdata structure NULL\n");
358*4882a593Smuzhiyun 			goto put_node;
359*4882a593Smuzhiyun 		}
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 		timer_ops = tpdata->timer_ops;
362*4882a593Smuzhiyun 		if (!timer_ops || !timer_ops->request_by_node ||
363*4882a593Smuzhiyun 		    !timer_ops->set_source || !timer_ops->set_load ||
364*4882a593Smuzhiyun 		    !timer_ops->free || !timer_ops->start ||
365*4882a593Smuzhiyun 		    !timer_ops->stop || !timer_ops->get_irq ||
366*4882a593Smuzhiyun 		    !timer_ops->write_status) {
367*4882a593Smuzhiyun 			ret = -EINVAL;
368*4882a593Smuzhiyun 			dev_err(dev, "device does not have required timer ops\n");
369*4882a593Smuzhiyun 			goto put_node;
370*4882a593Smuzhiyun 		}
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 		timers[i].irq = -1;
373*4882a593Smuzhiyun 		timers[i].timer_ops = timer_ops;
374*4882a593Smuzhiyun 		ret = omap_rproc_request_timer(dev, np, &timers[i]);
375*4882a593Smuzhiyun 		if (ret) {
376*4882a593Smuzhiyun 			dev_err(dev, "request for timer %p failed: %d\n", np,
377*4882a593Smuzhiyun 				ret);
378*4882a593Smuzhiyun 			goto put_node;
379*4882a593Smuzhiyun 		}
380*4882a593Smuzhiyun 		of_node_put(np);
381*4882a593Smuzhiyun 
382*4882a593Smuzhiyun 		if (i >= oproc->num_timers) {
383*4882a593Smuzhiyun 			timers[i].irq = omap_rproc_get_timer_irq(&timers[i]);
384*4882a593Smuzhiyun 			if (timers[i].irq < 0) {
385*4882a593Smuzhiyun 				dev_err(dev, "get_irq for timer %p failed: %d\n",
386*4882a593Smuzhiyun 					np, timers[i].irq);
387*4882a593Smuzhiyun 				ret = -EBUSY;
388*4882a593Smuzhiyun 				goto free_timers;
389*4882a593Smuzhiyun 			}
390*4882a593Smuzhiyun 
391*4882a593Smuzhiyun 			ret = request_irq(timers[i].irq,
392*4882a593Smuzhiyun 					  omap_rproc_watchdog_isr, IRQF_SHARED,
393*4882a593Smuzhiyun 					  "rproc-wdt", rproc);
394*4882a593Smuzhiyun 			if (ret) {
395*4882a593Smuzhiyun 				dev_err(dev, "error requesting irq for timer %p\n",
396*4882a593Smuzhiyun 					np);
397*4882a593Smuzhiyun 				omap_rproc_release_timer(&timers[i]);
398*4882a593Smuzhiyun 				timers[i].odt = NULL;
399*4882a593Smuzhiyun 				timers[i].timer_ops = NULL;
400*4882a593Smuzhiyun 				timers[i].irq = -1;
401*4882a593Smuzhiyun 				goto free_timers;
402*4882a593Smuzhiyun 			}
403*4882a593Smuzhiyun 		}
404*4882a593Smuzhiyun 	}
405*4882a593Smuzhiyun 
406*4882a593Smuzhiyun start_timers:
407*4882a593Smuzhiyun 	for (i = 0; i < num_timers; i++) {
408*4882a593Smuzhiyun 		ret = omap_rproc_start_timer(&timers[i]);
409*4882a593Smuzhiyun 		if (ret) {
410*4882a593Smuzhiyun 			dev_err(dev, "start timer %p failed failed: %d\n", np,
411*4882a593Smuzhiyun 				ret);
412*4882a593Smuzhiyun 			break;
413*4882a593Smuzhiyun 		}
414*4882a593Smuzhiyun 	}
415*4882a593Smuzhiyun 	if (ret) {
416*4882a593Smuzhiyun 		while (i >= 0) {
417*4882a593Smuzhiyun 			omap_rproc_stop_timer(&timers[i]);
418*4882a593Smuzhiyun 			i--;
419*4882a593Smuzhiyun 		}
420*4882a593Smuzhiyun 		goto put_node;
421*4882a593Smuzhiyun 	}
422*4882a593Smuzhiyun 	return 0;
423*4882a593Smuzhiyun 
424*4882a593Smuzhiyun put_node:
425*4882a593Smuzhiyun 	if (configure)
426*4882a593Smuzhiyun 		of_node_put(np);
427*4882a593Smuzhiyun free_timers:
428*4882a593Smuzhiyun 	while (i--) {
429*4882a593Smuzhiyun 		if (i >= oproc->num_timers)
430*4882a593Smuzhiyun 			free_irq(timers[i].irq, rproc);
431*4882a593Smuzhiyun 		omap_rproc_release_timer(&timers[i]);
432*4882a593Smuzhiyun 		timers[i].odt = NULL;
433*4882a593Smuzhiyun 		timers[i].timer_ops = NULL;
434*4882a593Smuzhiyun 		timers[i].irq = -1;
435*4882a593Smuzhiyun 	}
436*4882a593Smuzhiyun 
437*4882a593Smuzhiyun 	return ret;
438*4882a593Smuzhiyun }
439*4882a593Smuzhiyun 
440*4882a593Smuzhiyun /**
441*4882a593Smuzhiyun  * omap_rproc_disable_timers() - disable the timers for a remoteproc
442*4882a593Smuzhiyun  * @rproc: handle of a remote processor
443*4882a593Smuzhiyun  * @configure: boolean flag used to release the timer handle
444*4882a593Smuzhiyun  *
445*4882a593Smuzhiyun  * This function is used primarily to disable the timers associated with
446*4882a593Smuzhiyun  * a remoteproc. The configure flag is provided to allow the driver to
447*4882a593Smuzhiyun  * to either stop and release a timer (during device shutdown) or to just
448*4882a593Smuzhiyun  * stop a timer (during a suspend operation).
449*4882a593Smuzhiyun  *
450*4882a593Smuzhiyun  * Return: 0 on success or no timers
451*4882a593Smuzhiyun  */
omap_rproc_disable_timers(struct rproc * rproc,bool configure)452*4882a593Smuzhiyun static int omap_rproc_disable_timers(struct rproc *rproc, bool configure)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun 	int i;
455*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
456*4882a593Smuzhiyun 	struct omap_rproc_timer *timers = oproc->timers;
457*4882a593Smuzhiyun 	int num_timers = oproc->num_timers + oproc->num_wd_timers;
458*4882a593Smuzhiyun 
459*4882a593Smuzhiyun 	if (!num_timers)
460*4882a593Smuzhiyun 		return 0;
461*4882a593Smuzhiyun 
462*4882a593Smuzhiyun 	for (i = 0; i < num_timers; i++) {
463*4882a593Smuzhiyun 		omap_rproc_stop_timer(&timers[i]);
464*4882a593Smuzhiyun 		if (configure) {
465*4882a593Smuzhiyun 			if (i >= oproc->num_timers)
466*4882a593Smuzhiyun 				free_irq(timers[i].irq, rproc);
467*4882a593Smuzhiyun 			omap_rproc_release_timer(&timers[i]);
468*4882a593Smuzhiyun 			timers[i].odt = NULL;
469*4882a593Smuzhiyun 			timers[i].timer_ops = NULL;
470*4882a593Smuzhiyun 			timers[i].irq = -1;
471*4882a593Smuzhiyun 		}
472*4882a593Smuzhiyun 	}
473*4882a593Smuzhiyun 
474*4882a593Smuzhiyun 	return 0;
475*4882a593Smuzhiyun }
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun /**
478*4882a593Smuzhiyun  * omap_rproc_mbox_callback() - inbound mailbox message handler
479*4882a593Smuzhiyun  * @client: mailbox client pointer used for requesting the mailbox channel
480*4882a593Smuzhiyun  * @data: mailbox payload
481*4882a593Smuzhiyun  *
482*4882a593Smuzhiyun  * This handler is invoked by omap's mailbox driver whenever a mailbox
483*4882a593Smuzhiyun  * message is received. Usually, the mailbox payload simply contains
484*4882a593Smuzhiyun  * the index of the virtqueue that is kicked by the remote processor,
485*4882a593Smuzhiyun  * and we let remoteproc core handle it.
486*4882a593Smuzhiyun  *
487*4882a593Smuzhiyun  * In addition to virtqueue indices, we also have some out-of-band values
488*4882a593Smuzhiyun  * that indicates different events. Those values are deliberately very
489*4882a593Smuzhiyun  * big so they don't coincide with virtqueue indices.
490*4882a593Smuzhiyun  */
omap_rproc_mbox_callback(struct mbox_client * client,void * data)491*4882a593Smuzhiyun static void omap_rproc_mbox_callback(struct mbox_client *client, void *data)
492*4882a593Smuzhiyun {
493*4882a593Smuzhiyun 	struct omap_rproc *oproc = container_of(client, struct omap_rproc,
494*4882a593Smuzhiyun 						client);
495*4882a593Smuzhiyun 	struct device *dev = oproc->rproc->dev.parent;
496*4882a593Smuzhiyun 	const char *name = oproc->rproc->name;
497*4882a593Smuzhiyun 	u32 msg = (u32)data;
498*4882a593Smuzhiyun 
499*4882a593Smuzhiyun 	dev_dbg(dev, "mbox msg: 0x%x\n", msg);
500*4882a593Smuzhiyun 
501*4882a593Smuzhiyun 	switch (msg) {
502*4882a593Smuzhiyun 	case RP_MBOX_CRASH:
503*4882a593Smuzhiyun 		/*
504*4882a593Smuzhiyun 		 * remoteproc detected an exception, notify the rproc core.
505*4882a593Smuzhiyun 		 * The remoteproc core will handle the recovery.
506*4882a593Smuzhiyun 		 */
507*4882a593Smuzhiyun 		dev_err(dev, "omap rproc %s crashed\n", name);
508*4882a593Smuzhiyun 		rproc_report_crash(oproc->rproc, RPROC_FATAL_ERROR);
509*4882a593Smuzhiyun 		break;
510*4882a593Smuzhiyun 	case RP_MBOX_ECHO_REPLY:
511*4882a593Smuzhiyun 		dev_info(dev, "received echo reply from %s\n", name);
512*4882a593Smuzhiyun 		break;
513*4882a593Smuzhiyun 	case RP_MBOX_SUSPEND_ACK:
514*4882a593Smuzhiyun 	case RP_MBOX_SUSPEND_CANCEL:
515*4882a593Smuzhiyun 		oproc->suspend_acked = msg == RP_MBOX_SUSPEND_ACK;
516*4882a593Smuzhiyun 		complete(&oproc->pm_comp);
517*4882a593Smuzhiyun 		break;
518*4882a593Smuzhiyun 	default:
519*4882a593Smuzhiyun 		if (msg >= RP_MBOX_READY && msg < RP_MBOX_END_MSG)
520*4882a593Smuzhiyun 			return;
521*4882a593Smuzhiyun 		if (msg > oproc->rproc->max_notifyid) {
522*4882a593Smuzhiyun 			dev_dbg(dev, "dropping unknown message 0x%x", msg);
523*4882a593Smuzhiyun 			return;
524*4882a593Smuzhiyun 		}
525*4882a593Smuzhiyun 		/* msg contains the index of the triggered vring */
526*4882a593Smuzhiyun 		if (rproc_vq_interrupt(oproc->rproc, msg) == IRQ_NONE)
527*4882a593Smuzhiyun 			dev_dbg(dev, "no message was found in vqid %d\n", msg);
528*4882a593Smuzhiyun 	}
529*4882a593Smuzhiyun }
530*4882a593Smuzhiyun 
531*4882a593Smuzhiyun /* kick a virtqueue */
omap_rproc_kick(struct rproc * rproc,int vqid)532*4882a593Smuzhiyun static void omap_rproc_kick(struct rproc *rproc, int vqid)
533*4882a593Smuzhiyun {
534*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
535*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
536*4882a593Smuzhiyun 	int ret;
537*4882a593Smuzhiyun 
538*4882a593Smuzhiyun 	/* wake up the rproc before kicking it */
539*4882a593Smuzhiyun 	ret = pm_runtime_get_sync(dev);
540*4882a593Smuzhiyun 	if (WARN_ON(ret < 0)) {
541*4882a593Smuzhiyun 		dev_err(dev, "pm_runtime_get_sync() failed during kick, ret = %d\n",
542*4882a593Smuzhiyun 			ret);
543*4882a593Smuzhiyun 		pm_runtime_put_noidle(dev);
544*4882a593Smuzhiyun 		return;
545*4882a593Smuzhiyun 	}
546*4882a593Smuzhiyun 
547*4882a593Smuzhiyun 	/* send the index of the triggered virtqueue in the mailbox payload */
548*4882a593Smuzhiyun 	ret = mbox_send_message(oproc->mbox, (void *)vqid);
549*4882a593Smuzhiyun 	if (ret < 0)
550*4882a593Smuzhiyun 		dev_err(dev, "failed to send mailbox message, status = %d\n",
551*4882a593Smuzhiyun 			ret);
552*4882a593Smuzhiyun 
553*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(dev);
554*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(dev);
555*4882a593Smuzhiyun }
556*4882a593Smuzhiyun 
557*4882a593Smuzhiyun /**
558*4882a593Smuzhiyun  * omap_rproc_write_dsp_boot_addr() - set boot address for DSP remote processor
559*4882a593Smuzhiyun  * @rproc: handle of a remote processor
560*4882a593Smuzhiyun  *
561*4882a593Smuzhiyun  * Set boot address for a supported DSP remote processor.
562*4882a593Smuzhiyun  *
563*4882a593Smuzhiyun  * Return: 0 on success, or -EINVAL if boot address is not aligned properly
564*4882a593Smuzhiyun  */
omap_rproc_write_dsp_boot_addr(struct rproc * rproc)565*4882a593Smuzhiyun static int omap_rproc_write_dsp_boot_addr(struct rproc *rproc)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
568*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
569*4882a593Smuzhiyun 	struct omap_rproc_boot_data *bdata = oproc->boot_data;
570*4882a593Smuzhiyun 	u32 offset = bdata->boot_reg;
571*4882a593Smuzhiyun 	u32 value;
572*4882a593Smuzhiyun 	u32 mask;
573*4882a593Smuzhiyun 
574*4882a593Smuzhiyun 	if (rproc->bootaddr & (SZ_1K - 1)) {
575*4882a593Smuzhiyun 		dev_err(dev, "invalid boot address 0x%llx, must be aligned on a 1KB boundary\n",
576*4882a593Smuzhiyun 			rproc->bootaddr);
577*4882a593Smuzhiyun 		return -EINVAL;
578*4882a593Smuzhiyun 	}
579*4882a593Smuzhiyun 
580*4882a593Smuzhiyun 	value = rproc->bootaddr >> bdata->boot_reg_shift;
581*4882a593Smuzhiyun 	mask = ~(SZ_1K - 1) >> bdata->boot_reg_shift;
582*4882a593Smuzhiyun 
583*4882a593Smuzhiyun 	return regmap_update_bits(bdata->syscon, offset, mask, value);
584*4882a593Smuzhiyun }
585*4882a593Smuzhiyun 
586*4882a593Smuzhiyun /*
587*4882a593Smuzhiyun  * Power up the remote processor.
588*4882a593Smuzhiyun  *
589*4882a593Smuzhiyun  * This function will be invoked only after the firmware for this rproc
590*4882a593Smuzhiyun  * was loaded, parsed successfully, and all of its resource requirements
591*4882a593Smuzhiyun  * were met.
592*4882a593Smuzhiyun  */
omap_rproc_start(struct rproc * rproc)593*4882a593Smuzhiyun static int omap_rproc_start(struct rproc *rproc)
594*4882a593Smuzhiyun {
595*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
596*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
597*4882a593Smuzhiyun 	int ret;
598*4882a593Smuzhiyun 	struct mbox_client *client = &oproc->client;
599*4882a593Smuzhiyun 
600*4882a593Smuzhiyun 	if (oproc->boot_data) {
601*4882a593Smuzhiyun 		ret = omap_rproc_write_dsp_boot_addr(rproc);
602*4882a593Smuzhiyun 		if (ret)
603*4882a593Smuzhiyun 			return ret;
604*4882a593Smuzhiyun 	}
605*4882a593Smuzhiyun 
606*4882a593Smuzhiyun 	client->dev = dev;
607*4882a593Smuzhiyun 	client->tx_done = NULL;
608*4882a593Smuzhiyun 	client->rx_callback = omap_rproc_mbox_callback;
609*4882a593Smuzhiyun 	client->tx_block = false;
610*4882a593Smuzhiyun 	client->knows_txdone = false;
611*4882a593Smuzhiyun 
612*4882a593Smuzhiyun 	oproc->mbox = mbox_request_channel(client, 0);
613*4882a593Smuzhiyun 	if (IS_ERR(oproc->mbox)) {
614*4882a593Smuzhiyun 		ret = -EBUSY;
615*4882a593Smuzhiyun 		dev_err(dev, "mbox_request_channel failed: %ld\n",
616*4882a593Smuzhiyun 			PTR_ERR(oproc->mbox));
617*4882a593Smuzhiyun 		return ret;
618*4882a593Smuzhiyun 	}
619*4882a593Smuzhiyun 
620*4882a593Smuzhiyun 	/*
621*4882a593Smuzhiyun 	 * Ping the remote processor. this is only for sanity-sake;
622*4882a593Smuzhiyun 	 * there is no functional effect whatsoever.
623*4882a593Smuzhiyun 	 *
624*4882a593Smuzhiyun 	 * Note that the reply will _not_ arrive immediately: this message
625*4882a593Smuzhiyun 	 * will wait in the mailbox fifo until the remote processor is booted.
626*4882a593Smuzhiyun 	 */
627*4882a593Smuzhiyun 	ret = mbox_send_message(oproc->mbox, (void *)RP_MBOX_ECHO_REQUEST);
628*4882a593Smuzhiyun 	if (ret < 0) {
629*4882a593Smuzhiyun 		dev_err(dev, "mbox_send_message failed: %d\n", ret);
630*4882a593Smuzhiyun 		goto put_mbox;
631*4882a593Smuzhiyun 	}
632*4882a593Smuzhiyun 
633*4882a593Smuzhiyun 	ret = omap_rproc_enable_timers(rproc, true);
634*4882a593Smuzhiyun 	if (ret) {
635*4882a593Smuzhiyun 		dev_err(dev, "omap_rproc_enable_timers failed: %d\n", ret);
636*4882a593Smuzhiyun 		goto put_mbox;
637*4882a593Smuzhiyun 	}
638*4882a593Smuzhiyun 
639*4882a593Smuzhiyun 	ret = reset_control_deassert(oproc->reset);
640*4882a593Smuzhiyun 	if (ret) {
641*4882a593Smuzhiyun 		dev_err(dev, "reset control deassert failed: %d\n", ret);
642*4882a593Smuzhiyun 		goto disable_timers;
643*4882a593Smuzhiyun 	}
644*4882a593Smuzhiyun 
645*4882a593Smuzhiyun 	/*
646*4882a593Smuzhiyun 	 * remote processor is up, so update the runtime pm status and
647*4882a593Smuzhiyun 	 * enable the auto-suspend. The device usage count is incremented
648*4882a593Smuzhiyun 	 * manually for balancing it for auto-suspend
649*4882a593Smuzhiyun 	 */
650*4882a593Smuzhiyun 	pm_runtime_set_active(dev);
651*4882a593Smuzhiyun 	pm_runtime_use_autosuspend(dev);
652*4882a593Smuzhiyun 	pm_runtime_get_noresume(dev);
653*4882a593Smuzhiyun 	pm_runtime_enable(dev);
654*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(dev);
655*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(dev);
656*4882a593Smuzhiyun 
657*4882a593Smuzhiyun 	return 0;
658*4882a593Smuzhiyun 
659*4882a593Smuzhiyun disable_timers:
660*4882a593Smuzhiyun 	omap_rproc_disable_timers(rproc, true);
661*4882a593Smuzhiyun put_mbox:
662*4882a593Smuzhiyun 	mbox_free_channel(oproc->mbox);
663*4882a593Smuzhiyun 	return ret;
664*4882a593Smuzhiyun }
665*4882a593Smuzhiyun 
666*4882a593Smuzhiyun /* power off the remote processor */
omap_rproc_stop(struct rproc * rproc)667*4882a593Smuzhiyun static int omap_rproc_stop(struct rproc *rproc)
668*4882a593Smuzhiyun {
669*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
670*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
671*4882a593Smuzhiyun 	int ret;
672*4882a593Smuzhiyun 
673*4882a593Smuzhiyun 	/*
674*4882a593Smuzhiyun 	 * cancel any possible scheduled runtime suspend by incrementing
675*4882a593Smuzhiyun 	 * the device usage count, and resuming the device. The remoteproc
676*4882a593Smuzhiyun 	 * also needs to be woken up if suspended, to avoid the remoteproc
677*4882a593Smuzhiyun 	 * OS to continue to remember any context that it has saved, and
678*4882a593Smuzhiyun 	 * avoid potential issues in misindentifying a subsequent device
679*4882a593Smuzhiyun 	 * reboot as a power restore boot
680*4882a593Smuzhiyun 	 */
681*4882a593Smuzhiyun 	ret = pm_runtime_get_sync(dev);
682*4882a593Smuzhiyun 	if (ret < 0) {
683*4882a593Smuzhiyun 		pm_runtime_put_noidle(dev);
684*4882a593Smuzhiyun 		return ret;
685*4882a593Smuzhiyun 	}
686*4882a593Smuzhiyun 
687*4882a593Smuzhiyun 	ret = reset_control_assert(oproc->reset);
688*4882a593Smuzhiyun 	if (ret)
689*4882a593Smuzhiyun 		goto out;
690*4882a593Smuzhiyun 
691*4882a593Smuzhiyun 	ret = omap_rproc_disable_timers(rproc, true);
692*4882a593Smuzhiyun 	if (ret)
693*4882a593Smuzhiyun 		goto enable_device;
694*4882a593Smuzhiyun 
695*4882a593Smuzhiyun 	mbox_free_channel(oproc->mbox);
696*4882a593Smuzhiyun 
697*4882a593Smuzhiyun 	/*
698*4882a593Smuzhiyun 	 * update the runtime pm states and status now that the remoteproc
699*4882a593Smuzhiyun 	 * has stopped
700*4882a593Smuzhiyun 	 */
701*4882a593Smuzhiyun 	pm_runtime_disable(dev);
702*4882a593Smuzhiyun 	pm_runtime_dont_use_autosuspend(dev);
703*4882a593Smuzhiyun 	pm_runtime_put_noidle(dev);
704*4882a593Smuzhiyun 	pm_runtime_set_suspended(dev);
705*4882a593Smuzhiyun 
706*4882a593Smuzhiyun 	return 0;
707*4882a593Smuzhiyun 
708*4882a593Smuzhiyun enable_device:
709*4882a593Smuzhiyun 	reset_control_deassert(oproc->reset);
710*4882a593Smuzhiyun out:
711*4882a593Smuzhiyun 	/* schedule the next auto-suspend */
712*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(dev);
713*4882a593Smuzhiyun 	pm_runtime_put_autosuspend(dev);
714*4882a593Smuzhiyun 	return ret;
715*4882a593Smuzhiyun }
716*4882a593Smuzhiyun 
717*4882a593Smuzhiyun /**
718*4882a593Smuzhiyun  * omap_rproc_da_to_va() - internal memory translation helper
719*4882a593Smuzhiyun  * @rproc: remote processor to apply the address translation for
720*4882a593Smuzhiyun  * @da: device address to translate
721*4882a593Smuzhiyun  * @len: length of the memory buffer
722*4882a593Smuzhiyun  *
723*4882a593Smuzhiyun  * Custom function implementing the rproc .da_to_va ops to provide address
724*4882a593Smuzhiyun  * translation (device address to kernel virtual address) for internal RAMs
725*4882a593Smuzhiyun  * present in a DSP or IPU device). The translated addresses can be used
726*4882a593Smuzhiyun  * either by the remoteproc core for loading, or by any rpmsg bus drivers.
727*4882a593Smuzhiyun  *
728*4882a593Smuzhiyun  * Return: translated virtual address in kernel memory space on success,
729*4882a593Smuzhiyun  *         or NULL on failure.
730*4882a593Smuzhiyun  */
omap_rproc_da_to_va(struct rproc * rproc,u64 da,size_t len,bool * is_iomem)731*4882a593Smuzhiyun static void *omap_rproc_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
732*4882a593Smuzhiyun {
733*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
734*4882a593Smuzhiyun 	int i;
735*4882a593Smuzhiyun 	u32 offset;
736*4882a593Smuzhiyun 
737*4882a593Smuzhiyun 	if (len <= 0)
738*4882a593Smuzhiyun 		return NULL;
739*4882a593Smuzhiyun 
740*4882a593Smuzhiyun 	if (!oproc->num_mems)
741*4882a593Smuzhiyun 		return NULL;
742*4882a593Smuzhiyun 
743*4882a593Smuzhiyun 	for (i = 0; i < oproc->num_mems; i++) {
744*4882a593Smuzhiyun 		if (da >= oproc->mem[i].dev_addr && da + len <=
745*4882a593Smuzhiyun 		    oproc->mem[i].dev_addr + oproc->mem[i].size) {
746*4882a593Smuzhiyun 			offset = da - oproc->mem[i].dev_addr;
747*4882a593Smuzhiyun 			/* __force to make sparse happy with type conversion */
748*4882a593Smuzhiyun 			return (__force void *)(oproc->mem[i].cpu_addr +
749*4882a593Smuzhiyun 						offset);
750*4882a593Smuzhiyun 		}
751*4882a593Smuzhiyun 	}
752*4882a593Smuzhiyun 
753*4882a593Smuzhiyun 	return NULL;
754*4882a593Smuzhiyun }
755*4882a593Smuzhiyun 
756*4882a593Smuzhiyun static const struct rproc_ops omap_rproc_ops = {
757*4882a593Smuzhiyun 	.start		= omap_rproc_start,
758*4882a593Smuzhiyun 	.stop		= omap_rproc_stop,
759*4882a593Smuzhiyun 	.kick		= omap_rproc_kick,
760*4882a593Smuzhiyun 	.da_to_va	= omap_rproc_da_to_va,
761*4882a593Smuzhiyun };
762*4882a593Smuzhiyun 
763*4882a593Smuzhiyun #ifdef CONFIG_PM
_is_rproc_in_standby(struct omap_rproc * oproc)764*4882a593Smuzhiyun static bool _is_rproc_in_standby(struct omap_rproc *oproc)
765*4882a593Smuzhiyun {
766*4882a593Smuzhiyun 	return ti_clk_is_in_standby(oproc->fck);
767*4882a593Smuzhiyun }
768*4882a593Smuzhiyun 
769*4882a593Smuzhiyun /* 1 sec is long enough time to let the remoteproc side suspend the device */
770*4882a593Smuzhiyun #define DEF_SUSPEND_TIMEOUT 1000
_omap_rproc_suspend(struct rproc * rproc,bool auto_suspend)771*4882a593Smuzhiyun static int _omap_rproc_suspend(struct rproc *rproc, bool auto_suspend)
772*4882a593Smuzhiyun {
773*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
774*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
775*4882a593Smuzhiyun 	unsigned long to = msecs_to_jiffies(DEF_SUSPEND_TIMEOUT);
776*4882a593Smuzhiyun 	unsigned long ta = jiffies + to;
777*4882a593Smuzhiyun 	u32 suspend_msg = auto_suspend ?
778*4882a593Smuzhiyun 				RP_MBOX_SUSPEND_AUTO : RP_MBOX_SUSPEND_SYSTEM;
779*4882a593Smuzhiyun 	int ret;
780*4882a593Smuzhiyun 
781*4882a593Smuzhiyun 	reinit_completion(&oproc->pm_comp);
782*4882a593Smuzhiyun 	oproc->suspend_acked = false;
783*4882a593Smuzhiyun 	ret = mbox_send_message(oproc->mbox, (void *)suspend_msg);
784*4882a593Smuzhiyun 	if (ret < 0) {
785*4882a593Smuzhiyun 		dev_err(dev, "PM mbox_send_message failed: %d\n", ret);
786*4882a593Smuzhiyun 		return ret;
787*4882a593Smuzhiyun 	}
788*4882a593Smuzhiyun 
789*4882a593Smuzhiyun 	ret = wait_for_completion_timeout(&oproc->pm_comp, to);
790*4882a593Smuzhiyun 	if (!oproc->suspend_acked)
791*4882a593Smuzhiyun 		return -EBUSY;
792*4882a593Smuzhiyun 
793*4882a593Smuzhiyun 	/*
794*4882a593Smuzhiyun 	 * The remoteproc side is returning the ACK message before saving the
795*4882a593Smuzhiyun 	 * context, because the context saving is performed within a SYS/BIOS
796*4882a593Smuzhiyun 	 * function, and it cannot have any inter-dependencies against the IPC
797*4882a593Smuzhiyun 	 * layer. Also, as the SYS/BIOS needs to preserve properly the processor
798*4882a593Smuzhiyun 	 * register set, sending this ACK or signalling the completion of the
799*4882a593Smuzhiyun 	 * context save through a shared memory variable can never be the
800*4882a593Smuzhiyun 	 * absolute last thing to be executed on the remoteproc side, and the
801*4882a593Smuzhiyun 	 * MPU cannot use the ACK message as a sync point to put the remoteproc
802*4882a593Smuzhiyun 	 * into reset. The only way to ensure that the remote processor has
803*4882a593Smuzhiyun 	 * completed saving the context is to check that the module has reached
804*4882a593Smuzhiyun 	 * STANDBY state (after saving the context, the SYS/BIOS executes the
805*4882a593Smuzhiyun 	 * appropriate target-specific WFI instruction causing the module to
806*4882a593Smuzhiyun 	 * enter STANDBY).
807*4882a593Smuzhiyun 	 */
808*4882a593Smuzhiyun 	while (!_is_rproc_in_standby(oproc)) {
809*4882a593Smuzhiyun 		if (time_after(jiffies, ta))
810*4882a593Smuzhiyun 			return -ETIME;
811*4882a593Smuzhiyun 		schedule();
812*4882a593Smuzhiyun 	}
813*4882a593Smuzhiyun 
814*4882a593Smuzhiyun 	ret = reset_control_assert(oproc->reset);
815*4882a593Smuzhiyun 	if (ret) {
816*4882a593Smuzhiyun 		dev_err(dev, "reset assert during suspend failed %d\n", ret);
817*4882a593Smuzhiyun 		return ret;
818*4882a593Smuzhiyun 	}
819*4882a593Smuzhiyun 
820*4882a593Smuzhiyun 	ret = omap_rproc_disable_timers(rproc, false);
821*4882a593Smuzhiyun 	if (ret) {
822*4882a593Smuzhiyun 		dev_err(dev, "disabling timers during suspend failed %d\n",
823*4882a593Smuzhiyun 			ret);
824*4882a593Smuzhiyun 		goto enable_device;
825*4882a593Smuzhiyun 	}
826*4882a593Smuzhiyun 
827*4882a593Smuzhiyun 	/*
828*4882a593Smuzhiyun 	 * IOMMUs would have to be disabled specifically for runtime suspend.
829*4882a593Smuzhiyun 	 * They are handled automatically through System PM callbacks for
830*4882a593Smuzhiyun 	 * regular system suspend
831*4882a593Smuzhiyun 	 */
832*4882a593Smuzhiyun 	if (auto_suspend) {
833*4882a593Smuzhiyun 		ret = omap_iommu_domain_deactivate(rproc->domain);
834*4882a593Smuzhiyun 		if (ret) {
835*4882a593Smuzhiyun 			dev_err(dev, "iommu domain deactivate failed %d\n",
836*4882a593Smuzhiyun 				ret);
837*4882a593Smuzhiyun 			goto enable_timers;
838*4882a593Smuzhiyun 		}
839*4882a593Smuzhiyun 	}
840*4882a593Smuzhiyun 
841*4882a593Smuzhiyun 	return 0;
842*4882a593Smuzhiyun 
843*4882a593Smuzhiyun enable_timers:
844*4882a593Smuzhiyun 	/* ignore errors on re-enabling code */
845*4882a593Smuzhiyun 	omap_rproc_enable_timers(rproc, false);
846*4882a593Smuzhiyun enable_device:
847*4882a593Smuzhiyun 	reset_control_deassert(oproc->reset);
848*4882a593Smuzhiyun 	return ret;
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun 
_omap_rproc_resume(struct rproc * rproc,bool auto_suspend)851*4882a593Smuzhiyun static int _omap_rproc_resume(struct rproc *rproc, bool auto_suspend)
852*4882a593Smuzhiyun {
853*4882a593Smuzhiyun 	struct device *dev = rproc->dev.parent;
854*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
855*4882a593Smuzhiyun 	int ret;
856*4882a593Smuzhiyun 
857*4882a593Smuzhiyun 	/*
858*4882a593Smuzhiyun 	 * IOMMUs would have to be enabled specifically for runtime resume.
859*4882a593Smuzhiyun 	 * They would have been already enabled automatically through System
860*4882a593Smuzhiyun 	 * PM callbacks for regular system resume
861*4882a593Smuzhiyun 	 */
862*4882a593Smuzhiyun 	if (auto_suspend) {
863*4882a593Smuzhiyun 		ret = omap_iommu_domain_activate(rproc->domain);
864*4882a593Smuzhiyun 		if (ret) {
865*4882a593Smuzhiyun 			dev_err(dev, "omap_iommu activate failed %d\n", ret);
866*4882a593Smuzhiyun 			goto out;
867*4882a593Smuzhiyun 		}
868*4882a593Smuzhiyun 	}
869*4882a593Smuzhiyun 
870*4882a593Smuzhiyun 	/* boot address could be lost after suspend, so restore it */
871*4882a593Smuzhiyun 	if (oproc->boot_data) {
872*4882a593Smuzhiyun 		ret = omap_rproc_write_dsp_boot_addr(rproc);
873*4882a593Smuzhiyun 		if (ret) {
874*4882a593Smuzhiyun 			dev_err(dev, "boot address restore failed %d\n", ret);
875*4882a593Smuzhiyun 			goto suspend_iommu;
876*4882a593Smuzhiyun 		}
877*4882a593Smuzhiyun 	}
878*4882a593Smuzhiyun 
879*4882a593Smuzhiyun 	ret = omap_rproc_enable_timers(rproc, false);
880*4882a593Smuzhiyun 	if (ret) {
881*4882a593Smuzhiyun 		dev_err(dev, "enabling timers during resume failed %d\n", ret);
882*4882a593Smuzhiyun 		goto suspend_iommu;
883*4882a593Smuzhiyun 	}
884*4882a593Smuzhiyun 
885*4882a593Smuzhiyun 	ret = reset_control_deassert(oproc->reset);
886*4882a593Smuzhiyun 	if (ret) {
887*4882a593Smuzhiyun 		dev_err(dev, "reset deassert during resume failed %d\n", ret);
888*4882a593Smuzhiyun 		goto disable_timers;
889*4882a593Smuzhiyun 	}
890*4882a593Smuzhiyun 
891*4882a593Smuzhiyun 	return 0;
892*4882a593Smuzhiyun 
893*4882a593Smuzhiyun disable_timers:
894*4882a593Smuzhiyun 	omap_rproc_disable_timers(rproc, false);
895*4882a593Smuzhiyun suspend_iommu:
896*4882a593Smuzhiyun 	if (auto_suspend)
897*4882a593Smuzhiyun 		omap_iommu_domain_deactivate(rproc->domain);
898*4882a593Smuzhiyun out:
899*4882a593Smuzhiyun 	return ret;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun 
omap_rproc_suspend(struct device * dev)902*4882a593Smuzhiyun static int __maybe_unused omap_rproc_suspend(struct device *dev)
903*4882a593Smuzhiyun {
904*4882a593Smuzhiyun 	struct platform_device *pdev = to_platform_device(dev);
905*4882a593Smuzhiyun 	struct rproc *rproc = platform_get_drvdata(pdev);
906*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
907*4882a593Smuzhiyun 	int ret = 0;
908*4882a593Smuzhiyun 
909*4882a593Smuzhiyun 	mutex_lock(&rproc->lock);
910*4882a593Smuzhiyun 	if (rproc->state == RPROC_OFFLINE)
911*4882a593Smuzhiyun 		goto out;
912*4882a593Smuzhiyun 
913*4882a593Smuzhiyun 	if (rproc->state == RPROC_SUSPENDED)
914*4882a593Smuzhiyun 		goto out;
915*4882a593Smuzhiyun 
916*4882a593Smuzhiyun 	if (rproc->state != RPROC_RUNNING) {
917*4882a593Smuzhiyun 		ret = -EBUSY;
918*4882a593Smuzhiyun 		goto out;
919*4882a593Smuzhiyun 	}
920*4882a593Smuzhiyun 
921*4882a593Smuzhiyun 	ret = _omap_rproc_suspend(rproc, false);
922*4882a593Smuzhiyun 	if (ret) {
923*4882a593Smuzhiyun 		dev_err(dev, "suspend failed %d\n", ret);
924*4882a593Smuzhiyun 		goto out;
925*4882a593Smuzhiyun 	}
926*4882a593Smuzhiyun 
927*4882a593Smuzhiyun 	/*
928*4882a593Smuzhiyun 	 * remoteproc is running at the time of system suspend, so remember
929*4882a593Smuzhiyun 	 * it so as to wake it up during system resume
930*4882a593Smuzhiyun 	 */
931*4882a593Smuzhiyun 	oproc->need_resume = true;
932*4882a593Smuzhiyun 	rproc->state = RPROC_SUSPENDED;
933*4882a593Smuzhiyun 
934*4882a593Smuzhiyun out:
935*4882a593Smuzhiyun 	mutex_unlock(&rproc->lock);
936*4882a593Smuzhiyun 	return ret;
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun 
omap_rproc_resume(struct device * dev)939*4882a593Smuzhiyun static int __maybe_unused omap_rproc_resume(struct device *dev)
940*4882a593Smuzhiyun {
941*4882a593Smuzhiyun 	struct platform_device *pdev = to_platform_device(dev);
942*4882a593Smuzhiyun 	struct rproc *rproc = platform_get_drvdata(pdev);
943*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
944*4882a593Smuzhiyun 	int ret = 0;
945*4882a593Smuzhiyun 
946*4882a593Smuzhiyun 	mutex_lock(&rproc->lock);
947*4882a593Smuzhiyun 	if (rproc->state == RPROC_OFFLINE)
948*4882a593Smuzhiyun 		goto out;
949*4882a593Smuzhiyun 
950*4882a593Smuzhiyun 	if (rproc->state != RPROC_SUSPENDED) {
951*4882a593Smuzhiyun 		ret = -EBUSY;
952*4882a593Smuzhiyun 		goto out;
953*4882a593Smuzhiyun 	}
954*4882a593Smuzhiyun 
955*4882a593Smuzhiyun 	/*
956*4882a593Smuzhiyun 	 * remoteproc was auto-suspended at the time of system suspend,
957*4882a593Smuzhiyun 	 * so no need to wake-up the processor (leave it in suspended
958*4882a593Smuzhiyun 	 * state, will be woken up during a subsequent runtime_resume)
959*4882a593Smuzhiyun 	 */
960*4882a593Smuzhiyun 	if (!oproc->need_resume)
961*4882a593Smuzhiyun 		goto out;
962*4882a593Smuzhiyun 
963*4882a593Smuzhiyun 	ret = _omap_rproc_resume(rproc, false);
964*4882a593Smuzhiyun 	if (ret) {
965*4882a593Smuzhiyun 		dev_err(dev, "resume failed %d\n", ret);
966*4882a593Smuzhiyun 		goto out;
967*4882a593Smuzhiyun 	}
968*4882a593Smuzhiyun 
969*4882a593Smuzhiyun 	oproc->need_resume = false;
970*4882a593Smuzhiyun 	rproc->state = RPROC_RUNNING;
971*4882a593Smuzhiyun 
972*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(dev);
973*4882a593Smuzhiyun out:
974*4882a593Smuzhiyun 	mutex_unlock(&rproc->lock);
975*4882a593Smuzhiyun 	return ret;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun 
omap_rproc_runtime_suspend(struct device * dev)978*4882a593Smuzhiyun static int omap_rproc_runtime_suspend(struct device *dev)
979*4882a593Smuzhiyun {
980*4882a593Smuzhiyun 	struct rproc *rproc = dev_get_drvdata(dev);
981*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
982*4882a593Smuzhiyun 	int ret;
983*4882a593Smuzhiyun 
984*4882a593Smuzhiyun 	mutex_lock(&rproc->lock);
985*4882a593Smuzhiyun 	if (rproc->state == RPROC_CRASHED) {
986*4882a593Smuzhiyun 		dev_dbg(dev, "rproc cannot be runtime suspended when crashed!\n");
987*4882a593Smuzhiyun 		ret = -EBUSY;
988*4882a593Smuzhiyun 		goto out;
989*4882a593Smuzhiyun 	}
990*4882a593Smuzhiyun 
991*4882a593Smuzhiyun 	if (WARN_ON(rproc->state != RPROC_RUNNING)) {
992*4882a593Smuzhiyun 		dev_err(dev, "rproc cannot be runtime suspended when not running!\n");
993*4882a593Smuzhiyun 		ret = -EBUSY;
994*4882a593Smuzhiyun 		goto out;
995*4882a593Smuzhiyun 	}
996*4882a593Smuzhiyun 
997*4882a593Smuzhiyun 	/*
998*4882a593Smuzhiyun 	 * do not even attempt suspend if the remote processor is not
999*4882a593Smuzhiyun 	 * idled for runtime auto-suspend
1000*4882a593Smuzhiyun 	 */
1001*4882a593Smuzhiyun 	if (!_is_rproc_in_standby(oproc)) {
1002*4882a593Smuzhiyun 		ret = -EBUSY;
1003*4882a593Smuzhiyun 		goto abort;
1004*4882a593Smuzhiyun 	}
1005*4882a593Smuzhiyun 
1006*4882a593Smuzhiyun 	ret = _omap_rproc_suspend(rproc, true);
1007*4882a593Smuzhiyun 	if (ret)
1008*4882a593Smuzhiyun 		goto abort;
1009*4882a593Smuzhiyun 
1010*4882a593Smuzhiyun 	rproc->state = RPROC_SUSPENDED;
1011*4882a593Smuzhiyun 	mutex_unlock(&rproc->lock);
1012*4882a593Smuzhiyun 	return 0;
1013*4882a593Smuzhiyun 
1014*4882a593Smuzhiyun abort:
1015*4882a593Smuzhiyun 	pm_runtime_mark_last_busy(dev);
1016*4882a593Smuzhiyun out:
1017*4882a593Smuzhiyun 	mutex_unlock(&rproc->lock);
1018*4882a593Smuzhiyun 	return ret;
1019*4882a593Smuzhiyun }
1020*4882a593Smuzhiyun 
omap_rproc_runtime_resume(struct device * dev)1021*4882a593Smuzhiyun static int omap_rproc_runtime_resume(struct device *dev)
1022*4882a593Smuzhiyun {
1023*4882a593Smuzhiyun 	struct rproc *rproc = dev_get_drvdata(dev);
1024*4882a593Smuzhiyun 	int ret;
1025*4882a593Smuzhiyun 
1026*4882a593Smuzhiyun 	mutex_lock(&rproc->lock);
1027*4882a593Smuzhiyun 	if (WARN_ON(rproc->state != RPROC_SUSPENDED)) {
1028*4882a593Smuzhiyun 		dev_err(dev, "rproc cannot be runtime resumed if not suspended! state=%d\n",
1029*4882a593Smuzhiyun 			rproc->state);
1030*4882a593Smuzhiyun 		ret = -EBUSY;
1031*4882a593Smuzhiyun 		goto out;
1032*4882a593Smuzhiyun 	}
1033*4882a593Smuzhiyun 
1034*4882a593Smuzhiyun 	ret = _omap_rproc_resume(rproc, true);
1035*4882a593Smuzhiyun 	if (ret) {
1036*4882a593Smuzhiyun 		dev_err(dev, "runtime resume failed %d\n", ret);
1037*4882a593Smuzhiyun 		goto out;
1038*4882a593Smuzhiyun 	}
1039*4882a593Smuzhiyun 
1040*4882a593Smuzhiyun 	rproc->state = RPROC_RUNNING;
1041*4882a593Smuzhiyun out:
1042*4882a593Smuzhiyun 	mutex_unlock(&rproc->lock);
1043*4882a593Smuzhiyun 	return ret;
1044*4882a593Smuzhiyun }
1045*4882a593Smuzhiyun #endif /* CONFIG_PM */
1046*4882a593Smuzhiyun 
1047*4882a593Smuzhiyun static const struct omap_rproc_mem_data ipu_mems[] = {
1048*4882a593Smuzhiyun 	{ .name = "l2ram", .dev_addr = 0x20000000 },
1049*4882a593Smuzhiyun 	{ },
1050*4882a593Smuzhiyun };
1051*4882a593Smuzhiyun 
1052*4882a593Smuzhiyun static const struct omap_rproc_mem_data dra7_dsp_mems[] = {
1053*4882a593Smuzhiyun 	{ .name = "l2ram", .dev_addr = 0x800000 },
1054*4882a593Smuzhiyun 	{ .name = "l1pram", .dev_addr = 0xe00000 },
1055*4882a593Smuzhiyun 	{ .name = "l1dram", .dev_addr = 0xf00000 },
1056*4882a593Smuzhiyun 	{ },
1057*4882a593Smuzhiyun };
1058*4882a593Smuzhiyun 
1059*4882a593Smuzhiyun static const struct omap_rproc_dev_data omap4_dsp_dev_data = {
1060*4882a593Smuzhiyun 	.device_name	= "dsp",
1061*4882a593Smuzhiyun };
1062*4882a593Smuzhiyun 
1063*4882a593Smuzhiyun static const struct omap_rproc_dev_data omap4_ipu_dev_data = {
1064*4882a593Smuzhiyun 	.device_name	= "ipu",
1065*4882a593Smuzhiyun 	.mems		= ipu_mems,
1066*4882a593Smuzhiyun };
1067*4882a593Smuzhiyun 
1068*4882a593Smuzhiyun static const struct omap_rproc_dev_data omap5_dsp_dev_data = {
1069*4882a593Smuzhiyun 	.device_name	= "dsp",
1070*4882a593Smuzhiyun };
1071*4882a593Smuzhiyun 
1072*4882a593Smuzhiyun static const struct omap_rproc_dev_data omap5_ipu_dev_data = {
1073*4882a593Smuzhiyun 	.device_name	= "ipu",
1074*4882a593Smuzhiyun 	.mems		= ipu_mems,
1075*4882a593Smuzhiyun };
1076*4882a593Smuzhiyun 
1077*4882a593Smuzhiyun static const struct omap_rproc_dev_data dra7_dsp_dev_data = {
1078*4882a593Smuzhiyun 	.device_name	= "dsp",
1079*4882a593Smuzhiyun 	.mems		= dra7_dsp_mems,
1080*4882a593Smuzhiyun };
1081*4882a593Smuzhiyun 
1082*4882a593Smuzhiyun static const struct omap_rproc_dev_data dra7_ipu_dev_data = {
1083*4882a593Smuzhiyun 	.device_name	= "ipu",
1084*4882a593Smuzhiyun 	.mems		= ipu_mems,
1085*4882a593Smuzhiyun };
1086*4882a593Smuzhiyun 
1087*4882a593Smuzhiyun static const struct of_device_id omap_rproc_of_match[] = {
1088*4882a593Smuzhiyun 	{
1089*4882a593Smuzhiyun 		.compatible     = "ti,omap4-dsp",
1090*4882a593Smuzhiyun 		.data           = &omap4_dsp_dev_data,
1091*4882a593Smuzhiyun 	},
1092*4882a593Smuzhiyun 	{
1093*4882a593Smuzhiyun 		.compatible     = "ti,omap4-ipu",
1094*4882a593Smuzhiyun 		.data           = &omap4_ipu_dev_data,
1095*4882a593Smuzhiyun 	},
1096*4882a593Smuzhiyun 	{
1097*4882a593Smuzhiyun 		.compatible     = "ti,omap5-dsp",
1098*4882a593Smuzhiyun 		.data           = &omap5_dsp_dev_data,
1099*4882a593Smuzhiyun 	},
1100*4882a593Smuzhiyun 	{
1101*4882a593Smuzhiyun 		.compatible     = "ti,omap5-ipu",
1102*4882a593Smuzhiyun 		.data           = &omap5_ipu_dev_data,
1103*4882a593Smuzhiyun 	},
1104*4882a593Smuzhiyun 	{
1105*4882a593Smuzhiyun 		.compatible     = "ti,dra7-dsp",
1106*4882a593Smuzhiyun 		.data           = &dra7_dsp_dev_data,
1107*4882a593Smuzhiyun 	},
1108*4882a593Smuzhiyun 	{
1109*4882a593Smuzhiyun 		.compatible     = "ti,dra7-ipu",
1110*4882a593Smuzhiyun 		.data           = &dra7_ipu_dev_data,
1111*4882a593Smuzhiyun 	},
1112*4882a593Smuzhiyun 	{
1113*4882a593Smuzhiyun 		/* end */
1114*4882a593Smuzhiyun 	},
1115*4882a593Smuzhiyun };
1116*4882a593Smuzhiyun MODULE_DEVICE_TABLE(of, omap_rproc_of_match);
1117*4882a593Smuzhiyun 
omap_rproc_get_firmware(struct platform_device * pdev)1118*4882a593Smuzhiyun static const char *omap_rproc_get_firmware(struct platform_device *pdev)
1119*4882a593Smuzhiyun {
1120*4882a593Smuzhiyun 	const char *fw_name;
1121*4882a593Smuzhiyun 	int ret;
1122*4882a593Smuzhiyun 
1123*4882a593Smuzhiyun 	ret = of_property_read_string(pdev->dev.of_node, "firmware-name",
1124*4882a593Smuzhiyun 				      &fw_name);
1125*4882a593Smuzhiyun 	if (ret)
1126*4882a593Smuzhiyun 		return ERR_PTR(ret);
1127*4882a593Smuzhiyun 
1128*4882a593Smuzhiyun 	return fw_name;
1129*4882a593Smuzhiyun }
1130*4882a593Smuzhiyun 
omap_rproc_get_boot_data(struct platform_device * pdev,struct rproc * rproc)1131*4882a593Smuzhiyun static int omap_rproc_get_boot_data(struct platform_device *pdev,
1132*4882a593Smuzhiyun 				    struct rproc *rproc)
1133*4882a593Smuzhiyun {
1134*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
1135*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
1136*4882a593Smuzhiyun 	const struct omap_rproc_dev_data *data;
1137*4882a593Smuzhiyun 	int ret;
1138*4882a593Smuzhiyun 
1139*4882a593Smuzhiyun 	data = of_device_get_match_data(&pdev->dev);
1140*4882a593Smuzhiyun 	if (!data)
1141*4882a593Smuzhiyun 		return -ENODEV;
1142*4882a593Smuzhiyun 
1143*4882a593Smuzhiyun 	if (!of_property_read_bool(np, "ti,bootreg"))
1144*4882a593Smuzhiyun 		return 0;
1145*4882a593Smuzhiyun 
1146*4882a593Smuzhiyun 	oproc->boot_data = devm_kzalloc(&pdev->dev, sizeof(*oproc->boot_data),
1147*4882a593Smuzhiyun 					GFP_KERNEL);
1148*4882a593Smuzhiyun 	if (!oproc->boot_data)
1149*4882a593Smuzhiyun 		return -ENOMEM;
1150*4882a593Smuzhiyun 
1151*4882a593Smuzhiyun 	oproc->boot_data->syscon =
1152*4882a593Smuzhiyun 			syscon_regmap_lookup_by_phandle(np, "ti,bootreg");
1153*4882a593Smuzhiyun 	if (IS_ERR(oproc->boot_data->syscon)) {
1154*4882a593Smuzhiyun 		ret = PTR_ERR(oproc->boot_data->syscon);
1155*4882a593Smuzhiyun 		return ret;
1156*4882a593Smuzhiyun 	}
1157*4882a593Smuzhiyun 
1158*4882a593Smuzhiyun 	if (of_property_read_u32_index(np, "ti,bootreg", 1,
1159*4882a593Smuzhiyun 				       &oproc->boot_data->boot_reg)) {
1160*4882a593Smuzhiyun 		dev_err(&pdev->dev, "couldn't get the boot register\n");
1161*4882a593Smuzhiyun 		return -EINVAL;
1162*4882a593Smuzhiyun 	}
1163*4882a593Smuzhiyun 
1164*4882a593Smuzhiyun 	of_property_read_u32_index(np, "ti,bootreg", 2,
1165*4882a593Smuzhiyun 				   &oproc->boot_data->boot_reg_shift);
1166*4882a593Smuzhiyun 
1167*4882a593Smuzhiyun 	return 0;
1168*4882a593Smuzhiyun }
1169*4882a593Smuzhiyun 
omap_rproc_of_get_internal_memories(struct platform_device * pdev,struct rproc * rproc)1170*4882a593Smuzhiyun static int omap_rproc_of_get_internal_memories(struct platform_device *pdev,
1171*4882a593Smuzhiyun 					       struct rproc *rproc)
1172*4882a593Smuzhiyun {
1173*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
1174*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
1175*4882a593Smuzhiyun 	const struct omap_rproc_dev_data *data;
1176*4882a593Smuzhiyun 	struct resource *res;
1177*4882a593Smuzhiyun 	int num_mems;
1178*4882a593Smuzhiyun 	int i;
1179*4882a593Smuzhiyun 
1180*4882a593Smuzhiyun 	data = of_device_get_match_data(dev);
1181*4882a593Smuzhiyun 	if (!data)
1182*4882a593Smuzhiyun 		return -ENODEV;
1183*4882a593Smuzhiyun 
1184*4882a593Smuzhiyun 	if (!data->mems)
1185*4882a593Smuzhiyun 		return 0;
1186*4882a593Smuzhiyun 
1187*4882a593Smuzhiyun 	num_mems = of_property_count_elems_of_size(dev->of_node, "reg",
1188*4882a593Smuzhiyun 						   sizeof(u32)) / 2;
1189*4882a593Smuzhiyun 
1190*4882a593Smuzhiyun 	oproc->mem = devm_kcalloc(dev, num_mems, sizeof(*oproc->mem),
1191*4882a593Smuzhiyun 				  GFP_KERNEL);
1192*4882a593Smuzhiyun 	if (!oproc->mem)
1193*4882a593Smuzhiyun 		return -ENOMEM;
1194*4882a593Smuzhiyun 
1195*4882a593Smuzhiyun 	for (i = 0; data->mems[i].name; i++) {
1196*4882a593Smuzhiyun 		res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
1197*4882a593Smuzhiyun 						   data->mems[i].name);
1198*4882a593Smuzhiyun 		if (!res) {
1199*4882a593Smuzhiyun 			dev_err(dev, "no memory defined for %s\n",
1200*4882a593Smuzhiyun 				data->mems[i].name);
1201*4882a593Smuzhiyun 			return -ENOMEM;
1202*4882a593Smuzhiyun 		}
1203*4882a593Smuzhiyun 		oproc->mem[i].cpu_addr = devm_ioremap_resource(dev, res);
1204*4882a593Smuzhiyun 		if (IS_ERR(oproc->mem[i].cpu_addr)) {
1205*4882a593Smuzhiyun 			dev_err(dev, "failed to parse and map %s memory\n",
1206*4882a593Smuzhiyun 				data->mems[i].name);
1207*4882a593Smuzhiyun 			return PTR_ERR(oproc->mem[i].cpu_addr);
1208*4882a593Smuzhiyun 		}
1209*4882a593Smuzhiyun 		oproc->mem[i].bus_addr = res->start;
1210*4882a593Smuzhiyun 		oproc->mem[i].dev_addr = data->mems[i].dev_addr;
1211*4882a593Smuzhiyun 		oproc->mem[i].size = resource_size(res);
1212*4882a593Smuzhiyun 
1213*4882a593Smuzhiyun 		dev_dbg(dev, "memory %8s: bus addr %pa size 0x%x va %pK da 0x%x\n",
1214*4882a593Smuzhiyun 			data->mems[i].name, &oproc->mem[i].bus_addr,
1215*4882a593Smuzhiyun 			oproc->mem[i].size, oproc->mem[i].cpu_addr,
1216*4882a593Smuzhiyun 			oproc->mem[i].dev_addr);
1217*4882a593Smuzhiyun 	}
1218*4882a593Smuzhiyun 	oproc->num_mems = num_mems;
1219*4882a593Smuzhiyun 
1220*4882a593Smuzhiyun 	return 0;
1221*4882a593Smuzhiyun }
1222*4882a593Smuzhiyun 
1223*4882a593Smuzhiyun #ifdef CONFIG_OMAP_REMOTEPROC_WATCHDOG
omap_rproc_count_wdog_timers(struct device * dev)1224*4882a593Smuzhiyun static int omap_rproc_count_wdog_timers(struct device *dev)
1225*4882a593Smuzhiyun {
1226*4882a593Smuzhiyun 	struct device_node *np = dev->of_node;
1227*4882a593Smuzhiyun 	int ret;
1228*4882a593Smuzhiyun 
1229*4882a593Smuzhiyun 	ret = of_count_phandle_with_args(np, "ti,watchdog-timers", NULL);
1230*4882a593Smuzhiyun 	if (ret <= 0) {
1231*4882a593Smuzhiyun 		dev_dbg(dev, "device does not have watchdog timers, status = %d\n",
1232*4882a593Smuzhiyun 			ret);
1233*4882a593Smuzhiyun 		ret = 0;
1234*4882a593Smuzhiyun 	}
1235*4882a593Smuzhiyun 
1236*4882a593Smuzhiyun 	return ret;
1237*4882a593Smuzhiyun }
1238*4882a593Smuzhiyun #else
omap_rproc_count_wdog_timers(struct device * dev)1239*4882a593Smuzhiyun static int omap_rproc_count_wdog_timers(struct device *dev)
1240*4882a593Smuzhiyun {
1241*4882a593Smuzhiyun 	return 0;
1242*4882a593Smuzhiyun }
1243*4882a593Smuzhiyun #endif
1244*4882a593Smuzhiyun 
omap_rproc_of_get_timers(struct platform_device * pdev,struct rproc * rproc)1245*4882a593Smuzhiyun static int omap_rproc_of_get_timers(struct platform_device *pdev,
1246*4882a593Smuzhiyun 				    struct rproc *rproc)
1247*4882a593Smuzhiyun {
1248*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
1249*4882a593Smuzhiyun 	struct omap_rproc *oproc = rproc->priv;
1250*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
1251*4882a593Smuzhiyun 	int num_timers;
1252*4882a593Smuzhiyun 
1253*4882a593Smuzhiyun 	/*
1254*4882a593Smuzhiyun 	 * Timer nodes are directly used in client nodes as phandles, so
1255*4882a593Smuzhiyun 	 * retrieve the count using appropriate size
1256*4882a593Smuzhiyun 	 */
1257*4882a593Smuzhiyun 	oproc->num_timers = of_count_phandle_with_args(np, "ti,timers", NULL);
1258*4882a593Smuzhiyun 	if (oproc->num_timers <= 0) {
1259*4882a593Smuzhiyun 		dev_dbg(dev, "device does not have timers, status = %d\n",
1260*4882a593Smuzhiyun 			oproc->num_timers);
1261*4882a593Smuzhiyun 		oproc->num_timers = 0;
1262*4882a593Smuzhiyun 	}
1263*4882a593Smuzhiyun 
1264*4882a593Smuzhiyun 	oproc->num_wd_timers = omap_rproc_count_wdog_timers(dev);
1265*4882a593Smuzhiyun 
1266*4882a593Smuzhiyun 	num_timers = oproc->num_timers + oproc->num_wd_timers;
1267*4882a593Smuzhiyun 	if (num_timers) {
1268*4882a593Smuzhiyun 		oproc->timers = devm_kcalloc(dev, num_timers,
1269*4882a593Smuzhiyun 					     sizeof(*oproc->timers),
1270*4882a593Smuzhiyun 					     GFP_KERNEL);
1271*4882a593Smuzhiyun 		if (!oproc->timers)
1272*4882a593Smuzhiyun 			return -ENOMEM;
1273*4882a593Smuzhiyun 
1274*4882a593Smuzhiyun 		dev_dbg(dev, "device has %d tick timers and %d watchdog timers\n",
1275*4882a593Smuzhiyun 			oproc->num_timers, oproc->num_wd_timers);
1276*4882a593Smuzhiyun 	}
1277*4882a593Smuzhiyun 
1278*4882a593Smuzhiyun 	return 0;
1279*4882a593Smuzhiyun }
1280*4882a593Smuzhiyun 
omap_rproc_probe(struct platform_device * pdev)1281*4882a593Smuzhiyun static int omap_rproc_probe(struct platform_device *pdev)
1282*4882a593Smuzhiyun {
1283*4882a593Smuzhiyun 	struct device_node *np = pdev->dev.of_node;
1284*4882a593Smuzhiyun 	struct omap_rproc *oproc;
1285*4882a593Smuzhiyun 	struct rproc *rproc;
1286*4882a593Smuzhiyun 	const char *firmware;
1287*4882a593Smuzhiyun 	int ret;
1288*4882a593Smuzhiyun 	struct reset_control *reset;
1289*4882a593Smuzhiyun 
1290*4882a593Smuzhiyun 	if (!np) {
1291*4882a593Smuzhiyun 		dev_err(&pdev->dev, "only DT-based devices are supported\n");
1292*4882a593Smuzhiyun 		return -ENODEV;
1293*4882a593Smuzhiyun 	}
1294*4882a593Smuzhiyun 
1295*4882a593Smuzhiyun 	reset = devm_reset_control_array_get_exclusive(&pdev->dev);
1296*4882a593Smuzhiyun 	if (IS_ERR(reset))
1297*4882a593Smuzhiyun 		return PTR_ERR(reset);
1298*4882a593Smuzhiyun 
1299*4882a593Smuzhiyun 	firmware = omap_rproc_get_firmware(pdev);
1300*4882a593Smuzhiyun 	if (IS_ERR(firmware))
1301*4882a593Smuzhiyun 		return PTR_ERR(firmware);
1302*4882a593Smuzhiyun 
1303*4882a593Smuzhiyun 	ret = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
1304*4882a593Smuzhiyun 	if (ret) {
1305*4882a593Smuzhiyun 		dev_err(&pdev->dev, "dma_set_coherent_mask: %d\n", ret);
1306*4882a593Smuzhiyun 		return ret;
1307*4882a593Smuzhiyun 	}
1308*4882a593Smuzhiyun 
1309*4882a593Smuzhiyun 	rproc = rproc_alloc(&pdev->dev, dev_name(&pdev->dev), &omap_rproc_ops,
1310*4882a593Smuzhiyun 			    firmware, sizeof(*oproc));
1311*4882a593Smuzhiyun 	if (!rproc)
1312*4882a593Smuzhiyun 		return -ENOMEM;
1313*4882a593Smuzhiyun 
1314*4882a593Smuzhiyun 	oproc = rproc->priv;
1315*4882a593Smuzhiyun 	oproc->rproc = rproc;
1316*4882a593Smuzhiyun 	oproc->reset = reset;
1317*4882a593Smuzhiyun 	/* All existing OMAP IPU and DSP processors have an MMU */
1318*4882a593Smuzhiyun 	rproc->has_iommu = true;
1319*4882a593Smuzhiyun 
1320*4882a593Smuzhiyun 	ret = omap_rproc_of_get_internal_memories(pdev, rproc);
1321*4882a593Smuzhiyun 	if (ret)
1322*4882a593Smuzhiyun 		goto free_rproc;
1323*4882a593Smuzhiyun 
1324*4882a593Smuzhiyun 	ret = omap_rproc_get_boot_data(pdev, rproc);
1325*4882a593Smuzhiyun 	if (ret)
1326*4882a593Smuzhiyun 		goto free_rproc;
1327*4882a593Smuzhiyun 
1328*4882a593Smuzhiyun 	ret = omap_rproc_of_get_timers(pdev, rproc);
1329*4882a593Smuzhiyun 	if (ret)
1330*4882a593Smuzhiyun 		goto free_rproc;
1331*4882a593Smuzhiyun 
1332*4882a593Smuzhiyun 	init_completion(&oproc->pm_comp);
1333*4882a593Smuzhiyun 	oproc->autosuspend_delay = DEFAULT_AUTOSUSPEND_DELAY;
1334*4882a593Smuzhiyun 
1335*4882a593Smuzhiyun 	of_property_read_u32(pdev->dev.of_node, "ti,autosuspend-delay-ms",
1336*4882a593Smuzhiyun 			     &oproc->autosuspend_delay);
1337*4882a593Smuzhiyun 
1338*4882a593Smuzhiyun 	pm_runtime_set_autosuspend_delay(&pdev->dev, oproc->autosuspend_delay);
1339*4882a593Smuzhiyun 
1340*4882a593Smuzhiyun 	oproc->fck = devm_clk_get(&pdev->dev, 0);
1341*4882a593Smuzhiyun 	if (IS_ERR(oproc->fck)) {
1342*4882a593Smuzhiyun 		ret = PTR_ERR(oproc->fck);
1343*4882a593Smuzhiyun 		goto free_rproc;
1344*4882a593Smuzhiyun 	}
1345*4882a593Smuzhiyun 
1346*4882a593Smuzhiyun 	ret = of_reserved_mem_device_init(&pdev->dev);
1347*4882a593Smuzhiyun 	if (ret) {
1348*4882a593Smuzhiyun 		dev_warn(&pdev->dev, "device does not have specific CMA pool.\n");
1349*4882a593Smuzhiyun 		dev_warn(&pdev->dev, "Typically this should be provided,\n");
1350*4882a593Smuzhiyun 		dev_warn(&pdev->dev, "only omit if you know what you are doing.\n");
1351*4882a593Smuzhiyun 	}
1352*4882a593Smuzhiyun 
1353*4882a593Smuzhiyun 	platform_set_drvdata(pdev, rproc);
1354*4882a593Smuzhiyun 
1355*4882a593Smuzhiyun 	ret = rproc_add(rproc);
1356*4882a593Smuzhiyun 	if (ret)
1357*4882a593Smuzhiyun 		goto release_mem;
1358*4882a593Smuzhiyun 
1359*4882a593Smuzhiyun 	return 0;
1360*4882a593Smuzhiyun 
1361*4882a593Smuzhiyun release_mem:
1362*4882a593Smuzhiyun 	of_reserved_mem_device_release(&pdev->dev);
1363*4882a593Smuzhiyun free_rproc:
1364*4882a593Smuzhiyun 	rproc_free(rproc);
1365*4882a593Smuzhiyun 	return ret;
1366*4882a593Smuzhiyun }
1367*4882a593Smuzhiyun 
omap_rproc_remove(struct platform_device * pdev)1368*4882a593Smuzhiyun static int omap_rproc_remove(struct platform_device *pdev)
1369*4882a593Smuzhiyun {
1370*4882a593Smuzhiyun 	struct rproc *rproc = platform_get_drvdata(pdev);
1371*4882a593Smuzhiyun 
1372*4882a593Smuzhiyun 	rproc_del(rproc);
1373*4882a593Smuzhiyun 	rproc_free(rproc);
1374*4882a593Smuzhiyun 	of_reserved_mem_device_release(&pdev->dev);
1375*4882a593Smuzhiyun 
1376*4882a593Smuzhiyun 	return 0;
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun 
1379*4882a593Smuzhiyun static const struct dev_pm_ops omap_rproc_pm_ops = {
1380*4882a593Smuzhiyun 	SET_SYSTEM_SLEEP_PM_OPS(omap_rproc_suspend, omap_rproc_resume)
1381*4882a593Smuzhiyun 	SET_RUNTIME_PM_OPS(omap_rproc_runtime_suspend,
1382*4882a593Smuzhiyun 			   omap_rproc_runtime_resume, NULL)
1383*4882a593Smuzhiyun };
1384*4882a593Smuzhiyun 
1385*4882a593Smuzhiyun static struct platform_driver omap_rproc_driver = {
1386*4882a593Smuzhiyun 	.probe = omap_rproc_probe,
1387*4882a593Smuzhiyun 	.remove = omap_rproc_remove,
1388*4882a593Smuzhiyun 	.driver = {
1389*4882a593Smuzhiyun 		.name = "omap-rproc",
1390*4882a593Smuzhiyun 		.pm = &omap_rproc_pm_ops,
1391*4882a593Smuzhiyun 		.of_match_table = omap_rproc_of_match,
1392*4882a593Smuzhiyun 	},
1393*4882a593Smuzhiyun };
1394*4882a593Smuzhiyun 
1395*4882a593Smuzhiyun module_platform_driver(omap_rproc_driver);
1396*4882a593Smuzhiyun 
1397*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
1398*4882a593Smuzhiyun MODULE_DESCRIPTION("OMAP Remote Processor control driver");
1399