1 // SPDX-License-Identifier: GPL-2.0
3 * Qualcomm Peripheral Image Loader for Q6V5
5 * Copyright (C) 2016-2018 Linaro Ltd.
6 * Copyright (C) 2014 Sony Mobile Communications AB
7 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
9 #include <linux/kernel.h>
10 #include <linux/platform_device.h>
11 #include <linux/interrupt.h>
12 #include <linux/module.h>
13 #include <linux/soc/qcom/smem.h>
14 #include <linux/soc/qcom/smem_state.h>
15 #include <linux/remoteproc.h>
16 #include "qcom_q6v5.h"
18 #define Q6V5_PANIC_DELAY_MS 200
21 * qcom_q6v5_prepare() - reinitialize the qcom_q6v5 context before start
22 * @q6v5: reference to qcom_q6v5 context to be reinitialized
24 * Return: 0 on success, negative errno on failure
26 int qcom_q6v5_prepare(struct qcom_q6v5
*q6v5
)
28 reinit_completion(&q6v5
->start_done
);
29 reinit_completion(&q6v5
->stop_done
);
32 q6v5
->handover_issued
= false;
34 enable_irq(q6v5
->handover_irq
);
38 EXPORT_SYMBOL_GPL(qcom_q6v5_prepare
);
41 * qcom_q6v5_unprepare() - unprepare the qcom_q6v5 context after stop
42 * @q6v5: reference to qcom_q6v5 context to be unprepared
44 * Return: 0 on success, 1 if handover hasn't yet been called
46 int qcom_q6v5_unprepare(struct qcom_q6v5
*q6v5
)
48 disable_irq(q6v5
->handover_irq
);
50 return !q6v5
->handover_issued
;
52 EXPORT_SYMBOL_GPL(qcom_q6v5_unprepare
);
54 static irqreturn_t
q6v5_wdog_interrupt(int irq
, void *data
)
56 struct qcom_q6v5
*q6v5
= data
;
60 /* Sometimes the stop triggers a watchdog rather than a stop-ack */
62 complete(&q6v5
->stop_done
);
66 msg
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, q6v5
->crash_reason
, &len
);
67 if (!IS_ERR(msg
) && len
> 0 && msg
[0])
68 dev_err(q6v5
->dev
, "watchdog received: %s\n", msg
);
70 dev_err(q6v5
->dev
, "watchdog without message\n");
72 rproc_report_crash(q6v5
->rproc
, RPROC_WATCHDOG
);
77 static irqreturn_t
q6v5_fatal_interrupt(int irq
, void *data
)
79 struct qcom_q6v5
*q6v5
= data
;
83 msg
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, q6v5
->crash_reason
, &len
);
84 if (!IS_ERR(msg
) && len
> 0 && msg
[0])
85 dev_err(q6v5
->dev
, "fatal error received: %s\n", msg
);
87 dev_err(q6v5
->dev
, "fatal error without message\n");
89 q6v5
->running
= false;
90 rproc_report_crash(q6v5
->rproc
, RPROC_FATAL_ERROR
);
95 static irqreturn_t
q6v5_ready_interrupt(int irq
, void *data
)
97 struct qcom_q6v5
*q6v5
= data
;
99 complete(&q6v5
->start_done
);
105 * qcom_q6v5_wait_for_start() - wait for remote processor start signal
106 * @q6v5: reference to qcom_q6v5 context
107 * @timeout: timeout to wait for the event, in jiffies
109 * qcom_q6v5_unprepare() should not be called when this function fails.
111 * Return: 0 on success, -ETIMEDOUT on timeout
113 int qcom_q6v5_wait_for_start(struct qcom_q6v5
*q6v5
, int timeout
)
117 ret
= wait_for_completion_timeout(&q6v5
->start_done
, timeout
);
119 disable_irq(q6v5
->handover_irq
);
121 return !ret
? -ETIMEDOUT
: 0;
123 EXPORT_SYMBOL_GPL(qcom_q6v5_wait_for_start
);
125 static irqreturn_t
q6v5_handover_interrupt(int irq
, void *data
)
127 struct qcom_q6v5
*q6v5
= data
;
130 q6v5
->handover(q6v5
);
132 q6v5
->handover_issued
= true;
137 static irqreturn_t
q6v5_stop_interrupt(int irq
, void *data
)
139 struct qcom_q6v5
*q6v5
= data
;
141 complete(&q6v5
->stop_done
);
147 * qcom_q6v5_request_stop() - request the remote processor to stop
148 * @q6v5: reference to qcom_q6v5 context
150 * Return: 0 on success, negative errno on failure
152 int qcom_q6v5_request_stop(struct qcom_q6v5
*q6v5
)
156 qcom_smem_state_update_bits(q6v5
->state
,
157 BIT(q6v5
->stop_bit
), BIT(q6v5
->stop_bit
));
159 ret
= wait_for_completion_timeout(&q6v5
->stop_done
, 5 * HZ
);
161 qcom_smem_state_update_bits(q6v5
->state
, BIT(q6v5
->stop_bit
), 0);
163 return ret
== 0 ? -ETIMEDOUT
: 0;
165 EXPORT_SYMBOL_GPL(qcom_q6v5_request_stop
);
168 * qcom_q6v5_panic() - panic handler to invoke a stop on the remote
169 * @q6v5: reference to qcom_q6v5 context
171 * Set the stop bit and sleep in order to allow the remote processor to flush
172 * its caches etc for post mortem debugging.
176 unsigned long qcom_q6v5_panic(struct qcom_q6v5
*q6v5
)
178 qcom_smem_state_update_bits(q6v5
->state
,
179 BIT(q6v5
->stop_bit
), BIT(q6v5
->stop_bit
));
181 return Q6V5_PANIC_DELAY_MS
;
183 EXPORT_SYMBOL_GPL(qcom_q6v5_panic
);
186 * qcom_q6v5_init() - initializer of the q6v5 common struct
187 * @q6v5: handle to be initialized
188 * @pdev: platform_device reference for acquiring resources
189 * @rproc: associated remoteproc instance
190 * @crash_reason: SMEM id for crash reason string, or 0 if none
191 * @handover: function to be called when proxy resources should be released
193 * Return: 0 on success, negative errno on failure
195 int qcom_q6v5_init(struct qcom_q6v5
*q6v5
, struct platform_device
*pdev
,
196 struct rproc
*rproc
, int crash_reason
,
197 void (*handover
)(struct qcom_q6v5
*q6v5
))
202 q6v5
->dev
= &pdev
->dev
;
203 q6v5
->crash_reason
= crash_reason
;
204 q6v5
->handover
= handover
;
206 init_completion(&q6v5
->start_done
);
207 init_completion(&q6v5
->stop_done
);
209 q6v5
->wdog_irq
= platform_get_irq_byname(pdev
, "wdog");
210 if (q6v5
->wdog_irq
< 0)
211 return q6v5
->wdog_irq
;
213 ret
= devm_request_threaded_irq(&pdev
->dev
, q6v5
->wdog_irq
,
214 NULL
, q6v5_wdog_interrupt
,
215 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
218 dev_err(&pdev
->dev
, "failed to acquire wdog IRQ\n");
222 q6v5
->fatal_irq
= platform_get_irq_byname(pdev
, "fatal");
223 if (q6v5
->fatal_irq
< 0)
224 return q6v5
->fatal_irq
;
226 ret
= devm_request_threaded_irq(&pdev
->dev
, q6v5
->fatal_irq
,
227 NULL
, q6v5_fatal_interrupt
,
228 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
231 dev_err(&pdev
->dev
, "failed to acquire fatal IRQ\n");
235 q6v5
->ready_irq
= platform_get_irq_byname(pdev
, "ready");
236 if (q6v5
->ready_irq
< 0)
237 return q6v5
->ready_irq
;
239 ret
= devm_request_threaded_irq(&pdev
->dev
, q6v5
->ready_irq
,
240 NULL
, q6v5_ready_interrupt
,
241 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
244 dev_err(&pdev
->dev
, "failed to acquire ready IRQ\n");
248 q6v5
->handover_irq
= platform_get_irq_byname(pdev
, "handover");
249 if (q6v5
->handover_irq
< 0)
250 return q6v5
->handover_irq
;
252 ret
= devm_request_threaded_irq(&pdev
->dev
, q6v5
->handover_irq
,
253 NULL
, q6v5_handover_interrupt
,
254 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
255 "q6v5 handover", q6v5
);
257 dev_err(&pdev
->dev
, "failed to acquire handover IRQ\n");
260 disable_irq(q6v5
->handover_irq
);
262 q6v5
->stop_irq
= platform_get_irq_byname(pdev
, "stop-ack");
263 if (q6v5
->stop_irq
< 0)
264 return q6v5
->stop_irq
;
266 ret
= devm_request_threaded_irq(&pdev
->dev
, q6v5
->stop_irq
,
267 NULL
, q6v5_stop_interrupt
,
268 IRQF_TRIGGER_RISING
| IRQF_ONESHOT
,
271 dev_err(&pdev
->dev
, "failed to acquire stop-ack IRQ\n");
275 q6v5
->state
= qcom_smem_state_get(&pdev
->dev
, "stop", &q6v5
->stop_bit
);
276 if (IS_ERR(q6v5
->state
)) {
277 dev_err(&pdev
->dev
, "failed to acquire stop state\n");
278 return PTR_ERR(q6v5
->state
);
283 EXPORT_SYMBOL_GPL(qcom_q6v5_init
);
285 MODULE_LICENSE("GPL v2");
286 MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Q6V5");