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/pm_domain.h>
19 #include <linux/pm_runtime.h>
20 #include <linux/qcom_scm.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/remoteproc.h>
23 #include <linux/soc/qcom/mdt_loader.h>
24 #include <linux/soc/qcom/smem.h>
25 #include <linux/soc/qcom/smem_state.h>
27 #include "qcom_common.h"
28 #include "qcom_q6v5.h"
29 #include "remoteproc_internal.h"
32 int crash_reason_smem;
33 const char *firmware_name;
38 char **active_pd_names;
39 char **proxy_pd_names;
42 const char *sysmon_name;
50 struct qcom_q6v5 q6v5;
53 struct clk *aggre2_clk;
55 struct regulator *cx_supply;
56 struct regulator *px_supply;
58 struct device *active_pds[1];
59 struct device *proxy_pds[3];
65 int crash_reason_smem;
68 struct completion start_done;
69 struct completion stop_done;
72 phys_addr_t mem_reloc;
76 struct qcom_rproc_glink glink_subdev;
77 struct qcom_rproc_subdev smd_subdev;
78 struct qcom_rproc_ssr ssr_subdev;
79 struct qcom_sysmon *sysmon;
82 static int adsp_pds_enable(struct qcom_adsp *adsp, struct device **pds,
88 for (i = 0; i < pd_count; i++) {
89 dev_pm_genpd_set_performance_state(pds[i], INT_MAX);
90 ret = pm_runtime_get_sync(pds[i]);
98 for (i--; i >= 0; i--) {
99 dev_pm_genpd_set_performance_state(pds[i], 0);
100 pm_runtime_put(pds[i]);
106 static void adsp_pds_disable(struct qcom_adsp *adsp, struct device **pds,
111 for (i = 0; i < pd_count; i++) {
112 dev_pm_genpd_set_performance_state(pds[i], 0);
113 pm_runtime_put(pds[i]);
117 static int adsp_load(struct rproc *rproc, const struct firmware *fw)
119 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
121 return qcom_mdt_load(adsp->dev, fw, rproc->firmware, adsp->pas_id,
122 adsp->mem_region, adsp->mem_phys, adsp->mem_size,
127 static int adsp_start(struct rproc *rproc)
129 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
132 qcom_q6v5_prepare(&adsp->q6v5);
134 ret = adsp_pds_enable(adsp, adsp->active_pds, adsp->active_pd_count);
138 ret = adsp_pds_enable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
140 goto disable_active_pds;
142 ret = clk_prepare_enable(adsp->xo);
144 goto disable_proxy_pds;
146 ret = clk_prepare_enable(adsp->aggre2_clk);
150 ret = regulator_enable(adsp->cx_supply);
152 goto disable_aggre2_clk;
154 ret = regulator_enable(adsp->px_supply);
156 goto disable_cx_supply;
158 ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
161 "failed to authenticate image and release reset\n");
162 goto disable_px_supply;
165 ret = qcom_q6v5_wait_for_start(&adsp->q6v5, msecs_to_jiffies(5000));
166 if (ret == -ETIMEDOUT) {
167 dev_err(adsp->dev, "start timed out\n");
168 qcom_scm_pas_shutdown(adsp->pas_id);
169 goto disable_px_supply;
175 regulator_disable(adsp->px_supply);
177 regulator_disable(adsp->cx_supply);
179 clk_disable_unprepare(adsp->aggre2_clk);
181 clk_disable_unprepare(adsp->xo);
183 adsp_pds_disable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
185 adsp_pds_disable(adsp, adsp->active_pds, adsp->active_pd_count);
187 qcom_q6v5_unprepare(&adsp->q6v5);
192 static void qcom_pas_handover(struct qcom_q6v5 *q6v5)
194 struct qcom_adsp *adsp = container_of(q6v5, struct qcom_adsp, q6v5);
196 regulator_disable(adsp->px_supply);
197 regulator_disable(adsp->cx_supply);
198 clk_disable_unprepare(adsp->aggre2_clk);
199 clk_disable_unprepare(adsp->xo);
200 adsp_pds_disable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
203 static int adsp_stop(struct rproc *rproc)
205 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
209 ret = qcom_q6v5_request_stop(&adsp->q6v5);
210 if (ret == -ETIMEDOUT)
211 dev_err(adsp->dev, "timed out on wait\n");
213 ret = qcom_scm_pas_shutdown(adsp->pas_id);
215 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
217 adsp_pds_disable(adsp, adsp->active_pds, adsp->active_pd_count);
218 handover = qcom_q6v5_unprepare(&adsp->q6v5);
220 qcom_pas_handover(&adsp->q6v5);
225 static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
227 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
230 offset = da - adsp->mem_reloc;
231 if (offset < 0 || offset + len > adsp->mem_size)
234 return adsp->mem_region + offset;
237 static const struct rproc_ops adsp_ops = {
240 .da_to_va = adsp_da_to_va,
241 .parse_fw = qcom_register_dump_segments,
245 static int adsp_init_clock(struct qcom_adsp *adsp)
249 adsp->xo = devm_clk_get(adsp->dev, "xo");
250 if (IS_ERR(adsp->xo)) {
251 ret = PTR_ERR(adsp->xo);
252 if (ret != -EPROBE_DEFER)
253 dev_err(adsp->dev, "failed to get xo clock");
257 if (adsp->has_aggre2_clk) {
258 adsp->aggre2_clk = devm_clk_get(adsp->dev, "aggre2");
259 if (IS_ERR(adsp->aggre2_clk)) {
260 ret = PTR_ERR(adsp->aggre2_clk);
261 if (ret != -EPROBE_DEFER)
263 "failed to get aggre2 clock");
271 static int adsp_init_regulator(struct qcom_adsp *adsp)
273 adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
274 if (IS_ERR(adsp->cx_supply))
275 return PTR_ERR(adsp->cx_supply);
277 regulator_set_load(adsp->cx_supply, 100000);
279 adsp->px_supply = devm_regulator_get(adsp->dev, "px");
280 return PTR_ERR_OR_ZERO(adsp->px_supply);
283 static int adsp_pds_attach(struct device *dev, struct device **devs,
293 /* Handle single power domain */
294 if (dev->pm_domain) {
296 pm_runtime_enable(dev);
300 while (pd_names[num_pds])
303 for (i = 0; i < num_pds; i++) {
304 devs[i] = dev_pm_domain_attach_by_name(dev, pd_names[i]);
305 if (IS_ERR_OR_NULL(devs[i])) {
306 ret = PTR_ERR(devs[i]) ? : -ENODATA;
314 for (i--; i >= 0; i--)
315 dev_pm_domain_detach(devs[i], false);
320 static void adsp_pds_detach(struct qcom_adsp *adsp, struct device **pds,
323 struct device *dev = adsp->dev;
326 /* Handle single power domain */
327 if (dev->pm_domain && pd_count) {
328 pm_runtime_disable(dev);
332 for (i = 0; i < pd_count; i++)
333 dev_pm_domain_detach(pds[i], false);
336 static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
338 struct device_node *node;
342 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
344 dev_err(adsp->dev, "no memory-region specified\n");
348 ret = of_address_to_resource(node, 0, &r);
352 adsp->mem_phys = adsp->mem_reloc = r.start;
353 adsp->mem_size = resource_size(&r);
354 adsp->mem_region = devm_ioremap_wc(adsp->dev, adsp->mem_phys, adsp->mem_size);
355 if (!adsp->mem_region) {
356 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
357 &r.start, adsp->mem_size);
364 static int adsp_probe(struct platform_device *pdev)
366 const struct adsp_data *desc;
367 struct qcom_adsp *adsp;
372 desc = of_device_get_match_data(&pdev->dev);
376 if (!qcom_scm_is_available())
377 return -EPROBE_DEFER;
379 fw_name = desc->firmware_name;
380 ret = of_property_read_string(pdev->dev.of_node, "firmware-name",
382 if (ret < 0 && ret != -EINVAL)
385 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
386 fw_name, sizeof(*adsp));
388 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
392 rproc->auto_boot = desc->auto_boot;
394 adsp = (struct qcom_adsp *)rproc->priv;
395 adsp->dev = &pdev->dev;
397 adsp->pas_id = desc->pas_id;
398 adsp->has_aggre2_clk = desc->has_aggre2_clk;
399 platform_set_drvdata(pdev, adsp);
401 ret = adsp_alloc_memory_region(adsp);
405 ret = adsp_init_clock(adsp);
409 ret = adsp_init_regulator(adsp);
413 ret = adsp_pds_attach(&pdev->dev, adsp->active_pds,
414 desc->active_pd_names);
417 adsp->active_pd_count = ret;
419 ret = adsp_pds_attach(&pdev->dev, adsp->proxy_pds,
420 desc->proxy_pd_names);
422 goto detach_active_pds;
423 adsp->proxy_pd_count = ret;
425 ret = qcom_q6v5_init(&adsp->q6v5, pdev, rproc, desc->crash_reason_smem,
428 goto detach_proxy_pds;
430 qcom_add_glink_subdev(rproc, &adsp->glink_subdev);
431 qcom_add_smd_subdev(rproc, &adsp->smd_subdev);
432 qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
433 adsp->sysmon = qcom_add_sysmon_subdev(rproc,
436 if (IS_ERR(adsp->sysmon)) {
437 ret = PTR_ERR(adsp->sysmon);
438 goto detach_proxy_pds;
441 ret = rproc_add(rproc);
443 goto detach_proxy_pds;
448 adsp_pds_detach(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
450 adsp_pds_detach(adsp, adsp->active_pds, adsp->active_pd_count);
457 static int adsp_remove(struct platform_device *pdev)
459 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
461 rproc_del(adsp->rproc);
463 qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
464 qcom_remove_sysmon_subdev(adsp->sysmon);
465 qcom_remove_smd_subdev(adsp->rproc, &adsp->smd_subdev);
466 qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
467 rproc_free(adsp->rproc);
472 static const struct adsp_data adsp_resource_init = {
473 .crash_reason_smem = 423,
474 .firmware_name = "adsp.mdt",
476 .has_aggre2_clk = false,
479 .sysmon_name = "adsp",
483 static const struct adsp_data sm8150_adsp_resource = {
484 .crash_reason_smem = 423,
485 .firmware_name = "adsp.mdt",
487 .has_aggre2_clk = false,
489 .active_pd_names = (char*[]){
493 .proxy_pd_names = (char*[]){
498 .sysmon_name = "adsp",
502 static const struct adsp_data msm8998_adsp_resource = {
503 .crash_reason_smem = 423,
504 .firmware_name = "adsp.mdt",
506 .has_aggre2_clk = false,
508 .proxy_pd_names = (char*[]){
513 .sysmon_name = "adsp",
517 static const struct adsp_data cdsp_resource_init = {
518 .crash_reason_smem = 601,
519 .firmware_name = "cdsp.mdt",
521 .has_aggre2_clk = false,
524 .sysmon_name = "cdsp",
528 static const struct adsp_data sm8150_cdsp_resource = {
529 .crash_reason_smem = 601,
530 .firmware_name = "cdsp.mdt",
532 .has_aggre2_clk = false,
534 .active_pd_names = (char*[]){
538 .proxy_pd_names = (char*[]){
543 .sysmon_name = "cdsp",
547 static const struct adsp_data mpss_resource_init = {
548 .crash_reason_smem = 421,
549 .firmware_name = "modem.mdt",
551 .has_aggre2_clk = false,
553 .active_pd_names = (char*[]){
557 .proxy_pd_names = (char*[]){
563 .sysmon_name = "modem",
567 static const struct adsp_data slpi_resource_init = {
568 .crash_reason_smem = 424,
569 .firmware_name = "slpi.mdt",
571 .has_aggre2_clk = true,
574 .sysmon_name = "slpi",
578 static const struct adsp_data sm8150_slpi_resource = {
579 .crash_reason_smem = 424,
580 .firmware_name = "slpi.mdt",
582 .has_aggre2_clk = false,
584 .active_pd_names = (char*[]){
588 .proxy_pd_names = (char*[]){
594 .sysmon_name = "slpi",
598 static const struct adsp_data msm8998_slpi_resource = {
599 .crash_reason_smem = 424,
600 .firmware_name = "slpi.mdt",
602 .has_aggre2_clk = true,
604 .proxy_pd_names = (char*[]){
609 .sysmon_name = "slpi",
613 static const struct adsp_data wcss_resource_init = {
614 .crash_reason_smem = 421,
615 .firmware_name = "wcnss.mdt",
619 .sysmon_name = "wcnss",
623 static const struct of_device_id adsp_of_match[] = {
624 { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
625 { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
626 { .compatible = "qcom,msm8996-slpi-pil", .data = &slpi_resource_init},
627 { .compatible = "qcom,msm8998-adsp-pas", .data = &msm8998_adsp_resource},
628 { .compatible = "qcom,msm8998-slpi-pas", .data = &msm8998_slpi_resource},
629 { .compatible = "qcom,qcs404-adsp-pas", .data = &adsp_resource_init },
630 { .compatible = "qcom,qcs404-cdsp-pas", .data = &cdsp_resource_init },
631 { .compatible = "qcom,qcs404-wcss-pas", .data = &wcss_resource_init },
632 { .compatible = "qcom,sdm845-adsp-pas", .data = &adsp_resource_init},
633 { .compatible = "qcom,sdm845-cdsp-pas", .data = &cdsp_resource_init},
634 { .compatible = "qcom,sm8150-adsp-pas", .data = &sm8150_adsp_resource},
635 { .compatible = "qcom,sm8150-cdsp-pas", .data = &sm8150_cdsp_resource},
636 { .compatible = "qcom,sm8150-mpss-pas", .data = &mpss_resource_init},
637 { .compatible = "qcom,sm8150-slpi-pas", .data = &sm8150_slpi_resource},
640 MODULE_DEVICE_TABLE(of, adsp_of_match);
642 static struct platform_driver adsp_driver = {
644 .remove = adsp_remove,
646 .name = "qcom_q6v5_pas",
647 .of_match_table = adsp_of_match,
651 module_platform_driver(adsp_driver);
652 MODULE_DESCRIPTION("Qualcomm Hexagon v5 Peripheral Authentication Service driver");
653 MODULE_LICENSE("GPL v2");