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