atmel_serial.c 67 KB

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