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