blob: c1ee5c818b4222166811bed27ae4a76be1888194 [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>
29#include <linux/soc/qcom/smem.h>
30#include <linux/soc/qcom/smem_state.h>
31
Bjorn Anderssonbde440e2017-01-27 02:28:32 -080032#include "qcom_common.h"
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070033#include "qcom_mdt_loader.h"
34#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;
73};
74
75static int adsp_load(struct rproc *rproc, const struct firmware *fw)
76{
77 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070078
Bjorn Andersson7f0dd072017-01-27 02:17:23 -080079 return qcom_mdt_load(adsp->dev, fw, rproc->firmware, adsp->pas_id,
80 adsp->mem_region, adsp->mem_phys, adsp->mem_size);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070081}
82
83static const struct rproc_fw_ops adsp_fw_ops = {
84 .find_rsc_table = qcom_mdt_find_rsc_table,
85 .load = adsp_load,
86};
87
88static int adsp_start(struct rproc *rproc)
89{
90 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
91 int ret;
92
Sarangdhar Joshif33a7352016-10-25 13:57:26 -070093 ret = clk_prepare_enable(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -070094 if (ret)
95 return ret;
96
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +053097 ret = clk_prepare_enable(adsp->aggre2_clk);
98 if (ret)
99 goto disable_xo_clk;
100
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700101 ret = regulator_enable(adsp->cx_supply);
102 if (ret)
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530103 goto disable_aggre2_clk;
104
105 ret = regulator_enable(adsp->px_supply);
106 if (ret)
107 goto disable_cx_supply;
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700108
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530109 ret = qcom_scm_pas_auth_and_reset(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700110 if (ret) {
111 dev_err(adsp->dev,
112 "failed to authenticate image and release reset\n");
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530113 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700114 }
115
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");
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530120 qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700121 ret = -ETIMEDOUT;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530122 goto disable_px_supply;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700123 }
124
125 ret = 0;
126
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530127disable_px_supply:
128 regulator_disable(adsp->px_supply);
129disable_cx_supply:
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700130 regulator_disable(adsp->cx_supply);
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530131disable_aggre2_clk:
132 clk_disable_unprepare(adsp->aggre2_clk);
133disable_xo_clk:
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700134 clk_disable_unprepare(adsp->xo);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700135
136 return ret;
137}
138
139static int adsp_stop(struct rproc *rproc)
140{
141 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
142 int ret;
143
144 qcom_smem_state_update_bits(adsp->state,
145 BIT(adsp->stop_bit),
146 BIT(adsp->stop_bit));
147
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");
152
153 qcom_smem_state_update_bits(adsp->state,
154 BIT(adsp->stop_bit),
155 0);
156
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530157 ret = qcom_scm_pas_shutdown(adsp->pas_id);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700158 if (ret)
159 dev_err(adsp->dev, "failed to shutdown: %d\n", ret);
160
161 return ret;
162}
163
164static void *adsp_da_to_va(struct rproc *rproc, u64 da, int len)
165{
166 struct qcom_adsp *adsp = (struct qcom_adsp *)rproc->priv;
167 int offset;
168
169 offset = da - adsp->mem_reloc;
170 if (offset < 0 || offset + len > adsp->mem_size)
171 return NULL;
172
173 return adsp->mem_region + offset;
174}
175
176static const struct rproc_ops adsp_ops = {
177 .start = adsp_start,
178 .stop = adsp_stop,
179 .da_to_va = adsp_da_to_va,
180};
181
182static irqreturn_t adsp_wdog_interrupt(int irq, void *dev)
183{
184 struct qcom_adsp *adsp = dev;
185
186 rproc_report_crash(adsp->rproc, RPROC_WATCHDOG);
187
188 return IRQ_HANDLED;
189}
190
191static irqreturn_t adsp_fatal_interrupt(int irq, void *dev)
192{
193 struct qcom_adsp *adsp = dev;
194 size_t len;
195 char *msg;
196
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530197 msg = qcom_smem_get(QCOM_SMEM_HOST_ANY, adsp->crash_reason_smem, &len);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700198 if (!IS_ERR(msg) && len > 0 && msg[0])
199 dev_err(adsp->dev, "fatal error received: %s\n", msg);
200
201 rproc_report_crash(adsp->rproc, RPROC_FATAL_ERROR);
202
203 if (!IS_ERR(msg))
204 msg[0] = '\0';
205
206 return IRQ_HANDLED;
207}
208
209static irqreturn_t adsp_ready_interrupt(int irq, void *dev)
210{
211 return IRQ_HANDLED;
212}
213
214static irqreturn_t adsp_handover_interrupt(int irq, void *dev)
215{
216 struct qcom_adsp *adsp = dev;
217
218 complete(&adsp->start_done);
219
220 return IRQ_HANDLED;
221}
222
223static irqreturn_t adsp_stop_ack_interrupt(int irq, void *dev)
224{
225 struct qcom_adsp *adsp = dev;
226
227 complete(&adsp->stop_done);
228
229 return IRQ_HANDLED;
230}
231
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700232static int adsp_init_clock(struct qcom_adsp *adsp)
233{
234 int ret;
235
236 adsp->xo = devm_clk_get(adsp->dev, "xo");
237 if (IS_ERR(adsp->xo)) {
238 ret = PTR_ERR(adsp->xo);
239 if (ret != -EPROBE_DEFER)
240 dev_err(adsp->dev, "failed to get xo clock");
241 return ret;
242 }
243
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530244 if (adsp->has_aggre2_clk) {
245 adsp->aggre2_clk = devm_clk_get(adsp->dev, "aggre2");
246 if (IS_ERR(adsp->aggre2_clk)) {
247 ret = PTR_ERR(adsp->aggre2_clk);
248 if (ret != -EPROBE_DEFER)
249 dev_err(adsp->dev,
250 "failed to get aggre2 clock");
251 return ret;
252 }
253 }
254
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700255 return 0;
256}
257
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700258static int adsp_init_regulator(struct qcom_adsp *adsp)
259{
260 adsp->cx_supply = devm_regulator_get(adsp->dev, "cx");
261 if (IS_ERR(adsp->cx_supply))
262 return PTR_ERR(adsp->cx_supply);
263
264 regulator_set_load(adsp->cx_supply, 100000);
265
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530266 adsp->px_supply = devm_regulator_get(adsp->dev, "px");
267 if (IS_ERR(adsp->px_supply))
268 return PTR_ERR(adsp->px_supply);
269
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700270 return 0;
271}
272
273static int adsp_request_irq(struct qcom_adsp *adsp,
274 struct platform_device *pdev,
275 const char *name,
276 irq_handler_t thread_fn)
277{
278 int ret;
279
280 ret = platform_get_irq_byname(pdev, name);
281 if (ret < 0) {
282 dev_err(&pdev->dev, "no %s IRQ defined\n", name);
283 return ret;
284 }
285
286 ret = devm_request_threaded_irq(&pdev->dev, ret,
287 NULL, thread_fn,
288 IRQF_ONESHOT,
289 "adsp", adsp);
290 if (ret)
291 dev_err(&pdev->dev, "request %s IRQ failed\n", name);
292
293 return ret;
294}
295
296static int adsp_alloc_memory_region(struct qcom_adsp *adsp)
297{
298 struct device_node *node;
299 struct resource r;
300 int ret;
301
302 node = of_parse_phandle(adsp->dev->of_node, "memory-region", 0);
303 if (!node) {
304 dev_err(adsp->dev, "no memory-region specified\n");
305 return -EINVAL;
306 }
307
308 ret = of_address_to_resource(node, 0, &r);
309 if (ret)
310 return ret;
311
312 adsp->mem_phys = adsp->mem_reloc = r.start;
313 adsp->mem_size = resource_size(&r);
314 adsp->mem_region = devm_ioremap_wc(adsp->dev, adsp->mem_phys, adsp->mem_size);
315 if (!adsp->mem_region) {
316 dev_err(adsp->dev, "unable to map memory region: %pa+%zx\n",
317 &r.start, adsp->mem_size);
318 return -EBUSY;
319 }
320
321 return 0;
322}
323
324static int adsp_probe(struct platform_device *pdev)
325{
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530326 const struct adsp_data *desc;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700327 struct qcom_adsp *adsp;
328 struct rproc *rproc;
329 int ret;
330
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530331 desc = of_device_get_match_data(&pdev->dev);
332 if (!desc)
333 return -EINVAL;
334
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700335 if (!qcom_scm_is_available())
336 return -EPROBE_DEFER;
337
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530338 if (!qcom_scm_pas_supported(desc->pas_id)) {
339 dev_err(&pdev->dev, "PAS is not available for subsystem\n");
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700340 return -ENXIO;
341 }
342
343 rproc = rproc_alloc(&pdev->dev, pdev->name, &adsp_ops,
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530344 desc->firmware_name, sizeof(*adsp));
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700345 if (!rproc) {
346 dev_err(&pdev->dev, "unable to allocate remoteproc\n");
347 return -ENOMEM;
348 }
349
350 rproc->fw_ops = &adsp_fw_ops;
351
352 adsp = (struct qcom_adsp *)rproc->priv;
353 adsp->dev = &pdev->dev;
354 adsp->rproc = rproc;
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530355 adsp->pas_id = desc->pas_id;
356 adsp->crash_reason_smem = desc->crash_reason_smem;
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530357 adsp->has_aggre2_clk = desc->has_aggre2_clk;
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700358 platform_set_drvdata(pdev, adsp);
359
360 init_completion(&adsp->start_done);
361 init_completion(&adsp->stop_done);
362
363 ret = adsp_alloc_memory_region(adsp);
364 if (ret)
365 goto free_rproc;
366
Sarangdhar Joshif33a7352016-10-25 13:57:26 -0700367 ret = adsp_init_clock(adsp);
368 if (ret)
369 goto free_rproc;
370
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700371 ret = adsp_init_regulator(adsp);
372 if (ret)
373 goto free_rproc;
374
375 ret = adsp_request_irq(adsp, pdev, "wdog", adsp_wdog_interrupt);
376 if (ret < 0)
377 goto free_rproc;
378 adsp->wdog_irq = ret;
379
380 ret = adsp_request_irq(adsp, pdev, "fatal", adsp_fatal_interrupt);
381 if (ret < 0)
382 goto free_rproc;
383 adsp->fatal_irq = ret;
384
385 ret = adsp_request_irq(adsp, pdev, "ready", adsp_ready_interrupt);
386 if (ret < 0)
387 goto free_rproc;
388 adsp->ready_irq = ret;
389
390 ret = adsp_request_irq(adsp, pdev, "handover", adsp_handover_interrupt);
391 if (ret < 0)
392 goto free_rproc;
393 adsp->handover_irq = ret;
394
395 ret = adsp_request_irq(adsp, pdev, "stop-ack", adsp_stop_ack_interrupt);
396 if (ret < 0)
397 goto free_rproc;
398 adsp->stop_ack_irq = ret;
399
400 adsp->state = qcom_smem_state_get(&pdev->dev, "stop",
401 &adsp->stop_bit);
402 if (IS_ERR(adsp->state)) {
403 ret = PTR_ERR(adsp->state);
404 goto free_rproc;
405 }
406
407 ret = rproc_add(rproc);
408 if (ret)
409 goto free_rproc;
410
411 return 0;
412
413free_rproc:
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800414 rproc_free(rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700415
416 return ret;
417}
418
419static int adsp_remove(struct platform_device *pdev)
420{
421 struct qcom_adsp *adsp = platform_get_drvdata(pdev);
422
423 qcom_smem_state_put(adsp->state);
424 rproc_del(adsp->rproc);
Bjorn Andersson90a80d82016-11-19 22:42:55 -0800425 rproc_free(adsp->rproc);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700426
427 return 0;
428}
429
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530430static const struct adsp_data adsp_resource_init = {
431 .crash_reason_smem = 423,
432 .firmware_name = "adsp.mdt",
433 .pas_id = 1,
Avaneesh Kumar Dwivedie323fc02017-01-30 20:33:07 +0530434 .has_aggre2_clk = false,
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,
442};
443
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700444static const struct of_device_id adsp_of_match[] = {
Avaneesh Kumar Dwivedic7715e42017-01-30 20:33:06 +0530445 { .compatible = "qcom,msm8974-adsp-pil", .data = &adsp_resource_init},
446 { .compatible = "qcom,msm8996-adsp-pil", .data = &adsp_resource_init},
Avaneesh Kumar Dwivedi90a068e2017-01-30 20:33:08 +0530447 { .compatible = "qcom,msm8996-slpi-pil", .data = &slpi_resource_init},
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700448 { },
449};
Bjorn Andersson62423472016-11-19 22:41:56 -0800450MODULE_DEVICE_TABLE(of, adsp_of_match);
Bjorn Anderssonb9e718e2016-08-22 22:57:44 -0700451
452static struct platform_driver adsp_driver = {
453 .probe = adsp_probe,
454 .remove = adsp_remove,
455 .driver = {
456 .name = "qcom_adsp_pil",
457 .of_match_table = adsp_of_match,
458 },
459};
460
461module_platform_driver(adsp_driver);
462MODULE_DESCRIPTION("Qualcomm MSM8974/MSM8996 ADSP Peripherial Image Loader");
463MODULE_LICENSE("GPL v2");