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 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
110 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
111 module_param_named(console_enable, initial_console_enable, bool, 0644);
113 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
114 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
115 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
117 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
119 if (state->wakeup_irq < 0)
121 enable_irq(state->wakeup_irq);
122 if (!state->wakeup_irq_no_set_wake)
123 enable_irq_wake(state->wakeup_irq);
125 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
127 if (state->wakeup_irq < 0)
129 disable_irq_nosync(state->wakeup_irq);
130 if (!state->wakeup_irq_no_set_wake)
131 disable_irq_wake(state->wakeup_irq);
135 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
137 return (state->fiq >= 0);
140 static void debug_force_irq(struct fiq_debugger_state *state)
142 unsigned int irq = state->signal_irq;
144 if (WARN_ON(!debug_have_fiq(state)))
146 if (state->pdata->force_irq) {
147 state->pdata->force_irq(state->pdev, irq);
149 struct irq_chip *chip = irq_get_chip(irq);
150 if (chip && chip->irq_retrigger)
151 chip->irq_retrigger(irq_get_irq_data(irq));
155 static void debug_uart_enable(struct fiq_debugger_state *state)
158 clk_enable(state->clk);
159 if (state->pdata->uart_enable)
160 state->pdata->uart_enable(state->pdev);
163 static void debug_uart_disable(struct fiq_debugger_state *state)
165 if (state->pdata->uart_disable)
166 state->pdata->uart_disable(state->pdev);
168 clk_disable(state->clk);
171 static void debug_uart_flush(struct fiq_debugger_state *state)
173 if (state->pdata->uart_flush)
174 state->pdata->uart_flush(state->pdev);
177 static void debug_puts(struct fiq_debugger_state *state, char *s)
182 state->pdata->uart_putc(state->pdev, '\r');
183 state->pdata->uart_putc(state->pdev, c);
187 static void debug_prompt(struct fiq_debugger_state *state)
189 debug_puts(state, "debug> ");
192 int log_buf_copy(char *dest, int idx, int len);
193 static void dump_kernel_log(struct fiq_debugger_state *state)
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...
204 saved_oip = oops_in_progress;
205 oops_in_progress = 1;
207 ret = log_buf_copy(buf, idx, 1023);
211 debug_puts(state, buf);
214 oops_in_progress = saved_oip;
217 static char *mode_name(unsigned cpsr)
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 "???";
231 static int debug_printf(void *cookie, const char *fmt, ...)
233 struct fiq_debugger_state *state = cookie;
238 vsnprintf(buf, sizeof(buf), fmt, ap);
241 debug_puts(state, buf);
242 return state->debug_abort;
245 /* Safe outside fiq context */
246 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
248 struct fiq_debugger_state *state = cookie;
251 unsigned long irq_flags;
254 vsnprintf(buf, 128, fmt, ap);
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;
264 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
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],
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]);
284 unsigned long sp_svc;
285 unsigned long lr_svc;
286 unsigned long spsr_svc;
288 unsigned long sp_abt;
289 unsigned long lr_abt;
290 unsigned long spsr_abt;
292 unsigned long sp_und;
293 unsigned long lr_und;
294 unsigned long spsr_und;
296 unsigned long sp_irq;
297 unsigned long lr_irq;
298 unsigned long spsr_irq;
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;
310 void __naked get_mode_regs(struct mode_regs *regs)
314 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
315 "stmia r0!, {r13 - r14}\n"
317 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
318 "stmia r0!, {r2, r13 - r14}\n"
320 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
321 "stmia r0!, {r2, r13 - r14}\n"
323 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
324 "stmia r0!, {r2, r13 - r14}\n"
326 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
327 "stmia r0!, {r2, r8 - r14}\n"
335 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
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 "
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);
356 static void dump_irqs(struct fiq_debugger_state *state)
360 debug_printf(state, "irqnr total since-last status name\n");
361 for (n = 0; n < NR_IRQS; n++) {
362 struct irqaction *act = irq_desc[n].action;
363 if (!act && !kstat_irqs(n))
365 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
367 kstat_irqs(n) - state->last_irqs[n],
368 irq_desc[n].status_use_accessors,
369 (act && act->name) ? act->name : "???");
370 state->last_irqs[n] = kstat_irqs(n);
374 struct stacktrace_state {
375 struct fiq_debugger_state *state;
379 static int report_trace(struct stackframe *frame, void *d)
381 struct stacktrace_state *sts = d;
384 debug_printf(sts->state,
385 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
386 frame->pc, frame->pc, frame->lr, frame->lr,
387 frame->sp, frame->fp);
391 debug_printf(sts->state, " ...\n");
393 return sts->depth == 0;
397 struct frame_tail *fp;
400 } __attribute__((packed));
402 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
403 struct frame_tail *tail)
405 struct frame_tail buftail[2];
407 /* Also check accessibility of one struct frame_tail beyond */
408 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
409 debug_printf(state, " invalid frame pointer %p\n", tail);
412 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
414 " failed to copy frame pointer %p\n", tail);
418 debug_printf(state, " %p\n", buftail[0].lr);
420 /* frame pointers should strictly progress back up the stack
421 * (towards higher addresses) */
422 if (tail >= buftail[0].fp)
425 return buftail[0].fp-1;
428 void dump_stacktrace(struct fiq_debugger_state *state,
429 struct pt_regs * const regs, unsigned int depth, void *ssp)
431 struct frame_tail *tail;
432 struct thread_info *real_thread_info = THREAD_INFO(ssp);
433 struct stacktrace_state sts;
437 *current_thread_info() = *real_thread_info;
440 debug_printf(state, "current NULL\n");
442 debug_printf(state, "pid: %d comm: %s\n",
443 current->pid, current->comm);
444 dump_regs(state, (unsigned *)regs);
446 if (!user_mode(regs)) {
447 struct stackframe frame;
448 frame.fp = regs->ARM_fp;
449 frame.sp = regs->ARM_sp;
450 frame.lr = regs->ARM_lr;
451 frame.pc = regs->ARM_pc;
453 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
454 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
455 regs->ARM_sp, regs->ARM_fp);
456 walk_stackframe(&frame, report_trace, &sts);
460 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
461 while (depth-- && tail && !((unsigned long) tail & 3))
462 tail = user_backtrace(state, tail);
465 static void do_ps(struct fiq_debugger_state *state)
467 struct task_struct *g;
468 struct task_struct *p;
470 static const char stat_nam[] = "RSDTtZX";
472 debug_printf(state, "pid ppid prio task pc\n");
473 read_lock(&tasklist_lock);
474 do_each_thread(g, p) {
475 task_state = p->state ? __ffs(p->state) + 1 : 0;
477 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
478 debug_printf(state, "%-13.13s %c", p->comm,
479 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
480 if (task_state == TASK_RUNNING)
481 debug_printf(state, " running\n");
483 debug_printf(state, " %08lx\n", thread_saved_pc(p));
484 } while_each_thread(g, p);
485 read_unlock(&tasklist_lock);
488 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
489 static void begin_syslog_dump(struct fiq_debugger_state *state)
491 state->syslog_dumping = true;
494 static void end_syslog_dump(struct fiq_debugger_state *state)
496 state->syslog_dumping = false;
499 extern int do_syslog(int type, char __user *bug, int count);
500 static void begin_syslog_dump(struct fiq_debugger_state *state)
502 do_syslog(5 /* clear */, NULL, 0);
505 static void end_syslog_dump(struct fiq_debugger_state *state)
512 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
516 debug_printf(state, "%s", buf);
522 static void do_sysrq(struct fiq_debugger_state *state, char rq)
524 begin_syslog_dump(state);
526 end_syslog_dump(state);
529 /* This function CANNOT be called in FIQ context */
530 static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
532 if (!strcmp(cmd, "ps"))
534 if (!strcmp(cmd, "sysrq"))
535 do_sysrq(state, 'h');
536 if (!strncmp(cmd, "sysrq ", 6))
537 do_sysrq(state, cmd[6]);
540 static void debug_help(struct fiq_debugger_state *state)
542 debug_printf(state, "FIQ Debugger commands:\n"
544 " regs Register dump\n"
545 " allregs Extended Register dump\n"
548 " irqs Interupt status\n"
550 " version Kernel version\n");
551 debug_printf(state, " sleep Allow sleep while in FIQ\n"
552 " nosleep Disable sleep while in FIQ\n"
553 " console Switch terminal to console\n"
555 " cpu <number> Switch to CPU<number>\n");
556 debug_printf(state, " ps Process list\n"
557 " sysrq sysrq options\n"
558 " sysrq <param> Execute sysrq with <param>\n");
561 static void take_affinity(void *info)
563 struct fiq_debugger_state *state = info;
564 struct cpumask cpumask;
566 cpumask_clear(&cpumask);
567 cpumask_set_cpu(get_cpu(), &cpumask);
569 irq_set_affinity(state->uart_irq, &cpumask);
572 static void switch_cpu(struct fiq_debugger_state *state, int cpu)
574 if (!debug_have_fiq(state))
575 smp_call_function_single(cpu, take_affinity, state, false);
576 state->current_cpu = cpu;
579 static bool debug_fiq_exec(struct fiq_debugger_state *state,
580 const char *cmd, unsigned *regs, void *svc_sp)
582 bool signal_helper = false;
584 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
586 } else if (!strcmp(cmd, "pc")) {
587 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
588 regs[15], regs[16], mode_name(regs[16]));
589 } else if (!strcmp(cmd, "regs")) {
590 dump_regs(state, regs);
591 } else if (!strcmp(cmd, "allregs")) {
592 dump_allregs(state, regs);
593 } else if (!strcmp(cmd, "bt")) {
594 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
595 } else if (!strncmp(cmd, "reboot", 6)) {
601 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
602 kernel_restart(tmp_cmd);
604 kernel_restart(NULL);
606 } else if (!strcmp(cmd, "irqs")) {
608 } else if (!strcmp(cmd, "kmsg")) {
609 dump_kernel_log(state);
610 } else if (!strcmp(cmd, "version")) {
611 debug_printf(state, "%s\n", linux_banner);
612 } else if (!strcmp(cmd, "sleep")) {
613 state->no_sleep = false;
614 debug_printf(state, "enabling sleep\n");
615 } else if (!strcmp(cmd, "nosleep")) {
616 state->no_sleep = true;
617 debug_printf(state, "disabling sleep\n");
618 } else if (!strcmp(cmd, "console")) {
619 state->console_enable = true;
620 debug_printf(state, "console mode\n");
621 } else if (!strcmp(cmd, "cpu")) {
622 debug_printf(state, "cpu %d\n", state->current_cpu);
623 } else if (!strncmp(cmd, "cpu ", 4)) {
624 unsigned long cpu = 0;
625 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
626 switch_cpu(state, cpu);
628 debug_printf(state, "invalid cpu\n");
629 debug_printf(state, "cpu %d\n", state->current_cpu);
631 if (state->debug_busy) {
633 "command processor busy. trying to abort.\n");
634 state->debug_abort = -1;
636 strcpy(state->debug_cmd, cmd);
637 state->debug_busy = 1;
642 if (!state->console_enable)
645 return signal_helper;
648 static void sleep_timer_expired(unsigned long data)
650 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
653 spin_lock_irqsave(&state->sleep_timer_lock, flags);
654 if (state->uart_enabled && !state->no_sleep) {
655 if (state->debug_enable && !state->console_enable) {
656 state->debug_enable = false;
657 debug_printf_nfiq(state, "suspending fiq debugger\n");
659 state->ignore_next_wakeup_irq = true;
660 debug_uart_disable(state);
661 state->uart_enabled = false;
662 enable_wakeup_irq(state);
664 wake_unlock(&state->debugger_wake_lock);
665 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
668 static void handle_wakeup(struct fiq_debugger_state *state)
672 spin_lock_irqsave(&state->sleep_timer_lock, flags);
673 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
674 state->ignore_next_wakeup_irq = false;
675 } else if (!state->uart_enabled) {
676 wake_lock(&state->debugger_wake_lock);
677 debug_uart_enable(state);
678 state->uart_enabled = true;
679 disable_wakeup_irq(state);
680 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
682 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
685 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
687 struct fiq_debugger_state *state = dev;
689 if (!state->no_sleep)
690 debug_puts(state, "WAKEUP\n");
691 handle_wakeup(state);
697 static void debug_handle_irq_context(struct fiq_debugger_state *state)
699 if (!state->no_sleep) {
702 spin_lock_irqsave(&state->sleep_timer_lock, flags);
703 wake_lock(&state->debugger_wake_lock);
704 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
705 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
707 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
710 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
711 for (i = 0; i < count; i++) {
712 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
713 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
714 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
715 pr_warn("fiq tty failed to consume byte\n");
717 tty_flip_buffer_push(state->tty);
720 if (state->debug_busy) {
721 debug_irq_exec(state, state->debug_cmd);
723 state->debug_busy = 0;
727 static int debug_getc(struct fiq_debugger_state *state)
729 return state->pdata->uart_getc(state->pdev);
732 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
733 int this_cpu, void *regs, void *svc_sp)
738 bool signal_helper = false;
740 if (this_cpu != state->current_cpu) {
744 if (atomic_inc_return(&state->unhandled_fiq_count) !=
745 MAX_UNHANDLED_FIQ_COUNT)
748 debug_printf(state, "fiq_debugger: cpu %d not responding, "
749 "reverting to cpu %d\n", state->current_cpu,
752 atomic_set(&state->unhandled_fiq_count, 0);
753 switch_cpu(state, this_cpu);
757 state->in_fiq = true;
759 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
761 if (!state->debug_enable) {
762 if ((c == 13) || (c == 10)) {
763 state->debug_enable = true;
764 state->debug_count = 0;
767 } else if (c == FIQ_DEBUGGER_BREAK) {
768 state->console_enable = false;
769 debug_puts(state, "fiq debugger mode\n");
770 state->debug_count = 0;
772 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
773 } else if (state->console_enable && state->tty_rbuf) {
774 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
775 signal_helper = true;
777 } else if ((c >= ' ') && (c < 127)) {
778 if (state->debug_count < (DEBUG_MAX - 1)) {
779 state->debug_buf[state->debug_count++] = c;
780 state->pdata->uart_putc(state->pdev, c);
782 } else if ((c == 8) || (c == 127)) {
783 if (state->debug_count > 0) {
784 state->debug_count--;
785 state->pdata->uart_putc(state->pdev, 8);
786 state->pdata->uart_putc(state->pdev, ' ');
787 state->pdata->uart_putc(state->pdev, 8);
789 } else if ((c == 13) || (c == 10)) {
790 if (c == '\r' || (c == '\n' && last_c != '\r')) {
791 state->pdata->uart_putc(state->pdev, '\r');
792 state->pdata->uart_putc(state->pdev, '\n');
794 if (state->debug_count) {
795 state->debug_buf[state->debug_count] = 0;
796 state->debug_count = 0;
798 debug_fiq_exec(state, state->debug_buf,
806 debug_uart_flush(state);
807 if (state->pdata->fiq_ack)
808 state->pdata->fiq_ack(state->pdev, state->fiq);
810 /* poke sleep timer if necessary */
811 if (state->debug_enable && !state->no_sleep)
812 signal_helper = true;
814 atomic_set(&state->unhandled_fiq_count, 0);
815 state->in_fiq = false;
817 return signal_helper;
820 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
822 struct fiq_debugger_state *state =
823 container_of(h, struct fiq_debugger_state, handler);
824 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
827 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
829 debug_force_irq(state);
833 * When not using FIQs, we only use this single interrupt as an entry point.
834 * This just effectively takes over the UART interrupt and does all the work
837 static irqreturn_t debug_uart_irq(int irq, void *dev)
839 struct fiq_debugger_state *state = dev;
842 handle_wakeup(state);
844 /* handle the debugger irq in regular context */
845 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
847 current_thread_info());
849 debug_handle_irq_context(state);
855 * If FIQs are used, not everything can happen in fiq context.
856 * FIQ handler does what it can and then signals this interrupt to finish the
857 * job in irq context.
859 static irqreturn_t debug_signal_irq(int irq, void *dev)
861 struct fiq_debugger_state *state = dev;
863 if (state->pdata->force_irq_ack)
864 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
866 debug_handle_irq_context(state);
871 static void debug_resume(struct fiq_glue_handler *h)
873 struct fiq_debugger_state *state =
874 container_of(h, struct fiq_debugger_state, handler);
875 if (state->pdata->uart_resume)
876 state->pdata->uart_resume(state->pdev);
879 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
880 struct tty_driver *debug_console_device(struct console *co, int *index)
882 struct fiq_debugger_state *state;
883 state = container_of(co, struct fiq_debugger_state, console);
885 return state->tty_driver;
888 static void debug_console_write(struct console *co,
889 const char *s, unsigned int count)
891 struct fiq_debugger_state *state;
893 state = container_of(co, struct fiq_debugger_state, console);
895 if (!state->console_enable && !state->syslog_dumping)
898 debug_uart_enable(state);
901 state->pdata->uart_putc(state->pdev, '\r');
902 state->pdata->uart_putc(state->pdev, *s++);
904 debug_uart_flush(state);
905 debug_uart_disable(state);
908 static struct console fiq_debugger_console = {
910 .device = debug_console_device,
911 .write = debug_console_write,
912 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
915 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
917 struct fiq_debugger_state *state = tty->driver->driver_state;
918 if (state->tty_open_count++)
921 tty->driver_data = state;
926 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
928 struct fiq_debugger_state *state = tty->driver_data;
929 if (--state->tty_open_count)
934 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
937 struct fiq_debugger_state *state = tty->driver_data;
939 if (!state->console_enable)
942 debug_uart_enable(state);
943 for (i = 0; i < count; i++)
944 state->pdata->uart_putc(state->pdev, *buf++);
945 debug_uart_disable(state);
950 int fiq_tty_write_room(struct tty_struct *tty)
955 static const struct tty_operations fiq_tty_driver_ops = {
956 .write = fiq_tty_write,
957 .write_room = fiq_tty_write_room,
958 .open = fiq_tty_open,
959 .close = fiq_tty_close,
962 static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
966 state->tty_driver = alloc_tty_driver(1);
967 if (!state->tty_driver) {
968 pr_err("Failed to allocate fiq debugger tty\n");
972 state->tty_driver->owner = THIS_MODULE;
973 state->tty_driver->driver_name = "fiq-debugger";
974 state->tty_driver->name = "ttyFIQ";
975 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
976 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
977 state->tty_driver->init_termios = tty_std_termios;
978 state->tty_driver->init_termios.c_cflag =
979 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
980 state->tty_driver->init_termios.c_ispeed =
981 state->tty_driver->init_termios.c_ospeed = 115200;
982 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
983 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
984 state->tty_driver->driver_state = state;
986 ret = tty_register_driver(state->tty_driver);
988 pr_err("Failed to register fiq tty: %d\n", ret);
992 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
993 if (!state->tty_rbuf) {
994 pr_err("Failed to allocate fiq debugger ringbuf\n");
999 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
1003 fiq_debugger_ringbuf_free(state->tty_rbuf);
1004 state->tty_rbuf = NULL;
1005 put_tty_driver(state->tty_driver);
1010 static int fiq_debugger_dev_suspend(struct device *dev)
1012 struct platform_device *pdev = to_platform_device(dev);
1013 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1015 if (state->pdata->uart_dev_suspend)
1016 return state->pdata->uart_dev_suspend(pdev);
1020 static int fiq_debugger_dev_resume(struct device *dev)
1022 struct platform_device *pdev = to_platform_device(dev);
1023 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1025 if (state->pdata->uart_dev_resume)
1026 return state->pdata->uart_dev_resume(pdev);
1030 static int fiq_debugger_probe(struct platform_device *pdev)
1033 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1034 struct fiq_debugger_state *state;
1038 if (!pdata->uart_getc || !pdata->uart_putc)
1040 if ((pdata->uart_enable && !pdata->uart_disable) ||
1041 (!pdata->uart_enable && pdata->uart_disable))
1044 fiq = platform_get_irq_byname(pdev, "fiq");
1045 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1047 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1049 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1051 if (fiq >= 0 && !pdata->fiq_enable)
1054 state = kzalloc(sizeof(*state), GFP_KERNEL);
1055 setup_timer(&state->sleep_timer, sleep_timer_expired,
1056 (unsigned long)state);
1057 state->pdata = pdata;
1059 state->no_sleep = initial_no_sleep;
1060 state->debug_enable = initial_debug_enable;
1061 state->console_enable = initial_console_enable;
1064 state->uart_irq = uart_irq;
1065 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1066 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1068 platform_set_drvdata(pdev, state);
1070 spin_lock_init(&state->sleep_timer_lock);
1072 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1073 state->no_sleep = true;
1074 state->ignore_next_wakeup_irq = !state->no_sleep;
1076 wake_lock_init(&state->debugger_wake_lock,
1077 WAKE_LOCK_SUSPEND, "serial-debug");
1079 state->clk = clk_get(&pdev->dev, NULL);
1080 if (IS_ERR(state->clk))
1083 /* do not call pdata->uart_enable here since uart_init may still
1084 * need to do some initialization before uart_enable can work.
1085 * So, only try to manage the clock during init.
1088 clk_enable(state->clk);
1090 if (pdata->uart_init) {
1091 ret = pdata->uart_init(pdev);
1096 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1097 state->no_sleep ? "" : "twice ");
1099 if (debug_have_fiq(state)) {
1100 state->handler.fiq = debug_fiq;
1101 state->handler.resume = debug_resume;
1102 ret = fiq_glue_register_handler(&state->handler);
1104 pr_err("%s: could not install fiq handler\n", __func__);
1105 goto err_register_fiq;
1108 pdata->fiq_enable(pdev, state->fiq, 1);
1110 ret = request_irq(state->uart_irq, debug_uart_irq,
1111 IRQF_NO_SUSPEND, "debug", state);
1113 pr_err("%s: could not install irq handler\n", __func__);
1114 goto err_register_irq;
1117 /* for irq-only mode, we want this irq to wake us up, if it
1120 enable_irq_wake(state->uart_irq);
1124 clk_disable(state->clk);
1126 if (state->signal_irq >= 0) {
1127 ret = request_irq(state->signal_irq, debug_signal_irq,
1128 IRQF_TRIGGER_RISING, "debug-signal", state);
1130 pr_err("serial_debugger: could not install signal_irq");
1133 if (state->wakeup_irq >= 0) {
1134 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1135 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1136 "debug-wakeup", state);
1138 pr_err("serial_debugger: "
1139 "could not install wakeup irq\n");
1140 state->wakeup_irq = -1;
1142 ret = enable_irq_wake(state->wakeup_irq);
1144 pr_err("serial_debugger: "
1145 "could not enable wakeup\n");
1146 state->wakeup_irq_no_set_wake = true;
1150 if (state->no_sleep)
1151 handle_wakeup(state);
1153 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1154 state->console = fiq_debugger_console;
1155 register_console(&state->console);
1156 fiq_debugger_tty_init(state);
1162 if (pdata->uart_free)
1163 pdata->uart_free(pdev);
1166 clk_disable(state->clk);
1168 clk_put(state->clk);
1169 wake_lock_destroy(&state->debugger_wake_lock);
1170 platform_set_drvdata(pdev, NULL);
1175 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1176 .suspend = fiq_debugger_dev_suspend,
1177 .resume = fiq_debugger_dev_resume,
1180 static struct platform_driver fiq_debugger_driver = {
1181 .probe = fiq_debugger_probe,
1183 .name = "fiq_debugger",
1184 .pm = &fiq_debugger_dev_pm_ops,
1188 static int __init fiq_debugger_init(void)
1190 return platform_driver_register(&fiq_debugger_driver);
1193 postcore_initcall(fiq_debugger_init);