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