Merge branch 'sii-m15w' into upstream
[pandora-kernel.git] / arch / powerpc / kernel / udbg_16550.c
1 /*
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  */
11 #include <linux/types.h>
12 #include <asm/udbg.h>
13 #include <asm/io.h>
14
15 extern u8 real_readb(volatile u8 __iomem  *addr);
16 extern void real_writeb(u8 data, volatile u8 __iomem *addr);
17
18 struct NS16550 {
19         /* this struct must be packed */
20         unsigned char rbr;  /* 0 */
21         unsigned char ier;  /* 1 */
22         unsigned char fcr;  /* 2 */
23         unsigned char lcr;  /* 3 */
24         unsigned char mcr;  /* 4 */
25         unsigned char lsr;  /* 5 */
26         unsigned char msr;  /* 6 */
27         unsigned char scr;  /* 7 */
28 };
29
30 #define thr rbr
31 #define iir fcr
32 #define dll rbr
33 #define dlm ier
34 #define dlab lcr
35
36 #define LSR_DR   0x01  /* Data ready */
37 #define LSR_OE   0x02  /* Overrun */
38 #define LSR_PE   0x04  /* Parity error */
39 #define LSR_FE   0x08  /* Framing error */
40 #define LSR_BI   0x10  /* Break */
41 #define LSR_THRE 0x20  /* Xmit holding register empty */
42 #define LSR_TEMT 0x40  /* Xmitter empty */
43 #define LSR_ERR  0x80  /* Error */
44
45 #define LCR_DLAB 0x80
46
47 static volatile struct NS16550 __iomem *udbg_comport;
48
49 static void udbg_550_putc(char c)
50 {
51         if (udbg_comport) {
52                 while ((in_8(&udbg_comport->lsr) & LSR_THRE) == 0)
53                         /* wait for idle */;
54                 out_8(&udbg_comport->thr, c);
55                 if (c == '\n')
56                         udbg_550_putc('\r');
57         }
58 }
59
60 static int udbg_550_getc_poll(void)
61 {
62         if (udbg_comport) {
63                 if ((in_8(&udbg_comport->lsr) & LSR_DR) != 0)
64                         return in_8(&udbg_comport->rbr);
65                 else
66                         return -1;
67         }
68         return -1;
69 }
70
71 static int udbg_550_getc(void)
72 {
73         if (udbg_comport) {
74                 while ((in_8(&udbg_comport->lsr) & LSR_DR) == 0)
75                         /* wait for char */;
76                 return in_8(&udbg_comport->rbr);
77         }
78         return -1;
79 }
80
81 void udbg_init_uart(void __iomem *comport, unsigned int speed,
82                     unsigned int clock)
83 {
84         unsigned int dll, base_bauds;
85
86         if (clock == 0)
87                 clock = 1843200;
88         if (speed == 0)
89                 speed = 9600;
90
91         base_bauds = clock / 16;
92         dll = base_bauds / speed;
93
94         if (comport) {
95                 udbg_comport = (struct NS16550 __iomem *)comport;
96                 out_8(&udbg_comport->lcr, 0x00);
97                 out_8(&udbg_comport->ier, 0xff);
98                 out_8(&udbg_comport->ier, 0x00);
99                 out_8(&udbg_comport->lcr, LCR_DLAB);
100                 out_8(&udbg_comport->dll, dll & 0xff);
101                 out_8(&udbg_comport->dlm, dll >> 8);
102                 /* 8 data, 1 stop, no parity */
103                 out_8(&udbg_comport->lcr, 0x03);
104                 /* RTS/DTR */
105                 out_8(&udbg_comport->mcr, 0x03);
106                 /* Clear & enable FIFOs */
107                 out_8(&udbg_comport->fcr ,0x07);
108                 udbg_putc = udbg_550_putc;
109                 udbg_getc = udbg_550_getc;
110                 udbg_getc_poll = udbg_550_getc_poll;
111         }
112 }
113
114 unsigned int udbg_probe_uart_speed(void __iomem *comport, unsigned int clock)
115 {
116         unsigned int dll, dlm, divisor, prescaler, speed;
117         u8 old_lcr;
118         volatile struct NS16550 __iomem *port = comport;
119
120         old_lcr = in_8(&port->lcr);
121
122         /* select divisor latch registers.  */
123         out_8(&port->lcr, LCR_DLAB);
124
125         /* now, read the divisor */
126         dll = in_8(&port->dll);
127         dlm = in_8(&port->dlm);
128         divisor = dlm << 8 | dll;
129
130         /* check prescaling */
131         if (in_8(&port->mcr) & 0x80)
132                 prescaler = 4;
133         else
134                 prescaler = 1;
135
136         /* restore the LCR */
137         out_8(&port->lcr, old_lcr);
138
139         /* calculate speed */
140         speed = (clock / prescaler) / (divisor * 16);
141
142         /* sanity check */
143         if (speed < 0 || speed > (clock / 16))
144                 speed = 9600;
145
146         return speed;
147 }
148
149 #ifdef CONFIG_PPC_MAPLE
150 void udbg_maple_real_putc(char c)
151 {
152         if (udbg_comport) {
153                 while ((real_readb(&udbg_comport->lsr) & LSR_THRE) == 0)
154                         /* wait for idle */;
155                 real_writeb(c, &udbg_comport->thr); eieio();
156                 if (c == '\n')
157                         udbg_maple_real_putc('\r');
158         }
159 }
160
161 void __init udbg_init_maple_realmode(void)
162 {
163         udbg_comport = (volatile struct NS16550 __iomem *)0xf40003f8;
164
165         udbg_putc = udbg_maple_real_putc;
166         udbg_getc = NULL;
167         udbg_getc_poll = NULL;
168 }
169 #endif /* CONFIG_PPC_MAPLE */