Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #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. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 19 | #include <linux/serial_core.h> |
Paul Gortmaker | 6816383 | 2012-02-09 18:48:19 -0500 | [diff] [blame] | 20 | #include <linux/sunserialcore.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/init.h> |
| 22 | |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 23 | #include <asm/prom.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | |
Martin Habets | 58d784a | 2007-12-11 03:37:04 -0800 | [diff] [blame] | 26 | static int sunserial_current_minor = 64; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
Martin Habets | 58d784a | 2007-12-11 03:37:04 -0800 | [diff] [blame] | 28 | int sunserial_register_minors(struct uart_driver *drv, int count) |
| 29 | { |
| 30 | int err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Martin Habets | 58d784a | 2007-12-11 03:37:04 -0800 | [diff] [blame] | 32 | 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 | } |
| 43 | EXPORT_SYMBOL(sunserial_register_minors); |
| 44 | |
| 45 | void 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 | } |
| 53 | EXPORT_SYMBOL(sunserial_unregister_minors); |
| 54 | |
David S. Miller | fb445ee | 2007-12-29 01:19:49 -0800 | [diff] [blame] | 55 | int sunserial_console_match(struct console *con, struct device_node *dp, |
David S. Miller | 4e3533d | 2009-11-24 14:03:34 -0800 | [diff] [blame] | 56 | struct uart_driver *drv, int line, bool ignore_line) |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 57 | { |
David S. Miller | 0a49289 | 2010-08-15 00:26:14 -0700 | [diff] [blame] | 58 | if (!con) |
| 59 | return 0; |
| 60 | |
| 61 | drv->cons = con; |
| 62 | |
| 63 | if (of_console_device != dp) |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 64 | return 0; |
| 65 | |
David S. Miller | 4e3533d | 2009-11-24 14:03:34 -0800 | [diff] [blame] | 66 | if (!ignore_line) { |
| 67 | int off = 0; |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 68 | |
David S. Miller | 4e3533d | 2009-11-24 14:03:34 -0800 | [diff] [blame] | 69 | if (of_console_options && |
| 70 | *of_console_options == 'b') |
| 71 | off = 1; |
| 72 | |
| 73 | if ((line & 1) != off) |
| 74 | return 0; |
| 75 | } |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 76 | |
David S. Miller | 0a49289 | 2010-08-15 00:26:14 -0700 | [diff] [blame] | 77 | if (!console_set_on_cmdline) { |
| 78 | con->index = line; |
David S. Miller | 7c1f6af | 2010-05-25 23:51:17 -0700 | [diff] [blame] | 79 | add_preferred_console(con->name, line, NULL); |
David S. Miller | 0a49289 | 2010-08-15 00:26:14 -0700 | [diff] [blame] | 80 | } |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 81 | return 1; |
| 82 | } |
| 83 | EXPORT_SYMBOL(sunserial_console_match); |
| 84 | |
David S. Miller | 457931d | 2009-11-24 14:09:56 -0800 | [diff] [blame] | 85 | void sunserial_console_termios(struct console *con, struct device_node *uart_dp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | { |
David S. Miller | 457931d | 2009-11-24 14:09:56 -0800 | [diff] [blame] | 87 | const char *mode, *s; |
Jan Engelhardt | b00ccd0 | 2007-05-27 14:48:54 -0700 | [diff] [blame] | 88 | char mode_prop[] = "ttyX-mode"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | int baud, bits, stop, cflag; |
| 90 | char parity; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
David S. Miller | 457931d | 2009-11-24 14:09:56 -0800 | [diff] [blame] | 92 | if (!strcmp(uart_dp->name, "rsc") || |
| 93 | !strcmp(uart_dp->name, "rsc-console") || |
| 94 | !strcmp(uart_dp->name, "rsc-control")) { |
| 95 | mode = of_get_property(uart_dp, |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 96 | "ssp-console-modes", NULL); |
| 97 | if (!mode) |
| 98 | mode = "115200,8,n,1,-"; |
David S. Miller | 457931d | 2009-11-24 14:09:56 -0800 | [diff] [blame] | 99 | } else if (!strcmp(uart_dp->name, "lom-console")) { |
| 100 | mode = "9600,8,n,1,-"; |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 101 | } else { |
David S. Miller | 457931d | 2009-11-24 14:09:56 -0800 | [diff] [blame] | 102 | struct device_node *dp; |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 103 | char c; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 105 | c = 'a'; |
| 106 | if (of_console_options) |
| 107 | c = *of_console_options; |
Eddie C. Dost | c126cf8 | 2006-01-18 14:54:31 -0800 | [diff] [blame] | 108 | |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 109 | mode_prop[3] = c; |
Eddie C. Dost | c126cf8 | 2006-01-18 14:54:31 -0800 | [diff] [blame] | 110 | |
David S. Miller | 457931d | 2009-11-24 14:09:56 -0800 | [diff] [blame] | 111 | dp = of_find_node_by_path("/options"); |
David S. Miller | c73fcc8 | 2007-07-20 16:59:26 -0700 | [diff] [blame] | 112 | mode = of_get_property(dp, mode_prop, NULL); |
| 113 | if (!mode) |
| 114 | mode = "9600,8,n,1,-"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | cflag = CREAD | HUPCL | CLOCAL; |
| 118 | |
| 119 | s = mode; |
| 120 | baud = simple_strtoul(s, NULL, 0); |
| 121 | s = strchr(s, ','); |
| 122 | bits = simple_strtoul(++s, NULL, 0); |
| 123 | s = strchr(s, ','); |
| 124 | parity = *(++s); |
| 125 | s = strchr(s, ','); |
| 126 | stop = simple_strtoul(++s, NULL, 0); |
| 127 | s = strchr(s, ','); |
| 128 | /* XXX handshake is not handled here. */ |
| 129 | |
| 130 | switch (baud) { |
| 131 | case 150: cflag |= B150; break; |
| 132 | case 300: cflag |= B300; break; |
| 133 | case 600: cflag |= B600; break; |
| 134 | case 1200: cflag |= B1200; break; |
| 135 | case 2400: cflag |= B2400; break; |
| 136 | case 4800: cflag |= B4800; break; |
| 137 | case 9600: cflag |= B9600; break; |
| 138 | case 19200: cflag |= B19200; break; |
| 139 | case 38400: cflag |= B38400; break; |
Eddie C. Dost | c126cf8 | 2006-01-18 14:54:31 -0800 | [diff] [blame] | 140 | case 57600: cflag |= B57600; break; |
| 141 | case 115200: cflag |= B115200; break; |
| 142 | case 230400: cflag |= B230400; break; |
| 143 | case 460800: cflag |= B460800; break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | default: baud = 9600; cflag |= B9600; break; |
| 145 | } |
| 146 | |
| 147 | switch (bits) { |
| 148 | case 5: cflag |= CS5; break; |
| 149 | case 6: cflag |= CS6; break; |
| 150 | case 7: cflag |= CS7; break; |
| 151 | case 8: cflag |= CS8; break; |
| 152 | default: cflag |= CS8; break; |
| 153 | } |
| 154 | |
| 155 | switch (parity) { |
| 156 | case 'o': cflag |= (PARENB | PARODD); break; |
| 157 | case 'e': cflag |= PARENB; break; |
| 158 | case 'n': default: break; |
| 159 | } |
| 160 | |
| 161 | switch (stop) { |
| 162 | case 2: cflag |= CSTOPB; break; |
| 163 | case 1: default: break; |
| 164 | } |
| 165 | |
| 166 | con->cflag = cflag; |
| 167 | } |
| 168 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | /* Sun serial MOUSE auto baud rate detection. */ |
| 170 | static struct mouse_baud_cflag { |
| 171 | int baud; |
| 172 | unsigned int cflag; |
| 173 | } mouse_baud_table[] = { |
| 174 | { 1200, B1200 }, |
| 175 | { 2400, B2400 }, |
| 176 | { 4800, B4800 }, |
| 177 | { 9600, B9600 }, |
| 178 | { -1, ~0 }, |
| 179 | { -1, ~0 }, |
| 180 | }; |
| 181 | |
| 182 | unsigned int suncore_mouse_baud_cflag_next(unsigned int cflag, int *new_baud) |
| 183 | { |
| 184 | int i; |
| 185 | |
| 186 | for (i = 0; mouse_baud_table[i].baud != -1; i++) |
| 187 | if (mouse_baud_table[i].cflag == (cflag & CBAUD)) |
| 188 | break; |
| 189 | |
| 190 | i += 1; |
| 191 | if (mouse_baud_table[i].baud == -1) |
| 192 | i = 0; |
| 193 | |
| 194 | *new_baud = mouse_baud_table[i].baud; |
| 195 | return mouse_baud_table[i].cflag; |
| 196 | } |
| 197 | |
| 198 | EXPORT_SYMBOL(suncore_mouse_baud_cflag_next); |
| 199 | |
| 200 | /* Basically, when the baud rate is wrong the mouse spits out |
| 201 | * breaks to us. |
| 202 | */ |
| 203 | int suncore_mouse_baud_detection(unsigned char ch, int is_break) |
| 204 | { |
| 205 | static int mouse_got_break = 0; |
| 206 | static int ctr = 0; |
| 207 | |
| 208 | if (is_break) { |
| 209 | /* Let a few normal bytes go by before we jump the gun |
| 210 | * and say we need to try another baud rate. |
| 211 | */ |
| 212 | if (mouse_got_break && ctr < 8) |
| 213 | return 1; |
| 214 | |
| 215 | /* Ok, we need to try another baud. */ |
| 216 | ctr = 0; |
| 217 | mouse_got_break = 1; |
| 218 | return 2; |
| 219 | } |
| 220 | if (mouse_got_break) { |
| 221 | ctr++; |
| 222 | if (ch == 0x87) { |
| 223 | /* Correct baud rate determined. */ |
| 224 | mouse_got_break = 0; |
| 225 | } |
| 226 | return 1; |
| 227 | } |
| 228 | return 0; |
| 229 | } |
| 230 | |
| 231 | EXPORT_SYMBOL(suncore_mouse_baud_detection); |
| 232 | |
| 233 | static int __init suncore_init(void) |
| 234 | { |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | static void __exit suncore_exit(void) |
| 239 | { |
| 240 | } |
| 241 | |
| 242 | module_init(suncore_init); |
| 243 | module_exit(suncore_exit); |
| 244 | |
| 245 | MODULE_AUTHOR("Eddie C. Dost, David S. Miller"); |
| 246 | MODULE_DESCRIPTION("Sun serial common layer"); |
| 247 | MODULE_LICENSE("GPL"); |