2 * arch/arm/common/fiq_debugger.c
4 * Serial Debugger Interface accessed through an FIQ interrupt.
6 * Copyright (C) 2008 Google, Inc.
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.
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.
19 #include <linux/module.h>
21 #include <linux/console.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/irq.h>
27 #include <linux/delay.h>
28 #include <linux/reboot.h>
29 #include <linux/sched.h>
30 #include <linux/slab.h>
31 #include <linux/smp.h>
32 #include <linux/timer.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
35 #include <linux/wakelock.h>
37 #include <asm/fiq_debugger.h>
38 #include <asm/fiq_glue.h>
39 #include <asm/stacktrace.h>
41 #include <linux/uaccess.h>
43 #include "fiq_debugger_ringbuf.h"
46 #define MAX_UNHANDLED_FIQ_COUNT 1000000
48 #define THREAD_INFO(sp) ((struct thread_info *) \
49 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
51 struct fiq_debugger_state {
52 struct fiq_glue_handler handler;
58 bool wakeup_irq_no_set_wake;
60 struct fiq_debugger_pdata *pdata;
61 struct platform_device *pdev;
63 char debug_cmd[DEBUG_MAX];
67 char debug_buf[DEBUG_MAX];
72 bool ignore_next_wakeup_irq;
73 struct timer_list sleep_timer;
74 spinlock_t sleep_timer_lock;
76 struct wake_lock debugger_wake_lock;
79 atomic_t unhandled_fiq_count;
82 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
83 struct console console;
84 struct tty_driver *tty_driver;
85 struct tty_struct *tty;
87 struct fiq_debugger_ringbuf *tty_rbuf;
91 unsigned int last_irqs[NR_IRQS];
92 unsigned int last_local_timer_irqs[NR_CPUS];
95 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
96 static bool initial_no_sleep = true;
98 static bool initial_no_sleep;
101 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
102 static bool initial_debug_enable = true;
103 static bool initial_console_enable = true;
105 static bool initial_debug_enable;
106 static bool initial_console_enable;
109 static bool fiq_kgdb_enable;
111 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
112 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
113 module_param_named(console_enable, initial_console_enable, bool, 0644);
114 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
116 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
117 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
118 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
120 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
122 if (state->wakeup_irq < 0)
124 enable_irq(state->wakeup_irq);
125 if (!state->wakeup_irq_no_set_wake)
126 enable_irq_wake(state->wakeup_irq);
128 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
130 if (state->wakeup_irq < 0)
132 disable_irq_nosync(state->wakeup_irq);
133 if (!state->wakeup_irq_no_set_wake)
134 disable_irq_wake(state->wakeup_irq);
138 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
140 return (state->fiq >= 0);
143 static void debug_force_irq(struct fiq_debugger_state *state)
145 unsigned int irq = state->signal_irq;
147 if (WARN_ON(!debug_have_fiq(state)))
149 if (state->pdata->force_irq) {
150 state->pdata->force_irq(state->pdev, irq);
152 struct irq_chip *chip = irq_get_chip(irq);
153 if (chip && chip->irq_retrigger)
154 chip->irq_retrigger(irq_get_irq_data(irq));
158 static void debug_uart_enable(struct fiq_debugger_state *state)
161 clk_enable(state->clk);
162 if (state->pdata->uart_enable)
163 state->pdata->uart_enable(state->pdev);
166 static void debug_uart_disable(struct fiq_debugger_state *state)
168 if (state->pdata->uart_disable)
169 state->pdata->uart_disable(state->pdev);
171 clk_disable(state->clk);
174 static void debug_uart_flush(struct fiq_debugger_state *state)
176 if (state->pdata->uart_flush)
177 state->pdata->uart_flush(state->pdev);
180 static void debug_putc(struct fiq_debugger_state *state, char c)
182 state->pdata->uart_putc(state->pdev, c);
185 static void debug_puts(struct fiq_debugger_state *state, char *s)
190 debug_putc(state, '\r');
191 debug_putc(state, c);
195 static void debug_prompt(struct fiq_debugger_state *state)
197 debug_puts(state, "debug> ");
200 int log_buf_copy(char *dest, int idx, int len);
201 static void dump_kernel_log(struct fiq_debugger_state *state)
208 /* setting oops_in_progress prevents log_buf_copy()
209 * from trying to take a spinlock which will make it
210 * very unhappy in some cases...
212 saved_oip = oops_in_progress;
213 oops_in_progress = 1;
215 ret = log_buf_copy(buf, idx, 1023);
219 debug_puts(state, buf);
222 oops_in_progress = saved_oip;
225 static char *mode_name(unsigned cpsr)
227 switch (cpsr & MODE_MASK) {
228 case USR_MODE: return "USR";
229 case FIQ_MODE: return "FIQ";
230 case IRQ_MODE: return "IRQ";
231 case SVC_MODE: return "SVC";
232 case ABT_MODE: return "ABT";
233 case UND_MODE: return "UND";
234 case SYSTEM_MODE: return "SYS";
235 default: return "???";
239 static int debug_printf(void *cookie, const char *fmt, ...)
241 struct fiq_debugger_state *state = cookie;
246 vsnprintf(buf, sizeof(buf), fmt, ap);
249 debug_puts(state, buf);
250 return state->debug_abort;
253 /* Safe outside fiq context */
254 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
256 struct fiq_debugger_state *state = cookie;
259 unsigned long irq_flags;
262 vsnprintf(buf, 128, fmt, ap);
265 local_irq_save(irq_flags);
266 debug_puts(state, buf);
267 debug_uart_flush(state);
268 local_irq_restore(irq_flags);
269 return state->debug_abort;
272 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
274 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
275 regs[0], regs[1], regs[2], regs[3]);
276 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
277 regs[4], regs[5], regs[6], regs[7]);
278 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
279 regs[8], regs[9], regs[10], regs[11],
280 mode_name(regs[16]));
281 if ((regs[16] & MODE_MASK) == USR_MODE)
282 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
283 "cpsr %08x\n", regs[12], regs[13], regs[14],
286 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
287 "cpsr %08x spsr %08x\n", regs[12], regs[13],
288 regs[14], regs[15], regs[16], regs[17]);
292 unsigned long sp_svc;
293 unsigned long lr_svc;
294 unsigned long spsr_svc;
296 unsigned long sp_abt;
297 unsigned long lr_abt;
298 unsigned long spsr_abt;
300 unsigned long sp_und;
301 unsigned long lr_und;
302 unsigned long spsr_und;
304 unsigned long sp_irq;
305 unsigned long lr_irq;
306 unsigned long spsr_irq;
308 unsigned long r8_fiq;
309 unsigned long r9_fiq;
310 unsigned long r10_fiq;
311 unsigned long r11_fiq;
312 unsigned long r12_fiq;
313 unsigned long sp_fiq;
314 unsigned long lr_fiq;
315 unsigned long spsr_fiq;
318 void __naked get_mode_regs(struct mode_regs *regs)
322 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
323 "stmia r0!, {r13 - r14}\n"
325 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
326 "stmia r0!, {r2, r13 - r14}\n"
328 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
329 "stmia r0!, {r2, r13 - r14}\n"
331 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
332 "stmia r0!, {r2, r13 - r14}\n"
334 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
335 "stmia r0!, {r2, r8 - r14}\n"
343 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
345 struct mode_regs mode_regs;
346 dump_regs(state, regs);
347 get_mode_regs(&mode_regs);
348 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
349 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
350 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
351 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
352 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
353 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
354 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
355 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
356 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
358 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
359 mode_regs.r11_fiq, mode_regs.r12_fiq);
360 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
361 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
364 static void dump_irqs(struct fiq_debugger_state *state)
368 debug_printf(state, "irqnr total since-last status name\n");
369 for (n = 0; n < NR_IRQS; n++) {
370 struct irqaction *act = irq_desc[n].action;
371 if (!act && !kstat_irqs(n))
373 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
375 kstat_irqs(n) - state->last_irqs[n],
376 irq_desc[n].status_use_accessors,
377 (act && act->name) ? act->name : "???");
378 state->last_irqs[n] = kstat_irqs(n);
382 struct stacktrace_state {
383 struct fiq_debugger_state *state;
387 static int report_trace(struct stackframe *frame, void *d)
389 struct stacktrace_state *sts = d;
392 debug_printf(sts->state,
393 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
394 frame->pc, frame->pc, frame->lr, frame->lr,
395 frame->sp, frame->fp);
399 debug_printf(sts->state, " ...\n");
401 return sts->depth == 0;
405 struct frame_tail *fp;
408 } __attribute__((packed));
410 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
411 struct frame_tail *tail)
413 struct frame_tail buftail[2];
415 /* Also check accessibility of one struct frame_tail beyond */
416 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
417 debug_printf(state, " invalid frame pointer %p\n", tail);
420 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
422 " failed to copy frame pointer %p\n", tail);
426 debug_printf(state, " %p\n", buftail[0].lr);
428 /* frame pointers should strictly progress back up the stack
429 * (towards higher addresses) */
430 if (tail >= buftail[0].fp)
433 return buftail[0].fp-1;
436 void dump_stacktrace(struct fiq_debugger_state *state,
437 struct pt_regs * const regs, unsigned int depth, void *ssp)
439 struct frame_tail *tail;
440 struct thread_info *real_thread_info = THREAD_INFO(ssp);
441 struct stacktrace_state sts;
445 *current_thread_info() = *real_thread_info;
448 debug_printf(state, "current NULL\n");
450 debug_printf(state, "pid: %d comm: %s\n",
451 current->pid, current->comm);
452 dump_regs(state, (unsigned *)regs);
454 if (!user_mode(regs)) {
455 struct stackframe frame;
456 frame.fp = regs->ARM_fp;
457 frame.sp = regs->ARM_sp;
458 frame.lr = regs->ARM_lr;
459 frame.pc = regs->ARM_pc;
461 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
462 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
463 regs->ARM_sp, regs->ARM_fp);
464 walk_stackframe(&frame, report_trace, &sts);
468 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
469 while (depth-- && tail && !((unsigned long) tail & 3))
470 tail = user_backtrace(state, tail);
473 static void do_ps(struct fiq_debugger_state *state)
475 struct task_struct *g;
476 struct task_struct *p;
478 static const char stat_nam[] = "RSDTtZX";
480 debug_printf(state, "pid ppid prio task pc\n");
481 read_lock(&tasklist_lock);
482 do_each_thread(g, p) {
483 task_state = p->state ? __ffs(p->state) + 1 : 0;
485 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
486 debug_printf(state, "%-13.13s %c", p->comm,
487 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
488 if (task_state == TASK_RUNNING)
489 debug_printf(state, " running\n");
491 debug_printf(state, " %08lx\n", thread_saved_pc(p));
492 } while_each_thread(g, p);
493 read_unlock(&tasklist_lock);
496 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
497 static void begin_syslog_dump(struct fiq_debugger_state *state)
499 state->syslog_dumping = true;
502 static void end_syslog_dump(struct fiq_debugger_state *state)
504 state->syslog_dumping = false;
507 extern int do_syslog(int type, char __user *bug, int count);
508 static void begin_syslog_dump(struct fiq_debugger_state *state)
510 do_syslog(5 /* clear */, NULL, 0);
513 static void end_syslog_dump(struct fiq_debugger_state *state)
520 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
524 debug_printf(state, "%s", buf);
530 static void do_sysrq(struct fiq_debugger_state *state, char rq)
532 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
533 debug_printf(state, "sysrq-g blocked\n");
536 begin_syslog_dump(state);
538 end_syslog_dump(state);
542 static void do_kgdb(struct fiq_debugger_state *state)
544 if (!fiq_kgdb_enable) {
545 debug_printf(state, "kgdb through fiq debugger not enabled\n");
549 debug_printf(state, "enabling console and triggering kgdb\n");
550 state->console_enable = true;
555 /* This function CANNOT be called in FIQ context */
556 static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
558 if (!strcmp(cmd, "ps"))
560 if (!strcmp(cmd, "sysrq"))
561 do_sysrq(state, 'h');
562 if (!strncmp(cmd, "sysrq ", 6))
563 do_sysrq(state, cmd[6]);
565 if (!strcmp(cmd, "kgdb"))
570 static void debug_help(struct fiq_debugger_state *state)
572 debug_printf(state, "FIQ Debugger commands:\n"
574 " regs Register dump\n"
575 " allregs Extended Register dump\n"
578 " irqs Interupt status\n"
580 " version Kernel version\n");
581 debug_printf(state, " sleep Allow sleep while in FIQ\n"
582 " nosleep Disable sleep while in FIQ\n"
583 " console Switch terminal to console\n"
585 " cpu <number> Switch to CPU<number>\n");
586 debug_printf(state, " ps Process list\n"
587 " sysrq sysrq options\n"
588 " sysrq <param> Execute sysrq with <param>\n");
590 debug_printf(state, " kgdb Enter kernel debugger\n");
594 static void take_affinity(void *info)
596 struct fiq_debugger_state *state = info;
597 struct cpumask cpumask;
599 cpumask_clear(&cpumask);
600 cpumask_set_cpu(get_cpu(), &cpumask);
602 irq_set_affinity(state->uart_irq, &cpumask);
605 static void switch_cpu(struct fiq_debugger_state *state, int cpu)
607 if (!debug_have_fiq(state))
608 smp_call_function_single(cpu, take_affinity, state, false);
609 state->current_cpu = cpu;
612 static bool debug_fiq_exec(struct fiq_debugger_state *state,
613 const char *cmd, unsigned *regs, void *svc_sp)
615 bool signal_helper = false;
617 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
619 } else if (!strcmp(cmd, "pc")) {
620 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
621 regs[15], regs[16], mode_name(regs[16]));
622 } else if (!strcmp(cmd, "regs")) {
623 dump_regs(state, regs);
624 } else if (!strcmp(cmd, "allregs")) {
625 dump_allregs(state, regs);
626 } else if (!strcmp(cmd, "bt")) {
627 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
628 } else if (!strncmp(cmd, "reboot", 6)) {
634 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
635 kernel_restart(tmp_cmd);
637 kernel_restart(NULL);
639 } else if (!strcmp(cmd, "irqs")) {
641 } else if (!strcmp(cmd, "kmsg")) {
642 dump_kernel_log(state);
643 } else if (!strcmp(cmd, "version")) {
644 debug_printf(state, "%s\n", linux_banner);
645 } else if (!strcmp(cmd, "sleep")) {
646 state->no_sleep = false;
647 debug_printf(state, "enabling sleep\n");
648 } else if (!strcmp(cmd, "nosleep")) {
649 state->no_sleep = true;
650 debug_printf(state, "disabling sleep\n");
651 } else if (!strcmp(cmd, "console")) {
652 state->console_enable = true;
653 debug_printf(state, "console mode\n");
654 } else if (!strcmp(cmd, "cpu")) {
655 debug_printf(state, "cpu %d\n", state->current_cpu);
656 } else if (!strncmp(cmd, "cpu ", 4)) {
657 unsigned long cpu = 0;
658 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
659 switch_cpu(state, cpu);
661 debug_printf(state, "invalid cpu\n");
662 debug_printf(state, "cpu %d\n", state->current_cpu);
664 if (state->debug_busy) {
666 "command processor busy. trying to abort.\n");
667 state->debug_abort = -1;
669 strcpy(state->debug_cmd, cmd);
670 state->debug_busy = 1;
675 if (!state->console_enable)
678 return signal_helper;
681 static void sleep_timer_expired(unsigned long data)
683 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
686 spin_lock_irqsave(&state->sleep_timer_lock, flags);
687 if (state->uart_enabled && !state->no_sleep) {
688 if (state->debug_enable && !state->console_enable) {
689 state->debug_enable = false;
690 debug_printf_nfiq(state, "suspending fiq debugger\n");
692 state->ignore_next_wakeup_irq = true;
693 debug_uart_disable(state);
694 state->uart_enabled = false;
695 enable_wakeup_irq(state);
697 wake_unlock(&state->debugger_wake_lock);
698 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
701 static void handle_wakeup(struct fiq_debugger_state *state)
705 spin_lock_irqsave(&state->sleep_timer_lock, flags);
706 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
707 state->ignore_next_wakeup_irq = false;
708 } else if (!state->uart_enabled) {
709 wake_lock(&state->debugger_wake_lock);
710 debug_uart_enable(state);
711 state->uart_enabled = true;
712 disable_wakeup_irq(state);
713 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
715 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
718 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
720 struct fiq_debugger_state *state = dev;
722 if (!state->no_sleep)
723 debug_puts(state, "WAKEUP\n");
724 handle_wakeup(state);
730 static void debug_handle_irq_context(struct fiq_debugger_state *state)
732 if (!state->no_sleep) {
735 spin_lock_irqsave(&state->sleep_timer_lock, flags);
736 wake_lock(&state->debugger_wake_lock);
737 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
738 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
740 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
743 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
744 for (i = 0; i < count; i++) {
745 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
746 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
747 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
748 pr_warn("fiq tty failed to consume byte\n");
750 tty_flip_buffer_push(state->tty);
753 if (state->debug_busy) {
754 debug_irq_exec(state, state->debug_cmd);
755 if (!state->console_enable)
757 state->debug_busy = 0;
761 static int debug_getc(struct fiq_debugger_state *state)
763 return state->pdata->uart_getc(state->pdev);
766 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
767 int this_cpu, void *regs, void *svc_sp)
772 bool signal_helper = false;
774 if (this_cpu != state->current_cpu) {
778 if (atomic_inc_return(&state->unhandled_fiq_count) !=
779 MAX_UNHANDLED_FIQ_COUNT)
782 debug_printf(state, "fiq_debugger: cpu %d not responding, "
783 "reverting to cpu %d\n", state->current_cpu,
786 atomic_set(&state->unhandled_fiq_count, 0);
787 switch_cpu(state, this_cpu);
791 state->in_fiq = true;
793 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
795 if (!state->debug_enable) {
796 if ((c == 13) || (c == 10)) {
797 state->debug_enable = true;
798 state->debug_count = 0;
801 } else if (c == FIQ_DEBUGGER_BREAK) {
802 state->console_enable = false;
803 debug_puts(state, "fiq debugger mode\n");
804 state->debug_count = 0;
806 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
807 } else if (state->console_enable && state->tty_rbuf) {
808 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
809 signal_helper = true;
811 } else if ((c >= ' ') && (c < 127)) {
812 if (state->debug_count < (DEBUG_MAX - 1)) {
813 state->debug_buf[state->debug_count++] = c;
814 debug_putc(state, c);
816 } else if ((c == 8) || (c == 127)) {
817 if (state->debug_count > 0) {
818 state->debug_count--;
819 debug_putc(state, 8);
820 debug_putc(state, ' ');
821 debug_putc(state, 8);
823 } else if ((c == 13) || (c == 10)) {
824 if (c == '\r' || (c == '\n' && last_c != '\r')) {
825 debug_putc(state, '\r');
826 debug_putc(state, '\n');
828 if (state->debug_count) {
829 state->debug_buf[state->debug_count] = 0;
830 state->debug_count = 0;
832 debug_fiq_exec(state, state->debug_buf,
840 debug_uart_flush(state);
841 if (state->pdata->fiq_ack)
842 state->pdata->fiq_ack(state->pdev, state->fiq);
844 /* poke sleep timer if necessary */
845 if (state->debug_enable && !state->no_sleep)
846 signal_helper = true;
848 atomic_set(&state->unhandled_fiq_count, 0);
849 state->in_fiq = false;
851 return signal_helper;
854 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
856 struct fiq_debugger_state *state =
857 container_of(h, struct fiq_debugger_state, handler);
858 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
861 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
863 debug_force_irq(state);
867 * When not using FIQs, we only use this single interrupt as an entry point.
868 * This just effectively takes over the UART interrupt and does all the work
871 static irqreturn_t debug_uart_irq(int irq, void *dev)
873 struct fiq_debugger_state *state = dev;
876 handle_wakeup(state);
878 /* handle the debugger irq in regular context */
879 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
881 current_thread_info());
883 debug_handle_irq_context(state);
889 * If FIQs are used, not everything can happen in fiq context.
890 * FIQ handler does what it can and then signals this interrupt to finish the
891 * job in irq context.
893 static irqreturn_t debug_signal_irq(int irq, void *dev)
895 struct fiq_debugger_state *state = dev;
897 if (state->pdata->force_irq_ack)
898 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
900 debug_handle_irq_context(state);
905 static void debug_resume(struct fiq_glue_handler *h)
907 struct fiq_debugger_state *state =
908 container_of(h, struct fiq_debugger_state, handler);
909 if (state->pdata->uart_resume)
910 state->pdata->uart_resume(state->pdev);
913 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
914 struct tty_driver *debug_console_device(struct console *co, int *index)
916 struct fiq_debugger_state *state;
917 state = container_of(co, struct fiq_debugger_state, console);
919 return state->tty_driver;
922 static void debug_console_write(struct console *co,
923 const char *s, unsigned int count)
925 struct fiq_debugger_state *state;
927 state = container_of(co, struct fiq_debugger_state, console);
929 if (!state->console_enable && !state->syslog_dumping)
932 debug_uart_enable(state);
935 debug_putc(state, '\r');
936 debug_putc(state, *s++);
938 debug_uart_flush(state);
939 debug_uart_disable(state);
942 static struct console fiq_debugger_console = {
944 .device = debug_console_device,
945 .write = debug_console_write,
946 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
949 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
951 struct fiq_debugger_state *state = tty->driver->driver_state;
952 if (state->tty_open_count++)
955 tty->driver_data = state;
960 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
962 struct fiq_debugger_state *state = tty->driver_data;
963 if (--state->tty_open_count)
968 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
971 struct fiq_debugger_state *state = tty->driver_data;
973 if (!state->console_enable)
976 debug_uart_enable(state);
977 for (i = 0; i < count; i++)
978 debug_putc(state, *buf++);
979 debug_uart_disable(state);
984 int fiq_tty_write_room(struct tty_struct *tty)
989 #ifdef CONFIG_CONSOLE_POLL
990 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
995 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
997 struct fiq_debugger_state *state = driver->ttys[line]->driver_data;
998 int c = NO_POLL_CHAR;
1000 debug_uart_enable(state);
1001 if (debug_have_fiq(state)) {
1002 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1004 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1005 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1008 c = debug_getc(state);
1009 if (c == FIQ_DEBUGGER_NO_CHAR)
1012 debug_uart_disable(state);
1017 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1019 struct fiq_debugger_state *state = driver->ttys[line]->driver_data;
1020 debug_uart_enable(state);
1021 debug_putc(state, ch);
1022 debug_uart_disable(state);
1026 static const struct tty_operations fiq_tty_driver_ops = {
1027 .write = fiq_tty_write,
1028 .write_room = fiq_tty_write_room,
1029 .open = fiq_tty_open,
1030 .close = fiq_tty_close,
1031 #ifdef CONFIG_CONSOLE_POLL
1032 .poll_init = fiq_tty_poll_init,
1033 .poll_get_char = fiq_tty_poll_get_char,
1034 .poll_put_char = fiq_tty_poll_put_char,
1038 static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
1042 state->tty_driver = alloc_tty_driver(1);
1043 if (!state->tty_driver) {
1044 pr_err("Failed to allocate fiq debugger tty\n");
1048 state->tty_driver->owner = THIS_MODULE;
1049 state->tty_driver->driver_name = "fiq-debugger";
1050 state->tty_driver->name = "ttyFIQ";
1051 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1052 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
1053 state->tty_driver->init_termios = tty_std_termios;
1054 state->tty_driver->init_termios.c_cflag =
1055 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1056 state->tty_driver->init_termios.c_ispeed =
1057 state->tty_driver->init_termios.c_ospeed = 115200;
1058 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
1059 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
1060 state->tty_driver->driver_state = state;
1062 ret = tty_register_driver(state->tty_driver);
1064 pr_err("Failed to register fiq tty: %d\n", ret);
1068 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1069 if (!state->tty_rbuf) {
1070 pr_err("Failed to allocate fiq debugger ringbuf\n");
1075 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
1079 fiq_debugger_ringbuf_free(state->tty_rbuf);
1080 state->tty_rbuf = NULL;
1081 put_tty_driver(state->tty_driver);
1086 static int fiq_debugger_dev_suspend(struct device *dev)
1088 struct platform_device *pdev = to_platform_device(dev);
1089 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1091 if (state->pdata->uart_dev_suspend)
1092 return state->pdata->uart_dev_suspend(pdev);
1096 static int fiq_debugger_dev_resume(struct device *dev)
1098 struct platform_device *pdev = to_platform_device(dev);
1099 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1101 if (state->pdata->uart_dev_resume)
1102 return state->pdata->uart_dev_resume(pdev);
1106 static int fiq_debugger_probe(struct platform_device *pdev)
1109 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1110 struct fiq_debugger_state *state;
1114 if (!pdata->uart_getc || !pdata->uart_putc)
1116 if ((pdata->uart_enable && !pdata->uart_disable) ||
1117 (!pdata->uart_enable && pdata->uart_disable))
1120 fiq = platform_get_irq_byname(pdev, "fiq");
1121 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1123 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1125 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1127 if (fiq >= 0 && !pdata->fiq_enable)
1130 state = kzalloc(sizeof(*state), GFP_KERNEL);
1131 setup_timer(&state->sleep_timer, sleep_timer_expired,
1132 (unsigned long)state);
1133 state->pdata = pdata;
1135 state->no_sleep = initial_no_sleep;
1136 state->debug_enable = initial_debug_enable;
1137 state->console_enable = initial_console_enable;
1140 state->uart_irq = uart_irq;
1141 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1142 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1144 platform_set_drvdata(pdev, state);
1146 spin_lock_init(&state->sleep_timer_lock);
1148 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1149 state->no_sleep = true;
1150 state->ignore_next_wakeup_irq = !state->no_sleep;
1152 wake_lock_init(&state->debugger_wake_lock,
1153 WAKE_LOCK_SUSPEND, "serial-debug");
1155 state->clk = clk_get(&pdev->dev, NULL);
1156 if (IS_ERR(state->clk))
1159 /* do not call pdata->uart_enable here since uart_init may still
1160 * need to do some initialization before uart_enable can work.
1161 * So, only try to manage the clock during init.
1164 clk_enable(state->clk);
1166 if (pdata->uart_init) {
1167 ret = pdata->uart_init(pdev);
1172 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1173 state->no_sleep ? "" : "twice ");
1175 if (debug_have_fiq(state)) {
1176 state->handler.fiq = debug_fiq;
1177 state->handler.resume = debug_resume;
1178 ret = fiq_glue_register_handler(&state->handler);
1180 pr_err("%s: could not install fiq handler\n", __func__);
1181 goto err_register_fiq;
1184 pdata->fiq_enable(pdev, state->fiq, 1);
1186 ret = request_irq(state->uart_irq, debug_uart_irq,
1187 IRQF_NO_SUSPEND, "debug", state);
1189 pr_err("%s: could not install irq handler\n", __func__);
1190 goto err_register_irq;
1193 /* for irq-only mode, we want this irq to wake us up, if it
1196 enable_irq_wake(state->uart_irq);
1200 clk_disable(state->clk);
1202 if (state->signal_irq >= 0) {
1203 ret = request_irq(state->signal_irq, debug_signal_irq,
1204 IRQF_TRIGGER_RISING, "debug-signal", state);
1206 pr_err("serial_debugger: could not install signal_irq");
1209 if (state->wakeup_irq >= 0) {
1210 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1211 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1212 "debug-wakeup", state);
1214 pr_err("serial_debugger: "
1215 "could not install wakeup irq\n");
1216 state->wakeup_irq = -1;
1218 ret = enable_irq_wake(state->wakeup_irq);
1220 pr_err("serial_debugger: "
1221 "could not enable wakeup\n");
1222 state->wakeup_irq_no_set_wake = true;
1226 if (state->no_sleep)
1227 handle_wakeup(state);
1229 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1230 state->console = fiq_debugger_console;
1231 register_console(&state->console);
1232 fiq_debugger_tty_init(state);
1238 if (pdata->uart_free)
1239 pdata->uart_free(pdev);
1242 clk_disable(state->clk);
1244 clk_put(state->clk);
1245 wake_lock_destroy(&state->debugger_wake_lock);
1246 platform_set_drvdata(pdev, NULL);
1251 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1252 .suspend = fiq_debugger_dev_suspend,
1253 .resume = fiq_debugger_dev_resume,
1256 static struct platform_driver fiq_debugger_driver = {
1257 .probe = fiq_debugger_probe,
1259 .name = "fiq_debugger",
1260 .pm = &fiq_debugger_dev_pm_ops,
1264 static int __init fiq_debugger_init(void)
1266 return platform_driver_register(&fiq_debugger_driver);
1269 postcore_initcall(fiq_debugger_init);