mn10300: add the MN10300/AM33 architecture to the kernel
[pandora-kernel.git] / arch / mn10300 / kernel / gdb-io-serial.c
1 /* 16550 serial driver for gdbstub I/O
2  *
3  * Copyright (C) 2007 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 Licence
8  * as published by the Free Software Foundation; either version
9  * 2 of the Licence, or (at your option) any later version.
10  */
11 #include <linux/string.h>
12 #include <linux/kernel.h>
13 #include <linux/signal.h>
14 #include <linux/sched.h>
15 #include <linux/mm.h>
16 #include <linux/console.h>
17 #include <linux/init.h>
18 #include <linux/nmi.h>
19
20 #include <asm/pgtable.h>
21 #include <asm/system.h>
22 #include <asm/gdb-stub.h>
23 #include <asm/exceptions.h>
24 #include <asm/serial-regs.h>
25 #include <asm/unit/serial.h>
26
27 /*
28  * initialise the GDB stub
29  */
30 void gdbstub_io_init(void)
31 {
32         u16 tmp;
33
34         /* set up the serial port */
35         GDBPORT_SERIAL_LCR = UART_LCR_WLEN8; /* 1N8 */
36         GDBPORT_SERIAL_FCR = (UART_FCR_ENABLE_FIFO | UART_FCR_CLEAR_RCVR |
37                               UART_FCR_CLEAR_XMIT);
38
39         FLOWCTL_CLEAR(DTR);
40         FLOWCTL_SET(RTS);
41
42         gdbstub_io_set_baud(115200);
43
44         /* we want to get serial receive interrupts */
45         XIRQxICR(GDBPORT_SERIAL_IRQ) = 0;
46         tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
47
48         IVAR0 = EXCEP_IRQ_LEVEL0;
49         set_intr_stub(EXCEP_IRQ_LEVEL0, gdbstub_io_rx_handler);
50
51         XIRQxICR(GDBPORT_SERIAL_IRQ) &= ~GxICR_REQUEST;
52         XIRQxICR(GDBPORT_SERIAL_IRQ) = GxICR_ENABLE | GxICR_LEVEL_0;
53         tmp = XIRQxICR(GDBPORT_SERIAL_IRQ);
54
55         GDBPORT_SERIAL_IER = UART_IER_RDI | UART_IER_RLSI;
56
57         /* permit level 0 IRQs to take place */
58         asm volatile(
59                 "       and %0,epsw     \n"
60                 "       or %1,epsw      \n"
61                 :
62                 : "i"(~EPSW_IM), "i"(EPSW_IE | EPSW_IM_1)
63                 );
64 }
65
66 /*
67  * set up the GDB stub serial port baud rate timers
68  */
69 void gdbstub_io_set_baud(unsigned baud)
70 {
71         unsigned value;
72         u8 lcr;
73
74         value = 18432000 / 16 / baud;
75
76         lcr = GDBPORT_SERIAL_LCR;
77         GDBPORT_SERIAL_LCR |= UART_LCR_DLAB;
78         GDBPORT_SERIAL_DLL = value & 0xff;
79         GDBPORT_SERIAL_DLM = (value >> 8) & 0xff;
80         GDBPORT_SERIAL_LCR = lcr;
81 }
82
83 /*
84  * wait for a character to come from the debugger
85  */
86 int gdbstub_io_rx_char(unsigned char *_ch, int nonblock)
87 {
88         unsigned ix;
89         u8 ch, st;
90
91         *_ch = 0xff;
92
93         if (gdbstub_rx_unget) {
94                 *_ch = gdbstub_rx_unget;
95                 gdbstub_rx_unget = 0;
96                 return 0;
97         }
98
99  try_again:
100         /* pull chars out of the buffer */
101         ix = gdbstub_rx_outp;
102         if (ix == gdbstub_rx_inp) {
103                 if (nonblock)
104                         return -EAGAIN;
105 #ifdef CONFIG_MN10300_WD_TIMER
106                 watchdog_alert_counter = 0;
107 #endif /* CONFIG_MN10300_WD_TIMER */
108                 goto try_again;
109         }
110
111         ch = gdbstub_rx_buffer[ix++];
112         st = gdbstub_rx_buffer[ix++];
113         gdbstub_rx_outp = ix & 0x00000fff;
114
115         if (st & UART_LSR_BI) {
116                 gdbstub_proto("### GDB Rx Break Detected ###\n");
117                 return -EINTR;
118         } else if (st & (UART_LSR_FE | UART_LSR_OE | UART_LSR_PE)) {
119                 gdbstub_proto("### GDB Rx Error (st=%02x) ###\n", st);
120                 return -EIO;
121         } else {
122                 gdbstub_proto("### GDB Rx %02x (st=%02x) ###\n", ch, st);
123                 *_ch = ch & 0x7f;
124                 return 0;
125         }
126 }
127
128 /*
129  * send a character to the debugger
130  */
131 void gdbstub_io_tx_char(unsigned char ch)
132 {
133         FLOWCTL_SET(DTR);
134         LSR_WAIT_FOR(THRE);
135         /* FLOWCTL_WAIT_FOR(CTS); */
136
137         if (ch == 0x0a) {
138                 GDBPORT_SERIAL_TX = 0x0d;
139                 LSR_WAIT_FOR(THRE);
140                 /* FLOWCTL_WAIT_FOR(CTS); */
141         }
142         GDBPORT_SERIAL_TX = ch;
143
144         FLOWCTL_CLEAR(DTR);
145 }
146
147 /*
148  * send a character to the debugger
149  */
150 void gdbstub_io_tx_flush(void)
151 {
152         LSR_WAIT_FOR(TEMT);
153         LSR_WAIT_FOR(THRE);
154         FLOWCTL_CLEAR(DTR);
155 }