import of ftp.dlink.com/GPL/DSMG-600_reB/ppclinux.tar.gz
[linux-2.4.21-pre4.git] / arch / ppc / kernel / gen550_dbg.c
1 /*
2  * arch/ppc/kernel/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  * Matt Porter <mporter@mvista.com>
12  *
13  * Copyright 2002 MontaVista Software Inc.
14  *
15  * This program is free software; you can redistribute  it and/or modify it
16  * under  the terms of  the GNU General Public License as published by the
17  * Free Software Foundation;  either version 2 of the  License, or (at your
18  * option) any later version.
19  */
20
21 #include <linux/config.h>
22 #include <linux/serialP.h>
23 #include <linux/serial_reg.h>
24 #include <asm/serial.h>
25 #include <asm/io.h>
26
27 #define SERIAL_BAUD     9600
28
29 extern struct serial_state rs_table[];
30
31 static void (*serial_outb)(unsigned long, unsigned char);
32 static unsigned long (*serial_inb)(unsigned long);
33
34 static int shift;
35
36 unsigned long direct_inb(unsigned long addr)
37 {
38         return readb(addr);
39 }
40
41 void direct_outb(unsigned long addr, unsigned char val)
42 {
43         writeb(val, addr);
44 }
45
46 unsigned long io_inb(unsigned long port)
47 {
48         return inb(port);
49 }
50
51 void io_outb(unsigned long port, unsigned char val)
52 {
53         outb(val, port);
54 }
55
56 unsigned long serial_init(int chan, void *ignored)
57 {
58         unsigned long com_port;
59         unsigned char lcr, dlm;
60
61         /* We need to find out which type io we're expecting.  If it's
62          * 'SERIAL_IO_PORT', we get an offset from the isa_io_base.
63          * If it's 'SERIAL_IO_MEM', we can the exact location.  -- Tom */
64         switch (rs_table[chan].io_type) {
65                 case SERIAL_IO_PORT:
66                         com_port = rs_table[chan].port;
67                         serial_outb = io_outb;
68                         serial_inb = io_inb;
69                         break;
70                 case SERIAL_IO_MEM:
71                         com_port = (unsigned long)rs_table[chan].iomem_base;
72                         serial_outb = direct_outb;
73                         serial_inb = direct_inb;
74                         break;
75                 default:
76                         /* We can't deal with it. */
77                         return -1;
78         }
79
80         /* How far apart the registers are. */
81         shift = rs_table[chan].iomem_reg_shift;
82         
83         /* save the LCR */
84         lcr = serial_inb(com_port + (UART_LCR << shift));
85         /* Access baud rate */
86         serial_outb(com_port + (UART_LCR << shift), 0x80);
87         dlm = serial_inb(com_port + (UART_DLM << shift));
88         /*
89          * Test if serial port is unconfigured.
90          * We assume that no-one uses less than 110 baud or
91          * less than 7 bits per character these days.
92          *  -- paulus.
93          */
94
95         /* Input clock. */
96         serial_outb(com_port + (UART_DLL << shift),
97                         (BASE_BAUD / SERIAL_BAUD) & 0xFF);
98         serial_outb(com_port + (UART_DLM << shift),
99                         (BASE_BAUD / SERIAL_BAUD) >> 8);
100         /* 8 data, 1 stop, no parity */
101         serial_outb(com_port + (UART_LCR << shift), 0x03);
102         /* RTS/DTR */
103         serial_outb(com_port + (UART_MCR << shift), 0x03);
104
105         /* Clear & enable FIFOs */
106         serial_outb(com_port + (UART_FCR << shift), 0x07);
107
108         return (com_port);
109 }
110
111 void
112 serial_putc(unsigned long com_port, unsigned char c)
113 {
114         while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_THRE) == 0)
115                 ;
116         serial_outb(com_port, c);
117 }
118
119 unsigned char
120 serial_getc(unsigned long com_port)
121 {
122         while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) == 0)
123                 ;
124         return serial_inb(com_port);
125 }
126
127 int
128 serial_tstc(unsigned long com_port)
129 {
130         return ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) != 0);
131 }
132
133 void
134 serial_close(unsigned long com_port)
135 {
136 }
137
138 void
139 gen550_init(int i, struct serial_struct *serial_req)
140 {
141         rs_table[i].io_type = serial_req->io_type;
142         rs_table[i].iomem_base = serial_req->iomem_base;
143         rs_table[i].iomem_reg_shift = serial_req->iomem_reg_shift;
144 }
145
146 #ifdef CONFIG_SERIAL_TEXT_DEBUG
147 void
148 gen550_progress(char *s, unsigned short hex)
149 {
150         volatile unsigned int progress_debugport;
151         volatile char c;
152
153         progress_debugport = serial_init(0, NULL);
154
155         serial_putc(progress_debugport, '\r');
156
157         while ((c = *s++) != 0)
158                 serial_putc(progress_debugport, c);
159
160         serial_putc(progress_debugport, '\n');
161         serial_putc(progress_debugport, '\r');
162 }
163 #endif /* CONFIG_SERIAL_TEXT_DEBUG */