8250_core.c 32 KB

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