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