8250_core.c 32 KB

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