au1x00_uart.c 31 KB

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