Linux 4.16.11
[linux/fpc-iii.git] / drivers / remoteproc / qcom_adsp_pil.c
blob373c167892d7d4754ccef7161f1f3fea1054afc6
1 /*
2 * Qualcomm ADSP/SLPI Peripheral Image Loader for MSM8974 and MSM8996
4 * Copyright (C) 2016 Linaro Ltd
5 * Copyright (C) 2014 Sony Mobile Communications AB
6 * Copyright (c) 2012-2013, The Linux Foundation. All rights reserved.
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * version 2 as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
18 #include <linux/clk.h>
19 #include <linux/firmware.h>
20 #include <linux/interrupt.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_device.h>
25 #include <linux/platform_device.h>
26 #include <linux/qcom_scm.h>
27 #include <linux/regulator/consumer.h>
28 #include <linux/remoteproc.h>
29 #include <linux/soc/qcom/mdt_loader.h>
30 #include <linux/soc/qcom/smem.h>
31 #include <linux/soc/qcom/smem_state.h>
33 #include "qcom_common.h"
34 #include "remoteproc_internal.h"
36 struct adsp_data {
37 int crash_reason_smem;
38 const char *firmware_name;
39 int pas_id;
40 bool has_aggre2_clk;
41 const char *ssr_name;
44 struct qcom_adsp {
45 struct device *dev;
46 struct rproc *rproc;
48 int wdog_irq;
49 int fatal_irq;
50 int ready_irq;
51 int handover_irq;
52 int stop_ack_irq;
54 struct qcom_smem_state *state;
55 unsigned stop_bit;
57 struct clk *xo;
58 struct clk *aggre2_clk;
60 struct regulator *cx_supply;
61 struct regulator *px_supply;
63 int pas_id;
64 int crash_reason_smem;
65 bool has_aggre2_clk;
67 struct completion start_done;
68 struct completion stop_done;
70 phys_addr_t mem_phys;
71 phys_addr_t mem_reloc;
72 void *mem_region;
73 size_t mem_size;
75 struct qcom_rproc_glink glink_subdev;
76 struct qcom_rproc_subdev smd_subdev;
77 struct qcom_rproc_ssr ssr_subdev;
80 static int adsp_load(struct rproc *rproc, const struct firmware *fw)
82 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
84 return qcom_mdt_load(adsp->dev, fw, rproc->firmware, adsp->pas_id,
85 adsp->mem_region, adsp->mem_phys, adsp->mem_size);
88 static int adsp_start(struct rproc *rproc)
90 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
91 int ret;
93 ret = clk_prepare_enable(adsp->xo);
94 if (ret)
95 return ret;
97 ret = clk_prepare_enable(adsp->aggre2_clk);
98 if (ret)
99 goto disable_xo_clk;
101 ret = regulator_enable(adsp->cx_supply);
102 if (ret)
103 goto disable_aggre2_clk;
105 ret = regulator_enable(adsp->px_supply);
106 if (ret)
107 goto disable_cx_supply;
109 ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
110 if (ret) {
111 dev_err(adsp->dev,
112 "failed to authenticate image and release reset\n");
113 goto disable_px_supply;
116 ret = wait_for_completion_timeout(&adsp->start_done,
117 msecs_to_jiffies(5000));
118 if (!ret) {
119 dev_err(adsp->dev, "start timed out\n");
120 qcom_scm_pas_shutdown(adsp->pas_id);
121 ret = -ETIMEDOUT;
122 goto disable_px_supply;
125 ret = 0;
127 disable_px_supply:
128 regulator_disable(adsp->px_supply);
129 disable_cx_supply:
130 regulator_disable(adsp->cx_supply);
131 disable_aggre2_clk:
132 clk_disable_unprepare(adsp->aggre2_clk);
133 disable_xo_clk:
134 clk_disable_unprepare(adsp->xo);
136 return ret;
139 static int adsp_stop(struct rproc *rproc)
141 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
142 int ret;
144 qcom_smem_state_update_bits(adsp->state,
145 BIT(adsp->stop_bit),
146 BIT(adsp->stop_bit));
148 ret = wait_for_completion_timeout(&adsp->stop_done,
149 msecs_to_jiffies(5000));
150 if (ret == 0)
151 dev_err(adsp->dev, "timed out on wait\n");
153 qcom_smem_state_update_bits(adsp->state,
154 BIT(adsp->stop_bit),
157 ret = qcom_scm_pas_shutdown(adsp->pas_id);
158 if (ret)
159 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
161 return ret;
164 static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
166 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
167 int offset;
169 offset = da - adsp->mem_reloc;
170 if (offset < 0 || offset + len > adsp->mem_size)
171 return NULL;
173 return adsp->mem_region + offset;
176 static const struct rproc_ops adsp_ops = {
177 .start = adsp_start,
178 .stop = adsp_stop,
179 .da_to_va = adsp_da_to_va,
180 .load = adsp_load,
183 static irqreturn_t adsp_wdog_interrupt(int irq, void *dev)
185 struct qcom_adsp *adsp = dev;
187 rproc_report_crash(adsp->rproc, RPROC_WATCHDOG);
189 return IRQ_HANDLED;
192 static irqreturn_t adsp_fatal_interrupt(int irq, void *dev)
194 struct qcom_adsp *adsp = dev;
195 size_t len;
196 char *msg;
198 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, adsp->crash_reason_smem, &len);
199 if (!IS_ERR(msg) && len > 0 && msg[0])
200 dev_err(adsp->dev, "fatal error received: %s\n", msg);
202 rproc_report_crash(adsp->rproc, RPROC_FATAL_ERROR);
204 if (!IS_ERR(msg))
205 msg[0] = '\0';
207 return IRQ_HANDLED;
210 static irqreturn_t adsp_ready_interrupt(int irq, void *dev)
212 return IRQ_HANDLED;
215 static irqreturn_t adsp_handover_interrupt(int irq, void *dev)
217 struct qcom_adsp *adsp = dev;
219 complete(&adsp->start_done);
221 return IRQ_HANDLED;
224 static irqreturn_t adsp_stop_ack_interrupt(int irq, void *dev)
226 struct qcom_adsp *adsp = dev;
228 complete(&adsp->stop_done);
230 return IRQ_HANDLED;
233 static int adsp_init_clock(struct qcom_adsp *adsp)
235 int ret;
237 adsp->xo = devm_clk_get(adsp->dev, "xo");
238 if (IS_ERR(adsp->xo)) {
239 ret = PTR_ERR(adsp->xo);
240 if (ret != -EPROBE_DEFER)
241 dev_err(adsp->dev, "failed to get xo clock");
242 return ret;
245 if (adsp->has_aggre2_clk) {
246 adsp->aggre2_clk = devm_clk_get(adsp->dev, "aggre2");
247 if (IS_ERR(adsp->aggre2_clk)) {
248 ret = PTR_ERR(adsp->aggre2_clk);
249 if (ret != -EPROBE_DEFER)
250 dev_err(adsp->dev,
251 "failed to get aggre2 clock");
252 return ret;
256 return 0;
259 static int adsp_init_regulator(struct qcom_adsp *adsp)
261 adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
262 if (IS_ERR(adsp->cx_supply))
263 return PTR_ERR(adsp->cx_supply);
265 regulator_set_load(adsp->cx_supply, 100000);
267 adsp->px_supply = devm_regulator_get(adsp->dev, "px");
268 return PTR_ERR_OR_ZERO(adsp->px_supply);
271 static int adsp_request_irq(struct qcom_adsp *adsp,
272 struct platform_device *pdev,
273 const char *name,
274 irq_handler_t thread_fn)
276 int ret;
278 ret = platform_get_irq_byname(pdev, name);
279 if (ret < 0) {
280 dev_err(&pdev->dev, "no %s IRQ defined\n", name);
281 return ret;
284 ret = devm_request_threaded_irq(&pdev->dev, ret,
285 NULL, thread_fn,
286 IRQF_ONESHOT,
287 "adsp", adsp);
288 if (ret)
289 dev_err(&pdev->dev, "request %s IRQ failed\n", name);
291 return ret;
294 static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
296 struct device_node *node;
297 struct resource r;
298 int ret;
300 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
301 if (!node) {
302 dev_err(adsp->dev, "no memory-region specified\n");
303 return -EINVAL;
306 ret = of_address_to_resource(node, 0, &r);
307 if (ret)
308 return ret;
310 adsp->mem_phys = adsp->mem_reloc = r.start;
311 adsp->mem_size = resource_size(&r);
312 adsp->mem_region = devm_ioremap_wc(adsp->dev, adsp->mem_phys, adsp->mem_size);
313 if (!adsp->mem_region) {
314 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
315 &r.start, adsp->mem_size);
316 return -EBUSY;
319 return 0;
322 static int adsp_probe(struct platform_device *pdev)
324 const struct adsp_data *desc;
325 struct qcom_adsp *adsp;
326 struct rproc *rproc;
327 int ret;
329 desc = of_device_get_match_data(&pdev->dev);
330 if (!desc)
331 return -EINVAL;
333 if (!qcom_scm_is_available())
334 return -EPROBE_DEFER;
336 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
337 desc->firmware_name, sizeof(*adsp));
338 if (!rproc) {
339 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
340 return -ENOMEM;
343 adsp = (struct qcom_adsp *)rproc->priv;
344 adsp->dev = &pdev->dev;
345 adsp->rproc = rproc;
346 adsp->pas_id = desc->pas_id;
347 adsp->crash_reason_smem = desc->crash_reason_smem;
348 adsp->has_aggre2_clk = desc->has_aggre2_clk;
349 platform_set_drvdata(pdev, adsp);
351 init_completion(&adsp->start_done);
352 init_completion(&adsp->stop_done);
354 ret = adsp_alloc_memory_region(adsp);
355 if (ret)
356 goto free_rproc;
358 ret = adsp_init_clock(adsp);
359 if (ret)
360 goto free_rproc;
362 ret = adsp_init_regulator(adsp);
363 if (ret)
364 goto free_rproc;
366 ret = adsp_request_irq(adsp, pdev, "wdog", adsp_wdog_interrupt);
367 if (ret < 0)
368 goto free_rproc;
369 adsp->wdog_irq = ret;
371 ret = adsp_request_irq(adsp, pdev, "fatal", adsp_fatal_interrupt);
372 if (ret < 0)
373 goto free_rproc;
374 adsp->fatal_irq = ret;
376 ret = adsp_request_irq(adsp, pdev, "ready", adsp_ready_interrupt);
377 if (ret < 0)
378 goto free_rproc;
379 adsp->ready_irq = ret;
381 ret = adsp_request_irq(adsp, pdev, "handover", adsp_handover_interrupt);
382 if (ret < 0)
383 goto free_rproc;
384 adsp->handover_irq = ret;
386 ret = adsp_request_irq(adsp, pdev, "stop-ack", adsp_stop_ack_interrupt);
387 if (ret < 0)
388 goto free_rproc;
389 adsp->stop_ack_irq = ret;
391 adsp->state = qcom_smem_state_get(&pdev->dev, "stop",
392 &adsp->stop_bit);
393 if (IS_ERR(adsp->state)) {
394 ret = PTR_ERR(adsp->state);
395 goto free_rproc;
398 qcom_add_glink_subdev(rproc, &adsp->glink_subdev);
399 qcom_add_smd_subdev(rproc, &adsp->smd_subdev);
400 qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
402 ret = rproc_add(rproc);
403 if (ret)
404 goto free_rproc;
406 return 0;
408 free_rproc:
409 rproc_free(rproc);
411 return ret;
414 static int adsp_remove(struct platform_device *pdev)
416 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
418 qcom_smem_state_put(adsp->state);
419 rproc_del(adsp->rproc);
421 qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
422 qcom_remove_smd_subdev(adsp->rproc, &adsp->smd_subdev);
423 qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
424 rproc_free(adsp->rproc);
426 return 0;
429 static const struct adsp_data adsp_resource_init = {
430 .crash_reason_smem = 423,
431 .firmware_name = "adsp.mdt",
432 .pas_id = 1,
433 .has_aggre2_clk = false,
434 .ssr_name = "lpass",
437 static const struct adsp_data slpi_resource_init = {
438 .crash_reason_smem = 424,
439 .firmware_name = "slpi.mdt",
440 .pas_id = 12,
441 .has_aggre2_clk = true,
442 .ssr_name = "dsps",
445 static const struct of_device_id adsp_of_match[] = {
446 { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
447 { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
448 { .compatible = "qcom,msm8996-slpi-pil", .data = &slpi_resource_init},
449 { },
451 MODULE_DEVICE_TABLE(of, adsp_of_match);
453 static struct platform_driver adsp_driver = {
454 .probe = adsp_probe,
455 .remove = adsp_remove,
456 .driver = {
457 .name = "qcom_adsp_pil",
458 .of_match_table = adsp_of_match,
462 module_platform_driver(adsp_driver);
463 MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
464 MODULE_LICENSE("GPL v2");