atmel_serial.c 67 KB

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