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