blob: 68e7265cc68d26c50f9d99f850bb4d05dbc7e1a1 [file] [log] [blame]
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001/*
2 * arch/arm/common/fiq_debugger.c
3 *
4 * Serial Debugger Interface accessed through an FIQ interrupt.
5 *
6 * Copyright (C) 2008 Google, Inc.
7 *
8 * This software is licensed under the terms of the GNU General Public
9 * License version 2, as published by the Free Software Foundation, and
10 * may be copied, distributed, and modified under those terms.
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
18#include <stdarg.h>
19#include <linux/module.h>
20#include <linux/io.h>
Colin Cross4df8d7b2010-08-16 14:51:51 -070021#include <linux/console.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070022#include <linux/interrupt.h>
23#include <linux/clk.h>
24#include <linux/platform_device.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070025#include <linux/kernel_stat.h>
26#include <linux/irq.h>
27#include <linux/delay.h>
28#include <linux/sched.h>
29#include <linux/slab.h>
Dima Zavin83b72702011-10-02 20:35:47 -070030#include <linux/smp.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070031#include <linux/timer.h>
Colin Cross4df8d7b2010-08-16 14:51:51 -070032#include <linux/tty.h>
33#include <linux/tty_flip.h>
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070034#include <linux/wakelock.h>
35
36#include <asm/fiq_debugger.h>
37#include <asm/fiq_glue.h>
38#include <asm/stacktrace.h>
39
40#include <mach/system.h>
41
42#include <linux/uaccess.h>
43
Colin Cross4df8d7b2010-08-16 14:51:51 -070044#include "fiq_debugger_ringbuf.h"
45
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070046#define DEBUG_MAX 64
Colin Cross24b3bd42010-10-01 23:41:38 -070047#define MAX_UNHANDLED_FIQ_COUNT 1000000
48
49#define THREAD_INFO(sp) ((struct thread_info *) \
50 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070051
52struct fiq_debugger_state {
53 struct fiq_glue_handler handler;
54
55 int fiq;
Dima Zavin83b72702011-10-02 20:35:47 -070056 int uart_irq;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070057 int signal_irq;
58 int wakeup_irq;
59 bool wakeup_irq_no_set_wake;
60 struct clk *clk;
61 struct fiq_debugger_pdata *pdata;
62 struct platform_device *pdev;
63
64 char debug_cmd[DEBUG_MAX];
65 int debug_busy;
66 int debug_abort;
67
68 char debug_buf[DEBUG_MAX];
69 int debug_count;
70
71 bool no_sleep;
72 bool debug_enable;
73 bool ignore_next_wakeup_irq;
74 struct timer_list sleep_timer;
Dima Zavin932de6c2011-10-13 22:38:45 -070075 spinlock_t sleep_timer_lock;
Dima Zavinefde6552011-10-05 14:08:20 -070076 bool uart_enabled;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070077 struct wake_lock debugger_wake_lock;
Colin Cross4df8d7b2010-08-16 14:51:51 -070078 bool console_enable;
Colin Cross24b3bd42010-10-01 23:41:38 -070079 int current_cpu;
80 atomic_t unhandled_fiq_count;
81 bool in_fiq;
Colin Cross4df8d7b2010-08-16 14:51:51 -070082
83#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
84 struct console console;
85 struct tty_driver *tty_driver;
86 struct tty_struct *tty;
87 int tty_open_count;
88 struct fiq_debugger_ringbuf *tty_rbuf;
89#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070090
91 unsigned int last_irqs[NR_IRQS];
Rebecca Schultz Zavinb824eef2010-10-22 15:55:17 -070092 unsigned int last_local_timer_irqs[NR_CPUS];
Iliyan Malchevc1db50b2010-06-05 17:36:24 -070093};
94
95#ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
96static bool initial_no_sleep = true;
97#else
98static bool initial_no_sleep;
99#endif
Dima Zavinc6fba162010-11-10 15:39:07 -0800100
101#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
102static bool initial_debug_enable = true;
103static bool initial_console_enable = true;
104#else
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700105static bool initial_debug_enable;
Colin Cross4df8d7b2010-08-16 14:51:51 -0700106static bool initial_console_enable;
Dima Zavinc6fba162010-11-10 15:39:07 -0800107#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700108
109module_param_named(no_sleep, initial_no_sleep, bool, 0644);
110module_param_named(debug_enable, initial_debug_enable, bool, 0644);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700111module_param_named(console_enable, initial_console_enable, bool, 0644);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700112
113#ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
114static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
115static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
116#else
117static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
118{
119 if (state->wakeup_irq < 0)
120 return;
121 enable_irq(state->wakeup_irq);
122 if (!state->wakeup_irq_no_set_wake)
123 enable_irq_wake(state->wakeup_irq);
124}
125static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
126{
127 if (state->wakeup_irq < 0)
128 return;
129 disable_irq_nosync(state->wakeup_irq);
130 if (!state->wakeup_irq_no_set_wake)
131 disable_irq_wake(state->wakeup_irq);
132}
133#endif
134
Dima Zavin83b72702011-10-02 20:35:47 -0700135static bool inline debug_have_fiq(struct fiq_debugger_state *state)
136{
137 return (state->fiq >= 0);
138}
139
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700140static void debug_force_irq(struct fiq_debugger_state *state)
141{
142 unsigned int irq = state->signal_irq;
Dima Zavin83b72702011-10-02 20:35:47 -0700143
144 if (WARN_ON(!debug_have_fiq(state)))
145 return;
146 if (state->pdata->force_irq) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700147 state->pdata->force_irq(state->pdev, irq);
Dima Zavin83b72702011-10-02 20:35:47 -0700148 } else {
Colin Cross999853b2011-04-08 17:26:06 -0700149 struct irq_chip *chip = irq_get_chip(irq);
150 if (chip && chip->irq_retrigger)
151 chip->irq_retrigger(irq_get_irq_data(irq));
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700152 }
153}
154
Dima Zavinefde6552011-10-05 14:08:20 -0700155static void debug_uart_enable(struct fiq_debugger_state *state)
156{
157 if (state->clk)
158 clk_enable(state->clk);
159 if (state->pdata->uart_enable)
160 state->pdata->uart_enable(state->pdev);
161}
162
163static void debug_uart_disable(struct fiq_debugger_state *state)
164{
165 if (state->pdata->uart_disable)
166 state->pdata->uart_disable(state->pdev);
167 if (state->clk)
168 clk_disable(state->clk);
169}
170
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700171static void debug_uart_flush(struct fiq_debugger_state *state)
172{
173 if (state->pdata->uart_flush)
174 state->pdata->uart_flush(state->pdev);
175}
176
177static void debug_puts(struct fiq_debugger_state *state, char *s)
178{
179 unsigned c;
180 while ((c = *s++)) {
181 if (c == '\n')
182 state->pdata->uart_putc(state->pdev, '\r');
183 state->pdata->uart_putc(state->pdev, c);
184 }
185}
186
187static void debug_prompt(struct fiq_debugger_state *state)
188{
189 debug_puts(state, "debug> ");
190}
191
192int log_buf_copy(char *dest, int idx, int len);
193static void dump_kernel_log(struct fiq_debugger_state *state)
194{
195 char buf[1024];
196 int idx = 0;
197 int ret;
198 int saved_oip;
199
200 /* setting oops_in_progress prevents log_buf_copy()
201 * from trying to take a spinlock which will make it
202 * very unhappy in some cases...
203 */
204 saved_oip = oops_in_progress;
205 oops_in_progress = 1;
206 for (;;) {
207 ret = log_buf_copy(buf, idx, 1023);
208 if (ret <= 0)
209 break;
210 buf[ret] = 0;
211 debug_puts(state, buf);
212 idx += ret;
213 }
214 oops_in_progress = saved_oip;
215}
216
217static char *mode_name(unsigned cpsr)
218{
219 switch (cpsr & MODE_MASK) {
220 case USR_MODE: return "USR";
221 case FIQ_MODE: return "FIQ";
222 case IRQ_MODE: return "IRQ";
223 case SVC_MODE: return "SVC";
224 case ABT_MODE: return "ABT";
225 case UND_MODE: return "UND";
226 case SYSTEM_MODE: return "SYS";
227 default: return "???";
228 }
229}
230
231static int debug_printf(void *cookie, const char *fmt, ...)
232{
233 struct fiq_debugger_state *state = cookie;
234 char buf[256];
235 va_list ap;
236
237 va_start(ap, fmt);
238 vsnprintf(buf, sizeof(buf), fmt, ap);
239 va_end(ap);
240
241 debug_puts(state, buf);
242 return state->debug_abort;
243}
244
245/* Safe outside fiq context */
246static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
247{
248 struct fiq_debugger_state *state = cookie;
249 char buf[256];
250 va_list ap;
251 unsigned long irq_flags;
252
253 va_start(ap, fmt);
254 vsnprintf(buf, 128, fmt, ap);
255 va_end(ap);
256
257 local_irq_save(irq_flags);
258 debug_puts(state, buf);
259 debug_uart_flush(state);
260 local_irq_restore(irq_flags);
261 return state->debug_abort;
262}
263
264static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
265{
266 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
267 regs[0], regs[1], regs[2], regs[3]);
268 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
269 regs[4], regs[5], regs[6], regs[7]);
270 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
271 regs[8], regs[9], regs[10], regs[11],
272 mode_name(regs[16]));
273 if ((regs[16] & MODE_MASK) == USR_MODE)
274 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
275 "cpsr %08x\n", regs[12], regs[13], regs[14],
276 regs[15], regs[16]);
277 else
278 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
279 "cpsr %08x spsr %08x\n", regs[12], regs[13],
280 regs[14], regs[15], regs[16], regs[17]);
281}
282
283struct mode_regs {
284 unsigned long sp_svc;
285 unsigned long lr_svc;
286 unsigned long spsr_svc;
287
288 unsigned long sp_abt;
289 unsigned long lr_abt;
290 unsigned long spsr_abt;
291
292 unsigned long sp_und;
293 unsigned long lr_und;
294 unsigned long spsr_und;
295
296 unsigned long sp_irq;
297 unsigned long lr_irq;
298 unsigned long spsr_irq;
299
300 unsigned long r8_fiq;
301 unsigned long r9_fiq;
302 unsigned long r10_fiq;
303 unsigned long r11_fiq;
304 unsigned long r12_fiq;
305 unsigned long sp_fiq;
306 unsigned long lr_fiq;
307 unsigned long spsr_fiq;
308};
309
310void __naked get_mode_regs(struct mode_regs *regs)
311{
312 asm volatile (
313 "mrs r1, cpsr\n"
314 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
315 "stmia r0!, {r13 - r14}\n"
316 "mrs r2, spsr\n"
317 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
318 "stmia r0!, {r2, r13 - r14}\n"
319 "mrs r2, spsr\n"
320 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
321 "stmia r0!, {r2, r13 - r14}\n"
322 "mrs r2, spsr\n"
323 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
324 "stmia r0!, {r2, r13 - r14}\n"
325 "mrs r2, spsr\n"
326 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
327 "stmia r0!, {r2, r8 - r14}\n"
328 "mrs r2, spsr\n"
329 "stmia r0!, {r2}\n"
330 "msr cpsr_c, r1\n"
331 "bx lr\n");
332}
333
334
335static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
336{
337 struct mode_regs mode_regs;
338 dump_regs(state, regs);
339 get_mode_regs(&mode_regs);
340 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
341 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
342 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
343 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
344 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
345 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
346 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
347 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
348 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
349 "r12 %08x\n",
350 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
351 mode_regs.r11_fiq, mode_regs.r12_fiq);
352 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
353 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
354}
355
356static void dump_irqs(struct fiq_debugger_state *state)
357{
358 int n;
Rebecca Schultz Zavinb824eef2010-10-22 15:55:17 -0700359 unsigned int cpu;
360
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700361 debug_printf(state, "irqnr total since-last status name\n");
362 for (n = 0; n < NR_IRQS; n++) {
363 struct irqaction *act = irq_desc[n].action;
364 if (!act && !kstat_irqs(n))
365 continue;
366 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
367 kstat_irqs(n),
368 kstat_irqs(n) - state->last_irqs[n],
Colin Cross999853b2011-04-08 17:26:06 -0700369 irq_desc[n].status_use_accessors,
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700370 (act && act->name) ? act->name : "???");
371 state->last_irqs[n] = kstat_irqs(n);
372 }
Rebecca Schultz Zavinb824eef2010-10-22 15:55:17 -0700373
374 for (cpu = 0; cpu < NR_CPUS; cpu++) {
375
376 debug_printf(state, "LOC %d: %10u %11u\n", cpu,
377 __IRQ_STAT(cpu, local_timer_irqs),
378 __IRQ_STAT(cpu, local_timer_irqs) -
379 state->last_local_timer_irqs[cpu]);
380 state->last_local_timer_irqs[cpu] =
381 __IRQ_STAT(cpu, local_timer_irqs);
382 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700383}
384
385struct stacktrace_state {
386 struct fiq_debugger_state *state;
387 unsigned int depth;
388};
389
390static int report_trace(struct stackframe *frame, void *d)
391{
392 struct stacktrace_state *sts = d;
393
394 if (sts->depth) {
395 debug_printf(sts->state,
396 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
397 frame->pc, frame->pc, frame->lr, frame->lr,
398 frame->sp, frame->fp);
399 sts->depth--;
400 return 0;
401 }
402 debug_printf(sts->state, " ...\n");
403
404 return sts->depth == 0;
405}
406
407struct frame_tail {
408 struct frame_tail *fp;
409 unsigned long sp;
410 unsigned long lr;
411} __attribute__((packed));
412
413static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
414 struct frame_tail *tail)
415{
416 struct frame_tail buftail[2];
417
418 /* Also check accessibility of one struct frame_tail beyond */
419 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
420 debug_printf(state, " invalid frame pointer %p\n", tail);
421 return NULL;
422 }
423 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
424 debug_printf(state,
425 " failed to copy frame pointer %p\n", tail);
426 return NULL;
427 }
428
429 debug_printf(state, " %p\n", buftail[0].lr);
430
431 /* frame pointers should strictly progress back up the stack
432 * (towards higher addresses) */
433 if (tail >= buftail[0].fp)
434 return NULL;
435
436 return buftail[0].fp-1;
437}
438
439void dump_stacktrace(struct fiq_debugger_state *state,
440 struct pt_regs * const regs, unsigned int depth, void *ssp)
441{
442 struct frame_tail *tail;
Colin Cross24b3bd42010-10-01 23:41:38 -0700443 struct thread_info *real_thread_info = THREAD_INFO(ssp);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700444 struct stacktrace_state sts;
445
446 sts.depth = depth;
447 sts.state = state;
448 *current_thread_info() = *real_thread_info;
449
450 if (!current)
451 debug_printf(state, "current NULL\n");
452 else
453 debug_printf(state, "pid: %d comm: %s\n",
454 current->pid, current->comm);
455 dump_regs(state, (unsigned *)regs);
456
457 if (!user_mode(regs)) {
458 struct stackframe frame;
459 frame.fp = regs->ARM_fp;
460 frame.sp = regs->ARM_sp;
461 frame.lr = regs->ARM_lr;
462 frame.pc = regs->ARM_pc;
463 debug_printf(state,
464 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
465 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
466 regs->ARM_sp, regs->ARM_fp);
467 walk_stackframe(&frame, report_trace, &sts);
468 return;
469 }
470
471 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
472 while (depth-- && tail && !((unsigned long) tail & 3))
473 tail = user_backtrace(state, tail);
474}
475
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800476static void do_ps(struct fiq_debugger_state *state)
477{
478 struct task_struct *g;
479 struct task_struct *p;
480 unsigned task_state;
481 static const char stat_nam[] = "RSDTtZX";
482
483 debug_printf(state, "pid ppid prio task pc\n");
484 read_lock(&tasklist_lock);
485 do_each_thread(g, p) {
486 task_state = p->state ? __ffs(p->state) + 1 : 0;
487 debug_printf(state,
488 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
489 debug_printf(state, "%-13.13s %c", p->comm,
490 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
491 if (task_state == TASK_RUNNING)
492 debug_printf(state, " running\n");
493 else
494 debug_printf(state, " %08lx\n", thread_saved_pc(p));
495 } while_each_thread(g, p);
496 read_unlock(&tasklist_lock);
497}
498
499extern int do_syslog(int type, char __user *bug, int count);
500static void do_sysrq(struct fiq_debugger_state *state, char rq)
501{
502 char buf[128];
503 int ret;
504 int idx = 0;
505 do_syslog(5 /* clear */, NULL, 0);
506 handle_sysrq(rq);
507 while (1) {
508 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
509 if (ret <= 0)
510 break;
511 buf[ret] = 0;
512 debug_printf(state, "%s", buf);
513 idx += ret;
514 }
515}
516
517/* This function CANNOT be called in FIQ context */
518static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
519{
520 if (!strcmp(cmd, "ps"))
521 do_ps(state);
522 if (!strcmp(cmd, "sysrq"))
523 do_sysrq(state, 'h');
524 if (!strncmp(cmd, "sysrq ", 6))
525 do_sysrq(state, cmd[6]);
526}
527
528static void debug_help(struct fiq_debugger_state *state)
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800529{
530 debug_printf(state, "FIQ Debugger commands:\n"
531 " pc PC status\n"
532 " regs Register dump\n"
533 " allregs Extended Register dump\n"
534 " bt Stack trace\n"
535 " reboot Reboot\n"
536 " irqs Interupt status\n"
537 " kmsg Kernel log\n"
538 " version Kernel version\n");
539 debug_printf(state, " sleep Allow sleep while in FIQ\n"
540 " nosleep Disable sleep while in FIQ\n"
541 " console Switch terminal to console\n"
542 " cpu Current CPU\n"
543 " cpu <number> Switch to CPU<number>\n");
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800544 debug_printf(state, " ps Process list\n"
545 " sysrq sysrq options\n"
546 " sysrq <param> Execute sysrq with <param>\n");
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800547}
548
Dima Zavin83b72702011-10-02 20:35:47 -0700549static void take_affinity(void *info)
550{
551 struct fiq_debugger_state *state = info;
552 struct cpumask cpumask;
553
554 cpumask_clear(&cpumask);
555 cpumask_set_cpu(get_cpu(), &cpumask);
556
557 irq_set_affinity(state->uart_irq, &cpumask);
558}
559
560static void switch_cpu(struct fiq_debugger_state *state, int cpu)
561{
562 if (!debug_have_fiq(state))
563 smp_call_function_single(cpu, take_affinity, state, false);
564 state->current_cpu = cpu;
565}
566
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800567static bool debug_fiq_exec(struct fiq_debugger_state *state,
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700568 const char *cmd, unsigned *regs, void *svc_sp)
569{
Dima Zavin83b72702011-10-02 20:35:47 -0700570 bool signal_helper = false;
571
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800572 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800573 debug_help(state);
Dmitry Shmidt5eed1db2010-11-16 15:40:13 -0800574 } else if (!strcmp(cmd, "pc")) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700575 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
576 regs[15], regs[16], mode_name(regs[16]));
577 } else if (!strcmp(cmd, "regs")) {
578 dump_regs(state, regs);
579 } else if (!strcmp(cmd, "allregs")) {
580 dump_allregs(state, regs);
581 } else if (!strcmp(cmd, "bt")) {
582 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
583 } else if (!strcmp(cmd, "reboot")) {
584 arch_reset(0, 0);
585 } else if (!strcmp(cmd, "irqs")) {
586 dump_irqs(state);
587 } else if (!strcmp(cmd, "kmsg")) {
588 dump_kernel_log(state);
589 } else if (!strcmp(cmd, "version")) {
590 debug_printf(state, "%s\n", linux_banner);
591 } else if (!strcmp(cmd, "sleep")) {
592 state->no_sleep = false;
Dima Zavin83b72702011-10-02 20:35:47 -0700593 debug_printf(state, "enabling sleep\n");
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700594 } else if (!strcmp(cmd, "nosleep")) {
595 state->no_sleep = true;
Dima Zavin83b72702011-10-02 20:35:47 -0700596 debug_printf(state, "disabling sleep\n");
Colin Cross4df8d7b2010-08-16 14:51:51 -0700597 } else if (!strcmp(cmd, "console")) {
598 state->console_enable = true;
599 debug_printf(state, "console mode\n");
Colin Cross24b3bd42010-10-01 23:41:38 -0700600 } else if (!strcmp(cmd, "cpu")) {
601 debug_printf(state, "cpu %d\n", state->current_cpu);
602 } else if (!strncmp(cmd, "cpu ", 4)) {
603 unsigned long cpu = 0;
604 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
Dima Zavin83b72702011-10-02 20:35:47 -0700605 switch_cpu(state, cpu);
Colin Cross24b3bd42010-10-01 23:41:38 -0700606 else
607 debug_printf(state, "invalid cpu\n");
608 debug_printf(state, "cpu %d\n", state->current_cpu);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700609 } else {
610 if (state->debug_busy) {
611 debug_printf(state,
612 "command processor busy. trying to abort.\n");
613 state->debug_abort = -1;
614 } else {
615 strcpy(state->debug_cmd, cmd);
616 state->debug_busy = 1;
617 }
618
Dima Zavin83b72702011-10-02 20:35:47 -0700619 return true;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700620 }
Colin Cross4df8d7b2010-08-16 14:51:51 -0700621 if (!state->console_enable)
622 debug_prompt(state);
Dima Zavin83b72702011-10-02 20:35:47 -0700623
624 return signal_helper;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700625}
626
627static void sleep_timer_expired(unsigned long data)
628{
629 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
Dima Zavin932de6c2011-10-13 22:38:45 -0700630 unsigned long flags;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700631
Dima Zavin932de6c2011-10-13 22:38:45 -0700632 spin_lock_irqsave(&state->sleep_timer_lock, flags);
Dima Zavinefde6552011-10-05 14:08:20 -0700633 if (state->uart_enabled && !state->no_sleep) {
Dima Zavin48ef31a2011-10-09 11:47:35 -0700634 if (state->debug_enable && !state->console_enable) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700635 state->debug_enable = false;
636 debug_printf_nfiq(state, "suspending fiq debugger\n");
637 }
638 state->ignore_next_wakeup_irq = true;
Dima Zavinefde6552011-10-05 14:08:20 -0700639 debug_uart_disable(state);
640 state->uart_enabled = false;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700641 enable_wakeup_irq(state);
642 }
643 wake_unlock(&state->debugger_wake_lock);
Dima Zavin932de6c2011-10-13 22:38:45 -0700644 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700645}
646
Dima Zavin83b72702011-10-02 20:35:47 -0700647static void handle_wakeup(struct fiq_debugger_state *state)
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700648{
Dima Zavin932de6c2011-10-13 22:38:45 -0700649 unsigned long flags;
650
651 spin_lock_irqsave(&state->sleep_timer_lock, flags);
Dima Zavin83b72702011-10-02 20:35:47 -0700652 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700653 state->ignore_next_wakeup_irq = false;
Dima Zavinefde6552011-10-05 14:08:20 -0700654 } else if (!state->uart_enabled) {
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700655 wake_lock(&state->debugger_wake_lock);
Dima Zavinefde6552011-10-05 14:08:20 -0700656 debug_uart_enable(state);
657 state->uart_enabled = true;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700658 disable_wakeup_irq(state);
659 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
660 }
Dima Zavin932de6c2011-10-13 22:38:45 -0700661 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
Dima Zavin83b72702011-10-02 20:35:47 -0700662}
663
664static irqreturn_t wakeup_irq_handler(int irq, void *dev)
665{
666 struct fiq_debugger_state *state = dev;
667
668 if (!state->no_sleep)
669 debug_puts(state, "WAKEUP\n");
670 handle_wakeup(state);
671
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700672 return IRQ_HANDLED;
673}
674
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700675
Dima Zavin83b72702011-10-02 20:35:47 -0700676static void debug_handle_irq_context(struct fiq_debugger_state *state)
677{
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700678 if (!state->no_sleep) {
Dima Zavin932de6c2011-10-13 22:38:45 -0700679 unsigned long flags;
680
681 spin_lock_irqsave(&state->sleep_timer_lock, flags);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700682 wake_lock(&state->debugger_wake_lock);
683 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
Dima Zavin932de6c2011-10-13 22:38:45 -0700684 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700685 }
Colin Cross4df8d7b2010-08-16 14:51:51 -0700686#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
687 if (state->tty) {
688 int i;
689 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
690 for (i = 0; i < count; i++) {
Dima Zavinb0092752011-10-25 21:24:10 -0700691 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700692 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
693 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
694 pr_warn("fiq tty failed to consume byte\n");
695 }
696 tty_flip_buffer_push(state->tty);
697 }
698#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700699 if (state->debug_busy) {
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800700 debug_irq_exec(state, state->debug_cmd);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700701 debug_prompt(state);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700702 state->debug_busy = 0;
703 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700704}
705
706static int debug_getc(struct fiq_debugger_state *state)
707{
708 return state->pdata->uart_getc(state->pdev);
709}
710
Dima Zavin83b72702011-10-02 20:35:47 -0700711static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
712 int this_cpu, void *regs, void *svc_sp)
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700713{
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700714 int c;
715 static int last_c;
716 int count = 0;
Dima Zavin83b72702011-10-02 20:35:47 -0700717 bool signal_helper = false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700718
719 if (this_cpu != state->current_cpu) {
720 if (state->in_fiq)
Dima Zavin83b72702011-10-02 20:35:47 -0700721 return false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700722
723 if (atomic_inc_return(&state->unhandled_fiq_count) !=
724 MAX_UNHANDLED_FIQ_COUNT)
Dima Zavin83b72702011-10-02 20:35:47 -0700725 return false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700726
727 debug_printf(state, "fiq_debugger: cpu %d not responding, "
728 "reverting to cpu %d\n", state->current_cpu,
729 this_cpu);
730
731 atomic_set(&state->unhandled_fiq_count, 0);
Dima Zavin83b72702011-10-02 20:35:47 -0700732 switch_cpu(state, this_cpu);
733 return false;
Colin Cross24b3bd42010-10-01 23:41:38 -0700734 }
735
736 state->in_fiq = true;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700737
738 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
739 count++;
740 if (!state->debug_enable) {
741 if ((c == 13) || (c == 10)) {
742 state->debug_enable = true;
743 state->debug_count = 0;
744 debug_prompt(state);
745 }
Colin Cross4df8d7b2010-08-16 14:51:51 -0700746 } else if (c == FIQ_DEBUGGER_BREAK) {
747 state->console_enable = false;
748 debug_puts(state, "fiq debugger mode\n");
749 state->debug_count = 0;
750 debug_prompt(state);
751#ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
752 } else if (state->console_enable && state->tty_rbuf) {
753 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
Dima Zavin83b72702011-10-02 20:35:47 -0700754 signal_helper = true;
Colin Cross4df8d7b2010-08-16 14:51:51 -0700755#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700756 } else if ((c >= ' ') && (c < 127)) {
757 if (state->debug_count < (DEBUG_MAX - 1)) {
758 state->debug_buf[state->debug_count++] = c;
759 state->pdata->uart_putc(state->pdev, c);
760 }
761 } else if ((c == 8) || (c == 127)) {
762 if (state->debug_count > 0) {
763 state->debug_count--;
764 state->pdata->uart_putc(state->pdev, 8);
765 state->pdata->uart_putc(state->pdev, ' ');
766 state->pdata->uart_putc(state->pdev, 8);
767 }
768 } else if ((c == 13) || (c == 10)) {
769 if (c == '\r' || (c == '\n' && last_c != '\r')) {
770 state->pdata->uart_putc(state->pdev, '\r');
771 state->pdata->uart_putc(state->pdev, '\n');
772 }
773 if (state->debug_count) {
774 state->debug_buf[state->debug_count] = 0;
775 state->debug_count = 0;
Dima Zavin83b72702011-10-02 20:35:47 -0700776 signal_helper |=
Dima Zavinb11ab5b2011-11-09 16:10:57 -0800777 debug_fiq_exec(state, state->debug_buf,
778 regs, svc_sp);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700779 } else {
780 debug_prompt(state);
781 }
782 }
783 last_c = c;
784 }
785 debug_uart_flush(state);
786 if (state->pdata->fiq_ack)
787 state->pdata->fiq_ack(state->pdev, state->fiq);
788
789 /* poke sleep timer if necessary */
790 if (state->debug_enable && !state->no_sleep)
Dima Zavin83b72702011-10-02 20:35:47 -0700791 signal_helper = true;
Colin Cross24b3bd42010-10-01 23:41:38 -0700792
793 atomic_set(&state->unhandled_fiq_count, 0);
794 state->in_fiq = false;
Dima Zavin83b72702011-10-02 20:35:47 -0700795
796 return signal_helper;
797}
798
799static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
800{
801 struct fiq_debugger_state *state =
802 container_of(h, struct fiq_debugger_state, handler);
803 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
804 bool need_irq;
805
806 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
807 if (need_irq)
808 debug_force_irq(state);
809}
810
811/*
812 * When not using FIQs, we only use this single interrupt as an entry point.
813 * This just effectively takes over the UART interrupt and does all the work
814 * in this context.
815 */
816static irqreturn_t debug_uart_irq(int irq, void *dev)
817{
818 struct fiq_debugger_state *state = dev;
819 bool not_done;
820
821 handle_wakeup(state);
822
823 /* handle the debugger irq in regular context */
824 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
825 get_irq_regs(),
826 current_thread_info());
827 if (not_done)
828 debug_handle_irq_context(state);
829
830 return IRQ_HANDLED;
831}
832
833/*
834 * If FIQs are used, not everything can happen in fiq context.
835 * FIQ handler does what it can and then signals this interrupt to finish the
836 * job in irq context.
837 */
838static irqreturn_t debug_signal_irq(int irq, void *dev)
839{
840 struct fiq_debugger_state *state = dev;
841
842 if (state->pdata->force_irq_ack)
843 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
844
845 debug_handle_irq_context(state);
846
847 return IRQ_HANDLED;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -0700848}
849
850static void debug_resume(struct fiq_glue_handler *h)
851{
852 struct fiq_debugger_state *state =
853 container_of(h, struct fiq_debugger_state, handler);
854 if (state->pdata->uart_resume)
855 state->pdata->uart_resume(state->pdev);
856}
857
Colin Cross4df8d7b2010-08-16 14:51:51 -0700858#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
859struct tty_driver *debug_console_device(struct console *co, int *index)
860{
861 struct fiq_debugger_state *state;
862 state = container_of(co, struct fiq_debugger_state, console);
863 *index = 0;
864 return state->tty_driver;
865}
866
867static void debug_console_write(struct console *co,
868 const char *s, unsigned int count)
869{
870 struct fiq_debugger_state *state;
871
872 state = container_of(co, struct fiq_debugger_state, console);
873
874 if (!state->console_enable)
875 return;
876
Dima Zavinefde6552011-10-05 14:08:20 -0700877 debug_uart_enable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700878 while (count--) {
879 if (*s == '\n')
880 state->pdata->uart_putc(state->pdev, '\r');
881 state->pdata->uart_putc(state->pdev, *s++);
882 }
883 debug_uart_flush(state);
Dima Zavinefde6552011-10-05 14:08:20 -0700884 debug_uart_disable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700885}
886
887static struct console fiq_debugger_console = {
888 .name = "ttyFIQ",
889 .device = debug_console_device,
890 .write = debug_console_write,
891 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
892};
893
894int fiq_tty_open(struct tty_struct *tty, struct file *filp)
895{
896 struct fiq_debugger_state *state = tty->driver->driver_state;
897 if (state->tty_open_count++)
898 return 0;
899
900 tty->driver_data = state;
901 state->tty = tty;
902 return 0;
903}
904
905void fiq_tty_close(struct tty_struct *tty, struct file *filp)
906{
907 struct fiq_debugger_state *state = tty->driver_data;
908 if (--state->tty_open_count)
909 return;
910 state->tty = NULL;
911}
912
913int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
914{
915 int i;
916 struct fiq_debugger_state *state = tty->driver_data;
917
918 if (!state->console_enable)
919 return count;
920
Dima Zavinefde6552011-10-05 14:08:20 -0700921 debug_uart_enable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700922 for (i = 0; i < count; i++)
923 state->pdata->uart_putc(state->pdev, *buf++);
Dima Zavinefde6552011-10-05 14:08:20 -0700924 debug_uart_disable(state);
Colin Cross4df8d7b2010-08-16 14:51:51 -0700925
926 return count;
927}
928
929int fiq_tty_write_room(struct tty_struct *tty)
930{
931 return 1024;
932}
933
934static const struct tty_operations fiq_tty_driver_ops = {
935 .write = fiq_tty_write,
936 .write_room = fiq_tty_write_room,
937 .open = fiq_tty_open,
938 .close = fiq_tty_close,
939};
940
941static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
942{
943 int ret = -EINVAL;
944
945 state->tty_driver = alloc_tty_driver(1);
946 if (!state->tty_driver) {
947 pr_err("Failed to allocate fiq debugger tty\n");
948 return -ENOMEM;
949 }
950
951 state->tty_driver->owner = THIS_MODULE;
952 state->tty_driver->driver_name = "fiq-debugger";
953 state->tty_driver->name = "ttyFIQ";
954 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
955 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
956 state->tty_driver->init_termios = tty_std_termios;
957 state->tty_driver->init_termios.c_cflag =
958 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
959 state->tty_driver->init_termios.c_ispeed =
960 state->tty_driver->init_termios.c_ospeed = 115200;
961 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
962 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
963 state->tty_driver->driver_state = state;
964
965 ret = tty_register_driver(state->tty_driver);
966 if (ret) {
967 pr_err("Failed to register fiq tty: %d\n", ret);
968 goto err;
969 }
970
971 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
972 if (!state->tty_rbuf) {
973 pr_err("Failed to allocate fiq debugger ringbuf\n");
974 ret = -ENOMEM;
975 goto err;
976 }
977
978 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
979 return 0;
980
981err:
982 fiq_debugger_ringbuf_free(state->tty_rbuf);
983 state->tty_rbuf = NULL;
984 put_tty_driver(state->tty_driver);
985 return ret;
986}
987#endif
988
Dima Zavinf4aea212011-10-10 15:24:34 -0700989static int fiq_debugger_dev_suspend(struct device *dev)
990{
991 struct platform_device *pdev = to_platform_device(dev);
992 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
993
994 if (state->pdata->uart_dev_suspend)
995 return state->pdata->uart_dev_suspend(pdev);
996 return 0;
997}
998
999static int fiq_debugger_dev_resume(struct device *dev)
1000{
1001 struct platform_device *pdev = to_platform_device(dev);
1002 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1003
1004 if (state->pdata->uart_dev_resume)
1005 return state->pdata->uart_dev_resume(pdev);
1006 return 0;
1007}
1008
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001009static int fiq_debugger_probe(struct platform_device *pdev)
1010{
1011 int ret;
1012 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1013 struct fiq_debugger_state *state;
Dima Zavin83b72702011-10-02 20:35:47 -07001014 int fiq;
1015 int uart_irq;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001016
Dima Zavin83b72702011-10-02 20:35:47 -07001017 if (!pdata->uart_getc || !pdata->uart_putc)
1018 return -EINVAL;
Dima Zavinefde6552011-10-05 14:08:20 -07001019 if ((pdata->uart_enable && !pdata->uart_disable) ||
1020 (!pdata->uart_enable && pdata->uart_disable))
1021 return -EINVAL;
Dima Zavin83b72702011-10-02 20:35:47 -07001022
1023 fiq = platform_get_irq_byname(pdev, "fiq");
1024 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1025
1026 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1027 * is required */
1028 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1029 return -EINVAL;
1030 if (fiq >= 0 && !pdata->fiq_enable)
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001031 return -EINVAL;
1032
1033 state = kzalloc(sizeof(*state), GFP_KERNEL);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001034 setup_timer(&state->sleep_timer, sleep_timer_expired,
1035 (unsigned long)state);
1036 state->pdata = pdata;
1037 state->pdev = pdev;
1038 state->no_sleep = initial_no_sleep;
1039 state->debug_enable = initial_debug_enable;
Colin Cross4df8d7b2010-08-16 14:51:51 -07001040 state->console_enable = initial_console_enable;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001041
Dima Zavin83b72702011-10-02 20:35:47 -07001042 state->fiq = fiq;
1043 state->uart_irq = uart_irq;
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001044 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1045 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1046
Dima Zavinf4aea212011-10-10 15:24:34 -07001047 platform_set_drvdata(pdev, state);
1048
Dima Zavin932de6c2011-10-13 22:38:45 -07001049 spin_lock_init(&state->sleep_timer_lock);
1050
Dima Zavin83b72702011-10-02 20:35:47 -07001051 if (state->wakeup_irq < 0 && debug_have_fiq(state))
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001052 state->no_sleep = true;
1053 state->ignore_next_wakeup_irq = !state->no_sleep;
1054
1055 wake_lock_init(&state->debugger_wake_lock,
1056 WAKE_LOCK_SUSPEND, "serial-debug");
1057
1058 state->clk = clk_get(&pdev->dev, NULL);
1059 if (IS_ERR(state->clk))
1060 state->clk = NULL;
1061
Dima Zavinefde6552011-10-05 14:08:20 -07001062 /* do not call pdata->uart_enable here since uart_init may still
1063 * need to do some initialization before uart_enable can work.
1064 * So, only try to manage the clock during init.
1065 */
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001066 if (state->clk)
1067 clk_enable(state->clk);
1068
1069 if (pdata->uart_init) {
1070 ret = pdata->uart_init(pdev);
1071 if (ret)
1072 goto err_uart_init;
1073 }
1074
1075 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1076 state->no_sleep ? "" : "twice ");
1077
Dima Zavin83b72702011-10-02 20:35:47 -07001078 if (debug_have_fiq(state)) {
1079 state->handler.fiq = debug_fiq;
1080 state->handler.resume = debug_resume;
1081 ret = fiq_glue_register_handler(&state->handler);
1082 if (ret) {
1083 pr_err("%s: could not install fiq handler\n", __func__);
1084 goto err_register_fiq;
1085 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001086
Dima Zavin83b72702011-10-02 20:35:47 -07001087 pdata->fiq_enable(pdev, state->fiq, 1);
1088 } else {
1089 ret = request_irq(state->uart_irq, debug_uart_irq,
Dima Zavinb8cfed02011-10-27 16:31:24 -07001090 IRQF_NO_SUSPEND, "debug", state);
Dima Zavin83b72702011-10-02 20:35:47 -07001091 if (ret) {
1092 pr_err("%s: could not install irq handler\n", __func__);
1093 goto err_register_irq;
1094 }
1095
1096 /* for irq-only mode, we want this irq to wake us up, if it
1097 * can.
1098 */
1099 enable_irq_wake(state->uart_irq);
1100 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001101
1102 if (state->clk)
1103 clk_disable(state->clk);
1104
Dima Zavin83b72702011-10-02 20:35:47 -07001105 if (state->signal_irq >= 0) {
1106 ret = request_irq(state->signal_irq, debug_signal_irq,
1107 IRQF_TRIGGER_RISING, "debug-signal", state);
1108 if (ret)
1109 pr_err("serial_debugger: could not install signal_irq");
1110 }
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001111
1112 if (state->wakeup_irq >= 0) {
1113 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1114 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1115 "debug-wakeup", state);
1116 if (ret) {
1117 pr_err("serial_debugger: "
1118 "could not install wakeup irq\n");
1119 state->wakeup_irq = -1;
1120 } else {
1121 ret = enable_irq_wake(state->wakeup_irq);
1122 if (ret) {
1123 pr_err("serial_debugger: "
1124 "could not enable wakeup\n");
1125 state->wakeup_irq_no_set_wake = true;
1126 }
1127 }
1128 }
1129 if (state->no_sleep)
Dima Zavin83b72702011-10-02 20:35:47 -07001130 handle_wakeup(state);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001131
Colin Cross4df8d7b2010-08-16 14:51:51 -07001132#if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1133 state->console = fiq_debugger_console;
1134 register_console(&state->console);
1135 fiq_debugger_tty_init(state);
1136#endif
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001137 return 0;
1138
Dima Zavin83b72702011-10-02 20:35:47 -07001139err_register_irq:
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001140err_register_fiq:
1141 if (pdata->uart_free)
1142 pdata->uart_free(pdev);
1143err_uart_init:
Dima Zavin0d5d8ec2011-10-20 14:48:37 -07001144 if (state->clk)
1145 clk_disable(state->clk);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001146 if (state->clk)
1147 clk_put(state->clk);
Dima Zavin0d5d8ec2011-10-20 14:48:37 -07001148 wake_lock_destroy(&state->debugger_wake_lock);
Dima Zavinf4aea212011-10-10 15:24:34 -07001149 platform_set_drvdata(pdev, NULL);
Dima Zavin0d5d8ec2011-10-20 14:48:37 -07001150 kfree(state);
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001151 return ret;
1152}
1153
Dima Zavinf4aea212011-10-10 15:24:34 -07001154static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1155 .suspend = fiq_debugger_dev_suspend,
1156 .resume = fiq_debugger_dev_resume,
1157};
1158
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001159static struct platform_driver fiq_debugger_driver = {
Dima Zavinf4aea212011-10-10 15:24:34 -07001160 .probe = fiq_debugger_probe,
1161 .driver = {
1162 .name = "fiq_debugger",
1163 .pm = &fiq_debugger_dev_pm_ops,
1164 },
Iliyan Malchevc1db50b2010-06-05 17:36:24 -07001165};
1166
1167static int __init fiq_debugger_init(void)
1168{
1169 return platform_driver_register(&fiq_debugger_driver);
1170}
1171
1172postcore_initcall(fiq_debugger_init);