blob: 6837f839ab78714dd435c3c70fe06215fd77835b [file] [log] [blame]
Milton Miller7f853352005-09-06 11:56:02 +10001/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002 * udbg for NS16550 compatible serial ports
Milton Miller7f853352005-09-06 11:56:02 +10003 *
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>
Jack Millera0496d42011-04-14 22:32:08 +000014#include <asm/reg_a2.h>
Milton Miller7f853352005-09-06 11:56:02 +100015
16extern u8 real_readb(volatile u8 __iomem *addr);
17extern void real_writeb(u8 data, volatile u8 __iomem *addr);
Olof Johansson39c870d2007-02-04 16:36:49 -060018extern u8 real_205_readb(volatile u8 __iomem *addr);
19extern void real_205_writeb(u8 data, volatile u8 __iomem *addr);
Milton Miller7f853352005-09-06 11:56:02 +100020
21struct NS16550 {
22 /* this struct must be packed */
23 unsigned char rbr; /* 0 */
24 unsigned char ier; /* 1 */
25 unsigned char fcr; /* 2 */
26 unsigned char lcr; /* 3 */
27 unsigned char mcr; /* 4 */
28 unsigned char lsr; /* 5 */
29 unsigned char msr; /* 6 */
30 unsigned char scr; /* 7 */
31};
32
33#define thr rbr
34#define iir fcr
35#define dll rbr
36#define dlm ier
37#define dlab lcr
38
39#define LSR_DR 0x01 /* Data ready */
40#define LSR_OE 0x02 /* Overrun */
41#define LSR_PE 0x04 /* Parity error */
42#define LSR_FE 0x08 /* Framing error */
43#define LSR_BI 0x10 /* Break */
44#define LSR_THRE 0x20 /* Xmit holding register empty */
45#define LSR_TEMT 0x40 /* Xmitter empty */
46#define LSR_ERR 0x80 /* Error */
47
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110048#define LCR_DLAB 0x80
49
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +110050static struct NS16550 __iomem *udbg_comport;
Milton Miller7f853352005-09-06 11:56:02 +100051
Andrew Klossneraf9c7242009-03-09 07:52:41 +000052static void udbg_550_flush(void)
Milton Miller7f853352005-09-06 11:56:02 +100053{
54 if (udbg_comport) {
55 while ((in_8(&udbg_comport->lsr) & LSR_THRE) == 0)
56 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +000057 }
58}
59
60static void udbg_550_putc(char c)
61{
62 if (udbg_comport) {
Milton Miller7f853352005-09-06 11:56:02 +100063 if (c == '\n')
64 udbg_550_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +000065 udbg_550_flush();
66 out_8(&udbg_comport->thr, c);
Milton Miller7f853352005-09-06 11:56:02 +100067 }
68}
69
70static int udbg_550_getc_poll(void)
71{
72 if (udbg_comport) {
73 if ((in_8(&udbg_comport->lsr) & LSR_DR) != 0)
74 return in_8(&udbg_comport->rbr);
75 else
76 return -1;
77 }
78 return -1;
79}
80
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110081static int udbg_550_getc(void)
Milton Miller7f853352005-09-06 11:56:02 +100082{
83 if (udbg_comport) {
84 while ((in_8(&udbg_comport->lsr) & LSR_DR) == 0)
85 /* wait for char */;
86 return in_8(&udbg_comport->rbr);
87 }
Benjamin Herrenschmidtbb6b9b22005-11-30 16:54:12 +110088 return -1;
Milton Miller7f853352005-09-06 11:56:02 +100089}
90
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110091void udbg_init_uart(void __iomem *comport, unsigned int speed,
92 unsigned int clock)
Milton Miller7f853352005-09-06 11:56:02 +100093{
Benjamin Herrenschmidt171505d2006-07-04 14:11:23 +100094 unsigned int dll, base_bauds;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110095
Benjamin Herrenschmidt171505d2006-07-04 14:11:23 +100096 if (clock == 0)
97 clock = 1843200;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +110098 if (speed == 0)
99 speed = 9600;
Benjamin Herrenschmidt171505d2006-07-04 14:11:23 +1000100
101 base_bauds = clock / 16;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100102 dll = base_bauds / speed;
Milton Miller7f853352005-09-06 11:56:02 +1000103
104 if (comport) {
105 udbg_comport = (struct NS16550 __iomem *)comport;
106 out_8(&udbg_comport->lcr, 0x00);
107 out_8(&udbg_comport->ier, 0xff);
108 out_8(&udbg_comport->ier, 0x00);
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100109 out_8(&udbg_comport->lcr, LCR_DLAB);
110 out_8(&udbg_comport->dll, dll & 0xff);
111 out_8(&udbg_comport->dlm, dll >> 8);
112 /* 8 data, 1 stop, no parity */
113 out_8(&udbg_comport->lcr, 0x03);
114 /* RTS/DTR */
115 out_8(&udbg_comport->mcr, 0x03);
116 /* Clear & enable FIFOs */
117 out_8(&udbg_comport->fcr ,0x07);
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000118 udbg_putc = udbg_550_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000119 udbg_flush = udbg_550_flush;
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000120 udbg_getc = udbg_550_getc;
121 udbg_getc_poll = udbg_550_getc_poll;
Milton Miller7f853352005-09-06 11:56:02 +1000122 }
123}
124
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100125unsigned int udbg_probe_uart_speed(void __iomem *comport, unsigned int clock)
126{
127 unsigned int dll, dlm, divisor, prescaler, speed;
128 u8 old_lcr;
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100129 struct NS16550 __iomem *port = comport;
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100130
131 old_lcr = in_8(&port->lcr);
132
133 /* select divisor latch registers. */
134 out_8(&port->lcr, LCR_DLAB);
135
136 /* now, read the divisor */
137 dll = in_8(&port->dll);
138 dlm = in_8(&port->dlm);
139 divisor = dlm << 8 | dll;
140
141 /* check prescaling */
142 if (in_8(&port->mcr) & 0x80)
143 prescaler = 4;
144 else
145 prescaler = 1;
146
147 /* restore the LCR */
148 out_8(&port->lcr, old_lcr);
149
150 /* calculate speed */
151 speed = (clock / prescaler) / (divisor * 16);
152
153 /* sanity check */
roel kluinbb5e6492008-10-14 14:36:31 +0000154 if (speed > (clock / 16))
Benjamin Herrenschmidt463ce0e2005-11-23 17:56:06 +1100155 speed = 9600;
156
157 return speed;
158}
159
Milton Miller7f853352005-09-06 11:56:02 +1000160#ifdef CONFIG_PPC_MAPLE
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000161void udbg_maple_real_flush(void)
Milton Miller7f853352005-09-06 11:56:02 +1000162{
163 if (udbg_comport) {
164 while ((real_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
165 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000166 }
167}
168
169void udbg_maple_real_putc(char c)
170{
171 if (udbg_comport) {
Milton Miller7f853352005-09-06 11:56:02 +1000172 if (c == '\n')
173 udbg_maple_real_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000174 udbg_maple_real_flush();
175 real_writeb(c, &udbg_comport->thr); eieio();
Milton Miller7f853352005-09-06 11:56:02 +1000176 }
177}
178
Michael Ellerman296167a2006-01-11 11:54:09 +1100179void __init udbg_init_maple_realmode(void)
Milton Miller7f853352005-09-06 11:56:02 +1000180{
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100181 udbg_comport = (struct NS16550 __iomem *)0xf40003f8;
Milton Miller7f853352005-09-06 11:56:02 +1000182
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000183 udbg_putc = udbg_maple_real_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000184 udbg_flush = udbg_maple_real_flush;
Milton Millerc8f1c8b2005-09-06 11:56:42 +1000185 udbg_getc = NULL;
186 udbg_getc_poll = NULL;
Milton Miller7f853352005-09-06 11:56:02 +1000187}
188#endif /* CONFIG_PPC_MAPLE */
Olof Johansson39c870d2007-02-04 16:36:49 -0600189
190#ifdef CONFIG_PPC_PASEMI
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000191void udbg_pas_real_flush(void)
Olof Johansson39c870d2007-02-04 16:36:49 -0600192{
193 if (udbg_comport) {
194 while ((real_205_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
195 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000196 }
197}
198
199void udbg_pas_real_putc(char c)
200{
201 if (udbg_comport) {
Olof Johansson39c870d2007-02-04 16:36:49 -0600202 if (c == '\n')
203 udbg_pas_real_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000204 udbg_pas_real_flush();
205 real_205_writeb(c, &udbg_comport->thr); eieio();
Olof Johansson39c870d2007-02-04 16:36:49 -0600206 }
207}
208
209void udbg_init_pas_realmode(void)
210{
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100211 udbg_comport = (struct NS16550 __iomem *)0xfcff03f8UL;
Olof Johansson39c870d2007-02-04 16:36:49 -0600212
213 udbg_putc = udbg_pas_real_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000214 udbg_flush = udbg_pas_real_flush;
Olof Johansson39c870d2007-02-04 16:36:49 -0600215 udbg_getc = NULL;
216 udbg_getc_poll = NULL;
217}
218#endif /* CONFIG_PPC_MAPLE */
David Gibsond9b55a02007-05-08 12:59:31 +1000219
220#ifdef CONFIG_PPC_EARLY_DEBUG_44x
221#include <platforms/44x/44x.h>
222
Benjamin Herrenschmidtf694cda2009-06-16 15:55:19 +0000223static void udbg_44x_as1_flush(void)
David Gibsond9b55a02007-05-08 12:59:31 +1000224{
225 if (udbg_comport) {
226 while ((as1_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
227 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000228 }
229}
230
231static void udbg_44x_as1_putc(char c)
232{
233 if (udbg_comport) {
David Gibsond9b55a02007-05-08 12:59:31 +1000234 if (c == '\n')
235 udbg_44x_as1_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000236 udbg_44x_as1_flush();
237 as1_writeb(c, &udbg_comport->thr); eieio();
David Gibsond9b55a02007-05-08 12:59:31 +1000238 }
239}
240
Hollis Blanchard70dea472007-09-17 05:56:47 -0500241static int udbg_44x_as1_getc(void)
242{
243 if (udbg_comport) {
244 while ((as1_readb(&udbg_comport->lsr) & LSR_DR) == 0)
245 ; /* wait for char */
246 return as1_readb(&udbg_comport->rbr);
247 }
248 return -1;
249}
250
David Gibsond9b55a02007-05-08 12:59:31 +1000251void __init udbg_init_44x_as1(void)
252{
253 udbg_comport =
Benjamin Herrenschmidtf276b5b2007-12-11 14:48:23 +1100254 (struct NS16550 __iomem *)PPC44x_EARLY_DEBUG_VIRTADDR;
David Gibsond9b55a02007-05-08 12:59:31 +1000255
256 udbg_putc = udbg_44x_as1_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000257 udbg_flush = udbg_44x_as1_flush;
Hollis Blanchard70dea472007-09-17 05:56:47 -0500258 udbg_getc = udbg_44x_as1_getc;
David Gibsond9b55a02007-05-08 12:59:31 +1000259}
260#endif /* CONFIG_PPC_EARLY_DEBUG_44x */
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100261
262#ifdef CONFIG_PPC_EARLY_DEBUG_40x
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000263static void udbg_40x_real_flush(void)
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100264{
265 if (udbg_comport) {
266 while ((real_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
267 /* wait for idle */;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000268 }
269}
270
271static void udbg_40x_real_putc(char c)
272{
273 if (udbg_comport) {
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100274 if (c == '\n')
275 udbg_40x_real_putc('\r');
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000276 udbg_40x_real_flush();
277 real_writeb(c, &udbg_comport->thr); eieio();
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100278 }
279}
280
281static int udbg_40x_real_getc(void)
282{
283 if (udbg_comport) {
284 while ((real_readb(&udbg_comport->lsr) & LSR_DR) == 0)
285 ; /* wait for char */
286 return real_readb(&udbg_comport->rbr);
287 }
288 return -1;
289}
290
291void __init udbg_init_40x_realmode(void)
292{
293 udbg_comport = (struct NS16550 __iomem *)
294 CONFIG_PPC_EARLY_DEBUG_40x_PHYSADDR;
295
296 udbg_putc = udbg_40x_real_putc;
Andrew Klossneraf9c7242009-03-09 07:52:41 +0000297 udbg_flush = udbg_40x_real_flush;
Benjamin Herrenschmidt9dae8af2007-12-21 15:39:26 +1100298 udbg_getc = udbg_40x_real_getc;
299 udbg_getc_poll = NULL;
300}
301#endif /* CONFIG_PPC_EARLY_DEBUG_40x */
Jack Millera0496d42011-04-14 22:32:08 +0000302
303#ifdef CONFIG_PPC_EARLY_DEBUG_WSP
304static void udbg_wsp_flush(void)
305{
306 if (udbg_comport) {
307 while ((readb(&udbg_comport->lsr) & LSR_THRE) == 0)
308 /* wait for idle */;
309 }
310}
311
312static void udbg_wsp_putc(char c)
313{
314 if (udbg_comport) {
315 if (c == '\n')
316 udbg_wsp_putc('\r');
317 udbg_wsp_flush();
318 writeb(c, &udbg_comport->thr); eieio();
319 }
320}
321
322static int udbg_wsp_getc(void)
323{
324 if (udbg_comport) {
325 while ((readb(&udbg_comport->lsr) & LSR_DR) == 0)
326 ; /* wait for char */
327 return readb(&udbg_comport->rbr);
328 }
329 return -1;
330}
331
332static int udbg_wsp_getc_poll(void)
333{
334 if (udbg_comport)
335 if (readb(&udbg_comport->lsr) & LSR_DR)
336 return readb(&udbg_comport->rbr);
337 return -1;
338}
339
340void __init udbg_init_wsp(void)
341{
342 udbg_comport = (struct NS16550 __iomem *)WSP_UART_VIRT;
343
344 udbg_init_uart(udbg_comport, 57600, 50000000);
345
346 udbg_putc = udbg_wsp_putc;
347 udbg_flush = udbg_wsp_flush;
348 udbg_getc = udbg_wsp_getc;
349 udbg_getc_poll = udbg_wsp_getc_poll;
350}
351#endif /* CONFIG_PPC_EARLY_DEBUG_WSP */