fsl_lpuart.c 68 KB

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