imx.c 63 KB

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