ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / arch / ppc / syslib / gen550_dbg.c
1 /*
2  * arch/ppc/syslib/gen550_dbg.c
3  *
4  * A library of polled 16550 serial routines.  These are intended to
5  * be used to support progress messages, xmon, kgdb, etc. on a
6  * variety of platforms.
7  *
8  * Adapted from lots of code ripped from the arch/ppc/boot/ polled
9  * 16550 support.
10  *
11  * Author: Matt Porter <mporter@mvista.com>
12  *
13  * 2002-2003 (c) MontaVista Software, Inc.  This file is licensed under
14  * the terms of the GNU General Public License version 2.  This program
15  * is licensed "as is" without any warranty of any kind, whether express
16  * or implied.
17  */
18
19 #include <linux/config.h>
20 #include <linux/types.h>
21 #include <linux/serial.h>
22 #include <linux/tty.h>          /* For linux/serial_core.h */
23 #include <linux/serial_core.h>
24 #include <linux/serialP.h>
25 #include <linux/serial_reg.h>
26 #include <asm/machdep.h>
27 #include <asm/serial.h>
28 #include <asm/io.h>
29
30 #define SERIAL_BAUD     9600
31
32 static struct serial_state rs_table[RS_TABLE_SIZE] = {
33         SERIAL_PORT_DFNS        /* defined in <asm/serial.h> */
34 };
35
36 static void (*serial_outb)(unsigned long, unsigned char);
37 static unsigned long (*serial_inb)(unsigned long);
38
39 static int shift;
40
41 unsigned long direct_inb(unsigned long addr)
42 {
43         return readb(addr);
44 }
45
46 void direct_outb(unsigned long addr, unsigned char val)
47 {
48         writeb(val, addr);
49 }
50
51 unsigned long io_inb(unsigned long port)
52 {
53         return inb(port);
54 }
55
56 void io_outb(unsigned long port, unsigned char val)
57 {
58         outb(val, port);
59 }
60
61 unsigned long serial_init(int chan, void *ignored)
62 {
63         unsigned long com_port;
64         unsigned char lcr, dlm;
65
66         /* We need to find out which type io we're expecting.  If it's
67          * 'SERIAL_IO_PORT', we get an offset from the isa_io_base.
68          * If it's 'SERIAL_IO_MEM', we can the exact location.  -- Tom */
69         switch (rs_table[chan].io_type) {
70                 case SERIAL_IO_PORT:
71                         com_port = rs_table[chan].port;
72                         serial_outb = io_outb;
73                         serial_inb = io_inb;
74                         break;
75                 case SERIAL_IO_MEM:
76                         com_port = (unsigned long)rs_table[chan].iomem_base;
77                         serial_outb = direct_outb;
78                         serial_inb = direct_inb;
79                         break;
80                 default:
81                         /* We can't deal with it. */
82                         return -1;
83         }
84
85         /* How far apart the registers are. */
86         shift = rs_table[chan].iomem_reg_shift;
87
88         /* save the LCR */
89         lcr = serial_inb(com_port + (UART_LCR << shift));
90         
91         /* Access baud rate */
92         serial_outb(com_port + (UART_LCR << shift), UART_LCR_DLAB);
93         dlm = serial_inb(com_port + (UART_DLM << shift));
94
95         /*
96          * Test if serial port is unconfigured
97          * We assume that no-one uses less than 110 baud or
98          * less than 7 bits per character these days.
99          *  -- paulus.
100          */
101         if ((dlm <= 4) && (lcr & 2)) {
102                 /* port is configured, put the old LCR back */
103                 serial_outb(com_port + (UART_LCR << shift), lcr);
104         }
105         else {
106                 /* Input clock. */
107                 serial_outb(com_port + (UART_DLL << shift),
108                         (rs_table[chan].baud_base / SERIAL_BAUD) & 0xFF);
109                 serial_outb(com_port + (UART_DLM << shift),
110                                 (rs_table[chan].baud_base / SERIAL_BAUD) >> 8);
111                 /* 8 data, 1 stop, no parity */
112                 serial_outb(com_port + (UART_LCR << shift), 0x03);
113                 /* RTS/DTR */
114                 serial_outb(com_port + (UART_MCR << shift), 0x03);
115
116                 /* Clear & enable FIFOs */
117                 serial_outb(com_port + (UART_FCR << shift), 0x07);
118         }
119
120         return (com_port);
121 }
122
123 void
124 serial_putc(unsigned long com_port, unsigned char c)
125 {
126         while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_THRE) == 0)
127                 ;
128         serial_outb(com_port, c);
129 }
130
131 unsigned char
132 serial_getc(unsigned long com_port)
133 {
134         while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) == 0)
135                 ;
136         return serial_inb(com_port);
137 }
138
139 int
140 serial_tstc(unsigned long com_port)
141 {
142         return ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) != 0);
143 }
144
145 void
146 serial_close(unsigned long com_port)
147 {
148 }
149
150 void
151 gen550_init(int i, struct uart_port *serial_req)
152 {
153         rs_table[i].io_type = serial_req->iotype;
154         rs_table[i].port = serial_req->iobase;
155         rs_table[i].iomem_base = serial_req->membase;
156         rs_table[i].iomem_reg_shift = serial_req->regshift;
157 }
158
159 #ifdef CONFIG_SERIAL_TEXT_DEBUG
160 void
161 gen550_progress(char *s, unsigned short hex)
162 {
163         volatile unsigned int progress_debugport;
164         volatile char c;
165
166         progress_debugport = serial_init(0, NULL);
167
168         serial_putc(progress_debugport, '\r');
169
170         while ((c = *s++) != 0)
171                 serial_putc(progress_debugport, c);
172
173         serial_putc(progress_debugport, '\n');
174         serial_putc(progress_debugport, '\r');
175 }
176 #endif /* CONFIG_SERIAL_TEXT_DEBUG */