imx.c 67 KB

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