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 
direct_inb(unsigned long addr)36 unsigned long direct_inb(unsigned long addr)
37 {
38 	return readb(addr);
39 }
40 
direct_outb(unsigned long addr,unsigned char val)41 void direct_outb(unsigned long addr, unsigned char val)
42 {
43 	writeb(val, addr);
44 }
45 
io_inb(unsigned long port)46 unsigned long io_inb(unsigned long port)
47 {
48 	return inb(port);
49 }
50 
io_outb(unsigned long port,unsigned char val)51 void io_outb(unsigned long port, unsigned char val)
52 {
53 	outb(val, port);
54 }
55 
serial_init(int chan,void * ignored)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 
86 	/* Access baud rate */
87 	serial_outb(com_port + (UART_LCR << shift), UART_LCR_DLAB);
88 	dlm = serial_inb(com_port + (UART_DLM << shift));
89 
90 	/*
91 	 * Test if serial port is unconfigured
92 	 * We assume that no-one uses less than 110 baud or
93 	 * less than 7 bits per character these days.
94 	 *  -- paulus.
95 	 */
96 	if ((dlm <= 4) && (lcr & 2)) {
97 		/* port is configured, put the old LCR back */
98 		serial_outb(com_port + (UART_LCR << shift), lcr);
99 	}
100 	else {
101 		/* Input clock. */
102 		serial_outb(com_port + (UART_DLL << shift),
103 			(rs_table[chan].baud_base / SERIAL_BAUD) & 0xFF);
104 		serial_outb(com_port + (UART_DLM << shift),
105 				(rs_table[chan].baud_base / SERIAL_BAUD) >> 8);
106 		/* 8 data, 1 stop, no parity */
107 		serial_outb(com_port + (UART_LCR << shift), 0x03);
108 		/* RTS/DTR */
109 		serial_outb(com_port + (UART_MCR << shift), 0x03);
110 
111 		/* Clear & enable FIFOs */
112 		serial_outb(com_port + (UART_FCR << shift), 0x07);
113 	}
114 
115 	return (com_port);
116 }
117 
118 void
serial_putc(unsigned long com_port,unsigned char c)119 serial_putc(unsigned long com_port, unsigned char c)
120 {
121 	while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_THRE) == 0)
122 		;
123 	serial_outb(com_port, c);
124 }
125 
126 unsigned char
serial_getc(unsigned long com_port)127 serial_getc(unsigned long com_port)
128 {
129 	while ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) == 0)
130 		;
131 	return serial_inb(com_port);
132 }
133 
134 int
serial_tstc(unsigned long com_port)135 serial_tstc(unsigned long com_port)
136 {
137 	return ((serial_inb(com_port + (UART_LSR << shift)) & UART_LSR_DR) != 0);
138 }
139 
140 void
serial_close(unsigned long com_port)141 serial_close(unsigned long com_port)
142 {
143 }
144 
145 void
gen550_init(int i,struct serial_struct * serial_req)146 gen550_init(int i, struct serial_struct *serial_req)
147 {
148 	rs_table[i].io_type = serial_req->io_type;
149 	rs_table[i].port = serial_req->port;
150 	rs_table[i].iomem_base = serial_req->iomem_base;
151 	rs_table[i].iomem_reg_shift = serial_req->iomem_reg_shift;
152 }
153 
154 #ifdef CONFIG_SERIAL_TEXT_DEBUG
155 void
gen550_progress(char * s,unsigned short hex)156 gen550_progress(char *s, unsigned short hex)
157 {
158 	volatile unsigned int progress_debugport;
159 	volatile char c;
160 
161 	progress_debugport = serial_init(0, NULL);
162 
163 	serial_putc(progress_debugport, '\r');
164 
165 	while ((c = *s++) != 0)
166 		serial_putc(progress_debugport, c);
167 
168 	serial_putc(progress_debugport, '\n');
169 	serial_putc(progress_debugport, '\r');
170 }
171 #endif /* CONFIG_SERIAL_TEXT_DEBUG */
172