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;
126 static bool fiq_debugger_disable;
128 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
129 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
130 module_param_named(console_enable, initial_console_enable, bool, 0644);
131 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
132 module_param_named(disable, fiq_debugger_disable, bool, 0644);
134 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
136 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
138 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
141 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
143 if (state->wakeup_irq < 0)
145 enable_irq(state->wakeup_irq);
146 if (!state->wakeup_irq_no_set_wake)
147 enable_irq_wake(state->wakeup_irq);
150 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
152 if (state->wakeup_irq < 0)
154 disable_irq_nosync(state->wakeup_irq);
155 if (!state->wakeup_irq_no_set_wake)
156 disable_irq_wake(state->wakeup_irq);
160 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
162 return (state->fiq >= 0);
165 #ifdef CONFIG_FIQ_GLUE
166 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
168 unsigned int irq = state->signal_irq;
170 if (WARN_ON(!fiq_debugger_have_fiq(state)))
172 if (state->pdata->force_irq) {
173 state->pdata->force_irq(state->pdev, irq);
175 struct irq_chip *chip = irq_get_chip(irq);
176 if (chip && chip->irq_retrigger)
177 chip->irq_retrigger(irq_get_irq_data(irq));
182 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
185 clk_enable(state->clk);
186 if (state->pdata->uart_enable)
187 state->pdata->uart_enable(state->pdev);
190 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
192 if (state->pdata->uart_disable)
193 state->pdata->uart_disable(state->pdev);
195 clk_disable(state->clk);
198 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
200 if (state->pdata->uart_flush)
201 state->pdata->uart_flush(state->pdev);
204 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
206 state->pdata->uart_putc(state->pdev, c);
209 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
214 fiq_debugger_putc(state, '\r');
215 fiq_debugger_putc(state, c);
219 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
221 fiq_debugger_puts(state, "debug> ");
224 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
228 struct kmsg_dumper dumper = { .active = true };
231 kmsg_dump_rewind_nolock(&dumper);
232 while (kmsg_dump_get_line_nolock(&dumper, true, buf,
233 sizeof(buf) - 1, &len)) {
235 fiq_debugger_puts(state, buf);
239 static void fiq_debugger_printf(struct fiq_debugger_output *output,
240 const char *fmt, ...)
242 struct fiq_debugger_state *state;
246 state = container_of(output, struct fiq_debugger_state, output);
248 vsnprintf(buf, sizeof(buf), fmt, ap);
251 fiq_debugger_puts(state, buf);
254 /* Safe outside fiq context */
255 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
257 struct fiq_debugger_state *state = cookie;
260 unsigned long irq_flags;
263 vsnprintf(buf, 128, fmt, ap);
266 local_irq_save(irq_flags);
267 fiq_debugger_puts(state, buf);
268 fiq_debugger_uart_flush(state);
269 local_irq_restore(irq_flags);
270 return state->debug_abort;
273 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
276 struct irq_desc *desc;
278 fiq_debugger_printf(&state->output,
279 "irqnr total since-last status name\n");
280 for_each_irq_desc(n, desc) {
281 struct irqaction *act = desc->action;
282 if (!act && !kstat_irqs(n))
284 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x %s\n", n,
286 kstat_irqs(n) - state->last_irqs[n],
287 desc->status_use_accessors,
288 (act && act->name) ? act->name : "???");
289 state->last_irqs[n] = kstat_irqs(n);
293 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
295 struct task_struct *g;
296 struct task_struct *p;
298 static const char stat_nam[] = "RSDTtZX";
300 fiq_debugger_printf(&state->output, "pid ppid prio task pc\n");
301 read_lock(&tasklist_lock);
302 do_each_thread(g, p) {
303 task_state = p->state ? __ffs(p->state) + 1 : 0;
304 fiq_debugger_printf(&state->output,
305 "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
306 fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
307 task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
308 if (task_state == TASK_RUNNING)
309 fiq_debugger_printf(&state->output, " running\n");
311 fiq_debugger_printf(&state->output, " %08lx\n",
313 } while_each_thread(g, p);
314 read_unlock(&tasklist_lock);
317 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
318 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
320 state->syslog_dumping = true;
323 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
325 state->syslog_dumping = false;
328 extern int do_syslog(int type, char __user *bug, int count);
329 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
331 do_syslog(5 /* clear */, NULL, 0);
334 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
336 fiq_debugger_dump_kernel_log(state);
340 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
342 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
343 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
346 fiq_debugger_begin_syslog_dump(state);
348 fiq_debugger_end_syslog_dump(state);
352 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
354 if (!fiq_kgdb_enable) {
355 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
359 fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
360 state->console_enable = true;
365 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
370 spin_lock_irqsave(&state->work_lock, flags);
371 if (state->work_cmd[0] != '\0') {
372 fiq_debugger_printf(&state->output, "work command processor busy\n");
373 spin_unlock_irqrestore(&state->work_lock, flags);
377 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
378 spin_unlock_irqrestore(&state->work_lock, flags);
380 schedule_work(&state->work);
383 static void fiq_debugger_work(struct work_struct *work)
385 struct fiq_debugger_state *state;
386 char work_cmd[DEBUG_MAX];
390 state = container_of(work, struct fiq_debugger_state, work);
392 spin_lock_irqsave(&state->work_lock, flags);
394 strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
395 state->work_cmd[0] = '\0';
397 spin_unlock_irqrestore(&state->work_lock, flags);
400 if (!strncmp(cmd, "reboot", 6)) {
407 kernel_restart(NULL);
409 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
414 /* This function CANNOT be called in FIQ context */
415 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
417 if (!strcmp(cmd, "ps"))
418 fiq_debugger_do_ps(state);
419 if (!strcmp(cmd, "sysrq"))
420 fiq_debugger_do_sysrq(state, 'h');
421 if (!strncmp(cmd, "sysrq ", 6))
422 fiq_debugger_do_sysrq(state, cmd[6]);
424 if (!strcmp(cmd, "kgdb"))
425 fiq_debugger_do_kgdb(state);
427 if (!strncmp(cmd, "reboot", 6))
428 fiq_debugger_schedule_work(state, cmd);
431 static void fiq_debugger_help(struct fiq_debugger_state *state)
433 fiq_debugger_printf(&state->output,
434 "FIQ Debugger commands:\n"
436 " regs Register dump\n"
437 " allregs Extended Register dump\n"
439 " reboot [<c>] Reboot with command <c>\n"
440 " reset [<c>] Hard reset with command <c>\n"
441 " irqs Interupt status\n"
443 " version Kernel version\n");
444 fiq_debugger_printf(&state->output,
445 " sleep Allow sleep while in FIQ\n"
446 " nosleep Disable sleep while in FIQ\n"
447 " console Switch terminal to console\n"
449 " cpu <number> Switch to CPU<number>\n");
450 fiq_debugger_printf(&state->output,
452 " sysrq sysrq options\n"
453 " sysrq <param> Execute sysrq with <param>\n");
455 fiq_debugger_printf(&state->output,
456 " kgdb Enter kernel debugger\n");
460 static void fiq_debugger_take_affinity(void *info)
462 struct fiq_debugger_state *state = info;
463 struct cpumask cpumask;
465 cpumask_clear(&cpumask);
466 cpumask_set_cpu(get_cpu(), &cpumask);
468 irq_set_affinity(state->uart_irq, &cpumask);
471 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
473 if (!fiq_debugger_have_fiq(state))
474 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
476 state->current_cpu = cpu;
479 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
480 const char *cmd, const struct pt_regs *regs,
483 bool signal_helper = false;
485 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
486 fiq_debugger_help(state);
487 } else if (!strcmp(cmd, "pc")) {
488 fiq_debugger_dump_pc(&state->output, regs);
489 } else if (!strcmp(cmd, "regs")) {
490 fiq_debugger_dump_regs(&state->output, regs);
491 } else if (!strcmp(cmd, "allregs")) {
492 fiq_debugger_dump_allregs(&state->output, regs);
493 } else if (!strcmp(cmd, "bt")) {
494 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
495 } else if (!strncmp(cmd, "reset", 5)) {
501 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
502 blocking_notifier_call_chain(&reboot_notifier_list,
503 SYS_RESTART, (char *)cmd);
504 machine_restart(tmp_cmd);
506 machine_restart(NULL);
508 } else if (!strcmp(cmd, "irqs")) {
509 fiq_debugger_dump_irqs(state);
510 } else if (!strcmp(cmd, "kmsg")) {
511 fiq_debugger_dump_kernel_log(state);
512 } else if (!strcmp(cmd, "version")) {
513 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
514 } else if (!strcmp(cmd, "sleep")) {
515 state->no_sleep = false;
516 fiq_debugger_printf(&state->output, "enabling sleep\n");
517 } else if (!strcmp(cmd, "nosleep")) {
518 state->no_sleep = true;
519 fiq_debugger_printf(&state->output, "disabling sleep\n");
520 } else if (!strcmp(cmd, "console")) {
521 fiq_debugger_printf(&state->output, "console mode\n");
522 fiq_debugger_uart_flush(state);
523 state->console_enable = true;
524 } else if (!strcmp(cmd, "cpu")) {
525 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
526 } else if (!strncmp(cmd, "cpu ", 4)) {
527 unsigned long cpu = 0;
528 if (kstrtoul(cmd + 4, 10, &cpu) == 0)
529 fiq_debugger_switch_cpu(state, cpu);
531 fiq_debugger_printf(&state->output, "invalid cpu\n");
532 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
534 if (state->debug_busy) {
535 fiq_debugger_printf(&state->output,
536 "command processor busy. trying to abort.\n");
537 state->debug_abort = -1;
539 strcpy(state->debug_cmd, cmd);
540 state->debug_busy = 1;
545 if (!state->console_enable)
546 fiq_debugger_prompt(state);
548 return signal_helper;
551 static void fiq_debugger_sleep_timer_expired(unsigned long data)
553 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
556 spin_lock_irqsave(&state->sleep_timer_lock, flags);
557 if (state->uart_enabled && !state->no_sleep) {
558 if (state->debug_enable && !state->console_enable) {
559 state->debug_enable = false;
560 fiq_debugger_printf_nfiq(state,
561 "suspending fiq debugger\n");
563 state->ignore_next_wakeup_irq = true;
564 fiq_debugger_uart_disable(state);
565 state->uart_enabled = false;
566 fiq_debugger_enable_wakeup_irq(state);
568 wake_unlock(&state->debugger_wake_lock);
569 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
572 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
576 spin_lock_irqsave(&state->sleep_timer_lock, flags);
577 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
578 state->ignore_next_wakeup_irq = false;
579 } else if (!state->uart_enabled) {
580 wake_lock(&state->debugger_wake_lock);
581 fiq_debugger_uart_enable(state);
582 state->uart_enabled = true;
583 fiq_debugger_disable_wakeup_irq(state);
584 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
586 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
589 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
591 struct fiq_debugger_state *state = dev;
593 if (!state->no_sleep)
594 fiq_debugger_puts(state, "WAKEUP\n");
595 fiq_debugger_handle_wakeup(state);
601 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
603 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
604 if (state->tty_port.ops) {
606 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
607 for (i = 0; i < count; i++) {
608 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
609 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
610 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
611 pr_warn("fiq tty failed to consume byte\n");
613 tty_flip_buffer_push(&state->tty_port);
618 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
620 if (!state->no_sleep) {
623 spin_lock_irqsave(&state->sleep_timer_lock, flags);
624 wake_lock(&state->debugger_wake_lock);
625 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
626 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
628 fiq_debugger_handle_console_irq_context(state);
629 if (state->debug_busy) {
630 fiq_debugger_irq_exec(state, state->debug_cmd);
631 if (!state->console_enable)
632 fiq_debugger_prompt(state);
633 state->debug_busy = 0;
637 static int fiq_debugger_getc(struct fiq_debugger_state *state)
639 return state->pdata->uart_getc(state->pdev);
642 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
643 int this_cpu, const struct pt_regs *regs, void *svc_sp)
648 bool signal_helper = false;
650 if ((this_cpu != state->current_cpu) && (cpu_online(state->current_cpu))) {
654 if (atomic_inc_return(&state->unhandled_fiq_count) !=
655 MAX_UNHANDLED_FIQ_COUNT)
658 fiq_debugger_printf(&state->output,
659 "fiq_debugger: cpu %d not responding, "
660 "reverting to cpu %d\n", state->current_cpu,
663 atomic_set(&state->unhandled_fiq_count, 0);
664 fiq_debugger_switch_cpu(state, this_cpu);
668 if (this_cpu != state->current_cpu)
669 state->current_cpu = this_cpu;
671 state->in_fiq = true;
673 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
675 if (!state->debug_enable) {
676 if ((c == 13) || (c == 10)) {
677 state->debug_enable = true;
678 state->debug_count = 0;
679 fiq_debugger_prompt(state);
681 } else if (c == FIQ_DEBUGGER_BREAK) {
682 state->console_enable = false;
683 fiq_debugger_puts(state, "fiq debugger mode\n");
684 state->debug_count = 0;
685 fiq_debugger_prompt(state);
686 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
687 } else if (state->console_enable && state->tty_rbuf) {
688 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
689 signal_helper = true;
691 } else if ((c >= ' ') && (c < 127)) {
692 if (state->debug_count < (DEBUG_MAX - 1)) {
693 state->debug_buf[state->debug_count++] = c;
694 fiq_debugger_putc(state, c);
696 } else if ((c == 8) || (c == 127)) {
697 if (state->debug_count > 0) {
698 state->debug_count--;
699 fiq_debugger_putc(state, 8);
700 fiq_debugger_putc(state, ' ');
701 fiq_debugger_putc(state, 8);
703 } else if ((c == 13) || (c == 10)) {
704 if (c == '\r' || (c == '\n' && last_c != '\r')) {
705 fiq_debugger_putc(state, '\r');
706 fiq_debugger_putc(state, '\n');
708 if (state->debug_count) {
709 state->debug_buf[state->debug_count] = 0;
710 state->debug_count = 0;
712 fiq_debugger_fiq_exec(state,
716 fiq_debugger_prompt(state);
721 if (!state->console_enable)
722 fiq_debugger_uart_flush(state);
723 if (state->pdata->fiq_ack)
724 state->pdata->fiq_ack(state->pdev, state->fiq);
726 /* poke sleep timer if necessary */
727 if (state->debug_enable && !state->no_sleep)
728 signal_helper = true;
730 atomic_set(&state->unhandled_fiq_count, 0);
731 state->in_fiq = false;
733 return signal_helper;
736 #ifdef CONFIG_FIQ_GLUE
737 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
738 const struct pt_regs *regs, void *svc_sp)
740 struct fiq_debugger_state *state =
741 container_of(h, struct fiq_debugger_state, handler);
742 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
745 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
748 fiq_debugger_force_irq(state);
753 * When not using FIQs, we only use this single interrupt as an entry point.
754 * This just effectively takes over the UART interrupt and does all the work
757 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
759 struct fiq_debugger_state *state = dev;
762 fiq_debugger_handle_wakeup(state);
764 /* handle the debugger irq in regular context */
765 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
767 current_thread_info());
769 fiq_debugger_handle_irq_context(state);
775 * If FIQs are used, not everything can happen in fiq context.
776 * FIQ handler does what it can and then signals this interrupt to finish the
777 * job in irq context.
779 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
781 struct fiq_debugger_state *state = dev;
783 if (state->pdata->force_irq_ack)
784 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
786 fiq_debugger_handle_irq_context(state);
791 #ifdef CONFIG_FIQ_GLUE
792 static void fiq_debugger_resume(struct fiq_glue_handler *h)
794 struct fiq_debugger_state *state =
795 container_of(h, struct fiq_debugger_state, handler);
796 if (state->pdata->uart_resume)
797 state->pdata->uart_resume(state->pdev);
801 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
802 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
805 return fiq_tty_driver;
808 static void fiq_debugger_console_write(struct console *co,
809 const char *s, unsigned int count)
811 struct fiq_debugger_state *state;
814 state = container_of(co, struct fiq_debugger_state, console);
816 if (!state->console_enable && !state->syslog_dumping)
819 #ifdef CONFIG_RK_CONSOLE_THREAD
820 if (state->pdata->console_write) {
821 state->pdata->console_write(state->pdev, s, count);
826 fiq_debugger_uart_enable(state);
827 spin_lock_irqsave(&state->console_lock, flags);
830 fiq_debugger_putc(state, '\r');
831 fiq_debugger_putc(state, *s++);
833 fiq_debugger_uart_flush(state);
834 spin_unlock_irqrestore(&state->console_lock, flags);
835 fiq_debugger_uart_disable(state);
838 static struct console fiq_debugger_console = {
840 .device = fiq_debugger_console_device,
841 .write = fiq_debugger_console_write,
842 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
845 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
847 int line = tty->index;
848 struct fiq_debugger_state **states = tty->driver->driver_state;
849 struct fiq_debugger_state *state = states[line];
851 return tty_port_open(&state->tty_port, tty, filp);
854 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
856 tty_port_close(tty->port, tty, filp);
859 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
862 int line = tty->index;
863 struct fiq_debugger_state **states = tty->driver->driver_state;
864 struct fiq_debugger_state *state = states[line];
866 if (!state->console_enable)
869 fiq_debugger_uart_enable(state);
870 spin_lock_irq(&state->console_lock);
871 for (i = 0; i < count; i++)
872 fiq_debugger_putc(state, *buf++);
873 spin_unlock_irq(&state->console_lock);
874 fiq_debugger_uart_disable(state);
879 int fiq_tty_write_room(struct tty_struct *tty)
884 #ifdef CONFIG_CONSOLE_POLL
885 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
890 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
892 struct fiq_debugger_state **states = driver->driver_state;
893 struct fiq_debugger_state *state = states[line];
894 int c = NO_POLL_CHAR;
896 fiq_debugger_uart_enable(state);
897 if (fiq_debugger_have_fiq(state)) {
898 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
900 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
901 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
904 c = fiq_debugger_getc(state);
905 if (c == FIQ_DEBUGGER_NO_CHAR)
908 fiq_debugger_uart_disable(state);
913 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
915 struct fiq_debugger_state **states = driver->driver_state;
916 struct fiq_debugger_state *state = states[line];
917 fiq_debugger_uart_enable(state);
918 fiq_debugger_putc(state, ch);
919 fiq_debugger_uart_disable(state);
923 static const struct tty_port_operations fiq_tty_port_ops;
925 static const struct tty_operations fiq_tty_driver_ops = {
926 .write = fiq_tty_write,
927 .write_room = fiq_tty_write_room,
928 .open = fiq_tty_open,
929 .close = fiq_tty_close,
930 #ifdef CONFIG_CONSOLE_POLL
931 .poll_init = fiq_tty_poll_init,
932 .poll_get_char = fiq_tty_poll_get_char,
933 .poll_put_char = fiq_tty_poll_put_char,
937 static int fiq_debugger_tty_init(void)
940 struct fiq_debugger_state **states = NULL;
942 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
944 pr_err("Failed to allocate fiq debugger state structres\n");
948 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
949 if (!fiq_tty_driver) {
950 pr_err("Failed to allocate fiq debugger tty\n");
955 fiq_tty_driver->owner = THIS_MODULE;
956 fiq_tty_driver->driver_name = "fiq-debugger";
957 fiq_tty_driver->name = "ttyFIQ";
958 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
959 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
960 fiq_tty_driver->init_termios = tty_std_termios;
961 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
962 TTY_DRIVER_DYNAMIC_DEV;
963 fiq_tty_driver->driver_state = states;
965 fiq_tty_driver->init_termios.c_cflag =
966 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
967 fiq_tty_driver->init_termios.c_ispeed = 115200;
968 fiq_tty_driver->init_termios.c_ospeed = 115200;
970 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
972 ret = tty_register_driver(fiq_tty_driver);
974 pr_err("Failed to register fiq tty: %d\n", ret);
978 pr_info("Registered FIQ tty driver\n");
982 put_tty_driver(fiq_tty_driver);
983 fiq_tty_driver = NULL;
989 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
992 struct device *tty_dev;
993 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
995 states[state->pdev->id] = state;
997 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
998 if (!state->tty_rbuf) {
999 pr_err("Failed to allocate fiq debugger ringbuf\n");
1004 tty_port_init(&state->tty_port);
1005 state->tty_port.ops = &fiq_tty_port_ops;
1007 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1008 state->pdev->id, &state->pdev->dev);
1009 if (IS_ERR(tty_dev)) {
1010 pr_err("Failed to register fiq debugger tty device\n");
1011 ret = PTR_ERR(tty_dev);
1015 device_set_wakeup_capable(tty_dev, 1);
1017 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1022 fiq_debugger_ringbuf_free(state->tty_rbuf);
1023 state->tty_rbuf = NULL;
1028 static int fiq_debugger_dev_suspend(struct device *dev)
1030 struct platform_device *pdev = to_platform_device(dev);
1031 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1033 if (state->pdata->uart_dev_suspend)
1034 return state->pdata->uart_dev_suspend(pdev);
1038 static int fiq_debugger_dev_resume(struct device *dev)
1040 struct platform_device *pdev = to_platform_device(dev);
1041 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1043 if (state->pdata->uart_dev_resume)
1044 return state->pdata->uart_dev_resume(pdev);
1048 static int fiq_debugger_probe(struct platform_device *pdev)
1051 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1052 struct fiq_debugger_state *state;
1056 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1059 if (!pdata->uart_getc || !pdata->uart_putc)
1061 if ((pdata->uart_enable && !pdata->uart_disable) ||
1062 (!pdata->uart_enable && pdata->uart_disable))
1065 fiq = platform_get_irq_byname(pdev, "fiq");
1066 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1068 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1070 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1072 if (fiq >= 0 && !pdata->fiq_enable)
1075 state = kzalloc(sizeof(*state), GFP_KERNEL);
1076 state->output.printf = fiq_debugger_printf;
1077 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1078 (unsigned long)state);
1079 state->pdata = pdata;
1081 state->no_sleep = initial_no_sleep;
1082 state->debug_enable = initial_debug_enable;
1083 state->console_enable = initial_console_enable;
1086 state->uart_irq = uart_irq;
1087 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1088 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1090 INIT_WORK(&state->work, fiq_debugger_work);
1091 spin_lock_init(&state->work_lock);
1093 platform_set_drvdata(pdev, state);
1095 spin_lock_init(&state->sleep_timer_lock);
1097 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1098 state->no_sleep = true;
1099 state->ignore_next_wakeup_irq = !state->no_sleep;
1101 wake_lock_init(&state->debugger_wake_lock,
1102 WAKE_LOCK_SUSPEND, "serial-debug");
1104 state->clk = clk_get(&pdev->dev, NULL);
1105 if (IS_ERR(state->clk))
1108 /* do not call pdata->uart_enable here since uart_init may still
1109 * need to do some initialization before uart_enable can work.
1110 * So, only try to manage the clock during init.
1113 clk_enable(state->clk);
1115 if (pdata->uart_init) {
1116 ret = pdata->uart_init(pdev);
1121 fiq_debugger_printf_nfiq(state,
1122 "<hit enter %sto activate fiq debugger>\n",
1123 state->no_sleep ? "" : "twice ");
1125 #ifdef CONFIG_FIQ_GLUE
1126 if (fiq_debugger_have_fiq(state)) {
1127 state->handler.fiq = fiq_debugger_fiq;
1128 state->handler.resume = fiq_debugger_resume;
1129 ret = fiq_glue_register_handler(&state->handler);
1131 pr_err("%s: could not install fiq handler\n", __func__);
1132 goto err_register_irq;
1135 pdata->fiq_enable(pdev, state->fiq, 1);
1139 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1140 IRQF_NO_SUSPEND, "debug", state);
1142 pr_err("%s: could not install irq handler\n", __func__);
1143 goto err_register_irq;
1146 /* for irq-only mode, we want this irq to wake us up, if it
1149 enable_irq_wake(state->uart_irq);
1153 clk_disable(state->clk);
1155 if (state->signal_irq >= 0) {
1156 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1157 IRQF_TRIGGER_RISING, "debug-signal", state);
1159 pr_err("serial_debugger: could not install signal_irq");
1162 if (state->wakeup_irq >= 0) {
1163 ret = request_irq(state->wakeup_irq,
1164 fiq_debugger_wakeup_irq_handler,
1165 IRQF_TRIGGER_FALLING,
1166 "debug-wakeup", state);
1168 pr_err("serial_debugger: "
1169 "could not install wakeup irq\n");
1170 state->wakeup_irq = -1;
1172 ret = enable_irq_wake(state->wakeup_irq);
1174 pr_err("serial_debugger: "
1175 "could not enable wakeup\n");
1176 state->wakeup_irq_no_set_wake = true;
1180 if (state->no_sleep)
1181 fiq_debugger_handle_wakeup(state);
1183 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1184 spin_lock_init(&state->console_lock);
1185 state->console = fiq_debugger_console;
1186 state->console.index = pdev->id;
1187 if (!console_set_on_cmdline)
1188 add_preferred_console(state->console.name,
1189 state->console.index, NULL);
1190 register_console(&state->console);
1191 fiq_debugger_tty_init_one(state);
1196 if (pdata->uart_free)
1197 pdata->uart_free(pdev);
1200 clk_disable(state->clk);
1202 clk_put(state->clk);
1203 wake_lock_destroy(&state->debugger_wake_lock);
1204 platform_set_drvdata(pdev, NULL);
1209 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1210 .suspend = fiq_debugger_dev_suspend,
1211 .resume = fiq_debugger_dev_resume,
1214 static struct platform_driver fiq_debugger_driver = {
1215 .probe = fiq_debugger_probe,
1217 .name = "fiq_debugger",
1218 .pm = &fiq_debugger_dev_pm_ops,
1222 #if defined(CONFIG_FIQ_DEBUGGER_UART_OVERLAY)
1223 int fiq_debugger_uart_overlay(void)
1225 struct device_node *onp = of_find_node_by_path("/uart_overlay@0");
1229 pr_err("serial_debugger: uart overlay not found\n");
1233 ret = of_overlay_create(onp);
1235 pr_err("serial_debugger: fail to create overlay: %d\n", ret);
1240 pr_info("serial_debugger: uart overlay applied\n");
1245 static int __init fiq_debugger_init(void)
1247 if (fiq_debugger_disable) {
1248 pr_err("serial_debugger: disabled\n");
1251 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1252 fiq_debugger_tty_init();
1254 #if defined(CONFIG_FIQ_DEBUGGER_UART_OVERLAY)
1255 fiq_debugger_uart_overlay();
1257 return platform_driver_register(&fiq_debugger_driver);
1260 postcore_initcall(fiq_debugger_init);