atmel_serial.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594
  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. * DMA support added by Chip Coldwell.
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. */
  27. #include <linux/module.h>
  28. #include <linux/tty.h>
  29. #include <linux/ioport.h>
  30. #include <linux/slab.h>
  31. #include <linux/init.h>
  32. #include <linux/serial.h>
  33. #include <linux/clk.h>
  34. #include <linux/console.h>
  35. #include <linux/sysrq.h>
  36. #include <linux/tty_flip.h>
  37. #include <linux/platform_device.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/atmel_pdc.h>
  40. #include <linux/atmel_serial.h>
  41. #include <asm/io.h>
  42. #include <asm/mach/serial_at91.h>
  43. #include <asm/arch/board.h>
  44. #ifdef CONFIG_ARM
  45. #include <asm/arch/cpu.h>
  46. #include <asm/arch/gpio.h>
  47. #endif
  48. #define PDC_BUFFER_SIZE 512
  49. /* Revisit: We should calculate this based on the actual port settings */
  50. #define PDC_RX_TIMEOUT (3 * 10) /* 3 bytes */
  51. #if defined(CONFIG_SERIAL_ATMEL_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  52. #define SUPPORT_SYSRQ
  53. #endif
  54. #include <linux/serial_core.h>
  55. #ifdef CONFIG_SERIAL_ATMEL_TTYAT
  56. /* Use device name ttyAT, major 204 and minor 154-169. This is necessary if we
  57. * should coexist with the 8250 driver, such as if we have an external 16C550
  58. * UART. */
  59. #define SERIAL_ATMEL_MAJOR 204
  60. #define MINOR_START 154
  61. #define ATMEL_DEVICENAME "ttyAT"
  62. #else
  63. /* Use device name ttyS, major 4, minor 64-68. This is the usual serial port
  64. * name, but it is legally reserved for the 8250 driver. */
  65. #define SERIAL_ATMEL_MAJOR TTY_MAJOR
  66. #define MINOR_START 64
  67. #define ATMEL_DEVICENAME "ttyS"
  68. #endif
  69. #define ATMEL_ISR_PASS_LIMIT 256
  70. /* UART registers. CR is write-only, hence no GET macro */
  71. #define UART_PUT_CR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_CR)
  72. #define UART_GET_MR(port) __raw_readl((port)->membase + ATMEL_US_MR)
  73. #define UART_PUT_MR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_MR)
  74. #define UART_PUT_IER(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IER)
  75. #define UART_PUT_IDR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_IDR)
  76. #define UART_GET_IMR(port) __raw_readl((port)->membase + ATMEL_US_IMR)
  77. #define UART_GET_CSR(port) __raw_readl((port)->membase + ATMEL_US_CSR)
  78. #define UART_GET_CHAR(port) __raw_readl((port)->membase + ATMEL_US_RHR)
  79. #define UART_PUT_CHAR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_THR)
  80. #define UART_GET_BRGR(port) __raw_readl((port)->membase + ATMEL_US_BRGR)
  81. #define UART_PUT_BRGR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_BRGR)
  82. #define UART_PUT_RTOR(port,v) __raw_writel(v, (port)->membase + ATMEL_US_RTOR)
  83. /* PDC registers */
  84. #define UART_PUT_PTCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_PTCR)
  85. #define UART_GET_PTSR(port) __raw_readl((port)->membase + ATMEL_PDC_PTSR)
  86. #define UART_PUT_RPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RPR)
  87. #define UART_GET_RPR(port) __raw_readl((port)->membase + ATMEL_PDC_RPR)
  88. #define UART_PUT_RCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RCR)
  89. #define UART_PUT_RNPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNPR)
  90. #define UART_PUT_RNCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_RNCR)
  91. #define UART_PUT_TPR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TPR)
  92. #define UART_PUT_TCR(port,v) __raw_writel(v, (port)->membase + ATMEL_PDC_TCR)
  93. #define UART_GET_TCR(port) __raw_readl((port)->membase + ATMEL_PDC_TCR)
  94. static int (*atmel_open_hook)(struct uart_port *);
  95. static void (*atmel_close_hook)(struct uart_port *);
  96. struct atmel_dma_buffer {
  97. unsigned char *buf;
  98. dma_addr_t dma_addr;
  99. unsigned int dma_size;
  100. unsigned int ofs;
  101. };
  102. struct atmel_uart_char {
  103. u16 status;
  104. u16 ch;
  105. };
  106. #define ATMEL_SERIAL_RINGSIZE 1024
  107. /*
  108. * We wrap our port structure around the generic uart_port.
  109. */
  110. struct atmel_uart_port {
  111. struct uart_port uart; /* uart */
  112. struct clk *clk; /* uart clock */
  113. unsigned short suspended; /* is port suspended? */
  114. int break_active; /* break being received */
  115. short use_dma_rx; /* enable PDC receiver */
  116. short pdc_rx_idx; /* current PDC RX buffer */
  117. struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
  118. short use_dma_tx; /* enable PDC transmitter */
  119. struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
  120. struct tasklet_struct tasklet;
  121. unsigned int irq_status;
  122. unsigned int irq_status_prev;
  123. struct circ_buf rx_ring;
  124. };
  125. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  126. #ifdef SUPPORT_SYSRQ
  127. static struct console atmel_console;
  128. #endif
  129. static inline struct atmel_uart_port *
  130. to_atmel_uart_port(struct uart_port *uart)
  131. {
  132. return container_of(uart, struct atmel_uart_port, uart);
  133. }
  134. #ifdef CONFIG_SERIAL_ATMEL_PDC
  135. static bool atmel_use_dma_rx(struct uart_port *port)
  136. {
  137. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  138. return atmel_port->use_dma_rx;
  139. }
  140. static bool atmel_use_dma_tx(struct uart_port *port)
  141. {
  142. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  143. return atmel_port->use_dma_tx;
  144. }
  145. #else
  146. static bool atmel_use_dma_rx(struct uart_port *port)
  147. {
  148. return false;
  149. }
  150. static bool atmel_use_dma_tx(struct uart_port *port)
  151. {
  152. return false;
  153. }
  154. #endif
  155. /*
  156. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  157. */
  158. static u_int atmel_tx_empty(struct uart_port *port)
  159. {
  160. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  161. }
  162. /*
  163. * Set state of the modem control output lines
  164. */
  165. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  166. {
  167. unsigned int control = 0;
  168. unsigned int mode;
  169. #ifdef CONFIG_ARCH_AT91RM9200
  170. if (cpu_is_at91rm9200()) {
  171. /*
  172. * AT91RM9200 Errata #39: RTS0 is not internally connected
  173. * to PA21. We need to drive the pin manually.
  174. */
  175. if (port->mapbase == AT91RM9200_BASE_US0) {
  176. if (mctrl & TIOCM_RTS)
  177. at91_set_gpio_value(AT91_PIN_PA21, 0);
  178. else
  179. at91_set_gpio_value(AT91_PIN_PA21, 1);
  180. }
  181. }
  182. #endif
  183. if (mctrl & TIOCM_RTS)
  184. control |= ATMEL_US_RTSEN;
  185. else
  186. control |= ATMEL_US_RTSDIS;
  187. if (mctrl & TIOCM_DTR)
  188. control |= ATMEL_US_DTREN;
  189. else
  190. control |= ATMEL_US_DTRDIS;
  191. UART_PUT_CR(port, control);
  192. /* Local loopback mode? */
  193. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  194. if (mctrl & TIOCM_LOOP)
  195. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  196. else
  197. mode |= ATMEL_US_CHMODE_NORMAL;
  198. UART_PUT_MR(port, mode);
  199. }
  200. /*
  201. * Get state of the modem control input lines
  202. */
  203. static u_int atmel_get_mctrl(struct uart_port *port)
  204. {
  205. unsigned int status, ret = 0;
  206. status = UART_GET_CSR(port);
  207. /*
  208. * The control signals are active low.
  209. */
  210. if (!(status & ATMEL_US_DCD))
  211. ret |= TIOCM_CD;
  212. if (!(status & ATMEL_US_CTS))
  213. ret |= TIOCM_CTS;
  214. if (!(status & ATMEL_US_DSR))
  215. ret |= TIOCM_DSR;
  216. if (!(status & ATMEL_US_RI))
  217. ret |= TIOCM_RI;
  218. return ret;
  219. }
  220. /*
  221. * Stop transmitting.
  222. */
  223. static void atmel_stop_tx(struct uart_port *port)
  224. {
  225. if (atmel_use_dma_tx(port)) {
  226. /* disable PDC transmit */
  227. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  228. UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
  229. } else
  230. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  231. }
  232. /*
  233. * Start transmitting.
  234. */
  235. static void atmel_start_tx(struct uart_port *port)
  236. {
  237. if (atmel_use_dma_tx(port)) {
  238. if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
  239. /* The transmitter is already running. Yes, we
  240. really need this.*/
  241. return;
  242. UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
  243. /* re-enable PDC transmit */
  244. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  245. } else
  246. UART_PUT_IER(port, ATMEL_US_TXRDY);
  247. }
  248. /*
  249. * Stop receiving - port is in process of being closed.
  250. */
  251. static void atmel_stop_rx(struct uart_port *port)
  252. {
  253. if (atmel_use_dma_rx(port)) {
  254. /* disable PDC receive */
  255. UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
  256. UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  257. } else
  258. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  259. }
  260. /*
  261. * Enable modem status interrupts
  262. */
  263. static void atmel_enable_ms(struct uart_port *port)
  264. {
  265. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
  266. | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  267. }
  268. /*
  269. * Control the transmission of a break signal
  270. */
  271. static void atmel_break_ctl(struct uart_port *port, int break_state)
  272. {
  273. if (break_state != 0)
  274. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  275. else
  276. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  277. }
  278. /*
  279. * Stores the incoming character in the ring buffer
  280. */
  281. static void
  282. atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
  283. unsigned int ch)
  284. {
  285. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  286. struct circ_buf *ring = &atmel_port->rx_ring;
  287. struct atmel_uart_char *c;
  288. if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
  289. /* Buffer overflow, ignore char */
  290. return;
  291. c = &((struct atmel_uart_char *)ring->buf)[ring->head];
  292. c->status = status;
  293. c->ch = ch;
  294. /* Make sure the character is stored before we update head. */
  295. smp_wmb();
  296. ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  297. }
  298. /*
  299. * Deal with parity, framing and overrun errors.
  300. */
  301. static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
  302. {
  303. /* clear error */
  304. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  305. if (status & ATMEL_US_RXBRK) {
  306. /* ignore side-effect */
  307. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  308. port->icount.brk++;
  309. }
  310. if (status & ATMEL_US_PARE)
  311. port->icount.parity++;
  312. if (status & ATMEL_US_FRAME)
  313. port->icount.frame++;
  314. if (status & ATMEL_US_OVRE)
  315. port->icount.overrun++;
  316. }
  317. /*
  318. * Characters received (called from interrupt handler)
  319. */
  320. static void atmel_rx_chars(struct uart_port *port)
  321. {
  322. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  323. unsigned int status, ch;
  324. status = UART_GET_CSR(port);
  325. while (status & ATMEL_US_RXRDY) {
  326. ch = UART_GET_CHAR(port);
  327. /*
  328. * note that the error handling code is
  329. * out of the main execution path
  330. */
  331. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  332. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  333. || atmel_port->break_active)) {
  334. /* clear error */
  335. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  336. if (status & ATMEL_US_RXBRK
  337. && !atmel_port->break_active) {
  338. atmel_port->break_active = 1;
  339. UART_PUT_IER(port, ATMEL_US_RXBRK);
  340. } else {
  341. /*
  342. * This is either the end-of-break
  343. * condition or we've received at
  344. * least one character without RXBRK
  345. * being set. In both cases, the next
  346. * RXBRK will indicate start-of-break.
  347. */
  348. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  349. status &= ~ATMEL_US_RXBRK;
  350. atmel_port->break_active = 0;
  351. }
  352. }
  353. atmel_buffer_rx_char(port, status, ch);
  354. status = UART_GET_CSR(port);
  355. }
  356. tasklet_schedule(&atmel_port->tasklet);
  357. }
  358. /*
  359. * Transmit characters (called from tasklet with TXRDY interrupt
  360. * disabled)
  361. */
  362. static void atmel_tx_chars(struct uart_port *port)
  363. {
  364. struct circ_buf *xmit = &port->info->xmit;
  365. if (port->x_char && UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  366. UART_PUT_CHAR(port, port->x_char);
  367. port->icount.tx++;
  368. port->x_char = 0;
  369. }
  370. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  371. return;
  372. while (UART_GET_CSR(port) & ATMEL_US_TXRDY) {
  373. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  374. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  375. port->icount.tx++;
  376. if (uart_circ_empty(xmit))
  377. break;
  378. }
  379. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  380. uart_write_wakeup(port);
  381. if (!uart_circ_empty(xmit))
  382. UART_PUT_IER(port, ATMEL_US_TXRDY);
  383. }
  384. /*
  385. * receive interrupt handler.
  386. */
  387. static void
  388. atmel_handle_receive(struct uart_port *port, unsigned int pending)
  389. {
  390. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  391. if (atmel_use_dma_rx(port)) {
  392. /*
  393. * PDC receive. Just schedule the tasklet and let it
  394. * figure out the details.
  395. *
  396. * TODO: We're not handling error flags correctly at
  397. * the moment.
  398. */
  399. if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
  400. UART_PUT_IDR(port, (ATMEL_US_ENDRX
  401. | ATMEL_US_TIMEOUT));
  402. tasklet_schedule(&atmel_port->tasklet);
  403. }
  404. if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
  405. ATMEL_US_FRAME | ATMEL_US_PARE))
  406. atmel_pdc_rxerr(port, pending);
  407. }
  408. /* Interrupt receive */
  409. if (pending & ATMEL_US_RXRDY)
  410. atmel_rx_chars(port);
  411. else if (pending & ATMEL_US_RXBRK) {
  412. /*
  413. * End of break detected. If it came along with a
  414. * character, atmel_rx_chars will handle it.
  415. */
  416. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  417. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  418. atmel_port->break_active = 0;
  419. }
  420. }
  421. /*
  422. * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
  423. */
  424. static void
  425. atmel_handle_transmit(struct uart_port *port, unsigned int pending)
  426. {
  427. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  428. if (atmel_use_dma_tx(port)) {
  429. /* PDC transmit */
  430. if (pending & (ATMEL_US_ENDTX | ATMEL_US_TXBUFE)) {
  431. UART_PUT_IDR(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
  432. tasklet_schedule(&atmel_port->tasklet);
  433. }
  434. } else {
  435. /* Interrupt transmit */
  436. if (pending & ATMEL_US_TXRDY) {
  437. UART_PUT_IDR(port, ATMEL_US_TXRDY);
  438. tasklet_schedule(&atmel_port->tasklet);
  439. }
  440. }
  441. }
  442. /*
  443. * status flags interrupt handler.
  444. */
  445. static void
  446. atmel_handle_status(struct uart_port *port, unsigned int pending,
  447. unsigned int status)
  448. {
  449. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  450. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
  451. | ATMEL_US_CTSIC)) {
  452. atmel_port->irq_status = status;
  453. tasklet_schedule(&atmel_port->tasklet);
  454. }
  455. }
  456. /*
  457. * Interrupt handler
  458. */
  459. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  460. {
  461. struct uart_port *port = dev_id;
  462. unsigned int status, pending, pass_counter = 0;
  463. do {
  464. status = UART_GET_CSR(port);
  465. pending = status & UART_GET_IMR(port);
  466. if (!pending)
  467. break;
  468. atmel_handle_receive(port, pending);
  469. atmel_handle_status(port, pending, status);
  470. atmel_handle_transmit(port, pending);
  471. } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
  472. return pass_counter ? IRQ_HANDLED : IRQ_NONE;
  473. }
  474. /*
  475. * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
  476. */
  477. static void atmel_tx_dma(struct uart_port *port)
  478. {
  479. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  480. struct circ_buf *xmit = &port->info->xmit;
  481. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  482. int count;
  483. /* nothing left to transmit? */
  484. if (UART_GET_TCR(port))
  485. return;
  486. xmit->tail += pdc->ofs;
  487. xmit->tail &= UART_XMIT_SIZE - 1;
  488. port->icount.tx += pdc->ofs;
  489. pdc->ofs = 0;
  490. /* more to transmit - setup next transfer */
  491. /* disable PDC transmit */
  492. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  493. if (!uart_circ_empty(xmit)) {
  494. dma_sync_single_for_device(port->dev,
  495. pdc->dma_addr,
  496. pdc->dma_size,
  497. DMA_TO_DEVICE);
  498. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  499. pdc->ofs = count;
  500. UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
  501. UART_PUT_TCR(port, count);
  502. /* re-enable PDC transmit and interrupts */
  503. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  504. UART_PUT_IER(port, ATMEL_US_ENDTX | ATMEL_US_TXBUFE);
  505. }
  506. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  507. uart_write_wakeup(port);
  508. }
  509. static void atmel_rx_from_ring(struct uart_port *port)
  510. {
  511. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  512. struct circ_buf *ring = &atmel_port->rx_ring;
  513. unsigned int flg;
  514. unsigned int status;
  515. while (ring->head != ring->tail) {
  516. struct atmel_uart_char c;
  517. /* Make sure c is loaded after head. */
  518. smp_rmb();
  519. c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
  520. ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  521. port->icount.rx++;
  522. status = c.status;
  523. flg = TTY_NORMAL;
  524. /*
  525. * note that the error handling code is
  526. * out of the main execution path
  527. */
  528. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  529. | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  530. if (status & ATMEL_US_RXBRK) {
  531. /* ignore side-effect */
  532. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  533. port->icount.brk++;
  534. if (uart_handle_break(port))
  535. continue;
  536. }
  537. if (status & ATMEL_US_PARE)
  538. port->icount.parity++;
  539. if (status & ATMEL_US_FRAME)
  540. port->icount.frame++;
  541. if (status & ATMEL_US_OVRE)
  542. port->icount.overrun++;
  543. status &= port->read_status_mask;
  544. if (status & ATMEL_US_RXBRK)
  545. flg = TTY_BREAK;
  546. else if (status & ATMEL_US_PARE)
  547. flg = TTY_PARITY;
  548. else if (status & ATMEL_US_FRAME)
  549. flg = TTY_FRAME;
  550. }
  551. if (uart_handle_sysrq_char(port, c.ch))
  552. continue;
  553. uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
  554. }
  555. /*
  556. * Drop the lock here since it might end up calling
  557. * uart_start(), which takes the lock.
  558. */
  559. spin_unlock(&port->lock);
  560. tty_flip_buffer_push(port->info->tty);
  561. spin_lock(&port->lock);
  562. }
  563. static void atmel_rx_from_dma(struct uart_port *port)
  564. {
  565. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  566. struct tty_struct *tty = port->info->tty;
  567. struct atmel_dma_buffer *pdc;
  568. int rx_idx = atmel_port->pdc_rx_idx;
  569. unsigned int head;
  570. unsigned int tail;
  571. unsigned int count;
  572. do {
  573. /* Reset the UART timeout early so that we don't miss one */
  574. UART_PUT_CR(port, ATMEL_US_STTTO);
  575. pdc = &atmel_port->pdc_rx[rx_idx];
  576. head = UART_GET_RPR(port) - pdc->dma_addr;
  577. tail = pdc->ofs;
  578. /* If the PDC has switched buffers, RPR won't contain
  579. * any address within the current buffer. Since head
  580. * is unsigned, we just need a one-way comparison to
  581. * find out.
  582. *
  583. * In this case, we just need to consume the entire
  584. * buffer and resubmit it for DMA. This will clear the
  585. * ENDRX bit as well, so that we can safely re-enable
  586. * all interrupts below.
  587. */
  588. head = min(head, pdc->dma_size);
  589. if (likely(head != tail)) {
  590. dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
  591. pdc->dma_size, DMA_FROM_DEVICE);
  592. /*
  593. * head will only wrap around when we recycle
  594. * the DMA buffer, and when that happens, we
  595. * explicitly set tail to 0. So head will
  596. * always be greater than tail.
  597. */
  598. count = head - tail;
  599. tty_insert_flip_string(tty, pdc->buf + pdc->ofs, count);
  600. dma_sync_single_for_device(port->dev, pdc->dma_addr,
  601. pdc->dma_size, DMA_FROM_DEVICE);
  602. port->icount.rx += count;
  603. pdc->ofs = head;
  604. }
  605. /*
  606. * If the current buffer is full, we need to check if
  607. * the next one contains any additional data.
  608. */
  609. if (head >= pdc->dma_size) {
  610. pdc->ofs = 0;
  611. UART_PUT_RNPR(port, pdc->dma_addr);
  612. UART_PUT_RNCR(port, pdc->dma_size);
  613. rx_idx = !rx_idx;
  614. atmel_port->pdc_rx_idx = rx_idx;
  615. }
  616. } while (head >= pdc->dma_size);
  617. /*
  618. * Drop the lock here since it might end up calling
  619. * uart_start(), which takes the lock.
  620. */
  621. spin_unlock(&port->lock);
  622. tty_flip_buffer_push(tty);
  623. spin_lock(&port->lock);
  624. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  625. }
  626. /*
  627. * tasklet handling tty stuff outside the interrupt handler.
  628. */
  629. static void atmel_tasklet_func(unsigned long data)
  630. {
  631. struct uart_port *port = (struct uart_port *)data;
  632. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  633. unsigned int status;
  634. unsigned int status_change;
  635. /* The interrupt handler does not take the lock */
  636. spin_lock(&port->lock);
  637. if (atmel_use_dma_tx(port))
  638. atmel_tx_dma(port);
  639. else
  640. atmel_tx_chars(port);
  641. status = atmel_port->irq_status;
  642. status_change = status ^ atmel_port->irq_status_prev;
  643. if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
  644. | ATMEL_US_DCD | ATMEL_US_CTS)) {
  645. /* TODO: All reads to CSR will clear these interrupts! */
  646. if (status_change & ATMEL_US_RI)
  647. port->icount.rng++;
  648. if (status_change & ATMEL_US_DSR)
  649. port->icount.dsr++;
  650. if (status_change & ATMEL_US_DCD)
  651. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  652. if (status_change & ATMEL_US_CTS)
  653. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  654. wake_up_interruptible(&port->info->delta_msr_wait);
  655. atmel_port->irq_status_prev = status;
  656. }
  657. if (atmel_use_dma_rx(port))
  658. atmel_rx_from_dma(port);
  659. else
  660. atmel_rx_from_ring(port);
  661. spin_unlock(&port->lock);
  662. }
  663. /*
  664. * Perform initialization and enable port for reception
  665. */
  666. static int atmel_startup(struct uart_port *port)
  667. {
  668. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  669. struct tty_struct *tty = port->info->tty;
  670. int retval;
  671. /*
  672. * Ensure that no interrupts are enabled otherwise when
  673. * request_irq() is called we could get stuck trying to
  674. * handle an unexpected interrupt
  675. */
  676. UART_PUT_IDR(port, -1);
  677. /*
  678. * Allocate the IRQ
  679. */
  680. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
  681. tty ? tty->name : "atmel_serial", port);
  682. if (retval) {
  683. printk("atmel_serial: atmel_startup - Can't get irq\n");
  684. return retval;
  685. }
  686. /*
  687. * Initialize DMA (if necessary)
  688. */
  689. if (atmel_use_dma_rx(port)) {
  690. int i;
  691. for (i = 0; i < 2; i++) {
  692. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  693. pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
  694. if (pdc->buf == NULL) {
  695. if (i != 0) {
  696. dma_unmap_single(port->dev,
  697. atmel_port->pdc_rx[0].dma_addr,
  698. PDC_BUFFER_SIZE,
  699. DMA_FROM_DEVICE);
  700. kfree(atmel_port->pdc_rx[0].buf);
  701. }
  702. free_irq(port->irq, port);
  703. return -ENOMEM;
  704. }
  705. pdc->dma_addr = dma_map_single(port->dev,
  706. pdc->buf,
  707. PDC_BUFFER_SIZE,
  708. DMA_FROM_DEVICE);
  709. pdc->dma_size = PDC_BUFFER_SIZE;
  710. pdc->ofs = 0;
  711. }
  712. atmel_port->pdc_rx_idx = 0;
  713. UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
  714. UART_PUT_RCR(port, PDC_BUFFER_SIZE);
  715. UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
  716. UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
  717. }
  718. if (atmel_use_dma_tx(port)) {
  719. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  720. struct circ_buf *xmit = &port->info->xmit;
  721. pdc->buf = xmit->buf;
  722. pdc->dma_addr = dma_map_single(port->dev,
  723. pdc->buf,
  724. UART_XMIT_SIZE,
  725. DMA_TO_DEVICE);
  726. pdc->dma_size = UART_XMIT_SIZE;
  727. pdc->ofs = 0;
  728. }
  729. /*
  730. * If there is a specific "open" function (to register
  731. * control line interrupts)
  732. */
  733. if (atmel_open_hook) {
  734. retval = atmel_open_hook(port);
  735. if (retval) {
  736. free_irq(port->irq, port);
  737. return retval;
  738. }
  739. }
  740. /*
  741. * Finally, enable the serial port
  742. */
  743. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  744. /* enable xmit & rcvr */
  745. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  746. if (atmel_use_dma_rx(port)) {
  747. /* set UART timeout */
  748. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  749. UART_PUT_CR(port, ATMEL_US_STTTO);
  750. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  751. /* enable PDC controller */
  752. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  753. } else {
  754. /* enable receive only */
  755. UART_PUT_IER(port, ATMEL_US_RXRDY);
  756. }
  757. return 0;
  758. }
  759. /*
  760. * Disable the port
  761. */
  762. static void atmel_shutdown(struct uart_port *port)
  763. {
  764. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  765. /*
  766. * Ensure everything is stopped.
  767. */
  768. atmel_stop_rx(port);
  769. atmel_stop_tx(port);
  770. /*
  771. * Shut-down the DMA.
  772. */
  773. if (atmel_use_dma_rx(port)) {
  774. int i;
  775. for (i = 0; i < 2; i++) {
  776. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  777. dma_unmap_single(port->dev,
  778. pdc->dma_addr,
  779. pdc->dma_size,
  780. DMA_FROM_DEVICE);
  781. kfree(pdc->buf);
  782. }
  783. }
  784. if (atmel_use_dma_tx(port)) {
  785. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  786. dma_unmap_single(port->dev,
  787. pdc->dma_addr,
  788. pdc->dma_size,
  789. DMA_TO_DEVICE);
  790. }
  791. /*
  792. * Disable all interrupts, port and break condition.
  793. */
  794. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  795. UART_PUT_IDR(port, -1);
  796. /*
  797. * Free the interrupt
  798. */
  799. free_irq(port->irq, port);
  800. /*
  801. * If there is a specific "close" function (to unregister
  802. * control line interrupts)
  803. */
  804. if (atmel_close_hook)
  805. atmel_close_hook(port);
  806. }
  807. /*
  808. * Power / Clock management.
  809. */
  810. static void atmel_serial_pm(struct uart_port *port, unsigned int state,
  811. unsigned int oldstate)
  812. {
  813. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  814. switch (state) {
  815. case 0:
  816. /*
  817. * Enable the peripheral clock for this serial port.
  818. * This is called on uart_open() or a resume event.
  819. */
  820. clk_enable(atmel_port->clk);
  821. break;
  822. case 3:
  823. /*
  824. * Disable the peripheral clock for this serial port.
  825. * This is called on uart_close() or a suspend event.
  826. */
  827. clk_disable(atmel_port->clk);
  828. break;
  829. default:
  830. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  831. }
  832. }
  833. /*
  834. * Change the port parameters
  835. */
  836. static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  837. struct ktermios *old)
  838. {
  839. unsigned long flags;
  840. unsigned int mode, imr, quot, baud;
  841. /* Get current mode register */
  842. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
  843. | ATMEL_US_NBSTOP | ATMEL_US_PAR);
  844. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  845. quot = uart_get_divisor(port, baud);
  846. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  847. quot /= 8;
  848. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  849. }
  850. /* byte size */
  851. switch (termios->c_cflag & CSIZE) {
  852. case CS5:
  853. mode |= ATMEL_US_CHRL_5;
  854. break;
  855. case CS6:
  856. mode |= ATMEL_US_CHRL_6;
  857. break;
  858. case CS7:
  859. mode |= ATMEL_US_CHRL_7;
  860. break;
  861. default:
  862. mode |= ATMEL_US_CHRL_8;
  863. break;
  864. }
  865. /* stop bits */
  866. if (termios->c_cflag & CSTOPB)
  867. mode |= ATMEL_US_NBSTOP_2;
  868. /* parity */
  869. if (termios->c_cflag & PARENB) {
  870. /* Mark or Space parity */
  871. if (termios->c_cflag & CMSPAR) {
  872. if (termios->c_cflag & PARODD)
  873. mode |= ATMEL_US_PAR_MARK;
  874. else
  875. mode |= ATMEL_US_PAR_SPACE;
  876. } else if (termios->c_cflag & PARODD)
  877. mode |= ATMEL_US_PAR_ODD;
  878. else
  879. mode |= ATMEL_US_PAR_EVEN;
  880. } else
  881. mode |= ATMEL_US_PAR_NONE;
  882. spin_lock_irqsave(&port->lock, flags);
  883. port->read_status_mask = ATMEL_US_OVRE;
  884. if (termios->c_iflag & INPCK)
  885. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  886. if (termios->c_iflag & (BRKINT | PARMRK))
  887. port->read_status_mask |= ATMEL_US_RXBRK;
  888. if (atmel_use_dma_rx(port))
  889. /* need to enable error interrupts */
  890. UART_PUT_IER(port, port->read_status_mask);
  891. /*
  892. * Characters to ignore
  893. */
  894. port->ignore_status_mask = 0;
  895. if (termios->c_iflag & IGNPAR)
  896. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  897. if (termios->c_iflag & IGNBRK) {
  898. port->ignore_status_mask |= ATMEL_US_RXBRK;
  899. /*
  900. * If we're ignoring parity and break indicators,
  901. * ignore overruns too (for real raw support).
  902. */
  903. if (termios->c_iflag & IGNPAR)
  904. port->ignore_status_mask |= ATMEL_US_OVRE;
  905. }
  906. /* TODO: Ignore all characters if CREAD is set.*/
  907. /* update the per-port timeout */
  908. uart_update_timeout(port, termios->c_cflag, baud);
  909. /* save/disable interrupts and drain transmitter */
  910. imr = UART_GET_IMR(port);
  911. UART_PUT_IDR(port, -1);
  912. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  913. cpu_relax();
  914. /* disable receiver and transmitter */
  915. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  916. /* set the parity, stop bits and data size */
  917. UART_PUT_MR(port, mode);
  918. /* set the baud rate */
  919. UART_PUT_BRGR(port, quot);
  920. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  921. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  922. /* restore interrupts */
  923. UART_PUT_IER(port, imr);
  924. /* CTS flow-control and modem-status interrupts */
  925. if (UART_ENABLE_MS(port, termios->c_cflag))
  926. port->ops->enable_ms(port);
  927. spin_unlock_irqrestore(&port->lock, flags);
  928. }
  929. /*
  930. * Return string describing the specified port
  931. */
  932. static const char *atmel_type(struct uart_port *port)
  933. {
  934. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  935. }
  936. /*
  937. * Release the memory region(s) being used by 'port'.
  938. */
  939. static void atmel_release_port(struct uart_port *port)
  940. {
  941. struct platform_device *pdev = to_platform_device(port->dev);
  942. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  943. release_mem_region(port->mapbase, size);
  944. if (port->flags & UPF_IOREMAP) {
  945. iounmap(port->membase);
  946. port->membase = NULL;
  947. }
  948. }
  949. /*
  950. * Request the memory region(s) being used by 'port'.
  951. */
  952. static int atmel_request_port(struct uart_port *port)
  953. {
  954. struct platform_device *pdev = to_platform_device(port->dev);
  955. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  956. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  957. return -EBUSY;
  958. if (port->flags & UPF_IOREMAP) {
  959. port->membase = ioremap(port->mapbase, size);
  960. if (port->membase == NULL) {
  961. release_mem_region(port->mapbase, size);
  962. return -ENOMEM;
  963. }
  964. }
  965. return 0;
  966. }
  967. /*
  968. * Configure/autoconfigure the port.
  969. */
  970. static void atmel_config_port(struct uart_port *port, int flags)
  971. {
  972. if (flags & UART_CONFIG_TYPE) {
  973. port->type = PORT_ATMEL;
  974. atmel_request_port(port);
  975. }
  976. }
  977. /*
  978. * Verify the new serial_struct (for TIOCSSERIAL).
  979. */
  980. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  981. {
  982. int ret = 0;
  983. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  984. ret = -EINVAL;
  985. if (port->irq != ser->irq)
  986. ret = -EINVAL;
  987. if (ser->io_type != SERIAL_IO_MEM)
  988. ret = -EINVAL;
  989. if (port->uartclk / 16 != ser->baud_base)
  990. ret = -EINVAL;
  991. if ((void *)port->mapbase != ser->iomem_base)
  992. ret = -EINVAL;
  993. if (port->iobase != ser->port)
  994. ret = -EINVAL;
  995. if (ser->hub6 != 0)
  996. ret = -EINVAL;
  997. return ret;
  998. }
  999. static struct uart_ops atmel_pops = {
  1000. .tx_empty = atmel_tx_empty,
  1001. .set_mctrl = atmel_set_mctrl,
  1002. .get_mctrl = atmel_get_mctrl,
  1003. .stop_tx = atmel_stop_tx,
  1004. .start_tx = atmel_start_tx,
  1005. .stop_rx = atmel_stop_rx,
  1006. .enable_ms = atmel_enable_ms,
  1007. .break_ctl = atmel_break_ctl,
  1008. .startup = atmel_startup,
  1009. .shutdown = atmel_shutdown,
  1010. .set_termios = atmel_set_termios,
  1011. .type = atmel_type,
  1012. .release_port = atmel_release_port,
  1013. .request_port = atmel_request_port,
  1014. .config_port = atmel_config_port,
  1015. .verify_port = atmel_verify_port,
  1016. .pm = atmel_serial_pm,
  1017. };
  1018. /*
  1019. * Configure the port from the platform device resource info.
  1020. */
  1021. static void __devinit atmel_init_port(struct atmel_uart_port *atmel_port,
  1022. struct platform_device *pdev)
  1023. {
  1024. struct uart_port *port = &atmel_port->uart;
  1025. struct atmel_uart_data *data = pdev->dev.platform_data;
  1026. port->iotype = UPIO_MEM;
  1027. port->flags = UPF_BOOT_AUTOCONF;
  1028. port->ops = &atmel_pops;
  1029. port->fifosize = 1;
  1030. port->line = pdev->id;
  1031. port->dev = &pdev->dev;
  1032. port->mapbase = pdev->resource[0].start;
  1033. port->irq = pdev->resource[1].start;
  1034. tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
  1035. (unsigned long)port);
  1036. memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
  1037. if (data->regs)
  1038. /* Already mapped by setup code */
  1039. port->membase = data->regs;
  1040. else {
  1041. port->flags |= UPF_IOREMAP;
  1042. port->membase = NULL;
  1043. }
  1044. /* for console, the clock could already be configured */
  1045. if (!atmel_port->clk) {
  1046. atmel_port->clk = clk_get(&pdev->dev, "usart");
  1047. clk_enable(atmel_port->clk);
  1048. port->uartclk = clk_get_rate(atmel_port->clk);
  1049. }
  1050. atmel_port->use_dma_rx = data->use_dma_rx;
  1051. atmel_port->use_dma_tx = data->use_dma_tx;
  1052. if (atmel_use_dma_tx(port))
  1053. port->fifosize = PDC_BUFFER_SIZE;
  1054. }
  1055. /*
  1056. * Register board-specific modem-control line handlers.
  1057. */
  1058. void __init atmel_register_uart_fns(struct atmel_port_fns *fns)
  1059. {
  1060. if (fns->enable_ms)
  1061. atmel_pops.enable_ms = fns->enable_ms;
  1062. if (fns->get_mctrl)
  1063. atmel_pops.get_mctrl = fns->get_mctrl;
  1064. if (fns->set_mctrl)
  1065. atmel_pops.set_mctrl = fns->set_mctrl;
  1066. atmel_open_hook = fns->open;
  1067. atmel_close_hook = fns->close;
  1068. atmel_pops.pm = fns->pm;
  1069. atmel_pops.set_wake = fns->set_wake;
  1070. }
  1071. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1072. static void atmel_console_putchar(struct uart_port *port, int ch)
  1073. {
  1074. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1075. cpu_relax();
  1076. UART_PUT_CHAR(port, ch);
  1077. }
  1078. /*
  1079. * Interrupts are disabled on entering
  1080. */
  1081. static void atmel_console_write(struct console *co, const char *s, u_int count)
  1082. {
  1083. struct uart_port *port = &atmel_ports[co->index].uart;
  1084. unsigned int status, imr;
  1085. unsigned int pdc_tx;
  1086. /*
  1087. * First, save IMR and then disable interrupts
  1088. */
  1089. imr = UART_GET_IMR(port);
  1090. UART_PUT_IDR(port, ATMEL_US_RXRDY | ATMEL_US_TXRDY);
  1091. /* Store PDC transmit status and disable it */
  1092. pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
  1093. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  1094. uart_console_write(port, s, count, atmel_console_putchar);
  1095. /*
  1096. * Finally, wait for transmitter to become empty
  1097. * and restore IMR
  1098. */
  1099. do {
  1100. status = UART_GET_CSR(port);
  1101. } while (!(status & ATMEL_US_TXRDY));
  1102. /* Restore PDC transmit status */
  1103. if (pdc_tx)
  1104. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  1105. /* set interrupts back the way they were */
  1106. UART_PUT_IER(port, imr);
  1107. }
  1108. /*
  1109. * If the port was already initialised (eg, by a boot loader),
  1110. * try to determine the current setup.
  1111. */
  1112. static void __init atmel_console_get_options(struct uart_port *port, int *baud,
  1113. int *parity, int *bits)
  1114. {
  1115. unsigned int mr, quot;
  1116. /*
  1117. * If the baud rate generator isn't running, the port wasn't
  1118. * initialized by the boot loader.
  1119. */
  1120. quot = UART_GET_BRGR(port) & ATMEL_US_CD;
  1121. if (!quot)
  1122. return;
  1123. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  1124. if (mr == ATMEL_US_CHRL_8)
  1125. *bits = 8;
  1126. else
  1127. *bits = 7;
  1128. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  1129. if (mr == ATMEL_US_PAR_EVEN)
  1130. *parity = 'e';
  1131. else if (mr == ATMEL_US_PAR_ODD)
  1132. *parity = 'o';
  1133. /*
  1134. * The serial core only rounds down when matching this to a
  1135. * supported baud rate. Make sure we don't end up slightly
  1136. * lower than one of those, as it would make us fall through
  1137. * to a much lower baud rate than we really want.
  1138. */
  1139. *baud = port->uartclk / (16 * (quot - 1));
  1140. }
  1141. static int __init atmel_console_setup(struct console *co, char *options)
  1142. {
  1143. struct uart_port *port = &atmel_ports[co->index].uart;
  1144. int baud = 115200;
  1145. int bits = 8;
  1146. int parity = 'n';
  1147. int flow = 'n';
  1148. if (port->membase == NULL) {
  1149. /* Port not initialized yet - delay setup */
  1150. return -ENODEV;
  1151. }
  1152. UART_PUT_IDR(port, -1);
  1153. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1154. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1155. if (options)
  1156. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1157. else
  1158. atmel_console_get_options(port, &baud, &parity, &bits);
  1159. return uart_set_options(port, co, baud, parity, bits, flow);
  1160. }
  1161. static struct uart_driver atmel_uart;
  1162. static struct console atmel_console = {
  1163. .name = ATMEL_DEVICENAME,
  1164. .write = atmel_console_write,
  1165. .device = uart_console_device,
  1166. .setup = atmel_console_setup,
  1167. .flags = CON_PRINTBUFFER,
  1168. .index = -1,
  1169. .data = &atmel_uart,
  1170. };
  1171. #define ATMEL_CONSOLE_DEVICE &atmel_console
  1172. /*
  1173. * Early console initialization (before VM subsystem initialized).
  1174. */
  1175. static int __init atmel_console_init(void)
  1176. {
  1177. if (atmel_default_console_device) {
  1178. add_preferred_console(ATMEL_DEVICENAME,
  1179. atmel_default_console_device->id, NULL);
  1180. atmel_init_port(&atmel_ports[atmel_default_console_device->id],
  1181. atmel_default_console_device);
  1182. register_console(&atmel_console);
  1183. }
  1184. return 0;
  1185. }
  1186. console_initcall(atmel_console_init);
  1187. /*
  1188. * Late console initialization.
  1189. */
  1190. static int __init atmel_late_console_init(void)
  1191. {
  1192. if (atmel_default_console_device
  1193. && !(atmel_console.flags & CON_ENABLED))
  1194. register_console(&atmel_console);
  1195. return 0;
  1196. }
  1197. core_initcall(atmel_late_console_init);
  1198. static inline bool atmel_is_console_port(struct uart_port *port)
  1199. {
  1200. return port->cons && port->cons->index == port->line;
  1201. }
  1202. #else
  1203. #define ATMEL_CONSOLE_DEVICE NULL
  1204. static inline bool atmel_is_console_port(struct uart_port *port)
  1205. {
  1206. return false;
  1207. }
  1208. #endif
  1209. static struct uart_driver atmel_uart = {
  1210. .owner = THIS_MODULE,
  1211. .driver_name = "atmel_serial",
  1212. .dev_name = ATMEL_DEVICENAME,
  1213. .major = SERIAL_ATMEL_MAJOR,
  1214. .minor = MINOR_START,
  1215. .nr = ATMEL_MAX_UART,
  1216. .cons = ATMEL_CONSOLE_DEVICE,
  1217. };
  1218. #ifdef CONFIG_PM
  1219. static bool atmel_serial_clk_will_stop(void)
  1220. {
  1221. #ifdef CONFIG_ARCH_AT91
  1222. return at91_suspend_entering_slow_clock();
  1223. #else
  1224. return false;
  1225. #endif
  1226. }
  1227. static int atmel_serial_suspend(struct platform_device *pdev,
  1228. pm_message_t state)
  1229. {
  1230. struct uart_port *port = platform_get_drvdata(pdev);
  1231. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1232. if (atmel_is_console_port(port) && console_suspend_enabled) {
  1233. /* Drain the TX shifter */
  1234. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  1235. cpu_relax();
  1236. }
  1237. if (device_may_wakeup(&pdev->dev)
  1238. && !atmel_serial_clk_will_stop())
  1239. enable_irq_wake(port->irq);
  1240. else {
  1241. uart_suspend_port(&atmel_uart, port);
  1242. atmel_port->suspended = 1;
  1243. }
  1244. return 0;
  1245. }
  1246. static int atmel_serial_resume(struct platform_device *pdev)
  1247. {
  1248. struct uart_port *port = platform_get_drvdata(pdev);
  1249. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1250. if (atmel_port->suspended) {
  1251. uart_resume_port(&atmel_uart, port);
  1252. atmel_port->suspended = 0;
  1253. } else
  1254. disable_irq_wake(port->irq);
  1255. return 0;
  1256. }
  1257. #else
  1258. #define atmel_serial_suspend NULL
  1259. #define atmel_serial_resume NULL
  1260. #endif
  1261. static int __devinit atmel_serial_probe(struct platform_device *pdev)
  1262. {
  1263. struct atmel_uart_port *port;
  1264. void *data;
  1265. int ret;
  1266. BUILD_BUG_ON(!is_power_of_2(ATMEL_SERIAL_RINGSIZE));
  1267. port = &atmel_ports[pdev->id];
  1268. atmel_init_port(port, pdev);
  1269. if (!atmel_use_dma_rx(&port->uart)) {
  1270. ret = -ENOMEM;
  1271. data = kmalloc(sizeof(struct atmel_uart_char)
  1272. * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
  1273. if (!data)
  1274. goto err_alloc_ring;
  1275. port->rx_ring.buf = data;
  1276. }
  1277. ret = uart_add_one_port(&atmel_uart, &port->uart);
  1278. if (ret)
  1279. goto err_add_port;
  1280. device_init_wakeup(&pdev->dev, 1);
  1281. platform_set_drvdata(pdev, port);
  1282. return 0;
  1283. err_add_port:
  1284. kfree(port->rx_ring.buf);
  1285. port->rx_ring.buf = NULL;
  1286. err_alloc_ring:
  1287. if (!atmel_is_console_port(&port->uart)) {
  1288. clk_disable(port->clk);
  1289. clk_put(port->clk);
  1290. port->clk = NULL;
  1291. }
  1292. return ret;
  1293. }
  1294. static int __devexit atmel_serial_remove(struct platform_device *pdev)
  1295. {
  1296. struct uart_port *port = platform_get_drvdata(pdev);
  1297. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1298. int ret = 0;
  1299. device_init_wakeup(&pdev->dev, 0);
  1300. platform_set_drvdata(pdev, NULL);
  1301. ret = uart_remove_one_port(&atmel_uart, port);
  1302. tasklet_kill(&atmel_port->tasklet);
  1303. kfree(atmel_port->rx_ring.buf);
  1304. /* "port" is allocated statically, so we shouldn't free it */
  1305. clk_disable(atmel_port->clk);
  1306. clk_put(atmel_port->clk);
  1307. return ret;
  1308. }
  1309. static struct platform_driver atmel_serial_driver = {
  1310. .probe = atmel_serial_probe,
  1311. .remove = __devexit_p(atmel_serial_remove),
  1312. .suspend = atmel_serial_suspend,
  1313. .resume = atmel_serial_resume,
  1314. .driver = {
  1315. .name = "atmel_usart",
  1316. .owner = THIS_MODULE,
  1317. },
  1318. };
  1319. static int __init atmel_serial_init(void)
  1320. {
  1321. int ret;
  1322. ret = uart_register_driver(&atmel_uart);
  1323. if (ret)
  1324. return ret;
  1325. ret = platform_driver_register(&atmel_serial_driver);
  1326. if (ret)
  1327. uart_unregister_driver(&atmel_uart);
  1328. return ret;
  1329. }
  1330. static void __exit atmel_serial_exit(void)
  1331. {
  1332. platform_driver_unregister(&atmel_serial_driver);
  1333. uart_unregister_driver(&atmel_uart);
  1334. }
  1335. module_init(atmel_serial_init);
  1336. module_exit(atmel_serial_exit);
  1337. MODULE_AUTHOR("Rick Bronson");
  1338. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  1339. MODULE_LICENSE("GPL");
  1340. MODULE_ALIAS("platform:atmel_usart");