Bryan Huntsman | 3f2bc4d | 2011-08-16 17:27:22 -0700 | [diff] [blame] | 1 | /* |
| 2 | * drivers/serial/msm_serial_debuger.c |
| 3 | * |
| 4 | * Serial Debugger Interface for MSM7K |
| 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_debugger.h> |
| 26 | #include <linux/kernel_stat.h> |
| 27 | #include <linux/irq.h> |
| 28 | #include <linux/delay.h> |
| 29 | |
| 30 | #include <mach/system.h> |
| 31 | #include <mach/fiq.h> |
| 32 | |
| 33 | #include "msm_serial.h" |
| 34 | |
| 35 | static unsigned int debug_port_base; |
| 36 | static int debug_signal_irq; |
| 37 | static struct clk *debug_clk; |
| 38 | static int debug_enable; |
| 39 | static int debugger_enable; |
| 40 | static struct { |
| 41 | unsigned int base; |
| 42 | int irq; |
| 43 | struct device *clk_device; |
| 44 | int signal_irq; |
| 45 | } init_data; |
| 46 | |
| 47 | static inline void msm_write(unsigned int val, unsigned int off) |
| 48 | { |
| 49 | __raw_writel(val, debug_port_base + off); |
| 50 | } |
| 51 | |
| 52 | static inline unsigned int msm_read(unsigned int off) |
| 53 | { |
| 54 | return __raw_readl(debug_port_base + off); |
| 55 | } |
| 56 | |
| 57 | static void debug_port_init(void) |
| 58 | { |
| 59 | /* reset everything */ |
| 60 | msm_write(UART_CR_CMD_RESET_RX, UART_CR); |
| 61 | msm_write(UART_CR_CMD_RESET_TX, UART_CR); |
| 62 | msm_write(UART_CR_CMD_RESET_ERR, UART_CR); |
| 63 | msm_write(UART_CR_CMD_RESET_BREAK_INT, UART_CR); |
| 64 | msm_write(UART_CR_CMD_RESET_CTS, UART_CR); |
| 65 | msm_write(UART_CR_CMD_SET_RFR, UART_CR); |
| 66 | |
| 67 | /* setup clock dividers */ |
| 68 | if (clk_get_rate(debug_clk) == 19200000) { |
| 69 | /* clock is TCXO (19.2MHz) */ |
| 70 | msm_write(0x06, UART_MREG); |
| 71 | msm_write(0xF1, UART_NREG); |
| 72 | msm_write(0x0F, UART_DREG); |
| 73 | msm_write(0x1A, UART_MNDREG); |
| 74 | } else { |
| 75 | /* clock must be TCXO/4 */ |
| 76 | msm_write(0x18, UART_MREG); |
| 77 | msm_write(0xF6, UART_NREG); |
| 78 | msm_write(0x0F, UART_DREG); |
| 79 | msm_write(0x0A, UART_MNDREG); |
| 80 | } |
| 81 | |
| 82 | msm_write(UART_CSR_115200, UART_CSR); |
| 83 | |
| 84 | /* rx interrupt on every character -- keep it simple */ |
| 85 | msm_write(0, UART_RFWR); |
| 86 | |
| 87 | /* enable TX and RX */ |
| 88 | msm_write(0x05, UART_CR); |
| 89 | |
| 90 | /* enable RX interrupt */ |
| 91 | msm_write(UART_IMR_RXLEV, UART_IMR); |
| 92 | } |
| 93 | |
| 94 | static inline int debug_getc(void) |
| 95 | { |
| 96 | if (msm_read(UART_SR) & UART_SR_RX_READY) { |
| 97 | return msm_read(UART_RF); |
| 98 | } else { |
| 99 | return -1; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | static inline void debug_putc(unsigned int c) |
| 104 | { |
| 105 | while (!(msm_read(UART_SR) & UART_SR_TX_READY)) ; |
| 106 | msm_write(c, UART_TF); |
| 107 | } |
| 108 | |
| 109 | static inline void debug_flush(void) |
| 110 | { |
| 111 | while (!(msm_read(UART_SR) & UART_SR_TX_EMPTY)) ; |
| 112 | } |
| 113 | |
| 114 | static void debug_puts(char *s) |
| 115 | { |
| 116 | unsigned c; |
| 117 | while ((c = *s++)) { |
| 118 | if (c == '\n') |
| 119 | debug_putc('\r'); |
| 120 | debug_putc(c); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | static void debug_prompt(void) |
| 125 | { |
| 126 | debug_puts("debug> "); |
| 127 | } |
| 128 | |
| 129 | int log_buf_copy(char *dest, int idx, int len); |
| 130 | static void dump_kernel_log(void) |
| 131 | { |
| 132 | char buf[1024]; |
| 133 | int idx = 0; |
| 134 | int ret; |
| 135 | int saved_oip; |
| 136 | |
| 137 | /* setting oops_in_progress prevents log_buf_copy() |
| 138 | * from trying to take a spinlock which will make it |
| 139 | * very unhappy in some cases... |
| 140 | */ |
| 141 | saved_oip = oops_in_progress; |
| 142 | oops_in_progress = 1; |
| 143 | for (;;) { |
| 144 | ret = log_buf_copy(buf, idx, 1023); |
| 145 | if (ret <= 0) |
| 146 | break; |
| 147 | buf[ret] = 0; |
| 148 | debug_puts(buf); |
| 149 | idx += ret; |
| 150 | } |
| 151 | oops_in_progress = saved_oip; |
| 152 | } |
| 153 | |
| 154 | static char *mode_name(unsigned cpsr) |
| 155 | { |
| 156 | switch (cpsr & MODE_MASK) { |
| 157 | case USR_MODE: return "USR"; |
| 158 | case FIQ_MODE: return "FIQ"; |
| 159 | case IRQ_MODE: return "IRQ"; |
| 160 | case SVC_MODE: return "SVC"; |
| 161 | case ABT_MODE: return "ABT"; |
| 162 | case UND_MODE: return "UND"; |
| 163 | case SYSTEM_MODE: return "SYS"; |
| 164 | default: return "???"; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | #define DEBUG_MAX 64 |
| 169 | static char debug_cmd[DEBUG_MAX]; |
| 170 | static int debug_busy; |
| 171 | static int debug_abort; |
| 172 | |
| 173 | static int debug_printf(void *cookie, const char *fmt, ...) |
| 174 | { |
| 175 | char buf[256]; |
| 176 | va_list ap; |
| 177 | |
| 178 | va_start(ap, fmt); |
| 179 | vsnprintf(buf, 128, fmt, ap); |
| 180 | va_end(ap); |
| 181 | |
| 182 | debug_puts(buf); |
| 183 | return debug_abort; |
| 184 | } |
| 185 | |
| 186 | /* Safe outside fiq context */ |
| 187 | static int debug_printf_nfiq(void *cookie, const char *fmt, ...) |
| 188 | { |
| 189 | char buf[256]; |
| 190 | va_list ap; |
| 191 | unsigned long irq_flags; |
| 192 | |
| 193 | va_start(ap, fmt); |
| 194 | vsnprintf(buf, 128, fmt, ap); |
| 195 | va_end(ap); |
| 196 | |
| 197 | local_irq_save(irq_flags); |
| 198 | debug_puts(buf); |
| 199 | debug_flush(); |
| 200 | local_irq_restore(irq_flags); |
| 201 | return debug_abort; |
| 202 | } |
| 203 | |
| 204 | #define dprintf(fmt...) debug_printf(0, fmt) |
| 205 | |
| 206 | unsigned int last_irqs[NR_IRQS]; |
| 207 | |
| 208 | static void dump_irqs(void) |
| 209 | { |
| 210 | int n; |
| 211 | dprintf("irqnr total since-last status name\n"); |
| 212 | for (n = 1; n < NR_IRQS; n++) { |
| 213 | struct irqaction *act = irq_desc[n].action; |
| 214 | if (!act && !kstat_cpu(0).irqs[n]) |
| 215 | continue; |
| 216 | dprintf("%5d: %10u %11u %8x %s\n", n, |
| 217 | kstat_cpu(0).irqs[n], |
| 218 | kstat_cpu(0).irqs[n] - last_irqs[n], |
| 219 | irq_desc[n].status, |
| 220 | (act && act->name) ? act->name : "???"); |
| 221 | last_irqs[n] = kstat_cpu(0).irqs[n]; |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | static void debug_exec(const char *cmd, unsigned *regs) |
| 226 | { |
| 227 | if (!strcmp(cmd, "pc")) { |
| 228 | dprintf(" pc %08x cpsr %08x mode %s\n", |
| 229 | regs[15], regs[16], mode_name(regs[16])); |
| 230 | } else if (!strcmp(cmd, "regs")) { |
| 231 | dprintf(" r0 %08x r1 %08x r2 %08x r3 %08x\n", |
| 232 | regs[0], regs[1], regs[2], regs[3]); |
| 233 | dprintf(" r4 %08x r5 %08x r6 %08x r7 %08x\n", |
| 234 | regs[4], regs[5], regs[6], regs[7]); |
| 235 | dprintf(" r8 %08x r9 %08x r10 %08x r11 %08x mode %s\n", |
| 236 | regs[8], regs[9], regs[10], regs[11], |
| 237 | mode_name(regs[16])); |
| 238 | dprintf(" ip %08x sp %08x lr %08x pc %08x cpsr %08x\n", |
| 239 | regs[10], regs[13], regs[14], regs[15], regs[16]); |
| 240 | } else if (!strcmp(cmd, "reboot")) { |
| 241 | if (msm_hw_reset_hook) |
| 242 | msm_hw_reset_hook(); |
| 243 | } else if (!strcmp(cmd, "irqs")) { |
| 244 | dump_irqs(); |
| 245 | } else if (!strcmp(cmd, "kmsg")) { |
| 246 | dump_kernel_log(); |
| 247 | } else if (!strcmp(cmd, "version")) { |
| 248 | dprintf("%s\n", linux_banner); |
| 249 | } else { |
| 250 | if (debug_busy) { |
| 251 | dprintf("command processor busy. trying to abort.\n"); |
| 252 | debug_abort = -1; |
| 253 | } else { |
| 254 | strcpy(debug_cmd, cmd); |
| 255 | debug_busy = 1; |
| 256 | } |
| 257 | msm_trigger_irq(debug_signal_irq); |
| 258 | return; |
| 259 | } |
| 260 | debug_prompt(); |
| 261 | } |
| 262 | |
| 263 | static irqreturn_t debug_irq(int irq, void *dev) |
| 264 | { |
| 265 | if (debug_busy) { |
| 266 | struct kdbg_ctxt ctxt; |
| 267 | |
| 268 | ctxt.printf = debug_printf_nfiq; |
| 269 | kernel_debugger(&ctxt, debug_cmd); |
| 270 | debug_prompt(); |
| 271 | |
| 272 | debug_busy = 0; |
| 273 | } |
| 274 | return IRQ_HANDLED; |
| 275 | } |
| 276 | |
| 277 | static char debug_buf[DEBUG_MAX]; |
| 278 | static int debug_count; |
| 279 | |
| 280 | static void debug_fiq(void *data, void *regs) |
| 281 | { |
| 282 | int c; |
| 283 | static int last_c; |
| 284 | |
| 285 | while ((c = debug_getc()) != -1) { |
| 286 | if (!debug_enable) { |
| 287 | if ((c == 13) || (c == 10)) { |
| 288 | debug_enable = true; |
| 289 | debug_count = 0; |
| 290 | debug_prompt(); |
| 291 | } |
| 292 | } else if ((c >= ' ') && (c < 127)) { |
| 293 | if (debug_count < (DEBUG_MAX - 1)) { |
| 294 | debug_buf[debug_count++] = c; |
| 295 | debug_putc(c); |
| 296 | } |
| 297 | } else if ((c == 8) || (c == 127)) { |
| 298 | if (debug_count > 0) { |
| 299 | debug_count--; |
| 300 | debug_putc(8); |
| 301 | debug_putc(' '); |
| 302 | debug_putc(8); |
| 303 | } |
| 304 | } else if ((c == 13) || (c == 10)) { |
| 305 | if (c == '\r' || (c == '\n' && last_c != '\r')) { |
| 306 | debug_putc('\r'); |
| 307 | debug_putc('\n'); |
| 308 | } |
| 309 | if (debug_count) { |
| 310 | debug_buf[debug_count] = 0; |
| 311 | debug_count = 0; |
| 312 | debug_exec(debug_buf, regs); |
| 313 | } else { |
| 314 | debug_prompt(); |
| 315 | } |
| 316 | } |
| 317 | last_c = c; |
| 318 | } |
| 319 | debug_flush(); |
| 320 | } |
| 321 | |
| 322 | #if defined(CONFIG_MSM_SERIAL_DEBUGGER_CONSOLE) |
| 323 | static void debug_console_write(struct console *co, |
| 324 | const char *s, unsigned int count) |
| 325 | { |
| 326 | unsigned long irq_flags; |
| 327 | |
| 328 | /* disable irq's while TXing outside of FIQ context */ |
| 329 | local_irq_save(irq_flags); |
| 330 | while (count--) { |
| 331 | if (*s == '\n') |
| 332 | debug_putc('\r'); |
| 333 | debug_putc(*s++); |
| 334 | } |
| 335 | debug_flush(); |
| 336 | local_irq_restore(irq_flags); |
| 337 | } |
| 338 | |
| 339 | static struct console msm_serial_debug_console = { |
| 340 | .name = "debug_console", |
| 341 | .write = debug_console_write, |
| 342 | .flags = CON_PRINTBUFFER | CON_ANYTIME | CON_ENABLED, |
| 343 | }; |
| 344 | #endif |
| 345 | |
| 346 | void msm_serial_debug_enable(int enable) { |
| 347 | debug_enable = enable; |
| 348 | } |
| 349 | |
| 350 | void msm_serial_debug_init(unsigned int base, int irq, |
| 351 | struct device *clk_device, int signal_irq) |
| 352 | { |
| 353 | int ret; |
| 354 | void *port; |
| 355 | |
| 356 | debug_clk = clk_get(clk_device, "uart_clk"); |
| 357 | if (debug_clk) |
| 358 | clk_enable(debug_clk); |
| 359 | |
| 360 | port = ioremap(base, 4096); |
| 361 | if (!port) |
| 362 | return; |
| 363 | |
| 364 | init_data.base = base; |
| 365 | init_data.irq = irq; |
| 366 | init_data.clk_device = clk_device; |
| 367 | init_data.signal_irq = signal_irq; |
| 368 | debug_port_base = (unsigned int) port; |
| 369 | debug_signal_irq = signal_irq; |
| 370 | debug_port_init(); |
| 371 | |
| 372 | debug_prompt(); |
| 373 | |
| 374 | msm_fiq_select(irq); |
| 375 | msm_fiq_set_handler(debug_fiq, 0); |
| 376 | msm_fiq_enable(irq); |
| 377 | |
| 378 | ret = request_irq(signal_irq, debug_irq, |
| 379 | IRQF_TRIGGER_RISING, "debug", 0); |
| 380 | if (ret) |
| 381 | printk(KERN_ERR |
| 382 | "serial_debugger: could not install signal_irq"); |
| 383 | |
| 384 | #if defined(CONFIG_MSM_SERIAL_DEBUGGER_CONSOLE) |
| 385 | register_console(&msm_serial_debug_console); |
| 386 | #endif |
| 387 | debugger_enable = 1; |
| 388 | } |
| 389 | static int msm_serial_debug_remove(const char *val, struct kernel_param *kp) |
| 390 | { |
| 391 | int ret; |
| 392 | static int pre_stat = 1; |
| 393 | ret = param_set_bool(val, kp); |
| 394 | if (ret) |
| 395 | return ret; |
| 396 | |
| 397 | if (pre_stat == *(int *)kp->arg) |
| 398 | return 0; |
| 399 | |
| 400 | pre_stat = *(int *)kp->arg; |
| 401 | |
| 402 | if (*(int *)kp->arg) { |
| 403 | msm_serial_debug_init(init_data.base, init_data.irq, |
| 404 | init_data.clk_device, init_data.signal_irq); |
| 405 | printk(KERN_INFO "enable FIQ serial debugger\n"); |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | #if defined(CONFIG_MSM_SERIAL_DEBUGGER_CONSOLE) |
| 410 | unregister_console(&msm_serial_debug_console); |
| 411 | #endif |
| 412 | free_irq(init_data.signal_irq, 0); |
| 413 | msm_fiq_set_handler(NULL, 0); |
| 414 | msm_fiq_disable(init_data.irq); |
| 415 | msm_fiq_unselect(init_data.irq); |
| 416 | clk_disable(debug_clk); |
| 417 | printk(KERN_INFO "disable FIQ serial debugger\n"); |
| 418 | return 0; |
| 419 | } |
| 420 | module_param_call(enable, msm_serial_debug_remove, param_get_bool, |
| 421 | &debugger_enable, S_IWUSR | S_IRUGO); |