2 * Qualcomm ADSP 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/smem.h>
30 #include <linux/soc/qcom/smem_state.h>
32 #include "qcom_mdt_loader.h"
33 #include "remoteproc_internal.h"
35 #define ADSP_CRASH_REASON_SMEM 423
36 #define ADSP_FIRMWARE_NAME "adsp.mdt"
49 struct qcom_smem_state
*state
;
54 struct regulator
*cx_supply
;
56 struct completion start_done
;
57 struct completion stop_done
;
60 phys_addr_t mem_reloc
;
65 static int adsp_load(struct rproc
*rproc
, const struct firmware
*fw
)
67 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
73 ret
= qcom_scm_pas_init_image(ADSP_PAS_ID
, fw
->data
, fw
->size
);
75 dev_err(&rproc
->dev
, "invalid firmware metadata\n");
79 ret
= qcom_mdt_parse(fw
, &fw_addr
, &fw_size
, &relocate
);
81 dev_err(&rproc
->dev
, "failed to parse mdt header\n");
86 adsp
->mem_reloc
= fw_addr
;
88 ret
= qcom_scm_pas_mem_setup(ADSP_PAS_ID
, adsp
->mem_phys
, fw_size
);
90 dev_err(&rproc
->dev
, "unable to setup memory for image\n");
95 return qcom_mdt_load(rproc
, fw
, rproc
->firmware
);
98 static const struct rproc_fw_ops adsp_fw_ops
= {
99 .find_rsc_table
= qcom_mdt_find_rsc_table
,
103 static int adsp_start(struct rproc
*rproc
)
105 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
108 ret
= clk_prepare_enable(adsp
->xo
);
112 ret
= regulator_enable(adsp
->cx_supply
);
116 ret
= qcom_scm_pas_auth_and_reset(ADSP_PAS_ID
);
119 "failed to authenticate image and release reset\n");
120 goto disable_regulators
;
123 ret
= wait_for_completion_timeout(&adsp
->start_done
,
124 msecs_to_jiffies(5000));
126 dev_err(adsp
->dev
, "start timed out\n");
127 qcom_scm_pas_shutdown(ADSP_PAS_ID
);
129 goto disable_regulators
;
135 regulator_disable(adsp
->cx_supply
);
137 clk_disable_unprepare(adsp
->xo
);
142 static int adsp_stop(struct rproc
*rproc
)
144 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
147 qcom_smem_state_update_bits(adsp
->state
,
149 BIT(adsp
->stop_bit
));
151 ret
= wait_for_completion_timeout(&adsp
->stop_done
,
152 msecs_to_jiffies(5000));
154 dev_err(adsp
->dev
, "timed out on wait\n");
156 qcom_smem_state_update_bits(adsp
->state
,
160 ret
= qcom_scm_pas_shutdown(ADSP_PAS_ID
);
162 dev_err(adsp
->dev
, "failed to shutdown: %d\n", ret
);
167 static void *adsp_da_to_va(struct rproc
*rproc
, u64 da
, int len
)
169 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
172 offset
= da
- adsp
->mem_reloc
;
173 if (offset
< 0 || offset
+ len
> adsp
->mem_size
)
176 return adsp
->mem_region
+ offset
;
179 static const struct rproc_ops adsp_ops
= {
182 .da_to_va
= adsp_da_to_va
,
185 static irqreturn_t
adsp_wdog_interrupt(int irq
, void *dev
)
187 struct qcom_adsp
*adsp
= dev
;
189 rproc_report_crash(adsp
->rproc
, RPROC_WATCHDOG
);
194 static irqreturn_t
adsp_fatal_interrupt(int irq
, void *dev
)
196 struct qcom_adsp
*adsp
= dev
;
200 msg
= qcom_smem_get(QCOM_SMEM_HOST_ANY
, ADSP_CRASH_REASON_SMEM
, &len
);
201 if (!IS_ERR(msg
) && len
> 0 && msg
[0])
202 dev_err(adsp
->dev
, "fatal error received: %s\n", msg
);
204 rproc_report_crash(adsp
->rproc
, RPROC_FATAL_ERROR
);
212 static irqreturn_t
adsp_ready_interrupt(int irq
, void *dev
)
217 static irqreturn_t
adsp_handover_interrupt(int irq
, void *dev
)
219 struct qcom_adsp
*adsp
= dev
;
221 complete(&adsp
->start_done
);
226 static irqreturn_t
adsp_stop_ack_interrupt(int irq
, void *dev
)
228 struct qcom_adsp
*adsp
= dev
;
230 complete(&adsp
->stop_done
);
235 static int adsp_init_clock(struct qcom_adsp
*adsp
)
239 adsp
->xo
= devm_clk_get(adsp
->dev
, "xo");
240 if (IS_ERR(adsp
->xo
)) {
241 ret
= PTR_ERR(adsp
->xo
);
242 if (ret
!= -EPROBE_DEFER
)
243 dev_err(adsp
->dev
, "failed to get xo clock");
250 static int adsp_init_regulator(struct qcom_adsp
*adsp
)
252 adsp
->cx_supply
= devm_regulator_get(adsp
->dev
, "cx");
253 if (IS_ERR(adsp
->cx_supply
))
254 return PTR_ERR(adsp
->cx_supply
);
256 regulator_set_load(adsp
->cx_supply
, 100000);
261 static int adsp_request_irq(struct qcom_adsp
*adsp
,
262 struct platform_device
*pdev
,
264 irq_handler_t thread_fn
)
268 ret
= platform_get_irq_byname(pdev
, name
);
270 dev_err(&pdev
->dev
, "no %s IRQ defined\n", name
);
274 ret
= devm_request_threaded_irq(&pdev
->dev
, ret
,
279 dev_err(&pdev
->dev
, "request %s IRQ failed\n", name
);
284 static int adsp_alloc_memory_region(struct qcom_adsp
*adsp
)
286 struct device_node
*node
;
290 node
= of_parse_phandle(adsp
->dev
->of_node
, "memory-region", 0);
292 dev_err(adsp
->dev
, "no memory-region specified\n");
296 ret
= of_address_to_resource(node
, 0, &r
);
300 adsp
->mem_phys
= adsp
->mem_reloc
= r
.start
;
301 adsp
->mem_size
= resource_size(&r
);
302 adsp
->mem_region
= devm_ioremap_wc(adsp
->dev
, adsp
->mem_phys
, adsp
->mem_size
);
303 if (!adsp
->mem_region
) {
304 dev_err(adsp
->dev
, "unable to map memory region: %pa+%zx\n",
305 &r
.start
, adsp
->mem_size
);
312 static int adsp_probe(struct platform_device
*pdev
)
314 struct qcom_adsp
*adsp
;
318 if (!qcom_scm_is_available())
319 return -EPROBE_DEFER
;
321 if (!qcom_scm_pas_supported(ADSP_PAS_ID
)) {
322 dev_err(&pdev
->dev
, "PAS is not available for ADSP\n");
326 rproc
= rproc_alloc(&pdev
->dev
, pdev
->name
, &adsp_ops
,
327 ADSP_FIRMWARE_NAME
, sizeof(*adsp
));
329 dev_err(&pdev
->dev
, "unable to allocate remoteproc\n");
333 rproc
->fw_ops
= &adsp_fw_ops
;
335 adsp
= (struct qcom_adsp
*)rproc
->priv
;
336 adsp
->dev
= &pdev
->dev
;
338 platform_set_drvdata(pdev
, adsp
);
340 init_completion(&adsp
->start_done
);
341 init_completion(&adsp
->stop_done
);
343 ret
= adsp_alloc_memory_region(adsp
);
347 ret
= adsp_init_clock(adsp
);
351 ret
= adsp_init_regulator(adsp
);
355 ret
= adsp_request_irq(adsp
, pdev
, "wdog", adsp_wdog_interrupt
);
358 adsp
->wdog_irq
= ret
;
360 ret
= adsp_request_irq(adsp
, pdev
, "fatal", adsp_fatal_interrupt
);
363 adsp
->fatal_irq
= ret
;
365 ret
= adsp_request_irq(adsp
, pdev
, "ready", adsp_ready_interrupt
);
368 adsp
->ready_irq
= ret
;
370 ret
= adsp_request_irq(adsp
, pdev
, "handover", adsp_handover_interrupt
);
373 adsp
->handover_irq
= ret
;
375 ret
= adsp_request_irq(adsp
, pdev
, "stop-ack", adsp_stop_ack_interrupt
);
378 adsp
->stop_ack_irq
= ret
;
380 adsp
->state
= qcom_smem_state_get(&pdev
->dev
, "stop",
382 if (IS_ERR(adsp
->state
)) {
383 ret
= PTR_ERR(adsp
->state
);
387 ret
= rproc_add(rproc
);
399 static int adsp_remove(struct platform_device
*pdev
)
401 struct qcom_adsp
*adsp
= platform_get_drvdata(pdev
);
403 qcom_smem_state_put(adsp
->state
);
404 rproc_del(adsp
->rproc
);
405 rproc_free(adsp
->rproc
);
410 static const struct of_device_id adsp_of_match
[] = {
411 { .compatible
= "qcom,msm8974-adsp-pil" },
412 { .compatible
= "qcom,msm8996-adsp-pil" },
415 MODULE_DEVICE_TABLE(of
, adsp_of_match
);
417 static struct platform_driver adsp_driver
= {
419 .remove
= adsp_remove
,
421 .name
= "qcom_adsp_pil",
422 .of_match_table
= adsp_of_match
,
426 module_platform_driver(adsp_driver
);
427 MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
428 MODULE_LICENSE("GPL v2");