fiq_debugger: use pt_regs for registers
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / fiq_debugger / fiq_debugger.c
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  */
17
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>
37
38 #ifdef CONFIG_FIQ_GLUE
39 #include <asm/fiq_glue.h>
40 #endif
41 #include <asm/stacktrace.h>
42
43 #include <linux/uaccess.h>
44
45 #include "fiq_debugger.h"
46 #include "fiq_debugger_ringbuf.h"
47
48 #define DEBUG_MAX 64
49 #define MAX_UNHANDLED_FIQ_COUNT 1000000
50
51 #define MAX_FIQ_DEBUGGER_PORTS 4
52
53 #define THREAD_INFO(sp) ((struct thread_info *) \
54                 ((unsigned long)(sp) & ~(THREAD_SIZE - 1)))
55
56 struct fiq_debugger_state {
57 #ifdef CONFIG_FIQ_GLUE
58         struct fiq_glue_handler handler;
59 #endif
60
61         int fiq;
62         int uart_irq;
63         int signal_irq;
64         int wakeup_irq;
65         bool wakeup_irq_no_set_wake;
66         struct clk *clk;
67         struct fiq_debugger_pdata *pdata;
68         struct platform_device *pdev;
69
70         char debug_cmd[DEBUG_MAX];
71         int debug_busy;
72         int debug_abort;
73
74         char debug_buf[DEBUG_MAX];
75         int debug_count;
76
77         bool no_sleep;
78         bool debug_enable;
79         bool ignore_next_wakeup_irq;
80         struct timer_list sleep_timer;
81         spinlock_t sleep_timer_lock;
82         bool uart_enabled;
83         struct wake_lock debugger_wake_lock;
84         bool console_enable;
85         int current_cpu;
86         atomic_t unhandled_fiq_count;
87         bool in_fiq;
88
89         struct work_struct work;
90         spinlock_t work_lock;
91         char work_cmd[DEBUG_MAX];
92
93 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
94         spinlock_t console_lock;
95         struct console console;
96         struct tty_port tty_port;
97         struct fiq_debugger_ringbuf *tty_rbuf;
98         bool syslog_dumping;
99 #endif
100
101         unsigned int last_irqs[NR_IRQS];
102         unsigned int last_local_timer_irqs[NR_CPUS];
103 };
104
105 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
106 struct tty_driver *fiq_tty_driver;
107 #endif
108
109 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
110 static bool initial_no_sleep = true;
111 #else
112 static bool initial_no_sleep;
113 #endif
114
115 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
116 static bool initial_debug_enable = true;
117 static bool initial_console_enable = true;
118 #else
119 static bool initial_debug_enable;
120 static bool initial_console_enable;
121 #endif
122
123 static bool fiq_kgdb_enable;
124
125 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
126 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
127 module_param_named(console_enable, initial_console_enable, bool, 0644);
128 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
129
130 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
131 static inline
132 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
133 static inline
134 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
135 #else
136 static inline
137 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
138 {
139         if (state->wakeup_irq < 0)
140                 return;
141         enable_irq(state->wakeup_irq);
142         if (!state->wakeup_irq_no_set_wake)
143                 enable_irq_wake(state->wakeup_irq);
144 }
145 static inline
146 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
147 {
148         if (state->wakeup_irq < 0)
149                 return;
150         disable_irq_nosync(state->wakeup_irq);
151         if (!state->wakeup_irq_no_set_wake)
152                 disable_irq_wake(state->wakeup_irq);
153 }
154 #endif
155
156 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
157 {
158         return (state->fiq >= 0);
159 }
160
161 #ifdef CONFIG_FIQ_GLUE
162 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
163 {
164         unsigned int irq = state->signal_irq;
165
166         if (WARN_ON(!fiq_debugger_have_fiq(state)))
167                 return;
168         if (state->pdata->force_irq) {
169                 state->pdata->force_irq(state->pdev, irq);
170         } else {
171                 struct irq_chip *chip = irq_get_chip(irq);
172                 if (chip && chip->irq_retrigger)
173                         chip->irq_retrigger(irq_get_irq_data(irq));
174         }
175 }
176 #endif
177
178 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
179 {
180         if (state->clk)
181                 clk_enable(state->clk);
182         if (state->pdata->uart_enable)
183                 state->pdata->uart_enable(state->pdev);
184 }
185
186 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
187 {
188         if (state->pdata->uart_disable)
189                 state->pdata->uart_disable(state->pdev);
190         if (state->clk)
191                 clk_disable(state->clk);
192 }
193
194 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
195 {
196         if (state->pdata->uart_flush)
197                 state->pdata->uart_flush(state->pdev);
198 }
199
200 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
201 {
202         state->pdata->uart_putc(state->pdev, c);
203 }
204
205 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
206 {
207         unsigned c;
208         while ((c = *s++)) {
209                 if (c == '\n')
210                         fiq_debugger_putc(state, '\r');
211                 fiq_debugger_putc(state, c);
212         }
213 }
214
215 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
216 {
217         fiq_debugger_puts(state, "debug> ");
218 }
219
220 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
221 {
222         char buf[512];
223         size_t len;
224         struct kmsg_dumper dumper = { .active = true };
225
226
227         kmsg_dump_rewind_nolock(&dumper);
228         while (kmsg_dump_get_line_nolock(&dumper, true, buf,
229                                          sizeof(buf) - 1, &len)) {
230                 buf[len] = 0;
231                 fiq_debugger_puts(state, buf);
232         }
233 }
234
235 static char *mode_name(unsigned cpsr)
236 {
237         switch (cpsr & MODE_MASK) {
238         case USR_MODE: return "USR";
239         case FIQ_MODE: return "FIQ";
240         case IRQ_MODE: return "IRQ";
241         case SVC_MODE: return "SVC";
242         case ABT_MODE: return "ABT";
243         case UND_MODE: return "UND";
244         case SYSTEM_MODE: return "SYS";
245         default: return "???";
246         }
247 }
248
249 static int fiq_debugger_printf(void *cookie, const char *fmt, ...)
250 {
251         struct fiq_debugger_state *state = cookie;
252         char buf[256];
253         va_list ap;
254
255         va_start(ap, fmt);
256         vsnprintf(buf, sizeof(buf), fmt, ap);
257         va_end(ap);
258
259         fiq_debugger_puts(state, buf);
260         return state->debug_abort;
261 }
262
263 /* Safe outside fiq context */
264 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
265 {
266         struct fiq_debugger_state *state = cookie;
267         char buf[256];
268         va_list ap;
269         unsigned long irq_flags;
270
271         va_start(ap, fmt);
272         vsnprintf(buf, 128, fmt, ap);
273         va_end(ap);
274
275         local_irq_save(irq_flags);
276         fiq_debugger_puts(state, buf);
277         fiq_debugger_uart_flush(state);
278         local_irq_restore(irq_flags);
279         return state->debug_abort;
280 }
281
282 static void fiq_debugger_dump_regs(struct fiq_debugger_state *state,
283                 const struct pt_regs *regs)
284 {
285         fiq_debugger_printf(state,
286                         " r0 %08x  r1 %08x  r2 %08x  r3 %08x\n",
287                         regs->ARM_r0, regs->ARM_r1, regs->ARM_r2, regs->ARM_r3);
288         fiq_debugger_printf(state,
289                         " r4 %08x  r5 %08x  r6 %08x  r7 %08x\n",
290                         regs->ARM_r4, regs->ARM_r5, regs->ARM_r6, regs->ARM_r7);
291         fiq_debugger_printf(state,
292                         " r8 %08x  r9 %08x r10 %08x r11 %08x  mode %s\n",
293                         regs->ARM_r8, regs->ARM_r9, regs->ARM_r10, regs->ARM_fp,
294                         mode_name(regs->ARM_cpsr));
295         fiq_debugger_printf(state,
296                         " ip %08x  sp %08x  lr %08x  pc %08x cpsr %08x\n",
297                         regs->ARM_ip, regs->ARM_sp, regs->ARM_lr, regs->ARM_pc,
298                         regs->ARM_cpsr);
299 }
300
301 struct mode_regs {
302         unsigned long sp_svc;
303         unsigned long lr_svc;
304         unsigned long spsr_svc;
305
306         unsigned long sp_abt;
307         unsigned long lr_abt;
308         unsigned long spsr_abt;
309
310         unsigned long sp_und;
311         unsigned long lr_und;
312         unsigned long spsr_und;
313
314         unsigned long sp_irq;
315         unsigned long lr_irq;
316         unsigned long spsr_irq;
317
318         unsigned long r8_fiq;
319         unsigned long r9_fiq;
320         unsigned long r10_fiq;
321         unsigned long r11_fiq;
322         unsigned long r12_fiq;
323         unsigned long sp_fiq;
324         unsigned long lr_fiq;
325         unsigned long spsr_fiq;
326 };
327
328 void __naked get_mode_regs(struct mode_regs *regs)
329 {
330         asm volatile (
331         "mrs    r1, cpsr\n"
332         "msr    cpsr_c, #0xd3 @(SVC_MODE | PSR_I_BIT | PSR_F_BIT)\n"
333         "stmia  r0!, {r13 - r14}\n"
334         "mrs    r2, spsr\n"
335         "msr    cpsr_c, #0xd7 @(ABT_MODE | PSR_I_BIT | PSR_F_BIT)\n"
336         "stmia  r0!, {r2, r13 - r14}\n"
337         "mrs    r2, spsr\n"
338         "msr    cpsr_c, #0xdb @(UND_MODE | PSR_I_BIT | PSR_F_BIT)\n"
339         "stmia  r0!, {r2, r13 - r14}\n"
340         "mrs    r2, spsr\n"
341         "msr    cpsr_c, #0xd2 @(IRQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
342         "stmia  r0!, {r2, r13 - r14}\n"
343         "mrs    r2, spsr\n"
344         "msr    cpsr_c, #0xd1 @(FIQ_MODE | PSR_I_BIT | PSR_F_BIT)\n"
345         "stmia  r0!, {r2, r8 - r14}\n"
346         "mrs    r2, spsr\n"
347         "stmia  r0!, {r2}\n"
348         "msr    cpsr_c, r1\n"
349         "bx     lr\n");
350 }
351
352
353 static void fiq_debugger_dump_allregs(struct fiq_debugger_state *state,
354                 const struct pt_regs *regs)
355 {
356         struct mode_regs mode_regs;
357         unsigned long mode = regs->ARM_cpsr & MODE_MASK;
358
359         fiq_debugger_dump_regs(state, regs);
360         get_mode_regs(&mode_regs);
361
362         fiq_debugger_printf(state,
363                         "%csvc: sp %08x  lr %08x  spsr %08x\n",
364                         mode == SVC_MODE ? '*' : ' ',
365                         mode_regs.sp_svc, mode_regs.lr_svc, mode_regs.spsr_svc);
366         fiq_debugger_printf(state,
367                         "%cabt: sp %08x  lr %08x  spsr %08x\n",
368                         mode == ABT_MODE ? '*' : ' ',
369                         mode_regs.sp_abt, mode_regs.lr_abt, mode_regs.spsr_abt);
370         fiq_debugger_printf(state,
371                         "%cund: sp %08x  lr %08x  spsr %08x\n",
372                         mode == UND_MODE ? '*' : ' ',
373                         mode_regs.sp_und, mode_regs.lr_und, mode_regs.spsr_und);
374         fiq_debugger_printf(state,
375                         "%cirq: sp %08x  lr %08x  spsr %08x\n",
376                         mode == IRQ_MODE ? '*' : ' ',
377                         mode_regs.sp_irq, mode_regs.lr_irq, mode_regs.spsr_irq);
378         fiq_debugger_printf(state,
379                         "%cfiq: r8 %08x  r9 %08x  r10 %08x  r11 %08x  r12 %08x\n",
380                         mode == FIQ_MODE ? '*' : ' ',
381                         mode_regs.r8_fiq, mode_regs.r9_fiq, mode_regs.r10_fiq,
382                         mode_regs.r11_fiq, mode_regs.r12_fiq);
383         fiq_debugger_printf(state,
384                         " fiq: sp %08x  lr %08x  spsr %08x\n",
385                         mode_regs.sp_fiq, mode_regs.lr_fiq, mode_regs.spsr_fiq);
386 }
387
388 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
389 {
390         int n;
391         struct irq_desc *desc;
392
393         fiq_debugger_printf(state,
394                         "irqnr       total  since-last   status  name\n");
395         for_each_irq_desc(n, desc) {
396                 struct irqaction *act = desc->action;
397                 if (!act && !kstat_irqs(n))
398                         continue;
399                 fiq_debugger_printf(state, "%5d: %10u %11u %8x  %s\n", n,
400                         kstat_irqs(n),
401                         kstat_irqs(n) - state->last_irqs[n],
402                         desc->status_use_accessors,
403                         (act && act->name) ? act->name : "???");
404                 state->last_irqs[n] = kstat_irqs(n);
405         }
406 }
407
408 struct stacktrace_state {
409         struct fiq_debugger_state *state;
410         unsigned int depth;
411 };
412
413 static int report_trace(struct stackframe *frame, void *d)
414 {
415         struct stacktrace_state *sts = d;
416
417         if (sts->depth) {
418                 fiq_debugger_printf(sts->state,
419                         "  pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
420                         frame->pc, frame->pc, frame->lr, frame->lr,
421                         frame->sp, frame->fp);
422                 sts->depth--;
423                 return 0;
424         }
425         fiq_debugger_printf(sts->state, "  ...\n");
426
427         return sts->depth == 0;
428 }
429
430 struct frame_tail {
431         struct frame_tail *fp;
432         unsigned long sp;
433         unsigned long lr;
434 } __attribute__((packed));
435
436 static struct frame_tail *user_backtrace(struct fiq_debugger_state *state,
437                                         struct frame_tail *tail)
438 {
439         struct frame_tail buftail[2];
440
441         /* Also check accessibility of one struct frame_tail beyond */
442         if (!access_ok(VERIFY_READ, tail, sizeof(buftail))) {
443                 fiq_debugger_printf(state, "  invalid frame pointer %p\n",
444                                 tail);
445                 return NULL;
446         }
447         if (__copy_from_user_inatomic(buftail, tail, sizeof(buftail))) {
448                 fiq_debugger_printf(state,
449                         "  failed to copy frame pointer %p\n", tail);
450                 return NULL;
451         }
452
453         fiq_debugger_printf(state, "  %p\n", buftail[0].lr);
454
455         /* frame pointers should strictly progress back up the stack
456          * (towards higher addresses) */
457         if (tail >= buftail[0].fp)
458                 return NULL;
459
460         return buftail[0].fp-1;
461 }
462
463 void fiq_debugger_dump_stacktrace(struct fiq_debugger_state *state,
464                 const struct pt_regs *regs, unsigned int depth, void *ssp)
465 {
466         struct frame_tail *tail;
467         struct thread_info *real_thread_info = THREAD_INFO(ssp);
468         struct stacktrace_state sts;
469
470         sts.depth = depth;
471         sts.state = state;
472         *current_thread_info() = *real_thread_info;
473
474         if (!current)
475                 fiq_debugger_printf(state, "current NULL\n");
476         else
477                 fiq_debugger_printf(state, "pid: %d  comm: %s\n",
478                         current->pid, current->comm);
479         fiq_debugger_dump_regs(state, regs);
480
481         if (!user_mode(regs)) {
482                 struct stackframe frame;
483                 frame.fp = regs->ARM_fp;
484                 frame.sp = regs->ARM_sp;
485                 frame.lr = regs->ARM_lr;
486                 frame.pc = regs->ARM_pc;
487                 fiq_debugger_printf(state,
488                         "  pc: %p (%pF), lr %p (%pF), sp %p, fp %p\n",
489                         regs->ARM_pc, regs->ARM_pc, regs->ARM_lr, regs->ARM_lr,
490                         regs->ARM_sp, regs->ARM_fp);
491                 walk_stackframe(&frame, report_trace, &sts);
492                 return;
493         }
494
495         tail = ((struct frame_tail *) regs->ARM_fp) - 1;
496         while (depth-- && tail && !((unsigned long) tail & 3))
497                 tail = user_backtrace(state, tail);
498 }
499
500 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
501 {
502         struct task_struct *g;
503         struct task_struct *p;
504         unsigned task_state;
505         static const char stat_nam[] = "RSDTtZX";
506
507         fiq_debugger_printf(state, "pid   ppid  prio task            pc\n");
508         read_lock(&tasklist_lock);
509         do_each_thread(g, p) {
510                 task_state = p->state ? __ffs(p->state) + 1 : 0;
511                 fiq_debugger_printf(state,
512                              "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
513                 fiq_debugger_printf(state, "%-13.13s %c", p->comm,
514                              task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
515                 if (task_state == TASK_RUNNING)
516                         fiq_debugger_printf(state, " running\n");
517                 else
518                         fiq_debugger_printf(state, " %08lx\n",
519                                         thread_saved_pc(p));
520         } while_each_thread(g, p);
521         read_unlock(&tasklist_lock);
522 }
523
524 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
525 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
526 {
527         state->syslog_dumping = true;
528 }
529
530 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
531 {
532         state->syslog_dumping = false;
533 }
534 #else
535 extern int do_syslog(int type, char __user *bug, int count);
536 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
537 {
538         do_syslog(5 /* clear */, NULL, 0);
539 }
540
541 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
542 {
543         fiq_debugger_dump_kernel_log(state);
544 }
545 #endif
546
547 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
548 {
549         if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
550                 fiq_debugger_printf(state, "sysrq-g blocked\n");
551                 return;
552         }
553         fiq_debugger_begin_syslog_dump(state);
554         handle_sysrq(rq);
555         fiq_debugger_end_syslog_dump(state);
556 }
557
558 #ifdef CONFIG_KGDB
559 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
560 {
561         if (!fiq_kgdb_enable) {
562                 fiq_debugger_printf(state, "kgdb through fiq debugger not enabled\n");
563                 return;
564         }
565
566         fiq_debugger_printf(state, "enabling console and triggering kgdb\n");
567         state->console_enable = true;
568         handle_sysrq('g');
569 }
570 #endif
571
572 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
573                 char *cmd)
574 {
575         unsigned long flags;
576
577         spin_lock_irqsave(&state->work_lock, flags);
578         if (state->work_cmd[0] != '\0') {
579                 fiq_debugger_printf(state, "work command processor busy\n");
580                 spin_unlock_irqrestore(&state->work_lock, flags);
581                 return;
582         }
583
584         strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
585         spin_unlock_irqrestore(&state->work_lock, flags);
586
587         schedule_work(&state->work);
588 }
589
590 static void fiq_debugger_work(struct work_struct *work)
591 {
592         struct fiq_debugger_state *state;
593         char work_cmd[DEBUG_MAX];
594         char *cmd;
595         unsigned long flags;
596
597         state = container_of(work, struct fiq_debugger_state, work);
598
599         spin_lock_irqsave(&state->work_lock, flags);
600
601         strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
602         state->work_cmd[0] = '\0';
603
604         spin_unlock_irqrestore(&state->work_lock, flags);
605
606         cmd = work_cmd;
607         if (!strncmp(cmd, "reboot", 6)) {
608                 cmd += 6;
609                 while (*cmd == ' ')
610                         cmd++;
611                 if (cmd != '\0')
612                         kernel_restart(cmd);
613                 else
614                         kernel_restart(NULL);
615         } else {
616                 fiq_debugger_printf(state, "unknown work command '%s'\n",
617                                 work_cmd);
618         }
619 }
620
621 /* This function CANNOT be called in FIQ context */
622 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
623 {
624         if (!strcmp(cmd, "ps"))
625                 fiq_debugger_do_ps(state);
626         if (!strcmp(cmd, "sysrq"))
627                 fiq_debugger_do_sysrq(state, 'h');
628         if (!strncmp(cmd, "sysrq ", 6))
629                 fiq_debugger_do_sysrq(state, cmd[6]);
630 #ifdef CONFIG_KGDB
631         if (!strcmp(cmd, "kgdb"))
632                 fiq_debugger_do_kgdb(state);
633 #endif
634         if (!strncmp(cmd, "reboot", 6))
635                 fiq_debugger_schedule_work(state, cmd);
636 }
637
638 static void fiq_debugger_help(struct fiq_debugger_state *state)
639 {
640         fiq_debugger_printf(state,
641                                 "FIQ Debugger commands:\n"
642                                 " pc            PC status\n"
643                                 " regs          Register dump\n"
644                                 " allregs       Extended Register dump\n"
645                                 " bt            Stack trace\n"
646                                 " reboot [<c>]  Reboot with command <c>\n"
647                                 " reset [<c>]   Hard reset with command <c>\n"
648                                 " irqs          Interupt status\n"
649                                 " kmsg          Kernel log\n"
650                                 " version       Kernel version\n");
651         fiq_debugger_printf(state,
652                                 " sleep         Allow sleep while in FIQ\n"
653                                 " nosleep       Disable sleep while in FIQ\n"
654                                 " console       Switch terminal to console\n"
655                                 " cpu           Current CPU\n"
656                                 " cpu <number>  Switch to CPU<number>\n");
657         fiq_debugger_printf(state,
658                                 " ps            Process list\n"
659                                 " sysrq         sysrq options\n"
660                                 " sysrq <param> Execute sysrq with <param>\n");
661 #ifdef CONFIG_KGDB
662         fiq_debugger_printf(state,
663                                 " kgdb          Enter kernel debugger\n");
664 #endif
665 }
666
667 static void fiq_debugger_take_affinity(void *info)
668 {
669         struct fiq_debugger_state *state = info;
670         struct cpumask cpumask;
671
672         cpumask_clear(&cpumask);
673         cpumask_set_cpu(get_cpu(), &cpumask);
674
675         irq_set_affinity(state->uart_irq, &cpumask);
676 }
677
678 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
679 {
680         if (!fiq_debugger_have_fiq(state))
681                 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
682                                 false);
683         state->current_cpu = cpu;
684 }
685
686 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
687                         const char *cmd, const struct pt_regs *regs,
688                         void *svc_sp)
689 {
690         bool signal_helper = false;
691
692         if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
693                 fiq_debugger_help(state);
694         } else if (!strcmp(cmd, "pc")) {
695                 fiq_debugger_printf(state, " pc %08x cpsr %08x mode %s\n",
696                         regs->ARM_pc, regs->ARM_cpsr,
697                         mode_name(regs->ARM_cpsr));
698         } else if (!strcmp(cmd, "regs")) {
699                 fiq_debugger_dump_regs(state, regs);
700         } else if (!strcmp(cmd, "allregs")) {
701                 fiq_debugger_dump_allregs(state, regs);
702         } else if (!strcmp(cmd, "bt")) {
703                 fiq_debugger_dump_stacktrace(state, regs, 100, svc_sp);
704         } else if (!strncmp(cmd, "reset", 5)) {
705                 cmd += 5;
706                 while (*cmd == ' ')
707                         cmd++;
708                 if (*cmd) {
709                         char tmp_cmd[32];
710                         strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
711                         machine_restart(tmp_cmd);
712                 } else {
713                         machine_restart(NULL);
714                 }
715         } else if (!strcmp(cmd, "irqs")) {
716                 fiq_debugger_dump_irqs(state);
717         } else if (!strcmp(cmd, "kmsg")) {
718                 fiq_debugger_dump_kernel_log(state);
719         } else if (!strcmp(cmd, "version")) {
720                 fiq_debugger_printf(state, "%s\n", linux_banner);
721         } else if (!strcmp(cmd, "sleep")) {
722                 state->no_sleep = false;
723                 fiq_debugger_printf(state, "enabling sleep\n");
724         } else if (!strcmp(cmd, "nosleep")) {
725                 state->no_sleep = true;
726                 fiq_debugger_printf(state, "disabling sleep\n");
727         } else if (!strcmp(cmd, "console")) {
728                 fiq_debugger_printf(state, "console mode\n");
729                 fiq_debugger_uart_flush(state);
730                 state->console_enable = true;
731         } else if (!strcmp(cmd, "cpu")) {
732                 fiq_debugger_printf(state, "cpu %d\n", state->current_cpu);
733         } else if (!strncmp(cmd, "cpu ", 4)) {
734                 unsigned long cpu = 0;
735                 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
736                         fiq_debugger_switch_cpu(state, cpu);
737                 else
738                         fiq_debugger_printf(state, "invalid cpu\n");
739                 fiq_debugger_printf(state, "cpu %d\n", state->current_cpu);
740         } else {
741                 if (state->debug_busy) {
742                         fiq_debugger_printf(state,
743                                 "command processor busy. trying to abort.\n");
744                         state->debug_abort = -1;
745                 } else {
746                         strcpy(state->debug_cmd, cmd);
747                         state->debug_busy = 1;
748                 }
749
750                 return true;
751         }
752         if (!state->console_enable)
753                 fiq_debugger_prompt(state);
754
755         return signal_helper;
756 }
757
758 static void fiq_debugger_sleep_timer_expired(unsigned long data)
759 {
760         struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
761         unsigned long flags;
762
763         spin_lock_irqsave(&state->sleep_timer_lock, flags);
764         if (state->uart_enabled && !state->no_sleep) {
765                 if (state->debug_enable && !state->console_enable) {
766                         state->debug_enable = false;
767                         fiq_debugger_printf_nfiq(state,
768                                         "suspending fiq debugger\n");
769                 }
770                 state->ignore_next_wakeup_irq = true;
771                 fiq_debugger_uart_disable(state);
772                 state->uart_enabled = false;
773                 fiq_debugger_enable_wakeup_irq(state);
774         }
775         wake_unlock(&state->debugger_wake_lock);
776         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
777 }
778
779 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
780 {
781         unsigned long flags;
782
783         spin_lock_irqsave(&state->sleep_timer_lock, flags);
784         if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
785                 state->ignore_next_wakeup_irq = false;
786         } else if (!state->uart_enabled) {
787                 wake_lock(&state->debugger_wake_lock);
788                 fiq_debugger_uart_enable(state);
789                 state->uart_enabled = true;
790                 fiq_debugger_disable_wakeup_irq(state);
791                 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
792         }
793         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
794 }
795
796 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
797 {
798         struct fiq_debugger_state *state = dev;
799
800         if (!state->no_sleep)
801                 fiq_debugger_puts(state, "WAKEUP\n");
802         fiq_debugger_handle_wakeup(state);
803
804         return IRQ_HANDLED;
805 }
806
807 static
808 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
809 {
810 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
811         if (state->tty_port.ops) {
812                 int i;
813                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
814                 for (i = 0; i < count; i++) {
815                         int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
816                         tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
817                         if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
818                                 pr_warn("fiq tty failed to consume byte\n");
819                 }
820                 tty_flip_buffer_push(&state->tty_port);
821         }
822 #endif
823 }
824
825 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
826 {
827         if (!state->no_sleep) {
828                 unsigned long flags;
829
830                 spin_lock_irqsave(&state->sleep_timer_lock, flags);
831                 wake_lock(&state->debugger_wake_lock);
832                 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
833                 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
834         }
835         fiq_debugger_handle_console_irq_context(state);
836         if (state->debug_busy) {
837                 fiq_debugger_irq_exec(state, state->debug_cmd);
838                 if (!state->console_enable)
839                         fiq_debugger_prompt(state);
840                 state->debug_busy = 0;
841         }
842 }
843
844 static int fiq_debugger_getc(struct fiq_debugger_state *state)
845 {
846         return state->pdata->uart_getc(state->pdev);
847 }
848
849 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
850                         int this_cpu, const struct pt_regs *regs, void *svc_sp)
851 {
852         int c;
853         static int last_c;
854         int count = 0;
855         bool signal_helper = false;
856
857         if (this_cpu != state->current_cpu) {
858                 if (state->in_fiq)
859                         return false;
860
861                 if (atomic_inc_return(&state->unhandled_fiq_count) !=
862                                         MAX_UNHANDLED_FIQ_COUNT)
863                         return false;
864
865                 fiq_debugger_printf(state,
866                         "fiq_debugger: cpu %d not responding, "
867                         "reverting to cpu %d\n", state->current_cpu,
868                         this_cpu);
869
870                 atomic_set(&state->unhandled_fiq_count, 0);
871                 fiq_debugger_switch_cpu(state, this_cpu);
872                 return false;
873         }
874
875         state->in_fiq = true;
876
877         while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
878                 count++;
879                 if (!state->debug_enable) {
880                         if ((c == 13) || (c == 10)) {
881                                 state->debug_enable = true;
882                                 state->debug_count = 0;
883                                 fiq_debugger_prompt(state);
884                         }
885                 } else if (c == FIQ_DEBUGGER_BREAK) {
886                         state->console_enable = false;
887                         fiq_debugger_puts(state, "fiq debugger mode\n");
888                         state->debug_count = 0;
889                         fiq_debugger_prompt(state);
890 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
891                 } else if (state->console_enable && state->tty_rbuf) {
892                         fiq_debugger_ringbuf_push(state->tty_rbuf, c);
893                         signal_helper = true;
894 #endif
895                 } else if ((c >= ' ') && (c < 127)) {
896                         if (state->debug_count < (DEBUG_MAX - 1)) {
897                                 state->debug_buf[state->debug_count++] = c;
898                                 fiq_debugger_putc(state, c);
899                         }
900                 } else if ((c == 8) || (c == 127)) {
901                         if (state->debug_count > 0) {
902                                 state->debug_count--;
903                                 fiq_debugger_putc(state, 8);
904                                 fiq_debugger_putc(state, ' ');
905                                 fiq_debugger_putc(state, 8);
906                         }
907                 } else if ((c == 13) || (c == 10)) {
908                         if (c == '\r' || (c == '\n' && last_c != '\r')) {
909                                 fiq_debugger_putc(state, '\r');
910                                 fiq_debugger_putc(state, '\n');
911                         }
912                         if (state->debug_count) {
913                                 state->debug_buf[state->debug_count] = 0;
914                                 state->debug_count = 0;
915                                 signal_helper |=
916                                         fiq_debugger_fiq_exec(state,
917                                                         state->debug_buf,
918                                                         regs, svc_sp);
919                         } else {
920                                 fiq_debugger_prompt(state);
921                         }
922                 }
923                 last_c = c;
924         }
925         if (!state->console_enable)
926                 fiq_debugger_uart_flush(state);
927         if (state->pdata->fiq_ack)
928                 state->pdata->fiq_ack(state->pdev, state->fiq);
929
930         /* poke sleep timer if necessary */
931         if (state->debug_enable && !state->no_sleep)
932                 signal_helper = true;
933
934         atomic_set(&state->unhandled_fiq_count, 0);
935         state->in_fiq = false;
936
937         return signal_helper;
938 }
939
940 #ifdef CONFIG_FIQ_GLUE
941 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
942                 const struct pt_regs *regs, void *svc_sp)
943 {
944         struct fiq_debugger_state *state =
945                 container_of(h, struct fiq_debugger_state, handler);
946         unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
947         bool need_irq;
948
949         need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
950                         svc_sp);
951         if (need_irq)
952                 fiq_debugger_force_irq(state);
953 }
954 #endif
955
956 /*
957  * When not using FIQs, we only use this single interrupt as an entry point.
958  * This just effectively takes over the UART interrupt and does all the work
959  * in this context.
960  */
961 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
962 {
963         struct fiq_debugger_state *state = dev;
964         bool not_done;
965
966         fiq_debugger_handle_wakeup(state);
967
968         /* handle the debugger irq in regular context */
969         not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
970                                               get_irq_regs(),
971                                               current_thread_info());
972         if (not_done)
973                 fiq_debugger_handle_irq_context(state);
974
975         return IRQ_HANDLED;
976 }
977
978 /*
979  * If FIQs are used, not everything can happen in fiq context.
980  * FIQ handler does what it can and then signals this interrupt to finish the
981  * job in irq context.
982  */
983 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
984 {
985         struct fiq_debugger_state *state = dev;
986
987         if (state->pdata->force_irq_ack)
988                 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
989
990         fiq_debugger_handle_irq_context(state);
991
992         return IRQ_HANDLED;
993 }
994
995 #ifdef CONFIG_FIQ_GLUE
996 static void fiq_debugger_resume(struct fiq_glue_handler *h)
997 {
998         struct fiq_debugger_state *state =
999                 container_of(h, struct fiq_debugger_state, handler);
1000         if (state->pdata->uart_resume)
1001                 state->pdata->uart_resume(state->pdev);
1002 }
1003 #endif
1004
1005 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1006 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
1007 {
1008         *index = co->index;
1009         return fiq_tty_driver;
1010 }
1011
1012 static void fiq_debugger_console_write(struct console *co,
1013                                 const char *s, unsigned int count)
1014 {
1015         struct fiq_debugger_state *state;
1016         unsigned long flags;
1017
1018         state = container_of(co, struct fiq_debugger_state, console);
1019
1020         if (!state->console_enable && !state->syslog_dumping)
1021                 return;
1022
1023         fiq_debugger_uart_enable(state);
1024         spin_lock_irqsave(&state->console_lock, flags);
1025         while (count--) {
1026                 if (*s == '\n')
1027                         fiq_debugger_putc(state, '\r');
1028                 fiq_debugger_putc(state, *s++);
1029         }
1030         fiq_debugger_uart_flush(state);
1031         spin_unlock_irqrestore(&state->console_lock, flags);
1032         fiq_debugger_uart_disable(state);
1033 }
1034
1035 static struct console fiq_debugger_console = {
1036         .name = "ttyFIQ",
1037         .device = fiq_debugger_console_device,
1038         .write = fiq_debugger_console_write,
1039         .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
1040 };
1041
1042 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1043 {
1044         int line = tty->index;
1045         struct fiq_debugger_state **states = tty->driver->driver_state;
1046         struct fiq_debugger_state *state = states[line];
1047
1048         return tty_port_open(&state->tty_port, tty, filp);
1049 }
1050
1051 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1052 {
1053         tty_port_close(tty->port, tty, filp);
1054 }
1055
1056 int  fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1057 {
1058         int i;
1059         int line = tty->index;
1060         struct fiq_debugger_state **states = tty->driver->driver_state;
1061         struct fiq_debugger_state *state = states[line];
1062
1063         if (!state->console_enable)
1064                 return count;
1065
1066         fiq_debugger_uart_enable(state);
1067         spin_lock_irq(&state->console_lock);
1068         for (i = 0; i < count; i++)
1069                 fiq_debugger_putc(state, *buf++);
1070         spin_unlock_irq(&state->console_lock);
1071         fiq_debugger_uart_disable(state);
1072
1073         return count;
1074 }
1075
1076 int  fiq_tty_write_room(struct tty_struct *tty)
1077 {
1078         return 16;
1079 }
1080
1081 #ifdef CONFIG_CONSOLE_POLL
1082 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1083 {
1084         return 0;
1085 }
1086
1087 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1088 {
1089         struct fiq_debugger_state **states = driver->driver_state;
1090         struct fiq_debugger_state *state = states[line];
1091         int c = NO_POLL_CHAR;
1092
1093         fiq_debugger_uart_enable(state);
1094         if (fiq_debugger_have_fiq(state)) {
1095                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1096                 if (count > 0) {
1097                         c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1098                         fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1099                 }
1100         } else {
1101                 c = fiq_debugger_getc(state);
1102                 if (c == FIQ_DEBUGGER_NO_CHAR)
1103                         c = NO_POLL_CHAR;
1104         }
1105         fiq_debugger_uart_disable(state);
1106
1107         return c;
1108 }
1109
1110 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1111 {
1112         struct fiq_debugger_state **states = driver->driver_state;
1113         struct fiq_debugger_state *state = states[line];
1114         fiq_debugger_uart_enable(state);
1115         fiq_debugger_putc(state, ch);
1116         fiq_debugger_uart_disable(state);
1117 }
1118 #endif
1119
1120 static const struct tty_port_operations fiq_tty_port_ops;
1121
1122 static const struct tty_operations fiq_tty_driver_ops = {
1123         .write = fiq_tty_write,
1124         .write_room = fiq_tty_write_room,
1125         .open = fiq_tty_open,
1126         .close = fiq_tty_close,
1127 #ifdef CONFIG_CONSOLE_POLL
1128         .poll_init = fiq_tty_poll_init,
1129         .poll_get_char = fiq_tty_poll_get_char,
1130         .poll_put_char = fiq_tty_poll_put_char,
1131 #endif
1132 };
1133
1134 static int fiq_debugger_tty_init(void)
1135 {
1136         int ret;
1137         struct fiq_debugger_state **states = NULL;
1138
1139         states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1140         if (!states) {
1141                 pr_err("Failed to allocate fiq debugger state structres\n");
1142                 return -ENOMEM;
1143         }
1144
1145         fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
1146         if (!fiq_tty_driver) {
1147                 pr_err("Failed to allocate fiq debugger tty\n");
1148                 ret = -ENOMEM;
1149                 goto err_free_state;
1150         }
1151
1152         fiq_tty_driver->owner           = THIS_MODULE;
1153         fiq_tty_driver->driver_name     = "fiq-debugger";
1154         fiq_tty_driver->name            = "ttyFIQ";
1155         fiq_tty_driver->type            = TTY_DRIVER_TYPE_SERIAL;
1156         fiq_tty_driver->subtype         = SERIAL_TYPE_NORMAL;
1157         fiq_tty_driver->init_termios    = tty_std_termios;
1158         fiq_tty_driver->flags           = TTY_DRIVER_REAL_RAW |
1159                                           TTY_DRIVER_DYNAMIC_DEV;
1160         fiq_tty_driver->driver_state    = states;
1161
1162         fiq_tty_driver->init_termios.c_cflag =
1163                                         B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1164         fiq_tty_driver->init_termios.c_ispeed = 115200;
1165         fiq_tty_driver->init_termios.c_ospeed = 115200;
1166
1167         tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1168
1169         ret = tty_register_driver(fiq_tty_driver);
1170         if (ret) {
1171                 pr_err("Failed to register fiq tty: %d\n", ret);
1172                 goto err_free_tty;
1173         }
1174
1175         pr_info("Registered FIQ tty driver\n");
1176         return 0;
1177
1178 err_free_tty:
1179         put_tty_driver(fiq_tty_driver);
1180         fiq_tty_driver = NULL;
1181 err_free_state:
1182         kfree(states);
1183         return ret;
1184 }
1185
1186 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1187 {
1188         int ret;
1189         struct device *tty_dev;
1190         struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1191
1192         states[state->pdev->id] = state;
1193
1194         state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1195         if (!state->tty_rbuf) {
1196                 pr_err("Failed to allocate fiq debugger ringbuf\n");
1197                 ret = -ENOMEM;
1198                 goto err;
1199         }
1200
1201         tty_port_init(&state->tty_port);
1202         state->tty_port.ops = &fiq_tty_port_ops;
1203
1204         tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1205                                            state->pdev->id, &state->pdev->dev);
1206         if (IS_ERR(tty_dev)) {
1207                 pr_err("Failed to register fiq debugger tty device\n");
1208                 ret = PTR_ERR(tty_dev);
1209                 goto err;
1210         }
1211
1212         device_set_wakeup_capable(tty_dev, 1);
1213
1214         pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1215
1216         return 0;
1217
1218 err:
1219         fiq_debugger_ringbuf_free(state->tty_rbuf);
1220         state->tty_rbuf = NULL;
1221         return ret;
1222 }
1223 #endif
1224
1225 static int fiq_debugger_dev_suspend(struct device *dev)
1226 {
1227         struct platform_device *pdev = to_platform_device(dev);
1228         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1229
1230         if (state->pdata->uart_dev_suspend)
1231                 return state->pdata->uart_dev_suspend(pdev);
1232         return 0;
1233 }
1234
1235 static int fiq_debugger_dev_resume(struct device *dev)
1236 {
1237         struct platform_device *pdev = to_platform_device(dev);
1238         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1239
1240         if (state->pdata->uart_dev_resume)
1241                 return state->pdata->uart_dev_resume(pdev);
1242         return 0;
1243 }
1244
1245 static int fiq_debugger_probe(struct platform_device *pdev)
1246 {
1247         int ret;
1248         struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1249         struct fiq_debugger_state *state;
1250         int fiq;
1251         int uart_irq;
1252
1253         if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1254                 return -EINVAL;
1255
1256         if (!pdata->uart_getc || !pdata->uart_putc)
1257                 return -EINVAL;
1258         if ((pdata->uart_enable && !pdata->uart_disable) ||
1259             (!pdata->uart_enable && pdata->uart_disable))
1260                 return -EINVAL;
1261
1262         fiq = platform_get_irq_byname(pdev, "fiq");
1263         uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1264
1265         /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1266          * is required */
1267         if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1268                 return -EINVAL;
1269         if (fiq >= 0 && !pdata->fiq_enable)
1270                 return -EINVAL;
1271
1272         state = kzalloc(sizeof(*state), GFP_KERNEL);
1273         setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1274                     (unsigned long)state);
1275         state->pdata = pdata;
1276         state->pdev = pdev;
1277         state->no_sleep = initial_no_sleep;
1278         state->debug_enable = initial_debug_enable;
1279         state->console_enable = initial_console_enable;
1280
1281         state->fiq = fiq;
1282         state->uart_irq = uart_irq;
1283         state->signal_irq = platform_get_irq_byname(pdev, "signal");
1284         state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1285
1286         INIT_WORK(&state->work, fiq_debugger_work);
1287         spin_lock_init(&state->work_lock);
1288
1289         platform_set_drvdata(pdev, state);
1290
1291         spin_lock_init(&state->sleep_timer_lock);
1292
1293         if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1294                 state->no_sleep = true;
1295         state->ignore_next_wakeup_irq = !state->no_sleep;
1296
1297         wake_lock_init(&state->debugger_wake_lock,
1298                         WAKE_LOCK_SUSPEND, "serial-debug");
1299
1300         state->clk = clk_get(&pdev->dev, NULL);
1301         if (IS_ERR(state->clk))
1302                 state->clk = NULL;
1303
1304         /* do not call pdata->uart_enable here since uart_init may still
1305          * need to do some initialization before uart_enable can work.
1306          * So, only try to manage the clock during init.
1307          */
1308         if (state->clk)
1309                 clk_enable(state->clk);
1310
1311         if (pdata->uart_init) {
1312                 ret = pdata->uart_init(pdev);
1313                 if (ret)
1314                         goto err_uart_init;
1315         }
1316
1317         fiq_debugger_printf_nfiq(state,
1318                                 "<hit enter %sto activate fiq debugger>\n",
1319                                 state->no_sleep ? "" : "twice ");
1320
1321 #ifdef CONFIG_FIQ_GLUE
1322         if (fiq_debugger_have_fiq(state)) {
1323                 state->handler.fiq = fiq_debugger_fiq;
1324                 state->handler.resume = fiq_debugger_resume;
1325                 ret = fiq_glue_register_handler(&state->handler);
1326                 if (ret) {
1327                         pr_err("%s: could not install fiq handler\n", __func__);
1328                         goto err_register_irq;
1329                 }
1330
1331                 pdata->fiq_enable(pdev, state->fiq, 1);
1332         } else
1333 #endif
1334         {
1335                 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1336                                   IRQF_NO_SUSPEND, "debug", state);
1337                 if (ret) {
1338                         pr_err("%s: could not install irq handler\n", __func__);
1339                         goto err_register_irq;
1340                 }
1341
1342                 /* for irq-only mode, we want this irq to wake us up, if it
1343                  * can.
1344                  */
1345                 enable_irq_wake(state->uart_irq);
1346         }
1347
1348         if (state->clk)
1349                 clk_disable(state->clk);
1350
1351         if (state->signal_irq >= 0) {
1352                 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1353                           IRQF_TRIGGER_RISING, "debug-signal", state);
1354                 if (ret)
1355                         pr_err("serial_debugger: could not install signal_irq");
1356         }
1357
1358         if (state->wakeup_irq >= 0) {
1359                 ret = request_irq(state->wakeup_irq,
1360                                   fiq_debugger_wakeup_irq_handler,
1361                                   IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1362                                   "debug-wakeup", state);
1363                 if (ret) {
1364                         pr_err("serial_debugger: "
1365                                 "could not install wakeup irq\n");
1366                         state->wakeup_irq = -1;
1367                 } else {
1368                         ret = enable_irq_wake(state->wakeup_irq);
1369                         if (ret) {
1370                                 pr_err("serial_debugger: "
1371                                         "could not enable wakeup\n");
1372                                 state->wakeup_irq_no_set_wake = true;
1373                         }
1374                 }
1375         }
1376         if (state->no_sleep)
1377                 fiq_debugger_handle_wakeup(state);
1378
1379 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1380         spin_lock_init(&state->console_lock);
1381         state->console = fiq_debugger_console;
1382         state->console.index = pdev->id;
1383         if (!console_set_on_cmdline)
1384                 add_preferred_console(state->console.name,
1385                         state->console.index, NULL);
1386         register_console(&state->console);
1387         fiq_debugger_tty_init_one(state);
1388 #endif
1389         return 0;
1390
1391 err_register_irq:
1392         if (pdata->uart_free)
1393                 pdata->uart_free(pdev);
1394 err_uart_init:
1395         if (state->clk)
1396                 clk_disable(state->clk);
1397         if (state->clk)
1398                 clk_put(state->clk);
1399         wake_lock_destroy(&state->debugger_wake_lock);
1400         platform_set_drvdata(pdev, NULL);
1401         kfree(state);
1402         return ret;
1403 }
1404
1405 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1406         .suspend        = fiq_debugger_dev_suspend,
1407         .resume         = fiq_debugger_dev_resume,
1408 };
1409
1410 static struct platform_driver fiq_debugger_driver = {
1411         .probe  = fiq_debugger_probe,
1412         .driver = {
1413                 .name   = "fiq_debugger",
1414                 .pm     = &fiq_debugger_dev_pm_ops,
1415         },
1416 };
1417
1418 static int __init fiq_debugger_init(void)
1419 {
1420 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1421         fiq_debugger_tty_init();
1422 #endif
1423         return platform_driver_register(&fiq_debugger_driver);
1424 }
1425
1426 postcore_initcall(fiq_debugger_init);