atmel_serial.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999
  1. /*
  2. * linux/drivers/char/atmel_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 <linux/atmel_pdc.h>
  37. #include <asm/io.h>
  38. #include <asm/mach/serial_at91.h>
  39. #include <asm/arch/board.h>
  40. #ifdef CONFIG_ARM
  41. #include <asm/arch/cpu.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) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  65. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  66. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  67. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  68. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  69. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  70. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  71. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  72. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  73. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  74. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  75. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  76. // #define UART_GET_CR(port) __raw_readl((port)->membase + ATMEL_US_CR) // is write-only
  77. /* PDC registers */
  78. #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
  79. #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
  80. #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
  81. #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
  82. #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
  83. #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
  84. #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
  85. #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
  86. #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
  87. //#define UART_PUT_TNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TNPR)
  88. //#define UART_PUT_TNCR(port,v) __raw_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_ARCH_AT91RM9200
  118. if (cpu_is_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 ktermios * termios, struct ktermios * old)
  407. {
  408. unsigned long flags;
  409. unsigned int mode, imr, quot, baud;
  410. /* Get current mode register */
  411. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL | ATMEL_US_NBSTOP | ATMEL_US_PAR);
  412. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
  413. quot = uart_get_divisor(port, baud);
  414. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  415. quot /= 8;
  416. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  417. }
  418. /* byte size */
  419. switch (termios->c_cflag & CSIZE) {
  420. case CS5:
  421. mode |= ATMEL_US_CHRL_5;
  422. break;
  423. case CS6:
  424. mode |= ATMEL_US_CHRL_6;
  425. break;
  426. case CS7:
  427. mode |= ATMEL_US_CHRL_7;
  428. break;
  429. default:
  430. mode |= ATMEL_US_CHRL_8;
  431. break;
  432. }
  433. /* stop bits */
  434. if (termios->c_cflag & CSTOPB)
  435. mode |= ATMEL_US_NBSTOP_2;
  436. /* parity */
  437. if (termios->c_cflag & PARENB) {
  438. if (termios->c_cflag & CMSPAR) { /* Mark or Space parity */
  439. if (termios->c_cflag & PARODD)
  440. mode |= ATMEL_US_PAR_MARK;
  441. else
  442. mode |= ATMEL_US_PAR_SPACE;
  443. }
  444. else if (termios->c_cflag & PARODD)
  445. mode |= ATMEL_US_PAR_ODD;
  446. else
  447. mode |= ATMEL_US_PAR_EVEN;
  448. }
  449. else
  450. mode |= ATMEL_US_PAR_NONE;
  451. spin_lock_irqsave(&port->lock, flags);
  452. port->read_status_mask = ATMEL_US_OVRE;
  453. if (termios->c_iflag & INPCK)
  454. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  455. if (termios->c_iflag & (BRKINT | PARMRK))
  456. port->read_status_mask |= ATMEL_US_RXBRK;
  457. /*
  458. * Characters to ignore
  459. */
  460. port->ignore_status_mask = 0;
  461. if (termios->c_iflag & IGNPAR)
  462. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  463. if (termios->c_iflag & IGNBRK) {
  464. port->ignore_status_mask |= ATMEL_US_RXBRK;
  465. /*
  466. * If we're ignoring parity and break indicators,
  467. * ignore overruns too (for real raw support).
  468. */
  469. if (termios->c_iflag & IGNPAR)
  470. port->ignore_status_mask |= ATMEL_US_OVRE;
  471. }
  472. // TODO: Ignore all characters if CREAD is set.
  473. /* update the per-port timeout */
  474. uart_update_timeout(port, termios->c_cflag, baud);
  475. /* disable interrupts and drain transmitter */
  476. imr = UART_GET_IMR(port); /* get interrupt mask */
  477. UART_PUT_IDR(port, -1); /* disable all interrupts */
  478. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY)) { barrier(); }
  479. /* disable receiver and transmitter */
  480. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  481. /* set the parity, stop bits and data size */
  482. UART_PUT_MR(port, mode);
  483. /* set the baud rate */
  484. UART_PUT_BRGR(port, quot);
  485. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  486. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  487. /* restore interrupts */
  488. UART_PUT_IER(port, imr);
  489. /* CTS flow-control and modem-status interrupts */
  490. if (UART_ENABLE_MS(port, termios->c_cflag))
  491. port->ops->enable_ms(port);
  492. spin_unlock_irqrestore(&port->lock, flags);
  493. }
  494. /*
  495. * Return string describing the specified port
  496. */
  497. static const char *atmel_type(struct uart_port *port)
  498. {
  499. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  500. }
  501. /*
  502. * Release the memory region(s) being used by 'port'.
  503. */
  504. static void atmel_release_port(struct uart_port *port)
  505. {
  506. struct platform_device *pdev = to_platform_device(port->dev);
  507. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  508. release_mem_region(port->mapbase, size);
  509. if (port->flags & UPF_IOREMAP) {
  510. iounmap(port->membase);
  511. port->membase = NULL;
  512. }
  513. }
  514. /*
  515. * Request the memory region(s) being used by 'port'.
  516. */
  517. static int atmel_request_port(struct uart_port *port)
  518. {
  519. struct platform_device *pdev = to_platform_device(port->dev);
  520. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  521. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  522. return -EBUSY;
  523. if (port->flags & UPF_IOREMAP) {
  524. port->membase = ioremap(port->mapbase, size);
  525. if (port->membase == NULL) {
  526. release_mem_region(port->mapbase, size);
  527. return -ENOMEM;
  528. }
  529. }
  530. return 0;
  531. }
  532. /*
  533. * Configure/autoconfigure the port.
  534. */
  535. static void atmel_config_port(struct uart_port *port, int flags)
  536. {
  537. if (flags & UART_CONFIG_TYPE) {
  538. port->type = PORT_ATMEL;
  539. atmel_request_port(port);
  540. }
  541. }
  542. /*
  543. * Verify the new serial_struct (for TIOCSSERIAL).
  544. */
  545. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  546. {
  547. int ret = 0;
  548. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  549. ret = -EINVAL;
  550. if (port->irq != ser->irq)
  551. ret = -EINVAL;
  552. if (ser->io_type != SERIAL_IO_MEM)
  553. ret = -EINVAL;
  554. if (port->uartclk / 16 != ser->baud_base)
  555. ret = -EINVAL;
  556. if ((void *)port->mapbase != ser->iomem_base)
  557. ret = -EINVAL;
  558. if (port->iobase != ser->port)
  559. ret = -EINVAL;
  560. if (ser->hub6 != 0)
  561. ret = -EINVAL;
  562. return ret;
  563. }
  564. static struct uart_ops atmel_pops = {
  565. .tx_empty = atmel_tx_empty,
  566. .set_mctrl = atmel_set_mctrl,
  567. .get_mctrl = atmel_get_mctrl,
  568. .stop_tx = atmel_stop_tx,
  569. .start_tx = atmel_start_tx,
  570. .stop_rx = atmel_stop_rx,
  571. .enable_ms = atmel_enable_ms,
  572. .break_ctl = atmel_break_ctl,
  573. .startup = atmel_startup,
  574. .shutdown = atmel_shutdown,
  575. .set_termios = atmel_set_termios,
  576. .type = atmel_type,
  577. .release_port = atmel_release_port,
  578. .request_port = atmel_request_port,
  579. .config_port = atmel_config_port,
  580. .verify_port = atmel_verify_port,
  581. .pm = atmel_serial_pm,
  582. };
  583. /*
  584. * Configure the port from the platform device resource info.
  585. */
  586. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port, struct platform_device *pdev)
  587. {
  588. struct uart_port *port = &atmel_port->uart;
  589. struct atmel_uart_data *data = pdev->dev.platform_data;
  590. port->iotype = UPIO_MEM;
  591. port->flags = UPF_BOOT_AUTOCONF;
  592. port->ops = &atmel_pops;
  593. port->fifosize = 1;
  594. port->line = pdev->id;
  595. port->dev = &pdev->dev;
  596. port->mapbase = pdev->resource[0].start;
  597. port->irq = pdev->resource[1].start;
  598. if (data->regs)
  599. /* Already mapped by setup code */
  600. port->membase = data->regs;
  601. else {
  602. port->flags |= UPF_IOREMAP;
  603. port->membase = NULL;
  604. }
  605. if (!atmel_port->clk) { /* for console, the clock could already be configured */
  606. atmel_port->clk = clk_get(&pdev->dev, "usart");
  607. clk_enable(atmel_port->clk);
  608. port->uartclk = clk_get_rate(atmel_port->clk);
  609. }
  610. }
  611. /*
  612. * Register board-specific modem-control line handlers.
  613. */
  614. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  615. {
  616. if (fns->enable_ms)
  617. atmel_pops.enable_ms = fns->enable_ms;
  618. if (fns->get_mctrl)
  619. atmel_pops.get_mctrl = fns->get_mctrl;
  620. if (fns->set_mctrl)
  621. atmel_pops.set_mctrl = fns->set_mctrl;
  622. atmel_open_hook = fns->open;
  623. atmel_close_hook = fns->close;
  624. atmel_pops.pm = fns->pm;
  625. atmel_pops.set_wake = fns->set_wake;
  626. }
  627. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  628. static void atmel_console_putchar(struct uart_port *port, int ch)
  629. {
  630. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  631. barrier();
  632. UART_PUT_CHAR(port, ch);
  633. }
  634. /*
  635. * Interrupts are disabled on entering
  636. */
  637. static void atmel_console_write(struct console *co, const char *s, u_int count)
  638. {
  639. struct uart_port *port = &atmel_ports[co->index].uart;
  640. unsigned int status, imr;
  641. /*
  642. * First, save IMR and then disable interrupts
  643. */
  644. imr = UART_GET_IMR(port); /* get interrupt mask */
  645. UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
  646. uart_console_write(port, s, count, atmel_console_putchar);
  647. /*
  648. * Finally, wait for transmitter to become empty
  649. * and restore IMR
  650. */
  651. do {
  652. status = UART_GET_CSR(port);
  653. } while (!(status & ATMEL_US_TXRDY));
  654. UART_PUT_IER(port, imr); /* set interrupts back the way they were */
  655. }
  656. /*
  657. * If the port was already initialised (eg, by a boot loader), try to determine
  658. * the current setup.
  659. */
  660. static void __init atmel_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
  661. {
  662. unsigned int mr, quot;
  663. // TODO: CR is a write-only register
  664. // unsigned int cr;
  665. //
  666. // cr = UART_GET_CR(port) & (ATMEL_US_RXEN | ATMEL_US_TXEN);
  667. // if (cr == (ATMEL_US_RXEN | ATMEL_US_TXEN)) {
  668. // /* ok, the port was enabled */
  669. // }
  670. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  671. if (mr == ATMEL_US_CHRL_8)
  672. *bits = 8;
  673. else
  674. *bits = 7;
  675. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  676. if (mr == ATMEL_US_PAR_EVEN)
  677. *parity = 'e';
  678. else if (mr == ATMEL_US_PAR_ODD)
  679. *parity = 'o';
  680. /*
  681. * The serial core only rounds down when matching this to a
  682. * supported baud rate. Make sure we don't end up slightly
  683. * lower than one of those, as it would make us fall through
  684. * to a much lower baud rate than we really want.
  685. */
  686. quot = UART_GET_BRGR(port);
  687. *baud = port->uartclk / (16 * (quot - 1));
  688. }
  689. static int __init atmel_console_setup(struct console *co, char *options)
  690. {
  691. struct uart_port *port = &atmel_ports[co->index].uart;
  692. int baud = 115200;
  693. int bits = 8;
  694. int parity = 'n';
  695. int flow = 'n';
  696. if (port->membase == 0) /* Port not initialized yet - delay setup */
  697. return -ENODEV;
  698. UART_PUT_IDR(port, -1); /* disable interrupts */
  699. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  700. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  701. if (options)
  702. uart_parse_options(options, &baud, &parity, &bits, &flow);
  703. else
  704. atmel_console_get_options(port, &baud, &parity, &bits);
  705. return uart_set_options(port, co, baud, parity, bits, flow);
  706. }
  707. static struct uart_driver atmel_uart;
  708. static struct console atmel_console = {
  709. .name = ATMEL_DEVICENAME,
  710. .write = atmel_console_write,
  711. .device = uart_console_device,
  712. .setup = atmel_console_setup,
  713. .flags = CON_PRINTBUFFER,
  714. .index = -1,
  715. .data = &atmel_uart,
  716. };
  717. #define ATMEL_CONSOLE_DEVICE &atmel_console
  718. /*
  719. * Early console initialization (before VM subsystem initialized).
  720. */
  721. static int __init atmel_console_init(void)
  722. {
  723. if (atmel_default_console_device) {
  724. add_preferred_console(ATMEL_DEVICENAME, atmel_default_console_device->id, NULL);
  725. atmel_init_port(&(atmel_ports[atmel_default_console_device->id]), atmel_default_console_device);
  726. register_console(&atmel_console);
  727. }
  728. return 0;
  729. }
  730. console_initcall(atmel_console_init);
  731. /*
  732. * Late console initialization.
  733. */
  734. static int __init atmel_late_console_init(void)
  735. {
  736. if (atmel_default_console_device && !(atmel_console.flags & CON_ENABLED))
  737. register_console(&atmel_console);
  738. return 0;
  739. }
  740. core_initcall(atmel_late_console_init);
  741. #else
  742. #define ATMEL_CONSOLE_DEVICE NULL
  743. #endif
  744. static struct uart_driver atmel_uart = {
  745. .owner = THIS_MODULE,
  746. .driver_name = "atmel_serial",
  747. .dev_name = ATMEL_DEVICENAME,
  748. .major = SERIAL_ATMEL_MAJOR,
  749. .minor = MINOR_START,
  750. .nr = ATMEL_MAX_UART,
  751. .cons = ATMEL_CONSOLE_DEVICE,
  752. };
  753. #ifdef CONFIG_PM
  754. static int atmel_serial_suspend(struct platform_device *pdev, pm_message_t state)
  755. {
  756. struct uart_port *port = platform_get_drvdata(pdev);
  757. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  758. if (device_may_wakeup(&pdev->dev) && !at91_suspend_entering_slow_clock())
  759. enable_irq_wake(port->irq);
  760. else {
  761. uart_suspend_port(&atmel_uart, port);
  762. atmel_port->suspended = 1;
  763. }
  764. return 0;
  765. }
  766. static int atmel_serial_resume(struct platform_device *pdev)
  767. {
  768. struct uart_port *port = platform_get_drvdata(pdev);
  769. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  770. if (atmel_port->suspended) {
  771. uart_resume_port(&atmel_uart, port);
  772. atmel_port->suspended = 0;
  773. }
  774. else
  775. disable_irq_wake(port->irq);
  776. return 0;
  777. }
  778. #else
  779. #define atmel_serial_suspend NULL
  780. #define atmel_serial_resume NULL
  781. #endif
  782. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  783. {
  784. struct atmel_uart_port *port;
  785. int ret;
  786. port = &atmel_ports[pdev->id];
  787. atmel_init_port(port, pdev);
  788. ret = uart_add_one_port(&atmel_uart, &port->uart);
  789. if (!ret) {
  790. device_init_wakeup(&pdev->dev, 1);
  791. platform_set_drvdata(pdev, port);
  792. }
  793. return ret;
  794. }
  795. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  796. {
  797. struct uart_port *port = platform_get_drvdata(pdev);
  798. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *) port;
  799. int ret = 0;
  800. clk_disable(atmel_port->clk);
  801. clk_put(atmel_port->clk);
  802. device_init_wakeup(&pdev->dev, 0);
  803. platform_set_drvdata(pdev, NULL);
  804. if (port) {
  805. ret = uart_remove_one_port(&atmel_uart, port);
  806. kfree(port);
  807. }
  808. return ret;
  809. }
  810. static struct platform_driver atmel_serial_driver = {
  811. .probe = atmel_serial_probe,
  812. .remove = __devexit_p(atmel_serial_remove),
  813. .suspend = atmel_serial_suspend,
  814. .resume = atmel_serial_resume,
  815. .driver = {
  816. .name = "atmel_usart",
  817. .owner = THIS_MODULE,
  818. },
  819. };
  820. static int __init atmel_serial_init(void)
  821. {
  822. int ret;
  823. ret = uart_register_driver(&atmel_uart);
  824. if (ret)
  825. return ret;
  826. ret = platform_driver_register(&atmel_serial_driver);
  827. if (ret)
  828. uart_unregister_driver(&atmel_uart);
  829. return ret;
  830. }
  831. static void __exit atmel_serial_exit(void)
  832. {
  833. platform_driver_unregister(&atmel_serial_driver);
  834. uart_unregister_driver(&atmel_uart);
  835. }
  836. module_init(atmel_serial_init);
  837. module_exit(atmel_serial_exit);
  838. MODULE_AUTHOR("Rick Bronson");
  839. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  840. MODULE_LICENSE("GPL");