blob: 50d3b5e4ec732f568aa6a13f6e8d8f3d150ab851 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* suncore.c
2 *
3 * Common SUN serial routines. Based entirely
4 * upon drivers/sbus/char/sunserial.c which is:
5 *
6 * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
7 *
8 * Adaptation to new UART layer is:
9 *
10 * Copyright (C) 2002 David S. Miller (davem@redhat.com)
11 */
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/kernel.h>
15#include <linux/console.h>
16#include <linux/tty.h>
17#include <linux/errno.h>
18#include <linux/string.h>
David S. Millerc73fcc82007-07-20 16:59:26 -070019#include <linux/serial_core.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/init.h>
21
David S. Millerc73fcc82007-07-20 16:59:26 -070022#include <asm/prom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include "suncore.h"
25
Martin Habets58d784a2007-12-11 03:37:04 -080026static int sunserial_current_minor = 64;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Martin Habets58d784a2007-12-11 03:37:04 -080028int sunserial_register_minors(struct uart_driver *drv, int count)
29{
30 int err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Martin Habets58d784a2007-12-11 03:37:04 -080032 drv->minor = sunserial_current_minor;
33 drv->nr += count;
34 /* Register the driver on the first call */
35 if (drv->nr == count)
36 err = uart_register_driver(drv);
37 if (err == 0) {
38 sunserial_current_minor += count;
39 drv->tty_driver->name_base = drv->minor - 64;
40 }
41 return err;
42}
43EXPORT_SYMBOL(sunserial_register_minors);
44
45void sunserial_unregister_minors(struct uart_driver *drv, int count)
46{
47 drv->nr -= count;
48 sunserial_current_minor -= count;
49
50 if (drv->nr == 0)
51 uart_unregister_driver(drv);
52}
53EXPORT_SYMBOL(sunserial_unregister_minors);
54
David S. Millerfb445ee2007-12-29 01:19:49 -080055int sunserial_console_match(struct console *con, struct device_node *dp,
David S. Miller4e3533d2009-11-24 14:03:34 -080056 struct uart_driver *drv, int line, bool ignore_line)
David S. Millerc73fcc82007-07-20 16:59:26 -070057{
David S. Millerc73fcc82007-07-20 16:59:26 -070058 if (!con || of_console_device != dp)
59 return 0;
60
David S. Miller4e3533d2009-11-24 14:03:34 -080061 if (!ignore_line) {
62 int off = 0;
David S. Millerc73fcc82007-07-20 16:59:26 -070063
David S. Miller4e3533d2009-11-24 14:03:34 -080064 if (of_console_options &&
65 *of_console_options == 'b')
66 off = 1;
67
68 if ((line & 1) != off)
69 return 0;
70 }
David S. Millerc73fcc82007-07-20 16:59:26 -070071
72 con->index = line;
73 drv->cons = con;
74 add_preferred_console(con->name, line, NULL);
75
76 return 1;
77}
78EXPORT_SYMBOL(sunserial_console_match);
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080void
81sunserial_console_termios(struct console *con)
82{
David S. Millerc73fcc82007-07-20 16:59:26 -070083 struct device_node *dp;
84 const char *od, *mode, *s;
Jan Engelhardtb00ccd02007-05-27 14:48:54 -070085 char mode_prop[] = "ttyX-mode";
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 int baud, bits, stop, cflag;
87 char parity;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
David S. Millerc73fcc82007-07-20 16:59:26 -070089 dp = of_find_node_by_path("/options");
90 od = of_get_property(dp, "output-device", NULL);
91 if (!strcmp(od, "rsc")) {
92 mode = of_get_property(of_console_device,
93 "ssp-console-modes", NULL);
94 if (!mode)
95 mode = "115200,8,n,1,-";
96 } else {
97 char c;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
David S. Millerc73fcc82007-07-20 16:59:26 -070099 c = 'a';
100 if (of_console_options)
101 c = *of_console_options;
Eddie C. Dostc126cf82006-01-18 14:54:31 -0800102
David S. Millerc73fcc82007-07-20 16:59:26 -0700103 mode_prop[3] = c;
Eddie C. Dostc126cf82006-01-18 14:54:31 -0800104
David S. Millerc73fcc82007-07-20 16:59:26 -0700105 mode = of_get_property(dp, mode_prop, NULL);
106 if (!mode)
107 mode = "9600,8,n,1,-";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 cflag = CREAD | HUPCL | CLOCAL;
111
112 s = mode;
113 baud = simple_strtoul(s, NULL, 0);
114 s = strchr(s, ',');
115 bits = simple_strtoul(++s, NULL, 0);
116 s = strchr(s, ',');
117 parity = *(++s);
118 s = strchr(s, ',');
119 stop = simple_strtoul(++s, NULL, 0);
120 s = strchr(s, ',');
121 /* XXX handshake is not handled here. */
122
123 switch (baud) {
124 case 150: cflag |= B150; break;
125 case 300: cflag |= B300; break;
126 case 600: cflag |= B600; break;
127 case 1200: cflag |= B1200; break;
128 case 2400: cflag |= B2400; break;
129 case 4800: cflag |= B4800; break;
130 case 9600: cflag |= B9600; break;
131 case 19200: cflag |= B19200; break;
132 case 38400: cflag |= B38400; break;
Eddie C. Dostc126cf82006-01-18 14:54:31 -0800133 case 57600: cflag |= B57600; break;
134 case 115200: cflag |= B115200; break;
135 case 230400: cflag |= B230400; break;
136 case 460800: cflag |= B460800; break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 default: baud = 9600; cflag |= B9600; break;
138 }
139
140 switch (bits) {
141 case 5: cflag |= CS5; break;
142 case 6: cflag |= CS6; break;
143 case 7: cflag |= CS7; break;
144 case 8: cflag |= CS8; break;
145 default: cflag |= CS8; break;
146 }
147
148 switch (parity) {
149 case 'o': cflag |= (PARENB | PARODD); break;
150 case 'e': cflag |= PARENB; break;
151 case 'n': default: break;
152 }
153
154 switch (stop) {
155 case 2: cflag |= CSTOPB; break;
156 case 1: default: break;
157 }
158
159 con->cflag = cflag;
160}
161
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162/* Sun serial MOUSE auto baud rate detection. */
163static struct mouse_baud_cflag {
164 int baud;
165 unsigned int cflag;
166} mouse_baud_table[] = {
167 { 1200, B1200 },
168 { 2400, B2400 },
169 { 4800, B4800 },
170 { 9600, B9600 },
171 { -1, ~0 },
172 { -1, ~0 },
173};
174
175unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud)
176{
177 int i;
178
179 for (i = 0; mouse_baud_table[i].baud != -1; i++)
180 if (mouse_baud_table[i].cflag == (cflag & CBAUD))
181 break;
182
183 i += 1;
184 if (mouse_baud_table[i].baud == -1)
185 i = 0;
186
187 *new_baud = mouse_baud_table[i].baud;
188 return mouse_baud_table[i].cflag;
189}
190
191EXPORT_SYMBOL(suncore_mouse_baud_cflag_next);
192
193/* Basically, when the baud rate is wrong the mouse spits out
194 * breaks to us.
195 */
196int suncore_mouse_baud_detection(unsigned char ch, int is_break)
197{
198 static int mouse_got_break = 0;
199 static int ctr = 0;
200
201 if (is_break) {
202 /* Let a few normal bytes go by before we jump the gun
203 * and say we need to try another baud rate.
204 */
205 if (mouse_got_break && ctr < 8)
206 return 1;
207
208 /* Ok, we need to try another baud. */
209 ctr = 0;
210 mouse_got_break = 1;
211 return 2;
212 }
213 if (mouse_got_break) {
214 ctr++;
215 if (ch == 0x87) {
216 /* Correct baud rate determined. */
217 mouse_got_break = 0;
218 }
219 return 1;
220 }
221 return 0;
222}
223
224EXPORT_SYMBOL(suncore_mouse_baud_detection);
225
226static int __init suncore_init(void)
227{
228 return 0;
229}
230
231static void __exit suncore_exit(void)
232{
233}
234
235module_init(suncore_init);
236module_exit(suncore_exit);
237
238MODULE_AUTHOR("Eddie C. Dost, David S. Miller");
239MODULE_DESCRIPTION("Sun serial common layer");
240MODULE_LICENSE("GPL");