2 * drivers/soc/rockchip/rk_fiq_debugger.c
4 * Serial Debugger Interface for Rockchip
6 * Copyright (C) 2012 ROCKCHIP, Inc.
7 * Copyright (C) 2008 Google, Inc.
9 * This software is licensed under the terms of the GNU General Public
10 * License version 2, as published by the Free Software Foundation, and
11 * may be copied, distributed, and modified under those terms.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
20 #include <linux/module.h>
23 #include <linux/of_address.h>
24 #include <linux/of_irq.h>
25 #include <linux/interrupt.h>
26 #include <linux/clk.h>
27 #include <linux/platform_device.h>
28 #include <linux/irq.h>
29 #include <linux/serial_reg.h>
30 #include <linux/slab.h>
31 #include <linux/stacktrace.h>
32 #include <linux/uaccess.h>
33 #include <linux/kfifo.h>
34 #include <linux/kthread.h>
35 #include <linux/sched/rt.h>
36 #include <../drivers/staging/android/fiq_debugger/fiq_debugger.h>
37 #include <linux/irqchip/arm-gic.h>
38 #include <linux/clk.h>
39 #include <linux/delay.h>
40 #include <linux/soc/rockchip/rk_fiq_debugger.h>
42 #ifdef CONFIG_FIQ_DEBUGGER_EL3_TO_EL1
43 #include <linux/rockchip/rockchip_sip.h>
46 #define UART_USR 0x1f /* In: UART Status Register */
47 #define UART_USR_RX_FIFO_FULL 0x10 /* Receive FIFO full */
48 #define UART_USR_RX_FIFO_NOT_EMPTY 0x08 /* Receive FIFO not empty */
49 #define UART_USR_TX_FIFO_EMPTY 0x04 /* Transmit FIFO empty */
50 #define UART_USR_TX_FIFO_NOT_FULL 0x02 /* Transmit FIFO not full */
51 #define UART_USR_BUSY 0x01 /* UART busy indicator */
52 #define UART_SRR 0x22 /* software reset register */
54 struct rk_fiq_debugger {
57 struct fiq_debugger_pdata pdata;
58 void __iomem *debug_port_base;
60 #ifdef CONFIG_RK_CONSOLE_THREAD
61 struct task_struct *console_task;
65 static int rk_fiq_debugger_id;
68 #ifdef CONFIG_FIQ_DEBUGGER_EL3_TO_EL1
69 static bool tf_fiq_sup;
72 static inline void rk_fiq_write(struct rk_fiq_debugger *t,
73 unsigned int val, unsigned int off)
75 __raw_writel(val, t->debug_port_base + off * 4);
78 static inline unsigned int rk_fiq_read(struct rk_fiq_debugger *t,
81 return __raw_readl(t->debug_port_base + off * 4);
84 static inline unsigned int rk_fiq_read_lsr(struct rk_fiq_debugger *t)
88 lsr = rk_fiq_read(t, UART_LSR);
89 if (lsr & UART_LSR_BI)
95 static int debug_port_init(struct platform_device *pdev)
98 struct rk_fiq_debugger *t;
100 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
102 if (rk_fiq_read(t, UART_LSR) & UART_LSR_DR)
103 (void)rk_fiq_read(t, UART_RX);
105 switch (t->baudrate) {
115 rk_fiq_write(t, 0x07, UART_SRR);
117 /* set uart to loop back mode */
118 rk_fiq_write(t, 0x10, UART_MCR);
120 rk_fiq_write(t, 0x83, UART_LCR);
122 rk_fiq_write(t, dll, UART_DLL);
123 rk_fiq_write(t, dlm, UART_DLM);
124 rk_fiq_write(t, 0x03, UART_LCR);
126 /* enable rx and lsr interrupt */
127 rk_fiq_write(t, UART_IER_RLSI | UART_IER_RDI, UART_IER);
128 /* interrupt on every character when receive,but we can enable fifo for TX
129 I found that if we enable the RX fifo, some problem may vanish such as when
130 you continuously input characters in the command line the uart irq may be disable
131 because of the uart irq is served when CPU is at IRQ exception,but it is
132 found unregistered, so it is disable.
133 hhb@rock-chips.com */
134 rk_fiq_write(t, 0xc1, UART_FCR);
135 rk_fiq_write(t, 0x0, UART_MCR);
140 static int debug_getc(struct platform_device *pdev)
143 struct rk_fiq_debugger *t;
145 static unsigned int n;
148 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
150 lsr = rk_fiq_read_lsr(t);
152 if (lsr & UART_LSR_BI || t->break_seen) {
153 t->break_seen = false;
154 return FIQ_DEBUGGER_NO_CHAR;
157 if (lsr & UART_LSR_DR) {
158 temp = rk_fiq_read(t, UART_RX);
159 buf[n & 0x1f] = temp;
161 if (temp == 'q' && n > 2) {
162 if ((buf[(n - 2) & 0x1f] == 'i') &&
163 (buf[(n - 3) & 0x1f] == 'f'))
164 return FIQ_DEBUGGER_BREAK;
172 return FIQ_DEBUGGER_NO_CHAR;
175 static void debug_putc(struct platform_device *pdev, unsigned int c)
177 struct rk_fiq_debugger *t;
179 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
181 while (!(rk_fiq_read(t, UART_USR) & UART_USR_TX_FIFO_NOT_FULL))
183 rk_fiq_write(t, c, UART_TX);
186 static void debug_flush(struct platform_device *pdev)
188 struct rk_fiq_debugger *t;
189 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
191 while (!(rk_fiq_read_lsr(t) & UART_LSR_TEMT))
195 static int uart_dev_resume(struct platform_device *pdev)
197 debug_port_init(pdev);
201 #ifdef CONFIG_RK_CONSOLE_THREAD
202 #define FIFO_SIZE SZ_64K
203 static DEFINE_KFIFO(fifo, unsigned char, FIFO_SIZE);
204 static bool console_thread_stop;
206 static int console_thread(void *data)
208 struct platform_device *pdev = data;
209 struct rk_fiq_debugger *t;
211 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
214 set_current_state(TASK_INTERRUPTIBLE);
216 if (kthread_should_stop())
218 set_current_state(TASK_RUNNING);
219 while (!console_thread_stop && kfifo_get(&fifo, &c))
221 if (!console_thread_stop)
228 static void console_write(struct platform_device *pdev, const char *s, unsigned int count)
230 unsigned int fifo_count = FIFO_SIZE;
231 unsigned char c, r = '\r';
232 struct rk_fiq_debugger *t;
233 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
235 if (console_thread_stop ||
237 system_state == SYSTEM_HALT ||
238 system_state == SYSTEM_POWER_OFF ||
239 system_state == SYSTEM_RESTART) {
240 if (!console_thread_stop) {
241 console_thread_stop = true;
244 while (fifo_count-- && kfifo_get(&fifo, &c))
251 debug_putc(pdev, *s++);
259 kfifo_put(&fifo, *s++);
261 wake_up_process(t->console_task);
267 static void fiq_enable(struct platform_device *pdev, unsigned int irq, bool on)
275 #ifdef CONFIG_FIQ_DEBUGGER_EL3_TO_EL1
276 static struct pt_regs fiq_pt_regs;
278 static void rk_fiq_debugger_switch_cpu(struct platform_device *pdev,
281 psci_fiq_debugger_switch_cpu(cpu);
284 static void rk_fiq_debugger_enable_debug(struct platform_device *pdev, bool val)
286 psci_fiq_debugger_enable_debug(val);
289 static void fiq_debugger_uart_irq_tf(void *reg_base, u64 sp_el1)
291 memcpy(&fiq_pt_regs, reg_base, 8*31);
293 memcpy(&fiq_pt_regs.pstate, reg_base + 0x110, 8);
294 if (fiq_pt_regs.pstate & 0x10)
295 memcpy(&fiq_pt_regs.sp, reg_base + 0xf8, 8);
297 fiq_pt_regs.sp = sp_el1;
299 memcpy(&fiq_pt_regs.pc, reg_base + 0x118, 8);
301 fiq_debugger_fiq(&fiq_pt_regs);
304 static int fiq_debugger_uart_dev_resume(struct platform_device *pdev)
306 struct rk_fiq_debugger *t;
308 t = container_of(dev_get_platdata(&pdev->dev), typeof(*t), pdata);
309 psci_fiq_debugger_uart_irq_tf_init(serial_id, fiq_debugger_uart_irq_tf);
314 void rk_serial_debug_init(void __iomem *base, int irq, int signal_irq,
315 int wakeup_irq, unsigned int baudrate)
317 struct rk_fiq_debugger *t = NULL;
318 struct platform_device *pdev = NULL;
319 struct resource *res = NULL;
321 #ifdef CONFIG_FIQ_DEBUGGER_EL3_TO_EL1
322 /* tf means trust firmware*/
327 pr_err("Invalid fiq debugger uart base\n");
331 t = kzalloc(sizeof(struct rk_fiq_debugger), GFP_KERNEL);
333 pr_err("Failed to allocate for fiq debugger\n");
338 t->baudrate = baudrate;
339 t->pdata.uart_init = debug_port_init;
340 t->pdata.uart_getc = debug_getc;
341 t->pdata.uart_putc = debug_putc;
342 t->pdata.uart_dev_resume = uart_dev_resume;
343 #ifndef CONFIG_RK_CONSOLE_THREAD
344 t->pdata.uart_flush = debug_flush;
346 t->pdata.fiq_enable = fiq_enable;
347 t->pdata.force_irq = NULL;
348 t->debug_port_base = base;
350 res = kzalloc(sizeof(struct resource) * 3, GFP_KERNEL);
352 pr_err("Failed to alloc fiq debugger resources\n");
356 pdev = kzalloc(sizeof(struct platform_device), GFP_KERNEL);
358 pr_err("Failed to alloc fiq debugger platform device\n");
362 #ifdef CONFIG_FIQ_DEBUGGER_EL3_TO_EL1
363 tf_ver = rockchip_psci_smc_get_tf_ver();
365 if (tf_ver >= 0x00010005)
370 if (tf_fiq_sup && (signal_irq > 0)) {
371 t->pdata.switch_cpu = rk_fiq_debugger_switch_cpu;
372 t->pdata.enable_debug = rk_fiq_debugger_enable_debug;
373 t->pdata.uart_dev_resume = fiq_debugger_uart_dev_resume;
374 psci_fiq_debugger_set_print_port(serial_id, 0);
375 psci_fiq_debugger_uart_irq_tf_init(serial_id,
376 fiq_debugger_uart_irq_tf);
378 t->pdata.switch_cpu = NULL;
379 t->pdata.enable_debug = NULL;
384 res[0].flags = IORESOURCE_IRQ;
387 #if defined(CONFIG_FIQ_GLUE)
391 res[0].name = "uart_irq";
392 #elif defined(CONFIG_FIQ_DEBUGGER_EL3_TO_EL1)
393 if (tf_fiq_sup && (signal_irq > 0))
396 res[0].name = "uart_irq";
398 res[0].name = "uart_irq";
403 if (signal_irq > 0) {
404 res[1].flags = IORESOURCE_IRQ;
405 res[1].start = signal_irq;
406 res[1].end = signal_irq;
407 res[1].name = "signal";
411 if (wakeup_irq > 0) {
412 res[2].flags = IORESOURCE_IRQ;
413 res[2].start = wakeup_irq;
414 res[2].end = wakeup_irq;
415 res[2].name = "wakeup";
419 #ifdef CONFIG_RK_CONSOLE_THREAD
420 t->console_task = kthread_create(console_thread, pdev, "kconsole");
421 if (!IS_ERR(t->console_task))
422 t->pdata.console_write = console_write;
425 pdev->name = "fiq_debugger";
426 pdev->id = rk_fiq_debugger_id++;
427 pdev->dev.platform_data = &t->pdata;
428 pdev->resource = res;
429 pdev->num_resources = res_count;
430 if (platform_device_register(pdev)) {
431 pr_err("Failed to register fiq debugger\n");
444 static const struct of_device_id ids[] __initconst = {
445 { .compatible = "rockchip,fiq-debugger" },
449 static int __init rk_fiq_debugger_init(void) {
452 struct device_node *np;
453 unsigned int id, ok = 0;
454 unsigned int irq, signal_irq = 0, wake_irq = 0;
455 unsigned int baudrate = 0, irq_mode = 0;
459 np = of_find_matching_node(NULL, ids);
462 pr_err("fiq-debugger is missing in device tree!\n");
466 if (!of_device_is_available(np)) {
467 pr_err("fiq-debugger is disabled in device tree\n");
471 if (of_property_read_u32(np, "rockchip,serial-id", &serial_id))
474 if (of_property_read_u32(np, "rockchip,irq-mode-enable", &irq_mode))
479 else if (of_property_read_u32(np, "rockchip,signal-irq", &signal_irq))
482 if (of_property_read_u32(np, "rockchip,wake-irq", &wake_irq))
485 if (of_property_read_u32(np, "rockchip,baudrate", &baudrate))
488 if (signal_irq > 0) {
489 signal_irq = irq_of_parse_and_map(np, 0);
497 np = of_find_node_by_name(np, "serial");
499 id = of_alias_get_id(np, "serial");
500 if (id == serial_id) {
510 pclk = of_clk_get_by_name(np, "apb_pclk");
511 clk = of_clk_get_by_name(np, "baudclk");
512 if (unlikely(IS_ERR(clk)) || unlikely(IS_ERR(pclk))) {
513 pr_err("fiq-debugger get clock fail\n");
517 clk_prepare_enable(clk);
518 clk_prepare_enable(pclk);
520 irq = irq_of_parse_and_map(np, 0);
524 base = of_iomap(np, 0);
526 rk_serial_debug_init(base, irq, signal_irq, wake_irq, baudrate);
530 #ifdef CONFIG_FIQ_GLUE
531 postcore_initcall_sync(rk_fiq_debugger_init);
533 arch_initcall_sync(rk_fiq_debugger_init);