8250_core.c 31 KB

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