imx.c 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for Motorola/Freescale IMX serial ports
  4. *
  5. * Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
  6. *
  7. * Author: Sascha Hauer <sascha@saschahauer.de>
  8. * Copyright (C) 2004 Pengutronix
  9. */
  10. #include <linux/circ_buf.h>
  11. #include <linux/module.h>
  12. #include <linux/ioport.h>
  13. #include <linux/init.h>
  14. #include <linux/console.h>
  15. #include <linux/sysrq.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/tty.h>
  18. #include <linux/tty_flip.h>
  19. #include <linux/serial_core.h>
  20. #include <linux/serial.h>
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/ktime.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/rational.h>
  26. #include <linux/slab.h>
  27. #include <linux/of.h>
  28. #include <linux/io.h>
  29. #include <linux/iopoll.h>
  30. #include <linux/dma-mapping.h>
  31. #include <asm/irq.h>
  32. #include <linux/dma/imx-dma.h>
  33. #include "serial_mctrl_gpio.h"
  34. /* Register definitions */
  35. #define URXD0 0x0 /* Receiver Register */
  36. #define URTX0 0x40 /* Transmitter Register */
  37. #define UCR1 0x80 /* Control Register 1 */
  38. #define UCR2 0x84 /* Control Register 2 */
  39. #define UCR3 0x88 /* Control Register 3 */
  40. #define UCR4 0x8c /* Control Register 4 */
  41. #define UFCR 0x90 /* FIFO Control Register */
  42. #define USR1 0x94 /* Status Register 1 */
  43. #define USR2 0x98 /* Status Register 2 */
  44. #define UESC 0x9c /* Escape Character Register */
  45. #define UTIM 0xa0 /* Escape Timer Register */
  46. #define UBIR 0xa4 /* BRM Incremental Register */
  47. #define UBMR 0xa8 /* BRM Modulator Register */
  48. #define UBRC 0xac /* Baud Rate Count Register */
  49. #define IMX21_ONEMS 0xb0 /* One Millisecond register */
  50. #define IMX1_UTS 0xd0 /* UART Test Register on i.mx1 */
  51. #define IMX21_UTS 0xb4 /* UART Test Register on all other i.mx*/
  52. /* UART Control Register Bit Fields.*/
  53. #define URXD_DUMMY_READ (1<<16)
  54. #define URXD_CHARRDY (1<<15)
  55. #define URXD_ERR (1<<14)
  56. #define URXD_OVRRUN (1<<13)
  57. #define URXD_FRMERR (1<<12)
  58. #define URXD_BRK (1<<11)
  59. #define URXD_PRERR (1<<10)
  60. #define URXD_RX_DATA (0xFF<<0)
  61. #define UCR1_ADEN (1<<15) /* Auto detect interrupt */
  62. #define UCR1_ADBR (1<<14) /* Auto detect baud rate */
  63. #define UCR1_TRDYEN (1<<13) /* Transmitter ready interrupt enable */
  64. #define UCR1_IDEN (1<<12) /* Idle condition interrupt */
  65. #define UCR1_ICD_REG(x) (((x) & 3) << 10) /* idle condition detect */
  66. #define UCR1_RRDYEN (1<<9) /* Recv ready interrupt enable */
  67. #define UCR1_RXDMAEN (1<<8) /* Recv ready DMA enable */
  68. #define UCR1_IREN (1<<7) /* Infrared interface enable */
  69. #define UCR1_TXMPTYEN (1<<6) /* Transimitter empty interrupt enable */
  70. #define UCR1_RTSDEN (1<<5) /* RTS delta interrupt enable */
  71. #define UCR1_SNDBRK (1<<4) /* Send break */
  72. #define UCR1_TXDMAEN (1<<3) /* Transmitter ready DMA enable */
  73. #define IMX1_UCR1_UARTCLKEN (1<<2) /* UART clock enabled, i.mx1 only */
  74. #define UCR1_ATDMAEN (1<<2) /* Aging DMA Timer Enable */
  75. #define UCR1_DOZE (1<<1) /* Doze */
  76. #define UCR1_UARTEN (1<<0) /* UART enabled */
  77. #define UCR2_ESCI (1<<15) /* Escape seq interrupt enable */
  78. #define UCR2_IRTS (1<<14) /* Ignore RTS pin */
  79. #define UCR2_CTSC (1<<13) /* CTS pin control */
  80. #define UCR2_CTS (1<<12) /* Clear to send */
  81. #define UCR2_ESCEN (1<<11) /* Escape enable */
  82. #define UCR2_PREN (1<<8) /* Parity enable */
  83. #define UCR2_PROE (1<<7) /* Parity odd/even */
  84. #define UCR2_STPB (1<<6) /* Stop */
  85. #define UCR2_WS (1<<5) /* Word size */
  86. #define UCR2_RTSEN (1<<4) /* Request to send interrupt enable */
  87. #define UCR2_ATEN (1<<3) /* Aging Timer Enable */
  88. #define UCR2_TXEN (1<<2) /* Transmitter enabled */
  89. #define UCR2_RXEN (1<<1) /* Receiver enabled */
  90. #define UCR2_SRST (1<<0) /* SW reset */
  91. #define UCR3_DTREN (1<<13) /* DTR interrupt enable */
  92. #define UCR3_PARERREN (1<<12) /* Parity enable */
  93. #define UCR3_FRAERREN (1<<11) /* Frame error interrupt enable */
  94. #define UCR3_DSR (1<<10) /* Data set ready */
  95. #define UCR3_DCD (1<<9) /* Data carrier detect */
  96. #define UCR3_RI (1<<8) /* Ring indicator */
  97. #define UCR3_ADNIMP (1<<7) /* Autobaud Detection Not Improved */
  98. #define UCR3_RXDSEN (1<<6) /* Receive status interrupt enable */
  99. #define UCR3_AIRINTEN (1<<5) /* Async IR wake interrupt enable */
  100. #define UCR3_AWAKEN (1<<4) /* Async wake interrupt enable */
  101. #define UCR3_DTRDEN (1<<3) /* Data Terminal Ready Delta Enable. */
  102. #define IMX21_UCR3_RXDMUXSEL (1<<2) /* RXD Muxed Input Select */
  103. #define UCR3_INVT (1<<1) /* Inverted Infrared transmission */
  104. #define UCR3_BPEN (1<<0) /* Preset registers enable */
  105. #define UCR4_CTSTL_SHF 10 /* CTS trigger level shift */
  106. #define UCR4_CTSTL_MASK 0x3F /* CTS trigger is 6 bits wide */
  107. #define UCR4_INVR (1<<9) /* Inverted infrared reception */
  108. #define UCR4_ENIRI (1<<8) /* Serial infrared interrupt enable */
  109. #define UCR4_WKEN (1<<7) /* Wake interrupt enable */
  110. #define UCR4_REF16 (1<<6) /* Ref freq 16 MHz */
  111. #define UCR4_IDDMAEN (1<<6) /* DMA IDLE Condition Detected */
  112. #define UCR4_IRSC (1<<5) /* IR special case */
  113. #define UCR4_TCEN (1<<3) /* Transmit complete interrupt enable */
  114. #define UCR4_BKEN (1<<2) /* Break condition interrupt enable */
  115. #define UCR4_OREN (1<<1) /* Receiver overrun interrupt enable */
  116. #define UCR4_DREN (1<<0) /* Recv data ready interrupt enable */
  117. #define UFCR_RXTL_SHF 0 /* Receiver trigger level shift */
  118. #define UFCR_RXTL_MASK 0x3F /* Receiver trigger 6 bits wide */
  119. #define UFCR_DCEDTE (1<<6) /* DCE/DTE mode select */
  120. #define UFCR_RFDIV (7<<7) /* Reference freq divider mask */
  121. #define UFCR_RFDIV_REG(x) (((x) < 7 ? 6 - (x) : 6) << 7)
  122. #define UFCR_TXTL_SHF 10 /* Transmitter trigger level shift */
  123. #define USR1_PARITYERR (1<<15) /* Parity error interrupt flag */
  124. #define USR1_RTSS (1<<14) /* RTS pin status */
  125. #define USR1_TRDY (1<<13) /* Transmitter ready interrupt/dma flag */
  126. #define USR1_RTSD (1<<12) /* RTS delta */
  127. #define USR1_ESCF (1<<11) /* Escape seq interrupt flag */
  128. #define USR1_FRAMERR (1<<10) /* Frame error interrupt flag */
  129. #define USR1_RRDY (1<<9) /* Receiver ready interrupt/dma flag */
  130. #define USR1_AGTIM (1<<8) /* Ageing timer interrupt flag */
  131. #define USR1_DTRD (1<<7) /* DTR Delta */
  132. #define USR1_RXDS (1<<6) /* Receiver idle interrupt flag */
  133. #define USR1_AIRINT (1<<5) /* Async IR wake interrupt flag */
  134. #define USR1_AWAKE (1<<4) /* Aysnc wake interrupt flag */
  135. #define USR2_ADET (1<<15) /* Auto baud rate detect complete */
  136. #define USR2_TXFE (1<<14) /* Transmit buffer FIFO empty */
  137. #define USR2_DTRF (1<<13) /* DTR edge interrupt flag */
  138. #define USR2_IDLE (1<<12) /* Idle condition */
  139. #define USR2_RIDELT (1<<10) /* Ring Interrupt Delta */
  140. #define USR2_RIIN (1<<9) /* Ring Indicator Input */
  141. #define USR2_IRINT (1<<8) /* Serial infrared interrupt flag */
  142. #define USR2_WAKE (1<<7) /* Wake */
  143. #define USR2_DCDIN (1<<5) /* Data Carrier Detect Input */
  144. #define USR2_RTSF (1<<4) /* RTS edge interrupt flag */
  145. #define USR2_TXDC (1<<3) /* Transmitter complete */
  146. #define USR2_BRCD (1<<2) /* Break condition */
  147. #define USR2_ORE (1<<1) /* Overrun error */
  148. #define USR2_RDR (1<<0) /* Recv data ready */
  149. #define UTS_FRCPERR (1<<13) /* Force parity error */
  150. #define UTS_LOOP (1<<12) /* Loop tx and rx */
  151. #define UTS_TXEMPTY (1<<6) /* TxFIFO empty */
  152. #define UTS_RXEMPTY (1<<5) /* RxFIFO empty */
  153. #define UTS_TXFULL (1<<4) /* TxFIFO full */
  154. #define UTS_RXFULL (1<<3) /* RxFIFO full */
  155. #define UTS_SOFTRST (1<<0) /* Software reset */
  156. /* We've been assigned a range on the "Low-density serial ports" major */
  157. #define SERIAL_IMX_MAJOR 207
  158. #define MINOR_START 16
  159. #define DEV_NAME "ttymxc"
  160. /*
  161. * This determines how often we check the modem status signals
  162. * for any change. They generally aren't connected to an IRQ
  163. * so we have to poll them. We also check immediately before
  164. * filling the TX fifo incase CTS has been dropped.
  165. */
  166. #define MCTRL_TIMEOUT (250*HZ/1000)
  167. #define DRIVER_NAME "IMX-uart"
  168. #define UART_NR 8
  169. /* i.MX21 type uart runs on all i.mx except i.MX1 and i.MX6q */
  170. enum imx_uart_type {
  171. IMX1_UART,
  172. IMX21_UART,
  173. };
  174. /* device type dependent stuff */
  175. struct imx_uart_data {
  176. unsigned uts_reg;
  177. enum imx_uart_type devtype;
  178. };
  179. enum imx_tx_state {
  180. OFF,
  181. WAIT_AFTER_RTS,
  182. SEND,
  183. WAIT_AFTER_SEND,
  184. };
  185. struct imx_port {
  186. struct uart_port port;
  187. struct timer_list timer;
  188. unsigned int old_status;
  189. unsigned int have_rtscts:1;
  190. unsigned int have_rtsgpio:1;
  191. unsigned int dte_mode:1;
  192. unsigned int inverted_tx:1;
  193. unsigned int inverted_rx:1;
  194. struct clk *clk_ipg;
  195. struct clk *clk_per;
  196. const struct imx_uart_data *devdata;
  197. struct mctrl_gpios *gpios;
  198. /* counter to stop 0xff flood */
  199. int idle_counter;
  200. /* DMA fields */
  201. unsigned int dma_is_enabled:1;
  202. unsigned int dma_is_rxing:1;
  203. unsigned int dma_is_txing:1;
  204. struct dma_chan *dma_chan_rx, *dma_chan_tx;
  205. struct scatterlist rx_sgl, tx_sgl[2];
  206. void *rx_buf;
  207. struct circ_buf rx_ring;
  208. unsigned int rx_buf_size;
  209. unsigned int rx_period_length;
  210. unsigned int rx_periods;
  211. dma_cookie_t rx_cookie;
  212. unsigned int tx_bytes;
  213. unsigned int dma_tx_nents;
  214. unsigned int saved_reg[10];
  215. bool context_saved;
  216. enum imx_tx_state tx_state;
  217. struct hrtimer trigger_start_tx;
  218. struct hrtimer trigger_stop_tx;
  219. unsigned int rxtl;
  220. };
  221. struct imx_port_ucrs {
  222. unsigned int ucr1;
  223. unsigned int ucr2;
  224. unsigned int ucr3;
  225. };
  226. static const struct imx_uart_data imx_uart_imx1_devdata = {
  227. .uts_reg = IMX1_UTS,
  228. .devtype = IMX1_UART,
  229. };
  230. static const struct imx_uart_data imx_uart_imx21_devdata = {
  231. .uts_reg = IMX21_UTS,
  232. .devtype = IMX21_UART,
  233. };
  234. static const struct of_device_id imx_uart_dt_ids[] = {
  235. /*
  236. * For reasons unknown to me, some UART devices (e.g. imx6ul's) are
  237. * compatible to fsl,imx6q-uart, but not fsl,imx21-uart, while the
  238. * original imx6q's UART is compatible to fsl,imx21-uart. This driver
  239. * doesn't make any distinction between these two variants.
  240. */
  241. { .compatible = "fsl,imx6q-uart", .data = &imx_uart_imx21_devdata, },
  242. { .compatible = "fsl,imx1-uart", .data = &imx_uart_imx1_devdata, },
  243. { .compatible = "fsl,imx21-uart", .data = &imx_uart_imx21_devdata, },
  244. { /* sentinel */ }
  245. };
  246. MODULE_DEVICE_TABLE(of, imx_uart_dt_ids);
  247. static inline struct imx_port *to_imx_port(struct uart_port *port)
  248. {
  249. return container_of(port, struct imx_port, port);
  250. }
  251. static inline void imx_uart_writel(struct imx_port *sport, u32 val, u32 offset)
  252. {
  253. writel(val, sport->port.membase + offset);
  254. }
  255. static inline u32 imx_uart_readl(struct imx_port *sport, u32 offset)
  256. {
  257. return readl(sport->port.membase + offset);
  258. }
  259. static inline unsigned imx_uart_uts_reg(struct imx_port *sport)
  260. {
  261. return sport->devdata->uts_reg;
  262. }
  263. static inline int imx_uart_is_imx1(struct imx_port *sport)
  264. {
  265. return sport->devdata->devtype == IMX1_UART;
  266. }
  267. /*
  268. * Save and restore functions for UCR1, UCR2 and UCR3 registers
  269. */
  270. #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
  271. static void imx_uart_ucrs_save(struct imx_port *sport,
  272. struct imx_port_ucrs *ucr)
  273. {
  274. /* save control registers */
  275. ucr->ucr1 = imx_uart_readl(sport, UCR1);
  276. ucr->ucr2 = imx_uart_readl(sport, UCR2);
  277. ucr->ucr3 = imx_uart_readl(sport, UCR3);
  278. }
  279. static void imx_uart_ucrs_restore(struct imx_port *sport,
  280. struct imx_port_ucrs *ucr)
  281. {
  282. /* restore control registers */
  283. imx_uart_writel(sport, ucr->ucr1, UCR1);
  284. imx_uart_writel(sport, ucr->ucr2, UCR2);
  285. imx_uart_writel(sport, ucr->ucr3, UCR3);
  286. }
  287. #endif
  288. /* called with port.lock taken and irqs caller dependent */
  289. static void imx_uart_rts_active(struct imx_port *sport, u32 *ucr2)
  290. {
  291. *ucr2 &= ~(UCR2_CTSC | UCR2_CTS);
  292. mctrl_gpio_set(sport->gpios, sport->port.mctrl | TIOCM_RTS);
  293. }
  294. /* called with port.lock taken and irqs caller dependent */
  295. static void imx_uart_rts_inactive(struct imx_port *sport, u32 *ucr2)
  296. {
  297. *ucr2 &= ~UCR2_CTSC;
  298. *ucr2 |= UCR2_CTS;
  299. mctrl_gpio_set(sport->gpios, sport->port.mctrl & ~TIOCM_RTS);
  300. }
  301. static void start_hrtimer_ms(struct hrtimer *hrt, unsigned long msec)
  302. {
  303. hrtimer_start(hrt, ms_to_ktime(msec), HRTIMER_MODE_REL);
  304. }
  305. /* called with port.lock taken and irqs off */
  306. static void imx_uart_soft_reset(struct imx_port *sport)
  307. {
  308. int i = 10;
  309. u32 ucr2, ubir, ubmr, uts;
  310. /*
  311. * According to the Reference Manual description of the UART SRST bit:
  312. *
  313. * "Reset the transmit and receive state machines,
  314. * all FIFOs and register USR1, USR2, UBIR, UBMR, UBRC, URXD, UTXD
  315. * and UTS[6-3]".
  316. *
  317. * We don't need to restore the old values from USR1, USR2, URXD and
  318. * UTXD. UBRC is read only, so only save/restore the other three
  319. * registers.
  320. */
  321. ubir = imx_uart_readl(sport, UBIR);
  322. ubmr = imx_uart_readl(sport, UBMR);
  323. uts = imx_uart_readl(sport, IMX21_UTS);
  324. ucr2 = imx_uart_readl(sport, UCR2);
  325. imx_uart_writel(sport, ucr2 & ~UCR2_SRST, UCR2);
  326. while (!(imx_uart_readl(sport, UCR2) & UCR2_SRST) && (--i > 0))
  327. udelay(1);
  328. /* Restore the registers */
  329. imx_uart_writel(sport, ubir, UBIR);
  330. imx_uart_writel(sport, ubmr, UBMR);
  331. imx_uart_writel(sport, uts, IMX21_UTS);
  332. sport->idle_counter = 0;
  333. }
  334. static void imx_uart_disable_loopback_rs485(struct imx_port *sport)
  335. {
  336. unsigned int uts;
  337. /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
  338. uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
  339. uts &= ~UTS_LOOP;
  340. imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
  341. }
  342. /* called with port.lock taken and irqs off */
  343. static void imx_uart_start_rx(struct uart_port *port)
  344. {
  345. struct imx_port *sport = to_imx_port(port);
  346. unsigned int ucr1, ucr2;
  347. ucr1 = imx_uart_readl(sport, UCR1);
  348. ucr2 = imx_uart_readl(sport, UCR2);
  349. ucr2 |= UCR2_RXEN;
  350. if (sport->dma_is_enabled) {
  351. ucr1 |= UCR1_RXDMAEN | UCR1_ATDMAEN;
  352. } else {
  353. ucr1 |= UCR1_RRDYEN;
  354. ucr2 |= UCR2_ATEN;
  355. }
  356. /* Write UCR2 first as it includes RXEN */
  357. imx_uart_writel(sport, ucr2, UCR2);
  358. imx_uart_writel(sport, ucr1, UCR1);
  359. imx_uart_disable_loopback_rs485(sport);
  360. }
  361. /* called with port.lock taken and irqs off */
  362. static void imx_uart_stop_tx(struct uart_port *port)
  363. {
  364. struct imx_port *sport = to_imx_port(port);
  365. u32 ucr1, ucr4, usr2;
  366. if (sport->tx_state == OFF)
  367. return;
  368. /*
  369. * We are maybe in the SMP context, so if the DMA TX thread is running
  370. * on other cpu, we have to wait for it to finish.
  371. */
  372. if (sport->dma_is_txing)
  373. return;
  374. ucr1 = imx_uart_readl(sport, UCR1);
  375. imx_uart_writel(sport, ucr1 & ~UCR1_TRDYEN, UCR1);
  376. ucr4 = imx_uart_readl(sport, UCR4);
  377. usr2 = imx_uart_readl(sport, USR2);
  378. if ((!(usr2 & USR2_TXDC)) && (ucr4 & UCR4_TCEN)) {
  379. /* The shifter is still busy, so retry once TC triggers */
  380. return;
  381. }
  382. ucr4 &= ~UCR4_TCEN;
  383. imx_uart_writel(sport, ucr4, UCR4);
  384. /* in rs485 mode disable transmitter */
  385. if (port->rs485.flags & SER_RS485_ENABLED) {
  386. if (sport->tx_state == SEND) {
  387. sport->tx_state = WAIT_AFTER_SEND;
  388. if (port->rs485.delay_rts_after_send > 0) {
  389. start_hrtimer_ms(&sport->trigger_stop_tx,
  390. port->rs485.delay_rts_after_send);
  391. return;
  392. }
  393. /* continue without any delay */
  394. }
  395. if (sport->tx_state == WAIT_AFTER_RTS ||
  396. sport->tx_state == WAIT_AFTER_SEND) {
  397. u32 ucr2;
  398. hrtimer_try_to_cancel(&sport->trigger_start_tx);
  399. ucr2 = imx_uart_readl(sport, UCR2);
  400. if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
  401. imx_uart_rts_active(sport, &ucr2);
  402. else
  403. imx_uart_rts_inactive(sport, &ucr2);
  404. imx_uart_writel(sport, ucr2, UCR2);
  405. if (!port->rs485_rx_during_tx_gpio)
  406. imx_uart_start_rx(port);
  407. sport->tx_state = OFF;
  408. }
  409. } else {
  410. sport->tx_state = OFF;
  411. }
  412. }
  413. static void imx_uart_stop_rx_with_loopback_ctrl(struct uart_port *port, bool loopback)
  414. {
  415. struct imx_port *sport = to_imx_port(port);
  416. u32 ucr1, ucr2, ucr4, uts;
  417. ucr1 = imx_uart_readl(sport, UCR1);
  418. ucr2 = imx_uart_readl(sport, UCR2);
  419. ucr4 = imx_uart_readl(sport, UCR4);
  420. if (sport->dma_is_enabled) {
  421. ucr1 &= ~(UCR1_RXDMAEN | UCR1_ATDMAEN);
  422. } else {
  423. ucr1 &= ~UCR1_RRDYEN;
  424. ucr2 &= ~UCR2_ATEN;
  425. ucr4 &= ~UCR4_OREN;
  426. }
  427. imx_uart_writel(sport, ucr1, UCR1);
  428. imx_uart_writel(sport, ucr4, UCR4);
  429. /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
  430. if (port->rs485.flags & SER_RS485_ENABLED &&
  431. port->rs485.flags & SER_RS485_RTS_ON_SEND &&
  432. sport->have_rtscts && !sport->have_rtsgpio && loopback) {
  433. uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
  434. uts |= UTS_LOOP;
  435. imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
  436. ucr2 |= UCR2_RXEN;
  437. } else {
  438. ucr2 &= ~UCR2_RXEN;
  439. }
  440. imx_uart_writel(sport, ucr2, UCR2);
  441. }
  442. /* called with port.lock taken and irqs off */
  443. static void imx_uart_stop_rx(struct uart_port *port)
  444. {
  445. /*
  446. * Stop RX and enable loopback in order to make sure RS485 bus
  447. * is not blocked. Se comment in imx_uart_probe().
  448. */
  449. imx_uart_stop_rx_with_loopback_ctrl(port, true);
  450. }
  451. /* called with port.lock taken and irqs off */
  452. static void imx_uart_enable_ms(struct uart_port *port)
  453. {
  454. struct imx_port *sport = to_imx_port(port);
  455. mod_timer(&sport->timer, jiffies);
  456. mctrl_gpio_enable_ms(sport->gpios);
  457. }
  458. static void imx_uart_dma_tx(struct imx_port *sport);
  459. /* called with port.lock taken and irqs off */
  460. static inline void imx_uart_transmit_buffer(struct imx_port *sport)
  461. {
  462. struct tty_port *tport = &sport->port.state->port;
  463. unsigned char c;
  464. if (sport->port.x_char) {
  465. /* Send next char */
  466. imx_uart_writel(sport, sport->port.x_char, URTX0);
  467. sport->port.icount.tx++;
  468. sport->port.x_char = 0;
  469. return;
  470. }
  471. if (kfifo_is_empty(&tport->xmit_fifo) ||
  472. uart_tx_stopped(&sport->port)) {
  473. imx_uart_stop_tx(&sport->port);
  474. return;
  475. }
  476. if (sport->dma_is_enabled) {
  477. u32 ucr1;
  478. /*
  479. * We've just sent a X-char Ensure the TX DMA is enabled
  480. * and the TX IRQ is disabled.
  481. **/
  482. ucr1 = imx_uart_readl(sport, UCR1);
  483. ucr1 &= ~UCR1_TRDYEN;
  484. if (sport->dma_is_txing) {
  485. ucr1 |= UCR1_TXDMAEN;
  486. imx_uart_writel(sport, ucr1, UCR1);
  487. } else {
  488. imx_uart_writel(sport, ucr1, UCR1);
  489. imx_uart_dma_tx(sport);
  490. }
  491. return;
  492. }
  493. while (!(imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL) &&
  494. uart_fifo_get(&sport->port, &c))
  495. imx_uart_writel(sport, c, URTX0);
  496. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  497. uart_write_wakeup(&sport->port);
  498. if (kfifo_is_empty(&tport->xmit_fifo))
  499. imx_uart_stop_tx(&sport->port);
  500. }
  501. static void imx_uart_dma_tx_callback(void *data)
  502. {
  503. struct imx_port *sport = data;
  504. struct tty_port *tport = &sport->port.state->port;
  505. struct scatterlist *sgl = &sport->tx_sgl[0];
  506. unsigned long flags;
  507. u32 ucr1;
  508. uart_port_lock_irqsave(&sport->port, &flags);
  509. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  510. ucr1 = imx_uart_readl(sport, UCR1);
  511. ucr1 &= ~UCR1_TXDMAEN;
  512. imx_uart_writel(sport, ucr1, UCR1);
  513. uart_xmit_advance(&sport->port, sport->tx_bytes);
  514. dev_dbg(sport->port.dev, "we finish the TX DMA.\n");
  515. sport->dma_is_txing = 0;
  516. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  517. uart_write_wakeup(&sport->port);
  518. if (!kfifo_is_empty(&tport->xmit_fifo) &&
  519. !uart_tx_stopped(&sport->port))
  520. imx_uart_dma_tx(sport);
  521. else if (sport->port.rs485.flags & SER_RS485_ENABLED) {
  522. u32 ucr4 = imx_uart_readl(sport, UCR4);
  523. ucr4 |= UCR4_TCEN;
  524. imx_uart_writel(sport, ucr4, UCR4);
  525. }
  526. uart_port_unlock_irqrestore(&sport->port, flags);
  527. }
  528. /* called with port.lock taken and irqs off */
  529. static void imx_uart_dma_tx(struct imx_port *sport)
  530. {
  531. struct tty_port *tport = &sport->port.state->port;
  532. struct scatterlist *sgl = sport->tx_sgl;
  533. struct dma_async_tx_descriptor *desc;
  534. struct dma_chan *chan = sport->dma_chan_tx;
  535. struct device *dev = sport->port.dev;
  536. u32 ucr1, ucr4;
  537. int ret;
  538. if (sport->dma_is_txing)
  539. return;
  540. ucr4 = imx_uart_readl(sport, UCR4);
  541. ucr4 &= ~UCR4_TCEN;
  542. imx_uart_writel(sport, ucr4, UCR4);
  543. sg_init_table(sgl, ARRAY_SIZE(sport->tx_sgl));
  544. sport->tx_bytes = kfifo_len(&tport->xmit_fifo);
  545. sport->dma_tx_nents = kfifo_dma_out_prepare(&tport->xmit_fifo, sgl,
  546. ARRAY_SIZE(sport->tx_sgl), sport->tx_bytes);
  547. ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  548. if (ret == 0) {
  549. dev_err(dev, "DMA mapping error for TX.\n");
  550. return;
  551. }
  552. desc = dmaengine_prep_slave_sg(chan, sgl, ret,
  553. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  554. if (!desc) {
  555. dma_unmap_sg(dev, sgl, sport->dma_tx_nents,
  556. DMA_TO_DEVICE);
  557. dev_err(dev, "We cannot prepare for the TX slave dma!\n");
  558. return;
  559. }
  560. desc->callback = imx_uart_dma_tx_callback;
  561. desc->callback_param = sport;
  562. dev_dbg(dev, "TX: prepare to send %u bytes by DMA.\n", sport->tx_bytes);
  563. ucr1 = imx_uart_readl(sport, UCR1);
  564. ucr1 |= UCR1_TXDMAEN;
  565. imx_uart_writel(sport, ucr1, UCR1);
  566. /* fire it */
  567. sport->dma_is_txing = 1;
  568. dmaengine_submit(desc);
  569. dma_async_issue_pending(chan);
  570. return;
  571. }
  572. /* called with port.lock taken and irqs off */
  573. static void imx_uart_start_tx(struct uart_port *port)
  574. {
  575. struct imx_port *sport = to_imx_port(port);
  576. struct tty_port *tport = &sport->port.state->port;
  577. u32 ucr1;
  578. if (!sport->port.x_char && kfifo_is_empty(&tport->xmit_fifo))
  579. return;
  580. /*
  581. * We cannot simply do nothing here if sport->tx_state == SEND already
  582. * because UCR1_TXMPTYEN might already have been cleared in
  583. * imx_uart_stop_tx(), but tx_state is still SEND.
  584. */
  585. if (port->rs485.flags & SER_RS485_ENABLED) {
  586. if (sport->tx_state == OFF) {
  587. u32 ucr2 = imx_uart_readl(sport, UCR2);
  588. if (port->rs485.flags & SER_RS485_RTS_ON_SEND)
  589. imx_uart_rts_active(sport, &ucr2);
  590. else
  591. imx_uart_rts_inactive(sport, &ucr2);
  592. imx_uart_writel(sport, ucr2, UCR2);
  593. /*
  594. * Since we are about to transmit we can not stop RX
  595. * with loopback enabled because that will make our
  596. * transmitted data being just looped to RX.
  597. */
  598. if (!(port->rs485.flags & SER_RS485_RX_DURING_TX) &&
  599. !port->rs485_rx_during_tx_gpio)
  600. imx_uart_stop_rx_with_loopback_ctrl(port, false);
  601. sport->tx_state = WAIT_AFTER_RTS;
  602. if (port->rs485.delay_rts_before_send > 0) {
  603. start_hrtimer_ms(&sport->trigger_start_tx,
  604. port->rs485.delay_rts_before_send);
  605. return;
  606. }
  607. /* continue without any delay */
  608. }
  609. if (sport->tx_state == WAIT_AFTER_SEND
  610. || sport->tx_state == WAIT_AFTER_RTS) {
  611. hrtimer_try_to_cancel(&sport->trigger_stop_tx);
  612. /*
  613. * Enable transmitter and shifter empty irq only if DMA
  614. * is off. In the DMA case this is done in the
  615. * tx-callback.
  616. */
  617. if (!sport->dma_is_enabled) {
  618. u32 ucr4 = imx_uart_readl(sport, UCR4);
  619. ucr4 |= UCR4_TCEN;
  620. imx_uart_writel(sport, ucr4, UCR4);
  621. }
  622. sport->tx_state = SEND;
  623. }
  624. } else {
  625. sport->tx_state = SEND;
  626. }
  627. if (!sport->dma_is_enabled) {
  628. ucr1 = imx_uart_readl(sport, UCR1);
  629. imx_uart_writel(sport, ucr1 | UCR1_TRDYEN, UCR1);
  630. }
  631. if (sport->dma_is_enabled) {
  632. if (sport->port.x_char) {
  633. /* We have X-char to send, so enable TX IRQ and
  634. * disable TX DMA to let TX interrupt to send X-char */
  635. ucr1 = imx_uart_readl(sport, UCR1);
  636. ucr1 &= ~UCR1_TXDMAEN;
  637. ucr1 |= UCR1_TRDYEN;
  638. imx_uart_writel(sport, ucr1, UCR1);
  639. return;
  640. }
  641. if (!kfifo_is_empty(&tport->xmit_fifo) &&
  642. !uart_tx_stopped(port))
  643. imx_uart_dma_tx(sport);
  644. return;
  645. }
  646. }
  647. static irqreturn_t __imx_uart_rtsint(int irq, void *dev_id)
  648. {
  649. struct imx_port *sport = dev_id;
  650. u32 usr1;
  651. imx_uart_writel(sport, USR1_RTSD, USR1);
  652. usr1 = imx_uart_readl(sport, USR1) & USR1_RTSS;
  653. /*
  654. * Update sport->old_status here, so any follow-up calls to
  655. * imx_uart_mctrl_check() will be able to recognize that RTS
  656. * state changed since last imx_uart_mctrl_check() call.
  657. *
  658. * In case RTS has been detected as asserted here and later on
  659. * deasserted by the time imx_uart_mctrl_check() was called,
  660. * imx_uart_mctrl_check() can detect the RTS state change and
  661. * trigger uart_handle_cts_change() to unblock the port for
  662. * further TX transfers.
  663. */
  664. if (usr1 & USR1_RTSS)
  665. sport->old_status |= TIOCM_CTS;
  666. else
  667. sport->old_status &= ~TIOCM_CTS;
  668. uart_handle_cts_change(&sport->port, usr1);
  669. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  670. return IRQ_HANDLED;
  671. }
  672. static irqreturn_t imx_uart_rtsint(int irq, void *dev_id)
  673. {
  674. struct imx_port *sport = dev_id;
  675. irqreturn_t ret;
  676. uart_port_lock(&sport->port);
  677. ret = __imx_uart_rtsint(irq, dev_id);
  678. uart_port_unlock(&sport->port);
  679. return ret;
  680. }
  681. static irqreturn_t imx_uart_txint(int irq, void *dev_id)
  682. {
  683. struct imx_port *sport = dev_id;
  684. uart_port_lock(&sport->port);
  685. imx_uart_transmit_buffer(sport);
  686. uart_port_unlock(&sport->port);
  687. return IRQ_HANDLED;
  688. }
  689. /* Check if hardware Rx flood is in progress, and issue soft reset to stop it.
  690. * This is to be called from Rx ISRs only when some bytes were actually
  691. * received.
  692. *
  693. * A way to reproduce the flood (checked on iMX6SX) is: open iMX UART at 9600
  694. * 8N1, and from external source send 0xf0 char at 115200 8N1. In about 90% of
  695. * cases this starts a flood of "receiving" of 0xff characters by the iMX6 UART
  696. * that is terminated by any activity on RxD line, or could be stopped by
  697. * issuing soft reset to the UART (just stop/start of RX does not help). Note
  698. * that what we do here is sending isolated start bit about 2.4 times shorter
  699. * than it is to be on UART configured baud rate.
  700. */
  701. static void imx_uart_check_flood(struct imx_port *sport, u32 usr2)
  702. {
  703. /* To detect hardware 0xff flood we monitor RxD line between RX
  704. * interrupts to isolate "receiving" of char(s) with no activity
  705. * on RxD line, that'd never happen on actual data transfers.
  706. *
  707. * We use USR2_WAKE bit to check for activity on RxD line, but we have a
  708. * race here if we clear USR2_WAKE when receiving of a char is in
  709. * progress, so we might get RX interrupt later with USR2_WAKE bit
  710. * cleared. Note though that as we don't try to clear USR2_WAKE when we
  711. * detected no activity, this race may hide actual activity only once.
  712. *
  713. * Yet another case where receive interrupt may occur without RxD
  714. * activity is expiration of aging timer, so we consider this as well.
  715. *
  716. * We use 'idle_counter' to ensure that we got at least so many RX
  717. * interrupts without any detected activity on RxD line. 2 cases
  718. * described plus 1 to be on the safe side gives us a margin of 3,
  719. * below. In practice I was not able to produce a false positive to
  720. * induce soft reset at regular data transfers even using 1 as the
  721. * margin, so 3 is actually very strong.
  722. *
  723. * We count interrupts, not chars in 'idle-counter' for simplicity.
  724. */
  725. if (usr2 & USR2_WAKE) {
  726. imx_uart_writel(sport, USR2_WAKE, USR2);
  727. sport->idle_counter = 0;
  728. } else if (++sport->idle_counter > 3) {
  729. dev_warn(sport->port.dev, "RX flood detected: soft reset.");
  730. imx_uart_soft_reset(sport); /* also clears 'sport->idle_counter' */
  731. }
  732. }
  733. static irqreturn_t __imx_uart_rxint(int irq, void *dev_id)
  734. {
  735. struct imx_port *sport = dev_id;
  736. struct tty_port *port = &sport->port.state->port;
  737. u32 usr2, rx;
  738. /* If we received something, check for 0xff flood */
  739. usr2 = imx_uart_readl(sport, USR2);
  740. if (usr2 & USR2_RDR)
  741. imx_uart_check_flood(sport, usr2);
  742. while ((rx = imx_uart_readl(sport, URXD0)) & URXD_CHARRDY) {
  743. unsigned int flg = TTY_NORMAL;
  744. sport->port.icount.rx++;
  745. if (unlikely(rx & URXD_ERR)) {
  746. if (rx & URXD_BRK) {
  747. sport->port.icount.brk++;
  748. if (uart_handle_break(&sport->port))
  749. continue;
  750. }
  751. else if (rx & URXD_PRERR)
  752. sport->port.icount.parity++;
  753. else if (rx & URXD_FRMERR)
  754. sport->port.icount.frame++;
  755. if (rx & URXD_OVRRUN)
  756. sport->port.icount.overrun++;
  757. if (rx & sport->port.ignore_status_mask)
  758. continue;
  759. rx &= (sport->port.read_status_mask | 0xFF);
  760. if (rx & URXD_BRK)
  761. flg = TTY_BREAK;
  762. else if (rx & URXD_PRERR)
  763. flg = TTY_PARITY;
  764. else if (rx & URXD_FRMERR)
  765. flg = TTY_FRAME;
  766. if (rx & URXD_OVRRUN)
  767. flg = TTY_OVERRUN;
  768. sport->port.sysrq = 0;
  769. } else if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx)) {
  770. continue;
  771. }
  772. if (sport->port.ignore_status_mask & URXD_DUMMY_READ)
  773. continue;
  774. if (tty_insert_flip_char(port, rx, flg) == 0)
  775. sport->port.icount.buf_overrun++;
  776. }
  777. tty_flip_buffer_push(port);
  778. return IRQ_HANDLED;
  779. }
  780. static irqreturn_t imx_uart_rxint(int irq, void *dev_id)
  781. {
  782. struct imx_port *sport = dev_id;
  783. irqreturn_t ret;
  784. uart_port_lock(&sport->port);
  785. ret = __imx_uart_rxint(irq, dev_id);
  786. uart_port_unlock(&sport->port);
  787. return ret;
  788. }
  789. static void imx_uart_clear_rx_errors(struct imx_port *sport);
  790. /*
  791. * We have a modem side uart, so the meanings of RTS and CTS are inverted.
  792. */
  793. static unsigned int imx_uart_get_hwmctrl(struct imx_port *sport)
  794. {
  795. unsigned int tmp = TIOCM_DSR;
  796. unsigned usr1 = imx_uart_readl(sport, USR1);
  797. unsigned usr2 = imx_uart_readl(sport, USR2);
  798. if (usr1 & USR1_RTSS)
  799. tmp |= TIOCM_CTS;
  800. /* in DCE mode DCDIN is always 0 */
  801. if (!(usr2 & USR2_DCDIN))
  802. tmp |= TIOCM_CAR;
  803. if (sport->dte_mode)
  804. if (!(imx_uart_readl(sport, USR2) & USR2_RIIN))
  805. tmp |= TIOCM_RI;
  806. return tmp;
  807. }
  808. /*
  809. * Handle any change of modem status signal since we were last called.
  810. */
  811. static void imx_uart_mctrl_check(struct imx_port *sport)
  812. {
  813. unsigned int status, changed;
  814. status = imx_uart_get_hwmctrl(sport);
  815. changed = status ^ sport->old_status;
  816. if (changed == 0)
  817. return;
  818. sport->old_status = status;
  819. if (changed & TIOCM_RI && status & TIOCM_RI)
  820. sport->port.icount.rng++;
  821. if (changed & TIOCM_DSR)
  822. sport->port.icount.dsr++;
  823. if (changed & TIOCM_CAR)
  824. uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);
  825. if (changed & TIOCM_CTS)
  826. uart_handle_cts_change(&sport->port, status & TIOCM_CTS);
  827. wake_up_interruptible(&sport->port.state->port.delta_msr_wait);
  828. }
  829. static irqreturn_t imx_uart_int(int irq, void *dev_id)
  830. {
  831. struct imx_port *sport = dev_id;
  832. unsigned int usr1, usr2, ucr1, ucr2, ucr3, ucr4;
  833. irqreturn_t ret = IRQ_NONE;
  834. uart_port_lock(&sport->port);
  835. usr1 = imx_uart_readl(sport, USR1);
  836. usr2 = imx_uart_readl(sport, USR2);
  837. ucr1 = imx_uart_readl(sport, UCR1);
  838. ucr2 = imx_uart_readl(sport, UCR2);
  839. ucr3 = imx_uart_readl(sport, UCR3);
  840. ucr4 = imx_uart_readl(sport, UCR4);
  841. /*
  842. * Even if a condition is true that can trigger an irq only handle it if
  843. * the respective irq source is enabled. This prevents some undesired
  844. * actions, for example if a character that sits in the RX FIFO and that
  845. * should be fetched via DMA is tried to be fetched using PIO. Or the
  846. * receiver is currently off and so reading from URXD0 results in an
  847. * exception. So just mask the (raw) status bits for disabled irqs.
  848. */
  849. if ((ucr1 & UCR1_RRDYEN) == 0)
  850. usr1 &= ~USR1_RRDY;
  851. if ((ucr2 & UCR2_ATEN) == 0)
  852. usr1 &= ~USR1_AGTIM;
  853. if ((ucr1 & UCR1_TRDYEN) == 0)
  854. usr1 &= ~USR1_TRDY;
  855. if ((ucr4 & UCR4_TCEN) == 0)
  856. usr2 &= ~USR2_TXDC;
  857. if ((ucr3 & UCR3_DTRDEN) == 0)
  858. usr1 &= ~USR1_DTRD;
  859. if ((ucr1 & UCR1_RTSDEN) == 0)
  860. usr1 &= ~USR1_RTSD;
  861. if ((ucr3 & UCR3_AWAKEN) == 0)
  862. usr1 &= ~USR1_AWAKE;
  863. if ((ucr4 & UCR4_OREN) == 0)
  864. usr2 &= ~USR2_ORE;
  865. if (usr1 & (USR1_RRDY | USR1_AGTIM)) {
  866. imx_uart_writel(sport, USR1_AGTIM, USR1);
  867. __imx_uart_rxint(irq, dev_id);
  868. ret = IRQ_HANDLED;
  869. }
  870. if ((usr1 & USR1_TRDY) || (usr2 & USR2_TXDC)) {
  871. imx_uart_transmit_buffer(sport);
  872. ret = IRQ_HANDLED;
  873. }
  874. if (usr1 & USR1_DTRD) {
  875. imx_uart_writel(sport, USR1_DTRD, USR1);
  876. imx_uart_mctrl_check(sport);
  877. ret = IRQ_HANDLED;
  878. }
  879. if (usr1 & USR1_RTSD) {
  880. __imx_uart_rtsint(irq, dev_id);
  881. ret = IRQ_HANDLED;
  882. }
  883. if (usr1 & USR1_AWAKE) {
  884. imx_uart_writel(sport, USR1_AWAKE, USR1);
  885. ret = IRQ_HANDLED;
  886. }
  887. if (usr2 & USR2_ORE) {
  888. sport->port.icount.overrun++;
  889. imx_uart_writel(sport, USR2_ORE, USR2);
  890. ret = IRQ_HANDLED;
  891. }
  892. uart_port_unlock(&sport->port);
  893. return ret;
  894. }
  895. /*
  896. * Return TIOCSER_TEMT when transmitter is not busy.
  897. */
  898. static unsigned int imx_uart_tx_empty(struct uart_port *port)
  899. {
  900. struct imx_port *sport = to_imx_port(port);
  901. unsigned int ret;
  902. ret = (imx_uart_readl(sport, USR2) & USR2_TXDC) ? TIOCSER_TEMT : 0;
  903. /* If the TX DMA is working, return 0. */
  904. if (sport->dma_is_txing)
  905. ret = 0;
  906. return ret;
  907. }
  908. /* called with port.lock taken and irqs off */
  909. static unsigned int imx_uart_get_mctrl(struct uart_port *port)
  910. {
  911. struct imx_port *sport = to_imx_port(port);
  912. unsigned int ret = imx_uart_get_hwmctrl(sport);
  913. mctrl_gpio_get(sport->gpios, &ret);
  914. return ret;
  915. }
  916. /* called with port.lock taken and irqs off */
  917. static void imx_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  918. {
  919. struct imx_port *sport = to_imx_port(port);
  920. u32 ucr3, uts;
  921. if (!(port->rs485.flags & SER_RS485_ENABLED)) {
  922. u32 ucr2;
  923. /*
  924. * Turn off autoRTS if RTS is lowered and restore autoRTS
  925. * setting if RTS is raised.
  926. */
  927. ucr2 = imx_uart_readl(sport, UCR2);
  928. ucr2 &= ~(UCR2_CTS | UCR2_CTSC);
  929. if (mctrl & TIOCM_RTS) {
  930. ucr2 |= UCR2_CTS;
  931. /*
  932. * UCR2_IRTS is unset if and only if the port is
  933. * configured for CRTSCTS, so we use inverted UCR2_IRTS
  934. * to get the state to restore to.
  935. */
  936. if (!(ucr2 & UCR2_IRTS))
  937. ucr2 |= UCR2_CTSC;
  938. }
  939. imx_uart_writel(sport, ucr2, UCR2);
  940. }
  941. ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_DSR;
  942. if (!(mctrl & TIOCM_DTR))
  943. ucr3 |= UCR3_DSR;
  944. imx_uart_writel(sport, ucr3, UCR3);
  945. uts = imx_uart_readl(sport, imx_uart_uts_reg(sport)) & ~UTS_LOOP;
  946. if (mctrl & TIOCM_LOOP)
  947. uts |= UTS_LOOP;
  948. imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
  949. mctrl_gpio_set(sport->gpios, mctrl);
  950. }
  951. /*
  952. * Interrupts always disabled.
  953. */
  954. static void imx_uart_break_ctl(struct uart_port *port, int break_state)
  955. {
  956. struct imx_port *sport = to_imx_port(port);
  957. unsigned long flags;
  958. u32 ucr1;
  959. uart_port_lock_irqsave(&sport->port, &flags);
  960. ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_SNDBRK;
  961. if (break_state != 0)
  962. ucr1 |= UCR1_SNDBRK;
  963. imx_uart_writel(sport, ucr1, UCR1);
  964. uart_port_unlock_irqrestore(&sport->port, flags);
  965. }
  966. /*
  967. * This is our per-port timeout handler, for checking the
  968. * modem status signals.
  969. */
  970. static void imx_uart_timeout(struct timer_list *t)
  971. {
  972. struct imx_port *sport = from_timer(sport, t, timer);
  973. unsigned long flags;
  974. if (sport->port.state) {
  975. uart_port_lock_irqsave(&sport->port, &flags);
  976. imx_uart_mctrl_check(sport);
  977. uart_port_unlock_irqrestore(&sport->port, flags);
  978. mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);
  979. }
  980. }
  981. /*
  982. * There are two kinds of RX DMA interrupts(such as in the MX6Q):
  983. * [1] the RX DMA buffer is full.
  984. * [2] the aging timer expires
  985. *
  986. * Condition [2] is triggered when a character has been sitting in the FIFO
  987. * for at least 8 byte durations.
  988. */
  989. static void imx_uart_dma_rx_callback(void *data)
  990. {
  991. struct imx_port *sport = data;
  992. struct dma_chan *chan = sport->dma_chan_rx;
  993. struct scatterlist *sgl = &sport->rx_sgl;
  994. struct tty_port *port = &sport->port.state->port;
  995. struct dma_tx_state state;
  996. struct circ_buf *rx_ring = &sport->rx_ring;
  997. enum dma_status status;
  998. unsigned int w_bytes = 0;
  999. unsigned int r_bytes;
  1000. unsigned int bd_size;
  1001. status = dmaengine_tx_status(chan, sport->rx_cookie, &state);
  1002. if (status == DMA_ERROR) {
  1003. uart_port_lock(&sport->port);
  1004. imx_uart_clear_rx_errors(sport);
  1005. uart_port_unlock(&sport->port);
  1006. return;
  1007. }
  1008. /*
  1009. * The state-residue variable represents the empty space
  1010. * relative to the entire buffer. Taking this in consideration
  1011. * the head is always calculated base on the buffer total
  1012. * length - DMA transaction residue. The UART script from the
  1013. * SDMA firmware will jump to the next buffer descriptor,
  1014. * once a DMA transaction if finalized (IMX53 RM - A.4.1.2.4).
  1015. * Taking this in consideration the tail is always at the
  1016. * beginning of the buffer descriptor that contains the head.
  1017. */
  1018. /* Calculate the head */
  1019. rx_ring->head = sg_dma_len(sgl) - state.residue;
  1020. /* Calculate the tail. */
  1021. bd_size = sg_dma_len(sgl) / sport->rx_periods;
  1022. rx_ring->tail = ((rx_ring->head-1) / bd_size) * bd_size;
  1023. if (rx_ring->head <= sg_dma_len(sgl) &&
  1024. rx_ring->head > rx_ring->tail) {
  1025. /* Move data from tail to head */
  1026. r_bytes = rx_ring->head - rx_ring->tail;
  1027. /* If we received something, check for 0xff flood */
  1028. uart_port_lock(&sport->port);
  1029. imx_uart_check_flood(sport, imx_uart_readl(sport, USR2));
  1030. uart_port_unlock(&sport->port);
  1031. if (!(sport->port.ignore_status_mask & URXD_DUMMY_READ)) {
  1032. /* CPU claims ownership of RX DMA buffer */
  1033. dma_sync_sg_for_cpu(sport->port.dev, sgl, 1,
  1034. DMA_FROM_DEVICE);
  1035. w_bytes = tty_insert_flip_string(port,
  1036. sport->rx_buf + rx_ring->tail, r_bytes);
  1037. /* UART retrieves ownership of RX DMA buffer */
  1038. dma_sync_sg_for_device(sport->port.dev, sgl, 1,
  1039. DMA_FROM_DEVICE);
  1040. if (w_bytes != r_bytes)
  1041. sport->port.icount.buf_overrun++;
  1042. sport->port.icount.rx += w_bytes;
  1043. }
  1044. } else {
  1045. WARN_ON(rx_ring->head > sg_dma_len(sgl));
  1046. WARN_ON(rx_ring->head <= rx_ring->tail);
  1047. }
  1048. if (w_bytes) {
  1049. tty_flip_buffer_push(port);
  1050. dev_dbg(sport->port.dev, "We get %d bytes.\n", w_bytes);
  1051. }
  1052. }
  1053. static int imx_uart_start_rx_dma(struct imx_port *sport)
  1054. {
  1055. struct scatterlist *sgl = &sport->rx_sgl;
  1056. struct dma_chan *chan = sport->dma_chan_rx;
  1057. struct device *dev = sport->port.dev;
  1058. struct dma_async_tx_descriptor *desc;
  1059. int ret;
  1060. sport->rx_ring.head = 0;
  1061. sport->rx_ring.tail = 0;
  1062. sg_init_one(sgl, sport->rx_buf, sport->rx_buf_size);
  1063. ret = dma_map_sg(dev, sgl, 1, DMA_FROM_DEVICE);
  1064. if (ret == 0) {
  1065. dev_err(dev, "DMA mapping error for RX.\n");
  1066. return -EINVAL;
  1067. }
  1068. desc = dmaengine_prep_dma_cyclic(chan, sg_dma_address(sgl),
  1069. sg_dma_len(sgl), sg_dma_len(sgl) / sport->rx_periods,
  1070. DMA_DEV_TO_MEM, DMA_PREP_INTERRUPT);
  1071. if (!desc) {
  1072. dma_unmap_sg(dev, sgl, 1, DMA_FROM_DEVICE);
  1073. dev_err(dev, "We cannot prepare for the RX slave dma!\n");
  1074. return -EINVAL;
  1075. }
  1076. desc->callback = imx_uart_dma_rx_callback;
  1077. desc->callback_param = sport;
  1078. dev_dbg(dev, "RX: prepare for the DMA.\n");
  1079. sport->dma_is_rxing = 1;
  1080. sport->rx_cookie = dmaengine_submit(desc);
  1081. dma_async_issue_pending(chan);
  1082. return 0;
  1083. }
  1084. static void imx_uart_clear_rx_errors(struct imx_port *sport)
  1085. {
  1086. struct tty_port *port = &sport->port.state->port;
  1087. u32 usr1, usr2;
  1088. usr1 = imx_uart_readl(sport, USR1);
  1089. usr2 = imx_uart_readl(sport, USR2);
  1090. if (usr2 & USR2_BRCD) {
  1091. sport->port.icount.brk++;
  1092. imx_uart_writel(sport, USR2_BRCD, USR2);
  1093. uart_handle_break(&sport->port);
  1094. if (tty_insert_flip_char(port, 0, TTY_BREAK) == 0)
  1095. sport->port.icount.buf_overrun++;
  1096. tty_flip_buffer_push(port);
  1097. } else {
  1098. if (usr1 & USR1_FRAMERR) {
  1099. sport->port.icount.frame++;
  1100. imx_uart_writel(sport, USR1_FRAMERR, USR1);
  1101. } else if (usr1 & USR1_PARITYERR) {
  1102. sport->port.icount.parity++;
  1103. imx_uart_writel(sport, USR1_PARITYERR, USR1);
  1104. }
  1105. }
  1106. if (usr2 & USR2_ORE) {
  1107. sport->port.icount.overrun++;
  1108. imx_uart_writel(sport, USR2_ORE, USR2);
  1109. }
  1110. sport->idle_counter = 0;
  1111. }
  1112. #define TXTL_DEFAULT 8
  1113. #define RXTL_DEFAULT 8 /* 8 characters or aging timer */
  1114. #define RXTL_CONSOLE_DEFAULT 1
  1115. #define TXTL_DMA 8 /* DMA burst setting */
  1116. #define RXTL_DMA 9 /* DMA burst setting */
  1117. static void imx_uart_setup_ufcr(struct imx_port *sport,
  1118. unsigned char txwl, unsigned char rxwl)
  1119. {
  1120. unsigned int val;
  1121. /* set receiver / transmitter trigger level */
  1122. val = imx_uart_readl(sport, UFCR) & (UFCR_RFDIV | UFCR_DCEDTE);
  1123. val |= txwl << UFCR_TXTL_SHF | rxwl;
  1124. imx_uart_writel(sport, val, UFCR);
  1125. }
  1126. static void imx_uart_dma_exit(struct imx_port *sport)
  1127. {
  1128. if (sport->dma_chan_rx) {
  1129. dmaengine_terminate_sync(sport->dma_chan_rx);
  1130. dma_release_channel(sport->dma_chan_rx);
  1131. sport->dma_chan_rx = NULL;
  1132. sport->rx_cookie = -EINVAL;
  1133. kfree(sport->rx_buf);
  1134. sport->rx_buf = NULL;
  1135. }
  1136. if (sport->dma_chan_tx) {
  1137. dmaengine_terminate_sync(sport->dma_chan_tx);
  1138. dma_release_channel(sport->dma_chan_tx);
  1139. sport->dma_chan_tx = NULL;
  1140. }
  1141. }
  1142. static int imx_uart_dma_init(struct imx_port *sport)
  1143. {
  1144. struct dma_slave_config slave_config = {};
  1145. struct device *dev = sport->port.dev;
  1146. struct dma_chan *chan;
  1147. int ret;
  1148. /* Prepare for RX : */
  1149. chan = dma_request_chan(dev, "rx");
  1150. if (IS_ERR(chan)) {
  1151. dev_dbg(dev, "cannot get the DMA channel.\n");
  1152. sport->dma_chan_rx = NULL;
  1153. ret = PTR_ERR(chan);
  1154. goto err;
  1155. }
  1156. sport->dma_chan_rx = chan;
  1157. slave_config.direction = DMA_DEV_TO_MEM;
  1158. slave_config.src_addr = sport->port.mapbase + URXD0;
  1159. slave_config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1160. /* one byte less than the watermark level to enable the aging timer */
  1161. slave_config.src_maxburst = RXTL_DMA - 1;
  1162. ret = dmaengine_slave_config(sport->dma_chan_rx, &slave_config);
  1163. if (ret) {
  1164. dev_err(dev, "error in RX dma configuration.\n");
  1165. goto err;
  1166. }
  1167. sport->rx_buf_size = sport->rx_period_length * sport->rx_periods;
  1168. sport->rx_buf = kzalloc(sport->rx_buf_size, GFP_KERNEL);
  1169. if (!sport->rx_buf) {
  1170. ret = -ENOMEM;
  1171. goto err;
  1172. }
  1173. sport->rx_ring.buf = sport->rx_buf;
  1174. /* Prepare for TX : */
  1175. chan = dma_request_chan(dev, "tx");
  1176. if (IS_ERR(chan)) {
  1177. dev_err(dev, "cannot get the TX DMA channel!\n");
  1178. sport->dma_chan_tx = NULL;
  1179. ret = PTR_ERR(chan);
  1180. goto err;
  1181. }
  1182. sport->dma_chan_tx = chan;
  1183. slave_config.direction = DMA_MEM_TO_DEV;
  1184. slave_config.dst_addr = sport->port.mapbase + URTX0;
  1185. slave_config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1186. slave_config.dst_maxburst = TXTL_DMA;
  1187. ret = dmaengine_slave_config(sport->dma_chan_tx, &slave_config);
  1188. if (ret) {
  1189. dev_err(dev, "error in TX dma configuration.");
  1190. goto err;
  1191. }
  1192. return 0;
  1193. err:
  1194. imx_uart_dma_exit(sport);
  1195. return ret;
  1196. }
  1197. static void imx_uart_enable_dma(struct imx_port *sport)
  1198. {
  1199. u32 ucr1;
  1200. imx_uart_setup_ufcr(sport, TXTL_DMA, RXTL_DMA);
  1201. /* set UCR1 */
  1202. ucr1 = imx_uart_readl(sport, UCR1);
  1203. ucr1 |= UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN;
  1204. imx_uart_writel(sport, ucr1, UCR1);
  1205. sport->dma_is_enabled = 1;
  1206. }
  1207. static void imx_uart_disable_dma(struct imx_port *sport)
  1208. {
  1209. u32 ucr1;
  1210. /* clear UCR1 */
  1211. ucr1 = imx_uart_readl(sport, UCR1);
  1212. ucr1 &= ~(UCR1_RXDMAEN | UCR1_TXDMAEN | UCR1_ATDMAEN);
  1213. imx_uart_writel(sport, ucr1, UCR1);
  1214. imx_uart_setup_ufcr(sport, TXTL_DEFAULT, sport->rxtl);
  1215. sport->dma_is_enabled = 0;
  1216. }
  1217. /* half the RX buffer size */
  1218. #define CTSTL 16
  1219. static int imx_uart_startup(struct uart_port *port)
  1220. {
  1221. struct imx_port *sport = to_imx_port(port);
  1222. int retval;
  1223. unsigned long flags;
  1224. int dma_is_inited = 0;
  1225. u32 ucr1, ucr2, ucr3, ucr4;
  1226. retval = clk_prepare_enable(sport->clk_per);
  1227. if (retval)
  1228. return retval;
  1229. retval = clk_prepare_enable(sport->clk_ipg);
  1230. if (retval) {
  1231. clk_disable_unprepare(sport->clk_per);
  1232. return retval;
  1233. }
  1234. if (uart_console(&sport->port))
  1235. sport->rxtl = RXTL_CONSOLE_DEFAULT;
  1236. else
  1237. sport->rxtl = RXTL_DEFAULT;
  1238. imx_uart_setup_ufcr(sport, TXTL_DEFAULT, sport->rxtl);
  1239. /* disable the DREN bit (Data Ready interrupt enable) before
  1240. * requesting IRQs
  1241. */
  1242. ucr4 = imx_uart_readl(sport, UCR4);
  1243. /* set the trigger level for CTS */
  1244. ucr4 &= ~(UCR4_CTSTL_MASK << UCR4_CTSTL_SHF);
  1245. ucr4 |= CTSTL << UCR4_CTSTL_SHF;
  1246. imx_uart_writel(sport, ucr4 & ~UCR4_DREN, UCR4);
  1247. /* Can we enable the DMA support? */
  1248. if (!uart_console(port) && imx_uart_dma_init(sport) == 0) {
  1249. lockdep_set_subclass(&port->lock, 1);
  1250. dma_is_inited = 1;
  1251. }
  1252. uart_port_lock_irqsave(&sport->port, &flags);
  1253. /* Reset fifo's and state machines */
  1254. imx_uart_soft_reset(sport);
  1255. /*
  1256. * Finally, clear and enable interrupts
  1257. */
  1258. imx_uart_writel(sport, USR1_RTSD | USR1_DTRD, USR1);
  1259. imx_uart_writel(sport, USR2_ORE, USR2);
  1260. ucr1 = imx_uart_readl(sport, UCR1) & ~UCR1_RRDYEN;
  1261. ucr1 |= UCR1_UARTEN;
  1262. if (sport->have_rtscts)
  1263. ucr1 |= UCR1_RTSDEN;
  1264. imx_uart_writel(sport, ucr1, UCR1);
  1265. ucr4 = imx_uart_readl(sport, UCR4) & ~(UCR4_OREN | UCR4_INVR);
  1266. if (!dma_is_inited)
  1267. ucr4 |= UCR4_OREN;
  1268. if (sport->inverted_rx)
  1269. ucr4 |= UCR4_INVR;
  1270. imx_uart_writel(sport, ucr4, UCR4);
  1271. ucr3 = imx_uart_readl(sport, UCR3) & ~UCR3_INVT;
  1272. /*
  1273. * configure tx polarity before enabling tx
  1274. */
  1275. if (sport->inverted_tx)
  1276. ucr3 |= UCR3_INVT;
  1277. if (!imx_uart_is_imx1(sport)) {
  1278. ucr3 |= UCR3_DTRDEN | UCR3_RI | UCR3_DCD;
  1279. if (sport->dte_mode)
  1280. /* disable broken interrupts */
  1281. ucr3 &= ~(UCR3_RI | UCR3_DCD);
  1282. }
  1283. imx_uart_writel(sport, ucr3, UCR3);
  1284. ucr2 = imx_uart_readl(sport, UCR2) & ~UCR2_ATEN;
  1285. ucr2 |= (UCR2_RXEN | UCR2_TXEN);
  1286. if (!sport->have_rtscts)
  1287. ucr2 |= UCR2_IRTS;
  1288. /*
  1289. * make sure the edge sensitive RTS-irq is disabled,
  1290. * we're using RTSD instead.
  1291. */
  1292. if (!imx_uart_is_imx1(sport))
  1293. ucr2 &= ~UCR2_RTSEN;
  1294. imx_uart_writel(sport, ucr2, UCR2);
  1295. /*
  1296. * Enable modem status interrupts
  1297. */
  1298. imx_uart_enable_ms(&sport->port);
  1299. if (dma_is_inited) {
  1300. imx_uart_enable_dma(sport);
  1301. imx_uart_start_rx_dma(sport);
  1302. } else {
  1303. ucr1 = imx_uart_readl(sport, UCR1);
  1304. ucr1 |= UCR1_RRDYEN;
  1305. imx_uart_writel(sport, ucr1, UCR1);
  1306. ucr2 = imx_uart_readl(sport, UCR2);
  1307. ucr2 |= UCR2_ATEN;
  1308. imx_uart_writel(sport, ucr2, UCR2);
  1309. }
  1310. imx_uart_disable_loopback_rs485(sport);
  1311. uart_port_unlock_irqrestore(&sport->port, flags);
  1312. return 0;
  1313. }
  1314. static void imx_uart_shutdown(struct uart_port *port)
  1315. {
  1316. struct imx_port *sport = to_imx_port(port);
  1317. unsigned long flags;
  1318. u32 ucr1, ucr2, ucr4, uts;
  1319. int loops;
  1320. if (sport->dma_is_enabled) {
  1321. dmaengine_terminate_sync(sport->dma_chan_tx);
  1322. if (sport->dma_is_txing) {
  1323. dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
  1324. sport->dma_tx_nents, DMA_TO_DEVICE);
  1325. sport->dma_is_txing = 0;
  1326. }
  1327. dmaengine_terminate_sync(sport->dma_chan_rx);
  1328. if (sport->dma_is_rxing) {
  1329. dma_unmap_sg(sport->port.dev, &sport->rx_sgl,
  1330. 1, DMA_FROM_DEVICE);
  1331. sport->dma_is_rxing = 0;
  1332. }
  1333. uart_port_lock_irqsave(&sport->port, &flags);
  1334. imx_uart_stop_tx(port);
  1335. imx_uart_stop_rx(port);
  1336. imx_uart_disable_dma(sport);
  1337. uart_port_unlock_irqrestore(&sport->port, flags);
  1338. imx_uart_dma_exit(sport);
  1339. }
  1340. mctrl_gpio_disable_ms_sync(sport->gpios);
  1341. uart_port_lock_irqsave(&sport->port, &flags);
  1342. ucr2 = imx_uart_readl(sport, UCR2);
  1343. ucr2 &= ~(UCR2_TXEN | UCR2_ATEN);
  1344. imx_uart_writel(sport, ucr2, UCR2);
  1345. uart_port_unlock_irqrestore(&sport->port, flags);
  1346. /*
  1347. * Stop our timer.
  1348. */
  1349. del_timer_sync(&sport->timer);
  1350. /*
  1351. * Disable all interrupts, port and break condition.
  1352. */
  1353. uart_port_lock_irqsave(&sport->port, &flags);
  1354. ucr1 = imx_uart_readl(sport, UCR1);
  1355. ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN | UCR1_RXDMAEN |
  1356. UCR1_ATDMAEN | UCR1_SNDBRK);
  1357. /* See SER_RS485_ENABLED/UTS_LOOP comment in imx_uart_probe() */
  1358. if (port->rs485.flags & SER_RS485_ENABLED &&
  1359. port->rs485.flags & SER_RS485_RTS_ON_SEND &&
  1360. sport->have_rtscts && !sport->have_rtsgpio) {
  1361. uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
  1362. uts |= UTS_LOOP;
  1363. imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
  1364. ucr1 |= UCR1_UARTEN;
  1365. } else {
  1366. ucr1 &= ~UCR1_UARTEN;
  1367. }
  1368. imx_uart_writel(sport, ucr1, UCR1);
  1369. ucr4 = imx_uart_readl(sport, UCR4);
  1370. ucr4 &= ~UCR4_TCEN;
  1371. imx_uart_writel(sport, ucr4, UCR4);
  1372. /*
  1373. * We have to ensure the tx state machine ends up in OFF. This
  1374. * is especially important for rs485 where we must not leave
  1375. * the RTS signal high, blocking the bus indefinitely.
  1376. *
  1377. * All interrupts are now disabled, so imx_uart_stop_tx() will
  1378. * no longer be called from imx_uart_transmit_buffer(). It may
  1379. * still be called via the hrtimers, and if those are in play,
  1380. * we have to honour the delays.
  1381. */
  1382. if (sport->tx_state == WAIT_AFTER_RTS || sport->tx_state == SEND)
  1383. imx_uart_stop_tx(port);
  1384. /*
  1385. * In many cases (rs232 mode, or if tx_state was
  1386. * WAIT_AFTER_RTS, or if tx_state was SEND and there is no
  1387. * delay_rts_after_send), this will have moved directly to
  1388. * OFF. In rs485 mode, tx_state might already have been
  1389. * WAIT_AFTER_SEND and the hrtimer thus already started, or
  1390. * the above imx_uart_stop_tx() call could have started it. In
  1391. * those cases, we have to wait for the hrtimer to fire and
  1392. * complete the transition to OFF.
  1393. */
  1394. loops = port->rs485.flags & SER_RS485_ENABLED ?
  1395. port->rs485.delay_rts_after_send : 0;
  1396. while (sport->tx_state != OFF && loops--) {
  1397. uart_port_unlock_irqrestore(&sport->port, flags);
  1398. msleep(1);
  1399. uart_port_lock_irqsave(&sport->port, &flags);
  1400. }
  1401. if (sport->tx_state != OFF) {
  1402. dev_warn(sport->port.dev, "unexpected tx_state %d\n",
  1403. sport->tx_state);
  1404. /*
  1405. * This machine may be busted, but ensure the RTS
  1406. * signal is inactive in order not to block other
  1407. * devices.
  1408. */
  1409. if (port->rs485.flags & SER_RS485_ENABLED) {
  1410. ucr2 = imx_uart_readl(sport, UCR2);
  1411. if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
  1412. imx_uart_rts_active(sport, &ucr2);
  1413. else
  1414. imx_uart_rts_inactive(sport, &ucr2);
  1415. imx_uart_writel(sport, ucr2, UCR2);
  1416. }
  1417. sport->tx_state = OFF;
  1418. }
  1419. uart_port_unlock_irqrestore(&sport->port, flags);
  1420. clk_disable_unprepare(sport->clk_per);
  1421. clk_disable_unprepare(sport->clk_ipg);
  1422. }
  1423. /* called with port.lock taken and irqs off */
  1424. static void imx_uart_flush_buffer(struct uart_port *port)
  1425. {
  1426. struct imx_port *sport = to_imx_port(port);
  1427. struct scatterlist *sgl = &sport->tx_sgl[0];
  1428. if (!sport->dma_chan_tx)
  1429. return;
  1430. sport->tx_bytes = 0;
  1431. dmaengine_terminate_all(sport->dma_chan_tx);
  1432. if (sport->dma_is_txing) {
  1433. u32 ucr1;
  1434. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents,
  1435. DMA_TO_DEVICE);
  1436. ucr1 = imx_uart_readl(sport, UCR1);
  1437. ucr1 &= ~UCR1_TXDMAEN;
  1438. imx_uart_writel(sport, ucr1, UCR1);
  1439. sport->dma_is_txing = 0;
  1440. }
  1441. imx_uart_soft_reset(sport);
  1442. }
  1443. static void
  1444. imx_uart_set_termios(struct uart_port *port, struct ktermios *termios,
  1445. const struct ktermios *old)
  1446. {
  1447. struct imx_port *sport = to_imx_port(port);
  1448. unsigned long flags;
  1449. u32 ucr2, old_ucr2, ufcr;
  1450. unsigned int baud, quot;
  1451. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1452. unsigned long div;
  1453. unsigned long num, denom, old_ubir, old_ubmr;
  1454. uint64_t tdiv64;
  1455. /*
  1456. * We only support CS7 and CS8.
  1457. */
  1458. while ((termios->c_cflag & CSIZE) != CS7 &&
  1459. (termios->c_cflag & CSIZE) != CS8) {
  1460. termios->c_cflag &= ~CSIZE;
  1461. termios->c_cflag |= old_csize;
  1462. old_csize = CS8;
  1463. }
  1464. del_timer_sync(&sport->timer);
  1465. /*
  1466. * Ask the core to calculate the divisor for us.
  1467. */
  1468. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1469. quot = uart_get_divisor(port, baud);
  1470. uart_port_lock_irqsave(&sport->port, &flags);
  1471. /*
  1472. * Read current UCR2 and save it for future use, then clear all the bits
  1473. * except those we will or may need to preserve.
  1474. */
  1475. old_ucr2 = imx_uart_readl(sport, UCR2);
  1476. ucr2 = old_ucr2 & (UCR2_TXEN | UCR2_RXEN | UCR2_ATEN | UCR2_CTS);
  1477. ucr2 |= UCR2_SRST | UCR2_IRTS;
  1478. if ((termios->c_cflag & CSIZE) == CS8)
  1479. ucr2 |= UCR2_WS;
  1480. if (!sport->have_rtscts)
  1481. termios->c_cflag &= ~CRTSCTS;
  1482. if (port->rs485.flags & SER_RS485_ENABLED) {
  1483. /*
  1484. * RTS is mandatory for rs485 operation, so keep
  1485. * it under manual control and keep transmitter
  1486. * disabled.
  1487. */
  1488. if (port->rs485.flags & SER_RS485_RTS_AFTER_SEND)
  1489. imx_uart_rts_active(sport, &ucr2);
  1490. else
  1491. imx_uart_rts_inactive(sport, &ucr2);
  1492. } else if (termios->c_cflag & CRTSCTS) {
  1493. /*
  1494. * Only let receiver control RTS output if we were not requested
  1495. * to have RTS inactive (which then should take precedence).
  1496. */
  1497. if (ucr2 & UCR2_CTS)
  1498. ucr2 |= UCR2_CTSC;
  1499. }
  1500. if (termios->c_cflag & CRTSCTS)
  1501. ucr2 &= ~UCR2_IRTS;
  1502. if (termios->c_cflag & CSTOPB)
  1503. ucr2 |= UCR2_STPB;
  1504. if (termios->c_cflag & PARENB) {
  1505. ucr2 |= UCR2_PREN;
  1506. if (termios->c_cflag & PARODD)
  1507. ucr2 |= UCR2_PROE;
  1508. }
  1509. sport->port.read_status_mask = 0;
  1510. if (termios->c_iflag & INPCK)
  1511. sport->port.read_status_mask |= (URXD_FRMERR | URXD_PRERR);
  1512. if (termios->c_iflag & (BRKINT | PARMRK))
  1513. sport->port.read_status_mask |= URXD_BRK;
  1514. /*
  1515. * Characters to ignore
  1516. */
  1517. sport->port.ignore_status_mask = 0;
  1518. if (termios->c_iflag & IGNPAR)
  1519. sport->port.ignore_status_mask |= URXD_PRERR | URXD_FRMERR;
  1520. if (termios->c_iflag & IGNBRK) {
  1521. sport->port.ignore_status_mask |= URXD_BRK;
  1522. /*
  1523. * If we're ignoring parity and break indicators,
  1524. * ignore overruns too (for real raw support).
  1525. */
  1526. if (termios->c_iflag & IGNPAR)
  1527. sport->port.ignore_status_mask |= URXD_OVRRUN;
  1528. }
  1529. if ((termios->c_cflag & CREAD) == 0)
  1530. sport->port.ignore_status_mask |= URXD_DUMMY_READ;
  1531. /*
  1532. * Update the per-port timeout.
  1533. */
  1534. uart_update_timeout(port, termios->c_cflag, baud);
  1535. /* custom-baudrate handling */
  1536. div = sport->port.uartclk / (baud * 16);
  1537. if (baud == 38400 && quot != div)
  1538. baud = sport->port.uartclk / (quot * 16);
  1539. div = sport->port.uartclk / (baud * 16);
  1540. if (div > 7)
  1541. div = 7;
  1542. if (!div)
  1543. div = 1;
  1544. rational_best_approximation(16 * div * baud, sport->port.uartclk,
  1545. 1 << 16, 1 << 16, &num, &denom);
  1546. tdiv64 = sport->port.uartclk;
  1547. tdiv64 *= num;
  1548. do_div(tdiv64, denom * 16 * div);
  1549. tty_termios_encode_baud_rate(termios,
  1550. (speed_t)tdiv64, (speed_t)tdiv64);
  1551. num -= 1;
  1552. denom -= 1;
  1553. ufcr = imx_uart_readl(sport, UFCR);
  1554. ufcr = (ufcr & (~UFCR_RFDIV)) | UFCR_RFDIV_REG(div);
  1555. imx_uart_writel(sport, ufcr, UFCR);
  1556. /*
  1557. * Two registers below should always be written both and in this
  1558. * particular order. One consequence is that we need to check if any of
  1559. * them changes and then update both. We do need the check for change
  1560. * as even writing the same values seem to "restart"
  1561. * transmission/receiving logic in the hardware, that leads to data
  1562. * breakage even when rate doesn't in fact change. E.g., user switches
  1563. * RTS/CTS handshake and suddenly gets broken bytes.
  1564. */
  1565. old_ubir = imx_uart_readl(sport, UBIR);
  1566. old_ubmr = imx_uart_readl(sport, UBMR);
  1567. if (old_ubir != num || old_ubmr != denom) {
  1568. imx_uart_writel(sport, num, UBIR);
  1569. imx_uart_writel(sport, denom, UBMR);
  1570. }
  1571. if (!imx_uart_is_imx1(sport))
  1572. imx_uart_writel(sport, sport->port.uartclk / div / 1000,
  1573. IMX21_ONEMS);
  1574. imx_uart_writel(sport, ucr2, UCR2);
  1575. if (UART_ENABLE_MS(&sport->port, termios->c_cflag))
  1576. imx_uart_enable_ms(&sport->port);
  1577. uart_port_unlock_irqrestore(&sport->port, flags);
  1578. }
  1579. static const char *imx_uart_type(struct uart_port *port)
  1580. {
  1581. return port->type == PORT_IMX ? "IMX" : NULL;
  1582. }
  1583. /*
  1584. * Configure/autoconfigure the port.
  1585. */
  1586. static void imx_uart_config_port(struct uart_port *port, int flags)
  1587. {
  1588. if (flags & UART_CONFIG_TYPE)
  1589. port->type = PORT_IMX;
  1590. }
  1591. /*
  1592. * Verify the new serial_struct (for TIOCSSERIAL).
  1593. * The only change we allow are to the flags and type, and
  1594. * even then only between PORT_IMX and PORT_UNKNOWN
  1595. */
  1596. static int
  1597. imx_uart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1598. {
  1599. int ret = 0;
  1600. if (ser->type != PORT_UNKNOWN && ser->type != PORT_IMX)
  1601. ret = -EINVAL;
  1602. if (port->irq != ser->irq)
  1603. ret = -EINVAL;
  1604. if (ser->io_type != UPIO_MEM)
  1605. ret = -EINVAL;
  1606. if (port->uartclk / 16 != ser->baud_base)
  1607. ret = -EINVAL;
  1608. if (port->mapbase != (unsigned long)ser->iomem_base)
  1609. ret = -EINVAL;
  1610. if (port->iobase != ser->port)
  1611. ret = -EINVAL;
  1612. if (ser->hub6 != 0)
  1613. ret = -EINVAL;
  1614. return ret;
  1615. }
  1616. #if defined(CONFIG_CONSOLE_POLL)
  1617. static int imx_uart_poll_init(struct uart_port *port)
  1618. {
  1619. struct imx_port *sport = to_imx_port(port);
  1620. unsigned long flags;
  1621. u32 ucr1, ucr2;
  1622. int retval;
  1623. retval = clk_prepare_enable(sport->clk_ipg);
  1624. if (retval)
  1625. return retval;
  1626. retval = clk_prepare_enable(sport->clk_per);
  1627. if (retval)
  1628. clk_disable_unprepare(sport->clk_ipg);
  1629. imx_uart_setup_ufcr(sport, TXTL_DEFAULT, sport->rxtl);
  1630. uart_port_lock_irqsave(&sport->port, &flags);
  1631. /*
  1632. * Be careful about the order of enabling bits here. First enable the
  1633. * receiver (UARTEN + RXEN) and only then the corresponding irqs.
  1634. * This prevents that a character that already sits in the RX fifo is
  1635. * triggering an irq but the try to fetch it from there results in an
  1636. * exception because UARTEN or RXEN is still off.
  1637. */
  1638. ucr1 = imx_uart_readl(sport, UCR1);
  1639. ucr2 = imx_uart_readl(sport, UCR2);
  1640. if (imx_uart_is_imx1(sport))
  1641. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1642. ucr1 |= UCR1_UARTEN;
  1643. ucr1 &= ~(UCR1_TRDYEN | UCR1_RTSDEN | UCR1_RRDYEN);
  1644. ucr2 |= UCR2_RXEN | UCR2_TXEN;
  1645. ucr2 &= ~UCR2_ATEN;
  1646. imx_uart_writel(sport, ucr1, UCR1);
  1647. imx_uart_writel(sport, ucr2, UCR2);
  1648. /* now enable irqs */
  1649. imx_uart_writel(sport, ucr1 | UCR1_RRDYEN, UCR1);
  1650. imx_uart_writel(sport, ucr2 | UCR2_ATEN, UCR2);
  1651. uart_port_unlock_irqrestore(&sport->port, flags);
  1652. return 0;
  1653. }
  1654. static int imx_uart_poll_get_char(struct uart_port *port)
  1655. {
  1656. struct imx_port *sport = to_imx_port(port);
  1657. if (!(imx_uart_readl(sport, USR2) & USR2_RDR))
  1658. return NO_POLL_CHAR;
  1659. return imx_uart_readl(sport, URXD0) & URXD_RX_DATA;
  1660. }
  1661. static void imx_uart_poll_put_char(struct uart_port *port, unsigned char c)
  1662. {
  1663. struct imx_port *sport = to_imx_port(port);
  1664. unsigned int status;
  1665. /* drain */
  1666. do {
  1667. status = imx_uart_readl(sport, USR1);
  1668. } while (~status & USR1_TRDY);
  1669. /* write */
  1670. imx_uart_writel(sport, c, URTX0);
  1671. /* flush */
  1672. do {
  1673. status = imx_uart_readl(sport, USR2);
  1674. } while (~status & USR2_TXDC);
  1675. }
  1676. #endif
  1677. /* called with port.lock taken and irqs off or from .probe without locking */
  1678. static int imx_uart_rs485_config(struct uart_port *port, struct ktermios *termios,
  1679. struct serial_rs485 *rs485conf)
  1680. {
  1681. struct imx_port *sport = to_imx_port(port);
  1682. u32 ucr2, ufcr;
  1683. if (rs485conf->flags & SER_RS485_ENABLED) {
  1684. /* Enable receiver if low-active RTS signal is requested */
  1685. if (sport->have_rtscts && !sport->have_rtsgpio &&
  1686. !(rs485conf->flags & SER_RS485_RTS_ON_SEND))
  1687. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  1688. /* disable transmitter */
  1689. ucr2 = imx_uart_readl(sport, UCR2);
  1690. if (rs485conf->flags & SER_RS485_RTS_AFTER_SEND)
  1691. imx_uart_rts_active(sport, &ucr2);
  1692. else
  1693. imx_uart_rts_inactive(sport, &ucr2);
  1694. imx_uart_writel(sport, ucr2, UCR2);
  1695. }
  1696. /* Make sure Rx is enabled in case Tx is active with Rx disabled */
  1697. if (!(rs485conf->flags & SER_RS485_ENABLED) ||
  1698. rs485conf->flags & SER_RS485_RX_DURING_TX) {
  1699. /* If the receiver trigger is 0, set it to a default value */
  1700. ufcr = imx_uart_readl(sport, UFCR);
  1701. if ((ufcr & UFCR_RXTL_MASK) == 0)
  1702. imx_uart_setup_ufcr(sport, TXTL_DEFAULT, sport->rxtl);
  1703. imx_uart_start_rx(port);
  1704. }
  1705. return 0;
  1706. }
  1707. static const struct uart_ops imx_uart_pops = {
  1708. .tx_empty = imx_uart_tx_empty,
  1709. .set_mctrl = imx_uart_set_mctrl,
  1710. .get_mctrl = imx_uart_get_mctrl,
  1711. .stop_tx = imx_uart_stop_tx,
  1712. .start_tx = imx_uart_start_tx,
  1713. .stop_rx = imx_uart_stop_rx,
  1714. .enable_ms = imx_uart_enable_ms,
  1715. .break_ctl = imx_uart_break_ctl,
  1716. .startup = imx_uart_startup,
  1717. .shutdown = imx_uart_shutdown,
  1718. .flush_buffer = imx_uart_flush_buffer,
  1719. .set_termios = imx_uart_set_termios,
  1720. .type = imx_uart_type,
  1721. .config_port = imx_uart_config_port,
  1722. .verify_port = imx_uart_verify_port,
  1723. #if defined(CONFIG_CONSOLE_POLL)
  1724. .poll_init = imx_uart_poll_init,
  1725. .poll_get_char = imx_uart_poll_get_char,
  1726. .poll_put_char = imx_uart_poll_put_char,
  1727. #endif
  1728. };
  1729. static struct imx_port *imx_uart_ports[UART_NR];
  1730. #if IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE)
  1731. static void imx_uart_console_putchar(struct uart_port *port, unsigned char ch)
  1732. {
  1733. struct imx_port *sport = to_imx_port(port);
  1734. while (imx_uart_readl(sport, imx_uart_uts_reg(sport)) & UTS_TXFULL)
  1735. barrier();
  1736. imx_uart_writel(sport, ch, URTX0);
  1737. }
  1738. /*
  1739. * Interrupts are disabled on entering
  1740. */
  1741. static void
  1742. imx_uart_console_write(struct console *co, const char *s, unsigned int count)
  1743. {
  1744. struct imx_port *sport = imx_uart_ports[co->index];
  1745. struct imx_port_ucrs old_ucr;
  1746. unsigned long flags;
  1747. unsigned int ucr1, usr2;
  1748. int locked = 1;
  1749. if (sport->port.sysrq)
  1750. locked = 0;
  1751. else if (oops_in_progress)
  1752. locked = uart_port_trylock_irqsave(&sport->port, &flags);
  1753. else
  1754. uart_port_lock_irqsave(&sport->port, &flags);
  1755. /*
  1756. * First, save UCR1/2/3 and then disable interrupts
  1757. */
  1758. imx_uart_ucrs_save(sport, &old_ucr);
  1759. ucr1 = old_ucr.ucr1;
  1760. if (imx_uart_is_imx1(sport))
  1761. ucr1 |= IMX1_UCR1_UARTCLKEN;
  1762. ucr1 |= UCR1_UARTEN;
  1763. ucr1 &= ~(UCR1_TRDYEN | UCR1_RRDYEN | UCR1_RTSDEN);
  1764. imx_uart_writel(sport, ucr1, UCR1);
  1765. imx_uart_writel(sport, old_ucr.ucr2 | UCR2_TXEN, UCR2);
  1766. uart_console_write(&sport->port, s, count, imx_uart_console_putchar);
  1767. /*
  1768. * Finally, wait for transmitter to become empty
  1769. * and restore UCR1/2/3
  1770. */
  1771. read_poll_timeout_atomic(imx_uart_readl, usr2, usr2 & USR2_TXDC,
  1772. 0, USEC_PER_SEC, false, sport, USR2);
  1773. imx_uart_ucrs_restore(sport, &old_ucr);
  1774. if (locked)
  1775. uart_port_unlock_irqrestore(&sport->port, flags);
  1776. }
  1777. /*
  1778. * If the port was already initialised (eg, by a boot loader),
  1779. * try to determine the current setup.
  1780. */
  1781. static void
  1782. imx_uart_console_get_options(struct imx_port *sport, int *baud,
  1783. int *parity, int *bits)
  1784. {
  1785. if (imx_uart_readl(sport, UCR1) & UCR1_UARTEN) {
  1786. /* ok, the port was enabled */
  1787. unsigned int ucr2, ubir, ubmr, uartclk;
  1788. unsigned int baud_raw;
  1789. unsigned int ucfr_rfdiv;
  1790. ucr2 = imx_uart_readl(sport, UCR2);
  1791. *parity = 'n';
  1792. if (ucr2 & UCR2_PREN) {
  1793. if (ucr2 & UCR2_PROE)
  1794. *parity = 'o';
  1795. else
  1796. *parity = 'e';
  1797. }
  1798. if (ucr2 & UCR2_WS)
  1799. *bits = 8;
  1800. else
  1801. *bits = 7;
  1802. ubir = imx_uart_readl(sport, UBIR) & 0xffff;
  1803. ubmr = imx_uart_readl(sport, UBMR) & 0xffff;
  1804. ucfr_rfdiv = (imx_uart_readl(sport, UFCR) & UFCR_RFDIV) >> 7;
  1805. if (ucfr_rfdiv == 6)
  1806. ucfr_rfdiv = 7;
  1807. else
  1808. ucfr_rfdiv = 6 - ucfr_rfdiv;
  1809. uartclk = clk_get_rate(sport->clk_per);
  1810. uartclk /= ucfr_rfdiv;
  1811. { /*
  1812. * The next code provides exact computation of
  1813. * baud_raw = round(((uartclk/16) * (ubir + 1)) / (ubmr + 1))
  1814. * without need of float support or long long division,
  1815. * which would be required to prevent 32bit arithmetic overflow
  1816. */
  1817. unsigned int mul = ubir + 1;
  1818. unsigned int div = 16 * (ubmr + 1);
  1819. unsigned int rem = uartclk % div;
  1820. baud_raw = (uartclk / div) * mul;
  1821. baud_raw += (rem * mul + div / 2) / div;
  1822. *baud = (baud_raw + 50) / 100 * 100;
  1823. }
  1824. if (*baud != baud_raw)
  1825. dev_info(sport->port.dev, "Console IMX rounded baud rate from %d to %d\n",
  1826. baud_raw, *baud);
  1827. }
  1828. }
  1829. static int
  1830. imx_uart_console_setup(struct console *co, char *options)
  1831. {
  1832. struct imx_port *sport;
  1833. int baud = 9600;
  1834. int bits = 8;
  1835. int parity = 'n';
  1836. int flow = 'n';
  1837. int retval;
  1838. /*
  1839. * Check whether an invalid uart number has been specified, and
  1840. * if so, search for the first available port that does have
  1841. * console support.
  1842. */
  1843. if (co->index == -1 || co->index >= ARRAY_SIZE(imx_uart_ports))
  1844. co->index = 0;
  1845. sport = imx_uart_ports[co->index];
  1846. if (sport == NULL)
  1847. return -ENODEV;
  1848. /* For setting the registers, we only need to enable the ipg clock. */
  1849. retval = clk_prepare_enable(sport->clk_ipg);
  1850. if (retval)
  1851. goto error_console;
  1852. if (options)
  1853. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1854. else
  1855. imx_uart_console_get_options(sport, &baud, &parity, &bits);
  1856. imx_uart_setup_ufcr(sport, TXTL_DEFAULT, sport->rxtl);
  1857. retval = uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1858. if (retval) {
  1859. clk_disable_unprepare(sport->clk_ipg);
  1860. goto error_console;
  1861. }
  1862. retval = clk_prepare_enable(sport->clk_per);
  1863. if (retval)
  1864. clk_disable_unprepare(sport->clk_ipg);
  1865. error_console:
  1866. return retval;
  1867. }
  1868. static int
  1869. imx_uart_console_exit(struct console *co)
  1870. {
  1871. struct imx_port *sport = imx_uart_ports[co->index];
  1872. clk_disable_unprepare(sport->clk_per);
  1873. clk_disable_unprepare(sport->clk_ipg);
  1874. return 0;
  1875. }
  1876. static struct uart_driver imx_uart_uart_driver;
  1877. static struct console imx_uart_console = {
  1878. .name = DEV_NAME,
  1879. .write = imx_uart_console_write,
  1880. .device = uart_console_device,
  1881. .setup = imx_uart_console_setup,
  1882. .exit = imx_uart_console_exit,
  1883. .flags = CON_PRINTBUFFER,
  1884. .index = -1,
  1885. .data = &imx_uart_uart_driver,
  1886. };
  1887. #define IMX_CONSOLE &imx_uart_console
  1888. #else
  1889. #define IMX_CONSOLE NULL
  1890. #endif
  1891. static struct uart_driver imx_uart_uart_driver = {
  1892. .owner = THIS_MODULE,
  1893. .driver_name = DRIVER_NAME,
  1894. .dev_name = DEV_NAME,
  1895. .major = SERIAL_IMX_MAJOR,
  1896. .minor = MINOR_START,
  1897. .nr = ARRAY_SIZE(imx_uart_ports),
  1898. .cons = IMX_CONSOLE,
  1899. };
  1900. static enum hrtimer_restart imx_trigger_start_tx(struct hrtimer *t)
  1901. {
  1902. struct imx_port *sport = container_of(t, struct imx_port, trigger_start_tx);
  1903. unsigned long flags;
  1904. uart_port_lock_irqsave(&sport->port, &flags);
  1905. if (sport->tx_state == WAIT_AFTER_RTS)
  1906. imx_uart_start_tx(&sport->port);
  1907. uart_port_unlock_irqrestore(&sport->port, flags);
  1908. return HRTIMER_NORESTART;
  1909. }
  1910. static enum hrtimer_restart imx_trigger_stop_tx(struct hrtimer *t)
  1911. {
  1912. struct imx_port *sport = container_of(t, struct imx_port, trigger_stop_tx);
  1913. unsigned long flags;
  1914. uart_port_lock_irqsave(&sport->port, &flags);
  1915. if (sport->tx_state == WAIT_AFTER_SEND)
  1916. imx_uart_stop_tx(&sport->port);
  1917. uart_port_unlock_irqrestore(&sport->port, flags);
  1918. return HRTIMER_NORESTART;
  1919. }
  1920. static const struct serial_rs485 imx_rs485_supported = {
  1921. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
  1922. SER_RS485_RX_DURING_TX,
  1923. .delay_rts_before_send = 1,
  1924. .delay_rts_after_send = 1,
  1925. };
  1926. /* Default RX DMA buffer configuration */
  1927. #define RX_DMA_PERIODS 16
  1928. #define RX_DMA_PERIOD_LEN (PAGE_SIZE / 4)
  1929. static int imx_uart_probe(struct platform_device *pdev)
  1930. {
  1931. struct device_node *np = pdev->dev.of_node;
  1932. struct imx_port *sport;
  1933. void __iomem *base;
  1934. u32 dma_buf_conf[2];
  1935. int ret = 0;
  1936. u32 ucr1, ucr2, uts;
  1937. struct resource *res;
  1938. int txirq, rxirq, rtsirq;
  1939. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1940. if (!sport)
  1941. return -ENOMEM;
  1942. sport->devdata = of_device_get_match_data(&pdev->dev);
  1943. ret = of_alias_get_id(np, "serial");
  1944. if (ret < 0) {
  1945. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1946. return ret;
  1947. }
  1948. sport->port.line = ret;
  1949. sport->have_rtscts = of_property_read_bool(np, "uart-has-rtscts") ||
  1950. of_property_read_bool(np, "fsl,uart-has-rtscts"); /* deprecated */
  1951. sport->dte_mode = of_property_read_bool(np, "fsl,dte-mode");
  1952. sport->have_rtsgpio = of_property_present(np, "rts-gpios");
  1953. sport->inverted_tx = of_property_read_bool(np, "fsl,inverted-tx");
  1954. sport->inverted_rx = of_property_read_bool(np, "fsl,inverted-rx");
  1955. if (!of_property_read_u32_array(np, "fsl,dma-info", dma_buf_conf, 2)) {
  1956. sport->rx_period_length = dma_buf_conf[0];
  1957. sport->rx_periods = dma_buf_conf[1];
  1958. } else {
  1959. sport->rx_period_length = RX_DMA_PERIOD_LEN;
  1960. sport->rx_periods = RX_DMA_PERIODS;
  1961. }
  1962. if (sport->port.line >= ARRAY_SIZE(imx_uart_ports)) {
  1963. dev_err(&pdev->dev, "serial%d out of range\n",
  1964. sport->port.line);
  1965. return -EINVAL;
  1966. }
  1967. base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1968. if (IS_ERR(base))
  1969. return PTR_ERR(base);
  1970. rxirq = platform_get_irq(pdev, 0);
  1971. if (rxirq < 0)
  1972. return rxirq;
  1973. txirq = platform_get_irq_optional(pdev, 1);
  1974. rtsirq = platform_get_irq_optional(pdev, 2);
  1975. sport->port.dev = &pdev->dev;
  1976. sport->port.mapbase = res->start;
  1977. sport->port.membase = base;
  1978. sport->port.type = PORT_IMX;
  1979. sport->port.iotype = UPIO_MEM;
  1980. sport->port.irq = rxirq;
  1981. sport->port.fifosize = 32;
  1982. sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_IMX_CONSOLE);
  1983. sport->port.ops = &imx_uart_pops;
  1984. sport->port.rs485_config = imx_uart_rs485_config;
  1985. /* RTS is required to control the RS485 transmitter */
  1986. if (sport->have_rtscts || sport->have_rtsgpio)
  1987. sport->port.rs485_supported = imx_rs485_supported;
  1988. sport->port.flags = UPF_BOOT_AUTOCONF;
  1989. timer_setup(&sport->timer, imx_uart_timeout, 0);
  1990. sport->gpios = mctrl_gpio_init(&sport->port, 0);
  1991. if (IS_ERR(sport->gpios))
  1992. return PTR_ERR(sport->gpios);
  1993. sport->clk_ipg = devm_clk_get(&pdev->dev, "ipg");
  1994. if (IS_ERR(sport->clk_ipg)) {
  1995. ret = PTR_ERR(sport->clk_ipg);
  1996. dev_err(&pdev->dev, "failed to get ipg clk: %d\n", ret);
  1997. return ret;
  1998. }
  1999. sport->clk_per = devm_clk_get(&pdev->dev, "per");
  2000. if (IS_ERR(sport->clk_per)) {
  2001. ret = PTR_ERR(sport->clk_per);
  2002. dev_err(&pdev->dev, "failed to get per clk: %d\n", ret);
  2003. return ret;
  2004. }
  2005. sport->port.uartclk = clk_get_rate(sport->clk_per);
  2006. /* For register access, we only need to enable the ipg clock. */
  2007. ret = clk_prepare_enable(sport->clk_ipg);
  2008. if (ret) {
  2009. dev_err(&pdev->dev, "failed to enable ipg clk: %d\n", ret);
  2010. return ret;
  2011. }
  2012. ret = uart_get_rs485_mode(&sport->port);
  2013. if (ret)
  2014. goto err_clk;
  2015. /*
  2016. * If using the i.MX UART RTS/CTS control then the RTS (CTS_B)
  2017. * signal cannot be set low during transmission in case the
  2018. * receiver is off (limitation of the i.MX UART IP).
  2019. */
  2020. if (sport->port.rs485.flags & SER_RS485_ENABLED &&
  2021. sport->have_rtscts && !sport->have_rtsgpio &&
  2022. (!(sport->port.rs485.flags & SER_RS485_RTS_ON_SEND) &&
  2023. !(sport->port.rs485.flags & SER_RS485_RX_DURING_TX)))
  2024. dev_err(&pdev->dev,
  2025. "low-active RTS not possible when receiver is off, enabling receiver\n");
  2026. /* Disable interrupts before requesting them */
  2027. ucr1 = imx_uart_readl(sport, UCR1);
  2028. ucr1 &= ~(UCR1_ADEN | UCR1_TRDYEN | UCR1_IDEN | UCR1_RRDYEN | UCR1_RTSDEN);
  2029. imx_uart_writel(sport, ucr1, UCR1);
  2030. /* Disable Ageing Timer interrupt */
  2031. ucr2 = imx_uart_readl(sport, UCR2);
  2032. ucr2 &= ~UCR2_ATEN;
  2033. imx_uart_writel(sport, ucr2, UCR2);
  2034. /*
  2035. * In case RS485 is enabled without GPIO RTS control, the UART IP
  2036. * is used to control CTS signal. Keep both the UART and Receiver
  2037. * enabled, otherwise the UART IP pulls CTS signal always HIGH no
  2038. * matter how the UCR2 CTSC and CTS bits are set. To prevent any
  2039. * data from being fed into the RX FIFO, enable loopback mode in
  2040. * UTS register, which disconnects the RX path from external RXD
  2041. * pin and connects it to the Transceiver, which is disabled, so
  2042. * no data can be fed to the RX FIFO that way.
  2043. */
  2044. if (sport->port.rs485.flags & SER_RS485_ENABLED &&
  2045. sport->have_rtscts && !sport->have_rtsgpio) {
  2046. uts = imx_uart_readl(sport, imx_uart_uts_reg(sport));
  2047. uts |= UTS_LOOP;
  2048. imx_uart_writel(sport, uts, imx_uart_uts_reg(sport));
  2049. ucr1 = imx_uart_readl(sport, UCR1);
  2050. ucr1 |= UCR1_UARTEN;
  2051. imx_uart_writel(sport, ucr1, UCR1);
  2052. ucr2 = imx_uart_readl(sport, UCR2);
  2053. ucr2 |= UCR2_RXEN;
  2054. imx_uart_writel(sport, ucr2, UCR2);
  2055. }
  2056. if (!imx_uart_is_imx1(sport) && sport->dte_mode) {
  2057. /*
  2058. * The DCEDTE bit changes the direction of DSR, DCD, DTR and RI
  2059. * and influences if UCR3_RI and UCR3_DCD changes the level of RI
  2060. * and DCD (when they are outputs) or enables the respective
  2061. * irqs. So set this bit early, i.e. before requesting irqs.
  2062. */
  2063. u32 ufcr = imx_uart_readl(sport, UFCR);
  2064. if (!(ufcr & UFCR_DCEDTE))
  2065. imx_uart_writel(sport, ufcr | UFCR_DCEDTE, UFCR);
  2066. /*
  2067. * Disable UCR3_RI and UCR3_DCD irqs. They are also not
  2068. * enabled later because they cannot be cleared
  2069. * (confirmed on i.MX25) which makes them unusable.
  2070. */
  2071. imx_uart_writel(sport,
  2072. IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP | UCR3_DSR,
  2073. UCR3);
  2074. } else {
  2075. u32 ucr3 = UCR3_DSR;
  2076. u32 ufcr = imx_uart_readl(sport, UFCR);
  2077. if (ufcr & UFCR_DCEDTE)
  2078. imx_uart_writel(sport, ufcr & ~UFCR_DCEDTE, UFCR);
  2079. if (!imx_uart_is_imx1(sport))
  2080. ucr3 |= IMX21_UCR3_RXDMUXSEL | UCR3_ADNIMP;
  2081. imx_uart_writel(sport, ucr3, UCR3);
  2082. }
  2083. hrtimer_init(&sport->trigger_start_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2084. hrtimer_init(&sport->trigger_stop_tx, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  2085. sport->trigger_start_tx.function = imx_trigger_start_tx;
  2086. sport->trigger_stop_tx.function = imx_trigger_stop_tx;
  2087. /*
  2088. * Allocate the IRQ(s) i.MX1 has three interrupts whereas later
  2089. * chips only have one interrupt.
  2090. */
  2091. if (txirq > 0) {
  2092. ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_rxint, 0,
  2093. dev_name(&pdev->dev), sport);
  2094. if (ret) {
  2095. dev_err(&pdev->dev, "failed to request rx irq: %d\n",
  2096. ret);
  2097. goto err_clk;
  2098. }
  2099. ret = devm_request_irq(&pdev->dev, txirq, imx_uart_txint, 0,
  2100. dev_name(&pdev->dev), sport);
  2101. if (ret) {
  2102. dev_err(&pdev->dev, "failed to request tx irq: %d\n",
  2103. ret);
  2104. goto err_clk;
  2105. }
  2106. ret = devm_request_irq(&pdev->dev, rtsirq, imx_uart_rtsint, 0,
  2107. dev_name(&pdev->dev), sport);
  2108. if (ret) {
  2109. dev_err(&pdev->dev, "failed to request rts irq: %d\n",
  2110. ret);
  2111. goto err_clk;
  2112. }
  2113. } else {
  2114. ret = devm_request_irq(&pdev->dev, rxirq, imx_uart_int, 0,
  2115. dev_name(&pdev->dev), sport);
  2116. if (ret) {
  2117. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  2118. goto err_clk;
  2119. }
  2120. }
  2121. imx_uart_ports[sport->port.line] = sport;
  2122. platform_set_drvdata(pdev, sport);
  2123. ret = uart_add_one_port(&imx_uart_uart_driver, &sport->port);
  2124. err_clk:
  2125. clk_disable_unprepare(sport->clk_ipg);
  2126. return ret;
  2127. }
  2128. static void imx_uart_remove(struct platform_device *pdev)
  2129. {
  2130. struct imx_port *sport = platform_get_drvdata(pdev);
  2131. uart_remove_one_port(&imx_uart_uart_driver, &sport->port);
  2132. }
  2133. static void imx_uart_restore_context(struct imx_port *sport)
  2134. {
  2135. unsigned long flags;
  2136. uart_port_lock_irqsave(&sport->port, &flags);
  2137. if (!sport->context_saved) {
  2138. uart_port_unlock_irqrestore(&sport->port, flags);
  2139. return;
  2140. }
  2141. imx_uart_writel(sport, sport->saved_reg[4], UFCR);
  2142. imx_uart_writel(sport, sport->saved_reg[5], UESC);
  2143. imx_uart_writel(sport, sport->saved_reg[6], UTIM);
  2144. imx_uart_writel(sport, sport->saved_reg[7], UBIR);
  2145. imx_uart_writel(sport, sport->saved_reg[8], UBMR);
  2146. imx_uart_writel(sport, sport->saved_reg[9], IMX21_UTS);
  2147. imx_uart_writel(sport, sport->saved_reg[0], UCR1);
  2148. imx_uart_writel(sport, sport->saved_reg[1] | UCR2_SRST, UCR2);
  2149. imx_uart_writel(sport, sport->saved_reg[2], UCR3);
  2150. imx_uart_writel(sport, sport->saved_reg[3], UCR4);
  2151. sport->context_saved = false;
  2152. uart_port_unlock_irqrestore(&sport->port, flags);
  2153. }
  2154. static void imx_uart_save_context(struct imx_port *sport)
  2155. {
  2156. unsigned long flags;
  2157. /* Save necessary regs */
  2158. uart_port_lock_irqsave(&sport->port, &flags);
  2159. sport->saved_reg[0] = imx_uart_readl(sport, UCR1);
  2160. sport->saved_reg[1] = imx_uart_readl(sport, UCR2);
  2161. sport->saved_reg[2] = imx_uart_readl(sport, UCR3);
  2162. sport->saved_reg[3] = imx_uart_readl(sport, UCR4);
  2163. sport->saved_reg[4] = imx_uart_readl(sport, UFCR);
  2164. sport->saved_reg[5] = imx_uart_readl(sport, UESC);
  2165. sport->saved_reg[6] = imx_uart_readl(sport, UTIM);
  2166. sport->saved_reg[7] = imx_uart_readl(sport, UBIR);
  2167. sport->saved_reg[8] = imx_uart_readl(sport, UBMR);
  2168. sport->saved_reg[9] = imx_uart_readl(sport, IMX21_UTS);
  2169. sport->context_saved = true;
  2170. uart_port_unlock_irqrestore(&sport->port, flags);
  2171. }
  2172. static void imx_uart_enable_wakeup(struct imx_port *sport, bool on)
  2173. {
  2174. u32 ucr3;
  2175. ucr3 = imx_uart_readl(sport, UCR3);
  2176. if (on) {
  2177. imx_uart_writel(sport, USR1_AWAKE, USR1);
  2178. ucr3 |= UCR3_AWAKEN;
  2179. } else {
  2180. ucr3 &= ~UCR3_AWAKEN;
  2181. }
  2182. imx_uart_writel(sport, ucr3, UCR3);
  2183. if (sport->have_rtscts) {
  2184. u32 ucr1 = imx_uart_readl(sport, UCR1);
  2185. if (on) {
  2186. imx_uart_writel(sport, USR1_RTSD, USR1);
  2187. ucr1 |= UCR1_RTSDEN;
  2188. } else {
  2189. ucr1 &= ~UCR1_RTSDEN;
  2190. }
  2191. imx_uart_writel(sport, ucr1, UCR1);
  2192. }
  2193. }
  2194. static int imx_uart_suspend_noirq(struct device *dev)
  2195. {
  2196. struct imx_port *sport = dev_get_drvdata(dev);
  2197. imx_uart_save_context(sport);
  2198. clk_disable(sport->clk_ipg);
  2199. pinctrl_pm_select_sleep_state(dev);
  2200. return 0;
  2201. }
  2202. static int imx_uart_resume_noirq(struct device *dev)
  2203. {
  2204. struct imx_port *sport = dev_get_drvdata(dev);
  2205. int ret;
  2206. pinctrl_pm_select_default_state(dev);
  2207. ret = clk_enable(sport->clk_ipg);
  2208. if (ret)
  2209. return ret;
  2210. imx_uart_restore_context(sport);
  2211. return 0;
  2212. }
  2213. static int imx_uart_suspend(struct device *dev)
  2214. {
  2215. struct imx_port *sport = dev_get_drvdata(dev);
  2216. int ret;
  2217. uart_suspend_port(&imx_uart_uart_driver, &sport->port);
  2218. disable_irq(sport->port.irq);
  2219. ret = clk_prepare_enable(sport->clk_ipg);
  2220. if (ret)
  2221. return ret;
  2222. /* enable wakeup from i.MX UART */
  2223. imx_uart_enable_wakeup(sport, true);
  2224. return 0;
  2225. }
  2226. static int imx_uart_resume(struct device *dev)
  2227. {
  2228. struct imx_port *sport = dev_get_drvdata(dev);
  2229. /* disable wakeup from i.MX UART */
  2230. imx_uart_enable_wakeup(sport, false);
  2231. uart_resume_port(&imx_uart_uart_driver, &sport->port);
  2232. enable_irq(sport->port.irq);
  2233. clk_disable_unprepare(sport->clk_ipg);
  2234. return 0;
  2235. }
  2236. static int imx_uart_freeze(struct device *dev)
  2237. {
  2238. struct imx_port *sport = dev_get_drvdata(dev);
  2239. uart_suspend_port(&imx_uart_uart_driver, &sport->port);
  2240. return clk_prepare_enable(sport->clk_ipg);
  2241. }
  2242. static int imx_uart_thaw(struct device *dev)
  2243. {
  2244. struct imx_port *sport = dev_get_drvdata(dev);
  2245. uart_resume_port(&imx_uart_uart_driver, &sport->port);
  2246. clk_disable_unprepare(sport->clk_ipg);
  2247. return 0;
  2248. }
  2249. static const struct dev_pm_ops imx_uart_pm_ops = {
  2250. .suspend_noirq = imx_uart_suspend_noirq,
  2251. .resume_noirq = imx_uart_resume_noirq,
  2252. .freeze_noirq = imx_uart_suspend_noirq,
  2253. .thaw_noirq = imx_uart_resume_noirq,
  2254. .restore_noirq = imx_uart_resume_noirq,
  2255. .suspend = imx_uart_suspend,
  2256. .resume = imx_uart_resume,
  2257. .freeze = imx_uart_freeze,
  2258. .thaw = imx_uart_thaw,
  2259. .restore = imx_uart_thaw,
  2260. };
  2261. static struct platform_driver imx_uart_platform_driver = {
  2262. .probe = imx_uart_probe,
  2263. .remove_new = imx_uart_remove,
  2264. .driver = {
  2265. .name = "imx-uart",
  2266. .of_match_table = imx_uart_dt_ids,
  2267. .pm = &imx_uart_pm_ops,
  2268. },
  2269. };
  2270. static int __init imx_uart_init(void)
  2271. {
  2272. int ret = uart_register_driver(&imx_uart_uart_driver);
  2273. if (ret)
  2274. return ret;
  2275. ret = platform_driver_register(&imx_uart_platform_driver);
  2276. if (ret != 0)
  2277. uart_unregister_driver(&imx_uart_uart_driver);
  2278. return ret;
  2279. }
  2280. static void __exit imx_uart_exit(void)
  2281. {
  2282. platform_driver_unregister(&imx_uart_platform_driver);
  2283. uart_unregister_driver(&imx_uart_uart_driver);
  2284. }
  2285. module_init(imx_uart_init);
  2286. module_exit(imx_uart_exit);
  2287. MODULE_AUTHOR("Sascha Hauer");
  2288. MODULE_DESCRIPTION("IMX generic serial port driver");
  2289. MODULE_LICENSE("GPL");
  2290. MODULE_ALIAS("platform:imx-uart");