8250_core.c 32 KB

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