blob: 87f8d406950032637fdd50088423bbf58d04e274 [file] [log] [blame]
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -07001/*
2 * Qualcomm ADSP Peripheral Image Loader for MSM8974 and MSM8996
3 *
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.
7 *
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.
11 *
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.
16 */
17
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070018#include <linux/clk.h>
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070019#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/smem.h>
30#include <linux/soc/qcom/smem_state.h>
31
32#include "qcom_mdt_loader.h"
33#include "remoteproc_internal.h"
34
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053035struct adsp_data {
36 int crash_reason_smem;
37 const char *firmware_name;
38 int pas_id;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053039 bool has_aggre2_clk;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053040};
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070041
42struct qcom_adsp {
43 struct device *dev;
44 struct rproc *rproc;
45
46 int wdog_irq;
47 int fatal_irq;
48 int ready_irq;
49 int handover_irq;
50 int stop_ack_irq;
51
52 struct qcom_smem_state *state;
53 unsigned stop_bit;
54
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070055 struct clk *xo;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053056 struct clk *aggre2_clk;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070057
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070058 struct regulator *cx_supply;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053059 struct regulator *px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070060
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053061 int pas_id;
62 int crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053063 bool has_aggre2_clk;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053064
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070065 struct completion start_done;
66 struct completion stop_done;
67
68 phys_addr_t mem_phys;
69 phys_addr_t mem_reloc;
70 void *mem_region;
71 size_t mem_size;
72};
73
74static int adsp_load(struct rproc *rproc, const struct firmware *fw)
75{
76 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
77 phys_addr_t fw_addr;
78 size_t fw_size;
79 bool relocate;
80 int ret;
81
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053082 ret = qcom_scm_pas_init_image(adsp->pas_id, fw->data, fw->size);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070083 if (ret) {
84 dev_err(&rproc->dev, "invalid firmware metadata\n");
85 return ret;
86 }
87
88 ret = qcom_mdt_parse(fw, &fw_addr, &fw_size, &relocate);
89 if (ret) {
90 dev_err(&rproc->dev, "failed to parse mdt header\n");
91 return ret;
92 }
93
94 if (relocate) {
95 adsp->mem_reloc = fw_addr;
96
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053097 ret = qcom_scm_pas_mem_setup(adsp->pas_id,
98 adsp->mem_phys, fw_size);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070099 if (ret) {
100 dev_err(&rproc->dev, "unable to setup memory for image\n");
101 return ret;
102 }
103 }
104
105 return qcom_mdt_load(rproc, fw, rproc->firmware);
106}
107
108static const struct rproc_fw_ops adsp_fw_ops = {
109 .find_rsc_table = qcom_mdt_find_rsc_table,
110 .load = adsp_load,
111};
112
113static int adsp_start(struct rproc *rproc)
114{
115 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
116 int ret;
117
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700118 ret = clk_prepare_enable(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700119 if (ret)
120 return ret;
121
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530122 ret = clk_prepare_enable(adsp->aggre2_clk);
123 if (ret)
124 goto disable_xo_clk;
125
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700126 ret = regulator_enable(adsp->cx_supply);
127 if (ret)
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530128 goto disable_aggre2_clk;
129
130 ret = regulator_enable(adsp->px_supply);
131 if (ret)
132 goto disable_cx_supply;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700133
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530134 ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700135 if (ret) {
136 dev_err(adsp->dev,
137 "failed to authenticate image and release reset\n");
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530138 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700139 }
140
141 ret = wait_for_completion_timeout(&adsp->start_done,
142 msecs_to_jiffies(5000));
143 if (!ret) {
144 dev_err(adsp->dev, "start timed out\n");
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530145 qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700146 ret = -ETIMEDOUT;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530147 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700148 }
149
150 ret = 0;
151
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530152disable_px_supply:
153 regulator_disable(adsp->px_supply);
154disable_cx_supply:
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700155 regulator_disable(adsp->cx_supply);
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530156disable_aggre2_clk:
157 clk_disable_unprepare(adsp->aggre2_clk);
158disable_xo_clk:
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700159 clk_disable_unprepare(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700160
161 return ret;
162}
163
164static int adsp_stop(struct rproc *rproc)
165{
166 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
167 int ret;
168
169 qcom_smem_state_update_bits(adsp->state,
170 BIT(adsp->stop_bit),
171 BIT(adsp->stop_bit));
172
173 ret = wait_for_completion_timeout(&adsp->stop_done,
174 msecs_to_jiffies(5000));
175 if (ret == 0)
176 dev_err(adsp->dev, "timed out on wait\n");
177
178 qcom_smem_state_update_bits(adsp->state,
179 BIT(adsp->stop_bit),
180 0);
181
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530182 ret = qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700183 if (ret)
184 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
185
186 return ret;
187}
188
189static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
190{
191 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
192 int offset;
193
194 offset = da - adsp->mem_reloc;
195 if (offset < 0 || offset + len > adsp->mem_size)
196 return NULL;
197
198 return adsp->mem_region + offset;
199}
200
201static const struct rproc_ops adsp_ops = {
202 .start = adsp_start,
203 .stop = adsp_stop,
204 .da_to_va = adsp_da_to_va,
205};
206
207static irqreturn_t adsp_wdog_interrupt(int irq, void *dev)
208{
209 struct qcom_adsp *adsp = dev;
210
211 rproc_report_crash(adsp->rproc, RPROC_WATCHDOG);
212
213 return IRQ_HANDLED;
214}
215
216static irqreturn_t adsp_fatal_interrupt(int irq, void *dev)
217{
218 struct qcom_adsp *adsp = dev;
219 size_t len;
220 char *msg;
221
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530222 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, adsp->crash_reason_smem, &len);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700223 if (!IS_ERR(msg) && len > 0 && msg[0])
224 dev_err(adsp->dev, "fatal error received: %s\n", msg);
225
226 rproc_report_crash(adsp->rproc, RPROC_FATAL_ERROR);
227
228 if (!IS_ERR(msg))
229 msg[0] = '\0';
230
231 return IRQ_HANDLED;
232}
233
234static irqreturn_t adsp_ready_interrupt(int irq, void *dev)
235{
236 return IRQ_HANDLED;
237}
238
239static irqreturn_t adsp_handover_interrupt(int irq, void *dev)
240{
241 struct qcom_adsp *adsp = dev;
242
243 complete(&adsp->start_done);
244
245 return IRQ_HANDLED;
246}
247
248static irqreturn_t adsp_stop_ack_interrupt(int irq, void *dev)
249{
250 struct qcom_adsp *adsp = dev;
251
252 complete(&adsp->stop_done);
253
254 return IRQ_HANDLED;
255}
256
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700257static int adsp_init_clock(struct qcom_adsp *adsp)
258{
259 int ret;
260
261 adsp->xo = devm_clk_get(adsp->dev, "xo");
262 if (IS_ERR(adsp->xo)) {
263 ret = PTR_ERR(adsp->xo);
264 if (ret != -EPROBE_DEFER)
265 dev_err(adsp->dev, "failed to get xo clock");
266 return ret;
267 }
268
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530269 if (adsp->has_aggre2_clk) {
270 adsp->aggre2_clk = devm_clk_get(adsp->dev, "aggre2");
271 if (IS_ERR(adsp->aggre2_clk)) {
272 ret = PTR_ERR(adsp->aggre2_clk);
273 if (ret != -EPROBE_DEFER)
274 dev_err(adsp->dev,
275 "failed to get aggre2 clock");
276 return ret;
277 }
278 }
279
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700280 return 0;
281}
282
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700283static int adsp_init_regulator(struct qcom_adsp *adsp)
284{
285 adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
286 if (IS_ERR(adsp->cx_supply))
287 return PTR_ERR(adsp->cx_supply);
288
289 regulator_set_load(adsp->cx_supply, 100000);
290
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530291 adsp->px_supply = devm_regulator_get(adsp->dev, "px");
292 if (IS_ERR(adsp->px_supply))
293 return PTR_ERR(adsp->px_supply);
294
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700295 return 0;
296}
297
298static int adsp_request_irq(struct qcom_adsp *adsp,
299 struct platform_device *pdev,
300 const char *name,
301 irq_handler_t thread_fn)
302{
303 int ret;
304
305 ret = platform_get_irq_byname(pdev, name);
306 if (ret < 0) {
307 dev_err(&pdev->dev, "no %s IRQ defined\n", name);
308 return ret;
309 }
310
311 ret = devm_request_threaded_irq(&pdev->dev, ret,
312 NULL, thread_fn,
313 IRQF_ONESHOT,
314 "adsp", adsp);
315 if (ret)
316 dev_err(&pdev->dev, "request %s IRQ failed\n", name);
317
318 return ret;
319}
320
321static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
322{
323 struct device_node *node;
324 struct resource r;
325 int ret;
326
327 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
328 if (!node) {
329 dev_err(adsp->dev, "no memory-region specified\n");
330 return -EINVAL;
331 }
332
333 ret = of_address_to_resource(node, 0, &r);
334 if (ret)
335 return ret;
336
337 adsp->mem_phys = adsp->mem_reloc = r.start;
338 adsp->mem_size = resource_size(&r);
339 adsp->mem_region = devm_ioremap_wc(adsp->dev, adsp->mem_phys, adsp->mem_size);
340 if (!adsp->mem_region) {
341 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
342 &r.start, adsp->mem_size);
343 return -EBUSY;
344 }
345
346 return 0;
347}
348
349static int adsp_probe(struct platform_device *pdev)
350{
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530351 const struct adsp_data *desc;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700352 struct qcom_adsp *adsp;
353 struct rproc *rproc;
354 int ret;
355
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530356 desc = of_device_get_match_data(&pdev->dev);
357 if (!desc)
358 return -EINVAL;
359
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700360 if (!qcom_scm_is_available())
361 return -EPROBE_DEFER;
362
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530363 if (!qcom_scm_pas_supported(desc->pas_id)) {
364 dev_err(&pdev->dev, "PAS is not available for subsystem\n");
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700365 return -ENXIO;
366 }
367
368 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530369 desc->firmware_name, sizeof(*adsp));
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700370 if (!rproc) {
371 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
372 return -ENOMEM;
373 }
374
375 rproc->fw_ops = &adsp_fw_ops;
376
377 adsp = (struct qcom_adsp *)rproc->priv;
378 adsp->dev = &pdev->dev;
379 adsp->rproc = rproc;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530380 adsp->pas_id = desc->pas_id;
381 adsp->crash_reason_smem = desc->crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530382 adsp->has_aggre2_clk = desc->has_aggre2_clk;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700383 platform_set_drvdata(pdev, adsp);
384
385 init_completion(&adsp->start_done);
386 init_completion(&adsp->stop_done);
387
388 ret = adsp_alloc_memory_region(adsp);
389 if (ret)
390 goto free_rproc;
391
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700392 ret = adsp_init_clock(adsp);
393 if (ret)
394 goto free_rproc;
395
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700396 ret = adsp_init_regulator(adsp);
397 if (ret)
398 goto free_rproc;
399
400 ret = adsp_request_irq(adsp, pdev, "wdog", adsp_wdog_interrupt);
401 if (ret < 0)
402 goto free_rproc;
403 adsp->wdog_irq = ret;
404
405 ret = adsp_request_irq(adsp, pdev, "fatal", adsp_fatal_interrupt);
406 if (ret < 0)
407 goto free_rproc;
408 adsp->fatal_irq = ret;
409
410 ret = adsp_request_irq(adsp, pdev, "ready", adsp_ready_interrupt);
411 if (ret < 0)
412 goto free_rproc;
413 adsp->ready_irq = ret;
414
415 ret = adsp_request_irq(adsp, pdev, "handover", adsp_handover_interrupt);
416 if (ret < 0)
417 goto free_rproc;
418 adsp->handover_irq = ret;
419
420 ret = adsp_request_irq(adsp, pdev, "stop-ack", adsp_stop_ack_interrupt);
421 if (ret < 0)
422 goto free_rproc;
423 adsp->stop_ack_irq = ret;
424
425 adsp->state = qcom_smem_state_get(&pdev->dev, "stop",
426 &adsp->stop_bit);
427 if (IS_ERR(adsp->state)) {
428 ret = PTR_ERR(adsp->state);
429 goto free_rproc;
430 }
431
432 ret = rproc_add(rproc);
433 if (ret)
434 goto free_rproc;
435
436 return 0;
437
438free_rproc:
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800439 rproc_free(rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700440
441 return ret;
442}
443
444static int adsp_remove(struct platform_device *pdev)
445{
446 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
447
448 qcom_smem_state_put(adsp->state);
449 rproc_del(adsp->rproc);
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800450 rproc_free(adsp->rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700451
452 return 0;
453}
454
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530455static const struct adsp_data adsp_resource_init = {
456 .crash_reason_smem = 423,
457 .firmware_name = "adsp.mdt",
458 .pas_id = 1,
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530459 .has_aggre2_clk = false,
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530460};
461
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700462static const struct of_device_id adsp_of_match[] = {
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530463 { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
464 { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700465 { },
466};
Bjorn Andersson62423472016-11-19 22:41:56 -0800467MODULE_DEVICE_TABLE(of, adsp_of_match);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700468
469static struct platform_driver adsp_driver = {
470 .probe = adsp_probe,
471 .remove = adsp_remove,
472 .driver = {
473 .name = "qcom_adsp_pil",
474 .of_match_table = adsp_of_match,
475 },
476};
477
478module_platform_driver(adsp_driver);
479MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
480MODULE_LICENSE("GPL v2");