ARM: mmp: fix potential NULL dereference
[linux/fpc-iii.git] / arch / frv / kernel / gdb-io.c
blob0707d35079baefd1d3427f5af86ee1d7b98c2d5f
1 /* gdb-io.c: FR403 GDB stub I/O
3 * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #include <linux/string.h>
13 #include <linux/kernel.h>
14 #include <linux/signal.h>
15 #include <linux/sched.h>
16 #include <linux/mm.h>
17 #include <linux/console.h>
18 #include <linux/init.h>
19 #include <linux/serial_reg.h>
21 #include <asm/pgtable.h>
22 #include <asm/irc-regs.h>
23 #include <asm/timer-regs.h>
24 #include <asm/gdb-stub.h>
25 #include "gdb-io.h"
27 #ifdef CONFIG_GDBSTUB_UART0
28 #define __UART(X) (*(volatile uint8_t *)(UART0_BASE + (UART_##X)))
29 #define __UART_IRR_NMI 0xff0f0000
30 #else /* CONFIG_GDBSTUB_UART1 */
31 #define __UART(X) (*(volatile uint8_t *)(UART1_BASE + (UART_##X)))
32 #define __UART_IRR_NMI 0xfff00000
33 #endif
35 #define LSR_WAIT_FOR(STATE) \
36 do { \
37 gdbstub_do_rx(); \
38 } while (!(__UART(LSR) & UART_LSR_##STATE))
40 #define FLOWCTL_QUERY(LINE) ({ __UART(MSR) & UART_MSR_##LINE; })
41 #define FLOWCTL_CLEAR(LINE) do { __UART(MCR) &= ~UART_MCR_##LINE; mb(); } while (0)
42 #define FLOWCTL_SET(LINE) do { __UART(MCR) |= UART_MCR_##LINE; mb(); } while (0)
44 #define FLOWCTL_WAIT_FOR(LINE) \
45 do { \
46 gdbstub_do_rx(); \
47 } while(!FLOWCTL_QUERY(LINE))
49 /*****************************************************************************/
51 * initialise the GDB stub
52 * - called with PSR.ET==0, so can't incur external interrupts
54 void gdbstub_io_init(void)
56 /* set up the serial port */
57 __UART(LCR) = UART_LCR_WLEN8; /* 1N8 */
58 __UART(FCR) =
59 UART_FCR_ENABLE_FIFO |
60 UART_FCR_CLEAR_RCVR |
61 UART_FCR_CLEAR_XMIT |
62 UART_FCR_TRIGGER_1;
64 FLOWCTL_CLEAR(DTR);
65 FLOWCTL_SET(RTS);
67 // gdbstub_set_baud(115200);
69 /* we want to get serial receive interrupts */
70 __UART(IER) = UART_IER_RDI | UART_IER_RLSI;
71 mb();
73 __set_IRR(6, __UART_IRR_NMI); /* map ERRs and UARTx to NMI */
75 } /* end gdbstub_io_init() */
77 /*****************************************************************************/
79 * set up the GDB stub serial port baud rate timers
81 void gdbstub_set_baud(unsigned baud)
83 unsigned value, high, low;
84 u8 lcr;
86 /* work out the divisor to give us the nearest higher baud rate */
87 value = __serial_clock_speed_HZ / 16 / baud;
89 /* determine the baud rate range */
90 high = __serial_clock_speed_HZ / 16 / value;
91 low = __serial_clock_speed_HZ / 16 / (value + 1);
93 /* pick the nearest bound */
94 if (low + (high - low) / 2 > baud)
95 value++;
97 lcr = __UART(LCR);
98 __UART(LCR) |= UART_LCR_DLAB;
99 mb();
100 __UART(DLL) = value & 0xff;
101 __UART(DLM) = (value >> 8) & 0xff;
102 mb();
103 __UART(LCR) = lcr;
104 mb();
106 } /* end gdbstub_set_baud() */
108 /*****************************************************************************/
110 * receive characters into the receive FIFO
112 void gdbstub_do_rx(void)
114 unsigned ix, nix;
116 ix = gdbstub_rx_inp;
118 while (__UART(LSR) & UART_LSR_DR) {
119 nix = (ix + 2) & 0xfff;
120 if (nix == gdbstub_rx_outp)
121 break;
123 gdbstub_rx_buffer[ix++] = __UART(LSR);
124 gdbstub_rx_buffer[ix++] = __UART(RX);
125 ix = nix;
128 gdbstub_rx_inp = ix;
130 __clr_RC(15);
131 __clr_IRL();
133 } /* end gdbstub_do_rx() */
135 /*****************************************************************************/
137 * wait for a character to come from the debugger
139 int gdbstub_rx_char(unsigned char *_ch, int nonblock)
141 unsigned ix;
142 u8 ch, st;
144 *_ch = 0xff;
146 if (gdbstub_rx_unget) {
147 *_ch = gdbstub_rx_unget;
148 gdbstub_rx_unget = 0;
149 return 0;
152 try_again:
153 gdbstub_do_rx();
155 /* pull chars out of the buffer */
156 ix = gdbstub_rx_outp;
157 if (ix == gdbstub_rx_inp) {
158 if (nonblock)
159 return -EAGAIN;
160 //watchdog_alert_counter = 0;
161 goto try_again;
164 st = gdbstub_rx_buffer[ix++];
165 ch = gdbstub_rx_buffer[ix++];
166 gdbstub_rx_outp = ix & 0x00000fff;
168 if (st & UART_LSR_BI) {
169 gdbstub_proto("### GDB Rx Break Detected ###\n");
170 return -EINTR;
172 else if (st & (UART_LSR_FE|UART_LSR_OE|UART_LSR_PE)) {
173 gdbstub_io("### GDB Rx Error (st=%02x) ###\n",st);
174 return -EIO;
176 else {
177 gdbstub_io("### GDB Rx %02x (st=%02x) ###\n",ch,st);
178 *_ch = ch & 0x7f;
179 return 0;
182 } /* end gdbstub_rx_char() */
184 /*****************************************************************************/
186 * send a character to the debugger
188 void gdbstub_tx_char(unsigned char ch)
190 FLOWCTL_SET(DTR);
191 LSR_WAIT_FOR(THRE);
192 // FLOWCTL_WAIT_FOR(CTS);
194 if (ch == 0x0a) {
195 __UART(TX) = 0x0d;
196 mb();
197 LSR_WAIT_FOR(THRE);
198 // FLOWCTL_WAIT_FOR(CTS);
200 __UART(TX) = ch;
201 mb();
203 FLOWCTL_CLEAR(DTR);
204 } /* end gdbstub_tx_char() */
206 /*****************************************************************************/
208 * send a character to the debugger
210 void gdbstub_tx_flush(void)
212 LSR_WAIT_FOR(TEMT);
213 LSR_WAIT_FOR(THRE);
214 FLOWCTL_CLEAR(DTR);
215 } /* end gdbstub_tx_flush() */