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