atmel_serial.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215
  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 <linux/atmel_serial.h>
  38. #include <asm/io.h>
  39. #include <asm/mach/serial_at91.h>
  40. #include <asm/arch/board.h>
  41. #ifdef CONFIG_ARM
  42. #include <asm/arch/cpu.h>
  43. #include <asm/arch/gpio.h>
  44. #endif
  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. /* UART registers. CR is write-only, hence no GET macro */
  65. #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  66. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  67. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  68. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  69. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  70. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  71. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  72. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  73. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  74. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  75. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  76. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  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. static int (*atmel_open_hook)(struct uart_port *);
  88. static void (*atmel_close_hook)(struct uart_port *);
  89. struct atmel_uart_char {
  90. u16 status;
  91. u16 ch;
  92. };
  93. #define ATMEL_SERIAL_RINGSIZE 1024
  94. /*
  95. * We wrap our port structure around the generic uart_port.
  96. */
  97. struct atmel_uart_port {
  98. struct uart_port uart; /* uart */
  99. struct clk *clk; /* uart clock */
  100. unsigned short suspended; /* is port suspended? */
  101. int break_active; /* break being received */
  102. struct tasklet_struct tasklet;
  103. unsigned int irq_status;
  104. unsigned int irq_status_prev;
  105. struct circ_buf rx_ring;
  106. };
  107. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  108. #ifdef SUPPORT_SYSRQ
  109. static struct console atmel_console;
  110. #endif
  111. /*
  112. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  113. */
  114. static u_int atmel_tx_empty(struct uart_port *port)
  115. {
  116. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  117. }
  118. /*
  119. * Set state of the modem control output lines
  120. */
  121. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  122. {
  123. unsigned int control = 0;
  124. unsigned int mode;
  125. #ifdef CONFIG_ARCH_AT91RM9200
  126. if (cpu_is_at91rm9200()) {
  127. /*
  128. * AT91RM9200 Errata #39: RTS0 is not internally connected
  129. * to PA21. We need to drive the pin manually.
  130. */
  131. if (port->mapbase == AT91RM9200_BASE_US0) {
  132. if (mctrl & TIOCM_RTS)
  133. at91_set_gpio_value(AT91_PIN_PA21, 0);
  134. else
  135. at91_set_gpio_value(AT91_PIN_PA21, 1);
  136. }
  137. }
  138. #endif
  139. if (mctrl & TIOCM_RTS)
  140. control |= ATMEL_US_RTSEN;
  141. else
  142. control |= ATMEL_US_RTSDIS;
  143. if (mctrl & TIOCM_DTR)
  144. control |= ATMEL_US_DTREN;
  145. else
  146. control |= ATMEL_US_DTRDIS;
  147. UART_PUT_CR(port, control);
  148. /* Local loopback mode? */
  149. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  150. if (mctrl & TIOCM_LOOP)
  151. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  152. else
  153. mode |= ATMEL_US_CHMODE_NORMAL;
  154. UART_PUT_MR(port, mode);
  155. }
  156. /*
  157. * Get state of the modem control input lines
  158. */
  159. static u_int atmel_get_mctrl(struct uart_port *port)
  160. {
  161. unsigned int status, ret = 0;
  162. status = UART_GET_CSR(port);
  163. /*
  164. * The control signals are active low.
  165. */
  166. if (!(status & ATMEL_US_DCD))
  167. ret |= TIOCM_CD;
  168. if (!(status & ATMEL_US_CTS))
  169. ret |= TIOCM_CTS;
  170. if (!(status & ATMEL_US_DSR))
  171. ret |= TIOCM_DSR;
  172. if (!(status & ATMEL_US_RI))
  173. ret |= TIOCM_RI;
  174. return ret;
  175. }
  176. /*
  177. * Stop transmitting.
  178. */
  179. static void atmel_stop_tx(struct uart_port *port)
  180. {
  181. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  182. }
  183. /*
  184. * Start transmitting.
  185. */
  186. static void atmel_start_tx(struct uart_port *port)
  187. {
  188. UART_PUT_IER(port, ATMEL_US_TXRDY);
  189. }
  190. /*
  191. * Stop receiving - port is in process of being closed.
  192. */
  193. static void atmel_stop_rx(struct uart_port *port)
  194. {
  195. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  196. }
  197. /*
  198. * Enable modem status interrupts
  199. */
  200. static void atmel_enable_ms(struct uart_port *port)
  201. {
  202. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
  203. | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  204. }
  205. /*
  206. * Control the transmission of a break signal
  207. */
  208. static void atmel_break_ctl(struct uart_port *port, int break_state)
  209. {
  210. if (break_state != 0)
  211. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  212. else
  213. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  214. }
  215. /*
  216. * Stores the incoming character in the ring buffer
  217. */
  218. static void
  219. atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
  220. unsigned int ch)
  221. {
  222. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  223. struct circ_buf *ring = &atmel_port->rx_ring;
  224. struct atmel_uart_char *c;
  225. if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
  226. /* Buffer overflow, ignore char */
  227. return;
  228. c = &((struct atmel_uart_char *)ring->buf)[ring->head];
  229. c->status = status;
  230. c->ch = ch;
  231. /* Make sure the character is stored before we update head. */
  232. smp_wmb();
  233. ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  234. }
  235. /*
  236. * Characters received (called from interrupt handler)
  237. */
  238. static void atmel_rx_chars(struct uart_port *port)
  239. {
  240. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  241. unsigned int status, ch;
  242. status = UART_GET_CSR(port);
  243. while (status & ATMEL_US_RXRDY) {
  244. ch = UART_GET_CHAR(port);
  245. /*
  246. * note that the error handling code is
  247. * out of the main execution path
  248. */
  249. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  250. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  251. || atmel_port->break_active)) {
  252. /* clear error */
  253. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  254. if (status & ATMEL_US_RXBRK
  255. && !atmel_port->break_active) {
  256. atmel_port->break_active = 1;
  257. UART_PUT_IER(port, ATMEL_US_RXBRK);
  258. } else {
  259. /*
  260. * This is either the end-of-break
  261. * condition or we've received at
  262. * least one character without RXBRK
  263. * being set. In both cases, the next
  264. * RXBRK will indicate start-of-break.
  265. */
  266. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  267. status &= ~ATMEL_US_RXBRK;
  268. atmel_port->break_active = 0;
  269. }
  270. }
  271. atmel_buffer_rx_char(port, status, ch);
  272. status = UART_GET_CSR(port);
  273. }
  274. tasklet_schedule(&atmel_port->tasklet);
  275. }
  276. /*
  277. * Transmit characters (called from tasklet with TXRDY interrupt
  278. * disabled)
  279. */
  280. static void atmel_tx_chars(struct uart_port *port)
  281. {
  282. struct circ_buf *xmit = &port->info->xmit;
  283. if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  284. UART_PUT_CHAR(port, port->x_char);
  285. port->icount.tx++;
  286. port->x_char = 0;
  287. }
  288. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  289. return;
  290. while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  291. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  292. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  293. port->icount.tx++;
  294. if (uart_circ_empty(xmit))
  295. break;
  296. }
  297. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  298. uart_write_wakeup(port);
  299. if (!uart_circ_empty(xmit))
  300. UART_PUT_IER(port, ATMEL_US_TXRDY);
  301. }
  302. /*
  303. * receive interrupt handler.
  304. */
  305. static void
  306. atmel_handle_receive(struct uart_port *port, unsigned int pending)
  307. {
  308. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  309. /* Interrupt receive */
  310. if (pending & ATMEL_US_RXRDY)
  311. atmel_rx_chars(port);
  312. else if (pending & ATMEL_US_RXBRK) {
  313. /*
  314. * End of break detected. If it came along with a
  315. * character, atmel_rx_chars will handle it.
  316. */
  317. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  318. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  319. atmel_port->break_active = 0;
  320. }
  321. }
  322. /*
  323. * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
  324. */
  325. static void
  326. atmel_handle_transmit(struct uart_port *port, unsigned int pending)
  327. {
  328. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  329. /* Interrupt transmit */
  330. if (pending & ATMEL_US_TXRDY) {
  331. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  332. tasklet_schedule(&atmel_port->tasklet);
  333. }
  334. }
  335. /*
  336. * status flags interrupt handler.
  337. */
  338. static void
  339. atmel_handle_status(struct uart_port *port, unsigned int pending,
  340. unsigned int status)
  341. {
  342. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  343. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
  344. | ATMEL_US_CTSIC)) {
  345. atmel_port->irq_status = status;
  346. tasklet_schedule(&atmel_port->tasklet);
  347. }
  348. }
  349. /*
  350. * Interrupt handler
  351. */
  352. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  353. {
  354. struct uart_port *port = dev_id;
  355. unsigned int status, pending, pass_counter = 0;
  356. status = UART_GET_CSR(port);
  357. pending = status & UART_GET_IMR(port);
  358. while (pending) {
  359. atmel_handle_receive(port, pending);
  360. atmel_handle_status(port, pending, status);
  361. atmel_handle_transmit(port, pending);
  362. if (pass_counter++ > ATMEL_ISR_PASS_LIMIT)
  363. break;
  364. status = UART_GET_CSR(port);
  365. pending = status & UART_GET_IMR(port);
  366. }
  367. return IRQ_HANDLED;
  368. }
  369. static void atmel_rx_from_ring(struct uart_port *port)
  370. {
  371. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  372. struct circ_buf *ring = &atmel_port->rx_ring;
  373. unsigned int flg;
  374. unsigned int status;
  375. while (ring->head != ring->tail) {
  376. struct atmel_uart_char c;
  377. /* Make sure c is loaded after head. */
  378. smp_rmb();
  379. c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
  380. ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  381. port->icount.rx++;
  382. status = c.status;
  383. flg = TTY_NORMAL;
  384. /*
  385. * note that the error handling code is
  386. * out of the main execution path
  387. */
  388. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  389. | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  390. if (status & ATMEL_US_RXBRK) {
  391. /* ignore side-effect */
  392. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  393. port->icount.brk++;
  394. if (uart_handle_break(port))
  395. continue;
  396. }
  397. if (status & ATMEL_US_PARE)
  398. port->icount.parity++;
  399. if (status & ATMEL_US_FRAME)
  400. port->icount.frame++;
  401. if (status & ATMEL_US_OVRE)
  402. port->icount.overrun++;
  403. status &= port->read_status_mask;
  404. if (status & ATMEL_US_RXBRK)
  405. flg = TTY_BREAK;
  406. else if (status & ATMEL_US_PARE)
  407. flg = TTY_PARITY;
  408. else if (status & ATMEL_US_FRAME)
  409. flg = TTY_FRAME;
  410. }
  411. if (uart_handle_sysrq_char(port, c.ch))
  412. continue;
  413. uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
  414. }
  415. /*
  416. * Drop the lock here since it might end up calling
  417. * uart_start(), which takes the lock.
  418. */
  419. spin_unlock(&port->lock);
  420. tty_flip_buffer_push(port->info->tty);
  421. spin_lock(&port->lock);
  422. }
  423. /*
  424. * tasklet handling tty stuff outside the interrupt handler.
  425. */
  426. static void atmel_tasklet_func(unsigned long data)
  427. {
  428. struct uart_port *port = (struct uart_port *)data;
  429. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  430. unsigned int status;
  431. unsigned int status_change;
  432. /* The interrupt handler does not take the lock */
  433. spin_lock(&port->lock);
  434. atmel_tx_chars(port);
  435. status = atmel_port->irq_status;
  436. status_change = status ^ atmel_port->irq_status_prev;
  437. if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
  438. | ATMEL_US_DCD | ATMEL_US_CTS)) {
  439. /* TODO: All reads to CSR will clear these interrupts! */
  440. if (status_change & ATMEL_US_RI)
  441. port->icount.rng++;
  442. if (status_change & ATMEL_US_DSR)
  443. port->icount.dsr++;
  444. if (status_change & ATMEL_US_DCD)
  445. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  446. if (status_change & ATMEL_US_CTS)
  447. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  448. wake_up_interruptible(&port->info->delta_msr_wait);
  449. atmel_port->irq_status_prev = status;
  450. }
  451. atmel_rx_from_ring(port);
  452. spin_unlock(&port->lock);
  453. }
  454. /*
  455. * Perform initialization and enable port for reception
  456. */
  457. static int atmel_startup(struct uart_port *port)
  458. {
  459. int retval;
  460. /*
  461. * Ensure that no interrupts are enabled otherwise when
  462. * request_irq() is called we could get stuck trying to
  463. * handle an unexpected interrupt
  464. */
  465. UART_PUT_IDR(port, -1);
  466. /*
  467. * Allocate the IRQ
  468. */
  469. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
  470. "atmel_serial", port);
  471. if (retval) {
  472. printk("atmel_serial: atmel_startup - Can't get irq\n");
  473. return retval;
  474. }
  475. /*
  476. * If there is a specific "open" function (to register
  477. * control line interrupts)
  478. */
  479. if (atmel_open_hook) {
  480. retval = atmel_open_hook(port);
  481. if (retval) {
  482. free_irq(port->irq, port);
  483. return retval;
  484. }
  485. }
  486. /*
  487. * Finally, enable the serial port
  488. */
  489. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  490. /* enable xmit & rcvr */
  491. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  492. /* enable receive only */
  493. UART_PUT_IER(port, ATMEL_US_RXRDY);
  494. return 0;
  495. }
  496. /*
  497. * Disable the port
  498. */
  499. static void atmel_shutdown(struct uart_port *port)
  500. {
  501. /*
  502. * Disable all interrupts, port and break condition.
  503. */
  504. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  505. UART_PUT_IDR(port, -1);
  506. /*
  507. * Free the interrupt
  508. */
  509. free_irq(port->irq, port);
  510. /*
  511. * If there is a specific "close" function (to unregister
  512. * control line interrupts)
  513. */
  514. if (atmel_close_hook)
  515. atmel_close_hook(port);
  516. }
  517. /*
  518. * Power / Clock management.
  519. */
  520. static void atmel_serial_pm(struct uart_port *port, unsigned int state,
  521. unsigned int oldstate)
  522. {
  523. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  524. switch (state) {
  525. case 0:
  526. /*
  527. * Enable the peripheral clock for this serial port.
  528. * This is called on uart_open() or a resume event.
  529. */
  530. clk_enable(atmel_port->clk);
  531. break;
  532. case 3:
  533. /*
  534. * Disable the peripheral clock for this serial port.
  535. * This is called on uart_close() or a suspend event.
  536. */
  537. clk_disable(atmel_port->clk);
  538. break;
  539. default:
  540. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  541. }
  542. }
  543. /*
  544. * Change the port parameters
  545. */
  546. static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  547. struct ktermios *old)
  548. {
  549. unsigned long flags;
  550. unsigned int mode, imr, quot, baud;
  551. /* Get current mode register */
  552. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
  553. | ATMEL_US_NBSTOP | ATMEL_US_PAR);
  554. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  555. quot = uart_get_divisor(port, baud);
  556. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  557. quot /= 8;
  558. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  559. }
  560. /* byte size */
  561. switch (termios->c_cflag & CSIZE) {
  562. case CS5:
  563. mode |= ATMEL_US_CHRL_5;
  564. break;
  565. case CS6:
  566. mode |= ATMEL_US_CHRL_6;
  567. break;
  568. case CS7:
  569. mode |= ATMEL_US_CHRL_7;
  570. break;
  571. default:
  572. mode |= ATMEL_US_CHRL_8;
  573. break;
  574. }
  575. /* stop bits */
  576. if (termios->c_cflag & CSTOPB)
  577. mode |= ATMEL_US_NBSTOP_2;
  578. /* parity */
  579. if (termios->c_cflag & PARENB) {
  580. /* Mark or Space parity */
  581. if (termios->c_cflag & CMSPAR) {
  582. if (termios->c_cflag & PARODD)
  583. mode |= ATMEL_US_PAR_MARK;
  584. else
  585. mode |= ATMEL_US_PAR_SPACE;
  586. } else if (termios->c_cflag & PARODD)
  587. mode |= ATMEL_US_PAR_ODD;
  588. else
  589. mode |= ATMEL_US_PAR_EVEN;
  590. } else
  591. mode |= ATMEL_US_PAR_NONE;
  592. spin_lock_irqsave(&port->lock, flags);
  593. port->read_status_mask = ATMEL_US_OVRE;
  594. if (termios->c_iflag & INPCK)
  595. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  596. if (termios->c_iflag & (BRKINT | PARMRK))
  597. port->read_status_mask |= ATMEL_US_RXBRK;
  598. /*
  599. * Characters to ignore
  600. */
  601. port->ignore_status_mask = 0;
  602. if (termios->c_iflag & IGNPAR)
  603. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  604. if (termios->c_iflag & IGNBRK) {
  605. port->ignore_status_mask |= ATMEL_US_RXBRK;
  606. /*
  607. * If we're ignoring parity and break indicators,
  608. * ignore overruns too (for real raw support).
  609. */
  610. if (termios->c_iflag & IGNPAR)
  611. port->ignore_status_mask |= ATMEL_US_OVRE;
  612. }
  613. /* TODO: Ignore all characters if CREAD is set.*/
  614. /* update the per-port timeout */
  615. uart_update_timeout(port, termios->c_cflag, baud);
  616. /* save/disable interrupts and drain transmitter */
  617. imr = UART_GET_IMR(port);
  618. UART_PUT_IDR(port, -1);
  619. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  620. cpu_relax();
  621. /* disable receiver and transmitter */
  622. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  623. /* set the parity, stop bits and data size */
  624. UART_PUT_MR(port, mode);
  625. /* set the baud rate */
  626. UART_PUT_BRGR(port, quot);
  627. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  628. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  629. /* restore interrupts */
  630. UART_PUT_IER(port, imr);
  631. /* CTS flow-control and modem-status interrupts */
  632. if (UART_ENABLE_MS(port, termios->c_cflag))
  633. port->ops->enable_ms(port);
  634. spin_unlock_irqrestore(&port->lock, flags);
  635. }
  636. /*
  637. * Return string describing the specified port
  638. */
  639. static const char *atmel_type(struct uart_port *port)
  640. {
  641. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  642. }
  643. /*
  644. * Release the memory region(s) being used by 'port'.
  645. */
  646. static void atmel_release_port(struct uart_port *port)
  647. {
  648. struct platform_device *pdev = to_platform_device(port->dev);
  649. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  650. release_mem_region(port->mapbase, size);
  651. if (port->flags & UPF_IOREMAP) {
  652. iounmap(port->membase);
  653. port->membase = NULL;
  654. }
  655. }
  656. /*
  657. * Request the memory region(s) being used by 'port'.
  658. */
  659. static int atmel_request_port(struct uart_port *port)
  660. {
  661. struct platform_device *pdev = to_platform_device(port->dev);
  662. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  663. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  664. return -EBUSY;
  665. if (port->flags & UPF_IOREMAP) {
  666. port->membase = ioremap(port->mapbase, size);
  667. if (port->membase == NULL) {
  668. release_mem_region(port->mapbase, size);
  669. return -ENOMEM;
  670. }
  671. }
  672. return 0;
  673. }
  674. /*
  675. * Configure/autoconfigure the port.
  676. */
  677. static void atmel_config_port(struct uart_port *port, int flags)
  678. {
  679. if (flags & UART_CONFIG_TYPE) {
  680. port->type = PORT_ATMEL;
  681. atmel_request_port(port);
  682. }
  683. }
  684. /*
  685. * Verify the new serial_struct (for TIOCSSERIAL).
  686. */
  687. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  688. {
  689. int ret = 0;
  690. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  691. ret = -EINVAL;
  692. if (port->irq != ser->irq)
  693. ret = -EINVAL;
  694. if (ser->io_type != SERIAL_IO_MEM)
  695. ret = -EINVAL;
  696. if (port->uartclk / 16 != ser->baud_base)
  697. ret = -EINVAL;
  698. if ((void *)port->mapbase != ser->iomem_base)
  699. ret = -EINVAL;
  700. if (port->iobase != ser->port)
  701. ret = -EINVAL;
  702. if (ser->hub6 != 0)
  703. ret = -EINVAL;
  704. return ret;
  705. }
  706. static struct uart_ops atmel_pops = {
  707. .tx_empty = atmel_tx_empty,
  708. .set_mctrl = atmel_set_mctrl,
  709. .get_mctrl = atmel_get_mctrl,
  710. .stop_tx = atmel_stop_tx,
  711. .start_tx = atmel_start_tx,
  712. .stop_rx = atmel_stop_rx,
  713. .enable_ms = atmel_enable_ms,
  714. .break_ctl = atmel_break_ctl,
  715. .startup = atmel_startup,
  716. .shutdown = atmel_shutdown,
  717. .set_termios = atmel_set_termios,
  718. .type = atmel_type,
  719. .release_port = atmel_release_port,
  720. .request_port = atmel_request_port,
  721. .config_port = atmel_config_port,
  722. .verify_port = atmel_verify_port,
  723. .pm = atmel_serial_pm,
  724. };
  725. /*
  726. * Configure the port from the platform device resource info.
  727. */
  728. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
  729. struct platform_device *pdev)
  730. {
  731. struct uart_port *port = &atmel_port->uart;
  732. struct atmel_uart_data *data = pdev->dev.platform_data;
  733. port->iotype = UPIO_MEM;
  734. port->flags = UPF_BOOT_AUTOCONF;
  735. port->ops = &atmel_pops;
  736. port->fifosize = 1;
  737. port->line = pdev->id;
  738. port->dev = &pdev->dev;
  739. port->mapbase = pdev->resource[0].start;
  740. port->irq = pdev->resource[1].start;
  741. tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
  742. (unsigned long)port);
  743. memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
  744. if (data->regs)
  745. /* Already mapped by setup code */
  746. port->membase = data->regs;
  747. else {
  748. port->flags |= UPF_IOREMAP;
  749. port->membase = NULL;
  750. }
  751. /* for console, the clock could already be configured */
  752. if (!atmel_port->clk) {
  753. atmel_port->clk = clk_get(&pdev->dev, "usart");
  754. clk_enable(atmel_port->clk);
  755. port->uartclk = clk_get_rate(atmel_port->clk);
  756. }
  757. }
  758. /*
  759. * Register board-specific modem-control line handlers.
  760. */
  761. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  762. {
  763. if (fns->enable_ms)
  764. atmel_pops.enable_ms = fns->enable_ms;
  765. if (fns->get_mctrl)
  766. atmel_pops.get_mctrl = fns->get_mctrl;
  767. if (fns->set_mctrl)
  768. atmel_pops.set_mctrl = fns->set_mctrl;
  769. atmel_open_hook = fns->open;
  770. atmel_close_hook = fns->close;
  771. atmel_pops.pm = fns->pm;
  772. atmel_pops.set_wake = fns->set_wake;
  773. }
  774. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  775. static void atmel_console_putchar(struct uart_port *port, int ch)
  776. {
  777. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  778. cpu_relax();
  779. UART_PUT_CHAR(port, ch);
  780. }
  781. /*
  782. * Interrupts are disabled on entering
  783. */
  784. static void atmel_console_write(struct console *co, const char *s, u_int count)
  785. {
  786. struct uart_port *port = &atmel_ports[co->index].uart;
  787. unsigned int status, imr;
  788. /*
  789. * First, save IMR and then disable interrupts
  790. */
  791. imr = UART_GET_IMR(port);
  792. UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
  793. uart_console_write(port, s, count, atmel_console_putchar);
  794. /*
  795. * Finally, wait for transmitter to become empty
  796. * and restore IMR
  797. */
  798. do {
  799. status = UART_GET_CSR(port);
  800. } while (!(status & ATMEL_US_TXRDY));
  801. /* set interrupts back the way they were */
  802. UART_PUT_IER(port, imr);
  803. }
  804. /*
  805. * If the port was already initialised (eg, by a boot loader),
  806. * try to determine the current setup.
  807. */
  808. static void __init atmel_console_get_options(struct uart_port *port, int *baud,
  809. int *parity, int *bits)
  810. {
  811. unsigned int mr, quot;
  812. /*
  813. * If the baud rate generator isn't running, the port wasn't
  814. * initialized by the boot loader.
  815. */
  816. quot = UART_GET_BRGR(port);
  817. if (!quot)
  818. return;
  819. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  820. if (mr == ATMEL_US_CHRL_8)
  821. *bits = 8;
  822. else
  823. *bits = 7;
  824. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  825. if (mr == ATMEL_US_PAR_EVEN)
  826. *parity = 'e';
  827. else if (mr == ATMEL_US_PAR_ODD)
  828. *parity = 'o';
  829. /*
  830. * The serial core only rounds down when matching this to a
  831. * supported baud rate. Make sure we don't end up slightly
  832. * lower than one of those, as it would make us fall through
  833. * to a much lower baud rate than we really want.
  834. */
  835. *baud = port->uartclk / (16 * (quot - 1));
  836. }
  837. static int __init atmel_console_setup(struct console *co, char *options)
  838. {
  839. struct uart_port *port = &atmel_ports[co->index].uart;
  840. int baud = 115200;
  841. int bits = 8;
  842. int parity = 'n';
  843. int flow = 'n';
  844. if (port->membase == NULL) {
  845. /* Port not initialized yet - delay setup */
  846. return -ENODEV;
  847. }
  848. UART_PUT_IDR(port, -1);
  849. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  850. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  851. if (options)
  852. uart_parse_options(options, &baud, &parity, &bits, &flow);
  853. else
  854. atmel_console_get_options(port, &baud, &parity, &bits);
  855. return uart_set_options(port, co, baud, parity, bits, flow);
  856. }
  857. static struct uart_driver atmel_uart;
  858. static struct console atmel_console = {
  859. .name = ATMEL_DEVICENAME,
  860. .write = atmel_console_write,
  861. .device = uart_console_device,
  862. .setup = atmel_console_setup,
  863. .flags = CON_PRINTBUFFER,
  864. .index = -1,
  865. .data = &atmel_uart,
  866. };
  867. #define ATMEL_CONSOLE_DEVICE &atmel_console
  868. /*
  869. * Early console initialization (before VM subsystem initialized).
  870. */
  871. static int __init atmel_console_init(void)
  872. {
  873. if (atmel_default_console_device) {
  874. add_preferred_console(ATMEL_DEVICENAME,
  875. atmel_default_console_device->id, NULL);
  876. atmel_init_port(&atmel_ports[atmel_default_console_device->id],
  877. atmel_default_console_device);
  878. register_console(&atmel_console);
  879. }
  880. return 0;
  881. }
  882. console_initcall(atmel_console_init);
  883. /*
  884. * Late console initialization.
  885. */
  886. static int __init atmel_late_console_init(void)
  887. {
  888. if (atmel_default_console_device
  889. && !(atmel_console.flags & CON_ENABLED))
  890. register_console(&atmel_console);
  891. return 0;
  892. }
  893. core_initcall(atmel_late_console_init);
  894. static inline bool atmel_is_console_port(struct uart_port *port)
  895. {
  896. return port->cons && port->cons->index == port->line;
  897. }
  898. #else
  899. #define ATMEL_CONSOLE_DEVICE NULL
  900. static inline bool atmel_is_console_port(struct uart_port *port)
  901. {
  902. return false;
  903. }
  904. #endif
  905. static struct uart_driver atmel_uart = {
  906. .owner = THIS_MODULE,
  907. .driver_name = "atmel_serial",
  908. .dev_name = ATMEL_DEVICENAME,
  909. .major = SERIAL_ATMEL_MAJOR,
  910. .minor = MINOR_START,
  911. .nr = ATMEL_MAX_UART,
  912. .cons = ATMEL_CONSOLE_DEVICE,
  913. };
  914. #ifdef CONFIG_PM
  915. static int atmel_serial_suspend(struct platform_device *pdev,
  916. pm_message_t state)
  917. {
  918. struct uart_port *port = platform_get_drvdata(pdev);
  919. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  920. if (device_may_wakeup(&pdev->dev)
  921. && !at91_suspend_entering_slow_clock())
  922. enable_irq_wake(port->irq);
  923. else {
  924. uart_suspend_port(&atmel_uart, port);
  925. atmel_port->suspended = 1;
  926. }
  927. return 0;
  928. }
  929. static int atmel_serial_resume(struct platform_device *pdev)
  930. {
  931. struct uart_port *port = platform_get_drvdata(pdev);
  932. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  933. if (atmel_port->suspended) {
  934. uart_resume_port(&atmel_uart, port);
  935. atmel_port->suspended = 0;
  936. } else
  937. disable_irq_wake(port->irq);
  938. return 0;
  939. }
  940. #else
  941. #define atmel_serial_suspend NULL
  942. #define atmel_serial_resume NULL
  943. #endif
  944. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  945. {
  946. struct atmel_uart_port *port;
  947. void *data;
  948. int ret;
  949. BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
  950. port = &atmel_ports[pdev->id];
  951. atmel_init_port(port, pdev);
  952. ret = -ENOMEM;
  953. data = kmalloc(ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
  954. if (!data)
  955. goto err_alloc_ring;
  956. port->rx_ring.buf = data;
  957. ret = uart_add_one_port(&atmel_uart, &port->uart);
  958. if (ret)
  959. goto err_add_port;
  960. device_init_wakeup(&pdev->dev, 1);
  961. platform_set_drvdata(pdev, port);
  962. return 0;
  963. err_add_port:
  964. kfree(port->rx_ring.buf);
  965. port->rx_ring.buf = NULL;
  966. err_alloc_ring:
  967. if (!atmel_is_console_port(&port->uart)) {
  968. clk_disable(port->clk);
  969. clk_put(port->clk);
  970. port->clk = NULL;
  971. }
  972. return ret;
  973. }
  974. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  975. {
  976. struct uart_port *port = platform_get_drvdata(pdev);
  977. struct atmel_uart_port *atmel_port = (struct atmel_uart_port *)port;
  978. int ret = 0;
  979. device_init_wakeup(&pdev->dev, 0);
  980. platform_set_drvdata(pdev, NULL);
  981. ret = uart_remove_one_port(&atmel_uart, port);
  982. tasklet_kill(&atmel_port->tasklet);
  983. kfree(atmel_port->rx_ring.buf);
  984. /* "port" is allocated statically, so we shouldn't free it */
  985. clk_disable(atmel_port->clk);
  986. clk_put(atmel_port->clk);
  987. return ret;
  988. }
  989. static struct platform_driver atmel_serial_driver = {
  990. .probe = atmel_serial_probe,
  991. .remove = __devexit_p(atmel_serial_remove),
  992. .suspend = atmel_serial_suspend,
  993. .resume = atmel_serial_resume,
  994. .driver = {
  995. .name = "atmel_usart",
  996. .owner = THIS_MODULE,
  997. },
  998. };
  999. static int __init atmel_serial_init(void)
  1000. {
  1001. int ret;
  1002. ret = uart_register_driver(&atmel_uart);
  1003. if (ret)
  1004. return ret;
  1005. ret = platform_driver_register(&atmel_serial_driver);
  1006. if (ret)
  1007. uart_unregister_driver(&atmel_uart);
  1008. return ret;
  1009. }
  1010. static void __exit atmel_serial_exit(void)
  1011. {
  1012. platform_driver_unregister(&atmel_serial_driver);
  1013. uart_unregister_driver(&atmel_uart);
  1014. }
  1015. module_init(atmel_serial_init);
  1016. module_exit(atmel_serial_exit);
  1017. MODULE_AUTHOR("Rick Bronson");
  1018. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  1019. MODULE_LICENSE("GPL");