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 "qcom_q6v5.h"
35 #include "remoteproc_internal.h"
38 int crash_reason_smem
;
39 const char *firmware_name
;
44 const char *sysmon_name
;
52 struct qcom_q6v5 q6v5
;
55 struct clk
*aggre2_clk
;
57 struct regulator
*cx_supply
;
58 struct regulator
*px_supply
;
61 int crash_reason_smem
;
64 struct completion start_done
;
65 struct completion stop_done
;
68 phys_addr_t mem_reloc
;
72 struct qcom_rproc_glink glink_subdev
;
73 struct qcom_rproc_subdev smd_subdev
;
74 struct qcom_rproc_ssr ssr_subdev
;
75 struct qcom_sysmon
*sysmon
;
78 static int adsp_load(struct rproc
*rproc
, const struct firmware
*fw
)
80 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
82 return qcom_mdt_load(adsp
->dev
, fw
, rproc
->firmware
, adsp
->pas_id
,
83 adsp
->mem_region
, adsp
->mem_phys
, adsp
->mem_size
,
88 static int adsp_start(struct rproc
*rproc
)
90 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
93 qcom_q6v5_prepare(&adsp
->q6v5
);
95 ret
= clk_prepare_enable(adsp
->xo
);
99 ret
= clk_prepare_enable(adsp
->aggre2_clk
);
103 ret
= regulator_enable(adsp
->cx_supply
);
105 goto disable_aggre2_clk
;
107 ret
= regulator_enable(adsp
->px_supply
);
109 goto disable_cx_supply
;
111 ret
= qcom_scm_pas_auth_and_reset(adsp
->pas_id
);
114 "failed to authenticate image and release reset\n");
115 goto disable_px_supply
;
118 ret
= qcom_q6v5_wait_for_start(&adsp
->q6v5
, msecs_to_jiffies(5000));
119 if (ret
== -ETIMEDOUT
) {
120 dev_err(adsp
->dev
, "start timed out\n");
121 qcom_scm_pas_shutdown(adsp
->pas_id
);
122 goto disable_px_supply
;
128 regulator_disable(adsp
->px_supply
);
130 regulator_disable(adsp
->cx_supply
);
132 clk_disable_unprepare(adsp
->aggre2_clk
);
134 clk_disable_unprepare(adsp
->xo
);
139 static void qcom_pas_handover(struct qcom_q6v5
*q6v5
)
141 struct qcom_adsp
*adsp
= container_of(q6v5
, struct qcom_adsp
, q6v5
);
143 regulator_disable(adsp
->px_supply
);
144 regulator_disable(adsp
->cx_supply
);
145 clk_disable_unprepare(adsp
->aggre2_clk
);
146 clk_disable_unprepare(adsp
->xo
);
149 static int adsp_stop(struct rproc
*rproc
)
151 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
155 ret
= qcom_q6v5_request_stop(&adsp
->q6v5
);
156 if (ret
== -ETIMEDOUT
)
157 dev_err(adsp
->dev
, "timed out on wait\n");
159 ret
= qcom_scm_pas_shutdown(adsp
->pas_id
);
161 dev_err(adsp
->dev
, "failed to shutdown: %d\n", ret
);
163 handover
= qcom_q6v5_unprepare(&adsp
->q6v5
);
165 qcom_pas_handover(&adsp
->q6v5
);
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 int adsp_init_clock(struct qcom_adsp
*adsp
)
194 adsp
->xo
= devm_clk_get(adsp
->dev
, "xo");
195 if (IS_ERR(adsp
->xo
)) {
196 ret
= PTR_ERR(adsp
->xo
);
197 if (ret
!= -EPROBE_DEFER
)
198 dev_err(adsp
->dev
, "failed to get xo clock");
202 if (adsp
->has_aggre2_clk
) {
203 adsp
->aggre2_clk
= devm_clk_get(adsp
->dev
, "aggre2");
204 if (IS_ERR(adsp
->aggre2_clk
)) {
205 ret
= PTR_ERR(adsp
->aggre2_clk
);
206 if (ret
!= -EPROBE_DEFER
)
208 "failed to get aggre2 clock");
216 static int adsp_init_regulator(struct qcom_adsp
*adsp
)
218 adsp
->cx_supply
= devm_regulator_get(adsp
->dev
, "cx");
219 if (IS_ERR(adsp
->cx_supply
))
220 return PTR_ERR(adsp
->cx_supply
);
222 regulator_set_load(adsp
->cx_supply
, 100000);
224 adsp
->px_supply
= devm_regulator_get(adsp
->dev
, "px");
225 return PTR_ERR_OR_ZERO(adsp
->px_supply
);
228 static int adsp_alloc_memory_region(struct qcom_adsp
*adsp
)
230 struct device_node
*node
;
234 node
= of_parse_phandle(adsp
->dev
->of_node
, "memory-region", 0);
236 dev_err(adsp
->dev
, "no memory-region specified\n");
240 ret
= of_address_to_resource(node
, 0, &r
);
244 adsp
->mem_phys
= adsp
->mem_reloc
= r
.start
;
245 adsp
->mem_size
= resource_size(&r
);
246 adsp
->mem_region
= devm_ioremap_wc(adsp
->dev
, adsp
->mem_phys
, adsp
->mem_size
);
247 if (!adsp
->mem_region
) {
248 dev_err(adsp
->dev
, "unable to map memory region: %pa+%zx\n",
249 &r
.start
, adsp
->mem_size
);
256 static int adsp_probe(struct platform_device
*pdev
)
258 const struct adsp_data
*desc
;
259 struct qcom_adsp
*adsp
;
263 desc
= of_device_get_match_data(&pdev
->dev
);
267 if (!qcom_scm_is_available())
268 return -EPROBE_DEFER
;
270 rproc
= rproc_alloc(&pdev
->dev
, pdev
->name
, &adsp_ops
,
271 desc
->firmware_name
, sizeof(*adsp
));
273 dev_err(&pdev
->dev
, "unable to allocate remoteproc\n");
277 adsp
= (struct qcom_adsp
*)rproc
->priv
;
278 adsp
->dev
= &pdev
->dev
;
280 adsp
->pas_id
= desc
->pas_id
;
281 adsp
->has_aggre2_clk
= desc
->has_aggre2_clk
;
282 platform_set_drvdata(pdev
, adsp
);
284 ret
= adsp_alloc_memory_region(adsp
);
288 ret
= adsp_init_clock(adsp
);
292 ret
= adsp_init_regulator(adsp
);
296 ret
= qcom_q6v5_init(&adsp
->q6v5
, pdev
, rproc
, desc
->crash_reason_smem
,
301 qcom_add_glink_subdev(rproc
, &adsp
->glink_subdev
);
302 qcom_add_smd_subdev(rproc
, &adsp
->smd_subdev
);
303 qcom_add_ssr_subdev(rproc
, &adsp
->ssr_subdev
, desc
->ssr_name
);
304 adsp
->sysmon
= qcom_add_sysmon_subdev(rproc
,
308 ret
= rproc_add(rproc
);
320 static int adsp_remove(struct platform_device
*pdev
)
322 struct qcom_adsp
*adsp
= platform_get_drvdata(pdev
);
324 rproc_del(adsp
->rproc
);
326 qcom_remove_glink_subdev(adsp
->rproc
, &adsp
->glink_subdev
);
327 qcom_remove_sysmon_subdev(adsp
->sysmon
);
328 qcom_remove_smd_subdev(adsp
->rproc
, &adsp
->smd_subdev
);
329 qcom_remove_ssr_subdev(adsp
->rproc
, &adsp
->ssr_subdev
);
330 rproc_free(adsp
->rproc
);
335 static const struct adsp_data adsp_resource_init
= {
336 .crash_reason_smem
= 423,
337 .firmware_name
= "adsp.mdt",
339 .has_aggre2_clk
= false,
341 .sysmon_name
= "adsp",
345 static const struct adsp_data slpi_resource_init
= {
346 .crash_reason_smem
= 424,
347 .firmware_name
= "slpi.mdt",
349 .has_aggre2_clk
= true,
351 .sysmon_name
= "slpi",
355 static const struct of_device_id adsp_of_match
[] = {
356 { .compatible
= "qcom,msm8974-adsp-pil", .data
= &adsp_resource_init
},
357 { .compatible
= "qcom,msm8996-adsp-pil", .data
= &adsp_resource_init
},
358 { .compatible
= "qcom,msm8996-slpi-pil", .data
= &slpi_resource_init
},
361 MODULE_DEVICE_TABLE(of
, adsp_of_match
);
363 static struct platform_driver adsp_driver
= {
365 .remove
= adsp_remove
,
367 .name
= "qcom_adsp_pil",
368 .of_match_table
= adsp_of_match
,
372 module_platform_driver(adsp_driver
);
373 MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
374 MODULE_LICENSE("GPL v2");