au1x00_uart.c 31 KB

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