atmel_serial.c 44 KB

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