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