1 // SPDX-License-Identifier: GPL-2.0-only
3 * Qualcomm Wireless Connectivity Subsystem Peripheral Image Loader
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/delay.h>
12 #include <linux/firmware.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
17 #include <linux/of_address.h>
18 #include <linux/of_device.h>
19 #include <linux/platform_device.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>
26 #include <linux/rpmsg/qcom_smd.h>
28 #include "qcom_common.h"
29 #include "remoteproc_internal.h"
30 #include "qcom_pil_info.h"
31 #include "qcom_wcnss.h"
33 #define WCNSS_CRASH_REASON_SMEM 422
34 #define WCNSS_FIRMWARE_NAME "/*(DEBLOBBED)*/"
35 #define WCNSS_PAS_ID 6
36 #define WCNSS_SSCTL_ID 0x13
38 #define WCNSS_SPARE_NVBIN_DLND BIT(25)
40 #define WCNSS_PMU_IRIS_XO_CFG BIT(3)
41 #define WCNSS_PMU_IRIS_XO_EN BIT(4)
42 #define WCNSS_PMU_GC_BUS_MUX_SEL_TOP BIT(5)
43 #define WCNSS_PMU_IRIS_XO_CFG_STS BIT(6) /* 1: in progress, 0: done */
45 #define WCNSS_PMU_IRIS_RESET BIT(7)
46 #define WCNSS_PMU_IRIS_RESET_STS BIT(8) /* 1: in progress, 0: done */
47 #define WCNSS_PMU_IRIS_XO_READ BIT(9)
48 #define WCNSS_PMU_IRIS_XO_READ_STS BIT(10)
50 #define WCNSS_PMU_XO_MODE_MASK GENMASK(2, 1)
51 #define WCNSS_PMU_XO_MODE_19p2 0
52 #define WCNSS_PMU_XO_MODE_48 3
58 const struct wcnss_vreg_info *vregs;
66 void __iomem *pmu_cfg;
67 void __iomem *spare_out;
77 struct qcom_smem_state *state;
80 struct mutex iris_lock;
81 struct qcom_iris *iris;
83 struct regulator_bulk_data *vregs;
86 struct completion start_done;
87 struct completion stop_done;
90 phys_addr_t mem_reloc;
94 struct qcom_rproc_subdev smd_subdev;
95 struct qcom_sysmon *sysmon;
98 static const struct wcnss_data riva_data = {
100 .spare_offset = 0xb4,
102 .vregs = (struct wcnss_vreg_info[]) {
103 { "vddmx", 1050000, 1150000, 0 },
104 { "vddcx", 1050000, 1150000, 0 },
105 { "vddpx", 1800000, 1800000, 0 },
110 static const struct wcnss_data pronto_v1_data = {
111 .pmu_offset = 0x1004,
112 .spare_offset = 0x1088,
114 .vregs = (struct wcnss_vreg_info[]) {
115 { "vddmx", 950000, 1150000, 0 },
116 { "vddcx", .super_turbo = true},
117 { "vddpx", 1800000, 1800000, 0 },
122 static const struct wcnss_data pronto_v2_data = {
123 .pmu_offset = 0x1004,
124 .spare_offset = 0x1088,
126 .vregs = (struct wcnss_vreg_info[]) {
127 { "vddmx", 1287500, 1287500, 0 },
128 { "vddcx", .super_turbo = true },
129 { "vddpx", 1800000, 1800000, 0 },
134 void qcom_wcnss_assign_iris(struct qcom_wcnss *wcnss,
135 struct qcom_iris *iris,
138 mutex_lock(&wcnss->iris_lock);
141 wcnss->use_48mhz_xo = use_48mhz_xo;
143 mutex_unlock(&wcnss->iris_lock);
146 static int wcnss_load(struct rproc *rproc, const struct firmware *fw)
148 struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
151 ret = qcom_mdt_load(wcnss->dev, fw, rproc->firmware, WCNSS_PAS_ID,
152 wcnss->mem_region, wcnss->mem_phys,
153 wcnss->mem_size, &wcnss->mem_reloc);
157 qcom_pil_info_store("wcnss", wcnss->mem_phys, wcnss->mem_size);
162 static void wcnss_indicate_nv_download(struct qcom_wcnss *wcnss)
166 /* Indicate NV download capability */
167 val = readl(wcnss->spare_out);
168 val |= WCNSS_SPARE_NVBIN_DLND;
169 writel(val, wcnss->spare_out);
172 static void wcnss_configure_iris(struct qcom_wcnss *wcnss)
176 /* Clear PMU cfg register */
177 writel(0, wcnss->pmu_cfg);
179 val = WCNSS_PMU_GC_BUS_MUX_SEL_TOP | WCNSS_PMU_IRIS_XO_EN;
180 writel(val, wcnss->pmu_cfg);
183 val &= ~WCNSS_PMU_XO_MODE_MASK;
184 if (wcnss->use_48mhz_xo)
185 val |= WCNSS_PMU_XO_MODE_48 << 1;
187 val |= WCNSS_PMU_XO_MODE_19p2 << 1;
188 writel(val, wcnss->pmu_cfg);
191 val |= WCNSS_PMU_IRIS_RESET;
192 writel(val, wcnss->pmu_cfg);
194 /* Wait for PMU.iris_reg_reset_sts */
195 while (readl(wcnss->pmu_cfg) & WCNSS_PMU_IRIS_RESET_STS)
198 /* Clear IRIS reset */
199 val &= ~WCNSS_PMU_IRIS_RESET;
200 writel(val, wcnss->pmu_cfg);
202 /* Start IRIS XO configuration */
203 val |= WCNSS_PMU_IRIS_XO_CFG;
204 writel(val, wcnss->pmu_cfg);
206 /* Wait for XO configuration to finish */
207 while (readl(wcnss->pmu_cfg) & WCNSS_PMU_IRIS_XO_CFG_STS)
210 /* Stop IRIS XO configuration */
211 val &= ~WCNSS_PMU_GC_BUS_MUX_SEL_TOP;
212 val &= ~WCNSS_PMU_IRIS_XO_CFG;
213 writel(val, wcnss->pmu_cfg);
215 /* Add some delay for XO to settle */
219 static int wcnss_start(struct rproc *rproc)
221 struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
224 mutex_lock(&wcnss->iris_lock);
226 dev_err(wcnss->dev, "no iris registered\n");
228 goto release_iris_lock;
231 ret = regulator_bulk_enable(wcnss->num_vregs, wcnss->vregs);
233 goto release_iris_lock;
235 ret = qcom_iris_enable(wcnss->iris);
237 goto disable_regulators;
239 wcnss_indicate_nv_download(wcnss);
240 wcnss_configure_iris(wcnss);
242 ret = qcom_scm_pas_auth_and_reset(WCNSS_PAS_ID);
245 "failed to authenticate image and release reset\n");
249 ret = wait_for_completion_timeout(&wcnss->start_done,
250 msecs_to_jiffies(5000));
251 if (wcnss->ready_irq > 0 && ret == 0) {
252 /* We have a ready_irq, but it didn't fire in time. */
253 dev_err(wcnss->dev, "start timed out\n");
254 qcom_scm_pas_shutdown(WCNSS_PAS_ID);
262 qcom_iris_disable(wcnss->iris);
264 regulator_bulk_disable(wcnss->num_vregs, wcnss->vregs);
266 mutex_unlock(&wcnss->iris_lock);
271 static int wcnss_stop(struct rproc *rproc)
273 struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
277 qcom_smem_state_update_bits(wcnss->state,
278 BIT(wcnss->stop_bit),
279 BIT(wcnss->stop_bit));
281 ret = wait_for_completion_timeout(&wcnss->stop_done,
282 msecs_to_jiffies(5000));
284 dev_err(wcnss->dev, "timed out on wait\n");
286 qcom_smem_state_update_bits(wcnss->state,
287 BIT(wcnss->stop_bit),
291 ret = qcom_scm_pas_shutdown(WCNSS_PAS_ID);
293 dev_err(wcnss->dev, "failed to shutdown: %d\n", ret);
298 static void *wcnss_da_to_va(struct rproc *rproc, u64 da, size_t len)
300 struct qcom_wcnss *wcnss = (struct qcom_wcnss *)rproc->priv;
303 offset = da - wcnss->mem_reloc;
304 if (offset < 0 || offset + len > wcnss->mem_size)
307 return wcnss->mem_region + offset;
310 static const struct rproc_ops wcnss_ops = {
311 .start = wcnss_start,
313 .da_to_va = wcnss_da_to_va,
314 .parse_fw = qcom_register_dump_segments,
318 static irqreturn_t wcnss_wdog_interrupt(int irq, void *dev)
320 struct qcom_wcnss *wcnss = dev;
322 rproc_report_crash(wcnss->rproc, RPROC_WATCHDOG);
327 static irqreturn_t wcnss_fatal_interrupt(int irq, void *dev)
329 struct qcom_wcnss *wcnss = dev;
333 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, WCNSS_CRASH_REASON_SMEM, &len);
334 if (!IS_ERR(msg) && len > 0 && msg[0])
335 dev_err(wcnss->dev, "fatal error received: %s\n", msg);
337 rproc_report_crash(wcnss->rproc, RPROC_FATAL_ERROR);
342 static irqreturn_t wcnss_ready_interrupt(int irq, void *dev)
344 struct qcom_wcnss *wcnss = dev;
346 complete(&wcnss->start_done);
351 static irqreturn_t wcnss_handover_interrupt(int irq, void *dev)
354 * XXX: At this point we're supposed to release the resources that we
355 * have been holding on behalf of the WCNSS. Unfortunately this
356 * interrupt comes way before the other side seems to be done.
358 * So we're currently relying on the ready interrupt firing later then
359 * this and we just disable the resources at the end of wcnss_start().
365 static irqreturn_t wcnss_stop_ack_interrupt(int irq, void *dev)
367 struct qcom_wcnss *wcnss = dev;
369 complete(&wcnss->stop_done);
374 static int wcnss_init_regulators(struct qcom_wcnss *wcnss,
375 const struct wcnss_vreg_info *info,
378 struct regulator_bulk_data *bulk;
382 bulk = devm_kcalloc(wcnss->dev,
383 num_vregs, sizeof(struct regulator_bulk_data),
388 for (i = 0; i < num_vregs; i++)
389 bulk[i].supply = info[i].name;
391 ret = devm_regulator_bulk_get(wcnss->dev, num_vregs, bulk);
395 for (i = 0; i < num_vregs; i++) {
396 if (info[i].max_voltage)
397 regulator_set_voltage(bulk[i].consumer,
399 info[i].max_voltage);
402 regulator_set_load(bulk[i].consumer, info[i].load_uA);
406 wcnss->num_vregs = num_vregs;
411 static int wcnss_request_irq(struct qcom_wcnss *wcnss,
412 struct platform_device *pdev,
415 irq_handler_t thread_fn)
420 ret = platform_get_irq_byname(pdev, name);
421 if (ret < 0 && optional) {
422 dev_dbg(&pdev->dev, "no %s IRQ defined, ignoring\n", name);
424 } else if (ret < 0) {
425 dev_err(&pdev->dev, "no %s IRQ defined\n", name);
431 ret = devm_request_threaded_irq(&pdev->dev, ret,
433 IRQF_TRIGGER_RISING | IRQF_ONESHOT,
436 dev_err(&pdev->dev, "request %s IRQ failed\n", name);
440 /* Return the IRQ number if the IRQ was successfully acquired */
444 static int wcnss_alloc_memory_region(struct qcom_wcnss *wcnss)
446 struct device_node *node;
450 node = of_parse_phandle(wcnss->dev->of_node, "memory-region", 0);
452 dev_err(wcnss->dev, "no memory-region specified\n");
456 ret = of_address_to_resource(node, 0, &r);
461 wcnss->mem_phys = wcnss->mem_reloc = r.start;
462 wcnss->mem_size = resource_size(&r);
463 wcnss->mem_region = devm_ioremap_wc(wcnss->dev, wcnss->mem_phys, wcnss->mem_size);
464 if (!wcnss->mem_region) {
465 dev_err(wcnss->dev, "unable to map memory region: %pa+%zx\n",
466 &r.start, wcnss->mem_size);
473 static int wcnss_probe(struct platform_device *pdev)
475 const struct wcnss_data *data;
476 struct qcom_wcnss *wcnss;
477 struct resource *res;
482 data = of_device_get_match_data(&pdev->dev);
484 if (!qcom_scm_is_available())
485 return -EPROBE_DEFER;
487 if (!qcom_scm_pas_supported(WCNSS_PAS_ID)) {
488 dev_err(&pdev->dev, "PAS is not available for WCNSS\n");
492 rproc = rproc_alloc(&pdev->dev, pdev->name, &wcnss_ops,
493 WCNSS_FIRMWARE_NAME, sizeof(*wcnss));
495 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
498 rproc_coredump_set_elf_info(rproc, ELFCLASS32, EM_NONE);
500 wcnss = (struct qcom_wcnss *)rproc->priv;
501 wcnss->dev = &pdev->dev;
502 wcnss->rproc = rproc;
503 platform_set_drvdata(pdev, wcnss);
505 init_completion(&wcnss->start_done);
506 init_completion(&wcnss->stop_done);
508 mutex_init(&wcnss->iris_lock);
510 res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pmu");
511 mmio = devm_ioremap_resource(&pdev->dev, res);
517 ret = wcnss_alloc_memory_region(wcnss);
521 wcnss->pmu_cfg = mmio + data->pmu_offset;
522 wcnss->spare_out = mmio + data->spare_offset;
524 ret = wcnss_init_regulators(wcnss, data->vregs, data->num_vregs);
528 ret = wcnss_request_irq(wcnss, pdev, "wdog", false, wcnss_wdog_interrupt);
531 wcnss->wdog_irq = ret;
533 ret = wcnss_request_irq(wcnss, pdev, "fatal", false, wcnss_fatal_interrupt);
536 wcnss->fatal_irq = ret;
538 ret = wcnss_request_irq(wcnss, pdev, "ready", true, wcnss_ready_interrupt);
541 wcnss->ready_irq = ret;
543 ret = wcnss_request_irq(wcnss, pdev, "handover", true, wcnss_handover_interrupt);
546 wcnss->handover_irq = ret;
548 ret = wcnss_request_irq(wcnss, pdev, "stop-ack", true, wcnss_stop_ack_interrupt);
551 wcnss->stop_ack_irq = ret;
553 if (wcnss->stop_ack_irq) {
554 wcnss->state = qcom_smem_state_get(&pdev->dev, "stop",
556 if (IS_ERR(wcnss->state)) {
557 ret = PTR_ERR(wcnss->state);
562 qcom_add_smd_subdev(rproc, &wcnss->smd_subdev);
563 wcnss->sysmon = qcom_add_sysmon_subdev(rproc, "wcnss", WCNSS_SSCTL_ID);
564 if (IS_ERR(wcnss->sysmon)) {
565 ret = PTR_ERR(wcnss->sysmon);
569 ret = rproc_add(rproc);
573 return of_platform_populate(pdev->dev.of_node, NULL, NULL, &pdev->dev);
581 static int wcnss_remove(struct platform_device *pdev)
583 struct qcom_wcnss *wcnss = platform_get_drvdata(pdev);
585 of_platform_depopulate(&pdev->dev);
587 qcom_smem_state_put(wcnss->state);
588 rproc_del(wcnss->rproc);
590 qcom_remove_sysmon_subdev(wcnss->sysmon);
591 qcom_remove_smd_subdev(wcnss->rproc, &wcnss->smd_subdev);
592 rproc_free(wcnss->rproc);
597 static const struct of_device_id wcnss_of_match[] = {
598 { .compatible = "qcom,riva-pil", &riva_data },
599 { .compatible = "qcom,pronto-v1-pil", &pronto_v1_data },
600 { .compatible = "qcom,pronto-v2-pil", &pronto_v2_data },
603 MODULE_DEVICE_TABLE(of, wcnss_of_match);
605 static struct platform_driver wcnss_driver = {
606 .probe = wcnss_probe,
607 .remove = wcnss_remove,
609 .name = "qcom-wcnss-pil",
610 .of_match_table = wcnss_of_match,
614 static int __init wcnss_init(void)
618 ret = platform_driver_register(&wcnss_driver);
622 ret = platform_driver_register(&qcom_iris_driver);
624 platform_driver_unregister(&wcnss_driver);
628 module_init(wcnss_init);
630 static void __exit wcnss_exit(void)
632 platform_driver_unregister(&qcom_iris_driver);
633 platform_driver_unregister(&wcnss_driver);
635 module_exit(wcnss_exit);
637 MODULE_DESCRIPTION("Qualcomm Peripheral Image Loader for Wireless Subsystem");
638 MODULE_LICENSE("GPL v2");