Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Travis Geiselbrecht |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files |
| 6 | * (the "Software"), to deal in the Software without restriction, |
| 7 | * including without limitation the rights to use, copy, modify, merge, |
| 8 | * publish, distribute, sublicense, and/or sell copies of the Software, |
| 9 | * and to permit persons to whom the Software is furnished to do so, |
| 10 | * subject to the following conditions: |
| 11 | * |
| 12 | * The above copyright notice and this permission notice shall be |
| 13 | * included in all copies or substantial portions of the Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 22 | */ |
| 23 | #include <stdarg.h> |
| 24 | #include <reg.h> |
| 25 | #include <debug.h> |
| 26 | #include <printf.h> |
| 27 | #include <kernel/thread.h> |
Travis Geiselbrecht | 2ce7b97 | 2009-06-28 12:33:13 -0700 | [diff] [blame] | 28 | #include <kernel/timer.h> |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 29 | #include <platform/debug.h> |
| 30 | #include <arch/ops.h> |
| 31 | #include <platform/omap5912.h> |
Travis Geiselbrecht | 2ce7b97 | 2009-06-28 12:33:13 -0700 | [diff] [blame] | 32 | #include <lib/cbuf.h> |
| 33 | |
| 34 | static cbuf_t debug_buf; |
| 35 | static timer_t debug_timer; |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 36 | |
| 37 | static void write_uart_reg(int uart, int reg, unsigned char data) |
| 38 | { |
| 39 | unsigned long base; |
| 40 | int mul = 4; |
| 41 | |
| 42 | switch(uart) { |
| 43 | case 0: base = UART0_BASE; break; |
| 44 | case 1: base = UART1_BASE; break; |
| 45 | case 2: base = UART2_BASE; break; |
| 46 | default: return; |
| 47 | } |
| 48 | |
| 49 | *(volatile unsigned char *)(base + reg * mul) = data; |
| 50 | } |
| 51 | |
| 52 | static unsigned char read_uart_reg(int uart, int reg) |
| 53 | { |
| 54 | unsigned long base; |
| 55 | int mul = 4; |
| 56 | |
| 57 | switch(uart) { |
| 58 | case 0: base = UART0_BASE; break; |
| 59 | case 1: base = UART1_BASE; break; |
| 60 | case 2: base = UART2_BASE; break; |
| 61 | default: return 0; |
| 62 | } |
| 63 | |
| 64 | return *(volatile unsigned char *)(base + reg * mul); |
| 65 | } |
| 66 | |
| 67 | static int uart_init(void) |
| 68 | { |
| 69 | /* clear the tx & rx fifo and disable */ |
| 70 | write_uart_reg(0, UART_FCR, 0x6); |
| 71 | |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | static int uart_putc(int port, char c ) |
| 76 | { |
| 77 | while (!(read_uart_reg(port, UART_LSR) & (1<<6))) // wait for the shift register to empty |
| 78 | ; |
| 79 | write_uart_reg(port, UART_THR, c); |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | static int uart_getc(int port, bool wait) /* returns -1 if no data available */ |
| 84 | { |
| 85 | if (wait) { |
| 86 | while (!(read_uart_reg(port, UART_LSR) & (1<<0))) // wait for data to show up in the rx fifo |
| 87 | ; |
| 88 | } else { |
| 89 | if (!(read_uart_reg(port, UART_LSR) & (1<<0))) |
| 90 | return -1; |
| 91 | } |
| 92 | return read_uart_reg(port, UART_RHR); |
| 93 | } |
| 94 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 95 | void _dputc(char c) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 96 | { |
| 97 | if (c == '\n') |
| 98 | uart_putc(0, '\r'); |
| 99 | uart_putc(0, c); |
| 100 | } |
| 101 | |
Travis Geiselbrecht | 2ce7b97 | 2009-06-28 12:33:13 -0700 | [diff] [blame] | 102 | static enum handler_return debug_timer_callback(timer_t *t, time_t now, void *arg) |
| 103 | { |
| 104 | signed char c; |
| 105 | c = uart_getc(0, false); |
| 106 | if (c > 0) { |
| 107 | cbuf_write(&debug_buf, &c, 1, false); |
| 108 | return INT_RESCHEDULE; |
| 109 | } else { |
| 110 | return INT_NO_RESCHEDULE; |
| 111 | } |
| 112 | } |
| 113 | |
Travis Geiselbrecht | 28da92a | 2009-06-28 11:12:40 -0700 | [diff] [blame] | 114 | int dgetc(char *c, bool wait) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 115 | { |
Travis Geiselbrecht | 2ce7b97 | 2009-06-28 12:33:13 -0700 | [diff] [blame] | 116 | ssize_t len; |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 117 | |
Travis Geiselbrecht | 2ce7b97 | 2009-06-28 12:33:13 -0700 | [diff] [blame] | 118 | len = cbuf_read(&debug_buf, c, 1, wait); |
| 119 | return len; |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void debug_dump_regs(void) |
| 123 | { |
| 124 | PANIC_UNIMPLEMENTED; |
| 125 | } |
| 126 | |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 127 | void platform_halt(void) |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 128 | { |
Travis Geiselbrecht | eb94605 | 2008-09-07 22:32:49 -0700 | [diff] [blame] | 129 | dprintf(ALWAYS, "HALT: spinning forever...\n"); |
Travis Geiselbrecht | 1d0df69 | 2008-09-01 02:26:09 -0700 | [diff] [blame] | 130 | for(;;); |
| 131 | } |
| 132 | |
| 133 | void debug_dump_memory_bytes(void *mem, int len) |
| 134 | { |
| 135 | PANIC_UNIMPLEMENTED; |
| 136 | } |
| 137 | |
| 138 | void debug_dump_memory_halfwords(void *mem, int len) |
| 139 | { |
| 140 | PANIC_UNIMPLEMENTED; |
| 141 | } |
| 142 | |
| 143 | void debug_dump_memory_words(void *mem, int len) |
| 144 | { |
| 145 | PANIC_UNIMPLEMENTED; |
| 146 | } |
| 147 | |
| 148 | void debug_set_trace_level(int trace_type, int level) |
| 149 | { |
| 150 | PANIC_UNIMPLEMENTED; |
| 151 | } |
| 152 | |
Travis Geiselbrecht | 2ce7b97 | 2009-06-28 12:33:13 -0700 | [diff] [blame] | 153 | void platform_init_debug(void) |
| 154 | { |
| 155 | cbuf_initialize(&debug_buf, 512); |
| 156 | timer_set_periodic(&debug_timer, 10, &debug_timer_callback, NULL); |
| 157 | } |