at91_serial.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. /*
  2. * linux/drivers/char/at91_serial.c
  3. *
  4. * Driver for Atmel AT91RM9200 Serial ports
  5. *
  6. * Copyright (C) 2003 Rick Bronson
  7. *
  8. * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
  9. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. *
  25. */
  26. #include <linux/config.h>
  27. #include <linux/module.h>
  28. #include <linux/tty.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <linux/serial.h>
  33. #include <linux/console.h>
  34. #include <linux/sysrq.h>
  35. #include <linux/tty_flip.h>
  36. #include <asm/io.h>
  37. #include <asm/arch/at91rm9200_usart.h>
  38. #include <asm/mach/serial_at91rm9200.h>
  39. #include <asm/arch/board.h>
  40. #include <asm/arch/pio.h>
  41. #if defined(CONFIG_SERIAL_AT91_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  42. #define SUPPORT_SYSRQ
  43. #endif
  44. #include <linux/serial_core.h>
  45. #ifdef CONFIG_SERIAL_AT91_TTYAT
  46. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  47. * should coexist with the 8250 driver, such as if we have an external 16C550
  48. * UART. */
  49. #define SERIAL_AT91_MAJOR 204
  50. #define MINOR_START 154
  51. #define AT91_DEVICENAME "ttyAT"
  52. #else
  53. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  54. * name, but it is legally reserved for the 8250 driver. */
  55. #define SERIAL_AT91_MAJOR TTY_MAJOR
  56. #define MINOR_START 64
  57. #define AT91_DEVICENAME "ttyS"
  58. #endif
  59. #define AT91_VA_BASE_DBGU ((unsigned long) AT91_VA_BASE_SYS + AT91_DBGU)
  60. #define AT91_ISR_PASS_LIMIT 256
  61. #define UART_PUT_CR(port,v) writel(v, (port)->membase + AT91_US_CR)
  62. #define UART_GET_MR(port) readl((port)->membase + AT91_US_MR)
  63. #define UART_PUT_MR(port,v) writel(v, (port)->membase + AT91_US_MR)
  64. #define UART_PUT_IER(port,v) writel(v, (port)->membase + AT91_US_IER)
  65. #define UART_PUT_IDR(port,v) writel(v, (port)->membase + AT91_US_IDR)
  66. #define UART_GET_IMR(port) readl((port)->membase + AT91_US_IMR)
  67. #define UART_GET_CSR(port) readl((port)->membase + AT91_US_CSR)
  68. #define UART_GET_CHAR(port) readl((port)->membase + AT91_US_RHR)
  69. #define UART_PUT_CHAR(port,v) writel(v, (port)->membase + AT91_US_THR)
  70. #define UART_GET_BRGR(port) readl((port)->membase + AT91_US_BRGR)
  71. #define UART_PUT_BRGR(port,v) writel(v, (port)->membase + AT91_US_BRGR)
  72. #define UART_PUT_RTOR(port,v) writel(v, (port)->membase + AT91_US_RTOR)
  73. // #define UART_GET_CR(port) readl((port)->membase + AT91_US_CR) // is write-only
  74. /* PDC registers */
  75. #define UART_PUT_PTCR(port,v) writel(v, (port)->membase + AT91_PDC_PTCR)
  76. #define UART_PUT_RPR(port,v) writel(v, (port)->membase + AT91_PDC_RPR)
  77. #define UART_PUT_RCR(port,v) writel(v, (port)->membase + AT91_PDC_RCR)
  78. #define UART_GET_RCR(port) readl((port)->membase + AT91_PDC_RCR)
  79. #define UART_PUT_RNPR(port,v) writel(v, (port)->membase + AT91_PDC_RNPR)
  80. #define UART_PUT_RNCR(port,v) writel(v, (port)->membase + AT91_PDC_RNCR)
  81. static int (*at91_open)(struct uart_port *);
  82. static void (*at91_close)(struct uart_port *);
  83. #ifdef SUPPORT_SYSRQ
  84. static struct console at91_console;
  85. #endif
  86. /*
  87. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  88. */
  89. static u_int at91_tx_empty(struct uart_port *port)
  90. {
  91. return (UART_GET_CSR(port) & AT91_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  92. }
  93. /*
  94. * Set state of the modem control output lines
  95. */
  96. static void at91_set_mctrl(struct uart_port *port, u_int mctrl)
  97. {
  98. unsigned int control = 0;
  99. /*
  100. * Errata #39: RTS0 is not internally connected to PA21. We need to drive
  101. * the pin manually.
  102. */
  103. if (port->mapbase == AT91_VA_BASE_US0) {
  104. if (mctrl & TIOCM_RTS)
  105. at91_sys_write(AT91_PIOA + PIO_CODR, AT91_PA21_RTS0);
  106. else
  107. at91_sys_write(AT91_PIOA + PIO_SODR, AT91_PA21_RTS0);
  108. }
  109. if (mctrl & TIOCM_RTS)
  110. control |= AT91_US_RTSEN;
  111. else
  112. control |= AT91_US_RTSDIS;
  113. if (mctrl & TIOCM_DTR)
  114. control |= AT91_US_DTREN;
  115. else
  116. control |= AT91_US_DTRDIS;
  117. UART_PUT_CR(port,control);
  118. }
  119. /*
  120. * Get state of the modem control input lines
  121. */
  122. static u_int at91_get_mctrl(struct uart_port *port)
  123. {
  124. unsigned int status, ret = 0;
  125. status = UART_GET_CSR(port);
  126. /*
  127. * The control signals are active low.
  128. */
  129. if (!(status & AT91_US_DCD))
  130. ret |= TIOCM_CD;
  131. if (!(status & AT91_US_CTS))
  132. ret |= TIOCM_CTS;
  133. if (!(status & AT91_US_DSR))
  134. ret |= TIOCM_DSR;
  135. if (!(status & AT91_US_RI))
  136. ret |= TIOCM_RI;
  137. return ret;
  138. }
  139. /*
  140. * Stop transmitting.
  141. */
  142. static void at91_stop_tx(struct uart_port *port)
  143. {
  144. UART_PUT_IDR(port, AT91_US_TXRDY);
  145. port->read_status_mask &= ~AT91_US_TXRDY;
  146. }
  147. /*
  148. * Start transmitting.
  149. */
  150. static void at91_start_tx(struct uart_port *port)
  151. {
  152. port->read_status_mask |= AT91_US_TXRDY;
  153. UART_PUT_IER(port, AT91_US_TXRDY);
  154. }
  155. /*
  156. * Stop receiving - port is in process of being closed.
  157. */
  158. static void at91_stop_rx(struct uart_port *port)
  159. {
  160. UART_PUT_IDR(port, AT91_US_RXRDY);
  161. }
  162. /*
  163. * Enable modem status interrupts
  164. */
  165. static void at91_enable_ms(struct uart_port *port)
  166. {
  167. port->read_status_mask |= (AT91_US_RIIC | AT91_US_DSRIC | AT91_US_DCDIC | AT91_US_CTSIC);
  168. UART_PUT_IER(port, AT91_US_RIIC | AT91_US_DSRIC | AT91_US_DCDIC | AT91_US_CTSIC);
  169. }
  170. /*
  171. * Control the transmission of a break signal
  172. */
  173. static void at91_break_ctl(struct uart_port *port, int break_state)
  174. {
  175. if (break_state != 0)
  176. UART_PUT_CR(port, AT91_US_STTBRK); /* start break */
  177. else
  178. UART_PUT_CR(port, AT91_US_STPBRK); /* stop break */
  179. }
  180. /*
  181. * Characters received (called from interrupt handler)
  182. */
  183. static void at91_rx_chars(struct uart_port *port, struct pt_regs *regs)
  184. {
  185. struct tty_struct *tty = port->info->tty;
  186. unsigned int status, ch, flg;
  187. status = UART_GET_CSR(port) & port->read_status_mask;
  188. while (status & (AT91_US_RXRDY)) {
  189. ch = UART_GET_CHAR(port);
  190. if (tty->flip.count >= TTY_FLIPBUF_SIZE)
  191. goto ignore_char;
  192. port->icount.rx++;
  193. flg = TTY_NORMAL;
  194. /*
  195. * note that the error handling code is
  196. * out of the main execution path
  197. */
  198. if (unlikely(status & (AT91_US_PARE | AT91_US_FRAME | AT91_US_OVRE))) {
  199. UART_PUT_CR(port, AT91_US_RSTSTA); /* clear error */
  200. if (status & (AT91_US_PARE))
  201. port->icount.parity++;
  202. if (status & (AT91_US_FRAME))
  203. port->icount.frame++;
  204. if (status & (AT91_US_OVRE))
  205. port->icount.overrun++;
  206. if (status & AT91_US_PARE)
  207. flg = TTY_PARITY;
  208. else if (status & AT91_US_FRAME)
  209. flg = TTY_FRAME;
  210. if (status & AT91_US_OVRE) {
  211. /*
  212. * overrun does *not* affect the character
  213. * we read from the FIFO
  214. */
  215. tty_insert_flip_char(tty, ch, flg);
  216. ch = 0;
  217. flg = TTY_OVERRUN;
  218. }
  219. #ifdef SUPPORT_SYSRQ
  220. port->sysrq = 0;
  221. #endif
  222. }
  223. if (uart_handle_sysrq_char(port, ch, regs))
  224. goto ignore_char;
  225. tty_insert_flip_char(tty, ch, flg);
  226. ignore_char:
  227. status = UART_GET_CSR(port) & port->read_status_mask;
  228. }
  229. tty_flip_buffer_push(tty);
  230. }
  231. /*
  232. * Transmit characters (called from interrupt handler)
  233. */
  234. static void at91_tx_chars(struct uart_port *port)
  235. {
  236. struct circ_buf *xmit = &port->info->xmit;
  237. if (port->x_char) {
  238. UART_PUT_CHAR(port, port->x_char);
  239. port->icount.tx++;
  240. port->x_char = 0;
  241. return;
  242. }
  243. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  244. at91_stop_tx(port);
  245. return;
  246. }
  247. while (UART_GET_CSR(port) & AT91_US_TXRDY) {
  248. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  249. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  250. port->icount.tx++;
  251. if (uart_circ_empty(xmit))
  252. break;
  253. }
  254. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  255. uart_write_wakeup(port);
  256. if (uart_circ_empty(xmit))
  257. at91_stop_tx(port);
  258. }
  259. /*
  260. * Interrupt handler
  261. */
  262. static irqreturn_t at91_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  263. {
  264. struct uart_port *port = dev_id;
  265. unsigned int status, pending, pass_counter = 0;
  266. status = UART_GET_CSR(port);
  267. pending = status & port->read_status_mask;
  268. if (pending) {
  269. do {
  270. if (pending & AT91_US_RXRDY)
  271. at91_rx_chars(port, regs);
  272. /* Clear the relevent break bits */
  273. if (pending & AT91_US_RXBRK) {
  274. UART_PUT_CR(port, AT91_US_RSTSTA);
  275. port->icount.brk++;
  276. uart_handle_break(port);
  277. }
  278. // TODO: All reads to CSR will clear these interrupts!
  279. if (pending & AT91_US_RIIC) port->icount.rng++;
  280. if (pending & AT91_US_DSRIC) port->icount.dsr++;
  281. if (pending & AT91_US_DCDIC)
  282. uart_handle_dcd_change(port, !(status & AT91_US_DCD));
  283. if (pending & AT91_US_CTSIC)
  284. uart_handle_cts_change(port, !(status & AT91_US_CTS));
  285. if (pending & (AT91_US_RIIC | AT91_US_DSRIC | AT91_US_DCDIC | AT91_US_CTSIC))
  286. wake_up_interruptible(&port->info->delta_msr_wait);
  287. if (pending & AT91_US_TXRDY)
  288. at91_tx_chars(port);
  289. if (pass_counter++ > AT91_ISR_PASS_LIMIT)
  290. break;
  291. status = UART_GET_CSR(port);
  292. pending = status & port->read_status_mask;
  293. } while (pending);
  294. }
  295. return IRQ_HANDLED;
  296. }
  297. /*
  298. * Perform initialization and enable port for reception
  299. */
  300. static int at91_startup(struct uart_port *port)
  301. {
  302. int retval;
  303. /*
  304. * Ensure that no interrupts are enabled otherwise when
  305. * request_irq() is called we could get stuck trying to
  306. * handle an unexpected interrupt
  307. */
  308. UART_PUT_IDR(port, -1);
  309. /*
  310. * Allocate the IRQ
  311. */
  312. retval = request_irq(port->irq, at91_interrupt, SA_SHIRQ, "at91_serial", port);
  313. if (retval) {
  314. printk("at91_serial: at91_startup - Can't get irq\n");
  315. return retval;
  316. }
  317. /*
  318. * If there is a specific "open" function (to register
  319. * control line interrupts)
  320. */
  321. if (at91_open) {
  322. retval = at91_open(port);
  323. if (retval) {
  324. free_irq(port->irq, port);
  325. return retval;
  326. }
  327. }
  328. port->read_status_mask = AT91_US_RXRDY | AT91_US_TXRDY | AT91_US_OVRE
  329. | AT91_US_FRAME | AT91_US_PARE | AT91_US_RXBRK;
  330. /*
  331. * Finally, enable the serial port
  332. */
  333. UART_PUT_CR(port, AT91_US_RSTSTA | AT91_US_RSTRX);
  334. UART_PUT_CR(port, AT91_US_TXEN | AT91_US_RXEN); /* enable xmit & rcvr */
  335. UART_PUT_IER(port, AT91_US_RXRDY); /* do receive only */
  336. return 0;
  337. }
  338. /*
  339. * Disable the port
  340. */
  341. static void at91_shutdown(struct uart_port *port)
  342. {
  343. /*
  344. * Disable all interrupts, port and break condition.
  345. */
  346. UART_PUT_CR(port, AT91_US_RSTSTA);
  347. UART_PUT_IDR(port, -1);
  348. /*
  349. * Free the interrupt
  350. */
  351. free_irq(port->irq, port);
  352. /*
  353. * If there is a specific "close" function (to unregister
  354. * control line interrupts)
  355. */
  356. if (at91_close)
  357. at91_close(port);
  358. }
  359. /*
  360. * Power / Clock management.
  361. */
  362. static void at91_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  363. {
  364. switch (state) {
  365. case 0:
  366. /*
  367. * Enable the peripheral clock for this serial port.
  368. * This is called on uart_open() or a resume event.
  369. */
  370. at91_sys_write(AT91_PMC_PCER, 1 << port->irq);
  371. break;
  372. case 3:
  373. /*
  374. * Disable the peripheral clock for this serial port.
  375. * This is called on uart_close() or a suspend event.
  376. */
  377. if (port->irq != AT91_ID_SYS) /* is this a shared clock? */
  378. at91_sys_write(AT91_PMC_PCDR, 1 << port->irq);
  379. break;
  380. default:
  381. printk(KERN_ERR "at91_serial: unknown pm %d\n", state);
  382. }
  383. }
  384. /*
  385. * Change the port parameters
  386. */
  387. static void at91_set_termios(struct uart_port *port, struct termios * termios, struct termios * old)
  388. {
  389. unsigned long flags;
  390. unsigned int mode, imr, quot, baud;
  391. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  392. quot = uart_get_divisor(port, baud);
  393. /* Get current mode register */
  394. mode = UART_GET_MR(port) & ~(AT91_US_CHRL | AT91_US_NBSTOP | AT91_US_PAR);
  395. /* byte size */
  396. switch (termios->c_cflag & CSIZE) {
  397. case CS5:
  398. mode |= AT91_US_CHRL_5;
  399. break;
  400. case CS6:
  401. mode |= AT91_US_CHRL_6;
  402. break;
  403. case CS7:
  404. mode |= AT91_US_CHRL_7;
  405. break;
  406. default:
  407. mode |= AT91_US_CHRL_8;
  408. break;
  409. }
  410. /* stop bits */
  411. if (termios->c_cflag & CSTOPB)
  412. mode |= AT91_US_NBSTOP_2;
  413. /* parity */
  414. if (termios->c_cflag & PARENB) {
  415. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  416. if (termios->c_cflag & PARODD)
  417. mode |= AT91_US_PAR_MARK;
  418. else
  419. mode |= AT91_US_PAR_SPACE;
  420. }
  421. else if (termios->c_cflag & PARODD)
  422. mode |= AT91_US_PAR_ODD;
  423. else
  424. mode |= AT91_US_PAR_EVEN;
  425. }
  426. else
  427. mode |= AT91_US_PAR_NONE;
  428. spin_lock_irqsave(&port->lock, flags);
  429. port->read_status_mask |= AT91_US_OVRE;
  430. if (termios->c_iflag & INPCK)
  431. port->read_status_mask |= AT91_US_FRAME | AT91_US_PARE;
  432. if (termios->c_iflag & (BRKINT | PARMRK))
  433. port->read_status_mask |= AT91_US_RXBRK;
  434. /*
  435. * Characters to ignore
  436. */
  437. port->ignore_status_mask = 0;
  438. if (termios->c_iflag & IGNPAR)
  439. port->ignore_status_mask |= (AT91_US_FRAME | AT91_US_PARE);
  440. if (termios->c_iflag & IGNBRK) {
  441. port->ignore_status_mask |= AT91_US_RXBRK;
  442. /*
  443. * If we're ignoring parity and break indicators,
  444. * ignore overruns too (for real raw support).
  445. */
  446. if (termios->c_iflag & IGNPAR)
  447. port->ignore_status_mask |= AT91_US_OVRE;
  448. }
  449. // TODO: Ignore all characters if CREAD is set.
  450. /* update the per-port timeout */
  451. uart_update_timeout(port, termios->c_cflag, baud);
  452. /* disable interrupts and drain transmitter */
  453. imr = UART_GET_IMR(port); /* get interrupt mask */
  454. UART_PUT_IDR(port, -1); /* disable all interrupts */
  455. while (!(UART_GET_CSR(port) & AT91_US_TXEMPTY)) { barrier(); }
  456. /* disable receiver and transmitter */
  457. UART_PUT_CR(port, AT91_US_TXDIS | AT91_US_RXDIS);
  458. /* set the parity, stop bits and data size */
  459. UART_PUT_MR(port, mode);
  460. /* set the baud rate */
  461. UART_PUT_BRGR(port, quot);
  462. UART_PUT_CR(port, AT91_US_RSTSTA | AT91_US_RSTRX);
  463. UART_PUT_CR(port, AT91_US_TXEN | AT91_US_RXEN);
  464. /* restore interrupts */
  465. UART_PUT_IER(port, imr);
  466. /* CTS flow-control and modem-status interrupts */
  467. if (UART_ENABLE_MS(port, termios->c_cflag))
  468. port->ops->enable_ms(port);
  469. spin_unlock_irqrestore(&port->lock, flags);
  470. }
  471. /*
  472. * Return string describing the specified port
  473. */
  474. static const char *at91_type(struct uart_port *port)
  475. {
  476. return (port->type == PORT_AT91RM9200) ? "AT91_SERIAL" : NULL;
  477. }
  478. /*
  479. * Release the memory region(s) being used by 'port'.
  480. */
  481. static void at91_release_port(struct uart_port *port)
  482. {
  483. release_mem_region(port->mapbase,
  484. (port->mapbase == AT91_VA_BASE_DBGU) ? 512 : SZ_16K);
  485. }
  486. /*
  487. * Request the memory region(s) being used by 'port'.
  488. */
  489. static int at91_request_port(struct uart_port *port)
  490. {
  491. return request_mem_region(port->mapbase,
  492. (port->mapbase == AT91_VA_BASE_DBGU) ? 512 : SZ_16K,
  493. "at91_serial") != NULL ? 0 : -EBUSY;
  494. }
  495. /*
  496. * Configure/autoconfigure the port.
  497. */
  498. static void at91_config_port(struct uart_port *port, int flags)
  499. {
  500. if (flags & UART_CONFIG_TYPE) {
  501. port->type = PORT_AT91RM9200;
  502. at91_request_port(port);
  503. }
  504. }
  505. /*
  506. * Verify the new serial_struct (for TIOCSSERIAL).
  507. */
  508. static int at91_verify_port(struct uart_port *port, struct serial_struct *ser)
  509. {
  510. int ret = 0;
  511. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AT91RM9200)
  512. ret = -EINVAL;
  513. if (port->irq != ser->irq)
  514. ret = -EINVAL;
  515. if (ser->io_type != SERIAL_IO_MEM)
  516. ret = -EINVAL;
  517. if (port->uartclk / 16 != ser->baud_base)
  518. ret = -EINVAL;
  519. if ((void *)port->mapbase != ser->iomem_base)
  520. ret = -EINVAL;
  521. if (port->iobase != ser->port)
  522. ret = -EINVAL;
  523. if (ser->hub6 != 0)
  524. ret = -EINVAL;
  525. return ret;
  526. }
  527. static struct uart_ops at91_pops = {
  528. .tx_empty = at91_tx_empty,
  529. .set_mctrl = at91_set_mctrl,
  530. .get_mctrl = at91_get_mctrl,
  531. .stop_tx = at91_stop_tx,
  532. .start_tx = at91_start_tx,
  533. .stop_rx = at91_stop_rx,
  534. .enable_ms = at91_enable_ms,
  535. .break_ctl = at91_break_ctl,
  536. .startup = at91_startup,
  537. .shutdown = at91_shutdown,
  538. .set_termios = at91_set_termios,
  539. .type = at91_type,
  540. .release_port = at91_release_port,
  541. .request_port = at91_request_port,
  542. .config_port = at91_config_port,
  543. .verify_port = at91_verify_port,
  544. .pm = at91_serial_pm,
  545. };
  546. static struct uart_port at91_ports[AT91_NR_UART];
  547. void __init at91_init_ports(void)
  548. {
  549. static int first = 1;
  550. int i;
  551. if (!first)
  552. return;
  553. first = 0;
  554. for (i = 0; i < AT91_NR_UART; i++) {
  555. at91_ports[i].iotype = UPIO_MEM;
  556. at91_ports[i].flags = UPF_BOOT_AUTOCONF;
  557. at91_ports[i].uartclk = at91_master_clock;
  558. at91_ports[i].ops = &at91_pops;
  559. at91_ports[i].fifosize = 1;
  560. at91_ports[i].line = i;
  561. }
  562. }
  563. void __init at91_register_uart_fns(struct at91rm9200_port_fns *fns)
  564. {
  565. if (fns->enable_ms)
  566. at91_pops.enable_ms = fns->enable_ms;
  567. if (fns->get_mctrl)
  568. at91_pops.get_mctrl = fns->get_mctrl;
  569. if (fns->set_mctrl)
  570. at91_pops.set_mctrl = fns->set_mctrl;
  571. at91_open = fns->open;
  572. at91_close = fns->close;
  573. at91_pops.pm = fns->pm;
  574. at91_pops.set_wake = fns->set_wake;
  575. }
  576. /*
  577. * Setup ports.
  578. */
  579. void __init at91_register_uart(int idx, int port)
  580. {
  581. if ((idx < 0) || (idx >= AT91_NR_UART)) {
  582. printk(KERN_ERR "%s: bad index number %d\n", __FUNCTION__, idx);
  583. return;
  584. }
  585. switch (port) {
  586. case 0:
  587. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US0;
  588. at91_ports[idx].mapbase = AT91_VA_BASE_US0;
  589. at91_ports[idx].irq = AT91_ID_US0;
  590. AT91_CfgPIO_USART0();
  591. break;
  592. case 1:
  593. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US1;
  594. at91_ports[idx].mapbase = AT91_VA_BASE_US1;
  595. at91_ports[idx].irq = AT91_ID_US1;
  596. AT91_CfgPIO_USART1();
  597. break;
  598. case 2:
  599. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US2;
  600. at91_ports[idx].mapbase = AT91_VA_BASE_US2;
  601. at91_ports[idx].irq = AT91_ID_US2;
  602. AT91_CfgPIO_USART2();
  603. break;
  604. case 3:
  605. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US3;
  606. at91_ports[idx].mapbase = AT91_VA_BASE_US3;
  607. at91_ports[idx].irq = AT91_ID_US3;
  608. AT91_CfgPIO_USART3();
  609. break;
  610. case 4:
  611. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_DBGU;
  612. at91_ports[idx].mapbase = AT91_VA_BASE_DBGU;
  613. at91_ports[idx].irq = AT91_ID_SYS;
  614. AT91_CfgPIO_DBGU();
  615. break;
  616. default:
  617. printk(KERN_ERR "%s : bad port number %d\n", __FUNCTION__, port);
  618. }
  619. }
  620. #ifdef CONFIG_SERIAL_AT91_CONSOLE
  621. /*
  622. * Interrupts are disabled on entering
  623. */
  624. static void at91_console_write(struct console *co, const char *s, u_int count)
  625. {
  626. struct uart_port *port = at91_ports + co->index;
  627. unsigned int status, i, imr;
  628. /*
  629. * First, save IMR and then disable interrupts
  630. */
  631. imr = UART_GET_IMR(port); /* get interrupt mask */
  632. UART_PUT_IDR(port, AT91_US_RXRDY | AT91_US_TXRDY);
  633. /*
  634. * Now, do each character
  635. */
  636. for (i = 0; i < count; i++) {
  637. do {
  638. status = UART_GET_CSR(port);
  639. } while (!(status & AT91_US_TXRDY));
  640. UART_PUT_CHAR(port, s[i]);
  641. if (s[i] == '\n') {
  642. do {
  643. status = UART_GET_CSR(port);
  644. } while (!(status & AT91_US_TXRDY));
  645. UART_PUT_CHAR(port, '\r');
  646. }
  647. }
  648. /*
  649. * Finally, wait for transmitter to become empty
  650. * and restore IMR
  651. */
  652. do {
  653. status = UART_GET_CSR(port);
  654. } while (!(status & AT91_US_TXRDY));
  655. UART_PUT_IER(port, imr); /* set interrupts back the way they were */
  656. }
  657. /*
  658. * If the port was already initialised (eg, by a boot loader), try to determine
  659. * the current setup.
  660. */
  661. static void __init at91_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  662. {
  663. unsigned int mr, quot;
  664. // TODO: CR is a write-only register
  665. // unsigned int cr;
  666. //
  667. // cr = UART_GET_CR(port) & (AT91_US_RXEN | AT91_US_TXEN);
  668. // if (cr == (AT91_US_RXEN | AT91_US_TXEN)) {
  669. // /* ok, the port was enabled */
  670. // }
  671. mr = UART_GET_MR(port) & AT91_US_CHRL;
  672. if (mr == AT91_US_CHRL_8)
  673. *bits = 8;
  674. else
  675. *bits = 7;
  676. mr = UART_GET_MR(port) & AT91_US_PAR;
  677. if (mr == AT91_US_PAR_EVEN)
  678. *parity = 'e';
  679. else if (mr == AT91_US_PAR_ODD)
  680. *parity = 'o';
  681. quot = UART_GET_BRGR(port);
  682. *baud = port->uartclk / (16 * (quot));
  683. }
  684. static int __init at91_console_setup(struct console *co, char *options)
  685. {
  686. struct uart_port *port;
  687. int baud = 115200;
  688. int bits = 8;
  689. int parity = 'n';
  690. int flow = 'n';
  691. /*
  692. * Check whether an invalid uart number has been specified, and
  693. * if so, search for the first available port that does have
  694. * console support.
  695. */
  696. port = uart_get_console(at91_ports, AT91_NR_UART, co);
  697. /*
  698. * Enable the serial console, in-case bootloader did not do it.
  699. */
  700. at91_sys_write(AT91_PMC_PCER, 1 << port->irq); /* enable clock */
  701. UART_PUT_IDR(port, -1); /* disable interrupts */
  702. UART_PUT_CR(port, AT91_US_RSTSTA | AT91_US_RSTRX);
  703. UART_PUT_CR(port, AT91_US_TXEN | AT91_US_RXEN);
  704. if (options)
  705. uart_parse_options(options, &baud, &parity, &bits, &flow);
  706. else
  707. at91_console_get_options(port, &baud, &parity, &bits);
  708. return uart_set_options(port, co, baud, parity, bits, flow);
  709. }
  710. static struct uart_driver at91_uart;
  711. static struct console at91_console = {
  712. .name = AT91_DEVICENAME,
  713. .write = at91_console_write,
  714. .device = uart_console_device,
  715. .setup = at91_console_setup,
  716. .flags = CON_PRINTBUFFER,
  717. .index = -1,
  718. .data = &at91_uart,
  719. };
  720. #define AT91_CONSOLE_DEVICE &at91_console
  721. static int __init at91_console_init(void)
  722. {
  723. at91_init_ports();
  724. at91_console.index = at91_console_port;
  725. register_console(&at91_console);
  726. return 0;
  727. }
  728. console_initcall(at91_console_init);
  729. #else
  730. #define AT91_CONSOLE_DEVICE NULL
  731. #endif
  732. static struct uart_driver at91_uart = {
  733. .owner = THIS_MODULE,
  734. .driver_name = AT91_DEVICENAME,
  735. .dev_name = AT91_DEVICENAME,
  736. .devfs_name = AT91_DEVICENAME,
  737. .major = SERIAL_AT91_MAJOR,
  738. .minor = MINOR_START,
  739. .nr = AT91_NR_UART,
  740. .cons = AT91_CONSOLE_DEVICE,
  741. };
  742. static int __init at91_serial_init(void)
  743. {
  744. int ret, i;
  745. at91_init_ports();
  746. ret = uart_register_driver(&at91_uart);
  747. if (ret)
  748. return ret;
  749. for (i = 0; i < AT91_NR_UART; i++) {
  750. if (at91_serial_map[i] >= 0)
  751. uart_add_one_port(&at91_uart, &at91_ports[i]);
  752. }
  753. return 0;
  754. }
  755. static void __exit at91_serial_exit(void)
  756. {
  757. int i;
  758. for (i = 0; i < AT91_NR_UART; i++) {
  759. if (at91_serial_map[i] >= 0)
  760. uart_remove_one_port(&at91_uart, &at91_ports[i]);
  761. }
  762. uart_unregister_driver(&at91_uart);
  763. }
  764. module_init(at91_serial_init);
  765. module_exit(at91_serial_exit);
  766. MODULE_AUTHOR("Rick Bronson");
  767. MODULE_DESCRIPTION("AT91 generic serial port driver");
  768. MODULE_LICENSE("GPL");