Merge tag 'lsk-v3.10-15.05-android' into develop-3.10
[firefly-linux-kernel-4.4.55.git] / drivers / staging / android / fiq_debugger / fiq_debugger.c
1 /*
2  * drivers/staging/android/fiq_debugger.c
3  *
4  * Serial Debugger Interface accessed through an FIQ interrupt.
5  *
6  * Copyright (C) 2008 Google, Inc.
7  *
8  * This software is licensed under the terms of the GNU General Public
9  * License version 2, as published by the Free Software Foundation, and
10  * may be copied, distributed, and modified under those terms.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  */
17
18 #include <stdarg.h>
19 #include <linux/module.h>
20 #include <linux/io.h>
21 #include <linux/console.h>
22 #include <linux/interrupt.h>
23 #include <linux/clk.h>
24 #include <linux/platform_device.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/kmsg_dump.h>
27 #include <linux/irq.h>
28 #include <linux/delay.h>
29 #include <linux/reboot.h>
30 #include <linux/sched.h>
31 #include <linux/slab.h>
32 #include <linux/smp.h>
33 #include <linux/timer.h>
34 #include <linux/tty.h>
35 #include <linux/tty_flip.h>
36 #include <linux/wakelock.h>
37
38 #ifdef CONFIG_FIQ_GLUE
39 #include <asm/fiq_glue.h>
40 #endif
41
42 #include <linux/uaccess.h>
43 #include <linux/rockchip/grf.h>
44 #include <linux/rockchip/iomap.h>
45 #include <linux/rockchip/cpu.h>
46
47 #include "fiq_debugger.h"
48 #include "fiq_debugger_priv.h"
49 #include "fiq_debugger_ringbuf.h"
50
51 #ifdef CONFIG_RK29_WATCHDOG
52 extern void rk29_wdt_keepalive(void);
53 #define wdt_keepalive() rk29_wdt_keepalive()
54 #else
55 #define wdt_keepalive() do {} while (0)
56 #endif
57 #define DEBUG_MAX 64
58 #define CMD_COUNT 0x0f
59 #define MAX_UNHANDLED_FIQ_COUNT 1000000
60
61 #ifdef CONFIG_ARCH_ROCKCHIP
62 #define MAX_FIQ_DEBUGGER_PORTS 1
63 #else
64 #define MAX_FIQ_DEBUGGER_PORTS 4
65 #endif
66
67 struct fiq_debugger_state {
68 #ifdef CONFIG_FIQ_GLUE
69         struct fiq_glue_handler handler;
70 #endif
71         struct fiq_debugger_output output;
72
73         int fiq;
74         int uart_irq;
75         int signal_irq;
76         int wakeup_irq;
77         bool wakeup_irq_no_set_wake;
78         struct clk *clk;
79         struct fiq_debugger_pdata *pdata;
80         struct platform_device *pdev;
81
82         char debug_cmd[DEBUG_MAX];
83         int debug_busy;
84         int debug_abort;
85
86         char debug_buf[DEBUG_MAX];
87         int debug_count;
88
89 #ifdef CONFIG_ARCH_ROCKCHIP
90         char cmd_buf[CMD_COUNT+1][DEBUG_MAX];
91         int back_pointer;
92         int current_pointer;
93 #endif
94         bool no_sleep;
95         bool debug_enable;
96         bool ignore_next_wakeup_irq;
97         struct timer_list sleep_timer;
98         spinlock_t sleep_timer_lock;
99         bool uart_enabled;
100         struct wake_lock debugger_wake_lock;
101         bool console_enable;
102         int current_cpu;
103         atomic_t unhandled_fiq_count;
104         bool in_fiq;
105
106         struct work_struct work;
107         spinlock_t work_lock;
108         char work_cmd[DEBUG_MAX];
109
110 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
111         spinlock_t console_lock;
112         struct console console;
113         struct tty_port tty_port;
114         struct fiq_debugger_ringbuf *tty_rbuf;
115         bool syslog_dumping;
116 #endif
117
118 #ifdef CONFIG_ARCH_ROCKCHIP
119         unsigned int last_irqs[1024];
120         unsigned int last_local_irqs[NR_CPUS][32];
121 #else
122         unsigned int last_irqs[NR_IRQS];
123         unsigned int last_local_timer_irqs[NR_CPUS];
124 #endif
125 };
126
127 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
128 struct tty_driver *fiq_tty_driver;
129 #endif
130
131 #ifdef CONFIG_FIQ_DEBUGGER_NO_SLEEP
132 static bool initial_no_sleep = true;
133 #else
134 static bool initial_no_sleep;
135 #endif
136
137 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE_DEFAULT_ENABLE
138 static bool initial_debug_enable = true;
139 static bool initial_console_enable = true;
140 #else
141 static bool initial_debug_enable;
142 static bool initial_console_enable;
143 #endif
144
145 static bool fiq_kgdb_enable;
146
147 static unsigned long jif = 0, recv_count0 = 0, recv_count1 = 0;
148
149 module_param_named(no_sleep, initial_no_sleep, bool, 0644);
150 module_param_named(debug_enable, initial_debug_enable, bool, 0644);
151 module_param_named(console_enable, initial_console_enable, bool, 0644);
152 module_param_named(kgdb_enable, fiq_kgdb_enable, bool, 0644);
153
154 void gic_set_irq_secure(struct irq_data *d);
155 void gic_set_irq_priority(struct irq_data *d, u8 pri);
156
157 #ifdef CONFIG_FIQ_DEBUGGER_WAKEUP_IRQ_ALWAYS_ON
158 static inline
159 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state) {}
160 static inline
161 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state) {}
162 #else
163 static inline
164 void fiq_debugger_enable_wakeup_irq(struct fiq_debugger_state *state)
165 {
166         if (state->wakeup_irq < 0)
167                 return;
168         enable_irq(state->wakeup_irq);
169         if (!state->wakeup_irq_no_set_wake)
170                 enable_irq_wake(state->wakeup_irq);
171 }
172 static inline
173 void fiq_debugger_disable_wakeup_irq(struct fiq_debugger_state *state)
174 {
175         if (state->wakeup_irq < 0)
176                 return;
177         disable_irq_nosync(state->wakeup_irq);
178         if (!state->wakeup_irq_no_set_wake)
179                 disable_irq_wake(state->wakeup_irq);
180 }
181 #endif
182
183 static inline bool fiq_debugger_have_fiq(struct fiq_debugger_state *state)
184 {
185         return (state->fiq >= 0);
186 }
187
188 #ifdef CONFIG_FIQ_GLUE
189 static void fiq_debugger_force_irq(struct fiq_debugger_state *state)
190 {
191         unsigned int irq = state->signal_irq;
192
193         if (WARN_ON(!fiq_debugger_have_fiq(state)))
194                 return;
195         if (state->pdata->force_irq) {
196                 state->pdata->force_irq(state->pdev, irq);
197         } else {
198                 struct irq_chip *chip = irq_get_chip(irq);
199                 if (chip && chip->irq_retrigger)
200                         chip->irq_retrigger(irq_get_irq_data(irq));
201         }
202 }
203 #endif
204
205 static void fiq_debugger_uart_enable(struct fiq_debugger_state *state)
206 {
207         if (state->clk)
208                 clk_enable(state->clk);
209         if (state->pdata->uart_enable)
210                 state->pdata->uart_enable(state->pdev);
211 }
212
213 static void fiq_debugger_uart_disable(struct fiq_debugger_state *state)
214 {
215         if (state->pdata->uart_disable)
216                 state->pdata->uart_disable(state->pdev);
217         if (state->clk)
218                 clk_disable(state->clk);
219 }
220
221 static void fiq_debugger_uart_flush(struct fiq_debugger_state *state)
222 {
223         if (state->pdata->uart_flush)
224                 state->pdata->uart_flush(state->pdev);
225 }
226
227 static void fiq_debugger_putc(struct fiq_debugger_state *state, char c)
228 {
229         state->pdata->uart_putc(state->pdev, c);
230 }
231
232 static void fiq_debugger_puts(struct fiq_debugger_state *state, char *s)
233 {
234         unsigned c;
235         while ((c = *s++)) {
236                 if (c == '\n')
237                         fiq_debugger_putc(state, '\r');
238                 fiq_debugger_putc(state, c);
239         }
240 }
241
242 static void fiq_debugger_prompt(struct fiq_debugger_state *state)
243 {
244         fiq_debugger_puts(state, "debug> ");
245 }
246
247 static void fiq_debugger_dump_kernel_log(struct fiq_debugger_state *state)
248 {
249 #ifdef CONFIG_ARCH_ROCKCHIP
250         char buf[968];
251 #else
252         char buf[512];
253 #endif
254         size_t len;
255         struct kmsg_dumper dumper = { .active = true };
256
257
258         kmsg_dump_rewind_nolock(&dumper);
259         while (kmsg_dump_get_line_nolock(&dumper, true, buf,
260                                          sizeof(buf) - 1, &len)) {
261                 buf[len] = 0;
262                 fiq_debugger_puts(state, buf);
263 #ifdef CONFIG_ARCH_ROCKCHIP
264                 wdt_keepalive();
265 #endif
266         }
267 }
268
269 #ifdef CONFIG_RK_LAST_LOG
270 #include <linux/ctype.h>
271 extern char *rk_last_log_get(unsigned *size);
272 static void fiq_debugger_dump_last_kernel_log(struct fiq_debugger_state *state)
273 {
274         unsigned size, i, c;
275         char *s = rk_last_log_get(&size);
276
277         for (i = 0; i < size; i++) {
278                 if (i % 1024 == 0)
279                         wdt_keepalive();
280                 c = s[i];
281                 if (c == '\n') {
282                         state->pdata->uart_putc(state->pdev, '\r');
283                         state->pdata->uart_putc(state->pdev, c);
284                 } else if (isascii(c) && isprint(c)) {
285                         state->pdata->uart_putc(state->pdev, c);
286                 }
287         }
288 }
289 #endif
290
291 static void fiq_debugger_printf(struct fiq_debugger_output *output,
292                                const char *fmt, ...)
293 {
294         struct fiq_debugger_state *state;
295         char buf[256];
296         va_list ap;
297
298         state = container_of(output, struct fiq_debugger_state, output);
299         va_start(ap, fmt);
300         vsnprintf(buf, sizeof(buf), fmt, ap);
301         va_end(ap);
302
303         fiq_debugger_puts(state, buf);
304 }
305
306 /* Safe outside fiq context */
307 static int fiq_debugger_printf_nfiq(void *cookie, const char *fmt, ...)
308 {
309         struct fiq_debugger_state *state = cookie;
310         char buf[256];
311         va_list ap;
312         unsigned long irq_flags;
313
314         va_start(ap, fmt);
315         vsnprintf(buf, 128, fmt, ap);
316         va_end(ap);
317
318         local_irq_save(irq_flags);
319         fiq_debugger_puts(state, buf);
320         fiq_debugger_uart_flush(state);
321         local_irq_restore(irq_flags);
322         return state->debug_abort;
323 }
324
325 static void fiq_debugger_dump_irqs(struct fiq_debugger_state *state)
326 {
327         int n;
328         unsigned int cpu;
329         struct irq_desc *desc;
330
331         fiq_debugger_printf(&state->output,
332                         "irqnr       total  since-last   status  name\n");
333         for_each_irq_desc(n, desc) {
334                 struct irqaction *act = desc->action;
335                 if (!act && !kstat_irqs(n))
336                         continue;
337                 fiq_debugger_printf(&state->output, "%5d: %10u %11u %8x  %s\n", n,
338                         kstat_irqs(n),
339                         kstat_irqs(n) - state->last_irqs[n],
340                         desc->status_use_accessors,
341                         (act && act->name) ? act->name : "???");
342                 state->last_irqs[n] = kstat_irqs(n);
343         }
344
345 #ifdef CONFIG_ARCH_ROCKCHIP
346         for (n = 16; n < 32; n++) {
347                 desc = irq_to_desc(n);
348                 if (!desc)
349                         continue;
350                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
351                         unsigned int irqs = kstat_irqs_cpu(n, cpu);
352                         struct irqaction *act = desc->action;
353                         const char *name = (act && act->name) ? act->name : "???";
354                         if (!irqs)
355                                 continue;
356                         fiq_debugger_printf(&state->output,
357                                 "%5d: %10u %11u           %s (CPU%d)\n", n,
358                                 irqs, irqs - state->last_local_irqs[cpu][n],
359                                 name, cpu);
360                         state->last_local_irqs[cpu][n] = irqs;
361                 }
362         }
363         for (n = 0; n < NR_IPI; n++) {
364 #define S(x,s)  [x] = s
365 #ifdef CONFIG_ARM
366                 enum ipi_msg_type {
367                         IPI_WAKEUP,
368                         IPI_TIMER,
369                         IPI_RESCHEDULE,
370                         IPI_CALL_FUNC,
371                         IPI_CALL_FUNC_SINGLE,
372                         IPI_CPU_STOP,
373                         IPI_COMPLETION,
374                         IPI_CPU_BACKTRACE,
375                 };
376                 static const char *ipi_types[NR_IPI] = {
377                         S(IPI_WAKEUP, "CPU wakeup"),
378                         S(IPI_TIMER, "Timer broadcast"),
379                         S(IPI_RESCHEDULE, "Rescheduling"),
380                         S(IPI_CALL_FUNC, "Function call"),
381                         S(IPI_CALL_FUNC_SINGLE, "Single function call"),
382                         S(IPI_CPU_STOP, "CPU stop"),
383                         S(IPI_COMPLETION, "Completion"),
384                         S(IPI_CPU_BACKTRACE, "CPU backtrace"),
385                 };
386 #elif defined(CONFIG_ARM64)
387                 enum ipi_msg_type {
388                         IPI_RESCHEDULE,
389                         IPI_CALL_FUNC,
390                         IPI_CALL_FUNC_SINGLE,
391                         IPI_CPU_STOP,
392                         IPI_TIMER,
393                 };
394                 static const char *ipi_types[NR_IPI] = {
395                         S(IPI_RESCHEDULE, "Rescheduling"),
396                         S(IPI_CALL_FUNC, "Function call"),
397                         S(IPI_CALL_FUNC_SINGLE, "Single function call"),
398                         S(IPI_CPU_STOP, "CPU stop"),
399                         S(IPI_TIMER, "Timer broadcast"),
400                 };
401 #endif
402 #undef S
403                 for (cpu = 0; cpu < NR_CPUS; cpu++) {
404                         unsigned int irqs = __get_irq_stat(cpu, ipi_irqs[n]);
405                         if (irqs == 0)
406                                 continue;
407                         fiq_debugger_printf(&state->output,
408                                 "%5d: %10u %11u           %s (CPU%d)\n",
409                                 n, irqs, irqs - state->last_local_irqs[cpu][n],
410                                 ipi_types[n], cpu);
411                         state->last_local_irqs[cpu][n] = irqs;
412                 }
413         }
414 #endif
415 }
416
417 static void fiq_debugger_do_ps(struct fiq_debugger_state *state)
418 {
419         struct task_struct *g;
420         struct task_struct *p;
421         unsigned task_state;
422         static const char stat_nam[] = "RSDTtZX";
423
424         fiq_debugger_printf(&state->output, "pid   ppid  prio task            pc\n");
425         read_lock(&tasklist_lock);
426         do_each_thread(g, p) {
427                 task_state = p->state ? __ffs(p->state) + 1 : 0;
428                 fiq_debugger_printf(&state->output,
429                              "%5d %5d %4d ", p->pid, p->parent->pid, p->prio);
430                 fiq_debugger_printf(&state->output, "%-13.13s %c", p->comm,
431                              task_state >= sizeof(stat_nam) ? '?' : stat_nam[task_state]);
432                 if (task_state == TASK_RUNNING)
433                         fiq_debugger_printf(&state->output, " running\n");
434                 else
435                         fiq_debugger_printf(&state->output, " %08lx\n",
436                                         thread_saved_pc(p));
437         } while_each_thread(g, p);
438         read_unlock(&tasklist_lock);
439 }
440
441 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
442 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
443 {
444         state->syslog_dumping = true;
445 }
446
447 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
448 {
449         state->syslog_dumping = false;
450 }
451 #else
452 extern int do_syslog(int type, char __user *bug, int count);
453 static void fiq_debugger_begin_syslog_dump(struct fiq_debugger_state *state)
454 {
455         do_syslog(5 /* clear */, NULL, 0);
456 }
457
458 static void fiq_debugger_end_syslog_dump(struct fiq_debugger_state *state)
459 {
460         fiq_debugger_dump_kernel_log(state);
461 }
462 #endif
463
464 static void fiq_debugger_do_sysrq(struct fiq_debugger_state *state, char rq)
465 {
466         if ((rq == 'g' || rq == 'G') && !fiq_kgdb_enable) {
467                 fiq_debugger_printf(&state->output, "sysrq-g blocked\n");
468                 return;
469         }
470         fiq_debugger_begin_syslog_dump(state);
471         handle_sysrq(rq);
472         fiq_debugger_end_syslog_dump(state);
473 }
474
475 #ifdef CONFIG_KGDB
476 static void fiq_debugger_do_kgdb(struct fiq_debugger_state *state)
477 {
478         if (!fiq_kgdb_enable) {
479                 fiq_debugger_printf(&state->output, "kgdb through fiq debugger not enabled\n");
480                 return;
481         }
482
483         fiq_debugger_printf(&state->output, "enabling console and triggering kgdb\n");
484         state->console_enable = true;
485         handle_sysrq('g');
486 }
487 #endif
488
489 static void fiq_debugger_schedule_work(struct fiq_debugger_state *state,
490                 char *cmd)
491 {
492         unsigned long flags;
493
494         spin_lock_irqsave(&state->work_lock, flags);
495         if (state->work_cmd[0] != '\0') {
496                 fiq_debugger_printf(&state->output, "work command processor busy\n");
497                 spin_unlock_irqrestore(&state->work_lock, flags);
498                 return;
499         }
500
501         strlcpy(state->work_cmd, cmd, sizeof(state->work_cmd));
502         spin_unlock_irqrestore(&state->work_lock, flags);
503
504         schedule_work(&state->work);
505 }
506
507 static void fiq_debugger_work(struct work_struct *work)
508 {
509         struct fiq_debugger_state *state;
510         char work_cmd[DEBUG_MAX];
511         char *cmd;
512         unsigned long flags;
513
514         state = container_of(work, struct fiq_debugger_state, work);
515
516         spin_lock_irqsave(&state->work_lock, flags);
517
518         strlcpy(work_cmd, state->work_cmd, sizeof(work_cmd));
519         state->work_cmd[0] = '\0';
520
521         spin_unlock_irqrestore(&state->work_lock, flags);
522
523         cmd = work_cmd;
524         if (!strncmp(cmd, "reboot", 6)) {
525                 cmd += 6;
526                 while (*cmd == ' ')
527                         cmd++;
528                 if (cmd != '\0')
529                         kernel_restart(cmd);
530                 else
531                         kernel_restart(NULL);
532         } else {
533                 fiq_debugger_printf(&state->output, "unknown work command '%s'\n",
534                                 work_cmd);
535         }
536 }
537
538 /* This function CANNOT be called in FIQ context */
539 static void fiq_debugger_irq_exec(struct fiq_debugger_state *state, char *cmd)
540 {
541         int invalid_cmd = 0;
542         if (!strcmp(cmd, "ps"))
543                 fiq_debugger_do_ps(state);
544         if (!strcmp(cmd, "sysrq"))
545                 fiq_debugger_do_sysrq(state, 'h');
546         if (!strncmp(cmd, "sysrq ", 6))
547                 fiq_debugger_do_sysrq(state, cmd[6]);
548 #ifdef CONFIG_KGDB
549         if (!strcmp(cmd, "kgdb"))
550                 fiq_debugger_do_kgdb(state);
551 #endif
552         if (!strncmp(cmd, "reboot", 6))
553                 fiq_debugger_schedule_work(state, cmd);
554 #ifdef CONFIG_ARCH_ROCKCHIP
555         else {
556                 invalid_cmd = 1;
557                 memset(state->debug_buf, 0, DEBUG_MAX);
558         }
559
560         if (invalid_cmd == 0) {
561                 state->current_pointer = (state->current_pointer-1) & CMD_COUNT;
562                 if (strcmp(state->cmd_buf[state->current_pointer], state->debug_buf)) {
563                         state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
564                         memset(state->cmd_buf[state->current_pointer], 0, DEBUG_MAX);
565                         strcpy(state->cmd_buf[state->current_pointer], state->debug_buf);
566                 }
567                 memset(state->debug_buf, 0, DEBUG_MAX);
568                 state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
569                 state->back_pointer = state->current_pointer;
570         }
571 #endif
572 }
573
574 #ifdef CONFIG_ARCH_ROCKCHIP
575 static char cmd_buf[][16] = {
576                 {"pc"},
577                 {"regs"},
578                 {"allregs"},
579                 {"bt"},
580                 {"reboot"},
581                 {"irqs"},
582                 {"kmsg"},
583 #ifdef CONFIG_RK_LAST_LOG
584                 {"last_kmsg"},
585 #endif
586                 {"version"},
587                 {"sleep"},
588                 {"nosleep"},
589                 {"console"},
590                 {"cpu"},
591                 {"ps"},
592                 {"sysrq"},
593                 {"reset"},
594 #ifdef CONFIG_KGDB
595                 {"kgdb"},
596 #endif
597 };
598 #endif
599
600
601 static void fiq_debugger_help(struct fiq_debugger_state *state)
602 {
603         fiq_debugger_printf(&state->output,
604                                 "FIQ Debugger commands:\n"
605                                 " pc            PC status\n"
606                                 " regs          Register dump\n"
607                                 " allregs       Extended Register dump\n"
608                                 " bt            Stack trace\n");
609         fiq_debugger_printf(&state->output,
610                                 " reboot [<c>]  Reboot with command <c>\n"
611                                 " reset [<c>]   Hard reset with command <c>\n"
612                                 " irqs          Interupt status\n"
613                                 " kmsg          Kernel log\n"
614                                 " version       Kernel version\n");
615 #ifdef CONFIG_RK_LAST_LOG
616         fiq_debugger_printf(&state->output,
617                                 " last_kmsg     Last kernel log\n");
618 #endif
619         fiq_debugger_printf(&state->output,
620                                 " sleep         Allow sleep while in FIQ\n"
621                                 " nosleep       Disable sleep while in FIQ\n"
622                                 " console       Switch terminal to console\n"
623                                 " cpu           Current CPU\n"
624                                 " cpu <number>  Switch to CPU<number>\n");
625         fiq_debugger_printf(&state->output,
626                                 " ps            Process list\n"
627                                 " sysrq         sysrq options\n"
628                                 " sysrq <param> Execute sysrq with <param>\n");
629 #ifdef CONFIG_KGDB
630         fiq_debugger_printf(&state->output,
631                                 " kgdb          Enter kernel debugger\n");
632 #endif
633 }
634
635 static void fiq_debugger_take_affinity(void *info)
636 {
637         struct fiq_debugger_state *state = info;
638         struct cpumask cpumask;
639
640         cpumask_clear(&cpumask);
641         cpumask_set_cpu(get_cpu(), &cpumask);
642
643         irq_set_affinity(state->uart_irq, &cpumask);
644 }
645
646 static void fiq_debugger_switch_cpu(struct fiq_debugger_state *state, int cpu)
647 {
648         if (!fiq_debugger_have_fiq(state))
649                 smp_call_function_single(cpu, fiq_debugger_take_affinity, state,
650                                 false);
651 #ifdef CONFIG_ARCH_ROCKCHIP
652         else {
653                 struct cpumask cpumask;
654
655                 if (!cpu_online(cpu)) {
656                         fiq_debugger_printf(&state->output, "cpu %d offline\n", cpu);
657                         return;
658                 }
659
660                 cpumask_clear(&cpumask);
661                 cpumask_set_cpu(cpu, &cpumask);
662
663                 irq_set_affinity(state->fiq, &cpumask);
664                 irq_set_affinity(state->uart_irq, &cpumask);
665         }
666 #endif
667         state->current_cpu = cpu;
668 }
669
670 static bool fiq_debugger_fiq_exec(struct fiq_debugger_state *state,
671                         const char *cmd, const struct pt_regs *regs,
672                         void *svc_sp)
673 {
674         bool signal_helper = false;
675
676         if (!strcmp(cmd, "help") || !strcmp(cmd, "?")) {
677                 fiq_debugger_help(state);
678         } else if (!strcmp(cmd, "pc")) {
679                 fiq_debugger_dump_pc(&state->output, regs);
680         } else if (!strcmp(cmd, "regs")) {
681                 fiq_debugger_dump_regs(&state->output, regs);
682         } else if (!strcmp(cmd, "allregs")) {
683                 fiq_debugger_dump_allregs(&state->output, regs);
684         } else if (!strcmp(cmd, "bt")) {
685                 fiq_debugger_dump_stacktrace(&state->output, regs, 100, svc_sp);
686         } else if (!strncmp(cmd, "reset", 5)) {
687                 cmd += 5;
688                 while (*cmd == ' ')
689                         cmd++;
690                 if (*cmd) {
691                         char tmp_cmd[32];
692                         strlcpy(tmp_cmd, cmd, sizeof(tmp_cmd));
693                         machine_restart(tmp_cmd);
694                 } else {
695                         machine_restart(NULL);
696                 }
697         } else if (!strcmp(cmd, "irqs")) {
698                 fiq_debugger_dump_irqs(state);
699         } else if (!strcmp(cmd, "kmsg")) {
700                 fiq_debugger_dump_kernel_log(state);
701 #ifdef CONFIG_RK_LAST_LOG
702         } else if (!strcmp(cmd, "last_kmsg")) {
703                 fiq_debugger_dump_last_kernel_log(state);
704 #endif
705         } else if (!strcmp(cmd, "version")) {
706                 fiq_debugger_printf(&state->output, "%s\n", linux_banner);
707         } else if (!strcmp(cmd, "sleep")) {
708                 state->no_sleep = false;
709                 fiq_debugger_printf(&state->output, "enabling sleep\n");
710         } else if (!strcmp(cmd, "nosleep")) {
711                 state->no_sleep = true;
712                 fiq_debugger_printf(&state->output, "disabling sleep\n");
713         } else if (!strcmp(cmd, "console")) {
714                 fiq_debugger_printf(&state->output, "console mode\n");
715                 fiq_debugger_uart_flush(state);
716                 state->console_enable = true;
717         } else if (!strcmp(cmd, "cpu")) {
718                 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
719         } else if (!strncmp(cmd, "cpu ", 4)) {
720                 unsigned long cpu = 0;
721                 if (strict_strtoul(cmd + 4, 10, &cpu) == 0)
722                         fiq_debugger_switch_cpu(state, cpu);
723                 else
724                         fiq_debugger_printf(&state->output, "invalid cpu\n");
725                 fiq_debugger_printf(&state->output, "cpu %d\n", state->current_cpu);
726         } else {
727                 if (state->debug_busy) {
728                         fiq_debugger_printf(&state->output,
729                                 "command processor busy. trying to abort.\n");
730                         state->debug_abort = -1;
731                 } else {
732                         strcpy(state->debug_cmd, cmd);
733                         state->debug_busy = 1;
734                 }
735
736                 return true;
737         }
738         if (!state->console_enable)
739                 fiq_debugger_prompt(state);
740
741         return signal_helper;
742 }
743
744 static void fiq_debugger_sleep_timer_expired(unsigned long data)
745 {
746         struct fiq_debugger_state *state = (struct fiq_debugger_state *)data;
747         unsigned long flags;
748
749         spin_lock_irqsave(&state->sleep_timer_lock, flags);
750         if (state->uart_enabled && !state->no_sleep) {
751                 if (state->debug_enable && !state->console_enable) {
752                         state->debug_enable = false;
753                         fiq_debugger_printf_nfiq(state,
754                                         "suspending fiq debugger\n");
755                 }
756                 state->ignore_next_wakeup_irq = true;
757                 fiq_debugger_uart_disable(state);
758                 state->uart_enabled = false;
759                 fiq_debugger_enable_wakeup_irq(state);
760         }
761         wake_unlock(&state->debugger_wake_lock);
762         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
763 }
764
765 static void fiq_debugger_handle_wakeup(struct fiq_debugger_state *state)
766 {
767         unsigned long flags;
768
769         spin_lock_irqsave(&state->sleep_timer_lock, flags);
770         if (state->wakeup_irq >= 0 && state->ignore_next_wakeup_irq) {
771                 state->ignore_next_wakeup_irq = false;
772         } else if (!state->uart_enabled) {
773                 wake_lock(&state->debugger_wake_lock);
774                 fiq_debugger_uart_enable(state);
775                 state->uart_enabled = true;
776                 fiq_debugger_disable_wakeup_irq(state);
777                 mod_timer(&state->sleep_timer, jiffies + HZ / 2);
778         }
779         spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
780 }
781
782 static irqreturn_t fiq_debugger_wakeup_irq_handler(int irq, void *dev)
783 {
784         struct fiq_debugger_state *state = dev;
785
786         if (!state->no_sleep)
787                 fiq_debugger_puts(state, "WAKEUP\n");
788         fiq_debugger_handle_wakeup(state);
789
790         return IRQ_HANDLED;
791 }
792
793 static
794 void fiq_debugger_handle_console_irq_context(struct fiq_debugger_state *state)
795 {
796 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
797         if (state->tty_port.ops) {
798                 int i;
799                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
800                 for (i = 0; i < count; i++) {
801                         int c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
802                         tty_insert_flip_char(&state->tty_port, c, TTY_NORMAL);
803                         if (!fiq_debugger_ringbuf_consume(state->tty_rbuf, 1))
804                                 pr_warn("fiq tty failed to consume byte\n");
805                 }
806                 tty_flip_buffer_push(&state->tty_port);
807         }
808 #endif
809 }
810
811 static void fiq_debugger_handle_irq_context(struct fiq_debugger_state *state)
812 {
813         if (!state->no_sleep) {
814                 unsigned long flags;
815
816                 spin_lock_irqsave(&state->sleep_timer_lock, flags);
817                 wake_lock(&state->debugger_wake_lock);
818                 mod_timer(&state->sleep_timer, jiffies + HZ * 5);
819                 spin_unlock_irqrestore(&state->sleep_timer_lock, flags);
820         }
821         fiq_debugger_handle_console_irq_context(state);
822         if (state->debug_busy) {
823                 fiq_debugger_irq_exec(state, state->debug_cmd);
824                 if (!state->console_enable)
825                         fiq_debugger_prompt(state);
826                 state->debug_busy = 0;
827         }
828 }
829
830 static int fiq_debugger_getc(struct fiq_debugger_state *state)
831 {
832         return state->pdata->uart_getc(state->pdev);
833 }
834
835 static int fiq_debugger_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                                 fiq_debugger_printf(&state->output, 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                                         fiq_debugger_printf(&state->output, 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 fiq_debugger_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                 fiq_debugger_printf(&state->output, state->debug_buf);
933
934         }
935 }
936
937 static bool fiq_debugger_handle_uart_interrupt(struct fiq_debugger_state *state,
938                         int this_cpu, const struct pt_regs *regs, void *svc_sp)
939 {
940         int c;
941         static int last_c;
942         int count = 0;
943         bool signal_helper = false;
944         unsigned long ms = 0;
945
946         if (this_cpu != state->current_cpu) {
947                 if (state->in_fiq)
948                         return false;
949
950                 if (atomic_inc_return(&state->unhandled_fiq_count) !=
951                                         MAX_UNHANDLED_FIQ_COUNT)
952                         return false;
953
954                 fiq_debugger_printf(&state->output,
955                         "fiq_debugger: cpu %d not responding, "
956                         "reverting to cpu %d\n", state->current_cpu,
957                         this_cpu);
958
959                 atomic_set(&state->unhandled_fiq_count, 0);
960                 fiq_debugger_switch_cpu(state, this_cpu);
961                 return false;
962         }
963
964         state->in_fiq = true;
965
966         while ((c = fiq_debugger_getc(state)) != FIQ_DEBUGGER_NO_CHAR) {
967                 recv_count0++;
968                 if((recv_count0 - recv_count1) > 128) {
969                         ms = jiffies_to_msecs(jiffies - jif);
970                         if(ms < 1000) {
971                                 if(cpu_is_rk3288()){
972                                         writel_relaxed((0x00c0 << 16),
973                                         RK_GRF_VIRT + RK3288_GRF_UOC0_CON3);
974                                 }
975                         }
976                         jif = jiffies;
977                         recv_count1 = recv_count0;
978                 }
979                 count++;
980                 if (!state->debug_enable) {
981                         if ((c == 13) || (c == 10)) {
982                                 state->debug_enable = true;
983                                 state->debug_count = 0;
984                                 fiq_debugger_prompt(state);
985                         }
986                 } else if (c == FIQ_DEBUGGER_BREAK) {
987                         state->console_enable = false;
988 #ifdef CONFIG_ARCH_ROCKCHIP
989                         fiq_debugger_puts(state, "\nWelcome to ");
990 #endif
991                         fiq_debugger_puts(state, "fiq debugger mode\n");
992                         state->debug_count = 0;
993 #ifdef CONFIG_ARCH_ROCKCHIP
994                         fiq_debugger_puts(state, "Enter ? to get command help\n");
995                         state->back_pointer = CMD_COUNT;
996                         state->current_pointer = CMD_COUNT;
997                         memset(state->cmd_buf, 0, (CMD_COUNT+1)*DEBUG_MAX);
998 #endif
999                         fiq_debugger_prompt(state);
1000 #ifdef CONFIG_FIQ_DEBUGGER_CONSOLE
1001                 } else if (state->console_enable && state->tty_rbuf) {
1002                         fiq_debugger_ringbuf_push(state->tty_rbuf, c);
1003                         signal_helper = true;
1004 #endif
1005 #ifdef CONFIG_ARCH_ROCKCHIP
1006                 } else if (last_c == '[' && (c == 'A' || c == 'B' || c == 'C' || c == 'D')) {
1007                         if (state->debug_count > 0) {
1008                                 state->debug_count--;
1009                                 state->pdata->uart_putc(state->pdev, 8);
1010                                 state->pdata->uart_putc(state->pdev, ' ');
1011                                 state->pdata->uart_putc(state->pdev, 8);
1012                         }
1013                         fiq_debugger_cmd_check_back(state, c);
1014                         //tab
1015                 } else if (c == 9) {
1016                         fiq_debugger_cmd_tab(state);
1017 #endif
1018                 } else if ((c >= ' ') && (c < 127)) {
1019                         if (state->debug_count < (DEBUG_MAX - 1)) {
1020                                 state->debug_buf[state->debug_count++] = c;
1021                                 fiq_debugger_putc(state, c);
1022                         }
1023                 } else if ((c == 8) || (c == 127)) {
1024                         if (state->debug_count > 0) {
1025                                 state->debug_count--;
1026                                 fiq_debugger_putc(state, 8);
1027                                 fiq_debugger_putc(state, ' ');
1028                                 fiq_debugger_putc(state, 8);
1029                         }
1030                 } else if ((c == 13) || (c == 10)) {
1031                         if (c == '\r' || (c == '\n' && last_c != '\r')) {
1032                                 fiq_debugger_putc(state, '\r');
1033                                 fiq_debugger_putc(state, '\n');
1034                         }
1035                         if (state->debug_count) {
1036                                 state->debug_buf[state->debug_count] = 0;
1037                                 state->debug_count = 0;
1038                                 signal_helper |=
1039                                         fiq_debugger_fiq_exec(state,
1040                                                         state->debug_buf,
1041                                                        regs, svc_sp);
1042 #ifdef CONFIG_ARCH_ROCKCHIP
1043                                 if (signal_helper == false) {
1044                                         state->current_pointer = (state->current_pointer-1) & CMD_COUNT;
1045                                         if (strcmp(state->cmd_buf[state->current_pointer], state->debug_buf)) {
1046                                                 state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
1047                                                 memset(state->cmd_buf[state->current_pointer], 0, DEBUG_MAX);
1048                                                 strcpy(state->cmd_buf[state->current_pointer], state->debug_buf);
1049                                         }
1050                                         memset(state->debug_buf, 0, DEBUG_MAX);
1051                                         state->current_pointer = (state->current_pointer+1) & CMD_COUNT;
1052                                         state->back_pointer = state->current_pointer;
1053                                 }
1054 #endif
1055                         } else {
1056                                 fiq_debugger_prompt(state);
1057                         }
1058                 }
1059                 last_c = c;
1060         }
1061         if (!state->console_enable)
1062                 fiq_debugger_uart_flush(state);
1063         if (state->pdata->fiq_ack)
1064                 state->pdata->fiq_ack(state->pdev, state->fiq);
1065
1066         /* poke sleep timer if necessary */
1067         if (state->debug_enable && !state->no_sleep)
1068                 signal_helper = true;
1069
1070         atomic_set(&state->unhandled_fiq_count, 0);
1071         state->in_fiq = false;
1072
1073         return signal_helper;
1074 }
1075
1076 #ifdef CONFIG_FIQ_GLUE
1077 static void fiq_debugger_fiq(struct fiq_glue_handler *h,
1078                 void *regs, void *svc_sp)
1079 {
1080         struct fiq_debugger_state *state =
1081                 container_of(h, struct fiq_debugger_state, handler);
1082         unsigned int this_cpu = THREAD_INFO(svc_sp)->cpu;
1083         bool need_irq;
1084
1085         /* RK2928 USB-UART function, otg dp/dm default in uart status;
1086          * connect with otg cable&usb device, dp/dm will be hi-z status 
1087          * and make uart controller enter infinite fiq loop 
1088          */
1089 #ifdef CONFIG_RK_USB_UART
1090         if (cpu_is_rk3188()) {
1091                 if (!(readl_relaxed(RK_GRF_VIRT + RK3188_GRF_SOC_STATUS0) & (1 << 13)) ||
1092                      (readl_relaxed(RK_GRF_VIRT + RK3188_GRF_SOC_STATUS0) & (1 << 10))) {
1093                         /* id low or bvalid high, enter usb phy */
1094                         writel_relaxed((0x0300 << 16), RK_GRF_VIRT + RK3188_GRF_UOC0_CON0);
1095                 }
1096         } else if (cpu_is_rk3288()) {
1097                 if (!(readl_relaxed(RK_GRF_VIRT + RK3288_GRF_SOC_STATUS2) & (1 << 17)) ||
1098                      (readl_relaxed(RK_GRF_VIRT + RK3288_GRF_SOC_STATUS2) & (1 << 14))) {
1099                         /* id low or bvalid high, enter usb phy */
1100                         writel_relaxed((0x00c0 << 16), RK_GRF_VIRT + RK3288_GRF_UOC0_CON3);
1101                 }
1102         }
1103 #endif
1104         need_irq = fiq_debugger_handle_uart_interrupt(state, this_cpu, regs,
1105                         svc_sp);
1106         if (need_irq)
1107                 fiq_debugger_force_irq(state);
1108 }
1109 #endif
1110
1111 /*
1112  * When not using FIQs, we only use this single interrupt as an entry point.
1113  * This just effectively takes over the UART interrupt and does all the work
1114  * in this context.
1115  */
1116 static irqreturn_t fiq_debugger_uart_irq(int irq, void *dev)
1117 {
1118         struct fiq_debugger_state *state = dev;
1119         bool not_done;
1120
1121         fiq_debugger_handle_wakeup(state);
1122
1123         /* handle the debugger irq in regular context */
1124         not_done = fiq_debugger_handle_uart_interrupt(state, smp_processor_id(),
1125                                               get_irq_regs(),
1126                                               current_thread_info());
1127         if (not_done)
1128                 fiq_debugger_handle_irq_context(state);
1129
1130         return IRQ_HANDLED;
1131 }
1132
1133 /*
1134  * If FIQs are used, not everything can happen in fiq context.
1135  * FIQ handler does what it can and then signals this interrupt to finish the
1136  * job in irq context.
1137  */
1138 static irqreturn_t fiq_debugger_signal_irq(int irq, void *dev)
1139 {
1140         struct fiq_debugger_state *state = dev;
1141
1142         if (state->pdata->force_irq_ack)
1143                 state->pdata->force_irq_ack(state->pdev, state->signal_irq);
1144
1145         fiq_debugger_handle_irq_context(state);
1146
1147         return IRQ_HANDLED;
1148 }
1149
1150 #ifdef CONFIG_FIQ_GLUE
1151 static void fiq_debugger_resume(struct fiq_glue_handler *h)
1152 {
1153         struct fiq_debugger_state *state =
1154                 container_of(h, struct fiq_debugger_state, handler);
1155         if (state->pdata->uart_resume)
1156                 state->pdata->uart_resume(state->pdev);
1157 }
1158 #endif
1159
1160 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1161 struct tty_driver *fiq_debugger_console_device(struct console *co, int *index)
1162 {
1163         *index = co->index;
1164         return fiq_tty_driver;
1165 }
1166
1167 static void fiq_debugger_console_write(struct console *co,
1168                                 const char *s, unsigned int count)
1169 {
1170         struct fiq_debugger_state *state;
1171         unsigned long flags;
1172
1173         state = container_of(co, struct fiq_debugger_state, console);
1174
1175         if (!state->console_enable && !state->syslog_dumping)
1176                 return;
1177
1178 #ifdef CONFIG_RK_CONSOLE_THREAD
1179         if (state->pdata->console_write) {
1180                 state->pdata->console_write(state->pdev, s, count);
1181                 return;
1182         }
1183 #endif
1184
1185         fiq_debugger_uart_enable(state);
1186         spin_lock_irqsave(&state->console_lock, flags);
1187         while (count--) {
1188                 if (*s == '\n')
1189                         fiq_debugger_putc(state, '\r');
1190                 fiq_debugger_putc(state, *s++);
1191         }
1192         fiq_debugger_uart_flush(state);
1193         spin_unlock_irqrestore(&state->console_lock, flags);
1194         fiq_debugger_uart_disable(state);
1195 }
1196
1197 static struct console fiq_debugger_console = {
1198         .name = "ttyFIQ",
1199         .device = fiq_debugger_console_device,
1200         .write = fiq_debugger_console_write,
1201         .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED,
1202 };
1203
1204 int fiq_tty_open(struct tty_struct *tty, struct file *filp)
1205 {
1206         int line = tty->index;
1207         struct fiq_debugger_state **states = tty->driver->driver_state;
1208         struct fiq_debugger_state *state = states[line];
1209
1210         return tty_port_open(&state->tty_port, tty, filp);
1211 }
1212
1213 void fiq_tty_close(struct tty_struct *tty, struct file *filp)
1214 {
1215         tty_port_close(tty->port, tty, filp);
1216 }
1217
1218 int  fiq_tty_write(struct tty_struct *tty, const unsigned char *buf, int count)
1219 {
1220         int i;
1221         int line = tty->index;
1222         struct fiq_debugger_state **states = tty->driver->driver_state;
1223         struct fiq_debugger_state *state = states[line];
1224
1225         if (!state->console_enable)
1226                 return count;
1227
1228         fiq_debugger_uart_enable(state);
1229         spin_lock_irq(&state->console_lock);
1230         for (i = 0; i < count; i++)
1231                 fiq_debugger_putc(state, *buf++);
1232         spin_unlock_irq(&state->console_lock);
1233         fiq_debugger_uart_disable(state);
1234
1235         return count;
1236 }
1237
1238 int  fiq_tty_write_room(struct tty_struct *tty)
1239 {
1240         return 16;
1241 }
1242
1243 #ifdef CONFIG_CONSOLE_POLL
1244 static int fiq_tty_poll_init(struct tty_driver *driver, int line, char *options)
1245 {
1246         return 0;
1247 }
1248
1249 static int fiq_tty_poll_get_char(struct tty_driver *driver, int line)
1250 {
1251         struct fiq_debugger_state **states = driver->driver_state;
1252         struct fiq_debugger_state *state = states[line];
1253         int c = NO_POLL_CHAR;
1254
1255         fiq_debugger_uart_enable(state);
1256         if (fiq_debugger_have_fiq(state)) {
1257                 int count = fiq_debugger_ringbuf_level(state->tty_rbuf);
1258                 if (count > 0) {
1259                         c = fiq_debugger_ringbuf_peek(state->tty_rbuf, 0);
1260                         fiq_debugger_ringbuf_consume(state->tty_rbuf, 1);
1261                 }
1262         } else {
1263                 c = fiq_debugger_getc(state);
1264                 if (c == FIQ_DEBUGGER_NO_CHAR)
1265                         c = NO_POLL_CHAR;
1266         }
1267         fiq_debugger_uart_disable(state);
1268
1269         return c;
1270 }
1271
1272 static void fiq_tty_poll_put_char(struct tty_driver *driver, int line, char ch)
1273 {
1274         struct fiq_debugger_state **states = driver->driver_state;
1275         struct fiq_debugger_state *state = states[line];
1276         fiq_debugger_uart_enable(state);
1277         fiq_debugger_putc(state, ch);
1278         fiq_debugger_uart_disable(state);
1279 }
1280 #endif
1281
1282 static const struct tty_port_operations fiq_tty_port_ops;
1283
1284 static const struct tty_operations fiq_tty_driver_ops = {
1285         .write = fiq_tty_write,
1286         .write_room = fiq_tty_write_room,
1287         .open = fiq_tty_open,
1288         .close = fiq_tty_close,
1289 #ifdef CONFIG_CONSOLE_POLL
1290         .poll_init = fiq_tty_poll_init,
1291         .poll_get_char = fiq_tty_poll_get_char,
1292         .poll_put_char = fiq_tty_poll_put_char,
1293 #endif
1294 };
1295
1296 static int fiq_debugger_tty_init(void)
1297 {
1298         int ret;
1299         struct fiq_debugger_state **states = NULL;
1300
1301         states = kzalloc(sizeof(*states) * MAX_FIQ_DEBUGGER_PORTS, GFP_KERNEL);
1302         if (!states) {
1303                 pr_err("Failed to allocate fiq debugger state structres\n");
1304                 return -ENOMEM;
1305         }
1306
1307         fiq_tty_driver = alloc_tty_driver(MAX_FIQ_DEBUGGER_PORTS);
1308         if (!fiq_tty_driver) {
1309                 pr_err("Failed to allocate fiq debugger tty\n");
1310                 ret = -ENOMEM;
1311                 goto err_free_state;
1312         }
1313
1314         fiq_tty_driver->owner           = THIS_MODULE;
1315         fiq_tty_driver->driver_name     = "fiq-debugger";
1316         fiq_tty_driver->name            = "ttyFIQ";
1317         fiq_tty_driver->type            = TTY_DRIVER_TYPE_SERIAL;
1318         fiq_tty_driver->subtype         = SERIAL_TYPE_NORMAL;
1319         fiq_tty_driver->init_termios    = tty_std_termios;
1320         fiq_tty_driver->flags           = TTY_DRIVER_REAL_RAW |
1321                                           TTY_DRIVER_DYNAMIC_DEV;
1322         fiq_tty_driver->driver_state    = states;
1323
1324         fiq_tty_driver->init_termios.c_cflag =
1325                                         B115200 | CS8 | CREAD | HUPCL | CLOCAL;
1326         fiq_tty_driver->init_termios.c_ispeed = 115200;
1327         fiq_tty_driver->init_termios.c_ospeed = 115200;
1328
1329         tty_set_operations(fiq_tty_driver, &fiq_tty_driver_ops);
1330
1331         ret = tty_register_driver(fiq_tty_driver);
1332         if (ret) {
1333                 pr_err("Failed to register fiq tty: %d\n", ret);
1334                 goto err_free_tty;
1335         }
1336
1337         pr_info("Registered FIQ tty driver\n");
1338         return 0;
1339
1340 err_free_tty:
1341         put_tty_driver(fiq_tty_driver);
1342         fiq_tty_driver = NULL;
1343 err_free_state:
1344         kfree(states);
1345         return ret;
1346 }
1347
1348 static int fiq_debugger_tty_init_one(struct fiq_debugger_state *state)
1349 {
1350         int ret;
1351         struct device *tty_dev;
1352         struct fiq_debugger_state **states = fiq_tty_driver->driver_state;
1353
1354         states[state->pdev->id] = state;
1355
1356         state->tty_rbuf = fiq_debugger_ringbuf_alloc(1024);
1357         if (!state->tty_rbuf) {
1358                 pr_err("Failed to allocate fiq debugger ringbuf\n");
1359                 ret = -ENOMEM;
1360                 goto err;
1361         }
1362
1363         tty_port_init(&state->tty_port);
1364         state->tty_port.ops = &fiq_tty_port_ops;
1365
1366         tty_dev = tty_port_register_device(&state->tty_port, fiq_tty_driver,
1367                                            state->pdev->id, &state->pdev->dev);
1368         if (IS_ERR(tty_dev)) {
1369                 pr_err("Failed to register fiq debugger tty device\n");
1370                 ret = PTR_ERR(tty_dev);
1371                 goto err;
1372         }
1373
1374         device_set_wakeup_capable(tty_dev, 1);
1375
1376         pr_info("Registered fiq debugger ttyFIQ%d\n", state->pdev->id);
1377
1378         return 0;
1379
1380 err:
1381         fiq_debugger_ringbuf_free(state->tty_rbuf);
1382         state->tty_rbuf = NULL;
1383         return ret;
1384 }
1385 #endif
1386
1387 static int fiq_debugger_dev_suspend(struct device *dev)
1388 {
1389         struct platform_device *pdev = to_platform_device(dev);
1390         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1391
1392         if (state->pdata->uart_dev_suspend)
1393                 return state->pdata->uart_dev_suspend(pdev);
1394         return 0;
1395 }
1396
1397 static int fiq_debugger_dev_resume(struct device *dev)
1398 {
1399         struct platform_device *pdev = to_platform_device(dev);
1400         struct fiq_debugger_state *state = platform_get_drvdata(pdev);
1401
1402         if (state->pdata->uart_dev_resume)
1403                 return state->pdata->uart_dev_resume(pdev);
1404         return 0;
1405 }
1406
1407 static int fiq_debugger_probe(struct platform_device *pdev)
1408 {
1409         int ret;
1410         struct fiq_debugger_pdata *pdata = dev_get_platdata(&pdev->dev);
1411         struct fiq_debugger_state *state;
1412         int fiq;
1413         int uart_irq;
1414
1415         if (pdev->id >= MAX_FIQ_DEBUGGER_PORTS)
1416                 return -EINVAL;
1417
1418         if (!pdata->uart_getc || !pdata->uart_putc)
1419                 return -EINVAL;
1420         if ((pdata->uart_enable && !pdata->uart_disable) ||
1421             (!pdata->uart_enable && pdata->uart_disable))
1422                 return -EINVAL;
1423
1424         fiq = platform_get_irq_byname(pdev, "fiq");
1425         uart_irq = platform_get_irq_byname(pdev, "uart_irq");
1426
1427         /* uart_irq mode and fiq mode are mutually exclusive, but one of them
1428          * is required */
1429         if ((uart_irq < 0 && fiq < 0) || (uart_irq >= 0 && fiq >= 0))
1430                 return -EINVAL;
1431         if (fiq >= 0 && !pdata->fiq_enable)
1432                 return -EINVAL;
1433
1434         state = kzalloc(sizeof(*state), GFP_KERNEL);
1435         state->output.printf = fiq_debugger_printf;
1436         setup_timer(&state->sleep_timer, fiq_debugger_sleep_timer_expired,
1437                     (unsigned long)state);
1438         state->pdata = pdata;
1439         state->pdev = pdev;
1440         state->no_sleep = initial_no_sleep;
1441         state->debug_enable = initial_debug_enable;
1442         state->console_enable = initial_console_enable;
1443
1444         state->fiq = fiq;
1445         state->uart_irq = uart_irq;
1446         state->signal_irq = platform_get_irq_byname(pdev, "signal");
1447         state->wakeup_irq = platform_get_irq_byname(pdev, "wakeup");
1448
1449         INIT_WORK(&state->work, fiq_debugger_work);
1450         spin_lock_init(&state->work_lock);
1451
1452         platform_set_drvdata(pdev, state);
1453
1454         spin_lock_init(&state->sleep_timer_lock);
1455
1456         if (state->wakeup_irq < 0 && fiq_debugger_have_fiq(state))
1457                 state->no_sleep = true;
1458         state->ignore_next_wakeup_irq = !state->no_sleep;
1459
1460         wake_lock_init(&state->debugger_wake_lock,
1461                         WAKE_LOCK_SUSPEND, "serial-debug");
1462
1463         state->clk = clk_get(&pdev->dev, NULL);
1464         if (IS_ERR(state->clk))
1465                 state->clk = NULL;
1466
1467         /* do not call pdata->uart_enable here since uart_init may still
1468          * need to do some initialization before uart_enable can work.
1469          * So, only try to manage the clock during init.
1470          */
1471         if (state->clk)
1472                 clk_enable(state->clk);
1473
1474         if (pdata->uart_init) {
1475                 ret = pdata->uart_init(pdev);
1476                 if (ret)
1477                         goto err_uart_init;
1478         }
1479
1480         fiq_debugger_printf_nfiq(state,
1481                                 "<hit enter %sto activate fiq debugger>\n",
1482                                 state->no_sleep ? "" : "twice ");
1483
1484 #ifdef CONFIG_FIQ_GLUE
1485         if (fiq_debugger_have_fiq(state)) {
1486                 state->handler.fiq = fiq_debugger_fiq;
1487                 state->handler.resume = fiq_debugger_resume;
1488                 ret = fiq_glue_register_handler(&state->handler);
1489                 if (ret) {
1490                         pr_err("%s: could not install fiq handler\n", __func__);
1491                         goto err_register_irq;
1492                 }
1493 #ifdef CONFIG_ARCH_ROCKCHIP
1494                 //set state->fiq to secure state, so fiq is avalable
1495                 gic_set_irq_secure(irq_get_irq_data(state->fiq));
1496                 //set state->fiq priority a little higher than other interrupts (normal is 0xa0)
1497                 gic_set_irq_priority(irq_get_irq_data(state->fiq), 0x90);
1498 #endif
1499                 pdata->fiq_enable(pdev, state->fiq, 1);
1500         } else
1501 #endif
1502         {
1503                 ret = request_irq(state->uart_irq, fiq_debugger_uart_irq,
1504                                   IRQF_NO_SUSPEND, "debug", state);
1505                 if (ret) {
1506                         pr_err("%s: could not install irq handler\n", __func__);
1507                         goto err_register_irq;
1508                 }
1509
1510                 /* for irq-only mode, we want this irq to wake us up, if it
1511                  * can.
1512                  */
1513                 enable_irq_wake(state->uart_irq);
1514         }
1515
1516         if (state->clk)
1517                 clk_disable(state->clk);
1518
1519         if (state->signal_irq >= 0) {
1520                 ret = request_irq(state->signal_irq, fiq_debugger_signal_irq,
1521                           IRQF_TRIGGER_RISING, "debug-signal", state);
1522                 if (ret)
1523                         pr_err("serial_debugger: could not install signal_irq");
1524         }
1525
1526         if (state->wakeup_irq >= 0) {
1527                 ret = request_irq(state->wakeup_irq,
1528                                   fiq_debugger_wakeup_irq_handler,
1529                                   IRQF_TRIGGER_FALLING | IRQF_DISABLED,
1530                                   "debug-wakeup", state);
1531                 if (ret) {
1532                         pr_err("serial_debugger: "
1533                                 "could not install wakeup irq\n");
1534                         state->wakeup_irq = -1;
1535                 } else {
1536                         ret = enable_irq_wake(state->wakeup_irq);
1537                         if (ret) {
1538                                 pr_err("serial_debugger: "
1539                                         "could not enable wakeup\n");
1540                                 state->wakeup_irq_no_set_wake = true;
1541                         }
1542                 }
1543         }
1544         if (state->no_sleep)
1545                 fiq_debugger_handle_wakeup(state);
1546
1547 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1548         spin_lock_init(&state->console_lock);
1549         state->console = fiq_debugger_console;
1550         state->console.index = pdev->id;
1551         if (!console_set_on_cmdline)
1552                 add_preferred_console(state->console.name,
1553                         state->console.index, NULL);
1554         register_console(&state->console);
1555         fiq_debugger_tty_init_one(state);
1556 #endif
1557         return 0;
1558
1559 err_register_irq:
1560         if (pdata->uart_free)
1561                 pdata->uart_free(pdev);
1562 err_uart_init:
1563         if (state->clk)
1564                 clk_disable(state->clk);
1565         if (state->clk)
1566                 clk_put(state->clk);
1567         wake_lock_destroy(&state->debugger_wake_lock);
1568         platform_set_drvdata(pdev, NULL);
1569         kfree(state);
1570         return ret;
1571 }
1572
1573 static const struct dev_pm_ops fiq_debugger_dev_pm_ops = {
1574         .suspend        = fiq_debugger_dev_suspend,
1575         .resume         = fiq_debugger_dev_resume,
1576 };
1577
1578 static struct platform_driver fiq_debugger_driver = {
1579         .probe  = fiq_debugger_probe,
1580         .driver = {
1581                 .name   = "fiq_debugger",
1582                 .pm     = &fiq_debugger_dev_pm_ops,
1583         },
1584 };
1585
1586 static int __init fiq_debugger_init(void)
1587 {
1588 #if defined(CONFIG_FIQ_DEBUGGER_CONSOLE)
1589         fiq_debugger_tty_init();
1590 #endif
1591         return platform_driver_register(&fiq_debugger_driver);
1592 }
1593
1594 postcore_initcall(fiq_debugger_init);