au1x00_uart.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312
  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, unsigned int tty_stop)
  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, unsigned int tty_start)
  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, 0);
  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, 0);
  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 long flags;
  475. unsigned char status;
  476. unsigned int ret;
  477. spin_lock_irqsave(&up->port.lock, flags);
  478. status = serial_in(up, UART_MSR);
  479. spin_unlock_irqrestore(&up->port.lock, flags);
  480. ret = 0;
  481. if (status & UART_MSR_DCD)
  482. ret |= TIOCM_CAR;
  483. if (status & UART_MSR_RI)
  484. ret |= TIOCM_RNG;
  485. if (status & UART_MSR_DSR)
  486. ret |= TIOCM_DSR;
  487. if (status & UART_MSR_CTS)
  488. ret |= TIOCM_CTS;
  489. return ret;
  490. }
  491. static void serial8250_set_mctrl(struct uart_port *port, unsigned int mctrl)
  492. {
  493. struct uart_8250_port *up = (struct uart_8250_port *)port;
  494. unsigned char mcr = 0;
  495. if (mctrl & TIOCM_RTS)
  496. mcr |= UART_MCR_RTS;
  497. if (mctrl & TIOCM_DTR)
  498. mcr |= UART_MCR_DTR;
  499. if (mctrl & TIOCM_OUT1)
  500. mcr |= UART_MCR_OUT1;
  501. if (mctrl & TIOCM_OUT2)
  502. mcr |= UART_MCR_OUT2;
  503. if (mctrl & TIOCM_LOOP)
  504. mcr |= UART_MCR_LOOP;
  505. mcr = (mcr & up->mcr_mask) | up->mcr_force;
  506. serial_out(up, UART_MCR, mcr);
  507. }
  508. static void serial8250_break_ctl(struct uart_port *port, int break_state)
  509. {
  510. struct uart_8250_port *up = (struct uart_8250_port *)port;
  511. unsigned long flags;
  512. spin_lock_irqsave(&up->port.lock, flags);
  513. if (break_state == -1)
  514. up->lcr |= UART_LCR_SBC;
  515. else
  516. up->lcr &= ~UART_LCR_SBC;
  517. serial_out(up, UART_LCR, up->lcr);
  518. spin_unlock_irqrestore(&up->port.lock, flags);
  519. }
  520. static int serial8250_startup(struct uart_port *port)
  521. {
  522. struct uart_8250_port *up = (struct uart_8250_port *)port;
  523. unsigned long flags;
  524. int retval;
  525. /*
  526. * Clear the FIFO buffers and disable them.
  527. * (they will be reeanbled in set_termios())
  528. */
  529. if (uart_config[up->port.type].flags & UART_CLEAR_FIFO) {
  530. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  531. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  532. UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
  533. serial_outp(up, UART_FCR, 0);
  534. }
  535. /*
  536. * Clear the interrupt registers.
  537. */
  538. (void) serial_inp(up, UART_LSR);
  539. (void) serial_inp(up, UART_RX);
  540. (void) serial_inp(up, UART_IIR);
  541. (void) serial_inp(up, UART_MSR);
  542. /*
  543. * At this point, there's no way the LSR could still be 0xff;
  544. * if it is, then bail out, because there's likely no UART
  545. * here.
  546. */
  547. if (!(up->port.flags & UPF_BUGGY_UART) &&
  548. (serial_inp(up, UART_LSR) == 0xff)) {
  549. printk("ttyS%d: LSR safety check engaged!\n", up->port.line);
  550. return -ENODEV;
  551. }
  552. retval = serial_link_irq_chain(up);
  553. if (retval)
  554. return retval;
  555. /*
  556. * Now, initialize the UART
  557. */
  558. serial_outp(up, UART_LCR, UART_LCR_WLEN8);
  559. spin_lock_irqsave(&up->port.lock, flags);
  560. if (up->port.flags & UPF_FOURPORT) {
  561. if (!is_real_interrupt(up->port.irq))
  562. up->port.mctrl |= TIOCM_OUT1;
  563. } else
  564. /*
  565. * Most PC uarts need OUT2 raised to enable interrupts.
  566. */
  567. if (is_real_interrupt(up->port.irq))
  568. up->port.mctrl |= TIOCM_OUT2;
  569. serial8250_set_mctrl(&up->port, up->port.mctrl);
  570. spin_unlock_irqrestore(&up->port.lock, flags);
  571. /*
  572. * Finally, enable interrupts. Note: Modem status interrupts
  573. * are set via set_termios(), which will be occurring imminently
  574. * anyway, so we don't enable them here.
  575. */
  576. up->ier = UART_IER_RLSI | UART_IER_RDI;
  577. serial_outp(up, UART_IER, up->ier);
  578. if (up->port.flags & UPF_FOURPORT) {
  579. unsigned int icp;
  580. /*
  581. * Enable interrupts on the AST Fourport board
  582. */
  583. icp = (up->port.iobase & 0xfe0) | 0x01f;
  584. outb_p(0x80, icp);
  585. (void) inb_p(icp);
  586. }
  587. /*
  588. * And clear the interrupt registers again for luck.
  589. */
  590. (void) serial_inp(up, UART_LSR);
  591. (void) serial_inp(up, UART_RX);
  592. (void) serial_inp(up, UART_IIR);
  593. (void) serial_inp(up, UART_MSR);
  594. return 0;
  595. }
  596. static void serial8250_shutdown(struct uart_port *port)
  597. {
  598. struct uart_8250_port *up = (struct uart_8250_port *)port;
  599. unsigned long flags;
  600. /*
  601. * Disable interrupts from this port
  602. */
  603. up->ier = 0;
  604. serial_outp(up, UART_IER, 0);
  605. spin_lock_irqsave(&up->port.lock, flags);
  606. if (up->port.flags & UPF_FOURPORT) {
  607. /* reset interrupts on the AST Fourport board */
  608. inb((up->port.iobase & 0xfe0) | 0x1f);
  609. up->port.mctrl |= TIOCM_OUT1;
  610. } else
  611. up->port.mctrl &= ~TIOCM_OUT2;
  612. serial8250_set_mctrl(&up->port, up->port.mctrl);
  613. spin_unlock_irqrestore(&up->port.lock, flags);
  614. /*
  615. * Disable break condition and FIFOs
  616. */
  617. serial_out(up, UART_LCR, serial_inp(up, UART_LCR) & ~UART_LCR_SBC);
  618. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO |
  619. UART_FCR_CLEAR_RCVR |
  620. UART_FCR_CLEAR_XMIT);
  621. serial_outp(up, UART_FCR, 0);
  622. /*
  623. * Read data port to reset things, and then unlink from
  624. * the IRQ chain.
  625. */
  626. (void) serial_in(up, UART_RX);
  627. if (!is_real_interrupt(up->port.irq))
  628. del_timer_sync(&up->timer);
  629. else
  630. serial_unlink_irq_chain(up);
  631. }
  632. static unsigned int serial8250_get_divisor(struct uart_port *port, unsigned int baud)
  633. {
  634. unsigned int quot;
  635. /*
  636. * Handle magic divisors for baud rates above baud_base on
  637. * SMSC SuperIO chips.
  638. */
  639. if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  640. baud == (port->uartclk/4))
  641. quot = 0x8001;
  642. else if ((port->flags & UPF_MAGIC_MULTIPLIER) &&
  643. baud == (port->uartclk/8))
  644. quot = 0x8002;
  645. else
  646. quot = uart_get_divisor(port, baud);
  647. return quot;
  648. }
  649. static void
  650. serial8250_set_termios(struct uart_port *port, struct termios *termios,
  651. struct termios *old)
  652. {
  653. struct uart_8250_port *up = (struct uart_8250_port *)port;
  654. unsigned char cval, fcr = 0;
  655. unsigned long flags;
  656. unsigned int baud, quot;
  657. switch (termios->c_cflag & CSIZE) {
  658. case CS5:
  659. cval = UART_LCR_WLEN5;
  660. break;
  661. case CS6:
  662. cval = UART_LCR_WLEN6;
  663. break;
  664. case CS7:
  665. cval = UART_LCR_WLEN7;
  666. break;
  667. default:
  668. case CS8:
  669. cval = UART_LCR_WLEN8;
  670. break;
  671. }
  672. if (termios->c_cflag & CSTOPB)
  673. cval |= UART_LCR_STOP;
  674. if (termios->c_cflag & PARENB)
  675. cval |= UART_LCR_PARITY;
  676. if (!(termios->c_cflag & PARODD))
  677. cval |= UART_LCR_EPAR;
  678. #ifdef CMSPAR
  679. if (termios->c_cflag & CMSPAR)
  680. cval |= UART_LCR_SPAR;
  681. #endif
  682. /*
  683. * Ask the core to calculate the divisor for us.
  684. */
  685. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  686. quot = serial8250_get_divisor(port, baud);
  687. quot = 0x35; /* FIXME */
  688. /*
  689. * Work around a bug in the Oxford Semiconductor 952 rev B
  690. * chip which causes it to seriously miscalculate baud rates
  691. * when DLL is 0.
  692. */
  693. if ((quot & 0xff) == 0 && up->port.type == PORT_16C950 &&
  694. up->rev == 0x5201)
  695. quot ++;
  696. if (uart_config[up->port.type].flags & UART_USE_FIFO) {
  697. if (baud < 2400)
  698. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_1;
  699. else
  700. fcr = UART_FCR_ENABLE_FIFO | UART_FCR_R_TRIGGER_8;
  701. }
  702. /*
  703. * Ok, we're now changing the port state. Do it with
  704. * interrupts disabled.
  705. */
  706. spin_lock_irqsave(&up->port.lock, flags);
  707. /*
  708. * Update the per-port timeout.
  709. */
  710. uart_update_timeout(port, termios->c_cflag, baud);
  711. up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
  712. if (termios->c_iflag & INPCK)
  713. up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
  714. if (termios->c_iflag & (BRKINT | PARMRK))
  715. up->port.read_status_mask |= UART_LSR_BI;
  716. /*
  717. * Characteres to ignore
  718. */
  719. up->port.ignore_status_mask = 0;
  720. if (termios->c_iflag & IGNPAR)
  721. up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
  722. if (termios->c_iflag & IGNBRK) {
  723. up->port.ignore_status_mask |= UART_LSR_BI;
  724. /*
  725. * If we're ignoring parity and break indicators,
  726. * ignore overruns too (for real raw support).
  727. */
  728. if (termios->c_iflag & IGNPAR)
  729. up->port.ignore_status_mask |= UART_LSR_OE;
  730. }
  731. /*
  732. * ignore all characters if CREAD is not set
  733. */
  734. if ((termios->c_cflag & CREAD) == 0)
  735. up->port.ignore_status_mask |= UART_LSR_DR;
  736. /*
  737. * CTS flow control flag and modem status interrupts
  738. */
  739. up->ier &= ~UART_IER_MSI;
  740. if (UART_ENABLE_MS(&up->port, termios->c_cflag))
  741. up->ier |= UART_IER_MSI;
  742. serial_out(up, UART_IER, up->ier);
  743. serial_outp(up, 0x28, quot & 0xffff);
  744. up->lcr = cval; /* Save LCR */
  745. if (up->port.type != PORT_16750) {
  746. if (fcr & UART_FCR_ENABLE_FIFO) {
  747. /* emulated UARTs (Lucent Venus 167x) need two steps */
  748. serial_outp(up, UART_FCR, UART_FCR_ENABLE_FIFO);
  749. }
  750. serial_outp(up, UART_FCR, fcr); /* set fcr */
  751. }
  752. spin_unlock_irqrestore(&up->port.lock, flags);
  753. }
  754. static void
  755. serial8250_pm(struct uart_port *port, unsigned int state,
  756. unsigned int oldstate)
  757. {
  758. struct uart_8250_port *up = (struct uart_8250_port *)port;
  759. if (state) {
  760. /* sleep */
  761. if (up->pm)
  762. up->pm(port, state, oldstate);
  763. } else {
  764. /* wake */
  765. if (up->pm)
  766. up->pm(port, state, oldstate);
  767. }
  768. }
  769. /*
  770. * Resource handling. This is complicated by the fact that resources
  771. * depend on the port type. Maybe we should be claiming the standard
  772. * 8250 ports, and then trying to get other resources as necessary?
  773. */
  774. static int
  775. serial8250_request_std_resource(struct uart_8250_port *up, struct resource **res)
  776. {
  777. unsigned int size = 8 << up->port.regshift;
  778. int ret = 0;
  779. switch (up->port.iotype) {
  780. case SERIAL_IO_MEM:
  781. if (up->port.mapbase) {
  782. *res = request_mem_region(up->port.mapbase, size, "serial");
  783. if (!*res)
  784. ret = -EBUSY;
  785. }
  786. break;
  787. case SERIAL_IO_HUB6:
  788. case SERIAL_IO_PORT:
  789. *res = request_region(up->port.iobase, size, "serial");
  790. if (!*res)
  791. ret = -EBUSY;
  792. break;
  793. }
  794. return ret;
  795. }
  796. static void serial8250_release_port(struct uart_port *port)
  797. {
  798. struct uart_8250_port *up = (struct uart_8250_port *)port;
  799. unsigned long start, offset = 0, size = 0;
  800. size <<= up->port.regshift;
  801. switch (up->port.iotype) {
  802. case SERIAL_IO_MEM:
  803. if (up->port.mapbase) {
  804. /*
  805. * Unmap the area.
  806. */
  807. iounmap(up->port.membase);
  808. up->port.membase = NULL;
  809. start = up->port.mapbase;
  810. if (size)
  811. release_mem_region(start + offset, size);
  812. release_mem_region(start, 8 << up->port.regshift);
  813. }
  814. break;
  815. case SERIAL_IO_HUB6:
  816. case SERIAL_IO_PORT:
  817. start = up->port.iobase;
  818. if (size)
  819. release_region(start + offset, size);
  820. release_region(start + offset, 8 << up->port.regshift);
  821. break;
  822. default:
  823. break;
  824. }
  825. }
  826. static int serial8250_request_port(struct uart_port *port)
  827. {
  828. struct uart_8250_port *up = (struct uart_8250_port *)port;
  829. struct resource *res = NULL, *res_rsa = NULL;
  830. int ret = 0;
  831. ret = serial8250_request_std_resource(up, &res);
  832. /*
  833. * If we have a mapbase, then request that as well.
  834. */
  835. if (ret == 0 && up->port.flags & UPF_IOREMAP) {
  836. int size = res->end - res->start + 1;
  837. up->port.membase = ioremap(up->port.mapbase, size);
  838. if (!up->port.membase)
  839. ret = -ENOMEM;
  840. }
  841. if (ret < 0) {
  842. if (res_rsa)
  843. release_resource(res_rsa);
  844. if (res)
  845. release_resource(res);
  846. }
  847. return ret;
  848. }
  849. static void serial8250_config_port(struct uart_port *port, int flags)
  850. {
  851. struct uart_8250_port *up = (struct uart_8250_port *)port;
  852. struct resource *res_std = NULL, *res_rsa = NULL;
  853. int probeflags = PROBE_ANY;
  854. probeflags &= ~PROBE_RSA;
  855. if (flags & UART_CONFIG_TYPE)
  856. autoconfig(up, probeflags);
  857. /*
  858. * If the port wasn't an RSA port, release the resource.
  859. */
  860. if (up->port.type != PORT_RSA && res_rsa)
  861. release_resource(res_rsa);
  862. if (up->port.type == PORT_UNKNOWN && res_std)
  863. release_resource(res_std);
  864. }
  865. static int
  866. serial8250_verify_port(struct uart_port *port, struct serial_struct *ser)
  867. {
  868. if (ser->irq >= NR_IRQS || ser->irq < 0 ||
  869. ser->baud_base < 9600 || ser->type < PORT_UNKNOWN ||
  870. ser->type > PORT_MAX_8250 || ser->type == PORT_CIRRUS ||
  871. ser->type == PORT_STARTECH)
  872. return -EINVAL;
  873. return 0;
  874. }
  875. static const char *
  876. serial8250_type(struct uart_port *port)
  877. {
  878. int type = port->type;
  879. if (type >= ARRAY_SIZE(uart_config))
  880. type = 0;
  881. return uart_config[type].name;
  882. }
  883. static struct uart_ops serial8250_pops = {
  884. .tx_empty = serial8250_tx_empty,
  885. .set_mctrl = serial8250_set_mctrl,
  886. .get_mctrl = serial8250_get_mctrl,
  887. .stop_tx = serial8250_stop_tx,
  888. .start_tx = serial8250_start_tx,
  889. .stop_rx = serial8250_stop_rx,
  890. .enable_ms = serial8250_enable_ms,
  891. .break_ctl = serial8250_break_ctl,
  892. .startup = serial8250_startup,
  893. .shutdown = serial8250_shutdown,
  894. .set_termios = serial8250_set_termios,
  895. .pm = serial8250_pm,
  896. .type = serial8250_type,
  897. .release_port = serial8250_release_port,
  898. .request_port = serial8250_request_port,
  899. .config_port = serial8250_config_port,
  900. .verify_port = serial8250_verify_port,
  901. };
  902. static struct uart_8250_port serial8250_ports[UART_NR];
  903. static void __init serial8250_isa_init_ports(void)
  904. {
  905. struct uart_8250_port *up;
  906. static int first = 1;
  907. int i;
  908. if (!first)
  909. return;
  910. first = 0;
  911. for (i = 0, up = serial8250_ports; i < ARRAY_SIZE(old_serial_port);
  912. i++, up++) {
  913. up->port.iobase = old_serial_port[i].port;
  914. up->port.irq = old_serial_port[i].irq;
  915. up->port.uartclk = get_au1x00_uart_baud_base();
  916. up->port.flags = old_serial_port[i].flags;
  917. up->port.hub6 = old_serial_port[i].hub6;
  918. up->port.membase = old_serial_port[i].iomem_base;
  919. up->port.iotype = old_serial_port[i].io_type;
  920. up->port.regshift = old_serial_port[i].iomem_reg_shift;
  921. up->port.ops = &serial8250_pops;
  922. }
  923. }
  924. static void __init serial8250_register_ports(struct uart_driver *drv)
  925. {
  926. int i;
  927. serial8250_isa_init_ports();
  928. for (i = 0; i < UART_NR; i++) {
  929. struct uart_8250_port *up = &serial8250_ports[i];
  930. up->port.line = i;
  931. up->port.ops = &serial8250_pops;
  932. init_timer(&up->timer);
  933. up->timer.function = serial8250_timeout;
  934. /*
  935. * ALPHA_KLUDGE_MCR needs to be killed.
  936. */
  937. up->mcr_mask = ~ALPHA_KLUDGE_MCR;
  938. up->mcr_force = ALPHA_KLUDGE_MCR;
  939. uart_add_one_port(drv, &up->port);
  940. }
  941. }
  942. #ifdef CONFIG_SERIAL_AU1X00_CONSOLE
  943. #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
  944. /*
  945. * Wait for transmitter & holding register to empty
  946. */
  947. static inline void wait_for_xmitr(struct uart_8250_port *up)
  948. {
  949. unsigned int status, tmout = 10000;
  950. /* Wait up to 10ms for the character(s) to be sent. */
  951. do {
  952. status = serial_in(up, UART_LSR);
  953. if (status & UART_LSR_BI)
  954. up->lsr_break_flag = UART_LSR_BI;
  955. if (--tmout == 0)
  956. break;
  957. udelay(1);
  958. } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
  959. /* Wait up to 1s for flow control if necessary */
  960. if (up->port.flags & UPF_CONS_FLOW) {
  961. tmout = 1000000;
  962. while (--tmout &&
  963. ((serial_in(up, UART_MSR) & UART_MSR_CTS) == 0))
  964. udelay(1);
  965. }
  966. }
  967. /*
  968. * Print a string to the serial port trying not to disturb
  969. * any possible real use of the port...
  970. *
  971. * The console_lock must be held when we get here.
  972. */
  973. static void
  974. serial8250_console_write(struct console *co, const char *s, unsigned int count)
  975. {
  976. struct uart_8250_port *up = &serial8250_ports[co->index];
  977. unsigned int ier;
  978. int i;
  979. /*
  980. * First save the UER then disable the interrupts
  981. */
  982. ier = serial_in(up, UART_IER);
  983. serial_out(up, UART_IER, 0);
  984. /*
  985. * Now, do each character
  986. */
  987. for (i = 0; i < count; i++, s++) {
  988. wait_for_xmitr(up);
  989. /*
  990. * Send the character out.
  991. * If a LF, also do CR...
  992. */
  993. serial_out(up, UART_TX, *s);
  994. if (*s == 10) {
  995. wait_for_xmitr(up);
  996. serial_out(up, UART_TX, 13);
  997. }
  998. }
  999. /*
  1000. * Finally, wait for transmitter to become empty
  1001. * and restore the IER
  1002. */
  1003. wait_for_xmitr(up);
  1004. serial_out(up, UART_IER, ier);
  1005. }
  1006. static int __init serial8250_console_setup(struct console *co, char *options)
  1007. {
  1008. struct uart_port *port;
  1009. int baud = 9600;
  1010. int bits = 8;
  1011. int parity = 'n';
  1012. int flow = 'n';
  1013. /*
  1014. * Check whether an invalid uart number has been specified, and
  1015. * if so, search for the first available port that does have
  1016. * console support.
  1017. */
  1018. if (co->index >= UART_NR)
  1019. co->index = 0;
  1020. port = &serial8250_ports[co->index].port;
  1021. /*
  1022. * Temporary fix.
  1023. */
  1024. spin_lock_init(&port->lock);
  1025. if (options)
  1026. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1027. return uart_set_options(port, co, baud, parity, bits, flow);
  1028. }
  1029. extern struct uart_driver serial8250_reg;
  1030. static struct console serial8250_console = {
  1031. .name = "ttyS",
  1032. .write = serial8250_console_write,
  1033. .device = uart_console_device,
  1034. .setup = serial8250_console_setup,
  1035. .flags = CON_PRINTBUFFER,
  1036. .index = -1,
  1037. .data = &serial8250_reg,
  1038. };
  1039. static int __init serial8250_console_init(void)
  1040. {
  1041. serial8250_isa_init_ports();
  1042. register_console(&serial8250_console);
  1043. return 0;
  1044. }
  1045. console_initcall(serial8250_console_init);
  1046. #define SERIAL8250_CONSOLE &serial8250_console
  1047. #else
  1048. #define SERIAL8250_CONSOLE NULL
  1049. #endif
  1050. static struct uart_driver serial8250_reg = {
  1051. .owner = THIS_MODULE,
  1052. .driver_name = "serial",
  1053. .devfs_name = "tts/",
  1054. .dev_name = "ttyS",
  1055. .major = TTY_MAJOR,
  1056. .minor = 64,
  1057. .nr = UART_NR,
  1058. .cons = SERIAL8250_CONSOLE,
  1059. };
  1060. int __init early_serial_setup(struct uart_port *port)
  1061. {
  1062. serial8250_isa_init_ports();
  1063. serial8250_ports[port->line].port = *port;
  1064. serial8250_ports[port->line].port.ops = &serial8250_pops;
  1065. return 0;
  1066. }
  1067. /**
  1068. * serial8250_suspend_port - suspend one serial port
  1069. * @line: serial line number
  1070. * @level: the level of port suspension, as per uart_suspend_port
  1071. *
  1072. * Suspend one serial port.
  1073. */
  1074. void serial8250_suspend_port(int line)
  1075. {
  1076. uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
  1077. }
  1078. /**
  1079. * serial8250_resume_port - resume one serial port
  1080. * @line: serial line number
  1081. * @level: the level of port resumption, as per uart_resume_port
  1082. *
  1083. * Resume one serial port.
  1084. */
  1085. void serial8250_resume_port(int line)
  1086. {
  1087. uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
  1088. }
  1089. static int __init serial8250_init(void)
  1090. {
  1091. int ret, i;
  1092. printk(KERN_INFO "Serial: Au1x00 driver\n");
  1093. for (i = 0; i < NR_IRQS; i++)
  1094. spin_lock_init(&irq_lists[i].lock);
  1095. ret = uart_register_driver(&serial8250_reg);
  1096. if (ret >= 0)
  1097. serial8250_register_ports(&serial8250_reg);
  1098. return ret;
  1099. }
  1100. static void __exit serial8250_exit(void)
  1101. {
  1102. int i;
  1103. for (i = 0; i < UART_NR; i++)
  1104. uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
  1105. uart_unregister_driver(&serial8250_reg);
  1106. }
  1107. module_init(serial8250_init);
  1108. module_exit(serial8250_exit);
  1109. EXPORT_SYMBOL(serial8250_suspend_port);
  1110. EXPORT_SYMBOL(serial8250_resume_port);
  1111. MODULE_LICENSE("GPL");
  1112. MODULE_DESCRIPTION("Au1x00 serial driver\n");