blob: 0707d35079baefd1d3427f5af86ee1d7b98c2d5f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* gdb-io.c: FR403 GDB stub I/O
2 *
3 * Copyright (C) 2003 Red Hat, Inc. All Rights Reserved.
4 * Written by David Howells (dhowells@redhat.com)
5 *
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.
10 */
11
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>
20
21#include <asm/pgtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/irc-regs.h>
23#include <asm/timer-regs.h>
24#include <asm/gdb-stub.h>
25#include "gdb-io.h"
26
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
34
35#define LSR_WAIT_FOR(STATE) \
36do { \
37 gdbstub_do_rx(); \
38} while (!(__UART(LSR) & UART_LSR_##STATE))
39
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)
43
44#define FLOWCTL_WAIT_FOR(LINE) \
45do { \
46 gdbstub_do_rx(); \
47} while(!FLOWCTL_QUERY(LINE))
48
49/*****************************************************************************/
50/*
51 * initialise the GDB stub
52 * - called with PSR.ET==0, so can't incur external interrupts
53 */
54void gdbstub_io_init(void)
55{
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;
63
64 FLOWCTL_CLEAR(DTR);
65 FLOWCTL_SET(RTS);
66
67// gdbstub_set_baud(115200);
68
69 /* we want to get serial receive interrupts */
70 __UART(IER) = UART_IER_RDI | UART_IER_RLSI;
71 mb();
72
73 __set_IRR(6, __UART_IRR_NMI); /* map ERRs and UARTx to NMI */
74
75} /* end gdbstub_io_init() */
76
77/*****************************************************************************/
78/*
79 * set up the GDB stub serial port baud rate timers
80 */
81void gdbstub_set_baud(unsigned baud)
82{
83 unsigned value, high, low;
84 u8 lcr;
85
86 /* work out the divisor to give us the nearest higher baud rate */
87 value = __serial_clock_speed_HZ / 16 / baud;
88
89 /* determine the baud rate range */
90 high = __serial_clock_speed_HZ / 16 / value;
91 low = __serial_clock_speed_HZ / 16 / (value + 1);
92
93 /* pick the nearest bound */
94 if (low + (high - low) / 2 > baud)
95 value++;
96
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();
105
106} /* end gdbstub_set_baud() */
107
108/*****************************************************************************/
109/*
110 * receive characters into the receive FIFO
111 */
112void gdbstub_do_rx(void)
113{
114 unsigned ix, nix;
115
116 ix = gdbstub_rx_inp;
117
118 while (__UART(LSR) & UART_LSR_DR) {
119 nix = (ix + 2) & 0xfff;
120 if (nix == gdbstub_rx_outp)
121 break;
122
123 gdbstub_rx_buffer[ix++] = __UART(LSR);
124 gdbstub_rx_buffer[ix++] = __UART(RX);
125 ix = nix;
126 }
127
128 gdbstub_rx_inp = ix;
129
130 __clr_RC(15);
131 __clr_IRL();
132
133} /* end gdbstub_do_rx() */
134
135/*****************************************************************************/
136/*
137 * wait for a character to come from the debugger
138 */
139int gdbstub_rx_char(unsigned char *_ch, int nonblock)
140{
141 unsigned ix;
142 u8 ch, st;
143
144 *_ch = 0xff;
145
146 if (gdbstub_rx_unget) {
147 *_ch = gdbstub_rx_unget;
148 gdbstub_rx_unget = 0;
149 return 0;
150 }
151
152 try_again:
153 gdbstub_do_rx();
154
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;
162 }
163
164 st = gdbstub_rx_buffer[ix++];
165 ch = gdbstub_rx_buffer[ix++];
166 gdbstub_rx_outp = ix & 0x00000fff;
167
168 if (st & UART_LSR_BI) {
169 gdbstub_proto("### GDB Rx Break Detected ###\n");
170 return -EINTR;
171 }
172 else if (st & (UART_LSR_FE|UART_LSR_OE|UART_LSR_PE)) {
David Howells7ca8b9c2010-05-24 14:32:54 -0700173 gdbstub_io("### GDB Rx Error (st=%02x) ###\n",st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 return -EIO;
175 }
176 else {
David Howells7ca8b9c2010-05-24 14:32:54 -0700177 gdbstub_io("### GDB Rx %02x (st=%02x) ###\n",ch,st);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 *_ch = ch & 0x7f;
179 return 0;
180 }
181
182} /* end gdbstub_rx_char() */
183
184/*****************************************************************************/
185/*
186 * send a character to the debugger
187 */
188void gdbstub_tx_char(unsigned char ch)
189{
190 FLOWCTL_SET(DTR);
191 LSR_WAIT_FOR(THRE);
192// FLOWCTL_WAIT_FOR(CTS);
193
194 if (ch == 0x0a) {
195 __UART(TX) = 0x0d;
196 mb();
197 LSR_WAIT_FOR(THRE);
198// FLOWCTL_WAIT_FOR(CTS);
199 }
200 __UART(TX) = ch;
201 mb();
202
203 FLOWCTL_CLEAR(DTR);
204} /* end gdbstub_tx_char() */
205
206/*****************************************************************************/
207/*
208 * send a character to the debugger
209 */
210void gdbstub_tx_flush(void)
211{
212 LSR_WAIT_FOR(TEMT);
213 LSR_WAIT_FOR(THRE);
214 FLOWCTL_CLEAR(DTR);
215} /* end gdbstub_tx_flush() */