Commit | Line | Data |
---|---|---|
e3b3d0f5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
1da177e4 | 2 | /* |
b6830f6d | 3 | * Universal/legacy driver for 8250/16550-type serial ports |
1da177e4 LT |
4 | * |
5 | * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. | |
6 | * | |
7 | * Copyright (C) 2001 Russell King. | |
8 | * | |
b6830f6d PH |
9 | * Supports: ISA-compatible 8250/16550 ports |
10 | * PNP 8250/16550 ports | |
11 | * early_serial_setup() ports | |
12 | * userspace-configurable "phantom" ports | |
13 | * "serial8250" platform devices | |
14 | * serial8250_register_8250_port() ports | |
1da177e4 | 15 | */ |
1da177e4 | 16 | |
1da177e4 LT |
17 | #include <linux/module.h> |
18 | #include <linux/moduleparam.h> | |
19 | #include <linux/ioport.h> | |
20 | #include <linux/init.h> | |
21 | #include <linux/console.h> | |
22 | #include <linux/sysrq.h> | |
1da177e4 | 23 | #include <linux/delay.h> |
d052d1be | 24 | #include <linux/platform_device.h> |
1da177e4 | 25 | #include <linux/tty.h> |
cd3ecad1 | 26 | #include <linux/ratelimit.h> |
1da177e4 | 27 | #include <linux/tty_flip.h> |
1da177e4 LT |
28 | #include <linux/serial.h> |
29 | #include <linux/serial_8250.h> | |
78512ece | 30 | #include <linux/nmi.h> |
f392ecfa | 31 | #include <linux/mutex.h> |
5a0e3ad6 | 32 | #include <linux/slab.h> |
0b4af1d9 | 33 | #include <linux/uaccess.h> |
d74d5d1b | 34 | #include <linux/pm_runtime.h> |
2584cf83 | 35 | #include <linux/io.h> |
6816383a PG |
36 | #ifdef CONFIG_SPARC |
37 | #include <linux/sunserialcore.h> | |
38 | #endif | |
1da177e4 | 39 | |
1da177e4 LT |
40 | #include <asm/irq.h> |
41 | ||
42 | #include "8250.h" | |
43 | ||
44 | /* | |
45 | * Configuration: | |
40663cc7 | 46 | * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option |
1da177e4 LT |
47 | * is unsafe when used on edge-triggered interrupts. |
48 | */ | |
408b664a | 49 | static unsigned int share_irqs = SERIAL8250_SHARE_IRQS; |
1da177e4 | 50 | |
a61c2d78 DJ |
51 | static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS; |
52 | ||
8440838b DM |
53 | static struct uart_driver serial8250_reg; |
54 | ||
d41a4b51 CE |
55 | static unsigned int skip_txen_test; /* force skip of txen test at init time */ |
56 | ||
e7328ae1 | 57 | #define PASS_LIMIT 512 |
1da177e4 | 58 | |
a4ed1e41 | 59 | #include <asm/serial.h> |
1da177e4 LT |
60 | /* |
61 | * SERIAL_PORT_DFNS tells us about built-in ports that have no | |
62 | * standard enumeration mechanism. Platforms that can find all | |
63 | * serial ports via mechanisms like ACPI or PCI need not supply it. | |
64 | */ | |
65 | #ifndef SERIAL_PORT_DFNS | |
66 | #define SERIAL_PORT_DFNS | |
67 | #endif | |
68 | ||
cb3592be | 69 | static const struct old_serial_port old_serial_port[] = { |
1da177e4 LT |
70 | SERIAL_PORT_DFNS /* defined in asm/serial.h */ |
71 | }; | |
72 | ||
026d02a2 | 73 | #define UART_NR CONFIG_SERIAL_8250_NR_UARTS |
1da177e4 LT |
74 | |
75 | #ifdef CONFIG_SERIAL_8250_RSA | |
76 | ||
77 | #define PORT_RSA_MAX 4 | |
78 | static unsigned long probe_rsa[PORT_RSA_MAX]; | |
79 | static unsigned int probe_rsa_count; | |
80 | #endif /* CONFIG_SERIAL_8250_RSA */ | |
81 | ||
1da177e4 | 82 | struct irq_info { |
25db8ad5 AC |
83 | struct hlist_node node; |
84 | int irq; | |
85 | spinlock_t lock; /* Protects list not the hash */ | |
1da177e4 LT |
86 | struct list_head *head; |
87 | }; | |
88 | ||
25db8ad5 AC |
89 | #define NR_IRQ_HASH 32 /* Can be adjusted later */ |
90 | static struct hlist_head irq_lists[NR_IRQ_HASH]; | |
91 | static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */ | |
1da177e4 LT |
92 | |
93 | /* | |
b6830f6d PH |
94 | * This is the serial driver's interrupt routine. |
95 | * | |
96 | * Arjan thinks the old way was overly complex, so it got simplified. | |
97 | * Alan disagrees, saying that need the complexity to handle the weird | |
98 | * nature of ISA shared interrupts. (This is a special exception.) | |
99 | * | |
100 | * In order to handle ISA shared interrupts properly, we need to check | |
101 | * that all ports have been serviced, and therefore the ISA interrupt | |
102 | * line has been de-asserted. | |
103 | * | |
104 | * This means we need to loop through all ports. checking that they | |
105 | * don't have an interrupt pending. | |
1da177e4 | 106 | */ |
b6830f6d | 107 | static irqreturn_t serial8250_interrupt(int irq, void *dev_id) |
7d6a07d1 | 108 | { |
b6830f6d PH |
109 | struct irq_info *i = dev_id; |
110 | struct list_head *l, *end = NULL; | |
111 | int pass_counter = 0, handled = 0; | |
7d6a07d1 | 112 | |
934014d5 | 113 | pr_debug("%s(%d): start\n", __func__, irq); |
583d28e9 | 114 | |
b6830f6d | 115 | spin_lock(&i->lock); |
cc419fa0 | 116 | |
b6830f6d PH |
117 | l = i->head; |
118 | do { | |
119 | struct uart_8250_port *up; | |
120 | struct uart_port *port; | |
cc419fa0 | 121 | |
b6830f6d PH |
122 | up = list_entry(l, struct uart_8250_port, list); |
123 | port = &up->port; | |
1da177e4 | 124 | |
b6830f6d PH |
125 | if (port->handle_irq(port)) { |
126 | handled = 1; | |
127 | end = NULL; | |
128 | } else if (end == NULL) | |
129 | end = l; | |
1da177e4 | 130 | |
b6830f6d | 131 | l = l->next; |
1da177e4 | 132 | |
9d7c249a | 133 | if (l == i->head && pass_counter++ > PASS_LIMIT) |
b6830f6d | 134 | break; |
b6830f6d | 135 | } while (l != end); |
c627f2ce | 136 | |
b6830f6d | 137 | spin_unlock(&i->lock); |
12bf3f24 | 138 | |
934014d5 | 139 | pr_debug("%s(%d): end\n", __func__, irq); |
1da177e4 | 140 | |
b6830f6d | 141 | return IRQ_RETVAL(handled); |
40b36daa AW |
142 | } |
143 | ||
1da177e4 | 144 | /* |
b6830f6d PH |
145 | * To support ISA shared interrupts, we need to have one interrupt |
146 | * handler that ensures that the IRQ line has been deasserted | |
147 | * before returning. Failing to do this will result in the IRQ | |
148 | * line being stuck active, and, since ISA irqs are edge triggered, | |
149 | * no more IRQs will be seen. | |
1da177e4 | 150 | */ |
b6830f6d | 151 | static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up) |
1da177e4 | 152 | { |
b6830f6d | 153 | spin_lock_irq(&i->lock); |
1da177e4 | 154 | |
b6830f6d PH |
155 | if (!list_empty(i->head)) { |
156 | if (i->head == &up->list) | |
157 | i->head = i->head->next; | |
158 | list_del(&up->list); | |
159 | } else { | |
160 | BUG_ON(i->head != &up->list); | |
161 | i->head = NULL; | |
162 | } | |
163 | spin_unlock_irq(&i->lock); | |
164 | /* List empty so throw away the hash node */ | |
165 | if (i->head == NULL) { | |
166 | hlist_del(&i->node); | |
167 | kfree(i); | |
1da177e4 LT |
168 | } |
169 | } | |
170 | ||
b6830f6d | 171 | static int serial_link_irq_chain(struct uart_8250_port *up) |
0ad372b9 | 172 | { |
b6830f6d PH |
173 | struct hlist_head *h; |
174 | struct hlist_node *n; | |
175 | struct irq_info *i; | |
176 | int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? IRQF_SHARED : 0; | |
0ad372b9 | 177 | |
b6830f6d | 178 | mutex_lock(&hash_mutex); |
d74d5d1b | 179 | |
b6830f6d | 180 | h = &irq_lists[up->port.irq % NR_IRQ_HASH]; |
d74d5d1b | 181 | |
b6830f6d PH |
182 | hlist_for_each(n, h) { |
183 | i = hlist_entry(n, struct irq_info, node); | |
184 | if (i->irq == up->port.irq) | |
185 | break; | |
186 | } | |
d74d5d1b | 187 | |
b6830f6d PH |
188 | if (n == NULL) { |
189 | i = kzalloc(sizeof(struct irq_info), GFP_KERNEL); | |
190 | if (i == NULL) { | |
191 | mutex_unlock(&hash_mutex); | |
192 | return -ENOMEM; | |
193 | } | |
194 | spin_lock_init(&i->lock); | |
195 | i->irq = up->port.irq; | |
196 | hlist_add_head(&i->node, h); | |
197 | } | |
198 | mutex_unlock(&hash_mutex); | |
d74d5d1b | 199 | |
b6830f6d | 200 | spin_lock_irq(&i->lock); |
d74d5d1b | 201 | |
b6830f6d PH |
202 | if (i->head) { |
203 | list_add(&up->list, i->head); | |
204 | spin_unlock_irq(&i->lock); | |
d74d5d1b | 205 | |
b6830f6d PH |
206 | ret = 0; |
207 | } else { | |
208 | INIT_LIST_HEAD(&up->list); | |
209 | i->head = &up->list; | |
210 | spin_unlock_irq(&i->lock); | |
211 | irq_flags |= up->port.irqflags; | |
212 | ret = request_irq(up->port.irq, serial8250_interrupt, | |
b2c663d1 | 213 | irq_flags, up->port.name, i); |
b6830f6d PH |
214 | if (ret < 0) |
215 | serial_do_unlink(i, up); | |
216 | } | |
d74d5d1b | 217 | |
b6830f6d | 218 | return ret; |
d74d5d1b SAS |
219 | } |
220 | ||
b6830f6d | 221 | static void serial_unlink_irq_chain(struct uart_8250_port *up) |
1da177e4 | 222 | { |
dc96efb7 | 223 | /* |
b6830f6d PH |
224 | * yes, some broken gcc emit "warning: 'i' may be used uninitialized" |
225 | * but no, we are not going to take a patch that assigns NULL below. | |
dc96efb7 | 226 | */ |
b6830f6d PH |
227 | struct irq_info *i; |
228 | struct hlist_node *n; | |
229 | struct hlist_head *h; | |
1da177e4 | 230 | |
b6830f6d | 231 | mutex_lock(&hash_mutex); |
1da177e4 | 232 | |
b6830f6d | 233 | h = &irq_lists[up->port.irq % NR_IRQ_HASH]; |
1da177e4 | 234 | |
b6830f6d PH |
235 | hlist_for_each(n, h) { |
236 | i = hlist_entry(n, struct irq_info, node); | |
237 | if (i->irq == up->port.irq) | |
238 | break; | |
1da177e4 LT |
239 | } |
240 | ||
b6830f6d PH |
241 | BUG_ON(n == NULL); |
242 | BUG_ON(i->head == NULL); | |
1da177e4 | 243 | |
b6830f6d PH |
244 | if (list_empty(i->head)) |
245 | free_irq(up->port.irq, i); | |
1da177e4 | 246 | |
b6830f6d PH |
247 | serial_do_unlink(i, up); |
248 | mutex_unlock(&hash_mutex); | |
1da177e4 LT |
249 | } |
250 | ||
251 | /* | |
b6830f6d PH |
252 | * This function is used to handle ports that do not have an |
253 | * interrupt. This doesn't work very well for 16450's, but gives | |
254 | * barely passable results for a 16550A. (Although at the expense | |
255 | * of much CPU overhead). | |
1da177e4 | 256 | */ |
7c335645 | 257 | static void serial8250_timeout(struct timer_list *t) |
1da177e4 | 258 | { |
7c335645 | 259 | struct uart_8250_port *up = from_timer(up, t, timer); |
1da177e4 | 260 | |
b6830f6d PH |
261 | up->port.handle_irq(&up->port); |
262 | mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port)); | |
263 | } | |
1da177e4 | 264 | |
7c335645 | 265 | static void serial8250_backup_timeout(struct timer_list *t) |
0ec3f585 | 266 | { |
7c335645 | 267 | struct uart_8250_port *up = from_timer(up, t, timer); |
b6830f6d | 268 | unsigned int iir, ier = 0, lsr; |
0ec3f585 | 269 | unsigned long flags; |
1da177e4 | 270 | |
b6830f6d | 271 | spin_lock_irqsave(&up->port.lock, flags); |
45a7bd63 | 272 | |
1da177e4 | 273 | /* |
b6830f6d PH |
274 | * Must disable interrupts or else we risk racing with the interrupt |
275 | * based handler. | |
1da177e4 | 276 | */ |
b6830f6d PH |
277 | if (up->port.irq) { |
278 | ier = serial_in(up, UART_IER); | |
279 | serial_out(up, UART_IER, 0); | |
1da177e4 | 280 | } |
235dae5d | 281 | |
b6830f6d | 282 | iir = serial_in(up, UART_IIR); |
1da177e4 | 283 | |
b6830f6d PH |
284 | /* |
285 | * This should be a safe test for anyone who doesn't trust the | |
286 | * IIR bits on their UART, but it's specifically designed for | |
287 | * the "Diva" UART used on the management processor on many HP | |
288 | * ia64 and parisc boxes. | |
289 | */ | |
290 | lsr = serial_in(up, UART_LSR); | |
291 | up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS; | |
292 | if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) && | |
293 | (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) && | |
294 | (lsr & UART_LSR_THRE)) { | |
295 | iir &= ~(UART_IIR_ID | UART_IIR_NO_INT); | |
296 | iir |= UART_IIR_THRI; | |
cab68f89 | 297 | } |
c161afe9 | 298 | |
b6830f6d PH |
299 | if (!(iir & UART_IIR_NO_INT)) |
300 | serial8250_tx_chars(up); | |
1da177e4 | 301 | |
b6830f6d PH |
302 | if (up->port.irq) |
303 | serial_out(up, UART_IER, ier); | |
1da177e4 | 304 | |
b6830f6d | 305 | spin_unlock_irqrestore(&up->port.lock, flags); |
54ec52b6 | 306 | |
b6830f6d PH |
307 | /* Standard timer interval plus 0.2s to keep the port running */ |
308 | mod_timer(&up->timer, | |
309 | jiffies + uart_poll_timeout(&up->port) + HZ / 5); | |
f2eda27d RK |
310 | } |
311 | ||
b6830f6d | 312 | static int univ8250_setup_irq(struct uart_8250_port *up) |
1da177e4 | 313 | { |
dfe42443 | 314 | struct uart_port *port = &up->port; |
b6830f6d | 315 | int retval = 0; |
1da177e4 | 316 | |
b6830f6d PH |
317 | /* |
318 | * The above check will only give an accurate result the first time | |
319 | * the port is opened so this value needs to be preserved. | |
320 | */ | |
321 | if (up->bugs & UART_BUG_THRE) { | |
286d9b8c | 322 | pr_debug("%s - using backup timer\n", port->name); |
1da177e4 | 323 | |
841b86f3 | 324 | up->timer.function = serial8250_backup_timeout; |
b6830f6d PH |
325 | mod_timer(&up->timer, jiffies + |
326 | uart_poll_timeout(port) + HZ / 5); | |
327 | } | |
1da177e4 | 328 | |
b6830f6d PH |
329 | /* |
330 | * If the "interrupt" for this port doesn't correspond with any | |
331 | * hardware interrupt, we use a timer-based system. The original | |
332 | * driver used to do this with IRQ0. | |
333 | */ | |
334 | if (!port->irq) { | |
b6830f6d PH |
335 | mod_timer(&up->timer, jiffies + uart_poll_timeout(port)); |
336 | } else | |
337 | retval = serial_link_irq_chain(up); | |
1da177e4 | 338 | |
b6830f6d | 339 | return retval; |
1da177e4 LT |
340 | } |
341 | ||
b6830f6d | 342 | static void univ8250_release_irq(struct uart_8250_port *up) |
1da177e4 | 343 | { |
dfe42443 | 344 | struct uart_port *port = &up->port; |
1da177e4 | 345 | |
b6830f6d | 346 | del_timer_sync(&up->timer); |
841b86f3 | 347 | up->timer.function = serial8250_timeout; |
b6830f6d PH |
348 | if (port->irq) |
349 | serial_unlink_irq_chain(up); | |
1da177e4 LT |
350 | } |
351 | ||
cd52b759 | 352 | #ifdef CONFIG_SERIAL_8250_RSA |
1da177e4 LT |
353 | static int serial8250_request_rsa_resource(struct uart_8250_port *up) |
354 | { | |
355 | unsigned long start = UART_RSA_BASE << up->port.regshift; | |
356 | unsigned int size = 8 << up->port.regshift; | |
dfe42443 | 357 | struct uart_port *port = &up->port; |
0b30d668 | 358 | int ret = -EINVAL; |
1da177e4 | 359 | |
dfe42443 | 360 | switch (port->iotype) { |
1da177e4 LT |
361 | case UPIO_HUB6: |
362 | case UPIO_PORT: | |
dfe42443 | 363 | start += port->iobase; |
0b30d668 SS |
364 | if (request_region(start, size, "serial-rsa")) |
365 | ret = 0; | |
366 | else | |
1da177e4 LT |
367 | ret = -EBUSY; |
368 | break; | |
369 | } | |
370 | ||
371 | return ret; | |
372 | } | |
373 | ||
374 | static void serial8250_release_rsa_resource(struct uart_8250_port *up) | |
375 | { | |
376 | unsigned long offset = UART_RSA_BASE << up->port.regshift; | |
377 | unsigned int size = 8 << up->port.regshift; | |
dfe42443 | 378 | struct uart_port *port = &up->port; |
1da177e4 | 379 | |
dfe42443 | 380 | switch (port->iotype) { |
1da177e4 LT |
381 | case UPIO_HUB6: |
382 | case UPIO_PORT: | |
dfe42443 | 383 | release_region(port->iobase + offset, size); |
1da177e4 LT |
384 | break; |
385 | } | |
386 | } | |
cd52b759 | 387 | #endif |
1da177e4 | 388 | |
40375393 PH |
389 | static const struct uart_ops *base_ops; |
390 | static struct uart_ops univ8250_port_ops; | |
391 | ||
a4416cd1 PH |
392 | static const struct uart_8250_ops univ8250_driver_ops = { |
393 | .setup_irq = univ8250_setup_irq, | |
394 | .release_irq = univ8250_release_irq, | |
395 | }; | |
396 | ||
1da177e4 LT |
397 | static struct uart_8250_port serial8250_ports[UART_NR]; |
398 | ||
ae14a795 SAS |
399 | /** |
400 | * serial8250_get_port - retrieve struct uart_8250_port | |
401 | * @line: serial line number | |
402 | * | |
403 | * This function retrieves struct uart_8250_port for the specific line. | |
404 | * This struct *must* *not* be used to perform a 8250 or serial core operation | |
405 | * which is not accessible otherwise. Its only purpose is to make the struct | |
406 | * accessible to the runtime-pm callbacks for context suspend/restore. | |
407 | * The lock assumption made here is none because runtime-pm suspend/resume | |
408 | * callbacks should not be invoked if there is any operation performed on the | |
409 | * port. | |
410 | */ | |
411 | struct uart_8250_port *serial8250_get_port(int line) | |
412 | { | |
413 | return &serial8250_ports[line]; | |
414 | } | |
415 | EXPORT_SYMBOL_GPL(serial8250_get_port); | |
416 | ||
af7f3743 | 417 | static void (*serial8250_isa_config)(int port, struct uart_port *up, |
98838d95 | 418 | u32 *capabilities); |
af7f3743 AC |
419 | |
420 | void serial8250_set_isa_configurator( | |
98838d95 | 421 | void (*v)(int port, struct uart_port *up, u32 *capabilities)) |
af7f3743 AC |
422 | { |
423 | serial8250_isa_config = v; | |
424 | } | |
425 | EXPORT_SYMBOL(serial8250_set_isa_configurator); | |
426 | ||
40375393 PH |
427 | #ifdef CONFIG_SERIAL_8250_RSA |
428 | ||
429 | static void univ8250_config_port(struct uart_port *port, int flags) | |
430 | { | |
431 | struct uart_8250_port *up = up_to_u8250p(port); | |
432 | ||
433 | up->probe &= ~UART_PROBE_RSA; | |
434 | if (port->type == PORT_RSA) { | |
435 | if (serial8250_request_rsa_resource(up) == 0) | |
436 | up->probe |= UART_PROBE_RSA; | |
437 | } else if (flags & UART_CONFIG_TYPE) { | |
438 | int i; | |
439 | ||
440 | for (i = 0; i < probe_rsa_count; i++) { | |
441 | if (probe_rsa[i] == up->port.iobase) { | |
442 | if (serial8250_request_rsa_resource(up) == 0) | |
443 | up->probe |= UART_PROBE_RSA; | |
444 | break; | |
445 | } | |
446 | } | |
447 | } | |
448 | ||
449 | base_ops->config_port(port, flags); | |
450 | ||
451 | if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA) | |
452 | serial8250_release_rsa_resource(up); | |
453 | } | |
454 | ||
455 | static int univ8250_request_port(struct uart_port *port) | |
456 | { | |
457 | struct uart_8250_port *up = up_to_u8250p(port); | |
458 | int ret; | |
459 | ||
460 | ret = base_ops->request_port(port); | |
461 | if (ret == 0 && port->type == PORT_RSA) { | |
462 | ret = serial8250_request_rsa_resource(up); | |
463 | if (ret < 0) | |
464 | base_ops->release_port(port); | |
465 | } | |
466 | ||
467 | return ret; | |
468 | } | |
469 | ||
470 | static void univ8250_release_port(struct uart_port *port) | |
471 | { | |
472 | struct uart_8250_port *up = up_to_u8250p(port); | |
473 | ||
474 | if (port->type == PORT_RSA) | |
475 | serial8250_release_rsa_resource(up); | |
476 | base_ops->release_port(port); | |
477 | } | |
478 | ||
479 | static void univ8250_rsa_support(struct uart_ops *ops) | |
480 | { | |
481 | ops->config_port = univ8250_config_port; | |
482 | ops->request_port = univ8250_request_port; | |
483 | ops->release_port = univ8250_release_port; | |
484 | } | |
485 | ||
486 | #else | |
487 | #define univ8250_rsa_support(x) do { } while (0) | |
488 | #endif /* CONFIG_SERIAL_8250_RSA */ | |
489 | ||
c7ac15ce AS |
490 | static inline void serial8250_apply_quirks(struct uart_8250_port *up) |
491 | { | |
492 | up->port.quirks |= skip_txen_test ? UPQ_NO_TXEN_TEST : 0; | |
493 | } | |
494 | ||
1da177e4 LT |
495 | static void __init serial8250_isa_init_ports(void) |
496 | { | |
497 | struct uart_8250_port *up; | |
498 | static int first = 1; | |
4c0ebb80 | 499 | int i, irqflag = 0; |
1da177e4 LT |
500 | |
501 | if (!first) | |
502 | return; | |
503 | first = 0; | |
504 | ||
835d844d SY |
505 | if (nr_uarts > UART_NR) |
506 | nr_uarts = UART_NR; | |
507 | ||
317a6842 | 508 | for (i = 0; i < nr_uarts; i++) { |
1da177e4 | 509 | struct uart_8250_port *up = &serial8250_ports[i]; |
dfe42443 | 510 | struct uart_port *port = &up->port; |
1da177e4 | 511 | |
dfe42443 | 512 | port->line = i; |
0a16e2c1 | 513 | serial8250_init_port(up); |
40375393 PH |
514 | if (!base_ops) |
515 | base_ops = port->ops; | |
516 | port->ops = &univ8250_port_ops; | |
1da177e4 | 517 | |
7c335645 | 518 | timer_setup(&up->timer, serial8250_timeout, 0); |
1da177e4 | 519 | |
a4416cd1 PH |
520 | up->ops = &univ8250_driver_ops; |
521 | ||
1da177e4 LT |
522 | /* |
523 | * ALPHA_KLUDGE_MCR needs to be killed. | |
524 | */ | |
525 | up->mcr_mask = ~ALPHA_KLUDGE_MCR; | |
526 | up->mcr_force = ALPHA_KLUDGE_MCR; | |
1da177e4 LT |
527 | } |
528 | ||
40375393 PH |
529 | /* chain base port ops to support Remote Supervisor Adapter */ |
530 | univ8250_port_ops = *base_ops; | |
531 | univ8250_rsa_support(&univ8250_port_ops); | |
532 | ||
4c0ebb80 AGR |
533 | if (share_irqs) |
534 | irqflag = IRQF_SHARED; | |
535 | ||
44454bcd | 536 | for (i = 0, up = serial8250_ports; |
a61c2d78 | 537 | i < ARRAY_SIZE(old_serial_port) && i < nr_uarts; |
1da177e4 | 538 | i++, up++) { |
dfe42443 PG |
539 | struct uart_port *port = &up->port; |
540 | ||
541 | port->iobase = old_serial_port[i].port; | |
542 | port->irq = irq_canonicalize(old_serial_port[i].irq); | |
b0f8aed2 | 543 | port->irqflags = 0; |
dfe42443 PG |
544 | port->uartclk = old_serial_port[i].baud_base * 16; |
545 | port->flags = old_serial_port[i].flags; | |
b0f8aed2 | 546 | port->hub6 = 0; |
dfe42443 PG |
547 | port->membase = old_serial_port[i].iomem_base; |
548 | port->iotype = old_serial_port[i].io_type; | |
549 | port->regshift = old_serial_port[i].iomem_reg_shift; | |
1a53e079 PH |
550 | serial8250_set_defaults(up); |
551 | ||
dfe42443 | 552 | port->irqflags |= irqflag; |
af7f3743 AC |
553 | if (serial8250_isa_config != NULL) |
554 | serial8250_isa_config(i, &up->port, &up->capabilities); | |
1da177e4 LT |
555 | } |
556 | } | |
557 | ||
558 | static void __init | |
559 | serial8250_register_ports(struct uart_driver *drv, struct device *dev) | |
560 | { | |
561 | int i; | |
562 | ||
b8e7e40a AC |
563 | for (i = 0; i < nr_uarts; i++) { |
564 | struct uart_8250_port *up = &serial8250_ports[i]; | |
b8e7e40a | 565 | |
e4fda3a0 MS |
566 | if (up->port.type == PORT_8250_CIR) |
567 | continue; | |
568 | ||
835d844d SY |
569 | if (up->port.dev) |
570 | continue; | |
1da177e4 LT |
571 | |
572 | up->port.dev = dev; | |
b5d228cc | 573 | |
c7ac15ce | 574 | serial8250_apply_quirks(up); |
1da177e4 LT |
575 | uart_add_one_port(drv, &up->port); |
576 | } | |
577 | } | |
578 | ||
579 | #ifdef CONFIG_SERIAL_8250_CONSOLE | |
580 | ||
6e281571 PH |
581 | static void univ8250_console_write(struct console *co, const char *s, |
582 | unsigned int count) | |
583 | { | |
584 | struct uart_8250_port *up = &serial8250_ports[co->index]; | |
585 | ||
586 | serial8250_console_write(up, s, count); | |
587 | } | |
588 | ||
6e281571 PH |
589 | static int univ8250_console_setup(struct console *co, char *options) |
590 | { | |
87515772 | 591 | struct uart_port *port; |
d70a7b16 | 592 | int retval; |
6e281571 | 593 | |
1da177e4 LT |
594 | /* |
595 | * Check whether an invalid uart number has been specified, and | |
596 | * if so, search for the first available port that does have | |
597 | * console support. | |
598 | */ | |
317a6842 | 599 | if (co->index >= nr_uarts) |
1da177e4 | 600 | co->index = 0; |
87515772 | 601 | port = &serial8250_ports[co->index].port; |
6e281571 | 602 | /* link port to console */ |
87515772 | 603 | port->cons = co; |
1da177e4 | 604 | |
d70a7b16 PH |
605 | retval = serial8250_console_setup(port, options, false); |
606 | if (retval != 0) | |
607 | port->cons = NULL; | |
608 | return retval; | |
1da177e4 LT |
609 | } |
610 | ||
c7cef0a8 | 611 | /** |
6e281571 | 612 | * univ8250_console_match - non-standard console matching |
c7cef0a8 PH |
613 | * @co: registering console |
614 | * @name: name from console command line | |
615 | * @idx: index from console command line | |
616 | * @options: ptr to option string from console command line | |
617 | * | |
618 | * Only attempts to match console command lines of the form: | |
bd94c407 | 619 | * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>] |
ca782f16 | 620 | * console=uart[8250],0x<addr>[,<options>] |
c7cef0a8 PH |
621 | * This form is used to register an initial earlycon boot console and |
622 | * replace it with the serial8250_console at 8250 driver init. | |
623 | * | |
624 | * Performs console setup for a match (as required by interface) | |
ca782f16 | 625 | * If no <options> are specified, then assume the h/w is already setup. |
c7cef0a8 PH |
626 | * |
627 | * Returns 0 if console matches; otherwise non-zero to use default matching | |
628 | */ | |
6e281571 PH |
629 | static int univ8250_console_match(struct console *co, char *name, int idx, |
630 | char *options) | |
18a8bd94 | 631 | { |
c7cef0a8 PH |
632 | char match[] = "uart"; /* 8250-specific earlycon name */ |
633 | unsigned char iotype; | |
46e36683 | 634 | resource_size_t addr; |
c7cef0a8 PH |
635 | int i; |
636 | ||
637 | if (strncmp(name, match, 4) != 0) | |
638 | return -ENODEV; | |
639 | ||
640 | if (uart_parse_earlycon(options, &iotype, &addr, &options)) | |
641 | return -ENODEV; | |
642 | ||
643 | /* try to match the port specified on the command line */ | |
644 | for (i = 0; i < nr_uarts; i++) { | |
645 | struct uart_port *port = &serial8250_ports[i].port; | |
646 | ||
647 | if (port->iotype != iotype) | |
648 | continue; | |
bd94c407 MY |
649 | if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 || |
650 | iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE) | |
651 | && (port->mapbase != addr)) | |
c7cef0a8 PH |
652 | continue; |
653 | if (iotype == UPIO_PORT && port->iobase != addr) | |
654 | continue; | |
655 | ||
656 | co->index = i; | |
87515772 PH |
657 | port->cons = co; |
658 | return serial8250_console_setup(port, options, true); | |
c7cef0a8 PH |
659 | } |
660 | ||
661 | return -ENODEV; | |
18a8bd94 YL |
662 | } |
663 | ||
6e281571 | 664 | static struct console univ8250_console = { |
1da177e4 | 665 | .name = "ttyS", |
6e281571 | 666 | .write = univ8250_console_write, |
1da177e4 | 667 | .device = uart_console_device, |
6e281571 PH |
668 | .setup = univ8250_console_setup, |
669 | .match = univ8250_console_match, | |
6741f551 | 670 | .flags = CON_PRINTBUFFER | CON_ANYTIME, |
1da177e4 LT |
671 | .index = -1, |
672 | .data = &serial8250_reg, | |
673 | }; | |
674 | ||
6e281571 | 675 | static int __init univ8250_console_init(void) |
1da177e4 | 676 | { |
59cfc45f JK |
677 | if (nr_uarts == 0) |
678 | return -ENODEV; | |
679 | ||
1da177e4 | 680 | serial8250_isa_init_ports(); |
6e281571 | 681 | register_console(&univ8250_console); |
1da177e4 LT |
682 | return 0; |
683 | } | |
6e281571 | 684 | console_initcall(univ8250_console_init); |
1da177e4 | 685 | |
149a44cc | 686 | #define SERIAL8250_CONSOLE (&univ8250_console) |
1da177e4 LT |
687 | #else |
688 | #define SERIAL8250_CONSOLE NULL | |
689 | #endif | |
690 | ||
691 | static struct uart_driver serial8250_reg = { | |
692 | .owner = THIS_MODULE, | |
693 | .driver_name = "serial", | |
1da177e4 LT |
694 | .dev_name = "ttyS", |
695 | .major = TTY_MAJOR, | |
696 | .minor = 64, | |
1da177e4 LT |
697 | .cons = SERIAL8250_CONSOLE, |
698 | }; | |
699 | ||
d856c666 RK |
700 | /* |
701 | * early_serial_setup - early registration for 8250 ports | |
702 | * | |
703 | * Setup an 8250 port structure prior to console initialisation. Use | |
704 | * after console initialisation will cause undefined behaviour. | |
705 | */ | |
1da177e4 LT |
706 | int __init early_serial_setup(struct uart_port *port) |
707 | { | |
b430428a DD |
708 | struct uart_port *p; |
709 | ||
59cfc45f | 710 | if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0) |
1da177e4 LT |
711 | return -ENODEV; |
712 | ||
713 | serial8250_isa_init_ports(); | |
b430428a DD |
714 | p = &serial8250_ports[port->line].port; |
715 | p->iobase = port->iobase; | |
716 | p->membase = port->membase; | |
717 | p->irq = port->irq; | |
1c2f0493 | 718 | p->irqflags = port->irqflags; |
b430428a DD |
719 | p->uartclk = port->uartclk; |
720 | p->fifosize = port->fifosize; | |
721 | p->regshift = port->regshift; | |
722 | p->iotype = port->iotype; | |
723 | p->flags = port->flags; | |
724 | p->mapbase = port->mapbase; | |
ee97d0e3 | 725 | p->mapsize = port->mapsize; |
b430428a | 726 | p->private_data = port->private_data; |
125c97d8 HD |
727 | p->type = port->type; |
728 | p->line = port->line; | |
7d6a07d1 | 729 | |
1a53e079 PH |
730 | serial8250_set_defaults(up_to_u8250p(p)); |
731 | ||
7d6a07d1 DD |
732 | if (port->serial_in) |
733 | p->serial_in = port->serial_in; | |
734 | if (port->serial_out) | |
735 | p->serial_out = port->serial_out; | |
583d28e9 JI |
736 | if (port->handle_irq) |
737 | p->handle_irq = port->handle_irq; | |
7d6a07d1 | 738 | |
1da177e4 LT |
739 | return 0; |
740 | } | |
741 | ||
742 | /** | |
743 | * serial8250_suspend_port - suspend one serial port | |
744 | * @line: serial line number | |
1da177e4 LT |
745 | * |
746 | * Suspend one serial port. | |
747 | */ | |
748 | void serial8250_suspend_port(int line) | |
749 | { | |
4516d50a PH |
750 | struct uart_8250_port *up = &serial8250_ports[line]; |
751 | struct uart_port *port = &up->port; | |
752 | ||
753 | if (!console_suspend_enabled && uart_console(port) && | |
754 | port->type != PORT_8250) { | |
755 | unsigned char canary = 0xa5; | |
756 | serial_out(up, UART_SCR, canary); | |
f01a0bd8 PH |
757 | if (serial_in(up, UART_SCR) == canary) |
758 | up->canary = canary; | |
4516d50a PH |
759 | } |
760 | ||
761 | uart_suspend_port(&serial8250_reg, port); | |
1da177e4 | 762 | } |
b34f9faf | 763 | EXPORT_SYMBOL(serial8250_suspend_port); |
1da177e4 LT |
764 | |
765 | /** | |
766 | * serial8250_resume_port - resume one serial port | |
767 | * @line: serial line number | |
1da177e4 LT |
768 | * |
769 | * Resume one serial port. | |
770 | */ | |
771 | void serial8250_resume_port(int line) | |
772 | { | |
b5b82df6 | 773 | struct uart_8250_port *up = &serial8250_ports[line]; |
dfe42443 | 774 | struct uart_port *port = &up->port; |
b5b82df6 | 775 | |
4516d50a PH |
776 | up->canary = 0; |
777 | ||
b5b82df6 | 778 | if (up->capabilities & UART_NATSEMI) { |
b5b82df6 | 779 | /* Ensure it's still in high speed mode */ |
4fd996a1 | 780 | serial_port_out(port, UART_LCR, 0xE0); |
b5b82df6 | 781 | |
0d0389e5 | 782 | ns16550a_goto_highspeed(up); |
b5b82df6 | 783 | |
4fd996a1 | 784 | serial_port_out(port, UART_LCR, 0); |
dfe42443 | 785 | port->uartclk = 921600*16; |
b5b82df6 | 786 | } |
dfe42443 | 787 | uart_resume_port(&serial8250_reg, port); |
1da177e4 | 788 | } |
b34f9faf | 789 | EXPORT_SYMBOL(serial8250_resume_port); |
1da177e4 LT |
790 | |
791 | /* | |
792 | * Register a set of serial devices attached to a platform device. The | |
793 | * list is terminated with a zero flags entry, which means we expect | |
794 | * all entries to have at least UPF_BOOT_AUTOCONF set. | |
795 | */ | |
9671f099 | 796 | static int serial8250_probe(struct platform_device *dev) |
1da177e4 | 797 | { |
574de559 | 798 | struct plat_serial8250_port *p = dev_get_platdata(&dev->dev); |
2655a2c7 | 799 | struct uart_8250_port uart; |
4c0ebb80 | 800 | int ret, i, irqflag = 0; |
1da177e4 | 801 | |
2655a2c7 | 802 | memset(&uart, 0, sizeof(uart)); |
1da177e4 | 803 | |
4c0ebb80 AGR |
804 | if (share_irqs) |
805 | irqflag = IRQF_SHARED; | |
806 | ||
ec9f47cd | 807 | for (i = 0; p && p->flags != 0; p++, i++) { |
2655a2c7 AC |
808 | uart.port.iobase = p->iobase; |
809 | uart.port.membase = p->membase; | |
810 | uart.port.irq = p->irq; | |
811 | uart.port.irqflags = p->irqflags; | |
812 | uart.port.uartclk = p->uartclk; | |
813 | uart.port.regshift = p->regshift; | |
814 | uart.port.iotype = p->iotype; | |
815 | uart.port.flags = p->flags; | |
816 | uart.port.mapbase = p->mapbase; | |
817 | uart.port.hub6 = p->hub6; | |
818 | uart.port.private_data = p->private_data; | |
819 | uart.port.type = p->type; | |
820 | uart.port.serial_in = p->serial_in; | |
821 | uart.port.serial_out = p->serial_out; | |
822 | uart.port.handle_irq = p->handle_irq; | |
823 | uart.port.handle_break = p->handle_break; | |
824 | uart.port.set_termios = p->set_termios; | |
db405a8f | 825 | uart.port.set_ldisc = p->set_ldisc; |
144ef5c2 | 826 | uart.port.get_mctrl = p->get_mctrl; |
2655a2c7 AC |
827 | uart.port.pm = p->pm; |
828 | uart.port.dev = &dev->dev; | |
829 | uart.port.irqflags |= irqflag; | |
830 | ret = serial8250_register_8250_port(&uart); | |
ec9f47cd | 831 | if (ret < 0) { |
3ae5eaec | 832 | dev_err(&dev->dev, "unable to register port at index %d " |
4f640efb JB |
833 | "(IO%lx MEM%llx IRQ%d): %d\n", i, |
834 | p->iobase, (unsigned long long)p->mapbase, | |
835 | p->irq, ret); | |
ec9f47cd | 836 | } |
1da177e4 LT |
837 | } |
838 | return 0; | |
839 | } | |
840 | ||
841 | /* | |
842 | * Remove serial ports registered against a platform device. | |
843 | */ | |
ae8d8a14 | 844 | static int serial8250_remove(struct platform_device *dev) |
1da177e4 LT |
845 | { |
846 | int i; | |
847 | ||
317a6842 | 848 | for (i = 0; i < nr_uarts; i++) { |
1da177e4 LT |
849 | struct uart_8250_port *up = &serial8250_ports[i]; |
850 | ||
3ae5eaec | 851 | if (up->port.dev == &dev->dev) |
1da177e4 LT |
852 | serial8250_unregister_port(i); |
853 | } | |
854 | return 0; | |
855 | } | |
856 | ||
3ae5eaec | 857 | static int serial8250_suspend(struct platform_device *dev, pm_message_t state) |
1da177e4 LT |
858 | { |
859 | int i; | |
860 | ||
1da177e4 LT |
861 | for (i = 0; i < UART_NR; i++) { |
862 | struct uart_8250_port *up = &serial8250_ports[i]; | |
863 | ||
3ae5eaec | 864 | if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) |
1da177e4 LT |
865 | uart_suspend_port(&serial8250_reg, &up->port); |
866 | } | |
867 | ||
868 | return 0; | |
869 | } | |
870 | ||
3ae5eaec | 871 | static int serial8250_resume(struct platform_device *dev) |
1da177e4 LT |
872 | { |
873 | int i; | |
874 | ||
1da177e4 LT |
875 | for (i = 0; i < UART_NR; i++) { |
876 | struct uart_8250_port *up = &serial8250_ports[i]; | |
877 | ||
3ae5eaec | 878 | if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev) |
b5b82df6 | 879 | serial8250_resume_port(i); |
1da177e4 LT |
880 | } |
881 | ||
882 | return 0; | |
883 | } | |
884 | ||
3ae5eaec | 885 | static struct platform_driver serial8250_isa_driver = { |
1da177e4 | 886 | .probe = serial8250_probe, |
2d47b716 | 887 | .remove = serial8250_remove, |
1da177e4 LT |
888 | .suspend = serial8250_suspend, |
889 | .resume = serial8250_resume, | |
3ae5eaec RK |
890 | .driver = { |
891 | .name = "serial8250", | |
892 | }, | |
1da177e4 LT |
893 | }; |
894 | ||
895 | /* | |
896 | * This "device" covers _all_ ISA 8250-compatible serial devices listed | |
897 | * in the table in include/asm/serial.h | |
898 | */ | |
899 | static struct platform_device *serial8250_isa_devs; | |
900 | ||
901 | /* | |
2655a2c7 | 902 | * serial8250_register_8250_port and serial8250_unregister_port allows for |
1da177e4 LT |
903 | * 16x50 serial ports to be configured at run-time, to support PCMCIA |
904 | * modems and PCI multiport cards. | |
905 | */ | |
f392ecfa | 906 | static DEFINE_MUTEX(serial_mutex); |
1da177e4 LT |
907 | |
908 | static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port) | |
909 | { | |
910 | int i; | |
911 | ||
912 | /* | |
913 | * First, find a port entry which matches. | |
914 | */ | |
317a6842 | 915 | for (i = 0; i < nr_uarts; i++) |
1da177e4 LT |
916 | if (uart_match_port(&serial8250_ports[i].port, port)) |
917 | return &serial8250_ports[i]; | |
918 | ||
59b3e898 SAS |
919 | /* try line number first if still available */ |
920 | i = port->line; | |
921 | if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN && | |
922 | serial8250_ports[i].port.iobase == 0) | |
923 | return &serial8250_ports[i]; | |
1da177e4 LT |
924 | /* |
925 | * We didn't find a matching entry, so look for the first | |
926 | * free entry. We look for one which hasn't been previously | |
927 | * used (indicated by zero iobase). | |
928 | */ | |
317a6842 | 929 | for (i = 0; i < nr_uarts; i++) |
1da177e4 LT |
930 | if (serial8250_ports[i].port.type == PORT_UNKNOWN && |
931 | serial8250_ports[i].port.iobase == 0) | |
932 | return &serial8250_ports[i]; | |
933 | ||
934 | /* | |
935 | * That also failed. Last resort is to find any entry which | |
936 | * doesn't have a real port associated with it. | |
937 | */ | |
317a6842 | 938 | for (i = 0; i < nr_uarts; i++) |
1da177e4 LT |
939 | if (serial8250_ports[i].port.type == PORT_UNKNOWN) |
940 | return &serial8250_ports[i]; | |
941 | ||
942 | return NULL; | |
943 | } | |
944 | ||
945 | /** | |
f73fa05b | 946 | * serial8250_register_8250_port - register a serial port |
58bcd332 | 947 | * @up: serial port template |
1da177e4 LT |
948 | * |
949 | * Configure the serial port specified by the request. If the | |
950 | * port exists and is in use, it is hung up and unregistered | |
951 | * first. | |
952 | * | |
953 | * The port is then probed and if necessary the IRQ is autodetected | |
954 | * If this fails an error is returned. | |
955 | * | |
956 | * On success the port is ready to use and the line number is returned. | |
957 | */ | |
f73fa05b | 958 | int serial8250_register_8250_port(struct uart_8250_port *up) |
1da177e4 LT |
959 | { |
960 | struct uart_8250_port *uart; | |
961 | int ret = -ENOSPC; | |
962 | ||
f73fa05b | 963 | if (up->port.uartclk == 0) |
1da177e4 LT |
964 | return -EINVAL; |
965 | ||
f392ecfa | 966 | mutex_lock(&serial_mutex); |
1da177e4 | 967 | |
f73fa05b | 968 | uart = serial8250_find_match_or_unused(&up->port); |
65ecc9c0 | 969 | if (uart && uart->port.type != PORT_8250_CIR) { |
835d844d SY |
970 | if (uart->port.dev) |
971 | uart_remove_one_port(&serial8250_reg, &uart->port); | |
1da177e4 | 972 | |
f73fa05b MD |
973 | uart->port.iobase = up->port.iobase; |
974 | uart->port.membase = up->port.membase; | |
975 | uart->port.irq = up->port.irq; | |
976 | uart->port.irqflags = up->port.irqflags; | |
977 | uart->port.uartclk = up->port.uartclk; | |
978 | uart->port.fifosize = up->port.fifosize; | |
979 | uart->port.regshift = up->port.regshift; | |
980 | uart->port.iotype = up->port.iotype; | |
981 | uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF; | |
a2d33d87 | 982 | uart->bugs = up->bugs; |
f73fa05b | 983 | uart->port.mapbase = up->port.mapbase; |
ee97d0e3 | 984 | uart->port.mapsize = up->port.mapsize; |
f73fa05b | 985 | uart->port.private_data = up->port.private_data; |
9660497c HK |
986 | uart->tx_loadsz = up->tx_loadsz; |
987 | uart->capabilities = up->capabilities; | |
234abab1 SAS |
988 | uart->port.throttle = up->port.throttle; |
989 | uart->port.unthrottle = up->port.unthrottle; | |
46c55b4b RRD |
990 | uart->port.rs485_config = up->port.rs485_config; |
991 | uart->port.rs485 = up->port.rs485; | |
d53e1428 | 992 | uart->dma = up->dma; |
9660497c | 993 | |
6a3f45dc HK |
994 | /* Take tx_loadsz from fifosize if it wasn't set separately */ |
995 | if (uart->port.fifosize && !uart->tx_loadsz) | |
996 | uart->tx_loadsz = uart->port.fifosize; | |
997 | ||
f73fa05b MD |
998 | if (up->port.dev) |
999 | uart->port.dev = up->port.dev; | |
1000 | ||
1001 | if (up->port.flags & UPF_FIXED_TYPE) | |
afd483bd | 1002 | uart->port.type = up->port.type; |
8e23fcc8 | 1003 | |
1a53e079 PH |
1004 | serial8250_set_defaults(uart); |
1005 | ||
7d6a07d1 | 1006 | /* Possibly override default I/O functions. */ |
f73fa05b MD |
1007 | if (up->port.serial_in) |
1008 | uart->port.serial_in = up->port.serial_in; | |
1009 | if (up->port.serial_out) | |
1010 | uart->port.serial_out = up->port.serial_out; | |
1011 | if (up->port.handle_irq) | |
1012 | uart->port.handle_irq = up->port.handle_irq; | |
235dae5d | 1013 | /* Possibly override set_termios call */ |
f73fa05b MD |
1014 | if (up->port.set_termios) |
1015 | uart->port.set_termios = up->port.set_termios; | |
db405a8f EB |
1016 | if (up->port.set_ldisc) |
1017 | uart->port.set_ldisc = up->port.set_ldisc; | |
144ef5c2 WAZ |
1018 | if (up->port.get_mctrl) |
1019 | uart->port.get_mctrl = up->port.get_mctrl; | |
4bf4ea9d PH |
1020 | if (up->port.set_mctrl) |
1021 | uart->port.set_mctrl = up->port.set_mctrl; | |
0238d2b4 JZ |
1022 | if (up->port.get_divisor) |
1023 | uart->port.get_divisor = up->port.get_divisor; | |
1024 | if (up->port.set_divisor) | |
1025 | uart->port.set_divisor = up->port.set_divisor; | |
b99b121b SAS |
1026 | if (up->port.startup) |
1027 | uart->port.startup = up->port.startup; | |
1028 | if (up->port.shutdown) | |
1029 | uart->port.shutdown = up->port.shutdown; | |
f73fa05b MD |
1030 | if (up->port.pm) |
1031 | uart->port.pm = up->port.pm; | |
1032 | if (up->port.handle_break) | |
1033 | uart->port.handle_break = up->port.handle_break; | |
1034 | if (up->dl_read) | |
1035 | uart->dl_read = up->dl_read; | |
1036 | if (up->dl_write) | |
1037 | uart->dl_write = up->dl_write; | |
1da177e4 | 1038 | |
9527b82a SY |
1039 | if (uart->port.type != PORT_8250_CIR) { |
1040 | if (serial8250_isa_config != NULL) | |
1041 | serial8250_isa_config(0, &uart->port, | |
1042 | &uart->capabilities); | |
1043 | ||
cf0a1579 | 1044 | serial8250_apply_quirks(uart); |
9527b82a SY |
1045 | ret = uart_add_one_port(&serial8250_reg, |
1046 | &uart->port); | |
1047 | if (ret == 0) | |
1048 | ret = uart->port.line; | |
1049 | } else { | |
1050 | dev_info(uart->port.dev, | |
1051 | "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n", | |
1052 | uart->port.iobase, | |
1053 | (unsigned long long)uart->port.mapbase, | |
1054 | uart->port.irq); | |
af7f3743 | 1055 | |
9527b82a SY |
1056 | ret = 0; |
1057 | } | |
1da177e4 | 1058 | } |
f392ecfa | 1059 | mutex_unlock(&serial_mutex); |
1da177e4 LT |
1060 | |
1061 | return ret; | |
1062 | } | |
f73fa05b MD |
1063 | EXPORT_SYMBOL(serial8250_register_8250_port); |
1064 | ||
1da177e4 LT |
1065 | /** |
1066 | * serial8250_unregister_port - remove a 16x50 serial port at runtime | |
1067 | * @line: serial line number | |
1068 | * | |
1069 | * Remove one serial port. This may not be called from interrupt | |
1070 | * context. We hand the port back to the our control. | |
1071 | */ | |
1072 | void serial8250_unregister_port(int line) | |
1073 | { | |
1074 | struct uart_8250_port *uart = &serial8250_ports[line]; | |
1075 | ||
f392ecfa | 1076 | mutex_lock(&serial_mutex); |
bf2a0be4 MK |
1077 | |
1078 | if (uart->em485) { | |
1079 | unsigned long flags; | |
1080 | ||
1081 | spin_lock_irqsave(&uart->port.lock, flags); | |
1082 | serial8250_em485_destroy(uart); | |
1083 | spin_unlock_irqrestore(&uart->port.lock, flags); | |
1084 | } | |
1085 | ||
1da177e4 LT |
1086 | uart_remove_one_port(&serial8250_reg, &uart->port); |
1087 | if (serial8250_isa_devs) { | |
1088 | uart->port.flags &= ~UPF_BOOT_AUTOCONF; | |
1089 | uart->port.type = PORT_UNKNOWN; | |
1090 | uart->port.dev = &serial8250_isa_devs->dev; | |
5db496b9 | 1091 | uart->capabilities = 0; |
c7ac15ce | 1092 | serial8250_apply_quirks(uart); |
1da177e4 LT |
1093 | uart_add_one_port(&serial8250_reg, &uart->port); |
1094 | } else { | |
1095 | uart->port.dev = NULL; | |
1096 | } | |
f392ecfa | 1097 | mutex_unlock(&serial_mutex); |
1da177e4 LT |
1098 | } |
1099 | EXPORT_SYMBOL(serial8250_unregister_port); | |
1100 | ||
1101 | static int __init serial8250_init(void) | |
1102 | { | |
25db8ad5 | 1103 | int ret; |
1da177e4 | 1104 | |
59cfc45f JK |
1105 | if (nr_uarts == 0) |
1106 | return -ENODEV; | |
1107 | ||
835d844d | 1108 | serial8250_isa_init_ports(); |
a61c2d78 | 1109 | |
9f59fbf0 PR |
1110 | pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n", |
1111 | nr_uarts, share_irqs ? "en" : "dis"); | |
1da177e4 | 1112 | |
b70ac771 DM |
1113 | #ifdef CONFIG_SPARC |
1114 | ret = sunserial_register_minors(&serial8250_reg, UART_NR); | |
1115 | #else | |
1116 | serial8250_reg.nr = UART_NR; | |
1da177e4 | 1117 | ret = uart_register_driver(&serial8250_reg); |
b70ac771 | 1118 | #endif |
1da177e4 LT |
1119 | if (ret) |
1120 | goto out; | |
1121 | ||
835d844d SY |
1122 | ret = serial8250_pnp_init(); |
1123 | if (ret) | |
1124 | goto unreg_uart_drv; | |
1125 | ||
7493a314 DT |
1126 | serial8250_isa_devs = platform_device_alloc("serial8250", |
1127 | PLAT8250_DEV_LEGACY); | |
1128 | if (!serial8250_isa_devs) { | |
1129 | ret = -ENOMEM; | |
835d844d | 1130 | goto unreg_pnp; |
1da177e4 LT |
1131 | } |
1132 | ||
7493a314 DT |
1133 | ret = platform_device_add(serial8250_isa_devs); |
1134 | if (ret) | |
1135 | goto put_dev; | |
1136 | ||
1da177e4 LT |
1137 | serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev); |
1138 | ||
bc965a7f RK |
1139 | ret = platform_driver_register(&serial8250_isa_driver); |
1140 | if (ret == 0) | |
1141 | goto out; | |
1da177e4 | 1142 | |
bc965a7f | 1143 | platform_device_del(serial8250_isa_devs); |
25db8ad5 | 1144 | put_dev: |
7493a314 | 1145 | platform_device_put(serial8250_isa_devs); |
835d844d SY |
1146 | unreg_pnp: |
1147 | serial8250_pnp_exit(); | |
25db8ad5 | 1148 | unreg_uart_drv: |
b70ac771 DM |
1149 | #ifdef CONFIG_SPARC |
1150 | sunserial_unregister_minors(&serial8250_reg, UART_NR); | |
1151 | #else | |
1da177e4 | 1152 | uart_unregister_driver(&serial8250_reg); |
b70ac771 | 1153 | #endif |
25db8ad5 | 1154 | out: |
1da177e4 LT |
1155 | return ret; |
1156 | } | |
1157 | ||
1158 | static void __exit serial8250_exit(void) | |
1159 | { | |
1160 | struct platform_device *isa_dev = serial8250_isa_devs; | |
1161 | ||
1162 | /* | |
1163 | * This tells serial8250_unregister_port() not to re-register | |
1164 | * the ports (thereby making serial8250_isa_driver permanently | |
1165 | * in use.) | |
1166 | */ | |
1167 | serial8250_isa_devs = NULL; | |
1168 | ||
3ae5eaec | 1169 | platform_driver_unregister(&serial8250_isa_driver); |
1da177e4 LT |
1170 | platform_device_unregister(isa_dev); |
1171 | ||
835d844d SY |
1172 | serial8250_pnp_exit(); |
1173 | ||
b70ac771 DM |
1174 | #ifdef CONFIG_SPARC |
1175 | sunserial_unregister_minors(&serial8250_reg, UART_NR); | |
1176 | #else | |
1da177e4 | 1177 | uart_unregister_driver(&serial8250_reg); |
b70ac771 | 1178 | #endif |
1da177e4 LT |
1179 | } |
1180 | ||
1181 | module_init(serial8250_init); | |
1182 | module_exit(serial8250_exit); | |
1183 | ||
1da177e4 | 1184 | MODULE_LICENSE("GPL"); |
d87a6d95 | 1185 | MODULE_DESCRIPTION("Generic 8250/16x50 serial driver"); |
1da177e4 | 1186 | |
3b60daf8 | 1187 | module_param_hw(share_irqs, uint, other, 0644); |
90ad7c44 | 1188 | MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)"); |
1da177e4 | 1189 | |
a61c2d78 DJ |
1190 | module_param(nr_uarts, uint, 0644); |
1191 | MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")"); | |
1192 | ||
d41a4b51 CE |
1193 | module_param(skip_txen_test, uint, 0644); |
1194 | MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time"); | |
1195 | ||
1da177e4 | 1196 | #ifdef CONFIG_SERIAL_8250_RSA |
3b60daf8 | 1197 | module_param_hw_array(probe_rsa, ulong, ioport, &probe_rsa_count, 0444); |
1da177e4 LT |
1198 | MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA"); |
1199 | #endif | |
1200 | MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR); | |
f2b8dfd9 | 1201 | |
9326b047 | 1202 | #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS |
f2b8dfd9 JB |
1203 | #ifndef MODULE |
1204 | /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name | |
1205 | * working as well for the module options so we don't break people. We | |
1206 | * need to keep the names identical and the convenient macros will happily | |
1207 | * refuse to let us do that by failing the build with redefinition errors | |
1208 | * of global variables. So we stick them inside a dummy function to avoid | |
1209 | * those conflicts. The options still get parsed, and the redefined | |
1210 | * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive. | |
1211 | * | |
1212 | * This is hacky. I'm sorry. | |
1213 | */ | |
1214 | static void __used s8250_options(void) | |
1215 | { | |
1216 | #undef MODULE_PARAM_PREFIX | |
9196d8ac | 1217 | #define MODULE_PARAM_PREFIX "8250_core." |
f2b8dfd9 JB |
1218 | |
1219 | module_param_cb(share_irqs, ¶m_ops_uint, &share_irqs, 0644); | |
1220 | module_param_cb(nr_uarts, ¶m_ops_uint, &nr_uarts, 0644); | |
1221 | module_param_cb(skip_txen_test, ¶m_ops_uint, &skip_txen_test, 0644); | |
1222 | #ifdef CONFIG_SERIAL_8250_RSA | |
1223 | __module_param_call(MODULE_PARAM_PREFIX, probe_rsa, | |
1224 | ¶m_array_ops, .arr = &__param_arr_probe_rsa, | |
91f9d330 | 1225 | 0444, -1, 0); |
f2b8dfd9 JB |
1226 | #endif |
1227 | } | |
1228 | #else | |
9196d8ac | 1229 | MODULE_ALIAS("8250_core"); |
f2b8dfd9 | 1230 | #endif |
f2b8dfd9 | 1231 | #endif |