blob: 3c68da096a153579f53a0d6cf3e38a4ce26b4f10 [file] [log] [blame]
Matt Wagantallc2bbdc32012-03-21 19:44:50 -07001/*
2 * Copyright (c) 2012, Code Aurora Forum. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 and
6 * only version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 */
13
14#include <linux/init.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
17#include <linux/io.h>
Matt Wagantallc2bbdc32012-03-21 19:44:50 -070018#include <linux/err.h>
19#include <linux/of.h>
Matt Wagantalld41ce772012-05-10 23:16:41 -070020#include <linux/clk.h>
Stephen Boyd633eb622012-06-13 12:05:35 -070021#include <linux/workqueue.h>
22#include <linux/interrupt.h>
23#include <linux/delay.h>
24
Matt Wagantall8c2246d2012-08-12 17:08:04 -070025#include <mach/clk.h>
Stephen Boyd633eb622012-06-13 12:05:35 -070026#include <mach/subsystem_restart.h>
27#include <mach/subsystem_notif.h>
28#include <mach/scm.h>
29#include <mach/peripheral-loader.h>
30
Matt Wagantallc2bbdc32012-03-21 19:44:50 -070031#include "peripheral-loader.h"
32#include "pil-q6v5.h"
Matt Wagantall10a59672012-07-26 11:52:02 -070033#include "scm-pas.h"
Stephen Boyd633eb622012-06-13 12:05:35 -070034#include "ramdump.h"
35#include "sysmon.h"
Matt Wagantallc2bbdc32012-03-21 19:44:50 -070036
Matt Wagantallc2bbdc32012-03-21 19:44:50 -070037#define QDSP6SS_RST_EVB 0x010
Matt Wagantall4d89c2e2012-05-25 19:28:34 -070038#define PROXY_TIMEOUT_MS 10000
Matt Wagantallc2bbdc32012-03-21 19:44:50 -070039
Stephen Boyd633eb622012-06-13 12:05:35 -070040struct lpass_data {
41 struct q6v5_data *q6;
42 struct subsys_device *subsys;
43 struct subsys_desc subsys_desc;
44 void *ramdump_dev;
45 int wdog_irq;
46 struct work_struct work;
47 void *riva_notif_hdle;
48 void *modem_notif_hdle;
49 int crash_shutdown;
50};
51
52#define subsys_to_drv(d) container_of(d, struct lpass_data, subsys_desc)
53
Matt Wagantall8c2246d2012-08-12 17:08:04 -070054static int pil_lpass_enable_clks(struct q6v5_data *drv)
55{
56 int ret;
57
58 ret = clk_reset(drv->core_clk, CLK_RESET_DEASSERT);
59 if (ret)
60 goto err_reset;
61 ret = clk_prepare_enable(drv->core_clk);
62 if (ret)
63 goto err_core_clk;
64 ret = clk_prepare_enable(drv->ahb_clk);
65 if (ret)
66 goto err_ahb_clk;
67 ret = clk_prepare_enable(drv->axi_clk);
68 if (ret)
69 goto err_axi_clk;
70 ret = clk_prepare_enable(drv->reg_clk);
71 if (ret)
72 goto err_reg_clk;
73
74 return 0;
75
76err_reg_clk:
77 clk_disable_unprepare(drv->axi_clk);
78err_axi_clk:
79 clk_disable_unprepare(drv->ahb_clk);
80err_ahb_clk:
81 clk_disable_unprepare(drv->core_clk);
82err_core_clk:
83 clk_reset(drv->core_clk, CLK_RESET_ASSERT);
84err_reset:
85 return ret;
86}
87
88static void pil_lpass_disable_clks(struct q6v5_data *drv)
89{
90 clk_disable_unprepare(drv->reg_clk);
91 clk_disable_unprepare(drv->axi_clk);
92 clk_disable_unprepare(drv->ahb_clk);
93 clk_disable_unprepare(drv->core_clk);
94 clk_reset(drv->core_clk, CLK_RESET_ASSERT);
95}
96
Matt Wagantallc2bbdc32012-03-21 19:44:50 -070097static int pil_lpass_shutdown(struct pil_desc *pil)
98{
Stephen Boyd3826cd42012-07-05 17:37:53 -070099 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700100
Matt Wagantallb7747992012-05-11 19:37:51 -0700101 pil_q6v5_halt_axi_port(pil, drv->axi_halt_base);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700102
Matt Wagantalld41ce772012-05-10 23:16:41 -0700103 /*
104 * If the shutdown function is called before the reset function, clocks
105 * will not be enabled yet. Enable them here so that register writes
106 * performed during the shutdown succeed.
107 */
108 if (drv->is_booted == false)
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700109 pil_lpass_enable_clks(drv);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700110
111 pil_q6v5_shutdown(pil);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700112 pil_lpass_disable_clks(drv);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700113
Matt Wagantalld41ce772012-05-10 23:16:41 -0700114 drv->is_booted = false;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700115
116 return 0;
117}
118
119static int pil_lpass_reset(struct pil_desc *pil)
120{
Stephen Boyd3826cd42012-07-05 17:37:53 -0700121 struct q6v5_data *drv = container_of(pil, struct q6v5_data, desc);
Matt Wagantalld41ce772012-05-10 23:16:41 -0700122 int ret;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700123
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700124 ret = pil_lpass_enable_clks(drv);
Matt Wagantalld41ce772012-05-10 23:16:41 -0700125 if (ret)
126 return ret;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700127
128 /* Program Image Address */
129 writel_relaxed(((drv->start_addr >> 4) & 0x0FFFFFF0),
130 drv->reg_base + QDSP6SS_RST_EVB);
131
Matt Wagantalld41ce772012-05-10 23:16:41 -0700132 ret = pil_q6v5_reset(pil);
133 if (ret) {
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700134 pil_lpass_disable_clks(drv);
Matt Wagantalld41ce772012-05-10 23:16:41 -0700135 return ret;
136 }
137
138 drv->is_booted = true;
139
140 return 0;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700141}
142
143static struct pil_reset_ops pil_lpass_ops = {
144 .init_image = pil_q6v5_init_image,
145 .proxy_vote = pil_q6v5_make_proxy_votes,
146 .proxy_unvote = pil_q6v5_remove_proxy_votes,
147 .auth_and_reset = pil_lpass_reset,
148 .shutdown = pil_lpass_shutdown,
149};
150
Matt Wagantall10a59672012-07-26 11:52:02 -0700151static int pil_lpass_init_image_trusted(struct pil_desc *pil,
152 const u8 *metadata, size_t size)
153{
154 return pas_init_image(PAS_Q6, metadata, size);
155}
156
157static int pil_lpass_reset_trusted(struct pil_desc *pil)
158{
159 return pas_auth_and_reset(PAS_Q6);
160}
161
162static int pil_lpass_shutdown_trusted(struct pil_desc *pil)
163{
164 return pas_shutdown(PAS_Q6);
165}
166
167static struct pil_reset_ops pil_lpass_ops_trusted = {
168 .init_image = pil_lpass_init_image_trusted,
169 .proxy_vote = pil_q6v5_make_proxy_votes,
170 .proxy_unvote = pil_q6v5_remove_proxy_votes,
171 .auth_and_reset = pil_lpass_reset_trusted,
172 .shutdown = pil_lpass_shutdown_trusted,
173};
174
Stephen Boyd633eb622012-06-13 12:05:35 -0700175static int riva_notifier_cb(struct notifier_block *this, unsigned long code,
176 void *ss_handle)
177{
178 int ret;
179 switch (code) {
180 case SUBSYS_BEFORE_SHUTDOWN:
181 pr_debug("%s: R-Notify: Shutdown started\n", __func__);
182 ret = sysmon_send_event(SYSMON_SS_LPASS, "wcnss",
183 SUBSYS_BEFORE_SHUTDOWN);
184 if (ret < 0)
185 pr_err("%s: sysmon_send_event error %d", __func__, ret);
186 break;
187 }
188 return NOTIFY_DONE;
189}
190
191static struct notifier_block rnb = {
192 .notifier_call = riva_notifier_cb,
193};
194
195static int modem_notifier_cb(struct notifier_block *this, unsigned long code,
196 void *ss_handle)
197{
198 int ret;
199 switch (code) {
200 case SUBSYS_BEFORE_SHUTDOWN:
201 pr_debug("%s: M-Notify: Shutdown started\n", __func__);
202 ret = sysmon_send_event(SYSMON_SS_LPASS, "modem",
203 SUBSYS_BEFORE_SHUTDOWN);
204 if (ret < 0)
205 pr_err("%s: sysmon_send_event error %d", __func__, ret);
206 break;
207 }
208 return NOTIFY_DONE;
209}
210
211static struct notifier_block mnb = {
212 .notifier_call = modem_notifier_cb,
213};
214
215static void adsp_log_failure_reason(void)
216{
217 char *reason;
218 char buffer[81];
219 unsigned size;
220
221 reason = smem_get_entry(SMEM_SSR_REASON_LPASS0, &size);
222
223 if (!reason) {
224 pr_err("ADSP subsystem failure reason: (unknown, smem_get_entry failed).");
225 return;
226 }
227
228 if (reason[0] == '\0') {
229 pr_err("ADSP subsystem failure reason: (unknown, init value found)");
230 return;
231 }
232
233 size = min(size, sizeof(buffer) - 1);
234 memcpy(buffer, reason, size);
235 buffer[size] = '\0';
236 pr_err("ADSP subsystem failure reason: %s", buffer);
237 memset((void *)reason, 0x0, size);
238 wmb();
239}
240
241static void restart_adsp(struct lpass_data *drv)
242{
243 adsp_log_failure_reason();
244 subsystem_restart_dev(drv->subsys);
245}
246
247static void adsp_fatal_fn(struct work_struct *work)
248{
249 struct lpass_data *drv = container_of(work, struct lpass_data, work);
250
251 pr_err("Watchdog bite received from ADSP!\n");
252 restart_adsp(drv);
253}
254
255static void adsp_smsm_state_cb(void *data, uint32_t old_state,
256 uint32_t new_state)
257{
258 struct lpass_data *drv = data;
259
260 /* Ignore if we're the one that set SMSM_RESET */
261 if (drv->crash_shutdown)
262 return;
263
264 if (new_state & SMSM_RESET) {
265 pr_err("%s: ADSP SMSM state changed to SMSM_RESET, new_state = %#x, old_state = %#x\n",
266 __func__, new_state, old_state);
267 restart_adsp(drv);
268 }
269}
270
271#define SCM_Q6_NMI_CMD 0x1
272
273static void send_q6_nmi(void)
274{
275 /* Send NMI to QDSP6 via an SCM call. */
276 scm_call_atomic1(SCM_SVC_UTIL, SCM_Q6_NMI_CMD, 0x1);
277 pr_debug("%s: Q6 NMI was sent.\n", __func__);
278}
279
280#define subsys_to_lpass(d) container_of(d, struct lpass_data, subsys_desc)
281
282static int adsp_shutdown(const struct subsys_desc *subsys)
283{
284 struct lpass_data *drv = subsys_to_lpass(subsys);
285
286 send_q6_nmi();
287 /* The write needs to go through before the q6 is shutdown. */
288 mb();
289 pil_force_shutdown("adsp");
290 disable_irq_nosync(drv->wdog_irq);
291
292 return 0;
293}
294
295static int adsp_powerup(const struct subsys_desc *subsys)
296{
297 struct lpass_data *drv = subsys_to_lpass(subsys);
298 int ret = 0;
299
300 if (get_restart_level() == RESET_SUBSYS_INDEPENDENT) {
301 pr_debug("%s: Wait for ADSP power up!", __func__);
302 msleep(10000);
303 }
304
305 ret = pil_force_boot("adsp");
306 enable_irq(drv->wdog_irq);
307
308 return ret;
309}
310
311static struct ramdump_segment segments = { 0xdc00000, 0x1800000 };
312
313static int adsp_ramdump(int enable, const struct subsys_desc *subsys)
314{
315 struct lpass_data *drv = subsys_to_lpass(subsys);
316
317 if (!enable)
318 return 0;
319 return do_ramdump(drv->ramdump_dev, &segments, 1);
320}
321
322static void adsp_crash_shutdown(const struct subsys_desc *subsys)
323{
324 struct lpass_data *drv = subsys_to_lpass(subsys);
325
326 drv->crash_shutdown = 1;
327 send_q6_nmi();
328}
329
330static irqreturn_t adsp_wdog_bite_irq(int irq, void *dev_id)
331{
332 struct lpass_data *drv = dev_id;
333
334 disable_irq_nosync(drv->wdog_irq);
335 schedule_work(&drv->work);
336
337 return IRQ_HANDLED;
338}
339
Stephen Boyd3e4e9752012-06-27 12:46:32 -0700340static int lpass_start(const struct subsys_desc *desc)
341{
342 void *ret;
343 struct lpass_data *drv = subsys_to_drv(desc);
344
345 ret = pil_get(drv->q6->desc.name);
346 if (IS_ERR(ret))
347 return PTR_ERR(ret);
348 return 0;
349}
350
351static void lpass_stop(const struct subsys_desc *desc)
352{
353 struct lpass_data *drv = subsys_to_drv(desc);
354 pil_put(drv->q6->pil);
355}
356
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700357static int __devinit pil_lpass_driver_probe(struct platform_device *pdev)
358{
Stephen Boyd633eb622012-06-13 12:05:35 -0700359 struct lpass_data *drv;
360 struct q6v5_data *q6;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700361 struct pil_desc *desc;
Stephen Boyd633eb622012-06-13 12:05:35 -0700362 int ret;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700363
Stephen Boyd633eb622012-06-13 12:05:35 -0700364 drv = devm_kzalloc(&pdev->dev, sizeof(*drv), GFP_KERNEL);
365 if (!drv)
366 return -ENOMEM;
Stephen Boyd3826cd42012-07-05 17:37:53 -0700367 platform_set_drvdata(pdev, drv);
Matt Wagantall55252f12012-05-02 18:02:54 -0700368
Stephen Boyd633eb622012-06-13 12:05:35 -0700369 drv->wdog_irq = platform_get_irq(pdev, 0);
370 if (drv->wdog_irq < 0)
371 return drv->wdog_irq;
372
373 q6 = pil_q6v5_init(pdev);
374 if (IS_ERR(q6))
375 return PTR_ERR(q6);
376 drv->q6 = q6;
377
378 desc = &q6->desc;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700379 desc->owner = THIS_MODULE;
Matt Wagantall4d89c2e2012-05-25 19:28:34 -0700380 desc->proxy_timeout = PROXY_TIMEOUT_MS;
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700381
Stephen Boyd633eb622012-06-13 12:05:35 -0700382 q6->core_clk = devm_clk_get(&pdev->dev, "core_clk");
383 if (IS_ERR(q6->core_clk))
384 return PTR_ERR(q6->core_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700385
Stephen Boyd633eb622012-06-13 12:05:35 -0700386 q6->ahb_clk = devm_clk_get(&pdev->dev, "iface_clk");
387 if (IS_ERR(q6->ahb_clk))
388 return PTR_ERR(q6->ahb_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700389
Stephen Boyd633eb622012-06-13 12:05:35 -0700390 q6->axi_clk = devm_clk_get(&pdev->dev, "bus_clk");
391 if (IS_ERR(q6->axi_clk))
392 return PTR_ERR(q6->axi_clk);
Matt Wagantall8c2246d2012-08-12 17:08:04 -0700393
Stephen Boyd633eb622012-06-13 12:05:35 -0700394 q6->reg_clk = devm_clk_get(&pdev->dev, "reg_clk");
395 if (IS_ERR(q6->reg_clk))
396 return PTR_ERR(q6->reg_clk);
Matt Wagantall56865f02012-08-09 15:03:36 -0700397
Matt Wagantall10a59672012-07-26 11:52:02 -0700398 if (pas_supported(PAS_Q6) > 0) {
399 desc->ops = &pil_lpass_ops_trusted;
400 dev_info(&pdev->dev, "using secure boot\n");
401 } else {
402 desc->ops = &pil_lpass_ops;
403 dev_info(&pdev->dev, "using non-secure boot\n");
404 }
405
Stephen Boyd633eb622012-06-13 12:05:35 -0700406 drv->q6->pil = msm_pil_register(desc);
407 if (IS_ERR(drv->q6->pil))
408 return PTR_ERR(drv->q6->pil);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700409
Stephen Boyd633eb622012-06-13 12:05:35 -0700410 drv->subsys_desc.name = desc->name;
411 drv->subsys_desc.owner = THIS_MODULE;
412 drv->subsys_desc.dev = &pdev->dev;
413 drv->subsys_desc.shutdown = adsp_shutdown;
414 drv->subsys_desc.powerup = adsp_powerup;
415 drv->subsys_desc.ramdump = adsp_ramdump;
416 drv->subsys_desc.crash_shutdown = adsp_crash_shutdown;
Stephen Boyd3e4e9752012-06-27 12:46:32 -0700417 drv->subsys_desc.start = lpass_start;
418 drv->subsys_desc.stop = lpass_stop;
Stephen Boyd633eb622012-06-13 12:05:35 -0700419
420 INIT_WORK(&drv->work, adsp_fatal_fn);
421
422 drv->ramdump_dev = create_ramdump_device("adsp");
423 if (!drv->ramdump_dev) {
424 ret = -ENOMEM;
425 goto err_ramdump;
426 }
427
428 drv->subsys = subsys_register(&drv->subsys_desc);
429 if (IS_ERR(drv->subsys)) {
430 ret = PTR_ERR(drv->subsys);
431 goto err_subsys;
432 }
433
434 ret = devm_request_irq(&pdev->dev, drv->wdog_irq, adsp_wdog_bite_irq,
435 IRQF_TRIGGER_RISING, dev_name(&pdev->dev), drv);
436 if (ret)
437 goto err_irq;
438
439 ret = smsm_state_cb_register(SMSM_Q6_STATE, SMSM_RESET,
440 adsp_smsm_state_cb, drv);
441 if (ret < 0)
442 goto err_smsm;
443
444 drv->riva_notif_hdle = subsys_notif_register_notifier("riva", &rnb);
445 if (IS_ERR(drv->riva_notif_hdle)) {
446 ret = PTR_ERR(drv->riva_notif_hdle);
447 goto err_notif_riva;
448 }
449
450 drv->modem_notif_hdle = subsys_notif_register_notifier("modem", &mnb);
451 if (IS_ERR(drv->modem_notif_hdle)) {
452 ret = PTR_ERR(drv->modem_notif_hdle);
453 goto err_notif_modem;
454 }
455 return 0;
456err_notif_modem:
457 subsys_notif_unregister_notifier(drv->riva_notif_hdle, &rnb);
458err_notif_riva:
459 smsm_state_cb_deregister(SMSM_Q6_STATE, SMSM_RESET,
460 adsp_smsm_state_cb, drv);
461err_smsm:
462err_irq:
463 subsys_unregister(drv->subsys);
464err_subsys:
465 destroy_ramdump_device(drv->ramdump_dev);
466err_ramdump:
467 msm_pil_unregister(drv->q6->pil);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700468 return 0;
469}
470
471static int __devexit pil_lpass_driver_exit(struct platform_device *pdev)
472{
Stephen Boyd633eb622012-06-13 12:05:35 -0700473 struct lpass_data *drv = platform_get_drvdata(pdev);
474 subsys_notif_unregister_notifier(drv->riva_notif_hdle, &rnb);
475 subsys_notif_unregister_notifier(drv->modem_notif_hdle, &mnb);
476 smsm_state_cb_deregister(SMSM_Q6_STATE, SMSM_RESET,
477 adsp_smsm_state_cb, drv);
478 subsys_unregister(drv->subsys);
479 destroy_ramdump_device(drv->ramdump_dev);
480 msm_pil_unregister(drv->q6->pil);
Matt Wagantallc2bbdc32012-03-21 19:44:50 -0700481 return 0;
482}
483
484static struct of_device_id lpass_match_table[] = {
485 { .compatible = "qcom,pil-q6v5-lpass" },
486 {}
487};
488
489static struct platform_driver pil_lpass_driver = {
490 .probe = pil_lpass_driver_probe,
491 .remove = __devexit_p(pil_lpass_driver_exit),
492 .driver = {
493 .name = "pil-q6v5-lpass",
494 .of_match_table = lpass_match_table,
495 .owner = THIS_MODULE,
496 },
497};
498
499static int __init pil_lpass_init(void)
500{
501 return platform_driver_register(&pil_lpass_driver);
502}
503module_init(pil_lpass_init);
504
505static void __exit pil_lpass_exit(void)
506{
507 platform_driver_unregister(&pil_lpass_driver);
508}
509module_exit(pil_lpass_exit);
510
511MODULE_DESCRIPTION("Support for booting low-power audio subsystems with QDSP6v5 (Hexagon) processors");
512MODULE_LICENSE("GPL v2");