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/sched.h>
29 #include <linux/slab.h>
30 #include <linux/smp.h>
31 #include <linux/timer.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/wakelock.h>
36 #include <asm/fiq_debugger.h>
37 #include <asm/fiq_glue.h>
38 #include <asm/stacktrace.h>
40 #include <mach/system.h>
42 #include <linux/uaccess.h>
44 #include "fiq_debugger_ringbuf.h"
46 #ifdef CONFIG_RK29_WATCHDOG
47 extern void rk29_wdt_keepalive(void);
48 #define wdt_keepalive() rk29_wdt_keepalive()
50 #define wdt_keepalive() do {} while (0)
54 #define CMD_COUNT 0x0f
55 #define MAX_UNHANDLED_FIQ_COUNT 1000000
57 #define THREAD_INFO(sp) ((struct thread_info *) \
58 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
60 struct fiq_debugger_state {
61 struct fiq_glue_handler handler;
67 bool wakeup_irq_no_set_wake;
69 struct fiq_debugger_pdata *pdata;
70 struct platform_device *pdev;
72 char debug_cmd[DEBUG_MAX];
76 char debug_buf[DEBUG_MAX];
79 char cmd_buf[CMD_COUNT+1][DEBUG_MAX];
85 bool ignore_next_wakeup_irq;
86 struct timer_list sleep_timer;
87 spinlock_t sleep_timer_lock;
89 struct wake_lock debugger_wake_lock;
92 atomic_t unhandled_fiq_count;
95 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
96 struct console console;
97 struct tty_driver *tty_driver;
98 struct tty_struct *tty;
100 struct fiq_debugger_ringbuf *tty_rbuf;
104 unsigned int last_irqs[NR_IRQS];
105 unsigned int last_local_timer_irqs[NR_CPUS];
108 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
109 static bool initial_no_sleep = true;
111 static bool initial_no_sleep;
114 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
115 static bool initial_debug_enable = true;
116 static bool initial_console_enable = true;
118 static bool initial_debug_enable;
119 static bool initial_console_enable;
122 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
123 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
124 module_param_named(console_enable, initial_console_enable, bool, 0644);
126 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
127 static inline void enable_wakeup_irq(struct fiq_debugger_state *state) {}
128 static inline void disable_wakeup_irq(struct fiq_debugger_state *state) {}
130 static inline void enable_wakeup_irq(struct fiq_debugger_state *state)
132 if (state->wakeup_irq < 0)
134 enable_irq(state->wakeup_irq);
135 if (!state->wakeup_irq_no_set_wake)
136 enable_irq_wake(state->wakeup_irq);
138 static inline void disable_wakeup_irq(struct fiq_debugger_state *state)
140 if (state->wakeup_irq < 0)
142 disable_irq_nosync(state->wakeup_irq);
143 if (!state->wakeup_irq_no_set_wake)
144 disable_irq_wake(state->wakeup_irq);
148 static bool inline debug_have_fiq(struct fiq_debugger_state *state)
150 return (state->fiq >= 0);
153 static void debug_force_irq(struct fiq_debugger_state *state)
155 unsigned int irq = state->signal_irq;
157 if (WARN_ON(!debug_have_fiq(state)))
159 if (state->pdata->force_irq) {
160 state->pdata->force_irq(state->pdev, irq);
162 struct irq_chip *chip = irq_get_chip(irq);
163 if (chip && chip->irq_retrigger)
164 chip->irq_retrigger(irq_get_irq_data(irq));
168 static void debug_uart_enable(struct fiq_debugger_state *state)
171 clk_enable(state->clk);
172 if (state->pdata->uart_enable)
173 state->pdata->uart_enable(state->pdev);
176 static void debug_uart_disable(struct fiq_debugger_state *state)
178 if (state->pdata->uart_disable)
179 state->pdata->uart_disable(state->pdev);
181 clk_disable(state->clk);
184 static void debug_uart_flush(struct fiq_debugger_state *state)
186 if (state->pdata->uart_flush)
187 state->pdata->uart_flush(state->pdev);
190 static void debug_puts(struct fiq_debugger_state *state, char *s)
195 state->pdata->uart_putc(state->pdev, '\r');
196 state->pdata->uart_putc(state->pdev, c);
200 static void debug_prompt(struct fiq_debugger_state *state)
202 debug_puts(state, "debug> ");
205 int log_buf_copy(char *dest, int idx, int len);
206 static void dump_kernel_log(struct fiq_debugger_state *state)
213 /* setting oops_in_progress prevents log_buf_copy()
214 * from trying to take a spinlock which will make it
215 * very unhappy in some cases...
217 saved_oip = oops_in_progress;
218 oops_in_progress = 1;
221 ret = log_buf_copy(buf, idx, 1023);
225 debug_puts(state, buf);
228 oops_in_progress = saved_oip;
231 #ifdef CONFIG_RK29_LAST_LOG
232 #include <linux/ctype.h>
233 extern char *last_log_get(unsigned *size);
234 static void dump_last_kernel_log(struct fiq_debugger_state *state)
237 char *s = last_log_get(&size);
239 for (i = 0; i < size; i++) {
244 state->pdata->uart_putc(state->pdev, '\r');
245 state->pdata->uart_putc(state->pdev, c);
246 } else if (isascii(c) && isprint(c)) {
247 state->pdata->uart_putc(state->pdev, c);
253 static char *mode_name(unsigned cpsr)
255 switch (cpsr & MODE_MASK) {
256 case USR_MODE: return "USR";
257 case FIQ_MODE: return "FIQ";
258 case IRQ_MODE: return "IRQ";
259 case SVC_MODE: return "SVC";
260 case ABT_MODE: return "ABT";
261 case UND_MODE: return "UND";
262 case SYSTEM_MODE: return "SYS";
263 default: return "???";
267 static int debug_printf(void *cookie, const char *fmt, ...)
269 struct fiq_debugger_state *state = cookie;
274 vsnprintf(buf, sizeof(buf), fmt, ap);
277 debug_puts(state, buf);
278 return state->debug_abort;
281 /* Safe outside fiq context */
282 static int debug_printf_nfiq(void *cookie, const char *fmt, ...)
284 struct fiq_debugger_state *state = cookie;
287 unsigned long irq_flags;
290 vsnprintf(buf, 128, fmt, ap);
293 local_irq_save(irq_flags);
294 debug_puts(state, buf);
295 debug_uart_flush(state);
296 local_irq_restore(irq_flags);
297 return state->debug_abort;
300 static void dump_regs(struct fiq_debugger_state *state, unsigned *regs)
302 debug_printf(state, " r0 %08x r1 %08x r2 %08x r3 %08x\n",
303 regs[0], regs[1], regs[2], regs[3]);
304 debug_printf(state, " r4 %08x r5 %08x r6 %08x r7 %08x\n",
305 regs[4], regs[5], regs[6], regs[7]);
306 debug_printf(state, " r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n",
307 regs[8], regs[9], regs[10], regs[11],
308 mode_name(regs[16]));
309 if ((regs[16] & MODE_MASK) == USR_MODE)
310 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
311 "cpsr %08x\n", regs[12], regs[13], regs[14],
314 debug_printf(state, " ip %08x sp %08x lr %08x pc %08x "
315 "cpsr %08x spsr %08x\n", regs[12], regs[13],
316 regs[14], regs[15], regs[16], regs[17]);
320 unsigned long sp_svc;
321 unsigned long lr_svc;
322 unsigned long spsr_svc;
324 unsigned long sp_abt;
325 unsigned long lr_abt;
326 unsigned long spsr_abt;
328 unsigned long sp_und;
329 unsigned long lr_und;
330 unsigned long spsr_und;
332 unsigned long sp_irq;
333 unsigned long lr_irq;
334 unsigned long spsr_irq;
336 unsigned long r8_fiq;
337 unsigned long r9_fiq;
338 unsigned long r10_fiq;
339 unsigned long r11_fiq;
340 unsigned long r12_fiq;
341 unsigned long sp_fiq;
342 unsigned long lr_fiq;
343 unsigned long spsr_fiq;
346 void __naked get_mode_regs(struct mode_regs *regs)
350 "msr cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
351 "stmia r0!, {r13 - r14}\n"
353 "msr cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
354 "stmia r0!, {r2, r13 - r14}\n"
356 "msr cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
357 "stmia r0!, {r2, r13 - r14}\n"
359 "msr cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
360 "stmia r0!, {r2, r13 - r14}\n"
362 "msr cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
363 "stmia r0!, {r2, r8 - r14}\n"
371 static void dump_allregs(struct fiq_debugger_state *state, unsigned *regs)
373 struct mode_regs mode_regs;
374 dump_regs(state, regs);
375 get_mode_regs(&mode_regs);
376 debug_printf(state, " svc: sp %08x lr %08x spsr %08x\n",
377 mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
378 debug_printf(state, " abt: sp %08x lr %08x spsr %08x\n",
379 mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
380 debug_printf(state, " und: sp %08x lr %08x spsr %08x\n",
381 mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
382 debug_printf(state, " irq: sp %08x lr %08x spsr %08x\n",
383 mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
384 debug_printf(state, " fiq: r8 %08x r9 %08x r10 %08x r11 %08x "
386 mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
387 mode_regs.r11_fiq, mode_regs.r12_fiq);
388 debug_printf(state, " fiq: sp %08x lr %08x spsr %08x\n",
389 mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
392 static void dump_irqs(struct fiq_debugger_state *state)
397 debug_printf(state, "irqnr total since-last status name\n");
398 for (n = 0; n < NR_IRQS; n++) {
399 struct irqaction *act = irq_desc[n].action;
400 if (!act && !kstat_irqs(n))
402 debug_printf(state, "%5d: %10u %11u %8x %s\n", n,
404 kstat_irqs(n) - state->last_irqs[n],
405 irq_desc[n].status_use_accessors,
406 (act && act->name) ? act->name : "???");
407 state->last_irqs[n] = kstat_irqs(n);
410 #ifdef CONFIG_LOCAL_TIMERS
411 for (cpu = 0; cpu < NR_CPUS; cpu++) {
413 debug_printf(state, "LOC %d: %10u %11u\n", cpu,
414 __IRQ_STAT(cpu, local_timer_irqs),
415 __IRQ_STAT(cpu, local_timer_irqs) -
416 state->last_local_timer_irqs[cpu]);
417 state->last_local_timer_irqs[cpu] =
418 __IRQ_STAT(cpu, local_timer_irqs);
423 struct stacktrace_state {
424 struct fiq_debugger_state *state;
428 static int report_trace(struct stackframe *frame, void *d)
430 struct stacktrace_state *sts = d;
433 debug_printf(sts->state,
434 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
435 frame->pc, frame->pc, frame->lr, frame->lr,
436 frame->sp, frame->fp);
440 debug_printf(sts->state, " ...\n");
442 return sts->depth == 0;
446 struct frame_tail *fp;
449 } __attribute__((packed));
451 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
452 struct frame_tail *tail)
454 struct frame_tail buftail[2];
456 /* Also check accessibility of one struct frame_tail beyond */
457 if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
458 debug_printf(state, " invalid frame pointer %p\n", tail);
461 if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
463 " failed to copy frame pointer %p\n", tail);
467 debug_printf(state, " %p\n", buftail[0].lr);
469 /* frame pointers should strictly progress back up the stack
470 * (towards higher addresses) */
471 if (tail >= buftail[0].fp)
474 return buftail[0].fp-1;
477 void dump_stacktrace(struct fiq_debugger_state *state,
478 struct pt_regs * const regs, unsigned int depth, void *ssp)
480 struct frame_tail *tail;
481 struct thread_info *real_thread_info = THREAD_INFO(ssp);
482 struct stacktrace_state sts;
486 *current_thread_info() = *real_thread_info;
489 debug_printf(state, "current NULL\n");
491 debug_printf(state, "pid: %d comm: %s\n",
492 current->pid, current->comm);
493 dump_regs(state, (unsigned *)regs);
495 if (!user_mode(regs)) {
496 struct stackframe frame;
497 frame.fp = regs->ARM_fp;
498 frame.sp = regs->ARM_sp;
499 frame.lr = regs->ARM_lr;
500 frame.pc = regs->ARM_pc;
502 " pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
503 regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
504 regs->ARM_sp, regs->ARM_fp);
505 walk_stackframe(&frame, report_trace, &sts);
509 tail = ((struct frame_tail *) regs->ARM_fp) - 1;
510 while (depth-- && tail && !((unsigned long) tail & 3))
511 tail = user_backtrace(state, tail);
514 static void do_ps(struct fiq_debugger_state *state)
516 struct task_struct *g;
517 struct task_struct *p;
519 static const char stat_nam[] = "RSDTtZX";
521 debug_printf(state, "pid ppid prio task pc\n");
522 read_lock(&tasklist_lock);
523 do_each_thread(g, p) {
524 task_state = p->state ? __ffs(p->state) + 1 : 0;
526 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
527 debug_printf(state, "%-13.13s %c", p->comm,
528 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
529 if (task_state == TASK_RUNNING)
530 debug_printf(state, " running\n");
532 debug_printf(state, " %08lx\n", thread_saved_pc(p));
533 } while_each_thread(g, p);
534 read_unlock(&tasklist_lock);
537 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
538 static void begin_syslog_dump(struct fiq_debugger_state *state)
540 state->syslog_dumping = true;
543 static void end_syslog_dump(struct fiq_debugger_state *state)
545 state->syslog_dumping = false;
548 extern int do_syslog(int type, char __user *bug, int count);
549 static void begin_syslog_dump(struct fiq_debugger_state *state)
551 do_syslog(5 /* clear */, NULL, 0);
554 static void end_syslog_dump(struct fiq_debugger_state *state)
561 ret = log_buf_copy(buf, idx, sizeof(buf) - 1);
565 debug_printf(state, "%s", buf);
571 static void do_sysrq(struct fiq_debugger_state *state, char rq)
573 begin_syslog_dump(state);
575 end_syslog_dump(state);
578 /* This function CANNOT be called in FIQ context */
579 static void debug_irq_exec(struct fiq_debugger_state *state, char *cmd)
582 if (!strcmp(cmd, "ps"))
584 else if (!strcmp(cmd, "sysrq"))
585 do_sysrq(state, 'h');
586 else if (!strncmp(cmd, "sysrq ", 6))
587 do_sysrq(state, cmd[6]);
590 memset(state->debug_buf, 0, DEBUG_MAX);
593 if (invalid_cmd == 0) {
594 state->current_pointer = (state->current_pointer-1) & CMD_COUNT;
595 if (strcmp(state->cmd_buf[state->current_pointer], state->debug_buf)) {
596 state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
597 memset(state->cmd_buf[state->current_pointer], 0, DEBUG_MAX);
598 strcpy(state->cmd_buf[state->current_pointer], state->debug_buf);
600 memset(state->debug_buf, 0, DEBUG_MAX);
601 state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
602 state->back_pointer = state->current_pointer;
606 static char cmd_buf[][16] = {
614 #ifdef CONFIG_RK29_LAST_LOG
626 static void debug_help(struct fiq_debugger_state *state)
628 debug_printf(state, "FIQ Debugger commands:\n"
630 " regs Register dump\n"
631 " allregs Extended Register dump\n"
634 " irqs Interupt status\n"
636 " version Kernel version\n");
637 #ifdef CONFIG_RK29_LAST_LOG
638 debug_printf(state, " last_kmsg Last kernel log\n");
640 debug_printf(state, " sleep Allow sleep while in FIQ\n"
641 " nosleep Disable sleep while in FIQ\n"
642 " console Switch terminal to console\n"
644 " cpu <number> Switch to CPU<number>\n");
645 debug_printf(state, " ps Process list\n"
646 " sysrq sysrq options\n"
647 " sysrq <param> Execute sysrq with <param>\n");
650 static void take_affinity(void *info)
652 struct fiq_debugger_state *state = info;
653 struct cpumask cpumask;
655 cpumask_clear(&cpumask);
656 cpumask_set_cpu(get_cpu(), &cpumask);
658 irq_set_affinity(state->uart_irq, &cpumask);
661 static void switch_cpu(struct fiq_debugger_state *state, int cpu)
663 if (!debug_have_fiq(state))
664 smp_call_function_single(cpu, take_affinity, state, false);
665 #ifdef CONFIG_PLAT_RK
667 struct cpumask cpumask;
669 if (!cpu_online(cpu)) {
670 debug_printf(state, "cpu %d offline\n", cpu);
674 cpumask_clear(&cpumask);
675 cpumask_set_cpu(cpu, &cpumask);
677 irq_set_affinity(state->fiq, &cpumask);
678 irq_set_affinity(state->uart_irq, &cpumask);
681 state->current_cpu = cpu;
684 static bool debug_fiq_exec(struct fiq_debugger_state *state,
685 const char *cmd, unsigned *regs, void *svc_sp)
687 bool signal_helper = false;
689 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
691 } else if (!strcmp(cmd, "pc")) {
692 debug_printf(state, " pc %08x cpsr %08x mode %s\n",
693 regs[15], regs[16], mode_name(regs[16]));
694 } else if (!strcmp(cmd, "regs")) {
695 dump_regs(state, regs);
696 } else if (!strcmp(cmd, "allregs")) {
697 dump_allregs(state, regs);
698 } else if (!strcmp(cmd, "bt")) {
699 dump_stacktrace(state, (struct pt_regs *)regs, 100, svc_sp);
700 } else if (!strcmp(cmd, "reboot")) {
702 } else if (!strncmp(cmd, "reboot ", 7)) {
703 arch_reset(0, &cmd[7]);
704 } else if (!strcmp(cmd, "irqs")) {
706 } else if (!strcmp(cmd, "kmsg")) {
707 dump_kernel_log(state);
708 #ifdef CONFIG_RK29_LAST_LOG
709 } else if (!strcmp(cmd, "last_kmsg")) {
710 dump_last_kernel_log(state);
712 } else if (!strcmp(cmd, "version")) {
713 debug_printf(state, "%s\n", linux_banner);
714 } else if (!strcmp(cmd, "sleep")) {
715 state->no_sleep = false;
716 debug_printf(state, "enabling sleep\n");
717 } else if (!strcmp(cmd, "nosleep")) {
718 state->no_sleep = true;
719 debug_printf(state, "disabling sleep\n");
720 } else if (!strcmp(cmd, "console")) {
721 state->console_enable = true;
722 debug_printf(state, "console mode\n");
723 } else if (!strcmp(cmd, "cpu")) {
724 debug_printf(state, "cpu %d\n", state->current_cpu);
725 } else if (!strncmp(cmd, "cpu ", 4)) {
726 unsigned long cpu = 0;
727 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
728 switch_cpu(state, cpu);
730 debug_printf(state, "invalid cpu\n");
731 debug_printf(state, "cpu %d\n", state->current_cpu);
733 if (state->debug_busy) {
735 "command processor busy. trying to abort.\n");
736 state->debug_abort = -1;
738 strcpy(state->debug_cmd, cmd);
739 state->debug_busy = 1;
744 if (!state->console_enable)
747 return signal_helper;
750 static void sleep_timer_expired(unsigned long data)
752 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
755 spin_lock_irqsave(&state->sleep_timer_lock, flags);
756 if (state->uart_enabled && !state->no_sleep) {
757 if (state->debug_enable && !state->console_enable) {
758 state->debug_enable = false;
759 debug_printf_nfiq(state, "suspending fiq debugger\n");
761 state->ignore_next_wakeup_irq = true;
762 debug_uart_disable(state);
763 state->uart_enabled = false;
764 enable_wakeup_irq(state);
766 wake_unlock(&state->debugger_wake_lock);
767 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
770 static void handle_wakeup(struct fiq_debugger_state *state)
774 spin_lock_irqsave(&state->sleep_timer_lock, flags);
775 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
776 state->ignore_next_wakeup_irq = false;
777 } else if (!state->uart_enabled) {
778 wake_lock(&state->debugger_wake_lock);
779 debug_uart_enable(state);
780 state->uart_enabled = true;
781 disable_wakeup_irq(state);
782 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
784 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
787 static irqreturn_t wakeup_irq_handler(int irq, void *dev)
789 struct fiq_debugger_state *state = dev;
791 if (!state->no_sleep)
792 debug_puts(state, "WAKEUP\n");
793 handle_wakeup(state);
799 static void debug_handle_irq_context(struct fiq_debugger_state *state)
801 if (!state->no_sleep) {
804 spin_lock_irqsave(&state->sleep_timer_lock, flags);
805 wake_lock(&state->debugger_wake_lock);
806 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
807 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
809 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
812 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
813 for (i = 0; i < count; i++) {
814 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
815 tty_insert_flip_char(state->tty, c, TTY_NORMAL);
816 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
817 pr_warn("fiq tty failed to consume byte\n");
819 tty_flip_buffer_push(state->tty);
822 if (state->debug_busy) {
823 debug_irq_exec(state, state->debug_cmd);
825 state->debug_busy = 0;
829 static int debug_getc(struct fiq_debugger_state *state)
831 return state->pdata->uart_getc(state->pdev);
835 static int debug_cmd_check_back(struct fiq_debugger_state *state, char c)
840 state->back_pointer = (state->back_pointer-1) & CMD_COUNT;
841 if (state->back_pointer != state->current_pointer) {
842 s = state->cmd_buf[state->back_pointer];
844 for(i = 0; i < strlen(state->debug_buf)-1; i++) {
845 state->pdata->uart_putc(state->pdev, 8);
846 state->pdata->uart_putc(state->pdev, ' ');
847 state->pdata->uart_putc(state->pdev, 8);
849 memset(state->debug_buf, 0, DEBUG_MAX);
850 strcpy(state->debug_buf, s);
851 state->debug_count = strlen(state->debug_buf);
852 debug_printf(state, state->debug_buf);
854 state->back_pointer = (state->back_pointer+1) & CMD_COUNT;
858 state->back_pointer = (state->back_pointer+1) & CMD_COUNT;
860 } else if (c == 'B') {
862 if (state->back_pointer != state->current_pointer) {
863 state->back_pointer = (state->back_pointer+1) & CMD_COUNT;
864 if(state->back_pointer == state->current_pointer){
867 s = state->cmd_buf[state->back_pointer];
869 for(i = 0; i < strlen(state->debug_buf)-1; i++) {
870 state->pdata->uart_putc(state->pdev, 8);
871 state->pdata->uart_putc(state->pdev, ' ');
872 state->pdata->uart_putc(state->pdev, 8);
874 memset(state->debug_buf, 0, DEBUG_MAX);
875 strcpy(state->debug_buf, s);
876 state->debug_count = strlen(state->debug_buf);
877 debug_printf(state, state->debug_buf);
882 for(i = 0; i < strlen(state->debug_buf)-1; i++) {
883 state->pdata->uart_putc(state->pdev, 8);
884 state->pdata->uart_putc(state->pdev, ' ');
885 state->pdata->uart_putc(state->pdev, 8);
887 memset(state->debug_buf, 0, DEBUG_MAX);
888 state->debug_count = 0;
894 static void debug_cmd_tab(struct fiq_debugger_state *state)
899 for (i = 0; i < ARRAY_SIZE(cmd_buf); i++) {
903 for (j = 1; j <= strlen(state->debug_buf); j++) {
905 for (i = 0; i < ARRAY_SIZE(cmd_buf); i++) {
906 if (cmd_buf[i][15] == 1) {
907 if (strncmp(state->debug_buf, cmd_buf[i], j)) {
919 for (i = 0; i < ARRAY_SIZE(cmd_buf); i++) {
920 if (cmd_buf[i][15] == 1)
924 for(j = 0; j < strlen(state->debug_buf); j++) {
925 state->pdata->uart_putc(state->pdev, 8);
926 state->pdata->uart_putc(state->pdev, ' ');
927 state->pdata->uart_putc(state->pdev, 8);
929 memset(state->debug_buf, 0, DEBUG_MAX);
930 strcpy(state->debug_buf, cmd_buf[i]);
931 state->debug_count = strlen(state->debug_buf);
932 debug_printf(state, state->debug_buf);
937 static bool debug_handle_uart_interrupt(struct fiq_debugger_state *state,
938 int this_cpu, void *regs, void *svc_sp)
943 bool signal_helper = false;
945 if (this_cpu != state->current_cpu) {
949 if (atomic_inc_return(&state->unhandled_fiq_count) !=
950 MAX_UNHANDLED_FIQ_COUNT)
953 debug_printf(state, "fiq_debugger: cpu %d not responding, "
954 "reverting to cpu %d\n", state->current_cpu,
957 atomic_set(&state->unhandled_fiq_count, 0);
958 switch_cpu(state, this_cpu);
962 state->in_fiq = true;
964 while ((c = debug_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
966 if (!state->debug_enable) {
967 if ((c == 13) || (c == 10)) {
968 state->debug_enable = true;
969 state->debug_count = 0;
972 } else if (c == FIQ_DEBUGGER_BREAK) {
973 state->console_enable = false;
974 debug_puts(state, "fiq debugger mode\n");
975 state->debug_count = 0;
976 state->back_pointer = CMD_COUNT;
977 state->current_pointer = CMD_COUNT;
978 memset(state->cmd_buf, 0, (CMD_COUNT+1)*DEBUG_MAX);
980 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
981 } else if (state->console_enable && state->tty_rbuf) {
982 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
983 signal_helper = true;
985 } else if (last_c == '[' && (c == 'A' || c == 'B' || c == 'C' || c == 'D')) {
986 if (state->debug_count > 0) {
987 state->debug_count--;
988 state->pdata->uart_putc(state->pdev, 8);
989 state->pdata->uart_putc(state->pdev, ' ');
990 state->pdata->uart_putc(state->pdev, 8);
992 debug_cmd_check_back(state, c);
995 debug_cmd_tab(state);
996 } else if ((c >= ' ') && (c < 127)) {
997 if (state->debug_count < (DEBUG_MAX - 1)) {
998 state->debug_buf[state->debug_count++] = c;
999 state->pdata->uart_putc(state->pdev, c);
1001 } else if ((c == 8) || (c == 127)) {
1002 if (state->debug_count > 0) {
1003 state->debug_count--;
1004 state->pdata->uart_putc(state->pdev, 8);
1005 state->pdata->uart_putc(state->pdev, ' ');
1006 state->pdata->uart_putc(state->pdev, 8);
1007 state->debug_buf[state->debug_count] = 0;
1009 } else if ((c == 13) || (c == 10)) {
1010 if (c == '\r' || (c == '\n' && last_c != '\r')) {
1011 state->pdata->uart_putc(state->pdev, '\r');
1012 state->pdata->uart_putc(state->pdev, '\n');
1014 if (state->debug_count) {
1015 state->debug_buf[state->debug_count] = 0;
1016 state->debug_count = 0;
1018 debug_fiq_exec(state, state->debug_buf,
1020 if (signal_helper == false) {
1021 state->current_pointer = (state->current_pointer-1) & CMD_COUNT;
1022 if (strcmp(state->cmd_buf[state->current_pointer], state->debug_buf)) {
1023 state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
1024 memset(state->cmd_buf[state->current_pointer], 0, DEBUG_MAX);
1025 strcpy(state->cmd_buf[state->current_pointer], state->debug_buf);
1027 memset(state->debug_buf, 0, DEBUG_MAX);
1028 state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
1029 state->back_pointer = state->current_pointer;
1032 debug_prompt(state);
1037 debug_uart_flush(state);
1038 if (state->pdata->fiq_ack)
1039 state->pdata->fiq_ack(state->pdev, state->fiq);
1041 /* poke sleep timer if necessary */
1042 if (state->debug_enable && !state->no_sleep)
1043 signal_helper = true;
1045 atomic_set(&state->unhandled_fiq_count, 0);
1046 state->in_fiq = false;
1048 return signal_helper;
1051 static void debug_fiq(struct fiq_glue_handler *h, void *regs, void *svc_sp)
1053 struct fiq_debugger_state *state =
1054 container_of(h, struct fiq_debugger_state, handler);
1055 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
1058 /* RK2928 USB-UART function, otg dp/dm default in uart status;
1059 * connect with otg cable&usb device, dp/dm will be hi-z status
1060 * and make uart controller enter infinite fiq loop
1062 #ifdef CONFIG_RK_USB_UART
1063 #ifdef CONFIG_ARCH_RK2928
1064 if(!(readl_relaxed(RK2928_GRF_BASE + 0x014c) & (1<<10))){//id low
1065 writel_relaxed(0x34000000, RK2928_GRF_BASE + 0x190); //enter usb phy
1067 #elif defined(CONFIG_ARCH_RK3188)
1068 if(!(readl_relaxed(RK30_GRF_BASE + 0x00ac) & (1 << 13))){//id low
1069 writel_relaxed((0x0300 << 16), RK30_GRF_BASE + 0x010c); //enter usb phy
1073 need_irq = debug_handle_uart_interrupt(state, this_cpu, regs, svc_sp);
1075 debug_force_irq(state);
1079 * When not using FIQs, we only use this single interrupt as an entry point.
1080 * This just effectively takes over the UART interrupt and does all the work
1083 static irqreturn_t debug_uart_irq(int irq, void *dev)
1085 struct fiq_debugger_state *state = dev;
1088 handle_wakeup(state);
1090 /* handle the debugger irq in regular context */
1091 not_done = debug_handle_uart_interrupt(state, smp_processor_id(),
1093 current_thread_info());
1095 debug_handle_irq_context(state);
1101 * If FIQs are used, not everything can happen in fiq context.
1102 * FIQ handler does what it can and then signals this interrupt to finish the
1103 * job in irq context.
1105 static irqreturn_t debug_signal_irq(int irq, void *dev)
1107 struct fiq_debugger_state *state = dev;
1109 if (state->pdata->force_irq_ack)
1110 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
1112 debug_handle_irq_context(state);
1117 static void debug_resume(struct fiq_glue_handler *h)
1119 struct fiq_debugger_state *state =
1120 container_of(h, struct fiq_debugger_state, handler);
1121 if (state->pdata->uart_resume)
1122 state->pdata->uart_resume(state->pdev);
1125 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1126 struct tty_driver *debug_console_device(struct console *co, int *index)
1128 struct fiq_debugger_state *state;
1129 state = container_of(co, struct fiq_debugger_state, console);
1131 return state->tty_driver;
1134 static void debug_console_write(struct console *co,
1135 const char *s, unsigned int count)
1137 struct fiq_debugger_state *state;
1139 state = container_of(co, struct fiq_debugger_state, console);
1141 if (!state->console_enable && !state->syslog_dumping)
1144 #ifdef CONFIG_RK_CONSOLE_THREAD
1145 if (state->pdata->console_write) {
1146 state->pdata->console_write(state->pdev, s, count);
1151 debug_uart_enable(state);
1154 state->pdata->uart_putc(state->pdev, '\r');
1155 state->pdata->uart_putc(state->pdev, *s++);
1157 debug_uart_flush(state);
1158 debug_uart_disable(state);
1161 static struct console fiq_debugger_console = {
1163 .device = debug_console_device,
1164 .write = debug_console_write,
1165 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
1168 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1170 struct fiq_debugger_state *state = tty->driver->driver_state;
1171 if (state->tty_open_count++)
1174 tty->driver_data = state;
1179 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1181 struct fiq_debugger_state *state = tty->driver_data;
1182 if (--state->tty_open_count)
1187 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1190 struct fiq_debugger_state *state = tty->driver_data;
1192 if (!state->console_enable)
1195 debug_uart_enable(state);
1196 for (i = 0; i < count; i++)
1197 state->pdata->uart_putc(state->pdev, *buf++);
1198 debug_uart_disable(state);
1203 int fiq_tty_write_room(struct tty_struct *tty)
1208 static const struct tty_operations fiq_tty_driver_ops = {
1209 .write = fiq_tty_write,
1210 .write_room = fiq_tty_write_room,
1211 .open = fiq_tty_open,
1212 .close = fiq_tty_close,
1215 static int fiq_debugger_tty_init(struct fiq_debugger_state *state)
1219 state->tty_driver = alloc_tty_driver(1);
1220 if (!state->tty_driver) {
1221 pr_err("Failed to allocate fiq debugger tty\n");
1225 state->tty_driver->owner = THIS_MODULE;
1226 state->tty_driver->driver_name = "fiq-debugger";
1227 state->tty_driver->name = "ttyFIQ";
1228 state->tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
1229 state->tty_driver->subtype = SERIAL_TYPE_NORMAL;
1230 state->tty_driver->init_termios = tty_std_termios;
1231 state->tty_driver->init_termios.c_cflag =
1232 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1233 state->tty_driver->init_termios.c_ispeed =
1234 state->tty_driver->init_termios.c_ospeed = 115200;
1235 state->tty_driver->flags = TTY_DRIVER_REAL_RAW;
1236 tty_set_operations(state->tty_driver, &fiq_tty_driver_ops);
1237 state->tty_driver->driver_state = state;
1239 ret = tty_register_driver(state->tty_driver);
1241 pr_err("Failed to register fiq tty: %d\n", ret);
1245 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1246 if (!state->tty_rbuf) {
1247 pr_err("Failed to allocate fiq debugger ringbuf\n");
1252 pr_info("Registered FIQ tty driver %p\n", state->tty_driver);
1256 fiq_debugger_ringbuf_free(state->tty_rbuf);
1257 state->tty_rbuf = NULL;
1258 put_tty_driver(state->tty_driver);
1263 static int fiq_debugger_dev_suspend(struct device *dev)
1265 struct platform_device *pdev = to_platform_device(dev);
1266 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1268 if (state->pdata->uart_dev_suspend)
1269 return state->pdata->uart_dev_suspend(pdev);
1273 static int fiq_debugger_dev_resume(struct device *dev)
1275 struct platform_device *pdev = to_platform_device(dev);
1276 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1278 if (state->pdata->uart_dev_resume)
1279 return state->pdata->uart_dev_resume(pdev);
1283 static int fiq_debugger_probe(struct platform_device *pdev)
1286 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1287 struct fiq_debugger_state *state;
1291 if (!pdata->uart_getc || !pdata->uart_putc)
1293 if ((pdata->uart_enable && !pdata->uart_disable) ||
1294 (!pdata->uart_enable && pdata->uart_disable))
1297 fiq = platform_get_irq_byname(pdev, "fiq");
1298 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1300 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1302 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1304 if (fiq >= 0 && !pdata->fiq_enable)
1307 state = kzalloc(sizeof(*state), GFP_KERNEL);
1308 setup_timer(&state->sleep_timer, sleep_timer_expired,
1309 (unsigned long)state);
1310 state->pdata = pdata;
1312 state->no_sleep = initial_no_sleep;
1313 state->debug_enable = initial_debug_enable;
1314 state->console_enable = initial_console_enable;
1317 state->uart_irq = uart_irq;
1318 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1319 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1321 platform_set_drvdata(pdev, state);
1323 spin_lock_init(&state->sleep_timer_lock);
1325 if (state->wakeup_irq < 0 && debug_have_fiq(state))
1326 state->no_sleep = true;
1327 state->ignore_next_wakeup_irq = !state->no_sleep;
1329 wake_lock_init(&state->debugger_wake_lock,
1330 WAKE_LOCK_SUSPEND, "serial-debug");
1332 state->clk = clk_get(&pdev->dev, NULL);
1333 if (IS_ERR(state->clk))
1336 /* do not call pdata->uart_enable here since uart_init may still
1337 * need to do some initialization before uart_enable can work.
1338 * So, only try to manage the clock during init.
1341 clk_enable(state->clk);
1343 if (pdata->uart_init) {
1344 ret = pdata->uart_init(pdev);
1349 debug_printf_nfiq(state, "<hit enter %sto activate fiq debugger>\n",
1350 state->no_sleep ? "" : "twice ");
1352 if (debug_have_fiq(state)) {
1353 state->handler.fiq = debug_fiq;
1354 state->handler.resume = debug_resume;
1355 ret = fiq_glue_register_handler(&state->handler);
1357 pr_err("%s: could not install fiq handler\n", __func__);
1358 goto err_register_fiq;
1361 pdata->fiq_enable(pdev, state->fiq, 1);
1363 ret = request_irq(state->uart_irq, debug_uart_irq,
1364 IRQF_NO_SUSPEND, "debug", state);
1366 pr_err("%s: could not install irq handler\n", __func__);
1367 goto err_register_irq;
1370 /* for irq-only mode, we want this irq to wake us up, if it
1373 enable_irq_wake(state->uart_irq);
1377 clk_disable(state->clk);
1379 if (state->signal_irq >= 0) {
1380 ret = request_irq(state->signal_irq, debug_signal_irq,
1381 IRQF_TRIGGER_RISING, "debug-signal", state);
1383 pr_err("serial_debugger: could not install signal_irq");
1386 if (state->wakeup_irq >= 0) {
1387 ret = request_irq(state->wakeup_irq, wakeup_irq_handler,
1388 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1389 "debug-wakeup", state);
1391 pr_err("serial_debugger: "
1392 "could not install wakeup irq\n");
1393 state->wakeup_irq = -1;
1395 ret = enable_irq_wake(state->wakeup_irq);
1397 pr_err("serial_debugger: "
1398 "could not enable wakeup\n");
1399 state->wakeup_irq_no_set_wake = true;
1403 if (state->no_sleep)
1404 handle_wakeup(state);
1406 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1407 state->console = fiq_debugger_console;
1408 register_console(&state->console);
1409 fiq_debugger_tty_init(state);
1415 if (pdata->uart_free)
1416 pdata->uart_free(pdev);
1419 clk_disable(state->clk);
1421 clk_put(state->clk);
1422 wake_lock_destroy(&state->debugger_wake_lock);
1423 platform_set_drvdata(pdev, NULL);
1428 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1429 .suspend = fiq_debugger_dev_suspend,
1430 .resume = fiq_debugger_dev_resume,
1433 static struct platform_driver fiq_debugger_driver = {
1434 .probe = fiq_debugger_probe,
1436 .name = "fiq_debugger",
1437 .pm = &fiq_debugger_dev_pm_ops,
1441 static int __init fiq_debugger_init(void)
1443 return platform_driver_register(&fiq_debugger_driver);
1446 postcore_initcall(fiq_debugger_init);