blob: c4e9eba360238dc1db8068db2ee8a23faeaeae60 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Peter Hurleyb6830f62015-06-27 09:19:00 -04002 * Universal/legacy driver for 8250/16550-type serial ports
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
5 *
6 * Copyright (C) 2001 Russell King.
7 *
Peter Hurleyb6830f62015-06-27 09:19:00 -04008 * 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 Torvalds1da177e2005-04-16 15:20:36 -070015 * 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 Torvalds1da177e2005-04-16 15:20:36 -070019 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#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 Torvalds1da177e2005-04-16 15:20:36 -070027#include <linux/delay.h>
Russell Kingd052d1b2005-10-29 19:07:23 +010028#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/tty.h>
Daniel Drakecd3ecad2010-10-20 16:00:48 -070030#include <linux/ratelimit.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/tty_flip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/serial.h>
33#include <linux/serial_8250.h>
Andrew Morton78512ec2005-11-07 00:59:13 -080034#include <linux/nmi.h>
Arjan van de Venf392ecf2006-01-12 18:44:32 +000035#include <linux/mutex.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Ricardo Ribalda Delgado0b4af1d2014-09-09 07:17:45 +020037#include <linux/uaccess.h>
Sebastian Andrzej Siewiord74d5d12014-09-10 21:29:57 +020038#include <linux/pm_runtime.h>
Dan Williams2584cf82015-08-10 23:07:05 -040039#include <linux/io.h>
Paul Gortmaker68163832012-02-09 18:48:19 -050040#ifdef CONFIG_SPARC
41#include <linux/sunserialcore.h>
42#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <asm/irq.h>
45
46#include "8250.h"
47
48/*
49 * Configuration:
Thomas Gleixner40663cc2006-07-01 19:29:43 -070050 * share_irqs - whether we pass IRQF_SHARED to request_irq(). This option
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 * is unsafe when used on edge-triggered interrupts.
52 */
Adrian Bunk408b6642005-05-01 08:59:29 -070053static unsigned int share_irqs = SERIAL8250_SHARE_IRQS;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Dave Jonesa61c2d72006-01-07 23:18:19 +000055static unsigned int nr_uarts = CONFIG_SERIAL_8250_RUNTIME_UARTS;
56
David S. Miller84408382008-10-13 10:45:26 +010057static struct uart_driver serial8250_reg;
58
Chuck Ebbertd41a4b52009-10-01 15:44:26 -070059static unsigned int skip_txen_test; /* force skip of txen test at init time */
60
Jiri Slabye7328ae2011-06-05 22:51:49 +020061#define PASS_LIMIT 512
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Bryan Wua4ed1e42008-05-31 16:10:04 +080063#include <asm/serial.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064/*
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 Vencb3592b2005-11-28 21:04:11 +000073static const struct old_serial_port old_serial_port[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 SERIAL_PORT_DFNS /* defined in asm/serial.h */
75};
76
Russell King026d02a2005-06-29 18:45:19 +010077#define UART_NR CONFIG_SERIAL_8250_NR_UARTS
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79#ifdef CONFIG_SERIAL_8250_RSA
80
81#define PORT_RSA_MAX 4
82static unsigned long probe_rsa[PORT_RSA_MAX];
83static unsigned int probe_rsa_count;
84#endif /* CONFIG_SERIAL_8250_RSA */
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086struct irq_info {
Alan Cox25db8ad2008-08-19 20:49:40 -070087 struct hlist_node node;
88 int irq;
89 spinlock_t lock; /* Protects list not the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct list_head *head;
91};
92
Alan Cox25db8ad2008-08-19 20:49:40 -070093#define NR_IRQ_HASH 32 /* Can be adjusted later */
94static struct hlist_head irq_lists[NR_IRQ_HASH];
95static DEFINE_MUTEX(hash_mutex); /* Used to walk the hash */
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 * 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 Howells7d12e782006-10-05 14:55:46 +0100111static irqreturn_t serial8250_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct irq_info *i = dev_id;
114 struct list_head *l, *end = NULL;
115 int pass_counter = 0, handled = 0;
116
Jiri Slaby934014d2016-05-09 09:11:59 +0200117 pr_debug("%s(%d): start\n", __func__, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 spin_lock(&i->lock);
120
121 l = i->head;
122 do {
123 struct uart_8250_port *up;
Jamie Iles583d28e2011-08-15 10:17:52 +0100124 struct uart_port *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
126 up = list_entry(l, struct uart_8250_port, list);
Jamie Iles583d28e2011-08-15 10:17:52 +0100127 port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Dan Williams49b532f2012-04-06 11:49:44 -0700129 if (port->handle_irq(port)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 handled = 1;
Marc St-Jeanbeab6972007-05-06 14:48:45 -0700131 end = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 } 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 Drakecd3ecad2010-10-20 16:00:48 -0700139 printk_ratelimited(KERN_ERR
140 "serial8250: too much work for irq%d\n", irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 break;
142 }
143 } while (l != end);
144
145 spin_unlock(&i->lock);
146
Jiri Slaby934014d2016-05-09 09:11:59 +0200147 pr_debug("%s(%d): end\n", __func__, irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
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 */
159static 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 Torvalds1da177e2005-04-16 15:20:36 -0700171 spin_unlock_irq(&i->lock);
Alan Cox25db8ad2008-08-19 20:49:40 -0700172 /* List empty so throw away the hash node */
173 if (i->head == NULL) {
174 hlist_del(&i->node);
175 kfree(i);
176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
179static int serial_link_irq_chain(struct uart_8250_port *up)
180{
Alan Cox25db8ad2008-08-19 20:49:40 -0700181 struct hlist_head *h;
182 struct hlist_node *n;
183 struct irq_info *i;
Andy Shevchenkof2fda492020-02-11 15:55:59 +0200184 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Alan Cox25db8ad2008-08-19 20:49:40 -0700186 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 Torvalds1da177e2005-04-16 15:20:36 -0700208 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 ret = request_irq(up->port.irq, serial8250_interrupt,
Andy Shevchenkof2fda492020-02-11 15:55:59 +0200220 up->port.irqflags, "serial", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 if (ret < 0)
222 serial_do_unlink(i, up);
223 }
224
225 return ret;
226}
227
228static void serial_unlink_irq_chain(struct uart_8250_port *up)
229{
Paul Gortmakerbd2fe272014-02-19 15:00:13 -0500230 /*
231 * yes, some broken gcc emit "warning: 'i' may be used uninitialized"
232 * but no, we are not going to take a patch that assigns NULL below.
233 */
Alan Cox25db8ad2008-08-19 20:49:40 -0700234 struct irq_info *i;
235 struct hlist_node *n;
236 struct hlist_head *h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
Alan Cox25db8ad2008-08-19 20:49:40 -0700238 mutex_lock(&hash_mutex);
239
240 h = &irq_lists[up->port.irq % NR_IRQ_HASH];
241
242 hlist_for_each(n, h) {
243 i = hlist_entry(n, struct irq_info, node);
244 if (i->irq == up->port.irq)
245 break;
246 }
247
248 BUG_ON(n == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 BUG_ON(i->head == NULL);
250
251 if (list_empty(i->head))
252 free_irq(up->port.irq, i);
253
254 serial_do_unlink(i, up);
Alan Cox25db8ad2008-08-19 20:49:40 -0700255 mutex_unlock(&hash_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256}
257
258/*
259 * This function is used to handle ports that do not have an
260 * interrupt. This doesn't work very well for 16450's, but gives
261 * barely passable results for a 16550A. (Although at the expense
262 * of much CPU overhead).
263 */
264static void serial8250_timeout(unsigned long data)
265{
266 struct uart_8250_port *up = (struct uart_8250_port *)data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Paul Gortmakera0431472011-12-04 18:42:21 -0500268 up->port.handle_irq(&up->port);
Anton Vorontsov54381062010-10-01 17:21:25 +0400269 mod_timer(&up->timer, jiffies + uart_poll_timeout(&up->port));
Alex Williamson40b36da2007-02-14 00:33:04 -0800270}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Alex Williamson40b36da2007-02-14 00:33:04 -0800272static void serial8250_backup_timeout(unsigned long data)
273{
274 struct uart_8250_port *up = (struct uart_8250_port *)data;
Corey Minyardad4c2aa2007-08-22 14:01:18 -0700275 unsigned int iir, ier = 0, lsr;
276 unsigned long flags;
Alex Williamson40b36da2007-02-14 00:33:04 -0800277
Al Cooperdbb3b1c2011-07-25 16:19:52 -0400278 spin_lock_irqsave(&up->port.lock, flags);
279
Alex Williamson40b36da2007-02-14 00:33:04 -0800280 /*
281 * Must disable interrupts or else we risk racing with the interrupt
282 * based handler.
283 */
Alan Coxd4e33fa2012-01-26 17:44:09 +0000284 if (up->port.irq) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800285 ier = serial_in(up, UART_IER);
286 serial_out(up, UART_IER, 0);
287 }
288
289 iir = serial_in(up, UART_IIR);
290
291 /*
292 * This should be a safe test for anyone who doesn't trust the
293 * IIR bits on their UART, but it's specifically designed for
294 * the "Diva" UART used on the management processor on many HP
295 * ia64 and parisc boxes.
296 */
Corey Minyardad4c2aa2007-08-22 14:01:18 -0700297 lsr = serial_in(up, UART_LSR);
298 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
Alex Williamson40b36da2007-02-14 00:33:04 -0800299 if ((iir & UART_IIR_NO_INT) && (up->ier & UART_IER_THRI) &&
Alan Coxebd2c8f2009-09-19 13:13:28 -0700300 (!uart_circ_empty(&up->port.state->xmit) || up->port.x_char) &&
Corey Minyardad4c2aa2007-08-22 14:01:18 -0700301 (lsr & UART_LSR_THRE)) {
Alex Williamson40b36da2007-02-14 00:33:04 -0800302 iir &= ~(UART_IIR_ID | UART_IIR_NO_INT);
303 iir |= UART_IIR_THRI;
304 }
305
306 if (!(iir & UART_IIR_NO_INT))
Paul Gortmaker3986fb22011-12-04 18:42:20 -0500307 serial8250_tx_chars(up);
Alex Williamson40b36da2007-02-14 00:33:04 -0800308
Alan Coxd4e33fa2012-01-26 17:44:09 +0000309 if (up->port.irq)
Alex Williamson40b36da2007-02-14 00:33:04 -0800310 serial_out(up, UART_IER, ier);
311
Al Cooperdbb3b1c2011-07-25 16:19:52 -0400312 spin_unlock_irqrestore(&up->port.lock, flags);
313
Alex Williamson40b36da2007-02-14 00:33:04 -0800314 /* Standard timer interval plus 0.2s to keep the port running */
Alan Cox6f803cd2008-02-08 04:18:52 -0800315 mod_timer(&up->timer,
Anton Vorontsov54381062010-10-01 17:21:25 +0400316 jiffies + uart_poll_timeout(&up->port) + HZ / 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Peter Hurleya4416cd2015-02-24 14:25:07 -0500319static int univ8250_setup_irq(struct uart_8250_port *up)
320{
321 struct uart_port *port = &up->port;
322 int retval = 0;
323
324 /*
325 * The above check will only give an accurate result the first time
326 * the port is opened so this value needs to be preserved.
327 */
328 if (up->bugs & UART_BUG_THRE) {
329 pr_debug("ttyS%d - using backup timer\n", serial_index(port));
330
331 up->timer.function = serial8250_backup_timeout;
332 up->timer.data = (unsigned long)up;
333 mod_timer(&up->timer, jiffies +
334 uart_poll_timeout(port) + HZ / 5);
335 }
336
337 /*
338 * If the "interrupt" for this port doesn't correspond with any
339 * hardware interrupt, we use a timer-based system. The original
340 * driver used to do this with IRQ0.
341 */
342 if (!port->irq) {
343 up->timer.data = (unsigned long)up;
344 mod_timer(&up->timer, jiffies + uart_poll_timeout(port));
345 } else
346 retval = serial_link_irq_chain(up);
347
348 return retval;
349}
350
351static void univ8250_release_irq(struct uart_8250_port *up)
352{
353 struct uart_port *port = &up->port;
354
355 del_timer_sync(&up->timer);
356 up->timer.function = serial8250_timeout;
357 if (port->irq)
358 serial_unlink_irq_chain(up);
359}
360
Peter Hurleycd52b752015-02-24 14:25:12 -0500361#ifdef CONFIG_SERIAL_8250_RSA
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362static int serial8250_request_rsa_resource(struct uart_8250_port *up)
363{
364 unsigned long start = UART_RSA_BASE << up->port.regshift;
365 unsigned int size = 8 << up->port.regshift;
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500366 struct uart_port *port = &up->port;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +0400367 int ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500369 switch (port->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 case UPIO_HUB6:
371 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500372 start += port->iobase;
Sergei Shtylyov0b30d662006-09-09 22:23:56 +0400373 if (request_region(start, size, "serial-rsa"))
374 ret = 0;
375 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 ret = -EBUSY;
377 break;
378 }
379
380 return ret;
381}
382
383static void serial8250_release_rsa_resource(struct uart_8250_port *up)
384{
385 unsigned long offset = UART_RSA_BASE << up->port.regshift;
386 unsigned int size = 8 << up->port.regshift;
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500387 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500389 switch (port->iotype) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 case UPIO_HUB6:
391 case UPIO_PORT:
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500392 release_region(port->iobase + offset, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 break;
394 }
395}
Peter Hurleycd52b752015-02-24 14:25:12 -0500396#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
Peter Hurley40375392015-02-24 14:25:14 -0500398static const struct uart_ops *base_ops;
399static struct uart_ops univ8250_port_ops;
400
Peter Hurleya4416cd2015-02-24 14:25:07 -0500401static const struct uart_8250_ops univ8250_driver_ops = {
402 .setup_irq = univ8250_setup_irq,
403 .release_irq = univ8250_release_irq,
404};
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406static struct uart_8250_port serial8250_ports[UART_NR];
407
Sebastian Andrzej Siewiorae14a792014-09-05 21:02:36 +0200408/**
409 * serial8250_get_port - retrieve struct uart_8250_port
410 * @line: serial line number
411 *
412 * This function retrieves struct uart_8250_port for the specific line.
413 * This struct *must* *not* be used to perform a 8250 or serial core operation
414 * which is not accessible otherwise. Its only purpose is to make the struct
415 * accessible to the runtime-pm callbacks for context suspend/restore.
416 * The lock assumption made here is none because runtime-pm suspend/resume
417 * callbacks should not be invoked if there is any operation performed on the
418 * port.
419 */
420struct uart_8250_port *serial8250_get_port(int line)
421{
422 return &serial8250_ports[line];
423}
424EXPORT_SYMBOL_GPL(serial8250_get_port);
425
Alan Coxaf7f3742010-10-18 11:38:02 -0700426static void (*serial8250_isa_config)(int port, struct uart_port *up,
427 unsigned short *capabilities);
428
429void serial8250_set_isa_configurator(
430 void (*v)(int port, struct uart_port *up, unsigned short *capabilities))
431{
432 serial8250_isa_config = v;
433}
434EXPORT_SYMBOL(serial8250_set_isa_configurator);
435
Peter Hurley40375392015-02-24 14:25:14 -0500436#ifdef CONFIG_SERIAL_8250_RSA
437
438static void univ8250_config_port(struct uart_port *port, int flags)
439{
440 struct uart_8250_port *up = up_to_u8250p(port);
441
442 up->probe &= ~UART_PROBE_RSA;
443 if (port->type == PORT_RSA) {
444 if (serial8250_request_rsa_resource(up) == 0)
445 up->probe |= UART_PROBE_RSA;
446 } else if (flags & UART_CONFIG_TYPE) {
447 int i;
448
449 for (i = 0; i < probe_rsa_count; i++) {
450 if (probe_rsa[i] == up->port.iobase) {
451 if (serial8250_request_rsa_resource(up) == 0)
452 up->probe |= UART_PROBE_RSA;
453 break;
454 }
455 }
456 }
457
458 base_ops->config_port(port, flags);
459
460 if (port->type != PORT_RSA && up->probe & UART_PROBE_RSA)
461 serial8250_release_rsa_resource(up);
462}
463
464static int univ8250_request_port(struct uart_port *port)
465{
466 struct uart_8250_port *up = up_to_u8250p(port);
467 int ret;
468
469 ret = base_ops->request_port(port);
470 if (ret == 0 && port->type == PORT_RSA) {
471 ret = serial8250_request_rsa_resource(up);
472 if (ret < 0)
473 base_ops->release_port(port);
474 }
475
476 return ret;
477}
478
479static void univ8250_release_port(struct uart_port *port)
480{
481 struct uart_8250_port *up = up_to_u8250p(port);
482
483 if (port->type == PORT_RSA)
484 serial8250_release_rsa_resource(up);
485 base_ops->release_port(port);
486}
487
488static void univ8250_rsa_support(struct uart_ops *ops)
489{
490 ops->config_port = univ8250_config_port;
491 ops->request_port = univ8250_request_port;
492 ops->release_port = univ8250_release_port;
493}
494
495#else
496#define univ8250_rsa_support(x) do { } while (0)
497#endif /* CONFIG_SERIAL_8250_RSA */
498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499static void __init serial8250_isa_init_ports(void)
500{
501 struct uart_8250_port *up;
502 static int first = 1;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -0200503 int i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
505 if (!first)
506 return;
507 first = 0;
508
Sean Young835d8442012-09-07 19:06:23 +0100509 if (nr_uarts > UART_NR)
510 nr_uarts = UART_NR;
511
Kyle McMartin317a6842013-06-03 09:38:26 -0400512 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 struct uart_8250_port *up = &serial8250_ports[i];
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500514 struct uart_port *port = &up->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500516 port->line = i;
Peter Hurley0a16e2c2015-02-24 14:25:08 -0500517 serial8250_init_port(up);
Peter Hurley40375392015-02-24 14:25:14 -0500518 if (!base_ops)
519 base_ops = port->ops;
520 port->ops = &univ8250_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
522 init_timer(&up->timer);
523 up->timer.function = serial8250_timeout;
524
Peter Hurleya4416cd2015-02-24 14:25:07 -0500525 up->ops = &univ8250_driver_ops;
526
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 /*
528 * ALPHA_KLUDGE_MCR needs to be killed.
529 */
530 up->mcr_mask = ~ALPHA_KLUDGE_MCR;
531 up->mcr_force = ALPHA_KLUDGE_MCR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 }
533
Peter Hurley40375392015-02-24 14:25:14 -0500534 /* chain base port ops to support Remote Supervisor Adapter */
535 univ8250_port_ops = *base_ops;
536 univ8250_rsa_support(&univ8250_port_ops);
537
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -0200538 if (share_irqs)
539 irqflag = IRQF_SHARED;
540
Russell King44454bc2005-06-30 22:41:22 +0100541 for (i = 0, up = serial8250_ports;
Dave Jonesa61c2d72006-01-07 23:18:19 +0000542 i < ARRAY_SIZE(old_serial_port) && i < nr_uarts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 i++, up++) {
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500544 struct uart_port *port = &up->port;
545
546 port->iobase = old_serial_port[i].port;
547 port->irq = irq_canonicalize(old_serial_port[i].irq);
Jiri Slabyb0f8aed2016-05-09 09:11:58 +0200548 port->irqflags = 0;
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500549 port->uartclk = old_serial_port[i].baud_base * 16;
550 port->flags = old_serial_port[i].flags;
Jiri Slabyb0f8aed2016-05-09 09:11:58 +0200551 port->hub6 = 0;
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500552 port->membase = old_serial_port[i].iomem_base;
553 port->iotype = old_serial_port[i].io_type;
554 port->regshift = old_serial_port[i].iomem_reg_shift;
Peter Hurley1a53e072015-02-24 14:25:09 -0500555 serial8250_set_defaults(up);
556
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500557 port->irqflags |= irqflag;
Alan Coxaf7f3742010-10-18 11:38:02 -0700558 if (serial8250_isa_config != NULL)
559 serial8250_isa_config(i, &up->port, &up->capabilities);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 }
561}
562
563static void __init
564serial8250_register_ports(struct uart_driver *drv, struct device *dev)
565{
566 int i;
567
Alan Coxb8e7e402009-05-28 14:01:35 +0100568 for (i = 0; i < nr_uarts; i++) {
569 struct uart_8250_port *up = &serial8250_ports[i];
Alan Coxb8e7e402009-05-28 14:01:35 +0100570
Maciej S. Szmigieroe4fda3a2015-09-27 16:25:56 +0200571 if (up->port.type == PORT_8250_CIR)
572 continue;
573
Sean Young835d8442012-09-07 19:06:23 +0100574 if (up->port.dev)
575 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576
577 up->port.dev = dev;
Shmulik Ladkanib5d228c2009-12-09 12:31:29 -0800578
Peter Hurleye13cb722015-02-24 14:25:05 -0500579 if (skip_txen_test)
580 up->port.flags |= UPF_NO_TXEN_TEST;
581
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 uart_add_one_port(drv, &up->port);
583 }
584}
585
586#ifdef CONFIG_SERIAL_8250_CONSOLE
587
Peter Hurley6e281572015-02-24 14:25:06 -0500588static void univ8250_console_write(struct console *co, const char *s,
589 unsigned int count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Peter Hurley6e281572015-02-24 14:25:06 -0500591 struct uart_8250_port *up = &serial8250_ports[co->index];
592
593 serial8250_console_write(up, s, count);
594}
595
Peter Hurley6e281572015-02-24 14:25:06 -0500596static int univ8250_console_setup(struct console *co, char *options)
597{
Peter Hurley87515772015-04-06 10:48:49 -0400598 struct uart_port *port;
Peter Hurleyd70a7b12016-01-10 14:39:34 -0800599 int retval;
Peter Hurley6e281572015-02-24 14:25:06 -0500600
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 /*
602 * Check whether an invalid uart number has been specified, and
603 * if so, search for the first available port that does have
604 * console support.
605 */
Kyle McMartin317a6842013-06-03 09:38:26 -0400606 if (co->index >= nr_uarts)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 co->index = 0;
Peter Hurley87515772015-04-06 10:48:49 -0400608 port = &serial8250_ports[co->index].port;
Peter Hurley6e281572015-02-24 14:25:06 -0500609 /* link port to console */
Peter Hurley87515772015-04-06 10:48:49 -0400610 port->cons = co;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611
Peter Hurleyd70a7b12016-01-10 14:39:34 -0800612 retval = serial8250_console_setup(port, options, false);
613 if (retval != 0)
614 port->cons = NULL;
615 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616}
617
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400618/**
Peter Hurley6e281572015-02-24 14:25:06 -0500619 * univ8250_console_match - non-standard console matching
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400620 * @co: registering console
621 * @name: name from console command line
622 * @idx: index from console command line
623 * @options: ptr to option string from console command line
624 *
625 * Only attempts to match console command lines of the form:
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900626 * console=uart[8250],io|mmio|mmio16|mmio32,<addr>[,<options>]
Peter Hurleyca782f12015-04-06 10:52:39 -0400627 * console=uart[8250],0x<addr>[,<options>]
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400628 * This form is used to register an initial earlycon boot console and
629 * replace it with the serial8250_console at 8250 driver init.
630 *
631 * Performs console setup for a match (as required by interface)
Peter Hurleyca782f12015-04-06 10:52:39 -0400632 * If no <options> are specified, then assume the h/w is already setup.
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400633 *
634 * Returns 0 if console matches; otherwise non-zero to use default matching
635 */
Peter Hurley6e281572015-02-24 14:25:06 -0500636static int univ8250_console_match(struct console *co, char *name, int idx,
637 char *options)
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700638{
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400639 char match[] = "uart"; /* 8250-specific earlycon name */
640 unsigned char iotype;
Alexander Sverdlin46e36682016-09-02 13:20:21 +0200641 resource_size_t addr;
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400642 int i;
643
644 if (strncmp(name, match, 4) != 0)
645 return -ENODEV;
646
647 if (uart_parse_earlycon(options, &iotype, &addr, &options))
648 return -ENODEV;
649
650 /* try to match the port specified on the command line */
651 for (i = 0; i < nr_uarts; i++) {
652 struct uart_port *port = &serial8250_ports[i].port;
653
654 if (port->iotype != iotype)
655 continue;
Masahiro Yamadabd94c402015-10-28 12:46:05 +0900656 if ((iotype == UPIO_MEM || iotype == UPIO_MEM16 ||
657 iotype == UPIO_MEM32 || iotype == UPIO_MEM32BE)
658 && (port->mapbase != addr))
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400659 continue;
660 if (iotype == UPIO_PORT && port->iobase != addr)
661 continue;
662
663 co->index = i;
Peter Hurley87515772015-04-06 10:48:49 -0400664 port->cons = co;
665 return serial8250_console_setup(port, options, true);
Peter Hurleyc7cef0a2015-03-09 16:27:12 -0400666 }
667
668 return -ENODEV;
Yinghai Lu18a8bd92007-07-15 23:37:59 -0700669}
670
Peter Hurley6e281572015-02-24 14:25:06 -0500671static struct console univ8250_console = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 .name = "ttyS",
Peter Hurley6e281572015-02-24 14:25:06 -0500673 .write = univ8250_console_write,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 .device = uart_console_device,
Peter Hurley6e281572015-02-24 14:25:06 -0500675 .setup = univ8250_console_setup,
676 .match = univ8250_console_match,
Herbert Xu1f3636392016-12-11 10:05:49 +0800677 .flags = CON_PRINTBUFFER | CON_ANYTIME,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 .index = -1,
679 .data = &serial8250_reg,
680};
681
Peter Hurley6e281572015-02-24 14:25:06 -0500682static int __init univ8250_console_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
Jan Kiszka59cfc452015-05-05 08:26:27 +0200684 if (nr_uarts == 0)
685 return -ENODEV;
686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 serial8250_isa_init_ports();
Peter Hurley6e281572015-02-24 14:25:06 -0500688 register_console(&univ8250_console);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return 0;
690}
Peter Hurley6e281572015-02-24 14:25:06 -0500691console_initcall(univ8250_console_init);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
Anton Wuerfel149a44c2016-01-14 16:08:17 +0100693#define SERIAL8250_CONSOLE (&univ8250_console)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694#else
695#define SERIAL8250_CONSOLE NULL
696#endif
697
698static struct uart_driver serial8250_reg = {
699 .owner = THIS_MODULE,
700 .driver_name = "serial",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 .dev_name = "ttyS",
702 .major = TTY_MAJOR,
703 .minor = 64,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 .cons = SERIAL8250_CONSOLE,
705};
706
Russell Kingd856c662006-02-23 10:22:13 +0000707/*
708 * early_serial_setup - early registration for 8250 ports
709 *
710 * Setup an 8250 port structure prior to console initialisation. Use
711 * after console initialisation will cause undefined behaviour.
712 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713int __init early_serial_setup(struct uart_port *port)
714{
David Daneyb4304282009-01-02 13:49:41 +0000715 struct uart_port *p;
716
Jan Kiszka59cfc452015-05-05 08:26:27 +0200717 if (port->line >= ARRAY_SIZE(serial8250_ports) || nr_uarts == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 return -ENODEV;
719
720 serial8250_isa_init_ports();
David Daneyb4304282009-01-02 13:49:41 +0000721 p = &serial8250_ports[port->line].port;
722 p->iobase = port->iobase;
723 p->membase = port->membase;
724 p->irq = port->irq;
Vikram Pandita1c2f0492009-09-19 13:13:19 -0700725 p->irqflags = port->irqflags;
David Daneyb4304282009-01-02 13:49:41 +0000726 p->uartclk = port->uartclk;
727 p->fifosize = port->fifosize;
728 p->regshift = port->regshift;
729 p->iotype = port->iotype;
730 p->flags = port->flags;
731 p->mapbase = port->mapbase;
Mans Rullgardee97d0e32015-03-08 14:30:04 +0000732 p->mapsize = port->mapsize;
David Daneyb4304282009-01-02 13:49:41 +0000733 p->private_data = port->private_data;
Helge Deller125c97d2009-01-13 22:51:07 +0100734 p->type = port->type;
735 p->line = port->line;
David Daney7d6a07d2009-01-02 13:49:47 +0000736
Peter Hurley1a53e072015-02-24 14:25:09 -0500737 serial8250_set_defaults(up_to_u8250p(p));
738
David Daney7d6a07d2009-01-02 13:49:47 +0000739 if (port->serial_in)
740 p->serial_in = port->serial_in;
741 if (port->serial_out)
742 p->serial_out = port->serial_out;
Jamie Iles583d28e2011-08-15 10:17:52 +0100743 if (port->handle_irq)
744 p->handle_irq = port->handle_irq;
David Daney7d6a07d2009-01-02 13:49:47 +0000745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 return 0;
747}
748
749/**
750 * serial8250_suspend_port - suspend one serial port
751 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 *
753 * Suspend one serial port.
754 */
755void serial8250_suspend_port(int line)
756{
Peter Hurley4516d502015-01-22 12:24:30 -0500757 struct uart_8250_port *up = &serial8250_ports[line];
758 struct uart_port *port = &up->port;
759
760 if (!console_suspend_enabled && uart_console(port) &&
761 port->type != PORT_8250) {
762 unsigned char canary = 0xa5;
763 serial_out(up, UART_SCR, canary);
Peter Hurleyf01a0bd2015-03-09 14:05:01 -0400764 if (serial_in(up, UART_SCR) == canary)
765 up->canary = canary;
Peter Hurley4516d502015-01-22 12:24:30 -0500766 }
767
768 uart_suspend_port(&serial8250_reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
Anton Wuerfelb34f9fa2016-01-14 16:08:15 +0100770EXPORT_SYMBOL(serial8250_suspend_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771
772/**
773 * serial8250_resume_port - resume one serial port
774 * @line: serial line number
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *
776 * Resume one serial port.
777 */
778void serial8250_resume_port(int line)
779{
David Woodhouseb5b82df2007-05-17 14:27:39 +0800780 struct uart_8250_port *up = &serial8250_ports[line];
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500781 struct uart_port *port = &up->port;
David Woodhouseb5b82df2007-05-17 14:27:39 +0800782
Peter Hurley4516d502015-01-22 12:24:30 -0500783 up->canary = 0;
784
David Woodhouseb5b82df2007-05-17 14:27:39 +0800785 if (up->capabilities & UART_NATSEMI) {
David Woodhouseb5b82df2007-05-17 14:27:39 +0800786 /* Ensure it's still in high speed mode */
Paul Gortmaker4fd996a2012-03-08 19:12:13 -0500787 serial_port_out(port, UART_LCR, 0xE0);
David Woodhouseb5b82df2007-05-17 14:27:39 +0800788
Yin Kangkai0d0389e2011-02-09 11:35:18 +0800789 ns16550a_goto_highspeed(up);
David Woodhouseb5b82df2007-05-17 14:27:39 +0800790
Paul Gortmaker4fd996a2012-03-08 19:12:13 -0500791 serial_port_out(port, UART_LCR, 0);
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500792 port->uartclk = 921600*16;
David Woodhouseb5b82df2007-05-17 14:27:39 +0800793 }
Paul Gortmakerdfe42442012-03-08 19:12:11 -0500794 uart_resume_port(&serial8250_reg, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Anton Wuerfelb34f9fa2016-01-14 16:08:15 +0100796EXPORT_SYMBOL(serial8250_resume_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798/*
799 * Register a set of serial devices attached to a platform device. The
800 * list is terminated with a zero flags entry, which means we expect
801 * all entries to have at least UPF_BOOT_AUTOCONF set.
802 */
Bill Pemberton9671f092012-11-19 13:21:50 -0500803static int serial8250_probe(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804{
Jingoo Han574de552013-07-30 17:06:57 +0900805 struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
Alan Cox2655a2c2012-07-12 12:59:50 +0100806 struct uart_8250_port uart;
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -0200807 int ret, i, irqflag = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Alan Cox2655a2c2012-07-12 12:59:50 +0100809 memset(&uart, 0, sizeof(uart));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
André Goddard Rosa4c0ebb82009-10-25 12:01:34 -0200811 if (share_irqs)
812 irqflag = IRQF_SHARED;
813
Russell Kingec9f47c2005-06-27 11:12:54 +0100814 for (i = 0; p && p->flags != 0; p++, i++) {
Alan Cox2655a2c2012-07-12 12:59:50 +0100815 uart.port.iobase = p->iobase;
816 uart.port.membase = p->membase;
817 uart.port.irq = p->irq;
818 uart.port.irqflags = p->irqflags;
819 uart.port.uartclk = p->uartclk;
820 uart.port.regshift = p->regshift;
821 uart.port.iotype = p->iotype;
822 uart.port.flags = p->flags;
823 uart.port.mapbase = p->mapbase;
824 uart.port.hub6 = p->hub6;
825 uart.port.private_data = p->private_data;
826 uart.port.type = p->type;
827 uart.port.serial_in = p->serial_in;
828 uart.port.serial_out = p->serial_out;
829 uart.port.handle_irq = p->handle_irq;
830 uart.port.handle_break = p->handle_break;
831 uart.port.set_termios = p->set_termios;
Wan Ahmad Zainie144ef5c22016-04-06 12:06:51 +0800832 uart.port.get_mctrl = p->get_mctrl;
Alan Cox2655a2c2012-07-12 12:59:50 +0100833 uart.port.pm = p->pm;
834 uart.port.dev = &dev->dev;
835 uart.port.irqflags |= irqflag;
836 ret = serial8250_register_8250_port(&uart);
Russell Kingec9f47c2005-06-27 11:12:54 +0100837 if (ret < 0) {
Russell King3ae5eae2005-11-09 22:32:44 +0000838 dev_err(&dev->dev, "unable to register port at index %d "
Josh Boyer4f640ef2007-07-23 18:43:44 -0700839 "(IO%lx MEM%llx IRQ%d): %d\n", i,
840 p->iobase, (unsigned long long)p->mapbase,
841 p->irq, ret);
Russell Kingec9f47c2005-06-27 11:12:54 +0100842 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 }
844 return 0;
845}
846
847/*
848 * Remove serial ports registered against a platform device.
849 */
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500850static int serial8250_remove(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851{
852 int i;
853
Kyle McMartin317a6842013-06-03 09:38:26 -0400854 for (i = 0; i < nr_uarts; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 struct uart_8250_port *up = &serial8250_ports[i];
856
Russell King3ae5eae2005-11-09 22:32:44 +0000857 if (up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 serial8250_unregister_port(i);
859 }
860 return 0;
861}
862
Russell King3ae5eae2005-11-09 22:32:44 +0000863static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864{
865 int i;
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 for (i = 0; i < UART_NR; i++) {
868 struct uart_8250_port *up = &serial8250_ports[i];
869
Russell King3ae5eae2005-11-09 22:32:44 +0000870 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 uart_suspend_port(&serial8250_reg, &up->port);
872 }
873
874 return 0;
875}
876
Russell King3ae5eae2005-11-09 22:32:44 +0000877static int serial8250_resume(struct platform_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 int i;
880
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 for (i = 0; i < UART_NR; i++) {
882 struct uart_8250_port *up = &serial8250_ports[i];
883
Russell King3ae5eae2005-11-09 22:32:44 +0000884 if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
David Woodhouseb5b82df2007-05-17 14:27:39 +0800885 serial8250_resume_port(i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
887
888 return 0;
889}
890
Russell King3ae5eae2005-11-09 22:32:44 +0000891static struct platform_driver serial8250_isa_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 .probe = serial8250_probe,
Bill Pemberton2d47b712012-11-19 13:21:34 -0500893 .remove = serial8250_remove,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 .suspend = serial8250_suspend,
895 .resume = serial8250_resume,
Russell King3ae5eae2005-11-09 22:32:44 +0000896 .driver = {
897 .name = "serial8250",
898 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899};
900
901/*
902 * This "device" covers _all_ ISA 8250-compatible serial devices listed
903 * in the table in include/asm/serial.h
904 */
905static struct platform_device *serial8250_isa_devs;
906
907/*
Alan Cox2655a2c2012-07-12 12:59:50 +0100908 * serial8250_register_8250_port and serial8250_unregister_port allows for
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * 16x50 serial ports to be configured at run-time, to support PCMCIA
910 * modems and PCI multiport cards.
911 */
Arjan van de Venf392ecf2006-01-12 18:44:32 +0000912static DEFINE_MUTEX(serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
915{
916 int i;
917
918 /*
919 * First, find a port entry which matches.
920 */
Kyle McMartin317a6842013-06-03 09:38:26 -0400921 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (uart_match_port(&serial8250_ports[i].port, port))
923 return &serial8250_ports[i];
924
Sebastian Andrzej Siewior59b3e892014-09-10 21:29:59 +0200925 /* try line number first if still available */
926 i = port->line;
927 if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
928 serial8250_ports[i].port.iobase == 0)
929 return &serial8250_ports[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /*
931 * We didn't find a matching entry, so look for the first
932 * free entry. We look for one which hasn't been previously
933 * used (indicated by zero iobase).
934 */
Kyle McMartin317a6842013-06-03 09:38:26 -0400935 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
937 serial8250_ports[i].port.iobase == 0)
938 return &serial8250_ports[i];
939
940 /*
941 * That also failed. Last resort is to find any entry which
942 * doesn't have a real port associated with it.
943 */
Kyle McMartin317a6842013-06-03 09:38:26 -0400944 for (i = 0; i < nr_uarts; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 if (serial8250_ports[i].port.type == PORT_UNKNOWN)
946 return &serial8250_ports[i];
947
948 return NULL;
949}
950
951/**
Magnus Dammf73fa052012-05-02 21:47:27 +0900952 * serial8250_register_8250_port - register a serial port
Randy Dunlap58bcd332012-06-08 18:51:23 -0700953 * @up: serial port template
Magnus Dammf73fa052012-05-02 21:47:27 +0900954 *
955 * Configure the serial port specified by the request. If the
956 * port exists and is in use, it is hung up and unregistered
957 * first.
958 *
959 * The port is then probed and if necessary the IRQ is autodetected
960 * If this fails an error is returned.
961 *
962 * On success the port is ready to use and the line number is returned.
963 */
964int serial8250_register_8250_port(struct uart_8250_port *up)
965{
966 struct uart_8250_port *uart;
967 int ret = -ENOSPC;
968
969 if (up->port.uartclk == 0)
970 return -EINVAL;
971
972 mutex_lock(&serial_mutex);
973
974 uart = serial8250_find_match_or_unused(&up->port);
Sean Young65ecc9c2012-09-07 19:06:24 +0100975 if (uart && uart->port.type != PORT_8250_CIR) {
Sean Young835d8442012-09-07 19:06:23 +0100976 if (uart->port.dev)
977 uart_remove_one_port(&serial8250_reg, &uart->port);
Magnus Dammf73fa052012-05-02 21:47:27 +0900978
979 uart->port.iobase = up->port.iobase;
980 uart->port.membase = up->port.membase;
981 uart->port.irq = up->port.irq;
982 uart->port.irqflags = up->port.irqflags;
983 uart->port.uartclk = up->port.uartclk;
984 uart->port.fifosize = up->port.fifosize;
985 uart->port.regshift = up->port.regshift;
986 uart->port.iotype = up->port.iotype;
987 uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
Alan Coxa2d33d82012-07-12 13:00:06 +0100988 uart->bugs = up->bugs;
Magnus Dammf73fa052012-05-02 21:47:27 +0900989 uart->port.mapbase = up->port.mapbase;
Mans Rullgardee97d0e32015-03-08 14:30:04 +0000990 uart->port.mapsize = up->port.mapsize;
Magnus Dammf73fa052012-05-02 21:47:27 +0900991 uart->port.private_data = up->port.private_data;
Heikki Krogerus96604972013-01-10 11:25:05 +0200992 uart->tx_loadsz = up->tx_loadsz;
993 uart->capabilities = up->capabilities;
Sebastian Andrzej Siewior234abab2014-09-10 21:29:56 +0200994 uart->port.throttle = up->port.throttle;
995 uart->port.unthrottle = up->port.unthrottle;
Ricardo Ribalda Delgado46c55b42014-11-06 09:22:51 +0100996 uart->port.rs485_config = up->port.rs485_config;
997 uart->port.rs485 = up->port.rs485;
Peter Hurleyd53e1422015-02-24 14:25:11 -0500998 uart->dma = up->dma;
Heikki Krogerus96604972013-01-10 11:25:05 +0200999
Heikki Krogerus6a3f45d2013-03-25 13:34:44 +02001000 /* Take tx_loadsz from fifosize if it wasn't set separately */
1001 if (uart->port.fifosize && !uart->tx_loadsz)
1002 uart->tx_loadsz = uart->port.fifosize;
1003
Magnus Dammf73fa052012-05-02 21:47:27 +09001004 if (up->port.dev)
1005 uart->port.dev = up->port.dev;
1006
Peter Hurleye13cb722015-02-24 14:25:05 -05001007 if (skip_txen_test)
1008 uart->port.flags |= UPF_NO_TXEN_TEST;
1009
Magnus Dammf73fa052012-05-02 21:47:27 +09001010 if (up->port.flags & UPF_FIXED_TYPE)
Peter Hurleyafd483b2015-02-24 14:25:10 -05001011 uart->port.type = up->port.type;
Magnus Dammf73fa052012-05-02 21:47:27 +09001012
Peter Hurley1a53e072015-02-24 14:25:09 -05001013 serial8250_set_defaults(uart);
1014
Magnus Dammf73fa052012-05-02 21:47:27 +09001015 /* Possibly override default I/O functions. */
1016 if (up->port.serial_in)
1017 uart->port.serial_in = up->port.serial_in;
1018 if (up->port.serial_out)
1019 uart->port.serial_out = up->port.serial_out;
1020 if (up->port.handle_irq)
1021 uart->port.handle_irq = up->port.handle_irq;
1022 /* Possibly override set_termios call */
1023 if (up->port.set_termios)
1024 uart->port.set_termios = up->port.set_termios;
Wan Ahmad Zainie144ef5c22016-04-06 12:06:51 +08001025 if (up->port.get_mctrl)
1026 uart->port.get_mctrl = up->port.get_mctrl;
Peter Hurley4bf4ea92014-12-30 20:28:15 -05001027 if (up->port.set_mctrl)
1028 uart->port.set_mctrl = up->port.set_mctrl;
Sebastian Andrzej Siewiorb99b1212014-09-05 21:02:37 +02001029 if (up->port.startup)
1030 uart->port.startup = up->port.startup;
1031 if (up->port.shutdown)
1032 uart->port.shutdown = up->port.shutdown;
Magnus Dammf73fa052012-05-02 21:47:27 +09001033 if (up->port.pm)
1034 uart->port.pm = up->port.pm;
1035 if (up->port.handle_break)
1036 uart->port.handle_break = up->port.handle_break;
1037 if (up->dl_read)
1038 uart->dl_read = up->dl_read;
1039 if (up->dl_write)
1040 uart->dl_write = up->dl_write;
1041
Maciej S. Szmigieroe4fda3a2015-09-27 16:25:56 +02001042 if (uart->port.type != PORT_8250_CIR) {
1043 if (serial8250_isa_config != NULL)
1044 serial8250_isa_config(0, &uart->port,
1045 &uart->capabilities);
Magnus Dammf73fa052012-05-02 21:47:27 +09001046
Maciej S. Szmigieroe4fda3a2015-09-27 16:25:56 +02001047 ret = uart_add_one_port(&serial8250_reg,
1048 &uart->port);
1049 if (ret == 0)
1050 ret = uart->port.line;
1051 } else {
1052 dev_info(uart->port.dev,
1053 "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
1054 uart->port.iobase,
1055 (unsigned long long)uart->port.mapbase,
1056 uart->port.irq);
1057
1058 ret = 0;
1059 }
Magnus Dammf73fa052012-05-02 21:47:27 +09001060 }
1061 mutex_unlock(&serial_mutex);
1062
1063 return ret;
1064}
1065EXPORT_SYMBOL(serial8250_register_8250_port);
1066
1067/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 * serial8250_unregister_port - remove a 16x50 serial port at runtime
1069 * @line: serial line number
1070 *
1071 * Remove one serial port. This may not be called from interrupt
1072 * context. We hand the port back to the our control.
1073 */
1074void serial8250_unregister_port(int line)
1075{
1076 struct uart_8250_port *uart = &serial8250_ports[line];
1077
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001078 mutex_lock(&serial_mutex);
Matwey V. Kornilovbf2a0be2016-02-15 21:42:12 +03001079
1080 if (uart->em485) {
1081 unsigned long flags;
1082
1083 spin_lock_irqsave(&uart->port.lock, flags);
1084 serial8250_em485_destroy(uart);
1085 spin_unlock_irqrestore(&uart->port.lock, flags);
1086 }
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 uart_remove_one_port(&serial8250_reg, &uart->port);
1089 if (serial8250_isa_devs) {
1090 uart->port.flags &= ~UPF_BOOT_AUTOCONF;
Peter Hurleye13cb722015-02-24 14:25:05 -05001091 if (skip_txen_test)
1092 uart->port.flags |= UPF_NO_TXEN_TEST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 uart->port.type = PORT_UNKNOWN;
1094 uart->port.dev = &serial8250_isa_devs->dev;
Peter Hurley5db496b2015-02-24 14:25:03 -05001095 uart->capabilities = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 uart_add_one_port(&serial8250_reg, &uart->port);
1097 } else {
1098 uart->port.dev = NULL;
1099 }
Arjan van de Venf392ecf2006-01-12 18:44:32 +00001100 mutex_unlock(&serial_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101}
1102EXPORT_SYMBOL(serial8250_unregister_port);
1103
1104static int __init serial8250_init(void)
1105{
Alan Cox25db8ad2008-08-19 20:49:40 -07001106 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jan Kiszka59cfc452015-05-05 08:26:27 +02001108 if (nr_uarts == 0)
1109 return -ENODEV;
1110
Sean Young835d8442012-09-07 19:06:23 +01001111 serial8250_isa_init_ports();
Dave Jonesa61c2d72006-01-07 23:18:19 +00001112
Phillip Raffeck9f59fbf2016-01-14 16:08:19 +01001113 pr_info("Serial: 8250/16550 driver, %d ports, IRQ sharing %sabled\n",
1114 nr_uarts, share_irqs ? "en" : "dis");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
David Millerb70ac772008-10-13 10:36:31 +01001116#ifdef CONFIG_SPARC
1117 ret = sunserial_register_minors(&serial8250_reg, UART_NR);
1118#else
1119 serial8250_reg.nr = UART_NR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 ret = uart_register_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01001121#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 if (ret)
1123 goto out;
1124
Sean Young835d8442012-09-07 19:06:23 +01001125 ret = serial8250_pnp_init();
1126 if (ret)
1127 goto unreg_uart_drv;
1128
Dmitry Torokhov7493a312006-01-13 22:06:43 +00001129 serial8250_isa_devs = platform_device_alloc("serial8250",
1130 PLAT8250_DEV_LEGACY);
1131 if (!serial8250_isa_devs) {
1132 ret = -ENOMEM;
Sean Young835d8442012-09-07 19:06:23 +01001133 goto unreg_pnp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
Dmitry Torokhov7493a312006-01-13 22:06:43 +00001136 ret = platform_device_add(serial8250_isa_devs);
1137 if (ret)
1138 goto put_dev;
1139
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
1141
Russell Kingbc965a72006-01-18 09:54:29 +00001142 ret = platform_driver_register(&serial8250_isa_driver);
1143 if (ret == 0)
1144 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
Russell Kingbc965a72006-01-18 09:54:29 +00001146 platform_device_del(serial8250_isa_devs);
Alan Cox25db8ad2008-08-19 20:49:40 -07001147put_dev:
Dmitry Torokhov7493a312006-01-13 22:06:43 +00001148 platform_device_put(serial8250_isa_devs);
Sean Young835d8442012-09-07 19:06:23 +01001149unreg_pnp:
1150 serial8250_pnp_exit();
Alan Cox25db8ad2008-08-19 20:49:40 -07001151unreg_uart_drv:
David Millerb70ac772008-10-13 10:36:31 +01001152#ifdef CONFIG_SPARC
1153 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1154#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01001156#endif
Alan Cox25db8ad2008-08-19 20:49:40 -07001157out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 return ret;
1159}
1160
1161static void __exit serial8250_exit(void)
1162{
1163 struct platform_device *isa_dev = serial8250_isa_devs;
1164
1165 /*
1166 * This tells serial8250_unregister_port() not to re-register
1167 * the ports (thereby making serial8250_isa_driver permanently
1168 * in use.)
1169 */
1170 serial8250_isa_devs = NULL;
1171
Russell King3ae5eae2005-11-09 22:32:44 +00001172 platform_driver_unregister(&serial8250_isa_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 platform_device_unregister(isa_dev);
1174
Sean Young835d8442012-09-07 19:06:23 +01001175 serial8250_pnp_exit();
1176
David Millerb70ac772008-10-13 10:36:31 +01001177#ifdef CONFIG_SPARC
1178 sunserial_unregister_minors(&serial8250_reg, UART_NR);
1179#else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 uart_unregister_driver(&serial8250_reg);
David Millerb70ac772008-10-13 10:36:31 +01001181#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
1184module_init(serial8250_init);
1185module_exit(serial8250_exit);
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187MODULE_LICENSE("GPL");
Adrian Bunkd87a6d92008-07-16 21:53:31 +01001188MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190module_param(share_irqs, uint, 0644);
Anton Wuerfel90ad7c42016-01-14 16:08:18 +01001191MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices (unsafe)");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192
Dave Jonesa61c2d72006-01-07 23:18:19 +00001193module_param(nr_uarts, uint, 0644);
1194MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
1195
Chuck Ebbertd41a4b52009-10-01 15:44:26 -07001196module_param(skip_txen_test, uint, 0644);
1197MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
1198
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199#ifdef CONFIG_SERIAL_8250_RSA
1200module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
1201MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
1202#endif
1203MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
Josh Boyerf2b8dfd2013-03-10 10:33:40 -04001204
Jiri Slaby9326b042013-03-19 11:34:57 +01001205#ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
Josh Boyerf2b8dfd2013-03-10 10:33:40 -04001206#ifndef MODULE
1207/* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
1208 * working as well for the module options so we don't break people. We
1209 * need to keep the names identical and the convenient macros will happily
1210 * refuse to let us do that by failing the build with redefinition errors
1211 * of global variables. So we stick them inside a dummy function to avoid
1212 * those conflicts. The options still get parsed, and the redefined
1213 * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
1214 *
1215 * This is hacky. I'm sorry.
1216 */
1217static void __used s8250_options(void)
1218{
1219#undef MODULE_PARAM_PREFIX
Jiri Slaby9196d8a2013-03-19 11:34:56 +01001220#define MODULE_PARAM_PREFIX "8250_core."
Josh Boyerf2b8dfd2013-03-10 10:33:40 -04001221
1222 module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
1223 module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
1224 module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
1225#ifdef CONFIG_SERIAL_8250_RSA
1226 __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
1227 &param_array_ops, .arr = &__param_arr_probe_rsa,
Jani Nikula91f9d332014-08-27 06:22:23 +09301228 0444, -1, 0);
Josh Boyerf2b8dfd2013-03-10 10:33:40 -04001229#endif
1230}
1231#else
Jiri Slaby9196d8a2013-03-19 11:34:56 +01001232MODULE_ALIAS("8250_core");
Josh Boyerf2b8dfd2013-03-10 10:33:40 -04001233#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234#endif