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