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 <[email protected]>
  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. ret = devm_request_irq(&pdev->dev, rtsirq, imx_rtsint, 0,
  1826. dev_name(&pdev->dev), sport);
  1827. if (ret) {
  1828. dev_err(&pdev->dev, "failed to request rts irq: %d\n",
  1829. ret);
  1830. return ret;
  1831. }
  1832. } else {
  1833. ret = devm_request_irq(&pdev->dev, rxirq, imx_int, 0,
  1834. dev_name(&pdev->dev), sport);
  1835. if (ret) {
  1836. dev_err(&pdev->dev, "failed to request irq: %d\n", ret);
  1837. return ret;
  1838. }
  1839. }
  1840. imx_ports[sport->port.line] = sport;
  1841. platform_set_drvdata(pdev, sport);
  1842. return uart_add_one_port(&imx_reg, &sport->port);
  1843. }
  1844. static int serial_imx_remove(struct platform_device *pdev)
  1845. {
  1846. struct imx_port *sport = platform_get_drvdata(pdev);
  1847. return uart_remove_one_port(&imx_reg, &sport->port);
  1848. }
  1849. static void serial_imx_restore_context(struct imx_port *sport)
  1850. {
  1851. if (!sport->context_saved)
  1852. return;
  1853. writel(sport->saved_reg[4], sport->port.membase + UFCR);
  1854. writel(sport->saved_reg[5], sport->port.membase + UESC);
  1855. writel(sport->saved_reg[6], sport->port.membase + UTIM);
  1856. writel(sport->saved_reg[7], sport->port.membase + UBIR);
  1857. writel(sport->saved_reg[8], sport->port.membase + UBMR);
  1858. writel(sport->saved_reg[9], sport->port.membase + IMX21_UTS);
  1859. writel(sport->saved_reg[0], sport->port.membase + UCR1);
  1860. writel(sport->saved_reg[1] | UCR2_SRST, sport->port.membase + UCR2);
  1861. writel(sport->saved_reg[2], sport->port.membase + UCR3);
  1862. writel(sport->saved_reg[3], sport->port.membase + UCR4);
  1863. sport->context_saved = false;
  1864. }
  1865. static void serial_imx_save_context(struct imx_port *sport)
  1866. {
  1867. /* Save necessary regs */
  1868. sport->saved_reg[0] = readl(sport->port.membase + UCR1);
  1869. sport->saved_reg[1] = readl(sport->port.membase + UCR2);
  1870. sport->saved_reg[2] = readl(sport->port.membase + UCR3);
  1871. sport->saved_reg[3] = readl(sport->port.membase + UCR4);
  1872. sport->saved_reg[4] = readl(sport->port.membase + UFCR);
  1873. sport->saved_reg[5] = readl(sport->port.membase + UESC);
  1874. sport->saved_reg[6] = readl(sport->port.membase + UTIM);
  1875. sport->saved_reg[7] = readl(sport->port.membase + UBIR);
  1876. sport->saved_reg[8] = readl(sport->port.membase + UBMR);
  1877. sport->saved_reg[9] = readl(sport->port.membase + IMX21_UTS);
  1878. sport->context_saved = true;
  1879. }
  1880. static void serial_imx_enable_wakeup(struct imx_port *sport, bool on)
  1881. {
  1882. unsigned int val;
  1883. val = readl(sport->port.membase + UCR3);
  1884. if (on)
  1885. val |= UCR3_AWAKEN;
  1886. else
  1887. val &= ~UCR3_AWAKEN;
  1888. writel(val, sport->port.membase + UCR3);
  1889. if (sport->have_rtscts) {
  1890. val = readl(sport->port.membase + UCR1);
  1891. if (on)
  1892. val |= UCR1_RTSDEN;
  1893. else
  1894. val &= ~UCR1_RTSDEN;
  1895. writel(val, sport->port.membase + UCR1);
  1896. }
  1897. }
  1898. static int imx_serial_port_suspend_noirq(struct device *dev)
  1899. {
  1900. struct platform_device *pdev = to_platform_device(dev);
  1901. struct imx_port *sport = platform_get_drvdata(pdev);
  1902. int ret;
  1903. ret = clk_enable(sport->clk_ipg);
  1904. if (ret)
  1905. return ret;
  1906. serial_imx_save_context(sport);
  1907. clk_disable(sport->clk_ipg);
  1908. return 0;
  1909. }
  1910. static int imx_serial_port_resume_noirq(struct device *dev)
  1911. {
  1912. struct platform_device *pdev = to_platform_device(dev);
  1913. struct imx_port *sport = platform_get_drvdata(pdev);
  1914. int ret;
  1915. ret = clk_enable(sport->clk_ipg);
  1916. if (ret)
  1917. return ret;
  1918. serial_imx_restore_context(sport);
  1919. clk_disable(sport->clk_ipg);
  1920. return 0;
  1921. }
  1922. static int imx_serial_port_suspend(struct device *dev)
  1923. {
  1924. struct platform_device *pdev = to_platform_device(dev);
  1925. struct imx_port *sport = platform_get_drvdata(pdev);
  1926. /* enable wakeup from i.MX UART */
  1927. serial_imx_enable_wakeup(sport, true);
  1928. uart_suspend_port(&imx_reg, &sport->port);
  1929. /* Needed to enable clock in suspend_noirq */
  1930. return clk_prepare(sport->clk_ipg);
  1931. }
  1932. static int imx_serial_port_resume(struct device *dev)
  1933. {
  1934. struct platform_device *pdev = to_platform_device(dev);
  1935. struct imx_port *sport = platform_get_drvdata(pdev);
  1936. /* disable wakeup from i.MX UART */
  1937. serial_imx_enable_wakeup(sport, false);
  1938. uart_resume_port(&imx_reg, &sport->port);
  1939. clk_unprepare(sport->clk_ipg);
  1940. return 0;
  1941. }
  1942. static const struct dev_pm_ops imx_serial_port_pm_ops = {
  1943. .suspend_noirq = imx_serial_port_suspend_noirq,
  1944. .resume_noirq = imx_serial_port_resume_noirq,
  1945. .suspend = imx_serial_port_suspend,
  1946. .resume = imx_serial_port_resume,
  1947. };
  1948. static struct platform_driver serial_imx_driver = {
  1949. .probe = serial_imx_probe,
  1950. .remove = serial_imx_remove,
  1951. .id_table = imx_uart_devtype,
  1952. .driver = {
  1953. .name = "imx-uart",
  1954. .of_match_table = imx_uart_dt_ids,
  1955. .pm = &imx_serial_port_pm_ops,
  1956. },
  1957. };
  1958. static int __init imx_serial_init(void)
  1959. {
  1960. int ret = uart_register_driver(&imx_reg);
  1961. if (ret)
  1962. return ret;
  1963. ret = platform_driver_register(&serial_imx_driver);
  1964. if (ret != 0)
  1965. uart_unregister_driver(&imx_reg);
  1966. return ret;
  1967. }
  1968. static void __exit imx_serial_exit(void)
  1969. {
  1970. platform_driver_unregister(&serial_imx_driver);
  1971. uart_unregister_driver(&imx_reg);
  1972. }
  1973. module_init(imx_serial_init);
  1974. module_exit(imx_serial_exit);
  1975. MODULE_AUTHOR("Sascha Hauer");
  1976. MODULE_DESCRIPTION("IMX generic serial port driver");
  1977. MODULE_LICENSE("GPL");
  1978. MODULE_ALIAS("platform:imx-uart");