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