2 * Qualcomm ADSP/SLPI Peripheral Image Loader for MSM8974 and MSM8996
4 * Copyright (C) 2016 Linaro Ltd
5 * Copyright (C) 2014 Sony Mobile Communications AB
6 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk.h>
19 #include <linux/firmware.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/qcom_scm.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/remoteproc.h>
29 #include <linux/soc/qcom/mdt_loader.h>
30 #include <linux/soc/qcom/smem.h>
31 #include <linux/soc/qcom/smem_state.h>
33 #include "qcom_common.h"
34 #include "remoteproc_internal.h"
37 int crash_reason_smem
;
38 const char *firmware_name
;
43 const char *sysmon_name
;
57 struct qcom_smem_state
*state
;
61 struct clk
*aggre2_clk
;
63 struct regulator
*cx_supply
;
64 struct regulator
*px_supply
;
67 int crash_reason_smem
;
70 struct completion start_done
;
71 struct completion stop_done
;
74 phys_addr_t mem_reloc
;
78 struct qcom_rproc_glink glink_subdev
;
79 struct qcom_rproc_subdev smd_subdev
;
80 struct qcom_rproc_ssr ssr_subdev
;
81 struct qcom_sysmon
*sysmon
;
84 static int adsp_load(struct rproc
*rproc
, const struct firmware
*fw
)
86 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
88 return qcom_mdt_load(adsp
->dev
, fw
, rproc
->firmware
, adsp
->pas_id
,
89 adsp
->mem_region
, adsp
->mem_phys
, adsp
->mem_size
,
94 static int adsp_start(struct rproc
*rproc
)
96 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
99 ret
= clk_prepare_enable(adsp
->xo
);
103 ret
= clk_prepare_enable(adsp
->aggre2_clk
);
107 ret
= regulator_enable(adsp
->cx_supply
);
109 goto disable_aggre2_clk
;
111 ret
= regulator_enable(adsp
->px_supply
);
113 goto disable_cx_supply
;
115 ret
= qcom_scm_pas_auth_and_reset(adsp
->pas_id
);
118 "failed to authenticate image and release reset\n");
119 goto disable_px_supply
;
122 ret
= wait_for_completion_timeout(&adsp
->start_done
,
123 msecs_to_jiffies(5000));
125 dev_err(adsp
->dev
, "start timed out\n");
126 qcom_scm_pas_shutdown(adsp
->pas_id
);
128 goto disable_px_supply
;
134 regulator_disable(adsp
->px_supply
);
136 regulator_disable(adsp
->cx_supply
);
138 clk_disable_unprepare(adsp
->aggre2_clk
);
140 clk_disable_unprepare(adsp
->xo
);
145 static int adsp_stop(struct rproc
*rproc
)
147 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
150 qcom_smem_state_update_bits(adsp
->state
,
152 BIT(adsp
->stop_bit
));
154 ret
= wait_for_completion_timeout(&adsp
->stop_done
,
155 msecs_to_jiffies(5000));
157 dev_err(adsp
->dev
, "timed out on wait\n");
159 qcom_smem_state_update_bits(adsp
->state
,
163 ret
= qcom_scm_pas_shutdown(adsp
->pas_id
);
165 dev_err(adsp
->dev
, "failed to shutdown: %d\n", ret
);
170 static void *adsp_da_to_va(struct rproc
*rproc
, u64 da
, int len
)
172 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
175 offset
= da
- adsp
->mem_reloc
;
176 if (offset
< 0 || offset
+ len
> adsp
->mem_size
)
179 return adsp
->mem_region
+ offset
;
182 static const struct rproc_ops adsp_ops
= {
185 .da_to_va
= adsp_da_to_va
,
186 .parse_fw
= qcom_register_dump_segments
,
190 static irqreturn_t
adsp_wdog_interrupt(int irq
, void *dev
)
192 struct qcom_adsp
*adsp
= dev
;
194 rproc_report_crash(adsp
->rproc
, RPROC_WATCHDOG
);
199 static irqreturn_t
adsp_fatal_interrupt(int irq
, void *dev
)
201 struct qcom_adsp
*adsp
= dev
;
205 msg
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, adsp
->crash_reason_smem
, &len
);
206 if (!IS_ERR(msg
) && len
> 0 && msg
[0])
207 dev_err(adsp
->dev
, "fatal error received: %s\n", msg
);
209 rproc_report_crash(adsp
->rproc
, RPROC_FATAL_ERROR
);
214 static irqreturn_t
adsp_ready_interrupt(int irq
, void *dev
)
219 static irqreturn_t
adsp_handover_interrupt(int irq
, void *dev
)
221 struct qcom_adsp
*adsp
= dev
;
223 complete(&adsp
->start_done
);
228 static irqreturn_t
adsp_stop_ack_interrupt(int irq
, void *dev
)
230 struct qcom_adsp
*adsp
= dev
;
232 complete(&adsp
->stop_done
);
237 static int adsp_init_clock(struct qcom_adsp
*adsp
)
241 adsp
->xo
= devm_clk_get(adsp
->dev
, "xo");
242 if (IS_ERR(adsp
->xo
)) {
243 ret
= PTR_ERR(adsp
->xo
);
244 if (ret
!= -EPROBE_DEFER
)
245 dev_err(adsp
->dev
, "failed to get xo clock");
249 if (adsp
->has_aggre2_clk
) {
250 adsp
->aggre2_clk
= devm_clk_get(adsp
->dev
, "aggre2");
251 if (IS_ERR(adsp
->aggre2_clk
)) {
252 ret
= PTR_ERR(adsp
->aggre2_clk
);
253 if (ret
!= -EPROBE_DEFER
)
255 "failed to get aggre2 clock");
263 static int adsp_init_regulator(struct qcom_adsp
*adsp
)
265 adsp
->cx_supply
= devm_regulator_get(adsp
->dev
, "cx");
266 if (IS_ERR(adsp
->cx_supply
))
267 return PTR_ERR(adsp
->cx_supply
);
269 regulator_set_load(adsp
->cx_supply
, 100000);
271 adsp
->px_supply
= devm_regulator_get(adsp
->dev
, "px");
272 return PTR_ERR_OR_ZERO(adsp
->px_supply
);
275 static int adsp_request_irq(struct qcom_adsp
*adsp
,
276 struct platform_device
*pdev
,
278 irq_handler_t thread_fn
)
282 ret
= platform_get_irq_byname(pdev
, name
);
284 dev_err(&pdev
->dev
, "no %s IRQ defined\n", name
);
288 ret
= devm_request_threaded_irq(&pdev
->dev
, ret
,
293 dev_err(&pdev
->dev
, "request %s IRQ failed\n", name
);
298 static int adsp_alloc_memory_region(struct qcom_adsp
*adsp
)
300 struct device_node
*node
;
304 node
= of_parse_phandle(adsp
->dev
->of_node
, "memory-region", 0);
306 dev_err(adsp
->dev
, "no memory-region specified\n");
310 ret
= of_address_to_resource(node
, 0, &r
);
314 adsp
->mem_phys
= adsp
->mem_reloc
= r
.start
;
315 adsp
->mem_size
= resource_size(&r
);
316 adsp
->mem_region
= devm_ioremap_wc(adsp
->dev
, adsp
->mem_phys
, adsp
->mem_size
);
317 if (!adsp
->mem_region
) {
318 dev_err(adsp
->dev
, "unable to map memory region: %pa+%zx\n",
319 &r
.start
, adsp
->mem_size
);
326 static int adsp_probe(struct platform_device
*pdev
)
328 const struct adsp_data
*desc
;
329 struct qcom_adsp
*adsp
;
333 desc
= of_device_get_match_data(&pdev
->dev
);
337 if (!qcom_scm_is_available())
338 return -EPROBE_DEFER
;
340 rproc
= rproc_alloc(&pdev
->dev
, pdev
->name
, &adsp_ops
,
341 desc
->firmware_name
, sizeof(*adsp
));
343 dev_err(&pdev
->dev
, "unable to allocate remoteproc\n");
347 adsp
= (struct qcom_adsp
*)rproc
->priv
;
348 adsp
->dev
= &pdev
->dev
;
350 adsp
->pas_id
= desc
->pas_id
;
351 adsp
->crash_reason_smem
= desc
->crash_reason_smem
;
352 adsp
->has_aggre2_clk
= desc
->has_aggre2_clk
;
353 platform_set_drvdata(pdev
, adsp
);
355 init_completion(&adsp
->start_done
);
356 init_completion(&adsp
->stop_done
);
358 ret
= adsp_alloc_memory_region(adsp
);
362 ret
= adsp_init_clock(adsp
);
366 ret
= adsp_init_regulator(adsp
);
370 ret
= adsp_request_irq(adsp
, pdev
, "wdog", adsp_wdog_interrupt
);
373 adsp
->wdog_irq
= ret
;
375 ret
= adsp_request_irq(adsp
, pdev
, "fatal", adsp_fatal_interrupt
);
378 adsp
->fatal_irq
= ret
;
380 ret
= adsp_request_irq(adsp
, pdev
, "ready", adsp_ready_interrupt
);
383 adsp
->ready_irq
= ret
;
385 ret
= adsp_request_irq(adsp
, pdev
, "handover", adsp_handover_interrupt
);
388 adsp
->handover_irq
= ret
;
390 ret
= adsp_request_irq(adsp
, pdev
, "stop-ack", adsp_stop_ack_interrupt
);
393 adsp
->stop_ack_irq
= ret
;
395 adsp
->state
= qcom_smem_state_get(&pdev
->dev
, "stop",
397 if (IS_ERR(adsp
->state
)) {
398 ret
= PTR_ERR(adsp
->state
);
402 qcom_add_glink_subdev(rproc
, &adsp
->glink_subdev
);
403 qcom_add_smd_subdev(rproc
, &adsp
->smd_subdev
);
404 qcom_add_ssr_subdev(rproc
, &adsp
->ssr_subdev
, desc
->ssr_name
);
405 adsp
->sysmon
= qcom_add_sysmon_subdev(rproc
,
409 ret
= rproc_add(rproc
);
421 static int adsp_remove(struct platform_device
*pdev
)
423 struct qcom_adsp
*adsp
= platform_get_drvdata(pdev
);
425 qcom_smem_state_put(adsp
->state
);
426 rproc_del(adsp
->rproc
);
428 qcom_remove_glink_subdev(adsp
->rproc
, &adsp
->glink_subdev
);
429 qcom_remove_sysmon_subdev(adsp
->sysmon
);
430 qcom_remove_smd_subdev(adsp
->rproc
, &adsp
->smd_subdev
);
431 qcom_remove_ssr_subdev(adsp
->rproc
, &adsp
->ssr_subdev
);
432 rproc_free(adsp
->rproc
);
437 static const struct adsp_data adsp_resource_init
= {
438 .crash_reason_smem
= 423,
439 .firmware_name
= "adsp.mdt",
441 .has_aggre2_clk
= false,
443 .sysmon_name
= "adsp",
447 static const struct adsp_data slpi_resource_init
= {
448 .crash_reason_smem
= 424,
449 .firmware_name
= "slpi.mdt",
451 .has_aggre2_clk
= true,
453 .sysmon_name
= "slpi",
457 static const struct of_device_id adsp_of_match
[] = {
458 { .compatible
= "qcom,msm8974-adsp-pil", .data
= &adsp_resource_init
},
459 { .compatible
= "qcom,msm8996-adsp-pil", .data
= &adsp_resource_init
},
460 { .compatible
= "qcom,msm8996-slpi-pil", .data
= &slpi_resource_init
},
463 MODULE_DEVICE_TABLE(of
, adsp_of_match
);
465 static struct platform_driver adsp_driver
= {
467 .remove
= adsp_remove
,
469 .name
= "qcom_adsp_pil",
470 .of_match_table
= adsp_of_match
,
474 module_platform_driver(adsp_driver
);
475 MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
476 MODULE_LICENSE("GPL v2");