ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / mips / mips-boards / generic / gdb_hook.c
1 /*
2  * Carsten Langgaard, carstenl@mips.com
3  * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
4  *
5  *  This program is free software; you can distribute it and/or modify it
6  *  under the terms of the GNU General Public License (Version 2) as
7  *  published by the Free Software Foundation.
8  *
9  *  This program is distributed in the hope it will be useful, but WITHOUT
10  *  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  *  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  *  for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  59 Temple Place - Suite 330, Boston MA 02111-1307, USA.
17  *
18  * This is the interface to the remote debugger stub.
19  */
20 #include <linux/types.h>
21 #include <linux/config.h>
22 #include <linux/serial.h>
23 #include <linux/serialP.h>
24 #include <linux/serial_reg.h>
25
26 #include <asm/serial.h>
27 #include <asm/io.h>
28
29 static struct serial_state rs_table[RS_TABLE_SIZE] = {
30         SERIAL_PORT_DFNS        /* Defined in serial.h */
31 };
32
33 static struct async_struct kdb_port_info = {0};
34
35 int (*generic_putDebugChar)(char);
36 char (*generic_getDebugChar)(void);
37
38 static __inline__ unsigned int serial_in(struct async_struct *info, int offset)
39 {
40         return inb(info->port + offset);
41 }
42
43 static __inline__ void serial_out(struct async_struct *info, int offset,
44                                 int value)
45 {
46         outb(value, info->port+offset);
47 }
48
49 int rs_kgdb_hook(int tty_no, int speed) {
50         int t;
51         struct serial_state *ser = &rs_table[tty_no];
52
53         kdb_port_info.state = ser;
54         kdb_port_info.magic = SERIAL_MAGIC;
55         kdb_port_info.port = ser->port;
56         kdb_port_info.flags = ser->flags;
57
58         /*
59          * Clear all interrupts
60          */
61         serial_in(&kdb_port_info, UART_LSR);
62         serial_in(&kdb_port_info, UART_RX);
63         serial_in(&kdb_port_info, UART_IIR);
64         serial_in(&kdb_port_info, UART_MSR);
65
66         /*
67          * Now, initialize the UART
68          */
69         serial_out(&kdb_port_info, UART_LCR, UART_LCR_WLEN8);   /* reset DLAB */
70         if (kdb_port_info.flags & ASYNC_FOURPORT) {
71                 kdb_port_info.MCR = UART_MCR_DTR | UART_MCR_RTS;
72                 t = UART_MCR_DTR | UART_MCR_OUT1;
73         } else {
74                 kdb_port_info.MCR
75                         = UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2;
76                 t = UART_MCR_DTR | UART_MCR_RTS;
77         }
78
79         kdb_port_info.MCR = t;          /* no interrupts, please */
80         serial_out(&kdb_port_info, UART_MCR, kdb_port_info.MCR);
81
82         /*
83          * and set the speed of the serial port
84          */
85         if (speed == 0)
86                 speed = 9600;
87
88         t = kdb_port_info.state->baud_base / speed;
89         /* set DLAB */
90         serial_out(&kdb_port_info, UART_LCR, UART_LCR_WLEN8 | UART_LCR_DLAB);
91         serial_out(&kdb_port_info, UART_DLL, t & 0xff);/* LS of divisor */
92         serial_out(&kdb_port_info, UART_DLM, t >> 8);  /* MS of divisor */
93         /* reset DLAB */
94         serial_out(&kdb_port_info, UART_LCR, UART_LCR_WLEN8);
95
96         return speed;
97 }
98
99 int putDebugChar(char c)
100 {
101         return generic_putDebugChar(c);
102 }
103
104 char getDebugChar(void)
105 {
106         return generic_getDebugChar();
107 }
108
109 int rs_putDebugChar(char c)
110 {
111
112         if (!kdb_port_info.state) {     /* need to init device first */
113                 return 0;
114         }
115
116         while ((serial_in(&kdb_port_info, UART_LSR) & UART_LSR_THRE) == 0)
117                 ;
118
119         serial_out(&kdb_port_info, UART_TX, c);
120
121         return 1;
122 }
123
124 char rs_getDebugChar(void)
125 {
126         if (!kdb_port_info.state) {     /* need to init device first */
127                 return 0;
128         }
129
130         while (!(serial_in(&kdb_port_info, UART_LSR) & 1))
131                 ;
132
133         return serial_in(&kdb_port_info, UART_RX);
134 }