atmel_serial.c 44 KB

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