imx.c 67 KB

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