imx.c 63 KB

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