at91_serial.c 24 KB

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