imx.c 71 KB

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