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