blob: acb74a17bbbfa1bc2a916a872e8296bcd71aa0c1 [file] [log] [blame]
Milton Miller7f853352005-09-06 11:56:02 +10001/*
2 * udbg for for NS16550 compatable serial ports
3 *
4 * Copyright (C) 2001-2005 PPC 64 Team, IBM Corp
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
Milton Miller7f853352005-09-06 11:56:02 +100011#include <linux/types.h>
Milton Miller188d2ce2005-09-06 11:57:00 +100012#include <asm/udbg.h>
Milton Miller7f853352005-09-06 11:56:02 +100013#include <asm/io.h>
Milton Miller7f853352005-09-06 11:56:02 +100014
15extern u8 real_readb(volatile u8 __iomem *addr);
16extern void real_writeb(u8 data, volatile u8 __iomem *addr);
Olof Johansson39c870d2007-02-04 16:36:49 -060017extern u8 real_205_readb(volatile u8 __iomem *addr);
18extern void real_205_writeb(u8 data, volatile u8 __iomem *addr);
Milton Miller7f853352005-09-06 11:56:02 +100019
20struct NS16550 {
21 /* this struct must be packed */
22 unsigned char rbr; /* 0 */
23 unsigned char ier; /* 1 */
24 unsigned char fcr; /* 2 */
25 unsigned char lcr; /* 3 */
26 unsigned char mcr; /* 4 */
27 unsigned char lsr; /* 5 */
28 unsigned char msr; /* 6 */
29 unsigned char scr; /* 7 */
30};
31
32#define thr rbr
33#define iir fcr
34#define dll rbr
35#define dlm ier
36#define dlab lcr
37
38#define LSR_DR 0x01 /* Data ready */
39#define LSR_OE 0x02 /* Overrun */
40#define LSR_PE 0x04 /* Parity error */
41#define LSR_FE 0x08 /* Framing error */
42#define LSR_BI 0x10 /* Break */
43#define LSR_THRE 0x20 /* Xmit holding register empty */
44#define LSR_TEMT 0x40 /* Xmitter empty */
45#define LSR_ERR 0x80 /* Error */
46
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110047#define LCR_DLAB 0x80
48
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +110049static struct NS16550 __iomem *udbg_comport;
Milton Miller7f853352005-09-06 11:56:02 +100050
Andrew Klossneraf9c7242009-03-09 07:52:41 +000051static void udbg_550_flush(void)
Milton Miller7f853352005-09-06 11:56:02 +100052{
53 if (udbg_comport) {
54 while ((in_8(&udbg_comport->lsr) & LSR_THRE) == 0)
55 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +000056 }
57}
58
59static void udbg_550_putc(char c)
60{
61 if (udbg_comport) {
Milton Miller7f853352005-09-06 11:56:02 +100062 if (c == '\n')
63 udbg_550_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +000064 udbg_550_flush();
65 out_8(&udbg_comport->thr, c);
Milton Miller7f853352005-09-06 11:56:02 +100066 }
67}
68
69static int udbg_550_getc_poll(void)
70{
71 if (udbg_comport) {
72 if ((in_8(&udbg_comport->lsr) & LSR_DR) != 0)
73 return in_8(&udbg_comport->rbr);
74 else
75 return -1;
76 }
77 return -1;
78}
79
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110080static int udbg_550_getc(void)
Milton Miller7f853352005-09-06 11:56:02 +100081{
82 if (udbg_comport) {
83 while ((in_8(&udbg_comport->lsr) & LSR_DR) == 0)
84 /* wait for char */;
85 return in_8(&udbg_comport->rbr);
86 }
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110087 return -1;
Milton Miller7f853352005-09-06 11:56:02 +100088}
89
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110090void udbg_init_uart(void __iomem *comport, unsigned int speed,
91 unsigned int clock)
Milton Miller7f853352005-09-06 11:56:02 +100092{
Benjamin Herrenschmidt171505d2006-07-04 14:11:23 +100093 unsigned int dll, base_bauds;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110094
Benjamin Herrenschmidt171505d2006-07-04 14:11:23 +100095 if (clock == 0)
96 clock = 1843200;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110097 if (speed == 0)
98 speed = 9600;
Benjamin Herrenschmidt171505d2006-07-04 14:11:23 +100099
100 base_bauds = clock / 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100101 dll = base_bauds / speed;
Milton Miller7f853352005-09-06 11:56:02 +1000102
103 if (comport) {
104 udbg_comport = (struct NS16550 __iomem *)comport;
105 out_8(&udbg_comport->lcr, 0x00);
106 out_8(&udbg_comport->ier, 0xff);
107 out_8(&udbg_comport->ier, 0x00);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100108 out_8(&udbg_comport->lcr, LCR_DLAB);
109 out_8(&udbg_comport->dll, dll & 0xff);
110 out_8(&udbg_comport->dlm, dll >> 8);
111 /* 8 data, 1 stop, no parity */
112 out_8(&udbg_comport->lcr, 0x03);
113 /* RTS/DTR */
114 out_8(&udbg_comport->mcr, 0x03);
115 /* Clear & enable FIFOs */
116 out_8(&udbg_comport->fcr ,0x07);
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000117 udbg_putc = udbg_550_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000118 udbg_flush = udbg_550_flush;
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000119 udbg_getc = udbg_550_getc;
120 udbg_getc_poll = udbg_550_getc_poll;
Milton Miller7f853352005-09-06 11:56:02 +1000121 }
122}
123
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100124unsigned int udbg_probe_uart_speed(void __iomem *comport, unsigned int clock)
125{
126 unsigned int dll, dlm, divisor, prescaler, speed;
127 u8 old_lcr;
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100128 struct NS16550 __iomem *port = comport;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100129
130 old_lcr = in_8(&port->lcr);
131
132 /* select divisor latch registers. */
133 out_8(&port->lcr, LCR_DLAB);
134
135 /* now, read the divisor */
136 dll = in_8(&port->dll);
137 dlm = in_8(&port->dlm);
138 divisor = dlm << 8 | dll;
139
140 /* check prescaling */
141 if (in_8(&port->mcr) & 0x80)
142 prescaler = 4;
143 else
144 prescaler = 1;
145
146 /* restore the LCR */
147 out_8(&port->lcr, old_lcr);
148
149 /* calculate speed */
150 speed = (clock / prescaler) / (divisor * 16);
151
152 /* sanity check */
roel kluinbb5e6492008-10-14 14:36:31 +0000153 if (speed > (clock / 16))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100154 speed = 9600;
155
156 return speed;
157}
158
Milton Miller7f853352005-09-06 11:56:02 +1000159#ifdef CONFIG_PPC_MAPLE
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000160void udbg_maple_real_flush(void)
Milton Miller7f853352005-09-06 11:56:02 +1000161{
162 if (udbg_comport) {
163 while ((real_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
164 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000165 }
166}
167
168void udbg_maple_real_putc(char c)
169{
170 if (udbg_comport) {
Milton Miller7f853352005-09-06 11:56:02 +1000171 if (c == '\n')
172 udbg_maple_real_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000173 udbg_maple_real_flush();
174 real_writeb(c, &udbg_comport->thr); eieio();
Milton Miller7f853352005-09-06 11:56:02 +1000175 }
176}
177
Michael Ellerman296167a2006-01-11 11:54:09 +1100178void __init udbg_init_maple_realmode(void)
Milton Miller7f853352005-09-06 11:56:02 +1000179{
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100180 udbg_comport = (struct NS16550 __iomem *)0xf40003f8;
Milton Miller7f853352005-09-06 11:56:02 +1000181
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000182 udbg_putc = udbg_maple_real_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000183 udbg_flush = udbg_maple_real_flush;
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000184 udbg_getc = NULL;
185 udbg_getc_poll = NULL;
Milton Miller7f853352005-09-06 11:56:02 +1000186}
187#endif /* CONFIG_PPC_MAPLE */
Olof Johansson39c870d2007-02-04 16:36:49 -0600188
189#ifdef CONFIG_PPC_PASEMI
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000190void udbg_pas_real_flush(void)
Olof Johansson39c870d2007-02-04 16:36:49 -0600191{
192 if (udbg_comport) {
193 while ((real_205_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
194 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000195 }
196}
197
198void udbg_pas_real_putc(char c)
199{
200 if (udbg_comport) {
Olof Johansson39c870d2007-02-04 16:36:49 -0600201 if (c == '\n')
202 udbg_pas_real_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000203 udbg_pas_real_flush();
204 real_205_writeb(c, &udbg_comport->thr); eieio();
Olof Johansson39c870d2007-02-04 16:36:49 -0600205 }
206}
207
208void udbg_init_pas_realmode(void)
209{
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100210 udbg_comport = (struct NS16550 __iomem *)0xfcff03f8UL;
Olof Johansson39c870d2007-02-04 16:36:49 -0600211
212 udbg_putc = udbg_pas_real_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000213 udbg_flush = udbg_pas_real_flush;
Olof Johansson39c870d2007-02-04 16:36:49 -0600214 udbg_getc = NULL;
215 udbg_getc_poll = NULL;
216}
217#endif /* CONFIG_PPC_MAPLE */
David Gibsond9b55a02007-05-08 12:59:31 +1000218
219#ifdef CONFIG_PPC_EARLY_DEBUG_44x
220#include <platforms/44x/44x.h>
221
Benjamin Herrenschmidtf694cda2009-06-16 15:55:19 +0000222static void udbg_44x_as1_flush(void)
David Gibsond9b55a02007-05-08 12:59:31 +1000223{
224 if (udbg_comport) {
225 while ((as1_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
226 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000227 }
228}
229
230static void udbg_44x_as1_putc(char c)
231{
232 if (udbg_comport) {
David Gibsond9b55a02007-05-08 12:59:31 +1000233 if (c == '\n')
234 udbg_44x_as1_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000235 udbg_44x_as1_flush();
236 as1_writeb(c, &udbg_comport->thr); eieio();
David Gibsond9b55a02007-05-08 12:59:31 +1000237 }
238}
239
Hollis Blanchard70dea472007-09-17 05:56:47 -0500240static int udbg_44x_as1_getc(void)
241{
242 if (udbg_comport) {
243 while ((as1_readb(&udbg_comport->lsr) & LSR_DR) == 0)
244 ; /* wait for char */
245 return as1_readb(&udbg_comport->rbr);
246 }
247 return -1;
248}
249
David Gibsond9b55a02007-05-08 12:59:31 +1000250void __init udbg_init_44x_as1(void)
251{
252 udbg_comport =
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100253 (struct NS16550 __iomem *)PPC44x_EARLY_DEBUG_VIRTADDR;
David Gibsond9b55a02007-05-08 12:59:31 +1000254
255 udbg_putc = udbg_44x_as1_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000256 udbg_flush = udbg_44x_as1_flush;
Hollis Blanchard70dea472007-09-17 05:56:47 -0500257 udbg_getc = udbg_44x_as1_getc;
David Gibsond9b55a02007-05-08 12:59:31 +1000258}
259#endif /* CONFIG_PPC_EARLY_DEBUG_44x */
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100260
261#ifdef CONFIG_PPC_EARLY_DEBUG_40x
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000262static void udbg_40x_real_flush(void)
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100263{
264 if (udbg_comport) {
265 while ((real_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
266 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000267 }
268}
269
270static void udbg_40x_real_putc(char c)
271{
272 if (udbg_comport) {
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100273 if (c == '\n')
274 udbg_40x_real_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000275 udbg_40x_real_flush();
276 real_writeb(c, &udbg_comport->thr); eieio();
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100277 }
278}
279
280static int udbg_40x_real_getc(void)
281{
282 if (udbg_comport) {
283 while ((real_readb(&udbg_comport->lsr) & LSR_DR) == 0)
284 ; /* wait for char */
285 return real_readb(&udbg_comport->rbr);
286 }
287 return -1;
288}
289
290void __init udbg_init_40x_realmode(void)
291{
292 udbg_comport = (struct NS16550 __iomem *)
293 CONFIG_PPC_EARLY_DEBUG_40x_PHYSADDR;
294
295 udbg_putc = udbg_40x_real_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000296 udbg_flush = udbg_40x_real_flush;
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100297 udbg_getc = udbg_40x_real_getc;
298 udbg_getc_poll = NULL;
299}
300#endif /* CONFIG_PPC_EARLY_DEBUG_40x */