blob: ac8f9a77b82108e59f31d08ee8b2d56035070769 [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;
Bjorn Andersson1e140df2017-07-24 22:56:43 -070041 const char *ssr_name;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053042};
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070043
44struct qcom_adsp {
45 struct device *dev;
46 struct rproc *rproc;
47
48 int wdog_irq;
49 int fatal_irq;
50 int ready_irq;
51 int handover_irq;
52 int stop_ack_irq;
53
54 struct qcom_smem_state *state;
55 unsigned stop_bit;
56
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070057 struct clk *xo;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053058 struct clk *aggre2_clk;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070059
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070060 struct regulator *cx_supply;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053061 struct regulator *px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070062
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053063 int pas_id;
64 int crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053065 bool has_aggre2_clk;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +053066
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070067 struct completion start_done;
68 struct completion stop_done;
69
70 phys_addr_t mem_phys;
71 phys_addr_t mem_reloc;
72 void *mem_region;
73 size_t mem_size;
Bjorn Andersson4b489212017-01-29 14:05:50 -080074
Bjorn Anderssoneea07022017-08-29 16:13:35 -070075 struct qcom_rproc_glink glink_subdev;
Bjorn Andersson4b489212017-01-29 14:05:50 -080076 struct qcom_rproc_subdev smd_subdev;
Bjorn Andersson1e140df2017-07-24 22:56:43 -070077 struct qcom_rproc_ssr ssr_subdev;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070078};
79
80static int adsp_load(struct rproc *rproc, const struct firmware *fw)
81{
82 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070083
Bjorn Andersson7f0dd072017-01-27 02:17:23 -080084 return qcom_mdt_load(adsp->dev, fw, rproc->firmware, adsp->pas_id,
Bjorn Andersson4dd27f52018-01-05 16:04:19 -080085 adsp->mem_region, adsp->mem_phys, adsp->mem_size,
86 &adsp->mem_reloc);
87
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070088}
89
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070090static int adsp_start(struct rproc *rproc)
91{
92 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
93 int ret;
94
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070095 ret = clk_prepare_enable(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070096 if (ret)
97 return ret;
98
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053099 ret = clk_prepare_enable(adsp->aggre2_clk);
100 if (ret)
101 goto disable_xo_clk;
102
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700103 ret = regulator_enable(adsp->cx_supply);
104 if (ret)
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530105 goto disable_aggre2_clk;
106
107 ret = regulator_enable(adsp->px_supply);
108 if (ret)
109 goto disable_cx_supply;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700110
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530111 ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700112 if (ret) {
113 dev_err(adsp->dev,
114 "failed to authenticate image and release reset\n");
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530115 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700116 }
117
118 ret = wait_for_completion_timeout(&adsp->start_done,
119 msecs_to_jiffies(5000));
120 if (!ret) {
121 dev_err(adsp->dev, "start timed out\n");
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530122 qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700123 ret = -ETIMEDOUT;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530124 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700125 }
126
127 ret = 0;
128
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530129disable_px_supply:
130 regulator_disable(adsp->px_supply);
131disable_cx_supply:
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700132 regulator_disable(adsp->cx_supply);
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530133disable_aggre2_clk:
134 clk_disable_unprepare(adsp->aggre2_clk);
135disable_xo_clk:
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700136 clk_disable_unprepare(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700137
138 return ret;
139}
140
141static int adsp_stop(struct rproc *rproc)
142{
143 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
144 int ret;
145
146 qcom_smem_state_update_bits(adsp->state,
147 BIT(adsp->stop_bit),
148 BIT(adsp->stop_bit));
149
150 ret = wait_for_completion_timeout(&adsp->stop_done,
151 msecs_to_jiffies(5000));
152 if (ret == 0)
153 dev_err(adsp->dev, "timed out on wait\n");
154
155 qcom_smem_state_update_bits(adsp->state,
156 BIT(adsp->stop_bit),
157 0);
158
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530159 ret = qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700160 if (ret)
161 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
162
163 return ret;
164}
165
166static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
167{
168 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
169 int offset;
170
171 offset = da - adsp->mem_reloc;
172 if (offset < 0 || offset + len > adsp->mem_size)
173 return NULL;
174
175 return adsp->mem_region + offset;
176}
177
178static const struct rproc_ops adsp_ops = {
179 .start = adsp_start,
180 .stop = adsp_stop,
181 .da_to_va = adsp_da_to_va,
Sarangdhar Joshidcb57ed2018-01-05 16:04:20 -0800182 .parse_fw = qcom_register_dump_segments,
Bjorn Andersson0f21f9c2018-01-05 15:58:01 -0800183 .load = adsp_load,
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700184};
185
186static irqreturn_t adsp_wdog_interrupt(int irq, void *dev)
187{
188 struct qcom_adsp *adsp = dev;
189
190 rproc_report_crash(adsp->rproc, RPROC_WATCHDOG);
191
192 return IRQ_HANDLED;
193}
194
195static irqreturn_t adsp_fatal_interrupt(int irq, void *dev)
196{
197 struct qcom_adsp *adsp = dev;
198 size_t len;
199 char *msg;
200
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530201 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, adsp->crash_reason_smem, &len);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700202 if (!IS_ERR(msg) && len > 0 && msg[0])
203 dev_err(adsp->dev, "fatal error received: %s\n", msg);
204
205 rproc_report_crash(adsp->rproc, RPROC_FATAL_ERROR);
206
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700207 return IRQ_HANDLED;
208}
209
210static irqreturn_t adsp_ready_interrupt(int irq, void *dev)
211{
212 return IRQ_HANDLED;
213}
214
215static irqreturn_t adsp_handover_interrupt(int irq, void *dev)
216{
217 struct qcom_adsp *adsp = dev;
218
219 complete(&adsp->start_done);
220
221 return IRQ_HANDLED;
222}
223
224static irqreturn_t adsp_stop_ack_interrupt(int irq, void *dev)
225{
226 struct qcom_adsp *adsp = dev;
227
228 complete(&adsp->stop_done);
229
230 return IRQ_HANDLED;
231}
232
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700233static int adsp_init_clock(struct qcom_adsp *adsp)
234{
235 int ret;
236
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;
243 }
244
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530245 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;
253 }
254 }
255
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700256 return 0;
257}
258
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700259static int adsp_init_regulator(struct qcom_adsp *adsp)
260{
261 adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
262 if (IS_ERR(adsp->cx_supply))
263 return PTR_ERR(adsp->cx_supply);
264
265 regulator_set_load(adsp->cx_supply, 100000);
266
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530267 adsp->px_supply = devm_regulator_get(adsp->dev, "px");
Himanshu Jhac76929b2017-08-29 19:13:18 +0530268 return PTR_ERR_OR_ZERO(adsp->px_supply);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700269}
270
271static int adsp_request_irq(struct qcom_adsp *adsp,
272 struct platform_device *pdev,
273 const char *name,
274 irq_handler_t thread_fn)
275{
276 int ret;
277
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;
282 }
283
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);
290
291 return ret;
292}
293
294static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
295{
296 struct device_node *node;
297 struct resource r;
298 int ret;
299
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;
304 }
305
306 ret = of_address_to_resource(node, 0, &r);
307 if (ret)
308 return ret;
309
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;
317 }
318
319 return 0;
320}
321
322static int adsp_probe(struct platform_device *pdev)
323{
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530324 const struct adsp_data *desc;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700325 struct qcom_adsp *adsp;
326 struct rproc *rproc;
327 int ret;
328
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530329 desc = of_device_get_match_data(&pdev->dev);
330 if (!desc)
331 return -EINVAL;
332
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700333 if (!qcom_scm_is_available())
334 return -EPROBE_DEFER;
335
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700336 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530337 desc->firmware_name, sizeof(*adsp));
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700338 if (!rproc) {
339 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
340 return -ENOMEM;
341 }
342
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700343 adsp = (struct qcom_adsp *)rproc->priv;
344 adsp->dev = &pdev->dev;
345 adsp->rproc = rproc;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530346 adsp->pas_id = desc->pas_id;
347 adsp->crash_reason_smem = desc->crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530348 adsp->has_aggre2_clk = desc->has_aggre2_clk;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700349 platform_set_drvdata(pdev, adsp);
350
351 init_completion(&adsp->start_done);
352 init_completion(&adsp->stop_done);
353
354 ret = adsp_alloc_memory_region(adsp);
355 if (ret)
356 goto free_rproc;
357
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700358 ret = adsp_init_clock(adsp);
359 if (ret)
360 goto free_rproc;
361
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700362 ret = adsp_init_regulator(adsp);
363 if (ret)
364 goto free_rproc;
365
366 ret = adsp_request_irq(adsp, pdev, "wdog", adsp_wdog_interrupt);
367 if (ret < 0)
368 goto free_rproc;
369 adsp->wdog_irq = ret;
370
371 ret = adsp_request_irq(adsp, pdev, "fatal", adsp_fatal_interrupt);
372 if (ret < 0)
373 goto free_rproc;
374 adsp->fatal_irq = ret;
375
376 ret = adsp_request_irq(adsp, pdev, "ready", adsp_ready_interrupt);
377 if (ret < 0)
378 goto free_rproc;
379 adsp->ready_irq = ret;
380
381 ret = adsp_request_irq(adsp, pdev, "handover", adsp_handover_interrupt);
382 if (ret < 0)
383 goto free_rproc;
384 adsp->handover_irq = ret;
385
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;
390
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;
396 }
397
Bjorn Anderssoneea07022017-08-29 16:13:35 -0700398 qcom_add_glink_subdev(rproc, &adsp->glink_subdev);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800399 qcom_add_smd_subdev(rproc, &adsp->smd_subdev);
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700400 qcom_add_ssr_subdev(rproc, &adsp->ssr_subdev, desc->ssr_name);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800401
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700402 ret = rproc_add(rproc);
403 if (ret)
404 goto free_rproc;
405
406 return 0;
407
408free_rproc:
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800409 rproc_free(rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700410
411 return ret;
412}
413
414static int adsp_remove(struct platform_device *pdev)
415{
416 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
417
418 qcom_smem_state_put(adsp->state);
419 rproc_del(adsp->rproc);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800420
Bjorn Anderssoneea07022017-08-29 16:13:35 -0700421 qcom_remove_glink_subdev(adsp->rproc, &adsp->glink_subdev);
Bjorn Andersson4b489212017-01-29 14:05:50 -0800422 qcom_remove_smd_subdev(adsp->rproc, &adsp->smd_subdev);
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700423 qcom_remove_ssr_subdev(adsp->rproc, &adsp->ssr_subdev);
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800424 rproc_free(adsp->rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700425
426 return 0;
427}
428
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530429static const struct adsp_data adsp_resource_init = {
430 .crash_reason_smem = 423,
431 .firmware_name = "adsp.mdt",
432 .pas_id = 1,
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530433 .has_aggre2_clk = false,
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700434 .ssr_name = "lpass",
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530435};
436
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +0530437static 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,
Bjorn Andersson1e140df2017-07-24 22:56:43 -0700442 .ssr_name = "dsps",
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +0530443};
444
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700445static const struct of_device_id adsp_of_match[] = {
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530446 { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
447 { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +0530448 { .compatible = "qcom,msm8996-slpi-pil", .data = &slpi_resource_init},
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700449 { },
450};
Bjorn Andersson62423472016-11-19 22:41:56 -0800451MODULE_DEVICE_TABLE(of, adsp_of_match);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700452
453static 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,
459 },
460};
461
462module_platform_driver(adsp_driver);
463MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
464MODULE_LICENSE("GPL v2");