8250_core.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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.type == PORT_8250_CIR)
  471. continue;
  472. if (up->port.dev)
  473. continue;
  474. up->port.dev = dev;
  475. if (skip_txen_test)
  476. up->port.flags |= UPF_NO_TXEN_TEST;
  477. uart_add_one_port(drv, &up->port);
  478. }
  479. }
  480. #ifdef CONFIG_SERIAL_8250_CONSOLE
  481. static void univ8250_console_write(struct console *co, const char *s,
  482. unsigned int count)
  483. {
  484. struct uart_8250_port *up = &serial8250_ports[co->index];
  485. serial8250_console_write(up, s, count);
  486. }
  487. static int univ8250_console_setup(struct console *co, char *options)
  488. {
  489. struct uart_port *port;
  490. /*
  491. * Check whether an invalid uart number has been specified, and
  492. * if so, search for the first available port that does have
  493. * console support.
  494. */
  495. if (co->index >= nr_uarts)
  496. co->index = 0;
  497. port = &serial8250_ports[co->index].port;
  498. /* link port to console */
  499. port->cons = co;
  500. return serial8250_console_setup(port, options, false);
  501. }
  502. /**
  503. * univ8250_console_match - non-standard console matching
  504. * @co: registering console
  505. * @name: name from console command line
  506. * @idx: index from console command line
  507. * @options: ptr to option string from console command line
  508. *
  509. * Only attempts to match console command lines of the form:
  510. * console=uart[8250],io|mmio|mmio32,<addr>[,<options>]
  511. * console=uart[8250],0x<addr>[,<options>]
  512. * This form is used to register an initial earlycon boot console and
  513. * replace it with the serial8250_console at 8250 driver init.
  514. *
  515. * Performs console setup for a match (as required by interface)
  516. * If no <options> are specified, then assume the h/w is already setup.
  517. *
  518. * Returns 0 if console matches; otherwise non-zero to use default matching
  519. */
  520. static int univ8250_console_match(struct console *co, char *name, int idx,
  521. char *options)
  522. {
  523. char match[] = "uart"; /* 8250-specific earlycon name */
  524. unsigned char iotype;
  525. unsigned long addr;
  526. int i;
  527. if (strncmp(name, match, 4) != 0)
  528. return -ENODEV;
  529. if (uart_parse_earlycon(options, &iotype, &addr, &options))
  530. return -ENODEV;
  531. /* try to match the port specified on the command line */
  532. for (i = 0; i < nr_uarts; i++) {
  533. struct uart_port *port = &serial8250_ports[i].port;
  534. if (port->iotype != iotype)
  535. continue;
  536. if ((iotype == UPIO_MEM || iotype == UPIO_MEM32) &&
  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. /**
  634. * serial8250_resume_port - resume one serial port
  635. * @line: serial line number
  636. *
  637. * Resume one serial port.
  638. */
  639. void serial8250_resume_port(int line)
  640. {
  641. struct uart_8250_port *up = &serial8250_ports[line];
  642. struct uart_port *port = &up->port;
  643. up->canary = 0;
  644. if (up->capabilities & UART_NATSEMI) {
  645. /* Ensure it's still in high speed mode */
  646. serial_port_out(port, UART_LCR, 0xE0);
  647. ns16550a_goto_highspeed(up);
  648. serial_port_out(port, UART_LCR, 0);
  649. port->uartclk = 921600*16;
  650. }
  651. uart_resume_port(&serial8250_reg, port);
  652. }
  653. /*
  654. * Register a set of serial devices attached to a platform device. The
  655. * list is terminated with a zero flags entry, which means we expect
  656. * all entries to have at least UPF_BOOT_AUTOCONF set.
  657. */
  658. static int serial8250_probe(struct platform_device *dev)
  659. {
  660. struct plat_serial8250_port *p = dev_get_platdata(&dev->dev);
  661. struct uart_8250_port uart;
  662. int ret, i, irqflag = 0;
  663. memset(&uart, 0, sizeof(uart));
  664. if (share_irqs)
  665. irqflag = IRQF_SHARED;
  666. for (i = 0; p && p->flags != 0; p++, i++) {
  667. uart.port.iobase = p->iobase;
  668. uart.port.membase = p->membase;
  669. uart.port.irq = p->irq;
  670. uart.port.irqflags = p->irqflags;
  671. uart.port.uartclk = p->uartclk;
  672. uart.port.regshift = p->regshift;
  673. uart.port.iotype = p->iotype;
  674. uart.port.flags = p->flags;
  675. uart.port.mapbase = p->mapbase;
  676. uart.port.hub6 = p->hub6;
  677. uart.port.private_data = p->private_data;
  678. uart.port.type = p->type;
  679. uart.port.serial_in = p->serial_in;
  680. uart.port.serial_out = p->serial_out;
  681. uart.port.handle_irq = p->handle_irq;
  682. uart.port.handle_break = p->handle_break;
  683. uart.port.set_termios = p->set_termios;
  684. uart.port.pm = p->pm;
  685. uart.port.dev = &dev->dev;
  686. uart.port.irqflags |= irqflag;
  687. ret = serial8250_register_8250_port(&uart);
  688. if (ret < 0) {
  689. dev_err(&dev->dev, "unable to register port at index %d "
  690. "(IO%lx MEM%llx IRQ%d): %d\n", i,
  691. p->iobase, (unsigned long long)p->mapbase,
  692. p->irq, ret);
  693. }
  694. }
  695. return 0;
  696. }
  697. /*
  698. * Remove serial ports registered against a platform device.
  699. */
  700. static int serial8250_remove(struct platform_device *dev)
  701. {
  702. int i;
  703. for (i = 0; i < nr_uarts; i++) {
  704. struct uart_8250_port *up = &serial8250_ports[i];
  705. if (up->port.dev == &dev->dev)
  706. serial8250_unregister_port(i);
  707. }
  708. return 0;
  709. }
  710. static int serial8250_suspend(struct platform_device *dev, pm_message_t state)
  711. {
  712. int i;
  713. for (i = 0; i < UART_NR; i++) {
  714. struct uart_8250_port *up = &serial8250_ports[i];
  715. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  716. uart_suspend_port(&serial8250_reg, &up->port);
  717. }
  718. return 0;
  719. }
  720. static int serial8250_resume(struct platform_device *dev)
  721. {
  722. int i;
  723. for (i = 0; i < UART_NR; i++) {
  724. struct uart_8250_port *up = &serial8250_ports[i];
  725. if (up->port.type != PORT_UNKNOWN && up->port.dev == &dev->dev)
  726. serial8250_resume_port(i);
  727. }
  728. return 0;
  729. }
  730. static struct platform_driver serial8250_isa_driver = {
  731. .probe = serial8250_probe,
  732. .remove = serial8250_remove,
  733. .suspend = serial8250_suspend,
  734. .resume = serial8250_resume,
  735. .driver = {
  736. .name = "serial8250",
  737. },
  738. };
  739. /*
  740. * This "device" covers _all_ ISA 8250-compatible serial devices listed
  741. * in the table in include/asm/serial.h
  742. */
  743. static struct platform_device *serial8250_isa_devs;
  744. /*
  745. * serial8250_register_8250_port and serial8250_unregister_port allows for
  746. * 16x50 serial ports to be configured at run-time, to support PCMCIA
  747. * modems and PCI multiport cards.
  748. */
  749. static DEFINE_MUTEX(serial_mutex);
  750. static struct uart_8250_port *serial8250_find_match_or_unused(struct uart_port *port)
  751. {
  752. int i;
  753. /*
  754. * First, find a port entry which matches.
  755. */
  756. for (i = 0; i < nr_uarts; i++)
  757. if (uart_match_port(&serial8250_ports[i].port, port))
  758. return &serial8250_ports[i];
  759. /* try line number first if still available */
  760. i = port->line;
  761. if (i < nr_uarts && serial8250_ports[i].port.type == PORT_UNKNOWN &&
  762. serial8250_ports[i].port.iobase == 0)
  763. return &serial8250_ports[i];
  764. /*
  765. * We didn't find a matching entry, so look for the first
  766. * free entry. We look for one which hasn't been previously
  767. * used (indicated by zero iobase).
  768. */
  769. for (i = 0; i < nr_uarts; i++)
  770. if (serial8250_ports[i].port.type == PORT_UNKNOWN &&
  771. serial8250_ports[i].port.iobase == 0)
  772. return &serial8250_ports[i];
  773. /*
  774. * That also failed. Last resort is to find any entry which
  775. * doesn't have a real port associated with it.
  776. */
  777. for (i = 0; i < nr_uarts; i++)
  778. if (serial8250_ports[i].port.type == PORT_UNKNOWN)
  779. return &serial8250_ports[i];
  780. return NULL;
  781. }
  782. /**
  783. * serial8250_register_8250_port - register a serial port
  784. * @up: serial port template
  785. *
  786. * Configure the serial port specified by the request. If the
  787. * port exists and is in use, it is hung up and unregistered
  788. * first.
  789. *
  790. * The port is then probed and if necessary the IRQ is autodetected
  791. * If this fails an error is returned.
  792. *
  793. * On success the port is ready to use and the line number is returned.
  794. */
  795. int serial8250_register_8250_port(struct uart_8250_port *up)
  796. {
  797. struct uart_8250_port *uart;
  798. int ret = -ENOSPC;
  799. if (up->port.uartclk == 0)
  800. return -EINVAL;
  801. mutex_lock(&serial_mutex);
  802. uart = serial8250_find_match_or_unused(&up->port);
  803. if (uart && uart->port.type != PORT_8250_CIR) {
  804. if (uart->port.dev)
  805. uart_remove_one_port(&serial8250_reg, &uart->port);
  806. uart->port.iobase = up->port.iobase;
  807. uart->port.membase = up->port.membase;
  808. uart->port.irq = up->port.irq;
  809. uart->port.irqflags = up->port.irqflags;
  810. uart->port.uartclk = up->port.uartclk;
  811. uart->port.fifosize = up->port.fifosize;
  812. uart->port.regshift = up->port.regshift;
  813. uart->port.iotype = up->port.iotype;
  814. uart->port.flags = up->port.flags | UPF_BOOT_AUTOCONF;
  815. uart->bugs = up->bugs;
  816. uart->port.mapbase = up->port.mapbase;
  817. uart->port.mapsize = up->port.mapsize;
  818. uart->port.private_data = up->port.private_data;
  819. uart->tx_loadsz = up->tx_loadsz;
  820. uart->capabilities = up->capabilities;
  821. uart->port.throttle = up->port.throttle;
  822. uart->port.unthrottle = up->port.unthrottle;
  823. uart->port.rs485_config = up->port.rs485_config;
  824. uart->port.rs485 = up->port.rs485;
  825. uart->dma = up->dma;
  826. /* Take tx_loadsz from fifosize if it wasn't set separately */
  827. if (uart->port.fifosize && !uart->tx_loadsz)
  828. uart->tx_loadsz = uart->port.fifosize;
  829. if (up->port.dev)
  830. uart->port.dev = up->port.dev;
  831. if (skip_txen_test)
  832. uart->port.flags |= UPF_NO_TXEN_TEST;
  833. if (up->port.flags & UPF_FIXED_TYPE)
  834. uart->port.type = up->port.type;
  835. serial8250_set_defaults(uart);
  836. /* Possibly override default I/O functions. */
  837. if (up->port.serial_in)
  838. uart->port.serial_in = up->port.serial_in;
  839. if (up->port.serial_out)
  840. uart->port.serial_out = up->port.serial_out;
  841. if (up->port.handle_irq)
  842. uart->port.handle_irq = up->port.handle_irq;
  843. /* Possibly override set_termios call */
  844. if (up->port.set_termios)
  845. uart->port.set_termios = up->port.set_termios;
  846. if (up->port.set_mctrl)
  847. uart->port.set_mctrl = up->port.set_mctrl;
  848. if (up->port.startup)
  849. uart->port.startup = up->port.startup;
  850. if (up->port.shutdown)
  851. uart->port.shutdown = up->port.shutdown;
  852. if (up->port.pm)
  853. uart->port.pm = up->port.pm;
  854. if (up->port.handle_break)
  855. uart->port.handle_break = up->port.handle_break;
  856. if (up->dl_read)
  857. uart->dl_read = up->dl_read;
  858. if (up->dl_write)
  859. uart->dl_write = up->dl_write;
  860. if (uart->port.type != PORT_8250_CIR) {
  861. if (serial8250_isa_config != NULL)
  862. serial8250_isa_config(0, &uart->port,
  863. &uart->capabilities);
  864. ret = uart_add_one_port(&serial8250_reg,
  865. &uart->port);
  866. if (ret == 0)
  867. ret = uart->port.line;
  868. } else {
  869. dev_info(uart->port.dev,
  870. "skipping CIR port at 0x%lx / 0x%llx, IRQ %d\n",
  871. uart->port.iobase,
  872. (unsigned long long)uart->port.mapbase,
  873. uart->port.irq);
  874. ret = 0;
  875. }
  876. }
  877. mutex_unlock(&serial_mutex);
  878. return ret;
  879. }
  880. EXPORT_SYMBOL(serial8250_register_8250_port);
  881. /**
  882. * serial8250_unregister_port - remove a 16x50 serial port at runtime
  883. * @line: serial line number
  884. *
  885. * Remove one serial port. This may not be called from interrupt
  886. * context. We hand the port back to the our control.
  887. */
  888. void serial8250_unregister_port(int line)
  889. {
  890. struct uart_8250_port *uart = &serial8250_ports[line];
  891. mutex_lock(&serial_mutex);
  892. uart_remove_one_port(&serial8250_reg, &uart->port);
  893. if (serial8250_isa_devs) {
  894. uart->port.flags &= ~UPF_BOOT_AUTOCONF;
  895. if (skip_txen_test)
  896. uart->port.flags |= UPF_NO_TXEN_TEST;
  897. uart->port.type = PORT_UNKNOWN;
  898. uart->port.dev = &serial8250_isa_devs->dev;
  899. uart->capabilities = 0;
  900. uart_add_one_port(&serial8250_reg, &uart->port);
  901. } else {
  902. uart->port.dev = NULL;
  903. }
  904. mutex_unlock(&serial_mutex);
  905. }
  906. EXPORT_SYMBOL(serial8250_unregister_port);
  907. static int __init serial8250_init(void)
  908. {
  909. int ret;
  910. if (nr_uarts == 0)
  911. return -ENODEV;
  912. serial8250_isa_init_ports();
  913. printk(KERN_INFO "Serial: 8250/16550 driver, "
  914. "%d ports, IRQ sharing %sabled\n", nr_uarts,
  915. share_irqs ? "en" : "dis");
  916. #ifdef CONFIG_SPARC
  917. ret = sunserial_register_minors(&serial8250_reg, UART_NR);
  918. #else
  919. serial8250_reg.nr = UART_NR;
  920. ret = uart_register_driver(&serial8250_reg);
  921. #endif
  922. if (ret)
  923. goto out;
  924. ret = serial8250_pnp_init();
  925. if (ret)
  926. goto unreg_uart_drv;
  927. serial8250_isa_devs = platform_device_alloc("serial8250",
  928. PLAT8250_DEV_LEGACY);
  929. if (!serial8250_isa_devs) {
  930. ret = -ENOMEM;
  931. goto unreg_pnp;
  932. }
  933. ret = platform_device_add(serial8250_isa_devs);
  934. if (ret)
  935. goto put_dev;
  936. serial8250_register_ports(&serial8250_reg, &serial8250_isa_devs->dev);
  937. ret = platform_driver_register(&serial8250_isa_driver);
  938. if (ret == 0)
  939. goto out;
  940. platform_device_del(serial8250_isa_devs);
  941. put_dev:
  942. platform_device_put(serial8250_isa_devs);
  943. unreg_pnp:
  944. serial8250_pnp_exit();
  945. unreg_uart_drv:
  946. #ifdef CONFIG_SPARC
  947. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  948. #else
  949. uart_unregister_driver(&serial8250_reg);
  950. #endif
  951. out:
  952. return ret;
  953. }
  954. static void __exit serial8250_exit(void)
  955. {
  956. struct platform_device *isa_dev = serial8250_isa_devs;
  957. /*
  958. * This tells serial8250_unregister_port() not to re-register
  959. * the ports (thereby making serial8250_isa_driver permanently
  960. * in use.)
  961. */
  962. serial8250_isa_devs = NULL;
  963. platform_driver_unregister(&serial8250_isa_driver);
  964. platform_device_unregister(isa_dev);
  965. serial8250_pnp_exit();
  966. #ifdef CONFIG_SPARC
  967. sunserial_unregister_minors(&serial8250_reg, UART_NR);
  968. #else
  969. uart_unregister_driver(&serial8250_reg);
  970. #endif
  971. }
  972. module_init(serial8250_init);
  973. module_exit(serial8250_exit);
  974. EXPORT_SYMBOL(serial8250_suspend_port);
  975. EXPORT_SYMBOL(serial8250_resume_port);
  976. MODULE_LICENSE("GPL");
  977. MODULE_DESCRIPTION("Generic 8250/16x50 serial driver");
  978. module_param(share_irqs, uint, 0644);
  979. MODULE_PARM_DESC(share_irqs, "Share IRQs with other non-8250/16x50 devices"
  980. " (unsafe)");
  981. module_param(nr_uarts, uint, 0644);
  982. MODULE_PARM_DESC(nr_uarts, "Maximum number of UARTs supported. (1-" __MODULE_STRING(CONFIG_SERIAL_8250_NR_UARTS) ")");
  983. module_param(skip_txen_test, uint, 0644);
  984. MODULE_PARM_DESC(skip_txen_test, "Skip checking for the TXEN bug at init time");
  985. #ifdef CONFIG_SERIAL_8250_RSA
  986. module_param_array(probe_rsa, ulong, &probe_rsa_count, 0444);
  987. MODULE_PARM_DESC(probe_rsa, "Probe I/O ports for RSA");
  988. #endif
  989. MODULE_ALIAS_CHARDEV_MAJOR(TTY_MAJOR);
  990. #ifdef CONFIG_SERIAL_8250_DEPRECATED_OPTIONS
  991. #ifndef MODULE
  992. /* This module was renamed to 8250_core in 3.7. Keep the old "8250" name
  993. * working as well for the module options so we don't break people. We
  994. * need to keep the names identical and the convenient macros will happily
  995. * refuse to let us do that by failing the build with redefinition errors
  996. * of global variables. So we stick them inside a dummy function to avoid
  997. * those conflicts. The options still get parsed, and the redefined
  998. * MODULE_PARAM_PREFIX lets us keep the "8250." syntax alive.
  999. *
  1000. * This is hacky. I'm sorry.
  1001. */
  1002. static void __used s8250_options(void)
  1003. {
  1004. #undef MODULE_PARAM_PREFIX
  1005. #define MODULE_PARAM_PREFIX "8250_core."
  1006. module_param_cb(share_irqs, &param_ops_uint, &share_irqs, 0644);
  1007. module_param_cb(nr_uarts, &param_ops_uint, &nr_uarts, 0644);
  1008. module_param_cb(skip_txen_test, &param_ops_uint, &skip_txen_test, 0644);
  1009. #ifdef CONFIG_SERIAL_8250_RSA
  1010. __module_param_call(MODULE_PARAM_PREFIX, probe_rsa,
  1011. &param_array_ops, .arr = &__param_arr_probe_rsa,
  1012. 0444, -1, 0);
  1013. #endif
  1014. }
  1015. #else
  1016. MODULE_ALIAS("8250_core");
  1017. #endif
  1018. #endif