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