atmel_serial.c 47 KB

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