Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * linux/drivers/char/8250.c |
| 3 | * |
| 4 | * Driver for 8250/16550-type serial ports |
| 5 | * |
| 6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. |
| 7 | * |
| 8 | * Copyright (C) 2001 Russell King. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * $Id: 8250.c,v 1.90 2002/07/28 10:03:27 rmk Exp $ |
| 16 | * |
| 17 | * A note about mapbase / membase |
| 18 | * |
| 19 | * mapbase is the physical address of the IO port. |
| 20 | * membase is an 'ioremapped' cookie. |
| 21 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) |
| 24 | #define SUPPORT_SYSRQ |
| 25 | #endif |
| 26 | |
| 27 | #include <linux/module.h> |
| 28 | #include <linux/moduleparam.h> |
| 29 | #include <linux/ioport.h> |
| 30 | #include <linux/init.h> |
| 31 | #include <linux/console.h> |
| 32 | #include <linux/sysrq.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | #include <linux/delay.h> |
Russell King | d052d1b | 2005-10-29 19:07:23 +0100 | [diff] [blame] | 34 | #include <linux/platform_device.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | #include <linux/tty.h> |
| 36 | #include <linux/tty_flip.h> |
| 37 | #include <linux/serial_reg.h> |
| 38 | #include <linux/serial_core.h> |
| 39 | #include <linux/serial.h> |
| 40 | #include <linux/serial_8250.h> |
Andrew Morton | 78512ec | 2005-11-07 00:59:13 -0800 | [diff] [blame] | 41 | #include <linux/nmi.h> |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 42 | #include <linux/mutex.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
| 44 | #include <asm/io.h> |
| 45 | #include <asm/irq.h> |
| 46 | |
| 47 | #include "8250.h" |
| 48 | |
| 49 | /* |
| 50 | * Configuration: |
Thomas Gleixner | 40663cc | 2006-07-01 19:29:43 -0700 | [diff] [blame] | 51 | * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | * is unsafe when used on edge-triggered interrupts. |
| 53 | */ |
Adrian Bunk | 408b664 | 2005-05-01 08:59:29 -0700 | [diff] [blame] | 54 | static unsigned int share_irqs = SERIAL8250_SHARE_IRQS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 56 | static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS; |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
| 59 | * Debugging. |
| 60 | */ |
| 61 | #if 0 |
| 62 | #define DEBUG_AUTOCONF(fmt...) printk(fmt) |
| 63 | #else |
| 64 | #define DEBUG_AUTOCONF(fmt...) do { } while (0) |
| 65 | #endif |
| 66 | |
| 67 | #if 0 |
| 68 | #define DEBUG_INTR(fmt...) printk(fmt) |
| 69 | #else |
| 70 | #define DEBUG_INTR(fmt...) do { } while (0) |
| 71 | #endif |
| 72 | |
| 73 | #define PASS_LIMIT 256 |
| 74 | |
| 75 | /* |
| 76 | * We default to IRQ0 for the "no irq" hack. Some |
| 77 | * machine types want others as well - they're free |
| 78 | * to redefine this in their header file. |
| 79 | */ |
| 80 | #define is_real_interrupt(irq) ((irq) != 0) |
| 81 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | #ifdef CONFIG_SERIAL_8250_DETECT_IRQ |
| 83 | #define CONFIG_SERIAL_DETECT_IRQ 1 |
| 84 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | #ifdef CONFIG_SERIAL_8250_MANY_PORTS |
| 86 | #define CONFIG_SERIAL_MANY_PORTS 1 |
| 87 | #endif |
| 88 | |
| 89 | /* |
| 90 | * HUB6 is always on. This will be removed once the header |
| 91 | * files have been cleaned. |
| 92 | */ |
| 93 | #define CONFIG_HUB6 1 |
| 94 | |
| 95 | #include <asm/serial.h> |
| 96 | |
| 97 | /* |
| 98 | * SERIAL_PORT_DFNS tells us about built-in ports that have no |
| 99 | * standard enumeration mechanism. Platforms that can find all |
| 100 | * serial ports via mechanisms like ACPI or PCI need not supply it. |
| 101 | */ |
| 102 | #ifndef SERIAL_PORT_DFNS |
| 103 | #define SERIAL_PORT_DFNS |
| 104 | #endif |
| 105 | |
Arjan van de Ven | cb3592b | 2005-11-28 21:04:11 +0000 | [diff] [blame] | 106 | static const struct old_serial_port old_serial_port[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | SERIAL_PORT_DFNS /* defined in asm/serial.h */ |
| 108 | }; |
| 109 | |
Russell King | 026d02a | 2005-06-29 18:45:19 +0100 | [diff] [blame] | 110 | #define UART_NR CONFIG_SERIAL_8250_NR_UARTS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | |
| 112 | #ifdef CONFIG_SERIAL_8250_RSA |
| 113 | |
| 114 | #define PORT_RSA_MAX 4 |
| 115 | static unsigned long probe_rsa[PORT_RSA_MAX]; |
| 116 | static unsigned int probe_rsa_count; |
| 117 | #endif /* CONFIG_SERIAL_8250_RSA */ |
| 118 | |
| 119 | struct uart_8250_port { |
| 120 | struct uart_port port; |
| 121 | struct timer_list timer; /* "no irq" timer */ |
| 122 | struct list_head list; /* ports on this IRQ */ |
Russell King | 4ba5e35 | 2005-06-23 10:43:04 +0100 | [diff] [blame] | 123 | unsigned short capabilities; /* port capabilities */ |
| 124 | unsigned short bugs; /* port bugs */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | unsigned int tx_loadsz; /* transmit fifo load size */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | unsigned char acr; |
| 127 | unsigned char ier; |
| 128 | unsigned char lcr; |
| 129 | unsigned char mcr; |
| 130 | unsigned char mcr_mask; /* mask of user bits */ |
| 131 | unsigned char mcr_force; /* mask of forced bits */ |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 132 | |
| 133 | /* |
| 134 | * Some bits in registers are cleared on a read, so they must |
| 135 | * be saved whenever the register is read but the bits will not |
| 136 | * be immediately processed. |
| 137 | */ |
| 138 | #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS |
| 139 | unsigned char lsr_saved_flags; |
| 140 | #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA |
| 141 | unsigned char msr_saved_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 142 | |
| 143 | /* |
| 144 | * We provide a per-port pm hook. |
| 145 | */ |
| 146 | void (*pm)(struct uart_port *port, |
| 147 | unsigned int state, unsigned int old); |
| 148 | }; |
| 149 | |
| 150 | struct irq_info { |
| 151 | spinlock_t lock; |
| 152 | struct list_head *head; |
| 153 | }; |
| 154 | |
| 155 | static struct irq_info irq_lists[NR_IRQS]; |
| 156 | |
| 157 | /* |
| 158 | * Here we define the default xmit fifo size used for each type of UART. |
| 159 | */ |
| 160 | static const struct serial8250_config uart_config[] = { |
| 161 | [PORT_UNKNOWN] = { |
| 162 | .name = "unknown", |
| 163 | .fifo_size = 1, |
| 164 | .tx_loadsz = 1, |
| 165 | }, |
| 166 | [PORT_8250] = { |
| 167 | .name = "8250", |
| 168 | .fifo_size = 1, |
| 169 | .tx_loadsz = 1, |
| 170 | }, |
| 171 | [PORT_16450] = { |
| 172 | .name = "16450", |
| 173 | .fifo_size = 1, |
| 174 | .tx_loadsz = 1, |
| 175 | }, |
| 176 | [PORT_16550] = { |
| 177 | .name = "16550", |
| 178 | .fifo_size = 1, |
| 179 | .tx_loadsz = 1, |
| 180 | }, |
| 181 | [PORT_16550A] = { |
| 182 | .name = "16550A", |
| 183 | .fifo_size = 16, |
| 184 | .tx_loadsz = 16, |
| 185 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, |
| 186 | .flags = UART_CAP_FIFO, |
| 187 | }, |
| 188 | [PORT_CIRRUS] = { |
| 189 | .name = "Cirrus", |
| 190 | .fifo_size = 1, |
| 191 | .tx_loadsz = 1, |
| 192 | }, |
| 193 | [PORT_16650] = { |
| 194 | .name = "ST16650", |
| 195 | .fifo_size = 1, |
| 196 | .tx_loadsz = 1, |
| 197 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, |
| 198 | }, |
| 199 | [PORT_16650V2] = { |
| 200 | .name = "ST16650V2", |
| 201 | .fifo_size = 32, |
| 202 | .tx_loadsz = 16, |
| 203 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | |
| 204 | UART_FCR_T_TRIG_00, |
| 205 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, |
| 206 | }, |
| 207 | [PORT_16750] = { |
| 208 | .name = "TI16750", |
| 209 | .fifo_size = 64, |
| 210 | .tx_loadsz = 64, |
| 211 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | |
| 212 | UART_FCR7_64BYTE, |
| 213 | .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE, |
| 214 | }, |
| 215 | [PORT_STARTECH] = { |
| 216 | .name = "Startech", |
| 217 | .fifo_size = 1, |
| 218 | .tx_loadsz = 1, |
| 219 | }, |
| 220 | [PORT_16C950] = { |
| 221 | .name = "16C950/954", |
| 222 | .fifo_size = 128, |
| 223 | .tx_loadsz = 128, |
| 224 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, |
| 225 | .flags = UART_CAP_FIFO, |
| 226 | }, |
| 227 | [PORT_16654] = { |
| 228 | .name = "ST16654", |
| 229 | .fifo_size = 64, |
| 230 | .tx_loadsz = 32, |
| 231 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | |
| 232 | UART_FCR_T_TRIG_10, |
| 233 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, |
| 234 | }, |
| 235 | [PORT_16850] = { |
| 236 | .name = "XR16850", |
| 237 | .fifo_size = 128, |
| 238 | .tx_loadsz = 128, |
| 239 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, |
| 240 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, |
| 241 | }, |
| 242 | [PORT_RSA] = { |
| 243 | .name = "RSA", |
| 244 | .fifo_size = 2048, |
| 245 | .tx_loadsz = 2048, |
| 246 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11, |
| 247 | .flags = UART_CAP_FIFO, |
| 248 | }, |
| 249 | [PORT_NS16550A] = { |
| 250 | .name = "NS16550A", |
| 251 | .fifo_size = 16, |
| 252 | .tx_loadsz = 16, |
| 253 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, |
| 254 | .flags = UART_CAP_FIFO | UART_NATSEMI, |
| 255 | }, |
| 256 | [PORT_XSCALE] = { |
| 257 | .name = "XScale", |
| 258 | .fifo_size = 32, |
| 259 | .tx_loadsz = 32, |
| 260 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, |
| 261 | .flags = UART_CAP_FIFO | UART_CAP_UUE, |
| 262 | }, |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 263 | [PORT_RM9000] = { |
| 264 | .name = "RM9000", |
| 265 | .fifo_size = 16, |
| 266 | .tx_loadsz = 16, |
| 267 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, |
| 268 | .flags = UART_CAP_FIFO, |
| 269 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | }; |
| 271 | |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 272 | #if defined (CONFIG_SERIAL_8250_AU1X00) |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 273 | |
| 274 | /* Au1x00 UART hardware has a weird register layout */ |
| 275 | static const u8 au_io_in_map[] = { |
| 276 | [UART_RX] = 0, |
| 277 | [UART_IER] = 2, |
| 278 | [UART_IIR] = 3, |
| 279 | [UART_LCR] = 5, |
| 280 | [UART_MCR] = 6, |
| 281 | [UART_LSR] = 7, |
| 282 | [UART_MSR] = 8, |
| 283 | }; |
| 284 | |
| 285 | static const u8 au_io_out_map[] = { |
| 286 | [UART_TX] = 1, |
| 287 | [UART_IER] = 2, |
| 288 | [UART_FCR] = 4, |
| 289 | [UART_LCR] = 5, |
| 290 | [UART_MCR] = 6, |
| 291 | }; |
| 292 | |
| 293 | /* sane hardware needs no mapping */ |
| 294 | static inline int map_8250_in_reg(struct uart_8250_port *up, int offset) |
| 295 | { |
| 296 | if (up->port.iotype != UPIO_AU) |
| 297 | return offset; |
| 298 | return au_io_in_map[offset]; |
| 299 | } |
| 300 | |
| 301 | static inline int map_8250_out_reg(struct uart_8250_port *up, int offset) |
| 302 | { |
| 303 | if (up->port.iotype != UPIO_AU) |
| 304 | return offset; |
| 305 | return au_io_out_map[offset]; |
| 306 | } |
| 307 | |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 308 | #elif defined(CONFIG_SERIAL_8250_RM9K) |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 309 | |
| 310 | static const u8 |
| 311 | regmap_in[8] = { |
| 312 | [UART_RX] = 0x00, |
| 313 | [UART_IER] = 0x0c, |
| 314 | [UART_IIR] = 0x14, |
| 315 | [UART_LCR] = 0x1c, |
| 316 | [UART_MCR] = 0x20, |
| 317 | [UART_LSR] = 0x24, |
| 318 | [UART_MSR] = 0x28, |
| 319 | [UART_SCR] = 0x2c |
| 320 | }, |
| 321 | regmap_out[8] = { |
| 322 | [UART_TX] = 0x04, |
| 323 | [UART_IER] = 0x0c, |
| 324 | [UART_FCR] = 0x18, |
| 325 | [UART_LCR] = 0x1c, |
| 326 | [UART_MCR] = 0x20, |
| 327 | [UART_LSR] = 0x24, |
| 328 | [UART_MSR] = 0x28, |
| 329 | [UART_SCR] = 0x2c |
| 330 | }; |
| 331 | |
| 332 | static inline int map_8250_in_reg(struct uart_8250_port *up, int offset) |
| 333 | { |
| 334 | if (up->port.iotype != UPIO_RM9000) |
| 335 | return offset; |
| 336 | return regmap_in[offset]; |
| 337 | } |
| 338 | |
| 339 | static inline int map_8250_out_reg(struct uart_8250_port *up, int offset) |
| 340 | { |
| 341 | if (up->port.iotype != UPIO_RM9000) |
| 342 | return offset; |
| 343 | return regmap_out[offset]; |
| 344 | } |
| 345 | |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 346 | #else |
| 347 | |
| 348 | /* sane hardware needs no mapping */ |
| 349 | #define map_8250_in_reg(up, offset) (offset) |
| 350 | #define map_8250_out_reg(up, offset) (offset) |
| 351 | |
| 352 | #endif |
| 353 | |
Russell King | ea8874d | 2006-01-04 19:43:24 +0000 | [diff] [blame] | 354 | static unsigned int serial_in(struct uart_8250_port *up, int offset) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | { |
Zang Roy-r61911 | 3be91ec | 2006-06-30 02:29:58 -0700 | [diff] [blame] | 356 | unsigned int tmp; |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 357 | offset = map_8250_in_reg(up, offset) << up->port.regshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | |
| 359 | switch (up->port.iotype) { |
| 360 | case UPIO_HUB6: |
| 361 | outb(up->port.hub6 - 1 + offset, up->port.iobase); |
| 362 | return inb(up->port.iobase + 1); |
| 363 | |
| 364 | case UPIO_MEM: |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 365 | case UPIO_DWAPB: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | return readb(up->port.membase + offset); |
| 367 | |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 368 | case UPIO_RM9000: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | case UPIO_MEM32: |
| 370 | return readl(up->port.membase + offset); |
| 371 | |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 372 | #ifdef CONFIG_SERIAL_8250_AU1X00 |
| 373 | case UPIO_AU: |
| 374 | return __raw_readl(up->port.membase + offset); |
| 375 | #endif |
| 376 | |
Zang Roy-r61911 | 3be91ec | 2006-06-30 02:29:58 -0700 | [diff] [blame] | 377 | case UPIO_TSI: |
| 378 | if (offset == UART_IIR) { |
Al Viro | 9e84b60 | 2006-09-23 01:39:45 +0100 | [diff] [blame] | 379 | tmp = readl(up->port.membase + (UART_IIR & ~3)); |
| 380 | return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */ |
Zang Roy-r61911 | 3be91ec | 2006-06-30 02:29:58 -0700 | [diff] [blame] | 381 | } else |
| 382 | return readb(up->port.membase + offset); |
| 383 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | default: |
| 385 | return inb(up->port.iobase + offset); |
| 386 | } |
| 387 | } |
| 388 | |
Russell King | ea8874d | 2006-01-04 19:43:24 +0000 | [diff] [blame] | 389 | static void |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | serial_out(struct uart_8250_port *up, int offset, int value) |
| 391 | { |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 392 | /* Save the offset before it's remapped */ |
| 393 | int save_offset = offset; |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 394 | offset = map_8250_out_reg(up, offset) << up->port.regshift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | switch (up->port.iotype) { |
| 397 | case UPIO_HUB6: |
| 398 | outb(up->port.hub6 - 1 + offset, up->port.iobase); |
| 399 | outb(value, up->port.iobase + 1); |
| 400 | break; |
| 401 | |
| 402 | case UPIO_MEM: |
| 403 | writeb(value, up->port.membase + offset); |
| 404 | break; |
| 405 | |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 406 | case UPIO_RM9000: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | case UPIO_MEM32: |
| 408 | writel(value, up->port.membase + offset); |
| 409 | break; |
| 410 | |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 411 | #ifdef CONFIG_SERIAL_8250_AU1X00 |
| 412 | case UPIO_AU: |
| 413 | __raw_writel(value, up->port.membase + offset); |
| 414 | break; |
| 415 | #endif |
Zang Roy-r61911 | 3be91ec | 2006-06-30 02:29:58 -0700 | [diff] [blame] | 416 | case UPIO_TSI: |
| 417 | if (!((offset == UART_IER) && (value & UART_IER_UUE))) |
| 418 | writeb(value, up->port.membase + offset); |
| 419 | break; |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 420 | |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 421 | case UPIO_DWAPB: |
| 422 | /* Save the LCR value so it can be re-written when a |
| 423 | * Busy Detect interrupt occurs. */ |
| 424 | if (save_offset == UART_LCR) |
| 425 | up->lcr = value; |
| 426 | writeb(value, up->port.membase + offset); |
| 427 | /* Read the IER to ensure any interrupt is cleared before |
| 428 | * returning from ISR. */ |
| 429 | if (save_offset == UART_TX || save_offset == UART_IER) |
| 430 | value = serial_in(up, UART_IER); |
| 431 | break; |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | default: |
| 434 | outb(value, up->port.iobase + offset); |
| 435 | } |
| 436 | } |
| 437 | |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 438 | static void |
| 439 | serial_out_sync(struct uart_8250_port *up, int offset, int value) |
| 440 | { |
| 441 | switch (up->port.iotype) { |
| 442 | case UPIO_MEM: |
| 443 | case UPIO_MEM32: |
| 444 | #ifdef CONFIG_SERIAL_8250_AU1X00 |
| 445 | case UPIO_AU: |
| 446 | #endif |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 447 | case UPIO_DWAPB: |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 448 | serial_out(up, offset, value); |
| 449 | serial_in(up, UART_LCR); /* safe, no side-effects */ |
| 450 | break; |
| 451 | default: |
| 452 | serial_out(up, offset, value); |
| 453 | } |
| 454 | } |
| 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | /* |
| 457 | * We used to support using pause I/O for certain machines. We |
| 458 | * haven't supported this for a while, but just in case it's badly |
| 459 | * needed for certain old 386 machines, I've left these #define's |
| 460 | * in.... |
| 461 | */ |
| 462 | #define serial_inp(up, offset) serial_in(up, offset) |
| 463 | #define serial_outp(up, offset, value) serial_out(up, offset, value) |
| 464 | |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 465 | /* Uart divisor latch read */ |
| 466 | static inline int _serial_dl_read(struct uart_8250_port *up) |
| 467 | { |
| 468 | return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8; |
| 469 | } |
| 470 | |
| 471 | /* Uart divisor latch write */ |
| 472 | static inline void _serial_dl_write(struct uart_8250_port *up, int value) |
| 473 | { |
| 474 | serial_outp(up, UART_DLL, value & 0xff); |
| 475 | serial_outp(up, UART_DLM, value >> 8 & 0xff); |
| 476 | } |
| 477 | |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 478 | #if defined(CONFIG_SERIAL_8250_AU1X00) |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 479 | /* Au1x00 haven't got a standard divisor latch */ |
| 480 | static int serial_dl_read(struct uart_8250_port *up) |
| 481 | { |
| 482 | if (up->port.iotype == UPIO_AU) |
| 483 | return __raw_readl(up->port.membase + 0x28); |
| 484 | else |
| 485 | return _serial_dl_read(up); |
| 486 | } |
| 487 | |
| 488 | static void serial_dl_write(struct uart_8250_port *up, int value) |
| 489 | { |
| 490 | if (up->port.iotype == UPIO_AU) |
| 491 | __raw_writel(value, up->port.membase + 0x28); |
| 492 | else |
| 493 | _serial_dl_write(up, value); |
| 494 | } |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 495 | #elif defined(CONFIG_SERIAL_8250_RM9K) |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 496 | static int serial_dl_read(struct uart_8250_port *up) |
| 497 | { |
| 498 | return (up->port.iotype == UPIO_RM9000) ? |
| 499 | (((__raw_readl(up->port.membase + 0x10) << 8) | |
| 500 | (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) : |
| 501 | _serial_dl_read(up); |
| 502 | } |
| 503 | |
| 504 | static void serial_dl_write(struct uart_8250_port *up, int value) |
| 505 | { |
| 506 | if (up->port.iotype == UPIO_RM9000) { |
| 507 | __raw_writel(value, up->port.membase + 0x08); |
| 508 | __raw_writel(value >> 8, up->port.membase + 0x10); |
| 509 | } else { |
| 510 | _serial_dl_write(up, value); |
| 511 | } |
| 512 | } |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 513 | #else |
| 514 | #define serial_dl_read(up) _serial_dl_read(up) |
| 515 | #define serial_dl_write(up, value) _serial_dl_write(up, value) |
| 516 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | |
| 518 | /* |
| 519 | * For the 16C950 |
| 520 | */ |
| 521 | static void serial_icr_write(struct uart_8250_port *up, int offset, int value) |
| 522 | { |
| 523 | serial_out(up, UART_SCR, offset); |
| 524 | serial_out(up, UART_ICR, value); |
| 525 | } |
| 526 | |
| 527 | static unsigned int serial_icr_read(struct uart_8250_port *up, int offset) |
| 528 | { |
| 529 | unsigned int value; |
| 530 | |
| 531 | serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD); |
| 532 | serial_out(up, UART_SCR, offset); |
| 533 | value = serial_in(up, UART_ICR); |
| 534 | serial_icr_write(up, UART_ACR, up->acr); |
| 535 | |
| 536 | return value; |
| 537 | } |
| 538 | |
| 539 | /* |
| 540 | * FIFO support. |
| 541 | */ |
| 542 | static inline void serial8250_clear_fifos(struct uart_8250_port *p) |
| 543 | { |
| 544 | if (p->capabilities & UART_CAP_FIFO) { |
| 545 | serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO); |
| 546 | serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO | |
| 547 | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); |
| 548 | serial_outp(p, UART_FCR, 0); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * IER sleep support. UARTs which have EFRs need the "extended |
| 554 | * capability" bit enabled. Note that on XR16C850s, we need to |
| 555 | * reset LCR to write to IER. |
| 556 | */ |
| 557 | static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep) |
| 558 | { |
| 559 | if (p->capabilities & UART_CAP_SLEEP) { |
| 560 | if (p->capabilities & UART_CAP_EFR) { |
| 561 | serial_outp(p, UART_LCR, 0xBF); |
| 562 | serial_outp(p, UART_EFR, UART_EFR_ECB); |
| 563 | serial_outp(p, UART_LCR, 0); |
| 564 | } |
| 565 | serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0); |
| 566 | if (p->capabilities & UART_CAP_EFR) { |
| 567 | serial_outp(p, UART_LCR, 0xBF); |
| 568 | serial_outp(p, UART_EFR, 0); |
| 569 | serial_outp(p, UART_LCR, 0); |
| 570 | } |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | #ifdef CONFIG_SERIAL_8250_RSA |
| 575 | /* |
| 576 | * Attempts to turn on the RSA FIFO. Returns zero on failure. |
| 577 | * We set the port uart clock rate if we succeed. |
| 578 | */ |
| 579 | static int __enable_rsa(struct uart_8250_port *up) |
| 580 | { |
| 581 | unsigned char mode; |
| 582 | int result; |
| 583 | |
| 584 | mode = serial_inp(up, UART_RSA_MSR); |
| 585 | result = mode & UART_RSA_MSR_FIFO; |
| 586 | |
| 587 | if (!result) { |
| 588 | serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO); |
| 589 | mode = serial_inp(up, UART_RSA_MSR); |
| 590 | result = mode & UART_RSA_MSR_FIFO; |
| 591 | } |
| 592 | |
| 593 | if (result) |
| 594 | up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16; |
| 595 | |
| 596 | return result; |
| 597 | } |
| 598 | |
| 599 | static void enable_rsa(struct uart_8250_port *up) |
| 600 | { |
| 601 | if (up->port.type == PORT_RSA) { |
| 602 | if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) { |
| 603 | spin_lock_irq(&up->port.lock); |
| 604 | __enable_rsa(up); |
| 605 | spin_unlock_irq(&up->port.lock); |
| 606 | } |
| 607 | if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) |
| 608 | serial_outp(up, UART_RSA_FRR, 0); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | /* |
| 613 | * Attempts to turn off the RSA FIFO. Returns zero on failure. |
| 614 | * It is unknown why interrupts were disabled in here. However, |
| 615 | * the caller is expected to preserve this behaviour by grabbing |
| 616 | * the spinlock before calling this function. |
| 617 | */ |
| 618 | static void disable_rsa(struct uart_8250_port *up) |
| 619 | { |
| 620 | unsigned char mode; |
| 621 | int result; |
| 622 | |
| 623 | if (up->port.type == PORT_RSA && |
| 624 | up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) { |
| 625 | spin_lock_irq(&up->port.lock); |
| 626 | |
| 627 | mode = serial_inp(up, UART_RSA_MSR); |
| 628 | result = !(mode & UART_RSA_MSR_FIFO); |
| 629 | |
| 630 | if (!result) { |
| 631 | serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO); |
| 632 | mode = serial_inp(up, UART_RSA_MSR); |
| 633 | result = !(mode & UART_RSA_MSR_FIFO); |
| 634 | } |
| 635 | |
| 636 | if (result) |
| 637 | up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16; |
| 638 | spin_unlock_irq(&up->port.lock); |
| 639 | } |
| 640 | } |
| 641 | #endif /* CONFIG_SERIAL_8250_RSA */ |
| 642 | |
| 643 | /* |
| 644 | * This is a quickie test to see how big the FIFO is. |
| 645 | * It doesn't work at all the time, more's the pity. |
| 646 | */ |
| 647 | static int size_fifo(struct uart_8250_port *up) |
| 648 | { |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 649 | unsigned char old_fcr, old_mcr, old_lcr; |
| 650 | unsigned short old_dl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | int count; |
| 652 | |
| 653 | old_lcr = serial_inp(up, UART_LCR); |
| 654 | serial_outp(up, UART_LCR, 0); |
| 655 | old_fcr = serial_inp(up, UART_FCR); |
| 656 | old_mcr = serial_inp(up, UART_MCR); |
| 657 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | |
| 658 | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); |
| 659 | serial_outp(up, UART_MCR, UART_MCR_LOOP); |
| 660 | serial_outp(up, UART_LCR, UART_LCR_DLAB); |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 661 | old_dl = serial_dl_read(up); |
| 662 | serial_dl_write(up, 0x0001); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | serial_outp(up, UART_LCR, 0x03); |
| 664 | for (count = 0; count < 256; count++) |
| 665 | serial_outp(up, UART_TX, count); |
| 666 | mdelay(20);/* FIXME - schedule_timeout */ |
| 667 | for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) && |
| 668 | (count < 256); count++) |
| 669 | serial_inp(up, UART_RX); |
| 670 | serial_outp(up, UART_FCR, old_fcr); |
| 671 | serial_outp(up, UART_MCR, old_mcr); |
| 672 | serial_outp(up, UART_LCR, UART_LCR_DLAB); |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 673 | serial_dl_write(up, old_dl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | serial_outp(up, UART_LCR, old_lcr); |
| 675 | |
| 676 | return count; |
| 677 | } |
| 678 | |
| 679 | /* |
| 680 | * Read UART ID using the divisor method - set DLL and DLM to zero |
| 681 | * and the revision will be in DLL and device type in DLM. We |
| 682 | * preserve the device state across this. |
| 683 | */ |
| 684 | static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p) |
| 685 | { |
| 686 | unsigned char old_dll, old_dlm, old_lcr; |
| 687 | unsigned int id; |
| 688 | |
| 689 | old_lcr = serial_inp(p, UART_LCR); |
| 690 | serial_outp(p, UART_LCR, UART_LCR_DLAB); |
| 691 | |
| 692 | old_dll = serial_inp(p, UART_DLL); |
| 693 | old_dlm = serial_inp(p, UART_DLM); |
| 694 | |
| 695 | serial_outp(p, UART_DLL, 0); |
| 696 | serial_outp(p, UART_DLM, 0); |
| 697 | |
| 698 | id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8; |
| 699 | |
| 700 | serial_outp(p, UART_DLL, old_dll); |
| 701 | serial_outp(p, UART_DLM, old_dlm); |
| 702 | serial_outp(p, UART_LCR, old_lcr); |
| 703 | |
| 704 | return id; |
| 705 | } |
| 706 | |
| 707 | /* |
| 708 | * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's. |
| 709 | * When this function is called we know it is at least a StarTech |
| 710 | * 16650 V2, but it might be one of several StarTech UARTs, or one of |
| 711 | * its clones. (We treat the broken original StarTech 16650 V1 as a |
| 712 | * 16550, and why not? Startech doesn't seem to even acknowledge its |
| 713 | * existence.) |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 714 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | * What evil have men's minds wrought... |
| 716 | */ |
| 717 | static void autoconfig_has_efr(struct uart_8250_port *up) |
| 718 | { |
| 719 | unsigned int id1, id2, id3, rev; |
| 720 | |
| 721 | /* |
| 722 | * Everything with an EFR has SLEEP |
| 723 | */ |
| 724 | up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; |
| 725 | |
| 726 | /* |
| 727 | * First we check to see if it's an Oxford Semiconductor UART. |
| 728 | * |
| 729 | * If we have to do this here because some non-National |
| 730 | * Semiconductor clone chips lock up if you try writing to the |
| 731 | * LSR register (which serial_icr_read does) |
| 732 | */ |
| 733 | |
| 734 | /* |
| 735 | * Check for Oxford Semiconductor 16C950. |
| 736 | * |
| 737 | * EFR [4] must be set else this test fails. |
| 738 | * |
| 739 | * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca) |
| 740 | * claims that it's needed for 952 dual UART's (which are not |
| 741 | * recommended for new designs). |
| 742 | */ |
| 743 | up->acr = 0; |
| 744 | serial_out(up, UART_LCR, 0xBF); |
| 745 | serial_out(up, UART_EFR, UART_EFR_ECB); |
| 746 | serial_out(up, UART_LCR, 0x00); |
| 747 | id1 = serial_icr_read(up, UART_ID1); |
| 748 | id2 = serial_icr_read(up, UART_ID2); |
| 749 | id3 = serial_icr_read(up, UART_ID3); |
| 750 | rev = serial_icr_read(up, UART_REV); |
| 751 | |
| 752 | DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev); |
| 753 | |
| 754 | if (id1 == 0x16 && id2 == 0xC9 && |
| 755 | (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) { |
| 756 | up->port.type = PORT_16C950; |
Russell King | 4ba5e35 | 2005-06-23 10:43:04 +0100 | [diff] [blame] | 757 | |
| 758 | /* |
| 759 | * Enable work around for the Oxford Semiconductor 952 rev B |
| 760 | * chip which causes it to seriously miscalculate baud rates |
| 761 | * when DLL is 0. |
| 762 | */ |
| 763 | if (id3 == 0x52 && rev == 0x01) |
| 764 | up->bugs |= UART_BUG_QUOT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | return; |
| 766 | } |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 767 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | /* |
| 769 | * We check for a XR16C850 by setting DLL and DLM to 0, and then |
| 770 | * reading back DLL and DLM. The chip type depends on the DLM |
| 771 | * value read back: |
| 772 | * 0x10 - XR16C850 and the DLL contains the chip revision. |
| 773 | * 0x12 - XR16C2850. |
| 774 | * 0x14 - XR16C854. |
| 775 | */ |
| 776 | id1 = autoconfig_read_divisor_id(up); |
| 777 | DEBUG_AUTOCONF("850id=%04x ", id1); |
| 778 | |
| 779 | id2 = id1 >> 8; |
| 780 | if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | up->port.type = PORT_16850; |
| 782 | return; |
| 783 | } |
| 784 | |
| 785 | /* |
| 786 | * It wasn't an XR16C850. |
| 787 | * |
| 788 | * We distinguish between the '654 and the '650 by counting |
| 789 | * how many bytes are in the FIFO. I'm using this for now, |
| 790 | * since that's the technique that was sent to me in the |
| 791 | * serial driver update, but I'm not convinced this works. |
| 792 | * I've had problems doing this in the past. -TYT |
| 793 | */ |
| 794 | if (size_fifo(up) == 64) |
| 795 | up->port.type = PORT_16654; |
| 796 | else |
| 797 | up->port.type = PORT_16650V2; |
| 798 | } |
| 799 | |
| 800 | /* |
| 801 | * We detected a chip without a FIFO. Only two fall into |
| 802 | * this category - the original 8250 and the 16450. The |
| 803 | * 16450 has a scratch register (accessible with LCR=0) |
| 804 | */ |
| 805 | static void autoconfig_8250(struct uart_8250_port *up) |
| 806 | { |
| 807 | unsigned char scratch, status1, status2; |
| 808 | |
| 809 | up->port.type = PORT_8250; |
| 810 | |
| 811 | scratch = serial_in(up, UART_SCR); |
| 812 | serial_outp(up, UART_SCR, 0xa5); |
| 813 | status1 = serial_in(up, UART_SCR); |
| 814 | serial_outp(up, UART_SCR, 0x5a); |
| 815 | status2 = serial_in(up, UART_SCR); |
| 816 | serial_outp(up, UART_SCR, scratch); |
| 817 | |
| 818 | if (status1 == 0xa5 && status2 == 0x5a) |
| 819 | up->port.type = PORT_16450; |
| 820 | } |
| 821 | |
| 822 | static int broken_efr(struct uart_8250_port *up) |
| 823 | { |
| 824 | /* |
| 825 | * Exar ST16C2550 "A2" devices incorrectly detect as |
| 826 | * having an EFR, and report an ID of 0x0201. See |
| 827 | * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf |
| 828 | */ |
| 829 | if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16) |
| 830 | return 1; |
| 831 | |
| 832 | return 0; |
| 833 | } |
| 834 | |
| 835 | /* |
| 836 | * We know that the chip has FIFOs. Does it have an EFR? The |
| 837 | * EFR is located in the same register position as the IIR and |
| 838 | * we know the top two bits of the IIR are currently set. The |
| 839 | * EFR should contain zero. Try to read the EFR. |
| 840 | */ |
| 841 | static void autoconfig_16550a(struct uart_8250_port *up) |
| 842 | { |
| 843 | unsigned char status1, status2; |
| 844 | unsigned int iersave; |
| 845 | |
| 846 | up->port.type = PORT_16550A; |
| 847 | up->capabilities |= UART_CAP_FIFO; |
| 848 | |
| 849 | /* |
| 850 | * Check for presence of the EFR when DLAB is set. |
| 851 | * Only ST16C650V1 UARTs pass this test. |
| 852 | */ |
| 853 | serial_outp(up, UART_LCR, UART_LCR_DLAB); |
| 854 | if (serial_in(up, UART_EFR) == 0) { |
| 855 | serial_outp(up, UART_EFR, 0xA8); |
| 856 | if (serial_in(up, UART_EFR) != 0) { |
| 857 | DEBUG_AUTOCONF("EFRv1 "); |
| 858 | up->port.type = PORT_16650; |
| 859 | up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; |
| 860 | } else { |
| 861 | DEBUG_AUTOCONF("Motorola 8xxx DUART "); |
| 862 | } |
| 863 | serial_outp(up, UART_EFR, 0); |
| 864 | return; |
| 865 | } |
| 866 | |
| 867 | /* |
| 868 | * Maybe it requires 0xbf to be written to the LCR. |
| 869 | * (other ST16C650V2 UARTs, TI16C752A, etc) |
| 870 | */ |
| 871 | serial_outp(up, UART_LCR, 0xBF); |
| 872 | if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) { |
| 873 | DEBUG_AUTOCONF("EFRv2 "); |
| 874 | autoconfig_has_efr(up); |
| 875 | return; |
| 876 | } |
| 877 | |
| 878 | /* |
| 879 | * Check for a National Semiconductor SuperIO chip. |
| 880 | * Attempt to switch to bank 2, read the value of the LOOP bit |
| 881 | * from EXCR1. Switch back to bank 0, change it in MCR. Then |
| 882 | * switch back to bank 2, read it from EXCR1 again and check |
| 883 | * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 884 | */ |
| 885 | serial_outp(up, UART_LCR, 0); |
| 886 | status1 = serial_in(up, UART_MCR); |
| 887 | serial_outp(up, UART_LCR, 0xE0); |
| 888 | status2 = serial_in(up, 0x02); /* EXCR1 */ |
| 889 | |
| 890 | if (!((status2 ^ status1) & UART_MCR_LOOP)) { |
| 891 | serial_outp(up, UART_LCR, 0); |
| 892 | serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP); |
| 893 | serial_outp(up, UART_LCR, 0xE0); |
| 894 | status2 = serial_in(up, 0x02); /* EXCR1 */ |
| 895 | serial_outp(up, UART_LCR, 0); |
| 896 | serial_outp(up, UART_MCR, status1); |
| 897 | |
| 898 | if ((status2 ^ status1) & UART_MCR_LOOP) { |
David Woodhouse | 857dde2 | 2005-05-21 15:52:23 +0100 | [diff] [blame] | 899 | unsigned short quot; |
| 900 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 901 | serial_outp(up, UART_LCR, 0xE0); |
David Woodhouse | 857dde2 | 2005-05-21 15:52:23 +0100 | [diff] [blame] | 902 | |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 903 | quot = serial_dl_read(up); |
David Woodhouse | 857dde2 | 2005-05-21 15:52:23 +0100 | [diff] [blame] | 904 | quot <<= 3; |
| 905 | |
David Woodhouse | b5b82df | 2007-05-17 14:27:39 +0800 | [diff] [blame] | 906 | status1 = serial_in(up, 0x04); /* EXCR2 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */ |
| 908 | status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */ |
| 909 | serial_outp(up, 0x04, status1); |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 910 | |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 911 | serial_dl_write(up, quot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 912 | |
David Woodhouse | 857dde2 | 2005-05-21 15:52:23 +0100 | [diff] [blame] | 913 | serial_outp(up, UART_LCR, 0); |
| 914 | |
| 915 | up->port.uartclk = 921600*16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 916 | up->port.type = PORT_NS16550A; |
| 917 | up->capabilities |= UART_NATSEMI; |
| 918 | return; |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | /* |
| 923 | * No EFR. Try to detect a TI16750, which only sets bit 5 of |
| 924 | * the IIR when 64 byte FIFO mode is enabled when DLAB is set. |
| 925 | * Try setting it with and without DLAB set. Cheap clones |
| 926 | * set bit 5 without DLAB set. |
| 927 | */ |
| 928 | serial_outp(up, UART_LCR, 0); |
| 929 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); |
| 930 | status1 = serial_in(up, UART_IIR) >> 5; |
| 931 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); |
| 932 | serial_outp(up, UART_LCR, UART_LCR_DLAB); |
| 933 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); |
| 934 | status2 = serial_in(up, UART_IIR) >> 5; |
| 935 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); |
| 936 | serial_outp(up, UART_LCR, 0); |
| 937 | |
| 938 | DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2); |
| 939 | |
| 940 | if (status1 == 6 && status2 == 7) { |
| 941 | up->port.type = PORT_16750; |
| 942 | up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP; |
| 943 | return; |
| 944 | } |
| 945 | |
| 946 | /* |
| 947 | * Try writing and reading the UART_IER_UUE bit (b6). |
| 948 | * If it works, this is probably one of the Xscale platform's |
| 949 | * internal UARTs. |
| 950 | * We're going to explicitly set the UUE bit to 0 before |
| 951 | * trying to write and read a 1 just to make sure it's not |
| 952 | * already a 1 and maybe locked there before we even start start. |
| 953 | */ |
| 954 | iersave = serial_in(up, UART_IER); |
| 955 | serial_outp(up, UART_IER, iersave & ~UART_IER_UUE); |
| 956 | if (!(serial_in(up, UART_IER) & UART_IER_UUE)) { |
| 957 | /* |
| 958 | * OK it's in a known zero state, try writing and reading |
| 959 | * without disturbing the current state of the other bits. |
| 960 | */ |
| 961 | serial_outp(up, UART_IER, iersave | UART_IER_UUE); |
| 962 | if (serial_in(up, UART_IER) & UART_IER_UUE) { |
| 963 | /* |
| 964 | * It's an Xscale. |
| 965 | * We'll leave the UART_IER_UUE bit set to 1 (enabled). |
| 966 | */ |
| 967 | DEBUG_AUTOCONF("Xscale "); |
| 968 | up->port.type = PORT_XSCALE; |
| 969 | up->capabilities |= UART_CAP_UUE; |
| 970 | return; |
| 971 | } |
| 972 | } else { |
| 973 | /* |
| 974 | * If we got here we couldn't force the IER_UUE bit to 0. |
| 975 | * Log it and continue. |
| 976 | */ |
| 977 | DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 "); |
| 978 | } |
| 979 | serial_outp(up, UART_IER, iersave); |
| 980 | } |
| 981 | |
| 982 | /* |
| 983 | * This routine is called by rs_init() to initialize a specific serial |
| 984 | * port. It determines what type of UART chip this serial port is |
| 985 | * using: 8250, 16450, 16550, 16550A. The important question is |
| 986 | * whether or not this UART is a 16550A or not, since this will |
| 987 | * determine whether or not we can use its FIFO features or not. |
| 988 | */ |
| 989 | static void autoconfig(struct uart_8250_port *up, unsigned int probeflags) |
| 990 | { |
| 991 | unsigned char status1, scratch, scratch2, scratch3; |
| 992 | unsigned char save_lcr, save_mcr; |
| 993 | unsigned long flags; |
| 994 | |
| 995 | if (!up->port.iobase && !up->port.mapbase && !up->port.membase) |
| 996 | return; |
| 997 | |
| 998 | DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ", |
| 999 | up->port.line, up->port.iobase, up->port.membase); |
| 1000 | |
| 1001 | /* |
| 1002 | * We really do need global IRQs disabled here - we're going to |
| 1003 | * be frobbing the chips IRQ enable register to see if it exists. |
| 1004 | */ |
| 1005 | spin_lock_irqsave(&up->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | |
| 1007 | up->capabilities = 0; |
Russell King | 4ba5e35 | 2005-06-23 10:43:04 +0100 | [diff] [blame] | 1008 | up->bugs = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | |
| 1010 | if (!(up->port.flags & UPF_BUGGY_UART)) { |
| 1011 | /* |
| 1012 | * Do a simple existence test first; if we fail this, |
| 1013 | * there's no point trying anything else. |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 1014 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1015 | * 0x80 is used as a nonsense port to prevent against |
| 1016 | * false positives due to ISA bus float. The |
| 1017 | * assumption is that 0x80 is a non-existent port; |
| 1018 | * which should be safe since include/asm/io.h also |
| 1019 | * makes this assumption. |
| 1020 | * |
| 1021 | * Note: this is safe as long as MCR bit 4 is clear |
| 1022 | * and the device is in "PC" mode. |
| 1023 | */ |
| 1024 | scratch = serial_inp(up, UART_IER); |
| 1025 | serial_outp(up, UART_IER, 0); |
| 1026 | #ifdef __i386__ |
| 1027 | outb(0xff, 0x080); |
| 1028 | #endif |
Thomas Hoehn | 4821200 | 2007-02-10 01:46:05 -0800 | [diff] [blame] | 1029 | /* |
| 1030 | * Mask out IER[7:4] bits for test as some UARTs (e.g. TL |
| 1031 | * 16C754B) allow only to modify them if an EFR bit is set. |
| 1032 | */ |
| 1033 | scratch2 = serial_inp(up, UART_IER) & 0x0f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | serial_outp(up, UART_IER, 0x0F); |
| 1035 | #ifdef __i386__ |
| 1036 | outb(0, 0x080); |
| 1037 | #endif |
Thomas Hoehn | 4821200 | 2007-02-10 01:46:05 -0800 | [diff] [blame] | 1038 | scratch3 = serial_inp(up, UART_IER) & 0x0f; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | serial_outp(up, UART_IER, scratch); |
| 1040 | if (scratch2 != 0 || scratch3 != 0x0F) { |
| 1041 | /* |
| 1042 | * We failed; there's nothing here |
| 1043 | */ |
| 1044 | DEBUG_AUTOCONF("IER test failed (%02x, %02x) ", |
| 1045 | scratch2, scratch3); |
| 1046 | goto out; |
| 1047 | } |
| 1048 | } |
| 1049 | |
| 1050 | save_mcr = serial_in(up, UART_MCR); |
| 1051 | save_lcr = serial_in(up, UART_LCR); |
| 1052 | |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 1053 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | * Check to see if a UART is really there. Certain broken |
| 1055 | * internal modems based on the Rockwell chipset fail this |
| 1056 | * test, because they apparently don't implement the loopback |
| 1057 | * test mode. So this test is skipped on the COM 1 through |
| 1058 | * COM 4 ports. This *should* be safe, since no board |
| 1059 | * manufacturer would be stupid enough to design a board |
| 1060 | * that conflicts with COM 1-4 --- we hope! |
| 1061 | */ |
| 1062 | if (!(up->port.flags & UPF_SKIP_TEST)) { |
| 1063 | serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A); |
| 1064 | status1 = serial_inp(up, UART_MSR) & 0xF0; |
| 1065 | serial_outp(up, UART_MCR, save_mcr); |
| 1066 | if (status1 != 0x90) { |
| 1067 | DEBUG_AUTOCONF("LOOP test failed (%02x) ", |
| 1068 | status1); |
| 1069 | goto out; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | /* |
| 1074 | * We're pretty sure there's a port here. Lets find out what |
| 1075 | * type of port it is. The IIR top two bits allows us to find |
Russell King | 6f0d618 | 2005-09-09 16:17:58 +0100 | [diff] [blame] | 1076 | * out if it's 8250 or 16450, 16550, 16550A or later. This |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1077 | * determines what we test for next. |
| 1078 | * |
| 1079 | * We also initialise the EFR (if any) to zero for later. The |
| 1080 | * EFR occupies the same register location as the FCR and IIR. |
| 1081 | */ |
| 1082 | serial_outp(up, UART_LCR, 0xBF); |
| 1083 | serial_outp(up, UART_EFR, 0); |
| 1084 | serial_outp(up, UART_LCR, 0); |
| 1085 | |
| 1086 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); |
| 1087 | scratch = serial_in(up, UART_IIR) >> 6; |
| 1088 | |
| 1089 | DEBUG_AUTOCONF("iir=%d ", scratch); |
| 1090 | |
| 1091 | switch (scratch) { |
| 1092 | case 0: |
| 1093 | autoconfig_8250(up); |
| 1094 | break; |
| 1095 | case 1: |
| 1096 | up->port.type = PORT_UNKNOWN; |
| 1097 | break; |
| 1098 | case 2: |
| 1099 | up->port.type = PORT_16550; |
| 1100 | break; |
| 1101 | case 3: |
| 1102 | autoconfig_16550a(up); |
| 1103 | break; |
| 1104 | } |
| 1105 | |
| 1106 | #ifdef CONFIG_SERIAL_8250_RSA |
| 1107 | /* |
| 1108 | * Only probe for RSA ports if we got the region. |
| 1109 | */ |
| 1110 | if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) { |
| 1111 | int i; |
| 1112 | |
| 1113 | for (i = 0 ; i < probe_rsa_count; ++i) { |
| 1114 | if (probe_rsa[i] == up->port.iobase && |
| 1115 | __enable_rsa(up)) { |
| 1116 | up->port.type = PORT_RSA; |
| 1117 | break; |
| 1118 | } |
| 1119 | } |
| 1120 | } |
| 1121 | #endif |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 1122 | |
| 1123 | #ifdef CONFIG_SERIAL_8250_AU1X00 |
| 1124 | /* if access method is AU, it is a 16550 with a quirk */ |
| 1125 | if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU) |
| 1126 | up->bugs |= UART_BUG_NOMSR; |
| 1127 | #endif |
| 1128 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1129 | serial_outp(up, UART_LCR, save_lcr); |
| 1130 | |
| 1131 | if (up->capabilities != uart_config[up->port.type].flags) { |
| 1132 | printk(KERN_WARNING |
| 1133 | "ttyS%d: detected caps %08x should be %08x\n", |
| 1134 | up->port.line, up->capabilities, |
| 1135 | uart_config[up->port.type].flags); |
| 1136 | } |
| 1137 | |
| 1138 | up->port.fifosize = uart_config[up->port.type].fifo_size; |
| 1139 | up->capabilities = uart_config[up->port.type].flags; |
| 1140 | up->tx_loadsz = uart_config[up->port.type].tx_loadsz; |
| 1141 | |
| 1142 | if (up->port.type == PORT_UNKNOWN) |
| 1143 | goto out; |
| 1144 | |
| 1145 | /* |
| 1146 | * Reset the UART. |
| 1147 | */ |
| 1148 | #ifdef CONFIG_SERIAL_8250_RSA |
| 1149 | if (up->port.type == PORT_RSA) |
| 1150 | serial_outp(up, UART_RSA_FRR, 0); |
| 1151 | #endif |
| 1152 | serial_outp(up, UART_MCR, save_mcr); |
| 1153 | serial8250_clear_fifos(up); |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1154 | serial_in(up, UART_RX); |
Lennert Buytenhek | 5c8c755 | 2005-11-12 21:58:05 +0000 | [diff] [blame] | 1155 | if (up->capabilities & UART_CAP_UUE) |
| 1156 | serial_outp(up, UART_IER, UART_IER_UUE); |
| 1157 | else |
| 1158 | serial_outp(up, UART_IER, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 1160 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | spin_unlock_irqrestore(&up->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name); |
| 1163 | } |
| 1164 | |
| 1165 | static void autoconfig_irq(struct uart_8250_port *up) |
| 1166 | { |
| 1167 | unsigned char save_mcr, save_ier; |
| 1168 | unsigned char save_ICP = 0; |
| 1169 | unsigned int ICP = 0; |
| 1170 | unsigned long irqs; |
| 1171 | int irq; |
| 1172 | |
| 1173 | if (up->port.flags & UPF_FOURPORT) { |
| 1174 | ICP = (up->port.iobase & 0xfe0) | 0x1f; |
| 1175 | save_ICP = inb_p(ICP); |
| 1176 | outb_p(0x80, ICP); |
| 1177 | (void) inb_p(ICP); |
| 1178 | } |
| 1179 | |
| 1180 | /* forget possible initially masked and pending IRQ */ |
| 1181 | probe_irq_off(probe_irq_on()); |
| 1182 | save_mcr = serial_inp(up, UART_MCR); |
| 1183 | save_ier = serial_inp(up, UART_IER); |
| 1184 | serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2); |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 1185 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | irqs = probe_irq_on(); |
| 1187 | serial_outp(up, UART_MCR, 0); |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 1188 | udelay(10); |
| 1189 | if (up->port.flags & UPF_FOURPORT) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | serial_outp(up, UART_MCR, |
| 1191 | UART_MCR_DTR | UART_MCR_RTS); |
| 1192 | } else { |
| 1193 | serial_outp(up, UART_MCR, |
| 1194 | UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2); |
| 1195 | } |
| 1196 | serial_outp(up, UART_IER, 0x0f); /* enable all intrs */ |
| 1197 | (void)serial_inp(up, UART_LSR); |
| 1198 | (void)serial_inp(up, UART_RX); |
| 1199 | (void)serial_inp(up, UART_IIR); |
| 1200 | (void)serial_inp(up, UART_MSR); |
| 1201 | serial_outp(up, UART_TX, 0xFF); |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 1202 | udelay(20); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1203 | irq = probe_irq_off(irqs); |
| 1204 | |
| 1205 | serial_outp(up, UART_MCR, save_mcr); |
| 1206 | serial_outp(up, UART_IER, save_ier); |
| 1207 | |
| 1208 | if (up->port.flags & UPF_FOURPORT) |
| 1209 | outb_p(save_ICP, ICP); |
| 1210 | |
| 1211 | up->port.irq = (irq > 0) ? irq : 0; |
| 1212 | } |
| 1213 | |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1214 | static inline void __stop_tx(struct uart_8250_port *p) |
| 1215 | { |
| 1216 | if (p->ier & UART_IER_THRI) { |
| 1217 | p->ier &= ~UART_IER_THRI; |
| 1218 | serial_out(p, UART_IER, p->ier); |
| 1219 | } |
| 1220 | } |
| 1221 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1222 | static void serial8250_stop_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1223 | { |
| 1224 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1225 | |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1226 | __stop_tx(up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | |
| 1228 | /* |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1229 | * We really want to stop the transmitter from sending. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | */ |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1231 | if (up->port.type == PORT_16C950) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1232 | up->acr |= UART_ACR_TXDIS; |
| 1233 | serial_icr_write(up, UART_ACR, up->acr); |
| 1234 | } |
| 1235 | } |
| 1236 | |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1237 | static void transmit_chars(struct uart_8250_port *up); |
| 1238 | |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1239 | static void serial8250_start_tx(struct uart_port *port) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | { |
| 1241 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1242 | |
| 1243 | if (!(up->ier & UART_IER_THRI)) { |
| 1244 | up->ier |= UART_IER_THRI; |
| 1245 | serial_out(up, UART_IER, up->ier); |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1246 | |
Russell King | 67f7654 | 2005-06-23 22:26:43 +0100 | [diff] [blame] | 1247 | if (up->bugs & UART_BUG_TXEN) { |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1248 | unsigned char lsr, iir; |
| 1249 | lsr = serial_in(up, UART_LSR); |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1250 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 1251 | iir = serial_in(up, UART_IIR) & 0x0f; |
| 1252 | if ((up->port.type == PORT_RM9000) ? |
| 1253 | (lsr & UART_LSR_THRE && |
| 1254 | (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) : |
| 1255 | (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)) |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1256 | transmit_chars(up); |
| 1257 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1259 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | /* |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1261 | * Re-enable the transmitter if we disabled it. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1262 | */ |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1263 | if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1264 | up->acr &= ~UART_ACR_TXDIS; |
| 1265 | serial_icr_write(up, UART_ACR, up->acr); |
| 1266 | } |
| 1267 | } |
| 1268 | |
| 1269 | static void serial8250_stop_rx(struct uart_port *port) |
| 1270 | { |
| 1271 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1272 | |
| 1273 | up->ier &= ~UART_IER_RLSI; |
| 1274 | up->port.read_status_mask &= ~UART_LSR_DR; |
| 1275 | serial_out(up, UART_IER, up->ier); |
| 1276 | } |
| 1277 | |
| 1278 | static void serial8250_enable_ms(struct uart_port *port) |
| 1279 | { |
| 1280 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1281 | |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 1282 | /* no MSR capabilities */ |
| 1283 | if (up->bugs & UART_BUG_NOMSR) |
| 1284 | return; |
| 1285 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | up->ier |= UART_IER_MSI; |
| 1287 | serial_out(up, UART_IER, up->ier); |
| 1288 | } |
| 1289 | |
Russell King | ea8874d | 2006-01-04 19:43:24 +0000 | [diff] [blame] | 1290 | static void |
Thomas Koeller | cc79aa9 | 2007-02-20 13:58:05 -0800 | [diff] [blame] | 1291 | receive_chars(struct uart_8250_port *up, unsigned int *status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | { |
| 1293 | struct tty_struct *tty = up->port.info->tty; |
| 1294 | unsigned char ch, lsr = *status; |
| 1295 | int max_count = 256; |
| 1296 | char flag; |
| 1297 | |
| 1298 | do { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | ch = serial_inp(up, UART_RX); |
| 1300 | flag = TTY_NORMAL; |
| 1301 | up->port.icount.rx++; |
| 1302 | |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1303 | lsr |= up->lsr_saved_flags; |
| 1304 | up->lsr_saved_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1305 | |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1306 | if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | /* |
| 1308 | * For statistics only |
| 1309 | */ |
| 1310 | if (lsr & UART_LSR_BI) { |
| 1311 | lsr &= ~(UART_LSR_FE | UART_LSR_PE); |
| 1312 | up->port.icount.brk++; |
| 1313 | /* |
| 1314 | * We do the SysRQ and SAK checking |
| 1315 | * here because otherwise the break |
| 1316 | * may get masked by ignore_status_mask |
| 1317 | * or read_status_mask. |
| 1318 | */ |
| 1319 | if (uart_handle_break(&up->port)) |
| 1320 | goto ignore_char; |
| 1321 | } else if (lsr & UART_LSR_PE) |
| 1322 | up->port.icount.parity++; |
| 1323 | else if (lsr & UART_LSR_FE) |
| 1324 | up->port.icount.frame++; |
| 1325 | if (lsr & UART_LSR_OE) |
| 1326 | up->port.icount.overrun++; |
| 1327 | |
| 1328 | /* |
Russell King | 23907eb | 2005-04-16 15:26:39 -0700 | [diff] [blame] | 1329 | * Mask off conditions which should be ignored. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | */ |
| 1331 | lsr &= up->port.read_status_mask; |
| 1332 | |
| 1333 | if (lsr & UART_LSR_BI) { |
| 1334 | DEBUG_INTR("handling break...."); |
| 1335 | flag = TTY_BREAK; |
| 1336 | } else if (lsr & UART_LSR_PE) |
| 1337 | flag = TTY_PARITY; |
| 1338 | else if (lsr & UART_LSR_FE) |
| 1339 | flag = TTY_FRAME; |
| 1340 | } |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1341 | if (uart_handle_sysrq_char(&up->port, ch)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1342 | goto ignore_char; |
Russell King | 05ab301 | 2005-05-09 23:21:59 +0100 | [diff] [blame] | 1343 | |
| 1344 | uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag); |
| 1345 | |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 1346 | ignore_char: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | lsr = serial_inp(up, UART_LSR); |
| 1348 | } while ((lsr & UART_LSR_DR) && (max_count-- > 0)); |
| 1349 | spin_unlock(&up->port.lock); |
| 1350 | tty_flip_buffer_push(tty); |
| 1351 | spin_lock(&up->port.lock); |
| 1352 | *status = lsr; |
| 1353 | } |
| 1354 | |
Russell King | ea8874d | 2006-01-04 19:43:24 +0000 | [diff] [blame] | 1355 | static void transmit_chars(struct uart_8250_port *up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | { |
| 1357 | struct circ_buf *xmit = &up->port.info->xmit; |
| 1358 | int count; |
| 1359 | |
| 1360 | if (up->port.x_char) { |
| 1361 | serial_outp(up, UART_TX, up->port.x_char); |
| 1362 | up->port.icount.tx++; |
| 1363 | up->port.x_char = 0; |
| 1364 | return; |
| 1365 | } |
Russell King | b129a8c | 2005-08-31 10:12:14 +0100 | [diff] [blame] | 1366 | if (uart_tx_stopped(&up->port)) { |
| 1367 | serial8250_stop_tx(&up->port); |
| 1368 | return; |
| 1369 | } |
| 1370 | if (uart_circ_empty(xmit)) { |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1371 | __stop_tx(up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | return; |
| 1373 | } |
| 1374 | |
| 1375 | count = up->tx_loadsz; |
| 1376 | do { |
| 1377 | serial_out(up, UART_TX, xmit->buf[xmit->tail]); |
| 1378 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); |
| 1379 | up->port.icount.tx++; |
| 1380 | if (uart_circ_empty(xmit)) |
| 1381 | break; |
| 1382 | } while (--count > 0); |
| 1383 | |
| 1384 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) |
| 1385 | uart_write_wakeup(&up->port); |
| 1386 | |
| 1387 | DEBUG_INTR("THRE..."); |
| 1388 | |
| 1389 | if (uart_circ_empty(xmit)) |
Russell King | e763b90 | 2005-06-29 18:41:51 +0100 | [diff] [blame] | 1390 | __stop_tx(up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1393 | static unsigned int check_modem_status(struct uart_8250_port *up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1394 | { |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1395 | unsigned int status = serial_in(up, UART_MSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1396 | |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1397 | status |= up->msr_saved_flags; |
| 1398 | up->msr_saved_flags = 0; |
Taku Izumi | fdc30b3 | 2007-04-23 14:41:00 -0700 | [diff] [blame] | 1399 | if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI && |
| 1400 | up->port.info != NULL) { |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1401 | if (status & UART_MSR_TERI) |
| 1402 | up->port.icount.rng++; |
| 1403 | if (status & UART_MSR_DDSR) |
| 1404 | up->port.icount.dsr++; |
| 1405 | if (status & UART_MSR_DDCD) |
| 1406 | uart_handle_dcd_change(&up->port, status & UART_MSR_DCD); |
| 1407 | if (status & UART_MSR_DCTS) |
| 1408 | uart_handle_cts_change(&up->port, status & UART_MSR_CTS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1410 | wake_up_interruptible(&up->port.info->delta_msr_wait); |
| 1411 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1413 | return status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1414 | } |
| 1415 | |
| 1416 | /* |
| 1417 | * This handles the interrupt from one port. |
| 1418 | */ |
| 1419 | static inline void |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1420 | serial8250_handle_port(struct uart_8250_port *up) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1421 | { |
Russell King | 45e2460 | 2006-01-04 19:19:06 +0000 | [diff] [blame] | 1422 | unsigned int status; |
Jiri Kosina | 4bf3631 | 2007-04-23 14:41:21 -0700 | [diff] [blame] | 1423 | unsigned long flags; |
Russell King | 45e2460 | 2006-01-04 19:19:06 +0000 | [diff] [blame] | 1424 | |
Jiri Kosina | 4bf3631 | 2007-04-23 14:41:21 -0700 | [diff] [blame] | 1425 | spin_lock_irqsave(&up->port.lock, flags); |
Russell King | 45e2460 | 2006-01-04 19:19:06 +0000 | [diff] [blame] | 1426 | |
| 1427 | status = serial_inp(up, UART_LSR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1428 | |
| 1429 | DEBUG_INTR("status = %x...", status); |
| 1430 | |
| 1431 | if (status & UART_LSR_DR) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1432 | receive_chars(up, &status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1433 | check_modem_status(up); |
| 1434 | if (status & UART_LSR_THRE) |
| 1435 | transmit_chars(up); |
Russell King | 45e2460 | 2006-01-04 19:19:06 +0000 | [diff] [blame] | 1436 | |
Jiri Kosina | 4bf3631 | 2007-04-23 14:41:21 -0700 | [diff] [blame] | 1437 | spin_unlock_irqrestore(&up->port.lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* |
| 1441 | * This is the serial driver's interrupt routine. |
| 1442 | * |
| 1443 | * Arjan thinks the old way was overly complex, so it got simplified. |
| 1444 | * Alan disagrees, saying that need the complexity to handle the weird |
| 1445 | * nature of ISA shared interrupts. (This is a special exception.) |
| 1446 | * |
| 1447 | * In order to handle ISA shared interrupts properly, we need to check |
| 1448 | * that all ports have been serviced, and therefore the ISA interrupt |
| 1449 | * line has been de-asserted. |
| 1450 | * |
| 1451 | * This means we need to loop through all ports. checking that they |
| 1452 | * don't have an interrupt pending. |
| 1453 | */ |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1454 | static irqreturn_t serial8250_interrupt(int irq, void *dev_id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | { |
| 1456 | struct irq_info *i = dev_id; |
| 1457 | struct list_head *l, *end = NULL; |
| 1458 | int pass_counter = 0, handled = 0; |
| 1459 | |
| 1460 | DEBUG_INTR("serial8250_interrupt(%d)...", irq); |
| 1461 | |
| 1462 | spin_lock(&i->lock); |
| 1463 | |
| 1464 | l = i->head; |
| 1465 | do { |
| 1466 | struct uart_8250_port *up; |
| 1467 | unsigned int iir; |
| 1468 | |
| 1469 | up = list_entry(l, struct uart_8250_port, list); |
| 1470 | |
| 1471 | iir = serial_in(up, UART_IIR); |
| 1472 | if (!(iir & UART_IIR_NO_INT)) { |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1473 | serial8250_handle_port(up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1474 | |
| 1475 | handled = 1; |
| 1476 | |
| 1477 | end = NULL; |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 1478 | } else if (up->port.iotype == UPIO_DWAPB && |
| 1479 | (iir & UART_IIR_BUSY) == UART_IIR_BUSY) { |
| 1480 | /* The DesignWare APB UART has an Busy Detect (0x07) |
| 1481 | * interrupt meaning an LCR write attempt occured while the |
| 1482 | * UART was busy. The interrupt must be cleared by reading |
| 1483 | * the UART status register (USR) and the LCR re-written. */ |
| 1484 | unsigned int status; |
| 1485 | status = *(volatile u32 *)up->port.private_data; |
| 1486 | serial_out(up, UART_LCR, up->lcr); |
| 1487 | |
| 1488 | handled = 1; |
| 1489 | |
| 1490 | end = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | } else if (end == NULL) |
| 1492 | end = l; |
| 1493 | |
| 1494 | l = l->next; |
| 1495 | |
| 1496 | if (l == i->head && pass_counter++ > PASS_LIMIT) { |
| 1497 | /* If we hit this, we're dead. */ |
| 1498 | printk(KERN_ERR "serial8250: too much work for " |
| 1499 | "irq%d\n", irq); |
| 1500 | break; |
| 1501 | } |
| 1502 | } while (l != end); |
| 1503 | |
| 1504 | spin_unlock(&i->lock); |
| 1505 | |
| 1506 | DEBUG_INTR("end.\n"); |
| 1507 | |
| 1508 | return IRQ_RETVAL(handled); |
| 1509 | } |
| 1510 | |
| 1511 | /* |
| 1512 | * To support ISA shared interrupts, we need to have one interrupt |
| 1513 | * handler that ensures that the IRQ line has been deasserted |
| 1514 | * before returning. Failing to do this will result in the IRQ |
| 1515 | * line being stuck active, and, since ISA irqs are edge triggered, |
| 1516 | * no more IRQs will be seen. |
| 1517 | */ |
| 1518 | static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up) |
| 1519 | { |
| 1520 | spin_lock_irq(&i->lock); |
| 1521 | |
| 1522 | if (!list_empty(i->head)) { |
| 1523 | if (i->head == &up->list) |
| 1524 | i->head = i->head->next; |
| 1525 | list_del(&up->list); |
| 1526 | } else { |
| 1527 | BUG_ON(i->head != &up->list); |
| 1528 | i->head = NULL; |
| 1529 | } |
| 1530 | |
| 1531 | spin_unlock_irq(&i->lock); |
| 1532 | } |
| 1533 | |
| 1534 | static int serial_link_irq_chain(struct uart_8250_port *up) |
| 1535 | { |
| 1536 | struct irq_info *i = irq_lists + up->port.irq; |
Thomas Gleixner | 40663cc | 2006-07-01 19:29:43 -0700 | [diff] [blame] | 1537 | int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | |
| 1539 | spin_lock_irq(&i->lock); |
| 1540 | |
| 1541 | if (i->head) { |
| 1542 | list_add(&up->list, i->head); |
| 1543 | spin_unlock_irq(&i->lock); |
| 1544 | |
| 1545 | ret = 0; |
| 1546 | } else { |
| 1547 | INIT_LIST_HEAD(&up->list); |
| 1548 | i->head = &up->list; |
| 1549 | spin_unlock_irq(&i->lock); |
| 1550 | |
| 1551 | ret = request_irq(up->port.irq, serial8250_interrupt, |
| 1552 | irq_flags, "serial", i); |
| 1553 | if (ret < 0) |
| 1554 | serial_do_unlink(i, up); |
| 1555 | } |
| 1556 | |
| 1557 | return ret; |
| 1558 | } |
| 1559 | |
| 1560 | static void serial_unlink_irq_chain(struct uart_8250_port *up) |
| 1561 | { |
| 1562 | struct irq_info *i = irq_lists + up->port.irq; |
| 1563 | |
| 1564 | BUG_ON(i->head == NULL); |
| 1565 | |
| 1566 | if (list_empty(i->head)) |
| 1567 | free_irq(up->port.irq, i); |
| 1568 | |
| 1569 | serial_do_unlink(i, up); |
| 1570 | } |
| 1571 | |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1572 | /* Base timer interval for polling */ |
| 1573 | static inline int poll_timeout(int timeout) |
| 1574 | { |
| 1575 | return timeout > 6 ? (timeout / 2 - 2) : 1; |
| 1576 | } |
| 1577 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1578 | /* |
| 1579 | * This function is used to handle ports that do not have an |
| 1580 | * interrupt. This doesn't work very well for 16450's, but gives |
| 1581 | * barely passable results for a 16550A. (Although at the expense |
| 1582 | * of much CPU overhead). |
| 1583 | */ |
| 1584 | static void serial8250_timeout(unsigned long data) |
| 1585 | { |
| 1586 | struct uart_8250_port *up = (struct uart_8250_port *)data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1587 | unsigned int iir; |
| 1588 | |
| 1589 | iir = serial_in(up, UART_IIR); |
Russell King | 45e2460 | 2006-01-04 19:19:06 +0000 | [diff] [blame] | 1590 | if (!(iir & UART_IIR_NO_INT)) |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 1591 | serial8250_handle_port(up); |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1592 | mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout)); |
| 1593 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1594 | |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1595 | static void serial8250_backup_timeout(unsigned long data) |
| 1596 | { |
| 1597 | struct uart_8250_port *up = (struct uart_8250_port *)data; |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1598 | unsigned int iir, ier = 0, lsr; |
| 1599 | unsigned long flags; |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1600 | |
| 1601 | /* |
| 1602 | * Must disable interrupts or else we risk racing with the interrupt |
| 1603 | * based handler. |
| 1604 | */ |
| 1605 | if (is_real_interrupt(up->port.irq)) { |
| 1606 | ier = serial_in(up, UART_IER); |
| 1607 | serial_out(up, UART_IER, 0); |
| 1608 | } |
| 1609 | |
| 1610 | iir = serial_in(up, UART_IIR); |
| 1611 | |
| 1612 | /* |
| 1613 | * This should be a safe test for anyone who doesn't trust the |
| 1614 | * IIR bits on their UART, but it's specifically designed for |
| 1615 | * the "Diva" UART used on the management processor on many HP |
| 1616 | * ia64 and parisc boxes. |
| 1617 | */ |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1618 | spin_lock_irqsave(&up->port.lock, flags); |
| 1619 | lsr = serial_in(up, UART_LSR); |
| 1620 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; |
| 1621 | spin_unlock_irqrestore(&up->port.lock, flags); |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1622 | if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) && |
| 1623 | (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) && |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1624 | (lsr & UART_LSR_THRE)) { |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1625 | iir &= ~(UART_IIR_ID | UART_IIR_NO_INT); |
| 1626 | iir |= UART_IIR_THRI; |
| 1627 | } |
| 1628 | |
| 1629 | if (!(iir & UART_IIR_NO_INT)) |
| 1630 | serial8250_handle_port(up); |
| 1631 | |
| 1632 | if (is_real_interrupt(up->port.irq)) |
| 1633 | serial_out(up, UART_IER, ier); |
| 1634 | |
| 1635 | /* Standard timer interval plus 0.2s to keep the port running */ |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 1636 | mod_timer(&up->timer, |
| 1637 | jiffies + poll_timeout(up->port.timeout) + HZ / 5); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1638 | } |
| 1639 | |
| 1640 | static unsigned int serial8250_tx_empty(struct uart_port *port) |
| 1641 | { |
| 1642 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1643 | unsigned long flags; |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1644 | unsigned int lsr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | |
| 1646 | spin_lock_irqsave(&up->port.lock, flags); |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1647 | lsr = serial_in(up, UART_LSR); |
| 1648 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1649 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 1650 | |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1651 | return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | static unsigned int serial8250_get_mctrl(struct uart_port *port) |
| 1655 | { |
| 1656 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1657 | unsigned int status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1658 | unsigned int ret; |
| 1659 | |
Russell King | 2af7cd6 | 2006-01-04 16:55:09 +0000 | [diff] [blame] | 1660 | status = check_modem_status(up); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1661 | |
| 1662 | ret = 0; |
| 1663 | if (status & UART_MSR_DCD) |
| 1664 | ret |= TIOCM_CAR; |
| 1665 | if (status & UART_MSR_RI) |
| 1666 | ret |= TIOCM_RNG; |
| 1667 | if (status & UART_MSR_DSR) |
| 1668 | ret |= TIOCM_DSR; |
| 1669 | if (status & UART_MSR_CTS) |
| 1670 | ret |= TIOCM_CTS; |
| 1671 | return ret; |
| 1672 | } |
| 1673 | |
| 1674 | static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl) |
| 1675 | { |
| 1676 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1677 | unsigned char mcr = 0; |
| 1678 | |
| 1679 | if (mctrl & TIOCM_RTS) |
| 1680 | mcr |= UART_MCR_RTS; |
| 1681 | if (mctrl & TIOCM_DTR) |
| 1682 | mcr |= UART_MCR_DTR; |
| 1683 | if (mctrl & TIOCM_OUT1) |
| 1684 | mcr |= UART_MCR_OUT1; |
| 1685 | if (mctrl & TIOCM_OUT2) |
| 1686 | mcr |= UART_MCR_OUT2; |
| 1687 | if (mctrl & TIOCM_LOOP) |
| 1688 | mcr |= UART_MCR_LOOP; |
| 1689 | |
| 1690 | mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr; |
| 1691 | |
| 1692 | serial_out(up, UART_MCR, mcr); |
| 1693 | } |
| 1694 | |
| 1695 | static void serial8250_break_ctl(struct uart_port *port, int break_state) |
| 1696 | { |
| 1697 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1698 | unsigned long flags; |
| 1699 | |
| 1700 | spin_lock_irqsave(&up->port.lock, flags); |
| 1701 | if (break_state == -1) |
| 1702 | up->lcr |= UART_LCR_SBC; |
| 1703 | else |
| 1704 | up->lcr &= ~UART_LCR_SBC; |
| 1705 | serial_out(up, UART_LCR, up->lcr); |
| 1706 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 1707 | } |
| 1708 | |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1709 | #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) |
| 1710 | |
| 1711 | /* |
| 1712 | * Wait for transmitter & holding register to empty |
| 1713 | */ |
| 1714 | static inline void wait_for_xmitr(struct uart_8250_port *up, int bits) |
| 1715 | { |
| 1716 | unsigned int status, tmout = 10000; |
| 1717 | |
| 1718 | /* Wait up to 10ms for the character(s) to be sent. */ |
| 1719 | do { |
| 1720 | status = serial_in(up, UART_LSR); |
| 1721 | |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1722 | up->lsr_saved_flags |= status & LSR_SAVE_FLAGS; |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1723 | |
| 1724 | if (--tmout == 0) |
| 1725 | break; |
| 1726 | udelay(1); |
| 1727 | } while ((status & bits) != bits); |
| 1728 | |
| 1729 | /* Wait up to 1s for flow control if necessary */ |
| 1730 | if (up->port.flags & UPF_CONS_FLOW) { |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1731 | unsigned int tmout; |
| 1732 | for (tmout = 1000000; tmout; tmout--) { |
| 1733 | unsigned int msr = serial_in(up, UART_MSR); |
| 1734 | up->msr_saved_flags |= msr & MSR_SAVE_FLAGS; |
| 1735 | if (msr & UART_MSR_CTS) |
| 1736 | break; |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1737 | udelay(1); |
| 1738 | touch_nmi_watchdog(); |
| 1739 | } |
| 1740 | } |
| 1741 | } |
| 1742 | |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 1743 | #ifdef CONFIG_CONSOLE_POLL |
| 1744 | /* |
| 1745 | * Console polling routines for writing and reading from the uart while |
| 1746 | * in an interrupt or debug context. |
| 1747 | */ |
| 1748 | |
| 1749 | static int serial8250_get_poll_char(struct uart_port *port) |
| 1750 | { |
| 1751 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1752 | unsigned char lsr = serial_inp(up, UART_LSR); |
| 1753 | |
| 1754 | while (!(lsr & UART_LSR_DR)) |
| 1755 | lsr = serial_inp(up, UART_LSR); |
| 1756 | |
| 1757 | return serial_inp(up, UART_RX); |
| 1758 | } |
| 1759 | |
| 1760 | |
| 1761 | static void serial8250_put_poll_char(struct uart_port *port, |
| 1762 | unsigned char c) |
| 1763 | { |
| 1764 | unsigned int ier; |
| 1765 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1766 | |
| 1767 | /* |
| 1768 | * First save the IER then disable the interrupts |
| 1769 | */ |
| 1770 | ier = serial_in(up, UART_IER); |
| 1771 | if (up->capabilities & UART_CAP_UUE) |
| 1772 | serial_out(up, UART_IER, UART_IER_UUE); |
| 1773 | else |
| 1774 | serial_out(up, UART_IER, 0); |
| 1775 | |
| 1776 | wait_for_xmitr(up, BOTH_EMPTY); |
| 1777 | /* |
| 1778 | * Send the character out. |
| 1779 | * If a LF, also do CR... |
| 1780 | */ |
| 1781 | serial_out(up, UART_TX, c); |
| 1782 | if (c == 10) { |
| 1783 | wait_for_xmitr(up, BOTH_EMPTY); |
| 1784 | serial_out(up, UART_TX, 13); |
| 1785 | } |
| 1786 | |
| 1787 | /* |
| 1788 | * Finally, wait for transmitter to become empty |
| 1789 | * and restore the IER |
| 1790 | */ |
| 1791 | wait_for_xmitr(up, BOTH_EMPTY); |
| 1792 | serial_out(up, UART_IER, ier); |
| 1793 | } |
| 1794 | |
| 1795 | #endif /* CONFIG_CONSOLE_POLL */ |
| 1796 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | static int serial8250_startup(struct uart_port *port) |
| 1798 | { |
| 1799 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1800 | unsigned long flags; |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1801 | unsigned char lsr, iir; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1802 | int retval; |
| 1803 | |
| 1804 | up->capabilities = uart_config[up->port.type].flags; |
| 1805 | up->mcr = 0; |
| 1806 | |
| 1807 | if (up->port.type == PORT_16C950) { |
| 1808 | /* Wake up and initialize UART */ |
| 1809 | up->acr = 0; |
| 1810 | serial_outp(up, UART_LCR, 0xBF); |
| 1811 | serial_outp(up, UART_EFR, UART_EFR_ECB); |
| 1812 | serial_outp(up, UART_IER, 0); |
| 1813 | serial_outp(up, UART_LCR, 0); |
| 1814 | serial_icr_write(up, UART_CSR, 0); /* Reset the UART */ |
| 1815 | serial_outp(up, UART_LCR, 0xBF); |
| 1816 | serial_outp(up, UART_EFR, UART_EFR_ECB); |
| 1817 | serial_outp(up, UART_LCR, 0); |
| 1818 | } |
| 1819 | |
| 1820 | #ifdef CONFIG_SERIAL_8250_RSA |
| 1821 | /* |
| 1822 | * If this is an RSA port, see if we can kick it up to the |
| 1823 | * higher speed clock. |
| 1824 | */ |
| 1825 | enable_rsa(up); |
| 1826 | #endif |
| 1827 | |
| 1828 | /* |
| 1829 | * Clear the FIFO buffers and disable them. |
Alexey Dobriyan | 7f927fc | 2006-03-28 01:56:53 -0800 | [diff] [blame] | 1830 | * (they will be reenabled in set_termios()) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | */ |
| 1832 | serial8250_clear_fifos(up); |
| 1833 | |
| 1834 | /* |
| 1835 | * Clear the interrupt registers. |
| 1836 | */ |
| 1837 | (void) serial_inp(up, UART_LSR); |
| 1838 | (void) serial_inp(up, UART_RX); |
| 1839 | (void) serial_inp(up, UART_IIR); |
| 1840 | (void) serial_inp(up, UART_MSR); |
| 1841 | |
| 1842 | /* |
| 1843 | * At this point, there's no way the LSR could still be 0xff; |
| 1844 | * if it is, then bail out, because there's likely no UART |
| 1845 | * here. |
| 1846 | */ |
| 1847 | if (!(up->port.flags & UPF_BUGGY_UART) && |
| 1848 | (serial_inp(up, UART_LSR) == 0xff)) { |
| 1849 | printk("ttyS%d: LSR safety check engaged!\n", up->port.line); |
| 1850 | return -ENODEV; |
| 1851 | } |
| 1852 | |
| 1853 | /* |
| 1854 | * For a XR16C850, we need to set the trigger levels |
| 1855 | */ |
| 1856 | if (up->port.type == PORT_16850) { |
| 1857 | unsigned char fctr; |
| 1858 | |
| 1859 | serial_outp(up, UART_LCR, 0xbf); |
| 1860 | |
| 1861 | fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX); |
| 1862 | serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX); |
| 1863 | serial_outp(up, UART_TRG, UART_TRG_96); |
| 1864 | serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX); |
| 1865 | serial_outp(up, UART_TRG, UART_TRG_96); |
| 1866 | |
| 1867 | serial_outp(up, UART_LCR, 0); |
| 1868 | } |
| 1869 | |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1870 | if (is_real_interrupt(up->port.irq)) { |
Alex Williamson | 01c194d | 2008-04-28 02:14:09 -0700 | [diff] [blame] | 1871 | unsigned char iir1; |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1872 | /* |
| 1873 | * Test for UARTs that do not reassert THRE when the |
| 1874 | * transmitter is idle and the interrupt has already |
| 1875 | * been cleared. Real 16550s should always reassert |
| 1876 | * this interrupt whenever the transmitter is idle and |
| 1877 | * the interrupt is enabled. Delays are necessary to |
| 1878 | * allow register changes to become visible. |
| 1879 | */ |
| 1880 | spin_lock_irqsave(&up->port.lock, flags); |
| 1881 | |
| 1882 | wait_for_xmitr(up, UART_LSR_THRE); |
| 1883 | serial_out_sync(up, UART_IER, UART_IER_THRI); |
| 1884 | udelay(1); /* allow THRE to set */ |
Alex Williamson | 01c194d | 2008-04-28 02:14:09 -0700 | [diff] [blame] | 1885 | iir1 = serial_in(up, UART_IIR); |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1886 | serial_out(up, UART_IER, 0); |
| 1887 | serial_out_sync(up, UART_IER, UART_IER_THRI); |
| 1888 | udelay(1); /* allow a working UART time to re-assert THRE */ |
| 1889 | iir = serial_in(up, UART_IIR); |
| 1890 | serial_out(up, UART_IER, 0); |
| 1891 | |
| 1892 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 1893 | |
| 1894 | /* |
| 1895 | * If the interrupt is not reasserted, setup a timer to |
| 1896 | * kick the UART on a regular basis. |
| 1897 | */ |
Alex Williamson | 01c194d | 2008-04-28 02:14:09 -0700 | [diff] [blame] | 1898 | if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) { |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1899 | pr_debug("ttyS%d - using backup timer\n", port->line); |
| 1900 | up->timer.function = serial8250_backup_timeout; |
| 1901 | up->timer.data = (unsigned long)up; |
| 1902 | mod_timer(&up->timer, jiffies + |
Alan Cox | 6f803cd | 2008-02-08 04:18:52 -0800 | [diff] [blame] | 1903 | poll_timeout(up->port.timeout) + HZ / 5); |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1904 | } |
| 1905 | } |
| 1906 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1907 | /* |
| 1908 | * If the "interrupt" for this port doesn't correspond with any |
| 1909 | * hardware interrupt, we use a timer-based system. The original |
| 1910 | * driver used to do this with IRQ0. |
| 1911 | */ |
| 1912 | if (!is_real_interrupt(up->port.irq)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | up->timer.data = (unsigned long)up; |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 1914 | mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | } else { |
| 1916 | retval = serial_link_irq_chain(up); |
| 1917 | if (retval) |
| 1918 | return retval; |
| 1919 | } |
| 1920 | |
| 1921 | /* |
| 1922 | * Now, initialize the UART |
| 1923 | */ |
| 1924 | serial_outp(up, UART_LCR, UART_LCR_WLEN8); |
| 1925 | |
| 1926 | spin_lock_irqsave(&up->port.lock, flags); |
| 1927 | if (up->port.flags & UPF_FOURPORT) { |
| 1928 | if (!is_real_interrupt(up->port.irq)) |
| 1929 | up->port.mctrl |= TIOCM_OUT1; |
| 1930 | } else |
| 1931 | /* |
| 1932 | * Most PC uarts need OUT2 raised to enable interrupts. |
| 1933 | */ |
| 1934 | if (is_real_interrupt(up->port.irq)) |
| 1935 | up->port.mctrl |= TIOCM_OUT2; |
| 1936 | |
| 1937 | serial8250_set_mctrl(&up->port, up->port.mctrl); |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1938 | |
| 1939 | /* |
| 1940 | * Do a quick test to see if we receive an |
| 1941 | * interrupt when we enable the TX irq. |
| 1942 | */ |
| 1943 | serial_outp(up, UART_IER, UART_IER_THRI); |
| 1944 | lsr = serial_in(up, UART_LSR); |
| 1945 | iir = serial_in(up, UART_IIR); |
| 1946 | serial_outp(up, UART_IER, 0); |
| 1947 | |
| 1948 | if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) { |
Russell King | 67f7654 | 2005-06-23 22:26:43 +0100 | [diff] [blame] | 1949 | if (!(up->bugs & UART_BUG_TXEN)) { |
| 1950 | up->bugs |= UART_BUG_TXEN; |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1951 | pr_debug("ttyS%d - enabling bad tx status workarounds\n", |
| 1952 | port->line); |
| 1953 | } |
| 1954 | } else { |
Russell King | 67f7654 | 2005-06-23 22:26:43 +0100 | [diff] [blame] | 1955 | up->bugs &= ~UART_BUG_TXEN; |
Russell King | 55d3b28 | 2005-06-23 15:05:41 +0100 | [diff] [blame] | 1956 | } |
| 1957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 1959 | |
| 1960 | /* |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 1961 | * Clear the interrupt registers again for luck, and clear the |
| 1962 | * saved flags to avoid getting false values from polling |
| 1963 | * routines or the previous session. |
| 1964 | */ |
| 1965 | serial_inp(up, UART_LSR); |
| 1966 | serial_inp(up, UART_RX); |
| 1967 | serial_inp(up, UART_IIR); |
| 1968 | serial_inp(up, UART_MSR); |
| 1969 | up->lsr_saved_flags = 0; |
| 1970 | up->msr_saved_flags = 0; |
| 1971 | |
| 1972 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | * Finally, enable interrupts. Note: Modem status interrupts |
| 1974 | * are set via set_termios(), which will be occurring imminently |
| 1975 | * anyway, so we don't enable them here. |
| 1976 | */ |
| 1977 | up->ier = UART_IER_RLSI | UART_IER_RDI; |
| 1978 | serial_outp(up, UART_IER, up->ier); |
| 1979 | |
| 1980 | if (up->port.flags & UPF_FOURPORT) { |
| 1981 | unsigned int icp; |
| 1982 | /* |
| 1983 | * Enable interrupts on the AST Fourport board |
| 1984 | */ |
| 1985 | icp = (up->port.iobase & 0xfe0) | 0x01f; |
| 1986 | outb_p(0x80, icp); |
| 1987 | (void) inb_p(icp); |
| 1988 | } |
| 1989 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | static void serial8250_shutdown(struct uart_port *port) |
| 1994 | { |
| 1995 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 1996 | unsigned long flags; |
| 1997 | |
| 1998 | /* |
| 1999 | * Disable interrupts from this port |
| 2000 | */ |
| 2001 | up->ier = 0; |
| 2002 | serial_outp(up, UART_IER, 0); |
| 2003 | |
| 2004 | spin_lock_irqsave(&up->port.lock, flags); |
| 2005 | if (up->port.flags & UPF_FOURPORT) { |
| 2006 | /* reset interrupts on the AST Fourport board */ |
| 2007 | inb((up->port.iobase & 0xfe0) | 0x1f); |
| 2008 | up->port.mctrl |= TIOCM_OUT1; |
| 2009 | } else |
| 2010 | up->port.mctrl &= ~TIOCM_OUT2; |
| 2011 | |
| 2012 | serial8250_set_mctrl(&up->port, up->port.mctrl); |
| 2013 | spin_unlock_irqrestore(&up->port.lock, flags); |
| 2014 | |
| 2015 | /* |
| 2016 | * Disable break condition and FIFOs |
| 2017 | */ |
| 2018 | serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC); |
| 2019 | serial8250_clear_fifos(up); |
| 2020 | |
| 2021 | #ifdef CONFIG_SERIAL_8250_RSA |
| 2022 | /* |
| 2023 | * Reset the RSA board back to 115kbps compat mode. |
| 2024 | */ |
| 2025 | disable_rsa(up); |
| 2026 | #endif |
| 2027 | |
| 2028 | /* |
| 2029 | * Read data port to reset things, and then unlink from |
| 2030 | * the IRQ chain. |
| 2031 | */ |
| 2032 | (void) serial_in(up, UART_RX); |
| 2033 | |
Alex Williamson | 40b36da | 2007-02-14 00:33:04 -0800 | [diff] [blame] | 2034 | del_timer_sync(&up->timer); |
| 2035 | up->timer.function = serial8250_timeout; |
| 2036 | if (is_real_interrupt(up->port.irq)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2037 | serial_unlink_irq_chain(up); |
| 2038 | } |
| 2039 | |
| 2040 | static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud) |
| 2041 | { |
| 2042 | unsigned int quot; |
| 2043 | |
| 2044 | /* |
| 2045 | * Handle magic divisors for baud rates above baud_base on |
| 2046 | * SMSC SuperIO chips. |
| 2047 | */ |
| 2048 | if ((port->flags & UPF_MAGIC_MULTIPLIER) && |
| 2049 | baud == (port->uartclk/4)) |
| 2050 | quot = 0x8001; |
| 2051 | else if ((port->flags & UPF_MAGIC_MULTIPLIER) && |
| 2052 | baud == (port->uartclk/8)) |
| 2053 | quot = 0x8002; |
| 2054 | else |
| 2055 | quot = uart_get_divisor(port, baud); |
| 2056 | |
| 2057 | return quot; |
| 2058 | } |
| 2059 | |
| 2060 | static void |
Alan Cox | 606d099 | 2006-12-08 02:38:45 -0800 | [diff] [blame] | 2061 | serial8250_set_termios(struct uart_port *port, struct ktermios *termios, |
| 2062 | struct ktermios *old) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2063 | { |
| 2064 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 2065 | unsigned char cval, fcr = 0; |
| 2066 | unsigned long flags; |
| 2067 | unsigned int baud, quot; |
| 2068 | |
| 2069 | switch (termios->c_cflag & CSIZE) { |
| 2070 | case CS5: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 2071 | cval = UART_LCR_WLEN5; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | break; |
| 2073 | case CS6: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 2074 | cval = UART_LCR_WLEN6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | break; |
| 2076 | case CS7: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 2077 | cval = UART_LCR_WLEN7; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | break; |
| 2079 | default: |
| 2080 | case CS8: |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 2081 | cval = UART_LCR_WLEN8; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | break; |
| 2083 | } |
| 2084 | |
| 2085 | if (termios->c_cflag & CSTOPB) |
Russell King | 0a8b80c5 | 2005-06-24 19:48:22 +0100 | [diff] [blame] | 2086 | cval |= UART_LCR_STOP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2087 | if (termios->c_cflag & PARENB) |
| 2088 | cval |= UART_LCR_PARITY; |
| 2089 | if (!(termios->c_cflag & PARODD)) |
| 2090 | cval |= UART_LCR_EPAR; |
| 2091 | #ifdef CMSPAR |
| 2092 | if (termios->c_cflag & CMSPAR) |
| 2093 | cval |= UART_LCR_SPAR; |
| 2094 | #endif |
| 2095 | |
| 2096 | /* |
| 2097 | * Ask the core to calculate the divisor for us. |
| 2098 | */ |
Thomas Koeller | bd71c18 | 2007-05-06 14:48:47 -0700 | [diff] [blame] | 2099 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | quot = serial8250_get_divisor(port, baud); |
| 2101 | |
| 2102 | /* |
Russell King | 4ba5e35 | 2005-06-23 10:43:04 +0100 | [diff] [blame] | 2103 | * Oxford Semi 952 rev B workaround |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | */ |
Russell King | 4ba5e35 | 2005-06-23 10:43:04 +0100 | [diff] [blame] | 2105 | if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0) |
Alan Cox | 3e8d4e2 | 2008-02-04 22:27:53 -0800 | [diff] [blame] | 2106 | quot++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2107 | |
| 2108 | if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) { |
| 2109 | if (baud < 2400) |
| 2110 | fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1; |
| 2111 | else |
| 2112 | fcr = uart_config[up->port.type].fcr; |
| 2113 | } |
| 2114 | |
| 2115 | /* |
| 2116 | * MCR-based auto flow control. When AFE is enabled, RTS will be |
| 2117 | * deasserted when the receive FIFO contains more characters than |
| 2118 | * the trigger, or the MCR RTS bit is cleared. In the case where |
| 2119 | * the remote UART is not using CTS auto flow control, we must |
| 2120 | * have sufficient FIFO entries for the latency of the remote |
| 2121 | * UART to respond. IOW, at least 32 bytes of FIFO. |
| 2122 | */ |
| 2123 | if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) { |
| 2124 | up->mcr &= ~UART_MCR_AFE; |
| 2125 | if (termios->c_cflag & CRTSCTS) |
| 2126 | up->mcr |= UART_MCR_AFE; |
| 2127 | } |
| 2128 | |
| 2129 | /* |
| 2130 | * Ok, we're now changing the port state. Do it with |
| 2131 | * interrupts disabled. |
| 2132 | */ |
| 2133 | spin_lock_irqsave(&up->port.lock, flags); |
| 2134 | |
| 2135 | /* |
| 2136 | * Update the per-port timeout. |
| 2137 | */ |
| 2138 | uart_update_timeout(port, termios->c_cflag, baud); |
| 2139 | |
| 2140 | up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR; |
| 2141 | if (termios->c_iflag & INPCK) |
| 2142 | up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE; |
| 2143 | if (termios->c_iflag & (BRKINT | PARMRK)) |
| 2144 | up->port.read_status_mask |= UART_LSR_BI; |
| 2145 | |
| 2146 | /* |
| 2147 | * Characteres to ignore |
| 2148 | */ |
| 2149 | up->port.ignore_status_mask = 0; |
| 2150 | if (termios->c_iflag & IGNPAR) |
| 2151 | up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; |
| 2152 | if (termios->c_iflag & IGNBRK) { |
| 2153 | up->port.ignore_status_mask |= UART_LSR_BI; |
| 2154 | /* |
| 2155 | * If we're ignoring parity and break indicators, |
| 2156 | * ignore overruns too (for real raw support). |
| 2157 | */ |
| 2158 | if (termios->c_iflag & IGNPAR) |
| 2159 | up->port.ignore_status_mask |= UART_LSR_OE; |
| 2160 | } |
| 2161 | |
| 2162 | /* |
| 2163 | * ignore all characters if CREAD is not set |
| 2164 | */ |
| 2165 | if ((termios->c_cflag & CREAD) == 0) |
| 2166 | up->port.ignore_status_mask |= UART_LSR_DR; |
| 2167 | |
| 2168 | /* |
| 2169 | * CTS flow control flag and modem status interrupts |
| 2170 | */ |
| 2171 | up->ier &= ~UART_IER_MSI; |
Pantelis Antoniou | 21c614a | 2005-11-06 09:07:03 +0000 | [diff] [blame] | 2172 | if (!(up->bugs & UART_BUG_NOMSR) && |
| 2173 | UART_ENABLE_MS(&up->port, termios->c_cflag)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | up->ier |= UART_IER_MSI; |
| 2175 | if (up->capabilities & UART_CAP_UUE) |
| 2176 | up->ier |= UART_IER_UUE | UART_IER_RTOIE; |
| 2177 | |
| 2178 | serial_out(up, UART_IER, up->ier); |
| 2179 | |
| 2180 | if (up->capabilities & UART_CAP_EFR) { |
| 2181 | unsigned char efr = 0; |
| 2182 | /* |
| 2183 | * TI16C752/Startech hardware flow control. FIXME: |
| 2184 | * - TI16C752 requires control thresholds to be set. |
| 2185 | * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled. |
| 2186 | */ |
| 2187 | if (termios->c_cflag & CRTSCTS) |
| 2188 | efr |= UART_EFR_CTS; |
| 2189 | |
| 2190 | serial_outp(up, UART_LCR, 0xBF); |
| 2191 | serial_outp(up, UART_EFR, efr); |
| 2192 | } |
| 2193 | |
Jonathan McDowell | 255341c | 2006-08-14 23:05:32 -0700 | [diff] [blame] | 2194 | #ifdef CONFIG_ARCH_OMAP15XX |
| 2195 | /* Workaround to enable 115200 baud on OMAP1510 internal ports */ |
| 2196 | if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) { |
| 2197 | if (baud == 115200) { |
| 2198 | quot = 1; |
| 2199 | serial_out(up, UART_OMAP_OSC_12M_SEL, 1); |
| 2200 | } else |
| 2201 | serial_out(up, UART_OMAP_OSC_12M_SEL, 0); |
| 2202 | } |
| 2203 | #endif |
| 2204 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2205 | if (up->capabilities & UART_NATSEMI) { |
| 2206 | /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */ |
| 2207 | serial_outp(up, UART_LCR, 0xe0); |
| 2208 | } else { |
| 2209 | serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */ |
| 2210 | } |
| 2211 | |
Jon Anders Haugum | b32b19b | 2006-04-30 11:20:56 +0100 | [diff] [blame] | 2212 | serial_dl_write(up, quot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2213 | |
| 2214 | /* |
| 2215 | * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR |
| 2216 | * is written without DLAB set, this mode will be disabled. |
| 2217 | */ |
| 2218 | if (up->port.type == PORT_16750) |
| 2219 | serial_outp(up, UART_FCR, fcr); |
| 2220 | |
| 2221 | serial_outp(up, UART_LCR, cval); /* reset DLAB */ |
| 2222 | up->lcr = cval; /* Save LCR */ |
| 2223 | if (up->port.type != PORT_16750) { |
| 2224 | if (fcr & UART_FCR_ENABLE_FIFO) { |
| 2225 | /* emulated UARTs (Lucent Venus 167x) need two steps */ |
| 2226 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); |
| 2227 | } |
| 2228 | serial_outp(up, UART_FCR, fcr); /* set fcr */ |
| 2229 | } |
| 2230 | serial8250_set_mctrl(&up->port, up->port.mctrl); |
| 2231 | spin_unlock_irqrestore(&up->port.lock, flags); |
Alan Cox | e991a2b | 2008-04-28 02:14:06 -0700 | [diff] [blame] | 2232 | /* Don't rewrite B0 */ |
| 2233 | if (tty_termios_baud_rate(termios)) |
| 2234 | tty_termios_encode_baud_rate(termios, baud, baud); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2235 | } |
| 2236 | |
| 2237 | static void |
| 2238 | serial8250_pm(struct uart_port *port, unsigned int state, |
| 2239 | unsigned int oldstate) |
| 2240 | { |
| 2241 | struct uart_8250_port *p = (struct uart_8250_port *)port; |
| 2242 | |
| 2243 | serial8250_set_sleep(p, state != 0); |
| 2244 | |
| 2245 | if (p->pm) |
| 2246 | p->pm(port, state, oldstate); |
| 2247 | } |
| 2248 | |
| 2249 | /* |
| 2250 | * Resource handling. |
| 2251 | */ |
| 2252 | static int serial8250_request_std_resource(struct uart_8250_port *up) |
| 2253 | { |
| 2254 | unsigned int size = 8 << up->port.regshift; |
| 2255 | int ret = 0; |
| 2256 | |
| 2257 | switch (up->port.iotype) { |
Sergei Shtylyov | 85835f4 | 2006-04-30 11:15:58 +0100 | [diff] [blame] | 2258 | case UPIO_AU: |
| 2259 | size = 0x100000; |
| 2260 | /* fall thru */ |
Sergei Shtylyov | 0b30d66 | 2006-09-09 22:23:56 +0400 | [diff] [blame] | 2261 | case UPIO_TSI: |
| 2262 | case UPIO_MEM32: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | case UPIO_MEM: |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 2264 | case UPIO_DWAPB: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | if (!up->port.mapbase) |
| 2266 | break; |
| 2267 | |
| 2268 | if (!request_mem_region(up->port.mapbase, size, "serial")) { |
| 2269 | ret = -EBUSY; |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | if (up->port.flags & UPF_IOREMAP) { |
Alan Cox | 6f441fe | 2008-05-01 04:34:59 -0700 | [diff] [blame] | 2274 | up->port.membase = ioremap_nocache(up->port.mapbase, |
| 2275 | size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2276 | if (!up->port.membase) { |
| 2277 | release_mem_region(up->port.mapbase, size); |
| 2278 | ret = -ENOMEM; |
| 2279 | } |
| 2280 | } |
| 2281 | break; |
| 2282 | |
| 2283 | case UPIO_HUB6: |
| 2284 | case UPIO_PORT: |
| 2285 | if (!request_region(up->port.iobase, size, "serial")) |
| 2286 | ret = -EBUSY; |
| 2287 | break; |
| 2288 | } |
| 2289 | return ret; |
| 2290 | } |
| 2291 | |
| 2292 | static void serial8250_release_std_resource(struct uart_8250_port *up) |
| 2293 | { |
| 2294 | unsigned int size = 8 << up->port.regshift; |
| 2295 | |
| 2296 | switch (up->port.iotype) { |
Sergei Shtylyov | 85835f4 | 2006-04-30 11:15:58 +0100 | [diff] [blame] | 2297 | case UPIO_AU: |
| 2298 | size = 0x100000; |
| 2299 | /* fall thru */ |
Sergei Shtylyov | 0b30d66 | 2006-09-09 22:23:56 +0400 | [diff] [blame] | 2300 | case UPIO_TSI: |
| 2301 | case UPIO_MEM32: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2302 | case UPIO_MEM: |
Marc St-Jean | beab697 | 2007-05-06 14:48:45 -0700 | [diff] [blame] | 2303 | case UPIO_DWAPB: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2304 | if (!up->port.mapbase) |
| 2305 | break; |
| 2306 | |
| 2307 | if (up->port.flags & UPF_IOREMAP) { |
| 2308 | iounmap(up->port.membase); |
| 2309 | up->port.membase = NULL; |
| 2310 | } |
| 2311 | |
| 2312 | release_mem_region(up->port.mapbase, size); |
| 2313 | break; |
| 2314 | |
| 2315 | case UPIO_HUB6: |
| 2316 | case UPIO_PORT: |
| 2317 | release_region(up->port.iobase, size); |
| 2318 | break; |
| 2319 | } |
| 2320 | } |
| 2321 | |
| 2322 | static int serial8250_request_rsa_resource(struct uart_8250_port *up) |
| 2323 | { |
| 2324 | unsigned long start = UART_RSA_BASE << up->port.regshift; |
| 2325 | unsigned int size = 8 << up->port.regshift; |
Sergei Shtylyov | 0b30d66 | 2006-09-09 22:23:56 +0400 | [diff] [blame] | 2326 | int ret = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | |
| 2328 | switch (up->port.iotype) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2329 | case UPIO_HUB6: |
| 2330 | case UPIO_PORT: |
| 2331 | start += up->port.iobase; |
Sergei Shtylyov | 0b30d66 | 2006-09-09 22:23:56 +0400 | [diff] [blame] | 2332 | if (request_region(start, size, "serial-rsa")) |
| 2333 | ret = 0; |
| 2334 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2335 | ret = -EBUSY; |
| 2336 | break; |
| 2337 | } |
| 2338 | |
| 2339 | return ret; |
| 2340 | } |
| 2341 | |
| 2342 | static void serial8250_release_rsa_resource(struct uart_8250_port *up) |
| 2343 | { |
| 2344 | unsigned long offset = UART_RSA_BASE << up->port.regshift; |
| 2345 | unsigned int size = 8 << up->port.regshift; |
| 2346 | |
| 2347 | switch (up->port.iotype) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | case UPIO_HUB6: |
| 2349 | case UPIO_PORT: |
| 2350 | release_region(up->port.iobase + offset, size); |
| 2351 | break; |
| 2352 | } |
| 2353 | } |
| 2354 | |
| 2355 | static void serial8250_release_port(struct uart_port *port) |
| 2356 | { |
| 2357 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 2358 | |
| 2359 | serial8250_release_std_resource(up); |
| 2360 | if (up->port.type == PORT_RSA) |
| 2361 | serial8250_release_rsa_resource(up); |
| 2362 | } |
| 2363 | |
| 2364 | static int serial8250_request_port(struct uart_port *port) |
| 2365 | { |
| 2366 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 2367 | int ret = 0; |
| 2368 | |
| 2369 | ret = serial8250_request_std_resource(up); |
| 2370 | if (ret == 0 && up->port.type == PORT_RSA) { |
| 2371 | ret = serial8250_request_rsa_resource(up); |
| 2372 | if (ret < 0) |
| 2373 | serial8250_release_std_resource(up); |
| 2374 | } |
| 2375 | |
| 2376 | return ret; |
| 2377 | } |
| 2378 | |
| 2379 | static void serial8250_config_port(struct uart_port *port, int flags) |
| 2380 | { |
| 2381 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 2382 | int probeflags = PROBE_ANY; |
| 2383 | int ret; |
| 2384 | |
| 2385 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | * Find the region that we can probe for. This in turn |
| 2387 | * tells us whether we can probe for the type of port. |
| 2388 | */ |
| 2389 | ret = serial8250_request_std_resource(up); |
| 2390 | if (ret < 0) |
| 2391 | return; |
| 2392 | |
| 2393 | ret = serial8250_request_rsa_resource(up); |
| 2394 | if (ret < 0) |
| 2395 | probeflags &= ~PROBE_RSA; |
| 2396 | |
| 2397 | if (flags & UART_CONFIG_TYPE) |
| 2398 | autoconfig(up, probeflags); |
| 2399 | if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ) |
| 2400 | autoconfig_irq(up); |
| 2401 | |
| 2402 | if (up->port.type != PORT_RSA && probeflags & PROBE_RSA) |
| 2403 | serial8250_release_rsa_resource(up); |
| 2404 | if (up->port.type == PORT_UNKNOWN) |
| 2405 | serial8250_release_std_resource(up); |
| 2406 | } |
| 2407 | |
| 2408 | static int |
| 2409 | serial8250_verify_port(struct uart_port *port, struct serial_struct *ser) |
| 2410 | { |
| 2411 | if (ser->irq >= NR_IRQS || ser->irq < 0 || |
| 2412 | ser->baud_base < 9600 || ser->type < PORT_UNKNOWN || |
| 2413 | ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS || |
| 2414 | ser->type == PORT_STARTECH) |
| 2415 | return -EINVAL; |
| 2416 | return 0; |
| 2417 | } |
| 2418 | |
| 2419 | static const char * |
| 2420 | serial8250_type(struct uart_port *port) |
| 2421 | { |
| 2422 | int type = port->type; |
| 2423 | |
| 2424 | if (type >= ARRAY_SIZE(uart_config)) |
| 2425 | type = 0; |
| 2426 | return uart_config[type].name; |
| 2427 | } |
| 2428 | |
| 2429 | static struct uart_ops serial8250_pops = { |
| 2430 | .tx_empty = serial8250_tx_empty, |
| 2431 | .set_mctrl = serial8250_set_mctrl, |
| 2432 | .get_mctrl = serial8250_get_mctrl, |
| 2433 | .stop_tx = serial8250_stop_tx, |
| 2434 | .start_tx = serial8250_start_tx, |
| 2435 | .stop_rx = serial8250_stop_rx, |
| 2436 | .enable_ms = serial8250_enable_ms, |
| 2437 | .break_ctl = serial8250_break_ctl, |
| 2438 | .startup = serial8250_startup, |
| 2439 | .shutdown = serial8250_shutdown, |
| 2440 | .set_termios = serial8250_set_termios, |
| 2441 | .pm = serial8250_pm, |
| 2442 | .type = serial8250_type, |
| 2443 | .release_port = serial8250_release_port, |
| 2444 | .request_port = serial8250_request_port, |
| 2445 | .config_port = serial8250_config_port, |
| 2446 | .verify_port = serial8250_verify_port, |
Jason Wessel | f2d937f | 2008-04-17 20:05:37 +0200 | [diff] [blame] | 2447 | #ifdef CONFIG_CONSOLE_POLL |
| 2448 | .poll_get_char = serial8250_get_poll_char, |
| 2449 | .poll_put_char = serial8250_put_poll_char, |
| 2450 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | }; |
| 2452 | |
| 2453 | static struct uart_8250_port serial8250_ports[UART_NR]; |
| 2454 | |
| 2455 | static void __init serial8250_isa_init_ports(void) |
| 2456 | { |
| 2457 | struct uart_8250_port *up; |
| 2458 | static int first = 1; |
| 2459 | int i; |
| 2460 | |
| 2461 | if (!first) |
| 2462 | return; |
| 2463 | first = 0; |
| 2464 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2465 | for (i = 0; i < nr_uarts; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | struct uart_8250_port *up = &serial8250_ports[i]; |
| 2467 | |
| 2468 | up->port.line = i; |
| 2469 | spin_lock_init(&up->port.lock); |
| 2470 | |
| 2471 | init_timer(&up->timer); |
| 2472 | up->timer.function = serial8250_timeout; |
| 2473 | |
| 2474 | /* |
| 2475 | * ALPHA_KLUDGE_MCR needs to be killed. |
| 2476 | */ |
| 2477 | up->mcr_mask = ~ALPHA_KLUDGE_MCR; |
| 2478 | up->mcr_force = ALPHA_KLUDGE_MCR; |
| 2479 | |
| 2480 | up->port.ops = &serial8250_pops; |
| 2481 | } |
| 2482 | |
Russell King | 44454bc | 2005-06-30 22:41:22 +0100 | [diff] [blame] | 2483 | for (i = 0, up = serial8250_ports; |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2484 | i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | i++, up++) { |
| 2486 | up->port.iobase = old_serial_port[i].port; |
| 2487 | up->port.irq = irq_canonicalize(old_serial_port[i].irq); |
| 2488 | up->port.uartclk = old_serial_port[i].baud_base * 16; |
| 2489 | up->port.flags = old_serial_port[i].flags; |
| 2490 | up->port.hub6 = old_serial_port[i].hub6; |
| 2491 | up->port.membase = old_serial_port[i].iomem_base; |
| 2492 | up->port.iotype = old_serial_port[i].io_type; |
| 2493 | up->port.regshift = old_serial_port[i].iomem_reg_shift; |
| 2494 | if (share_irqs) |
| 2495 | up->port.flags |= UPF_SHARE_IRQ; |
| 2496 | } |
| 2497 | } |
| 2498 | |
| 2499 | static void __init |
| 2500 | serial8250_register_ports(struct uart_driver *drv, struct device *dev) |
| 2501 | { |
| 2502 | int i; |
| 2503 | |
| 2504 | serial8250_isa_init_ports(); |
| 2505 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2506 | for (i = 0; i < nr_uarts; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2507 | struct uart_8250_port *up = &serial8250_ports[i]; |
| 2508 | |
| 2509 | up->port.dev = dev; |
| 2510 | uart_add_one_port(drv, &up->port); |
| 2511 | } |
| 2512 | } |
| 2513 | |
| 2514 | #ifdef CONFIG_SERIAL_8250_CONSOLE |
| 2515 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2516 | static void serial8250_console_putchar(struct uart_port *port, int ch) |
| 2517 | { |
| 2518 | struct uart_8250_port *up = (struct uart_8250_port *)port; |
| 2519 | |
| 2520 | wait_for_xmitr(up, UART_LSR_THRE); |
| 2521 | serial_out(up, UART_TX, ch); |
| 2522 | } |
| 2523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | /* |
| 2525 | * Print a string to the serial port trying not to disturb |
| 2526 | * any possible real use of the port... |
| 2527 | * |
| 2528 | * The console_lock must be held when we get here. |
| 2529 | */ |
| 2530 | static void |
| 2531 | serial8250_console_write(struct console *co, const char *s, unsigned int count) |
| 2532 | { |
| 2533 | struct uart_8250_port *up = &serial8250_ports[co->index]; |
Russell King | d8a5a8d | 2006-05-02 16:04:29 +0100 | [diff] [blame] | 2534 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2535 | unsigned int ier; |
Russell King | d8a5a8d | 2006-05-02 16:04:29 +0100 | [diff] [blame] | 2536 | int locked = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2537 | |
Andrew Morton | 78512ec | 2005-11-07 00:59:13 -0800 | [diff] [blame] | 2538 | touch_nmi_watchdog(); |
| 2539 | |
Andrew Morton | 68aa2c0 | 2006-06-30 02:29:59 -0700 | [diff] [blame] | 2540 | local_irq_save(flags); |
| 2541 | if (up->port.sysrq) { |
| 2542 | /* serial8250_handle_port() already took the lock */ |
| 2543 | locked = 0; |
| 2544 | } else if (oops_in_progress) { |
| 2545 | locked = spin_trylock(&up->port.lock); |
Russell King | d8a5a8d | 2006-05-02 16:04:29 +0100 | [diff] [blame] | 2546 | } else |
Andrew Morton | 68aa2c0 | 2006-06-30 02:29:59 -0700 | [diff] [blame] | 2547 | spin_lock(&up->port.lock); |
Russell King | d8a5a8d | 2006-05-02 16:04:29 +0100 | [diff] [blame] | 2548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | /* |
Ralf Baechle | dc7bf13 | 2006-02-15 09:59:47 +0000 | [diff] [blame] | 2550 | * First save the IER then disable the interrupts |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | */ |
| 2552 | ier = serial_in(up, UART_IER); |
| 2553 | |
| 2554 | if (up->capabilities & UART_CAP_UUE) |
| 2555 | serial_out(up, UART_IER, UART_IER_UUE); |
| 2556 | else |
| 2557 | serial_out(up, UART_IER, 0); |
| 2558 | |
Russell King | d358788 | 2006-03-20 20:00:09 +0000 | [diff] [blame] | 2559 | uart_console_write(&up->port, s, count, serial8250_console_putchar); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2560 | |
| 2561 | /* |
| 2562 | * Finally, wait for transmitter to become empty |
| 2563 | * and restore the IER |
| 2564 | */ |
Alan Cox | f91a371 | 2006-01-21 14:59:12 +0000 | [diff] [blame] | 2565 | wait_for_xmitr(up, BOTH_EMPTY); |
Russell King | a88d75b | 2006-04-30 11:30:15 +0100 | [diff] [blame] | 2566 | serial_out(up, UART_IER, ier); |
Russell King | d8a5a8d | 2006-05-02 16:04:29 +0100 | [diff] [blame] | 2567 | |
Corey Minyard | ad4c2aa | 2007-08-22 14:01:18 -0700 | [diff] [blame] | 2568 | /* |
| 2569 | * The receive handling will happen properly because the |
| 2570 | * receive ready bit will still be set; it is not cleared |
| 2571 | * on read. However, modem control will not, we must |
| 2572 | * call it if we have saved something in the saved flags |
| 2573 | * while processing with interrupts off. |
| 2574 | */ |
| 2575 | if (up->msr_saved_flags) |
| 2576 | check_modem_status(up); |
| 2577 | |
Russell King | d8a5a8d | 2006-05-02 16:04:29 +0100 | [diff] [blame] | 2578 | if (locked) |
Andrew Morton | 68aa2c0 | 2006-06-30 02:29:59 -0700 | [diff] [blame] | 2579 | spin_unlock(&up->port.lock); |
| 2580 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2581 | } |
| 2582 | |
Vivek Goyal | 118c0ac | 2007-01-11 01:52:44 +0100 | [diff] [blame] | 2583 | static int __init serial8250_console_setup(struct console *co, char *options) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2584 | { |
| 2585 | struct uart_port *port; |
| 2586 | int baud = 9600; |
| 2587 | int bits = 8; |
| 2588 | int parity = 'n'; |
| 2589 | int flow = 'n'; |
| 2590 | |
| 2591 | /* |
| 2592 | * Check whether an invalid uart number has been specified, and |
| 2593 | * if so, search for the first available port that does have |
| 2594 | * console support. |
| 2595 | */ |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2596 | if (co->index >= nr_uarts) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | co->index = 0; |
| 2598 | port = &serial8250_ports[co->index].port; |
| 2599 | if (!port->iobase && !port->membase) |
| 2600 | return -ENODEV; |
| 2601 | |
| 2602 | if (options) |
| 2603 | uart_parse_options(options, &baud, &parity, &bits, &flow); |
| 2604 | |
| 2605 | return uart_set_options(port, co, baud, parity, bits, flow); |
| 2606 | } |
| 2607 | |
Daniel Ritz | b6b1d87 | 2007-08-03 16:07:43 +0200 | [diff] [blame] | 2608 | static int serial8250_console_early_setup(void) |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 2609 | { |
| 2610 | return serial8250_find_port_for_earlycon(); |
| 2611 | } |
| 2612 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2613 | static struct uart_driver serial8250_reg; |
| 2614 | static struct console serial8250_console = { |
| 2615 | .name = "ttyS", |
| 2616 | .write = serial8250_console_write, |
| 2617 | .device = uart_console_device, |
| 2618 | .setup = serial8250_console_setup, |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 2619 | .early_setup = serial8250_console_early_setup, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | .flags = CON_PRINTBUFFER, |
| 2621 | .index = -1, |
| 2622 | .data = &serial8250_reg, |
| 2623 | }; |
| 2624 | |
| 2625 | static int __init serial8250_console_init(void) |
| 2626 | { |
| 2627 | serial8250_isa_init_ports(); |
| 2628 | register_console(&serial8250_console); |
| 2629 | return 0; |
| 2630 | } |
| 2631 | console_initcall(serial8250_console_init); |
| 2632 | |
Yinghai Lu | 18a8bd9 | 2007-07-15 23:37:59 -0700 | [diff] [blame] | 2633 | int serial8250_find_port(struct uart_port *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | { |
| 2635 | int line; |
| 2636 | struct uart_port *port; |
| 2637 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2638 | for (line = 0; line < nr_uarts; line++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | port = &serial8250_ports[line].port; |
Russell King | 50aec3b5 | 2006-01-04 18:13:03 +0000 | [diff] [blame] | 2640 | if (uart_match_port(p, port)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2641 | return line; |
| 2642 | } |
| 2643 | return -ENODEV; |
| 2644 | } |
| 2645 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | #define SERIAL8250_CONSOLE &serial8250_console |
| 2647 | #else |
| 2648 | #define SERIAL8250_CONSOLE NULL |
| 2649 | #endif |
| 2650 | |
| 2651 | static struct uart_driver serial8250_reg = { |
| 2652 | .owner = THIS_MODULE, |
| 2653 | .driver_name = "serial", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | .dev_name = "ttyS", |
| 2655 | .major = TTY_MAJOR, |
| 2656 | .minor = 64, |
| 2657 | .nr = UART_NR, |
| 2658 | .cons = SERIAL8250_CONSOLE, |
| 2659 | }; |
| 2660 | |
Russell King | d856c66 | 2006-02-23 10:22:13 +0000 | [diff] [blame] | 2661 | /* |
| 2662 | * early_serial_setup - early registration for 8250 ports |
| 2663 | * |
| 2664 | * Setup an 8250 port structure prior to console initialisation. Use |
| 2665 | * after console initialisation will cause undefined behaviour. |
| 2666 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | int __init early_serial_setup(struct uart_port *port) |
| 2668 | { |
| 2669 | if (port->line >= ARRAY_SIZE(serial8250_ports)) |
| 2670 | return -ENODEV; |
| 2671 | |
| 2672 | serial8250_isa_init_ports(); |
| 2673 | serial8250_ports[port->line].port = *port; |
| 2674 | serial8250_ports[port->line].port.ops = &serial8250_pops; |
| 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | /** |
| 2679 | * serial8250_suspend_port - suspend one serial port |
| 2680 | * @line: serial line number |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | * |
| 2682 | * Suspend one serial port. |
| 2683 | */ |
| 2684 | void serial8250_suspend_port(int line) |
| 2685 | { |
| 2686 | uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port); |
| 2687 | } |
| 2688 | |
| 2689 | /** |
| 2690 | * serial8250_resume_port - resume one serial port |
| 2691 | * @line: serial line number |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2692 | * |
| 2693 | * Resume one serial port. |
| 2694 | */ |
| 2695 | void serial8250_resume_port(int line) |
| 2696 | { |
David Woodhouse | b5b82df | 2007-05-17 14:27:39 +0800 | [diff] [blame] | 2697 | struct uart_8250_port *up = &serial8250_ports[line]; |
| 2698 | |
| 2699 | if (up->capabilities & UART_NATSEMI) { |
| 2700 | unsigned char tmp; |
| 2701 | |
| 2702 | /* Ensure it's still in high speed mode */ |
| 2703 | serial_outp(up, UART_LCR, 0xE0); |
| 2704 | |
| 2705 | tmp = serial_in(up, 0x04); /* EXCR2 */ |
| 2706 | tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */ |
| 2707 | tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */ |
| 2708 | serial_outp(up, 0x04, tmp); |
| 2709 | |
| 2710 | serial_outp(up, UART_LCR, 0); |
| 2711 | } |
| 2712 | uart_resume_port(&serial8250_reg, &up->port); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | /* |
| 2716 | * Register a set of serial devices attached to a platform device. The |
| 2717 | * list is terminated with a zero flags entry, which means we expect |
| 2718 | * all entries to have at least UPF_BOOT_AUTOCONF set. |
| 2719 | */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2720 | static int __devinit serial8250_probe(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2722 | struct plat_serial8250_port *p = dev->dev.platform_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2723 | struct uart_port port; |
Russell King | ec9f47c | 2005-06-27 11:12:54 +0100 | [diff] [blame] | 2724 | int ret, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | |
| 2726 | memset(&port, 0, sizeof(struct uart_port)); |
| 2727 | |
Russell King | ec9f47c | 2005-06-27 11:12:54 +0100 | [diff] [blame] | 2728 | for (i = 0; p && p->flags != 0; p++, i++) { |
Will Newton | 74a19741 | 2008-02-04 22:27:50 -0800 | [diff] [blame] | 2729 | port.iobase = p->iobase; |
| 2730 | port.membase = p->membase; |
| 2731 | port.irq = p->irq; |
| 2732 | port.uartclk = p->uartclk; |
| 2733 | port.regshift = p->regshift; |
| 2734 | port.iotype = p->iotype; |
| 2735 | port.flags = p->flags; |
| 2736 | port.mapbase = p->mapbase; |
| 2737 | port.hub6 = p->hub6; |
| 2738 | port.private_data = p->private_data; |
| 2739 | port.dev = &dev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | if (share_irqs) |
| 2741 | port.flags |= UPF_SHARE_IRQ; |
Russell King | ec9f47c | 2005-06-27 11:12:54 +0100 | [diff] [blame] | 2742 | ret = serial8250_register_port(&port); |
| 2743 | if (ret < 0) { |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2744 | dev_err(&dev->dev, "unable to register port at index %d " |
Josh Boyer | 4f640ef | 2007-07-23 18:43:44 -0700 | [diff] [blame] | 2745 | "(IO%lx MEM%llx IRQ%d): %d\n", i, |
| 2746 | p->iobase, (unsigned long long)p->mapbase, |
| 2747 | p->irq, ret); |
Russell King | ec9f47c | 2005-06-27 11:12:54 +0100 | [diff] [blame] | 2748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | } |
| 2750 | return 0; |
| 2751 | } |
| 2752 | |
| 2753 | /* |
| 2754 | * Remove serial ports registered against a platform device. |
| 2755 | */ |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2756 | static int __devexit serial8250_remove(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2757 | { |
| 2758 | int i; |
| 2759 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2760 | for (i = 0; i < nr_uarts; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | struct uart_8250_port *up = &serial8250_ports[i]; |
| 2762 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2763 | if (up->port.dev == &dev->dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | serial8250_unregister_port(i); |
| 2765 | } |
| 2766 | return 0; |
| 2767 | } |
| 2768 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2769 | static int serial8250_suspend(struct platform_device *dev, pm_message_t state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | { |
| 2771 | int i; |
| 2772 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | for (i = 0; i < UART_NR; i++) { |
| 2774 | struct uart_8250_port *up = &serial8250_ports[i]; |
| 2775 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2776 | if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | uart_suspend_port(&serial8250_reg, &up->port); |
| 2778 | } |
| 2779 | |
| 2780 | return 0; |
| 2781 | } |
| 2782 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2783 | static int serial8250_resume(struct platform_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2784 | { |
| 2785 | int i; |
| 2786 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | for (i = 0; i < UART_NR; i++) { |
| 2788 | struct uart_8250_port *up = &serial8250_ports[i]; |
| 2789 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2790 | if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) |
David Woodhouse | b5b82df | 2007-05-17 14:27:39 +0800 | [diff] [blame] | 2791 | serial8250_resume_port(i); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
| 2794 | return 0; |
| 2795 | } |
| 2796 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2797 | static struct platform_driver serial8250_isa_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | .probe = serial8250_probe, |
| 2799 | .remove = __devexit_p(serial8250_remove), |
| 2800 | .suspend = serial8250_suspend, |
| 2801 | .resume = serial8250_resume, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2802 | .driver = { |
| 2803 | .name = "serial8250", |
Dmitry Torokhov | 7493a31 | 2006-01-13 22:06:43 +0000 | [diff] [blame] | 2804 | .owner = THIS_MODULE, |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2805 | }, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2806 | }; |
| 2807 | |
| 2808 | /* |
| 2809 | * This "device" covers _all_ ISA 8250-compatible serial devices listed |
| 2810 | * in the table in include/asm/serial.h |
| 2811 | */ |
| 2812 | static struct platform_device *serial8250_isa_devs; |
| 2813 | |
| 2814 | /* |
| 2815 | * serial8250_register_port and serial8250_unregister_port allows for |
| 2816 | * 16x50 serial ports to be configured at run-time, to support PCMCIA |
| 2817 | * modems and PCI multiport cards. |
| 2818 | */ |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2819 | static DEFINE_MUTEX(serial_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2820 | |
| 2821 | static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port) |
| 2822 | { |
| 2823 | int i; |
| 2824 | |
| 2825 | /* |
| 2826 | * First, find a port entry which matches. |
| 2827 | */ |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2828 | for (i = 0; i < nr_uarts; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | if (uart_match_port(&serial8250_ports[i].port, port)) |
| 2830 | return &serial8250_ports[i]; |
| 2831 | |
| 2832 | /* |
| 2833 | * We didn't find a matching entry, so look for the first |
| 2834 | * free entry. We look for one which hasn't been previously |
| 2835 | * used (indicated by zero iobase). |
| 2836 | */ |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2837 | for (i = 0; i < nr_uarts; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2838 | if (serial8250_ports[i].port.type == PORT_UNKNOWN && |
| 2839 | serial8250_ports[i].port.iobase == 0) |
| 2840 | return &serial8250_ports[i]; |
| 2841 | |
| 2842 | /* |
| 2843 | * That also failed. Last resort is to find any entry which |
| 2844 | * doesn't have a real port associated with it. |
| 2845 | */ |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2846 | for (i = 0; i < nr_uarts; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2847 | if (serial8250_ports[i].port.type == PORT_UNKNOWN) |
| 2848 | return &serial8250_ports[i]; |
| 2849 | |
| 2850 | return NULL; |
| 2851 | } |
| 2852 | |
| 2853 | /** |
| 2854 | * serial8250_register_port - register a serial port |
| 2855 | * @port: serial port template |
| 2856 | * |
| 2857 | * Configure the serial port specified by the request. If the |
| 2858 | * port exists and is in use, it is hung up and unregistered |
| 2859 | * first. |
| 2860 | * |
| 2861 | * The port is then probed and if necessary the IRQ is autodetected |
| 2862 | * If this fails an error is returned. |
| 2863 | * |
| 2864 | * On success the port is ready to use and the line number is returned. |
| 2865 | */ |
| 2866 | int serial8250_register_port(struct uart_port *port) |
| 2867 | { |
| 2868 | struct uart_8250_port *uart; |
| 2869 | int ret = -ENOSPC; |
| 2870 | |
| 2871 | if (port->uartclk == 0) |
| 2872 | return -EINVAL; |
| 2873 | |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2874 | mutex_lock(&serial_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2875 | |
| 2876 | uart = serial8250_find_match_or_unused(port); |
| 2877 | if (uart) { |
| 2878 | uart_remove_one_port(&serial8250_reg, &uart->port); |
| 2879 | |
Will Newton | 74a19741 | 2008-02-04 22:27:50 -0800 | [diff] [blame] | 2880 | uart->port.iobase = port->iobase; |
| 2881 | uart->port.membase = port->membase; |
| 2882 | uart->port.irq = port->irq; |
| 2883 | uart->port.uartclk = port->uartclk; |
| 2884 | uart->port.fifosize = port->fifosize; |
| 2885 | uart->port.regshift = port->regshift; |
| 2886 | uart->port.iotype = port->iotype; |
| 2887 | uart->port.flags = port->flags | UPF_BOOT_AUTOCONF; |
| 2888 | uart->port.mapbase = port->mapbase; |
| 2889 | uart->port.private_data = port->private_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | if (port->dev) |
| 2891 | uart->port.dev = port->dev; |
| 2892 | |
| 2893 | ret = uart_add_one_port(&serial8250_reg, &uart->port); |
| 2894 | if (ret == 0) |
| 2895 | ret = uart->port.line; |
| 2896 | } |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2897 | mutex_unlock(&serial_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2898 | |
| 2899 | return ret; |
| 2900 | } |
| 2901 | EXPORT_SYMBOL(serial8250_register_port); |
| 2902 | |
| 2903 | /** |
| 2904 | * serial8250_unregister_port - remove a 16x50 serial port at runtime |
| 2905 | * @line: serial line number |
| 2906 | * |
| 2907 | * Remove one serial port. This may not be called from interrupt |
| 2908 | * context. We hand the port back to the our control. |
| 2909 | */ |
| 2910 | void serial8250_unregister_port(int line) |
| 2911 | { |
| 2912 | struct uart_8250_port *uart = &serial8250_ports[line]; |
| 2913 | |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2914 | mutex_lock(&serial_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | uart_remove_one_port(&serial8250_reg, &uart->port); |
| 2916 | if (serial8250_isa_devs) { |
| 2917 | uart->port.flags &= ~UPF_BOOT_AUTOCONF; |
| 2918 | uart->port.type = PORT_UNKNOWN; |
| 2919 | uart->port.dev = &serial8250_isa_devs->dev; |
| 2920 | uart_add_one_port(&serial8250_reg, &uart->port); |
| 2921 | } else { |
| 2922 | uart->port.dev = NULL; |
| 2923 | } |
Arjan van de Ven | f392ecf | 2006-01-12 18:44:32 +0000 | [diff] [blame] | 2924 | mutex_unlock(&serial_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | } |
| 2926 | EXPORT_SYMBOL(serial8250_unregister_port); |
| 2927 | |
| 2928 | static int __init serial8250_init(void) |
| 2929 | { |
| 2930 | int ret, i; |
| 2931 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2932 | if (nr_uarts > UART_NR) |
| 2933 | nr_uarts = UART_NR; |
| 2934 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | printk(KERN_INFO "Serial: 8250/16550 driver $Revision: 1.90 $ " |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 2936 | "%d ports, IRQ sharing %sabled\n", nr_uarts, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2937 | share_irqs ? "en" : "dis"); |
| 2938 | |
| 2939 | for (i = 0; i < NR_IRQS; i++) |
| 2940 | spin_lock_init(&irq_lists[i].lock); |
| 2941 | |
| 2942 | ret = uart_register_driver(&serial8250_reg); |
| 2943 | if (ret) |
| 2944 | goto out; |
| 2945 | |
Dmitry Torokhov | 7493a31 | 2006-01-13 22:06:43 +0000 | [diff] [blame] | 2946 | serial8250_isa_devs = platform_device_alloc("serial8250", |
| 2947 | PLAT8250_DEV_LEGACY); |
| 2948 | if (!serial8250_isa_devs) { |
| 2949 | ret = -ENOMEM; |
Russell King | bc965a7 | 2006-01-18 09:54:29 +0000 | [diff] [blame] | 2950 | goto unreg_uart_drv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | } |
| 2952 | |
Dmitry Torokhov | 7493a31 | 2006-01-13 22:06:43 +0000 | [diff] [blame] | 2953 | ret = platform_device_add(serial8250_isa_devs); |
| 2954 | if (ret) |
| 2955 | goto put_dev; |
| 2956 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev); |
| 2958 | |
Russell King | bc965a7 | 2006-01-18 09:54:29 +0000 | [diff] [blame] | 2959 | ret = platform_driver_register(&serial8250_isa_driver); |
| 2960 | if (ret == 0) |
| 2961 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2962 | |
Russell King | bc965a7 | 2006-01-18 09:54:29 +0000 | [diff] [blame] | 2963 | platform_device_del(serial8250_isa_devs); |
Dmitry Torokhov | 7493a31 | 2006-01-13 22:06:43 +0000 | [diff] [blame] | 2964 | put_dev: |
| 2965 | platform_device_put(serial8250_isa_devs); |
Dmitry Torokhov | 7493a31 | 2006-01-13 22:06:43 +0000 | [diff] [blame] | 2966 | unreg_uart_drv: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2967 | uart_unregister_driver(&serial8250_reg); |
| 2968 | out: |
| 2969 | return ret; |
| 2970 | } |
| 2971 | |
| 2972 | static void __exit serial8250_exit(void) |
| 2973 | { |
| 2974 | struct platform_device *isa_dev = serial8250_isa_devs; |
| 2975 | |
| 2976 | /* |
| 2977 | * This tells serial8250_unregister_port() not to re-register |
| 2978 | * the ports (thereby making serial8250_isa_driver permanently |
| 2979 | * in use.) |
| 2980 | */ |
| 2981 | serial8250_isa_devs = NULL; |
| 2982 | |
Russell King | 3ae5eae | 2005-11-09 22:32:44 +0000 | [diff] [blame] | 2983 | platform_driver_unregister(&serial8250_isa_driver); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2984 | platform_device_unregister(isa_dev); |
| 2985 | |
| 2986 | uart_unregister_driver(&serial8250_reg); |
| 2987 | } |
| 2988 | |
| 2989 | module_init(serial8250_init); |
| 2990 | module_exit(serial8250_exit); |
| 2991 | |
| 2992 | EXPORT_SYMBOL(serial8250_suspend_port); |
| 2993 | EXPORT_SYMBOL(serial8250_resume_port); |
| 2994 | |
| 2995 | MODULE_LICENSE("GPL"); |
| 2996 | MODULE_DESCRIPTION("Generic 8250/16x50 serial driver $Revision: 1.90 $"); |
| 2997 | |
| 2998 | module_param(share_irqs, uint, 0644); |
| 2999 | MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices" |
| 3000 | " (unsafe)"); |
| 3001 | |
Dave Jones | a61c2d7 | 2006-01-07 23:18:19 +0000 | [diff] [blame] | 3002 | module_param(nr_uarts, uint, 0644); |
| 3003 | MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")"); |
| 3004 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | #ifdef CONFIG_SERIAL_8250_RSA |
| 3006 | module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444); |
| 3007 | MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA"); |
| 3008 | #endif |
| 3009 | MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR); |