2 * drivers/staging/android/fiq_debugger.c
4 * Serial Debugger Interface accessed through an FIQ interrupt.
6 * Copyright (C) 2008 Google, Inc.
7 * Copyright (C) 2010-2015 NVIDIA Corporation. All rights reserved.
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/module.h>
22 #include <linux/console.h>
23 #include <linux/interrupt.h>
24 #include <linux/clk.h>
25 #include <linux/platform_device.h>
26 #include <linux/kernel_stat.h>
27 #include <linux/kmsg_dump.h>
28 #include <linux/irq.h>
29 #include <linux/delay.h>
30 #include <linux/reboot.h>
31 #include <linux/sched.h>
32 #include <linux/slab.h>
33 #include <linux/smp.h>
34 #include <linux/timer.h>
35 #include <linux/tty.h>
36 #include <linux/tty_flip.h>
37 #include <linux/wakelock.h>
39 #ifdef CONFIG_FIQ_GLUE
40 #include <asm/fiq_glue.h>
43 #include <linux/uaccess.h>
45 #include "fiq_debugger.h"
46 #include "fiq_debugger_priv.h"
47 #include "fiq_debugger_ringbuf.h"
50 #define MAX_UNHANDLED_FIQ_COUNT 1000000
52 #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 struct console *ram_console;
100 unsigned int last_irqs[NR_IRQS];
101 unsigned int last_local_timer_irqs[NR_CPUS];
104 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
105 struct tty_driver *fiq_tty_driver;
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 static bool fiq_kgdb_enable;
124 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
125 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
126 module_param_named(console_enable, initial_console_enable, bool, 0644);
127 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
129 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
131 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
133 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
136 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
138 if (state->wakeup_irq < 0)
140 enable_irq(state->wakeup_irq);
141 if (!state->wakeup_irq_no_set_wake)
142 enable_irq_wake(state->wakeup_irq);
145 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
147 if (state->wakeup_irq < 0)
149 disable_irq_nosync(state->wakeup_irq);
150 if (!state->wakeup_irq_no_set_wake)
151 disable_irq_wake(state->wakeup_irq);
155 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
157 return (state->fiq >= 0);
160 #ifdef CONFIG_FIQ_GLUE
161 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
163 unsigned int irq = state->signal_irq;
165 if (WARN_ON(!fiq_debugger_have_fiq(state)))
167 if (state->pdata->force_irq) {
168 state->pdata->force_irq(state->pdev, irq);
170 struct irq_chip *chip = irq_get_chip(irq);
171 if (chip && chip->irq_retrigger)
172 chip->irq_retrigger(irq_get_irq_data(irq));
177 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
180 clk_enable(state->clk);
181 if (state->pdata->uart_enable)
182 state->pdata->uart_enable(state->pdev);
185 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
187 if (state->pdata->uart_disable)
188 state->pdata->uart_disable(state->pdev);
190 clk_disable(state->clk);
193 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
195 if (state->pdata->uart_flush)
196 state->pdata->uart_flush(state->pdev);
199 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
201 state->pdata->uart_putc(state->pdev, c);
204 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
209 fiq_debugger_putc(state, '\r');
210 fiq_debugger_putc(state, c);
214 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
216 fiq_debugger_puts(state, "debug> ");
219 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
223 struct kmsg_dumper dumper = { .active = true };
226 kmsg_dump_rewind_nolock(&dumper);
227 while (kmsg_dump_get_line_nolock(&dumper, true, buf,
228 sizeof(buf) - 1, &len)) {
230 fiq_debugger_puts(state, buf);
234 static void fiq_debugger_printf(struct fiq_debugger_output *output,
235 const char *fmt, ...)
237 struct fiq_debugger_state *state;
241 state = container_of(output, struct fiq_debugger_state, output);
243 n = vsnprintf(buf, sizeof(buf), fmt, ap);
244 if (NULL != state->ram_console)
245 state->ram_console->write(state->ram_console, buf, n);
248 fiq_debugger_puts(state, buf);
251 /* Safe outside fiq context */
252 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
254 struct fiq_debugger_state *state = cookie;
257 unsigned long irq_flags;
260 vsnprintf(buf, 128, fmt, ap);
263 local_irq_save(irq_flags);
264 fiq_debugger_puts(state, buf);
265 fiq_debugger_uart_flush(state);
266 local_irq_restore(irq_flags);
267 return state->debug_abort;
270 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
273 struct irq_desc *desc;
275 fiq_debugger_printf(&state->output,
276 "irqnr total since-last status name\n");
277 for_each_irq_desc(n, desc) {
278 struct irqaction *act = desc->action;
279 if (!act && !kstat_irqs(n))
281 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x %s\n", n,
283 kstat_irqs(n) - state->last_irqs[n],
284 desc->status_use_accessors,
285 (act && act->name) ? act->name : "???");
286 state->last_irqs[n] = kstat_irqs(n);
290 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
292 struct task_struct *g;
293 struct task_struct *p;
295 static const char stat_nam[] = "RSDTtZX";
297 fiq_debugger_printf(&state->output, "pid ppid prio task pc\n");
298 read_lock(&tasklist_lock);
299 do_each_thread(g, p) {
300 task_state = p->state ? __ffs(p->state) + 1 : 0;
301 fiq_debugger_printf(&state->output,
302 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
303 fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
304 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
305 if (task_state == TASK_RUNNING)
306 fiq_debugger_printf(&state->output, " running\n");
308 fiq_debugger_printf(&state->output, " %08lx\n",
310 } while_each_thread(g, p);
311 read_unlock(&tasklist_lock);
314 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
315 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
317 state->syslog_dumping = true;
320 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
322 state->syslog_dumping = false;
325 extern int do_syslog(int type, char __user *bug, int count);
326 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
328 do_syslog(5 /* clear */, NULL, 0);
331 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
333 fiq_debugger_dump_kernel_log(state);
337 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
339 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
340 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
343 fiq_debugger_begin_syslog_dump(state);
345 fiq_debugger_end_syslog_dump(state);
349 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
351 if (!fiq_kgdb_enable) {
352 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
356 fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
357 state->console_enable = true;
362 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
367 spin_lock_irqsave(&state->work_lock, flags);
368 if (state->work_cmd[0] != '\0') {
369 fiq_debugger_printf(&state->output, "work command processor busy\n");
370 spin_unlock_irqrestore(&state->work_lock, flags);
374 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
375 spin_unlock_irqrestore(&state->work_lock, flags);
377 schedule_work(&state->work);
380 static void fiq_debugger_work(struct work_struct *work)
382 struct fiq_debugger_state *state;
383 char work_cmd[DEBUG_MAX];
387 state = container_of(work, struct fiq_debugger_state, work);
389 spin_lock_irqsave(&state->work_lock, flags);
391 strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
392 state->work_cmd[0] = '\0';
394 spin_unlock_irqrestore(&state->work_lock, flags);
397 if (!strncmp(cmd, "reboot", 6)) {
404 kernel_restart(NULL);
406 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
411 /* This function CANNOT be called in FIQ context */
412 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
414 if (!strcmp(cmd, "ps"))
415 fiq_debugger_do_ps(state);
416 if (!strcmp(cmd, "sysrq"))
417 fiq_debugger_do_sysrq(state, 'h');
418 if (!strncmp(cmd, "sysrq ", 6))
419 fiq_debugger_do_sysrq(state, cmd[6]);
421 if (!strcmp(cmd, "kgdb"))
422 fiq_debugger_do_kgdb(state);
424 if (!strncmp(cmd, "reboot", 6))
425 fiq_debugger_schedule_work(state, cmd);
428 static void fiq_debugger_help(struct fiq_debugger_state *state)
430 fiq_debugger_printf(&state->output,
431 "FIQ Debugger commands:\n"
433 " regs Register dump\n"
434 " allregs Extended Register dump\n"
436 " reboot [<c>] Reboot with command <c>\n"
437 " reset [<c>] Hard reset with command <c>\n"
438 " irqs Interupt status\n"
440 " version Kernel version\n");
441 fiq_debugger_printf(&state->output,
442 " sleep Allow sleep while in FIQ\n"
443 " nosleep Disable sleep while in FIQ\n"
444 " console Switch terminal to console\n"
446 " cpu <number> Switch to CPU<number>\n");
447 fiq_debugger_printf(&state->output,
449 " sysrq sysrq options\n"
450 " sysrq <param> Execute sysrq with <param>\n");
452 fiq_debugger_printf(&state->output,
453 " kgdb Enter kernel debugger\n");
457 static void fiq_debugger_take_affinity(void *info)
459 struct fiq_debugger_state *state = info;
460 struct cpumask cpumask;
462 cpumask_clear(&cpumask);
463 cpumask_set_cpu(get_cpu(), &cpumask);
465 irq_set_affinity(state->uart_irq, &cpumask);
468 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
470 if (!fiq_debugger_have_fiq(state))
471 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
473 state->current_cpu = cpu;
476 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
477 const char *cmd, const struct pt_regs *regs,
480 bool signal_helper = false;
482 fiq_debugger_dump_allregs(&state->output, regs);
484 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
485 fiq_debugger_help(state);
486 } else if (!strcmp(cmd, "pc")) {
487 fiq_debugger_dump_pc(&state->output, regs);
488 } else if (!strcmp(cmd, "regs")) {
489 fiq_debugger_dump_regs(&state->output, regs);
490 } else if (!strcmp(cmd, "allregs")) {
491 fiq_debugger_dump_allregs(&state->output, regs);
492 } else if (!strcmp(cmd, "bt")) {
493 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
494 } else if (!strncmp(cmd, "reset", 5)) {
500 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
501 machine_restart(tmp_cmd);
503 machine_restart(NULL);
505 } else if (!strcmp(cmd, "irqs")) {
506 fiq_debugger_dump_irqs(state);
507 } else if (!strcmp(cmd, "kmsg")) {
508 fiq_debugger_dump_kernel_log(state);
509 } else if (!strcmp(cmd, "version")) {
510 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
511 } else if (!strcmp(cmd, "sleep")) {
512 state->no_sleep = false;
513 fiq_debugger_printf(&state->output, "enabling sleep\n");
514 } else if (!strcmp(cmd, "nosleep")) {
515 state->no_sleep = true;
516 fiq_debugger_printf(&state->output, "disabling sleep\n");
517 } else if (!strcmp(cmd, "console")) {
518 fiq_debugger_printf(&state->output, "console mode\n");
519 fiq_debugger_uart_flush(state);
520 state->console_enable = true;
521 } else if (!strcmp(cmd, "cpu")) {
522 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
523 } else if (!strncmp(cmd, "cpu ", 4)) {
524 unsigned long cpu = 0;
525 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
526 fiq_debugger_switch_cpu(state, cpu);
528 fiq_debugger_printf(&state->output, "invalid cpu\n");
529 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
531 if (state->debug_busy) {
532 fiq_debugger_printf(&state->output,
533 "command processor busy. trying to abort.\n");
534 state->debug_abort = -1;
536 strcpy(state->debug_cmd, cmd);
537 state->debug_busy = 1;
542 if (!state->console_enable)
543 fiq_debugger_prompt(state);
545 return signal_helper;
548 static void fiq_debugger_sleep_timer_expired(unsigned long data)
550 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
553 spin_lock_irqsave(&state->sleep_timer_lock, flags);
554 if (state->uart_enabled && !state->no_sleep) {
555 if (state->debug_enable && !state->console_enable) {
556 state->debug_enable = false;
557 fiq_debugger_printf_nfiq(state,
558 "suspending fiq debugger\n");
560 state->ignore_next_wakeup_irq = true;
561 fiq_debugger_uart_disable(state);
562 state->uart_enabled = false;
563 fiq_debugger_enable_wakeup_irq(state);
565 wake_unlock(&state->debugger_wake_lock);
566 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
569 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
573 spin_lock_irqsave(&state->sleep_timer_lock, flags);
574 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
575 state->ignore_next_wakeup_irq = false;
576 } else if (!state->uart_enabled) {
577 wake_lock(&state->debugger_wake_lock);
578 fiq_debugger_uart_enable(state);
579 state->uart_enabled = true;
580 fiq_debugger_disable_wakeup_irq(state);
581 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
583 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
586 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
588 struct fiq_debugger_state *state = dev;
590 if (!state->no_sleep)
591 fiq_debugger_puts(state, "WAKEUP\n");
592 fiq_debugger_handle_wakeup(state);
598 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
600 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
601 if (state->tty_port.ops) {
603 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
604 for (i = 0; i < count; i++) {
605 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
606 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
607 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
608 pr_warn("fiq tty failed to consume byte\n");
610 tty_flip_buffer_push(&state->tty_port);
615 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
617 if (!state->no_sleep) {
620 spin_lock_irqsave(&state->sleep_timer_lock, flags);
621 wake_lock(&state->debugger_wake_lock);
622 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
623 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
625 fiq_debugger_handle_console_irq_context(state);
626 if (state->debug_busy) {
627 fiq_debugger_irq_exec(state, state->debug_cmd);
628 if (!state->console_enable)
629 fiq_debugger_prompt(state);
630 state->debug_busy = 0;
634 static int fiq_debugger_getc(struct fiq_debugger_state *state)
636 return state->pdata->uart_getc(state->pdev);
639 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
640 int this_cpu, const struct pt_regs *regs, void *svc_sp)
645 bool signal_helper = false;
647 if (this_cpu != state->current_cpu) {
651 if (atomic_inc_return(&state->unhandled_fiq_count) !=
652 MAX_UNHANDLED_FIQ_COUNT)
655 fiq_debugger_printf(&state->output,
656 "fiq_debugger: cpu %d not responding, "
657 "reverting to cpu %d\n", state->current_cpu,
660 atomic_set(&state->unhandled_fiq_count, 0);
661 fiq_debugger_switch_cpu(state, this_cpu);
665 state->in_fiq = true;
667 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
669 if (!state->debug_enable) {
670 if ((c == 13) || (c == 10)) {
671 state->debug_enable = true;
672 state->debug_count = 0;
673 fiq_debugger_prompt(state);
675 } else if (c == FIQ_DEBUGGER_BREAK) {
676 state->console_enable = false;
677 fiq_debugger_puts(state, "fiq debugger mode\n");
678 state->debug_count = 0;
679 fiq_debugger_prompt(state);
680 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
681 } else if (state->console_enable && state->tty_rbuf) {
682 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
683 signal_helper = true;
685 } else if ((c >= ' ') && (c < 127)) {
686 if (state->debug_count < (DEBUG_MAX - 1)) {
687 state->debug_buf[state->debug_count++] = c;
688 fiq_debugger_putc(state, c);
690 } else if ((c == 8) || (c == 127)) {
691 if (state->debug_count > 0) {
692 state->debug_count--;
693 fiq_debugger_putc(state, 8);
694 fiq_debugger_putc(state, ' ');
695 fiq_debugger_putc(state, 8);
697 } else if ((c == 13) || (c == 10)) {
698 if (c == '\r' || (c == '\n' && last_c != '\r')) {
699 fiq_debugger_putc(state, '\r');
700 fiq_debugger_putc(state, '\n');
702 if (state->debug_count) {
703 state->debug_buf[state->debug_count] = 0;
704 state->debug_count = 0;
706 fiq_debugger_fiq_exec(state,
710 fiq_debugger_prompt(state);
715 if (!state->console_enable)
716 fiq_debugger_uart_flush(state);
717 if (state->pdata->fiq_ack)
718 state->pdata->fiq_ack(state->pdev, state->fiq);
720 fiq_debugger_dump_allregs(&state->output, regs);
721 fiq_debugger_dump_pc(&state->output, regs);
722 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
723 /* poke sleep timer if necessary */
724 if (state->debug_enable && !state->no_sleep)
725 signal_helper = true;
727 atomic_set(&state->unhandled_fiq_count, 0);
728 state->in_fiq = false;
730 return signal_helper;
733 #ifdef CONFIG_FIQ_GLUE
734 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
735 const struct pt_regs *regs, void *svc_sp)
737 struct fiq_debugger_state *state =
738 container_of(h, struct fiq_debugger_state, handler);
740 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
743 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
746 fiq_debugger_force_irq(state);
751 * When not using FIQs, we only use this single interrupt as an entry point.
752 * This just effectively takes over the UART interrupt and does all the work
755 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
757 struct fiq_debugger_state *state = dev;
760 fiq_debugger_handle_wakeup(state);
762 /* handle the debugger irq in regular context */
763 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
765 current_thread_info());
767 fiq_debugger_handle_irq_context(state);
773 * If FIQs are used, not everything can happen in fiq context.
774 * FIQ handler does what it can and then signals this interrupt to finish the
775 * job in irq context.
777 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
779 struct fiq_debugger_state *state = dev;
781 if (state->pdata->force_irq_ack)
782 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
784 fiq_debugger_handle_irq_context(state);
789 #ifdef CONFIG_FIQ_GLUE
790 static void fiq_debugger_resume(struct fiq_glue_handler *h)
792 struct fiq_debugger_state *state =
793 container_of(h, struct fiq_debugger_state, handler);
794 if (state->pdata->uart_resume)
795 state->pdata->uart_resume(state->pdev);
799 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
800 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
803 return fiq_tty_driver;
806 static void fiq_debugger_console_write(struct console *co,
807 const char *s, unsigned int count)
809 struct fiq_debugger_state *state;
812 state = container_of(co, struct fiq_debugger_state, console);
814 if (!state->console_enable && !state->syslog_dumping)
817 fiq_debugger_uart_enable(state);
818 spin_lock_irqsave(&state->console_lock, flags);
821 fiq_debugger_putc(state, '\r');
822 fiq_debugger_putc(state, *s++);
824 fiq_debugger_uart_flush(state);
825 spin_unlock_irqrestore(&state->console_lock, flags);
826 fiq_debugger_uart_disable(state);
829 static struct console fiq_debugger_console = {
831 .device = fiq_debugger_console_device,
832 .write = fiq_debugger_console_write,
833 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
836 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
838 int line = tty->index;
839 struct fiq_debugger_state **states = tty->driver->driver_state;
840 struct fiq_debugger_state *state = states[line];
842 return tty_port_open(&state->tty_port, tty, filp);
845 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
847 tty_port_close(tty->port, tty, filp);
850 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
853 int line = tty->index;
854 struct fiq_debugger_state **states = tty->driver->driver_state;
855 struct fiq_debugger_state *state = states[line];
857 if (!state->console_enable)
860 fiq_debugger_uart_enable(state);
861 spin_lock_irq(&state->console_lock);
862 for (i = 0; i < count; i++)
863 fiq_debugger_putc(state, *buf++);
864 spin_unlock_irq(&state->console_lock);
865 fiq_debugger_uart_disable(state);
870 int fiq_tty_write_room(struct tty_struct *tty)
875 #ifdef CONFIG_CONSOLE_POLL
876 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
881 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
883 struct fiq_debugger_state **states = driver->driver_state;
884 struct fiq_debugger_state *state = states[line];
885 int c = NO_POLL_CHAR;
887 fiq_debugger_uart_enable(state);
888 if (fiq_debugger_have_fiq(state)) {
889 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
891 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
892 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
895 c = fiq_debugger_getc(state);
896 if (c == FIQ_DEBUGGER_NO_CHAR)
899 fiq_debugger_uart_disable(state);
904 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
906 struct fiq_debugger_state **states = driver->driver_state;
907 struct fiq_debugger_state *state = states[line];
908 fiq_debugger_uart_enable(state);
909 fiq_debugger_putc(state, ch);
910 fiq_debugger_uart_disable(state);
914 static const struct tty_port_operations fiq_tty_port_ops;
916 static const struct tty_operations fiq_tty_driver_ops = {
917 .write = fiq_tty_write,
918 .write_room = fiq_tty_write_room,
919 .open = fiq_tty_open,
920 .close = fiq_tty_close,
921 #ifdef CONFIG_CONSOLE_POLL
922 .poll_init = fiq_tty_poll_init,
923 .poll_get_char = fiq_tty_poll_get_char,
924 .poll_put_char = fiq_tty_poll_put_char,
928 static int fiq_debugger_tty_init(void)
931 struct fiq_debugger_state **states = NULL;
933 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
935 pr_err("Failed to allocate fiq debugger state structres\n");
939 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
940 if (!fiq_tty_driver) {
941 pr_err("Failed to allocate fiq debugger tty\n");
946 fiq_tty_driver->owner = THIS_MODULE;
947 fiq_tty_driver->driver_name = "fiq-debugger";
948 fiq_tty_driver->name = "ttyFIQ";
949 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
950 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
951 fiq_tty_driver->init_termios = tty_std_termios;
952 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
953 TTY_DRIVER_DYNAMIC_DEV;
954 fiq_tty_driver->driver_state = states;
956 fiq_tty_driver->init_termios.c_cflag =
957 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
958 fiq_tty_driver->init_termios.c_ispeed = 115200;
959 fiq_tty_driver->init_termios.c_ospeed = 115200;
961 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
963 ret = tty_register_driver(fiq_tty_driver);
965 pr_err("Failed to register fiq tty: %d\n", ret);
969 pr_info("Registered FIQ tty driver\n");
973 put_tty_driver(fiq_tty_driver);
974 fiq_tty_driver = NULL;
980 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
983 struct device *tty_dev;
984 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
986 states[state->pdev->id] = state;
988 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
989 if (!state->tty_rbuf) {
990 pr_err("Failed to allocate fiq debugger ringbuf\n");
995 tty_port_init(&state->tty_port);
996 state->tty_port.ops = &fiq_tty_port_ops;
998 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
999 state->pdev->id, &state->pdev->dev);
1000 if (IS_ERR(tty_dev)) {
1001 pr_err("Failed to register fiq debugger tty device\n");
1002 ret = PTR_ERR(tty_dev);
1006 device_set_wakeup_capable(tty_dev, 1);
1008 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1013 fiq_debugger_ringbuf_free(state->tty_rbuf);
1014 state->tty_rbuf = NULL;
1019 static int fiq_debugger_dev_suspend(struct device *dev)
1021 struct platform_device *pdev = to_platform_device(dev);
1022 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1024 if (state->pdata->uart_dev_suspend)
1025 return state->pdata->uart_dev_suspend(pdev);
1029 static int fiq_debugger_dev_resume(struct device *dev)
1031 struct platform_device *pdev = to_platform_device(dev);
1032 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1034 if (state->pdata->uart_dev_resume)
1035 return state->pdata->uart_dev_resume(pdev);
1039 static int fiq_debugger_probe(struct platform_device *pdev)
1042 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1043 struct fiq_debugger_state *state;
1046 struct console *console;
1047 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1050 if (!pdata->uart_getc || !pdata->uart_putc)
1052 if ((pdata->uart_enable && !pdata->uart_disable) ||
1053 (!pdata->uart_enable && pdata->uart_disable))
1056 fiq = platform_get_irq_byname(pdev, "fiq");
1057 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1059 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1061 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1063 if (fiq >= 0 && !pdata->fiq_enable)
1066 state = kzalloc(sizeof(*state), GFP_KERNEL);
1067 state->output.printf = fiq_debugger_printf;
1068 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1069 (unsigned long)state);
1070 state->pdata = pdata;
1072 state->no_sleep = initial_no_sleep;
1073 state->debug_enable = initial_debug_enable;
1074 state->console_enable = initial_console_enable;
1077 state->uart_irq = uart_irq;
1078 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1079 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1081 INIT_WORK(&state->work, fiq_debugger_work);
1082 spin_lock_init(&state->work_lock);
1084 platform_set_drvdata(pdev, state);
1086 spin_lock_init(&state->sleep_timer_lock);
1088 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1089 state->no_sleep = true;
1090 state->ignore_next_wakeup_irq = !state->no_sleep;
1092 wake_lock_init(&state->debugger_wake_lock,
1093 WAKE_LOCK_SUSPEND, "serial-debug");
1095 state->clk = clk_get(&pdev->dev, NULL);
1096 if (IS_ERR(state->clk))
1099 /* do not call pdata->uart_enable here since uart_init may still
1100 * need to do some initialization before uart_enable can work.
1101 * So, only try to manage the clock during init.
1104 clk_enable(state->clk);
1106 if (pdata->uart_init) {
1107 ret = pdata->uart_init(pdev);
1112 fiq_debugger_printf_nfiq(state,
1113 "<hit enter %sto activate fiq debugger>\n",
1114 state->no_sleep ? "" : "twice ");
1116 #ifdef CONFIG_FIQ_GLUE
1117 if (fiq_debugger_have_fiq(state)) {
1118 state->handler.fiq = fiq_debugger_fiq;
1119 state->handler.resume = fiq_debugger_resume;
1120 ret = fiq_glue_register_handler(&state->handler);
1122 pr_err("%s: could not install fiq handler\n", __func__);
1123 goto err_register_irq;
1126 pdata->fiq_enable(pdev, state->fiq, 1);
1130 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1131 IRQF_NO_SUSPEND, "debug", state);
1133 pr_err("%s: could not install irq handler\n", __func__);
1134 goto err_register_irq;
1137 /* for irq-only mode, we want this irq to wake us up, if it
1140 enable_irq_wake(state->uart_irq);
1144 clk_disable(state->clk);
1146 if (state->signal_irq >= 0) {
1147 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1148 IRQF_TRIGGER_RISING, "debug-signal", state);
1150 pr_err("serial_debugger: could not install signal_irq");
1153 if (state->wakeup_irq >= 0) {
1154 ret = request_irq(state->wakeup_irq,
1155 fiq_debugger_wakeup_irq_handler,
1156 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1157 "debug-wakeup", state);
1159 pr_err("serial_debugger: "
1160 "could not install wakeup irq\n");
1161 state->wakeup_irq = -1;
1163 ret = enable_irq_wake(state->wakeup_irq);
1165 pr_err("serial_debugger: "
1166 "could not enable wakeup\n");
1167 state->wakeup_irq_no_set_wake = true;
1171 if (state->no_sleep)
1172 fiq_debugger_handle_wakeup(state);
1174 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1175 spin_lock_init(&state->console_lock);
1176 state->console = fiq_debugger_console;
1177 state->console.index = pdev->id;
1178 if (!console_set_on_cmdline)
1179 add_preferred_console(state->console.name,
1180 state->console.index, NULL);
1181 register_console(&state->console);
1182 fiq_debugger_tty_init_one(state);
1184 for_each_console(console)
1185 if (0 == strcmp(console->name, "pstore")) {
1186 state->ram_console = console;
1193 if (pdata->uart_free)
1194 pdata->uart_free(pdev);
1197 clk_disable(state->clk);
1199 clk_put(state->clk);
1200 wake_lock_destroy(&state->debugger_wake_lock);
1201 platform_set_drvdata(pdev, NULL);
1206 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1207 .suspend = fiq_debugger_dev_suspend,
1208 .resume = fiq_debugger_dev_resume,
1211 static struct platform_driver fiq_debugger_driver = {
1212 .probe = fiq_debugger_probe,
1214 .name = "fiq_debugger",
1215 .pm = &fiq_debugger_dev_pm_ops,
1219 static int __init fiq_debugger_init(void)
1221 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1222 fiq_debugger_tty_init();
1224 return platform_driver_register(&fiq_debugger_driver);
1227 postcore_initcall(fiq_debugger_init);