atmel_serial.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393
  1. /*
  2. * Driver for Atmel AT91 / AT32 Serial ports
  3. * Copyright (C) 2003 Rick Bronson
  4. *
  5. * Based on drivers/char/serial_sa1100.c, by Deep Blue Solutions Ltd.
  6. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  7. *
  8. * DMA support added by Chip Coldwell.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/tty.h>
  27. #include <linux/ioport.h>
  28. #include <linux/slab.h>
  29. #include <linux/init.h>
  30. #include <linux/serial.h>
  31. #include <linux/clk.h>
  32. #include <linux/console.h>
  33. #include <linux/sysrq.h>
  34. #include <linux/tty_flip.h>
  35. #include <linux/platform_device.h>
  36. #include <linux/of.h>
  37. #include <linux/of_device.h>
  38. #include <linux/dma-mapping.h>
  39. #include <linux/atmel_pdc.h>
  40. #include <linux/atmel_serial.h>
  41. #include <linux/uaccess.h>
  42. #include <linux/platform_data/atmel.h>
  43. #include <asm/io.h>
  44. #include <asm/ioctls.h>
  45. #ifdef CONFIG_ARM
  46. #include <mach/cpu.h>
  47. #include <asm/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. bool use_dma_rx; /* enable DMA receiver */
  121. bool use_pdc_rx; /* enable PDC receiver */
  122. short pdc_rx_idx; /* current PDC RX buffer */
  123. struct atmel_dma_buffer pdc_rx[2]; /* PDC receier */
  124. bool use_dma_tx; /* enable DMA transmitter */
  125. bool use_pdc_tx; /* enable PDC transmitter */
  126. struct atmel_dma_buffer pdc_tx; /* PDC transmitter */
  127. spinlock_t lock_tx; /* port lock */
  128. spinlock_t lock_rx; /* port lock */
  129. struct dma_chan *chan_tx;
  130. struct dma_chan *chan_rx;
  131. struct dma_async_tx_descriptor *desc_tx;
  132. struct dma_async_tx_descriptor *desc_rx;
  133. dma_cookie_t cookie_tx;
  134. dma_cookie_t cookie_rx;
  135. struct scatterlist sg_tx;
  136. struct scatterlist sg_rx;
  137. struct tasklet_struct tasklet;
  138. unsigned int irq_status;
  139. unsigned int irq_status_prev;
  140. struct circ_buf rx_ring;
  141. struct serial_rs485 rs485; /* rs485 settings */
  142. unsigned int tx_done_mask;
  143. int (*prepare_rx)(struct uart_port *port);
  144. int (*prepare_tx)(struct uart_port *port);
  145. void (*schedule_rx)(struct uart_port *port);
  146. void (*schedule_tx)(struct uart_port *port);
  147. void (*release_rx)(struct uart_port *port);
  148. void (*release_tx)(struct uart_port *port);
  149. };
  150. static struct atmel_uart_port atmel_ports[ATMEL_MAX_UART];
  151. static DECLARE_BITMAP(atmel_ports_in_use, ATMEL_MAX_UART);
  152. #ifdef SUPPORT_SYSRQ
  153. static struct console atmel_console;
  154. #endif
  155. #if defined(CONFIG_OF)
  156. static const struct of_device_id atmel_serial_dt_ids[] = {
  157. { .compatible = "atmel,at91rm9200-usart" },
  158. { .compatible = "atmel,at91sam9260-usart" },
  159. { /* sentinel */ }
  160. };
  161. MODULE_DEVICE_TABLE(of, atmel_serial_dt_ids);
  162. #endif
  163. static inline struct atmel_uart_port *
  164. to_atmel_uart_port(struct uart_port *uart)
  165. {
  166. return container_of(uart, struct atmel_uart_port, uart);
  167. }
  168. #ifdef CONFIG_SERIAL_ATMEL_PDC
  169. static bool atmel_use_pdc_rx(struct uart_port *port)
  170. {
  171. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  172. return atmel_port->use_pdc_rx;
  173. }
  174. static bool atmel_use_pdc_tx(struct uart_port *port)
  175. {
  176. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  177. return atmel_port->use_pdc_tx;
  178. }
  179. #else
  180. static bool atmel_use_pdc_rx(struct uart_port *port)
  181. {
  182. return false;
  183. }
  184. static bool atmel_use_pdc_tx(struct uart_port *port)
  185. {
  186. return false;
  187. }
  188. #endif
  189. static bool atmel_use_dma_tx(struct uart_port *port)
  190. {
  191. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  192. return atmel_port->use_dma_tx;
  193. }
  194. static bool atmel_use_dma_rx(struct uart_port *port)
  195. {
  196. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  197. return atmel_port->use_dma_rx;
  198. }
  199. /* Enable or disable the rs485 support */
  200. void atmel_config_rs485(struct uart_port *port, struct serial_rs485 *rs485conf)
  201. {
  202. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  203. unsigned int mode;
  204. unsigned long flags;
  205. spin_lock_irqsave(&port->lock, flags);
  206. /* Disable interrupts */
  207. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  208. mode = UART_GET_MR(port);
  209. /* Resetting serial mode to RS232 (0x0) */
  210. mode &= ~ATMEL_US_USMODE;
  211. atmel_port->rs485 = *rs485conf;
  212. if (rs485conf->flags & SER_RS485_ENABLED) {
  213. dev_dbg(port->dev, "Setting UART to RS485\n");
  214. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  215. if ((rs485conf->delay_rts_after_send) > 0)
  216. UART_PUT_TTGR(port, rs485conf->delay_rts_after_send);
  217. mode |= ATMEL_US_USMODE_RS485;
  218. } else {
  219. dev_dbg(port->dev, "Setting UART to RS232\n");
  220. if (atmel_use_pdc_tx(port))
  221. atmel_port->tx_done_mask = ATMEL_US_ENDTX |
  222. ATMEL_US_TXBUFE;
  223. else
  224. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  225. }
  226. UART_PUT_MR(port, mode);
  227. /* Enable interrupts */
  228. UART_PUT_IER(port, atmel_port->tx_done_mask);
  229. spin_unlock_irqrestore(&port->lock, flags);
  230. }
  231. /*
  232. * Return TIOCSER_TEMT when transmitter FIFO and Shift register is empty.
  233. */
  234. static u_int atmel_tx_empty(struct uart_port *port)
  235. {
  236. return (UART_GET_CSR(port) & ATMEL_US_TXEMPTY) ? TIOCSER_TEMT : 0;
  237. }
  238. /*
  239. * Set state of the modem control output lines
  240. */
  241. static void atmel_set_mctrl(struct uart_port *port, u_int mctrl)
  242. {
  243. unsigned int control = 0;
  244. unsigned int mode;
  245. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  246. #ifdef CONFIG_ARCH_AT91RM9200
  247. if (cpu_is_at91rm9200()) {
  248. /*
  249. * AT91RM9200 Errata #39: RTS0 is not internally connected
  250. * to PA21. We need to drive the pin manually.
  251. */
  252. if (port->mapbase == AT91RM9200_BASE_US0) {
  253. if (mctrl & TIOCM_RTS)
  254. at91_set_gpio_value(AT91_PIN_PA21, 0);
  255. else
  256. at91_set_gpio_value(AT91_PIN_PA21, 1);
  257. }
  258. }
  259. #endif
  260. if (mctrl & TIOCM_RTS)
  261. control |= ATMEL_US_RTSEN;
  262. else
  263. control |= ATMEL_US_RTSDIS;
  264. if (mctrl & TIOCM_DTR)
  265. control |= ATMEL_US_DTREN;
  266. else
  267. control |= ATMEL_US_DTRDIS;
  268. UART_PUT_CR(port, control);
  269. /* Local loopback mode? */
  270. mode = UART_GET_MR(port) & ~ATMEL_US_CHMODE;
  271. if (mctrl & TIOCM_LOOP)
  272. mode |= ATMEL_US_CHMODE_LOC_LOOP;
  273. else
  274. mode |= ATMEL_US_CHMODE_NORMAL;
  275. /* Resetting serial mode to RS232 (0x0) */
  276. mode &= ~ATMEL_US_USMODE;
  277. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  278. dev_dbg(port->dev, "Setting UART to RS485\n");
  279. if ((atmel_port->rs485.delay_rts_after_send) > 0)
  280. UART_PUT_TTGR(port,
  281. atmel_port->rs485.delay_rts_after_send);
  282. mode |= ATMEL_US_USMODE_RS485;
  283. } else {
  284. dev_dbg(port->dev, "Setting UART to RS232\n");
  285. }
  286. UART_PUT_MR(port, mode);
  287. }
  288. /*
  289. * Get state of the modem control input lines
  290. */
  291. static u_int atmel_get_mctrl(struct uart_port *port)
  292. {
  293. unsigned int status, ret = 0;
  294. status = UART_GET_CSR(port);
  295. /*
  296. * The control signals are active low.
  297. */
  298. if (!(status & ATMEL_US_DCD))
  299. ret |= TIOCM_CD;
  300. if (!(status & ATMEL_US_CTS))
  301. ret |= TIOCM_CTS;
  302. if (!(status & ATMEL_US_DSR))
  303. ret |= TIOCM_DSR;
  304. if (!(status & ATMEL_US_RI))
  305. ret |= TIOCM_RI;
  306. return ret;
  307. }
  308. /*
  309. * Stop transmitting.
  310. */
  311. static void atmel_stop_tx(struct uart_port *port)
  312. {
  313. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  314. if (atmel_use_pdc_tx(port)) {
  315. /* disable PDC transmit */
  316. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  317. }
  318. /* Disable interrupts */
  319. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  320. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  321. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
  322. atmel_start_rx(port);
  323. }
  324. /*
  325. * Start transmitting.
  326. */
  327. static void atmel_start_tx(struct uart_port *port)
  328. {
  329. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  330. if (atmel_use_pdc_tx(port)) {
  331. if (UART_GET_PTSR(port) & ATMEL_PDC_TXTEN)
  332. /* The transmitter is already running. Yes, we
  333. really need this.*/
  334. return;
  335. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  336. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX))
  337. atmel_stop_rx(port);
  338. /* re-enable PDC transmit */
  339. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  340. }
  341. /* Enable interrupts */
  342. UART_PUT_IER(port, atmel_port->tx_done_mask);
  343. }
  344. /*
  345. * start receiving - port is in process of being opened.
  346. */
  347. static void atmel_start_rx(struct uart_port *port)
  348. {
  349. UART_PUT_CR(port, ATMEL_US_RSTSTA); /* reset status and receiver */
  350. UART_PUT_CR(port, ATMEL_US_RXEN);
  351. if (atmel_use_pdc_rx(port)) {
  352. /* enable PDC controller */
  353. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  354. port->read_status_mask);
  355. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  356. } else {
  357. UART_PUT_IER(port, ATMEL_US_RXRDY);
  358. }
  359. }
  360. /*
  361. * Stop receiving - port is in process of being closed.
  362. */
  363. static void atmel_stop_rx(struct uart_port *port)
  364. {
  365. UART_PUT_CR(port, ATMEL_US_RXDIS);
  366. if (atmel_use_pdc_rx(port)) {
  367. /* disable PDC receive */
  368. UART_PUT_PTCR(port, ATMEL_PDC_RXTDIS);
  369. UART_PUT_IDR(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT |
  370. port->read_status_mask);
  371. } else {
  372. UART_PUT_IDR(port, ATMEL_US_RXRDY);
  373. }
  374. }
  375. /*
  376. * Enable modem status interrupts
  377. */
  378. static void atmel_enable_ms(struct uart_port *port)
  379. {
  380. UART_PUT_IER(port, ATMEL_US_RIIC | ATMEL_US_DSRIC
  381. | ATMEL_US_DCDIC | ATMEL_US_CTSIC);
  382. }
  383. /*
  384. * Control the transmission of a break signal
  385. */
  386. static void atmel_break_ctl(struct uart_port *port, int break_state)
  387. {
  388. if (break_state != 0)
  389. UART_PUT_CR(port, ATMEL_US_STTBRK); /* start break */
  390. else
  391. UART_PUT_CR(port, ATMEL_US_STPBRK); /* stop break */
  392. }
  393. /*
  394. * Stores the incoming character in the ring buffer
  395. */
  396. static void
  397. atmel_buffer_rx_char(struct uart_port *port, unsigned int status,
  398. unsigned int ch)
  399. {
  400. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  401. struct circ_buf *ring = &atmel_port->rx_ring;
  402. struct atmel_uart_char *c;
  403. if (!CIRC_SPACE(ring->head, ring->tail, ATMEL_SERIAL_RINGSIZE))
  404. /* Buffer overflow, ignore char */
  405. return;
  406. c = &((struct atmel_uart_char *)ring->buf)[ring->head];
  407. c->status = status;
  408. c->ch = ch;
  409. /* Make sure the character is stored before we update head. */
  410. smp_wmb();
  411. ring->head = (ring->head + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  412. }
  413. /*
  414. * Deal with parity, framing and overrun errors.
  415. */
  416. static void atmel_pdc_rxerr(struct uart_port *port, unsigned int status)
  417. {
  418. /* clear error */
  419. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  420. if (status & ATMEL_US_RXBRK) {
  421. /* ignore side-effect */
  422. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  423. port->icount.brk++;
  424. }
  425. if (status & ATMEL_US_PARE)
  426. port->icount.parity++;
  427. if (status & ATMEL_US_FRAME)
  428. port->icount.frame++;
  429. if (status & ATMEL_US_OVRE)
  430. port->icount.overrun++;
  431. }
  432. /*
  433. * Characters received (called from interrupt handler)
  434. */
  435. static void atmel_rx_chars(struct uart_port *port)
  436. {
  437. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  438. unsigned int status, ch;
  439. status = UART_GET_CSR(port);
  440. while (status & ATMEL_US_RXRDY) {
  441. ch = UART_GET_CHAR(port);
  442. /*
  443. * note that the error handling code is
  444. * out of the main execution path
  445. */
  446. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  447. | ATMEL_US_OVRE | ATMEL_US_RXBRK)
  448. || atmel_port->break_active)) {
  449. /* clear error */
  450. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  451. if (status & ATMEL_US_RXBRK
  452. && !atmel_port->break_active) {
  453. atmel_port->break_active = 1;
  454. UART_PUT_IER(port, ATMEL_US_RXBRK);
  455. } else {
  456. /*
  457. * This is either the end-of-break
  458. * condition or we've received at
  459. * least one character without RXBRK
  460. * being set. In both cases, the next
  461. * RXBRK will indicate start-of-break.
  462. */
  463. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  464. status &= ~ATMEL_US_RXBRK;
  465. atmel_port->break_active = 0;
  466. }
  467. }
  468. atmel_buffer_rx_char(port, status, ch);
  469. status = UART_GET_CSR(port);
  470. }
  471. tasklet_schedule(&atmel_port->tasklet);
  472. }
  473. /*
  474. * Transmit characters (called from tasklet with TXRDY interrupt
  475. * disabled)
  476. */
  477. static void atmel_tx_chars(struct uart_port *port)
  478. {
  479. struct circ_buf *xmit = &port->state->xmit;
  480. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  481. if (port->x_char && UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  482. UART_PUT_CHAR(port, port->x_char);
  483. port->icount.tx++;
  484. port->x_char = 0;
  485. }
  486. if (uart_circ_empty(xmit) || uart_tx_stopped(port))
  487. return;
  488. while (UART_GET_CSR(port) & atmel_port->tx_done_mask) {
  489. UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
  490. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  491. port->icount.tx++;
  492. if (uart_circ_empty(xmit))
  493. break;
  494. }
  495. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  496. uart_write_wakeup(port);
  497. if (!uart_circ_empty(xmit))
  498. /* Enable interrupts */
  499. UART_PUT_IER(port, atmel_port->tx_done_mask);
  500. }
  501. static void atmel_complete_tx_dma(void *arg)
  502. {
  503. struct atmel_uart_port *atmel_port = arg;
  504. struct uart_port *port = &atmel_port->uart;
  505. struct circ_buf *xmit = &port->state->xmit;
  506. struct dma_chan *chan = atmel_port->chan_tx;
  507. unsigned long flags;
  508. spin_lock_irqsave(&port->lock, flags);
  509. if (chan)
  510. dmaengine_terminate_all(chan);
  511. xmit->tail += sg_dma_len(&atmel_port->sg_tx);
  512. xmit->tail &= UART_XMIT_SIZE - 1;
  513. port->icount.tx += sg_dma_len(&atmel_port->sg_tx);
  514. spin_lock_irq(&atmel_port->lock_tx);
  515. async_tx_ack(atmel_port->desc_tx);
  516. atmel_port->cookie_tx = -EINVAL;
  517. atmel_port->desc_tx = NULL;
  518. spin_unlock_irq(&atmel_port->lock_tx);
  519. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  520. uart_write_wakeup(port);
  521. /* Do we really need this? */
  522. if (!uart_circ_empty(xmit))
  523. tasklet_schedule(&atmel_port->tasklet);
  524. spin_unlock_irqrestore(&port->lock, flags);
  525. }
  526. static void atmel_release_tx_dma(struct uart_port *port)
  527. {
  528. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  529. struct dma_chan *chan = atmel_port->chan_tx;
  530. if (chan) {
  531. dmaengine_terminate_all(chan);
  532. dma_release_channel(chan);
  533. dma_unmap_sg(port->dev, &atmel_port->sg_tx, 1,
  534. DMA_MEM_TO_DEV);
  535. }
  536. atmel_port->desc_tx = NULL;
  537. atmel_port->chan_tx = NULL;
  538. atmel_port->cookie_tx = -EINVAL;
  539. }
  540. /*
  541. * Called from tasklet with TXRDY interrupt is disabled.
  542. */
  543. static void atmel_tx_dma(struct uart_port *port)
  544. {
  545. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  546. struct circ_buf *xmit = &port->state->xmit;
  547. struct dma_chan *chan = atmel_port->chan_tx;
  548. struct dma_async_tx_descriptor *desc;
  549. struct scatterlist *sg = &atmel_port->sg_tx;
  550. /* Make sure we have an idle channel */
  551. if (atmel_port->desc_tx != NULL)
  552. return;
  553. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  554. /*
  555. * DMA is idle now.
  556. * Port xmit buffer is already mapped,
  557. * and it is one page... Just adjust
  558. * offsets and lengths. Since it is a circular buffer,
  559. * we have to transmit till the end, and then the rest.
  560. * Take the port lock to get a
  561. * consistent xmit buffer state.
  562. */
  563. sg->offset = xmit->tail & (UART_XMIT_SIZE - 1);
  564. sg_dma_address(sg) = (sg_dma_address(sg) &
  565. ~(UART_XMIT_SIZE - 1))
  566. + sg->offset;
  567. sg_dma_len(sg) = CIRC_CNT_TO_END(xmit->head,
  568. xmit->tail,
  569. UART_XMIT_SIZE);
  570. BUG_ON(!sg_dma_len(sg));
  571. desc = dmaengine_prep_slave_sg(chan,
  572. sg,
  573. 1,
  574. DMA_MEM_TO_DEV,
  575. DMA_PREP_INTERRUPT |
  576. DMA_CTRL_ACK);
  577. if (!desc) {
  578. dev_err(port->dev, "Failed to send via dma!\n");
  579. return;
  580. }
  581. dma_sync_sg_for_device(port->dev, sg, 1, DMA_MEM_TO_DEV);
  582. atmel_port->desc_tx = desc;
  583. desc->callback = atmel_complete_tx_dma;
  584. desc->callback_param = atmel_port;
  585. atmel_port->cookie_tx = dmaengine_submit(desc);
  586. } else {
  587. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  588. /* DMA done, stop TX, start RX for RS485 */
  589. atmel_start_rx(port);
  590. }
  591. }
  592. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  593. uart_write_wakeup(port);
  594. }
  595. static int atmel_prepare_tx_dma(struct uart_port *port)
  596. {
  597. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  598. dma_cap_mask_t mask;
  599. struct dma_slave_config config;
  600. int ret, nent;
  601. dma_cap_zero(mask);
  602. dma_cap_set(DMA_SLAVE, mask);
  603. atmel_port->chan_tx = dma_request_slave_channel(port->dev, "tx");
  604. if (atmel_port->chan_tx == NULL)
  605. goto chan_err;
  606. dev_info(port->dev, "using %s for tx DMA transfers\n",
  607. dma_chan_name(atmel_port->chan_tx));
  608. spin_lock_init(&atmel_port->lock_tx);
  609. sg_init_table(&atmel_port->sg_tx, 1);
  610. /* UART circular tx buffer is an aligned page. */
  611. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  612. sg_set_page(&atmel_port->sg_tx,
  613. virt_to_page(port->state->xmit.buf),
  614. UART_XMIT_SIZE,
  615. (int)port->state->xmit.buf & ~PAGE_MASK);
  616. nent = dma_map_sg(port->dev,
  617. &atmel_port->sg_tx,
  618. 1,
  619. DMA_MEM_TO_DEV);
  620. if (!nent) {
  621. dev_dbg(port->dev, "need to release resource of dma\n");
  622. goto chan_err;
  623. } else {
  624. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  625. sg_dma_len(&atmel_port->sg_tx),
  626. port->state->xmit.buf,
  627. sg_dma_address(&atmel_port->sg_tx));
  628. }
  629. /* Configure the slave DMA */
  630. memset(&config, 0, sizeof(config));
  631. config.direction = DMA_MEM_TO_DEV;
  632. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  633. config.dst_addr = port->mapbase + ATMEL_US_THR;
  634. ret = dmaengine_device_control(atmel_port->chan_tx,
  635. DMA_SLAVE_CONFIG,
  636. (unsigned long)&config);
  637. if (ret) {
  638. dev_err(port->dev, "DMA tx slave configuration failed\n");
  639. goto chan_err;
  640. }
  641. return 0;
  642. chan_err:
  643. dev_err(port->dev, "TX channel not available, switch to pio\n");
  644. atmel_port->use_dma_tx = 0;
  645. if (atmel_port->chan_tx)
  646. atmel_release_tx_dma(port);
  647. return -EINVAL;
  648. }
  649. static void atmel_flip_buffer_rx_dma(struct uart_port *port,
  650. char *buf, size_t count)
  651. {
  652. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  653. struct tty_port *tport = &port->state->port;
  654. dma_sync_sg_for_cpu(port->dev,
  655. &atmel_port->sg_rx,
  656. 1,
  657. DMA_DEV_TO_MEM);
  658. tty_insert_flip_string(tport, buf, count);
  659. dma_sync_sg_for_device(port->dev,
  660. &atmel_port->sg_rx,
  661. 1,
  662. DMA_DEV_TO_MEM);
  663. /*
  664. * Drop the lock here since it might end up calling
  665. * uart_start(), which takes the lock.
  666. */
  667. spin_unlock(&port->lock);
  668. tty_flip_buffer_push(tport);
  669. spin_lock(&port->lock);
  670. }
  671. static void atmel_complete_rx_dma(void *arg)
  672. {
  673. struct uart_port *port = arg;
  674. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  675. tasklet_schedule(&atmel_port->tasklet);
  676. }
  677. static void atmel_release_rx_dma(struct uart_port *port)
  678. {
  679. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  680. struct dma_chan *chan = atmel_port->chan_rx;
  681. if (chan) {
  682. dmaengine_terminate_all(chan);
  683. dma_release_channel(chan);
  684. dma_unmap_sg(port->dev, &atmel_port->sg_rx, 1,
  685. DMA_DEV_TO_MEM);
  686. }
  687. atmel_port->desc_rx = NULL;
  688. atmel_port->chan_rx = NULL;
  689. atmel_port->cookie_rx = -EINVAL;
  690. }
  691. static void atmel_rx_from_dma(struct uart_port *port)
  692. {
  693. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  694. struct circ_buf *ring = &atmel_port->rx_ring;
  695. struct dma_chan *chan = atmel_port->chan_rx;
  696. struct dma_tx_state state;
  697. enum dma_status dmastat;
  698. size_t pending, count;
  699. /* Reset the UART timeout early so that we don't miss one */
  700. UART_PUT_CR(port, ATMEL_US_STTTO);
  701. dmastat = dmaengine_tx_status(chan,
  702. atmel_port->cookie_rx,
  703. &state);
  704. /* Restart a new tasklet if DMA status is error */
  705. if (dmastat == DMA_ERROR) {
  706. dev_dbg(port->dev, "Get residue error, restart tasklet\n");
  707. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  708. tasklet_schedule(&atmel_port->tasklet);
  709. return;
  710. }
  711. /* current transfer size should no larger than dma buffer */
  712. pending = sg_dma_len(&atmel_port->sg_rx) - state.residue;
  713. BUG_ON(pending > sg_dma_len(&atmel_port->sg_rx));
  714. /*
  715. * This will take the chars we have so far,
  716. * ring->head will record the transfer size, only new bytes come
  717. * will insert into the framework.
  718. */
  719. if (pending > ring->head) {
  720. count = pending - ring->head;
  721. atmel_flip_buffer_rx_dma(port, ring->buf + ring->head, count);
  722. ring->head += count;
  723. if (ring->head == sg_dma_len(&atmel_port->sg_rx))
  724. ring->head = 0;
  725. port->icount.rx += count;
  726. }
  727. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  728. }
  729. static int atmel_prepare_rx_dma(struct uart_port *port)
  730. {
  731. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  732. struct dma_async_tx_descriptor *desc;
  733. dma_cap_mask_t mask;
  734. struct dma_slave_config config;
  735. struct circ_buf *ring;
  736. int ret, nent;
  737. ring = &atmel_port->rx_ring;
  738. dma_cap_zero(mask);
  739. dma_cap_set(DMA_CYCLIC, mask);
  740. atmel_port->chan_rx = dma_request_slave_channel(port->dev, "rx");
  741. if (atmel_port->chan_rx == NULL)
  742. goto chan_err;
  743. dev_info(port->dev, "using %s for rx DMA transfers\n",
  744. dma_chan_name(atmel_port->chan_rx));
  745. spin_lock_init(&atmel_port->lock_rx);
  746. sg_init_table(&atmel_port->sg_rx, 1);
  747. /* UART circular rx buffer is an aligned page. */
  748. BUG_ON((int)port->state->xmit.buf & ~PAGE_MASK);
  749. sg_set_page(&atmel_port->sg_rx,
  750. virt_to_page(ring->buf),
  751. ATMEL_SERIAL_RINGSIZE,
  752. (int)ring->buf & ~PAGE_MASK);
  753. nent = dma_map_sg(port->dev,
  754. &atmel_port->sg_rx,
  755. 1,
  756. DMA_DEV_TO_MEM);
  757. if (!nent) {
  758. dev_dbg(port->dev, "need to release resource of dma\n");
  759. goto chan_err;
  760. } else {
  761. dev_dbg(port->dev, "%s: mapped %d@%p to %x\n", __func__,
  762. sg_dma_len(&atmel_port->sg_rx),
  763. ring->buf,
  764. sg_dma_address(&atmel_port->sg_rx));
  765. }
  766. /* Configure the slave DMA */
  767. memset(&config, 0, sizeof(config));
  768. config.direction = DMA_DEV_TO_MEM;
  769. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  770. config.src_addr = port->mapbase + ATMEL_US_RHR;
  771. ret = dmaengine_device_control(atmel_port->chan_rx,
  772. DMA_SLAVE_CONFIG,
  773. (unsigned long)&config);
  774. if (ret) {
  775. dev_err(port->dev, "DMA rx slave configuration failed\n");
  776. goto chan_err;
  777. }
  778. /*
  779. * Prepare a cyclic dma transfer, assign 2 descriptors,
  780. * each one is half ring buffer size
  781. */
  782. desc = dmaengine_prep_dma_cyclic(atmel_port->chan_rx,
  783. sg_dma_address(&atmel_port->sg_rx),
  784. sg_dma_len(&atmel_port->sg_rx),
  785. sg_dma_len(&atmel_port->sg_rx)/2,
  786. DMA_DEV_TO_MEM,
  787. DMA_PREP_INTERRUPT);
  788. desc->callback = atmel_complete_rx_dma;
  789. desc->callback_param = port;
  790. atmel_port->desc_rx = desc;
  791. atmel_port->cookie_rx = dmaengine_submit(desc);
  792. return 0;
  793. chan_err:
  794. dev_err(port->dev, "RX channel not available, switch to pio\n");
  795. atmel_port->use_dma_rx = 0;
  796. if (atmel_port->chan_rx)
  797. atmel_release_rx_dma(port);
  798. return -EINVAL;
  799. }
  800. /*
  801. * receive interrupt handler.
  802. */
  803. static void
  804. atmel_handle_receive(struct uart_port *port, unsigned int pending)
  805. {
  806. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  807. if (atmel_use_pdc_rx(port)) {
  808. /*
  809. * PDC receive. Just schedule the tasklet and let it
  810. * figure out the details.
  811. *
  812. * TODO: We're not handling error flags correctly at
  813. * the moment.
  814. */
  815. if (pending & (ATMEL_US_ENDRX | ATMEL_US_TIMEOUT)) {
  816. UART_PUT_IDR(port, (ATMEL_US_ENDRX
  817. | ATMEL_US_TIMEOUT));
  818. tasklet_schedule(&atmel_port->tasklet);
  819. }
  820. if (pending & (ATMEL_US_RXBRK | ATMEL_US_OVRE |
  821. ATMEL_US_FRAME | ATMEL_US_PARE))
  822. atmel_pdc_rxerr(port, pending);
  823. }
  824. if (atmel_use_dma_rx(port)) {
  825. if (pending & ATMEL_US_TIMEOUT) {
  826. UART_PUT_IDR(port, ATMEL_US_TIMEOUT);
  827. tasklet_schedule(&atmel_port->tasklet);
  828. }
  829. }
  830. /* Interrupt receive */
  831. if (pending & ATMEL_US_RXRDY)
  832. atmel_rx_chars(port);
  833. else if (pending & ATMEL_US_RXBRK) {
  834. /*
  835. * End of break detected. If it came along with a
  836. * character, atmel_rx_chars will handle it.
  837. */
  838. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  839. UART_PUT_IDR(port, ATMEL_US_RXBRK);
  840. atmel_port->break_active = 0;
  841. }
  842. }
  843. /*
  844. * transmit interrupt handler. (Transmit is IRQF_NODELAY safe)
  845. */
  846. static void
  847. atmel_handle_transmit(struct uart_port *port, unsigned int pending)
  848. {
  849. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  850. if (pending & atmel_port->tx_done_mask) {
  851. /* Either PDC or interrupt transmission */
  852. UART_PUT_IDR(port, atmel_port->tx_done_mask);
  853. tasklet_schedule(&atmel_port->tasklet);
  854. }
  855. }
  856. /*
  857. * status flags interrupt handler.
  858. */
  859. static void
  860. atmel_handle_status(struct uart_port *port, unsigned int pending,
  861. unsigned int status)
  862. {
  863. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  864. if (pending & (ATMEL_US_RIIC | ATMEL_US_DSRIC | ATMEL_US_DCDIC
  865. | ATMEL_US_CTSIC)) {
  866. atmel_port->irq_status = status;
  867. tasklet_schedule(&atmel_port->tasklet);
  868. }
  869. }
  870. /*
  871. * Interrupt handler
  872. */
  873. static irqreturn_t atmel_interrupt(int irq, void *dev_id)
  874. {
  875. struct uart_port *port = dev_id;
  876. unsigned int status, pending, pass_counter = 0;
  877. do {
  878. status = UART_GET_CSR(port);
  879. pending = status & UART_GET_IMR(port);
  880. if (!pending)
  881. break;
  882. atmel_handle_receive(port, pending);
  883. atmel_handle_status(port, pending, status);
  884. atmel_handle_transmit(port, pending);
  885. } while (pass_counter++ < ATMEL_ISR_PASS_LIMIT);
  886. return pass_counter ? IRQ_HANDLED : IRQ_NONE;
  887. }
  888. static void atmel_release_tx_pdc(struct uart_port *port)
  889. {
  890. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  891. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  892. dma_unmap_single(port->dev,
  893. pdc->dma_addr,
  894. pdc->dma_size,
  895. DMA_TO_DEVICE);
  896. }
  897. /*
  898. * Called from tasklet with ENDTX and TXBUFE interrupts disabled.
  899. */
  900. static void atmel_tx_pdc(struct uart_port *port)
  901. {
  902. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  903. struct circ_buf *xmit = &port->state->xmit;
  904. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  905. int count;
  906. /* nothing left to transmit? */
  907. if (UART_GET_TCR(port))
  908. return;
  909. xmit->tail += pdc->ofs;
  910. xmit->tail &= UART_XMIT_SIZE - 1;
  911. port->icount.tx += pdc->ofs;
  912. pdc->ofs = 0;
  913. /* more to transmit - setup next transfer */
  914. /* disable PDC transmit */
  915. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  916. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port)) {
  917. dma_sync_single_for_device(port->dev,
  918. pdc->dma_addr,
  919. pdc->dma_size,
  920. DMA_TO_DEVICE);
  921. count = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
  922. pdc->ofs = count;
  923. UART_PUT_TPR(port, pdc->dma_addr + xmit->tail);
  924. UART_PUT_TCR(port, count);
  925. /* re-enable PDC transmit */
  926. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  927. /* Enable interrupts */
  928. UART_PUT_IER(port, atmel_port->tx_done_mask);
  929. } else {
  930. if ((atmel_port->rs485.flags & SER_RS485_ENABLED) &&
  931. !(atmel_port->rs485.flags & SER_RS485_RX_DURING_TX)) {
  932. /* DMA done, stop TX, start RX for RS485 */
  933. atmel_start_rx(port);
  934. }
  935. }
  936. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  937. uart_write_wakeup(port);
  938. }
  939. static int atmel_prepare_tx_pdc(struct uart_port *port)
  940. {
  941. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  942. struct atmel_dma_buffer *pdc = &atmel_port->pdc_tx;
  943. struct circ_buf *xmit = &port->state->xmit;
  944. pdc->buf = xmit->buf;
  945. pdc->dma_addr = dma_map_single(port->dev,
  946. pdc->buf,
  947. UART_XMIT_SIZE,
  948. DMA_TO_DEVICE);
  949. pdc->dma_size = UART_XMIT_SIZE;
  950. pdc->ofs = 0;
  951. return 0;
  952. }
  953. static void atmel_rx_from_ring(struct uart_port *port)
  954. {
  955. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  956. struct circ_buf *ring = &atmel_port->rx_ring;
  957. unsigned int flg;
  958. unsigned int status;
  959. while (ring->head != ring->tail) {
  960. struct atmel_uart_char c;
  961. /* Make sure c is loaded after head. */
  962. smp_rmb();
  963. c = ((struct atmel_uart_char *)ring->buf)[ring->tail];
  964. ring->tail = (ring->tail + 1) & (ATMEL_SERIAL_RINGSIZE - 1);
  965. port->icount.rx++;
  966. status = c.status;
  967. flg = TTY_NORMAL;
  968. /*
  969. * note that the error handling code is
  970. * out of the main execution path
  971. */
  972. if (unlikely(status & (ATMEL_US_PARE | ATMEL_US_FRAME
  973. | ATMEL_US_OVRE | ATMEL_US_RXBRK))) {
  974. if (status & ATMEL_US_RXBRK) {
  975. /* ignore side-effect */
  976. status &= ~(ATMEL_US_PARE | ATMEL_US_FRAME);
  977. port->icount.brk++;
  978. if (uart_handle_break(port))
  979. continue;
  980. }
  981. if (status & ATMEL_US_PARE)
  982. port->icount.parity++;
  983. if (status & ATMEL_US_FRAME)
  984. port->icount.frame++;
  985. if (status & ATMEL_US_OVRE)
  986. port->icount.overrun++;
  987. status &= port->read_status_mask;
  988. if (status & ATMEL_US_RXBRK)
  989. flg = TTY_BREAK;
  990. else if (status & ATMEL_US_PARE)
  991. flg = TTY_PARITY;
  992. else if (status & ATMEL_US_FRAME)
  993. flg = TTY_FRAME;
  994. }
  995. if (uart_handle_sysrq_char(port, c.ch))
  996. continue;
  997. uart_insert_char(port, status, ATMEL_US_OVRE, c.ch, flg);
  998. }
  999. /*
  1000. * Drop the lock here since it might end up calling
  1001. * uart_start(), which takes the lock.
  1002. */
  1003. spin_unlock(&port->lock);
  1004. tty_flip_buffer_push(&port->state->port);
  1005. spin_lock(&port->lock);
  1006. }
  1007. static void atmel_release_rx_pdc(struct uart_port *port)
  1008. {
  1009. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1010. int i;
  1011. for (i = 0; i < 2; i++) {
  1012. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  1013. dma_unmap_single(port->dev,
  1014. pdc->dma_addr,
  1015. pdc->dma_size,
  1016. DMA_FROM_DEVICE);
  1017. kfree(pdc->buf);
  1018. }
  1019. }
  1020. static void atmel_rx_from_pdc(struct uart_port *port)
  1021. {
  1022. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1023. struct tty_port *tport = &port->state->port;
  1024. struct atmel_dma_buffer *pdc;
  1025. int rx_idx = atmel_port->pdc_rx_idx;
  1026. unsigned int head;
  1027. unsigned int tail;
  1028. unsigned int count;
  1029. do {
  1030. /* Reset the UART timeout early so that we don't miss one */
  1031. UART_PUT_CR(port, ATMEL_US_STTTO);
  1032. pdc = &atmel_port->pdc_rx[rx_idx];
  1033. head = UART_GET_RPR(port) - pdc->dma_addr;
  1034. tail = pdc->ofs;
  1035. /* If the PDC has switched buffers, RPR won't contain
  1036. * any address within the current buffer. Since head
  1037. * is unsigned, we just need a one-way comparison to
  1038. * find out.
  1039. *
  1040. * In this case, we just need to consume the entire
  1041. * buffer and resubmit it for DMA. This will clear the
  1042. * ENDRX bit as well, so that we can safely re-enable
  1043. * all interrupts below.
  1044. */
  1045. head = min(head, pdc->dma_size);
  1046. if (likely(head != tail)) {
  1047. dma_sync_single_for_cpu(port->dev, pdc->dma_addr,
  1048. pdc->dma_size, DMA_FROM_DEVICE);
  1049. /*
  1050. * head will only wrap around when we recycle
  1051. * the DMA buffer, and when that happens, we
  1052. * explicitly set tail to 0. So head will
  1053. * always be greater than tail.
  1054. */
  1055. count = head - tail;
  1056. tty_insert_flip_string(tport, pdc->buf + pdc->ofs,
  1057. count);
  1058. dma_sync_single_for_device(port->dev, pdc->dma_addr,
  1059. pdc->dma_size, DMA_FROM_DEVICE);
  1060. port->icount.rx += count;
  1061. pdc->ofs = head;
  1062. }
  1063. /*
  1064. * If the current buffer is full, we need to check if
  1065. * the next one contains any additional data.
  1066. */
  1067. if (head >= pdc->dma_size) {
  1068. pdc->ofs = 0;
  1069. UART_PUT_RNPR(port, pdc->dma_addr);
  1070. UART_PUT_RNCR(port, pdc->dma_size);
  1071. rx_idx = !rx_idx;
  1072. atmel_port->pdc_rx_idx = rx_idx;
  1073. }
  1074. } while (head >= pdc->dma_size);
  1075. /*
  1076. * Drop the lock here since it might end up calling
  1077. * uart_start(), which takes the lock.
  1078. */
  1079. spin_unlock(&port->lock);
  1080. tty_flip_buffer_push(tport);
  1081. spin_lock(&port->lock);
  1082. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  1083. }
  1084. static int atmel_prepare_rx_pdc(struct uart_port *port)
  1085. {
  1086. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1087. int i;
  1088. for (i = 0; i < 2; i++) {
  1089. struct atmel_dma_buffer *pdc = &atmel_port->pdc_rx[i];
  1090. pdc->buf = kmalloc(PDC_BUFFER_SIZE, GFP_KERNEL);
  1091. if (pdc->buf == NULL) {
  1092. if (i != 0) {
  1093. dma_unmap_single(port->dev,
  1094. atmel_port->pdc_rx[0].dma_addr,
  1095. PDC_BUFFER_SIZE,
  1096. DMA_FROM_DEVICE);
  1097. kfree(atmel_port->pdc_rx[0].buf);
  1098. }
  1099. atmel_port->use_pdc_rx = 0;
  1100. return -ENOMEM;
  1101. }
  1102. pdc->dma_addr = dma_map_single(port->dev,
  1103. pdc->buf,
  1104. PDC_BUFFER_SIZE,
  1105. DMA_FROM_DEVICE);
  1106. pdc->dma_size = PDC_BUFFER_SIZE;
  1107. pdc->ofs = 0;
  1108. }
  1109. atmel_port->pdc_rx_idx = 0;
  1110. UART_PUT_RPR(port, atmel_port->pdc_rx[0].dma_addr);
  1111. UART_PUT_RCR(port, PDC_BUFFER_SIZE);
  1112. UART_PUT_RNPR(port, atmel_port->pdc_rx[1].dma_addr);
  1113. UART_PUT_RNCR(port, PDC_BUFFER_SIZE);
  1114. return 0;
  1115. }
  1116. /*
  1117. * tasklet handling tty stuff outside the interrupt handler.
  1118. */
  1119. static void atmel_tasklet_func(unsigned long data)
  1120. {
  1121. struct uart_port *port = (struct uart_port *)data;
  1122. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1123. unsigned int status;
  1124. unsigned int status_change;
  1125. /* The interrupt handler does not take the lock */
  1126. spin_lock(&port->lock);
  1127. atmel_port->schedule_tx(port);
  1128. status = atmel_port->irq_status;
  1129. status_change = status ^ atmel_port->irq_status_prev;
  1130. if (status_change & (ATMEL_US_RI | ATMEL_US_DSR
  1131. | ATMEL_US_DCD | ATMEL_US_CTS)) {
  1132. /* TODO: All reads to CSR will clear these interrupts! */
  1133. if (status_change & ATMEL_US_RI)
  1134. port->icount.rng++;
  1135. if (status_change & ATMEL_US_DSR)
  1136. port->icount.dsr++;
  1137. if (status_change & ATMEL_US_DCD)
  1138. uart_handle_dcd_change(port, !(status & ATMEL_US_DCD));
  1139. if (status_change & ATMEL_US_CTS)
  1140. uart_handle_cts_change(port, !(status & ATMEL_US_CTS));
  1141. wake_up_interruptible(&port->state->port.delta_msr_wait);
  1142. atmel_port->irq_status_prev = status;
  1143. }
  1144. atmel_port->schedule_rx(port);
  1145. spin_unlock(&port->lock);
  1146. }
  1147. static void atmel_set_ops(struct uart_port *port)
  1148. {
  1149. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1150. if (atmel_use_dma_rx(port)) {
  1151. atmel_port->prepare_rx = &atmel_prepare_rx_dma;
  1152. atmel_port->schedule_rx = &atmel_rx_from_dma;
  1153. atmel_port->release_rx = &atmel_release_rx_dma;
  1154. } else if (atmel_use_pdc_rx(port)) {
  1155. atmel_port->prepare_rx = &atmel_prepare_rx_pdc;
  1156. atmel_port->schedule_rx = &atmel_rx_from_pdc;
  1157. atmel_port->release_rx = &atmel_release_rx_pdc;
  1158. } else {
  1159. atmel_port->prepare_rx = NULL;
  1160. atmel_port->schedule_rx = &atmel_rx_from_ring;
  1161. atmel_port->release_rx = NULL;
  1162. }
  1163. if (atmel_use_dma_tx(port)) {
  1164. atmel_port->prepare_tx = &atmel_prepare_tx_dma;
  1165. atmel_port->schedule_tx = &atmel_tx_dma;
  1166. atmel_port->release_tx = &atmel_release_tx_dma;
  1167. } else if (atmel_use_pdc_tx(port)) {
  1168. atmel_port->prepare_tx = &atmel_prepare_tx_pdc;
  1169. atmel_port->schedule_tx = &atmel_tx_pdc;
  1170. atmel_port->release_tx = &atmel_release_tx_pdc;
  1171. } else {
  1172. atmel_port->prepare_tx = NULL;
  1173. atmel_port->schedule_tx = &atmel_tx_chars;
  1174. atmel_port->release_tx = NULL;
  1175. }
  1176. }
  1177. /*
  1178. * Perform initialization and enable port for reception
  1179. */
  1180. static int atmel_startup(struct uart_port *port)
  1181. {
  1182. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1183. struct tty_struct *tty = port->state->port.tty;
  1184. int retval;
  1185. /*
  1186. * Ensure that no interrupts are enabled otherwise when
  1187. * request_irq() is called we could get stuck trying to
  1188. * handle an unexpected interrupt
  1189. */
  1190. UART_PUT_IDR(port, -1);
  1191. /*
  1192. * Allocate the IRQ
  1193. */
  1194. retval = request_irq(port->irq, atmel_interrupt, IRQF_SHARED,
  1195. tty ? tty->name : "atmel_serial", port);
  1196. if (retval) {
  1197. printk("atmel_serial: atmel_startup - Can't get irq\n");
  1198. return retval;
  1199. }
  1200. /*
  1201. * Initialize DMA (if necessary)
  1202. */
  1203. if (atmel_port->prepare_rx) {
  1204. retval = atmel_port->prepare_rx(port);
  1205. if (retval < 0)
  1206. atmel_set_ops(port);
  1207. }
  1208. if (atmel_port->prepare_tx) {
  1209. retval = atmel_port->prepare_tx(port);
  1210. if (retval < 0)
  1211. atmel_set_ops(port);
  1212. }
  1213. /*
  1214. * If there is a specific "open" function (to register
  1215. * control line interrupts)
  1216. */
  1217. if (atmel_open_hook) {
  1218. retval = atmel_open_hook(port);
  1219. if (retval) {
  1220. free_irq(port->irq, port);
  1221. return retval;
  1222. }
  1223. }
  1224. /* Save current CSR for comparison in atmel_tasklet_func() */
  1225. atmel_port->irq_status_prev = UART_GET_CSR(port);
  1226. atmel_port->irq_status = atmel_port->irq_status_prev;
  1227. /*
  1228. * Finally, enable the serial port
  1229. */
  1230. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1231. /* enable xmit & rcvr */
  1232. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1233. if (atmel_use_pdc_rx(port)) {
  1234. /* set UART timeout */
  1235. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  1236. UART_PUT_CR(port, ATMEL_US_STTTO);
  1237. UART_PUT_IER(port, ATMEL_US_ENDRX | ATMEL_US_TIMEOUT);
  1238. /* enable PDC controller */
  1239. UART_PUT_PTCR(port, ATMEL_PDC_RXTEN);
  1240. } else if (atmel_use_dma_rx(port)) {
  1241. UART_PUT_RTOR(port, PDC_RX_TIMEOUT);
  1242. UART_PUT_CR(port, ATMEL_US_STTTO);
  1243. UART_PUT_IER(port, ATMEL_US_TIMEOUT);
  1244. } else {
  1245. /* enable receive only */
  1246. UART_PUT_IER(port, ATMEL_US_RXRDY);
  1247. }
  1248. return 0;
  1249. }
  1250. /*
  1251. * Disable the port
  1252. */
  1253. static void atmel_shutdown(struct uart_port *port)
  1254. {
  1255. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1256. /*
  1257. * Ensure everything is stopped.
  1258. */
  1259. atmel_stop_rx(port);
  1260. atmel_stop_tx(port);
  1261. /*
  1262. * Shut-down the DMA.
  1263. */
  1264. if (atmel_port->release_rx)
  1265. atmel_port->release_rx(port);
  1266. if (atmel_port->release_tx)
  1267. atmel_port->release_tx(port);
  1268. /*
  1269. * Disable all interrupts, port and break condition.
  1270. */
  1271. UART_PUT_CR(port, ATMEL_US_RSTSTA);
  1272. UART_PUT_IDR(port, -1);
  1273. /*
  1274. * Free the interrupt
  1275. */
  1276. free_irq(port->irq, port);
  1277. /*
  1278. * If there is a specific "close" function (to unregister
  1279. * control line interrupts)
  1280. */
  1281. if (atmel_close_hook)
  1282. atmel_close_hook(port);
  1283. }
  1284. /*
  1285. * Flush any TX data submitted for DMA. Called when the TX circular
  1286. * buffer is reset.
  1287. */
  1288. static void atmel_flush_buffer(struct uart_port *port)
  1289. {
  1290. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1291. if (atmel_use_pdc_tx(port)) {
  1292. UART_PUT_TCR(port, 0);
  1293. atmel_port->pdc_tx.ofs = 0;
  1294. }
  1295. }
  1296. /*
  1297. * Power / Clock management.
  1298. */
  1299. static void atmel_serial_pm(struct uart_port *port, unsigned int state,
  1300. unsigned int oldstate)
  1301. {
  1302. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1303. switch (state) {
  1304. case 0:
  1305. /*
  1306. * Enable the peripheral clock for this serial port.
  1307. * This is called on uart_open() or a resume event.
  1308. */
  1309. clk_prepare_enable(atmel_port->clk);
  1310. /* re-enable interrupts if we disabled some on suspend */
  1311. UART_PUT_IER(port, atmel_port->backup_imr);
  1312. break;
  1313. case 3:
  1314. /* Back up the interrupt mask and disable all interrupts */
  1315. atmel_port->backup_imr = UART_GET_IMR(port);
  1316. UART_PUT_IDR(port, -1);
  1317. /*
  1318. * Disable the peripheral clock for this serial port.
  1319. * This is called on uart_close() or a suspend event.
  1320. */
  1321. clk_disable_unprepare(atmel_port->clk);
  1322. break;
  1323. default:
  1324. printk(KERN_ERR "atmel_serial: unknown pm %d\n", state);
  1325. }
  1326. }
  1327. /*
  1328. * Change the port parameters
  1329. */
  1330. static void atmel_set_termios(struct uart_port *port, struct ktermios *termios,
  1331. struct ktermios *old)
  1332. {
  1333. unsigned long flags;
  1334. unsigned int mode, imr, quot, baud;
  1335. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1336. /* Get current mode register */
  1337. mode = UART_GET_MR(port) & ~(ATMEL_US_USCLKS | ATMEL_US_CHRL
  1338. | ATMEL_US_NBSTOP | ATMEL_US_PAR
  1339. | ATMEL_US_USMODE);
  1340. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
  1341. quot = uart_get_divisor(port, baud);
  1342. if (quot > 65535) { /* BRGR is 16-bit, so switch to slower clock */
  1343. quot /= 8;
  1344. mode |= ATMEL_US_USCLKS_MCK_DIV8;
  1345. }
  1346. /* byte size */
  1347. switch (termios->c_cflag & CSIZE) {
  1348. case CS5:
  1349. mode |= ATMEL_US_CHRL_5;
  1350. break;
  1351. case CS6:
  1352. mode |= ATMEL_US_CHRL_6;
  1353. break;
  1354. case CS7:
  1355. mode |= ATMEL_US_CHRL_7;
  1356. break;
  1357. default:
  1358. mode |= ATMEL_US_CHRL_8;
  1359. break;
  1360. }
  1361. /* stop bits */
  1362. if (termios->c_cflag & CSTOPB)
  1363. mode |= ATMEL_US_NBSTOP_2;
  1364. /* parity */
  1365. if (termios->c_cflag & PARENB) {
  1366. /* Mark or Space parity */
  1367. if (termios->c_cflag & CMSPAR) {
  1368. if (termios->c_cflag & PARODD)
  1369. mode |= ATMEL_US_PAR_MARK;
  1370. else
  1371. mode |= ATMEL_US_PAR_SPACE;
  1372. } else if (termios->c_cflag & PARODD)
  1373. mode |= ATMEL_US_PAR_ODD;
  1374. else
  1375. mode |= ATMEL_US_PAR_EVEN;
  1376. } else
  1377. mode |= ATMEL_US_PAR_NONE;
  1378. /* hardware handshake (RTS/CTS) */
  1379. if (termios->c_cflag & CRTSCTS)
  1380. mode |= ATMEL_US_USMODE_HWHS;
  1381. else
  1382. mode |= ATMEL_US_USMODE_NORMAL;
  1383. spin_lock_irqsave(&port->lock, flags);
  1384. port->read_status_mask = ATMEL_US_OVRE;
  1385. if (termios->c_iflag & INPCK)
  1386. port->read_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  1387. if (termios->c_iflag & (BRKINT | PARMRK))
  1388. port->read_status_mask |= ATMEL_US_RXBRK;
  1389. if (atmel_use_pdc_rx(port))
  1390. /* need to enable error interrupts */
  1391. UART_PUT_IER(port, port->read_status_mask);
  1392. /*
  1393. * Characters to ignore
  1394. */
  1395. port->ignore_status_mask = 0;
  1396. if (termios->c_iflag & IGNPAR)
  1397. port->ignore_status_mask |= (ATMEL_US_FRAME | ATMEL_US_PARE);
  1398. if (termios->c_iflag & IGNBRK) {
  1399. port->ignore_status_mask |= ATMEL_US_RXBRK;
  1400. /*
  1401. * If we're ignoring parity and break indicators,
  1402. * ignore overruns too (for real raw support).
  1403. */
  1404. if (termios->c_iflag & IGNPAR)
  1405. port->ignore_status_mask |= ATMEL_US_OVRE;
  1406. }
  1407. /* TODO: Ignore all characters if CREAD is set.*/
  1408. /* update the per-port timeout */
  1409. uart_update_timeout(port, termios->c_cflag, baud);
  1410. /*
  1411. * save/disable interrupts. The tty layer will ensure that the
  1412. * transmitter is empty if requested by the caller, so there's
  1413. * no need to wait for it here.
  1414. */
  1415. imr = UART_GET_IMR(port);
  1416. UART_PUT_IDR(port, -1);
  1417. /* disable receiver and transmitter */
  1418. UART_PUT_CR(port, ATMEL_US_TXDIS | ATMEL_US_RXDIS);
  1419. /* Resetting serial mode to RS232 (0x0) */
  1420. mode &= ~ATMEL_US_USMODE;
  1421. if (atmel_port->rs485.flags & SER_RS485_ENABLED) {
  1422. dev_dbg(port->dev, "Setting UART to RS485\n");
  1423. if ((atmel_port->rs485.delay_rts_after_send) > 0)
  1424. UART_PUT_TTGR(port,
  1425. atmel_port->rs485.delay_rts_after_send);
  1426. mode |= ATMEL_US_USMODE_RS485;
  1427. } else {
  1428. dev_dbg(port->dev, "Setting UART to RS232\n");
  1429. }
  1430. /* set the parity, stop bits and data size */
  1431. UART_PUT_MR(port, mode);
  1432. /* set the baud rate */
  1433. UART_PUT_BRGR(port, quot);
  1434. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1435. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1436. /* restore interrupts */
  1437. UART_PUT_IER(port, imr);
  1438. /* CTS flow-control and modem-status interrupts */
  1439. if (UART_ENABLE_MS(port, termios->c_cflag))
  1440. port->ops->enable_ms(port);
  1441. spin_unlock_irqrestore(&port->lock, flags);
  1442. }
  1443. static void atmel_set_ldisc(struct uart_port *port, int new)
  1444. {
  1445. if (new == N_PPS) {
  1446. port->flags |= UPF_HARDPPS_CD;
  1447. atmel_enable_ms(port);
  1448. } else {
  1449. port->flags &= ~UPF_HARDPPS_CD;
  1450. }
  1451. }
  1452. /*
  1453. * Return string describing the specified port
  1454. */
  1455. static const char *atmel_type(struct uart_port *port)
  1456. {
  1457. return (port->type == PORT_ATMEL) ? "ATMEL_SERIAL" : NULL;
  1458. }
  1459. /*
  1460. * Release the memory region(s) being used by 'port'.
  1461. */
  1462. static void atmel_release_port(struct uart_port *port)
  1463. {
  1464. struct platform_device *pdev = to_platform_device(port->dev);
  1465. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1466. release_mem_region(port->mapbase, size);
  1467. if (port->flags & UPF_IOREMAP) {
  1468. iounmap(port->membase);
  1469. port->membase = NULL;
  1470. }
  1471. }
  1472. /*
  1473. * Request the memory region(s) being used by 'port'.
  1474. */
  1475. static int atmel_request_port(struct uart_port *port)
  1476. {
  1477. struct platform_device *pdev = to_platform_device(port->dev);
  1478. int size = pdev->resource[0].end - pdev->resource[0].start + 1;
  1479. if (!request_mem_region(port->mapbase, size, "atmel_serial"))
  1480. return -EBUSY;
  1481. if (port->flags & UPF_IOREMAP) {
  1482. port->membase = ioremap(port->mapbase, size);
  1483. if (port->membase == NULL) {
  1484. release_mem_region(port->mapbase, size);
  1485. return -ENOMEM;
  1486. }
  1487. }
  1488. return 0;
  1489. }
  1490. /*
  1491. * Configure/autoconfigure the port.
  1492. */
  1493. static void atmel_config_port(struct uart_port *port, int flags)
  1494. {
  1495. if (flags & UART_CONFIG_TYPE) {
  1496. port->type = PORT_ATMEL;
  1497. atmel_request_port(port);
  1498. }
  1499. }
  1500. /*
  1501. * Verify the new serial_struct (for TIOCSSERIAL).
  1502. */
  1503. static int atmel_verify_port(struct uart_port *port, struct serial_struct *ser)
  1504. {
  1505. int ret = 0;
  1506. if (ser->type != PORT_UNKNOWN && ser->type != PORT_ATMEL)
  1507. ret = -EINVAL;
  1508. if (port->irq != ser->irq)
  1509. ret = -EINVAL;
  1510. if (ser->io_type != SERIAL_IO_MEM)
  1511. ret = -EINVAL;
  1512. if (port->uartclk / 16 != ser->baud_base)
  1513. ret = -EINVAL;
  1514. if ((void *)port->mapbase != ser->iomem_base)
  1515. ret = -EINVAL;
  1516. if (port->iobase != ser->port)
  1517. ret = -EINVAL;
  1518. if (ser->hub6 != 0)
  1519. ret = -EINVAL;
  1520. return ret;
  1521. }
  1522. #ifdef CONFIG_CONSOLE_POLL
  1523. static int atmel_poll_get_char(struct uart_port *port)
  1524. {
  1525. while (!(UART_GET_CSR(port) & ATMEL_US_RXRDY))
  1526. cpu_relax();
  1527. return UART_GET_CHAR(port);
  1528. }
  1529. static void atmel_poll_put_char(struct uart_port *port, unsigned char ch)
  1530. {
  1531. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1532. cpu_relax();
  1533. UART_PUT_CHAR(port, ch);
  1534. }
  1535. #endif
  1536. static int
  1537. atmel_ioctl(struct uart_port *port, unsigned int cmd, unsigned long arg)
  1538. {
  1539. struct serial_rs485 rs485conf;
  1540. switch (cmd) {
  1541. case TIOCSRS485:
  1542. if (copy_from_user(&rs485conf, (struct serial_rs485 *) arg,
  1543. sizeof(rs485conf)))
  1544. return -EFAULT;
  1545. atmel_config_rs485(port, &rs485conf);
  1546. break;
  1547. case TIOCGRS485:
  1548. if (copy_to_user((struct serial_rs485 *) arg,
  1549. &(to_atmel_uart_port(port)->rs485),
  1550. sizeof(rs485conf)))
  1551. return -EFAULT;
  1552. break;
  1553. default:
  1554. return -ENOIOCTLCMD;
  1555. }
  1556. return 0;
  1557. }
  1558. static struct uart_ops atmel_pops = {
  1559. .tx_empty = atmel_tx_empty,
  1560. .set_mctrl = atmel_set_mctrl,
  1561. .get_mctrl = atmel_get_mctrl,
  1562. .stop_tx = atmel_stop_tx,
  1563. .start_tx = atmel_start_tx,
  1564. .stop_rx = atmel_stop_rx,
  1565. .enable_ms = atmel_enable_ms,
  1566. .break_ctl = atmel_break_ctl,
  1567. .startup = atmel_startup,
  1568. .shutdown = atmel_shutdown,
  1569. .flush_buffer = atmel_flush_buffer,
  1570. .set_termios = atmel_set_termios,
  1571. .set_ldisc = atmel_set_ldisc,
  1572. .type = atmel_type,
  1573. .release_port = atmel_release_port,
  1574. .request_port = atmel_request_port,
  1575. .config_port = atmel_config_port,
  1576. .verify_port = atmel_verify_port,
  1577. .pm = atmel_serial_pm,
  1578. .ioctl = atmel_ioctl,
  1579. #ifdef CONFIG_CONSOLE_POLL
  1580. .poll_get_char = atmel_poll_get_char,
  1581. .poll_put_char = atmel_poll_put_char,
  1582. #endif
  1583. };
  1584. static void atmel_of_init_port(struct atmel_uart_port *atmel_port,
  1585. struct device_node *np)
  1586. {
  1587. u32 rs485_delay[2];
  1588. /* DMA/PDC usage specification */
  1589. if (of_get_property(np, "atmel,use-dma-rx", NULL)) {
  1590. if (of_get_property(np, "dmas", NULL)) {
  1591. atmel_port->use_dma_rx = true;
  1592. atmel_port->use_pdc_rx = false;
  1593. } else {
  1594. atmel_port->use_dma_rx = false;
  1595. atmel_port->use_pdc_rx = true;
  1596. }
  1597. } else {
  1598. atmel_port->use_dma_rx = false;
  1599. atmel_port->use_pdc_rx = false;
  1600. }
  1601. if (of_get_property(np, "atmel,use-dma-tx", NULL)) {
  1602. if (of_get_property(np, "dmas", NULL)) {
  1603. atmel_port->use_dma_tx = true;
  1604. atmel_port->use_pdc_tx = false;
  1605. } else {
  1606. atmel_port->use_dma_tx = false;
  1607. atmel_port->use_pdc_tx = true;
  1608. }
  1609. } else {
  1610. atmel_port->use_dma_tx = false;
  1611. atmel_port->use_pdc_tx = false;
  1612. }
  1613. /* rs485 properties */
  1614. if (of_property_read_u32_array(np, "rs485-rts-delay",
  1615. rs485_delay, 2) == 0) {
  1616. struct serial_rs485 *rs485conf = &atmel_port->rs485;
  1617. rs485conf->delay_rts_before_send = rs485_delay[0];
  1618. rs485conf->delay_rts_after_send = rs485_delay[1];
  1619. rs485conf->flags = 0;
  1620. if (of_get_property(np, "rs485-rx-during-tx", NULL))
  1621. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1622. if (of_get_property(np, "linux,rs485-enabled-at-boot-time", NULL))
  1623. rs485conf->flags |= SER_RS485_ENABLED;
  1624. }
  1625. }
  1626. /*
  1627. * Configure the port from the platform device resource info.
  1628. */
  1629. static int atmel_init_port(struct atmel_uart_port *atmel_port,
  1630. struct platform_device *pdev)
  1631. {
  1632. int ret;
  1633. struct uart_port *port = &atmel_port->uart;
  1634. struct atmel_uart_data *pdata = pdev->dev.platform_data;
  1635. if (pdev->dev.of_node) {
  1636. atmel_of_init_port(atmel_port, pdev->dev.of_node);
  1637. } else {
  1638. atmel_port->use_pdc_rx = pdata->use_dma_rx;
  1639. atmel_port->use_pdc_tx = pdata->use_dma_tx;
  1640. atmel_port->use_dma_rx = false;
  1641. atmel_port->use_dma_tx = false;
  1642. atmel_port->rs485 = pdata->rs485;
  1643. }
  1644. atmel_set_ops(port);
  1645. port->iotype = UPIO_MEM;
  1646. port->flags = UPF_BOOT_AUTOCONF;
  1647. port->ops = &atmel_pops;
  1648. port->fifosize = 1;
  1649. port->dev = &pdev->dev;
  1650. port->mapbase = pdev->resource[0].start;
  1651. port->irq = pdev->resource[1].start;
  1652. tasklet_init(&atmel_port->tasklet, atmel_tasklet_func,
  1653. (unsigned long)port);
  1654. memset(&atmel_port->rx_ring, 0, sizeof(atmel_port->rx_ring));
  1655. if (pdata && pdata->regs) {
  1656. /* Already mapped by setup code */
  1657. port->membase = pdata->regs;
  1658. } else {
  1659. port->flags |= UPF_IOREMAP;
  1660. port->membase = NULL;
  1661. }
  1662. /* for console, the clock could already be configured */
  1663. if (!atmel_port->clk) {
  1664. atmel_port->clk = clk_get(&pdev->dev, "usart");
  1665. if (IS_ERR(atmel_port->clk)) {
  1666. ret = PTR_ERR(atmel_port->clk);
  1667. atmel_port->clk = NULL;
  1668. return ret;
  1669. }
  1670. ret = clk_prepare_enable(atmel_port->clk);
  1671. if (ret) {
  1672. clk_put(atmel_port->clk);
  1673. atmel_port->clk = NULL;
  1674. return ret;
  1675. }
  1676. port->uartclk = clk_get_rate(atmel_port->clk);
  1677. clk_disable_unprepare(atmel_port->clk);
  1678. /* only enable clock when USART is in use */
  1679. }
  1680. /* Use TXEMPTY for interrupt when rs485 else TXRDY or ENDTX|TXBUFE */
  1681. if (atmel_port->rs485.flags & SER_RS485_ENABLED)
  1682. atmel_port->tx_done_mask = ATMEL_US_TXEMPTY;
  1683. else if (atmel_use_pdc_tx(port)) {
  1684. port->fifosize = PDC_BUFFER_SIZE;
  1685. atmel_port->tx_done_mask = ATMEL_US_ENDTX | ATMEL_US_TXBUFE;
  1686. } else {
  1687. atmel_port->tx_done_mask = ATMEL_US_TXRDY;
  1688. }
  1689. return 0;
  1690. }
  1691. struct platform_device *atmel_default_console_device; /* the serial console device */
  1692. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1693. static void atmel_console_putchar(struct uart_port *port, int ch)
  1694. {
  1695. while (!(UART_GET_CSR(port) & ATMEL_US_TXRDY))
  1696. cpu_relax();
  1697. UART_PUT_CHAR(port, ch);
  1698. }
  1699. /*
  1700. * Interrupts are disabled on entering
  1701. */
  1702. static void atmel_console_write(struct console *co, const char *s, u_int count)
  1703. {
  1704. struct uart_port *port = &atmel_ports[co->index].uart;
  1705. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1706. unsigned int status, imr;
  1707. unsigned int pdc_tx;
  1708. /*
  1709. * First, save IMR and then disable interrupts
  1710. */
  1711. imr = UART_GET_IMR(port);
  1712. UART_PUT_IDR(port, ATMEL_US_RXRDY | atmel_port->tx_done_mask);
  1713. /* Store PDC transmit status and disable it */
  1714. pdc_tx = UART_GET_PTSR(port) & ATMEL_PDC_TXTEN;
  1715. UART_PUT_PTCR(port, ATMEL_PDC_TXTDIS);
  1716. uart_console_write(port, s, count, atmel_console_putchar);
  1717. /*
  1718. * Finally, wait for transmitter to become empty
  1719. * and restore IMR
  1720. */
  1721. do {
  1722. status = UART_GET_CSR(port);
  1723. } while (!(status & ATMEL_US_TXRDY));
  1724. /* Restore PDC transmit status */
  1725. if (pdc_tx)
  1726. UART_PUT_PTCR(port, ATMEL_PDC_TXTEN);
  1727. /* set interrupts back the way they were */
  1728. UART_PUT_IER(port, imr);
  1729. }
  1730. /*
  1731. * If the port was already initialised (eg, by a boot loader),
  1732. * try to determine the current setup.
  1733. */
  1734. static void __init atmel_console_get_options(struct uart_port *port, int *baud,
  1735. int *parity, int *bits)
  1736. {
  1737. unsigned int mr, quot;
  1738. /*
  1739. * If the baud rate generator isn't running, the port wasn't
  1740. * initialized by the boot loader.
  1741. */
  1742. quot = UART_GET_BRGR(port) & ATMEL_US_CD;
  1743. if (!quot)
  1744. return;
  1745. mr = UART_GET_MR(port) & ATMEL_US_CHRL;
  1746. if (mr == ATMEL_US_CHRL_8)
  1747. *bits = 8;
  1748. else
  1749. *bits = 7;
  1750. mr = UART_GET_MR(port) & ATMEL_US_PAR;
  1751. if (mr == ATMEL_US_PAR_EVEN)
  1752. *parity = 'e';
  1753. else if (mr == ATMEL_US_PAR_ODD)
  1754. *parity = 'o';
  1755. /*
  1756. * The serial core only rounds down when matching this to a
  1757. * supported baud rate. Make sure we don't end up slightly
  1758. * lower than one of those, as it would make us fall through
  1759. * to a much lower baud rate than we really want.
  1760. */
  1761. *baud = port->uartclk / (16 * (quot - 1));
  1762. }
  1763. static int __init atmel_console_setup(struct console *co, char *options)
  1764. {
  1765. int ret;
  1766. struct uart_port *port = &atmel_ports[co->index].uart;
  1767. int baud = 115200;
  1768. int bits = 8;
  1769. int parity = 'n';
  1770. int flow = 'n';
  1771. if (port->membase == NULL) {
  1772. /* Port not initialized yet - delay setup */
  1773. return -ENODEV;
  1774. }
  1775. ret = clk_prepare_enable(atmel_ports[co->index].clk);
  1776. if (ret)
  1777. return ret;
  1778. UART_PUT_IDR(port, -1);
  1779. UART_PUT_CR(port, ATMEL_US_RSTSTA | ATMEL_US_RSTRX);
  1780. UART_PUT_CR(port, ATMEL_US_TXEN | ATMEL_US_RXEN);
  1781. if (options)
  1782. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1783. else
  1784. atmel_console_get_options(port, &baud, &parity, &bits);
  1785. return uart_set_options(port, co, baud, parity, bits, flow);
  1786. }
  1787. static struct uart_driver atmel_uart;
  1788. static struct console atmel_console = {
  1789. .name = ATMEL_DEVICENAME,
  1790. .write = atmel_console_write,
  1791. .device = uart_console_device,
  1792. .setup = atmel_console_setup,
  1793. .flags = CON_PRINTBUFFER,
  1794. .index = -1,
  1795. .data = &atmel_uart,
  1796. };
  1797. #define ATMEL_CONSOLE_DEVICE (&atmel_console)
  1798. /*
  1799. * Early console initialization (before VM subsystem initialized).
  1800. */
  1801. static int __init atmel_console_init(void)
  1802. {
  1803. int ret;
  1804. if (atmel_default_console_device) {
  1805. struct atmel_uart_data *pdata =
  1806. atmel_default_console_device->dev.platform_data;
  1807. int id = pdata->num;
  1808. struct atmel_uart_port *port = &atmel_ports[id];
  1809. port->backup_imr = 0;
  1810. port->uart.line = id;
  1811. add_preferred_console(ATMEL_DEVICENAME, id, NULL);
  1812. ret = atmel_init_port(port, atmel_default_console_device);
  1813. if (ret)
  1814. return ret;
  1815. register_console(&atmel_console);
  1816. }
  1817. return 0;
  1818. }
  1819. console_initcall(atmel_console_init);
  1820. /*
  1821. * Late console initialization.
  1822. */
  1823. static int __init atmel_late_console_init(void)
  1824. {
  1825. if (atmel_default_console_device
  1826. && !(atmel_console.flags & CON_ENABLED))
  1827. register_console(&atmel_console);
  1828. return 0;
  1829. }
  1830. core_initcall(atmel_late_console_init);
  1831. static inline bool atmel_is_console_port(struct uart_port *port)
  1832. {
  1833. return port->cons && port->cons->index == port->line;
  1834. }
  1835. #else
  1836. #define ATMEL_CONSOLE_DEVICE NULL
  1837. static inline bool atmel_is_console_port(struct uart_port *port)
  1838. {
  1839. return false;
  1840. }
  1841. #endif
  1842. static struct uart_driver atmel_uart = {
  1843. .owner = THIS_MODULE,
  1844. .driver_name = "atmel_serial",
  1845. .dev_name = ATMEL_DEVICENAME,
  1846. .major = SERIAL_ATMEL_MAJOR,
  1847. .minor = MINOR_START,
  1848. .nr = ATMEL_MAX_UART,
  1849. .cons = ATMEL_CONSOLE_DEVICE,
  1850. };
  1851. #ifdef CONFIG_PM
  1852. static bool atmel_serial_clk_will_stop(void)
  1853. {
  1854. #ifdef CONFIG_ARCH_AT91
  1855. return at91_suspend_entering_slow_clock();
  1856. #else
  1857. return false;
  1858. #endif
  1859. }
  1860. static int atmel_serial_suspend(struct platform_device *pdev,
  1861. pm_message_t state)
  1862. {
  1863. struct uart_port *port = platform_get_drvdata(pdev);
  1864. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1865. if (atmel_is_console_port(port) && console_suspend_enabled) {
  1866. /* Drain the TX shifter */
  1867. while (!(UART_GET_CSR(port) & ATMEL_US_TXEMPTY))
  1868. cpu_relax();
  1869. }
  1870. /* we can not wake up if we're running on slow clock */
  1871. atmel_port->may_wakeup = device_may_wakeup(&pdev->dev);
  1872. if (atmel_serial_clk_will_stop())
  1873. device_set_wakeup_enable(&pdev->dev, 0);
  1874. uart_suspend_port(&atmel_uart, port);
  1875. return 0;
  1876. }
  1877. static int atmel_serial_resume(struct platform_device *pdev)
  1878. {
  1879. struct uart_port *port = platform_get_drvdata(pdev);
  1880. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1881. uart_resume_port(&atmel_uart, port);
  1882. device_set_wakeup_enable(&pdev->dev, atmel_port->may_wakeup);
  1883. return 0;
  1884. }
  1885. #else
  1886. #define atmel_serial_suspend NULL
  1887. #define atmel_serial_resume NULL
  1888. #endif
  1889. static int atmel_serial_probe(struct platform_device *pdev)
  1890. {
  1891. struct atmel_uart_port *port;
  1892. struct device_node *np = pdev->dev.of_node;
  1893. struct atmel_uart_data *pdata = pdev->dev.platform_data;
  1894. void *data;
  1895. int ret = -ENODEV;
  1896. BUILD_BUG_ON(ATMEL_SERIAL_RINGSIZE & (ATMEL_SERIAL_RINGSIZE - 1));
  1897. if (np)
  1898. ret = of_alias_get_id(np, "serial");
  1899. else
  1900. if (pdata)
  1901. ret = pdata->num;
  1902. if (ret < 0)
  1903. /* port id not found in platform data nor device-tree aliases:
  1904. * auto-enumerate it */
  1905. ret = find_first_zero_bit(atmel_ports_in_use, ATMEL_MAX_UART);
  1906. if (ret >= ATMEL_MAX_UART) {
  1907. ret = -ENODEV;
  1908. goto err;
  1909. }
  1910. if (test_and_set_bit(ret, atmel_ports_in_use)) {
  1911. /* port already in use */
  1912. ret = -EBUSY;
  1913. goto err;
  1914. }
  1915. port = &atmel_ports[ret];
  1916. port->backup_imr = 0;
  1917. port->uart.line = ret;
  1918. ret = atmel_init_port(port, pdev);
  1919. if (ret)
  1920. goto err;
  1921. if (!atmel_use_pdc_rx(&port->uart)) {
  1922. ret = -ENOMEM;
  1923. data = kmalloc(sizeof(struct atmel_uart_char)
  1924. * ATMEL_SERIAL_RINGSIZE, GFP_KERNEL);
  1925. if (!data)
  1926. goto err_alloc_ring;
  1927. port->rx_ring.buf = data;
  1928. }
  1929. ret = uart_add_one_port(&atmel_uart, &port->uart);
  1930. if (ret)
  1931. goto err_add_port;
  1932. #ifdef CONFIG_SERIAL_ATMEL_CONSOLE
  1933. if (atmel_is_console_port(&port->uart)
  1934. && ATMEL_CONSOLE_DEVICE->flags & CON_ENABLED) {
  1935. /*
  1936. * The serial core enabled the clock for us, so undo
  1937. * the clk_prepare_enable() in atmel_console_setup()
  1938. */
  1939. clk_disable_unprepare(port->clk);
  1940. }
  1941. #endif
  1942. device_init_wakeup(&pdev->dev, 1);
  1943. platform_set_drvdata(pdev, port);
  1944. if (port->rs485.flags & SER_RS485_ENABLED) {
  1945. UART_PUT_MR(&port->uart, ATMEL_US_USMODE_NORMAL);
  1946. UART_PUT_CR(&port->uart, ATMEL_US_RTSEN);
  1947. }
  1948. return 0;
  1949. err_add_port:
  1950. kfree(port->rx_ring.buf);
  1951. port->rx_ring.buf = NULL;
  1952. err_alloc_ring:
  1953. if (!atmel_is_console_port(&port->uart)) {
  1954. clk_put(port->clk);
  1955. port->clk = NULL;
  1956. }
  1957. err:
  1958. return ret;
  1959. }
  1960. static int atmel_serial_remove(struct platform_device *pdev)
  1961. {
  1962. struct uart_port *port = platform_get_drvdata(pdev);
  1963. struct atmel_uart_port *atmel_port = to_atmel_uart_port(port);
  1964. int ret = 0;
  1965. device_init_wakeup(&pdev->dev, 0);
  1966. ret = uart_remove_one_port(&atmel_uart, port);
  1967. tasklet_kill(&atmel_port->tasklet);
  1968. kfree(atmel_port->rx_ring.buf);
  1969. /* "port" is allocated statically, so we shouldn't free it */
  1970. clear_bit(port->line, atmel_ports_in_use);
  1971. clk_put(atmel_port->clk);
  1972. return ret;
  1973. }
  1974. static struct platform_driver atmel_serial_driver = {
  1975. .probe = atmel_serial_probe,
  1976. .remove = atmel_serial_remove,
  1977. .suspend = atmel_serial_suspend,
  1978. .resume = atmel_serial_resume,
  1979. .driver = {
  1980. .name = "atmel_usart",
  1981. .owner = THIS_MODULE,
  1982. .of_match_table = of_match_ptr(atmel_serial_dt_ids),
  1983. },
  1984. };
  1985. static int __init atmel_serial_init(void)
  1986. {
  1987. int ret;
  1988. ret = uart_register_driver(&atmel_uart);
  1989. if (ret)
  1990. return ret;
  1991. ret = platform_driver_register(&atmel_serial_driver);
  1992. if (ret)
  1993. uart_unregister_driver(&atmel_uart);
  1994. return ret;
  1995. }
  1996. static void __exit atmel_serial_exit(void)
  1997. {
  1998. platform_driver_unregister(&atmel_serial_driver);
  1999. uart_unregister_driver(&atmel_uart);
  2000. }
  2001. module_init(atmel_serial_init);
  2002. module_exit(atmel_serial_exit);
  2003. MODULE_AUTHOR("Rick Bronson");
  2004. MODULE_DESCRIPTION("Atmel AT91 / AT32 serial port driver");
  2005. MODULE_LICENSE("GPL");
  2006. MODULE_ALIAS("platform:atmel_usart");