1 // SPDX-License-Identifier: GPL-2.0
3 * Qualcomm Technology Inc. ADSP Peripheral Image Loader for SDM845.
4 * Copyright (c) 2018, The Linux Foundation. All rights reserved.
8 #include <linux/delay.h>
9 #include <linux/firmware.h>
10 #include <linux/interrupt.h>
12 #include <linux/iopoll.h>
13 #include <linux/kernel.h>
14 #include <linux/mfd/syscon.h>
15 #include <linux/module.h>
16 #include <linux/of_address.h>
17 #include <linux/of_device.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_domain.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/regmap.h>
22 #include <linux/remoteproc.h>
23 #include <linux/reset.h>
24 #include <linux/soc/qcom/mdt_loader.h>
25 #include <linux/soc/qcom/smem.h>
26 #include <linux/soc/qcom/smem_state.h>
28 #include "qcom_common.h"
29 #include "qcom_pil_info.h"
30 #include "qcom_q6v5.h"
31 #include "remoteproc_internal.h"
34 #define ACK_TIMEOUT 1000
35 #define ACK_TIMEOUT_US 1000000
36 #define BOOT_FSM_TIMEOUT 10000
38 #define EVB_MASK GENMASK(27, 4)
39 /*QDSP6SS register offsets*/
40 #define RST_EVB_REG 0x10
41 #define CORE_START_REG 0x400
42 #define BOOT_CMD_REG 0x404
43 #define BOOT_STATUS_REG 0x408
44 #define RET_CFG_REG 0x1C
45 /*TCSR register offsets*/
46 #define LPASS_MASTER_IDLE_REG 0x8
47 #define LPASS_HALTACK_REG 0x4
48 #define LPASS_PWR_ON_REG 0x10
49 #define LPASS_HALTREQ_REG 0x0
51 #define QDSP6SS_XO_CBCR 0x38
52 #define QDSP6SS_CORE_CBCR 0x20
53 #define QDSP6SS_SLEEP_CBCR 0x3c
55 #define QCOM_Q6V5_RPROC_PROXY_PD_MAX 3
57 struct adsp_pil_data {
58 int crash_reason_smem;
59 const char *firmware_name;
62 const char *sysmon_name;
69 const char **proxy_pd_names;
70 const char *load_state;
77 struct qcom_q6v5 q6v5;
82 struct clk_bulk_data *clks;
84 void __iomem *qdsp6ss_base;
86 struct reset_control *pdc_sync_reset;
87 struct reset_control *restart;
89 struct regmap *halt_map;
90 unsigned int halt_lpass;
92 int crash_reason_smem;
93 const char *info_name;
95 struct completion start_done;
96 struct completion stop_done;
99 phys_addr_t mem_reloc;
103 struct device *proxy_pds[QCOM_Q6V5_RPROC_PROXY_PD_MAX];
104 size_t proxy_pd_count;
106 struct qcom_rproc_glink glink_subdev;
107 struct qcom_rproc_ssr ssr_subdev;
108 struct qcom_sysmon *sysmon;
110 int (*shutdown)(struct qcom_adsp *adsp);
113 static int qcom_rproc_pds_attach(struct device *dev, struct qcom_adsp *adsp,
114 const char **pd_names)
116 struct device **devs = adsp->proxy_pds;
124 /* Handle single power domain */
125 if (dev->pm_domain) {
127 pm_runtime_enable(dev);
131 while (pd_names[num_pds])
134 if (num_pds > ARRAY_SIZE(adsp->proxy_pds))
137 for (i = 0; i < num_pds; i++) {
138 devs[i] = dev_pm_domain_attach_by_name(dev, pd_names[i]);
139 if (IS_ERR_OR_NULL(devs[i])) {
140 ret = PTR_ERR(devs[i]) ? : -ENODATA;
148 for (i--; i >= 0; i--)
149 dev_pm_domain_detach(devs[i], false);
154 static void qcom_rproc_pds_detach(struct qcom_adsp *adsp, struct device **pds,
157 struct device *dev = adsp->dev;
160 /* Handle single power domain */
161 if (dev->pm_domain && pd_count) {
162 pm_runtime_disable(dev);
166 for (i = 0; i < pd_count; i++)
167 dev_pm_domain_detach(pds[i], false);
170 static int qcom_rproc_pds_enable(struct qcom_adsp *adsp, struct device **pds,
176 for (i = 0; i < pd_count; i++) {
177 dev_pm_genpd_set_performance_state(pds[i], INT_MAX);
178 ret = pm_runtime_resume_and_get(pds[i]);
180 dev_pm_genpd_set_performance_state(pds[i], 0);
181 goto unroll_pd_votes;
188 for (i--; i >= 0; i--) {
189 dev_pm_genpd_set_performance_state(pds[i], 0);
190 pm_runtime_put(pds[i]);
196 static void qcom_rproc_pds_disable(struct qcom_adsp *adsp, struct device **pds,
201 for (i = 0; i < pd_count; i++) {
202 dev_pm_genpd_set_performance_state(pds[i], 0);
203 pm_runtime_put(pds[i]);
207 static int qcom_wpss_shutdown(struct qcom_adsp *adsp)
211 regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 1);
213 /* Wait for halt ACK from QDSP6 */
214 regmap_read_poll_timeout(adsp->halt_map,
215 adsp->halt_lpass + LPASS_HALTACK_REG, val,
216 val, 1000, ACK_TIMEOUT_US);
218 /* Assert the WPSS PDC Reset */
219 reset_control_assert(adsp->pdc_sync_reset);
221 /* Place the WPSS processor into reset */
222 reset_control_assert(adsp->restart);
224 /* wait after asserting subsystem restart from AOSS */
225 usleep_range(200, 205);
227 /* Remove the WPSS reset */
228 reset_control_deassert(adsp->restart);
230 /* De-assert the WPSS PDC Reset */
231 reset_control_deassert(adsp->pdc_sync_reset);
233 usleep_range(100, 105);
235 clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
237 regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 0);
239 /* Wait for halt ACK from QDSP6 */
240 regmap_read_poll_timeout(adsp->halt_map,
241 adsp->halt_lpass + LPASS_HALTACK_REG, val,
242 !val, 1000, ACK_TIMEOUT_US);
247 static int qcom_adsp_shutdown(struct qcom_adsp *adsp)
249 unsigned long timeout;
253 /* Reset the retention logic */
254 val = readl(adsp->qdsp6ss_base + RET_CFG_REG);
256 writel(val, adsp->qdsp6ss_base + RET_CFG_REG);
258 clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
260 /* QDSP6 master port needs to be explicitly halted */
261 ret = regmap_read(adsp->halt_map,
262 adsp->halt_lpass + LPASS_PWR_ON_REG, &val);
266 ret = regmap_read(adsp->halt_map,
267 adsp->halt_lpass + LPASS_MASTER_IDLE_REG,
272 regmap_write(adsp->halt_map,
273 adsp->halt_lpass + LPASS_HALTREQ_REG, 1);
275 /* Wait for halt ACK from QDSP6 */
276 timeout = jiffies + msecs_to_jiffies(ACK_TIMEOUT);
278 ret = regmap_read(adsp->halt_map,
279 adsp->halt_lpass + LPASS_HALTACK_REG, &val);
280 if (ret || val || time_after(jiffies, timeout))
283 usleep_range(1000, 1100);
286 ret = regmap_read(adsp->halt_map,
287 adsp->halt_lpass + LPASS_MASTER_IDLE_REG, &val);
289 dev_err(adsp->dev, "port failed halt\n");
292 /* Assert the LPASS PDC Reset */
293 reset_control_assert(adsp->pdc_sync_reset);
294 /* Place the LPASS processor into reset */
295 reset_control_assert(adsp->restart);
296 /* wait after asserting subsystem restart from AOSS */
297 usleep_range(200, 300);
299 /* Clear the halt request for the AXIM and AHBM for Q6 */
300 regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 0);
302 /* De-assert the LPASS PDC Reset */
303 reset_control_deassert(adsp->pdc_sync_reset);
304 /* Remove the LPASS reset */
305 reset_control_deassert(adsp->restart);
306 /* wait after de-asserting subsystem restart from AOSS */
307 usleep_range(200, 300);
312 static int adsp_load(struct rproc *rproc, const struct firmware *fw)
314 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
317 ret = qcom_mdt_load_no_init(adsp->dev, fw, rproc->firmware, 0,
318 adsp->mem_region, adsp->mem_phys,
319 adsp->mem_size, &adsp->mem_reloc);
323 qcom_pil_info_store(adsp->info_name, adsp->mem_phys, adsp->mem_size);
328 static int adsp_start(struct rproc *rproc)
330 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
334 ret = qcom_q6v5_prepare(&adsp->q6v5);
338 ret = clk_prepare_enable(adsp->xo);
342 ret = qcom_rproc_pds_enable(adsp, adsp->proxy_pds,
343 adsp->proxy_pd_count);
347 ret = clk_bulk_prepare_enable(adsp->num_clks, adsp->clks);
349 dev_err(adsp->dev, "adsp clk_enable failed\n");
350 goto disable_power_domain;
353 /* Enable the XO clock */
354 writel(1, adsp->qdsp6ss_base + QDSP6SS_XO_CBCR);
356 /* Enable the QDSP6SS sleep clock */
357 writel(1, adsp->qdsp6ss_base + QDSP6SS_SLEEP_CBCR);
359 /* Enable the QDSP6 core clock */
360 writel(1, adsp->qdsp6ss_base + QDSP6SS_CORE_CBCR);
362 /* Program boot address */
363 writel(adsp->mem_phys >> 4, adsp->qdsp6ss_base + RST_EVB_REG);
365 /* De-assert QDSP6 stop core. QDSP6 will execute after out of reset */
366 writel(0x1, adsp->qdsp6ss_base + CORE_START_REG);
368 /* Trigger boot FSM to start QDSP6 */
369 writel(0x1, adsp->qdsp6ss_base + BOOT_CMD_REG);
371 /* Wait for core to come out of reset */
372 ret = readl_poll_timeout(adsp->qdsp6ss_base + BOOT_STATUS_REG,
373 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
375 dev_err(adsp->dev, "failed to bootup adsp\n");
376 goto disable_adsp_clks;
379 ret = qcom_q6v5_wait_for_start(&adsp->q6v5, msecs_to_jiffies(5 * HZ));
380 if (ret == -ETIMEDOUT) {
381 dev_err(adsp->dev, "start timed out\n");
382 goto disable_adsp_clks;
388 clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
389 disable_power_domain:
390 qcom_rproc_pds_disable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
392 clk_disable_unprepare(adsp->xo);
394 qcom_q6v5_unprepare(&adsp->q6v5);
399 static void qcom_adsp_pil_handover(struct qcom_q6v5 *q6v5)
401 struct qcom_adsp *adsp = container_of(q6v5, struct qcom_adsp, q6v5);
403 clk_disable_unprepare(adsp->xo);
404 qcom_rproc_pds_disable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
407 static int adsp_stop(struct rproc *rproc)
409 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
413 ret = qcom_q6v5_request_stop(&adsp->q6v5, adsp->sysmon);
414 if (ret == -ETIMEDOUT)
415 dev_err(adsp->dev, "timed out on wait\n");
417 ret = adsp->shutdown(adsp);
419 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
421 handover = qcom_q6v5_unprepare(&adsp->q6v5);
423 qcom_adsp_pil_handover(&adsp->q6v5);
428 static void *adsp_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
430 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
433 offset = da - adsp->mem_reloc;
434 if (offset < 0 || offset + len > adsp->mem_size)
437 return adsp->mem_region + offset;
440 static unsigned long adsp_panic(struct rproc *rproc)
442 struct qcom_adsp *adsp = rproc->priv;
444 return qcom_q6v5_panic(&adsp->q6v5);
447 static const struct rproc_ops adsp_ops = {
450 .da_to_va = adsp_da_to_va,
451 .parse_fw = qcom_register_dump_segments,
456 static int adsp_init_clock(struct qcom_adsp *adsp, const char **clk_ids)
461 adsp->xo = devm_clk_get(adsp->dev, "xo");
462 if (IS_ERR(adsp->xo)) {
463 ret = PTR_ERR(adsp->xo);
464 if (ret != -EPROBE_DEFER)
465 dev_err(adsp->dev, "failed to get xo clock");
469 for (i = 0; clk_ids[i]; i++)
472 adsp->num_clks = num_clks;
473 adsp->clks = devm_kcalloc(adsp->dev, adsp->num_clks,
474 sizeof(*adsp->clks), GFP_KERNEL);
478 for (i = 0; i < adsp->num_clks; i++)
479 adsp->clks[i].id = clk_ids[i];
481 return devm_clk_bulk_get(adsp->dev, adsp->num_clks, adsp->clks);
484 static int adsp_init_reset(struct qcom_adsp *adsp)
486 adsp->pdc_sync_reset = devm_reset_control_get_optional_exclusive(adsp->dev,
488 if (IS_ERR(adsp->pdc_sync_reset)) {
489 dev_err(adsp->dev, "failed to acquire pdc_sync reset\n");
490 return PTR_ERR(adsp->pdc_sync_reset);
493 adsp->restart = devm_reset_control_get_optional_exclusive(adsp->dev, "restart");
495 /* Fall back to the old "cc_lpass" if "restart" is absent */
497 adsp->restart = devm_reset_control_get_exclusive(adsp->dev, "cc_lpass");
499 if (IS_ERR(adsp->restart)) {
500 dev_err(adsp->dev, "failed to acquire restart\n");
501 return PTR_ERR(adsp->restart);
507 static int adsp_init_mmio(struct qcom_adsp *adsp,
508 struct platform_device *pdev)
510 struct device_node *syscon;
513 adsp->qdsp6ss_base = devm_platform_ioremap_resource(pdev, 0);
514 if (IS_ERR(adsp->qdsp6ss_base)) {
515 dev_err(adsp->dev, "failed to map QDSP6SS registers\n");
516 return PTR_ERR(adsp->qdsp6ss_base);
519 syscon = of_parse_phandle(pdev->dev.of_node, "qcom,halt-regs", 0);
521 dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
525 adsp->halt_map = syscon_node_to_regmap(syscon);
527 if (IS_ERR(adsp->halt_map))
528 return PTR_ERR(adsp->halt_map);
530 ret = of_property_read_u32_index(pdev->dev.of_node, "qcom,halt-regs",
531 1, &adsp->halt_lpass);
533 dev_err(&pdev->dev, "no offset in syscon\n");
540 static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
542 struct device_node *node;
546 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
548 dev_err(adsp->dev, "no memory-region specified\n");
552 ret = of_address_to_resource(node, 0, &r);
557 adsp->mem_phys = adsp->mem_reloc = r.start;
558 adsp->mem_size = resource_size(&r);
559 adsp->mem_region = devm_ioremap_wc(adsp->dev,
560 adsp->mem_phys, adsp->mem_size);
561 if (!adsp->mem_region) {
562 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
563 &r.start, adsp->mem_size);
570 static int adsp_probe(struct platform_device *pdev)
572 const struct adsp_pil_data *desc;
573 const char *firmware_name;
574 struct qcom_adsp *adsp;
578 desc = of_device_get_match_data(&pdev->dev);
582 firmware_name = desc->firmware_name;
583 ret = of_property_read_string(pdev->dev.of_node, "firmware-name",
585 if (ret < 0 && ret != -EINVAL) {
586 dev_err(&pdev->dev, "unable to read firmware-name\n");
590 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
591 firmware_name, sizeof(*adsp));
593 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
597 rproc->auto_boot = desc->auto_boot;
598 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
600 adsp = (struct qcom_adsp *)rproc->priv;
601 adsp->dev = &pdev->dev;
603 adsp->info_name = desc->sysmon_name;
604 platform_set_drvdata(pdev, adsp);
607 adsp->shutdown = qcom_wpss_shutdown;
609 adsp->shutdown = qcom_adsp_shutdown;
611 ret = adsp_alloc_memory_region(adsp);
615 ret = adsp_init_clock(adsp, desc->clk_ids);
619 ret = qcom_rproc_pds_attach(adsp->dev, adsp,
620 desc->proxy_pd_names);
622 dev_err(&pdev->dev, "Failed to attach proxy power domains\n");
625 adsp->proxy_pd_count = ret;
627 ret = adsp_init_reset(adsp);
631 ret = adsp_init_mmio(adsp, pdev);
635 ret = qcom_q6v5_init(&adsp->q6v5, pdev, rproc, desc->crash_reason_smem,
636 desc->load_state, qcom_adsp_pil_handover);
640 qcom_add_glink_subdev(rproc, &adsp->glink_subdev, desc->ssr_name);
641 qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
642 adsp->sysmon = qcom_add_sysmon_subdev(rproc,
645 if (IS_ERR(adsp->sysmon)) {
646 ret = PTR_ERR(adsp->sysmon);
650 ret = rproc_add(rproc);
657 qcom_rproc_pds_detach(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
665 static int adsp_remove(struct platform_device *pdev)
667 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
669 rproc_del(adsp->rproc);
671 qcom_q6v5_deinit(&adsp->q6v5);
672 qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
673 qcom_remove_sysmon_subdev(adsp->sysmon);
674 qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
675 qcom_rproc_pds_detach(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
676 rproc_free(adsp->rproc);
681 static const struct adsp_pil_data adsp_resource_init = {
682 .crash_reason_smem = 423,
683 .firmware_name = "adsp.mdt",
685 .sysmon_name = "adsp",
689 .clk_ids = (const char*[]) {
690 "sway_cbcr", "lpass_ahbs_aon_cbcr", "lpass_ahbm_aon_cbcr",
691 "qdsp6ss_xo", "qdsp6ss_sleep", "qdsp6ss_core", NULL
694 .proxy_pd_names = (const char*[]) {
699 static const struct adsp_pil_data cdsp_resource_init = {
700 .crash_reason_smem = 601,
701 .firmware_name = "cdsp.mdt",
703 .sysmon_name = "cdsp",
707 .clk_ids = (const char*[]) {
708 "sway", "tbu", "bimc", "ahb_aon", "q6ss_slave", "q6ss_master",
712 .proxy_pd_names = (const char*[]) {
717 static const struct adsp_pil_data wpss_resource_init = {
718 .crash_reason_smem = 626,
719 .firmware_name = "wpss.mdt",
721 .sysmon_name = "wpss",
725 .load_state = "wpss",
726 .clk_ids = (const char*[]) {
727 "ahb_bdg", "ahb", "rscp", NULL
730 .proxy_pd_names = (const char*[]) {
735 static const struct of_device_id adsp_of_match[] = {
736 { .compatible = "qcom,qcs404-cdsp-pil", .data = &cdsp_resource_init },
737 { .compatible = "qcom,sc7280-wpss-pil", .data = &wpss_resource_init },
738 { .compatible = "qcom,sdm845-adsp-pil", .data = &adsp_resource_init },
741 MODULE_DEVICE_TABLE(of, adsp_of_match);
743 static struct platform_driver adsp_pil_driver = {
745 .remove = adsp_remove,
747 .name = "qcom_q6v5_adsp",
748 .of_match_table = adsp_of_match,
752 module_platform_driver(adsp_pil_driver);
753 MODULE_DESCRIPTION("QTI SDM845 ADSP Peripheral Image Loader");
754 MODULE_LICENSE("GPL v2");