imx.c 63 KB

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