fsl_lpuart.c 76 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Freescale lpuart serial port driver
  4. *
  5. * Copyright 2012-2014 Freescale Semiconductor, Inc.
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/console.h>
  9. #include <linux/delay.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/dmaengine.h>
  12. #include <linux/dmapool.h>
  13. #include <linux/io.h>
  14. #include <linux/iopoll.h>
  15. #include <linux/irq.h>
  16. #include <linux/module.h>
  17. #include <linux/of.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_dma.h>
  20. #include <linux/serial_core.h>
  21. #include <linux/slab.h>
  22. #include <linux/tty_flip.h>
  23. /* All registers are 8-bit width */
  24. #define UARTBDH 0x00
  25. #define UARTBDL 0x01
  26. #define UARTCR1 0x02
  27. #define UARTCR2 0x03
  28. #define UARTSR1 0x04
  29. #define UARTCR3 0x06
  30. #define UARTDR 0x07
  31. #define UARTCR4 0x0a
  32. #define UARTCR5 0x0b
  33. #define UARTMODEM 0x0d
  34. #define UARTPFIFO 0x10
  35. #define UARTCFIFO 0x11
  36. #define UARTSFIFO 0x12
  37. #define UARTTWFIFO 0x13
  38. #define UARTTCFIFO 0x14
  39. #define UARTRWFIFO 0x15
  40. #define UARTBDH_LBKDIE 0x80
  41. #define UARTBDH_RXEDGIE 0x40
  42. #define UARTBDH_SBR_MASK 0x1f
  43. #define UARTCR1_LOOPS 0x80
  44. #define UARTCR1_RSRC 0x20
  45. #define UARTCR1_M 0x10
  46. #define UARTCR1_WAKE 0x08
  47. #define UARTCR1_ILT 0x04
  48. #define UARTCR1_PE 0x02
  49. #define UARTCR1_PT 0x01
  50. #define UARTCR2_TIE 0x80
  51. #define UARTCR2_TCIE 0x40
  52. #define UARTCR2_RIE 0x20
  53. #define UARTCR2_ILIE 0x10
  54. #define UARTCR2_TE 0x08
  55. #define UARTCR2_RE 0x04
  56. #define UARTCR2_RWU 0x02
  57. #define UARTCR2_SBK 0x01
  58. #define UARTSR1_TDRE 0x80
  59. #define UARTSR1_TC 0x40
  60. #define UARTSR1_RDRF 0x20
  61. #define UARTSR1_IDLE 0x10
  62. #define UARTSR1_OR 0x08
  63. #define UARTSR1_NF 0x04
  64. #define UARTSR1_FE 0x02
  65. #define UARTSR1_PE 0x01
  66. #define UARTCR3_R8 0x80
  67. #define UARTCR3_T8 0x40
  68. #define UARTCR3_TXDIR 0x20
  69. #define UARTCR3_TXINV 0x10
  70. #define UARTCR3_ORIE 0x08
  71. #define UARTCR3_NEIE 0x04
  72. #define UARTCR3_FEIE 0x02
  73. #define UARTCR3_PEIE 0x01
  74. #define UARTCR4_MAEN1 0x80
  75. #define UARTCR4_MAEN2 0x40
  76. #define UARTCR4_M10 0x20
  77. #define UARTCR4_BRFA_MASK 0x1f
  78. #define UARTCR4_BRFA_OFF 0
  79. #define UARTCR5_TDMAS 0x80
  80. #define UARTCR5_RDMAS 0x20
  81. #define UARTMODEM_RXRTSE 0x08
  82. #define UARTMODEM_TXRTSPOL 0x04
  83. #define UARTMODEM_TXRTSE 0x02
  84. #define UARTMODEM_TXCTSE 0x01
  85. #define UARTPFIFO_TXFE 0x80
  86. #define UARTPFIFO_FIFOSIZE_MASK 0x7
  87. #define UARTPFIFO_TXSIZE_OFF 4
  88. #define UARTPFIFO_RXFE 0x08
  89. #define UARTPFIFO_RXSIZE_OFF 0
  90. #define UARTCFIFO_TXFLUSH 0x80
  91. #define UARTCFIFO_RXFLUSH 0x40
  92. #define UARTCFIFO_RXOFE 0x04
  93. #define UARTCFIFO_TXOFE 0x02
  94. #define UARTCFIFO_RXUFE 0x01
  95. #define UARTSFIFO_TXEMPT 0x80
  96. #define UARTSFIFO_RXEMPT 0x40
  97. #define UARTSFIFO_RXOF 0x04
  98. #define UARTSFIFO_TXOF 0x02
  99. #define UARTSFIFO_RXUF 0x01
  100. /* 32-bit global registers only for i.MX7ULP/i.MX8x
  101. * Used to reset all internal logic and registers, except the Global Register.
  102. */
  103. #define UART_GLOBAL 0x8
  104. /* 32-bit register definition */
  105. #define UARTBAUD 0x00
  106. #define UARTSTAT 0x04
  107. #define UARTCTRL 0x08
  108. #define UARTDATA 0x0C
  109. #define UARTMATCH 0x10
  110. #define UARTMODIR 0x14
  111. #define UARTFIFO 0x18
  112. #define UARTWATER 0x1c
  113. #define UARTBAUD_MAEN1 0x80000000
  114. #define UARTBAUD_MAEN2 0x40000000
  115. #define UARTBAUD_M10 0x20000000
  116. #define UARTBAUD_TDMAE 0x00800000
  117. #define UARTBAUD_RDMAE 0x00200000
  118. #define UARTBAUD_MATCFG 0x00400000
  119. #define UARTBAUD_BOTHEDGE 0x00020000
  120. #define UARTBAUD_RESYNCDIS 0x00010000
  121. #define UARTBAUD_LBKDIE 0x00008000
  122. #define UARTBAUD_RXEDGIE 0x00004000
  123. #define UARTBAUD_SBNS 0x00002000
  124. #define UARTBAUD_SBR 0x00000000
  125. #define UARTBAUD_SBR_MASK 0x1fff
  126. #define UARTBAUD_OSR_MASK 0x1f
  127. #define UARTBAUD_OSR_SHIFT 24
  128. #define UARTSTAT_LBKDIF 0x80000000
  129. #define UARTSTAT_RXEDGIF 0x40000000
  130. #define UARTSTAT_MSBF 0x20000000
  131. #define UARTSTAT_RXINV 0x10000000
  132. #define UARTSTAT_RWUID 0x08000000
  133. #define UARTSTAT_BRK13 0x04000000
  134. #define UARTSTAT_LBKDE 0x02000000
  135. #define UARTSTAT_RAF 0x01000000
  136. #define UARTSTAT_TDRE 0x00800000
  137. #define UARTSTAT_TC 0x00400000
  138. #define UARTSTAT_RDRF 0x00200000
  139. #define UARTSTAT_IDLE 0x00100000
  140. #define UARTSTAT_OR 0x00080000
  141. #define UARTSTAT_NF 0x00040000
  142. #define UARTSTAT_FE 0x00020000
  143. #define UARTSTAT_PE 0x00010000
  144. #define UARTSTAT_MA1F 0x00008000
  145. #define UARTSTAT_M21F 0x00004000
  146. #define UARTCTRL_R8T9 0x80000000
  147. #define UARTCTRL_R9T8 0x40000000
  148. #define UARTCTRL_TXDIR 0x20000000
  149. #define UARTCTRL_TXINV 0x10000000
  150. #define UARTCTRL_ORIE 0x08000000
  151. #define UARTCTRL_NEIE 0x04000000
  152. #define UARTCTRL_FEIE 0x02000000
  153. #define UARTCTRL_PEIE 0x01000000
  154. #define UARTCTRL_TIE 0x00800000
  155. #define UARTCTRL_TCIE 0x00400000
  156. #define UARTCTRL_RIE 0x00200000
  157. #define UARTCTRL_ILIE 0x00100000
  158. #define UARTCTRL_TE 0x00080000
  159. #define UARTCTRL_RE 0x00040000
  160. #define UARTCTRL_RWU 0x00020000
  161. #define UARTCTRL_SBK 0x00010000
  162. #define UARTCTRL_MA1IE 0x00008000
  163. #define UARTCTRL_MA2IE 0x00004000
  164. #define UARTCTRL_IDLECFG 0x00000100
  165. #define UARTCTRL_LOOPS 0x00000080
  166. #define UARTCTRL_DOZEEN 0x00000040
  167. #define UARTCTRL_RSRC 0x00000020
  168. #define UARTCTRL_M 0x00000010
  169. #define UARTCTRL_WAKE 0x00000008
  170. #define UARTCTRL_ILT 0x00000004
  171. #define UARTCTRL_PE 0x00000002
  172. #define UARTCTRL_PT 0x00000001
  173. #define UARTDATA_NOISY 0x00008000
  174. #define UARTDATA_PARITYE 0x00004000
  175. #define UARTDATA_FRETSC 0x00002000
  176. #define UARTDATA_RXEMPT 0x00001000
  177. #define UARTDATA_IDLINE 0x00000800
  178. #define UARTDATA_MASK 0x3ff
  179. #define UARTMODIR_IREN 0x00020000
  180. #define UARTMODIR_TXCTSSRC 0x00000020
  181. #define UARTMODIR_TXCTSC 0x00000010
  182. #define UARTMODIR_RXRTSE 0x00000008
  183. #define UARTMODIR_TXRTSPOL 0x00000004
  184. #define UARTMODIR_TXRTSE 0x00000002
  185. #define UARTMODIR_TXCTSE 0x00000001
  186. #define UARTFIFO_TXEMPT 0x00800000
  187. #define UARTFIFO_RXEMPT 0x00400000
  188. #define UARTFIFO_TXOF 0x00020000
  189. #define UARTFIFO_RXUF 0x00010000
  190. #define UARTFIFO_TXFLUSH 0x00008000
  191. #define UARTFIFO_RXFLUSH 0x00004000
  192. #define UARTFIFO_TXOFE 0x00000200
  193. #define UARTFIFO_RXUFE 0x00000100
  194. #define UARTFIFO_TXFE 0x00000080
  195. #define UARTFIFO_FIFOSIZE_MASK 0x7
  196. #define UARTFIFO_TXSIZE_OFF 4
  197. #define UARTFIFO_RXFE 0x00000008
  198. #define UARTFIFO_RXSIZE_OFF 0
  199. #define UARTFIFO_DEPTH(x) (0x1 << ((x) ? ((x) + 1) : 0))
  200. #define UARTWATER_COUNT_MASK 0xff
  201. #define UARTWATER_TXCNT_OFF 8
  202. #define UARTWATER_RXCNT_OFF 24
  203. #define UARTWATER_WATER_MASK 0xff
  204. #define UARTWATER_TXWATER_OFF 0
  205. #define UARTWATER_RXWATER_OFF 16
  206. #define UART_GLOBAL_RST 0x2
  207. #define GLOBAL_RST_MIN_US 20
  208. #define GLOBAL_RST_MAX_US 40
  209. /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
  210. #define DMA_RX_TIMEOUT (10)
  211. #define DRIVER_NAME "fsl-lpuart"
  212. #define DEV_NAME "ttyLP"
  213. #define UART_NR 6
  214. /* IMX lpuart has four extra unused regs located at the beginning */
  215. #define IMX_REG_OFF 0x10
  216. enum lpuart_type {
  217. VF610_LPUART,
  218. LS1021A_LPUART,
  219. LS1028A_LPUART,
  220. IMX7ULP_LPUART,
  221. IMX8QXP_LPUART,
  222. IMXRT1050_LPUART,
  223. };
  224. struct lpuart_port {
  225. struct uart_port port;
  226. enum lpuart_type devtype;
  227. struct clk *ipg_clk;
  228. struct clk *baud_clk;
  229. unsigned int txfifo_size;
  230. unsigned int rxfifo_size;
  231. u8 rx_watermark;
  232. bool lpuart_dma_tx_use;
  233. bool lpuart_dma_rx_use;
  234. struct dma_chan *dma_tx_chan;
  235. struct dma_chan *dma_rx_chan;
  236. struct dma_async_tx_descriptor *dma_tx_desc;
  237. struct dma_async_tx_descriptor *dma_rx_desc;
  238. dma_cookie_t dma_tx_cookie;
  239. dma_cookie_t dma_rx_cookie;
  240. unsigned int dma_tx_bytes;
  241. unsigned int dma_rx_bytes;
  242. bool dma_tx_in_progress;
  243. unsigned int dma_rx_timeout;
  244. struct timer_list lpuart_timer;
  245. struct scatterlist rx_sgl, tx_sgl[2];
  246. struct circ_buf rx_ring;
  247. int rx_dma_rng_buf_len;
  248. unsigned int dma_tx_nents;
  249. wait_queue_head_t dma_wait;
  250. bool is_cs7; /* Set to true when character size is 7 */
  251. /* and the parity is enabled */
  252. };
  253. struct lpuart_soc_data {
  254. enum lpuart_type devtype;
  255. char iotype;
  256. u8 reg_off;
  257. u8 rx_watermark;
  258. };
  259. static const struct lpuart_soc_data vf_data = {
  260. .devtype = VF610_LPUART,
  261. .iotype = UPIO_MEM,
  262. .rx_watermark = 1,
  263. };
  264. static const struct lpuart_soc_data ls1021a_data = {
  265. .devtype = LS1021A_LPUART,
  266. .iotype = UPIO_MEM32BE,
  267. .rx_watermark = 1,
  268. };
  269. static const struct lpuart_soc_data ls1028a_data = {
  270. .devtype = LS1028A_LPUART,
  271. .iotype = UPIO_MEM32,
  272. .rx_watermark = 0,
  273. };
  274. static struct lpuart_soc_data imx7ulp_data = {
  275. .devtype = IMX7ULP_LPUART,
  276. .iotype = UPIO_MEM32,
  277. .reg_off = IMX_REG_OFF,
  278. .rx_watermark = 1,
  279. };
  280. static struct lpuart_soc_data imx8qxp_data = {
  281. .devtype = IMX8QXP_LPUART,
  282. .iotype = UPIO_MEM32,
  283. .reg_off = IMX_REG_OFF,
  284. .rx_watermark = 1,
  285. };
  286. static struct lpuart_soc_data imxrt1050_data = {
  287. .devtype = IMXRT1050_LPUART,
  288. .iotype = UPIO_MEM32,
  289. .reg_off = IMX_REG_OFF,
  290. .rx_watermark = 1,
  291. };
  292. static const struct of_device_id lpuart_dt_ids[] = {
  293. { .compatible = "fsl,vf610-lpuart", .data = &vf_data, },
  294. { .compatible = "fsl,ls1021a-lpuart", .data = &ls1021a_data, },
  295. { .compatible = "fsl,ls1028a-lpuart", .data = &ls1028a_data, },
  296. { .compatible = "fsl,imx7ulp-lpuart", .data = &imx7ulp_data, },
  297. { .compatible = "fsl,imx8qxp-lpuart", .data = &imx8qxp_data, },
  298. { .compatible = "fsl,imxrt1050-lpuart", .data = &imxrt1050_data},
  299. { /* sentinel */ }
  300. };
  301. MODULE_DEVICE_TABLE(of, lpuart_dt_ids);
  302. /* Forward declare this for the dma callbacks*/
  303. static void lpuart_dma_tx_complete(void *arg);
  304. static inline bool is_layerscape_lpuart(struct lpuart_port *sport)
  305. {
  306. return (sport->devtype == LS1021A_LPUART ||
  307. sport->devtype == LS1028A_LPUART);
  308. }
  309. static inline bool is_imx7ulp_lpuart(struct lpuart_port *sport)
  310. {
  311. return sport->devtype == IMX7ULP_LPUART;
  312. }
  313. static inline bool is_imx8qxp_lpuart(struct lpuart_port *sport)
  314. {
  315. return sport->devtype == IMX8QXP_LPUART;
  316. }
  317. static inline u32 lpuart32_read(struct uart_port *port, u32 off)
  318. {
  319. switch (port->iotype) {
  320. case UPIO_MEM32:
  321. return readl(port->membase + off);
  322. case UPIO_MEM32BE:
  323. return ioread32be(port->membase + off);
  324. default:
  325. return 0;
  326. }
  327. }
  328. static inline void lpuart32_write(struct uart_port *port, u32 val,
  329. u32 off)
  330. {
  331. switch (port->iotype) {
  332. case UPIO_MEM32:
  333. writel(val, port->membase + off);
  334. break;
  335. case UPIO_MEM32BE:
  336. iowrite32be(val, port->membase + off);
  337. break;
  338. }
  339. }
  340. static int __lpuart_enable_clks(struct lpuart_port *sport, bool is_en)
  341. {
  342. int ret = 0;
  343. if (is_en) {
  344. ret = clk_prepare_enable(sport->ipg_clk);
  345. if (ret)
  346. return ret;
  347. ret = clk_prepare_enable(sport->baud_clk);
  348. if (ret) {
  349. clk_disable_unprepare(sport->ipg_clk);
  350. return ret;
  351. }
  352. } else {
  353. clk_disable_unprepare(sport->baud_clk);
  354. clk_disable_unprepare(sport->ipg_clk);
  355. }
  356. return 0;
  357. }
  358. static unsigned int lpuart_get_baud_clk_rate(struct lpuart_port *sport)
  359. {
  360. if (is_imx8qxp_lpuart(sport))
  361. return clk_get_rate(sport->baud_clk);
  362. return clk_get_rate(sport->ipg_clk);
  363. }
  364. #define lpuart_enable_clks(x) __lpuart_enable_clks(x, true)
  365. #define lpuart_disable_clks(x) __lpuart_enable_clks(x, false)
  366. static void lpuart_stop_tx(struct uart_port *port)
  367. {
  368. unsigned char temp;
  369. temp = readb(port->membase + UARTCR2);
  370. temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
  371. writeb(temp, port->membase + UARTCR2);
  372. }
  373. static void lpuart32_stop_tx(struct uart_port *port)
  374. {
  375. unsigned long temp;
  376. temp = lpuart32_read(port, UARTCTRL);
  377. temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
  378. lpuart32_write(port, temp, UARTCTRL);
  379. }
  380. static void lpuart_stop_rx(struct uart_port *port)
  381. {
  382. unsigned char temp;
  383. temp = readb(port->membase + UARTCR2);
  384. writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
  385. }
  386. static void lpuart32_stop_rx(struct uart_port *port)
  387. {
  388. unsigned long temp;
  389. temp = lpuart32_read(port, UARTCTRL);
  390. lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
  391. }
  392. static void lpuart_dma_tx(struct lpuart_port *sport)
  393. {
  394. struct circ_buf *xmit = &sport->port.state->xmit;
  395. struct scatterlist *sgl = sport->tx_sgl;
  396. struct device *dev = sport->port.dev;
  397. struct dma_chan *chan = sport->dma_tx_chan;
  398. int ret;
  399. if (sport->dma_tx_in_progress)
  400. return;
  401. sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
  402. if (xmit->tail < xmit->head || xmit->head == 0) {
  403. sport->dma_tx_nents = 1;
  404. sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
  405. } else {
  406. sport->dma_tx_nents = 2;
  407. sg_init_table(sgl, 2);
  408. sg_set_buf(sgl, xmit->buf + xmit->tail,
  409. UART_XMIT_SIZE - xmit->tail);
  410. sg_set_buf(sgl + 1, xmit->buf, xmit->head);
  411. }
  412. ret = dma_map_sg(chan->device->dev, sgl, sport->dma_tx_nents,
  413. DMA_TO_DEVICE);
  414. if (!ret) {
  415. dev_err(dev, "DMA mapping error for TX.\n");
  416. return;
  417. }
  418. sport->dma_tx_desc = dmaengine_prep_slave_sg(chan, sgl,
  419. ret, DMA_MEM_TO_DEV,
  420. DMA_PREP_INTERRUPT);
  421. if (!sport->dma_tx_desc) {
  422. dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
  423. DMA_TO_DEVICE);
  424. dev_err(dev, "Cannot prepare TX slave DMA!\n");
  425. return;
  426. }
  427. sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
  428. sport->dma_tx_desc->callback_param = sport;
  429. sport->dma_tx_in_progress = true;
  430. sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
  431. dma_async_issue_pending(chan);
  432. }
  433. static bool lpuart_stopped_or_empty(struct uart_port *port)
  434. {
  435. return uart_circ_empty(&port->state->xmit) || uart_tx_stopped(port);
  436. }
  437. static void lpuart_dma_tx_complete(void *arg)
  438. {
  439. struct lpuart_port *sport = arg;
  440. struct scatterlist *sgl = &sport->tx_sgl[0];
  441. struct circ_buf *xmit = &sport->port.state->xmit;
  442. struct dma_chan *chan = sport->dma_tx_chan;
  443. unsigned long flags;
  444. spin_lock_irqsave(&sport->port.lock, flags);
  445. if (!sport->dma_tx_in_progress) {
  446. spin_unlock_irqrestore(&sport->port.lock, flags);
  447. return;
  448. }
  449. dma_unmap_sg(chan->device->dev, sgl, sport->dma_tx_nents,
  450. DMA_TO_DEVICE);
  451. xmit->tail = (xmit->tail + sport->dma_tx_bytes) & (UART_XMIT_SIZE - 1);
  452. sport->port.icount.tx += sport->dma_tx_bytes;
  453. sport->dma_tx_in_progress = false;
  454. spin_unlock_irqrestore(&sport->port.lock, flags);
  455. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  456. uart_write_wakeup(&sport->port);
  457. if (waitqueue_active(&sport->dma_wait)) {
  458. wake_up(&sport->dma_wait);
  459. return;
  460. }
  461. spin_lock_irqsave(&sport->port.lock, flags);
  462. if (!lpuart_stopped_or_empty(&sport->port))
  463. lpuart_dma_tx(sport);
  464. spin_unlock_irqrestore(&sport->port.lock, flags);
  465. }
  466. static dma_addr_t lpuart_dma_datareg_addr(struct lpuart_port *sport)
  467. {
  468. switch (sport->port.iotype) {
  469. case UPIO_MEM32:
  470. return sport->port.mapbase + UARTDATA;
  471. case UPIO_MEM32BE:
  472. return sport->port.mapbase + UARTDATA + sizeof(u32) - 1;
  473. }
  474. return sport->port.mapbase + UARTDR;
  475. }
  476. static int lpuart_dma_tx_request(struct uart_port *port)
  477. {
  478. struct lpuart_port *sport = container_of(port,
  479. struct lpuart_port, port);
  480. struct dma_slave_config dma_tx_sconfig = {};
  481. int ret;
  482. dma_tx_sconfig.dst_addr = lpuart_dma_datareg_addr(sport);
  483. dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  484. dma_tx_sconfig.dst_maxburst = 1;
  485. dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
  486. ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
  487. if (ret) {
  488. dev_err(sport->port.dev,
  489. "DMA slave config failed, err = %d\n", ret);
  490. return ret;
  491. }
  492. return 0;
  493. }
  494. static bool lpuart_is_32(struct lpuart_port *sport)
  495. {
  496. return sport->port.iotype == UPIO_MEM32 ||
  497. sport->port.iotype == UPIO_MEM32BE;
  498. }
  499. static void lpuart_flush_buffer(struct uart_port *port)
  500. {
  501. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  502. struct dma_chan *chan = sport->dma_tx_chan;
  503. u32 val;
  504. if (sport->lpuart_dma_tx_use) {
  505. if (sport->dma_tx_in_progress) {
  506. dma_unmap_sg(chan->device->dev, &sport->tx_sgl[0],
  507. sport->dma_tx_nents, DMA_TO_DEVICE);
  508. sport->dma_tx_in_progress = false;
  509. }
  510. dmaengine_terminate_async(chan);
  511. }
  512. if (lpuart_is_32(sport)) {
  513. val = lpuart32_read(&sport->port, UARTFIFO);
  514. val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
  515. lpuart32_write(&sport->port, val, UARTFIFO);
  516. } else {
  517. val = readb(sport->port.membase + UARTCFIFO);
  518. val |= UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH;
  519. writeb(val, sport->port.membase + UARTCFIFO);
  520. }
  521. }
  522. static void lpuart_wait_bit_set(struct uart_port *port, unsigned int offset,
  523. u8 bit)
  524. {
  525. while (!(readb(port->membase + offset) & bit))
  526. cpu_relax();
  527. }
  528. static void lpuart32_wait_bit_set(struct uart_port *port, unsigned int offset,
  529. u32 bit)
  530. {
  531. while (!(lpuart32_read(port, offset) & bit))
  532. cpu_relax();
  533. }
  534. #if defined(CONFIG_CONSOLE_POLL)
  535. static int lpuart_poll_init(struct uart_port *port)
  536. {
  537. struct lpuart_port *sport = container_of(port,
  538. struct lpuart_port, port);
  539. unsigned long flags;
  540. unsigned char temp;
  541. sport->port.fifosize = 0;
  542. spin_lock_irqsave(&sport->port.lock, flags);
  543. /* Disable Rx & Tx */
  544. writeb(0, sport->port.membase + UARTCR2);
  545. temp = readb(sport->port.membase + UARTPFIFO);
  546. /* Enable Rx and Tx FIFO */
  547. writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
  548. sport->port.membase + UARTPFIFO);
  549. /* flush Tx and Rx FIFO */
  550. writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
  551. sport->port.membase + UARTCFIFO);
  552. /* explicitly clear RDRF */
  553. if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
  554. readb(sport->port.membase + UARTDR);
  555. writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
  556. }
  557. writeb(0, sport->port.membase + UARTTWFIFO);
  558. writeb(1, sport->port.membase + UARTRWFIFO);
  559. /* Enable Rx and Tx */
  560. writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
  561. spin_unlock_irqrestore(&sport->port.lock, flags);
  562. return 0;
  563. }
  564. static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
  565. {
  566. /* drain */
  567. lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
  568. writeb(c, port->membase + UARTDR);
  569. }
  570. static int lpuart_poll_get_char(struct uart_port *port)
  571. {
  572. if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
  573. return NO_POLL_CHAR;
  574. return readb(port->membase + UARTDR);
  575. }
  576. static int lpuart32_poll_init(struct uart_port *port)
  577. {
  578. unsigned long flags;
  579. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  580. u32 temp;
  581. sport->port.fifosize = 0;
  582. spin_lock_irqsave(&sport->port.lock, flags);
  583. /* Disable Rx & Tx */
  584. lpuart32_write(&sport->port, 0, UARTCTRL);
  585. temp = lpuart32_read(&sport->port, UARTFIFO);
  586. /* Enable Rx and Tx FIFO */
  587. lpuart32_write(&sport->port, temp | UARTFIFO_RXFE | UARTFIFO_TXFE, UARTFIFO);
  588. /* flush Tx and Rx FIFO */
  589. lpuart32_write(&sport->port, UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH, UARTFIFO);
  590. /* explicitly clear RDRF */
  591. if (lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_RDRF) {
  592. lpuart32_read(&sport->port, UARTDATA);
  593. lpuart32_write(&sport->port, UARTFIFO_RXUF, UARTFIFO);
  594. }
  595. /* Enable Rx and Tx */
  596. lpuart32_write(&sport->port, UARTCTRL_RE | UARTCTRL_TE, UARTCTRL);
  597. spin_unlock_irqrestore(&sport->port.lock, flags);
  598. return 0;
  599. }
  600. static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
  601. {
  602. lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
  603. lpuart32_write(port, c, UARTDATA);
  604. }
  605. static int lpuart32_poll_get_char(struct uart_port *port)
  606. {
  607. if (!(lpuart32_read(port, UARTWATER) >> UARTWATER_RXCNT_OFF))
  608. return NO_POLL_CHAR;
  609. return lpuart32_read(port, UARTDATA);
  610. }
  611. #endif
  612. static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
  613. {
  614. struct circ_buf *xmit = &sport->port.state->xmit;
  615. if (sport->port.x_char) {
  616. writeb(sport->port.x_char, sport->port.membase + UARTDR);
  617. sport->port.icount.tx++;
  618. sport->port.x_char = 0;
  619. return;
  620. }
  621. if (lpuart_stopped_or_empty(&sport->port)) {
  622. lpuart_stop_tx(&sport->port);
  623. return;
  624. }
  625. while (!uart_circ_empty(xmit) &&
  626. (readb(sport->port.membase + UARTTCFIFO) < sport->txfifo_size)) {
  627. writeb(xmit->buf[xmit->tail], sport->port.membase + UARTDR);
  628. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  629. sport->port.icount.tx++;
  630. }
  631. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  632. uart_write_wakeup(&sport->port);
  633. if (uart_circ_empty(xmit))
  634. lpuart_stop_tx(&sport->port);
  635. }
  636. static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
  637. {
  638. struct circ_buf *xmit = &sport->port.state->xmit;
  639. unsigned long txcnt;
  640. if (sport->port.x_char) {
  641. lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
  642. sport->port.icount.tx++;
  643. sport->port.x_char = 0;
  644. return;
  645. }
  646. if (lpuart_stopped_or_empty(&sport->port)) {
  647. lpuart32_stop_tx(&sport->port);
  648. return;
  649. }
  650. txcnt = lpuart32_read(&sport->port, UARTWATER);
  651. txcnt = txcnt >> UARTWATER_TXCNT_OFF;
  652. txcnt &= UARTWATER_COUNT_MASK;
  653. while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
  654. lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
  655. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  656. sport->port.icount.tx++;
  657. txcnt = lpuart32_read(&sport->port, UARTWATER);
  658. txcnt = txcnt >> UARTWATER_TXCNT_OFF;
  659. txcnt &= UARTWATER_COUNT_MASK;
  660. }
  661. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  662. uart_write_wakeup(&sport->port);
  663. if (uart_circ_empty(xmit))
  664. lpuart32_stop_tx(&sport->port);
  665. }
  666. static void lpuart_start_tx(struct uart_port *port)
  667. {
  668. struct lpuart_port *sport = container_of(port,
  669. struct lpuart_port, port);
  670. unsigned char temp;
  671. temp = readb(port->membase + UARTCR2);
  672. writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
  673. if (sport->lpuart_dma_tx_use) {
  674. if (!lpuart_stopped_or_empty(port))
  675. lpuart_dma_tx(sport);
  676. } else {
  677. if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
  678. lpuart_transmit_buffer(sport);
  679. }
  680. }
  681. static void lpuart32_start_tx(struct uart_port *port)
  682. {
  683. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  684. unsigned long temp;
  685. if (sport->lpuart_dma_tx_use) {
  686. if (!lpuart_stopped_or_empty(port))
  687. lpuart_dma_tx(sport);
  688. } else {
  689. temp = lpuart32_read(port, UARTCTRL);
  690. lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
  691. if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
  692. lpuart32_transmit_buffer(sport);
  693. }
  694. }
  695. /* return TIOCSER_TEMT when transmitter is not busy */
  696. static unsigned int lpuart_tx_empty(struct uart_port *port)
  697. {
  698. struct lpuart_port *sport = container_of(port,
  699. struct lpuart_port, port);
  700. unsigned char sr1 = readb(port->membase + UARTSR1);
  701. unsigned char sfifo = readb(port->membase + UARTSFIFO);
  702. if (sport->dma_tx_in_progress)
  703. return 0;
  704. if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
  705. return TIOCSER_TEMT;
  706. return 0;
  707. }
  708. static unsigned int lpuart32_tx_empty(struct uart_port *port)
  709. {
  710. struct lpuart_port *sport = container_of(port,
  711. struct lpuart_port, port);
  712. unsigned long stat = lpuart32_read(port, UARTSTAT);
  713. unsigned long sfifo = lpuart32_read(port, UARTFIFO);
  714. unsigned long ctrl = lpuart32_read(port, UARTCTRL);
  715. if (sport->dma_tx_in_progress)
  716. return 0;
  717. /*
  718. * LPUART Transmission Complete Flag may never be set while queuing a break
  719. * character, so avoid checking for transmission complete when UARTCTRL_SBK
  720. * is asserted.
  721. */
  722. if ((stat & UARTSTAT_TC && sfifo & UARTFIFO_TXEMPT) || ctrl & UARTCTRL_SBK)
  723. return TIOCSER_TEMT;
  724. return 0;
  725. }
  726. static void lpuart_txint(struct lpuart_port *sport)
  727. {
  728. spin_lock(&sport->port.lock);
  729. lpuart_transmit_buffer(sport);
  730. spin_unlock(&sport->port.lock);
  731. }
  732. static void lpuart_rxint(struct lpuart_port *sport)
  733. {
  734. unsigned int flg, ignored = 0, overrun = 0;
  735. struct tty_port *port = &sport->port.state->port;
  736. unsigned char rx, sr;
  737. spin_lock(&sport->port.lock);
  738. while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
  739. flg = TTY_NORMAL;
  740. sport->port.icount.rx++;
  741. /*
  742. * to clear the FE, OR, NF, FE, PE flags,
  743. * read SR1 then read DR
  744. */
  745. sr = readb(sport->port.membase + UARTSR1);
  746. rx = readb(sport->port.membase + UARTDR);
  747. if (uart_prepare_sysrq_char(&sport->port, rx))
  748. continue;
  749. if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
  750. if (sr & UARTSR1_PE)
  751. sport->port.icount.parity++;
  752. else if (sr & UARTSR1_FE)
  753. sport->port.icount.frame++;
  754. if (sr & UARTSR1_OR)
  755. overrun++;
  756. if (sr & sport->port.ignore_status_mask) {
  757. if (++ignored > 100)
  758. goto out;
  759. continue;
  760. }
  761. sr &= sport->port.read_status_mask;
  762. if (sr & UARTSR1_PE)
  763. flg = TTY_PARITY;
  764. else if (sr & UARTSR1_FE)
  765. flg = TTY_FRAME;
  766. if (sr & UARTSR1_OR)
  767. flg = TTY_OVERRUN;
  768. sport->port.sysrq = 0;
  769. }
  770. if (tty_insert_flip_char(port, rx, flg) == 0)
  771. sport->port.icount.buf_overrun++;
  772. }
  773. out:
  774. if (overrun) {
  775. sport->port.icount.overrun += overrun;
  776. /*
  777. * Overruns cause FIFO pointers to become missaligned.
  778. * Flushing the receive FIFO reinitializes the pointers.
  779. */
  780. writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
  781. writeb(UARTSFIFO_RXOF, sport->port.membase + UARTSFIFO);
  782. }
  783. uart_unlock_and_check_sysrq(&sport->port);
  784. tty_flip_buffer_push(port);
  785. }
  786. static void lpuart32_txint(struct lpuart_port *sport)
  787. {
  788. spin_lock(&sport->port.lock);
  789. lpuart32_transmit_buffer(sport);
  790. spin_unlock(&sport->port.lock);
  791. }
  792. static void lpuart32_rxint(struct lpuart_port *sport)
  793. {
  794. unsigned int flg, ignored = 0;
  795. struct tty_port *port = &sport->port.state->port;
  796. unsigned long rx, sr;
  797. bool is_break;
  798. spin_lock(&sport->port.lock);
  799. while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
  800. flg = TTY_NORMAL;
  801. sport->port.icount.rx++;
  802. /*
  803. * to clear the FE, OR, NF, FE, PE flags,
  804. * read STAT then read DATA reg
  805. */
  806. sr = lpuart32_read(&sport->port, UARTSTAT);
  807. rx = lpuart32_read(&sport->port, UARTDATA);
  808. rx &= UARTDATA_MASK;
  809. /*
  810. * The LPUART can't distinguish between a break and a framing error,
  811. * thus we assume it is a break if the received data is zero.
  812. */
  813. is_break = (sr & UARTSTAT_FE) && !rx;
  814. if (is_break && uart_handle_break(&sport->port))
  815. continue;
  816. if (uart_prepare_sysrq_char(&sport->port, rx))
  817. continue;
  818. if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
  819. if (sr & UARTSTAT_PE) {
  820. sport->port.icount.parity++;
  821. } else if (sr & UARTSTAT_FE) {
  822. if (is_break)
  823. sport->port.icount.brk++;
  824. else
  825. sport->port.icount.frame++;
  826. }
  827. if (sr & UARTSTAT_OR)
  828. sport->port.icount.overrun++;
  829. if (sr & sport->port.ignore_status_mask) {
  830. if (++ignored > 100)
  831. goto out;
  832. continue;
  833. }
  834. sr &= sport->port.read_status_mask;
  835. if (sr & UARTSTAT_PE) {
  836. flg = TTY_PARITY;
  837. } else if (sr & UARTSTAT_FE) {
  838. if (is_break)
  839. flg = TTY_BREAK;
  840. else
  841. flg = TTY_FRAME;
  842. }
  843. if (sr & UARTSTAT_OR)
  844. flg = TTY_OVERRUN;
  845. }
  846. if (sport->is_cs7)
  847. rx &= 0x7F;
  848. if (tty_insert_flip_char(port, rx, flg) == 0)
  849. sport->port.icount.buf_overrun++;
  850. }
  851. out:
  852. uart_unlock_and_check_sysrq(&sport->port);
  853. tty_flip_buffer_push(port);
  854. }
  855. static irqreturn_t lpuart_int(int irq, void *dev_id)
  856. {
  857. struct lpuart_port *sport = dev_id;
  858. unsigned char sts;
  859. sts = readb(sport->port.membase + UARTSR1);
  860. /* SysRq, using dma, check for linebreak by framing err. */
  861. if (sts & UARTSR1_FE && sport->lpuart_dma_rx_use) {
  862. readb(sport->port.membase + UARTDR);
  863. uart_handle_break(&sport->port);
  864. /* linebreak produces some garbage, removing it */
  865. writeb(UARTCFIFO_RXFLUSH, sport->port.membase + UARTCFIFO);
  866. return IRQ_HANDLED;
  867. }
  868. if (sts & UARTSR1_RDRF && !sport->lpuart_dma_rx_use)
  869. lpuart_rxint(sport);
  870. if (sts & UARTSR1_TDRE && !sport->lpuart_dma_tx_use)
  871. lpuart_txint(sport);
  872. return IRQ_HANDLED;
  873. }
  874. static irqreturn_t lpuart32_int(int irq, void *dev_id)
  875. {
  876. struct lpuart_port *sport = dev_id;
  877. unsigned long sts, rxcount;
  878. sts = lpuart32_read(&sport->port, UARTSTAT);
  879. rxcount = lpuart32_read(&sport->port, UARTWATER);
  880. rxcount = rxcount >> UARTWATER_RXCNT_OFF;
  881. if ((sts & UARTSTAT_RDRF || rxcount > 0) && !sport->lpuart_dma_rx_use)
  882. lpuart32_rxint(sport);
  883. if ((sts & UARTSTAT_TDRE) && !sport->lpuart_dma_tx_use)
  884. lpuart32_txint(sport);
  885. lpuart32_write(&sport->port, sts, UARTSTAT);
  886. return IRQ_HANDLED;
  887. }
  888. static inline void lpuart_handle_sysrq_chars(struct uart_port *port,
  889. unsigned char *p, int count)
  890. {
  891. while (count--) {
  892. if (*p && uart_handle_sysrq_char(port, *p))
  893. return;
  894. p++;
  895. }
  896. }
  897. static void lpuart_handle_sysrq(struct lpuart_port *sport)
  898. {
  899. struct circ_buf *ring = &sport->rx_ring;
  900. int count;
  901. if (ring->head < ring->tail) {
  902. count = sport->rx_sgl.length - ring->tail;
  903. lpuart_handle_sysrq_chars(&sport->port,
  904. ring->buf + ring->tail, count);
  905. ring->tail = 0;
  906. }
  907. if (ring->head > ring->tail) {
  908. count = ring->head - ring->tail;
  909. lpuart_handle_sysrq_chars(&sport->port,
  910. ring->buf + ring->tail, count);
  911. ring->tail = ring->head;
  912. }
  913. }
  914. static int lpuart_tty_insert_flip_string(struct tty_port *port,
  915. unsigned char *chars, size_t size, bool is_cs7)
  916. {
  917. int i;
  918. if (is_cs7)
  919. for (i = 0; i < size; i++)
  920. chars[i] &= 0x7F;
  921. return tty_insert_flip_string(port, chars, size);
  922. }
  923. static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
  924. {
  925. struct tty_port *port = &sport->port.state->port;
  926. struct dma_tx_state state;
  927. enum dma_status dmastat;
  928. struct dma_chan *chan = sport->dma_rx_chan;
  929. struct circ_buf *ring = &sport->rx_ring;
  930. unsigned long flags;
  931. int count, copied;
  932. if (lpuart_is_32(sport)) {
  933. unsigned long sr = lpuart32_read(&sport->port, UARTSTAT);
  934. if (sr & (UARTSTAT_PE | UARTSTAT_FE)) {
  935. /* Clear the error flags */
  936. lpuart32_write(&sport->port, sr, UARTSTAT);
  937. if (sr & UARTSTAT_PE)
  938. sport->port.icount.parity++;
  939. else if (sr & UARTSTAT_FE)
  940. sport->port.icount.frame++;
  941. }
  942. } else {
  943. unsigned char sr = readb(sport->port.membase + UARTSR1);
  944. if (sr & (UARTSR1_PE | UARTSR1_FE)) {
  945. unsigned char cr2;
  946. /* Disable receiver during this operation... */
  947. cr2 = readb(sport->port.membase + UARTCR2);
  948. cr2 &= ~UARTCR2_RE;
  949. writeb(cr2, sport->port.membase + UARTCR2);
  950. /* Read DR to clear the error flags */
  951. readb(sport->port.membase + UARTDR);
  952. if (sr & UARTSR1_PE)
  953. sport->port.icount.parity++;
  954. else if (sr & UARTSR1_FE)
  955. sport->port.icount.frame++;
  956. /*
  957. * At this point parity/framing error is
  958. * cleared However, since the DMA already read
  959. * the data register and we had to read it
  960. * again after reading the status register to
  961. * properly clear the flags, the FIFO actually
  962. * underflowed... This requires a clearing of
  963. * the FIFO...
  964. */
  965. if (readb(sport->port.membase + UARTSFIFO) &
  966. UARTSFIFO_RXUF) {
  967. writeb(UARTSFIFO_RXUF,
  968. sport->port.membase + UARTSFIFO);
  969. writeb(UARTCFIFO_RXFLUSH,
  970. sport->port.membase + UARTCFIFO);
  971. }
  972. cr2 |= UARTCR2_RE;
  973. writeb(cr2, sport->port.membase + UARTCR2);
  974. }
  975. }
  976. async_tx_ack(sport->dma_rx_desc);
  977. spin_lock_irqsave(&sport->port.lock, flags);
  978. dmastat = dmaengine_tx_status(chan, sport->dma_rx_cookie, &state);
  979. if (dmastat == DMA_ERROR) {
  980. dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
  981. spin_unlock_irqrestore(&sport->port.lock, flags);
  982. return;
  983. }
  984. /* CPU claims ownership of RX DMA buffer */
  985. dma_sync_sg_for_cpu(chan->device->dev, &sport->rx_sgl, 1,
  986. DMA_FROM_DEVICE);
  987. /*
  988. * ring->head points to the end of data already written by the DMA.
  989. * ring->tail points to the beginning of data to be read by the
  990. * framework.
  991. * The current transfer size should not be larger than the dma buffer
  992. * length.
  993. */
  994. ring->head = sport->rx_sgl.length - state.residue;
  995. BUG_ON(ring->head > sport->rx_sgl.length);
  996. /*
  997. * Silent handling of keys pressed in the sysrq timeframe
  998. */
  999. if (sport->port.sysrq) {
  1000. lpuart_handle_sysrq(sport);
  1001. goto exit;
  1002. }
  1003. /*
  1004. * At this point ring->head may point to the first byte right after the
  1005. * last byte of the dma buffer:
  1006. * 0 <= ring->head <= sport->rx_sgl.length
  1007. *
  1008. * However ring->tail must always points inside the dma buffer:
  1009. * 0 <= ring->tail <= sport->rx_sgl.length - 1
  1010. *
  1011. * Since we use a ring buffer, we have to handle the case
  1012. * where head is lower than tail. In such a case, we first read from
  1013. * tail to the end of the buffer then reset tail.
  1014. */
  1015. if (ring->head < ring->tail) {
  1016. count = sport->rx_sgl.length - ring->tail;
  1017. copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
  1018. count, sport->is_cs7);
  1019. if (copied != count)
  1020. sport->port.icount.buf_overrun++;
  1021. ring->tail = 0;
  1022. sport->port.icount.rx += copied;
  1023. }
  1024. /* Finally we read data from tail to head */
  1025. if (ring->tail < ring->head) {
  1026. count = ring->head - ring->tail;
  1027. copied = lpuart_tty_insert_flip_string(port, ring->buf + ring->tail,
  1028. count, sport->is_cs7);
  1029. if (copied != count)
  1030. sport->port.icount.buf_overrun++;
  1031. /* Wrap ring->head if needed */
  1032. if (ring->head >= sport->rx_sgl.length)
  1033. ring->head = 0;
  1034. ring->tail = ring->head;
  1035. sport->port.icount.rx += copied;
  1036. }
  1037. exit:
  1038. dma_sync_sg_for_device(chan->device->dev, &sport->rx_sgl, 1,
  1039. DMA_FROM_DEVICE);
  1040. spin_unlock_irqrestore(&sport->port.lock, flags);
  1041. tty_flip_buffer_push(port);
  1042. mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
  1043. }
  1044. static void lpuart_dma_rx_complete(void *arg)
  1045. {
  1046. struct lpuart_port *sport = arg;
  1047. lpuart_copy_rx_to_tty(sport);
  1048. }
  1049. static void lpuart_timer_func(struct timer_list *t)
  1050. {
  1051. struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
  1052. lpuart_copy_rx_to_tty(sport);
  1053. }
  1054. static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
  1055. {
  1056. struct dma_slave_config dma_rx_sconfig = {};
  1057. struct circ_buf *ring = &sport->rx_ring;
  1058. int ret, nent;
  1059. struct tty_port *port = &sport->port.state->port;
  1060. struct tty_struct *tty = port->tty;
  1061. struct ktermios *termios = &tty->termios;
  1062. struct dma_chan *chan = sport->dma_rx_chan;
  1063. unsigned int bits = tty_get_frame_size(termios->c_cflag);
  1064. unsigned int baud = tty_get_baud_rate(tty);
  1065. /*
  1066. * Calculate length of one DMA buffer size to keep latency below
  1067. * 10ms at any baud rate.
  1068. */
  1069. sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits / 1000) * 2;
  1070. sport->rx_dma_rng_buf_len = (1 << fls(sport->rx_dma_rng_buf_len));
  1071. if (sport->rx_dma_rng_buf_len < 16)
  1072. sport->rx_dma_rng_buf_len = 16;
  1073. ring->buf = kzalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
  1074. if (!ring->buf)
  1075. return -ENOMEM;
  1076. sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
  1077. nent = dma_map_sg(chan->device->dev, &sport->rx_sgl, 1,
  1078. DMA_FROM_DEVICE);
  1079. if (!nent) {
  1080. dev_err(sport->port.dev, "DMA Rx mapping error\n");
  1081. return -EINVAL;
  1082. }
  1083. dma_rx_sconfig.src_addr = lpuart_dma_datareg_addr(sport);
  1084. dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1085. dma_rx_sconfig.src_maxburst = 1;
  1086. dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
  1087. ret = dmaengine_slave_config(chan, &dma_rx_sconfig);
  1088. if (ret < 0) {
  1089. dev_err(sport->port.dev,
  1090. "DMA Rx slave config failed, err = %d\n", ret);
  1091. return ret;
  1092. }
  1093. sport->dma_rx_desc = dmaengine_prep_dma_cyclic(chan,
  1094. sg_dma_address(&sport->rx_sgl),
  1095. sport->rx_sgl.length,
  1096. sport->rx_sgl.length / 2,
  1097. DMA_DEV_TO_MEM,
  1098. DMA_PREP_INTERRUPT);
  1099. if (!sport->dma_rx_desc) {
  1100. dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
  1101. return -EFAULT;
  1102. }
  1103. sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
  1104. sport->dma_rx_desc->callback_param = sport;
  1105. sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
  1106. dma_async_issue_pending(chan);
  1107. if (lpuart_is_32(sport)) {
  1108. unsigned long temp = lpuart32_read(&sport->port, UARTBAUD);
  1109. lpuart32_write(&sport->port, temp | UARTBAUD_RDMAE, UARTBAUD);
  1110. } else {
  1111. writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
  1112. sport->port.membase + UARTCR5);
  1113. }
  1114. return 0;
  1115. }
  1116. static void lpuart_dma_rx_free(struct uart_port *port)
  1117. {
  1118. struct lpuart_port *sport = container_of(port,
  1119. struct lpuart_port, port);
  1120. struct dma_chan *chan = sport->dma_rx_chan;
  1121. dmaengine_terminate_sync(chan);
  1122. del_timer_sync(&sport->lpuart_timer);
  1123. dma_unmap_sg(chan->device->dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
  1124. kfree(sport->rx_ring.buf);
  1125. sport->rx_ring.tail = 0;
  1126. sport->rx_ring.head = 0;
  1127. sport->dma_rx_desc = NULL;
  1128. sport->dma_rx_cookie = -EINVAL;
  1129. }
  1130. static int lpuart_config_rs485(struct uart_port *port, struct ktermios *termios,
  1131. struct serial_rs485 *rs485)
  1132. {
  1133. struct lpuart_port *sport = container_of(port,
  1134. struct lpuart_port, port);
  1135. u8 modem = readb(sport->port.membase + UARTMODEM) &
  1136. ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
  1137. writeb(modem, sport->port.membase + UARTMODEM);
  1138. if (rs485->flags & SER_RS485_ENABLED) {
  1139. /* Enable auto RS-485 RTS mode */
  1140. modem |= UARTMODEM_TXRTSE;
  1141. /*
  1142. * The hardware defaults to RTS logic HIGH while transfer.
  1143. * Switch polarity in case RTS shall be logic HIGH
  1144. * after transfer.
  1145. * Note: UART is assumed to be active high.
  1146. */
  1147. if (rs485->flags & SER_RS485_RTS_ON_SEND)
  1148. modem |= UARTMODEM_TXRTSPOL;
  1149. else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  1150. modem &= ~UARTMODEM_TXRTSPOL;
  1151. }
  1152. writeb(modem, sport->port.membase + UARTMODEM);
  1153. return 0;
  1154. }
  1155. static int lpuart32_config_rs485(struct uart_port *port, struct ktermios *termios,
  1156. struct serial_rs485 *rs485)
  1157. {
  1158. struct lpuart_port *sport = container_of(port,
  1159. struct lpuart_port, port);
  1160. unsigned long modem = lpuart32_read(&sport->port, UARTMODIR)
  1161. & ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
  1162. lpuart32_write(&sport->port, modem, UARTMODIR);
  1163. if (rs485->flags & SER_RS485_ENABLED) {
  1164. /* Enable auto RS-485 RTS mode */
  1165. modem |= UARTMODEM_TXRTSE;
  1166. /*
  1167. * The hardware defaults to RTS logic HIGH while transfer.
  1168. * Switch polarity in case RTS shall be logic HIGH
  1169. * after transfer.
  1170. * Note: UART is assumed to be active high.
  1171. */
  1172. if (rs485->flags & SER_RS485_RTS_ON_SEND)
  1173. modem |= UARTMODEM_TXRTSPOL;
  1174. else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  1175. modem &= ~UARTMODEM_TXRTSPOL;
  1176. }
  1177. lpuart32_write(&sport->port, modem, UARTMODIR);
  1178. return 0;
  1179. }
  1180. static unsigned int lpuart_get_mctrl(struct uart_port *port)
  1181. {
  1182. unsigned int mctrl = 0;
  1183. u8 reg;
  1184. reg = readb(port->membase + UARTCR1);
  1185. if (reg & UARTCR1_LOOPS)
  1186. mctrl |= TIOCM_LOOP;
  1187. return mctrl;
  1188. }
  1189. static unsigned int lpuart32_get_mctrl(struct uart_port *port)
  1190. {
  1191. unsigned int mctrl = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  1192. u32 reg;
  1193. reg = lpuart32_read(port, UARTCTRL);
  1194. if (reg & UARTCTRL_LOOPS)
  1195. mctrl |= TIOCM_LOOP;
  1196. return mctrl;
  1197. }
  1198. static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1199. {
  1200. u8 reg;
  1201. reg = readb(port->membase + UARTCR1);
  1202. /* for internal loopback we need LOOPS=1 and RSRC=0 */
  1203. reg &= ~(UARTCR1_LOOPS | UARTCR1_RSRC);
  1204. if (mctrl & TIOCM_LOOP)
  1205. reg |= UARTCR1_LOOPS;
  1206. writeb(reg, port->membase + UARTCR1);
  1207. }
  1208. static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
  1209. {
  1210. u32 reg;
  1211. reg = lpuart32_read(port, UARTCTRL);
  1212. /* for internal loopback we need LOOPS=1 and RSRC=0 */
  1213. reg &= ~(UARTCTRL_LOOPS | UARTCTRL_RSRC);
  1214. if (mctrl & TIOCM_LOOP)
  1215. reg |= UARTCTRL_LOOPS;
  1216. lpuart32_write(port, reg, UARTCTRL);
  1217. }
  1218. static void lpuart_break_ctl(struct uart_port *port, int break_state)
  1219. {
  1220. unsigned char temp;
  1221. temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
  1222. if (break_state != 0)
  1223. temp |= UARTCR2_SBK;
  1224. writeb(temp, port->membase + UARTCR2);
  1225. }
  1226. static void lpuart32_break_ctl(struct uart_port *port, int break_state)
  1227. {
  1228. unsigned long temp;
  1229. temp = lpuart32_read(port, UARTCTRL);
  1230. /*
  1231. * LPUART IP now has two known bugs, one is CTS has higher priority than the
  1232. * break signal, which causes the break signal sending through UARTCTRL_SBK
  1233. * may impacted by the CTS input if the HW flow control is enabled. It
  1234. * exists on all platforms we support in this driver.
  1235. * Another bug is i.MX8QM LPUART may have an additional break character
  1236. * being sent after SBK was cleared.
  1237. * To avoid above two bugs, we use Transmit Data Inversion function to send
  1238. * the break signal instead of UARTCTRL_SBK.
  1239. */
  1240. if (break_state != 0) {
  1241. /*
  1242. * Disable the transmitter to prevent any data from being sent out
  1243. * during break, then invert the TX line to send break.
  1244. */
  1245. temp &= ~UARTCTRL_TE;
  1246. lpuart32_write(port, temp, UARTCTRL);
  1247. temp |= UARTCTRL_TXINV;
  1248. lpuart32_write(port, temp, UARTCTRL);
  1249. } else {
  1250. /* Disable the TXINV to turn off break and re-enable transmitter. */
  1251. temp &= ~UARTCTRL_TXINV;
  1252. lpuart32_write(port, temp, UARTCTRL);
  1253. temp |= UARTCTRL_TE;
  1254. lpuart32_write(port, temp, UARTCTRL);
  1255. }
  1256. }
  1257. static void lpuart_setup_watermark(struct lpuart_port *sport)
  1258. {
  1259. unsigned char val, cr2;
  1260. unsigned char cr2_saved;
  1261. cr2 = readb(sport->port.membase + UARTCR2);
  1262. cr2_saved = cr2;
  1263. cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
  1264. UARTCR2_RIE | UARTCR2_RE);
  1265. writeb(cr2, sport->port.membase + UARTCR2);
  1266. val = readb(sport->port.membase + UARTPFIFO);
  1267. writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
  1268. sport->port.membase + UARTPFIFO);
  1269. /* flush Tx and Rx FIFO */
  1270. writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
  1271. sport->port.membase + UARTCFIFO);
  1272. /* explicitly clear RDRF */
  1273. if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
  1274. readb(sport->port.membase + UARTDR);
  1275. writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
  1276. }
  1277. writeb(0, sport->port.membase + UARTTWFIFO);
  1278. writeb(sport->rx_watermark, sport->port.membase + UARTRWFIFO);
  1279. /* Restore cr2 */
  1280. writeb(cr2_saved, sport->port.membase + UARTCR2);
  1281. }
  1282. static void lpuart_setup_watermark_enable(struct lpuart_port *sport)
  1283. {
  1284. unsigned char cr2;
  1285. lpuart_setup_watermark(sport);
  1286. cr2 = readb(sport->port.membase + UARTCR2);
  1287. cr2 |= UARTCR2_RIE | UARTCR2_RE | UARTCR2_TE;
  1288. writeb(cr2, sport->port.membase + UARTCR2);
  1289. }
  1290. static void lpuart32_setup_watermark(struct lpuart_port *sport)
  1291. {
  1292. unsigned long val, ctrl;
  1293. unsigned long ctrl_saved;
  1294. ctrl = lpuart32_read(&sport->port, UARTCTRL);
  1295. ctrl_saved = ctrl;
  1296. ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
  1297. UARTCTRL_RIE | UARTCTRL_RE);
  1298. lpuart32_write(&sport->port, ctrl, UARTCTRL);
  1299. /* enable FIFO mode */
  1300. val = lpuart32_read(&sport->port, UARTFIFO);
  1301. val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
  1302. val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
  1303. lpuart32_write(&sport->port, val, UARTFIFO);
  1304. /* set the watermark */
  1305. val = (sport->rx_watermark << UARTWATER_RXWATER_OFF) |
  1306. (0x0 << UARTWATER_TXWATER_OFF);
  1307. lpuart32_write(&sport->port, val, UARTWATER);
  1308. /* Restore cr2 */
  1309. lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
  1310. }
  1311. static void lpuart32_setup_watermark_enable(struct lpuart_port *sport)
  1312. {
  1313. u32 temp;
  1314. lpuart32_setup_watermark(sport);
  1315. temp = lpuart32_read(&sport->port, UARTCTRL);
  1316. temp |= UARTCTRL_RE | UARTCTRL_TE | UARTCTRL_ILIE;
  1317. lpuart32_write(&sport->port, temp, UARTCTRL);
  1318. }
  1319. static void rx_dma_timer_init(struct lpuart_port *sport)
  1320. {
  1321. timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
  1322. sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
  1323. add_timer(&sport->lpuart_timer);
  1324. }
  1325. static void lpuart_request_dma(struct lpuart_port *sport)
  1326. {
  1327. sport->dma_tx_chan = dma_request_chan(sport->port.dev, "tx");
  1328. if (IS_ERR(sport->dma_tx_chan)) {
  1329. dev_dbg_once(sport->port.dev,
  1330. "DMA tx channel request failed, operating without tx DMA (%ld)\n",
  1331. PTR_ERR(sport->dma_tx_chan));
  1332. sport->dma_tx_chan = NULL;
  1333. }
  1334. sport->dma_rx_chan = dma_request_chan(sport->port.dev, "rx");
  1335. if (IS_ERR(sport->dma_rx_chan)) {
  1336. dev_dbg_once(sport->port.dev,
  1337. "DMA rx channel request failed, operating without rx DMA (%ld)\n",
  1338. PTR_ERR(sport->dma_rx_chan));
  1339. sport->dma_rx_chan = NULL;
  1340. }
  1341. }
  1342. static void lpuart_tx_dma_startup(struct lpuart_port *sport)
  1343. {
  1344. u32 uartbaud;
  1345. int ret;
  1346. if (uart_console(&sport->port))
  1347. goto err;
  1348. if (!sport->dma_tx_chan)
  1349. goto err;
  1350. ret = lpuart_dma_tx_request(&sport->port);
  1351. if (ret)
  1352. goto err;
  1353. init_waitqueue_head(&sport->dma_wait);
  1354. sport->lpuart_dma_tx_use = true;
  1355. if (lpuart_is_32(sport)) {
  1356. uartbaud = lpuart32_read(&sport->port, UARTBAUD);
  1357. lpuart32_write(&sport->port,
  1358. uartbaud | UARTBAUD_TDMAE, UARTBAUD);
  1359. } else {
  1360. writeb(readb(sport->port.membase + UARTCR5) |
  1361. UARTCR5_TDMAS, sport->port.membase + UARTCR5);
  1362. }
  1363. return;
  1364. err:
  1365. sport->lpuart_dma_tx_use = false;
  1366. }
  1367. static void lpuart_rx_dma_startup(struct lpuart_port *sport)
  1368. {
  1369. int ret;
  1370. unsigned char cr3;
  1371. if (uart_console(&sport->port))
  1372. goto err;
  1373. if (!sport->dma_rx_chan)
  1374. goto err;
  1375. ret = lpuart_start_rx_dma(sport);
  1376. if (ret)
  1377. goto err;
  1378. /* set Rx DMA timeout */
  1379. sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
  1380. if (!sport->dma_rx_timeout)
  1381. sport->dma_rx_timeout = 1;
  1382. sport->lpuart_dma_rx_use = true;
  1383. rx_dma_timer_init(sport);
  1384. if (sport->port.has_sysrq && !lpuart_is_32(sport)) {
  1385. cr3 = readb(sport->port.membase + UARTCR3);
  1386. cr3 |= UARTCR3_FEIE;
  1387. writeb(cr3, sport->port.membase + UARTCR3);
  1388. }
  1389. return;
  1390. err:
  1391. sport->lpuart_dma_rx_use = false;
  1392. }
  1393. static int lpuart_startup(struct uart_port *port)
  1394. {
  1395. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1396. unsigned long flags;
  1397. unsigned char temp;
  1398. /* determine FIFO size and enable FIFO mode */
  1399. temp = readb(sport->port.membase + UARTPFIFO);
  1400. sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_TXSIZE_OFF) &
  1401. UARTPFIFO_FIFOSIZE_MASK);
  1402. sport->port.fifosize = sport->txfifo_size;
  1403. sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTPFIFO_RXSIZE_OFF) &
  1404. UARTPFIFO_FIFOSIZE_MASK);
  1405. lpuart_request_dma(sport);
  1406. spin_lock_irqsave(&sport->port.lock, flags);
  1407. lpuart_setup_watermark_enable(sport);
  1408. lpuart_rx_dma_startup(sport);
  1409. lpuart_tx_dma_startup(sport);
  1410. spin_unlock_irqrestore(&sport->port.lock, flags);
  1411. return 0;
  1412. }
  1413. static void lpuart32_configure(struct lpuart_port *sport)
  1414. {
  1415. unsigned long temp;
  1416. temp = lpuart32_read(&sport->port, UARTCTRL);
  1417. if (!sport->lpuart_dma_rx_use)
  1418. temp |= UARTCTRL_RIE;
  1419. if (!sport->lpuart_dma_tx_use)
  1420. temp |= UARTCTRL_TIE;
  1421. lpuart32_write(&sport->port, temp, UARTCTRL);
  1422. }
  1423. static int lpuart32_startup(struct uart_port *port)
  1424. {
  1425. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1426. unsigned long flags;
  1427. unsigned long temp;
  1428. /* determine FIFO size */
  1429. temp = lpuart32_read(&sport->port, UARTFIFO);
  1430. sport->txfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_TXSIZE_OFF) &
  1431. UARTFIFO_FIFOSIZE_MASK);
  1432. sport->port.fifosize = sport->txfifo_size;
  1433. sport->rxfifo_size = UARTFIFO_DEPTH((temp >> UARTFIFO_RXSIZE_OFF) &
  1434. UARTFIFO_FIFOSIZE_MASK);
  1435. /*
  1436. * The LS1021A and LS1028A have a fixed FIFO depth of 16 words.
  1437. * Although they support the RX/TXSIZE fields, their encoding is
  1438. * different. Eg the reference manual states 0b101 is 16 words.
  1439. */
  1440. if (is_layerscape_lpuart(sport)) {
  1441. sport->rxfifo_size = 16;
  1442. sport->txfifo_size = 16;
  1443. sport->port.fifosize = sport->txfifo_size;
  1444. }
  1445. lpuart_request_dma(sport);
  1446. spin_lock_irqsave(&sport->port.lock, flags);
  1447. lpuart32_setup_watermark_enable(sport);
  1448. lpuart_rx_dma_startup(sport);
  1449. lpuart_tx_dma_startup(sport);
  1450. lpuart32_configure(sport);
  1451. spin_unlock_irqrestore(&sport->port.lock, flags);
  1452. return 0;
  1453. }
  1454. static void lpuart_dma_shutdown(struct lpuart_port *sport)
  1455. {
  1456. if (sport->lpuart_dma_rx_use) {
  1457. lpuart_dma_rx_free(&sport->port);
  1458. sport->lpuart_dma_rx_use = false;
  1459. }
  1460. if (sport->lpuart_dma_tx_use) {
  1461. if (wait_event_interruptible_timeout(sport->dma_wait,
  1462. !sport->dma_tx_in_progress, msecs_to_jiffies(300)) <= 0) {
  1463. sport->dma_tx_in_progress = false;
  1464. dmaengine_terminate_sync(sport->dma_tx_chan);
  1465. }
  1466. sport->lpuart_dma_tx_use = false;
  1467. }
  1468. if (sport->dma_tx_chan)
  1469. dma_release_channel(sport->dma_tx_chan);
  1470. if (sport->dma_rx_chan)
  1471. dma_release_channel(sport->dma_rx_chan);
  1472. }
  1473. static void lpuart_shutdown(struct uart_port *port)
  1474. {
  1475. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1476. unsigned char temp;
  1477. unsigned long flags;
  1478. spin_lock_irqsave(&port->lock, flags);
  1479. /* disable Rx/Tx and interrupts */
  1480. temp = readb(port->membase + UARTCR2);
  1481. temp &= ~(UARTCR2_TE | UARTCR2_RE |
  1482. UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
  1483. writeb(temp, port->membase + UARTCR2);
  1484. spin_unlock_irqrestore(&port->lock, flags);
  1485. lpuart_dma_shutdown(sport);
  1486. }
  1487. static void lpuart32_shutdown(struct uart_port *port)
  1488. {
  1489. struct lpuart_port *sport =
  1490. container_of(port, struct lpuart_port, port);
  1491. unsigned long temp;
  1492. unsigned long flags;
  1493. spin_lock_irqsave(&port->lock, flags);
  1494. /* clear status */
  1495. temp = lpuart32_read(&sport->port, UARTSTAT);
  1496. lpuart32_write(&sport->port, temp, UARTSTAT);
  1497. /* disable Rx/Tx DMA */
  1498. temp = lpuart32_read(port, UARTBAUD);
  1499. temp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
  1500. lpuart32_write(port, temp, UARTBAUD);
  1501. /* disable Rx/Tx and interrupts */
  1502. temp = lpuart32_read(port, UARTCTRL);
  1503. temp &= ~(UARTCTRL_TE | UARTCTRL_RE |
  1504. UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
  1505. lpuart32_write(port, temp, UARTCTRL);
  1506. spin_unlock_irqrestore(&port->lock, flags);
  1507. lpuart_dma_shutdown(sport);
  1508. }
  1509. static void
  1510. lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
  1511. const struct ktermios *old)
  1512. {
  1513. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1514. unsigned long flags;
  1515. unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
  1516. unsigned int baud;
  1517. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1518. unsigned int sbr, brfa;
  1519. cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
  1520. old_cr2 = readb(sport->port.membase + UARTCR2);
  1521. cr3 = readb(sport->port.membase + UARTCR3);
  1522. cr4 = readb(sport->port.membase + UARTCR4);
  1523. bdh = readb(sport->port.membase + UARTBDH);
  1524. modem = readb(sport->port.membase + UARTMODEM);
  1525. /*
  1526. * only support CS8 and CS7, and for CS7 must enable PE.
  1527. * supported mode:
  1528. * - (7,e/o,1)
  1529. * - (8,n,1)
  1530. * - (8,m/s,1)
  1531. * - (8,e/o,1)
  1532. */
  1533. while ((termios->c_cflag & CSIZE) != CS8 &&
  1534. (termios->c_cflag & CSIZE) != CS7) {
  1535. termios->c_cflag &= ~CSIZE;
  1536. termios->c_cflag |= old_csize;
  1537. old_csize = CS8;
  1538. }
  1539. if ((termios->c_cflag & CSIZE) == CS8 ||
  1540. (termios->c_cflag & CSIZE) == CS7)
  1541. cr1 = old_cr1 & ~UARTCR1_M;
  1542. if (termios->c_cflag & CMSPAR) {
  1543. if ((termios->c_cflag & CSIZE) != CS8) {
  1544. termios->c_cflag &= ~CSIZE;
  1545. termios->c_cflag |= CS8;
  1546. }
  1547. cr1 |= UARTCR1_M;
  1548. }
  1549. /*
  1550. * When auto RS-485 RTS mode is enabled,
  1551. * hardware flow control need to be disabled.
  1552. */
  1553. if (sport->port.rs485.flags & SER_RS485_ENABLED)
  1554. termios->c_cflag &= ~CRTSCTS;
  1555. if (termios->c_cflag & CRTSCTS)
  1556. modem |= UARTMODEM_RXRTSE | UARTMODEM_TXCTSE;
  1557. else
  1558. modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
  1559. termios->c_cflag &= ~CSTOPB;
  1560. /* parity must be enabled when CS7 to match 8-bits format */
  1561. if ((termios->c_cflag & CSIZE) == CS7)
  1562. termios->c_cflag |= PARENB;
  1563. if (termios->c_cflag & PARENB) {
  1564. if (termios->c_cflag & CMSPAR) {
  1565. cr1 &= ~UARTCR1_PE;
  1566. if (termios->c_cflag & PARODD)
  1567. cr3 |= UARTCR3_T8;
  1568. else
  1569. cr3 &= ~UARTCR3_T8;
  1570. } else {
  1571. cr1 |= UARTCR1_PE;
  1572. if ((termios->c_cflag & CSIZE) == CS8)
  1573. cr1 |= UARTCR1_M;
  1574. if (termios->c_cflag & PARODD)
  1575. cr1 |= UARTCR1_PT;
  1576. else
  1577. cr1 &= ~UARTCR1_PT;
  1578. }
  1579. } else {
  1580. cr1 &= ~UARTCR1_PE;
  1581. }
  1582. /* ask the core to calculate the divisor */
  1583. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1584. /*
  1585. * Need to update the Ring buffer length according to the selected
  1586. * baud rate and restart Rx DMA path.
  1587. *
  1588. * Since timer function acqures sport->port.lock, need to stop before
  1589. * acquring same lock because otherwise del_timer_sync() can deadlock.
  1590. */
  1591. if (old && sport->lpuart_dma_rx_use)
  1592. lpuart_dma_rx_free(&sport->port);
  1593. spin_lock_irqsave(&sport->port.lock, flags);
  1594. sport->port.read_status_mask = 0;
  1595. if (termios->c_iflag & INPCK)
  1596. sport->port.read_status_mask |= UARTSR1_FE | UARTSR1_PE;
  1597. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1598. sport->port.read_status_mask |= UARTSR1_FE;
  1599. /* characters to ignore */
  1600. sport->port.ignore_status_mask = 0;
  1601. if (termios->c_iflag & IGNPAR)
  1602. sport->port.ignore_status_mask |= UARTSR1_PE;
  1603. if (termios->c_iflag & IGNBRK) {
  1604. sport->port.ignore_status_mask |= UARTSR1_FE;
  1605. /*
  1606. * if we're ignoring parity and break indicators,
  1607. * ignore overruns too (for real raw support).
  1608. */
  1609. if (termios->c_iflag & IGNPAR)
  1610. sport->port.ignore_status_mask |= UARTSR1_OR;
  1611. }
  1612. /* update the per-port timeout */
  1613. uart_update_timeout(port, termios->c_cflag, baud);
  1614. /* wait transmit engin complete */
  1615. lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
  1616. /* disable transmit and receive */
  1617. writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
  1618. sport->port.membase + UARTCR2);
  1619. sbr = sport->port.uartclk / (16 * baud);
  1620. brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
  1621. bdh &= ~UARTBDH_SBR_MASK;
  1622. bdh |= (sbr >> 8) & 0x1F;
  1623. cr4 &= ~UARTCR4_BRFA_MASK;
  1624. brfa &= UARTCR4_BRFA_MASK;
  1625. writeb(cr4 | brfa, sport->port.membase + UARTCR4);
  1626. writeb(bdh, sport->port.membase + UARTBDH);
  1627. writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
  1628. writeb(cr3, sport->port.membase + UARTCR3);
  1629. writeb(cr1, sport->port.membase + UARTCR1);
  1630. writeb(modem, sport->port.membase + UARTMODEM);
  1631. /* restore control register */
  1632. writeb(old_cr2, sport->port.membase + UARTCR2);
  1633. if (old && sport->lpuart_dma_rx_use) {
  1634. if (!lpuart_start_rx_dma(sport))
  1635. rx_dma_timer_init(sport);
  1636. else
  1637. sport->lpuart_dma_rx_use = false;
  1638. }
  1639. spin_unlock_irqrestore(&sport->port.lock, flags);
  1640. }
  1641. static void __lpuart32_serial_setbrg(struct uart_port *port,
  1642. unsigned int baudrate, bool use_rx_dma,
  1643. bool use_tx_dma)
  1644. {
  1645. u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
  1646. u32 clk = port->uartclk;
  1647. /*
  1648. * The idea is to use the best OSR (over-sampling rate) possible.
  1649. * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
  1650. * Loop to find the best OSR value possible, one that generates minimum
  1651. * baud_diff iterate through the rest of the supported values of OSR.
  1652. *
  1653. * Calculation Formula:
  1654. * Baud Rate = baud clock / ((OSR+1) × SBR)
  1655. */
  1656. baud_diff = baudrate;
  1657. osr = 0;
  1658. sbr = 0;
  1659. for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
  1660. /* calculate the temporary sbr value */
  1661. tmp_sbr = (clk / (baudrate * tmp_osr));
  1662. if (tmp_sbr == 0)
  1663. tmp_sbr = 1;
  1664. /*
  1665. * calculate the baud rate difference based on the temporary
  1666. * osr and sbr values
  1667. */
  1668. tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
  1669. /* select best values between sbr and sbr+1 */
  1670. tmp = clk / (tmp_osr * (tmp_sbr + 1));
  1671. if (tmp_diff > (baudrate - tmp)) {
  1672. tmp_diff = baudrate - tmp;
  1673. tmp_sbr++;
  1674. }
  1675. if (tmp_sbr > UARTBAUD_SBR_MASK)
  1676. continue;
  1677. if (tmp_diff <= baud_diff) {
  1678. baud_diff = tmp_diff;
  1679. osr = tmp_osr;
  1680. sbr = tmp_sbr;
  1681. if (!baud_diff)
  1682. break;
  1683. }
  1684. }
  1685. /* handle buadrate outside acceptable rate */
  1686. if (baud_diff > ((baudrate / 100) * 3))
  1687. dev_warn(port->dev,
  1688. "unacceptable baud rate difference of more than 3%%\n");
  1689. tmp = lpuart32_read(port, UARTBAUD);
  1690. if ((osr > 3) && (osr < 8))
  1691. tmp |= UARTBAUD_BOTHEDGE;
  1692. tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
  1693. tmp |= ((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT;
  1694. tmp &= ~UARTBAUD_SBR_MASK;
  1695. tmp |= sbr & UARTBAUD_SBR_MASK;
  1696. if (!use_rx_dma)
  1697. tmp &= ~UARTBAUD_RDMAE;
  1698. if (!use_tx_dma)
  1699. tmp &= ~UARTBAUD_TDMAE;
  1700. lpuart32_write(port, tmp, UARTBAUD);
  1701. }
  1702. static void lpuart32_serial_setbrg(struct lpuart_port *sport,
  1703. unsigned int baudrate)
  1704. {
  1705. __lpuart32_serial_setbrg(&sport->port, baudrate,
  1706. sport->lpuart_dma_rx_use,
  1707. sport->lpuart_dma_tx_use);
  1708. }
  1709. static void
  1710. lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
  1711. const struct ktermios *old)
  1712. {
  1713. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1714. unsigned long flags;
  1715. unsigned long ctrl, old_ctrl, bd, modem;
  1716. unsigned int baud;
  1717. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1718. ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
  1719. bd = lpuart32_read(&sport->port, UARTBAUD);
  1720. modem = lpuart32_read(&sport->port, UARTMODIR);
  1721. sport->is_cs7 = false;
  1722. /*
  1723. * only support CS8 and CS7, and for CS7 must enable PE.
  1724. * supported mode:
  1725. * - (7,e/o,1)
  1726. * - (8,n,1)
  1727. * - (8,m/s,1)
  1728. * - (8,e/o,1)
  1729. */
  1730. while ((termios->c_cflag & CSIZE) != CS8 &&
  1731. (termios->c_cflag & CSIZE) != CS7) {
  1732. termios->c_cflag &= ~CSIZE;
  1733. termios->c_cflag |= old_csize;
  1734. old_csize = CS8;
  1735. }
  1736. if ((termios->c_cflag & CSIZE) == CS8 ||
  1737. (termios->c_cflag & CSIZE) == CS7)
  1738. ctrl = old_ctrl & ~UARTCTRL_M;
  1739. if (termios->c_cflag & CMSPAR) {
  1740. if ((termios->c_cflag & CSIZE) != CS8) {
  1741. termios->c_cflag &= ~CSIZE;
  1742. termios->c_cflag |= CS8;
  1743. }
  1744. ctrl |= UARTCTRL_M;
  1745. }
  1746. /*
  1747. * When auto RS-485 RTS mode is enabled,
  1748. * hardware flow control need to be disabled.
  1749. */
  1750. if (sport->port.rs485.flags & SER_RS485_ENABLED)
  1751. termios->c_cflag &= ~CRTSCTS;
  1752. if (termios->c_cflag & CRTSCTS)
  1753. modem |= UARTMODIR_RXRTSE | UARTMODIR_TXCTSE;
  1754. else
  1755. modem &= ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
  1756. if (termios->c_cflag & CSTOPB)
  1757. bd |= UARTBAUD_SBNS;
  1758. else
  1759. bd &= ~UARTBAUD_SBNS;
  1760. /* parity must be enabled when CS7 to match 8-bits format */
  1761. if ((termios->c_cflag & CSIZE) == CS7)
  1762. termios->c_cflag |= PARENB;
  1763. if ((termios->c_cflag & PARENB)) {
  1764. if (termios->c_cflag & CMSPAR) {
  1765. ctrl &= ~UARTCTRL_PE;
  1766. ctrl |= UARTCTRL_M;
  1767. } else {
  1768. ctrl |= UARTCTRL_PE;
  1769. if ((termios->c_cflag & CSIZE) == CS8)
  1770. ctrl |= UARTCTRL_M;
  1771. if (termios->c_cflag & PARODD)
  1772. ctrl |= UARTCTRL_PT;
  1773. else
  1774. ctrl &= ~UARTCTRL_PT;
  1775. }
  1776. } else {
  1777. ctrl &= ~UARTCTRL_PE;
  1778. }
  1779. /* ask the core to calculate the divisor */
  1780. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 4);
  1781. /*
  1782. * Need to update the Ring buffer length according to the selected
  1783. * baud rate and restart Rx DMA path.
  1784. *
  1785. * Since timer function acqures sport->port.lock, need to stop before
  1786. * acquring same lock because otherwise del_timer_sync() can deadlock.
  1787. */
  1788. if (old && sport->lpuart_dma_rx_use)
  1789. lpuart_dma_rx_free(&sport->port);
  1790. spin_lock_irqsave(&sport->port.lock, flags);
  1791. sport->port.read_status_mask = 0;
  1792. if (termios->c_iflag & INPCK)
  1793. sport->port.read_status_mask |= UARTSTAT_FE | UARTSTAT_PE;
  1794. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1795. sport->port.read_status_mask |= UARTSTAT_FE;
  1796. /* characters to ignore */
  1797. sport->port.ignore_status_mask = 0;
  1798. if (termios->c_iflag & IGNPAR)
  1799. sport->port.ignore_status_mask |= UARTSTAT_PE;
  1800. if (termios->c_iflag & IGNBRK) {
  1801. sport->port.ignore_status_mask |= UARTSTAT_FE;
  1802. /*
  1803. * if we're ignoring parity and break indicators,
  1804. * ignore overruns too (for real raw support).
  1805. */
  1806. if (termios->c_iflag & IGNPAR)
  1807. sport->port.ignore_status_mask |= UARTSTAT_OR;
  1808. }
  1809. /* update the per-port timeout */
  1810. uart_update_timeout(port, termios->c_cflag, baud);
  1811. /*
  1812. * LPUART Transmission Complete Flag may never be set while queuing a break
  1813. * character, so skip waiting for transmission complete when UARTCTRL_SBK is
  1814. * asserted.
  1815. */
  1816. if (!(old_ctrl & UARTCTRL_SBK)) {
  1817. lpuart32_write(&sport->port, 0, UARTMODIR);
  1818. lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
  1819. }
  1820. /* disable transmit and receive */
  1821. lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
  1822. UARTCTRL);
  1823. lpuart32_write(&sport->port, bd, UARTBAUD);
  1824. lpuart32_serial_setbrg(sport, baud);
  1825. lpuart32_write(&sport->port, modem, UARTMODIR);
  1826. lpuart32_write(&sport->port, ctrl, UARTCTRL);
  1827. /* restore control register */
  1828. if ((ctrl & (UARTCTRL_PE | UARTCTRL_M)) == UARTCTRL_PE)
  1829. sport->is_cs7 = true;
  1830. if (old && sport->lpuart_dma_rx_use) {
  1831. if (!lpuart_start_rx_dma(sport))
  1832. rx_dma_timer_init(sport);
  1833. else
  1834. sport->lpuart_dma_rx_use = false;
  1835. }
  1836. spin_unlock_irqrestore(&sport->port.lock, flags);
  1837. }
  1838. static const char *lpuart_type(struct uart_port *port)
  1839. {
  1840. return "FSL_LPUART";
  1841. }
  1842. static void lpuart_release_port(struct uart_port *port)
  1843. {
  1844. /* nothing to do */
  1845. }
  1846. static int lpuart_request_port(struct uart_port *port)
  1847. {
  1848. return 0;
  1849. }
  1850. /* configure/autoconfigure the port */
  1851. static void lpuart_config_port(struct uart_port *port, int flags)
  1852. {
  1853. if (flags & UART_CONFIG_TYPE)
  1854. port->type = PORT_LPUART;
  1855. }
  1856. static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1857. {
  1858. int ret = 0;
  1859. if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
  1860. ret = -EINVAL;
  1861. if (port->irq != ser->irq)
  1862. ret = -EINVAL;
  1863. if (ser->io_type != UPIO_MEM)
  1864. ret = -EINVAL;
  1865. if (port->uartclk / 16 != ser->baud_base)
  1866. ret = -EINVAL;
  1867. if (port->iobase != ser->port)
  1868. ret = -EINVAL;
  1869. if (ser->hub6 != 0)
  1870. ret = -EINVAL;
  1871. return ret;
  1872. }
  1873. static const struct uart_ops lpuart_pops = {
  1874. .tx_empty = lpuart_tx_empty,
  1875. .set_mctrl = lpuart_set_mctrl,
  1876. .get_mctrl = lpuart_get_mctrl,
  1877. .stop_tx = lpuart_stop_tx,
  1878. .start_tx = lpuart_start_tx,
  1879. .stop_rx = lpuart_stop_rx,
  1880. .break_ctl = lpuart_break_ctl,
  1881. .startup = lpuart_startup,
  1882. .shutdown = lpuart_shutdown,
  1883. .set_termios = lpuart_set_termios,
  1884. .type = lpuart_type,
  1885. .request_port = lpuart_request_port,
  1886. .release_port = lpuart_release_port,
  1887. .config_port = lpuart_config_port,
  1888. .verify_port = lpuart_verify_port,
  1889. .flush_buffer = lpuart_flush_buffer,
  1890. #if defined(CONFIG_CONSOLE_POLL)
  1891. .poll_init = lpuart_poll_init,
  1892. .poll_get_char = lpuart_poll_get_char,
  1893. .poll_put_char = lpuart_poll_put_char,
  1894. #endif
  1895. };
  1896. static const struct uart_ops lpuart32_pops = {
  1897. .tx_empty = lpuart32_tx_empty,
  1898. .set_mctrl = lpuart32_set_mctrl,
  1899. .get_mctrl = lpuart32_get_mctrl,
  1900. .stop_tx = lpuart32_stop_tx,
  1901. .start_tx = lpuart32_start_tx,
  1902. .stop_rx = lpuart32_stop_rx,
  1903. .break_ctl = lpuart32_break_ctl,
  1904. .startup = lpuart32_startup,
  1905. .shutdown = lpuart32_shutdown,
  1906. .set_termios = lpuart32_set_termios,
  1907. .type = lpuart_type,
  1908. .request_port = lpuart_request_port,
  1909. .release_port = lpuart_release_port,
  1910. .config_port = lpuart_config_port,
  1911. .verify_port = lpuart_verify_port,
  1912. .flush_buffer = lpuart_flush_buffer,
  1913. #if defined(CONFIG_CONSOLE_POLL)
  1914. .poll_init = lpuart32_poll_init,
  1915. .poll_get_char = lpuart32_poll_get_char,
  1916. .poll_put_char = lpuart32_poll_put_char,
  1917. #endif
  1918. };
  1919. static struct lpuart_port *lpuart_ports[UART_NR];
  1920. #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
  1921. static void lpuart_console_putchar(struct uart_port *port, unsigned char ch)
  1922. {
  1923. lpuart_wait_bit_set(port, UARTSR1, UARTSR1_TDRE);
  1924. writeb(ch, port->membase + UARTDR);
  1925. }
  1926. static void lpuart32_console_putchar(struct uart_port *port, unsigned char ch)
  1927. {
  1928. lpuart32_wait_bit_set(port, UARTSTAT, UARTSTAT_TDRE);
  1929. lpuart32_write(port, ch, UARTDATA);
  1930. }
  1931. static void
  1932. lpuart_console_write(struct console *co, const char *s, unsigned int count)
  1933. {
  1934. struct lpuart_port *sport = lpuart_ports[co->index];
  1935. unsigned char old_cr2, cr2;
  1936. unsigned long flags;
  1937. int locked = 1;
  1938. if (oops_in_progress)
  1939. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1940. else
  1941. spin_lock_irqsave(&sport->port.lock, flags);
  1942. /* first save CR2 and then disable interrupts */
  1943. cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
  1944. cr2 |= UARTCR2_TE | UARTCR2_RE;
  1945. cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
  1946. writeb(cr2, sport->port.membase + UARTCR2);
  1947. uart_console_write(&sport->port, s, count, lpuart_console_putchar);
  1948. /* wait for transmitter finish complete and restore CR2 */
  1949. lpuart_wait_bit_set(&sport->port, UARTSR1, UARTSR1_TC);
  1950. writeb(old_cr2, sport->port.membase + UARTCR2);
  1951. if (locked)
  1952. spin_unlock_irqrestore(&sport->port.lock, flags);
  1953. }
  1954. static void
  1955. lpuart32_console_write(struct console *co, const char *s, unsigned int count)
  1956. {
  1957. struct lpuart_port *sport = lpuart_ports[co->index];
  1958. unsigned long old_cr, cr;
  1959. unsigned long flags;
  1960. int locked = 1;
  1961. if (oops_in_progress)
  1962. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1963. else
  1964. spin_lock_irqsave(&sport->port.lock, flags);
  1965. /* first save CR2 and then disable interrupts */
  1966. cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
  1967. cr |= UARTCTRL_TE | UARTCTRL_RE;
  1968. cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
  1969. lpuart32_write(&sport->port, cr, UARTCTRL);
  1970. uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
  1971. /* wait for transmitter finish complete and restore CR2 */
  1972. lpuart32_wait_bit_set(&sport->port, UARTSTAT, UARTSTAT_TC);
  1973. lpuart32_write(&sport->port, old_cr, UARTCTRL);
  1974. if (locked)
  1975. spin_unlock_irqrestore(&sport->port.lock, flags);
  1976. }
  1977. /*
  1978. * if the port was already initialised (eg, by a boot loader),
  1979. * try to determine the current setup.
  1980. */
  1981. static void __init
  1982. lpuart_console_get_options(struct lpuart_port *sport, int *baud,
  1983. int *parity, int *bits)
  1984. {
  1985. unsigned char cr, bdh, bdl, brfa;
  1986. unsigned int sbr, uartclk, baud_raw;
  1987. cr = readb(sport->port.membase + UARTCR2);
  1988. cr &= UARTCR2_TE | UARTCR2_RE;
  1989. if (!cr)
  1990. return;
  1991. /* ok, the port was enabled */
  1992. cr = readb(sport->port.membase + UARTCR1);
  1993. *parity = 'n';
  1994. if (cr & UARTCR1_PE) {
  1995. if (cr & UARTCR1_PT)
  1996. *parity = 'o';
  1997. else
  1998. *parity = 'e';
  1999. }
  2000. if (cr & UARTCR1_M)
  2001. *bits = 9;
  2002. else
  2003. *bits = 8;
  2004. bdh = readb(sport->port.membase + UARTBDH);
  2005. bdh &= UARTBDH_SBR_MASK;
  2006. bdl = readb(sport->port.membase + UARTBDL);
  2007. sbr = bdh;
  2008. sbr <<= 8;
  2009. sbr |= bdl;
  2010. brfa = readb(sport->port.membase + UARTCR4);
  2011. brfa &= UARTCR4_BRFA_MASK;
  2012. uartclk = lpuart_get_baud_clk_rate(sport);
  2013. /*
  2014. * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
  2015. */
  2016. baud_raw = uartclk / (16 * (sbr + brfa / 32));
  2017. if (*baud != baud_raw)
  2018. dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
  2019. "from %d to %d\n", baud_raw, *baud);
  2020. }
  2021. static void __init
  2022. lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
  2023. int *parity, int *bits)
  2024. {
  2025. unsigned long cr, bd;
  2026. unsigned int sbr, uartclk, baud_raw;
  2027. cr = lpuart32_read(&sport->port, UARTCTRL);
  2028. cr &= UARTCTRL_TE | UARTCTRL_RE;
  2029. if (!cr)
  2030. return;
  2031. /* ok, the port was enabled */
  2032. cr = lpuart32_read(&sport->port, UARTCTRL);
  2033. *parity = 'n';
  2034. if (cr & UARTCTRL_PE) {
  2035. if (cr & UARTCTRL_PT)
  2036. *parity = 'o';
  2037. else
  2038. *parity = 'e';
  2039. }
  2040. if (cr & UARTCTRL_M)
  2041. *bits = 9;
  2042. else
  2043. *bits = 8;
  2044. bd = lpuart32_read(&sport->port, UARTBAUD);
  2045. bd &= UARTBAUD_SBR_MASK;
  2046. if (!bd)
  2047. return;
  2048. sbr = bd;
  2049. uartclk = lpuart_get_baud_clk_rate(sport);
  2050. /*
  2051. * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
  2052. */
  2053. baud_raw = uartclk / (16 * sbr);
  2054. if (*baud != baud_raw)
  2055. dev_info(sport->port.dev, "Serial: Console lpuart rounded baud rate"
  2056. "from %d to %d\n", baud_raw, *baud);
  2057. }
  2058. static int __init lpuart_console_setup(struct console *co, char *options)
  2059. {
  2060. struct lpuart_port *sport;
  2061. int baud = 115200;
  2062. int bits = 8;
  2063. int parity = 'n';
  2064. int flow = 'n';
  2065. /*
  2066. * check whether an invalid uart number has been specified, and
  2067. * if so, search for the first available port that does have
  2068. * console support.
  2069. */
  2070. if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
  2071. co->index = 0;
  2072. sport = lpuart_ports[co->index];
  2073. if (sport == NULL)
  2074. return -ENODEV;
  2075. if (options)
  2076. uart_parse_options(options, &baud, &parity, &bits, &flow);
  2077. else
  2078. if (lpuart_is_32(sport))
  2079. lpuart32_console_get_options(sport, &baud, &parity, &bits);
  2080. else
  2081. lpuart_console_get_options(sport, &baud, &parity, &bits);
  2082. if (lpuart_is_32(sport))
  2083. lpuart32_setup_watermark(sport);
  2084. else
  2085. lpuart_setup_watermark(sport);
  2086. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  2087. }
  2088. static struct uart_driver lpuart_reg;
  2089. static struct console lpuart_console = {
  2090. .name = DEV_NAME,
  2091. .write = lpuart_console_write,
  2092. .device = uart_console_device,
  2093. .setup = lpuart_console_setup,
  2094. .flags = CON_PRINTBUFFER,
  2095. .index = -1,
  2096. .data = &lpuart_reg,
  2097. };
  2098. static struct console lpuart32_console = {
  2099. .name = DEV_NAME,
  2100. .write = lpuart32_console_write,
  2101. .device = uart_console_device,
  2102. .setup = lpuart_console_setup,
  2103. .flags = CON_PRINTBUFFER,
  2104. .index = -1,
  2105. .data = &lpuart_reg,
  2106. };
  2107. static void lpuart_early_write(struct console *con, const char *s, unsigned n)
  2108. {
  2109. struct earlycon_device *dev = con->data;
  2110. uart_console_write(&dev->port, s, n, lpuart_console_putchar);
  2111. }
  2112. static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
  2113. {
  2114. struct earlycon_device *dev = con->data;
  2115. uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
  2116. }
  2117. static int __init lpuart_early_console_setup(struct earlycon_device *device,
  2118. const char *opt)
  2119. {
  2120. if (!device->port.membase)
  2121. return -ENODEV;
  2122. device->con->write = lpuart_early_write;
  2123. return 0;
  2124. }
  2125. static int __init lpuart32_early_console_setup(struct earlycon_device *device,
  2126. const char *opt)
  2127. {
  2128. if (!device->port.membase)
  2129. return -ENODEV;
  2130. if (device->port.iotype != UPIO_MEM32)
  2131. device->port.iotype = UPIO_MEM32BE;
  2132. device->con->write = lpuart32_early_write;
  2133. return 0;
  2134. }
  2135. static int __init ls1028a_early_console_setup(struct earlycon_device *device,
  2136. const char *opt)
  2137. {
  2138. u32 cr;
  2139. if (!device->port.membase)
  2140. return -ENODEV;
  2141. device->port.iotype = UPIO_MEM32;
  2142. device->con->write = lpuart32_early_write;
  2143. /* set the baudrate */
  2144. if (device->port.uartclk && device->baud)
  2145. __lpuart32_serial_setbrg(&device->port, device->baud,
  2146. false, false);
  2147. /* enable transmitter */
  2148. cr = lpuart32_read(&device->port, UARTCTRL);
  2149. cr |= UARTCTRL_TE;
  2150. lpuart32_write(&device->port, cr, UARTCTRL);
  2151. return 0;
  2152. }
  2153. static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
  2154. const char *opt)
  2155. {
  2156. if (!device->port.membase)
  2157. return -ENODEV;
  2158. device->port.iotype = UPIO_MEM32;
  2159. device->port.membase += IMX_REG_OFF;
  2160. device->con->write = lpuart32_early_write;
  2161. return 0;
  2162. }
  2163. OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
  2164. OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
  2165. OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1028a-lpuart", ls1028a_early_console_setup);
  2166. OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
  2167. OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8ulp-lpuart", lpuart32_imx_early_console_setup);
  2168. OF_EARLYCON_DECLARE(lpuart32, "fsl,imx8qxp-lpuart", lpuart32_imx_early_console_setup);
  2169. OF_EARLYCON_DECLARE(lpuart32, "fsl,imxrt1050-lpuart", lpuart32_imx_early_console_setup);
  2170. EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
  2171. EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
  2172. #define LPUART_CONSOLE (&lpuart_console)
  2173. #define LPUART32_CONSOLE (&lpuart32_console)
  2174. #else
  2175. #define LPUART_CONSOLE NULL
  2176. #define LPUART32_CONSOLE NULL
  2177. #endif
  2178. static struct uart_driver lpuart_reg = {
  2179. .owner = THIS_MODULE,
  2180. .driver_name = DRIVER_NAME,
  2181. .dev_name = DEV_NAME,
  2182. .nr = ARRAY_SIZE(lpuart_ports),
  2183. .cons = LPUART_CONSOLE,
  2184. };
  2185. static const struct serial_rs485 lpuart_rs485_supported = {
  2186. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND,
  2187. /* delay_rts_* and RX_DURING_TX are not supported */
  2188. };
  2189. static int lpuart_global_reset(struct lpuart_port *sport)
  2190. {
  2191. struct uart_port *port = &sport->port;
  2192. void __iomem *global_addr;
  2193. unsigned long ctrl, bd;
  2194. unsigned int val = 0;
  2195. int ret;
  2196. ret = clk_prepare_enable(sport->ipg_clk);
  2197. if (ret) {
  2198. dev_err(sport->port.dev, "failed to enable uart ipg clk: %d\n", ret);
  2199. return ret;
  2200. }
  2201. if (is_imx7ulp_lpuart(sport) || is_imx8qxp_lpuart(sport)) {
  2202. /*
  2203. * If the transmitter is used by earlycon, wait for transmit engine to
  2204. * complete and then reset.
  2205. */
  2206. ctrl = lpuart32_read(port, UARTCTRL);
  2207. if (ctrl & UARTCTRL_TE) {
  2208. bd = lpuart32_read(&sport->port, UARTBAUD);
  2209. if (read_poll_timeout(lpuart32_tx_empty, val, val, 1, 100000, false,
  2210. port)) {
  2211. dev_warn(sport->port.dev,
  2212. "timeout waiting for transmit engine to complete\n");
  2213. clk_disable_unprepare(sport->ipg_clk);
  2214. return 0;
  2215. }
  2216. }
  2217. global_addr = port->membase + UART_GLOBAL - IMX_REG_OFF;
  2218. writel(UART_GLOBAL_RST, global_addr);
  2219. usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
  2220. writel(0, global_addr);
  2221. usleep_range(GLOBAL_RST_MIN_US, GLOBAL_RST_MAX_US);
  2222. /* Recover the transmitter for earlycon. */
  2223. if (ctrl & UARTCTRL_TE) {
  2224. lpuart32_write(port, bd, UARTBAUD);
  2225. lpuart32_write(port, ctrl, UARTCTRL);
  2226. }
  2227. }
  2228. clk_disable_unprepare(sport->ipg_clk);
  2229. return 0;
  2230. }
  2231. static int lpuart_probe(struct platform_device *pdev)
  2232. {
  2233. const struct lpuart_soc_data *sdata = of_device_get_match_data(&pdev->dev);
  2234. struct device_node *np = pdev->dev.of_node;
  2235. struct lpuart_port *sport;
  2236. struct resource *res;
  2237. irq_handler_t handler;
  2238. int ret;
  2239. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  2240. if (!sport)
  2241. return -ENOMEM;
  2242. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  2243. sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
  2244. if (IS_ERR(sport->port.membase))
  2245. return PTR_ERR(sport->port.membase);
  2246. sport->port.membase += sdata->reg_off;
  2247. sport->port.mapbase = res->start + sdata->reg_off;
  2248. sport->port.dev = &pdev->dev;
  2249. sport->port.type = PORT_LPUART;
  2250. sport->devtype = sdata->devtype;
  2251. sport->rx_watermark = sdata->rx_watermark;
  2252. ret = platform_get_irq(pdev, 0);
  2253. if (ret < 0)
  2254. return ret;
  2255. sport->port.irq = ret;
  2256. sport->port.iotype = sdata->iotype;
  2257. if (lpuart_is_32(sport))
  2258. sport->port.ops = &lpuart32_pops;
  2259. else
  2260. sport->port.ops = &lpuart_pops;
  2261. sport->port.has_sysrq = IS_ENABLED(CONFIG_SERIAL_FSL_LPUART_CONSOLE);
  2262. sport->port.flags = UPF_BOOT_AUTOCONF;
  2263. if (lpuart_is_32(sport))
  2264. sport->port.rs485_config = lpuart32_config_rs485;
  2265. else
  2266. sport->port.rs485_config = lpuart_config_rs485;
  2267. sport->port.rs485_supported = lpuart_rs485_supported;
  2268. sport->ipg_clk = devm_clk_get(&pdev->dev, "ipg");
  2269. if (IS_ERR(sport->ipg_clk)) {
  2270. ret = PTR_ERR(sport->ipg_clk);
  2271. dev_err(&pdev->dev, "failed to get uart ipg clk: %d\n", ret);
  2272. return ret;
  2273. }
  2274. sport->baud_clk = NULL;
  2275. if (is_imx8qxp_lpuart(sport)) {
  2276. sport->baud_clk = devm_clk_get(&pdev->dev, "baud");
  2277. if (IS_ERR(sport->baud_clk)) {
  2278. ret = PTR_ERR(sport->baud_clk);
  2279. dev_err(&pdev->dev, "failed to get uart baud clk: %d\n", ret);
  2280. return ret;
  2281. }
  2282. }
  2283. ret = of_alias_get_id(np, "serial");
  2284. if (ret < 0) {
  2285. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  2286. return ret;
  2287. }
  2288. if (ret >= ARRAY_SIZE(lpuart_ports)) {
  2289. dev_err(&pdev->dev, "serial%d out of range\n", ret);
  2290. return -EINVAL;
  2291. }
  2292. sport->port.line = ret;
  2293. ret = lpuart_enable_clks(sport);
  2294. if (ret)
  2295. return ret;
  2296. sport->port.uartclk = lpuart_get_baud_clk_rate(sport);
  2297. lpuart_ports[sport->port.line] = sport;
  2298. platform_set_drvdata(pdev, &sport->port);
  2299. if (lpuart_is_32(sport)) {
  2300. lpuart_reg.cons = LPUART32_CONSOLE;
  2301. handler = lpuart32_int;
  2302. } else {
  2303. lpuart_reg.cons = LPUART_CONSOLE;
  2304. handler = lpuart_int;
  2305. }
  2306. ret = lpuart_global_reset(sport);
  2307. if (ret)
  2308. goto failed_reset;
  2309. ret = uart_get_rs485_mode(&sport->port);
  2310. if (ret)
  2311. goto failed_get_rs485;
  2312. ret = uart_add_one_port(&lpuart_reg, &sport->port);
  2313. if (ret)
  2314. goto failed_attach_port;
  2315. ret = devm_request_irq(&pdev->dev, sport->port.irq, handler, 0,
  2316. DRIVER_NAME, sport);
  2317. if (ret)
  2318. goto failed_irq_request;
  2319. return 0;
  2320. failed_irq_request:
  2321. uart_remove_one_port(&lpuart_reg, &sport->port);
  2322. failed_attach_port:
  2323. failed_get_rs485:
  2324. failed_reset:
  2325. lpuart_disable_clks(sport);
  2326. return ret;
  2327. }
  2328. static int lpuart_remove(struct platform_device *pdev)
  2329. {
  2330. struct lpuart_port *sport = platform_get_drvdata(pdev);
  2331. uart_remove_one_port(&lpuart_reg, &sport->port);
  2332. lpuart_disable_clks(sport);
  2333. if (sport->dma_tx_chan)
  2334. dma_release_channel(sport->dma_tx_chan);
  2335. if (sport->dma_rx_chan)
  2336. dma_release_channel(sport->dma_rx_chan);
  2337. return 0;
  2338. }
  2339. static int __maybe_unused lpuart_suspend(struct device *dev)
  2340. {
  2341. struct lpuart_port *sport = dev_get_drvdata(dev);
  2342. unsigned long temp;
  2343. bool irq_wake;
  2344. if (lpuart_is_32(sport)) {
  2345. /* disable Rx/Tx and interrupts */
  2346. temp = lpuart32_read(&sport->port, UARTCTRL);
  2347. temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
  2348. lpuart32_write(&sport->port, temp, UARTCTRL);
  2349. } else {
  2350. /* disable Rx/Tx and interrupts */
  2351. temp = readb(sport->port.membase + UARTCR2);
  2352. temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
  2353. writeb(temp, sport->port.membase + UARTCR2);
  2354. }
  2355. uart_suspend_port(&lpuart_reg, &sport->port);
  2356. /* uart_suspend_port() might set wakeup flag */
  2357. irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  2358. if (sport->lpuart_dma_rx_use) {
  2359. /*
  2360. * EDMA driver during suspend will forcefully release any
  2361. * non-idle DMA channels. If port wakeup is enabled or if port
  2362. * is console port or 'no_console_suspend' is set the Rx DMA
  2363. * cannot resume as expected, hence gracefully release the
  2364. * Rx DMA path before suspend and start Rx DMA path on resume.
  2365. */
  2366. if (irq_wake) {
  2367. lpuart_dma_rx_free(&sport->port);
  2368. }
  2369. /* Disable Rx DMA to use UART port as wakeup source */
  2370. if (lpuart_is_32(sport)) {
  2371. temp = lpuart32_read(&sport->port, UARTBAUD);
  2372. lpuart32_write(&sport->port, temp & ~UARTBAUD_RDMAE,
  2373. UARTBAUD);
  2374. } else {
  2375. writeb(readb(sport->port.membase + UARTCR5) &
  2376. ~UARTCR5_RDMAS, sport->port.membase + UARTCR5);
  2377. }
  2378. }
  2379. if (sport->lpuart_dma_tx_use) {
  2380. sport->dma_tx_in_progress = false;
  2381. dmaengine_terminate_sync(sport->dma_tx_chan);
  2382. }
  2383. if (sport->port.suspended && !irq_wake)
  2384. lpuart_disable_clks(sport);
  2385. return 0;
  2386. }
  2387. static int __maybe_unused lpuart_resume(struct device *dev)
  2388. {
  2389. struct lpuart_port *sport = dev_get_drvdata(dev);
  2390. bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  2391. if (sport->port.suspended && !irq_wake)
  2392. lpuart_enable_clks(sport);
  2393. if (lpuart_is_32(sport))
  2394. lpuart32_setup_watermark_enable(sport);
  2395. else
  2396. lpuart_setup_watermark_enable(sport);
  2397. if (sport->lpuart_dma_rx_use) {
  2398. if (irq_wake) {
  2399. if (!lpuart_start_rx_dma(sport))
  2400. rx_dma_timer_init(sport);
  2401. else
  2402. sport->lpuart_dma_rx_use = false;
  2403. }
  2404. }
  2405. lpuart_tx_dma_startup(sport);
  2406. if (lpuart_is_32(sport))
  2407. lpuart32_configure(sport);
  2408. uart_resume_port(&lpuart_reg, &sport->port);
  2409. return 0;
  2410. }
  2411. static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
  2412. static struct platform_driver lpuart_driver = {
  2413. .probe = lpuart_probe,
  2414. .remove = lpuart_remove,
  2415. .driver = {
  2416. .name = "fsl-lpuart",
  2417. .of_match_table = lpuart_dt_ids,
  2418. .pm = &lpuart_pm_ops,
  2419. },
  2420. };
  2421. static int __init lpuart_serial_init(void)
  2422. {
  2423. int ret = uart_register_driver(&lpuart_reg);
  2424. if (ret)
  2425. return ret;
  2426. ret = platform_driver_register(&lpuart_driver);
  2427. if (ret)
  2428. uart_unregister_driver(&lpuart_reg);
  2429. return ret;
  2430. }
  2431. static void __exit lpuart_serial_exit(void)
  2432. {
  2433. platform_driver_unregister(&lpuart_driver);
  2434. uart_unregister_driver(&lpuart_reg);
  2435. }
  2436. module_init(lpuart_serial_init);
  2437. module_exit(lpuart_serial_exit);
  2438. MODULE_DESCRIPTION("Freescale lpuart serial port driver");
  2439. MODULE_LICENSE("GPL v2");