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 #ifdef CONFIG_FIQ_DEBUGGER_UART_OVERLAY
46 #include <linux/uaccess.h>
48 #include "fiq_debugger.h"
49 #include "fiq_debugger_priv.h"
50 #include "fiq_debugger_ringbuf.h"
53 #define MAX_UNHANDLED_FIQ_COUNT 1000000
55 #define MAX_FIQ_DEBUGGER_PORTS 4
57 struct fiq_debugger_state {
58 #ifdef CONFIG_FIQ_GLUE
59 struct fiq_glue_handler handler;
61 struct fiq_debugger_output output;
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];
81 bool ignore_next_wakeup_irq;
82 struct timer_list sleep_timer;
83 spinlock_t sleep_timer_lock;
85 struct wake_lock debugger_wake_lock;
88 atomic_t unhandled_fiq_count;
91 struct work_struct work;
93 char work_cmd[DEBUG_MAX];
95 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
96 spinlock_t console_lock;
97 struct console console;
98 struct tty_port tty_port;
99 struct fiq_debugger_ringbuf *tty_rbuf;
103 unsigned int last_irqs[NR_IRQS];
104 unsigned int last_local_timer_irqs[NR_CPUS];
107 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
108 struct tty_driver *fiq_tty_driver;
111 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
112 static bool initial_no_sleep = true;
114 static bool initial_no_sleep;
117 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
118 static bool initial_debug_enable = true;
119 static bool initial_console_enable = true;
121 static bool initial_debug_enable;
122 static bool initial_console_enable;
125 static bool fiq_kgdb_enable;
127 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
128 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
129 module_param_named(console_enable, initial_console_enable, bool, 0644);
130 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
132 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
134 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
136 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
139 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
141 if (state->wakeup_irq < 0)
143 enable_irq(state->wakeup_irq);
144 if (!state->wakeup_irq_no_set_wake)
145 enable_irq_wake(state->wakeup_irq);
148 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
150 if (state->wakeup_irq < 0)
152 disable_irq_nosync(state->wakeup_irq);
153 if (!state->wakeup_irq_no_set_wake)
154 disable_irq_wake(state->wakeup_irq);
158 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
160 return (state->fiq >= 0);
163 #ifdef CONFIG_FIQ_GLUE
164 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
166 unsigned int irq = state->signal_irq;
168 if (WARN_ON(!fiq_debugger_have_fiq(state)))
170 if (state->pdata->force_irq) {
171 state->pdata->force_irq(state->pdev, irq);
173 struct irq_chip *chip = irq_get_chip(irq);
174 if (chip && chip->irq_retrigger)
175 chip->irq_retrigger(irq_get_irq_data(irq));
180 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
183 clk_enable(state->clk);
184 if (state->pdata->uart_enable)
185 state->pdata->uart_enable(state->pdev);
188 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
190 if (state->pdata->uart_disable)
191 state->pdata->uart_disable(state->pdev);
193 clk_disable(state->clk);
196 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
198 if (state->pdata->uart_flush)
199 state->pdata->uart_flush(state->pdev);
202 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
204 state->pdata->uart_putc(state->pdev, c);
207 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
212 fiq_debugger_putc(state, '\r');
213 fiq_debugger_putc(state, c);
217 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
219 fiq_debugger_puts(state, "debug> ");
222 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
226 struct kmsg_dumper dumper = { .active = true };
229 kmsg_dump_rewind_nolock(&dumper);
230 while (kmsg_dump_get_line_nolock(&dumper, true, buf,
231 sizeof(buf) - 1, &len)) {
233 fiq_debugger_puts(state, buf);
237 static void fiq_debugger_printf(struct fiq_debugger_output *output,
238 const char *fmt, ...)
240 struct fiq_debugger_state *state;
244 state = container_of(output, struct fiq_debugger_state, output);
246 vsnprintf(buf, sizeof(buf), fmt, ap);
249 fiq_debugger_puts(state, buf);
252 /* Safe outside fiq context */
253 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
255 struct fiq_debugger_state *state = cookie;
258 unsigned long irq_flags;
261 vsnprintf(buf, 128, fmt, ap);
264 local_irq_save(irq_flags);
265 fiq_debugger_puts(state, buf);
266 fiq_debugger_uart_flush(state);
267 local_irq_restore(irq_flags);
268 return state->debug_abort;
271 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
274 struct irq_desc *desc;
276 fiq_debugger_printf(&state->output,
277 "irqnr total since-last status name\n");
278 for_each_irq_desc(n, desc) {
279 struct irqaction *act = desc->action;
280 if (!act && !kstat_irqs(n))
282 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x %s\n", n,
284 kstat_irqs(n) - state->last_irqs[n],
285 desc->status_use_accessors,
286 (act && act->name) ? act->name : "???");
287 state->last_irqs[n] = kstat_irqs(n);
291 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
293 struct task_struct *g;
294 struct task_struct *p;
296 static const char stat_nam[] = "RSDTtZX";
298 fiq_debugger_printf(&state->output, "pid ppid prio task pc\n");
299 read_lock(&tasklist_lock);
300 do_each_thread(g, p) {
301 task_state = p->state ? __ffs(p->state) + 1 : 0;
302 fiq_debugger_printf(&state->output,
303 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
304 fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
305 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
306 if (task_state == TASK_RUNNING)
307 fiq_debugger_printf(&state->output, " running\n");
309 fiq_debugger_printf(&state->output, " %08lx\n",
311 } while_each_thread(g, p);
312 read_unlock(&tasklist_lock);
315 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
316 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
318 state->syslog_dumping = true;
321 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
323 state->syslog_dumping = false;
326 extern int do_syslog(int type, char __user *bug, int count);
327 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
329 do_syslog(5 /* clear */, NULL, 0);
332 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
334 fiq_debugger_dump_kernel_log(state);
338 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
340 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
341 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
344 fiq_debugger_begin_syslog_dump(state);
346 fiq_debugger_end_syslog_dump(state);
350 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
352 if (!fiq_kgdb_enable) {
353 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
357 fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
358 state->console_enable = true;
363 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
368 spin_lock_irqsave(&state->work_lock, flags);
369 if (state->work_cmd[0] != '\0') {
370 fiq_debugger_printf(&state->output, "work command processor busy\n");
371 spin_unlock_irqrestore(&state->work_lock, flags);
375 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
376 spin_unlock_irqrestore(&state->work_lock, flags);
378 schedule_work(&state->work);
381 static void fiq_debugger_work(struct work_struct *work)
383 struct fiq_debugger_state *state;
384 char work_cmd[DEBUG_MAX];
388 state = container_of(work, struct fiq_debugger_state, work);
390 spin_lock_irqsave(&state->work_lock, flags);
392 strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
393 state->work_cmd[0] = '\0';
395 spin_unlock_irqrestore(&state->work_lock, flags);
398 if (!strncmp(cmd, "reboot", 6)) {
405 kernel_restart(NULL);
407 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
412 /* This function CANNOT be called in FIQ context */
413 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
415 if (!strcmp(cmd, "ps"))
416 fiq_debugger_do_ps(state);
417 if (!strcmp(cmd, "sysrq"))
418 fiq_debugger_do_sysrq(state, 'h');
419 if (!strncmp(cmd, "sysrq ", 6))
420 fiq_debugger_do_sysrq(state, cmd[6]);
422 if (!strcmp(cmd, "kgdb"))
423 fiq_debugger_do_kgdb(state);
425 if (!strncmp(cmd, "reboot", 6))
426 fiq_debugger_schedule_work(state, cmd);
429 static void fiq_debugger_help(struct fiq_debugger_state *state)
431 fiq_debugger_printf(&state->output,
432 "FIQ Debugger commands:\n"
434 " regs Register dump\n"
435 " allregs Extended Register dump\n"
437 " reboot [<c>] Reboot with command <c>\n"
438 " reset [<c>] Hard reset with command <c>\n"
439 " irqs Interupt status\n"
441 " version Kernel version\n");
442 fiq_debugger_printf(&state->output,
443 " sleep Allow sleep while in FIQ\n"
444 " nosleep Disable sleep while in FIQ\n"
445 " console Switch terminal to console\n"
447 " cpu <number> Switch to CPU<number>\n");
448 fiq_debugger_printf(&state->output,
450 " sysrq sysrq options\n"
451 " sysrq <param> Execute sysrq with <param>\n");
453 fiq_debugger_printf(&state->output,
454 " kgdb Enter kernel debugger\n");
458 static void fiq_debugger_take_affinity(void *info)
460 struct fiq_debugger_state *state = info;
461 struct cpumask cpumask;
463 cpumask_clear(&cpumask);
464 cpumask_set_cpu(get_cpu(), &cpumask);
466 irq_set_affinity(state->uart_irq, &cpumask);
469 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
471 if (!fiq_debugger_have_fiq(state))
472 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
474 state->current_cpu = cpu;
477 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
478 const char *cmd, const struct pt_regs *regs,
481 bool signal_helper = false;
483 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
484 fiq_debugger_help(state);
485 } else if (!strcmp(cmd, "pc")) {
486 fiq_debugger_dump_pc(&state->output, regs);
487 } else if (!strcmp(cmd, "regs")) {
488 fiq_debugger_dump_regs(&state->output, regs);
489 } else if (!strcmp(cmd, "allregs")) {
490 fiq_debugger_dump_allregs(&state->output, regs);
491 } else if (!strcmp(cmd, "bt")) {
492 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
493 } else if (!strncmp(cmd, "reset", 5)) {
499 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
500 machine_restart(tmp_cmd);
502 machine_restart(NULL);
504 } else if (!strcmp(cmd, "irqs")) {
505 fiq_debugger_dump_irqs(state);
506 } else if (!strcmp(cmd, "kmsg")) {
507 fiq_debugger_dump_kernel_log(state);
508 } else if (!strcmp(cmd, "version")) {
509 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
510 } else if (!strcmp(cmd, "sleep")) {
511 state->no_sleep = false;
512 fiq_debugger_printf(&state->output, "enabling sleep\n");
513 } else if (!strcmp(cmd, "nosleep")) {
514 state->no_sleep = true;
515 fiq_debugger_printf(&state->output, "disabling sleep\n");
516 } else if (!strcmp(cmd, "console")) {
517 fiq_debugger_printf(&state->output, "console mode\n");
518 fiq_debugger_uart_flush(state);
519 state->console_enable = true;
520 } else if (!strcmp(cmd, "cpu")) {
521 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
522 } else if (!strncmp(cmd, "cpu ", 4)) {
523 unsigned long cpu = 0;
524 if (kstrtoul(cmd + 4, 10, &cpu) == 0)
525 fiq_debugger_switch_cpu(state, cpu);
527 fiq_debugger_printf(&state->output, "invalid cpu\n");
528 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
530 if (state->debug_busy) {
531 fiq_debugger_printf(&state->output,
532 "command processor busy. trying to abort.\n");
533 state->debug_abort = -1;
535 strcpy(state->debug_cmd, cmd);
536 state->debug_busy = 1;
541 if (!state->console_enable)
542 fiq_debugger_prompt(state);
544 return signal_helper;
547 static void fiq_debugger_sleep_timer_expired(unsigned long data)
549 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
552 spin_lock_irqsave(&state->sleep_timer_lock, flags);
553 if (state->uart_enabled && !state->no_sleep) {
554 if (state->debug_enable && !state->console_enable) {
555 state->debug_enable = false;
556 fiq_debugger_printf_nfiq(state,
557 "suspending fiq debugger\n");
559 state->ignore_next_wakeup_irq = true;
560 fiq_debugger_uart_disable(state);
561 state->uart_enabled = false;
562 fiq_debugger_enable_wakeup_irq(state);
564 wake_unlock(&state->debugger_wake_lock);
565 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
568 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
572 spin_lock_irqsave(&state->sleep_timer_lock, flags);
573 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
574 state->ignore_next_wakeup_irq = false;
575 } else if (!state->uart_enabled) {
576 wake_lock(&state->debugger_wake_lock);
577 fiq_debugger_uart_enable(state);
578 state->uart_enabled = true;
579 fiq_debugger_disable_wakeup_irq(state);
580 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
582 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
585 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
587 struct fiq_debugger_state *state = dev;
589 if (!state->no_sleep)
590 fiq_debugger_puts(state, "WAKEUP\n");
591 fiq_debugger_handle_wakeup(state);
597 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
599 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
600 if (state->tty_port.ops) {
602 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
603 for (i = 0; i < count; i++) {
604 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
605 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
606 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
607 pr_warn("fiq tty failed to consume byte\n");
609 tty_flip_buffer_push(&state->tty_port);
614 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
616 if (!state->no_sleep) {
619 spin_lock_irqsave(&state->sleep_timer_lock, flags);
620 wake_lock(&state->debugger_wake_lock);
621 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
622 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
624 fiq_debugger_handle_console_irq_context(state);
625 if (state->debug_busy) {
626 fiq_debugger_irq_exec(state, state->debug_cmd);
627 if (!state->console_enable)
628 fiq_debugger_prompt(state);
629 state->debug_busy = 0;
633 static int fiq_debugger_getc(struct fiq_debugger_state *state)
635 return state->pdata->uart_getc(state->pdev);
638 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
639 int this_cpu, const struct pt_regs *regs, void *svc_sp)
644 bool signal_helper = false;
646 if (this_cpu != state->current_cpu) {
650 if (atomic_inc_return(&state->unhandled_fiq_count) !=
651 MAX_UNHANDLED_FIQ_COUNT)
654 fiq_debugger_printf(&state->output,
655 "fiq_debugger: cpu %d not responding, "
656 "reverting to cpu %d\n", state->current_cpu,
659 atomic_set(&state->unhandled_fiq_count, 0);
660 fiq_debugger_switch_cpu(state, this_cpu);
664 state->in_fiq = true;
666 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
668 if (!state->debug_enable) {
669 if ((c == 13) || (c == 10)) {
670 state->debug_enable = true;
671 state->debug_count = 0;
672 fiq_debugger_prompt(state);
674 } else if (c == FIQ_DEBUGGER_BREAK) {
675 state->console_enable = false;
676 fiq_debugger_puts(state, "fiq debugger mode\n");
677 state->debug_count = 0;
678 fiq_debugger_prompt(state);
679 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
680 } else if (state->console_enable && state->tty_rbuf) {
681 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
682 signal_helper = true;
684 } else if ((c >= ' ') && (c < 127)) {
685 if (state->debug_count < (DEBUG_MAX - 1)) {
686 state->debug_buf[state->debug_count++] = c;
687 fiq_debugger_putc(state, c);
689 } else if ((c == 8) || (c == 127)) {
690 if (state->debug_count > 0) {
691 state->debug_count--;
692 fiq_debugger_putc(state, 8);
693 fiq_debugger_putc(state, ' ');
694 fiq_debugger_putc(state, 8);
696 } else if ((c == 13) || (c == 10)) {
697 if (c == '\r' || (c == '\n' && last_c != '\r')) {
698 fiq_debugger_putc(state, '\r');
699 fiq_debugger_putc(state, '\n');
701 if (state->debug_count) {
702 state->debug_buf[state->debug_count] = 0;
703 state->debug_count = 0;
705 fiq_debugger_fiq_exec(state,
709 fiq_debugger_prompt(state);
714 if (!state->console_enable)
715 fiq_debugger_uart_flush(state);
716 if (state->pdata->fiq_ack)
717 state->pdata->fiq_ack(state->pdev, state->fiq);
719 /* poke sleep timer if necessary */
720 if (state->debug_enable && !state->no_sleep)
721 signal_helper = true;
723 atomic_set(&state->unhandled_fiq_count, 0);
724 state->in_fiq = false;
726 return signal_helper;
729 #ifdef CONFIG_FIQ_GLUE
730 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
731 const struct pt_regs *regs, void *svc_sp)
733 struct fiq_debugger_state *state =
734 container_of(h, struct fiq_debugger_state, handler);
735 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
738 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
741 fiq_debugger_force_irq(state);
746 * When not using FIQs, we only use this single interrupt as an entry point.
747 * This just effectively takes over the UART interrupt and does all the work
750 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
752 struct fiq_debugger_state *state = dev;
755 fiq_debugger_handle_wakeup(state);
757 /* handle the debugger irq in regular context */
758 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
760 current_thread_info());
762 fiq_debugger_handle_irq_context(state);
768 * If FIQs are used, not everything can happen in fiq context.
769 * FIQ handler does what it can and then signals this interrupt to finish the
770 * job in irq context.
772 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
774 struct fiq_debugger_state *state = dev;
776 if (state->pdata->force_irq_ack)
777 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
779 fiq_debugger_handle_irq_context(state);
784 #ifdef CONFIG_FIQ_GLUE
785 static void fiq_debugger_resume(struct fiq_glue_handler *h)
787 struct fiq_debugger_state *state =
788 container_of(h, struct fiq_debugger_state, handler);
789 if (state->pdata->uart_resume)
790 state->pdata->uart_resume(state->pdev);
794 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
795 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
798 return fiq_tty_driver;
801 static void fiq_debugger_console_write(struct console *co,
802 const char *s, unsigned int count)
804 struct fiq_debugger_state *state;
807 state = container_of(co, struct fiq_debugger_state, console);
809 if (!state->console_enable && !state->syslog_dumping)
812 fiq_debugger_uart_enable(state);
813 spin_lock_irqsave(&state->console_lock, flags);
816 fiq_debugger_putc(state, '\r');
817 fiq_debugger_putc(state, *s++);
819 fiq_debugger_uart_flush(state);
820 spin_unlock_irqrestore(&state->console_lock, flags);
821 fiq_debugger_uart_disable(state);
824 static struct console fiq_debugger_console = {
826 .device = fiq_debugger_console_device,
827 .write = fiq_debugger_console_write,
828 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
831 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
833 int line = tty->index;
834 struct fiq_debugger_state **states = tty->driver->driver_state;
835 struct fiq_debugger_state *state = states[line];
837 return tty_port_open(&state->tty_port, tty, filp);
840 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
842 tty_port_close(tty->port, tty, filp);
845 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
848 int line = tty->index;
849 struct fiq_debugger_state **states = tty->driver->driver_state;
850 struct fiq_debugger_state *state = states[line];
852 if (!state->console_enable)
855 fiq_debugger_uart_enable(state);
856 spin_lock_irq(&state->console_lock);
857 for (i = 0; i < count; i++)
858 fiq_debugger_putc(state, *buf++);
859 spin_unlock_irq(&state->console_lock);
860 fiq_debugger_uart_disable(state);
865 int fiq_tty_write_room(struct tty_struct *tty)
870 #ifdef CONFIG_CONSOLE_POLL
871 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
876 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
878 struct fiq_debugger_state **states = driver->driver_state;
879 struct fiq_debugger_state *state = states[line];
880 int c = NO_POLL_CHAR;
882 fiq_debugger_uart_enable(state);
883 if (fiq_debugger_have_fiq(state)) {
884 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
886 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
887 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
890 c = fiq_debugger_getc(state);
891 if (c == FIQ_DEBUGGER_NO_CHAR)
894 fiq_debugger_uart_disable(state);
899 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
901 struct fiq_debugger_state **states = driver->driver_state;
902 struct fiq_debugger_state *state = states[line];
903 fiq_debugger_uart_enable(state);
904 fiq_debugger_putc(state, ch);
905 fiq_debugger_uart_disable(state);
909 static const struct tty_port_operations fiq_tty_port_ops;
911 static const struct tty_operations fiq_tty_driver_ops = {
912 .write = fiq_tty_write,
913 .write_room = fiq_tty_write_room,
914 .open = fiq_tty_open,
915 .close = fiq_tty_close,
916 #ifdef CONFIG_CONSOLE_POLL
917 .poll_init = fiq_tty_poll_init,
918 .poll_get_char = fiq_tty_poll_get_char,
919 .poll_put_char = fiq_tty_poll_put_char,
923 static int fiq_debugger_tty_init(void)
926 struct fiq_debugger_state **states = NULL;
928 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
930 pr_err("Failed to allocate fiq debugger state structres\n");
934 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
935 if (!fiq_tty_driver) {
936 pr_err("Failed to allocate fiq debugger tty\n");
941 fiq_tty_driver->owner = THIS_MODULE;
942 fiq_tty_driver->driver_name = "fiq-debugger";
943 fiq_tty_driver->name = "ttyFIQ";
944 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
945 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
946 fiq_tty_driver->init_termios = tty_std_termios;
947 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
948 TTY_DRIVER_DYNAMIC_DEV;
949 fiq_tty_driver->driver_state = states;
951 fiq_tty_driver->init_termios.c_cflag =
952 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
953 fiq_tty_driver->init_termios.c_ispeed = 115200;
954 fiq_tty_driver->init_termios.c_ospeed = 115200;
956 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
958 ret = tty_register_driver(fiq_tty_driver);
960 pr_err("Failed to register fiq tty: %d\n", ret);
964 pr_info("Registered FIQ tty driver\n");
968 put_tty_driver(fiq_tty_driver);
969 fiq_tty_driver = NULL;
975 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
978 struct device *tty_dev;
979 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
981 states[state->pdev->id] = state;
983 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
984 if (!state->tty_rbuf) {
985 pr_err("Failed to allocate fiq debugger ringbuf\n");
990 tty_port_init(&state->tty_port);
991 state->tty_port.ops = &fiq_tty_port_ops;
993 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
994 state->pdev->id, &state->pdev->dev);
995 if (IS_ERR(tty_dev)) {
996 pr_err("Failed to register fiq debugger tty device\n");
997 ret = PTR_ERR(tty_dev);
1001 device_set_wakeup_capable(tty_dev, 1);
1003 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1008 fiq_debugger_ringbuf_free(state->tty_rbuf);
1009 state->tty_rbuf = NULL;
1014 static int fiq_debugger_dev_suspend(struct device *dev)
1016 struct platform_device *pdev = to_platform_device(dev);
1017 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1019 if (state->pdata->uart_dev_suspend)
1020 return state->pdata->uart_dev_suspend(pdev);
1024 static int fiq_debugger_dev_resume(struct device *dev)
1026 struct platform_device *pdev = to_platform_device(dev);
1027 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1029 if (state->pdata->uart_dev_resume)
1030 return state->pdata->uart_dev_resume(pdev);
1034 static int fiq_debugger_probe(struct platform_device *pdev)
1037 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1038 struct fiq_debugger_state *state;
1042 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1045 if (!pdata->uart_getc || !pdata->uart_putc)
1047 if ((pdata->uart_enable && !pdata->uart_disable) ||
1048 (!pdata->uart_enable && pdata->uart_disable))
1051 fiq = platform_get_irq_byname(pdev, "fiq");
1052 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1054 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1056 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1058 if (fiq >= 0 && !pdata->fiq_enable)
1061 state = kzalloc(sizeof(*state), GFP_KERNEL);
1062 state->output.printf = fiq_debugger_printf;
1063 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1064 (unsigned long)state);
1065 state->pdata = pdata;
1067 state->no_sleep = initial_no_sleep;
1068 state->debug_enable = initial_debug_enable;
1069 state->console_enable = initial_console_enable;
1072 state->uart_irq = uart_irq;
1073 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1074 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1076 INIT_WORK(&state->work, fiq_debugger_work);
1077 spin_lock_init(&state->work_lock);
1079 platform_set_drvdata(pdev, state);
1081 spin_lock_init(&state->sleep_timer_lock);
1083 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1084 state->no_sleep = true;
1085 state->ignore_next_wakeup_irq = !state->no_sleep;
1087 wake_lock_init(&state->debugger_wake_lock,
1088 WAKE_LOCK_SUSPEND, "serial-debug");
1090 state->clk = clk_get(&pdev->dev, NULL);
1091 if (IS_ERR(state->clk))
1094 /* do not call pdata->uart_enable here since uart_init may still
1095 * need to do some initialization before uart_enable can work.
1096 * So, only try to manage the clock during init.
1099 clk_enable(state->clk);
1101 if (pdata->uart_init) {
1102 ret = pdata->uart_init(pdev);
1107 fiq_debugger_printf_nfiq(state,
1108 "<hit enter %sto activate fiq debugger>\n",
1109 state->no_sleep ? "" : "twice ");
1111 #ifdef CONFIG_FIQ_GLUE
1112 if (fiq_debugger_have_fiq(state)) {
1113 state->handler.fiq = fiq_debugger_fiq;
1114 state->handler.resume = fiq_debugger_resume;
1115 ret = fiq_glue_register_handler(&state->handler);
1117 pr_err("%s: could not install fiq handler\n", __func__);
1118 goto err_register_irq;
1121 pdata->fiq_enable(pdev, state->fiq, 1);
1125 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1126 IRQF_NO_SUSPEND, "debug", state);
1128 pr_err("%s: could not install irq handler\n", __func__);
1129 goto err_register_irq;
1132 /* for irq-only mode, we want this irq to wake us up, if it
1135 enable_irq_wake(state->uart_irq);
1139 clk_disable(state->clk);
1141 if (state->signal_irq >= 0) {
1142 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1143 IRQF_TRIGGER_RISING, "debug-signal", state);
1145 pr_err("serial_debugger: could not install signal_irq");
1148 if (state->wakeup_irq >= 0) {
1149 ret = request_irq(state->wakeup_irq,
1150 fiq_debugger_wakeup_irq_handler,
1151 IRQF_TRIGGER_FALLING,
1152 "debug-wakeup", state);
1154 pr_err("serial_debugger: "
1155 "could not install wakeup irq\n");
1156 state->wakeup_irq = -1;
1158 ret = enable_irq_wake(state->wakeup_irq);
1160 pr_err("serial_debugger: "
1161 "could not enable wakeup\n");
1162 state->wakeup_irq_no_set_wake = true;
1166 if (state->no_sleep)
1167 fiq_debugger_handle_wakeup(state);
1169 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1170 spin_lock_init(&state->console_lock);
1171 state->console = fiq_debugger_console;
1172 state->console.index = pdev->id;
1173 if (!console_set_on_cmdline)
1174 add_preferred_console(state->console.name,
1175 state->console.index, NULL);
1176 register_console(&state->console);
1177 fiq_debugger_tty_init_one(state);
1182 if (pdata->uart_free)
1183 pdata->uart_free(pdev);
1186 clk_disable(state->clk);
1188 clk_put(state->clk);
1189 wake_lock_destroy(&state->debugger_wake_lock);
1190 platform_set_drvdata(pdev, NULL);
1195 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1196 .suspend = fiq_debugger_dev_suspend,
1197 .resume = fiq_debugger_dev_resume,
1200 static struct platform_driver fiq_debugger_driver = {
1201 .probe = fiq_debugger_probe,
1203 .name = "fiq_debugger",
1204 .pm = &fiq_debugger_dev_pm_ops,
1208 #if defined(CONFIG_FIQ_DEBUGGER_UART_OVERLAY)
1209 int fiq_debugger_uart_overlay(void)
1211 struct device_node *onp = of_find_node_by_path("/uart_overlay@0");
1215 pr_err("serial_debugger: uart overlay not found\n");
1219 ret = of_overlay_create(onp);
1221 pr_err("serial_debugger: fail to create overlay: %d\n", ret);
1226 pr_info("serial_debugger: uart overlay applied\n");
1231 static int __init fiq_debugger_init(void)
1233 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1234 fiq_debugger_tty_init();
1236 #if defined(CONFIG_FIQ_DEBUGGER_UART_OVERLAY)
1237 fiq_debugger_uart_overlay();
1239 return platform_driver_register(&fiq_debugger_driver);
1242 postcore_initcall(fiq_debugger_init);