1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm ADSP/SLPI Peripheral Image Loader for MSM8974 and MSM8996
5 * Copyright (C) 2016 Linaro Ltd
6 * Copyright (C) 2014 Sony Mobile Communications AB
7 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
10 #include <linux/clk.h>
11 #include <linux/firmware.h>
12 #include <linux/interrupt.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/of_address.h>
16 #include <linux/of_device.h>
17 #include <linux/platform_device.h>
18 #include <linux/qcom_scm.h>
19 #include <linux/regulator/consumer.h>
20 #include <linux/remoteproc.h>
21 #include <linux/soc/qcom/mdt_loader.h>
22 #include <linux/soc/qcom/smem.h>
23 #include <linux/soc/qcom/smem_state.h>
25 #include "qcom_common.h"
26 #include "qcom_q6v5.h"
27 #include "remoteproc_internal.h"
30 int crash_reason_smem
;
31 const char *firmware_name
;
36 const char *sysmon_name
;
44 struct qcom_q6v5 q6v5
;
47 struct clk
*aggre2_clk
;
49 struct regulator
*cx_supply
;
50 struct regulator
*px_supply
;
53 int crash_reason_smem
;
56 struct completion start_done
;
57 struct completion stop_done
;
60 phys_addr_t mem_reloc
;
64 struct qcom_rproc_glink glink_subdev
;
65 struct qcom_rproc_subdev smd_subdev
;
66 struct qcom_rproc_ssr ssr_subdev
;
67 struct qcom_sysmon
*sysmon
;
70 static int adsp_load(struct rproc
*rproc
, const struct firmware
*fw
)
72 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
74 return qcom_mdt_load(adsp
->dev
, fw
, rproc
->firmware
, adsp
->pas_id
,
75 adsp
->mem_region
, adsp
->mem_phys
, adsp
->mem_size
,
80 static int adsp_start(struct rproc
*rproc
)
82 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
85 qcom_q6v5_prepare(&adsp
->q6v5
);
87 ret
= clk_prepare_enable(adsp
->xo
);
91 ret
= clk_prepare_enable(adsp
->aggre2_clk
);
95 ret
= regulator_enable(adsp
->cx_supply
);
97 goto disable_aggre2_clk
;
99 ret
= regulator_enable(adsp
->px_supply
);
101 goto disable_cx_supply
;
103 ret
= qcom_scm_pas_auth_and_reset(adsp
->pas_id
);
106 "failed to authenticate image and release reset\n");
107 goto disable_px_supply
;
110 ret
= qcom_q6v5_wait_for_start(&adsp
->q6v5
, msecs_to_jiffies(5000));
111 if (ret
== -ETIMEDOUT
) {
112 dev_err(adsp
->dev
, "start timed out\n");
113 qcom_scm_pas_shutdown(adsp
->pas_id
);
114 goto disable_px_supply
;
120 regulator_disable(adsp
->px_supply
);
122 regulator_disable(adsp
->cx_supply
);
124 clk_disable_unprepare(adsp
->aggre2_clk
);
126 clk_disable_unprepare(adsp
->xo
);
131 static void qcom_pas_handover(struct qcom_q6v5
*q6v5
)
133 struct qcom_adsp
*adsp
= container_of(q6v5
, struct qcom_adsp
, q6v5
);
135 regulator_disable(adsp
->px_supply
);
136 regulator_disable(adsp
->cx_supply
);
137 clk_disable_unprepare(adsp
->aggre2_clk
);
138 clk_disable_unprepare(adsp
->xo
);
141 static int adsp_stop(struct rproc
*rproc
)
143 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
147 ret
= qcom_q6v5_request_stop(&adsp
->q6v5
);
148 if (ret
== -ETIMEDOUT
)
149 dev_err(adsp
->dev
, "timed out on wait\n");
151 ret
= qcom_scm_pas_shutdown(adsp
->pas_id
);
153 dev_err(adsp
->dev
, "failed to shutdown: %d\n", ret
);
155 handover
= qcom_q6v5_unprepare(&adsp
->q6v5
);
157 qcom_pas_handover(&adsp
->q6v5
);
162 static void *adsp_da_to_va(struct rproc
*rproc
, u64 da
, int len
)
164 struct qcom_adsp
*adsp
= (struct qcom_adsp
*)rproc
->priv
;
167 offset
= da
- adsp
->mem_reloc
;
168 if (offset
< 0 || offset
+ len
> adsp
->mem_size
)
171 return adsp
->mem_region
+ offset
;
174 static const struct rproc_ops adsp_ops
= {
177 .da_to_va
= adsp_da_to_va
,
178 .parse_fw
= qcom_register_dump_segments
,
182 static int adsp_init_clock(struct qcom_adsp
*adsp
)
186 adsp
->xo
= devm_clk_get(adsp
->dev
, "xo");
187 if (IS_ERR(adsp
->xo
)) {
188 ret
= PTR_ERR(adsp
->xo
);
189 if (ret
!= -EPROBE_DEFER
)
190 dev_err(adsp
->dev
, "failed to get xo clock");
194 if (adsp
->has_aggre2_clk
) {
195 adsp
->aggre2_clk
= devm_clk_get(adsp
->dev
, "aggre2");
196 if (IS_ERR(adsp
->aggre2_clk
)) {
197 ret
= PTR_ERR(adsp
->aggre2_clk
);
198 if (ret
!= -EPROBE_DEFER
)
200 "failed to get aggre2 clock");
208 static int adsp_init_regulator(struct qcom_adsp
*adsp
)
210 adsp
->cx_supply
= devm_regulator_get(adsp
->dev
, "cx");
211 if (IS_ERR(adsp
->cx_supply
))
212 return PTR_ERR(adsp
->cx_supply
);
214 regulator_set_load(adsp
->cx_supply
, 100000);
216 adsp
->px_supply
= devm_regulator_get(adsp
->dev
, "px");
217 return PTR_ERR_OR_ZERO(adsp
->px_supply
);
220 static int adsp_alloc_memory_region(struct qcom_adsp
*adsp
)
222 struct device_node
*node
;
226 node
= of_parse_phandle(adsp
->dev
->of_node
, "memory-region", 0);
228 dev_err(adsp
->dev
, "no memory-region specified\n");
232 ret
= of_address_to_resource(node
, 0, &r
);
236 adsp
->mem_phys
= adsp
->mem_reloc
= r
.start
;
237 adsp
->mem_size
= resource_size(&r
);
238 adsp
->mem_region
= devm_ioremap_wc(adsp
->dev
, adsp
->mem_phys
, adsp
->mem_size
);
239 if (!adsp
->mem_region
) {
240 dev_err(adsp
->dev
, "unable to map memory region: %pa+%zx\n",
241 &r
.start
, adsp
->mem_size
);
248 static int adsp_probe(struct platform_device
*pdev
)
250 const struct adsp_data
*desc
;
251 struct qcom_adsp
*adsp
;
256 desc
= of_device_get_match_data(&pdev
->dev
);
260 if (!qcom_scm_is_available())
261 return -EPROBE_DEFER
;
263 fw_name
= desc
->firmware_name
;
264 ret
= of_property_read_string(pdev
->dev
.of_node
, "firmware-name",
266 if (ret
< 0 && ret
!= -EINVAL
)
269 rproc
= rproc_alloc(&pdev
->dev
, pdev
->name
, &adsp_ops
,
270 fw_name
, sizeof(*adsp
));
272 dev_err(&pdev
->dev
, "unable to allocate remoteproc\n");
276 adsp
= (struct qcom_adsp
*)rproc
->priv
;
277 adsp
->dev
= &pdev
->dev
;
279 adsp
->pas_id
= desc
->pas_id
;
280 adsp
->has_aggre2_clk
= desc
->has_aggre2_clk
;
281 platform_set_drvdata(pdev
, adsp
);
283 ret
= adsp_alloc_memory_region(adsp
);
287 ret
= adsp_init_clock(adsp
);
291 ret
= adsp_init_regulator(adsp
);
295 ret
= qcom_q6v5_init(&adsp
->q6v5
, pdev
, rproc
, desc
->crash_reason_smem
,
300 qcom_add_glink_subdev(rproc
, &adsp
->glink_subdev
);
301 qcom_add_smd_subdev(rproc
, &adsp
->smd_subdev
);
302 qcom_add_ssr_subdev(rproc
, &adsp
->ssr_subdev
, desc
->ssr_name
);
303 adsp
->sysmon
= qcom_add_sysmon_subdev(rproc
,
306 if (IS_ERR(adsp
->sysmon
)) {
307 ret
= PTR_ERR(adsp
->sysmon
);
311 ret
= rproc_add(rproc
);
323 static int adsp_remove(struct platform_device
*pdev
)
325 struct qcom_adsp
*adsp
= platform_get_drvdata(pdev
);
327 rproc_del(adsp
->rproc
);
329 qcom_remove_glink_subdev(adsp
->rproc
, &adsp
->glink_subdev
);
330 qcom_remove_sysmon_subdev(adsp
->sysmon
);
331 qcom_remove_smd_subdev(adsp
->rproc
, &adsp
->smd_subdev
);
332 qcom_remove_ssr_subdev(adsp
->rproc
, &adsp
->ssr_subdev
);
333 rproc_free(adsp
->rproc
);
338 static const struct adsp_data adsp_resource_init
= {
339 .crash_reason_smem
= 423,
340 .firmware_name
= "adsp.mdt",
342 .has_aggre2_clk
= false,
344 .sysmon_name
= "adsp",
348 static const struct adsp_data cdsp_resource_init
= {
349 .crash_reason_smem
= 601,
350 .firmware_name
= "cdsp.mdt",
352 .has_aggre2_clk
= false,
354 .sysmon_name
= "cdsp",
358 static const struct adsp_data slpi_resource_init
= {
359 .crash_reason_smem
= 424,
360 .firmware_name
= "slpi.mdt",
362 .has_aggre2_clk
= true,
364 .sysmon_name
= "slpi",
368 static const struct adsp_data wcss_resource_init
= {
369 .crash_reason_smem
= 421,
370 .firmware_name
= "wcnss.mdt",
373 .sysmon_name
= "wcnss",
377 static const struct of_device_id adsp_of_match
[] = {
378 { .compatible
= "qcom,msm8974-adsp-pil", .data
= &adsp_resource_init
},
379 { .compatible
= "qcom,msm8996-adsp-pil", .data
= &adsp_resource_init
},
380 { .compatible
= "qcom,msm8996-slpi-pil", .data
= &slpi_resource_init
},
381 { .compatible
= "qcom,qcs404-adsp-pas", .data
= &adsp_resource_init
},
382 { .compatible
= "qcom,qcs404-cdsp-pas", .data
= &cdsp_resource_init
},
383 { .compatible
= "qcom,qcs404-wcss-pas", .data
= &wcss_resource_init
},
384 { .compatible
= "qcom,sdm845-adsp-pas", .data
= &adsp_resource_init
},
385 { .compatible
= "qcom,sdm845-cdsp-pas", .data
= &cdsp_resource_init
},
388 MODULE_DEVICE_TABLE(of
, adsp_of_match
);
390 static struct platform_driver adsp_driver
= {
392 .remove
= adsp_remove
,
394 .name
= "qcom_q6v5_pas",
395 .of_match_table
= adsp_of_match
,
399 module_platform_driver(adsp_driver
);
400 MODULE_DESCRIPTION("Qualcomm Hexagon v5 Peripheral Authentication Service driver");
401 MODULE_LICENSE("GPL v2");