blob: 42884ace324ce2dc8358d048330171a517f1620f [file] [log] [blame]
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -07001/*
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +05302 * Qualcomm ADSP/SLPI Peripheral Image Loader for MSM8974 and MSM8996
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -07003 *
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>
Bjorn Andersson2aad40d2017-01-27 03:12:57 -080029#include <linux/soc/qcom/mdt_loader.h>
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070030#include <linux/soc/qcom/smem.h>
31#include <linux/soc/qcom/smem_state.h>
32
Bjorn Anderssonbde440e2017-01-27 02:28:32 -080033#include "qcom_common.h"
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070034#include "remoteproc_internal.h"
35
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053036struct adsp_data {
37 int crash_reason_smem;
38 const char *firmware_name;
39 int pas_id;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053040 bool has_aggre2_clk;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053041};
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070042
43struct qcom_adsp {
44 struct device *dev;
45 struct rproc *rproc;
46
47 int wdog_irq;
48 int fatal_irq;
49 int ready_irq;
50 int handover_irq;
51 int stop_ack_irq;
52
53 struct qcom_smem_state *state;
54 unsigned stop_bit;
55
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070056 struct clk *xo;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053057 struct clk *aggre2_clk;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070058
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070059 struct regulator *cx_supply;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053060 struct regulator *px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070061
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053062 int pas_id;
63 int crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053064 bool has_aggre2_clk;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053065
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070066 struct completion start_done;
67 struct completion stop_done;
68
69 phys_addr_t mem_phys;
70 phys_addr_t mem_reloc;
71 void *mem_region;
72 size_t mem_size;
Bjorn Andersson4b489212017-01-29 14:05:50 -080073
Bjorn Anderssoneea07022017-08-29 16:13:35 -070074 struct qcom_rproc_glink glink_subdev;
Bjorn Andersson4b489212017-01-29 14:05:50 -080075 struct qcom_rproc_subdev smd_subdev;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070076};
77
78static int adsp_load(struct rproc *rproc, const struct firmware *fw)
79{
80 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070081
Bjorn Andersson7f0dd072017-01-27 02:17:23 -080082 return qcom_mdt_load(adsp->dev, fw, rproc->firmware, adsp->pas_id,
83 adsp->mem_region, adsp->mem_phys, adsp->mem_size);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070084}
85
86static const struct rproc_fw_ops adsp_fw_ops = {
87 .find_rsc_table = qcom_mdt_find_rsc_table,
88 .load = adsp_load,
89};
90
91static int adsp_start(struct rproc *rproc)
92{
93 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
94 int ret;
95
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070096 ret = clk_prepare_enable(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070097 if (ret)
98 return ret;
99
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530100 ret = clk_prepare_enable(adsp->aggre2_clk);
101 if (ret)
102 goto disable_xo_clk;
103
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700104 ret = regulator_enable(adsp->cx_supply);
105 if (ret)
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530106 goto disable_aggre2_clk;
107
108 ret = regulator_enable(adsp->px_supply);
109 if (ret)
110 goto disable_cx_supply;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700111
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530112 ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700113 if (ret) {
114 dev_err(adsp->dev,
115 "failed to authenticate image and release reset\n");
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530116 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700117 }
118
119 ret = wait_for_completion_timeout(&adsp->start_done,
120 msecs_to_jiffies(5000));
121 if (!ret) {
122 dev_err(adsp->dev, "start timed out\n");
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530123 qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700124 ret = -ETIMEDOUT;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530125 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700126 }
127
128 ret = 0;
129
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530130disable_px_supply:
131 regulator_disable(adsp->px_supply);
132disable_cx_supply:
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700133 regulator_disable(adsp->cx_supply);
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530134disable_aggre2_clk:
135 clk_disable_unprepare(adsp->aggre2_clk);
136disable_xo_clk:
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700137 clk_disable_unprepare(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700138
139 return ret;
140}
141
142static int adsp_stop(struct rproc *rproc)
143{
144 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
145 int ret;
146
147 qcom_smem_state_update_bits(adsp->state,
148 BIT(adsp->stop_bit),
149 BIT(adsp->stop_bit));
150
151 ret = wait_for_completion_timeout(&adsp->stop_done,
152 msecs_to_jiffies(5000));
153 if (ret == 0)
154 dev_err(adsp->dev, "timed out on wait\n");
155
156 qcom_smem_state_update_bits(adsp->state,
157 BIT(adsp->stop_bit),
158 0);
159
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530160 ret = qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700161 if (ret)
162 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
163
164 return ret;
165}
166
167static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
168{
169 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
170 int offset;
171
172 offset = da - adsp->mem_reloc;
173 if (offset < 0 || offset + len > adsp->mem_size)
174 return NULL;
175
176 return adsp->mem_region + offset;
177}
178
179static const struct rproc_ops adsp_ops = {
180 .start = adsp_start,
181 .stop = adsp_stop,
182 .da_to_va = adsp_da_to_va,
183};
184
185static irqreturn_t adsp_wdog_interrupt(int irq, void *dev)
186{
187 struct qcom_adsp *adsp = dev;
188
189 rproc_report_crash(adsp->rproc, RPROC_WATCHDOG);
190
191 return IRQ_HANDLED;
192}
193
194static irqreturn_t adsp_fatal_interrupt(int irq, void *dev)
195{
196 struct qcom_adsp *adsp = dev;
197 size_t len;
198 char *msg;
199
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530200 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, adsp->crash_reason_smem, &len);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700201 if (!IS_ERR(msg) && len > 0 && msg[0])
202 dev_err(adsp->dev, "fatal error received: %s\n", msg);
203
204 rproc_report_crash(adsp->rproc, RPROC_FATAL_ERROR);
205
206 if (!IS_ERR(msg))
207 msg[0] = '\0';
208
209 return IRQ_HANDLED;
210}
211
212static irqreturn_t adsp_ready_interrupt(int irq, void *dev)
213{
214 return IRQ_HANDLED;
215}
216
217static irqreturn_t adsp_handover_interrupt(int irq, void *dev)
218{
219 struct qcom_adsp *adsp = dev;
220
221 complete(&adsp->start_done);
222
223 return IRQ_HANDLED;
224}
225
226static irqreturn_t adsp_stop_ack_interrupt(int irq, void *dev)
227{
228 struct qcom_adsp *adsp = dev;
229
230 complete(&adsp->stop_done);
231
232 return IRQ_HANDLED;
233}
234
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700235static int adsp_init_clock(struct qcom_adsp *adsp)
236{
237 int ret;
238
239 adsp->xo = devm_clk_get(adsp->dev, "xo");
240 if (IS_ERR(adsp->xo)) {
241 ret = PTR_ERR(adsp->xo);
242 if (ret != -EPROBE_DEFER)
243 dev_err(adsp->dev, "failed to get xo clock");
244 return ret;
245 }
246
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530247 if (adsp->has_aggre2_clk) {
248 adsp->aggre2_clk = devm_clk_get(adsp->dev, "aggre2");
249 if (IS_ERR(adsp->aggre2_clk)) {
250 ret = PTR_ERR(adsp->aggre2_clk);
251 if (ret != -EPROBE_DEFER)
252 dev_err(adsp->dev,
253 "failed to get aggre2 clock");
254 return ret;
255 }
256 }
257
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700258 return 0;
259}
260
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700261static int adsp_init_regulator(struct qcom_adsp *adsp)
262{
263 adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
264 if (IS_ERR(adsp->cx_supply))
265 return PTR_ERR(adsp->cx_supply);
266
267 regulator_set_load(adsp->cx_supply, 100000);
268
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530269 adsp->px_supply = devm_regulator_get(adsp->dev, "px");
270 if (IS_ERR(adsp->px_supply))
271 return PTR_ERR(adsp->px_supply);
272
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700273 return 0;
274}
275
276static int adsp_request_irq(struct qcom_adsp *adsp,
277 struct platform_device *pdev,
278 const char *name,
279 irq_handler_t thread_fn)
280{
281 int ret;
282
283 ret = platform_get_irq_byname(pdev, name);
284 if (ret < 0) {
285 dev_err(&pdev->dev, "no %s IRQ defined\n", name);
286 return ret;
287 }
288
289 ret = devm_request_threaded_irq(&pdev->dev, ret,
290 NULL, thread_fn,
291 IRQF_ONESHOT,
292 "adsp", adsp);
293 if (ret)
294 dev_err(&pdev->dev, "request %s IRQ failed\n", name);
295
296 return ret;
297}
298
299static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
300{
301 struct device_node *node;
302 struct resource r;
303 int ret;
304
305 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
306 if (!node) {
307 dev_err(adsp->dev, "no memory-region specified\n");
308 return -EINVAL;
309 }
310
311 ret = of_address_to_resource(node, 0, &r);
312 if (ret)
313 return ret;
314
315 adsp->mem_phys = adsp->mem_reloc = r.start;
316 adsp->mem_size = resource_size(&r);
317 adsp->mem_region = devm_ioremap_wc(adsp->dev, adsp->mem_phys, adsp->mem_size);
318 if (!adsp->mem_region) {
319 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
320 &r.start, adsp->mem_size);
321 return -EBUSY;
322 }
323
324 return 0;
325}
326
327static int adsp_probe(struct platform_device *pdev)
328{
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530329 const struct adsp_data *desc;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700330 struct qcom_adsp *adsp;
331 struct rproc *rproc;
332 int ret;
333
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530334 desc = of_device_get_match_data(&pdev->dev);
335 if (!desc)
336 return -EINVAL;
337
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700338 if (!qcom_scm_is_available())
339 return -EPROBE_DEFER;
340
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700341 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530342 desc->firmware_name, sizeof(*adsp));
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700343 if (!rproc) {
344 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
345 return -ENOMEM;
346 }
347
348 rproc->fw_ops = &adsp_fw_ops;
349
350 adsp = (struct qcom_adsp *)rproc->priv;
351 adsp->dev = &pdev->dev;
352 adsp->rproc = rproc;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530353 adsp->pas_id = desc->pas_id;
354 adsp->crash_reason_smem = desc->crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530355 adsp->has_aggre2_clk = desc->has_aggre2_clk;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700356 platform_set_drvdata(pdev, adsp);
357
358 init_completion(&adsp->start_done);
359 init_completion(&adsp->stop_done);
360
361 ret = adsp_alloc_memory_region(adsp);
362 if (ret)
363 goto free_rproc;
364
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700365 ret = adsp_init_clock(adsp);
366 if (ret)
367 goto free_rproc;
368
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700369 ret = adsp_init_regulator(adsp);
370 if (ret)
371 goto free_rproc;
372
373 ret = adsp_request_irq(adsp, pdev, "wdog", adsp_wdog_interrupt);
374 if (ret < 0)
375 goto free_rproc;
376 adsp->wdog_irq = ret;
377
378 ret = adsp_request_irq(adsp, pdev, "fatal", adsp_fatal_interrupt);
379 if (ret < 0)
380 goto free_rproc;
381 adsp->fatal_irq = ret;
382
383 ret = adsp_request_irq(adsp, pdev, "ready", adsp_ready_interrupt);
384 if (ret < 0)
385 goto free_rproc;
386 adsp->ready_irq = ret;
387
388 ret = adsp_request_irq(adsp, pdev, "handover", adsp_handover_interrupt);
389 if (ret < 0)
390 goto free_rproc;
391 adsp->handover_irq = ret;
392
393 ret = adsp_request_irq(adsp, pdev, "stop-ack", adsp_stop_ack_interrupt);
394 if (ret < 0)
395 goto free_rproc;
396 adsp->stop_ack_irq = ret;
397
398 adsp->state = qcom_smem_state_get(&pdev->dev, "stop",
399 &adsp->stop_bit);
400 if (IS_ERR(adsp->state)) {
401 ret = PTR_ERR(adsp->state);
402 goto free_rproc;
403 }
404
Bjorn Anderssoneea07022017-08-29 16:13:35 -0700405 qcom_add_glink_subdev(rproc, &adsp->glink_subdev);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800406 qcom_add_smd_subdev(rproc, &adsp->smd_subdev);
407
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700408 ret = rproc_add(rproc);
409 if (ret)
410 goto free_rproc;
411
412 return 0;
413
414free_rproc:
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800415 rproc_free(rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700416
417 return ret;
418}
419
420static int adsp_remove(struct platform_device *pdev)
421{
422 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
423
424 qcom_smem_state_put(adsp->state);
425 rproc_del(adsp->rproc);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800426
Bjorn Anderssoneea07022017-08-29 16:13:35 -0700427 qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800428 qcom_remove_smd_subdev(adsp->rproc, &adsp->smd_subdev);
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800429 rproc_free(adsp->rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700430
431 return 0;
432}
433
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530434static const struct adsp_data adsp_resource_init = {
435 .crash_reason_smem = 423,
436 .firmware_name = "adsp.mdt",
437 .pas_id = 1,
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530438 .has_aggre2_clk = false,
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530439};
440
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +0530441static const struct adsp_data slpi_resource_init = {
442 .crash_reason_smem = 424,
443 .firmware_name = "slpi.mdt",
444 .pas_id = 12,
445 .has_aggre2_clk = true,
446};
447
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700448static const struct of_device_id adsp_of_match[] = {
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530449 { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
450 { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +0530451 { .compatible = "qcom,msm8996-slpi-pil", .data = &slpi_resource_init},
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700452 { },
453};
Bjorn Andersson62423472016-11-19 22:41:56 -0800454MODULE_DEVICE_TABLE(of, adsp_of_match);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700455
456static struct platform_driver adsp_driver = {
457 .probe = adsp_probe,
458 .remove = adsp_remove,
459 .driver = {
460 .name = "qcom_adsp_pil",
461 .of_match_table = adsp_of_match,
462 },
463};
464
465module_platform_driver(adsp_driver);
466MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
467MODULE_LICENSE("GPL v2");