Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * linux/drivers/char/8250.c | |
3 | * | |
4 | * Driver for 8250/16550-type serial ports | |
5 | * | |
6 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. | |
7 | * | |
8 | * Copyright (C) 2001 Russell King. | |
9 | * | |
10 | * This program is free software; you can redistribute it and/or modify | |
11 | * it under the terms of the GNU General Public License as published by | |
12 | * the Free Software Foundation; either version 2 of the License, or | |
13 | * (at your option) any later version. | |
14 | * | |
1da177e4 LT |
15 | * A note about mapbase / membase |
16 | * | |
17 | * mapbase is the physical address of the IO port. | |
18 | * membase is an 'ioremapped' cookie. | |
19 | */ | |
1da177e4 LT |
20 | |
21 | #if defined(CONFIG_SERIAL_8250_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ) | |
22 | #define SUPPORT_SYSRQ | |
23 | #endif | |
24 | ||
25 | #include <linux/module.h> | |
26 | #include <linux/moduleparam.h> | |
27 | #include <linux/ioport.h> | |
28 | #include <linux/init.h> | |
29 | #include <linux/console.h> | |
30 | #include <linux/sysrq.h> | |
1da177e4 | 31 | #include <linux/delay.h> |
d052d1be | 32 | #include <linux/platform_device.h> |
1da177e4 LT |
33 | #include <linux/tty.h> |
34 | #include <linux/tty_flip.h> | |
35 | #include <linux/serial_reg.h> | |
36 | #include <linux/serial_core.h> | |
37 | #include <linux/serial.h> | |
38 | #include <linux/serial_8250.h> | |
78512ece | 39 | #include <linux/nmi.h> |
f392ecfa | 40 | #include <linux/mutex.h> |
1da177e4 LT |
41 | |
42 | #include <asm/io.h> | |
43 | #include <asm/irq.h> | |
44 | ||
45 | #include "8250.h" | |
46 | ||
47 | /* | |
48 | * Configuration: | |
40663cc7 | 49 | * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option |
1da177e4 LT |
50 | * is unsafe when used on edge-triggered interrupts. |
51 | */ | |
408b664a | 52 | static unsigned int share_irqs = SERIAL8250_SHARE_IRQS; |
1da177e4 | 53 | |
a61c2d78 DJ |
54 | static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS; |
55 | ||
1da177e4 LT |
56 | /* |
57 | * Debugging. | |
58 | */ | |
59 | #if 0 | |
60 | #define DEBUG_AUTOCONF(fmt...) printk(fmt) | |
61 | #else | |
62 | #define DEBUG_AUTOCONF(fmt...) do { } while (0) | |
63 | #endif | |
64 | ||
65 | #if 0 | |
66 | #define DEBUG_INTR(fmt...) printk(fmt) | |
67 | #else | |
68 | #define DEBUG_INTR(fmt...) do { } while (0) | |
69 | #endif | |
70 | ||
71 | #define PASS_LIMIT 256 | |
72 | ||
73 | /* | |
74 | * We default to IRQ0 for the "no irq" hack. Some | |
75 | * machine types want others as well - they're free | |
76 | * to redefine this in their header file. | |
77 | */ | |
78 | #define is_real_interrupt(irq) ((irq) != 0) | |
79 | ||
1da177e4 LT |
80 | #ifdef CONFIG_SERIAL_8250_DETECT_IRQ |
81 | #define CONFIG_SERIAL_DETECT_IRQ 1 | |
82 | #endif | |
1da177e4 LT |
83 | #ifdef CONFIG_SERIAL_8250_MANY_PORTS |
84 | #define CONFIG_SERIAL_MANY_PORTS 1 | |
85 | #endif | |
86 | ||
87 | /* | |
88 | * HUB6 is always on. This will be removed once the header | |
89 | * files have been cleaned. | |
90 | */ | |
91 | #define CONFIG_HUB6 1 | |
92 | ||
a4ed1e41 | 93 | #include <asm/serial.h> |
1da177e4 LT |
94 | /* |
95 | * SERIAL_PORT_DFNS tells us about built-in ports that have no | |
96 | * standard enumeration mechanism. Platforms that can find all | |
97 | * serial ports via mechanisms like ACPI or PCI need not supply it. | |
98 | */ | |
99 | #ifndef SERIAL_PORT_DFNS | |
100 | #define SERIAL_PORT_DFNS | |
101 | #endif | |
102 | ||
cb3592be | 103 | static const struct old_serial_port old_serial_port[] = { |
1da177e4 LT |
104 | SERIAL_PORT_DFNS /* defined in asm/serial.h */ |
105 | }; | |
106 | ||
026d02a2 | 107 | #define UART_NR CONFIG_SERIAL_8250_NR_UARTS |
1da177e4 LT |
108 | |
109 | #ifdef CONFIG_SERIAL_8250_RSA | |
110 | ||
111 | #define PORT_RSA_MAX 4 | |
112 | static unsigned long probe_rsa[PORT_RSA_MAX]; | |
113 | static unsigned int probe_rsa_count; | |
114 | #endif /* CONFIG_SERIAL_8250_RSA */ | |
115 | ||
116 | struct uart_8250_port { | |
117 | struct uart_port port; | |
118 | struct timer_list timer; /* "no irq" timer */ | |
119 | struct list_head list; /* ports on this IRQ */ | |
4ba5e35d RK |
120 | unsigned short capabilities; /* port capabilities */ |
121 | unsigned short bugs; /* port bugs */ | |
1da177e4 | 122 | unsigned int tx_loadsz; /* transmit fifo load size */ |
1da177e4 LT |
123 | unsigned char acr; |
124 | unsigned char ier; | |
125 | unsigned char lcr; | |
126 | unsigned char mcr; | |
127 | unsigned char mcr_mask; /* mask of user bits */ | |
128 | unsigned char mcr_force; /* mask of forced bits */ | |
ad4c2aa6 CM |
129 | |
130 | /* | |
131 | * Some bits in registers are cleared on a read, so they must | |
132 | * be saved whenever the register is read but the bits will not | |
133 | * be immediately processed. | |
134 | */ | |
135 | #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS | |
136 | unsigned char lsr_saved_flags; | |
137 | #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA | |
138 | unsigned char msr_saved_flags; | |
1da177e4 LT |
139 | |
140 | /* | |
141 | * We provide a per-port pm hook. | |
142 | */ | |
143 | void (*pm)(struct uart_port *port, | |
144 | unsigned int state, unsigned int old); | |
145 | }; | |
146 | ||
147 | struct irq_info { | |
148 | spinlock_t lock; | |
149 | struct list_head *head; | |
150 | }; | |
151 | ||
152 | static struct irq_info irq_lists[NR_IRQS]; | |
153 | ||
154 | /* | |
155 | * Here we define the default xmit fifo size used for each type of UART. | |
156 | */ | |
157 | static const struct serial8250_config uart_config[] = { | |
158 | [PORT_UNKNOWN] = { | |
159 | .name = "unknown", | |
160 | .fifo_size = 1, | |
161 | .tx_loadsz = 1, | |
162 | }, | |
163 | [PORT_8250] = { | |
164 | .name = "8250", | |
165 | .fifo_size = 1, | |
166 | .tx_loadsz = 1, | |
167 | }, | |
168 | [PORT_16450] = { | |
169 | .name = "16450", | |
170 | .fifo_size = 1, | |
171 | .tx_loadsz = 1, | |
172 | }, | |
173 | [PORT_16550] = { | |
174 | .name = "16550", | |
175 | .fifo_size = 1, | |
176 | .tx_loadsz = 1, | |
177 | }, | |
178 | [PORT_16550A] = { | |
179 | .name = "16550A", | |
180 | .fifo_size = 16, | |
181 | .tx_loadsz = 16, | |
182 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, | |
183 | .flags = UART_CAP_FIFO, | |
184 | }, | |
185 | [PORT_CIRRUS] = { | |
186 | .name = "Cirrus", | |
187 | .fifo_size = 1, | |
188 | .tx_loadsz = 1, | |
189 | }, | |
190 | [PORT_16650] = { | |
191 | .name = "ST16650", | |
192 | .fifo_size = 1, | |
193 | .tx_loadsz = 1, | |
194 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, | |
195 | }, | |
196 | [PORT_16650V2] = { | |
197 | .name = "ST16650V2", | |
198 | .fifo_size = 32, | |
199 | .tx_loadsz = 16, | |
200 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | | |
201 | UART_FCR_T_TRIG_00, | |
202 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, | |
203 | }, | |
204 | [PORT_16750] = { | |
205 | .name = "TI16750", | |
206 | .fifo_size = 64, | |
207 | .tx_loadsz = 64, | |
208 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10 | | |
209 | UART_FCR7_64BYTE, | |
210 | .flags = UART_CAP_FIFO | UART_CAP_SLEEP | UART_CAP_AFE, | |
211 | }, | |
212 | [PORT_STARTECH] = { | |
213 | .name = "Startech", | |
214 | .fifo_size = 1, | |
215 | .tx_loadsz = 1, | |
216 | }, | |
217 | [PORT_16C950] = { | |
218 | .name = "16C950/954", | |
219 | .fifo_size = 128, | |
220 | .tx_loadsz = 128, | |
221 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, | |
222 | .flags = UART_CAP_FIFO, | |
223 | }, | |
224 | [PORT_16654] = { | |
225 | .name = "ST16654", | |
226 | .fifo_size = 64, | |
227 | .tx_loadsz = 32, | |
228 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_01 | | |
229 | UART_FCR_T_TRIG_10, | |
230 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, | |
231 | }, | |
232 | [PORT_16850] = { | |
233 | .name = "XR16850", | |
234 | .fifo_size = 128, | |
235 | .tx_loadsz = 128, | |
236 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, | |
237 | .flags = UART_CAP_FIFO | UART_CAP_EFR | UART_CAP_SLEEP, | |
238 | }, | |
239 | [PORT_RSA] = { | |
240 | .name = "RSA", | |
241 | .fifo_size = 2048, | |
242 | .tx_loadsz = 2048, | |
243 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_11, | |
244 | .flags = UART_CAP_FIFO, | |
245 | }, | |
246 | [PORT_NS16550A] = { | |
247 | .name = "NS16550A", | |
248 | .fifo_size = 16, | |
249 | .tx_loadsz = 16, | |
250 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, | |
251 | .flags = UART_CAP_FIFO | UART_NATSEMI, | |
252 | }, | |
253 | [PORT_XSCALE] = { | |
254 | .name = "XScale", | |
255 | .fifo_size = 32, | |
256 | .tx_loadsz = 32, | |
257 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, | |
258 | .flags = UART_CAP_FIFO | UART_CAP_UUE, | |
259 | }, | |
bd71c182 TK |
260 | [PORT_RM9000] = { |
261 | .name = "RM9000", | |
262 | .fifo_size = 16, | |
263 | .tx_loadsz = 16, | |
264 | .fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIG_10, | |
265 | .flags = UART_CAP_FIFO, | |
266 | }, | |
1da177e4 LT |
267 | }; |
268 | ||
bd71c182 | 269 | #if defined (CONFIG_SERIAL_8250_AU1X00) |
21c614a7 PA |
270 | |
271 | /* Au1x00 UART hardware has a weird register layout */ | |
272 | static const u8 au_io_in_map[] = { | |
273 | [UART_RX] = 0, | |
274 | [UART_IER] = 2, | |
275 | [UART_IIR] = 3, | |
276 | [UART_LCR] = 5, | |
277 | [UART_MCR] = 6, | |
278 | [UART_LSR] = 7, | |
279 | [UART_MSR] = 8, | |
280 | }; | |
281 | ||
282 | static const u8 au_io_out_map[] = { | |
283 | [UART_TX] = 1, | |
284 | [UART_IER] = 2, | |
285 | [UART_FCR] = 4, | |
286 | [UART_LCR] = 5, | |
287 | [UART_MCR] = 6, | |
288 | }; | |
289 | ||
290 | /* sane hardware needs no mapping */ | |
291 | static inline int map_8250_in_reg(struct uart_8250_port *up, int offset) | |
292 | { | |
293 | if (up->port.iotype != UPIO_AU) | |
294 | return offset; | |
295 | return au_io_in_map[offset]; | |
296 | } | |
297 | ||
298 | static inline int map_8250_out_reg(struct uart_8250_port *up, int offset) | |
299 | { | |
300 | if (up->port.iotype != UPIO_AU) | |
301 | return offset; | |
302 | return au_io_out_map[offset]; | |
303 | } | |
304 | ||
6f803cd0 | 305 | #elif defined(CONFIG_SERIAL_8250_RM9K) |
bd71c182 TK |
306 | |
307 | static const u8 | |
308 | regmap_in[8] = { | |
309 | [UART_RX] = 0x00, | |
310 | [UART_IER] = 0x0c, | |
311 | [UART_IIR] = 0x14, | |
312 | [UART_LCR] = 0x1c, | |
313 | [UART_MCR] = 0x20, | |
314 | [UART_LSR] = 0x24, | |
315 | [UART_MSR] = 0x28, | |
316 | [UART_SCR] = 0x2c | |
317 | }, | |
318 | regmap_out[8] = { | |
319 | [UART_TX] = 0x04, | |
320 | [UART_IER] = 0x0c, | |
321 | [UART_FCR] = 0x18, | |
322 | [UART_LCR] = 0x1c, | |
323 | [UART_MCR] = 0x20, | |
324 | [UART_LSR] = 0x24, | |
325 | [UART_MSR] = 0x28, | |
326 | [UART_SCR] = 0x2c | |
327 | }; | |
328 | ||
329 | static inline int map_8250_in_reg(struct uart_8250_port *up, int offset) | |
330 | { | |
331 | if (up->port.iotype != UPIO_RM9000) | |
332 | return offset; | |
333 | return regmap_in[offset]; | |
334 | } | |
335 | ||
336 | static inline int map_8250_out_reg(struct uart_8250_port *up, int offset) | |
337 | { | |
338 | if (up->port.iotype != UPIO_RM9000) | |
339 | return offset; | |
340 | return regmap_out[offset]; | |
341 | } | |
342 | ||
21c614a7 PA |
343 | #else |
344 | ||
345 | /* sane hardware needs no mapping */ | |
346 | #define map_8250_in_reg(up, offset) (offset) | |
347 | #define map_8250_out_reg(up, offset) (offset) | |
348 | ||
349 | #endif | |
350 | ||
ea8874dc | 351 | static unsigned int serial_in(struct uart_8250_port *up, int offset) |
1da177e4 | 352 | { |
3be91ec7 | 353 | unsigned int tmp; |
21c614a7 | 354 | offset = map_8250_in_reg(up, offset) << up->port.regshift; |
1da177e4 LT |
355 | |
356 | switch (up->port.iotype) { | |
357 | case UPIO_HUB6: | |
358 | outb(up->port.hub6 - 1 + offset, up->port.iobase); | |
359 | return inb(up->port.iobase + 1); | |
360 | ||
361 | case UPIO_MEM: | |
beab697a | 362 | case UPIO_DWAPB: |
1da177e4 LT |
363 | return readb(up->port.membase + offset); |
364 | ||
bd71c182 | 365 | case UPIO_RM9000: |
1da177e4 LT |
366 | case UPIO_MEM32: |
367 | return readl(up->port.membase + offset); | |
368 | ||
21c614a7 PA |
369 | #ifdef CONFIG_SERIAL_8250_AU1X00 |
370 | case UPIO_AU: | |
371 | return __raw_readl(up->port.membase + offset); | |
372 | #endif | |
373 | ||
3be91ec7 ZR |
374 | case UPIO_TSI: |
375 | if (offset == UART_IIR) { | |
9e84b60e AV |
376 | tmp = readl(up->port.membase + (UART_IIR & ~3)); |
377 | return (tmp >> 16) & 0xff; /* UART_IIR % 4 == 2 */ | |
3be91ec7 ZR |
378 | } else |
379 | return readb(up->port.membase + offset); | |
380 | ||
1da177e4 LT |
381 | default: |
382 | return inb(up->port.iobase + offset); | |
383 | } | |
384 | } | |
385 | ||
ea8874dc | 386 | static void |
1da177e4 LT |
387 | serial_out(struct uart_8250_port *up, int offset, int value) |
388 | { | |
beab697a MSJ |
389 | /* Save the offset before it's remapped */ |
390 | int save_offset = offset; | |
21c614a7 | 391 | offset = map_8250_out_reg(up, offset) << up->port.regshift; |
1da177e4 LT |
392 | |
393 | switch (up->port.iotype) { | |
394 | case UPIO_HUB6: | |
395 | outb(up->port.hub6 - 1 + offset, up->port.iobase); | |
396 | outb(value, up->port.iobase + 1); | |
397 | break; | |
398 | ||
399 | case UPIO_MEM: | |
400 | writeb(value, up->port.membase + offset); | |
401 | break; | |
402 | ||
bd71c182 | 403 | case UPIO_RM9000: |
1da177e4 LT |
404 | case UPIO_MEM32: |
405 | writel(value, up->port.membase + offset); | |
406 | break; | |
407 | ||
21c614a7 PA |
408 | #ifdef CONFIG_SERIAL_8250_AU1X00 |
409 | case UPIO_AU: | |
410 | __raw_writel(value, up->port.membase + offset); | |
411 | break; | |
412 | #endif | |
3be91ec7 ZR |
413 | case UPIO_TSI: |
414 | if (!((offset == UART_IER) && (value & UART_IER_UUE))) | |
415 | writeb(value, up->port.membase + offset); | |
416 | break; | |
21c614a7 | 417 | |
beab697a MSJ |
418 | case UPIO_DWAPB: |
419 | /* Save the LCR value so it can be re-written when a | |
420 | * Busy Detect interrupt occurs. */ | |
421 | if (save_offset == UART_LCR) | |
422 | up->lcr = value; | |
423 | writeb(value, up->port.membase + offset); | |
424 | /* Read the IER to ensure any interrupt is cleared before | |
425 | * returning from ISR. */ | |
426 | if (save_offset == UART_TX || save_offset == UART_IER) | |
427 | value = serial_in(up, UART_IER); | |
428 | break; | |
429 | ||
1da177e4 LT |
430 | default: |
431 | outb(value, up->port.iobase + offset); | |
432 | } | |
433 | } | |
434 | ||
40b36daa AW |
435 | static void |
436 | serial_out_sync(struct uart_8250_port *up, int offset, int value) | |
437 | { | |
438 | switch (up->port.iotype) { | |
439 | case UPIO_MEM: | |
440 | case UPIO_MEM32: | |
441 | #ifdef CONFIG_SERIAL_8250_AU1X00 | |
442 | case UPIO_AU: | |
443 | #endif | |
beab697a | 444 | case UPIO_DWAPB: |
40b36daa AW |
445 | serial_out(up, offset, value); |
446 | serial_in(up, UART_LCR); /* safe, no side-effects */ | |
447 | break; | |
448 | default: | |
449 | serial_out(up, offset, value); | |
450 | } | |
451 | } | |
452 | ||
1da177e4 LT |
453 | /* |
454 | * We used to support using pause I/O for certain machines. We | |
455 | * haven't supported this for a while, but just in case it's badly | |
456 | * needed for certain old 386 machines, I've left these #define's | |
457 | * in.... | |
458 | */ | |
459 | #define serial_inp(up, offset) serial_in(up, offset) | |
460 | #define serial_outp(up, offset, value) serial_out(up, offset, value) | |
461 | ||
b32b19b8 JAH |
462 | /* Uart divisor latch read */ |
463 | static inline int _serial_dl_read(struct uart_8250_port *up) | |
464 | { | |
465 | return serial_inp(up, UART_DLL) | serial_inp(up, UART_DLM) << 8; | |
466 | } | |
467 | ||
468 | /* Uart divisor latch write */ | |
469 | static inline void _serial_dl_write(struct uart_8250_port *up, int value) | |
470 | { | |
471 | serial_outp(up, UART_DLL, value & 0xff); | |
472 | serial_outp(up, UART_DLM, value >> 8 & 0xff); | |
473 | } | |
474 | ||
6f803cd0 | 475 | #if defined(CONFIG_SERIAL_8250_AU1X00) |
b32b19b8 JAH |
476 | /* Au1x00 haven't got a standard divisor latch */ |
477 | static int serial_dl_read(struct uart_8250_port *up) | |
478 | { | |
479 | if (up->port.iotype == UPIO_AU) | |
480 | return __raw_readl(up->port.membase + 0x28); | |
481 | else | |
482 | return _serial_dl_read(up); | |
483 | } | |
484 | ||
485 | static void serial_dl_write(struct uart_8250_port *up, int value) | |
486 | { | |
487 | if (up->port.iotype == UPIO_AU) | |
488 | __raw_writel(value, up->port.membase + 0x28); | |
489 | else | |
490 | _serial_dl_write(up, value); | |
491 | } | |
6f803cd0 | 492 | #elif defined(CONFIG_SERIAL_8250_RM9K) |
bd71c182 TK |
493 | static int serial_dl_read(struct uart_8250_port *up) |
494 | { | |
495 | return (up->port.iotype == UPIO_RM9000) ? | |
496 | (((__raw_readl(up->port.membase + 0x10) << 8) | | |
497 | (__raw_readl(up->port.membase + 0x08) & 0xff)) & 0xffff) : | |
498 | _serial_dl_read(up); | |
499 | } | |
500 | ||
501 | static void serial_dl_write(struct uart_8250_port *up, int value) | |
502 | { | |
503 | if (up->port.iotype == UPIO_RM9000) { | |
504 | __raw_writel(value, up->port.membase + 0x08); | |
505 | __raw_writel(value >> 8, up->port.membase + 0x10); | |
506 | } else { | |
507 | _serial_dl_write(up, value); | |
508 | } | |
509 | } | |
b32b19b8 JAH |
510 | #else |
511 | #define serial_dl_read(up) _serial_dl_read(up) | |
512 | #define serial_dl_write(up, value) _serial_dl_write(up, value) | |
513 | #endif | |
1da177e4 LT |
514 | |
515 | /* | |
516 | * For the 16C950 | |
517 | */ | |
518 | static void serial_icr_write(struct uart_8250_port *up, int offset, int value) | |
519 | { | |
520 | serial_out(up, UART_SCR, offset); | |
521 | serial_out(up, UART_ICR, value); | |
522 | } | |
523 | ||
524 | static unsigned int serial_icr_read(struct uart_8250_port *up, int offset) | |
525 | { | |
526 | unsigned int value; | |
527 | ||
528 | serial_icr_write(up, UART_ACR, up->acr | UART_ACR_ICRRD); | |
529 | serial_out(up, UART_SCR, offset); | |
530 | value = serial_in(up, UART_ICR); | |
531 | serial_icr_write(up, UART_ACR, up->acr); | |
532 | ||
533 | return value; | |
534 | } | |
535 | ||
536 | /* | |
537 | * FIFO support. | |
538 | */ | |
539 | static inline void serial8250_clear_fifos(struct uart_8250_port *p) | |
540 | { | |
541 | if (p->capabilities & UART_CAP_FIFO) { | |
542 | serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO); | |
543 | serial_outp(p, UART_FCR, UART_FCR_ENABLE_FIFO | | |
544 | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); | |
545 | serial_outp(p, UART_FCR, 0); | |
546 | } | |
547 | } | |
548 | ||
549 | /* | |
550 | * IER sleep support. UARTs which have EFRs need the "extended | |
551 | * capability" bit enabled. Note that on XR16C850s, we need to | |
552 | * reset LCR to write to IER. | |
553 | */ | |
554 | static inline void serial8250_set_sleep(struct uart_8250_port *p, int sleep) | |
555 | { | |
556 | if (p->capabilities & UART_CAP_SLEEP) { | |
557 | if (p->capabilities & UART_CAP_EFR) { | |
558 | serial_outp(p, UART_LCR, 0xBF); | |
559 | serial_outp(p, UART_EFR, UART_EFR_ECB); | |
560 | serial_outp(p, UART_LCR, 0); | |
561 | } | |
562 | serial_outp(p, UART_IER, sleep ? UART_IERX_SLEEP : 0); | |
563 | if (p->capabilities & UART_CAP_EFR) { | |
564 | serial_outp(p, UART_LCR, 0xBF); | |
565 | serial_outp(p, UART_EFR, 0); | |
566 | serial_outp(p, UART_LCR, 0); | |
567 | } | |
568 | } | |
569 | } | |
570 | ||
571 | #ifdef CONFIG_SERIAL_8250_RSA | |
572 | /* | |
573 | * Attempts to turn on the RSA FIFO. Returns zero on failure. | |
574 | * We set the port uart clock rate if we succeed. | |
575 | */ | |
576 | static int __enable_rsa(struct uart_8250_port *up) | |
577 | { | |
578 | unsigned char mode; | |
579 | int result; | |
580 | ||
581 | mode = serial_inp(up, UART_RSA_MSR); | |
582 | result = mode & UART_RSA_MSR_FIFO; | |
583 | ||
584 | if (!result) { | |
585 | serial_outp(up, UART_RSA_MSR, mode | UART_RSA_MSR_FIFO); | |
586 | mode = serial_inp(up, UART_RSA_MSR); | |
587 | result = mode & UART_RSA_MSR_FIFO; | |
588 | } | |
589 | ||
590 | if (result) | |
591 | up->port.uartclk = SERIAL_RSA_BAUD_BASE * 16; | |
592 | ||
593 | return result; | |
594 | } | |
595 | ||
596 | static void enable_rsa(struct uart_8250_port *up) | |
597 | { | |
598 | if (up->port.type == PORT_RSA) { | |
599 | if (up->port.uartclk != SERIAL_RSA_BAUD_BASE * 16) { | |
600 | spin_lock_irq(&up->port.lock); | |
601 | __enable_rsa(up); | |
602 | spin_unlock_irq(&up->port.lock); | |
603 | } | |
604 | if (up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) | |
605 | serial_outp(up, UART_RSA_FRR, 0); | |
606 | } | |
607 | } | |
608 | ||
609 | /* | |
610 | * Attempts to turn off the RSA FIFO. Returns zero on failure. | |
611 | * It is unknown why interrupts were disabled in here. However, | |
612 | * the caller is expected to preserve this behaviour by grabbing | |
613 | * the spinlock before calling this function. | |
614 | */ | |
615 | static void disable_rsa(struct uart_8250_port *up) | |
616 | { | |
617 | unsigned char mode; | |
618 | int result; | |
619 | ||
620 | if (up->port.type == PORT_RSA && | |
621 | up->port.uartclk == SERIAL_RSA_BAUD_BASE * 16) { | |
622 | spin_lock_irq(&up->port.lock); | |
623 | ||
624 | mode = serial_inp(up, UART_RSA_MSR); | |
625 | result = !(mode & UART_RSA_MSR_FIFO); | |
626 | ||
627 | if (!result) { | |
628 | serial_outp(up, UART_RSA_MSR, mode & ~UART_RSA_MSR_FIFO); | |
629 | mode = serial_inp(up, UART_RSA_MSR); | |
630 | result = !(mode & UART_RSA_MSR_FIFO); | |
631 | } | |
632 | ||
633 | if (result) | |
634 | up->port.uartclk = SERIAL_RSA_BAUD_BASE_LO * 16; | |
635 | spin_unlock_irq(&up->port.lock); | |
636 | } | |
637 | } | |
638 | #endif /* CONFIG_SERIAL_8250_RSA */ | |
639 | ||
640 | /* | |
641 | * This is a quickie test to see how big the FIFO is. | |
642 | * It doesn't work at all the time, more's the pity. | |
643 | */ | |
644 | static int size_fifo(struct uart_8250_port *up) | |
645 | { | |
b32b19b8 JAH |
646 | unsigned char old_fcr, old_mcr, old_lcr; |
647 | unsigned short old_dl; | |
1da177e4 LT |
648 | int count; |
649 | ||
650 | old_lcr = serial_inp(up, UART_LCR); | |
651 | serial_outp(up, UART_LCR, 0); | |
652 | old_fcr = serial_inp(up, UART_FCR); | |
653 | old_mcr = serial_inp(up, UART_MCR); | |
654 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | | |
655 | UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT); | |
656 | serial_outp(up, UART_MCR, UART_MCR_LOOP); | |
657 | serial_outp(up, UART_LCR, UART_LCR_DLAB); | |
b32b19b8 JAH |
658 | old_dl = serial_dl_read(up); |
659 | serial_dl_write(up, 0x0001); | |
1da177e4 LT |
660 | serial_outp(up, UART_LCR, 0x03); |
661 | for (count = 0; count < 256; count++) | |
662 | serial_outp(up, UART_TX, count); | |
663 | mdelay(20);/* FIXME - schedule_timeout */ | |
664 | for (count = 0; (serial_inp(up, UART_LSR) & UART_LSR_DR) && | |
665 | (count < 256); count++) | |
666 | serial_inp(up, UART_RX); | |
667 | serial_outp(up, UART_FCR, old_fcr); | |
668 | serial_outp(up, UART_MCR, old_mcr); | |
669 | serial_outp(up, UART_LCR, UART_LCR_DLAB); | |
b32b19b8 | 670 | serial_dl_write(up, old_dl); |
1da177e4 LT |
671 | serial_outp(up, UART_LCR, old_lcr); |
672 | ||
673 | return count; | |
674 | } | |
675 | ||
676 | /* | |
677 | * Read UART ID using the divisor method - set DLL and DLM to zero | |
678 | * and the revision will be in DLL and device type in DLM. We | |
679 | * preserve the device state across this. | |
680 | */ | |
681 | static unsigned int autoconfig_read_divisor_id(struct uart_8250_port *p) | |
682 | { | |
683 | unsigned char old_dll, old_dlm, old_lcr; | |
684 | unsigned int id; | |
685 | ||
686 | old_lcr = serial_inp(p, UART_LCR); | |
687 | serial_outp(p, UART_LCR, UART_LCR_DLAB); | |
688 | ||
689 | old_dll = serial_inp(p, UART_DLL); | |
690 | old_dlm = serial_inp(p, UART_DLM); | |
691 | ||
692 | serial_outp(p, UART_DLL, 0); | |
693 | serial_outp(p, UART_DLM, 0); | |
694 | ||
695 | id = serial_inp(p, UART_DLL) | serial_inp(p, UART_DLM) << 8; | |
696 | ||
697 | serial_outp(p, UART_DLL, old_dll); | |
698 | serial_outp(p, UART_DLM, old_dlm); | |
699 | serial_outp(p, UART_LCR, old_lcr); | |
700 | ||
701 | return id; | |
702 | } | |
703 | ||
704 | /* | |
705 | * This is a helper routine to autodetect StarTech/Exar/Oxsemi UART's. | |
706 | * When this function is called we know it is at least a StarTech | |
707 | * 16650 V2, but it might be one of several StarTech UARTs, or one of | |
708 | * its clones. (We treat the broken original StarTech 16650 V1 as a | |
709 | * 16550, and why not? Startech doesn't seem to even acknowledge its | |
710 | * existence.) | |
bd71c182 | 711 | * |
1da177e4 LT |
712 | * What evil have men's minds wrought... |
713 | */ | |
714 | static void autoconfig_has_efr(struct uart_8250_port *up) | |
715 | { | |
716 | unsigned int id1, id2, id3, rev; | |
717 | ||
718 | /* | |
719 | * Everything with an EFR has SLEEP | |
720 | */ | |
721 | up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; | |
722 | ||
723 | /* | |
724 | * First we check to see if it's an Oxford Semiconductor UART. | |
725 | * | |
726 | * If we have to do this here because some non-National | |
727 | * Semiconductor clone chips lock up if you try writing to the | |
728 | * LSR register (which serial_icr_read does) | |
729 | */ | |
730 | ||
731 | /* | |
732 | * Check for Oxford Semiconductor 16C950. | |
733 | * | |
734 | * EFR [4] must be set else this test fails. | |
735 | * | |
736 | * This shouldn't be necessary, but Mike Hudson (Exoray@isys.ca) | |
737 | * claims that it's needed for 952 dual UART's (which are not | |
738 | * recommended for new designs). | |
739 | */ | |
740 | up->acr = 0; | |
741 | serial_out(up, UART_LCR, 0xBF); | |
742 | serial_out(up, UART_EFR, UART_EFR_ECB); | |
743 | serial_out(up, UART_LCR, 0x00); | |
744 | id1 = serial_icr_read(up, UART_ID1); | |
745 | id2 = serial_icr_read(up, UART_ID2); | |
746 | id3 = serial_icr_read(up, UART_ID3); | |
747 | rev = serial_icr_read(up, UART_REV); | |
748 | ||
749 | DEBUG_AUTOCONF("950id=%02x:%02x:%02x:%02x ", id1, id2, id3, rev); | |
750 | ||
751 | if (id1 == 0x16 && id2 == 0xC9 && | |
752 | (id3 == 0x50 || id3 == 0x52 || id3 == 0x54)) { | |
753 | up->port.type = PORT_16C950; | |
4ba5e35d RK |
754 | |
755 | /* | |
756 | * Enable work around for the Oxford Semiconductor 952 rev B | |
757 | * chip which causes it to seriously miscalculate baud rates | |
758 | * when DLL is 0. | |
759 | */ | |
760 | if (id3 == 0x52 && rev == 0x01) | |
761 | up->bugs |= UART_BUG_QUOT; | |
1da177e4 LT |
762 | return; |
763 | } | |
bd71c182 | 764 | |
1da177e4 LT |
765 | /* |
766 | * We check for a XR16C850 by setting DLL and DLM to 0, and then | |
767 | * reading back DLL and DLM. The chip type depends on the DLM | |
768 | * value read back: | |
769 | * 0x10 - XR16C850 and the DLL contains the chip revision. | |
770 | * 0x12 - XR16C2850. | |
771 | * 0x14 - XR16C854. | |
772 | */ | |
773 | id1 = autoconfig_read_divisor_id(up); | |
774 | DEBUG_AUTOCONF("850id=%04x ", id1); | |
775 | ||
776 | id2 = id1 >> 8; | |
777 | if (id2 == 0x10 || id2 == 0x12 || id2 == 0x14) { | |
1da177e4 LT |
778 | up->port.type = PORT_16850; |
779 | return; | |
780 | } | |
781 | ||
782 | /* | |
783 | * It wasn't an XR16C850. | |
784 | * | |
785 | * We distinguish between the '654 and the '650 by counting | |
786 | * how many bytes are in the FIFO. I'm using this for now, | |
787 | * since that's the technique that was sent to me in the | |
788 | * serial driver update, but I'm not convinced this works. | |
789 | * I've had problems doing this in the past. -TYT | |
790 | */ | |
791 | if (size_fifo(up) == 64) | |
792 | up->port.type = PORT_16654; | |
793 | else | |
794 | up->port.type = PORT_16650V2; | |
795 | } | |
796 | ||
797 | /* | |
798 | * We detected a chip without a FIFO. Only two fall into | |
799 | * this category - the original 8250 and the 16450. The | |
800 | * 16450 has a scratch register (accessible with LCR=0) | |
801 | */ | |
802 | static void autoconfig_8250(struct uart_8250_port *up) | |
803 | { | |
804 | unsigned char scratch, status1, status2; | |
805 | ||
806 | up->port.type = PORT_8250; | |
807 | ||
808 | scratch = serial_in(up, UART_SCR); | |
809 | serial_outp(up, UART_SCR, 0xa5); | |
810 | status1 = serial_in(up, UART_SCR); | |
811 | serial_outp(up, UART_SCR, 0x5a); | |
812 | status2 = serial_in(up, UART_SCR); | |
813 | serial_outp(up, UART_SCR, scratch); | |
814 | ||
815 | if (status1 == 0xa5 && status2 == 0x5a) | |
816 | up->port.type = PORT_16450; | |
817 | } | |
818 | ||
819 | static int broken_efr(struct uart_8250_port *up) | |
820 | { | |
821 | /* | |
822 | * Exar ST16C2550 "A2" devices incorrectly detect as | |
823 | * having an EFR, and report an ID of 0x0201. See | |
824 | * http://www.exar.com/info.php?pdf=dan180_oct2004.pdf | |
825 | */ | |
826 | if (autoconfig_read_divisor_id(up) == 0x0201 && size_fifo(up) == 16) | |
827 | return 1; | |
828 | ||
829 | return 0; | |
830 | } | |
831 | ||
832 | /* | |
833 | * We know that the chip has FIFOs. Does it have an EFR? The | |
834 | * EFR is located in the same register position as the IIR and | |
835 | * we know the top two bits of the IIR are currently set. The | |
836 | * EFR should contain zero. Try to read the EFR. | |
837 | */ | |
838 | static void autoconfig_16550a(struct uart_8250_port *up) | |
839 | { | |
840 | unsigned char status1, status2; | |
841 | unsigned int iersave; | |
842 | ||
843 | up->port.type = PORT_16550A; | |
844 | up->capabilities |= UART_CAP_FIFO; | |
845 | ||
846 | /* | |
847 | * Check for presence of the EFR when DLAB is set. | |
848 | * Only ST16C650V1 UARTs pass this test. | |
849 | */ | |
850 | serial_outp(up, UART_LCR, UART_LCR_DLAB); | |
851 | if (serial_in(up, UART_EFR) == 0) { | |
852 | serial_outp(up, UART_EFR, 0xA8); | |
853 | if (serial_in(up, UART_EFR) != 0) { | |
854 | DEBUG_AUTOCONF("EFRv1 "); | |
855 | up->port.type = PORT_16650; | |
856 | up->capabilities |= UART_CAP_EFR | UART_CAP_SLEEP; | |
857 | } else { | |
858 | DEBUG_AUTOCONF("Motorola 8xxx DUART "); | |
859 | } | |
860 | serial_outp(up, UART_EFR, 0); | |
861 | return; | |
862 | } | |
863 | ||
864 | /* | |
865 | * Maybe it requires 0xbf to be written to the LCR. | |
866 | * (other ST16C650V2 UARTs, TI16C752A, etc) | |
867 | */ | |
868 | serial_outp(up, UART_LCR, 0xBF); | |
869 | if (serial_in(up, UART_EFR) == 0 && !broken_efr(up)) { | |
870 | DEBUG_AUTOCONF("EFRv2 "); | |
871 | autoconfig_has_efr(up); | |
872 | return; | |
873 | } | |
874 | ||
875 | /* | |
876 | * Check for a National Semiconductor SuperIO chip. | |
877 | * Attempt to switch to bank 2, read the value of the LOOP bit | |
878 | * from EXCR1. Switch back to bank 0, change it in MCR. Then | |
879 | * switch back to bank 2, read it from EXCR1 again and check | |
880 | * it's changed. If so, set baud_base in EXCR2 to 921600. -- dwmw2 | |
1da177e4 LT |
881 | */ |
882 | serial_outp(up, UART_LCR, 0); | |
883 | status1 = serial_in(up, UART_MCR); | |
884 | serial_outp(up, UART_LCR, 0xE0); | |
885 | status2 = serial_in(up, 0x02); /* EXCR1 */ | |
886 | ||
887 | if (!((status2 ^ status1) & UART_MCR_LOOP)) { | |
888 | serial_outp(up, UART_LCR, 0); | |
889 | serial_outp(up, UART_MCR, status1 ^ UART_MCR_LOOP); | |
890 | serial_outp(up, UART_LCR, 0xE0); | |
891 | status2 = serial_in(up, 0x02); /* EXCR1 */ | |
892 | serial_outp(up, UART_LCR, 0); | |
893 | serial_outp(up, UART_MCR, status1); | |
894 | ||
895 | if ((status2 ^ status1) & UART_MCR_LOOP) { | |
857dde2e DW |
896 | unsigned short quot; |
897 | ||
1da177e4 | 898 | serial_outp(up, UART_LCR, 0xE0); |
857dde2e | 899 | |
b32b19b8 | 900 | quot = serial_dl_read(up); |
857dde2e DW |
901 | quot <<= 3; |
902 | ||
b5b82df6 | 903 | status1 = serial_in(up, 0x04); /* EXCR2 */ |
1da177e4 LT |
904 | status1 &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */ |
905 | status1 |= 0x10; /* 1.625 divisor for baud_base --> 921600 */ | |
906 | serial_outp(up, 0x04, status1); | |
bd71c182 | 907 | |
b32b19b8 | 908 | serial_dl_write(up, quot); |
857dde2e | 909 | |
1da177e4 | 910 | serial_outp(up, UART_LCR, 0); |
1da177e4 | 911 | |
857dde2e | 912 | up->port.uartclk = 921600*16; |
1da177e4 LT |
913 | up->port.type = PORT_NS16550A; |
914 | up->capabilities |= UART_NATSEMI; | |
915 | return; | |
916 | } | |
917 | } | |
918 | ||
919 | /* | |
920 | * No EFR. Try to detect a TI16750, which only sets bit 5 of | |
921 | * the IIR when 64 byte FIFO mode is enabled when DLAB is set. | |
922 | * Try setting it with and without DLAB set. Cheap clones | |
923 | * set bit 5 without DLAB set. | |
924 | */ | |
925 | serial_outp(up, UART_LCR, 0); | |
926 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); | |
927 | status1 = serial_in(up, UART_IIR) >> 5; | |
928 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); | |
929 | serial_outp(up, UART_LCR, UART_LCR_DLAB); | |
930 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO | UART_FCR7_64BYTE); | |
931 | status2 = serial_in(up, UART_IIR) >> 5; | |
932 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); | |
933 | serial_outp(up, UART_LCR, 0); | |
934 | ||
935 | DEBUG_AUTOCONF("iir1=%d iir2=%d ", status1, status2); | |
936 | ||
937 | if (status1 == 6 && status2 == 7) { | |
938 | up->port.type = PORT_16750; | |
939 | up->capabilities |= UART_CAP_AFE | UART_CAP_SLEEP; | |
940 | return; | |
941 | } | |
942 | ||
943 | /* | |
944 | * Try writing and reading the UART_IER_UUE bit (b6). | |
945 | * If it works, this is probably one of the Xscale platform's | |
946 | * internal UARTs. | |
947 | * We're going to explicitly set the UUE bit to 0 before | |
948 | * trying to write and read a 1 just to make sure it's not | |
949 | * already a 1 and maybe locked there before we even start start. | |
950 | */ | |
951 | iersave = serial_in(up, UART_IER); | |
952 | serial_outp(up, UART_IER, iersave & ~UART_IER_UUE); | |
953 | if (!(serial_in(up, UART_IER) & UART_IER_UUE)) { | |
954 | /* | |
955 | * OK it's in a known zero state, try writing and reading | |
956 | * without disturbing the current state of the other bits. | |
957 | */ | |
958 | serial_outp(up, UART_IER, iersave | UART_IER_UUE); | |
959 | if (serial_in(up, UART_IER) & UART_IER_UUE) { | |
960 | /* | |
961 | * It's an Xscale. | |
962 | * We'll leave the UART_IER_UUE bit set to 1 (enabled). | |
963 | */ | |
964 | DEBUG_AUTOCONF("Xscale "); | |
965 | up->port.type = PORT_XSCALE; | |
966 | up->capabilities |= UART_CAP_UUE; | |
967 | return; | |
968 | } | |
969 | } else { | |
970 | /* | |
971 | * If we got here we couldn't force the IER_UUE bit to 0. | |
972 | * Log it and continue. | |
973 | */ | |
974 | DEBUG_AUTOCONF("Couldn't force IER_UUE to 0 "); | |
975 | } | |
976 | serial_outp(up, UART_IER, iersave); | |
977 | } | |
978 | ||
979 | /* | |
980 | * This routine is called by rs_init() to initialize a specific serial | |
981 | * port. It determines what type of UART chip this serial port is | |
982 | * using: 8250, 16450, 16550, 16550A. The important question is | |
983 | * whether or not this UART is a 16550A or not, since this will | |
984 | * determine whether or not we can use its FIFO features or not. | |
985 | */ | |
986 | static void autoconfig(struct uart_8250_port *up, unsigned int probeflags) | |
987 | { | |
988 | unsigned char status1, scratch, scratch2, scratch3; | |
989 | unsigned char save_lcr, save_mcr; | |
990 | unsigned long flags; | |
991 | ||
992 | if (!up->port.iobase && !up->port.mapbase && !up->port.membase) | |
993 | return; | |
994 | ||
995 | DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%p): ", | |
996 | up->port.line, up->port.iobase, up->port.membase); | |
997 | ||
998 | /* | |
999 | * We really do need global IRQs disabled here - we're going to | |
1000 | * be frobbing the chips IRQ enable register to see if it exists. | |
1001 | */ | |
1002 | spin_lock_irqsave(&up->port.lock, flags); | |
1da177e4 LT |
1003 | |
1004 | up->capabilities = 0; | |
4ba5e35d | 1005 | up->bugs = 0; |
1da177e4 LT |
1006 | |
1007 | if (!(up->port.flags & UPF_BUGGY_UART)) { | |
1008 | /* | |
1009 | * Do a simple existence test first; if we fail this, | |
1010 | * there's no point trying anything else. | |
bd71c182 | 1011 | * |
1da177e4 LT |
1012 | * 0x80 is used as a nonsense port to prevent against |
1013 | * false positives due to ISA bus float. The | |
1014 | * assumption is that 0x80 is a non-existent port; | |
1015 | * which should be safe since include/asm/io.h also | |
1016 | * makes this assumption. | |
1017 | * | |
1018 | * Note: this is safe as long as MCR bit 4 is clear | |
1019 | * and the device is in "PC" mode. | |
1020 | */ | |
1021 | scratch = serial_inp(up, UART_IER); | |
1022 | serial_outp(up, UART_IER, 0); | |
1023 | #ifdef __i386__ | |
1024 | outb(0xff, 0x080); | |
1025 | #endif | |
48212008 TH |
1026 | /* |
1027 | * Mask out IER[7:4] bits for test as some UARTs (e.g. TL | |
1028 | * 16C754B) allow only to modify them if an EFR bit is set. | |
1029 | */ | |
1030 | scratch2 = serial_inp(up, UART_IER) & 0x0f; | |
1da177e4 LT |
1031 | serial_outp(up, UART_IER, 0x0F); |
1032 | #ifdef __i386__ | |
1033 | outb(0, 0x080); | |
1034 | #endif | |
48212008 | 1035 | scratch3 = serial_inp(up, UART_IER) & 0x0f; |
1da177e4 LT |
1036 | serial_outp(up, UART_IER, scratch); |
1037 | if (scratch2 != 0 || scratch3 != 0x0F) { | |
1038 | /* | |
1039 | * We failed; there's nothing here | |
1040 | */ | |
1041 | DEBUG_AUTOCONF("IER test failed (%02x, %02x) ", | |
1042 | scratch2, scratch3); | |
1043 | goto out; | |
1044 | } | |
1045 | } | |
1046 | ||
1047 | save_mcr = serial_in(up, UART_MCR); | |
1048 | save_lcr = serial_in(up, UART_LCR); | |
1049 | ||
bd71c182 | 1050 | /* |
1da177e4 LT |
1051 | * Check to see if a UART is really there. Certain broken |
1052 | * internal modems based on the Rockwell chipset fail this | |
1053 | * test, because they apparently don't implement the loopback | |
1054 | * test mode. So this test is skipped on the COM 1 through | |
1055 | * COM 4 ports. This *should* be safe, since no board | |
1056 | * manufacturer would be stupid enough to design a board | |
1057 | * that conflicts with COM 1-4 --- we hope! | |
1058 | */ | |
1059 | if (!(up->port.flags & UPF_SKIP_TEST)) { | |
1060 | serial_outp(up, UART_MCR, UART_MCR_LOOP | 0x0A); | |
1061 | status1 = serial_inp(up, UART_MSR) & 0xF0; | |
1062 | serial_outp(up, UART_MCR, save_mcr); | |
1063 | if (status1 != 0x90) { | |
1064 | DEBUG_AUTOCONF("LOOP test failed (%02x) ", | |
1065 | status1); | |
1066 | goto out; | |
1067 | } | |
1068 | } | |
1069 | ||
1070 | /* | |
1071 | * We're pretty sure there's a port here. Lets find out what | |
1072 | * type of port it is. The IIR top two bits allows us to find | |
6f0d618f | 1073 | * out if it's 8250 or 16450, 16550, 16550A or later. This |
1da177e4 LT |
1074 | * determines what we test for next. |
1075 | * | |
1076 | * We also initialise the EFR (if any) to zero for later. The | |
1077 | * EFR occupies the same register location as the FCR and IIR. | |
1078 | */ | |
1079 | serial_outp(up, UART_LCR, 0xBF); | |
1080 | serial_outp(up, UART_EFR, 0); | |
1081 | serial_outp(up, UART_LCR, 0); | |
1082 | ||
1083 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); | |
1084 | scratch = serial_in(up, UART_IIR) >> 6; | |
1085 | ||
1086 | DEBUG_AUTOCONF("iir=%d ", scratch); | |
1087 | ||
1088 | switch (scratch) { | |
1089 | case 0: | |
1090 | autoconfig_8250(up); | |
1091 | break; | |
1092 | case 1: | |
1093 | up->port.type = PORT_UNKNOWN; | |
1094 | break; | |
1095 | case 2: | |
1096 | up->port.type = PORT_16550; | |
1097 | break; | |
1098 | case 3: | |
1099 | autoconfig_16550a(up); | |
1100 | break; | |
1101 | } | |
1102 | ||
1103 | #ifdef CONFIG_SERIAL_8250_RSA | |
1104 | /* | |
1105 | * Only probe for RSA ports if we got the region. | |
1106 | */ | |
1107 | if (up->port.type == PORT_16550A && probeflags & PROBE_RSA) { | |
1108 | int i; | |
1109 | ||
1110 | for (i = 0 ; i < probe_rsa_count; ++i) { | |
1111 | if (probe_rsa[i] == up->port.iobase && | |
1112 | __enable_rsa(up)) { | |
1113 | up->port.type = PORT_RSA; | |
1114 | break; | |
1115 | } | |
1116 | } | |
1117 | } | |
1118 | #endif | |
21c614a7 PA |
1119 | |
1120 | #ifdef CONFIG_SERIAL_8250_AU1X00 | |
1121 | /* if access method is AU, it is a 16550 with a quirk */ | |
1122 | if (up->port.type == PORT_16550A && up->port.iotype == UPIO_AU) | |
1123 | up->bugs |= UART_BUG_NOMSR; | |
1124 | #endif | |
1125 | ||
1da177e4 LT |
1126 | serial_outp(up, UART_LCR, save_lcr); |
1127 | ||
1128 | if (up->capabilities != uart_config[up->port.type].flags) { | |
1129 | printk(KERN_WARNING | |
1130 | "ttyS%d: detected caps %08x should be %08x\n", | |
1131 | up->port.line, up->capabilities, | |
1132 | uart_config[up->port.type].flags); | |
1133 | } | |
1134 | ||
1135 | up->port.fifosize = uart_config[up->port.type].fifo_size; | |
1136 | up->capabilities = uart_config[up->port.type].flags; | |
1137 | up->tx_loadsz = uart_config[up->port.type].tx_loadsz; | |
1138 | ||
1139 | if (up->port.type == PORT_UNKNOWN) | |
1140 | goto out; | |
1141 | ||
1142 | /* | |
1143 | * Reset the UART. | |
1144 | */ | |
1145 | #ifdef CONFIG_SERIAL_8250_RSA | |
1146 | if (up->port.type == PORT_RSA) | |
1147 | serial_outp(up, UART_RSA_FRR, 0); | |
1148 | #endif | |
1149 | serial_outp(up, UART_MCR, save_mcr); | |
1150 | serial8250_clear_fifos(up); | |
40b36daa | 1151 | serial_in(up, UART_RX); |
5c8c755c LB |
1152 | if (up->capabilities & UART_CAP_UUE) |
1153 | serial_outp(up, UART_IER, UART_IER_UUE); | |
1154 | else | |
1155 | serial_outp(up, UART_IER, 0); | |
1da177e4 | 1156 | |
bd71c182 | 1157 | out: |
1da177e4 | 1158 | spin_unlock_irqrestore(&up->port.lock, flags); |
1da177e4 LT |
1159 | DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name); |
1160 | } | |
1161 | ||
1162 | static void autoconfig_irq(struct uart_8250_port *up) | |
1163 | { | |
1164 | unsigned char save_mcr, save_ier; | |
1165 | unsigned char save_ICP = 0; | |
1166 | unsigned int ICP = 0; | |
1167 | unsigned long irqs; | |
1168 | int irq; | |
1169 | ||
1170 | if (up->port.flags & UPF_FOURPORT) { | |
1171 | ICP = (up->port.iobase & 0xfe0) | 0x1f; | |
1172 | save_ICP = inb_p(ICP); | |
1173 | outb_p(0x80, ICP); | |
1174 | (void) inb_p(ICP); | |
1175 | } | |
1176 | ||
1177 | /* forget possible initially masked and pending IRQ */ | |
1178 | probe_irq_off(probe_irq_on()); | |
1179 | save_mcr = serial_inp(up, UART_MCR); | |
1180 | save_ier = serial_inp(up, UART_IER); | |
1181 | serial_outp(up, UART_MCR, UART_MCR_OUT1 | UART_MCR_OUT2); | |
bd71c182 | 1182 | |
1da177e4 LT |
1183 | irqs = probe_irq_on(); |
1184 | serial_outp(up, UART_MCR, 0); | |
6f803cd0 AC |
1185 | udelay(10); |
1186 | if (up->port.flags & UPF_FOURPORT) { | |
1da177e4 LT |
1187 | serial_outp(up, UART_MCR, |
1188 | UART_MCR_DTR | UART_MCR_RTS); | |
1189 | } else { | |
1190 | serial_outp(up, UART_MCR, | |
1191 | UART_MCR_DTR | UART_MCR_RTS | UART_MCR_OUT2); | |
1192 | } | |
1193 | serial_outp(up, UART_IER, 0x0f); /* enable all intrs */ | |
1194 | (void)serial_inp(up, UART_LSR); | |
1195 | (void)serial_inp(up, UART_RX); | |
1196 | (void)serial_inp(up, UART_IIR); | |
1197 | (void)serial_inp(up, UART_MSR); | |
1198 | serial_outp(up, UART_TX, 0xFF); | |
6f803cd0 | 1199 | udelay(20); |
1da177e4 LT |
1200 | irq = probe_irq_off(irqs); |
1201 | ||
1202 | serial_outp(up, UART_MCR, save_mcr); | |
1203 | serial_outp(up, UART_IER, save_ier); | |
1204 | ||
1205 | if (up->port.flags & UPF_FOURPORT) | |
1206 | outb_p(save_ICP, ICP); | |
1207 | ||
1208 | up->port.irq = (irq > 0) ? irq : 0; | |
1209 | } | |
1210 | ||
e763b90c RK |
1211 | static inline void __stop_tx(struct uart_8250_port *p) |
1212 | { | |
1213 | if (p->ier & UART_IER_THRI) { | |
1214 | p->ier &= ~UART_IER_THRI; | |
1215 | serial_out(p, UART_IER, p->ier); | |
1216 | } | |
1217 | } | |
1218 | ||
b129a8cc | 1219 | static void serial8250_stop_tx(struct uart_port *port) |
1da177e4 LT |
1220 | { |
1221 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1222 | ||
e763b90c | 1223 | __stop_tx(up); |
1da177e4 LT |
1224 | |
1225 | /* | |
e763b90c | 1226 | * We really want to stop the transmitter from sending. |
1da177e4 | 1227 | */ |
e763b90c | 1228 | if (up->port.type == PORT_16C950) { |
1da177e4 LT |
1229 | up->acr |= UART_ACR_TXDIS; |
1230 | serial_icr_write(up, UART_ACR, up->acr); | |
1231 | } | |
1232 | } | |
1233 | ||
55d3b282 RK |
1234 | static void transmit_chars(struct uart_8250_port *up); |
1235 | ||
b129a8cc | 1236 | static void serial8250_start_tx(struct uart_port *port) |
1da177e4 LT |
1237 | { |
1238 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1239 | ||
1240 | if (!(up->ier & UART_IER_THRI)) { | |
1241 | up->ier |= UART_IER_THRI; | |
1242 | serial_out(up, UART_IER, up->ier); | |
55d3b282 | 1243 | |
67f7654e | 1244 | if (up->bugs & UART_BUG_TXEN) { |
55d3b282 RK |
1245 | unsigned char lsr, iir; |
1246 | lsr = serial_in(up, UART_LSR); | |
ad4c2aa6 | 1247 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; |
bd71c182 TK |
1248 | iir = serial_in(up, UART_IIR) & 0x0f; |
1249 | if ((up->port.type == PORT_RM9000) ? | |
1250 | (lsr & UART_LSR_THRE && | |
1251 | (iir == UART_IIR_NO_INT || iir == UART_IIR_THRI)) : | |
1252 | (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT)) | |
55d3b282 RK |
1253 | transmit_chars(up); |
1254 | } | |
1da177e4 | 1255 | } |
e763b90c | 1256 | |
1da177e4 | 1257 | /* |
e763b90c | 1258 | * Re-enable the transmitter if we disabled it. |
1da177e4 | 1259 | */ |
e763b90c | 1260 | if (up->port.type == PORT_16C950 && up->acr & UART_ACR_TXDIS) { |
1da177e4 LT |
1261 | up->acr &= ~UART_ACR_TXDIS; |
1262 | serial_icr_write(up, UART_ACR, up->acr); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | static void serial8250_stop_rx(struct uart_port *port) | |
1267 | { | |
1268 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1269 | ||
1270 | up->ier &= ~UART_IER_RLSI; | |
1271 | up->port.read_status_mask &= ~UART_LSR_DR; | |
1272 | serial_out(up, UART_IER, up->ier); | |
1273 | } | |
1274 | ||
1275 | static void serial8250_enable_ms(struct uart_port *port) | |
1276 | { | |
1277 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1278 | ||
21c614a7 PA |
1279 | /* no MSR capabilities */ |
1280 | if (up->bugs & UART_BUG_NOMSR) | |
1281 | return; | |
1282 | ||
1da177e4 LT |
1283 | up->ier |= UART_IER_MSI; |
1284 | serial_out(up, UART_IER, up->ier); | |
1285 | } | |
1286 | ||
ea8874dc | 1287 | static void |
cc79aa9d | 1288 | receive_chars(struct uart_8250_port *up, unsigned int *status) |
1da177e4 | 1289 | { |
df4f4dd4 | 1290 | struct tty_struct *tty = up->port.info->port.tty; |
1da177e4 LT |
1291 | unsigned char ch, lsr = *status; |
1292 | int max_count = 256; | |
1293 | char flag; | |
1294 | ||
1295 | do { | |
1da177e4 LT |
1296 | ch = serial_inp(up, UART_RX); |
1297 | flag = TTY_NORMAL; | |
1298 | up->port.icount.rx++; | |
1299 | ||
ad4c2aa6 CM |
1300 | lsr |= up->lsr_saved_flags; |
1301 | up->lsr_saved_flags = 0; | |
1da177e4 | 1302 | |
ad4c2aa6 | 1303 | if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) { |
1da177e4 LT |
1304 | /* |
1305 | * For statistics only | |
1306 | */ | |
1307 | if (lsr & UART_LSR_BI) { | |
1308 | lsr &= ~(UART_LSR_FE | UART_LSR_PE); | |
1309 | up->port.icount.brk++; | |
1310 | /* | |
1311 | * We do the SysRQ and SAK checking | |
1312 | * here because otherwise the break | |
1313 | * may get masked by ignore_status_mask | |
1314 | * or read_status_mask. | |
1315 | */ | |
1316 | if (uart_handle_break(&up->port)) | |
1317 | goto ignore_char; | |
1318 | } else if (lsr & UART_LSR_PE) | |
1319 | up->port.icount.parity++; | |
1320 | else if (lsr & UART_LSR_FE) | |
1321 | up->port.icount.frame++; | |
1322 | if (lsr & UART_LSR_OE) | |
1323 | up->port.icount.overrun++; | |
1324 | ||
1325 | /* | |
23907eb8 | 1326 | * Mask off conditions which should be ignored. |
1da177e4 LT |
1327 | */ |
1328 | lsr &= up->port.read_status_mask; | |
1329 | ||
1330 | if (lsr & UART_LSR_BI) { | |
1331 | DEBUG_INTR("handling break...."); | |
1332 | flag = TTY_BREAK; | |
1333 | } else if (lsr & UART_LSR_PE) | |
1334 | flag = TTY_PARITY; | |
1335 | else if (lsr & UART_LSR_FE) | |
1336 | flag = TTY_FRAME; | |
1337 | } | |
7d12e780 | 1338 | if (uart_handle_sysrq_char(&up->port, ch)) |
1da177e4 | 1339 | goto ignore_char; |
05ab3014 RK |
1340 | |
1341 | uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag); | |
1342 | ||
6f803cd0 | 1343 | ignore_char: |
1da177e4 LT |
1344 | lsr = serial_inp(up, UART_LSR); |
1345 | } while ((lsr & UART_LSR_DR) && (max_count-- > 0)); | |
1346 | spin_unlock(&up->port.lock); | |
1347 | tty_flip_buffer_push(tty); | |
1348 | spin_lock(&up->port.lock); | |
1349 | *status = lsr; | |
1350 | } | |
1351 | ||
ea8874dc | 1352 | static void transmit_chars(struct uart_8250_port *up) |
1da177e4 LT |
1353 | { |
1354 | struct circ_buf *xmit = &up->port.info->xmit; | |
1355 | int count; | |
1356 | ||
1357 | if (up->port.x_char) { | |
1358 | serial_outp(up, UART_TX, up->port.x_char); | |
1359 | up->port.icount.tx++; | |
1360 | up->port.x_char = 0; | |
1361 | return; | |
1362 | } | |
b129a8cc RK |
1363 | if (uart_tx_stopped(&up->port)) { |
1364 | serial8250_stop_tx(&up->port); | |
1365 | return; | |
1366 | } | |
1367 | if (uart_circ_empty(xmit)) { | |
e763b90c | 1368 | __stop_tx(up); |
1da177e4 LT |
1369 | return; |
1370 | } | |
1371 | ||
1372 | count = up->tx_loadsz; | |
1373 | do { | |
1374 | serial_out(up, UART_TX, xmit->buf[xmit->tail]); | |
1375 | xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1); | |
1376 | up->port.icount.tx++; | |
1377 | if (uart_circ_empty(xmit)) | |
1378 | break; | |
1379 | } while (--count > 0); | |
1380 | ||
1381 | if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) | |
1382 | uart_write_wakeup(&up->port); | |
1383 | ||
1384 | DEBUG_INTR("THRE..."); | |
1385 | ||
1386 | if (uart_circ_empty(xmit)) | |
e763b90c | 1387 | __stop_tx(up); |
1da177e4 LT |
1388 | } |
1389 | ||
2af7cd68 | 1390 | static unsigned int check_modem_status(struct uart_8250_port *up) |
1da177e4 | 1391 | { |
2af7cd68 RK |
1392 | unsigned int status = serial_in(up, UART_MSR); |
1393 | ||
ad4c2aa6 CM |
1394 | status |= up->msr_saved_flags; |
1395 | up->msr_saved_flags = 0; | |
fdc30b3d TI |
1396 | if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI && |
1397 | up->port.info != NULL) { | |
2af7cd68 RK |
1398 | if (status & UART_MSR_TERI) |
1399 | up->port.icount.rng++; | |
1400 | if (status & UART_MSR_DDSR) | |
1401 | up->port.icount.dsr++; | |
1402 | if (status & UART_MSR_DDCD) | |
1403 | uart_handle_dcd_change(&up->port, status & UART_MSR_DCD); | |
1404 | if (status & UART_MSR_DCTS) | |
1405 | uart_handle_cts_change(&up->port, status & UART_MSR_CTS); | |
1406 | ||
1407 | wake_up_interruptible(&up->port.info->delta_msr_wait); | |
1408 | } | |
1da177e4 | 1409 | |
2af7cd68 | 1410 | return status; |
1da177e4 LT |
1411 | } |
1412 | ||
1413 | /* | |
1414 | * This handles the interrupt from one port. | |
1415 | */ | |
1416 | static inline void | |
7d12e780 | 1417 | serial8250_handle_port(struct uart_8250_port *up) |
1da177e4 | 1418 | { |
45e24601 | 1419 | unsigned int status; |
4bf3631c | 1420 | unsigned long flags; |
45e24601 | 1421 | |
4bf3631c | 1422 | spin_lock_irqsave(&up->port.lock, flags); |
45e24601 RK |
1423 | |
1424 | status = serial_inp(up, UART_LSR); | |
1da177e4 LT |
1425 | |
1426 | DEBUG_INTR("status = %x...", status); | |
1427 | ||
1428 | if (status & UART_LSR_DR) | |
7d12e780 | 1429 | receive_chars(up, &status); |
1da177e4 LT |
1430 | check_modem_status(up); |
1431 | if (status & UART_LSR_THRE) | |
1432 | transmit_chars(up); | |
45e24601 | 1433 | |
4bf3631c | 1434 | spin_unlock_irqrestore(&up->port.lock, flags); |
1da177e4 LT |
1435 | } |
1436 | ||
1437 | /* | |
1438 | * This is the serial driver's interrupt routine. | |
1439 | * | |
1440 | * Arjan thinks the old way was overly complex, so it got simplified. | |
1441 | * Alan disagrees, saying that need the complexity to handle the weird | |
1442 | * nature of ISA shared interrupts. (This is a special exception.) | |
1443 | * | |
1444 | * In order to handle ISA shared interrupts properly, we need to check | |
1445 | * that all ports have been serviced, and therefore the ISA interrupt | |
1446 | * line has been de-asserted. | |
1447 | * | |
1448 | * This means we need to loop through all ports. checking that they | |
1449 | * don't have an interrupt pending. | |
1450 | */ | |
7d12e780 | 1451 | static irqreturn_t serial8250_interrupt(int irq, void *dev_id) |
1da177e4 LT |
1452 | { |
1453 | struct irq_info *i = dev_id; | |
1454 | struct list_head *l, *end = NULL; | |
1455 | int pass_counter = 0, handled = 0; | |
1456 | ||
1457 | DEBUG_INTR("serial8250_interrupt(%d)...", irq); | |
1458 | ||
1459 | spin_lock(&i->lock); | |
1460 | ||
1461 | l = i->head; | |
1462 | do { | |
1463 | struct uart_8250_port *up; | |
1464 | unsigned int iir; | |
1465 | ||
1466 | up = list_entry(l, struct uart_8250_port, list); | |
1467 | ||
1468 | iir = serial_in(up, UART_IIR); | |
1469 | if (!(iir & UART_IIR_NO_INT)) { | |
7d12e780 | 1470 | serial8250_handle_port(up); |
1da177e4 LT |
1471 | |
1472 | handled = 1; | |
1473 | ||
beab697a MSJ |
1474 | end = NULL; |
1475 | } else if (up->port.iotype == UPIO_DWAPB && | |
1476 | (iir & UART_IIR_BUSY) == UART_IIR_BUSY) { | |
1477 | /* The DesignWare APB UART has an Busy Detect (0x07) | |
1478 | * interrupt meaning an LCR write attempt occured while the | |
1479 | * UART was busy. The interrupt must be cleared by reading | |
1480 | * the UART status register (USR) and the LCR re-written. */ | |
1481 | unsigned int status; | |
1482 | status = *(volatile u32 *)up->port.private_data; | |
1483 | serial_out(up, UART_LCR, up->lcr); | |
1484 | ||
1485 | handled = 1; | |
1486 | ||
1da177e4 LT |
1487 | end = NULL; |
1488 | } else if (end == NULL) | |
1489 | end = l; | |
1490 | ||
1491 | l = l->next; | |
1492 | ||
1493 | if (l == i->head && pass_counter++ > PASS_LIMIT) { | |
1494 | /* If we hit this, we're dead. */ | |
1495 | printk(KERN_ERR "serial8250: too much work for " | |
1496 | "irq%d\n", irq); | |
1497 | break; | |
1498 | } | |
1499 | } while (l != end); | |
1500 | ||
1501 | spin_unlock(&i->lock); | |
1502 | ||
1503 | DEBUG_INTR("end.\n"); | |
1504 | ||
1505 | return IRQ_RETVAL(handled); | |
1506 | } | |
1507 | ||
1508 | /* | |
1509 | * To support ISA shared interrupts, we need to have one interrupt | |
1510 | * handler that ensures that the IRQ line has been deasserted | |
1511 | * before returning. Failing to do this will result in the IRQ | |
1512 | * line being stuck active, and, since ISA irqs are edge triggered, | |
1513 | * no more IRQs will be seen. | |
1514 | */ | |
1515 | static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up) | |
1516 | { | |
1517 | spin_lock_irq(&i->lock); | |
1518 | ||
1519 | if (!list_empty(i->head)) { | |
1520 | if (i->head == &up->list) | |
1521 | i->head = i->head->next; | |
1522 | list_del(&up->list); | |
1523 | } else { | |
1524 | BUG_ON(i->head != &up->list); | |
1525 | i->head = NULL; | |
1526 | } | |
1527 | ||
1528 | spin_unlock_irq(&i->lock); | |
1529 | } | |
1530 | ||
1531 | static int serial_link_irq_chain(struct uart_8250_port *up) | |
1532 | { | |
1533 | struct irq_info *i = irq_lists + up->port.irq; | |
40663cc7 | 1534 | int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0; |
1da177e4 LT |
1535 | |
1536 | spin_lock_irq(&i->lock); | |
1537 | ||
1538 | if (i->head) { | |
1539 | list_add(&up->list, i->head); | |
1540 | spin_unlock_irq(&i->lock); | |
1541 | ||
1542 | ret = 0; | |
1543 | } else { | |
1544 | INIT_LIST_HEAD(&up->list); | |
1545 | i->head = &up->list; | |
1546 | spin_unlock_irq(&i->lock); | |
1547 | ||
1548 | ret = request_irq(up->port.irq, serial8250_interrupt, | |
1549 | irq_flags, "serial", i); | |
1550 | if (ret < 0) | |
1551 | serial_do_unlink(i, up); | |
1552 | } | |
1553 | ||
1554 | return ret; | |
1555 | } | |
1556 | ||
1557 | static void serial_unlink_irq_chain(struct uart_8250_port *up) | |
1558 | { | |
1559 | struct irq_info *i = irq_lists + up->port.irq; | |
1560 | ||
1561 | BUG_ON(i->head == NULL); | |
1562 | ||
1563 | if (list_empty(i->head)) | |
1564 | free_irq(up->port.irq, i); | |
1565 | ||
1566 | serial_do_unlink(i, up); | |
1567 | } | |
1568 | ||
40b36daa AW |
1569 | /* Base timer interval for polling */ |
1570 | static inline int poll_timeout(int timeout) | |
1571 | { | |
1572 | return timeout > 6 ? (timeout / 2 - 2) : 1; | |
1573 | } | |
1574 | ||
1da177e4 LT |
1575 | /* |
1576 | * This function is used to handle ports that do not have an | |
1577 | * interrupt. This doesn't work very well for 16450's, but gives | |
1578 | * barely passable results for a 16550A. (Although at the expense | |
1579 | * of much CPU overhead). | |
1580 | */ | |
1581 | static void serial8250_timeout(unsigned long data) | |
1582 | { | |
1583 | struct uart_8250_port *up = (struct uart_8250_port *)data; | |
1da177e4 LT |
1584 | unsigned int iir; |
1585 | ||
1586 | iir = serial_in(up, UART_IIR); | |
45e24601 | 1587 | if (!(iir & UART_IIR_NO_INT)) |
7d12e780 | 1588 | serial8250_handle_port(up); |
40b36daa AW |
1589 | mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout)); |
1590 | } | |
1591 | ||
1592 | static void serial8250_backup_timeout(unsigned long data) | |
1593 | { | |
1594 | struct uart_8250_port *up = (struct uart_8250_port *)data; | |
ad4c2aa6 CM |
1595 | unsigned int iir, ier = 0, lsr; |
1596 | unsigned long flags; | |
40b36daa AW |
1597 | |
1598 | /* | |
1599 | * Must disable interrupts or else we risk racing with the interrupt | |
1600 | * based handler. | |
1601 | */ | |
1602 | if (is_real_interrupt(up->port.irq)) { | |
1603 | ier = serial_in(up, UART_IER); | |
1604 | serial_out(up, UART_IER, 0); | |
1605 | } | |
1da177e4 | 1606 | |
40b36daa AW |
1607 | iir = serial_in(up, UART_IIR); |
1608 | ||
1609 | /* | |
1610 | * This should be a safe test for anyone who doesn't trust the | |
1611 | * IIR bits on their UART, but it's specifically designed for | |
1612 | * the "Diva" UART used on the management processor on many HP | |
1613 | * ia64 and parisc boxes. | |
1614 | */ | |
ad4c2aa6 CM |
1615 | spin_lock_irqsave(&up->port.lock, flags); |
1616 | lsr = serial_in(up, UART_LSR); | |
1617 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; | |
1618 | spin_unlock_irqrestore(&up->port.lock, flags); | |
40b36daa AW |
1619 | if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) && |
1620 | (!uart_circ_empty(&up->port.info->xmit) || up->port.x_char) && | |
ad4c2aa6 | 1621 | (lsr & UART_LSR_THRE)) { |
40b36daa AW |
1622 | iir &= ~(UART_IIR_ID | UART_IIR_NO_INT); |
1623 | iir |= UART_IIR_THRI; | |
1624 | } | |
1625 | ||
1626 | if (!(iir & UART_IIR_NO_INT)) | |
1627 | serial8250_handle_port(up); | |
1628 | ||
1629 | if (is_real_interrupt(up->port.irq)) | |
1630 | serial_out(up, UART_IER, ier); | |
1631 | ||
1632 | /* Standard timer interval plus 0.2s to keep the port running */ | |
6f803cd0 AC |
1633 | mod_timer(&up->timer, |
1634 | jiffies + poll_timeout(up->port.timeout) + HZ / 5); | |
1da177e4 LT |
1635 | } |
1636 | ||
1637 | static unsigned int serial8250_tx_empty(struct uart_port *port) | |
1638 | { | |
1639 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1640 | unsigned long flags; | |
ad4c2aa6 | 1641 | unsigned int lsr; |
1da177e4 LT |
1642 | |
1643 | spin_lock_irqsave(&up->port.lock, flags); | |
ad4c2aa6 CM |
1644 | lsr = serial_in(up, UART_LSR); |
1645 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; | |
1da177e4 LT |
1646 | spin_unlock_irqrestore(&up->port.lock, flags); |
1647 | ||
ad4c2aa6 | 1648 | return lsr & UART_LSR_TEMT ? TIOCSER_TEMT : 0; |
1da177e4 LT |
1649 | } |
1650 | ||
1651 | static unsigned int serial8250_get_mctrl(struct uart_port *port) | |
1652 | { | |
1653 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
2af7cd68 | 1654 | unsigned int status; |
1da177e4 LT |
1655 | unsigned int ret; |
1656 | ||
2af7cd68 | 1657 | status = check_modem_status(up); |
1da177e4 LT |
1658 | |
1659 | ret = 0; | |
1660 | if (status & UART_MSR_DCD) | |
1661 | ret |= TIOCM_CAR; | |
1662 | if (status & UART_MSR_RI) | |
1663 | ret |= TIOCM_RNG; | |
1664 | if (status & UART_MSR_DSR) | |
1665 | ret |= TIOCM_DSR; | |
1666 | if (status & UART_MSR_CTS) | |
1667 | ret |= TIOCM_CTS; | |
1668 | return ret; | |
1669 | } | |
1670 | ||
1671 | static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl) | |
1672 | { | |
1673 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1674 | unsigned char mcr = 0; | |
1675 | ||
1676 | if (mctrl & TIOCM_RTS) | |
1677 | mcr |= UART_MCR_RTS; | |
1678 | if (mctrl & TIOCM_DTR) | |
1679 | mcr |= UART_MCR_DTR; | |
1680 | if (mctrl & TIOCM_OUT1) | |
1681 | mcr |= UART_MCR_OUT1; | |
1682 | if (mctrl & TIOCM_OUT2) | |
1683 | mcr |= UART_MCR_OUT2; | |
1684 | if (mctrl & TIOCM_LOOP) | |
1685 | mcr |= UART_MCR_LOOP; | |
1686 | ||
1687 | mcr = (mcr & up->mcr_mask) | up->mcr_force | up->mcr; | |
1688 | ||
1689 | serial_out(up, UART_MCR, mcr); | |
1690 | } | |
1691 | ||
1692 | static void serial8250_break_ctl(struct uart_port *port, int break_state) | |
1693 | { | |
1694 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1695 | unsigned long flags; | |
1696 | ||
1697 | spin_lock_irqsave(&up->port.lock, flags); | |
1698 | if (break_state == -1) | |
1699 | up->lcr |= UART_LCR_SBC; | |
1700 | else | |
1701 | up->lcr &= ~UART_LCR_SBC; | |
1702 | serial_out(up, UART_LCR, up->lcr); | |
1703 | spin_unlock_irqrestore(&up->port.lock, flags); | |
1704 | } | |
1705 | ||
40b36daa AW |
1706 | #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE) |
1707 | ||
1708 | /* | |
1709 | * Wait for transmitter & holding register to empty | |
1710 | */ | |
1711 | static inline void wait_for_xmitr(struct uart_8250_port *up, int bits) | |
1712 | { | |
1713 | unsigned int status, tmout = 10000; | |
1714 | ||
1715 | /* Wait up to 10ms for the character(s) to be sent. */ | |
1716 | do { | |
1717 | status = serial_in(up, UART_LSR); | |
1718 | ||
ad4c2aa6 | 1719 | up->lsr_saved_flags |= status & LSR_SAVE_FLAGS; |
40b36daa AW |
1720 | |
1721 | if (--tmout == 0) | |
1722 | break; | |
1723 | udelay(1); | |
1724 | } while ((status & bits) != bits); | |
1725 | ||
1726 | /* Wait up to 1s for flow control if necessary */ | |
1727 | if (up->port.flags & UPF_CONS_FLOW) { | |
ad4c2aa6 CM |
1728 | unsigned int tmout; |
1729 | for (tmout = 1000000; tmout; tmout--) { | |
1730 | unsigned int msr = serial_in(up, UART_MSR); | |
1731 | up->msr_saved_flags |= msr & MSR_SAVE_FLAGS; | |
1732 | if (msr & UART_MSR_CTS) | |
1733 | break; | |
40b36daa AW |
1734 | udelay(1); |
1735 | touch_nmi_watchdog(); | |
1736 | } | |
1737 | } | |
1738 | } | |
1739 | ||
f2d937f3 JW |
1740 | #ifdef CONFIG_CONSOLE_POLL |
1741 | /* | |
1742 | * Console polling routines for writing and reading from the uart while | |
1743 | * in an interrupt or debug context. | |
1744 | */ | |
1745 | ||
1746 | static int serial8250_get_poll_char(struct uart_port *port) | |
1747 | { | |
1748 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1749 | unsigned char lsr = serial_inp(up, UART_LSR); | |
1750 | ||
1751 | while (!(lsr & UART_LSR_DR)) | |
1752 | lsr = serial_inp(up, UART_LSR); | |
1753 | ||
1754 | return serial_inp(up, UART_RX); | |
1755 | } | |
1756 | ||
1757 | ||
1758 | static void serial8250_put_poll_char(struct uart_port *port, | |
1759 | unsigned char c) | |
1760 | { | |
1761 | unsigned int ier; | |
1762 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1763 | ||
1764 | /* | |
1765 | * First save the IER then disable the interrupts | |
1766 | */ | |
1767 | ier = serial_in(up, UART_IER); | |
1768 | if (up->capabilities & UART_CAP_UUE) | |
1769 | serial_out(up, UART_IER, UART_IER_UUE); | |
1770 | else | |
1771 | serial_out(up, UART_IER, 0); | |
1772 | ||
1773 | wait_for_xmitr(up, BOTH_EMPTY); | |
1774 | /* | |
1775 | * Send the character out. | |
1776 | * If a LF, also do CR... | |
1777 | */ | |
1778 | serial_out(up, UART_TX, c); | |
1779 | if (c == 10) { | |
1780 | wait_for_xmitr(up, BOTH_EMPTY); | |
1781 | serial_out(up, UART_TX, 13); | |
1782 | } | |
1783 | ||
1784 | /* | |
1785 | * Finally, wait for transmitter to become empty | |
1786 | * and restore the IER | |
1787 | */ | |
1788 | wait_for_xmitr(up, BOTH_EMPTY); | |
1789 | serial_out(up, UART_IER, ier); | |
1790 | } | |
1791 | ||
1792 | #endif /* CONFIG_CONSOLE_POLL */ | |
1793 | ||
1da177e4 LT |
1794 | static int serial8250_startup(struct uart_port *port) |
1795 | { | |
1796 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1797 | unsigned long flags; | |
55d3b282 | 1798 | unsigned char lsr, iir; |
1da177e4 LT |
1799 | int retval; |
1800 | ||
1801 | up->capabilities = uart_config[up->port.type].flags; | |
1802 | up->mcr = 0; | |
1803 | ||
1804 | if (up->port.type == PORT_16C950) { | |
1805 | /* Wake up and initialize UART */ | |
1806 | up->acr = 0; | |
1807 | serial_outp(up, UART_LCR, 0xBF); | |
1808 | serial_outp(up, UART_EFR, UART_EFR_ECB); | |
1809 | serial_outp(up, UART_IER, 0); | |
1810 | serial_outp(up, UART_LCR, 0); | |
1811 | serial_icr_write(up, UART_CSR, 0); /* Reset the UART */ | |
1812 | serial_outp(up, UART_LCR, 0xBF); | |
1813 | serial_outp(up, UART_EFR, UART_EFR_ECB); | |
1814 | serial_outp(up, UART_LCR, 0); | |
1815 | } | |
1816 | ||
1817 | #ifdef CONFIG_SERIAL_8250_RSA | |
1818 | /* | |
1819 | * If this is an RSA port, see if we can kick it up to the | |
1820 | * higher speed clock. | |
1821 | */ | |
1822 | enable_rsa(up); | |
1823 | #endif | |
1824 | ||
1825 | /* | |
1826 | * Clear the FIFO buffers and disable them. | |
7f927fcc | 1827 | * (they will be reenabled in set_termios()) |
1da177e4 LT |
1828 | */ |
1829 | serial8250_clear_fifos(up); | |
1830 | ||
1831 | /* | |
1832 | * Clear the interrupt registers. | |
1833 | */ | |
1834 | (void) serial_inp(up, UART_LSR); | |
1835 | (void) serial_inp(up, UART_RX); | |
1836 | (void) serial_inp(up, UART_IIR); | |
1837 | (void) serial_inp(up, UART_MSR); | |
1838 | ||
1839 | /* | |
1840 | * At this point, there's no way the LSR could still be 0xff; | |
1841 | * if it is, then bail out, because there's likely no UART | |
1842 | * here. | |
1843 | */ | |
1844 | if (!(up->port.flags & UPF_BUGGY_UART) && | |
1845 | (serial_inp(up, UART_LSR) == 0xff)) { | |
1846 | printk("ttyS%d: LSR safety check engaged!\n", up->port.line); | |
1847 | return -ENODEV; | |
1848 | } | |
1849 | ||
1850 | /* | |
1851 | * For a XR16C850, we need to set the trigger levels | |
1852 | */ | |
1853 | if (up->port.type == PORT_16850) { | |
1854 | unsigned char fctr; | |
1855 | ||
1856 | serial_outp(up, UART_LCR, 0xbf); | |
1857 | ||
1858 | fctr = serial_inp(up, UART_FCTR) & ~(UART_FCTR_RX|UART_FCTR_TX); | |
1859 | serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_RX); | |
1860 | serial_outp(up, UART_TRG, UART_TRG_96); | |
1861 | serial_outp(up, UART_FCTR, fctr | UART_FCTR_TRGD | UART_FCTR_TX); | |
1862 | serial_outp(up, UART_TRG, UART_TRG_96); | |
1863 | ||
1864 | serial_outp(up, UART_LCR, 0); | |
1865 | } | |
1866 | ||
40b36daa | 1867 | if (is_real_interrupt(up->port.irq)) { |
01c194d9 | 1868 | unsigned char iir1; |
40b36daa AW |
1869 | /* |
1870 | * Test for UARTs that do not reassert THRE when the | |
1871 | * transmitter is idle and the interrupt has already | |
1872 | * been cleared. Real 16550s should always reassert | |
1873 | * this interrupt whenever the transmitter is idle and | |
1874 | * the interrupt is enabled. Delays are necessary to | |
1875 | * allow register changes to become visible. | |
1876 | */ | |
1877 | spin_lock_irqsave(&up->port.lock, flags); | |
1878 | ||
1879 | wait_for_xmitr(up, UART_LSR_THRE); | |
1880 | serial_out_sync(up, UART_IER, UART_IER_THRI); | |
1881 | udelay(1); /* allow THRE to set */ | |
01c194d9 | 1882 | iir1 = serial_in(up, UART_IIR); |
40b36daa AW |
1883 | serial_out(up, UART_IER, 0); |
1884 | serial_out_sync(up, UART_IER, UART_IER_THRI); | |
1885 | udelay(1); /* allow a working UART time to re-assert THRE */ | |
1886 | iir = serial_in(up, UART_IIR); | |
1887 | serial_out(up, UART_IER, 0); | |
1888 | ||
1889 | spin_unlock_irqrestore(&up->port.lock, flags); | |
1890 | ||
1891 | /* | |
1892 | * If the interrupt is not reasserted, setup a timer to | |
1893 | * kick the UART on a regular basis. | |
1894 | */ | |
01c194d9 | 1895 | if (!(iir1 & UART_IIR_NO_INT) && (iir & UART_IIR_NO_INT)) { |
40b36daa AW |
1896 | pr_debug("ttyS%d - using backup timer\n", port->line); |
1897 | up->timer.function = serial8250_backup_timeout; | |
1898 | up->timer.data = (unsigned long)up; | |
1899 | mod_timer(&up->timer, jiffies + | |
6f803cd0 | 1900 | poll_timeout(up->port.timeout) + HZ / 5); |
40b36daa AW |
1901 | } |
1902 | } | |
1903 | ||
1da177e4 LT |
1904 | /* |
1905 | * If the "interrupt" for this port doesn't correspond with any | |
1906 | * hardware interrupt, we use a timer-based system. The original | |
1907 | * driver used to do this with IRQ0. | |
1908 | */ | |
1909 | if (!is_real_interrupt(up->port.irq)) { | |
1da177e4 | 1910 | up->timer.data = (unsigned long)up; |
40b36daa | 1911 | mod_timer(&up->timer, jiffies + poll_timeout(up->port.timeout)); |
1da177e4 LT |
1912 | } else { |
1913 | retval = serial_link_irq_chain(up); | |
1914 | if (retval) | |
1915 | return retval; | |
1916 | } | |
1917 | ||
1918 | /* | |
1919 | * Now, initialize the UART | |
1920 | */ | |
1921 | serial_outp(up, UART_LCR, UART_LCR_WLEN8); | |
1922 | ||
1923 | spin_lock_irqsave(&up->port.lock, flags); | |
1924 | if (up->port.flags & UPF_FOURPORT) { | |
1925 | if (!is_real_interrupt(up->port.irq)) | |
1926 | up->port.mctrl |= TIOCM_OUT1; | |
1927 | } else | |
1928 | /* | |
1929 | * Most PC uarts need OUT2 raised to enable interrupts. | |
1930 | */ | |
1931 | if (is_real_interrupt(up->port.irq)) | |
1932 | up->port.mctrl |= TIOCM_OUT2; | |
1933 | ||
1934 | serial8250_set_mctrl(&up->port, up->port.mctrl); | |
55d3b282 RK |
1935 | |
1936 | /* | |
1937 | * Do a quick test to see if we receive an | |
1938 | * interrupt when we enable the TX irq. | |
1939 | */ | |
1940 | serial_outp(up, UART_IER, UART_IER_THRI); | |
1941 | lsr = serial_in(up, UART_LSR); | |
1942 | iir = serial_in(up, UART_IIR); | |
1943 | serial_outp(up, UART_IER, 0); | |
1944 | ||
1945 | if (lsr & UART_LSR_TEMT && iir & UART_IIR_NO_INT) { | |
67f7654e RK |
1946 | if (!(up->bugs & UART_BUG_TXEN)) { |
1947 | up->bugs |= UART_BUG_TXEN; | |
55d3b282 RK |
1948 | pr_debug("ttyS%d - enabling bad tx status workarounds\n", |
1949 | port->line); | |
1950 | } | |
1951 | } else { | |
67f7654e | 1952 | up->bugs &= ~UART_BUG_TXEN; |
55d3b282 RK |
1953 | } |
1954 | ||
1da177e4 LT |
1955 | spin_unlock_irqrestore(&up->port.lock, flags); |
1956 | ||
ad4c2aa6 CM |
1957 | /* |
1958 | * Clear the interrupt registers again for luck, and clear the | |
1959 | * saved flags to avoid getting false values from polling | |
1960 | * routines or the previous session. | |
1961 | */ | |
1962 | serial_inp(up, UART_LSR); | |
1963 | serial_inp(up, UART_RX); | |
1964 | serial_inp(up, UART_IIR); | |
1965 | serial_inp(up, UART_MSR); | |
1966 | up->lsr_saved_flags = 0; | |
1967 | up->msr_saved_flags = 0; | |
1968 | ||
1da177e4 LT |
1969 | /* |
1970 | * Finally, enable interrupts. Note: Modem status interrupts | |
1971 | * are set via set_termios(), which will be occurring imminently | |
1972 | * anyway, so we don't enable them here. | |
1973 | */ | |
1974 | up->ier = UART_IER_RLSI | UART_IER_RDI; | |
1975 | serial_outp(up, UART_IER, up->ier); | |
1976 | ||
1977 | if (up->port.flags & UPF_FOURPORT) { | |
1978 | unsigned int icp; | |
1979 | /* | |
1980 | * Enable interrupts on the AST Fourport board | |
1981 | */ | |
1982 | icp = (up->port.iobase & 0xfe0) | 0x01f; | |
1983 | outb_p(0x80, icp); | |
1984 | (void) inb_p(icp); | |
1985 | } | |
1986 | ||
1da177e4 LT |
1987 | return 0; |
1988 | } | |
1989 | ||
1990 | static void serial8250_shutdown(struct uart_port *port) | |
1991 | { | |
1992 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
1993 | unsigned long flags; | |
1994 | ||
1995 | /* | |
1996 | * Disable interrupts from this port | |
1997 | */ | |
1998 | up->ier = 0; | |
1999 | serial_outp(up, UART_IER, 0); | |
2000 | ||
2001 | spin_lock_irqsave(&up->port.lock, flags); | |
2002 | if (up->port.flags & UPF_FOURPORT) { | |
2003 | /* reset interrupts on the AST Fourport board */ | |
2004 | inb((up->port.iobase & 0xfe0) | 0x1f); | |
2005 | up->port.mctrl |= TIOCM_OUT1; | |
2006 | } else | |
2007 | up->port.mctrl &= ~TIOCM_OUT2; | |
2008 | ||
2009 | serial8250_set_mctrl(&up->port, up->port.mctrl); | |
2010 | spin_unlock_irqrestore(&up->port.lock, flags); | |
2011 | ||
2012 | /* | |
2013 | * Disable break condition and FIFOs | |
2014 | */ | |
2015 | serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC); | |
2016 | serial8250_clear_fifos(up); | |
2017 | ||
2018 | #ifdef CONFIG_SERIAL_8250_RSA | |
2019 | /* | |
2020 | * Reset the RSA board back to 115kbps compat mode. | |
2021 | */ | |
2022 | disable_rsa(up); | |
2023 | #endif | |
2024 | ||
2025 | /* | |
2026 | * Read data port to reset things, and then unlink from | |
2027 | * the IRQ chain. | |
2028 | */ | |
2029 | (void) serial_in(up, UART_RX); | |
2030 | ||
40b36daa AW |
2031 | del_timer_sync(&up->timer); |
2032 | up->timer.function = serial8250_timeout; | |
2033 | if (is_real_interrupt(up->port.irq)) | |
1da177e4 LT |
2034 | serial_unlink_irq_chain(up); |
2035 | } | |
2036 | ||
2037 | static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud) | |
2038 | { | |
2039 | unsigned int quot; | |
2040 | ||
2041 | /* | |
2042 | * Handle magic divisors for baud rates above baud_base on | |
2043 | * SMSC SuperIO chips. | |
2044 | */ | |
2045 | if ((port->flags & UPF_MAGIC_MULTIPLIER) && | |
2046 | baud == (port->uartclk/4)) | |
2047 | quot = 0x8001; | |
2048 | else if ((port->flags & UPF_MAGIC_MULTIPLIER) && | |
2049 | baud == (port->uartclk/8)) | |
2050 | quot = 0x8002; | |
2051 | else | |
2052 | quot = uart_get_divisor(port, baud); | |
2053 | ||
2054 | return quot; | |
2055 | } | |
2056 | ||
2057 | static void | |
606d099c AC |
2058 | serial8250_set_termios(struct uart_port *port, struct ktermios *termios, |
2059 | struct ktermios *old) | |
1da177e4 LT |
2060 | { |
2061 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
2062 | unsigned char cval, fcr = 0; | |
2063 | unsigned long flags; | |
2064 | unsigned int baud, quot; | |
2065 | ||
2066 | switch (termios->c_cflag & CSIZE) { | |
2067 | case CS5: | |
0a8b80c5 | 2068 | cval = UART_LCR_WLEN5; |
1da177e4 LT |
2069 | break; |
2070 | case CS6: | |
0a8b80c5 | 2071 | cval = UART_LCR_WLEN6; |
1da177e4 LT |
2072 | break; |
2073 | case CS7: | |
0a8b80c5 | 2074 | cval = UART_LCR_WLEN7; |
1da177e4 LT |
2075 | break; |
2076 | default: | |
2077 | case CS8: | |
0a8b80c5 | 2078 | cval = UART_LCR_WLEN8; |
1da177e4 LT |
2079 | break; |
2080 | } | |
2081 | ||
2082 | if (termios->c_cflag & CSTOPB) | |
0a8b80c5 | 2083 | cval |= UART_LCR_STOP; |
1da177e4 LT |
2084 | if (termios->c_cflag & PARENB) |
2085 | cval |= UART_LCR_PARITY; | |
2086 | if (!(termios->c_cflag & PARODD)) | |
2087 | cval |= UART_LCR_EPAR; | |
2088 | #ifdef CMSPAR | |
2089 | if (termios->c_cflag & CMSPAR) | |
2090 | cval |= UART_LCR_SPAR; | |
2091 | #endif | |
2092 | ||
2093 | /* | |
2094 | * Ask the core to calculate the divisor for us. | |
2095 | */ | |
bd71c182 | 2096 | baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); |
1da177e4 LT |
2097 | quot = serial8250_get_divisor(port, baud); |
2098 | ||
2099 | /* | |
4ba5e35d | 2100 | * Oxford Semi 952 rev B workaround |
1da177e4 | 2101 | */ |
4ba5e35d | 2102 | if (up->bugs & UART_BUG_QUOT && (quot & 0xff) == 0) |
3e8d4e20 | 2103 | quot++; |
1da177e4 LT |
2104 | |
2105 | if (up->capabilities & UART_CAP_FIFO && up->port.fifosize > 1) { | |
2106 | if (baud < 2400) | |
2107 | fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1; | |
2108 | else | |
2109 | fcr = uart_config[up->port.type].fcr; | |
2110 | } | |
2111 | ||
2112 | /* | |
2113 | * MCR-based auto flow control. When AFE is enabled, RTS will be | |
2114 | * deasserted when the receive FIFO contains more characters than | |
2115 | * the trigger, or the MCR RTS bit is cleared. In the case where | |
2116 | * the remote UART is not using CTS auto flow control, we must | |
2117 | * have sufficient FIFO entries for the latency of the remote | |
2118 | * UART to respond. IOW, at least 32 bytes of FIFO. | |
2119 | */ | |
2120 | if (up->capabilities & UART_CAP_AFE && up->port.fifosize >= 32) { | |
2121 | up->mcr &= ~UART_MCR_AFE; | |
2122 | if (termios->c_cflag & CRTSCTS) | |
2123 | up->mcr |= UART_MCR_AFE; | |
2124 | } | |
2125 | ||
2126 | /* | |
2127 | * Ok, we're now changing the port state. Do it with | |
2128 | * interrupts disabled. | |
2129 | */ | |
2130 | spin_lock_irqsave(&up->port.lock, flags); | |
2131 | ||
2132 | /* | |
2133 | * Update the per-port timeout. | |
2134 | */ | |
2135 | uart_update_timeout(port, termios->c_cflag, baud); | |
2136 | ||
2137 | up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR; | |
2138 | if (termios->c_iflag & INPCK) | |
2139 | up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE; | |
2140 | if (termios->c_iflag & (BRKINT | PARMRK)) | |
2141 | up->port.read_status_mask |= UART_LSR_BI; | |
2142 | ||
2143 | /* | |
2144 | * Characteres to ignore | |
2145 | */ | |
2146 | up->port.ignore_status_mask = 0; | |
2147 | if (termios->c_iflag & IGNPAR) | |
2148 | up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE; | |
2149 | if (termios->c_iflag & IGNBRK) { | |
2150 | up->port.ignore_status_mask |= UART_LSR_BI; | |
2151 | /* | |
2152 | * If we're ignoring parity and break indicators, | |
2153 | * ignore overruns too (for real raw support). | |
2154 | */ | |
2155 | if (termios->c_iflag & IGNPAR) | |
2156 | up->port.ignore_status_mask |= UART_LSR_OE; | |
2157 | } | |
2158 | ||
2159 | /* | |
2160 | * ignore all characters if CREAD is not set | |
2161 | */ | |
2162 | if ((termios->c_cflag & CREAD) == 0) | |
2163 | up->port.ignore_status_mask |= UART_LSR_DR; | |
2164 | ||
2165 | /* | |
2166 | * CTS flow control flag and modem status interrupts | |
2167 | */ | |
2168 | up->ier &= ~UART_IER_MSI; | |
21c614a7 PA |
2169 | if (!(up->bugs & UART_BUG_NOMSR) && |
2170 | UART_ENABLE_MS(&up->port, termios->c_cflag)) | |
1da177e4 LT |
2171 | up->ier |= UART_IER_MSI; |
2172 | if (up->capabilities & UART_CAP_UUE) | |
2173 | up->ier |= UART_IER_UUE | UART_IER_RTOIE; | |
2174 | ||
2175 | serial_out(up, UART_IER, up->ier); | |
2176 | ||
2177 | if (up->capabilities & UART_CAP_EFR) { | |
2178 | unsigned char efr = 0; | |
2179 | /* | |
2180 | * TI16C752/Startech hardware flow control. FIXME: | |
2181 | * - TI16C752 requires control thresholds to be set. | |
2182 | * - UART_MCR_RTS is ineffective if auto-RTS mode is enabled. | |
2183 | */ | |
2184 | if (termios->c_cflag & CRTSCTS) | |
2185 | efr |= UART_EFR_CTS; | |
2186 | ||
2187 | serial_outp(up, UART_LCR, 0xBF); | |
2188 | serial_outp(up, UART_EFR, efr); | |
2189 | } | |
2190 | ||
255341c6 JM |
2191 | #ifdef CONFIG_ARCH_OMAP15XX |
2192 | /* Workaround to enable 115200 baud on OMAP1510 internal ports */ | |
2193 | if (cpu_is_omap1510() && is_omap_port((unsigned int)up->port.membase)) { | |
2194 | if (baud == 115200) { | |
2195 | quot = 1; | |
2196 | serial_out(up, UART_OMAP_OSC_12M_SEL, 1); | |
2197 | } else | |
2198 | serial_out(up, UART_OMAP_OSC_12M_SEL, 0); | |
2199 | } | |
2200 | #endif | |
2201 | ||
1da177e4 LT |
2202 | if (up->capabilities & UART_NATSEMI) { |
2203 | /* Switch to bank 2 not bank 1, to avoid resetting EXCR2 */ | |
2204 | serial_outp(up, UART_LCR, 0xe0); | |
2205 | } else { | |
2206 | serial_outp(up, UART_LCR, cval | UART_LCR_DLAB);/* set DLAB */ | |
2207 | } | |
2208 | ||
b32b19b8 | 2209 | serial_dl_write(up, quot); |
1da177e4 LT |
2210 | |
2211 | /* | |
2212 | * LCR DLAB must be set to enable 64-byte FIFO mode. If the FCR | |
2213 | * is written without DLAB set, this mode will be disabled. | |
2214 | */ | |
2215 | if (up->port.type == PORT_16750) | |
2216 | serial_outp(up, UART_FCR, fcr); | |
2217 | ||
2218 | serial_outp(up, UART_LCR, cval); /* reset DLAB */ | |
2219 | up->lcr = cval; /* Save LCR */ | |
2220 | if (up->port.type != PORT_16750) { | |
2221 | if (fcr & UART_FCR_ENABLE_FIFO) { | |
2222 | /* emulated UARTs (Lucent Venus 167x) need two steps */ | |
2223 | serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO); | |
2224 | } | |
2225 | serial_outp(up, UART_FCR, fcr); /* set fcr */ | |
2226 | } | |
2227 | serial8250_set_mctrl(&up->port, up->port.mctrl); | |
2228 | spin_unlock_irqrestore(&up->port.lock, flags); | |
e991a2bd AC |
2229 | /* Don't rewrite B0 */ |
2230 | if (tty_termios_baud_rate(termios)) | |
2231 | tty_termios_encode_baud_rate(termios, baud, baud); | |
1da177e4 LT |
2232 | } |
2233 | ||
2234 | static void | |
2235 | serial8250_pm(struct uart_port *port, unsigned int state, | |
2236 | unsigned int oldstate) | |
2237 | { | |
2238 | struct uart_8250_port *p = (struct uart_8250_port *)port; | |
2239 | ||
2240 | serial8250_set_sleep(p, state != 0); | |
2241 | ||
2242 | if (p->pm) | |
2243 | p->pm(port, state, oldstate); | |
2244 | } | |
2245 | ||
2246 | /* | |
2247 | * Resource handling. | |
2248 | */ | |
2249 | static int serial8250_request_std_resource(struct uart_8250_port *up) | |
2250 | { | |
2251 | unsigned int size = 8 << up->port.regshift; | |
2252 | int ret = 0; | |
2253 | ||
2254 | switch (up->port.iotype) { | |
85835f44 SS |
2255 | case UPIO_AU: |
2256 | size = 0x100000; | |
2257 | /* fall thru */ | |
0b30d668 SS |
2258 | case UPIO_TSI: |
2259 | case UPIO_MEM32: | |
1da177e4 | 2260 | case UPIO_MEM: |
beab697a | 2261 | case UPIO_DWAPB: |
1da177e4 LT |
2262 | if (!up->port.mapbase) |
2263 | break; | |
2264 | ||
2265 | if (!request_mem_region(up->port.mapbase, size, "serial")) { | |
2266 | ret = -EBUSY; | |
2267 | break; | |
2268 | } | |
2269 | ||
2270 | if (up->port.flags & UPF_IOREMAP) { | |
6f441fe9 AC |
2271 | up->port.membase = ioremap_nocache(up->port.mapbase, |
2272 | size); | |
1da177e4 LT |
2273 | if (!up->port.membase) { |
2274 | release_mem_region(up->port.mapbase, size); | |
2275 | ret = -ENOMEM; | |
2276 | } | |
2277 | } | |
2278 | break; | |
2279 | ||
2280 | case UPIO_HUB6: | |
2281 | case UPIO_PORT: | |
2282 | if (!request_region(up->port.iobase, size, "serial")) | |
2283 | ret = -EBUSY; | |
2284 | break; | |
2285 | } | |
2286 | return ret; | |
2287 | } | |
2288 | ||
2289 | static void serial8250_release_std_resource(struct uart_8250_port *up) | |
2290 | { | |
2291 | unsigned int size = 8 << up->port.regshift; | |
2292 | ||
2293 | switch (up->port.iotype) { | |
85835f44 SS |
2294 | case UPIO_AU: |
2295 | size = 0x100000; | |
2296 | /* fall thru */ | |
0b30d668 SS |
2297 | case UPIO_TSI: |
2298 | case UPIO_MEM32: | |
1da177e4 | 2299 | case UPIO_MEM: |
beab697a | 2300 | case UPIO_DWAPB: |
1da177e4 LT |
2301 | if (!up->port.mapbase) |
2302 | break; | |
2303 | ||
2304 | if (up->port.flags & UPF_IOREMAP) { | |
2305 | iounmap(up->port.membase); | |
2306 | up->port.membase = NULL; | |
2307 | } | |
2308 | ||
2309 | release_mem_region(up->port.mapbase, size); | |
2310 | break; | |
2311 | ||
2312 | case UPIO_HUB6: | |
2313 | case UPIO_PORT: | |
2314 | release_region(up->port.iobase, size); | |
2315 | break; | |
2316 | } | |
2317 | } | |
2318 | ||
2319 | static int serial8250_request_rsa_resource(struct uart_8250_port *up) | |
2320 | { | |
2321 | unsigned long start = UART_RSA_BASE << up->port.regshift; | |
2322 | unsigned int size = 8 << up->port.regshift; | |
0b30d668 | 2323 | int ret = -EINVAL; |
1da177e4 LT |
2324 | |
2325 | switch (up->port.iotype) { | |
1da177e4 LT |
2326 | case UPIO_HUB6: |
2327 | case UPIO_PORT: | |
2328 | start += up->port.iobase; | |
0b30d668 SS |
2329 | if (request_region(start, size, "serial-rsa")) |
2330 | ret = 0; | |
2331 | else | |
1da177e4 LT |
2332 | ret = -EBUSY; |
2333 | break; | |
2334 | } | |
2335 | ||
2336 | return ret; | |
2337 | } | |
2338 | ||
2339 | static void serial8250_release_rsa_resource(struct uart_8250_port *up) | |
2340 | { | |
2341 | unsigned long offset = UART_RSA_BASE << up->port.regshift; | |
2342 | unsigned int size = 8 << up->port.regshift; | |
2343 | ||
2344 | switch (up->port.iotype) { | |
1da177e4 LT |
2345 | case UPIO_HUB6: |
2346 | case UPIO_PORT: | |
2347 | release_region(up->port.iobase + offset, size); | |
2348 | break; | |
2349 | } | |
2350 | } | |
2351 | ||
2352 | static void serial8250_release_port(struct uart_port *port) | |
2353 | { | |
2354 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
2355 | ||
2356 | serial8250_release_std_resource(up); | |
2357 | if (up->port.type == PORT_RSA) | |
2358 | serial8250_release_rsa_resource(up); | |
2359 | } | |
2360 | ||
2361 | static int serial8250_request_port(struct uart_port *port) | |
2362 | { | |
2363 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
2364 | int ret = 0; | |
2365 | ||
2366 | ret = serial8250_request_std_resource(up); | |
2367 | if (ret == 0 && up->port.type == PORT_RSA) { | |
2368 | ret = serial8250_request_rsa_resource(up); | |
2369 | if (ret < 0) | |
2370 | serial8250_release_std_resource(up); | |
2371 | } | |
2372 | ||
2373 | return ret; | |
2374 | } | |
2375 | ||
2376 | static void serial8250_config_port(struct uart_port *port, int flags) | |
2377 | { | |
2378 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
2379 | int probeflags = PROBE_ANY; | |
2380 | int ret; | |
2381 | ||
1da177e4 LT |
2382 | /* |
2383 | * Find the region that we can probe for. This in turn | |
2384 | * tells us whether we can probe for the type of port. | |
2385 | */ | |
2386 | ret = serial8250_request_std_resource(up); | |
2387 | if (ret < 0) | |
2388 | return; | |
2389 | ||
2390 | ret = serial8250_request_rsa_resource(up); | |
2391 | if (ret < 0) | |
2392 | probeflags &= ~PROBE_RSA; | |
2393 | ||
2394 | if (flags & UART_CONFIG_TYPE) | |
2395 | autoconfig(up, probeflags); | |
2396 | if (up->port.type != PORT_UNKNOWN && flags & UART_CONFIG_IRQ) | |
2397 | autoconfig_irq(up); | |
2398 | ||
2399 | if (up->port.type != PORT_RSA && probeflags & PROBE_RSA) | |
2400 | serial8250_release_rsa_resource(up); | |
2401 | if (up->port.type == PORT_UNKNOWN) | |
2402 | serial8250_release_std_resource(up); | |
2403 | } | |
2404 | ||
2405 | static int | |
2406 | serial8250_verify_port(struct uart_port *port, struct serial_struct *ser) | |
2407 | { | |
2408 | if (ser->irq >= NR_IRQS || ser->irq < 0 || | |
2409 | ser->baud_base < 9600 || ser->type < PORT_UNKNOWN || | |
2410 | ser->type >= ARRAY_SIZE(uart_config) || ser->type == PORT_CIRRUS || | |
2411 | ser->type == PORT_STARTECH) | |
2412 | return -EINVAL; | |
2413 | return 0; | |
2414 | } | |
2415 | ||
2416 | static const char * | |
2417 | serial8250_type(struct uart_port *port) | |
2418 | { | |
2419 | int type = port->type; | |
2420 | ||
2421 | if (type >= ARRAY_SIZE(uart_config)) | |
2422 | type = 0; | |
2423 | return uart_config[type].name; | |
2424 | } | |
2425 | ||
2426 | static struct uart_ops serial8250_pops = { | |
2427 | .tx_empty = serial8250_tx_empty, | |
2428 | .set_mctrl = serial8250_set_mctrl, | |
2429 | .get_mctrl = serial8250_get_mctrl, | |
2430 | .stop_tx = serial8250_stop_tx, | |
2431 | .start_tx = serial8250_start_tx, | |
2432 | .stop_rx = serial8250_stop_rx, | |
2433 | .enable_ms = serial8250_enable_ms, | |
2434 | .break_ctl = serial8250_break_ctl, | |
2435 | .startup = serial8250_startup, | |
2436 | .shutdown = serial8250_shutdown, | |
2437 | .set_termios = serial8250_set_termios, | |
2438 | .pm = serial8250_pm, | |
2439 | .type = serial8250_type, | |
2440 | .release_port = serial8250_release_port, | |
2441 | .request_port = serial8250_request_port, | |
2442 | .config_port = serial8250_config_port, | |
2443 | .verify_port = serial8250_verify_port, | |
f2d937f3 JW |
2444 | #ifdef CONFIG_CONSOLE_POLL |
2445 | .poll_get_char = serial8250_get_poll_char, | |
2446 | .poll_put_char = serial8250_put_poll_char, | |
2447 | #endif | |
1da177e4 LT |
2448 | }; |
2449 | ||
2450 | static struct uart_8250_port serial8250_ports[UART_NR]; | |
2451 | ||
2452 | static void __init serial8250_isa_init_ports(void) | |
2453 | { | |
2454 | struct uart_8250_port *up; | |
2455 | static int first = 1; | |
2456 | int i; | |
2457 | ||
2458 | if (!first) | |
2459 | return; | |
2460 | first = 0; | |
2461 | ||
a61c2d78 | 2462 | for (i = 0; i < nr_uarts; i++) { |
1da177e4 LT |
2463 | struct uart_8250_port *up = &serial8250_ports[i]; |
2464 | ||
2465 | up->port.line = i; | |
2466 | spin_lock_init(&up->port.lock); | |
2467 | ||
2468 | init_timer(&up->timer); | |
2469 | up->timer.function = serial8250_timeout; | |
2470 | ||
2471 | /* | |
2472 | * ALPHA_KLUDGE_MCR needs to be killed. | |
2473 | */ | |
2474 | up->mcr_mask = ~ALPHA_KLUDGE_MCR; | |
2475 | up->mcr_force = ALPHA_KLUDGE_MCR; | |
2476 | ||
2477 | up->port.ops = &serial8250_pops; | |
2478 | } | |
2479 | ||
44454bcd | 2480 | for (i = 0, up = serial8250_ports; |
a61c2d78 | 2481 | i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; |
1da177e4 LT |
2482 | i++, up++) { |
2483 | up->port.iobase = old_serial_port[i].port; | |
2484 | up->port.irq = irq_canonicalize(old_serial_port[i].irq); | |
2485 | up->port.uartclk = old_serial_port[i].baud_base * 16; | |
2486 | up->port.flags = old_serial_port[i].flags; | |
2487 | up->port.hub6 = old_serial_port[i].hub6; | |
2488 | up->port.membase = old_serial_port[i].iomem_base; | |
2489 | up->port.iotype = old_serial_port[i].io_type; | |
2490 | up->port.regshift = old_serial_port[i].iomem_reg_shift; | |
2491 | if (share_irqs) | |
2492 | up->port.flags |= UPF_SHARE_IRQ; | |
2493 | } | |
2494 | } | |
2495 | ||
2496 | static void __init | |
2497 | serial8250_register_ports(struct uart_driver *drv, struct device *dev) | |
2498 | { | |
2499 | int i; | |
2500 | ||
2501 | serial8250_isa_init_ports(); | |
2502 | ||
a61c2d78 | 2503 | for (i = 0; i < nr_uarts; i++) { |
1da177e4 LT |
2504 | struct uart_8250_port *up = &serial8250_ports[i]; |
2505 | ||
2506 | up->port.dev = dev; | |
2507 | uart_add_one_port(drv, &up->port); | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | #ifdef CONFIG_SERIAL_8250_CONSOLE | |
2512 | ||
d358788f RK |
2513 | static void serial8250_console_putchar(struct uart_port *port, int ch) |
2514 | { | |
2515 | struct uart_8250_port *up = (struct uart_8250_port *)port; | |
2516 | ||
2517 | wait_for_xmitr(up, UART_LSR_THRE); | |
2518 | serial_out(up, UART_TX, ch); | |
2519 | } | |
2520 | ||
1da177e4 LT |
2521 | /* |
2522 | * Print a string to the serial port trying not to disturb | |
2523 | * any possible real use of the port... | |
2524 | * | |
2525 | * The console_lock must be held when we get here. | |
2526 | */ | |
2527 | static void | |
2528 | serial8250_console_write(struct console *co, const char *s, unsigned int count) | |
2529 | { | |
2530 | struct uart_8250_port *up = &serial8250_ports[co->index]; | |
d8a5a8d7 | 2531 | unsigned long flags; |
1da177e4 | 2532 | unsigned int ier; |
d8a5a8d7 | 2533 | int locked = 1; |
1da177e4 | 2534 | |
78512ece AM |
2535 | touch_nmi_watchdog(); |
2536 | ||
68aa2c0d AM |
2537 | local_irq_save(flags); |
2538 | if (up->port.sysrq) { | |
2539 | /* serial8250_handle_port() already took the lock */ | |
2540 | locked = 0; | |
2541 | } else if (oops_in_progress) { | |
2542 | locked = spin_trylock(&up->port.lock); | |
d8a5a8d7 | 2543 | } else |
68aa2c0d | 2544 | spin_lock(&up->port.lock); |
d8a5a8d7 | 2545 | |
1da177e4 | 2546 | /* |
dc7bf130 | 2547 | * First save the IER then disable the interrupts |
1da177e4 LT |
2548 | */ |
2549 | ier = serial_in(up, UART_IER); | |
2550 | ||
2551 | if (up->capabilities & UART_CAP_UUE) | |
2552 | serial_out(up, UART_IER, UART_IER_UUE); | |
2553 | else | |
2554 | serial_out(up, UART_IER, 0); | |
2555 | ||
d358788f | 2556 | uart_console_write(&up->port, s, count, serial8250_console_putchar); |
1da177e4 LT |
2557 | |
2558 | /* | |
2559 | * Finally, wait for transmitter to become empty | |
2560 | * and restore the IER | |
2561 | */ | |
f91a3715 | 2562 | wait_for_xmitr(up, BOTH_EMPTY); |
a88d75b2 | 2563 | serial_out(up, UART_IER, ier); |
d8a5a8d7 | 2564 | |
ad4c2aa6 CM |
2565 | /* |
2566 | * The receive handling will happen properly because the | |
2567 | * receive ready bit will still be set; it is not cleared | |
2568 | * on read. However, modem control will not, we must | |
2569 | * call it if we have saved something in the saved flags | |
2570 | * while processing with interrupts off. | |
2571 | */ | |
2572 | if (up->msr_saved_flags) | |
2573 | check_modem_status(up); | |
2574 | ||
d8a5a8d7 | 2575 | if (locked) |
68aa2c0d AM |
2576 | spin_unlock(&up->port.lock); |
2577 | local_irq_restore(flags); | |
1da177e4 LT |
2578 | } |
2579 | ||
118c0ace | 2580 | static int __init serial8250_console_setup(struct console *co, char *options) |
1da177e4 LT |
2581 | { |
2582 | struct uart_port *port; | |
2583 | int baud = 9600; | |
2584 | int bits = 8; | |
2585 | int parity = 'n'; | |
2586 | int flow = 'n'; | |
2587 | ||
2588 | /* | |
2589 | * Check whether an invalid uart number has been specified, and | |
2590 | * if so, search for the first available port that does have | |
2591 | * console support. | |
2592 | */ | |
a61c2d78 | 2593 | if (co->index >= nr_uarts) |
1da177e4 LT |
2594 | co->index = 0; |
2595 | port = &serial8250_ports[co->index].port; | |
2596 | if (!port->iobase && !port->membase) | |
2597 | return -ENODEV; | |
2598 | ||
2599 | if (options) | |
2600 | uart_parse_options(options, &baud, &parity, &bits, &flow); | |
2601 | ||
2602 | return uart_set_options(port, co, baud, parity, bits, flow); | |
2603 | } | |
2604 | ||
b6b1d877 | 2605 | static int serial8250_console_early_setup(void) |
18a8bd94 YL |
2606 | { |
2607 | return serial8250_find_port_for_earlycon(); | |
2608 | } | |
2609 | ||
1da177e4 LT |
2610 | static struct uart_driver serial8250_reg; |
2611 | static struct console serial8250_console = { | |
2612 | .name = "ttyS", | |
2613 | .write = serial8250_console_write, | |
2614 | .device = uart_console_device, | |
2615 | .setup = serial8250_console_setup, | |
18a8bd94 | 2616 | .early_setup = serial8250_console_early_setup, |
1da177e4 LT |
2617 | .flags = CON_PRINTBUFFER, |
2618 | .index = -1, | |
2619 | .data = &serial8250_reg, | |
2620 | }; | |
2621 | ||
2622 | static int __init serial8250_console_init(void) | |
2623 | { | |
05d81d22 EB |
2624 | if (nr_uarts > UART_NR) |
2625 | nr_uarts = UART_NR; | |
2626 | ||
1da177e4 LT |
2627 | serial8250_isa_init_ports(); |
2628 | register_console(&serial8250_console); | |
2629 | return 0; | |
2630 | } | |
2631 | console_initcall(serial8250_console_init); | |
2632 | ||
18a8bd94 | 2633 | int serial8250_find_port(struct uart_port *p) |
1da177e4 LT |
2634 | { |
2635 | int line; | |
2636 | struct uart_port *port; | |
2637 | ||
a61c2d78 | 2638 | for (line = 0; line < nr_uarts; line++) { |
1da177e4 | 2639 | port = &serial8250_ports[line].port; |
50aec3b5 | 2640 | if (uart_match_port(p, port)) |
1da177e4 LT |
2641 | return line; |
2642 | } | |
2643 | return -ENODEV; | |
2644 | } | |
2645 | ||
1da177e4 LT |
2646 | #define SERIAL8250_CONSOLE &serial8250_console |
2647 | #else | |
2648 | #define SERIAL8250_CONSOLE NULL | |
2649 | #endif | |
2650 | ||
2651 | static struct uart_driver serial8250_reg = { | |
2652 | .owner = THIS_MODULE, | |
2653 | .driver_name = "serial", | |
1da177e4 LT |
2654 | .dev_name = "ttyS", |
2655 | .major = TTY_MAJOR, | |
2656 | .minor = 64, | |
2657 | .nr = UART_NR, | |
2658 | .cons = SERIAL8250_CONSOLE, | |
2659 | }; | |
2660 | ||
d856c666 RK |
2661 | /* |
2662 | * early_serial_setup - early registration for 8250 ports | |
2663 | * | |
2664 | * Setup an 8250 port structure prior to console initialisation. Use | |
2665 | * after console initialisation will cause undefined behaviour. | |
2666 | */ | |
1da177e4 LT |
2667 | int __init early_serial_setup(struct uart_port *port) |
2668 | { | |
2669 | if (port->line >= ARRAY_SIZE(serial8250_ports)) | |
2670 | return -ENODEV; | |
2671 | ||
2672 | serial8250_isa_init_ports(); | |
2673 | serial8250_ports[port->line].port = *port; | |
2674 | serial8250_ports[port->line].port.ops = &serial8250_pops; | |
2675 | return 0; | |
2676 | } | |
2677 | ||
2678 | /** | |
2679 | * serial8250_suspend_port - suspend one serial port | |
2680 | * @line: serial line number | |
1da177e4 LT |
2681 | * |
2682 | * Suspend one serial port. | |
2683 | */ | |
2684 | void serial8250_suspend_port(int line) | |
2685 | { | |
2686 | uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port); | |
2687 | } | |
2688 | ||
2689 | /** | |
2690 | * serial8250_resume_port - resume one serial port | |
2691 | * @line: serial line number | |
1da177e4 LT |
2692 | * |
2693 | * Resume one serial port. | |
2694 | */ | |
2695 | void serial8250_resume_port(int line) | |
2696 | { | |
b5b82df6 DW |
2697 | struct uart_8250_port *up = &serial8250_ports[line]; |
2698 | ||
2699 | if (up->capabilities & UART_NATSEMI) { | |
2700 | unsigned char tmp; | |
2701 | ||
2702 | /* Ensure it's still in high speed mode */ | |
2703 | serial_outp(up, UART_LCR, 0xE0); | |
2704 | ||
2705 | tmp = serial_in(up, 0x04); /* EXCR2 */ | |
2706 | tmp &= ~0xB0; /* Disable LOCK, mask out PRESL[01] */ | |
2707 | tmp |= 0x10; /* 1.625 divisor for baud_base --> 921600 */ | |
2708 | serial_outp(up, 0x04, tmp); | |
2709 | ||
2710 | serial_outp(up, UART_LCR, 0); | |
2711 | } | |
2712 | uart_resume_port(&serial8250_reg, &up->port); | |
1da177e4 LT |
2713 | } |
2714 | ||
2715 | /* | |
2716 | * Register a set of serial devices attached to a platform device. The | |
2717 | * list is terminated with a zero flags entry, which means we expect | |
2718 | * all entries to have at least UPF_BOOT_AUTOCONF set. | |
2719 | */ | |
3ae5eaec | 2720 | static int __devinit serial8250_probe(struct platform_device *dev) |
1da177e4 | 2721 | { |
3ae5eaec | 2722 | struct plat_serial8250_port *p = dev->dev.platform_data; |
1da177e4 | 2723 | struct uart_port port; |
ec9f47cd | 2724 | int ret, i; |
1da177e4 LT |
2725 | |
2726 | memset(&port, 0, sizeof(struct uart_port)); | |
2727 | ||
ec9f47cd | 2728 | for (i = 0; p && p->flags != 0; p++, i++) { |
74a19741 WN |
2729 | port.iobase = p->iobase; |
2730 | port.membase = p->membase; | |
2731 | port.irq = p->irq; | |
2732 | port.uartclk = p->uartclk; | |
2733 | port.regshift = p->regshift; | |
2734 | port.iotype = p->iotype; | |
2735 | port.flags = p->flags; | |
2736 | port.mapbase = p->mapbase; | |
2737 | port.hub6 = p->hub6; | |
2738 | port.private_data = p->private_data; | |
2739 | port.dev = &dev->dev; | |
1da177e4 LT |
2740 | if (share_irqs) |
2741 | port.flags |= UPF_SHARE_IRQ; | |
ec9f47cd RK |
2742 | ret = serial8250_register_port(&port); |
2743 | if (ret < 0) { | |
3ae5eaec | 2744 | dev_err(&dev->dev, "unable to register port at index %d " |
4f640efb JB |
2745 | "(IO%lx MEM%llx IRQ%d): %d\n", i, |
2746 | p->iobase, (unsigned long long)p->mapbase, | |
2747 | p->irq, ret); | |
ec9f47cd | 2748 | } |
1da177e4 LT |
2749 | } |
2750 | return 0; | |
2751 | } | |
2752 | ||
2753 | /* | |
2754 | * Remove serial ports registered against a platform device. | |
2755 | */ | |
3ae5eaec | 2756 | static int __devexit serial8250_remove(struct platform_device *dev) |
1da177e4 LT |
2757 | { |
2758 | int i; | |
2759 | ||
a61c2d78 | 2760 | for (i = 0; i < nr_uarts; i++) { |
1da177e4 LT |
2761 | struct uart_8250_port *up = &serial8250_ports[i]; |
2762 | ||
3ae5eaec | 2763 | if (up->port.dev == &dev->dev) |
1da177e4 LT |
2764 | serial8250_unregister_port(i); |
2765 | } | |
2766 | return 0; | |
2767 | } | |
2768 | ||
3ae5eaec | 2769 | static int serial8250_suspend(struct platform_device *dev, pm_message_t state) |
1da177e4 LT |
2770 | { |
2771 | int i; | |
2772 | ||
1da177e4 LT |
2773 | for (i = 0; i < UART_NR; i++) { |
2774 | struct uart_8250_port *up = &serial8250_ports[i]; | |
2775 | ||
3ae5eaec | 2776 | if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) |
1da177e4 LT |
2777 | uart_suspend_port(&serial8250_reg, &up->port); |
2778 | } | |
2779 | ||
2780 | return 0; | |
2781 | } | |
2782 | ||
3ae5eaec | 2783 | static int serial8250_resume(struct platform_device *dev) |
1da177e4 LT |
2784 | { |
2785 | int i; | |
2786 | ||
1da177e4 LT |
2787 | for (i = 0; i < UART_NR; i++) { |
2788 | struct uart_8250_port *up = &serial8250_ports[i]; | |
2789 | ||
3ae5eaec | 2790 | if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) |
b5b82df6 | 2791 | serial8250_resume_port(i); |
1da177e4 LT |
2792 | } |
2793 | ||
2794 | return 0; | |
2795 | } | |
2796 | ||
3ae5eaec | 2797 | static struct platform_driver serial8250_isa_driver = { |
1da177e4 LT |
2798 | .probe = serial8250_probe, |
2799 | .remove = __devexit_p(serial8250_remove), | |
2800 | .suspend = serial8250_suspend, | |
2801 | .resume = serial8250_resume, | |
3ae5eaec RK |
2802 | .driver = { |
2803 | .name = "serial8250", | |
7493a314 | 2804 | .owner = THIS_MODULE, |
3ae5eaec | 2805 | }, |
1da177e4 LT |
2806 | }; |
2807 | ||
2808 | /* | |
2809 | * This "device" covers _all_ ISA 8250-compatible serial devices listed | |
2810 | * in the table in include/asm/serial.h | |
2811 | */ | |
2812 | static struct platform_device *serial8250_isa_devs; | |
2813 | ||
2814 | /* | |
2815 | * serial8250_register_port and serial8250_unregister_port allows for | |
2816 | * 16x50 serial ports to be configured at run-time, to support PCMCIA | |
2817 | * modems and PCI multiport cards. | |
2818 | */ | |
f392ecfa | 2819 | static DEFINE_MUTEX(serial_mutex); |
1da177e4 LT |
2820 | |
2821 | static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port) | |
2822 | { | |
2823 | int i; | |
2824 | ||
2825 | /* | |
2826 | * First, find a port entry which matches. | |
2827 | */ | |
a61c2d78 | 2828 | for (i = 0; i < nr_uarts; i++) |
1da177e4 LT |
2829 | if (uart_match_port(&serial8250_ports[i].port, port)) |
2830 | return &serial8250_ports[i]; | |
2831 | ||
2832 | /* | |
2833 | * We didn't find a matching entry, so look for the first | |
2834 | * free entry. We look for one which hasn't been previously | |
2835 | * used (indicated by zero iobase). | |
2836 | */ | |
a61c2d78 | 2837 | for (i = 0; i < nr_uarts; i++) |
1da177e4 LT |
2838 | if (serial8250_ports[i].port.type == PORT_UNKNOWN && |
2839 | serial8250_ports[i].port.iobase == 0) | |
2840 | return &serial8250_ports[i]; | |
2841 | ||
2842 | /* | |
2843 | * That also failed. Last resort is to find any entry which | |
2844 | * doesn't have a real port associated with it. | |
2845 | */ | |
a61c2d78 | 2846 | for (i = 0; i < nr_uarts; i++) |
1da177e4 LT |
2847 | if (serial8250_ports[i].port.type == PORT_UNKNOWN) |
2848 | return &serial8250_ports[i]; | |
2849 | ||
2850 | return NULL; | |
2851 | } | |
2852 | ||
2853 | /** | |
2854 | * serial8250_register_port - register a serial port | |
2855 | * @port: serial port template | |
2856 | * | |
2857 | * Configure the serial port specified by the request. If the | |
2858 | * port exists and is in use, it is hung up and unregistered | |
2859 | * first. | |
2860 | * | |
2861 | * The port is then probed and if necessary the IRQ is autodetected | |
2862 | * If this fails an error is returned. | |
2863 | * | |
2864 | * On success the port is ready to use and the line number is returned. | |
2865 | */ | |
2866 | int serial8250_register_port(struct uart_port *port) | |
2867 | { | |
2868 | struct uart_8250_port *uart; | |
2869 | int ret = -ENOSPC; | |
2870 | ||
2871 | if (port->uartclk == 0) | |
2872 | return -EINVAL; | |
2873 | ||
f392ecfa | 2874 | mutex_lock(&serial_mutex); |
1da177e4 LT |
2875 | |
2876 | uart = serial8250_find_match_or_unused(port); | |
2877 | if (uart) { | |
2878 | uart_remove_one_port(&serial8250_reg, &uart->port); | |
2879 | ||
74a19741 WN |
2880 | uart->port.iobase = port->iobase; |
2881 | uart->port.membase = port->membase; | |
2882 | uart->port.irq = port->irq; | |
2883 | uart->port.uartclk = port->uartclk; | |
2884 | uart->port.fifosize = port->fifosize; | |
2885 | uart->port.regshift = port->regshift; | |
2886 | uart->port.iotype = port->iotype; | |
2887 | uart->port.flags = port->flags | UPF_BOOT_AUTOCONF; | |
2888 | uart->port.mapbase = port->mapbase; | |
2889 | uart->port.private_data = port->private_data; | |
1da177e4 LT |
2890 | if (port->dev) |
2891 | uart->port.dev = port->dev; | |
2892 | ||
2893 | ret = uart_add_one_port(&serial8250_reg, &uart->port); | |
2894 | if (ret == 0) | |
2895 | ret = uart->port.line; | |
2896 | } | |
f392ecfa | 2897 | mutex_unlock(&serial_mutex); |
1da177e4 LT |
2898 | |
2899 | return ret; | |
2900 | } | |
2901 | EXPORT_SYMBOL(serial8250_register_port); | |
2902 | ||
2903 | /** | |
2904 | * serial8250_unregister_port - remove a 16x50 serial port at runtime | |
2905 | * @line: serial line number | |
2906 | * | |
2907 | * Remove one serial port. This may not be called from interrupt | |
2908 | * context. We hand the port back to the our control. | |
2909 | */ | |
2910 | void serial8250_unregister_port(int line) | |
2911 | { | |
2912 | struct uart_8250_port *uart = &serial8250_ports[line]; | |
2913 | ||
f392ecfa | 2914 | mutex_lock(&serial_mutex); |
1da177e4 LT |
2915 | uart_remove_one_port(&serial8250_reg, &uart->port); |
2916 | if (serial8250_isa_devs) { | |
2917 | uart->port.flags &= ~UPF_BOOT_AUTOCONF; | |
2918 | uart->port.type = PORT_UNKNOWN; | |
2919 | uart->port.dev = &serial8250_isa_devs->dev; | |
2920 | uart_add_one_port(&serial8250_reg, &uart->port); | |
2921 | } else { | |
2922 | uart->port.dev = NULL; | |
2923 | } | |
f392ecfa | 2924 | mutex_unlock(&serial_mutex); |
1da177e4 LT |
2925 | } |
2926 | EXPORT_SYMBOL(serial8250_unregister_port); | |
2927 | ||
2928 | static int __init serial8250_init(void) | |
2929 | { | |
2930 | int ret, i; | |
2931 | ||
a61c2d78 DJ |
2932 | if (nr_uarts > UART_NR) |
2933 | nr_uarts = UART_NR; | |
2934 | ||
d87a6d95 | 2935 | printk(KERN_INFO "Serial: 8250/16550 driver" |
a61c2d78 | 2936 | "%d ports, IRQ sharing %sabled\n", nr_uarts, |
1da177e4 LT |
2937 | share_irqs ? "en" : "dis"); |
2938 | ||
2939 | for (i = 0; i < NR_IRQS; i++) | |
2940 | spin_lock_init(&irq_lists[i].lock); | |
2941 | ||
2942 | ret = uart_register_driver(&serial8250_reg); | |
2943 | if (ret) | |
2944 | goto out; | |
2945 | ||
7493a314 DT |
2946 | serial8250_isa_devs = platform_device_alloc("serial8250", |
2947 | PLAT8250_DEV_LEGACY); | |
2948 | if (!serial8250_isa_devs) { | |
2949 | ret = -ENOMEM; | |
bc965a7f | 2950 | goto unreg_uart_drv; |
1da177e4 LT |
2951 | } |
2952 | ||
7493a314 DT |
2953 | ret = platform_device_add(serial8250_isa_devs); |
2954 | if (ret) | |
2955 | goto put_dev; | |
2956 | ||
1da177e4 LT |
2957 | serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev); |
2958 | ||
bc965a7f RK |
2959 | ret = platform_driver_register(&serial8250_isa_driver); |
2960 | if (ret == 0) | |
2961 | goto out; | |
1da177e4 | 2962 | |
bc965a7f | 2963 | platform_device_del(serial8250_isa_devs); |
7493a314 DT |
2964 | put_dev: |
2965 | platform_device_put(serial8250_isa_devs); | |
7493a314 | 2966 | unreg_uart_drv: |
1da177e4 LT |
2967 | uart_unregister_driver(&serial8250_reg); |
2968 | out: | |
2969 | return ret; | |
2970 | } | |
2971 | ||
2972 | static void __exit serial8250_exit(void) | |
2973 | { | |
2974 | struct platform_device *isa_dev = serial8250_isa_devs; | |
2975 | ||
2976 | /* | |
2977 | * This tells serial8250_unregister_port() not to re-register | |
2978 | * the ports (thereby making serial8250_isa_driver permanently | |
2979 | * in use.) | |
2980 | */ | |
2981 | serial8250_isa_devs = NULL; | |
2982 | ||
3ae5eaec | 2983 | platform_driver_unregister(&serial8250_isa_driver); |
1da177e4 LT |
2984 | platform_device_unregister(isa_dev); |
2985 | ||
2986 | uart_unregister_driver(&serial8250_reg); | |
2987 | } | |
2988 | ||
2989 | module_init(serial8250_init); | |
2990 | module_exit(serial8250_exit); | |
2991 | ||
2992 | EXPORT_SYMBOL(serial8250_suspend_port); | |
2993 | EXPORT_SYMBOL(serial8250_resume_port); | |
2994 | ||
2995 | MODULE_LICENSE("GPL"); | |
d87a6d95 | 2996 | MODULE_DESCRIPTION("Generic 8250/16x50 serial driver"); |
1da177e4 LT |
2997 | |
2998 | module_param(share_irqs, uint, 0644); | |
2999 | MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices" | |
3000 | " (unsafe)"); | |
3001 | ||
a61c2d78 DJ |
3002 | module_param(nr_uarts, uint, 0644); |
3003 | MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")"); | |
3004 | ||
1da177e4 LT |
3005 | #ifdef CONFIG_SERIAL_8250_RSA |
3006 | module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444); | |
3007 | MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA"); | |
3008 | #endif | |
3009 | MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR); |