atmel_serial.c 40 KB

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