au1x00_uart.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296
  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 SERIAL_IO_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 SERIAL_IO_HUB6:
  772. case SERIAL_IO_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 SERIAL_IO_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 SERIAL_IO_HUB6:
  800. case SERIAL_IO_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. /*
  952. * Print a string to the serial port trying not to disturb
  953. * any possible real use of the port...
  954. *
  955. * The console_lock must be held when we get here.
  956. */
  957. static void
  958. serial8250_console_write(struct console *co, const char *s, unsigned int count)
  959. {
  960. struct uart_8250_port *up = &serial8250_ports[co->index];
  961. unsigned int ier;
  962. int i;
  963. /*
  964. * First save the UER then disable the interrupts
  965. */
  966. ier = serial_in(up, UART_IER);
  967. serial_out(up, UART_IER, 0);
  968. /*
  969. * Now, do each character
  970. */
  971. for (i = 0; i < count; i++, s++) {
  972. wait_for_xmitr(up);
  973. /*
  974. * Send the character out.
  975. * If a LF, also do CR...
  976. */
  977. serial_out(up, UART_TX, *s);
  978. if (*s == 10) {
  979. wait_for_xmitr(up);
  980. serial_out(up, UART_TX, 13);
  981. }
  982. }
  983. /*
  984. * Finally, wait for transmitter to become empty
  985. * and restore the IER
  986. */
  987. wait_for_xmitr(up);
  988. serial_out(up, UART_IER, ier);
  989. }
  990. static int __init serial8250_console_setup(struct console *co, char *options)
  991. {
  992. struct uart_port *port;
  993. int baud = 9600;
  994. int bits = 8;
  995. int parity = 'n';
  996. int flow = 'n';
  997. /*
  998. * Check whether an invalid uart number has been specified, and
  999. * if so, search for the first available port that does have
  1000. * console support.
  1001. */
  1002. if (co->index >= UART_NR)
  1003. co->index = 0;
  1004. port = &serial8250_ports[co->index].port;
  1005. /*
  1006. * Temporary fix.
  1007. */
  1008. spin_lock_init(&port->lock);
  1009. if (options)
  1010. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1011. return uart_set_options(port, co, baud, parity, bits, flow);
  1012. }
  1013. extern struct uart_driver serial8250_reg;
  1014. static struct console serial8250_console = {
  1015. .name = "ttyS",
  1016. .write = serial8250_console_write,
  1017. .device = uart_console_device,
  1018. .setup = serial8250_console_setup,
  1019. .flags = CON_PRINTBUFFER,
  1020. .index = -1,
  1021. .data = &serial8250_reg,
  1022. };
  1023. static int __init serial8250_console_init(void)
  1024. {
  1025. serial8250_isa_init_ports();
  1026. register_console(&serial8250_console);
  1027. return 0;
  1028. }
  1029. console_initcall(serial8250_console_init);
  1030. #define SERIAL8250_CONSOLE &serial8250_console
  1031. #else
  1032. #define SERIAL8250_CONSOLE NULL
  1033. #endif
  1034. static struct uart_driver serial8250_reg = {
  1035. .owner = THIS_MODULE,
  1036. .driver_name = "serial",
  1037. .devfs_name = "tts/",
  1038. .dev_name = "ttyS",
  1039. .major = TTY_MAJOR,
  1040. .minor = 64,
  1041. .nr = UART_NR,
  1042. .cons = SERIAL8250_CONSOLE,
  1043. };
  1044. int __init early_serial_setup(struct uart_port *port)
  1045. {
  1046. serial8250_isa_init_ports();
  1047. serial8250_ports[port->line].port = *port;
  1048. serial8250_ports[port->line].port.ops = &serial8250_pops;
  1049. return 0;
  1050. }
  1051. /**
  1052. * serial8250_suspend_port - suspend one serial port
  1053. * @line: serial line number
  1054. * @level: the level of port suspension, as per uart_suspend_port
  1055. *
  1056. * Suspend one serial port.
  1057. */
  1058. void serial8250_suspend_port(int line)
  1059. {
  1060. uart_suspend_port(&serial8250_reg, &serial8250_ports[line].port);
  1061. }
  1062. /**
  1063. * serial8250_resume_port - resume one serial port
  1064. * @line: serial line number
  1065. * @level: the level of port resumption, as per uart_resume_port
  1066. *
  1067. * Resume one serial port.
  1068. */
  1069. void serial8250_resume_port(int line)
  1070. {
  1071. uart_resume_port(&serial8250_reg, &serial8250_ports[line].port);
  1072. }
  1073. static int __init serial8250_init(void)
  1074. {
  1075. int ret, i;
  1076. printk(KERN_INFO "Serial: Au1x00 driver\n");
  1077. for (i = 0; i < NR_IRQS; i++)
  1078. spin_lock_init(&irq_lists[i].lock);
  1079. ret = uart_register_driver(&serial8250_reg);
  1080. if (ret >= 0)
  1081. serial8250_register_ports(&serial8250_reg);
  1082. return ret;
  1083. }
  1084. static void __exit serial8250_exit(void)
  1085. {
  1086. int i;
  1087. for (i = 0; i < UART_NR; i++)
  1088. uart_remove_one_port(&serial8250_reg, &serial8250_ports[i].port);
  1089. uart_unregister_driver(&serial8250_reg);
  1090. }
  1091. module_init(serial8250_init);
  1092. module_exit(serial8250_exit);
  1093. EXPORT_SYMBOL(serial8250_suspend_port);
  1094. EXPORT_SYMBOL(serial8250_resume_port);
  1095. MODULE_LICENSE("GPL");
  1096. MODULE_DESCRIPTION("Au1x00 serial driver\n");