atmel_serial.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*
  2. * linux/drivers/char/at91_serial.c
  3. *
  4. * Driver for Atmel AT91 / AT32 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_pdc.h>
  38. #include <asm/mach/serial_at91.h>
  39. #include <asm/arch/board.h>
  40. #ifdef CONFIG_ARM
  41. #include <asm/arch/system.h>
  42. #include <asm/arch/gpio.h>
  43. #endif
  44. #include "atmel_serial.h"
  45. #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  46. #define SUPPORT_SYSRQ
  47. #endif
  48. #include <linux/serial_core.h>
  49. #ifdef CONFIG_SERIAL_ATMEL_TTYAT
  50. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  51. * should coexist with the 8250 driver, such as if we have an external 16C550
  52. * UART. */
  53. #define SERIAL_ATMEL_MAJOR 204
  54. #define MINOR_START 154
  55. #define ATMEL_DEVICENAME "ttyAT"
  56. #else
  57. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  58. * name, but it is legally reserved for the 8250 driver. */
  59. #define SERIAL_ATMEL_MAJOR TTY_MAJOR
  60. #define MINOR_START 64
  61. #define ATMEL_DEVICENAME "ttyS"
  62. #endif
  63. #define ATMEL_ISR_PASS_LIMIT 256
  64. #define UART_PUT_CR(port,v) writel(v, (port)->membase + ATMEL_US_CR)
  65. #define UART_GET_MR(port) readl((port)->membase + ATMEL_US_MR)
  66. #define UART_PUT_MR(port,v) writel(v, (port)->membase + ATMEL_US_MR)
  67. #define UART_PUT_IER(port,v) writel(v, (port)->membase + ATMEL_US_IER)
  68. #define UART_PUT_IDR(port,v) writel(v, (port)->membase + ATMEL_US_IDR)
  69. #define UART_GET_IMR(port) readl((port)->membase + ATMEL_US_IMR)
  70. #define UART_GET_CSR(port) readl((port)->membase + ATMEL_US_CSR)
  71. #define UART_GET_CHAR(port) readl((port)->membase + ATMEL_US_RHR)
  72. #define UART_PUT_CHAR(port,v) writel(v, (port)->membase + ATMEL_US_THR)
  73. #define UART_GET_BRGR(port) readl((port)->membase + ATMEL_US_BRGR)
  74. #define UART_PUT_BRGR(port,v) writel(v, (port)->membase + ATMEL_US_BRGR)
  75. #define UART_PUT_RTOR(port,v) writel(v, (port)->membase + ATMEL_US_RTOR)
  76. // #define UART_GET_CR(port) readl((port)->membase + ATMEL_US_CR) // is write-only
  77. /* PDC registers */
  78. #define UART_PUT_PTCR(port,v) writel(v, (port)->membase + ATMEL_PDC_PTCR)
  79. #define UART_GET_PTSR(port) readl((port)->membase + ATMEL_PDC_PTSR)
  80. #define UART_PUT_RPR(port,v) writel(v, (port)->membase + ATMEL_PDC_RPR)
  81. #define UART_GET_RPR(port) readl((port)->membase + ATMEL_PDC_RPR)
  82. #define UART_PUT_RCR(port,v) writel(v, (port)->membase + ATMEL_PDC_RCR)
  83. #define UART_PUT_RNPR(port,v) writel(v, (port)->membase + ATMEL_PDC_RNPR)
  84. #define UART_PUT_RNCR(port,v) writel(v, (port)->membase + ATMEL_PDC_RNCR)
  85. #define UART_PUT_TPR(port,v) writel(v, (port)->membase + ATMEL_PDC_TPR)
  86. #define UART_PUT_TCR(port,v) writel(v, (port)->membase + ATMEL_PDC_TCR)
  87. //#define UART_PUT_TNPR(port,v) writel(v, (port)->membase + ATMEL_PDC_TNPR)
  88. //#define UART_PUT_TNCR(port,v) writel(v, (port)->membase + ATMEL_PDC_TNCR)
  89. static int (*atmel_open_hook)(struct uart_port *);
  90. static void (*atmel_close_hook)(struct uart_port *);
  91. /*
  92. * We wrap our port structure around the generic uart_port.
  93. */
  94. struct atmel_uart_port {
  95. struct uart_port uart; /* uart */
  96. struct clk *clk; /* uart clock */
  97. unsigned short suspended; /* is port suspended? */
  98. };
  99. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  100. #ifdef SUPPORT_SYSRQ
  101. static struct console atmel_console;
  102. #endif
  103. /*
  104. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  105. */
  106. static u_int atmel_tx_empty(struct uart_port *port)
  107. {
  108. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  109. }
  110. /*
  111. * Set state of the modem control output lines
  112. */
  113. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  114. {
  115. unsigned int control = 0;
  116. unsigned int mode;
  117. #ifdef CONFIG_ARM
  118. if (arch_identify() == ARCH_ID_AT91RM9200) {
  119. /*
  120. * AT91RM9200 Errata #39: RTS0 is not internally connected to PA21.
  121. * We need to drive the pin manually.
  122. */
  123. if (port->mapbase == AT91RM9200_BASE_US0) {
  124. if (mctrl & TIOCM_RTS)
  125. at91_set_gpio_value(AT91_PIN_PA21, 0);
  126. else
  127. at91_set_gpio_value(AT91_PIN_PA21, 1);
  128. }
  129. }
  130. #endif
  131. if (mctrl & TIOCM_RTS)
  132. control |= ATMEL_US_RTSEN;
  133. else
  134. control |= ATMEL_US_RTSDIS;
  135. if (mctrl & TIOCM_DTR)
  136. control |= ATMEL_US_DTREN;
  137. else
  138. control |= ATMEL_US_DTRDIS;
  139. UART_PUT_CR(port, control);
  140. /* Local loopback mode? */
  141. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  142. if (mctrl & TIOCM_LOOP)
  143. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  144. else
  145. mode |= ATMEL_US_CHMODE_NORMAL;
  146. UART_PUT_MR(port, mode);
  147. }
  148. /*
  149. * Get state of the modem control input lines
  150. */
  151. static u_int atmel_get_mctrl(struct uart_port *port)
  152. {
  153. unsigned int status, ret = 0;
  154. status = UART_GET_CSR(port);
  155. /*
  156. * The control signals are active low.
  157. */
  158. if (!(status & ATMEL_US_DCD))
  159. ret |= TIOCM_CD;
  160. if (!(status & ATMEL_US_CTS))
  161. ret |= TIOCM_CTS;
  162. if (!(status & ATMEL_US_DSR))
  163. ret |= TIOCM_DSR;
  164. if (!(status & ATMEL_US_RI))
  165. ret |= TIOCM_RI;
  166. return ret;
  167. }
  168. /*
  169. * Stop transmitting.
  170. */
  171. static void atmel_stop_tx(struct uart_port *port)
  172. {
  173. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  174. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  175. }
  176. /*
  177. * Start transmitting.
  178. */
  179. static void atmel_start_tx(struct uart_port *port)
  180. {
  181. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  182. UART_PUT_IER(port, ATMEL_US_TXRDY);
  183. }
  184. /*
  185. * Stop receiving - port is in process of being closed.
  186. */
  187. static void atmel_stop_rx(struct uart_port *port)
  188. {
  189. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  190. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  191. }
  192. /*
  193. * Enable modem status interrupts
  194. */
  195. static void atmel_enable_ms(struct uart_port *port)
  196. {
  197. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  198. }
  199. /*
  200. * Control the transmission of a break signal
  201. */
  202. static void atmel_break_ctl(struct uart_port *port, int break_state)
  203. {
  204. if (break_state != 0)
  205. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  206. else
  207. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  208. }
  209. /*
  210. * Characters received (called from interrupt handler)
  211. */
  212. static void atmel_rx_chars(struct uart_port *port)
  213. {
  214. struct tty_struct *tty = port->info->tty;
  215. unsigned int status, ch, flg;
  216. status = UART_GET_CSR(port);
  217. while (status & ATMEL_US_RXRDY) {
  218. ch = UART_GET_CHAR(port);
  219. port->icount.rx++;
  220. flg = TTY_NORMAL;
  221. /*
  222. * note that the error handling code is
  223. * out of the main execution path
  224. */
  225. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  226. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* clear error */
  227. if (status & ATMEL_US_RXBRK) {
  228. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME); /* ignore side-effect */
  229. port->icount.brk++;
  230. if (uart_handle_break(port))
  231. goto ignore_char;
  232. }
  233. if (status & ATMEL_US_PARE)
  234. port->icount.parity++;
  235. if (status & ATMEL_US_FRAME)
  236. port->icount.frame++;
  237. if (status & ATMEL_US_OVRE)
  238. port->icount.overrun++;
  239. status &= port->read_status_mask;
  240. if (status & ATMEL_US_RXBRK)
  241. flg = TTY_BREAK;
  242. else if (status & ATMEL_US_PARE)
  243. flg = TTY_PARITY;
  244. else if (status & ATMEL_US_FRAME)
  245. flg = TTY_FRAME;
  246. }
  247. if (uart_handle_sysrq_char(port, ch))
  248. goto ignore_char;
  249. uart_insert_char(port, status, ATMEL_US_OVRE, ch, flg);
  250. ignore_char:
  251. status = UART_GET_CSR(port);
  252. }
  253. tty_flip_buffer_push(tty);
  254. }
  255. /*
  256. * Transmit characters (called from interrupt handler)
  257. */
  258. static void atmel_tx_chars(struct uart_port *port)
  259. {
  260. struct circ_buf *xmit = &port->info->xmit;
  261. if (port->x_char) {
  262. UART_PUT_CHAR(port, port->x_char);
  263. port->icount.tx++;
  264. port->x_char = 0;
  265. return;
  266. }
  267. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  268. atmel_stop_tx(port);
  269. return;
  270. }
  271. while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  272. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  273. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  274. port->icount.tx++;
  275. if (uart_circ_empty(xmit))
  276. break;
  277. }
  278. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  279. uart_write_wakeup(port);
  280. if (uart_circ_empty(xmit))
  281. atmel_stop_tx(port);
  282. }
  283. /*
  284. * Interrupt handler
  285. */
  286. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  287. {
  288. struct uart_port *port = dev_id;
  289. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  290. unsigned int status, pending, pass_counter = 0;
  291. status = UART_GET_CSR(port);
  292. pending = status & UART_GET_IMR(port);
  293. while (pending) {
  294. /* Interrupt receive */
  295. if (pending & ATMEL_US_RXRDY)
  296. atmel_rx_chars(port);
  297. // TODO: All reads to CSR will clear these interrupts!
  298. if (pending & ATMEL_US_RIIC) port->icount.rng++;
  299. if (pending & ATMEL_US_DSRIC) port->icount.dsr++;
  300. if (pending & ATMEL_US_DCDIC)
  301. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  302. if (pending & ATMEL_US_CTSIC)
  303. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  304. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC | ATMEL_US_CTSIC))
  305. wake_up_interruptible(&port->info->delta_msr_wait);
  306. /* Interrupt transmit */
  307. if (pending & ATMEL_US_TXRDY)
  308. atmel_tx_chars(port);
  309. if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
  310. break;
  311. status = UART_GET_CSR(port);
  312. pending = status & UART_GET_IMR(port);
  313. }
  314. return IRQ_HANDLED;
  315. }
  316. /*
  317. * Perform initialization and enable port for reception
  318. */
  319. static int atmel_startup(struct uart_port *port)
  320. {
  321. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  322. int retval;
  323. /*
  324. * Ensure that no interrupts are enabled otherwise when
  325. * request_irq() is called we could get stuck trying to
  326. * handle an unexpected interrupt
  327. */
  328. UART_PUT_IDR(port, -1);
  329. /*
  330. * Allocate the IRQ
  331. */
  332. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED, "atmel_serial", port);
  333. if (retval) {
  334. printk("atmel_serial: atmel_startup - Can't get irq\n");
  335. return retval;
  336. }
  337. /*
  338. * If there is a specific "open" function (to register
  339. * control line interrupts)
  340. */
  341. if (atmel_open_hook) {
  342. retval = atmel_open_hook(port);
  343. if (retval) {
  344. free_irq(port->irq, port);
  345. return retval;
  346. }
  347. }
  348. /*
  349. * Finally, enable the serial port
  350. */
  351. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  352. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN); /* enable xmit & rcvr */
  353. UART_PUT_IER(port, ATMEL_US_RXRDY); /* enable receive only */
  354. return 0;
  355. }
  356. /*
  357. * Disable the port
  358. */
  359. static void atmel_shutdown(struct uart_port *port)
  360. {
  361. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  362. /*
  363. * Disable all interrupts, port and break condition.
  364. */
  365. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  366. UART_PUT_IDR(port, -1);
  367. /*
  368. * Free the interrupt
  369. */
  370. free_irq(port->irq, port);
  371. /*
  372. * If there is a specific "close" function (to unregister
  373. * control line interrupts)
  374. */
  375. if (atmel_close_hook)
  376. atmel_close_hook(port);
  377. }
  378. /*
  379. * Power / Clock management.
  380. */
  381. static void atmel_serial_pm(struct uart_port *port, unsigned int state, unsigned int oldstate)
  382. {
  383. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  384. switch (state) {
  385. case 0:
  386. /*
  387. * Enable the peripheral clock for this serial port.
  388. * This is called on uart_open() or a resume event.
  389. */
  390. clk_enable(atmel_port->clk);
  391. break;
  392. case 3:
  393. /*
  394. * Disable the peripheral clock for this serial port.
  395. * This is called on uart_close() or a suspend event.
  396. */
  397. clk_disable(atmel_port->clk);
  398. break;
  399. default:
  400. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  401. }
  402. }
  403. /*
  404. * Change the port parameters
  405. */
  406. static void atmel_set_termios(struct uart_port *port, struct termios * termios, struct termios * old)
  407. {
  408. unsigned long flags;
  409. unsigned int mode, imr, quot, baud;
  410. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  411. quot = uart_get_divisor(port, baud);
  412. /* Get current mode register */
  413. mode = UART_GET_MR(port) & ~(ATMEL_US_CHRL | ATMEL_US_NBSTOP | ATMEL_US_PAR);
  414. /* byte size */
  415. switch (termios->c_cflag & CSIZE) {
  416. case CS5:
  417. mode |= ATMEL_US_CHRL_5;
  418. break;
  419. case CS6:
  420. mode |= ATMEL_US_CHRL_6;
  421. break;
  422. case CS7:
  423. mode |= ATMEL_US_CHRL_7;
  424. break;
  425. default:
  426. mode |= ATMEL_US_CHRL_8;
  427. break;
  428. }
  429. /* stop bits */
  430. if (termios->c_cflag & CSTOPB)
  431. mode |= ATMEL_US_NBSTOP_2;
  432. /* parity */
  433. if (termios->c_cflag & PARENB) {
  434. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  435. if (termios->c_cflag & PARODD)
  436. mode |= ATMEL_US_PAR_MARK;
  437. else
  438. mode |= ATMEL_US_PAR_SPACE;
  439. }
  440. else if (termios->c_cflag & PARODD)
  441. mode |= ATMEL_US_PAR_ODD;
  442. else
  443. mode |= ATMEL_US_PAR_EVEN;
  444. }
  445. else
  446. mode |= ATMEL_US_PAR_NONE;
  447. spin_lock_irqsave(&port->lock, flags);
  448. port->read_status_mask = ATMEL_US_OVRE;
  449. if (termios->c_iflag & INPCK)
  450. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  451. if (termios->c_iflag & (BRKINT | PARMRK))
  452. port->read_status_mask |= ATMEL_US_RXBRK;
  453. /*
  454. * Characters to ignore
  455. */
  456. port->ignore_status_mask = 0;
  457. if (termios->c_iflag & IGNPAR)
  458. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  459. if (termios->c_iflag & IGNBRK) {
  460. port->ignore_status_mask |= ATMEL_US_RXBRK;
  461. /*
  462. * If we're ignoring parity and break indicators,
  463. * ignore overruns too (for real raw support).
  464. */
  465. if (termios->c_iflag & IGNPAR)
  466. port->ignore_status_mask |= ATMEL_US_OVRE;
  467. }
  468. // TODO: Ignore all characters if CREAD is set.
  469. /* update the per-port timeout */
  470. uart_update_timeout(port, termios->c_cflag, baud);
  471. /* disable interrupts and drain transmitter */
  472. imr = UART_GET_IMR(port); /* get interrupt mask */
  473. UART_PUT_IDR(port, -1); /* disable all interrupts */
  474. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) { barrier(); }
  475. /* disable receiver and transmitter */
  476. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  477. /* set the parity, stop bits and data size */
  478. UART_PUT_MR(port, mode);
  479. /* set the baud rate */
  480. UART_PUT_BRGR(port, quot);
  481. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  482. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  483. /* restore interrupts */
  484. UART_PUT_IER(port, imr);
  485. /* CTS flow-control and modem-status interrupts */
  486. if (UART_ENABLE_MS(port, termios->c_cflag))
  487. port->ops->enable_ms(port);
  488. spin_unlock_irqrestore(&port->lock, flags);
  489. }
  490. /*
  491. * Return string describing the specified port
  492. */
  493. static const char *atmel_type(struct uart_port *port)
  494. {
  495. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  496. }
  497. /*
  498. * Release the memory region(s) being used by 'port'.
  499. */
  500. static void atmel_release_port(struct uart_port *port)
  501. {
  502. struct platform_device *pdev = to_platform_device(port->dev);
  503. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  504. release_mem_region(port->mapbase, size);
  505. if (port->flags & UPF_IOREMAP) {
  506. iounmap(port->membase);
  507. port->membase = NULL;
  508. }
  509. }
  510. /*
  511. * Request the memory region(s) being used by 'port'.
  512. */
  513. static int atmel_request_port(struct uart_port *port)
  514. {
  515. struct platform_device *pdev = to_platform_device(port->dev);
  516. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  517. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  518. return -EBUSY;
  519. if (port->flags & UPF_IOREMAP) {
  520. port->membase = ioremap(port->mapbase, size);
  521. if (port->membase == NULL) {
  522. release_mem_region(port->mapbase, size);
  523. return -ENOMEM;
  524. }
  525. }
  526. return 0;
  527. }
  528. /*
  529. * Configure/autoconfigure the port.
  530. */
  531. static void atmel_config_port(struct uart_port *port, int flags)
  532. {
  533. if (flags & UART_CONFIG_TYPE) {
  534. port->type = PORT_ATMEL;
  535. atmel_request_port(port);
  536. }
  537. }
  538. /*
  539. * Verify the new serial_struct (for TIOCSSERIAL).
  540. */
  541. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  542. {
  543. int ret = 0;
  544. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  545. ret = -EINVAL;
  546. if (port->irq != ser->irq)
  547. ret = -EINVAL;
  548. if (ser->io_type != SERIAL_IO_MEM)
  549. ret = -EINVAL;
  550. if (port->uartclk / 16 != ser->baud_base)
  551. ret = -EINVAL;
  552. if ((void *)port->mapbase != ser->iomem_base)
  553. ret = -EINVAL;
  554. if (port->iobase != ser->port)
  555. ret = -EINVAL;
  556. if (ser->hub6 != 0)
  557. ret = -EINVAL;
  558. return ret;
  559. }
  560. static struct uart_ops atmel_pops = {
  561. .tx_empty = atmel_tx_empty,
  562. .set_mctrl = atmel_set_mctrl,
  563. .get_mctrl = atmel_get_mctrl,
  564. .stop_tx = atmel_stop_tx,
  565. .start_tx = atmel_start_tx,
  566. .stop_rx = atmel_stop_rx,
  567. .enable_ms = atmel_enable_ms,
  568. .break_ctl = atmel_break_ctl,
  569. .startup = atmel_startup,
  570. .shutdown = atmel_shutdown,
  571. .set_termios = atmel_set_termios,
  572. .type = atmel_type,
  573. .release_port = atmel_release_port,
  574. .request_port = atmel_request_port,
  575. .config_port = atmel_config_port,
  576. .verify_port = atmel_verify_port,
  577. .pm = atmel_serial_pm,
  578. };
  579. /*
  580. * Configure the port from the platform device resource info.
  581. */
  582. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, struct platform_device *pdev)
  583. {
  584. struct uart_port *port = &atmel_port->uart;
  585. struct atmel_uart_data *data = pdev->dev.platform_data;
  586. port->iotype = UPIO_MEM;
  587. port->flags = UPF_BOOT_AUTOCONF;
  588. port->ops = &atmel_pops;
  589. port->fifosize = 1;
  590. port->line = pdev->id;
  591. port->dev = &pdev->dev;
  592. port->mapbase = pdev->resource[0].start;
  593. port->irq = pdev->resource[1].start;
  594. if (data->regs)
  595. /* Already mapped by setup code */
  596. port->membase = data->regs;
  597. else {
  598. port->flags |= UPF_IOREMAP;
  599. port->membase = NULL;
  600. }
  601. if (!atmel_port->clk) { /* for console, the clock could already be configured */
  602. atmel_port->clk = clk_get(&pdev->dev, "usart");
  603. clk_enable(atmel_port->clk);
  604. port->uartclk = clk_get_rate(atmel_port->clk);
  605. }
  606. }
  607. /*
  608. * Register board-specific modem-control line handlers.
  609. */
  610. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  611. {
  612. if (fns->enable_ms)
  613. atmel_pops.enable_ms = fns->enable_ms;
  614. if (fns->get_mctrl)
  615. atmel_pops.get_mctrl = fns->get_mctrl;
  616. if (fns->set_mctrl)
  617. atmel_pops.set_mctrl = fns->set_mctrl;
  618. atmel_open_hook = fns->open;
  619. atmel_close_hook = fns->close;
  620. atmel_pops.pm = fns->pm;
  621. atmel_pops.set_wake = fns->set_wake;
  622. }
  623. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  624. static void atmel_console_putchar(struct uart_port *port, int ch)
  625. {
  626. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  627. barrier();
  628. UART_PUT_CHAR(port, ch);
  629. }
  630. /*
  631. * Interrupts are disabled on entering
  632. */
  633. static void atmel_console_write(struct console *co, const char *s, u_int count)
  634. {
  635. struct uart_port *port = &atmel_ports[co->index].uart;
  636. unsigned int status, imr;
  637. /*
  638. * First, save IMR and then disable interrupts
  639. */
  640. imr = UART_GET_IMR(port); /* get interrupt mask */
  641. UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
  642. uart_console_write(port, s, count, atmel_console_putchar);
  643. /*
  644. * Finally, wait for transmitter to become empty
  645. * and restore IMR
  646. */
  647. do {
  648. status = UART_GET_CSR(port);
  649. } while (!(status & ATMEL_US_TXRDY));
  650. UART_PUT_IER(port, imr); /* set interrupts back the way they were */
  651. }
  652. /*
  653. * If the port was already initialised (eg, by a boot loader), try to determine
  654. * the current setup.
  655. */
  656. static void __init atmel_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  657. {
  658. unsigned int mr, quot;
  659. // TODO: CR is a write-only register
  660. // unsigned int cr;
  661. //
  662. // cr = UART_GET_CR(port) & (ATMEL_US_RXEN | ATMEL_US_TXEN);
  663. // if (cr == (ATMEL_US_RXEN | ATMEL_US_TXEN)) {
  664. // /* ok, the port was enabled */
  665. // }
  666. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  667. if (mr == ATMEL_US_CHRL_8)
  668. *bits = 8;
  669. else
  670. *bits = 7;
  671. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  672. if (mr == ATMEL_US_PAR_EVEN)
  673. *parity = 'e';
  674. else if (mr == ATMEL_US_PAR_ODD)
  675. *parity = 'o';
  676. /*
  677. * The serial core only rounds down when matching this to a
  678. * supported baud rate. Make sure we don't end up slightly
  679. * lower than one of those, as it would make us fall through
  680. * to a much lower baud rate than we really want.
  681. */
  682. quot = UART_GET_BRGR(port);
  683. *baud = port->uartclk / (16 * (quot - 1));
  684. }
  685. static int __init atmel_console_setup(struct console *co, char *options)
  686. {
  687. struct uart_port *port = &atmel_ports[co->index].uart;
  688. int baud = 115200;
  689. int bits = 8;
  690. int parity = 'n';
  691. int flow = 'n';
  692. if (port->membase == 0) /* Port not initialized yet - delay setup */
  693. return -ENODEV;
  694. UART_PUT_IDR(port, -1); /* disable interrupts */
  695. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  696. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  697. if (options)
  698. uart_parse_options(options, &baud, &parity, &bits, &flow);
  699. else
  700. atmel_console_get_options(port, &baud, &parity, &bits);
  701. return uart_set_options(port, co, baud, parity, bits, flow);
  702. }
  703. static struct uart_driver atmel_uart;
  704. static struct console atmel_console = {
  705. .name = ATMEL_DEVICENAME,
  706. .write = atmel_console_write,
  707. .device = uart_console_device,
  708. .setup = atmel_console_setup,
  709. .flags = CON_PRINTBUFFER,
  710. .index = -1,
  711. .data = &atmel_uart,
  712. };
  713. #define ATMEL_CONSOLE_DEVICE &atmel_console
  714. /*
  715. * Early console initialization (before VM subsystem initialized).
  716. */
  717. static int __init atmel_console_init(void)
  718. {
  719. if (atmel_default_console_device) {
  720. add_preferred_console(ATMEL_DEVICENAME, atmel_default_console_device->id, NULL);
  721. atmel_init_port(&(atmel_ports[atmel_default_console_device->id]), atmel_default_console_device);
  722. register_console(&atmel_console);
  723. }
  724. return 0;
  725. }
  726. console_initcall(atmel_console_init);
  727. /*
  728. * Late console initialization.
  729. */
  730. static int __init atmel_late_console_init(void)
  731. {
  732. if (atmel_default_console_device && !(atmel_console.flags & CON_ENABLED))
  733. register_console(&atmel_console);
  734. return 0;
  735. }
  736. core_initcall(atmel_late_console_init);
  737. #else
  738. #define ATMEL_CONSOLE_DEVICE NULL
  739. #endif
  740. static struct uart_driver atmel_uart = {
  741. .owner = THIS_MODULE,
  742. .driver_name = "atmel_serial",
  743. .dev_name = ATMEL_DEVICENAME,
  744. .major = SERIAL_ATMEL_MAJOR,
  745. .minor = MINOR_START,
  746. .nr = ATMEL_MAX_UART,
  747. .cons = ATMEL_CONSOLE_DEVICE,
  748. };
  749. #ifdef CONFIG_PM
  750. static int atmel_serial_suspend(struct platform_device *pdev, pm_message_t state)
  751. {
  752. struct uart_port *port = platform_get_drvdata(pdev);
  753. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  754. if (device_may_wakeup(&pdev->dev) && !at91_suspend_entering_slow_clock())
  755. enable_irq_wake(port->irq);
  756. else {
  757. disable_irq_wake(port->irq);
  758. uart_suspend_port(&atmel_uart, port);
  759. atmel_port->suspended = 1;
  760. }
  761. return 0;
  762. }
  763. static int atmel_serial_resume(struct platform_device *pdev)
  764. {
  765. struct uart_port *port = platform_get_drvdata(pdev);
  766. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  767. if (atmel_port->suspended) {
  768. uart_resume_port(&atmel_uart, port);
  769. atmel_port->suspended = 0;
  770. }
  771. return 0;
  772. }
  773. #else
  774. #define atmel_serial_suspend NULL
  775. #define atmel_serial_resume NULL
  776. #endif
  777. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  778. {
  779. struct atmel_uart_port *port;
  780. int ret;
  781. port = &atmel_ports[pdev->id];
  782. atmel_init_port(port, pdev);
  783. ret = uart_add_one_port(&atmel_uart, &port->uart);
  784. if (!ret) {
  785. device_init_wakeup(&pdev->dev, 1);
  786. platform_set_drvdata(pdev, port);
  787. }
  788. return ret;
  789. }
  790. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  791. {
  792. struct uart_port *port = platform_get_drvdata(pdev);
  793. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  794. int ret = 0;
  795. clk_disable(atmel_port->clk);
  796. clk_put(atmel_port->clk);
  797. device_init_wakeup(&pdev->dev, 0);
  798. platform_set_drvdata(pdev, NULL);
  799. if (port) {
  800. ret = uart_remove_one_port(&atmel_uart, port);
  801. kfree(port);
  802. }
  803. return ret;
  804. }
  805. static struct platform_driver atmel_serial_driver = {
  806. .probe = atmel_serial_probe,
  807. .remove = __devexit_p(atmel_serial_remove),
  808. .suspend = atmel_serial_suspend,
  809. .resume = atmel_serial_resume,
  810. .driver = {
  811. .name = "atmel_usart",
  812. .owner = THIS_MODULE,
  813. },
  814. };
  815. static int __init atmel_serial_init(void)
  816. {
  817. int ret;
  818. ret = uart_register_driver(&atmel_uart);
  819. if (ret)
  820. return ret;
  821. ret = platform_driver_register(&atmel_serial_driver);
  822. if (ret)
  823. uart_unregister_driver(&atmel_uart);
  824. return ret;
  825. }
  826. static void __exit atmel_serial_exit(void)
  827. {
  828. platform_driver_unregister(&atmel_serial_driver);
  829. uart_unregister_driver(&atmel_uart);
  830. }
  831. module_init(atmel_serial_init);
  832. module_exit(atmel_serial_exit);
  833. MODULE_AUTHOR("Rick Bronson");
  834. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  835. MODULE_LICENSE("GPL");