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