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/iommu.h>
13 #include <linux/iopoll.h>
14 #include <linux/kernel.h>
15 #include <linux/mfd/syscon.h>
16 #include <linux/module.h>
18 #include <linux/of_reserved_mem.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_domain.h>
21 #include <linux/pm_runtime.h>
22 #include <linux/regmap.h>
23 #include <linux/remoteproc.h>
24 #include <linux/reset.h>
25 #include <linux/soc/qcom/mdt_loader.h>
26 #include <linux/soc/qcom/smem.h>
27 #include <linux/soc/qcom/smem_state.h>
29 #include "qcom_common.h"
30 #include "qcom_pil_info.h"
31 #include "qcom_q6v5.h"
32 #include "remoteproc_internal.h"
35 #define ACK_TIMEOUT 1000
36 #define ACK_TIMEOUT_US 1000000
37 #define BOOT_FSM_TIMEOUT 10000
39 #define EVB_MASK GENMASK(27, 4)
40 /*QDSP6SS register offsets*/
41 #define RST_EVB_REG 0x10
42 #define CORE_START_REG 0x400
43 #define BOOT_CMD_REG 0x404
44 #define BOOT_STATUS_REG 0x408
45 #define RET_CFG_REG 0x1C
46 /*TCSR register offsets*/
47 #define LPASS_MASTER_IDLE_REG 0x8
48 #define LPASS_HALTACK_REG 0x4
49 #define LPASS_PWR_ON_REG 0x10
50 #define LPASS_HALTREQ_REG 0x0
52 #define SID_MASK_DEFAULT 0xF
54 #define QDSP6SS_XO_CBCR 0x38
55 #define QDSP6SS_CORE_CBCR 0x20
56 #define QDSP6SS_SLEEP_CBCR 0x3c
58 #define QCOM_Q6V5_RPROC_PROXY_PD_MAX 3
60 #define LPASS_BOOT_CORE_START BIT(0)
61 #define LPASS_BOOT_CMD_START BIT(0)
62 #define LPASS_EFUSE_Q6SS_EVB_SEL 0x0
64 struct adsp_pil_data {
65 int crash_reason_smem;
66 const char *firmware_name;
69 const char *sysmon_name;
77 const char **proxy_pd_names;
78 const char *load_state;
85 struct qcom_q6v5 q6v5;
90 struct clk_bulk_data *clks;
92 void __iomem *qdsp6ss_base;
93 void __iomem *lpass_efuse;
95 struct reset_control *pdc_sync_reset;
96 struct reset_control *restart;
98 struct regmap *halt_map;
99 unsigned int halt_lpass;
101 int crash_reason_smem;
102 const char *info_name;
104 struct completion start_done;
105 struct completion stop_done;
107 phys_addr_t mem_phys;
108 phys_addr_t mem_reloc;
113 struct device *proxy_pds[QCOM_Q6V5_RPROC_PROXY_PD_MAX];
114 size_t proxy_pd_count;
116 struct qcom_rproc_glink glink_subdev;
117 struct qcom_rproc_ssr ssr_subdev;
118 struct qcom_sysmon *sysmon;
120 int (*shutdown)(struct qcom_adsp *adsp);
123 static int qcom_rproc_pds_attach(struct device *dev, struct qcom_adsp *adsp,
124 const char **pd_names)
126 struct device **devs = adsp->proxy_pds;
134 /* Handle single power domain */
135 if (dev->pm_domain) {
137 pm_runtime_enable(dev);
141 while (pd_names[num_pds])
144 if (num_pds > ARRAY_SIZE(adsp->proxy_pds))
147 for (i = 0; i < num_pds; i++) {
148 devs[i] = dev_pm_domain_attach_by_name(dev, pd_names[i]);
149 if (IS_ERR_OR_NULL(devs[i])) {
150 ret = PTR_ERR(devs[i]) ? : -ENODATA;
158 for (i--; i >= 0; i--)
159 dev_pm_domain_detach(devs[i], false);
164 static void qcom_rproc_pds_detach(struct qcom_adsp *adsp, struct device **pds,
167 struct device *dev = adsp->dev;
170 /* Handle single power domain */
171 if (dev->pm_domain && pd_count) {
172 pm_runtime_disable(dev);
176 for (i = 0; i < pd_count; i++)
177 dev_pm_domain_detach(pds[i], false);
180 static int qcom_rproc_pds_enable(struct qcom_adsp *adsp, struct device **pds,
186 for (i = 0; i < pd_count; i++) {
187 dev_pm_genpd_set_performance_state(pds[i], INT_MAX);
188 ret = pm_runtime_resume_and_get(pds[i]);
190 dev_pm_genpd_set_performance_state(pds[i], 0);
191 goto unroll_pd_votes;
198 for (i--; i >= 0; i--) {
199 dev_pm_genpd_set_performance_state(pds[i], 0);
200 pm_runtime_put(pds[i]);
206 static void qcom_rproc_pds_disable(struct qcom_adsp *adsp, struct device **pds,
211 for (i = 0; i < pd_count; i++) {
212 dev_pm_genpd_set_performance_state(pds[i], 0);
213 pm_runtime_put(pds[i]);
217 static int qcom_wpss_shutdown(struct qcom_adsp *adsp)
221 regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 1);
223 /* Wait for halt ACK from QDSP6 */
224 regmap_read_poll_timeout(adsp->halt_map,
225 adsp->halt_lpass + LPASS_HALTACK_REG, val,
226 val, 1000, ACK_TIMEOUT_US);
228 /* Assert the WPSS PDC Reset */
229 reset_control_assert(adsp->pdc_sync_reset);
231 /* Place the WPSS processor into reset */
232 reset_control_assert(adsp->restart);
234 /* wait after asserting subsystem restart from AOSS */
235 usleep_range(200, 205);
237 /* Remove the WPSS reset */
238 reset_control_deassert(adsp->restart);
240 /* De-assert the WPSS PDC Reset */
241 reset_control_deassert(adsp->pdc_sync_reset);
243 usleep_range(100, 105);
245 clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
247 regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 0);
249 /* Wait for halt ACK from QDSP6 */
250 regmap_read_poll_timeout(adsp->halt_map,
251 adsp->halt_lpass + LPASS_HALTACK_REG, val,
252 !val, 1000, ACK_TIMEOUT_US);
257 static int qcom_adsp_shutdown(struct qcom_adsp *adsp)
259 unsigned long timeout;
263 /* Reset the retention logic */
264 val = readl(adsp->qdsp6ss_base + RET_CFG_REG);
266 writel(val, adsp->qdsp6ss_base + RET_CFG_REG);
268 clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
270 /* QDSP6 master port needs to be explicitly halted */
271 ret = regmap_read(adsp->halt_map,
272 adsp->halt_lpass + LPASS_PWR_ON_REG, &val);
276 ret = regmap_read(adsp->halt_map,
277 adsp->halt_lpass + LPASS_MASTER_IDLE_REG,
282 regmap_write(adsp->halt_map,
283 adsp->halt_lpass + LPASS_HALTREQ_REG, 1);
285 /* Wait for halt ACK from QDSP6 */
286 timeout = jiffies + msecs_to_jiffies(ACK_TIMEOUT);
288 ret = regmap_read(adsp->halt_map,
289 adsp->halt_lpass + LPASS_HALTACK_REG, &val);
290 if (ret || val || time_after(jiffies, timeout))
293 usleep_range(1000, 1100);
296 ret = regmap_read(adsp->halt_map,
297 adsp->halt_lpass + LPASS_MASTER_IDLE_REG, &val);
299 dev_err(adsp->dev, "port failed halt\n");
302 /* Assert the LPASS PDC Reset */
303 reset_control_assert(adsp->pdc_sync_reset);
304 /* Place the LPASS processor into reset */
305 reset_control_assert(adsp->restart);
306 /* wait after asserting subsystem restart from AOSS */
307 usleep_range(200, 300);
309 /* Clear the halt request for the AXIM and AHBM for Q6 */
310 regmap_write(adsp->halt_map, adsp->halt_lpass + LPASS_HALTREQ_REG, 0);
312 /* De-assert the LPASS PDC Reset */
313 reset_control_deassert(adsp->pdc_sync_reset);
314 /* Remove the LPASS reset */
315 reset_control_deassert(adsp->restart);
316 /* wait after de-asserting subsystem restart from AOSS */
317 usleep_range(200, 300);
322 static int adsp_load(struct rproc *rproc, const struct firmware *fw)
324 struct qcom_adsp *adsp = rproc->priv;
327 ret = qcom_mdt_load_no_init(adsp->dev, fw, rproc->firmware, 0,
328 adsp->mem_region, adsp->mem_phys,
329 adsp->mem_size, &adsp->mem_reloc);
333 qcom_pil_info_store(adsp->info_name, adsp->mem_phys, adsp->mem_size);
338 static void adsp_unmap_carveout(struct rproc *rproc)
340 struct qcom_adsp *adsp = rproc->priv;
343 iommu_unmap(rproc->domain, adsp->mem_phys, adsp->mem_size);
346 static int adsp_map_carveout(struct rproc *rproc)
348 struct qcom_adsp *adsp = rproc->priv;
349 struct of_phandle_args args;
354 if (!adsp->has_iommu)
360 ret = of_parse_phandle_with_args(adsp->dev->of_node, "iommus", "#iommu-cells", 0, &args);
364 sid = args.args[0] & SID_MASK_DEFAULT;
366 /* Add SID configuration for ADSP Firmware to SMMU */
367 iova = adsp->mem_phys | (sid << 32);
369 ret = iommu_map(rproc->domain, iova, adsp->mem_phys,
370 adsp->mem_size, IOMMU_READ | IOMMU_WRITE,
373 dev_err(adsp->dev, "Unable to map ADSP Physical Memory\n");
380 static int adsp_start(struct rproc *rproc)
382 struct qcom_adsp *adsp = rproc->priv;
386 ret = qcom_q6v5_prepare(&adsp->q6v5);
390 ret = adsp_map_carveout(rproc);
392 dev_err(adsp->dev, "ADSP smmu mapping failed\n");
396 ret = clk_prepare_enable(adsp->xo);
398 goto adsp_smmu_unmap;
400 ret = qcom_rproc_pds_enable(adsp, adsp->proxy_pds,
401 adsp->proxy_pd_count);
405 ret = clk_bulk_prepare_enable(adsp->num_clks, adsp->clks);
407 dev_err(adsp->dev, "adsp clk_enable failed\n");
408 goto disable_power_domain;
411 /* Enable the XO clock */
412 writel(1, adsp->qdsp6ss_base + QDSP6SS_XO_CBCR);
414 /* Enable the QDSP6SS sleep clock */
415 writel(1, adsp->qdsp6ss_base + QDSP6SS_SLEEP_CBCR);
417 /* Enable the QDSP6 core clock */
418 writel(1, adsp->qdsp6ss_base + QDSP6SS_CORE_CBCR);
420 /* Program boot address */
421 writel(adsp->mem_phys >> 4, adsp->qdsp6ss_base + RST_EVB_REG);
423 if (adsp->lpass_efuse)
424 writel(LPASS_EFUSE_Q6SS_EVB_SEL, adsp->lpass_efuse);
426 /* De-assert QDSP6 stop core. QDSP6 will execute after out of reset */
427 writel(LPASS_BOOT_CORE_START, adsp->qdsp6ss_base + CORE_START_REG);
429 /* Trigger boot FSM to start QDSP6 */
430 writel(LPASS_BOOT_CMD_START, adsp->qdsp6ss_base + BOOT_CMD_REG);
432 /* Wait for core to come out of reset */
433 ret = readl_poll_timeout(adsp->qdsp6ss_base + BOOT_STATUS_REG,
434 val, (val & BIT(0)) != 0, 10, BOOT_FSM_TIMEOUT);
436 dev_err(adsp->dev, "failed to bootup adsp\n");
437 goto disable_adsp_clks;
440 ret = qcom_q6v5_wait_for_start(&adsp->q6v5, msecs_to_jiffies(5 * HZ));
441 if (ret == -ETIMEDOUT) {
442 dev_err(adsp->dev, "start timed out\n");
443 goto disable_adsp_clks;
449 clk_bulk_disable_unprepare(adsp->num_clks, adsp->clks);
450 disable_power_domain:
451 qcom_rproc_pds_disable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
453 clk_disable_unprepare(adsp->xo);
455 adsp_unmap_carveout(rproc);
457 qcom_q6v5_unprepare(&adsp->q6v5);
462 static void qcom_adsp_pil_handover(struct qcom_q6v5 *q6v5)
464 struct qcom_adsp *adsp = container_of(q6v5, struct qcom_adsp, q6v5);
466 clk_disable_unprepare(adsp->xo);
467 qcom_rproc_pds_disable(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
470 static int adsp_stop(struct rproc *rproc)
472 struct qcom_adsp *adsp = rproc->priv;
476 ret = qcom_q6v5_request_stop(&adsp->q6v5, adsp->sysmon);
477 if (ret == -ETIMEDOUT)
478 dev_err(adsp->dev, "timed out on wait\n");
480 ret = adsp->shutdown(adsp);
482 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
484 adsp_unmap_carveout(rproc);
486 handover = qcom_q6v5_unprepare(&adsp->q6v5);
488 qcom_adsp_pil_handover(&adsp->q6v5);
493 static void *adsp_da_to_va(struct rproc *rproc, u64 da, size_t len, bool *is_iomem)
495 struct qcom_adsp *adsp = rproc->priv;
498 offset = da - adsp->mem_reloc;
499 if (offset < 0 || offset + len > adsp->mem_size)
502 return adsp->mem_region + offset;
505 static int adsp_parse_firmware(struct rproc *rproc, const struct firmware *fw)
507 struct qcom_adsp *adsp = rproc->priv;
510 ret = qcom_register_dump_segments(rproc, fw);
512 dev_err(&rproc->dev, "Error in registering dump segments\n");
516 if (adsp->has_iommu) {
517 ret = rproc_elf_load_rsc_table(rproc, fw);
519 dev_err(&rproc->dev, "Error in loading resource table\n");
526 static unsigned long adsp_panic(struct rproc *rproc)
528 struct qcom_adsp *adsp = rproc->priv;
530 return qcom_q6v5_panic(&adsp->q6v5);
533 static const struct rproc_ops adsp_ops = {
536 .da_to_va = adsp_da_to_va,
537 .parse_fw = adsp_parse_firmware,
542 static int adsp_init_clock(struct qcom_adsp *adsp, const char **clk_ids)
547 adsp->xo = devm_clk_get(adsp->dev, "xo");
548 if (IS_ERR(adsp->xo)) {
549 ret = PTR_ERR(adsp->xo);
550 if (ret != -EPROBE_DEFER)
551 dev_err(adsp->dev, "failed to get xo clock");
555 for (i = 0; clk_ids[i]; i++)
558 adsp->num_clks = num_clks;
559 adsp->clks = devm_kcalloc(adsp->dev, adsp->num_clks,
560 sizeof(*adsp->clks), GFP_KERNEL);
564 for (i = 0; i < adsp->num_clks; i++)
565 adsp->clks[i].id = clk_ids[i];
567 return devm_clk_bulk_get(adsp->dev, adsp->num_clks, adsp->clks);
570 static int adsp_init_reset(struct qcom_adsp *adsp)
572 adsp->pdc_sync_reset = devm_reset_control_get_optional_exclusive(adsp->dev,
574 if (IS_ERR(adsp->pdc_sync_reset)) {
575 dev_err(adsp->dev, "failed to acquire pdc_sync reset\n");
576 return PTR_ERR(adsp->pdc_sync_reset);
579 adsp->restart = devm_reset_control_get_optional_exclusive(adsp->dev, "restart");
581 /* Fall back to the old "cc_lpass" if "restart" is absent */
583 adsp->restart = devm_reset_control_get_exclusive(adsp->dev, "cc_lpass");
585 if (IS_ERR(adsp->restart)) {
586 dev_err(adsp->dev, "failed to acquire restart\n");
587 return PTR_ERR(adsp->restart);
593 static int adsp_init_mmio(struct qcom_adsp *adsp,
594 struct platform_device *pdev)
596 struct resource *efuse_region;
597 struct device_node *syscon;
600 adsp->qdsp6ss_base = devm_platform_ioremap_resource(pdev, 0);
601 if (IS_ERR(adsp->qdsp6ss_base)) {
602 dev_err(adsp->dev, "failed to map QDSP6SS registers\n");
603 return PTR_ERR(adsp->qdsp6ss_base);
606 efuse_region = platform_get_resource(pdev, IORESOURCE_MEM, 1);
608 adsp->lpass_efuse = NULL;
609 dev_dbg(adsp->dev, "failed to get efuse memory region\n");
611 adsp->lpass_efuse = devm_ioremap_resource(&pdev->dev, efuse_region);
612 if (IS_ERR(adsp->lpass_efuse)) {
613 dev_err(adsp->dev, "failed to map efuse registers\n");
614 return PTR_ERR(adsp->lpass_efuse);
617 syscon = of_parse_phandle(pdev->dev.of_node, "qcom,halt-regs", 0);
619 dev_err(&pdev->dev, "failed to parse qcom,halt-regs\n");
623 adsp->halt_map = syscon_node_to_regmap(syscon);
625 if (IS_ERR(adsp->halt_map))
626 return PTR_ERR(adsp->halt_map);
628 ret = of_property_read_u32_index(pdev->dev.of_node, "qcom,halt-regs",
629 1, &adsp->halt_lpass);
631 dev_err(&pdev->dev, "no offset in syscon\n");
638 static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
640 struct reserved_mem *rmem = NULL;
641 struct device_node *node;
643 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
645 rmem = of_reserved_mem_lookup(node);
649 dev_err(adsp->dev, "unable to resolve memory-region\n");
653 adsp->mem_phys = adsp->mem_reloc = rmem->base;
654 adsp->mem_size = rmem->size;
655 adsp->mem_region = devm_ioremap_wc(adsp->dev,
656 adsp->mem_phys, adsp->mem_size);
657 if (!adsp->mem_region) {
658 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
659 &rmem->base, adsp->mem_size);
666 static int adsp_probe(struct platform_device *pdev)
668 const struct adsp_pil_data *desc;
669 const char *firmware_name;
670 struct qcom_adsp *adsp;
674 desc = of_device_get_match_data(&pdev->dev);
678 firmware_name = desc->firmware_name;
679 ret = of_property_read_string(pdev->dev.of_node, "firmware-name",
681 if (ret < 0 && ret != -EINVAL) {
682 dev_err(&pdev->dev, "unable to read firmware-name\n");
686 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
687 firmware_name, sizeof(*adsp));
689 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
693 rproc->auto_boot = desc->auto_boot;
694 rproc->has_iommu = desc->has_iommu;
695 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
698 adsp->dev = &pdev->dev;
700 adsp->info_name = desc->sysmon_name;
701 adsp->has_iommu = desc->has_iommu;
703 platform_set_drvdata(pdev, adsp);
706 adsp->shutdown = qcom_wpss_shutdown;
708 adsp->shutdown = qcom_adsp_shutdown;
710 ret = adsp_alloc_memory_region(adsp);
714 ret = adsp_init_clock(adsp, desc->clk_ids);
718 ret = qcom_rproc_pds_attach(adsp->dev, adsp,
719 desc->proxy_pd_names);
721 dev_err(&pdev->dev, "Failed to attach proxy power domains\n");
724 adsp->proxy_pd_count = ret;
726 ret = adsp_init_reset(adsp);
730 ret = adsp_init_mmio(adsp, pdev);
734 ret = qcom_q6v5_init(&adsp->q6v5, pdev, rproc, desc->crash_reason_smem,
735 desc->load_state, qcom_adsp_pil_handover);
739 qcom_add_glink_subdev(rproc, &adsp->glink_subdev, desc->ssr_name);
740 qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
741 adsp->sysmon = qcom_add_sysmon_subdev(rproc,
744 if (IS_ERR(adsp->sysmon)) {
745 ret = PTR_ERR(adsp->sysmon);
749 ret = rproc_add(rproc);
756 qcom_rproc_pds_detach(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
764 static void adsp_remove(struct platform_device *pdev)
766 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
768 rproc_del(adsp->rproc);
770 qcom_q6v5_deinit(&adsp->q6v5);
771 qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
772 qcom_remove_sysmon_subdev(adsp->sysmon);
773 qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
774 qcom_rproc_pds_detach(adsp, adsp->proxy_pds, adsp->proxy_pd_count);
775 rproc_free(adsp->rproc);
778 static const struct adsp_pil_data adsp_resource_init = {
779 .crash_reason_smem = 423,
780 .firmware_name = "adsp.mdt",
782 .sysmon_name = "adsp",
786 .clk_ids = (const char*[]) {
787 "sway_cbcr", "lpass_ahbs_aon_cbcr", "lpass_ahbm_aon_cbcr",
788 "qdsp6ss_xo", "qdsp6ss_sleep", "qdsp6ss_core", NULL
791 .proxy_pd_names = (const char*[]) {
796 static const struct adsp_pil_data adsp_sc7280_resource_init = {
797 .crash_reason_smem = 423,
798 .firmware_name = "adsp.pbn",
799 .load_state = "adsp",
801 .sysmon_name = "adsp",
805 .clk_ids = (const char*[]) {
806 "gcc_cfg_noc_lpass", NULL
811 static const struct adsp_pil_data cdsp_resource_init = {
812 .crash_reason_smem = 601,
813 .firmware_name = "cdsp.mdt",
815 .sysmon_name = "cdsp",
819 .clk_ids = (const char*[]) {
820 "sway", "tbu", "bimc", "ahb_aon", "q6ss_slave", "q6ss_master",
824 .proxy_pd_names = (const char*[]) {
829 static const struct adsp_pil_data wpss_resource_init = {
830 .crash_reason_smem = 626,
831 .firmware_name = "wpss.mdt",
833 .sysmon_name = "wpss",
837 .load_state = "wpss",
838 .clk_ids = (const char*[]) {
839 "ahb_bdg", "ahb", "rscp", NULL
842 .proxy_pd_names = (const char*[]) {
847 static const struct of_device_id adsp_of_match[] = {
848 { .compatible = "qcom,qcs404-cdsp-pil", .data = &cdsp_resource_init },
849 { .compatible = "qcom,sc7280-adsp-pil", .data = &adsp_sc7280_resource_init },
850 { .compatible = "qcom,sc7280-wpss-pil", .data = &wpss_resource_init },
851 { .compatible = "qcom,sdm845-adsp-pil", .data = &adsp_resource_init },
854 MODULE_DEVICE_TABLE(of, adsp_of_match);
856 static struct platform_driver adsp_pil_driver = {
858 .remove_new = adsp_remove,
860 .name = "qcom_q6v5_adsp",
861 .of_match_table = adsp_of_match,
865 module_platform_driver(adsp_pil_driver);
866 MODULE_DESCRIPTION("QTI SDM845 ADSP Peripheral Image Loader");
867 MODULE_LICENSE("GPL v2");