2 * drivers/staging/android/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/kmsg_dump.h>
27 #include <linux/irq.h>
28 #include <linux/delay.h>
29 #include <linux/reboot.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/smp.h>
33 #include <linux/timer.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/wakelock.h>
38 #ifdef CONFIG_FIQ_GLUE
39 #include <asm/fiq_glue.h>
42 #include <linux/uaccess.h>
44 #include "fiq_debugger.h"
45 #include "fiq_debugger_priv.h"
46 #include "fiq_debugger_ringbuf.h"
49 #define MAX_UNHANDLED_FIQ_COUNT 1000000
51 #define MAX_FIQ_DEBUGGER_PORTS 4
53 struct fiq_debugger_state {
54 #ifdef CONFIG_FIQ_GLUE
55 struct fiq_glue_handler handler;
57 struct fiq_debugger_output output;
63 bool wakeup_irq_no_set_wake;
65 struct fiq_debugger_pdata *pdata;
66 struct platform_device *pdev;
68 char debug_cmd[DEBUG_MAX];
72 char debug_buf[DEBUG_MAX];
77 bool ignore_next_wakeup_irq;
78 struct timer_list sleep_timer;
79 spinlock_t sleep_timer_lock;
81 struct wake_lock debugger_wake_lock;
84 atomic_t unhandled_fiq_count;
87 struct work_struct work;
89 char work_cmd[DEBUG_MAX];
91 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
92 spinlock_t console_lock;
93 struct console console;
94 struct tty_port tty_port;
95 struct fiq_debugger_ringbuf *tty_rbuf;
99 unsigned int last_irqs[NR_IRQS];
100 unsigned int last_local_timer_irqs[NR_CPUS];
103 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
104 struct tty_driver *fiq_tty_driver;
107 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
108 static bool initial_no_sleep = true;
110 static bool initial_no_sleep;
113 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
114 static bool initial_debug_enable = true;
115 static bool initial_console_enable = true;
117 static bool initial_debug_enable;
118 static bool initial_console_enable;
121 static bool fiq_kgdb_enable;
123 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
124 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
125 module_param_named(console_enable, initial_console_enable, bool, 0644);
126 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
128 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
130 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
132 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
135 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
137 if (state->wakeup_irq < 0)
139 enable_irq(state->wakeup_irq);
140 if (!state->wakeup_irq_no_set_wake)
141 enable_irq_wake(state->wakeup_irq);
144 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
146 if (state->wakeup_irq < 0)
148 disable_irq_nosync(state->wakeup_irq);
149 if (!state->wakeup_irq_no_set_wake)
150 disable_irq_wake(state->wakeup_irq);
154 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
156 return (state->fiq >= 0);
159 #ifdef CONFIG_FIQ_GLUE
160 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
162 unsigned int irq = state->signal_irq;
164 if (WARN_ON(!fiq_debugger_have_fiq(state)))
166 if (state->pdata->force_irq) {
167 state->pdata->force_irq(state->pdev, irq);
169 struct irq_chip *chip = irq_get_chip(irq);
170 if (chip && chip->irq_retrigger)
171 chip->irq_retrigger(irq_get_irq_data(irq));
176 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
179 clk_enable(state->clk);
180 if (state->pdata->uart_enable)
181 state->pdata->uart_enable(state->pdev);
184 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
186 if (state->pdata->uart_disable)
187 state->pdata->uart_disable(state->pdev);
189 clk_disable(state->clk);
192 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
194 if (state->pdata->uart_flush)
195 state->pdata->uart_flush(state->pdev);
198 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
200 state->pdata->uart_putc(state->pdev, c);
203 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
208 fiq_debugger_putc(state, '\r');
209 fiq_debugger_putc(state, c);
213 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
215 fiq_debugger_puts(state, "debug> ");
218 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
222 struct kmsg_dumper dumper = { .active = true };
225 kmsg_dump_rewind_nolock(&dumper);
226 while (kmsg_dump_get_line_nolock(&dumper, true, buf,
227 sizeof(buf) - 1, &len)) {
229 fiq_debugger_puts(state, buf);
233 static void fiq_debugger_printf(struct fiq_debugger_output *output,
234 const char *fmt, ...)
236 struct fiq_debugger_state *state;
240 state = container_of(output, struct fiq_debugger_state, output);
242 vsnprintf(buf, sizeof(buf), fmt, ap);
245 fiq_debugger_puts(state, buf);
248 /* Safe outside fiq context */
249 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
251 struct fiq_debugger_state *state = cookie;
254 unsigned long irq_flags;
257 vsnprintf(buf, 128, fmt, ap);
260 local_irq_save(irq_flags);
261 fiq_debugger_puts(state, buf);
262 fiq_debugger_uart_flush(state);
263 local_irq_restore(irq_flags);
264 return state->debug_abort;
267 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
270 struct irq_desc *desc;
272 fiq_debugger_printf(&state->output,
273 "irqnr total since-last status name\n");
274 for_each_irq_desc(n, desc) {
275 struct irqaction *act = desc->action;
276 if (!act && !kstat_irqs(n))
278 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x %s\n", n,
280 kstat_irqs(n) - state->last_irqs[n],
281 desc->status_use_accessors,
282 (act && act->name) ? act->name : "???");
283 state->last_irqs[n] = kstat_irqs(n);
287 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
289 struct task_struct *g;
290 struct task_struct *p;
292 static const char stat_nam[] = "RSDTtZX";
294 fiq_debugger_printf(&state->output, "pid ppid prio task pc\n");
295 read_lock(&tasklist_lock);
296 do_each_thread(g, p) {
297 task_state = p->state ? __ffs(p->state) + 1 : 0;
298 fiq_debugger_printf(&state->output,
299 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
300 fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
301 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
302 if (task_state == TASK_RUNNING)
303 fiq_debugger_printf(&state->output, " running\n");
305 fiq_debugger_printf(&state->output, " %08lx\n",
307 } while_each_thread(g, p);
308 read_unlock(&tasklist_lock);
311 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
312 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
314 state->syslog_dumping = true;
317 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
319 state->syslog_dumping = false;
322 extern int do_syslog(int type, char __user *bug, int count);
323 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
325 do_syslog(5 /* clear */, NULL, 0);
328 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
330 fiq_debugger_dump_kernel_log(state);
334 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
336 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
337 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
340 fiq_debugger_begin_syslog_dump(state);
342 fiq_debugger_end_syslog_dump(state);
346 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
348 if (!fiq_kgdb_enable) {
349 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
353 fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
354 state->console_enable = true;
359 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
364 spin_lock_irqsave(&state->work_lock, flags);
365 if (state->work_cmd[0] != '\0') {
366 fiq_debugger_printf(&state->output, "work command processor busy\n");
367 spin_unlock_irqrestore(&state->work_lock, flags);
371 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
372 spin_unlock_irqrestore(&state->work_lock, flags);
374 schedule_work(&state->work);
377 static void fiq_debugger_work(struct work_struct *work)
379 struct fiq_debugger_state *state;
380 char work_cmd[DEBUG_MAX];
384 state = container_of(work, struct fiq_debugger_state, work);
386 spin_lock_irqsave(&state->work_lock, flags);
388 strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
389 state->work_cmd[0] = '\0';
391 spin_unlock_irqrestore(&state->work_lock, flags);
394 if (!strncmp(cmd, "reboot", 6)) {
401 kernel_restart(NULL);
403 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
408 /* This function CANNOT be called in FIQ context */
409 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
411 if (!strcmp(cmd, "ps"))
412 fiq_debugger_do_ps(state);
413 if (!strcmp(cmd, "sysrq"))
414 fiq_debugger_do_sysrq(state, 'h');
415 if (!strncmp(cmd, "sysrq ", 6))
416 fiq_debugger_do_sysrq(state, cmd[6]);
418 if (!strcmp(cmd, "kgdb"))
419 fiq_debugger_do_kgdb(state);
421 if (!strncmp(cmd, "reboot", 6))
422 fiq_debugger_schedule_work(state, cmd);
425 static void fiq_debugger_help(struct fiq_debugger_state *state)
427 fiq_debugger_printf(&state->output,
428 "FIQ Debugger commands:\n"
430 " regs Register dump\n"
431 " allregs Extended Register dump\n"
433 " reboot [<c>] Reboot with command <c>\n"
434 " reset [<c>] Hard reset with command <c>\n"
435 " irqs Interupt status\n"
437 " version Kernel version\n");
438 fiq_debugger_printf(&state->output,
439 " sleep Allow sleep while in FIQ\n"
440 " nosleep Disable sleep while in FIQ\n"
441 " console Switch terminal to console\n"
443 " cpu <number> Switch to CPU<number>\n");
444 fiq_debugger_printf(&state->output,
446 " sysrq sysrq options\n"
447 " sysrq <param> Execute sysrq with <param>\n");
449 fiq_debugger_printf(&state->output,
450 " kgdb Enter kernel debugger\n");
454 static void fiq_debugger_take_affinity(void *info)
456 struct fiq_debugger_state *state = info;
457 struct cpumask cpumask;
459 cpumask_clear(&cpumask);
460 cpumask_set_cpu(get_cpu(), &cpumask);
462 irq_set_affinity(state->uart_irq, &cpumask);
465 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
467 if (!fiq_debugger_have_fiq(state))
468 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
470 state->current_cpu = cpu;
473 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
474 const char *cmd, const struct pt_regs *regs,
477 bool signal_helper = false;
479 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
480 fiq_debugger_help(state);
481 } else if (!strcmp(cmd, "pc")) {
482 fiq_debugger_dump_pc(&state->output, regs);
483 } else if (!strcmp(cmd, "regs")) {
484 fiq_debugger_dump_regs(&state->output, regs);
485 } else if (!strcmp(cmd, "allregs")) {
486 fiq_debugger_dump_allregs(&state->output, regs);
487 } else if (!strcmp(cmd, "bt")) {
488 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
489 } else if (!strncmp(cmd, "reset", 5)) {
495 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
496 machine_restart(tmp_cmd);
498 machine_restart(NULL);
500 } else if (!strcmp(cmd, "irqs")) {
501 fiq_debugger_dump_irqs(state);
502 } else if (!strcmp(cmd, "kmsg")) {
503 fiq_debugger_dump_kernel_log(state);
504 } else if (!strcmp(cmd, "version")) {
505 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
506 } else if (!strcmp(cmd, "sleep")) {
507 state->no_sleep = false;
508 fiq_debugger_printf(&state->output, "enabling sleep\n");
509 } else if (!strcmp(cmd, "nosleep")) {
510 state->no_sleep = true;
511 fiq_debugger_printf(&state->output, "disabling sleep\n");
512 } else if (!strcmp(cmd, "console")) {
513 fiq_debugger_printf(&state->output, "console mode\n");
514 fiq_debugger_uart_flush(state);
515 state->console_enable = true;
516 } else if (!strcmp(cmd, "cpu")) {
517 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
518 } else if (!strncmp(cmd, "cpu ", 4)) {
519 unsigned long cpu = 0;
520 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
521 fiq_debugger_switch_cpu(state, cpu);
523 fiq_debugger_printf(&state->output, "invalid cpu\n");
524 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
526 if (state->debug_busy) {
527 fiq_debugger_printf(&state->output,
528 "command processor busy. trying to abort.\n");
529 state->debug_abort = -1;
531 strcpy(state->debug_cmd, cmd);
532 state->debug_busy = 1;
537 if (!state->console_enable)
538 fiq_debugger_prompt(state);
540 return signal_helper;
543 static void fiq_debugger_sleep_timer_expired(unsigned long data)
545 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
548 spin_lock_irqsave(&state->sleep_timer_lock, flags);
549 if (state->uart_enabled && !state->no_sleep) {
550 if (state->debug_enable && !state->console_enable) {
551 state->debug_enable = false;
552 fiq_debugger_printf_nfiq(state,
553 "suspending fiq debugger\n");
555 state->ignore_next_wakeup_irq = true;
556 fiq_debugger_uart_disable(state);
557 state->uart_enabled = false;
558 fiq_debugger_enable_wakeup_irq(state);
560 wake_unlock(&state->debugger_wake_lock);
561 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
564 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
568 spin_lock_irqsave(&state->sleep_timer_lock, flags);
569 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
570 state->ignore_next_wakeup_irq = false;
571 } else if (!state->uart_enabled) {
572 wake_lock(&state->debugger_wake_lock);
573 fiq_debugger_uart_enable(state);
574 state->uart_enabled = true;
575 fiq_debugger_disable_wakeup_irq(state);
576 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
578 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
581 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
583 struct fiq_debugger_state *state = dev;
585 if (!state->no_sleep)
586 fiq_debugger_puts(state, "WAKEUP\n");
587 fiq_debugger_handle_wakeup(state);
593 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
595 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
596 if (state->tty_port.ops) {
598 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
599 for (i = 0; i < count; i++) {
600 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
601 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
602 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
603 pr_warn("fiq tty failed to consume byte\n");
605 tty_flip_buffer_push(&state->tty_port);
610 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
612 if (!state->no_sleep) {
615 spin_lock_irqsave(&state->sleep_timer_lock, flags);
616 wake_lock(&state->debugger_wake_lock);
617 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
618 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
620 fiq_debugger_handle_console_irq_context(state);
621 if (state->debug_busy) {
622 fiq_debugger_irq_exec(state, state->debug_cmd);
623 if (!state->console_enable)
624 fiq_debugger_prompt(state);
625 state->debug_busy = 0;
629 static int fiq_debugger_getc(struct fiq_debugger_state *state)
631 return state->pdata->uart_getc(state->pdev);
634 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
635 int this_cpu, const struct pt_regs *regs, void *svc_sp)
640 bool signal_helper = false;
642 if (this_cpu != state->current_cpu) {
646 if (atomic_inc_return(&state->unhandled_fiq_count) !=
647 MAX_UNHANDLED_FIQ_COUNT)
650 fiq_debugger_printf(&state->output,
651 "fiq_debugger: cpu %d not responding, "
652 "reverting to cpu %d\n", state->current_cpu,
655 atomic_set(&state->unhandled_fiq_count, 0);
656 fiq_debugger_switch_cpu(state, this_cpu);
660 state->in_fiq = true;
662 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
664 if (!state->debug_enable) {
665 if ((c == 13) || (c == 10)) {
666 state->debug_enable = true;
667 state->debug_count = 0;
668 fiq_debugger_prompt(state);
670 } else if (c == FIQ_DEBUGGER_BREAK) {
671 state->console_enable = false;
672 fiq_debugger_puts(state, "fiq debugger mode\n");
673 state->debug_count = 0;
674 fiq_debugger_prompt(state);
675 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
676 } else if (state->console_enable && state->tty_rbuf) {
677 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
678 signal_helper = true;
680 } else if ((c >= ' ') && (c < 127)) {
681 if (state->debug_count < (DEBUG_MAX - 1)) {
682 state->debug_buf[state->debug_count++] = c;
683 fiq_debugger_putc(state, c);
685 } else if ((c == 8) || (c == 127)) {
686 if (state->debug_count > 0) {
687 state->debug_count--;
688 fiq_debugger_putc(state, 8);
689 fiq_debugger_putc(state, ' ');
690 fiq_debugger_putc(state, 8);
692 } else if ((c == 13) || (c == 10)) {
693 if (c == '\r' || (c == '\n' && last_c != '\r')) {
694 fiq_debugger_putc(state, '\r');
695 fiq_debugger_putc(state, '\n');
697 if (state->debug_count) {
698 state->debug_buf[state->debug_count] = 0;
699 state->debug_count = 0;
701 fiq_debugger_fiq_exec(state,
705 fiq_debugger_prompt(state);
710 if (!state->console_enable)
711 fiq_debugger_uart_flush(state);
712 if (state->pdata->fiq_ack)
713 state->pdata->fiq_ack(state->pdev, state->fiq);
715 /* poke sleep timer if necessary */
716 if (state->debug_enable && !state->no_sleep)
717 signal_helper = true;
719 atomic_set(&state->unhandled_fiq_count, 0);
720 state->in_fiq = false;
722 return signal_helper;
725 #ifdef CONFIG_FIQ_GLUE
726 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
727 const struct pt_regs *regs, void *svc_sp)
729 struct fiq_debugger_state *state =
730 container_of(h, struct fiq_debugger_state, handler);
731 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
734 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
737 fiq_debugger_force_irq(state);
742 * When not using FIQs, we only use this single interrupt as an entry point.
743 * This just effectively takes over the UART interrupt and does all the work
746 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
748 struct fiq_debugger_state *state = dev;
751 fiq_debugger_handle_wakeup(state);
753 /* handle the debugger irq in regular context */
754 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
756 current_thread_info());
758 fiq_debugger_handle_irq_context(state);
764 * If FIQs are used, not everything can happen in fiq context.
765 * FIQ handler does what it can and then signals this interrupt to finish the
766 * job in irq context.
768 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
770 struct fiq_debugger_state *state = dev;
772 if (state->pdata->force_irq_ack)
773 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
775 fiq_debugger_handle_irq_context(state);
780 #ifdef CONFIG_FIQ_GLUE
781 static void fiq_debugger_resume(struct fiq_glue_handler *h)
783 struct fiq_debugger_state *state =
784 container_of(h, struct fiq_debugger_state, handler);
785 if (state->pdata->uart_resume)
786 state->pdata->uart_resume(state->pdev);
790 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
791 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
794 return fiq_tty_driver;
797 static void fiq_debugger_console_write(struct console *co,
798 const char *s, unsigned int count)
800 struct fiq_debugger_state *state;
803 state = container_of(co, struct fiq_debugger_state, console);
805 if (!state->console_enable && !state->syslog_dumping)
808 fiq_debugger_uart_enable(state);
809 spin_lock_irqsave(&state->console_lock, flags);
812 fiq_debugger_putc(state, '\r');
813 fiq_debugger_putc(state, *s++);
815 fiq_debugger_uart_flush(state);
816 spin_unlock_irqrestore(&state->console_lock, flags);
817 fiq_debugger_uart_disable(state);
820 static struct console fiq_debugger_console = {
822 .device = fiq_debugger_console_device,
823 .write = fiq_debugger_console_write,
824 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
827 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
829 int line = tty->index;
830 struct fiq_debugger_state **states = tty->driver->driver_state;
831 struct fiq_debugger_state *state = states[line];
833 return tty_port_open(&state->tty_port, tty, filp);
836 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
838 tty_port_close(tty->port, tty, filp);
841 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
844 int line = tty->index;
845 struct fiq_debugger_state **states = tty->driver->driver_state;
846 struct fiq_debugger_state *state = states[line];
848 if (!state->console_enable)
851 fiq_debugger_uart_enable(state);
852 spin_lock_irq(&state->console_lock);
853 for (i = 0; i < count; i++)
854 fiq_debugger_putc(state, *buf++);
855 spin_unlock_irq(&state->console_lock);
856 fiq_debugger_uart_disable(state);
861 int fiq_tty_write_room(struct tty_struct *tty)
866 #ifdef CONFIG_CONSOLE_POLL
867 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
872 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
874 struct fiq_debugger_state **states = driver->driver_state;
875 struct fiq_debugger_state *state = states[line];
876 int c = NO_POLL_CHAR;
878 fiq_debugger_uart_enable(state);
879 if (fiq_debugger_have_fiq(state)) {
880 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
882 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
883 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
886 c = fiq_debugger_getc(state);
887 if (c == FIQ_DEBUGGER_NO_CHAR)
890 fiq_debugger_uart_disable(state);
895 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
897 struct fiq_debugger_state **states = driver->driver_state;
898 struct fiq_debugger_state *state = states[line];
899 fiq_debugger_uart_enable(state);
900 fiq_debugger_putc(state, ch);
901 fiq_debugger_uart_disable(state);
905 static const struct tty_port_operations fiq_tty_port_ops;
907 static const struct tty_operations fiq_tty_driver_ops = {
908 .write = fiq_tty_write,
909 .write_room = fiq_tty_write_room,
910 .open = fiq_tty_open,
911 .close = fiq_tty_close,
912 #ifdef CONFIG_CONSOLE_POLL
913 .poll_init = fiq_tty_poll_init,
914 .poll_get_char = fiq_tty_poll_get_char,
915 .poll_put_char = fiq_tty_poll_put_char,
919 static int fiq_debugger_tty_init(void)
922 struct fiq_debugger_state **states = NULL;
924 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
926 pr_err("Failed to allocate fiq debugger state structres\n");
930 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
931 if (!fiq_tty_driver) {
932 pr_err("Failed to allocate fiq debugger tty\n");
937 fiq_tty_driver->owner = THIS_MODULE;
938 fiq_tty_driver->driver_name = "fiq-debugger";
939 fiq_tty_driver->name = "ttyFIQ";
940 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
941 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
942 fiq_tty_driver->init_termios = tty_std_termios;
943 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
944 TTY_DRIVER_DYNAMIC_DEV;
945 fiq_tty_driver->driver_state = states;
947 fiq_tty_driver->init_termios.c_cflag =
948 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
949 fiq_tty_driver->init_termios.c_ispeed = 115200;
950 fiq_tty_driver->init_termios.c_ospeed = 115200;
952 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
954 ret = tty_register_driver(fiq_tty_driver);
956 pr_err("Failed to register fiq tty: %d\n", ret);
960 pr_info("Registered FIQ tty driver\n");
964 put_tty_driver(fiq_tty_driver);
965 fiq_tty_driver = NULL;
971 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
974 struct device *tty_dev;
975 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
977 states[state->pdev->id] = state;
979 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
980 if (!state->tty_rbuf) {
981 pr_err("Failed to allocate fiq debugger ringbuf\n");
986 tty_port_init(&state->tty_port);
987 state->tty_port.ops = &fiq_tty_port_ops;
989 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
990 state->pdev->id, &state->pdev->dev);
991 if (IS_ERR(tty_dev)) {
992 pr_err("Failed to register fiq debugger tty device\n");
993 ret = PTR_ERR(tty_dev);
997 device_set_wakeup_capable(tty_dev, 1);
999 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1004 fiq_debugger_ringbuf_free(state->tty_rbuf);
1005 state->tty_rbuf = NULL;
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 (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1041 if (!pdata->uart_getc || !pdata->uart_putc)
1043 if ((pdata->uart_enable && !pdata->uart_disable) ||
1044 (!pdata->uart_enable && pdata->uart_disable))
1047 fiq = platform_get_irq_byname(pdev, "fiq");
1048 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1050 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1052 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1054 if (fiq >= 0 && !pdata->fiq_enable)
1057 state = kzalloc(sizeof(*state), GFP_KERNEL);
1058 state->output.printf = fiq_debugger_printf;
1059 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1060 (unsigned long)state);
1061 state->pdata = pdata;
1063 state->no_sleep = initial_no_sleep;
1064 state->debug_enable = initial_debug_enable;
1065 state->console_enable = initial_console_enable;
1068 state->uart_irq = uart_irq;
1069 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1070 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1072 INIT_WORK(&state->work, fiq_debugger_work);
1073 spin_lock_init(&state->work_lock);
1075 platform_set_drvdata(pdev, state);
1077 spin_lock_init(&state->sleep_timer_lock);
1079 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1080 state->no_sleep = true;
1081 state->ignore_next_wakeup_irq = !state->no_sleep;
1083 wake_lock_init(&state->debugger_wake_lock,
1084 WAKE_LOCK_SUSPEND, "serial-debug");
1086 state->clk = clk_get(&pdev->dev, NULL);
1087 if (IS_ERR(state->clk))
1090 /* do not call pdata->uart_enable here since uart_init may still
1091 * need to do some initialization before uart_enable can work.
1092 * So, only try to manage the clock during init.
1095 clk_enable(state->clk);
1097 if (pdata->uart_init) {
1098 ret = pdata->uart_init(pdev);
1103 fiq_debugger_printf_nfiq(state,
1104 "<hit enter %sto activate fiq debugger>\n",
1105 state->no_sleep ? "" : "twice ");
1107 #ifdef CONFIG_FIQ_GLUE
1108 if (fiq_debugger_have_fiq(state)) {
1109 state->handler.fiq = fiq_debugger_fiq;
1110 state->handler.resume = fiq_debugger_resume;
1111 ret = fiq_glue_register_handler(&state->handler);
1113 pr_err("%s: could not install fiq handler\n", __func__);
1114 goto err_register_irq;
1117 pdata->fiq_enable(pdev, state->fiq, 1);
1121 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1122 IRQF_NO_SUSPEND, "debug", state);
1124 pr_err("%s: could not install irq handler\n", __func__);
1125 goto err_register_irq;
1128 /* for irq-only mode, we want this irq to wake us up, if it
1131 enable_irq_wake(state->uart_irq);
1135 clk_disable(state->clk);
1137 if (state->signal_irq >= 0) {
1138 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1139 IRQF_TRIGGER_RISING, "debug-signal", state);
1141 pr_err("serial_debugger: could not install signal_irq");
1144 if (state->wakeup_irq >= 0) {
1145 ret = request_irq(state->wakeup_irq,
1146 fiq_debugger_wakeup_irq_handler,
1147 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1148 "debug-wakeup", state);
1150 pr_err("serial_debugger: "
1151 "could not install wakeup irq\n");
1152 state->wakeup_irq = -1;
1154 ret = enable_irq_wake(state->wakeup_irq);
1156 pr_err("serial_debugger: "
1157 "could not enable wakeup\n");
1158 state->wakeup_irq_no_set_wake = true;
1162 if (state->no_sleep)
1163 fiq_debugger_handle_wakeup(state);
1165 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1166 spin_lock_init(&state->console_lock);
1167 state->console = fiq_debugger_console;
1168 state->console.index = pdev->id;
1169 if (!console_set_on_cmdline)
1170 add_preferred_console(state->console.name,
1171 state->console.index, NULL);
1172 register_console(&state->console);
1173 fiq_debugger_tty_init_one(state);
1178 if (pdata->uart_free)
1179 pdata->uart_free(pdev);
1182 clk_disable(state->clk);
1184 clk_put(state->clk);
1185 wake_lock_destroy(&state->debugger_wake_lock);
1186 platform_set_drvdata(pdev, NULL);
1191 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1192 .suspend = fiq_debugger_dev_suspend,
1193 .resume = fiq_debugger_dev_resume,
1196 static struct platform_driver fiq_debugger_driver = {
1197 .probe = fiq_debugger_probe,
1199 .name = "fiq_debugger",
1200 .pm = &fiq_debugger_dev_pm_ops,
1204 static int __init fiq_debugger_init(void)
1206 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1207 fiq_debugger_tty_init();
1209 return platform_driver_register(&fiq_debugger_driver);
1212 postcore_initcall(fiq_debugger_init);