GNU Linux-libre 4.9.332-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_RX_TIMEOUT:
1802                 serial8250_rx_dma_flush(up);
1803                 /* fall-through */
1804         case UART_IIR_RLSI:
1805                 return true;
1806         }
1807         return up->dma->rx_dma(up);
1808 }
1809
1810 /*
1811  * This handles the interrupt from one port.
1812  */
1813 int serial8250_handle_irq(struct uart_port *port, unsigned int iir)
1814 {
1815         unsigned char status;
1816         unsigned long flags;
1817         struct uart_8250_port *up = up_to_u8250p(port);
1818         bool skip_rx = false;
1819
1820         if (iir & UART_IIR_NO_INT)
1821                 return 0;
1822
1823         spin_lock_irqsave(&port->lock, flags);
1824
1825         status = serial_port_in(port, UART_LSR);
1826
1827         /*
1828          * If port is stopped and there are no error conditions in the
1829          * FIFO, then don't drain the FIFO, as this may lead to TTY buffer
1830          * overflow. Not servicing, RX FIFO would trigger auto HW flow
1831          * control when FIFO occupancy reaches preset threshold, thus
1832          * halting RX. This only works when auto HW flow control is
1833          * available.
1834          */
1835         if (!(status & (UART_LSR_FIFOE | UART_LSR_BRK_ERROR_BITS)) &&
1836             (port->status & (UPSTAT_AUTOCTS | UPSTAT_AUTORTS)) &&
1837             !(port->read_status_mask & UART_LSR_DR))
1838                 skip_rx = true;
1839
1840         if (status & (UART_LSR_DR | UART_LSR_BI) && !skip_rx) {
1841                 if (!up->dma || handle_rx_dma(up, iir))
1842                         status = serial8250_rx_chars(up, status);
1843         }
1844         serial8250_modem_status(up);
1845         if ((!up->dma || up->dma->tx_err) && (status & UART_LSR_THRE) &&
1846                 (up->ier & UART_IER_THRI))
1847                 serial8250_tx_chars(up);
1848
1849         spin_unlock_irqrestore(&port->lock, flags);
1850         return 1;
1851 }
1852 EXPORT_SYMBOL_GPL(serial8250_handle_irq);
1853
1854 static int serial8250_default_handle_irq(struct uart_port *port)
1855 {
1856         struct uart_8250_port *up = up_to_u8250p(port);
1857         unsigned int iir;
1858         int ret;
1859
1860         serial8250_rpm_get(up);
1861
1862         iir = serial_port_in(port, UART_IIR);
1863         ret = serial8250_handle_irq(port, iir);
1864
1865         serial8250_rpm_put(up);
1866         return ret;
1867 }
1868
1869 /*
1870  * These Exar UARTs have an extra interrupt indicator that could
1871  * fire for a few unimplemented interrupts.  One of which is a
1872  * wakeup event when coming out of sleep.  Put this here just
1873  * to be on the safe side that these interrupts don't go unhandled.
1874  */
1875 static int exar_handle_irq(struct uart_port *port)
1876 {
1877         unsigned int iir = serial_port_in(port, UART_IIR);
1878         int ret;
1879
1880         ret = serial8250_handle_irq(port, iir);
1881
1882         if ((port->type == PORT_XR17V35X) ||
1883            (port->type == PORT_XR17D15X)) {
1884                 serial_port_in(port, 0x80);
1885                 serial_port_in(port, 0x81);
1886                 serial_port_in(port, 0x82);
1887                 serial_port_in(port, 0x83);
1888         }
1889
1890         return ret;
1891 }
1892
1893 /*
1894  * Newer 16550 compatible parts such as the SC16C650 & Altera 16550 Soft IP
1895  * have a programmable TX threshold that triggers the THRE interrupt in
1896  * the IIR register. In this case, the THRE interrupt indicates the FIFO
1897  * has space available. Load it up with tx_loadsz bytes.
1898  */
1899 static int serial8250_tx_threshold_handle_irq(struct uart_port *port)
1900 {
1901         unsigned long flags;
1902         unsigned int iir = serial_port_in(port, UART_IIR);
1903
1904         /* TX Threshold IRQ triggered so load up FIFO */
1905         if ((iir & UART_IIR_ID) == UART_IIR_THRI) {
1906                 struct uart_8250_port *up = up_to_u8250p(port);
1907
1908                 spin_lock_irqsave(&port->lock, flags);
1909                 serial8250_tx_chars(up);
1910                 spin_unlock_irqrestore(&port->lock, flags);
1911         }
1912
1913         iir = serial_port_in(port, UART_IIR);
1914         return serial8250_handle_irq(port, iir);
1915 }
1916
1917 static unsigned int serial8250_tx_empty(struct uart_port *port)
1918 {
1919         struct uart_8250_port *up = up_to_u8250p(port);
1920         unsigned long flags;
1921         unsigned int lsr;
1922
1923         serial8250_rpm_get(up);
1924
1925         spin_lock_irqsave(&port->lock, flags);
1926         lsr = serial_port_in(port, UART_LSR);
1927         up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
1928         spin_unlock_irqrestore(&port->lock, flags);
1929
1930         serial8250_rpm_put(up);
1931
1932         return (lsr & BOTH_EMPTY) == BOTH_EMPTY ? TIOCSER_TEMT : 0;
1933 }
1934
1935 unsigned int serial8250_do_get_mctrl(struct uart_port *port)
1936 {
1937         struct uart_8250_port *up = up_to_u8250p(port);
1938         unsigned int status;
1939         unsigned int ret;
1940
1941         serial8250_rpm_get(up);
1942         status = serial8250_modem_status(up);
1943         serial8250_rpm_put(up);
1944
1945         ret = 0;
1946         if (status & UART_MSR_DCD)
1947                 ret |= TIOCM_CAR;
1948         if (status & UART_MSR_RI)
1949                 ret |= TIOCM_RNG;
1950         if (status & UART_MSR_DSR)
1951                 ret |= TIOCM_DSR;
1952         if (status & UART_MSR_CTS)
1953                 ret |= TIOCM_CTS;
1954         return ret;
1955 }
1956 EXPORT_SYMBOL_GPL(serial8250_do_get_mctrl);
1957
1958 static unsigned int serial8250_get_mctrl(struct uart_port *port)
1959 {
1960         if (port->get_mctrl)
1961                 return port->get_mctrl(port);
1962         return serial8250_do_get_mctrl(port);
1963 }
1964
1965 void serial8250_do_set_mctrl(struct uart_port *port, unsigned int mctrl)
1966 {
1967         struct uart_8250_port *up = up_to_u8250p(port);
1968         unsigned char mcr = 0;
1969
1970         if (mctrl & TIOCM_RTS)
1971                 mcr |= UART_MCR_RTS;
1972         if (mctrl & TIOCM_DTR)
1973                 mcr |= UART_MCR_DTR;
1974         if (mctrl & TIOCM_OUT1)
1975                 mcr |= UART_MCR_OUT1;
1976         if (mctrl & TIOCM_OUT2)
1977                 mcr |= UART_MCR_OUT2;
1978         if (mctrl & TIOCM_LOOP)
1979                 mcr |= UART_MCR_LOOP;
1980
1981         mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr;
1982
1983         serial8250_out_MCR(up, mcr);
1984 }
1985 EXPORT_SYMBOL_GPL(serial8250_do_set_mctrl);
1986
1987 static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
1988 {
1989         if (port->set_mctrl)
1990                 port->set_mctrl(port, mctrl);
1991         else
1992                 serial8250_do_set_mctrl(port, mctrl);
1993 }
1994
1995 static void serial8250_break_ctl(struct uart_port *port, int break_state)
1996 {
1997         struct uart_8250_port *up = up_to_u8250p(port);
1998         unsigned long flags;
1999
2000         serial8250_rpm_get(up);
2001         spin_lock_irqsave(&port->lock, flags);
2002         if (break_state == -1)
2003                 up->lcr |= UART_LCR_SBC;
2004         else
2005                 up->lcr &= ~UART_LCR_SBC;
2006         serial_port_out(port, UART_LCR, up->lcr);
2007         spin_unlock_irqrestore(&port->lock, flags);
2008         serial8250_rpm_put(up);
2009 }
2010
2011 /*
2012  *      Wait for transmitter & holding register to empty
2013  */
2014 static void wait_for_xmitr(struct uart_8250_port *up, int bits)
2015 {
2016         unsigned int status, tmout = 10000;
2017
2018         /* Wait up to 10ms for the character(s) to be sent. */
2019         for (;;) {
2020                 status = serial_in(up, UART_LSR);
2021
2022                 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
2023
2024                 if ((status & bits) == bits)
2025                         break;
2026                 if (--tmout == 0)
2027                         break;
2028                 udelay(1);
2029                 touch_nmi_watchdog();
2030         }
2031
2032         /* Wait up to 1s for flow control if necessary */
2033         if (up->port.flags & UPF_CONS_FLOW) {
2034                 for (tmout = 1000000; tmout; tmout--) {
2035                         unsigned int msr = serial_in(up, UART_MSR);
2036                         up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
2037                         if (msr & UART_MSR_CTS)
2038                                 break;
2039                         udelay(1);
2040                         touch_nmi_watchdog();
2041                 }
2042         }
2043 }
2044
2045 #ifdef CONFIG_CONSOLE_POLL
2046 /*
2047  * Console polling routines for writing and reading from the uart while
2048  * in an interrupt or debug context.
2049  */
2050
2051 static int serial8250_get_poll_char(struct uart_port *port)
2052 {
2053         struct uart_8250_port *up = up_to_u8250p(port);
2054         unsigned char lsr;
2055         int status;
2056
2057         serial8250_rpm_get(up);
2058
2059         lsr = serial_port_in(port, UART_LSR);
2060
2061         if (!(lsr & UART_LSR_DR)) {
2062                 status = NO_POLL_CHAR;
2063                 goto out;
2064         }
2065
2066         status = serial_port_in(port, UART_RX);
2067 out:
2068         serial8250_rpm_put(up);
2069         return status;
2070 }
2071
2072
2073 static void serial8250_put_poll_char(struct uart_port *port,
2074                          unsigned char c)
2075 {
2076         unsigned int ier;
2077         struct uart_8250_port *up = up_to_u8250p(port);
2078
2079         serial8250_rpm_get(up);
2080         /*
2081          *      First save the IER then disable the interrupts
2082          */
2083         ier = serial_port_in(port, UART_IER);
2084         if (up->capabilities & UART_CAP_UUE)
2085                 serial_port_out(port, UART_IER, UART_IER_UUE);
2086         else
2087                 serial_port_out(port, UART_IER, 0);
2088
2089         wait_for_xmitr(up, BOTH_EMPTY);
2090         /*
2091          *      Send the character out.
2092          */
2093         serial_port_out(port, UART_TX, c);
2094
2095         /*
2096          *      Finally, wait for transmitter to become empty
2097          *      and restore the IER
2098          */
2099         wait_for_xmitr(up, BOTH_EMPTY);
2100         serial_port_out(port, UART_IER, ier);
2101         serial8250_rpm_put(up);
2102 }
2103
2104 #endif /* CONFIG_CONSOLE_POLL */
2105
2106 int serial8250_do_startup(struct uart_port *port)
2107 {
2108         struct uart_8250_port *up = up_to_u8250p(port);
2109         unsigned long flags;
2110         unsigned char lsr, iir;
2111         int retval;
2112
2113         if (!port->fifosize)
2114                 port->fifosize = uart_config[port->type].fifo_size;
2115         if (!up->tx_loadsz)
2116                 up->tx_loadsz = uart_config[port->type].tx_loadsz;
2117         if (!up->capabilities)
2118                 up->capabilities = uart_config[port->type].flags;
2119         up->mcr = 0;
2120
2121         if (port->iotype != up->cur_iotype)
2122                 set_io_from_upio(port);
2123
2124         serial8250_rpm_get(up);
2125         if (port->type == PORT_16C950) {
2126                 /* Wake up and initialize UART */
2127                 up->acr = 0;
2128                 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2129                 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2130                 serial_port_out(port, UART_IER, 0);
2131                 serial_port_out(port, UART_LCR, 0);
2132                 serial_icr_write(up, UART_CSR, 0); /* Reset the UART */
2133                 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2134                 serial_port_out(port, UART_EFR, UART_EFR_ECB);
2135                 serial_port_out(port, UART_LCR, 0);
2136         }
2137
2138 #ifdef CONFIG_SERIAL_8250_RSA
2139         /*
2140          * If this is an RSA port, see if we can kick it up to the
2141          * higher speed clock.
2142          */
2143         enable_rsa(up);
2144 #endif
2145
2146         if (port->type == PORT_XR17V35X) {
2147                 /*
2148                  * First enable access to IER [7:5], ISR [5:4], FCR [5:4],
2149                  * MCR [7:5] and MSR [7:0]
2150                  */
2151                 serial_port_out(port, UART_XR_EFR, UART_EFR_ECB);
2152
2153                 /*
2154                  * Make sure all interrups are masked until initialization is
2155                  * complete and the FIFOs are cleared
2156                  */
2157                 serial_port_out(port, UART_IER, 0);
2158         }
2159
2160         /*
2161          * Clear the FIFO buffers and disable them.
2162          * (they will be reenabled in set_termios())
2163          */
2164         serial8250_clear_fifos(up);
2165
2166         /*
2167          * Clear the interrupt registers.
2168          */
2169         serial_port_in(port, UART_LSR);
2170         serial_port_in(port, UART_RX);
2171         serial_port_in(port, UART_IIR);
2172         serial_port_in(port, UART_MSR);
2173
2174         /*
2175          * At this point, there's no way the LSR could still be 0xff;
2176          * if it is, then bail out, because there's likely no UART
2177          * here.
2178          */
2179         if (!(port->flags & UPF_BUGGY_UART) &&
2180             (serial_port_in(port, UART_LSR) == 0xff)) {
2181                 printk_ratelimited(KERN_INFO "ttyS%d: LSR safety check engaged!\n",
2182                                    serial_index(port));
2183                 retval = -ENODEV;
2184                 goto out;
2185         }
2186
2187         /*
2188          * For a XR16C850, we need to set the trigger levels
2189          */
2190         if (port->type == PORT_16850) {
2191                 unsigned char fctr;
2192
2193                 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
2194
2195                 fctr = serial_in(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX);
2196                 serial_port_out(port, UART_FCTR,
2197                                 fctr | UART_FCTR_TRGD | UART_FCTR_RX);
2198                 serial_port_out(port, UART_TRG, UART_TRG_96);
2199                 serial_port_out(port, UART_FCTR,
2200                                 fctr | UART_FCTR_TRGD | UART_FCTR_TX);
2201                 serial_port_out(port, UART_TRG, UART_TRG_96);
2202
2203                 serial_port_out(port, UART_LCR, 0);
2204         }
2205
2206         /*
2207          * For the Altera 16550 variants, set TX threshold trigger level.
2208          */
2209         if (((port->type == PORT_ALTR_16550_F32) ||
2210              (port->type == PORT_ALTR_16550_F64) ||
2211              (port->type == PORT_ALTR_16550_F128)) && (port->fifosize > 1)) {
2212                 /* Bounds checking of TX threshold (valid 0 to fifosize-2) */
2213                 if ((up->tx_loadsz < 2) || (up->tx_loadsz > port->fifosize)) {
2214                         pr_err("ttyS%d TX FIFO Threshold errors, skipping\n",
2215                                serial_index(port));
2216                 } else {
2217                         serial_port_out(port, UART_ALTR_AFR,
2218                                         UART_ALTR_EN_TXFIFO_LW);
2219                         serial_port_out(port, UART_ALTR_TX_LOW,
2220                                         port->fifosize - up->tx_loadsz);
2221                         port->handle_irq = serial8250_tx_threshold_handle_irq;
2222                 }
2223         }
2224
2225         /* Check if we need to have shared IRQs */
2226         if (port->irq && (up->port.flags & UPF_SHARE_IRQ))
2227                 up->port.irqflags |= IRQF_SHARED;
2228
2229         if (port->irq) {
2230                 unsigned char iir1;
2231
2232                 if (port->irqflags & IRQF_SHARED)
2233                         disable_irq_nosync(port->irq);
2234
2235                 /*
2236                  * Test for UARTs that do not reassert THRE when the
2237                  * transmitter is idle and the interrupt has already
2238                  * been cleared.  Real 16550s should always reassert
2239                  * this interrupt whenever the transmitter is idle and
2240                  * the interrupt is enabled.  Delays are necessary to
2241                  * allow register changes to become visible.
2242                  */
2243                 spin_lock_irqsave(&port->lock, flags);
2244
2245                 wait_for_xmitr(up, UART_LSR_THRE);
2246                 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2247                 udelay(1); /* allow THRE to set */
2248                 iir1 = serial_port_in(port, UART_IIR);
2249                 serial_port_out(port, UART_IER, 0);
2250                 serial_port_out_sync(port, UART_IER, UART_IER_THRI);
2251                 udelay(1); /* allow a working UART time to re-assert THRE */
2252                 iir = serial_port_in(port, UART_IIR);
2253                 serial_port_out(port, UART_IER, 0);
2254
2255                 spin_unlock_irqrestore(&port->lock, flags);
2256
2257                 if (port->irqflags & IRQF_SHARED)
2258                         enable_irq(port->irq);
2259
2260                 /*
2261                  * If the interrupt is not reasserted, or we otherwise
2262                  * don't trust the iir, setup a timer to kick the UART
2263                  * on a regular basis.
2264                  */
2265                 if ((!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) ||
2266                     up->port.flags & UPF_BUG_THRE) {
2267                         up->bugs |= UART_BUG_THRE;
2268                 }
2269         }
2270
2271         retval = up->ops->setup_irq(up);
2272         if (retval)
2273                 goto out;
2274
2275         /*
2276          * Now, initialize the UART
2277          */
2278         serial_port_out(port, UART_LCR, UART_LCR_WLEN8);
2279
2280         spin_lock_irqsave(&port->lock, flags);
2281         if (up->port.flags & UPF_FOURPORT) {
2282                 if (!up->port.irq)
2283                         up->port.mctrl |= TIOCM_OUT1;
2284         } else
2285                 /*
2286                  * Most PC uarts need OUT2 raised to enable interrupts.
2287                  */
2288                 if (port->irq)
2289                         up->port.mctrl |= TIOCM_OUT2;
2290
2291         serial8250_set_mctrl(port, port->mctrl);
2292
2293         /*
2294          * Serial over Lan (SoL) hack:
2295          * Intel 8257x Gigabit ethernet chips have a 16550 emulation, to be
2296          * used for Serial Over Lan.  Those chips take a longer time than a
2297          * normal serial device to signalize that a transmission data was
2298          * queued. Due to that, the above test generally fails. One solution
2299          * would be to delay the reading of iir. However, this is not
2300          * reliable, since the timeout is variable. So, let's just don't
2301          * test if we receive TX irq.  This way, we'll never enable
2302          * UART_BUG_TXEN.
2303          */
2304         if (up->port.flags & UPF_NO_TXEN_TEST)
2305                 goto dont_test_tx_en;
2306
2307         /*
2308          * Do a quick test to see if we receive an interrupt when we enable
2309          * the TX irq.
2310          */
2311         serial_port_out(port, UART_IER, UART_IER_THRI);
2312         lsr = serial_port_in(port, UART_LSR);
2313         iir = serial_port_in(port, UART_IIR);
2314         serial_port_out(port, UART_IER, 0);
2315
2316         if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) {
2317                 if (!(up->bugs & UART_BUG_TXEN)) {
2318                         up->bugs |= UART_BUG_TXEN;
2319                         pr_debug("ttyS%d - enabling bad tx status workarounds\n",
2320                                  serial_index(port));
2321                 }
2322         } else {
2323                 up->bugs &= ~UART_BUG_TXEN;
2324         }
2325
2326 dont_test_tx_en:
2327         spin_unlock_irqrestore(&port->lock, flags);
2328
2329         /*
2330          * Clear the interrupt registers again for luck, and clear the
2331          * saved flags to avoid getting false values from polling
2332          * routines or the previous session.
2333          */
2334         serial_port_in(port, UART_LSR);
2335         serial_port_in(port, UART_RX);
2336         serial_port_in(port, UART_IIR);
2337         serial_port_in(port, UART_MSR);
2338         up->lsr_saved_flags = 0;
2339         up->msr_saved_flags = 0;
2340
2341         /*
2342          * Request DMA channels for both RX and TX.
2343          */
2344         if (up->dma) {
2345                 retval = serial8250_request_dma(up);
2346                 if (retval) {
2347                         pr_warn_ratelimited("ttyS%d - failed to request DMA\n",
2348                                             serial_index(port));
2349                         up->dma = NULL;
2350                 }
2351         }
2352
2353         /*
2354          * Set the IER shadow for rx interrupts but defer actual interrupt
2355          * enable until after the FIFOs are enabled; otherwise, an already-
2356          * active sender can swamp the interrupt handler with "too much work".
2357          */
2358         up->ier = UART_IER_RLSI | UART_IER_RDI;
2359
2360         if (port->flags & UPF_FOURPORT) {
2361                 unsigned int icp;
2362                 /*
2363                  * Enable interrupts on the AST Fourport board
2364                  */
2365                 icp = (port->iobase & 0xfe0) | 0x01f;
2366                 outb_p(0x80, icp);
2367                 inb_p(icp);
2368         }
2369         retval = 0;
2370 out:
2371         serial8250_rpm_put(up);
2372         return retval;
2373 }
2374 EXPORT_SYMBOL_GPL(serial8250_do_startup);
2375
2376 static int serial8250_startup(struct uart_port *port)
2377 {
2378         if (port->startup)
2379                 return port->startup(port);
2380         return serial8250_do_startup(port);
2381 }
2382
2383 void serial8250_do_shutdown(struct uart_port *port)
2384 {
2385         struct uart_8250_port *up = up_to_u8250p(port);
2386         unsigned long flags;
2387
2388         serial8250_rpm_get(up);
2389         /*
2390          * Disable interrupts from this port
2391          */
2392         spin_lock_irqsave(&port->lock, flags);
2393         up->ier = 0;
2394         serial_port_out(port, UART_IER, 0);
2395         spin_unlock_irqrestore(&port->lock, flags);
2396
2397         synchronize_irq(port->irq);
2398
2399         if (up->dma)
2400                 serial8250_release_dma(up);
2401
2402         spin_lock_irqsave(&port->lock, flags);
2403         if (port->flags & UPF_FOURPORT) {
2404                 /* reset interrupts on the AST Fourport board */
2405                 inb((port->iobase & 0xfe0) | 0x1f);
2406                 port->mctrl |= TIOCM_OUT1;
2407         } else
2408                 port->mctrl &= ~TIOCM_OUT2;
2409
2410         serial8250_set_mctrl(port, port->mctrl);
2411         spin_unlock_irqrestore(&port->lock, flags);
2412
2413         /*
2414          * Disable break condition and FIFOs
2415          */
2416         serial_port_out(port, UART_LCR,
2417                         serial_port_in(port, UART_LCR) & ~UART_LCR_SBC);
2418         serial8250_clear_fifos(up);
2419
2420 #ifdef CONFIG_SERIAL_8250_RSA
2421         /*
2422          * Reset the RSA board back to 115kbps compat mode.
2423          */
2424         disable_rsa(up);
2425 #endif
2426
2427         /*
2428          * Read data port to reset things, and then unlink from
2429          * the IRQ chain.
2430          */
2431         serial_port_in(port, UART_RX);
2432         serial8250_rpm_put(up);
2433
2434         up->ops->release_irq(up);
2435 }
2436 EXPORT_SYMBOL_GPL(serial8250_do_shutdown);
2437
2438 static void serial8250_shutdown(struct uart_port *port)
2439 {
2440         if (port->shutdown)
2441                 port->shutdown(port);
2442         else
2443                 serial8250_do_shutdown(port);
2444 }
2445
2446 /*
2447  * XR17V35x UARTs have an extra fractional divisor register (DLD)
2448  * Calculate divisor with extra 4-bit fractional portion
2449  */
2450 static unsigned int xr17v35x_get_divisor(struct uart_8250_port *up,
2451                                          unsigned int baud,
2452                                          unsigned int *frac)
2453 {
2454         struct uart_port *port = &up->port;
2455         unsigned int quot_16;
2456
2457         quot_16 = DIV_ROUND_CLOSEST(port->uartclk, baud);
2458         *frac = quot_16 & 0x0f;
2459
2460         return quot_16 >> 4;
2461 }
2462
2463 static unsigned int serial8250_get_divisor(struct uart_8250_port *up,
2464                                            unsigned int baud,
2465                                            unsigned int *frac)
2466 {
2467         struct uart_port *port = &up->port;
2468         unsigned int quot;
2469
2470         /*
2471          * Handle magic divisors for baud rates above baud_base on
2472          * SMSC SuperIO chips.
2473          *
2474          */
2475         if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2476             baud == (port->uartclk/4))
2477                 quot = 0x8001;
2478         else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
2479                  baud == (port->uartclk/8))
2480                 quot = 0x8002;
2481         else if (up->port.type == PORT_XR17V35X)
2482                 quot = xr17v35x_get_divisor(up, baud, frac);
2483         else
2484                 quot = uart_get_divisor(port, baud);
2485
2486         /*
2487          * Oxford Semi 952 rev B workaround
2488          */
2489         if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0)
2490                 quot++;
2491
2492         return quot;
2493 }
2494
2495 static unsigned char serial8250_compute_lcr(struct uart_8250_port *up,
2496                                             tcflag_t c_cflag)
2497 {
2498         unsigned char cval;
2499
2500         switch (c_cflag & CSIZE) {
2501         case CS5:
2502                 cval = UART_LCR_WLEN5;
2503                 break;
2504         case CS6:
2505                 cval = UART_LCR_WLEN6;
2506                 break;
2507         case CS7:
2508                 cval = UART_LCR_WLEN7;
2509                 break;
2510         default:
2511         case CS8:
2512                 cval = UART_LCR_WLEN8;
2513                 break;
2514         }
2515
2516         if (c_cflag & CSTOPB)
2517                 cval |= UART_LCR_STOP;
2518         if (c_cflag & PARENB) {
2519                 cval |= UART_LCR_PARITY;
2520                 if (up->bugs & UART_BUG_PARITY)
2521                         up->fifo_bug = true;
2522         }
2523         if (!(c_cflag & PARODD))
2524                 cval |= UART_LCR_EPAR;
2525 #ifdef CMSPAR
2526         if (c_cflag & CMSPAR)
2527                 cval |= UART_LCR_SPAR;
2528 #endif
2529
2530         return cval;
2531 }
2532
2533 static void serial8250_set_divisor(struct uart_port *port, unsigned int baud,
2534                             unsigned int quot, unsigned int quot_frac)
2535 {
2536         struct uart_8250_port *up = up_to_u8250p(port);
2537
2538         /* Workaround to enable 115200 baud on OMAP1510 internal ports */
2539         if (is_omap1510_8250(up)) {
2540                 if (baud == 115200) {
2541                         quot = 1;
2542                         serial_port_out(port, UART_OMAP_OSC_12M_SEL, 1);
2543                 } else
2544                         serial_port_out(port, UART_OMAP_OSC_12M_SEL, 0);
2545         }
2546
2547         /*
2548          * For NatSemi, switch to bank 2 not bank 1, to avoid resetting EXCR2,
2549          * otherwise just set DLAB
2550          */
2551         if (up->capabilities & UART_NATSEMI)
2552                 serial_port_out(port, UART_LCR, 0xe0);
2553         else
2554                 serial_port_out(port, UART_LCR, up->lcr | UART_LCR_DLAB);
2555
2556         serial_dl_write(up, quot);
2557
2558         /* XR17V35x UARTs have an extra fractional divisor register (DLD) */
2559         if (up->port.type == PORT_XR17V35X) {
2560                 /* Preserve bits not related to baudrate; DLD[7:4]. */
2561                 quot_frac |= serial_port_in(port, 0x2) & 0xf0;
2562                 serial_port_out(port, 0x2, quot_frac);
2563         }
2564 }
2565
2566 static unsigned int serial8250_get_baud_rate(struct uart_port *port,
2567                                              struct ktermios *termios,
2568                                              struct ktermios *old)
2569 {
2570         /*
2571          * Ask the core to calculate the divisor for us.
2572          * Allow 1% tolerance at the upper limit so uart clks marginally
2573          * slower than nominal still match standard baud rates without
2574          * causing transmission errors.
2575          */
2576         return uart_get_baud_rate(port, termios, old,
2577                                   port->uartclk / 16 / 0xffff,
2578                                   port->uartclk);
2579 }
2580
2581 void
2582 serial8250_do_set_termios(struct uart_port *port, struct ktermios *termios,
2583                           struct ktermios *old)
2584 {
2585         struct uart_8250_port *up = up_to_u8250p(port);
2586         unsigned char cval;
2587         unsigned long flags;
2588         unsigned int baud, quot, frac = 0;
2589
2590         cval = serial8250_compute_lcr(up, termios->c_cflag);
2591
2592         baud = serial8250_get_baud_rate(port, termios, old);
2593         quot = serial8250_get_divisor(up, baud, &frac);
2594
2595         /*
2596          * Ok, we're now changing the port state.  Do it with
2597          * interrupts disabled.
2598          */
2599         serial8250_rpm_get(up);
2600         spin_lock_irqsave(&port->lock, flags);
2601
2602         up->lcr = cval;                                 /* Save computed LCR */
2603
2604         if (up->capabilities & UART_CAP_FIFO && port->fifosize > 1) {
2605                 /* NOTE: If fifo_bug is not set, a user can set RX_trigger. */
2606                 if ((baud < 2400 && !up->dma) || up->fifo_bug) {
2607                         up->fcr &= ~UART_FCR_TRIGGER_MASK;
2608                         up->fcr |= UART_FCR_TRIGGER_1;
2609                 }
2610         }
2611
2612         /*
2613          * MCR-based auto flow control.  When AFE is enabled, RTS will be
2614          * deasserted when the receive FIFO contains more characters than
2615          * the trigger, or the MCR RTS bit is cleared.
2616          */
2617         if (up->capabilities & UART_CAP_AFE) {
2618                 up->mcr &= ~UART_MCR_AFE;
2619                 if (termios->c_cflag & CRTSCTS)
2620                         up->mcr |= UART_MCR_AFE;
2621         }
2622
2623         /*
2624          * Update the per-port timeout.
2625          */
2626         uart_update_timeout(port, termios->c_cflag, baud);
2627
2628         port->read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
2629         if (termios->c_iflag & INPCK)
2630                 port->read_status_mask |= UART_LSR_FE | UART_LSR_PE;
2631         if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
2632                 port->read_status_mask |= UART_LSR_BI;
2633
2634         /*
2635          * Characteres to ignore
2636          */
2637         port->ignore_status_mask = 0;
2638         if (termios->c_iflag & IGNPAR)
2639                 port->ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
2640         if (termios->c_iflag & IGNBRK) {
2641                 port->ignore_status_mask |= UART_LSR_BI;
2642                 /*
2643                  * If we're ignoring parity and break indicators,
2644                  * ignore overruns too (for real raw support).
2645                  */
2646                 if (termios->c_iflag & IGNPAR)
2647                         port->ignore_status_mask |= UART_LSR_OE;
2648         }
2649
2650         /*
2651          * ignore all characters if CREAD is not set
2652          */
2653         if ((termios->c_cflag & CREAD) == 0)
2654                 port->ignore_status_mask |= UART_LSR_DR;
2655
2656         /*
2657          * CTS flow control flag and modem status interrupts
2658          */
2659         up->ier &= ~UART_IER_MSI;
2660         if (!(up->bugs & UART_BUG_NOMSR) &&
2661                         UART_ENABLE_MS(&up->port, termios->c_cflag))
2662                 up->ier |= UART_IER_MSI;
2663         if (up->capabilities & UART_CAP_UUE)
2664                 up->ier |= UART_IER_UUE;
2665         if (up->capabilities & UART_CAP_RTOIE)
2666                 up->ier |= UART_IER_RTOIE;
2667
2668         serial_port_out(port, UART_IER, up->ier);
2669
2670         if (up->capabilities & UART_CAP_EFR) {
2671                 unsigned char efr = 0;
2672                 /*
2673                  * TI16C752/Startech hardware flow control.  FIXME:
2674                  * - TI16C752 requires control thresholds to be set.
2675                  * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled.
2676                  */
2677                 if (termios->c_cflag & CRTSCTS)
2678                         efr |= UART_EFR_CTS;
2679
2680                 serial_port_out(port, UART_LCR, UART_LCR_CONF_MODE_B);
2681                 if (port->flags & UPF_EXAR_EFR)
2682                         serial_port_out(port, UART_XR_EFR, efr);
2683                 else
2684                         serial_port_out(port, UART_EFR, efr);
2685         }
2686
2687         serial8250_set_divisor(port, baud, quot, frac);
2688
2689         /*
2690          * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR
2691          * is written without DLAB set, this mode will be disabled.
2692          */
2693         if (port->type == PORT_16750)
2694                 serial_port_out(port, UART_FCR, up->fcr);
2695
2696         serial_port_out(port, UART_LCR, up->lcr);       /* reset DLAB */
2697         if (port->type != PORT_16750) {
2698                 /* emulated UARTs (Lucent Venus 167x) need two steps */
2699                 if (up->fcr & UART_FCR_ENABLE_FIFO)
2700                         serial_port_out(port, UART_FCR, UART_FCR_ENABLE_FIFO);
2701                 serial_port_out(port, UART_FCR, up->fcr);       /* set fcr */
2702         }
2703         serial8250_set_mctrl(port, port->mctrl);
2704         spin_unlock_irqrestore(&port->lock, flags);
2705         serial8250_rpm_put(up);
2706
2707         /* Don't rewrite B0 */
2708         if (tty_termios_baud_rate(termios))
2709                 tty_termios_encode_baud_rate(termios, baud, baud);
2710 }
2711 EXPORT_SYMBOL(serial8250_do_set_termios);
2712
2713 static void
2714 serial8250_set_termios(struct uart_port *port, struct ktermios *termios,
2715                        struct ktermios *old)
2716 {
2717         if (port->set_termios)
2718                 port->set_termios(port, termios, old);
2719         else
2720                 serial8250_do_set_termios(port, termios, old);
2721 }
2722
2723 static void
2724 serial8250_set_ldisc(struct uart_port *port, struct ktermios *termios)
2725 {
2726         if (termios->c_line == N_PPS) {
2727                 port->flags |= UPF_HARDPPS_CD;
2728                 spin_lock_irq(&port->lock);
2729                 serial8250_enable_ms(port);
2730                 spin_unlock_irq(&port->lock);
2731         } else {
2732                 port->flags &= ~UPF_HARDPPS_CD;
2733                 if (!UART_ENABLE_MS(port, termios->c_cflag)) {
2734                         spin_lock_irq(&port->lock);
2735                         serial8250_disable_ms(port);
2736                         spin_unlock_irq(&port->lock);
2737                 }
2738         }
2739 }
2740
2741
2742 void serial8250_do_pm(struct uart_port *port, unsigned int state,
2743                       unsigned int oldstate)
2744 {
2745         struct uart_8250_port *p = up_to_u8250p(port);
2746
2747         serial8250_set_sleep(p, state != 0);
2748 }
2749 EXPORT_SYMBOL(serial8250_do_pm);
2750
2751 static void
2752 serial8250_pm(struct uart_port *port, unsigned int state,
2753               unsigned int oldstate)
2754 {
2755         if (port->pm)
2756                 port->pm(port, state, oldstate);
2757         else
2758                 serial8250_do_pm(port, state, oldstate);
2759 }
2760
2761 static unsigned int serial8250_port_size(struct uart_8250_port *pt)
2762 {
2763         if (pt->port.mapsize)
2764                 return pt->port.mapsize;
2765         if (pt->port.iotype == UPIO_AU) {
2766                 if (pt->port.type == PORT_RT2880)
2767                         return 0x100;
2768                 return 0x1000;
2769         }
2770         if (is_omap1_8250(pt))
2771                 return 0x16 << pt->port.regshift;
2772
2773         return 8 << pt->port.regshift;
2774 }
2775
2776 /*
2777  * Resource handling.
2778  */
2779 static int serial8250_request_std_resource(struct uart_8250_port *up)
2780 {
2781         unsigned int size = serial8250_port_size(up);
2782         struct uart_port *port = &up->port;
2783         int ret = 0;
2784
2785         switch (port->iotype) {
2786         case UPIO_AU:
2787         case UPIO_TSI:
2788         case UPIO_MEM32:
2789         case UPIO_MEM32BE:
2790         case UPIO_MEM16:
2791         case UPIO_MEM:
2792                 if (!port->mapbase) {
2793                         ret = -EINVAL;
2794                         break;
2795                 }
2796
2797                 if (!request_mem_region(port->mapbase, size, "serial")) {
2798                         ret = -EBUSY;
2799                         break;
2800                 }
2801
2802                 if (port->flags & UPF_IOREMAP) {
2803                         port->membase = ioremap_nocache(port->mapbase, size);
2804                         if (!port->membase) {
2805                                 release_mem_region(port->mapbase, size);
2806                                 ret = -ENOMEM;
2807                         }
2808                 }
2809                 break;
2810
2811         case UPIO_HUB6:
2812         case UPIO_PORT:
2813                 if (!request_region(port->iobase, size, "serial"))
2814                         ret = -EBUSY;
2815                 break;
2816         }
2817         return ret;
2818 }
2819
2820 static void serial8250_release_std_resource(struct uart_8250_port *up)
2821 {
2822         unsigned int size = serial8250_port_size(up);
2823         struct uart_port *port = &up->port;
2824
2825         switch (port->iotype) {
2826         case UPIO_AU:
2827         case UPIO_TSI:
2828         case UPIO_MEM32:
2829         case UPIO_MEM32BE:
2830         case UPIO_MEM16:
2831         case UPIO_MEM:
2832                 if (!port->mapbase)
2833                         break;
2834
2835                 if (port->flags & UPF_IOREMAP) {
2836                         iounmap(port->membase);
2837                         port->membase = NULL;
2838                 }
2839
2840                 release_mem_region(port->mapbase, size);
2841                 break;
2842
2843         case UPIO_HUB6:
2844         case UPIO_PORT:
2845                 release_region(port->iobase, size);
2846                 break;
2847         }
2848 }
2849
2850 static void serial8250_release_port(struct uart_port *port)
2851 {
2852         struct uart_8250_port *up = up_to_u8250p(port);
2853
2854         serial8250_release_std_resource(up);
2855 }
2856
2857 static int serial8250_request_port(struct uart_port *port)
2858 {
2859         struct uart_8250_port *up = up_to_u8250p(port);
2860
2861         return serial8250_request_std_resource(up);
2862 }
2863
2864 static int fcr_get_rxtrig_bytes(struct uart_8250_port *up)
2865 {
2866         const struct serial8250_config *conf_type = &uart_config[up->port.type];
2867         unsigned char bytes;
2868
2869         bytes = conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(up->fcr)];
2870
2871         return bytes ? bytes : -EOPNOTSUPP;
2872 }
2873
2874 static int bytes_to_fcr_rxtrig(struct uart_8250_port *up, unsigned char bytes)
2875 {
2876         const struct serial8250_config *conf_type = &uart_config[up->port.type];
2877         int i;
2878
2879         if (!conf_type->rxtrig_bytes[UART_FCR_R_TRIG_BITS(UART_FCR_R_TRIG_00)])
2880                 return -EOPNOTSUPP;
2881
2882         for (i = 1; i < UART_FCR_R_TRIG_MAX_STATE; i++) {
2883                 if (bytes < conf_type->rxtrig_bytes[i])
2884                         /* Use the nearest lower value */
2885                         return (--i) << UART_FCR_R_TRIG_SHIFT;
2886         }
2887
2888         return UART_FCR_R_TRIG_11;
2889 }
2890
2891 static int do_get_rxtrig(struct tty_port *port)
2892 {
2893         struct uart_state *state = container_of(port, struct uart_state, port);
2894         struct uart_port *uport = state->uart_port;
2895         struct uart_8250_port *up = up_to_u8250p(uport);
2896
2897         if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1)
2898                 return -EINVAL;
2899
2900         return fcr_get_rxtrig_bytes(up);
2901 }
2902
2903 static int do_serial8250_get_rxtrig(struct tty_port *port)
2904 {
2905         int rxtrig_bytes;
2906
2907         mutex_lock(&port->mutex);
2908         rxtrig_bytes = do_get_rxtrig(port);
2909         mutex_unlock(&port->mutex);
2910
2911         return rxtrig_bytes;
2912 }
2913
2914 static ssize_t serial8250_get_attr_rx_trig_bytes(struct device *dev,
2915         struct device_attribute *attr, char *buf)
2916 {
2917         struct tty_port *port = dev_get_drvdata(dev);
2918         int rxtrig_bytes;
2919
2920         rxtrig_bytes = do_serial8250_get_rxtrig(port);
2921         if (rxtrig_bytes < 0)
2922                 return rxtrig_bytes;
2923
2924         return snprintf(buf, PAGE_SIZE, "%d\n", rxtrig_bytes);
2925 }
2926
2927 static int do_set_rxtrig(struct tty_port *port, unsigned char bytes)
2928 {
2929         struct uart_state *state = container_of(port, struct uart_state, port);
2930         struct uart_port *uport = state->uart_port;
2931         struct uart_8250_port *up = up_to_u8250p(uport);
2932         int rxtrig;
2933
2934         if (!(up->capabilities & UART_CAP_FIFO) || uport->fifosize <= 1 ||
2935             up->fifo_bug)
2936                 return -EINVAL;
2937
2938         rxtrig = bytes_to_fcr_rxtrig(up, bytes);
2939         if (rxtrig < 0)
2940                 return rxtrig;
2941
2942         serial8250_clear_fifos(up);
2943         up->fcr &= ~UART_FCR_TRIGGER_MASK;
2944         up->fcr |= (unsigned char)rxtrig;
2945         serial_out(up, UART_FCR, up->fcr);
2946         return 0;
2947 }
2948
2949 static int do_serial8250_set_rxtrig(struct tty_port *port, unsigned char bytes)
2950 {
2951         int ret;
2952
2953         mutex_lock(&port->mutex);
2954         ret = do_set_rxtrig(port, bytes);
2955         mutex_unlock(&port->mutex);
2956
2957         return ret;
2958 }
2959
2960 static ssize_t serial8250_set_attr_rx_trig_bytes(struct device *dev,
2961         struct device_attribute *attr, const char *buf, size_t count)
2962 {
2963         struct tty_port *port = dev_get_drvdata(dev);
2964         unsigned char bytes;
2965         int ret;
2966
2967         if (!count)
2968                 return -EINVAL;
2969
2970         ret = kstrtou8(buf, 10, &bytes);
2971         if (ret < 0)
2972                 return ret;
2973
2974         ret = do_serial8250_set_rxtrig(port, bytes);
2975         if (ret < 0)
2976                 return ret;
2977
2978         return count;
2979 }
2980
2981 static DEVICE_ATTR(rx_trig_bytes, S_IRUSR | S_IWUSR | S_IRGRP,
2982                    serial8250_get_attr_rx_trig_bytes,
2983                    serial8250_set_attr_rx_trig_bytes);
2984
2985 static struct attribute *serial8250_dev_attrs[] = {
2986         &dev_attr_rx_trig_bytes.attr,
2987         NULL,
2988         };
2989
2990 static struct attribute_group serial8250_dev_attr_group = {
2991         .attrs = serial8250_dev_attrs,
2992         };
2993
2994 static void register_dev_spec_attr_grp(struct uart_8250_port *up)
2995 {
2996         const struct serial8250_config *conf_type = &uart_config[up->port.type];
2997
2998         if (conf_type->rxtrig_bytes[0])
2999                 up->port.attr_group = &serial8250_dev_attr_group;
3000 }
3001
3002 static void serial8250_config_port(struct uart_port *port, int flags)
3003 {
3004         struct uart_8250_port *up = up_to_u8250p(port);
3005         int ret;
3006
3007         /*
3008          * Find the region that we can probe for.  This in turn
3009          * tells us whether we can probe for the type of port.
3010          */
3011         ret = serial8250_request_std_resource(up);
3012         if (ret < 0)
3013                 return;
3014
3015         if (port->iotype != up->cur_iotype)
3016                 set_io_from_upio(port);
3017
3018         if (flags & UART_CONFIG_TYPE)
3019                 autoconfig(up);
3020
3021         /* if access method is AU, it is a 16550 with a quirk */
3022         if (port->type == PORT_16550A && port->iotype == UPIO_AU)
3023                 up->bugs |= UART_BUG_NOMSR;
3024
3025         /* HW bugs may trigger IRQ while IIR == NO_INT */
3026         if (port->type == PORT_TEGRA)
3027                 up->bugs |= UART_BUG_NOMSR;
3028
3029         if (port->type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ)
3030                 autoconfig_irq(up);
3031
3032         if (port->type == PORT_UNKNOWN)
3033                 serial8250_release_std_resource(up);
3034
3035         /* Fixme: probably not the best place for this */
3036         if ((port->type == PORT_XR17V35X) ||
3037            (port->type == PORT_XR17D15X))
3038                 port->handle_irq = exar_handle_irq;
3039
3040         register_dev_spec_attr_grp(up);
3041         up->fcr = uart_config[up->port.type].fcr;
3042 }
3043
3044 static int
3045 serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
3046 {
3047         if (ser->irq >= nr_irqs || ser->irq < 0 ||
3048             ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
3049             ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS ||
3050             ser->type == PORT_STARTECH)
3051                 return -EINVAL;
3052         return 0;
3053 }
3054
3055 static const char *serial8250_type(struct uart_port *port)
3056 {
3057         int type = port->type;
3058
3059         if (type >= ARRAY_SIZE(uart_config))
3060                 type = 0;
3061         return uart_config[type].name;
3062 }
3063
3064 static const struct uart_ops serial8250_pops = {
3065         .tx_empty       = serial8250_tx_empty,
3066         .set_mctrl      = serial8250_set_mctrl,
3067         .get_mctrl      = serial8250_get_mctrl,
3068         .stop_tx        = serial8250_stop_tx,
3069         .start_tx       = serial8250_start_tx,
3070         .throttle       = serial8250_throttle,
3071         .unthrottle     = serial8250_unthrottle,
3072         .stop_rx        = serial8250_stop_rx,
3073         .enable_ms      = serial8250_enable_ms,
3074         .break_ctl      = serial8250_break_ctl,
3075         .startup        = serial8250_startup,
3076         .shutdown       = serial8250_shutdown,
3077         .set_termios    = serial8250_set_termios,
3078         .set_ldisc      = serial8250_set_ldisc,
3079         .pm             = serial8250_pm,
3080         .type           = serial8250_type,
3081         .release_port   = serial8250_release_port,
3082         .request_port   = serial8250_request_port,
3083         .config_port    = serial8250_config_port,
3084         .verify_port    = serial8250_verify_port,
3085 #ifdef CONFIG_CONSOLE_POLL
3086         .poll_get_char = serial8250_get_poll_char,
3087         .poll_put_char = serial8250_put_poll_char,
3088 #endif
3089 };
3090
3091 void serial8250_init_port(struct uart_8250_port *up)
3092 {
3093         struct uart_port *port = &up->port;
3094
3095         spin_lock_init(&port->lock);
3096         port->ops = &serial8250_pops;
3097
3098         up->cur_iotype = 0xFF;
3099 }
3100 EXPORT_SYMBOL_GPL(serial8250_init_port);
3101
3102 void serial8250_set_defaults(struct uart_8250_port *up)
3103 {
3104         struct uart_port *port = &up->port;
3105
3106         if (up->port.flags & UPF_FIXED_TYPE) {
3107                 unsigned int type = up->port.type;
3108
3109                 if (!up->port.fifosize)
3110                         up->port.fifosize = uart_config[type].fifo_size;
3111                 if (!up->tx_loadsz)
3112                         up->tx_loadsz = uart_config[type].tx_loadsz;
3113                 if (!up->capabilities)
3114                         up->capabilities = uart_config[type].flags;
3115         }
3116
3117         set_io_from_upio(port);
3118
3119         /* default dma handlers */
3120         if (up->dma) {
3121                 if (!up->dma->tx_dma)
3122                         up->dma->tx_dma = serial8250_tx_dma;
3123                 if (!up->dma->rx_dma)
3124                         up->dma->rx_dma = serial8250_rx_dma;
3125         }
3126 }
3127 EXPORT_SYMBOL_GPL(serial8250_set_defaults);
3128
3129 #ifdef CONFIG_SERIAL_8250_CONSOLE
3130
3131 static void serial8250_console_putchar(struct uart_port *port, int ch)
3132 {
3133         struct uart_8250_port *up = up_to_u8250p(port);
3134
3135         wait_for_xmitr(up, UART_LSR_THRE);
3136         serial_port_out(port, UART_TX, ch);
3137 }
3138
3139 /*
3140  *      Restore serial console when h/w power-off detected
3141  */
3142 static void serial8250_console_restore(struct uart_8250_port *up)
3143 {
3144         struct uart_port *port = &up->port;
3145         struct ktermios termios;
3146         unsigned int baud, quot, frac = 0;
3147
3148         termios.c_cflag = port->cons->cflag;
3149         termios.c_ispeed = port->cons->ispeed;
3150         termios.c_ospeed = port->cons->ospeed;
3151         if (port->state->port.tty && termios.c_cflag == 0) {
3152                 termios.c_cflag = port->state->port.tty->termios.c_cflag;
3153                 termios.c_ispeed = port->state->port.tty->termios.c_ispeed;
3154                 termios.c_ospeed = port->state->port.tty->termios.c_ospeed;
3155         }
3156
3157         baud = serial8250_get_baud_rate(port, &termios, NULL);
3158         quot = serial8250_get_divisor(up, baud, &frac);
3159
3160         serial8250_set_divisor(port, baud, quot, frac);
3161         serial_port_out(port, UART_LCR, up->lcr);
3162         serial8250_out_MCR(up, up->mcr | UART_MCR_DTR | UART_MCR_RTS);
3163 }
3164
3165 /*
3166  *      Print a string to the serial port trying not to disturb
3167  *      any possible real use of the port...
3168  *
3169  *      The console_lock must be held when we get here.
3170  */
3171 void serial8250_console_write(struct uart_8250_port *up, const char *s,
3172                               unsigned int count)
3173 {
3174         struct uart_port *port = &up->port;
3175         unsigned long flags;
3176         unsigned int ier;
3177         int locked = 1;
3178
3179         touch_nmi_watchdog();
3180
3181         serial8250_rpm_get(up);
3182
3183         if (port->sysrq)
3184                 locked = 0;
3185         else if (oops_in_progress)
3186                 locked = spin_trylock_irqsave(&port->lock, flags);
3187         else
3188                 spin_lock_irqsave(&port->lock, flags);
3189
3190         /*
3191          *      First save the IER then disable the interrupts
3192          */
3193         ier = serial_port_in(port, UART_IER);
3194
3195         if (up->capabilities & UART_CAP_UUE)
3196                 serial_port_out(port, UART_IER, UART_IER_UUE);
3197         else
3198                 serial_port_out(port, UART_IER, 0);
3199
3200         /* check scratch reg to see if port powered off during system sleep */
3201         if (up->canary && (up->canary != serial_port_in(port, UART_SCR))) {
3202                 serial8250_console_restore(up);
3203                 up->canary = 0;
3204         }
3205
3206         uart_console_write(port, s, count, serial8250_console_putchar);
3207
3208         /*
3209          *      Finally, wait for transmitter to become empty
3210          *      and restore the IER
3211          */
3212         wait_for_xmitr(up, BOTH_EMPTY);
3213         serial_port_out(port, UART_IER, ier);
3214
3215         /*
3216          *      The receive handling will happen properly because the
3217          *      receive ready bit will still be set; it is not cleared
3218          *      on read.  However, modem control will not, we must
3219          *      call it if we have saved something in the saved flags
3220          *      while processing with interrupts off.
3221          */
3222         if (up->msr_saved_flags)
3223                 serial8250_modem_status(up);
3224
3225         if (locked)
3226                 spin_unlock_irqrestore(&port->lock, flags);
3227         serial8250_rpm_put(up);
3228 }
3229
3230 static unsigned int probe_baud(struct uart_port *port)
3231 {
3232         unsigned char lcr, dll, dlm;
3233         unsigned int quot;
3234
3235         lcr = serial_port_in(port, UART_LCR);
3236         serial_port_out(port, UART_LCR, lcr | UART_LCR_DLAB);
3237         dll = serial_port_in(port, UART_DLL);
3238         dlm = serial_port_in(port, UART_DLM);
3239         serial_port_out(port, UART_LCR, lcr);
3240
3241         quot = (dlm << 8) | dll;
3242         return (port->uartclk / 16) / quot;
3243 }
3244
3245 int serial8250_console_setup(struct uart_port *port, char *options, bool probe)
3246 {
3247         int baud = 9600;
3248         int bits = 8;
3249         int parity = 'n';
3250         int flow = 'n';
3251
3252         if (!port->iobase && !port->membase)
3253                 return -ENODEV;
3254
3255         if (options)
3256                 uart_parse_options(options, &baud, &parity, &bits, &flow);
3257         else if (probe)
3258                 baud = probe_baud(port);
3259
3260         return uart_set_options(port, port->cons, baud, parity, bits, flow);
3261 }
3262
3263 #endif /* CONFIG_SERIAL_8250_CONSOLE */
3264
3265 MODULE_LICENSE("GPL");