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