imx.c 68 KB

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