at91_serial.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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. port->icount.rx++;
  191. flg = TTY_NORMAL;
  192. /*
  193. * note that the error handling code is
  194. * out of the main execution path
  195. */
  196. if (unlikely(status & (AT91_US_PARE | AT91_US_FRAME | AT91_US_OVRE))) {
  197. UART_PUT_CR(port, AT91_US_RSTSTA); /* clear error */
  198. if (status & (AT91_US_PARE))
  199. port->icount.parity++;
  200. if (status & (AT91_US_FRAME))
  201. port->icount.frame++;
  202. if (status & (AT91_US_OVRE))
  203. port->icount.overrun++;
  204. if (status & AT91_US_PARE)
  205. flg = TTY_PARITY;
  206. else if (status & AT91_US_FRAME)
  207. flg = TTY_FRAME;
  208. if (status & AT91_US_OVRE) {
  209. /*
  210. * overrun does *not* affect the character
  211. * we read from the FIFO
  212. */
  213. tty_insert_flip_char(tty, ch, flg);
  214. ch = 0;
  215. flg = TTY_OVERRUN;
  216. }
  217. #ifdef SUPPORT_SYSRQ
  218. port->sysrq = 0;
  219. #endif
  220. }
  221. if (uart_handle_sysrq_char(port, ch, regs))
  222. goto ignore_char;
  223. tty_insert_flip_char(tty, ch, flg);
  224. ignore_char:
  225. status = UART_GET_CSR(port) & port->read_status_mask;
  226. }
  227. tty_flip_buffer_push(tty);
  228. }
  229. /*
  230. * Transmit characters (called from interrupt handler)
  231. */
  232. static void at91_tx_chars(struct uart_port *port)
  233. {
  234. struct circ_buf *xmit = &port->info->xmit;
  235. if (port->x_char) {
  236. UART_PUT_CHAR(port, port->x_char);
  237. port->icount.tx++;
  238. port->x_char = 0;
  239. return;
  240. }
  241. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  242. at91_stop_tx(port);
  243. return;
  244. }
  245. while (UART_GET_CSR(port) & AT91_US_TXRDY) {
  246. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  247. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  248. port->icount.tx++;
  249. if (uart_circ_empty(xmit))
  250. break;
  251. }
  252. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  253. uart_write_wakeup(port);
  254. if (uart_circ_empty(xmit))
  255. at91_stop_tx(port);
  256. }
  257. /*
  258. * Interrupt handler
  259. */
  260. static irqreturn_t at91_interrupt(int irq, void *dev_id, struct pt_regs *regs)
  261. {
  262. struct uart_port *port = dev_id;
  263. unsigned int status, pending, pass_counter = 0;
  264. status = UART_GET_CSR(port);
  265. pending = status & port->read_status_mask;
  266. if (pending) {
  267. do {
  268. if (pending & AT91_US_RXRDY)
  269. at91_rx_chars(port, regs);
  270. /* Clear the relevent break bits */
  271. if (pending & AT91_US_RXBRK) {
  272. UART_PUT_CR(port, AT91_US_RSTSTA);
  273. port->icount.brk++;
  274. uart_handle_break(port);
  275. }
  276. // TODO: All reads to CSR will clear these interrupts!
  277. if (pending & AT91_US_RIIC) port->icount.rng++;
  278. if (pending & AT91_US_DSRIC) port->icount.dsr++;
  279. if (pending & AT91_US_DCDIC)
  280. uart_handle_dcd_change(port, !(status & AT91_US_DCD));
  281. if (pending & AT91_US_CTSIC)
  282. uart_handle_cts_change(port, !(status & AT91_US_CTS));
  283. if (pending & (AT91_US_RIIC | AT91_US_DSRIC | AT91_US_DCDIC | AT91_US_CTSIC))
  284. wake_up_interruptible(&port->info->delta_msr_wait);
  285. if (pending & AT91_US_TXRDY)
  286. at91_tx_chars(port);
  287. if (pass_counter++ > AT91_ISR_PASS_LIMIT)
  288. break;
  289. status = UART_GET_CSR(port);
  290. pending = status & port->read_status_mask;
  291. } while (pending);
  292. }
  293. return IRQ_HANDLED;
  294. }
  295. /*
  296. * Perform initialization and enable port for reception
  297. */
  298. static int at91_startup(struct uart_port *port)
  299. {
  300. int retval;
  301. /*
  302. * Ensure that no interrupts are enabled otherwise when
  303. * request_irq() is called we could get stuck trying to
  304. * handle an unexpected interrupt
  305. */
  306. UART_PUT_IDR(port, -1);
  307. /*
  308. * Allocate the IRQ
  309. */
  310. retval = request_irq(port->irq, at91_interrupt, SA_SHIRQ, "at91_serial", port);
  311. if (retval) {
  312. printk("at91_serial: at91_startup - Can't get irq\n");
  313. return retval;
  314. }
  315. /*
  316. * If there is a specific "open" function (to register
  317. * control line interrupts)
  318. */
  319. if (at91_open) {
  320. retval = at91_open(port);
  321. if (retval) {
  322. free_irq(port->irq, port);
  323. return retval;
  324. }
  325. }
  326. port->read_status_mask = AT91_US_RXRDY | AT91_US_TXRDY | AT91_US_OVRE
  327. | AT91_US_FRAME | AT91_US_PARE | AT91_US_RXBRK;
  328. /*
  329. * Finally, enable the serial port
  330. */
  331. UART_PUT_CR(port, AT91_US_RSTSTA | AT91_US_RSTRX);
  332. UART_PUT_CR(port, AT91_US_TXEN | AT91_US_RXEN); /* enable xmit & rcvr */
  333. UART_PUT_IER(port, AT91_US_RXRDY); /* do receive only */
  334. return 0;
  335. }
  336. /*
  337. * Disable the port
  338. */
  339. static void at91_shutdown(struct uart_port *port)
  340. {
  341. /*
  342. * Disable all interrupts, port and break condition.
  343. */
  344. UART_PUT_CR(port, AT91_US_RSTSTA);
  345. UART_PUT_IDR(port, -1);
  346. /*
  347. * Free the interrupt
  348. */
  349. free_irq(port->irq, port);
  350. /*
  351. * If there is a specific "close" function (to unregister
  352. * control line interrupts)
  353. */
  354. if (at91_close)
  355. at91_close(port);
  356. }
  357. /*
  358. * Power / Clock management.
  359. */
  360. static void at91_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  361. {
  362. switch (state) {
  363. case 0:
  364. /*
  365. * Enable the peripheral clock for this serial port.
  366. * This is called on uart_open() or a resume event.
  367. */
  368. at91_sys_write(AT91_PMC_PCER, 1 << port->irq);
  369. break;
  370. case 3:
  371. /*
  372. * Disable the peripheral clock for this serial port.
  373. * This is called on uart_close() or a suspend event.
  374. */
  375. if (port->irq != AT91_ID_SYS) /* is this a shared clock? */
  376. at91_sys_write(AT91_PMC_PCDR, 1 << port->irq);
  377. break;
  378. default:
  379. printk(KERN_ERR "at91_serial: unknown pm %d\n", state);
  380. }
  381. }
  382. /*
  383. * Change the port parameters
  384. */
  385. static void at91_set_termios(struct uart_port *port, struct termios * termios, struct termios * old)
  386. {
  387. unsigned long flags;
  388. unsigned int mode, imr, quot, baud;
  389. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  390. quot = uart_get_divisor(port, baud);
  391. /* Get current mode register */
  392. mode = UART_GET_MR(port) & ~(AT91_US_CHRL | AT91_US_NBSTOP | AT91_US_PAR);
  393. /* byte size */
  394. switch (termios->c_cflag & CSIZE) {
  395. case CS5:
  396. mode |= AT91_US_CHRL_5;
  397. break;
  398. case CS6:
  399. mode |= AT91_US_CHRL_6;
  400. break;
  401. case CS7:
  402. mode |= AT91_US_CHRL_7;
  403. break;
  404. default:
  405. mode |= AT91_US_CHRL_8;
  406. break;
  407. }
  408. /* stop bits */
  409. if (termios->c_cflag & CSTOPB)
  410. mode |= AT91_US_NBSTOP_2;
  411. /* parity */
  412. if (termios->c_cflag & PARENB) {
  413. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  414. if (termios->c_cflag & PARODD)
  415. mode |= AT91_US_PAR_MARK;
  416. else
  417. mode |= AT91_US_PAR_SPACE;
  418. }
  419. else if (termios->c_cflag & PARODD)
  420. mode |= AT91_US_PAR_ODD;
  421. else
  422. mode |= AT91_US_PAR_EVEN;
  423. }
  424. else
  425. mode |= AT91_US_PAR_NONE;
  426. spin_lock_irqsave(&port->lock, flags);
  427. port->read_status_mask |= AT91_US_OVRE;
  428. if (termios->c_iflag & INPCK)
  429. port->read_status_mask |= AT91_US_FRAME | AT91_US_PARE;
  430. if (termios->c_iflag & (BRKINT | PARMRK))
  431. port->read_status_mask |= AT91_US_RXBRK;
  432. /*
  433. * Characters to ignore
  434. */
  435. port->ignore_status_mask = 0;
  436. if (termios->c_iflag & IGNPAR)
  437. port->ignore_status_mask |= (AT91_US_FRAME | AT91_US_PARE);
  438. if (termios->c_iflag & IGNBRK) {
  439. port->ignore_status_mask |= AT91_US_RXBRK;
  440. /*
  441. * If we're ignoring parity and break indicators,
  442. * ignore overruns too (for real raw support).
  443. */
  444. if (termios->c_iflag & IGNPAR)
  445. port->ignore_status_mask |= AT91_US_OVRE;
  446. }
  447. // TODO: Ignore all characters if CREAD is set.
  448. /* update the per-port timeout */
  449. uart_update_timeout(port, termios->c_cflag, baud);
  450. /* disable interrupts and drain transmitter */
  451. imr = UART_GET_IMR(port); /* get interrupt mask */
  452. UART_PUT_IDR(port, -1); /* disable all interrupts */
  453. while (!(UART_GET_CSR(port) & AT91_US_TXEMPTY)) { barrier(); }
  454. /* disable receiver and transmitter */
  455. UART_PUT_CR(port, AT91_US_TXDIS | AT91_US_RXDIS);
  456. /* set the parity, stop bits and data size */
  457. UART_PUT_MR(port, mode);
  458. /* set the baud rate */
  459. UART_PUT_BRGR(port, quot);
  460. UART_PUT_CR(port, AT91_US_RSTSTA | AT91_US_RSTRX);
  461. UART_PUT_CR(port, AT91_US_TXEN | AT91_US_RXEN);
  462. /* restore interrupts */
  463. UART_PUT_IER(port, imr);
  464. /* CTS flow-control and modem-status interrupts */
  465. if (UART_ENABLE_MS(port, termios->c_cflag))
  466. port->ops->enable_ms(port);
  467. spin_unlock_irqrestore(&port->lock, flags);
  468. }
  469. /*
  470. * Return string describing the specified port
  471. */
  472. static const char *at91_type(struct uart_port *port)
  473. {
  474. return (port->type == PORT_AT91RM9200) ? "AT91_SERIAL" : NULL;
  475. }
  476. /*
  477. * Release the memory region(s) being used by 'port'.
  478. */
  479. static void at91_release_port(struct uart_port *port)
  480. {
  481. release_mem_region(port->mapbase,
  482. (port->mapbase == AT91_VA_BASE_DBGU) ? 512 : SZ_16K);
  483. }
  484. /*
  485. * Request the memory region(s) being used by 'port'.
  486. */
  487. static int at91_request_port(struct uart_port *port)
  488. {
  489. return request_mem_region(port->mapbase,
  490. (port->mapbase == AT91_VA_BASE_DBGU) ? 512 : SZ_16K,
  491. "at91_serial") != NULL ? 0 : -EBUSY;
  492. }
  493. /*
  494. * Configure/autoconfigure the port.
  495. */
  496. static void at91_config_port(struct uart_port *port, int flags)
  497. {
  498. if (flags & UART_CONFIG_TYPE) {
  499. port->type = PORT_AT91RM9200;
  500. at91_request_port(port);
  501. }
  502. }
  503. /*
  504. * Verify the new serial_struct (for TIOCSSERIAL).
  505. */
  506. static int at91_verify_port(struct uart_port *port, struct serial_struct *ser)
  507. {
  508. int ret = 0;
  509. if (ser->type != PORT_UNKNOWN && ser->type != PORT_AT91RM9200)
  510. ret = -EINVAL;
  511. if (port->irq != ser->irq)
  512. ret = -EINVAL;
  513. if (ser->io_type != SERIAL_IO_MEM)
  514. ret = -EINVAL;
  515. if (port->uartclk / 16 != ser->baud_base)
  516. ret = -EINVAL;
  517. if ((void *)port->mapbase != ser->iomem_base)
  518. ret = -EINVAL;
  519. if (port->iobase != ser->port)
  520. ret = -EINVAL;
  521. if (ser->hub6 != 0)
  522. ret = -EINVAL;
  523. return ret;
  524. }
  525. static struct uart_ops at91_pops = {
  526. .tx_empty = at91_tx_empty,
  527. .set_mctrl = at91_set_mctrl,
  528. .get_mctrl = at91_get_mctrl,
  529. .stop_tx = at91_stop_tx,
  530. .start_tx = at91_start_tx,
  531. .stop_rx = at91_stop_rx,
  532. .enable_ms = at91_enable_ms,
  533. .break_ctl = at91_break_ctl,
  534. .startup = at91_startup,
  535. .shutdown = at91_shutdown,
  536. .set_termios = at91_set_termios,
  537. .type = at91_type,
  538. .release_port = at91_release_port,
  539. .request_port = at91_request_port,
  540. .config_port = at91_config_port,
  541. .verify_port = at91_verify_port,
  542. .pm = at91_serial_pm,
  543. };
  544. static struct uart_port at91_ports[AT91_NR_UART];
  545. void __init at91_init_ports(void)
  546. {
  547. static int first = 1;
  548. int i;
  549. if (!first)
  550. return;
  551. first = 0;
  552. for (i = 0; i < AT91_NR_UART; i++) {
  553. at91_ports[i].iotype = UPIO_MEM;
  554. at91_ports[i].flags = UPF_BOOT_AUTOCONF;
  555. at91_ports[i].uartclk = at91_master_clock;
  556. at91_ports[i].ops = &at91_pops;
  557. at91_ports[i].fifosize = 1;
  558. at91_ports[i].line = i;
  559. }
  560. }
  561. void __init at91_register_uart_fns(struct at91rm9200_port_fns *fns)
  562. {
  563. if (fns->enable_ms)
  564. at91_pops.enable_ms = fns->enable_ms;
  565. if (fns->get_mctrl)
  566. at91_pops.get_mctrl = fns->get_mctrl;
  567. if (fns->set_mctrl)
  568. at91_pops.set_mctrl = fns->set_mctrl;
  569. at91_open = fns->open;
  570. at91_close = fns->close;
  571. at91_pops.pm = fns->pm;
  572. at91_pops.set_wake = fns->set_wake;
  573. }
  574. /*
  575. * Setup ports.
  576. */
  577. void __init at91_register_uart(int idx, int port)
  578. {
  579. if ((idx < 0) || (idx >= AT91_NR_UART)) {
  580. printk(KERN_ERR "%s: bad index number %d\n", __FUNCTION__, idx);
  581. return;
  582. }
  583. switch (port) {
  584. case 0:
  585. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US0;
  586. at91_ports[idx].mapbase = AT91_VA_BASE_US0;
  587. at91_ports[idx].irq = AT91_ID_US0;
  588. AT91_CfgPIO_USART0();
  589. break;
  590. case 1:
  591. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US1;
  592. at91_ports[idx].mapbase = AT91_VA_BASE_US1;
  593. at91_ports[idx].irq = AT91_ID_US1;
  594. AT91_CfgPIO_USART1();
  595. break;
  596. case 2:
  597. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US2;
  598. at91_ports[idx].mapbase = AT91_VA_BASE_US2;
  599. at91_ports[idx].irq = AT91_ID_US2;
  600. AT91_CfgPIO_USART2();
  601. break;
  602. case 3:
  603. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_US3;
  604. at91_ports[idx].mapbase = AT91_VA_BASE_US3;
  605. at91_ports[idx].irq = AT91_ID_US3;
  606. AT91_CfgPIO_USART3();
  607. break;
  608. case 4:
  609. at91_ports[idx].membase = (void __iomem *) AT91_VA_BASE_DBGU;
  610. at91_ports[idx].mapbase = AT91_VA_BASE_DBGU;
  611. at91_ports[idx].irq = AT91_ID_SYS;
  612. AT91_CfgPIO_DBGU();
  613. break;
  614. default:
  615. printk(KERN_ERR "%s : bad port number %d\n", __FUNCTION__, port);
  616. }
  617. }
  618. #ifdef CONFIG_SERIAL_AT91_CONSOLE
  619. static void at91_console_putchar(struct uart_port *port, int ch)
  620. {
  621. while (!(UART_GET_CSR(port) & AT91_US_TXRDY))
  622. barrier();
  623. UART_PUT_CHAR(port, ch);
  624. }
  625. /*
  626. * Interrupts are disabled on entering
  627. */
  628. static void at91_console_write(struct console *co, const char *s, u_int count)
  629. {
  630. struct uart_port *port = at91_ports + co->index;
  631. unsigned int status, imr;
  632. /*
  633. * First, save IMR and then disable interrupts
  634. */
  635. imr = UART_GET_IMR(port); /* get interrupt mask */
  636. UART_PUT_IDR(port, AT91_US_RXRDY | AT91_US_TXRDY);
  637. uart_console_write(port, s, count, at91_console_putchar);
  638. /*
  639. * Finally, wait for transmitter to become empty
  640. * and restore IMR
  641. */
  642. do {
  643. status = UART_GET_CSR(port);
  644. } while (!(status & AT91_US_TXRDY));
  645. UART_PUT_IER(port, imr); /* set interrupts back the way they were */
  646. }
  647. /*
  648. * If the port was already initialised (eg, by a boot loader), try to determine
  649. * the current setup.
  650. */
  651. static void __init at91_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  652. {
  653. unsigned int mr, quot;
  654. // TODO: CR is a write-only register
  655. // unsigned int cr;
  656. //
  657. // cr = UART_GET_CR(port) & (AT91_US_RXEN | AT91_US_TXEN);
  658. // if (cr == (AT91_US_RXEN | AT91_US_TXEN)) {
  659. // /* ok, the port was enabled */
  660. // }
  661. mr = UART_GET_MR(port) & AT91_US_CHRL;
  662. if (mr == AT91_US_CHRL_8)
  663. *bits = 8;
  664. else
  665. *bits = 7;
  666. mr = UART_GET_MR(port) & AT91_US_PAR;
  667. if (mr == AT91_US_PAR_EVEN)
  668. *parity = 'e';
  669. else if (mr == AT91_US_PAR_ODD)
  670. *parity = 'o';
  671. quot = UART_GET_BRGR(port);
  672. *baud = port->uartclk / (16 * (quot));
  673. }
  674. static int __init at91_console_setup(struct console *co, char *options)
  675. {
  676. struct uart_port *port;
  677. int baud = 115200;
  678. int bits = 8;
  679. int parity = 'n';
  680. int flow = 'n';
  681. /*
  682. * Check whether an invalid uart number has been specified, and
  683. * if so, search for the first available port that does have
  684. * console support.
  685. */
  686. port = uart_get_console(at91_ports, AT91_NR_UART, co);
  687. /*
  688. * Enable the serial console, in-case bootloader did not do it.
  689. */
  690. at91_sys_write(AT91_PMC_PCER, 1 << port->irq); /* enable clock */
  691. UART_PUT_IDR(port, -1); /* disable interrupts */
  692. UART_PUT_CR(port, AT91_US_RSTSTA | AT91_US_RSTRX);
  693. UART_PUT_CR(port, AT91_US_TXEN | AT91_US_RXEN);
  694. if (options)
  695. uart_parse_options(options, &baud, &parity, &bits, &flow);
  696. else
  697. at91_console_get_options(port, &baud, &parity, &bits);
  698. return uart_set_options(port, co, baud, parity, bits, flow);
  699. }
  700. static struct uart_driver at91_uart;
  701. static struct console at91_console = {
  702. .name = AT91_DEVICENAME,
  703. .write = at91_console_write,
  704. .device = uart_console_device,
  705. .setup = at91_console_setup,
  706. .flags = CON_PRINTBUFFER,
  707. .index = -1,
  708. .data = &at91_uart,
  709. };
  710. #define AT91_CONSOLE_DEVICE &at91_console
  711. static int __init at91_console_init(void)
  712. {
  713. at91_init_ports();
  714. at91_console.index = at91_console_port;
  715. register_console(&at91_console);
  716. return 0;
  717. }
  718. console_initcall(at91_console_init);
  719. #else
  720. #define AT91_CONSOLE_DEVICE NULL
  721. #endif
  722. static struct uart_driver at91_uart = {
  723. .owner = THIS_MODULE,
  724. .driver_name = AT91_DEVICENAME,
  725. .dev_name = AT91_DEVICENAME,
  726. .devfs_name = AT91_DEVICENAME,
  727. .major = SERIAL_AT91_MAJOR,
  728. .minor = MINOR_START,
  729. .nr = AT91_NR_UART,
  730. .cons = AT91_CONSOLE_DEVICE,
  731. };
  732. static int __init at91_serial_init(void)
  733. {
  734. int ret, i;
  735. at91_init_ports();
  736. ret = uart_register_driver(&at91_uart);
  737. if (ret)
  738. return ret;
  739. for (i = 0; i < AT91_NR_UART; i++) {
  740. if (at91_serial_map[i] >= 0)
  741. uart_add_one_port(&at91_uart, &at91_ports[i]);
  742. }
  743. return 0;
  744. }
  745. static void __exit at91_serial_exit(void)
  746. {
  747. int i;
  748. for (i = 0; i < AT91_NR_UART; i++) {
  749. if (at91_serial_map[i] >= 0)
  750. uart_remove_one_port(&at91_uart, &at91_ports[i]);
  751. }
  752. uart_unregister_driver(&at91_uart);
  753. }
  754. module_init(at91_serial_init);
  755. module_exit(at91_serial_exit);
  756. MODULE_AUTHOR("Rick Bronson");
  757. MODULE_DESCRIPTION("AT91 generic serial port driver");
  758. MODULE_LICENSE("GPL");