1 /*
2 * drivers/staging/android/fiq_debugger.c
3 *
4 * Serial Debugger Interface accessed through an FIQ interrupt.
5 *
6 * Copyright (C) 2008 Google, Inc.
7 *
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.
11 *
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.
16 */
18 #include <stdarg.h>
19 #include <linux/module.h>
20 #include <linux/io.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>
40 #endif
42 #include <linux/uaccess.h>
44 #include "fiq_debugger.h"
45 #include "fiq_debugger_priv.h"
46 #include "fiq_debugger_ringbuf.h"
48 #define DEBUG_MAX 64
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;
56 #endif
57 struct fiq_debugger_output output;
59 int fiq;
60 int uart_irq;
61 int signal_irq;
62 int wakeup_irq;
63 bool wakeup_irq_no_set_wake;
64 struct clk *clk;
65 struct fiq_debugger_pdata *pdata;
66 struct platform_device *pdev;
68 char debug_cmd[DEBUG_MAX];
69 int debug_busy;
70 int debug_abort;
72 char debug_buf[DEBUG_MAX];
73 int debug_count;
75 bool no_sleep;
76 bool debug_enable;
77 bool ignore_next_wakeup_irq;
78 struct timer_list sleep_timer;
79 spinlock_t sleep_timer_lock;
80 bool uart_enabled;
81 struct wake_lock debugger_wake_lock;
82 bool console_enable;
83 int current_cpu;
84 atomic_t unhandled_fiq_count;
85 bool in_fiq;
87 struct work_struct work;
88 spinlock_t work_lock;
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;
96 bool syslog_dumping;
97 #endif
99 unsigned int last_irqs[NR_IRQS];
100 unsigned int last_local_timer_irqs[NR_CPUS];
101 };
103 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
104 struct tty_driver *fiq_tty_driver;
105 #endif
107 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
108 static bool initial_no_sleep = true;
109 #else
110 static bool initial_no_sleep;
111 #endif
113 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
114 static bool initial_debug_enable = true;
115 static bool initial_console_enable = true;
116 #else
117 static bool initial_debug_enable;
118 static bool initial_console_enable;
119 #endif
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
129 static inline
130 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
131 static inline
132 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
133 #else
134 static inline
135 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
136 {
137 if (state->wakeup_irq < 0)
138 return;
139 enable_irq(state->wakeup_irq);
140 if (!state->wakeup_irq_no_set_wake)
141 enable_irq_wake(state->wakeup_irq);
142 }
143 static inline
144 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
145 {
146 if (state->wakeup_irq < 0)
147 return;
148 disable_irq_nosync(state->wakeup_irq);
149 if (!state->wakeup_irq_no_set_wake)
150 disable_irq_wake(state->wakeup_irq);
151 }
152 #endif
154 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
155 {
156 return (state->fiq >= 0);
157 }
159 #ifdef CONFIG_FIQ_GLUE
160 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
161 {
162 unsigned int irq = state->signal_irq;
164 if (WARN_ON(!fiq_debugger_have_fiq(state)))
165 return;
166 if (state->pdata->force_irq) {
167 state->pdata->force_irq(state->pdev, irq);
168 } else {
169 struct irq_chip *chip = irq_get_chip(irq);
170 if (chip && chip->irq_retrigger)
171 chip->irq_retrigger(irq_get_irq_data(irq));
172 }
173 }
174 #endif
176 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
177 {
178 if (state->clk)
179 clk_enable(state->clk);
180 if (state->pdata->uart_enable)
181 state->pdata->uart_enable(state->pdev);
182 }
184 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
185 {
186 if (state->pdata->uart_disable)
187 state->pdata->uart_disable(state->pdev);
188 if (state->clk)
189 clk_disable(state->clk);
190 }
192 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
193 {
194 if (state->pdata->uart_flush)
195 state->pdata->uart_flush(state->pdev);
196 }
198 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
199 {
200 state->pdata->uart_putc(state->pdev, c);
201 }
203 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
204 {
205 unsigned c;
206 while ((c = *s++)) {
207 if (c == '\n')
208 fiq_debugger_putc(state, '\r');
209 fiq_debugger_putc(state, c);
210 }
211 }
213 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
214 {
215 fiq_debugger_puts(state, "debug> ");
216 }
218 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
219 {
220 char buf[512];
221 size_t len;
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)) {
228 buf[len] = 0;
229 fiq_debugger_puts(state, buf);
230 }
231 }
233 static void fiq_debugger_printf(struct fiq_debugger_output *output,
234 const char *fmt, ...)
235 {
236 struct fiq_debugger_state *state;
237 char buf[256];
238 va_list ap;
240 state = container_of(output, struct fiq_debugger_state, output);
241 va_start(ap, fmt);
242 vsnprintf(buf, sizeof(buf), fmt, ap);
243 va_end(ap);
245 fiq_debugger_puts(state, buf);
246 }
248 /* Safe outside fiq context */
249 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
250 {
251 struct fiq_debugger_state *state = cookie;
252 char buf[256];
253 va_list ap;
254 unsigned long irq_flags;
256 va_start(ap, fmt);
257 vsnprintf(buf, 128, fmt, ap);
258 va_end(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;
265 }
267 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
268 {
269 int n;
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))
277 continue;
278 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x %s\n", n,
279 kstat_irqs(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);
284 }
285 }
287 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
288 {
289 struct task_struct *g;
290 struct task_struct *p;
291 unsigned task_state;
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");
304 else
305 fiq_debugger_printf(&state->output, " %08lx\n",
306 thread_saved_pc(p));
307 } while_each_thread(g, p);
308 read_unlock(&tasklist_lock);
309 }
311 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
312 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
313 {
314 state->syslog_dumping = true;
315 }
317 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
318 {
319 state->syslog_dumping = false;
320 }
321 #else
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)
324 {
325 do_syslog(5 /* clear */, NULL, 0);
326 }
328 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
329 {
330 fiq_debugger_dump_kernel_log(state);
331 }
332 #endif
334 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
335 {
336 if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
337 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
338 return;
339 }
340 fiq_debugger_begin_syslog_dump(state);
341 handle_sysrq(rq);
342 fiq_debugger_end_syslog_dump(state);
343 }
345 #ifdef CONFIG_KGDB
346 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
347 {
348 if (!fiq_kgdb_enable) {
349 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
350 return;
351 }
353 fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
354 state->console_enable = true;
355 handle_sysrq('g');
356 }
357 #endif
359 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
360 char *cmd)
361 {
362 unsigned long flags;
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);
368 return;
369 }
371 strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
372 spin_unlock_irqrestore(&state->work_lock, flags);
374 schedule_work(&state->work);
375 }
377 static void fiq_debugger_work(struct work_struct *work)
378 {
379 struct fiq_debugger_state *state;
380 char work_cmd[DEBUG_MAX];
381 char *cmd;
382 unsigned long flags;
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);
393 cmd = work_cmd;
394 if (!strncmp(cmd, "reboot", 6)) {
395 cmd += 6;
396 while (*cmd == ' ')
397 cmd++;
398 if (cmd != '\0')
399 kernel_restart(cmd);
400 else
401 kernel_restart(NULL);
402 } else {
403 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
404 work_cmd);
405 }
406 }
408 /* This function CANNOT be called in FIQ context */
409 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
410 {
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]);
417 #ifdef CONFIG_KGDB
418 if (!strcmp(cmd, "kgdb"))
419 fiq_debugger_do_kgdb(state);
420 #endif
421 if (!strncmp(cmd, "reboot", 6))
422 fiq_debugger_schedule_work(state, cmd);
423 }
425 static void fiq_debugger_help(struct fiq_debugger_state *state)
426 {
427 fiq_debugger_printf(&state->output,
428 "FIQ Debugger commands:\n"
429 " pc PC status\n"
430 " regs Register dump\n"
431 " allregs Extended Register dump\n"
432 " bt Stack trace\n"
433 " reboot [<c>] Reboot with command <c>\n"
434 " reset [<c>] Hard reset with command <c>\n"
435 " irqs Interupt status\n"
436 " kmsg Kernel log\n"
437 " version Kernel version\n");
438 fiq_debugger_printf(&state->output,
439 " sleep Allow sleep while in FIQ\n"
440 " nosleep Disable sleep while in FIQ\n"
441 " console Switch terminal to console\n"
442 " cpu Current CPU\n"
443 " cpu <number> Switch to CPU<number>\n");
444 fiq_debugger_printf(&state->output,
445 " ps Process list\n"
446 " sysrq sysrq options\n"
447 " sysrq <param> Execute sysrq with <param>\n");
448 #ifdef CONFIG_KGDB
449 fiq_debugger_printf(&state->output,
450 " kgdb Enter kernel debugger\n");
451 #endif
452 }
454 static void fiq_debugger_take_affinity(void *info)
455 {
456 struct fiq_debugger_state *state = info;
457 struct cpumask cpumask;
459 cpumask_clear(&cpumask);
460 cpumask_set_cpu(get_cpu(), &cpumask);
462 irq_set_affinity(state->uart_irq, &cpumask);
463 }
465 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
466 {
467 if (!fiq_debugger_have_fiq(state))
468 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
469 false);
470 state->current_cpu = cpu;
471 }
473 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
474 const char *cmd, const struct pt_regs *regs,
475 void *svc_sp)
476 {
477 bool signal_helper = false;
479 if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
480 fiq_debugger_help(state);
481 } else if (!strcmp(cmd, "pc")) {
482 fiq_debugger_dump_pc(&state->output, regs);
483 } else if (!strcmp(cmd, "regs")) {
484 fiq_debugger_dump_regs(&state->output, regs);
485 } else if (!strcmp(cmd, "allregs")) {
486 fiq_debugger_dump_allregs(&state->output, regs);
487 } else if (!strcmp(cmd, "bt")) {
488 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
489 } else if (!strncmp(cmd, "reset", 5)) {
490 cmd += 5;
491 while (*cmd == ' ')
492 cmd++;
493 if (*cmd) {
494 char tmp_cmd[32];
495 strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
496 machine_restart(tmp_cmd);
497 } else {
498 machine_restart(NULL);
499 }
500 } else if (!strcmp(cmd, "irqs")) {
501 fiq_debugger_dump_irqs(state);
502 } else if (!strcmp(cmd, "kmsg")) {
503 fiq_debugger_dump_kernel_log(state);
504 } else if (!strcmp(cmd, "version")) {
505 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
506 } else if (!strcmp(cmd, "sleep")) {
507 state->no_sleep = false;
508 fiq_debugger_printf(&state->output, "enabling sleep\n");
509 } else if (!strcmp(cmd, "nosleep")) {
510 state->no_sleep = true;
511 fiq_debugger_printf(&state->output, "disabling sleep\n");
512 } else if (!strcmp(cmd, "console")) {
513 fiq_debugger_printf(&state->output, "console mode\n");
514 fiq_debugger_uart_flush(state);
515 state->console_enable = true;
516 } else if (!strcmp(cmd, "cpu")) {
517 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
518 } else if (!strncmp(cmd, "cpu ", 4)) {
519 unsigned long cpu = 0;
520 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
521 fiq_debugger_switch_cpu(state, cpu);
522 else
523 fiq_debugger_printf(&state->output, "invalid cpu\n");
524 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
525 } else {
526 if (state->debug_busy) {
527 fiq_debugger_printf(&state->output,
528 "command processor busy. trying to abort.\n");
529 state->debug_abort = -1;
530 } else {
531 strcpy(state->debug_cmd, cmd);
532 state->debug_busy = 1;
533 }
535 return true;
536 }
537 if (!state->console_enable)
538 fiq_debugger_prompt(state);
540 return signal_helper;
541 }
543 static void fiq_debugger_sleep_timer_expired(unsigned long data)
544 {
545 struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
546 unsigned long flags;
548 spin_lock_irqsave(&state->sleep_timer_lock, flags);
549 if (state->uart_enabled && !state->no_sleep) {
550 if (state->debug_enable && !state->console_enable) {
551 state->debug_enable = false;
552 fiq_debugger_printf_nfiq(state,
553 "suspending fiq debugger\n");
554 }
555 state->ignore_next_wakeup_irq = true;
556 fiq_debugger_uart_disable(state);
557 state->uart_enabled = false;
558 fiq_debugger_enable_wakeup_irq(state);
559 }
560 wake_unlock(&state->debugger_wake_lock);
561 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
562 }
564 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
565 {
566 unsigned long flags;
568 spin_lock_irqsave(&state->sleep_timer_lock, flags);
569 if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
570 state->ignore_next_wakeup_irq = false;
571 } else if (!state->uart_enabled) {
572 wake_lock(&state->debugger_wake_lock);
573 fiq_debugger_uart_enable(state);
574 state->uart_enabled = true;
575 fiq_debugger_disable_wakeup_irq(state);
576 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
577 }
578 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
579 }
581 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
582 {
583 struct fiq_debugger_state *state = dev;
585 if (!state->no_sleep)
586 fiq_debugger_puts(state, "WAKEUP\n");
587 fiq_debugger_handle_wakeup(state);
589 return IRQ_HANDLED;
590 }
592 static
593 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
594 {
595 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
596 if (state->tty_port.ops) {
597 int i;
598 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
599 for (i = 0; i < count; i++) {
600 int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
601 tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
602 if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
603 pr_warn("fiq tty failed to consume byte\n");
604 }
605 tty_flip_buffer_push(&state->tty_port);
606 }
607 #endif
608 }
610 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
611 {
612 if (!state->no_sleep) {
613 unsigned long flags;
615 spin_lock_irqsave(&state->sleep_timer_lock, flags);
616 wake_lock(&state->debugger_wake_lock);
617 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
618 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
619 }
620 fiq_debugger_handle_console_irq_context(state);
621 if (state->debug_busy) {
622 fiq_debugger_irq_exec(state, state->debug_cmd);
623 if (!state->console_enable)
624 fiq_debugger_prompt(state);
625 state->debug_busy = 0;
626 }
627 }
629 static int fiq_debugger_getc(struct fiq_debugger_state *state)
630 {
631 return state->pdata->uart_getc(state->pdev);
632 }
634 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
635 int this_cpu, const struct pt_regs *regs, void *svc_sp)
636 {
637 int c;
638 static int last_c;
639 int count = 0;
640 bool signal_helper = false;
642 if (this_cpu != state->current_cpu) {
643 if (state->in_fiq)
644 return false;
646 if (atomic_inc_return(&state->unhandled_fiq_count) !=
647 MAX_UNHANDLED_FIQ_COUNT)
648 return false;
650 fiq_debugger_printf(&state->output,
651 "fiq_debugger: cpu %d not responding, "
652 "reverting to cpu %d\n", state->current_cpu,
653 this_cpu);
655 atomic_set(&state->unhandled_fiq_count, 0);
656 fiq_debugger_switch_cpu(state, this_cpu);
657 return false;
658 }
660 state->in_fiq = true;
662 while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
663 count++;
664 if (!state->debug_enable) {
665 if ((c == 13) || (c == 10)) {
666 state->debug_enable = true;
667 state->debug_count = 0;
668 fiq_debugger_prompt(state);
669 }
670 } else if (c == FIQ_DEBUGGER_BREAK) {
671 state->console_enable = false;
672 fiq_debugger_puts(state, "fiq debugger mode\n");
673 state->debug_count = 0;
674 fiq_debugger_prompt(state);
675 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
676 } else if (state->console_enable && state->tty_rbuf) {
677 fiq_debugger_ringbuf_push(state->tty_rbuf, c);
678 signal_helper = true;
679 #endif
680 } else if ((c >= ' ') && (c < 127)) {
681 if (state->debug_count < (DEBUG_MAX - 1)) {
682 state->debug_buf[state->debug_count++] = c;
683 fiq_debugger_putc(state, c);
684 }
685 } else if ((c == 8) || (c == 127)) {
686 if (state->debug_count > 0) {
687 state->debug_count--;
688 fiq_debugger_putc(state, 8);
689 fiq_debugger_putc(state, ' ');
690 fiq_debugger_putc(state, 8);
691 }
692 } else if ((c == 13) || (c == 10)) {
693 if (c == '\r' || (c == '\n' && last_c != '\r')) {
694 fiq_debugger_putc(state, '\r');
695 fiq_debugger_putc(state, '\n');
696 }
697 if (state->debug_count) {
698 state->debug_buf[state->debug_count] = 0;
699 state->debug_count = 0;
700 signal_helper |=
701 fiq_debugger_fiq_exec(state,
702 state->debug_buf,
703 regs, svc_sp);
704 } else {
705 fiq_debugger_prompt(state);
706 }
707 }
708 last_c = c;
709 }
710 if (!state->console_enable)
711 fiq_debugger_uart_flush(state);
712 if (state->pdata->fiq_ack)
713 state->pdata->fiq_ack(state->pdev, state->fiq);
715 /* poke sleep timer if necessary */
716 if (state->debug_enable && !state->no_sleep)
717 signal_helper = true;
719 atomic_set(&state->unhandled_fiq_count, 0);
720 state->in_fiq = false;
722 return signal_helper;
723 }
725 #ifdef CONFIG_FIQ_GLUE
726 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
727 const struct pt_regs *regs, void *svc_sp)
728 {
729 struct fiq_debugger_state *state =
730 container_of(h, struct fiq_debugger_state, handler);
731 unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
732 bool need_irq;
734 need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
735 svc_sp);
736 if (need_irq)
737 fiq_debugger_force_irq(state);
738 }
739 #endif
741 /*
742 * When not using FIQs, we only use this single interrupt as an entry point.
743 * This just effectively takes over the UART interrupt and does all the work
744 * in this context.
745 */
746 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
747 {
748 struct fiq_debugger_state *state = dev;
749 bool not_done;
751 fiq_debugger_handle_wakeup(state);
753 /* handle the debugger irq in regular context */
754 not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
755 get_irq_regs(),
756 current_thread_info());
757 if (not_done)
758 fiq_debugger_handle_irq_context(state);
760 return IRQ_HANDLED;
761 }
763 /*
764 * If FIQs are used, not everything can happen in fiq context.
765 * FIQ handler does what it can and then signals this interrupt to finish the
766 * job in irq context.
767 */
768 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
769 {
770 struct fiq_debugger_state *state = dev;
772 if (state->pdata->force_irq_ack)
773 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
775 fiq_debugger_handle_irq_context(state);
777 return IRQ_HANDLED;
778 }
780 #ifdef CONFIG_FIQ_GLUE
781 static void fiq_debugger_resume(struct fiq_glue_handler *h)
782 {
783 struct fiq_debugger_state *state =
784 container_of(h, struct fiq_debugger_state, handler);
785 if (state->pdata->uart_resume)
786 state->pdata->uart_resume(state->pdev);
787 }
788 #endif
790 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
791 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
792 {
793 *index = co->index;
794 return fiq_tty_driver;
795 }
797 static void fiq_debugger_console_write(struct console *co,
798 const char *s, unsigned int count)
799 {
800 struct fiq_debugger_state *state;
801 unsigned long flags;
803 state = container_of(co, struct fiq_debugger_state, console);
805 if (!state->console_enable && !state->syslog_dumping)
806 return;
808 fiq_debugger_uart_enable(state);
809 spin_lock_irqsave(&state->console_lock, flags);
810 while (count--) {
811 if (*s == '\n')
812 fiq_debugger_putc(state, '\r');
813 fiq_debugger_putc(state, *s++);
814 }
815 fiq_debugger_uart_flush(state);
816 spin_unlock_irqrestore(&state->console_lock, flags);
817 fiq_debugger_uart_disable(state);
818 }
820 static struct console fiq_debugger_console = {
821 .name = "ttyFIQ",
822 .device = fiq_debugger_console_device,
823 .write = fiq_debugger_console_write,
824 .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
825 };
827 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
828 {
829 int line = tty->index;
830 struct fiq_debugger_state **states = tty->driver->driver_state;
831 struct fiq_debugger_state *state = states[line];
833 return tty_port_open(&state->tty_port, tty, filp);
834 }
836 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
837 {
838 tty_port_close(tty->port, tty, filp);
839 }
841 int fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
842 {
843 int i;
844 int line = tty->index;
845 struct fiq_debugger_state **states = tty->driver->driver_state;
846 struct fiq_debugger_state *state = states[line];
848 if (!state->console_enable)
849 return count;
851 fiq_debugger_uart_enable(state);
852 spin_lock_irq(&state->console_lock);
853 for (i = 0; i < count; i++)
854 fiq_debugger_putc(state, *buf++);
855 spin_unlock_irq(&state->console_lock);
856 fiq_debugger_uart_disable(state);
858 return count;
859 }
861 int fiq_tty_write_room(struct tty_struct *tty)
862 {
863 return 16;
864 }
866 #ifdef CONFIG_CONSOLE_POLL
867 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
868 {
869 return 0;
870 }
872 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
873 {
874 struct fiq_debugger_state **states = driver->driver_state;
875 struct fiq_debugger_state *state = states[line];
876 int c = NO_POLL_CHAR;
878 fiq_debugger_uart_enable(state);
879 if (fiq_debugger_have_fiq(state)) {
880 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
881 if (count > 0) {
882 c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
883 fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
884 }
885 } else {
886 c = fiq_debugger_getc(state);
887 if (c == FIQ_DEBUGGER_NO_CHAR)
888 c = NO_POLL_CHAR;
889 }
890 fiq_debugger_uart_disable(state);
892 return c;
893 }
895 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
896 {
897 struct fiq_debugger_state **states = driver->driver_state;
898 struct fiq_debugger_state *state = states[line];
899 fiq_debugger_uart_enable(state);
900 fiq_debugger_putc(state, ch);
901 fiq_debugger_uart_disable(state);
902 }
903 #endif
905 static const struct tty_port_operations fiq_tty_port_ops;
907 static const struct tty_operations fiq_tty_driver_ops = {
908 .write = fiq_tty_write,
909 .write_room = fiq_tty_write_room,
910 .open = fiq_tty_open,
911 .close = fiq_tty_close,
912 #ifdef CONFIG_CONSOLE_POLL
913 .poll_init = fiq_tty_poll_init,
914 .poll_get_char = fiq_tty_poll_get_char,
915 .poll_put_char = fiq_tty_poll_put_char,
916 #endif
917 };
919 static int fiq_debugger_tty_init(void)
920 {
921 int ret;
922 struct fiq_debugger_state **states = NULL;
924 states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
925 if (!states) {
926 pr_err("Failed to allocate fiq debugger state structres\n");
927 return -ENOMEM;
928 }
930 fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
931 if (!fiq_tty_driver) {
932 pr_err("Failed to allocate fiq debugger tty\n");
933 ret = -ENOMEM;
934 goto err_free_state;
935 }
937 fiq_tty_driver->owner = THIS_MODULE;
938 fiq_tty_driver->driver_name = "fiq-debugger";
939 fiq_tty_driver->name = "ttyFIQ";
940 fiq_tty_driver->type = TTY_DRIVER_TYPE_SERIAL;
941 fiq_tty_driver->subtype = SERIAL_TYPE_NORMAL;
942 fiq_tty_driver->init_termios = tty_std_termios;
943 fiq_tty_driver->flags = TTY_DRIVER_REAL_RAW |
944 TTY_DRIVER_DYNAMIC_DEV;
945 fiq_tty_driver->driver_state = states;
947 fiq_tty_driver->init_termios.c_cflag =
948 B115200 | CS8 | CREAD | HUPCL | CLOCAL;
949 fiq_tty_driver->init_termios.c_ispeed = 115200;
950 fiq_tty_driver->init_termios.c_ospeed = 115200;
952 tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
954 ret = tty_register_driver(fiq_tty_driver);
955 if (ret) {
956 pr_err("Failed to register fiq tty: %d\n", ret);
957 goto err_free_tty;
958 }
960 pr_info("Registered FIQ tty driver\n");
961 return 0;
963 err_free_tty:
964 put_tty_driver(fiq_tty_driver);
965 fiq_tty_driver = NULL;
966 err_free_state:
967 kfree(states);
968 return ret;
969 }
971 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
972 {
973 int ret;
974 struct device *tty_dev;
975 struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
977 states[state->pdev->id] = state;
979 state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
980 if (!state->tty_rbuf) {
981 pr_err("Failed to allocate fiq debugger ringbuf\n");
982 ret = -ENOMEM;
983 goto err;
984 }
986 tty_port_init(&state->tty_port);
987 state->tty_port.ops = &fiq_tty_port_ops;
989 tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
990 state->pdev->id, &state->pdev->dev);
991 if (IS_ERR(tty_dev)) {
992 pr_err("Failed to register fiq debugger tty device\n");
993 ret = PTR_ERR(tty_dev);
994 goto err;
995 }
997 device_set_wakeup_capable(tty_dev, 1);
999 pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1001 return 0;
1003 err:
1004 fiq_debugger_ringbuf_free(state->tty_rbuf);
1005 state->tty_rbuf = NULL;
1006 return ret;
1007 }
1008 #endif
1010 static int fiq_debugger_dev_suspend(struct device *dev)
1011 {
1012 struct platform_device *pdev = to_platform_device(dev);
1013 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1015 if (state->pdata->uart_dev_suspend)
1016 return state->pdata->uart_dev_suspend(pdev);
1017 return 0;
1018 }
1020 static int fiq_debugger_dev_resume(struct device *dev)
1021 {
1022 struct platform_device *pdev = to_platform_device(dev);
1023 struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1025 if (state->pdata->uart_dev_resume)
1026 return state->pdata->uart_dev_resume(pdev);
1027 return 0;
1028 }
1030 static int fiq_debugger_probe(struct platform_device *pdev)
1031 {
1032 int ret;
1033 struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1034 struct fiq_debugger_state *state;
1035 int fiq;
1036 int uart_irq;
1038 if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1039 return -EINVAL;
1041 if (!pdata->uart_getc || !pdata->uart_putc)
1042 return -EINVAL;
1043 if ((pdata->uart_enable && !pdata->uart_disable) ||
1044 (!pdata->uart_enable && pdata->uart_disable))
1045 return -EINVAL;
1047 fiq = platform_get_irq_byname(pdev, "fiq");
1048 uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1050 /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1051 * is required */
1052 if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1053 return -EINVAL;
1054 if (fiq >= 0 && !pdata->fiq_enable)
1055 return -EINVAL;
1057 state = kzalloc(sizeof(*state), GFP_KERNEL);
1058 state->output.printf = fiq_debugger_printf;
1059 setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1060 (unsigned long)state);
1061 state->pdata = pdata;
1062 state->pdev = pdev;
1063 state->no_sleep = initial_no_sleep;
1064 state->debug_enable = initial_debug_enable;
1065 state->console_enable = initial_console_enable;
1067 state->fiq = fiq;
1068 state->uart_irq = uart_irq;
1069 state->signal_irq = platform_get_irq_byname(pdev, "signal");
1070 state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1072 INIT_WORK(&state->work, fiq_debugger_work);
1073 spin_lock_init(&state->work_lock);
1075 platform_set_drvdata(pdev, state);
1077 spin_lock_init(&state->sleep_timer_lock);
1079 if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1080 state->no_sleep = true;
1081 state->ignore_next_wakeup_irq = !state->no_sleep;
1083 wake_lock_init(&state->debugger_wake_lock,
1084 WAKE_LOCK_SUSPEND, "serial-debug");
1086 state->clk = clk_get(&pdev->dev, NULL);
1087 if (IS_ERR(state->clk))
1088 state->clk = NULL;
1090 /* do not call pdata->uart_enable here since uart_init may still
1091 * need to do some initialization before uart_enable can work.
1092 * So, only try to manage the clock during init.
1093 */
1094 if (state->clk)
1095 clk_enable(state->clk);
1097 if (pdata->uart_init) {
1098 ret = pdata->uart_init(pdev);
1099 if (ret)
1100 goto err_uart_init;
1101 }
1103 fiq_debugger_printf_nfiq(state,
1104 "<hit enter %sto activate fiq debugger>\n",
1105 state->no_sleep ? "" : "twice ");
1107 #ifdef CONFIG_FIQ_GLUE
1108 if (fiq_debugger_have_fiq(state)) {
1109 state->handler.fiq = fiq_debugger_fiq;
1110 state->handler.resume = fiq_debugger_resume;
1111 ret = fiq_glue_register_handler(&state->handler);
1112 if (ret) {
1113 pr_err("%s: could not install fiq handler\n", __func__);
1114 goto err_register_irq;
1115 }
1117 pdata->fiq_enable(pdev, state->fiq, 1);
1118 } else
1119 #endif
1120 {
1121 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1122 IRQF_NO_SUSPEND, "debug", state);
1123 if (ret) {
1124 pr_err("%s: could not install irq handler\n", __func__);
1125 goto err_register_irq;
1126 }
1128 /* for irq-only mode, we want this irq to wake us up, if it
1129 * can.
1130 */
1131 enable_irq_wake(state->uart_irq);
1132 }
1134 if (state->clk)
1135 clk_disable(state->clk);
1137 if (state->signal_irq >= 0) {
1138 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1139 IRQF_TRIGGER_RISING, "debug-signal", state);
1140 if (ret)
1141 pr_err("serial_debugger: could not install signal_irq");
1142 }
1144 if (state->wakeup_irq >= 0) {
1145 ret = request_irq(state->wakeup_irq,
1146 fiq_debugger_wakeup_irq_handler,
1147 IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1148 "debug-wakeup", state);
1149 if (ret) {
1150 pr_err("serial_debugger: "
1151 "could not install wakeup irq\n");
1152 state->wakeup_irq = -1;
1153 } else {
1154 ret = enable_irq_wake(state->wakeup_irq);
1155 if (ret) {
1156 pr_err("serial_debugger: "
1157 "could not enable wakeup\n");
1158 state->wakeup_irq_no_set_wake = true;
1159 }
1160 }
1161 }
1162 if (state->no_sleep)
1163 fiq_debugger_handle_wakeup(state);
1165 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1166 spin_lock_init(&state->console_lock);
1167 state->console = fiq_debugger_console;
1168 state->console.index = pdev->id;
1169 if (!console_set_on_cmdline)
1170 add_preferred_console(state->console.name,
1171 state->console.index, NULL);
1172 register_console(&state->console);
1173 fiq_debugger_tty_init_one(state);
1174 #endif
1175 return 0;
1177 err_register_irq:
1178 if (pdata->uart_free)
1179 pdata->uart_free(pdev);
1180 err_uart_init:
1181 if (state->clk)
1182 clk_disable(state->clk);
1183 if (state->clk)
1184 clk_put(state->clk);
1185 wake_lock_destroy(&state->debugger_wake_lock);
1186 platform_set_drvdata(pdev, NULL);
1187 kfree(state);
1188 return ret;
1189 }
1191 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1192 .suspend = fiq_debugger_dev_suspend,
1193 .resume = fiq_debugger_dev_resume,
1194 };
1196 static struct platform_driver fiq_debugger_driver = {
1197 .probe = fiq_debugger_probe,
1198 .driver = {
1199 .name = "fiq_debugger",
1200 .pm = &fiq_debugger_dev_pm_ops,
1201 },
1202 };
1204 static int __init fiq_debugger_init(void)
1205 {
1206 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1207 fiq_debugger_tty_init();
1208 #endif
1209 return platform_driver_register(&fiq_debugger_driver);
1210 }
1212 postcore_initcall(fiq_debugger_init);