atmel_serial.c 47 KB

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