atmel_serial.c 63 KB

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