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