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"
432 " bt Stack trace\n");
433 fiq_debugger_printf(&state->output,
434 " reboot [<c>] Reboot with command <c>\n"
435 " reset [<c>] Hard reset with command <c>\n"
436 " irqs Interupt status\n"
438 " version Kernel version\n");
439 fiq_debugger_printf(&state->output,
440 " sleep Allow sleep while in FIQ\n"
441 " nosleep Disable sleep while in FIQ\n"
442 " console Switch terminal to console\n"
444 " cpu <number> Switch to CPU<number>\n");
445 fiq_debugger_printf(&state->output,
447 " sysrq sysrq options\n"
448 " sysrq <param> Execute sysrq with <param>\n");
450 fiq_debugger_printf(&state->output,
451 " kgdb Enter kernel debugger\n");
455 static void fiq_debugger_take_affinity(void *info)
457 struct fiq_debugger_state *state = info;
458 struct cpumask cpumask;
460 cpumask_clear(&cpumask);
461 cpumask_set_cpu(get_cpu(), &cpumask);
463 irq_set_affinity(state->uart_irq, &cpumask);
466 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
468 if (!fiq_debugger_have_fiq(state))
469 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
471 state->current_cpu = cpu;
474 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
475 const char *cmd, const struct pt_regs *regs,
478 bool signal_helper = false;
480 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
481 fiq_debugger_help(state);
482 } else if (!strcmp(cmd, "pc")) {
483 fiq_debugger_dump_pc(&state->output, regs);
484 } else if (!strcmp(cmd, "regs")) {
485 fiq_debugger_dump_regs(&state->output, regs);
486 } else if (!strcmp(cmd, "allregs")) {
487 fiq_debugger_dump_allregs(&state->output, regs);
488 } else if (!strcmp(cmd, "bt")) {
489 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
490 } else if (!strncmp(cmd, "reset", 5)) {
496 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
497 machine_restart(tmp_cmd);
499 machine_restart(NULL);
501 } else if (!strcmp(cmd, "irqs")) {
502 fiq_debugger_dump_irqs(state);
503 } else if (!strcmp(cmd, "kmsg")) {
504 fiq_debugger_dump_kernel_log(state);
505 } else if (!strcmp(cmd, "version")) {
506 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
507 } else if (!strcmp(cmd, "sleep")) {
508 state->no_sleep = false;
509 fiq_debugger_printf(&state->output, "enabling sleep\n");
510 } else if (!strcmp(cmd, "nosleep")) {
511 state->no_sleep = true;
512 fiq_debugger_printf(&state->output, "disabling sleep\n");
513 } else if (!strcmp(cmd, "console")) {
514 fiq_debugger_printf(&state->output, "console mode\n");
515 fiq_debugger_uart_flush(state);
516 state->console_enable = true;
517 } else if (!strcmp(cmd, "cpu")) {
518 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
519 } else if (!strncmp(cmd, "cpu ", 4)) {
520 unsigned long cpu = 0;
521 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
522 fiq_debugger_switch_cpu(state, cpu);
524 fiq_debugger_printf(&state->output, "invalid cpu\n");
525 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
527 if (state->debug_busy) {
528 fiq_debugger_printf(&state->output,
529 "command processor busy. trying to abort.\n");
530 state->debug_abort = -1;
532 strcpy(state->debug_cmd, cmd);
533 state->debug_busy = 1;
538 if (!state->console_enable)
539 fiq_debugger_prompt(state);
541 return signal_helper;
544 static void fiq_debugger_sleep_timer_expired(unsigned long data)
546 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
549 spin_lock_irqsave(&state->sleep_timer_lock, flags);
550 if (state->uart_enabled && !state->no_sleep) {
551 if (state->debug_enable && !state->console_enable) {
552 state->debug_enable = false;
553 fiq_debugger_printf_nfiq(state,
554 "suspending fiq debugger\n");
556 state->ignore_next_wakeup_irq = true;
557 fiq_debugger_uart_disable(state);
558 state->uart_enabled = false;
559 fiq_debugger_enable_wakeup_irq(state);
561 wake_unlock(&state->debugger_wake_lock);
562 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
565 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
569 spin_lock_irqsave(&state->sleep_timer_lock, flags);
570 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
571 state->ignore_next_wakeup_irq = false;
572 } else if (!state->uart_enabled) {
573 wake_lock(&state->debugger_wake_lock);
574 fiq_debugger_uart_enable(state);
575 state->uart_enabled = true;
576 fiq_debugger_disable_wakeup_irq(state);
577 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
579 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
582 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
584 struct fiq_debugger_state *state = dev;
586 if (!state->no_sleep)
587 fiq_debugger_puts(state, "WAKEUP\n");
588 fiq_debugger_handle_wakeup(state);
594 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
596 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
597 if (state->tty_port.ops) {
599 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
600 for (i = 0; i < count; i++) {
601 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
602 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
603 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
604 pr_warn("fiq tty failed to consume byte\n");
606 tty_flip_buffer_push(&state->tty_port);
611 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
613 if (!state->no_sleep) {
616 spin_lock_irqsave(&state->sleep_timer_lock, flags);
617 wake_lock(&state->debugger_wake_lock);
618 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
619 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
621 fiq_debugger_handle_console_irq_context(state);
622 if (state->debug_busy) {
623 fiq_debugger_irq_exec(state, state->debug_cmd);
624 if (!state->console_enable)
625 fiq_debugger_prompt(state);
626 state->debug_busy = 0;
630 static int fiq_debugger_getc(struct fiq_debugger_state *state)
632 return state->pdata->uart_getc(state->pdev);
635 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
636 int this_cpu, const struct pt_regs *regs, void *svc_sp)
641 bool signal_helper = false;
643 if (this_cpu != state->current_cpu) {
647 if (atomic_inc_return(&state->unhandled_fiq_count) !=
648 MAX_UNHANDLED_FIQ_COUNT)
651 fiq_debugger_printf(&state->output,
652 "fiq_debugger: cpu %d not responding, "
653 "reverting to cpu %d\n", state->current_cpu,
656 atomic_set(&state->unhandled_fiq_count, 0);
657 fiq_debugger_switch_cpu(state, this_cpu);
661 state->in_fiq = true;
663 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
665 if (!state->debug_enable) {
666 if ((c == 13) || (c == 10)) {
667 state->debug_enable = true;
668 state->debug_count = 0;
669 fiq_debugger_prompt(state);
671 } else if (c == FIQ_DEBUGGER_BREAK) {
672 state->console_enable = false;
673 fiq_debugger_puts(state, "fiq debugger mode\n");
674 state->debug_count = 0;
675 fiq_debugger_prompt(state);
676 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
677 } else if (state->console_enable && state->tty_rbuf) {
678 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
679 signal_helper = true;
681 } else if ((c >= ' ') && (c < 127)) {
682 if (state->debug_count < (DEBUG_MAX - 1)) {
683 state->debug_buf[state->debug_count++] = c;
684 fiq_debugger_putc(state, c);
686 } else if ((c == 8) || (c == 127)) {
687 if (state->debug_count > 0) {
688 state->debug_count--;
689 fiq_debugger_putc(state, 8);
690 fiq_debugger_putc(state, ' ');
691 fiq_debugger_putc(state, 8);
693 } else if ((c == 13) || (c == 10)) {
694 if (c == '\r' || (c == '\n' && last_c != '\r')) {
695 fiq_debugger_putc(state, '\r');
696 fiq_debugger_putc(state, '\n');
698 if (state->debug_count) {
699 state->debug_buf[state->debug_count] = 0;
700 state->debug_count = 0;
702 fiq_debugger_fiq_exec(state,
706 fiq_debugger_prompt(state);
711 if (!state->console_enable)
712 fiq_debugger_uart_flush(state);
713 if (state->pdata->fiq_ack)
714 state->pdata->fiq_ack(state->pdev, state->fiq);
716 /* poke sleep timer if necessary */
717 if (state->debug_enable && !state->no_sleep)
718 signal_helper = true;
720 atomic_set(&state->unhandled_fiq_count, 0);
721 state->in_fiq = false;
723 return signal_helper;
726 #ifdef CONFIG_FIQ_GLUE
727 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
728 const struct pt_regs *regs, void *svc_sp)
730 struct fiq_debugger_state *state =
731 container_of(h, struct fiq_debugger_state, handler);
732 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
735 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
738 fiq_debugger_force_irq(state);
743 * When not using FIQs, we only use this single interrupt as an entry point.
744 * This just effectively takes over the UART interrupt and does all the work
747 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
749 struct fiq_debugger_state *state = dev;
752 fiq_debugger_handle_wakeup(state);
754 /* handle the debugger irq in regular context */
755 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
757 current_thread_info());
759 fiq_debugger_handle_irq_context(state);
765 * If FIQs are used, not everything can happen in fiq context.
766 * FIQ handler does what it can and then signals this interrupt to finish the
767 * job in irq context.
769 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
771 struct fiq_debugger_state *state = dev;
773 if (state->pdata->force_irq_ack)
774 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
776 fiq_debugger_handle_irq_context(state);
781 #ifdef CONFIG_FIQ_GLUE
782 static void fiq_debugger_resume(struct fiq_glue_handler *h)
784 struct fiq_debugger_state *state =
785 container_of(h, struct fiq_debugger_state, handler);
786 if (state->pdata->uart_resume)
787 state->pdata->uart_resume(state->pdev);
791 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
792 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
795 return fiq_tty_driver;
798 static void fiq_debugger_console_write(struct console *co,
799 const char *s, unsigned int count)
801 struct fiq_debugger_state *state;
804 state = container_of(co, struct fiq_debugger_state, console);
806 if (!state->console_enable && !state->syslog_dumping)
809 fiq_debugger_uart_enable(state);
810 spin_lock_irqsave(&state->console_lock, flags);
813 fiq_debugger_putc(state, '\r');
814 fiq_debugger_putc(state, *s++);
816 fiq_debugger_uart_flush(state);
817 spin_unlock_irqrestore(&state->console_lock, flags);
818 fiq_debugger_uart_disable(state);
821 static struct console fiq_debugger_console = {
823 .device = fiq_debugger_console_device,
824 .write = fiq_debugger_console_write,
825 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
828 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
830 int line = tty->index;
831 struct fiq_debugger_state **states = tty->driver->driver_state;
832 struct fiq_debugger_state *state = states[line];
834 return tty_port_open(&state->tty_port, tty, filp);
837 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
839 tty_port_close(tty->port, tty, filp);
842 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
845 int line = tty->index;
846 struct fiq_debugger_state **states = tty->driver->driver_state;
847 struct fiq_debugger_state *state = states[line];
849 if (!state->console_enable)
852 fiq_debugger_uart_enable(state);
853 spin_lock_irq(&state->console_lock);
854 for (i = 0; i < count; i++)
855 fiq_debugger_putc(state, *buf++);
856 spin_unlock_irq(&state->console_lock);
857 fiq_debugger_uart_disable(state);
862 int fiq_tty_write_room(struct tty_struct *tty)
867 #ifdef CONFIG_CONSOLE_POLL
868 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
873 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
875 struct fiq_debugger_state **states = driver->driver_state;
876 struct fiq_debugger_state *state = states[line];
877 int c = NO_POLL_CHAR;
879 fiq_debugger_uart_enable(state);
880 if (fiq_debugger_have_fiq(state)) {
881 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
883 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
884 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
887 c = fiq_debugger_getc(state);
888 if (c == FIQ_DEBUGGER_NO_CHAR)
891 fiq_debugger_uart_disable(state);
896 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
898 struct fiq_debugger_state **states = driver->driver_state;
899 struct fiq_debugger_state *state = states[line];
900 fiq_debugger_uart_enable(state);
901 fiq_debugger_putc(state, ch);
902 fiq_debugger_uart_disable(state);
906 static const struct tty_port_operations fiq_tty_port_ops;
908 static const struct tty_operations fiq_tty_driver_ops = {
909 .write = fiq_tty_write,
910 .write_room = fiq_tty_write_room,
911 .open = fiq_tty_open,
912 .close = fiq_tty_close,
913 #ifdef CONFIG_CONSOLE_POLL
914 .poll_init = fiq_tty_poll_init,
915 .poll_get_char = fiq_tty_poll_get_char,
916 .poll_put_char = fiq_tty_poll_put_char,
920 static int fiq_debugger_tty_init(void)
923 struct fiq_debugger_state **states = NULL;
925 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
927 pr_err("Failed to allocate fiq debugger state structres\n");
931 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
932 if (!fiq_tty_driver) {
933 pr_err("Failed to allocate fiq debugger tty\n");
938 fiq_tty_driver->owner = THIS_MODULE;
939 fiq_tty_driver->driver_name = "fiq-debugger";
940 fiq_tty_driver->name = "ttyFIQ";
941 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
942 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
943 fiq_tty_driver->init_termios = tty_std_termios;
944 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
945 TTY_DRIVER_DYNAMIC_DEV;
946 fiq_tty_driver->driver_state = states;
948 fiq_tty_driver->init_termios.c_cflag =
949 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
950 fiq_tty_driver->init_termios.c_ispeed = 115200;
951 fiq_tty_driver->init_termios.c_ospeed = 115200;
953 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
955 ret = tty_register_driver(fiq_tty_driver);
957 pr_err("Failed to register fiq tty: %d\n", ret);
961 pr_info("Registered FIQ tty driver\n");
965 put_tty_driver(fiq_tty_driver);
966 fiq_tty_driver = NULL;
972 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
975 struct device *tty_dev;
976 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
978 states[state->pdev->id] = state;
980 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
981 if (!state->tty_rbuf) {
982 pr_err("Failed to allocate fiq debugger ringbuf\n");
987 tty_port_init(&state->tty_port);
988 state->tty_port.ops = &fiq_tty_port_ops;
990 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
991 state->pdev->id, &state->pdev->dev);
992 if (IS_ERR(tty_dev)) {
993 pr_err("Failed to register fiq debugger tty device\n");
994 ret = PTR_ERR(tty_dev);
998 device_set_wakeup_capable(tty_dev, 1);
1000 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1005 fiq_debugger_ringbuf_free(state->tty_rbuf);
1006 state->tty_rbuf = NULL;
1011 static int fiq_debugger_dev_suspend(struct device *dev)
1013 struct platform_device *pdev = to_platform_device(dev);
1014 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1016 if (state->pdata->uart_dev_suspend)
1017 return state->pdata->uart_dev_suspend(pdev);
1021 static int fiq_debugger_dev_resume(struct device *dev)
1023 struct platform_device *pdev = to_platform_device(dev);
1024 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1026 if (state->pdata->uart_dev_resume)
1027 return state->pdata->uart_dev_resume(pdev);
1031 static int fiq_debugger_probe(struct platform_device *pdev)
1034 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1035 struct fiq_debugger_state *state;
1039 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1042 if (!pdata->uart_getc || !pdata->uart_putc)
1044 if ((pdata->uart_enable && !pdata->uart_disable) ||
1045 (!pdata->uart_enable && pdata->uart_disable))
1048 fiq = platform_get_irq_byname(pdev, "fiq");
1049 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1051 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1053 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1055 if (fiq >= 0 && !pdata->fiq_enable)
1058 state = kzalloc(sizeof(*state), GFP_KERNEL);
1059 state->output.printf = fiq_debugger_printf;
1060 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1061 (unsigned long)state);
1062 state->pdata = pdata;
1064 state->no_sleep = initial_no_sleep;
1065 state->debug_enable = initial_debug_enable;
1066 state->console_enable = initial_console_enable;
1069 state->uart_irq = uart_irq;
1070 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1071 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1073 INIT_WORK(&state->work, fiq_debugger_work);
1074 spin_lock_init(&state->work_lock);
1076 platform_set_drvdata(pdev, state);
1078 spin_lock_init(&state->sleep_timer_lock);
1080 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1081 state->no_sleep = true;
1082 state->ignore_next_wakeup_irq = !state->no_sleep;
1084 wake_lock_init(&state->debugger_wake_lock,
1085 WAKE_LOCK_SUSPEND, "serial-debug");
1087 state->clk = clk_get(&pdev->dev, NULL);
1088 if (IS_ERR(state->clk))
1091 /* do not call pdata->uart_enable here since uart_init may still
1092 * need to do some initialization before uart_enable can work.
1093 * So, only try to manage the clock during init.
1096 clk_enable(state->clk);
1098 if (pdata->uart_init) {
1099 ret = pdata->uart_init(pdev);
1104 fiq_debugger_printf_nfiq(state,
1105 "<hit enter %sto activate fiq debugger>\n",
1106 state->no_sleep ? "" : "twice ");
1108 #ifdef CONFIG_FIQ_GLUE
1109 if (fiq_debugger_have_fiq(state)) {
1110 state->handler.fiq = fiq_debugger_fiq;
1111 state->handler.resume = fiq_debugger_resume;
1112 ret = fiq_glue_register_handler(&state->handler);
1114 pr_err("%s: could not install fiq handler\n", __func__);
1115 goto err_register_irq;
1118 pdata->fiq_enable(pdev, state->fiq, 1);
1122 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1123 IRQF_NO_SUSPEND, "debug", state);
1125 pr_err("%s: could not install irq handler\n", __func__);
1126 goto err_register_irq;
1129 /* for irq-only mode, we want this irq to wake us up, if it
1132 enable_irq_wake(state->uart_irq);
1136 clk_disable(state->clk);
1138 if (state->signal_irq >= 0) {
1139 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1140 IRQF_TRIGGER_RISING, "debug-signal", state);
1142 pr_err("serial_debugger: could not install signal_irq");
1145 if (state->wakeup_irq >= 0) {
1146 ret = request_irq(state->wakeup_irq,
1147 fiq_debugger_wakeup_irq_handler,
1148 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1149 "debug-wakeup", state);
1151 pr_err("serial_debugger: "
1152 "could not install wakeup irq\n");
1153 state->wakeup_irq = -1;
1155 ret = enable_irq_wake(state->wakeup_irq);
1157 pr_err("serial_debugger: "
1158 "could not enable wakeup\n");
1159 state->wakeup_irq_no_set_wake = true;
1163 if (state->no_sleep)
1164 fiq_debugger_handle_wakeup(state);
1166 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1167 spin_lock_init(&state->console_lock);
1168 state->console = fiq_debugger_console;
1169 state->console.index = pdev->id;
1170 if (!console_set_on_cmdline)
1171 add_preferred_console(state->console.name,
1172 state->console.index, NULL);
1173 register_console(&state->console);
1174 fiq_debugger_tty_init_one(state);
1179 if (pdata->uart_free)
1180 pdata->uart_free(pdev);
1183 clk_disable(state->clk);
1185 clk_put(state->clk);
1186 wake_lock_destroy(&state->debugger_wake_lock);
1187 platform_set_drvdata(pdev, NULL);
1192 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1193 .suspend = fiq_debugger_dev_suspend,
1194 .resume = fiq_debugger_dev_resume,
1197 static struct platform_driver fiq_debugger_driver = {
1198 .probe = fiq_debugger_probe,
1200 .name = "fiq_debugger",
1201 .pm = &fiq_debugger_dev_pm_ops,
1205 static int __init fiq_debugger_init(void)
1207 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1208 fiq_debugger_tty_init();
1210 return platform_driver_register(&fiq_debugger_driver);
1213 postcore_initcall(fiq_debugger_init);