au1x00_uart.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. /*
  2. * 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. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * A note about mapbase / membase
  14. *
  15. * mapbase is the physical address of the IO port. Currently, we don't
  16. * support this very well, and it may well be dropped from this driver
  17. * in future. As such, mapbase should be NULL.
  18. *
  19. * membase is an 'ioremapped' cookie. This is compatible with the old
  20. * serial.c driver, and is currently the preferred form.
  21. */
  22. #include <linux/config.h>
  23. #include <linux/module.h>
  24. #include <linux/tty.h>
  25. #include <linux/ioport.h>
  26. #include <linux/init.h>
  27. #include <linux/console.h>
  28. #include <linux/sysrq.h>
  29. #include <linux/serial.h>
  30. #include <linux/serialP.h>
  31. #include <linux/delay.h>
  32. #include <asm/serial.h>
  33. #include <asm/io.h>
  34. #include <asm/irq.h>
  35. #include <asm/mach-au1x00/au1000.h>
  36. #if defined(CONFIG_SERIAL_AU1X00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  37. #define SUPPORT_SYSRQ
  38. #endif
  39. #include <linux/serial_core.h>
  40. #include "8250.h"
  41. /*
  42. * Debugging.
  43. */
  44. #if 0
  45. #define DEBUG_AUTOCONF(fmt...) printk(fmt)
  46. #else
  47. #define DEBUG_AUTOCONF(fmt...) do { } while (0)
  48. #endif
  49. #if 0
  50. #define DEBUG_INTR(fmt...) printk(fmt)
  51. #else
  52. #define DEBUG_INTR(fmt...) do { } while (0)
  53. #endif
  54. #define PASS_LIMIT 256
  55. /*
  56. * We default to IRQ0 for the "no irq" hack. Some
  57. * machine types want others as well - they're free
  58. * to redefine this in their header file.
  59. */
  60. #define is_real_interrupt(irq) ((irq) != 0)
  61. static struct old_serial_port old_serial_port[] = {
  62. { .baud_base = 0,
  63. .iomem_base = (u8 *)UART0_ADDR,
  64. .irq = AU1000_UART0_INT,
  65. .flags = STD_COM_FLAGS,
  66. .iomem_reg_shift = 2,
  67. }, {
  68. .baud_base = 0,
  69. .iomem_base = (u8 *)UART1_ADDR,
  70. .irq = AU1000_UART1_INT,
  71. .flags = STD_COM_FLAGS,
  72. .iomem_reg_shift = 2
  73. }, {
  74. .baud_base = 0,
  75. .iomem_base = (u8 *)UART2_ADDR,
  76. .irq = AU1000_UART2_INT,
  77. .flags = STD_COM_FLAGS,
  78. .iomem_reg_shift = 2
  79. }, {
  80. .baud_base = 0,
  81. .iomem_base = (u8 *)UART3_ADDR,
  82. .irq = AU1000_UART3_INT,
  83. .flags = STD_COM_FLAGS,
  84. .iomem_reg_shift = 2
  85. }
  86. };
  87. #define UART_NR ARRAY_SIZE(old_serial_port)
  88. struct uart_8250_port {
  89. struct uart_port port;
  90. struct timer_list timer; /* "no irq" timer */
  91. struct list_head list; /* ports on this IRQ */
  92. unsigned short rev;
  93. unsigned char acr;
  94. unsigned char ier;
  95. unsigned char lcr;
  96. unsigned char mcr_mask; /* mask of user bits */
  97. unsigned char mcr_force; /* mask of forced bits */
  98. unsigned char lsr_break_flag;
  99. /*
  100. * We provide a per-port pm hook.
  101. */
  102. void (*pm)(struct uart_port *port,
  103. unsigned int state, unsigned int old);
  104. };
  105. struct irq_info {
  106. spinlock_t lock;
  107. struct list_head *head;
  108. };
  109. static struct irq_info irq_lists[NR_IRQS];
  110. /*
  111. * Here we define the default xmit fifo size used for each type of UART.
  112. */
  113. static const struct serial_uart_config uart_config[PORT_MAX_8250+1] = {
  114. { "unknown", 1, 0 },
  115. { "8250", 1, 0 },
  116. { "16450", 1, 0 },
  117. { "16550", 1, 0 },
  118. /* PORT_16550A */
  119. { "AU1X00_UART",16, UART_CLEAR_FIFO | UART_USE_FIFO },
  120. };
  121. static _INLINE_ unsigned int serial_in(struct uart_8250_port *up, int offset)
  122. {
  123. return au_readl((unsigned long)up->port.membase + offset);
  124. }
  125. static _INLINE_ void
  126. serial_out(struct uart_8250_port *up, int offset, int value)
  127. {
  128. au_writel(value, (unsigned long)up->port.membase + offset);
  129. }
  130. #define serial_inp(up, offset) serial_in(up, offset)
  131. #define serial_outp(up, offset, value) serial_out(up, offset, value)
  132. /*
  133. * This routine is called by rs_init() to initialize a specific serial
  134. * port. It determines what type of UART chip this serial port is
  135. * using: 8250, 16450, 16550, 16550A. The important question is
  136. * whether or not this UART is a 16550A or not, since this will
  137. * determine whether or not we can use its FIFO features or not.
  138. */
  139. static void autoconfig(struct uart_8250_port *up, unsigned int probeflags)
  140. {
  141. unsigned char save_lcr, save_mcr;
  142. unsigned long flags;
  143. if (!up->port.iobase && !up->port.mapbase && !up->port.membase)
  144. return;
  145. DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%08lx): ",
  146. up->port.line, up->port.iobase, up->port.membase);
  147. /*
  148. * We really do need global IRQs disabled here - we're going to
  149. * be frobbing the chips IRQ enable register to see if it exists.
  150. */
  151. spin_lock_irqsave(&up->port.lock, flags);
  152. // save_flags(flags); cli();
  153. save_mcr = serial_in(up, UART_MCR);
  154. save_lcr = serial_in(up, UART_LCR);
  155. up->port.type = PORT_16550A;
  156. serial_outp(up, UART_LCR, save_lcr);
  157. up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;
  158. if (up->port.type == PORT_UNKNOWN)
  159. goto out;
  160. /*
  161. * Reset the UART.
  162. */
  163. serial_outp(up, UART_MCR, save_mcr);
  164. serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |
  165. UART_FCR_CLEAR_RCVR |
  166. UART_FCR_CLEAR_XMIT));
  167. serial_outp(up, UART_FCR, 0);
  168. (void)serial_in(up, UART_RX);
  169. serial_outp(up, UART_IER, 0);
  170. out:
  171. spin_unlock_irqrestore(&up->port.lock, flags);
  172. // restore_flags(flags);
  173. DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);
  174. }
  175. static void serial8250_stop_tx(struct uart_port *port)
  176. {
  177. struct uart_8250_port *up = (struct uart_8250_port *)port;
  178. if (up->ier & UART_IER_THRI) {
  179. up->ier &= ~UART_IER_THRI;
  180. serial_out(up, UART_IER, up->ier);
  181. }
  182. }
  183. static void serial8250_start_tx(struct uart_port *port)
  184. {
  185. struct uart_8250_port *up = (struct uart_8250_port *)port;
  186. if (!(up->ier & UART_IER_THRI)) {
  187. up->ier |= UART_IER_THRI;
  188. serial_out(up, UART_IER, up->ier);
  189. }
  190. }
  191. static void serial8250_stop_rx(struct uart_port *port)
  192. {
  193. struct uart_8250_port *up = (struct uart_8250_port *)port;
  194. up->ier &= ~UART_IER_RLSI;
  195. up->port.read_status_mask &= ~UART_LSR_DR;
  196. serial_out(up, UART_IER, up->ier);
  197. }
  198. static void serial8250_enable_ms(struct uart_port *port)
  199. {
  200. struct uart_8250_port *up = (struct uart_8250_port *)port;
  201. up->ier |= UART_IER_MSI;
  202. serial_out(up, UART_IER, up->ier);
  203. }
  204. static _INLINE_ void
  205. receive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs)
  206. {
  207. struct tty_struct *tty = up->port.info->tty;
  208. unsigned char ch;
  209. int max_count = 256;
  210. do {
  211. if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {
  212. tty->flip.work.func((void *)tty);
  213. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  214. return; // if TTY_DONT_FLIP is set
  215. }
  216. ch = serial_inp(up, UART_RX);
  217. *tty->flip.char_buf_ptr = ch;
  218. *tty->flip.flag_buf_ptr = TTY_NORMAL;
  219. up->port.icount.rx++;
  220. if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |
  221. UART_LSR_FE | UART_LSR_OE))) {
  222. /*
  223. * For statistics only
  224. */
  225. if (*status & UART_LSR_BI) {
  226. *status &= ~(UART_LSR_FE | UART_LSR_PE);
  227. up->port.icount.brk++;
  228. /*
  229. * We do the SysRQ and SAK checking
  230. * here because otherwise the break
  231. * may get masked by ignore_status_mask
  232. * or read_status_mask.
  233. */
  234. if (uart_handle_break(&up->port))
  235. goto ignore_char;
  236. } else if (*status & UART_LSR_PE)
  237. up->port.icount.parity++;
  238. else if (*status & UART_LSR_FE)
  239. up->port.icount.frame++;
  240. if (*status & UART_LSR_OE)
  241. up->port.icount.overrun++;
  242. /*
  243. * Mask off conditions which should be ingored.
  244. */
  245. *status &= up->port.read_status_mask;
  246. #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
  247. if (up->port.line == up->port.cons->index) {
  248. /* Recover the break flag from console xmit */
  249. *status |= up->lsr_break_flag;
  250. up->lsr_break_flag = 0;
  251. }
  252. #endif
  253. if (*status & UART_LSR_BI) {
  254. DEBUG_INTR("handling break....");
  255. *tty->flip.flag_buf_ptr = TTY_BREAK;
  256. } else if (*status & UART_LSR_PE)
  257. *tty->flip.flag_buf_ptr = TTY_PARITY;
  258. else if (*status & UART_LSR_FE)
  259. *tty->flip.flag_buf_ptr = TTY_FRAME;
  260. }
  261. if (uart_handle_sysrq_char(&up->port, ch, regs))
  262. goto ignore_char;
  263. if ((*status & up->port.ignore_status_mask) == 0) {
  264. tty->flip.flag_buf_ptr++;
  265. tty->flip.char_buf_ptr++;
  266. tty->flip.count++;
  267. }
  268. if ((*status & UART_LSR_OE) &&
  269. tty->flip.count < TTY_FLIPBUF_SIZE) {
  270. /*
  271. * Overrun is special, since it's reported
  272. * immediately, and doesn't affect the current
  273. * character.
  274. */
  275. *tty->flip.flag_buf_ptr = TTY_OVERRUN;
  276. tty->flip.flag_buf_ptr++;
  277. tty->flip.char_buf_ptr++;
  278. tty->flip.count++;
  279. }
  280. ignore_char:
  281. *status = serial_inp(up, UART_LSR);
  282. } while ((*status & UART_LSR_DR) && (max_count-- > 0));
  283. spin_unlock(&up->port.lock);
  284. tty_flip_buffer_push(tty);
  285. spin_lock(&up->port.lock);
  286. }
  287. static _INLINE_ void transmit_chars(struct uart_8250_port *up)
  288. {
  289. struct circ_buf *xmit = &up->port.info->xmit;
  290. int count;
  291. if (up->port.x_char) {
  292. serial_outp(up, UART_TX, up->port.x_char);
  293. up->port.icount.tx++;
  294. up->port.x_char = 0;
  295. return;
  296. }
  297. if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
  298. serial8250_stop_tx(&up->port);
  299. return;
  300. }
  301. count = up->port.fifosize;
  302. do {
  303. serial_out(up, UART_TX, xmit->buf[xmit->tail]);
  304. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  305. up->port.icount.tx++;
  306. if (uart_circ_empty(xmit))
  307. break;
  308. } while (--count > 0);
  309. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  310. uart_write_wakeup(&up->port);
  311. DEBUG_INTR("THRE...");
  312. if (uart_circ_empty(xmit))
  313. serial8250_stop_tx(&up->port);
  314. }
  315. static _INLINE_ void check_modem_status(struct uart_8250_port *up)
  316. {
  317. int status;
  318. status = serial_in(up, UART_MSR);
  319. if ((status & UART_MSR_ANY_DELTA) == 0)
  320. return;
  321. if (status & UART_MSR_TERI)
  322. up->port.icount.rng++;
  323. if (status & UART_MSR_DDSR)
  324. up->port.icount.dsr++;
  325. if (status & UART_MSR_DDCD)
  326. uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
  327. if (status & UART_MSR_DCTS)
  328. uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
  329. wake_up_interruptible(&up->port.info->delta_msr_wait);
  330. }
  331. /*
  332. * This handles the interrupt from one port.
  333. */
  334. static inline void
  335. serial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs)
  336. {
  337. unsigned int status = serial_inp(up, UART_LSR);
  338. DEBUG_INTR("status = %x...", status);
  339. if (status & UART_LSR_DR)
  340. receive_chars(up, &status, regs);
  341. check_modem_status(up);
  342. if (status & UART_LSR_THRE)
  343. transmit_chars(up);
  344. }
  345. /*
  346. * This is the serial driver's interrupt routine.
  347. *
  348. * Arjan thinks the old way was overly complex, so it got simplified.
  349. * Alan disagrees, saying that need the complexity to handle the weird
  350. * nature of ISA shared interrupts. (This is a special exception.)
  351. *
  352. * In order to handle ISA shared interrupts properly, we need to check
  353. * that all ports have been serviced, and therefore the ISA interrupt
  354. * line has been de-asserted.
  355. *
  356. * This means we need to loop through all ports. checking that they
  357. * don't have an interrupt pending.
  358. */
  359. static irqreturn_t serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  360. {
  361. struct irq_info *i = dev_id;
  362. struct list_head *l, *end = NULL;
  363. int pass_counter = 0;
  364. DEBUG_INTR("serial8250_interrupt(%d)...", irq);
  365. spin_lock(&i->lock);
  366. l = i->head;
  367. do {
  368. struct uart_8250_port *up;
  369. unsigned int iir;
  370. up = list_entry(l, struct uart_8250_port, list);
  371. iir = serial_in(up, UART_IIR);
  372. if (!(iir & UART_IIR_NO_INT)) {
  373. spin_lock(&up->port.lock);
  374. serial8250_handle_port(up, regs);
  375. spin_unlock(&up->port.lock);
  376. end = NULL;
  377. } else if (end == NULL)
  378. end = l;
  379. l = l->next;
  380. if (l == i->head && pass_counter++ > PASS_LIMIT) {
  381. /* If we hit this, we're dead. */
  382. printk(KERN_ERR "serial8250: too much work for "
  383. "irq%d\n", irq);
  384. break;
  385. }
  386. } while (l != end);
  387. spin_unlock(&i->lock);
  388. DEBUG_INTR("end.\n");
  389. /* FIXME! Was it really ours? */
  390. return IRQ_HANDLED;
  391. }
  392. /*
  393. * To support ISA shared interrupts, we need to have one interrupt
  394. * handler that ensures that the IRQ line has been deasserted
  395. * before returning. Failing to do this will result in the IRQ
  396. * line being stuck active, and, since ISA irqs are edge triggered,
  397. * no more IRQs will be seen.
  398. */
  399. static void serial_do_unlink(struct irq_info *i, struct uart_8250_port *up)
  400. {
  401. spin_lock_irq(&i->lock);
  402. if (!list_empty(i->head)) {
  403. if (i->head == &up->list)
  404. i->head = i->head->next;
  405. list_del(&up->list);
  406. } else {
  407. BUG_ON(i->head != &up->list);
  408. i->head = NULL;
  409. }
  410. spin_unlock_irq(&i->lock);
  411. }
  412. static int serial_link_irq_chain(struct uart_8250_port *up)
  413. {
  414. struct irq_info *i = irq_lists + up->port.irq;
  415. int ret, irq_flags = up->port.flags & UPF_SHARE_IRQ ? SA_SHIRQ : 0;
  416. spin_lock_irq(&i->lock);
  417. if (i->head) {
  418. list_add(&up->list, i->head);
  419. spin_unlock_irq(&i->lock);
  420. ret = 0;
  421. } else {
  422. INIT_LIST_HEAD(&up->list);
  423. i->head = &up->list;
  424. spin_unlock_irq(&i->lock);
  425. ret = request_irq(up->port.irq, serial8250_interrupt,
  426. irq_flags, "serial", i);
  427. if (ret < 0)
  428. serial_do_unlink(i, up);
  429. }
  430. return ret;
  431. }
  432. static void serial_unlink_irq_chain(struct uart_8250_port *up)
  433. {
  434. struct irq_info *i = irq_lists + up->port.irq;
  435. BUG_ON(i->head == NULL);
  436. if (list_empty(i->head))
  437. free_irq(up->port.irq, i);
  438. serial_do_unlink(i, up);
  439. }
  440. /*
  441. * This function is used to handle ports that do not have an
  442. * interrupt. This doesn't work very well for 16450's, but gives
  443. * barely passable results for a 16550A. (Although at the expense
  444. * of much CPU overhead).
  445. */
  446. static void serial8250_timeout(unsigned long data)
  447. {
  448. struct uart_8250_port *up = (struct uart_8250_port *)data;
  449. unsigned int timeout;
  450. unsigned int iir;
  451. iir = serial_in(up, UART_IIR);
  452. if (!(iir & UART_IIR_NO_INT)) {
  453. spin_lock(&up->port.lock);
  454. serial8250_handle_port(up, NULL);
  455. spin_unlock(&up->port.lock);
  456. }
  457. timeout = up->port.timeout;
  458. timeout = timeout > 6 ? (timeout / 2 - 2) : 1;
  459. mod_timer(&up->timer, jiffies + timeout);
  460. }
  461. static unsigned int serial8250_tx_empty(struct uart_port *port)
  462. {
  463. struct uart_8250_port *up = (struct uart_8250_port *)port;
  464. unsigned long flags;
  465. unsigned int ret;
  466. spin_lock_irqsave(&up->port.lock, flags);
  467. ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
  468. spin_unlock_irqrestore(&up->port.lock, flags);
  469. return ret;
  470. }
  471. static unsigned int serial8250_get_mctrl(struct uart_port *port)
  472. {
  473. struct uart_8250_port *up = (struct uart_8250_port *)port;
  474. unsigned char status;
  475. unsigned int ret;
  476. status = serial_in(up, UART_MSR);
  477. ret = 0;
  478. if (status & UART_MSR_DCD)
  479. ret |= TIOCM_CAR;
  480. if (status & UART_MSR_RI)
  481. ret |= TIOCM_RNG;
  482. if (status & UART_MSR_DSR)
  483. ret |= TIOCM_DSR;
  484. if (status & UART_MSR_CTS)
  485. ret |= TIOCM_CTS;
  486. return ret;
  487. }
  488. static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  489. {
  490. struct uart_8250_port *up = (struct uart_8250_port *)port;
  491. unsigned char mcr = 0;
  492. if (mctrl & TIOCM_RTS)
  493. mcr |= UART_MCR_RTS;
  494. if (mctrl & TIOCM_DTR)
  495. mcr |= UART_MCR_DTR;
  496. if (mctrl & TIOCM_OUT1)
  497. mcr |= UART_MCR_OUT1;
  498. if (mctrl & TIOCM_OUT2)
  499. mcr |= UART_MCR_OUT2;
  500. if (mctrl & TIOCM_LOOP)
  501. mcr |= UART_MCR_LOOP;
  502. mcr = (mcr & up->mcr_mask) | up->mcr_force;
  503. serial_out(up, UART_MCR, mcr);
  504. }
  505. static void serial8250_break_ctl(struct uart_port *port, int break_state)
  506. {
  507. struct uart_8250_port *up = (struct uart_8250_port *)port;
  508. unsigned long flags;
  509. spin_lock_irqsave(&up->port.lock, flags);
  510. if (break_state == -1)
  511. up->lcr |= UART_LCR_SBC;
  512. else
  513. up->lcr &= ~UART_LCR_SBC;
  514. serial_out(up, UART_LCR, up->lcr);
  515. spin_unlock_irqrestore(&up->port.lock, flags);
  516. }
  517. static int serial8250_startup(struct uart_port *port)
  518. {
  519. struct uart_8250_port *up = (struct uart_8250_port *)port;
  520. unsigned long flags;
  521. int retval;
  522. /*
  523. * Clear the FIFO buffers and disable them.
  524. * (they will be reeanbled in set_termios())
  525. */
  526. if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
  527. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  528. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  529. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  530. serial_outp(up, UART_FCR, 0);
  531. }
  532. /*
  533. * Clear the interrupt registers.
  534. */
  535. (void) serial_inp(up, UART_LSR);
  536. (void) serial_inp(up, UART_RX);
  537. (void) serial_inp(up, UART_IIR);
  538. (void) serial_inp(up, UART_MSR);
  539. /*
  540. * At this point, there's no way the LSR could still be 0xff;
  541. * if it is, then bail out, because there's likely no UART
  542. * here.
  543. */
  544. if (!(up->port.flags & UPF_BUGGY_UART) &&
  545. (serial_inp(up, UART_LSR) == 0xff)) {
  546. printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
  547. return -ENODEV;
  548. }
  549. retval = serial_link_irq_chain(up);
  550. if (retval)
  551. return retval;
  552. /*
  553. * Now, initialize the UART
  554. */
  555. serial_outp(up, UART_LCR, UART_LCR_WLEN8);
  556. spin_lock_irqsave(&up->port.lock, flags);
  557. if (up->port.flags & UPF_FOURPORT) {
  558. if (!is_real_interrupt(up->port.irq))
  559. up->port.mctrl |= TIOCM_OUT1;
  560. } else
  561. /*
  562. * Most PC uarts need OUT2 raised to enable interrupts.
  563. */
  564. if (is_real_interrupt(up->port.irq))
  565. up->port.mctrl |= TIOCM_OUT2;
  566. serial8250_set_mctrl(&up->port, up->port.mctrl);
  567. spin_unlock_irqrestore(&up->port.lock, flags);
  568. /*
  569. * Finally, enable interrupts. Note: Modem status interrupts
  570. * are set via set_termios(), which will be occurring imminently
  571. * anyway, so we don't enable them here.
  572. */
  573. up->ier = UART_IER_RLSI | UART_IER_RDI;
  574. serial_outp(up, UART_IER, up->ier);
  575. if (up->port.flags & UPF_FOURPORT) {
  576. unsigned int icp;
  577. /*
  578. * Enable interrupts on the AST Fourport board
  579. */
  580. icp = (up->port.iobase & 0xfe0) | 0x01f;
  581. outb_p(0x80, icp);
  582. (void) inb_p(icp);
  583. }
  584. /*
  585. * And clear the interrupt registers again for luck.
  586. */
  587. (void) serial_inp(up, UART_LSR);
  588. (void) serial_inp(up, UART_RX);
  589. (void) serial_inp(up, UART_IIR);
  590. (void) serial_inp(up, UART_MSR);
  591. return 0;
  592. }
  593. static void serial8250_shutdown(struct uart_port *port)
  594. {
  595. struct uart_8250_port *up = (struct uart_8250_port *)port;
  596. unsigned long flags;
  597. /*
  598. * Disable interrupts from this port
  599. */
  600. up->ier = 0;
  601. serial_outp(up, UART_IER, 0);
  602. spin_lock_irqsave(&up->port.lock, flags);
  603. if (up->port.flags & UPF_FOURPORT) {
  604. /* reset interrupts on the AST Fourport board */
  605. inb((up->port.iobase & 0xfe0) | 0x1f);
  606. up->port.mctrl |= TIOCM_OUT1;
  607. } else
  608. up->port.mctrl &= ~TIOCM_OUT2;
  609. serial8250_set_mctrl(&up->port, up->port.mctrl);
  610. spin_unlock_irqrestore(&up->port.lock, flags);
  611. /*
  612. * Disable break condition and FIFOs
  613. */
  614. serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
  615. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  616. UART_FCR_CLEAR_RCVR |
  617. UART_FCR_CLEAR_XMIT);
  618. serial_outp(up, UART_FCR, 0);
  619. /*
  620. * Read data port to reset things, and then unlink from
  621. * the IRQ chain.
  622. */
  623. (void) serial_in(up, UART_RX);
  624. if (!is_real_interrupt(up->port.irq))
  625. del_timer_sync(&up->timer);
  626. else
  627. serial_unlink_irq_chain(up);
  628. }
  629. static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
  630. {
  631. unsigned int quot;
  632. /*
  633. * Handle magic divisors for baud rates above baud_base on
  634. * SMSC SuperIO chips.
  635. */
  636. if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  637. baud == (port->uartclk/4))
  638. quot = 0x8001;
  639. else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  640. baud == (port->uartclk/8))
  641. quot = 0x8002;
  642. else
  643. quot = uart_get_divisor(port, baud);
  644. return quot;
  645. }
  646. static void
  647. serial8250_set_termios(struct uart_port *port, struct termios *termios,
  648. struct termios *old)
  649. {
  650. struct uart_8250_port *up = (struct uart_8250_port *)port;
  651. unsigned char cval, fcr = 0;
  652. unsigned long flags;
  653. unsigned int baud, quot;
  654. switch (termios->c_cflag & CSIZE) {
  655. case CS5:
  656. cval = UART_LCR_WLEN5;
  657. break;
  658. case CS6:
  659. cval = UART_LCR_WLEN6;
  660. break;
  661. case CS7:
  662. cval = UART_LCR_WLEN7;
  663. break;
  664. default:
  665. case CS8:
  666. cval = UART_LCR_WLEN8;
  667. break;
  668. }
  669. if (termios->c_cflag & CSTOPB)
  670. cval |= UART_LCR_STOP;
  671. if (termios->c_cflag & PARENB)
  672. cval |= UART_LCR_PARITY;
  673. if (!(termios->c_cflag & PARODD))
  674. cval |= UART_LCR_EPAR;
  675. #ifdef CMSPAR
  676. if (termios->c_cflag & CMSPAR)
  677. cval |= UART_LCR_SPAR;
  678. #endif
  679. /*
  680. * Ask the core to calculate the divisor for us.
  681. */
  682. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  683. quot = serial8250_get_divisor(port, baud);
  684. quot = 0x35; /* FIXME */
  685. /*
  686. * Work around a bug in the Oxford Semiconductor 952 rev B
  687. * chip which causes it to seriously miscalculate baud rates
  688. * when DLL is 0.
  689. */
  690. if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
  691. up->rev == 0x5201)
  692. quot ++;
  693. if (uart_config[up->port.type].flags & UART_USE_FIFO) {
  694. if (baud < 2400)
  695. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
  696. else
  697. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
  698. }
  699. /*
  700. * Ok, we're now changing the port state. Do it with
  701. * interrupts disabled.
  702. */
  703. spin_lock_irqsave(&up->port.lock, flags);
  704. /*
  705. * Update the per-port timeout.
  706. */
  707. uart_update_timeout(port, termios->c_cflag, baud);
  708. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  709. if (termios->c_iflag & INPCK)
  710. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  711. if (termios->c_iflag & (BRKINT | PARMRK))
  712. up->port.read_status_mask |= UART_LSR_BI;
  713. /*
  714. * Characteres to ignore
  715. */
  716. up->port.ignore_status_mask = 0;
  717. if (termios->c_iflag & IGNPAR)
  718. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  719. if (termios->c_iflag & IGNBRK) {
  720. up->port.ignore_status_mask |= UART_LSR_BI;
  721. /*
  722. * If we're ignoring parity and break indicators,
  723. * ignore overruns too (for real raw support).
  724. */
  725. if (termios->c_iflag & IGNPAR)
  726. up->port.ignore_status_mask |= UART_LSR_OE;
  727. }
  728. /*
  729. * ignore all characters if CREAD is not set
  730. */
  731. if ((termios->c_cflag & CREAD) == 0)
  732. up->port.ignore_status_mask |= UART_LSR_DR;
  733. /*
  734. * CTS flow control flag and modem status interrupts
  735. */
  736. up->ier &= ~UART_IER_MSI;
  737. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  738. up->ier |= UART_IER_MSI;
  739. serial_out(up, UART_IER, up->ier);
  740. serial_outp(up, 0x28, quot & 0xffff);
  741. up->lcr = cval; /* Save LCR */
  742. if (up->port.type != PORT_16750) {
  743. if (fcr & UART_FCR_ENABLE_FIFO) {
  744. /* emulated UARTs (Lucent Venus 167x) need two steps */
  745. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  746. }
  747. serial_outp(up, UART_FCR, fcr); /* set fcr */
  748. }
  749. spin_unlock_irqrestore(&up->port.lock, flags);
  750. }
  751. static void
  752. serial8250_pm(struct uart_port *port, unsigned int state,
  753. unsigned int oldstate)
  754. {
  755. struct uart_8250_port *up = (struct uart_8250_port *)port;
  756. if (state) {
  757. /* sleep */
  758. if (up->pm)
  759. up->pm(port, state, oldstate);
  760. } else {
  761. /* wake */
  762. if (up->pm)
  763. up->pm(port, state, oldstate);
  764. }
  765. }
  766. /*
  767. * Resource handling. This is complicated by the fact that resources
  768. * depend on the port type. Maybe we should be claiming the standard
  769. * 8250 ports, and then trying to get other resources as necessary?
  770. */
  771. static int
  772. serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
  773. {
  774. unsigned int size = 8 << up->port.regshift;
  775. int ret = 0;
  776. switch (up->port.iotype) {
  777. case SERIAL_IO_MEM:
  778. if (up->port.mapbase) {
  779. *res = request_mem_region(up->port.mapbase, size, "serial");
  780. if (!*res)
  781. ret = -EBUSY;
  782. }
  783. break;
  784. case SERIAL_IO_HUB6:
  785. case SERIAL_IO_PORT:
  786. *res = request_region(up->port.iobase, size, "serial");
  787. if (!*res)
  788. ret = -EBUSY;
  789. break;
  790. }
  791. return ret;
  792. }
  793. static void serial8250_release_port(struct uart_port *port)
  794. {
  795. struct uart_8250_port *up = (struct uart_8250_port *)port;
  796. unsigned long start, offset = 0, size = 0;
  797. size <<= up->port.regshift;
  798. switch (up->port.iotype) {
  799. case SERIAL_IO_MEM:
  800. if (up->port.mapbase) {
  801. /*
  802. * Unmap the area.
  803. */
  804. iounmap(up->port.membase);
  805. up->port.membase = NULL;
  806. start = up->port.mapbase;
  807. if (size)
  808. release_mem_region(start + offset, size);
  809. release_mem_region(start, 8 << up->port.regshift);
  810. }
  811. break;
  812. case SERIAL_IO_HUB6:
  813. case SERIAL_IO_PORT:
  814. start = up->port.iobase;
  815. if (size)
  816. release_region(start + offset, size);
  817. release_region(start + offset, 8 << up->port.regshift);
  818. break;
  819. default:
  820. break;
  821. }
  822. }
  823. static int serial8250_request_port(struct uart_port *port)
  824. {
  825. struct uart_8250_port *up = (struct uart_8250_port *)port;
  826. struct resource *res = NULL, *res_rsa = NULL;
  827. int ret = 0;
  828. ret = serial8250_request_std_resource(up, &res);
  829. /*
  830. * If we have a mapbase, then request that as well.
  831. */
  832. if (ret == 0 && up->port.flags & UPF_IOREMAP) {
  833. int size = res->end - res->start + 1;
  834. up->port.membase = ioremap(up->port.mapbase, size);
  835. if (!up->port.membase)
  836. ret = -ENOMEM;
  837. }
  838. if (ret < 0) {
  839. if (res_rsa)
  840. release_resource(res_rsa);
  841. if (res)
  842. release_resource(res);
  843. }
  844. return ret;
  845. }
  846. static void serial8250_config_port(struct uart_port *port, int flags)
  847. {
  848. struct uart_8250_port *up = (struct uart_8250_port *)port;
  849. struct resource *res_std = NULL, *res_rsa = NULL;
  850. int probeflags = PROBE_ANY;
  851. probeflags &= ~PROBE_RSA;
  852. if (flags & UART_CONFIG_TYPE)
  853. autoconfig(up, probeflags);
  854. /*
  855. * If the port wasn't an RSA port, release the resource.
  856. */
  857. if (up->port.type != PORT_RSA && res_rsa)
  858. release_resource(res_rsa);
  859. if (up->port.type == PORT_UNKNOWN && res_std)
  860. release_resource(res_std);
  861. }
  862. static int
  863. serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
  864. {
  865. if (ser->irq >= NR_IRQS || ser->irq < 0 ||
  866. ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
  867. ser->type > PORT_MAX_8250 || ser->type == PORT_CIRRUS ||
  868. ser->type == PORT_STARTECH)
  869. return -EINVAL;
  870. return 0;
  871. }
  872. static const char *
  873. serial8250_type(struct uart_port *port)
  874. {
  875. int type = port->type;
  876. if (type >= ARRAY_SIZE(uart_config))
  877. type = 0;
  878. return uart_config[type].name;
  879. }
  880. static struct uart_ops serial8250_pops = {
  881. .tx_empty = serial8250_tx_empty,
  882. .set_mctrl = serial8250_set_mctrl,
  883. .get_mctrl = serial8250_get_mctrl,
  884. .stop_tx = serial8250_stop_tx,
  885. .start_tx = serial8250_start_tx,
  886. .stop_rx = serial8250_stop_rx,
  887. .enable_ms = serial8250_enable_ms,
  888. .break_ctl = serial8250_break_ctl,
  889. .startup = serial8250_startup,
  890. .shutdown = serial8250_shutdown,
  891. .set_termios = serial8250_set_termios,
  892. .pm = serial8250_pm,
  893. .type = serial8250_type,
  894. .release_port = serial8250_release_port,
  895. .request_port = serial8250_request_port,
  896. .config_port = serial8250_config_port,
  897. .verify_port = serial8250_verify_port,
  898. };
  899. static struct uart_8250_port serial8250_ports[UART_NR];
  900. static void __init serial8250_isa_init_ports(void)
  901. {
  902. struct uart_8250_port *up;
  903. static int first = 1;
  904. int i;
  905. if (!first)
  906. return;
  907. first = 0;
  908. for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
  909. i++, up++) {
  910. up->port.iobase = old_serial_port[i].port;
  911. up->port.irq = old_serial_port[i].irq;
  912. up->port.uartclk = get_au1x00_uart_baud_base();
  913. up->port.flags = old_serial_port[i].flags;
  914. up->port.hub6 = old_serial_port[i].hub6;
  915. up->port.membase = old_serial_port[i].iomem_base;
  916. up->port.iotype = old_serial_port[i].io_type;
  917. up->port.regshift = old_serial_port[i].iomem_reg_shift;
  918. up->port.ops = &serial8250_pops;
  919. }
  920. }
  921. static void __init serial8250_register_ports(struct uart_driver *drv)
  922. {
  923. int i;
  924. serial8250_isa_init_ports();
  925. for (i = 0; i < UART_NR; i++) {
  926. struct uart_8250_port *up = &serial8250_ports[i];
  927. up->port.line = i;
  928. up->port.ops = &serial8250_pops;
  929. init_timer(&up->timer);
  930. up->timer.function = serial8250_timeout;
  931. /*
  932. * ALPHA_KLUDGE_MCR needs to be killed.
  933. */
  934. up->mcr_mask = ~ALPHA_KLUDGE_MCR;
  935. up->mcr_force = ALPHA_KLUDGE_MCR;
  936. uart_add_one_port(drv, &up->port);
  937. }
  938. }
  939. #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
  940. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  941. /*
  942. * Wait for transmitter & holding register to empty
  943. */
  944. static inline void wait_for_xmitr(struct uart_8250_port *up)
  945. {
  946. unsigned int status, tmout = 10000;
  947. /* Wait up to 10ms for the character(s) to be sent. */
  948. do {
  949. status = serial_in(up, UART_LSR);
  950. if (status & UART_LSR_BI)
  951. up->lsr_break_flag = UART_LSR_BI;
  952. if (--tmout == 0)
  953. break;
  954. udelay(1);
  955. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  956. /* Wait up to 1s for flow control if necessary */
  957. if (up->port.flags & UPF_CONS_FLOW) {
  958. tmout = 1000000;
  959. while (--tmout &&
  960. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  961. udelay(1);
  962. }
  963. }
  964. /*
  965. * Print a string to the serial port trying not to disturb
  966. * any possible real use of the port...
  967. *
  968. * The console_lock must be held when we get here.
  969. */
  970. static void
  971. serial8250_console_write(struct console *co, const char *s, unsigned int count)
  972. {
  973. struct uart_8250_port *up = &serial8250_ports[co->index];
  974. unsigned int ier;
  975. int i;
  976. /*
  977. * First save the UER then disable the interrupts
  978. */
  979. ier = serial_in(up, UART_IER);
  980. serial_out(up, UART_IER, 0);
  981. /*
  982. * Now, do each character
  983. */
  984. for (i = 0; i < count; i++, s++) {
  985. wait_for_xmitr(up);
  986. /*
  987. * Send the character out.
  988. * If a LF, also do CR...
  989. */
  990. serial_out(up, UART_TX, *s);
  991. if (*s == 10) {
  992. wait_for_xmitr(up);
  993. serial_out(up, UART_TX, 13);
  994. }
  995. }
  996. /*
  997. * Finally, wait for transmitter to become empty
  998. * and restore the IER
  999. */
  1000. wait_for_xmitr(up);
  1001. serial_out(up, UART_IER, ier);
  1002. }
  1003. static int __init serial8250_console_setup(struct console *co, char *options)
  1004. {
  1005. struct uart_port *port;
  1006. int baud = 9600;
  1007. int bits = 8;
  1008. int parity = 'n';
  1009. int flow = 'n';
  1010. /*
  1011. * Check whether an invalid uart number has been specified, and
  1012. * if so, search for the first available port that does have
  1013. * console support.
  1014. */
  1015. if (co->index >= UART_NR)
  1016. co->index = 0;
  1017. port = &serial8250_ports[co->index].port;
  1018. /*
  1019. * Temporary fix.
  1020. */
  1021. spin_lock_init(&port->lock);
  1022. if (options)
  1023. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1024. return uart_set_options(port, co, baud, parity, bits, flow);
  1025. }
  1026. extern struct uart_driver serial8250_reg;
  1027. static struct console serial8250_console = {
  1028. .name = "ttyS",
  1029. .write = serial8250_console_write,
  1030. .device = uart_console_device,
  1031. .setup = serial8250_console_setup,
  1032. .flags = CON_PRINTBUFFER,
  1033. .index = -1,
  1034. .data = &serial8250_reg,
  1035. };
  1036. static int __init serial8250_console_init(void)
  1037. {
  1038. serial8250_isa_init_ports();
  1039. register_console(&serial8250_console);
  1040. return 0;
  1041. }
  1042. console_initcall(serial8250_console_init);
  1043. #define SERIAL8250_CONSOLE &serial8250_console
  1044. #else
  1045. #define SERIAL8250_CONSOLE NULL
  1046. #endif
  1047. static struct uart_driver serial8250_reg = {
  1048. .owner = THIS_MODULE,
  1049. .driver_name = "serial",
  1050. .devfs_name = "tts/",
  1051. .dev_name = "ttyS",
  1052. .major = TTY_MAJOR,
  1053. .minor = 64,
  1054. .nr = UART_NR,
  1055. .cons = SERIAL8250_CONSOLE,
  1056. };
  1057. int __init early_serial_setup(struct uart_port *port)
  1058. {
  1059. serial8250_isa_init_ports();
  1060. serial8250_ports[port->line].port = *port;
  1061. serial8250_ports[port->line].port.ops = &serial8250_pops;
  1062. return 0;
  1063. }
  1064. /**
  1065. * serial8250_suspend_port - suspend one serial port
  1066. * @line: serial line number
  1067. * @level: the level of port suspension, as per uart_suspend_port
  1068. *
  1069. * Suspend one serial port.
  1070. */
  1071. void serial8250_suspend_port(int line)
  1072. {
  1073. uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
  1074. }
  1075. /**
  1076. * serial8250_resume_port - resume one serial port
  1077. * @line: serial line number
  1078. * @level: the level of port resumption, as per uart_resume_port
  1079. *
  1080. * Resume one serial port.
  1081. */
  1082. void serial8250_resume_port(int line)
  1083. {
  1084. uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
  1085. }
  1086. static int __init serial8250_init(void)
  1087. {
  1088. int ret, i;
  1089. printk(KERN_INFO "Serial: Au1x00 driver\n");
  1090. for (i = 0; i < NR_IRQS; i++)
  1091. spin_lock_init(&irq_lists[i].lock);
  1092. ret = uart_register_driver(&serial8250_reg);
  1093. if (ret >= 0)
  1094. serial8250_register_ports(&serial8250_reg);
  1095. return ret;
  1096. }
  1097. static void __exit serial8250_exit(void)
  1098. {
  1099. int i;
  1100. for (i = 0; i < UART_NR; i++)
  1101. uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
  1102. uart_unregister_driver(&serial8250_reg);
  1103. }
  1104. module_init(serial8250_init);
  1105. module_exit(serial8250_exit);
  1106. EXPORT_SYMBOL(serial8250_suspend_port);
  1107. EXPORT_SYMBOL(serial8250_resume_port);
  1108. MODULE_LICENSE("GPL");
  1109. MODULE_DESCRIPTION("Au1x00 serial driver\n");