at91_serial.c 24 KB

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