fsl_lpuart.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394
  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 UARTWATER_COUNT_MASK 0xff
  197. #define UARTWATER_TXCNT_OFF 8
  198. #define UARTWATER_RXCNT_OFF 24
  199. #define UARTWATER_WATER_MASK 0xff
  200. #define UARTWATER_TXWATER_OFF 0
  201. #define UARTWATER_RXWATER_OFF 16
  202. /* Rx DMA timeout in ms, which is used to calculate Rx ring buffer size */
  203. #define DMA_RX_TIMEOUT (10)
  204. #define DRIVER_NAME "fsl-lpuart"
  205. #define DEV_NAME "ttyLP"
  206. #define UART_NR 6
  207. /* IMX lpuart has four extra unused regs located at the beginning */
  208. #define IMX_REG_OFF 0x10
  209. struct lpuart_port {
  210. struct uart_port port;
  211. struct clk *clk;
  212. unsigned int txfifo_size;
  213. unsigned int rxfifo_size;
  214. bool lpuart_dma_tx_use;
  215. bool lpuart_dma_rx_use;
  216. struct dma_chan *dma_tx_chan;
  217. struct dma_chan *dma_rx_chan;
  218. struct dma_async_tx_descriptor *dma_tx_desc;
  219. struct dma_async_tx_descriptor *dma_rx_desc;
  220. dma_cookie_t dma_tx_cookie;
  221. dma_cookie_t dma_rx_cookie;
  222. unsigned int dma_tx_bytes;
  223. unsigned int dma_rx_bytes;
  224. bool dma_tx_in_progress;
  225. unsigned int dma_rx_timeout;
  226. struct timer_list lpuart_timer;
  227. struct scatterlist rx_sgl, tx_sgl[2];
  228. struct circ_buf rx_ring;
  229. int rx_dma_rng_buf_len;
  230. unsigned int dma_tx_nents;
  231. wait_queue_head_t dma_wait;
  232. };
  233. struct lpuart_soc_data {
  234. char iotype;
  235. u8 reg_off;
  236. };
  237. static const struct lpuart_soc_data vf_data = {
  238. .iotype = UPIO_MEM,
  239. };
  240. static const struct lpuart_soc_data ls_data = {
  241. .iotype = UPIO_MEM32BE,
  242. };
  243. static struct lpuart_soc_data imx_data = {
  244. .iotype = UPIO_MEM32,
  245. .reg_off = IMX_REG_OFF,
  246. };
  247. static const struct of_device_id lpuart_dt_ids[] = {
  248. { .compatible = "fsl,vf610-lpuart", .data = &vf_data, },
  249. { .compatible = "fsl,ls1021a-lpuart", .data = &ls_data, },
  250. { .compatible = "fsl,imx7ulp-lpuart", .data = &imx_data, },
  251. { /* sentinel */ }
  252. };
  253. MODULE_DEVICE_TABLE(of, lpuart_dt_ids);
  254. /* Forward declare this for the dma callbacks*/
  255. static void lpuart_dma_tx_complete(void *arg);
  256. static inline u32 lpuart32_read(struct uart_port *port, u32 off)
  257. {
  258. switch (port->iotype) {
  259. case UPIO_MEM32:
  260. return readl(port->membase + off);
  261. case UPIO_MEM32BE:
  262. return ioread32be(port->membase + off);
  263. default:
  264. return 0;
  265. }
  266. }
  267. static inline void lpuart32_write(struct uart_port *port, u32 val,
  268. u32 off)
  269. {
  270. switch (port->iotype) {
  271. case UPIO_MEM32:
  272. writel(val, port->membase + off);
  273. break;
  274. case UPIO_MEM32BE:
  275. iowrite32be(val, port->membase + off);
  276. break;
  277. }
  278. }
  279. static void lpuart_stop_tx(struct uart_port *port)
  280. {
  281. unsigned char temp;
  282. temp = readb(port->membase + UARTCR2);
  283. temp &= ~(UARTCR2_TIE | UARTCR2_TCIE);
  284. writeb(temp, port->membase + UARTCR2);
  285. }
  286. static void lpuart32_stop_tx(struct uart_port *port)
  287. {
  288. unsigned long temp;
  289. temp = lpuart32_read(port, UARTCTRL);
  290. temp &= ~(UARTCTRL_TIE | UARTCTRL_TCIE);
  291. lpuart32_write(port, temp, UARTCTRL);
  292. }
  293. static void lpuart_stop_rx(struct uart_port *port)
  294. {
  295. unsigned char temp;
  296. temp = readb(port->membase + UARTCR2);
  297. writeb(temp & ~UARTCR2_RE, port->membase + UARTCR2);
  298. }
  299. static void lpuart32_stop_rx(struct uart_port *port)
  300. {
  301. unsigned long temp;
  302. temp = lpuart32_read(port, UARTCTRL);
  303. lpuart32_write(port, temp & ~UARTCTRL_RE, UARTCTRL);
  304. }
  305. static void lpuart_dma_tx(struct lpuart_port *sport)
  306. {
  307. struct circ_buf *xmit = &sport->port.state->xmit;
  308. struct scatterlist *sgl = sport->tx_sgl;
  309. struct device *dev = sport->port.dev;
  310. int ret;
  311. if (sport->dma_tx_in_progress)
  312. return;
  313. sport->dma_tx_bytes = uart_circ_chars_pending(xmit);
  314. if (xmit->tail < xmit->head || xmit->head == 0) {
  315. sport->dma_tx_nents = 1;
  316. sg_init_one(sgl, xmit->buf + xmit->tail, sport->dma_tx_bytes);
  317. } else {
  318. sport->dma_tx_nents = 2;
  319. sg_init_table(sgl, 2);
  320. sg_set_buf(sgl, xmit->buf + xmit->tail,
  321. UART_XMIT_SIZE - xmit->tail);
  322. sg_set_buf(sgl + 1, xmit->buf, xmit->head);
  323. }
  324. ret = dma_map_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  325. if (!ret) {
  326. dev_err(dev, "DMA mapping error for TX.\n");
  327. return;
  328. }
  329. sport->dma_tx_desc = dmaengine_prep_slave_sg(sport->dma_tx_chan, sgl,
  330. sport->dma_tx_nents,
  331. DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT);
  332. if (!sport->dma_tx_desc) {
  333. dma_unmap_sg(dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  334. dev_err(dev, "Cannot prepare TX slave DMA!\n");
  335. return;
  336. }
  337. sport->dma_tx_desc->callback = lpuart_dma_tx_complete;
  338. sport->dma_tx_desc->callback_param = sport;
  339. sport->dma_tx_in_progress = true;
  340. sport->dma_tx_cookie = dmaengine_submit(sport->dma_tx_desc);
  341. dma_async_issue_pending(sport->dma_tx_chan);
  342. }
  343. static void lpuart_dma_tx_complete(void *arg)
  344. {
  345. struct lpuart_port *sport = arg;
  346. struct scatterlist *sgl = &sport->tx_sgl[0];
  347. struct circ_buf *xmit = &sport->port.state->xmit;
  348. unsigned long flags;
  349. spin_lock_irqsave(&sport->port.lock, flags);
  350. dma_unmap_sg(sport->port.dev, sgl, sport->dma_tx_nents, DMA_TO_DEVICE);
  351. xmit->tail = (xmit->tail + sport->dma_tx_bytes) & (UART_XMIT_SIZE - 1);
  352. sport->port.icount.tx += sport->dma_tx_bytes;
  353. sport->dma_tx_in_progress = false;
  354. spin_unlock_irqrestore(&sport->port.lock, flags);
  355. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  356. uart_write_wakeup(&sport->port);
  357. if (waitqueue_active(&sport->dma_wait)) {
  358. wake_up(&sport->dma_wait);
  359. return;
  360. }
  361. spin_lock_irqsave(&sport->port.lock, flags);
  362. if (!uart_circ_empty(xmit) && !uart_tx_stopped(&sport->port))
  363. lpuart_dma_tx(sport);
  364. spin_unlock_irqrestore(&sport->port.lock, flags);
  365. }
  366. static int lpuart_dma_tx_request(struct uart_port *port)
  367. {
  368. struct lpuart_port *sport = container_of(port,
  369. struct lpuart_port, port);
  370. struct dma_slave_config dma_tx_sconfig = {};
  371. int ret;
  372. dma_tx_sconfig.dst_addr = sport->port.mapbase + UARTDR;
  373. dma_tx_sconfig.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  374. dma_tx_sconfig.dst_maxburst = 1;
  375. dma_tx_sconfig.direction = DMA_MEM_TO_DEV;
  376. ret = dmaengine_slave_config(sport->dma_tx_chan, &dma_tx_sconfig);
  377. if (ret) {
  378. dev_err(sport->port.dev,
  379. "DMA slave config failed, err = %d\n", ret);
  380. return ret;
  381. }
  382. return 0;
  383. }
  384. static void lpuart_flush_buffer(struct uart_port *port)
  385. {
  386. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  387. if (sport->lpuart_dma_tx_use) {
  388. if (sport->dma_tx_in_progress) {
  389. dma_unmap_sg(sport->port.dev, &sport->tx_sgl[0],
  390. sport->dma_tx_nents, DMA_TO_DEVICE);
  391. sport->dma_tx_in_progress = false;
  392. }
  393. dmaengine_terminate_all(sport->dma_tx_chan);
  394. }
  395. }
  396. #if defined(CONFIG_CONSOLE_POLL)
  397. static int lpuart_poll_init(struct uart_port *port)
  398. {
  399. struct lpuart_port *sport = container_of(port,
  400. struct lpuart_port, port);
  401. unsigned long flags;
  402. unsigned char temp;
  403. sport->port.fifosize = 0;
  404. spin_lock_irqsave(&sport->port.lock, flags);
  405. /* Disable Rx & Tx */
  406. writeb(0, sport->port.membase + UARTCR2);
  407. temp = readb(sport->port.membase + UARTPFIFO);
  408. /* Enable Rx and Tx FIFO */
  409. writeb(temp | UARTPFIFO_RXFE | UARTPFIFO_TXFE,
  410. sport->port.membase + UARTPFIFO);
  411. /* flush Tx and Rx FIFO */
  412. writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
  413. sport->port.membase + UARTCFIFO);
  414. /* explicitly clear RDRF */
  415. if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
  416. readb(sport->port.membase + UARTDR);
  417. writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
  418. }
  419. writeb(0, sport->port.membase + UARTTWFIFO);
  420. writeb(1, sport->port.membase + UARTRWFIFO);
  421. /* Enable Rx and Tx */
  422. writeb(UARTCR2_RE | UARTCR2_TE, sport->port.membase + UARTCR2);
  423. spin_unlock_irqrestore(&sport->port.lock, flags);
  424. return 0;
  425. }
  426. static void lpuart_poll_put_char(struct uart_port *port, unsigned char c)
  427. {
  428. /* drain */
  429. while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
  430. barrier();
  431. writeb(c, port->membase + UARTDR);
  432. }
  433. static int lpuart_poll_get_char(struct uart_port *port)
  434. {
  435. if (!(readb(port->membase + UARTSR1) & UARTSR1_RDRF))
  436. return NO_POLL_CHAR;
  437. return readb(port->membase + UARTDR);
  438. }
  439. static int lpuart32_poll_init(struct uart_port *port)
  440. {
  441. unsigned long flags;
  442. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  443. u32 temp;
  444. sport->port.fifosize = 0;
  445. spin_lock_irqsave(&sport->port.lock, flags);
  446. /* Disable Rx & Tx */
  447. writel(0, sport->port.membase + UARTCTRL);
  448. temp = readl(sport->port.membase + UARTFIFO);
  449. /* Enable Rx and Tx FIFO */
  450. writel(temp | UARTFIFO_RXFE | UARTFIFO_TXFE,
  451. sport->port.membase + UARTFIFO);
  452. /* flush Tx and Rx FIFO */
  453. writel(UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH,
  454. sport->port.membase + UARTFIFO);
  455. /* explicitly clear RDRF */
  456. if (readl(sport->port.membase + UARTSTAT) & UARTSTAT_RDRF) {
  457. readl(sport->port.membase + UARTDATA);
  458. writel(UARTFIFO_RXUF, sport->port.membase + UARTFIFO);
  459. }
  460. /* Enable Rx and Tx */
  461. writel(UARTCTRL_RE | UARTCTRL_TE, sport->port.membase + UARTCTRL);
  462. spin_unlock_irqrestore(&sport->port.lock, flags);
  463. return 0;
  464. }
  465. static void lpuart32_poll_put_char(struct uart_port *port, unsigned char c)
  466. {
  467. while (!(readl(port->membase + UARTSTAT) & UARTSTAT_TDRE))
  468. barrier();
  469. writel(c, port->membase + UARTDATA);
  470. }
  471. static int lpuart32_poll_get_char(struct uart_port *port)
  472. {
  473. if (!(readl(port->membase + UARTSTAT) & UARTSTAT_RDRF))
  474. return NO_POLL_CHAR;
  475. return readl(port->membase + UARTDATA);
  476. }
  477. #endif
  478. static inline void lpuart_transmit_buffer(struct lpuart_port *sport)
  479. {
  480. struct circ_buf *xmit = &sport->port.state->xmit;
  481. while (!uart_circ_empty(xmit) &&
  482. (readb(sport->port.membase + UARTTCFIFO) < sport->txfifo_size)) {
  483. writeb(xmit->buf[xmit->tail], sport->port.membase + UARTDR);
  484. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  485. sport->port.icount.tx++;
  486. }
  487. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  488. uart_write_wakeup(&sport->port);
  489. if (uart_circ_empty(xmit))
  490. lpuart_stop_tx(&sport->port);
  491. }
  492. static inline void lpuart32_transmit_buffer(struct lpuart_port *sport)
  493. {
  494. struct circ_buf *xmit = &sport->port.state->xmit;
  495. unsigned long txcnt;
  496. txcnt = lpuart32_read(&sport->port, UARTWATER);
  497. txcnt = txcnt >> UARTWATER_TXCNT_OFF;
  498. txcnt &= UARTWATER_COUNT_MASK;
  499. while (!uart_circ_empty(xmit) && (txcnt < sport->txfifo_size)) {
  500. lpuart32_write(&sport->port, xmit->buf[xmit->tail], UARTDATA);
  501. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  502. sport->port.icount.tx++;
  503. txcnt = lpuart32_read(&sport->port, UARTWATER);
  504. txcnt = txcnt >> UARTWATER_TXCNT_OFF;
  505. txcnt &= UARTWATER_COUNT_MASK;
  506. }
  507. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  508. uart_write_wakeup(&sport->port);
  509. if (uart_circ_empty(xmit))
  510. lpuart32_stop_tx(&sport->port);
  511. }
  512. static void lpuart_start_tx(struct uart_port *port)
  513. {
  514. struct lpuart_port *sport = container_of(port,
  515. struct lpuart_port, port);
  516. struct circ_buf *xmit = &sport->port.state->xmit;
  517. unsigned char temp;
  518. temp = readb(port->membase + UARTCR2);
  519. writeb(temp | UARTCR2_TIE, port->membase + UARTCR2);
  520. if (sport->lpuart_dma_tx_use) {
  521. if (!uart_circ_empty(xmit) && !uart_tx_stopped(port))
  522. lpuart_dma_tx(sport);
  523. } else {
  524. if (readb(port->membase + UARTSR1) & UARTSR1_TDRE)
  525. lpuart_transmit_buffer(sport);
  526. }
  527. }
  528. static void lpuart32_start_tx(struct uart_port *port)
  529. {
  530. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  531. unsigned long temp;
  532. temp = lpuart32_read(port, UARTCTRL);
  533. lpuart32_write(port, temp | UARTCTRL_TIE, UARTCTRL);
  534. if (lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE)
  535. lpuart32_transmit_buffer(sport);
  536. }
  537. /* return TIOCSER_TEMT when transmitter is not busy */
  538. static unsigned int lpuart_tx_empty(struct uart_port *port)
  539. {
  540. struct lpuart_port *sport = container_of(port,
  541. struct lpuart_port, port);
  542. unsigned char sr1 = readb(port->membase + UARTSR1);
  543. unsigned char sfifo = readb(port->membase + UARTSFIFO);
  544. if (sport->dma_tx_in_progress)
  545. return 0;
  546. if (sr1 & UARTSR1_TC && sfifo & UARTSFIFO_TXEMPT)
  547. return TIOCSER_TEMT;
  548. return 0;
  549. }
  550. static unsigned int lpuart32_tx_empty(struct uart_port *port)
  551. {
  552. return (lpuart32_read(port, UARTSTAT) & UARTSTAT_TC) ?
  553. TIOCSER_TEMT : 0;
  554. }
  555. static bool lpuart_is_32(struct lpuart_port *sport)
  556. {
  557. return sport->port.iotype == UPIO_MEM32 ||
  558. sport->port.iotype == UPIO_MEM32BE;
  559. }
  560. static irqreturn_t lpuart_txint(int irq, void *dev_id)
  561. {
  562. struct lpuart_port *sport = dev_id;
  563. struct circ_buf *xmit = &sport->port.state->xmit;
  564. unsigned long flags;
  565. spin_lock_irqsave(&sport->port.lock, flags);
  566. if (sport->port.x_char) {
  567. if (lpuart_is_32(sport))
  568. lpuart32_write(&sport->port, sport->port.x_char, UARTDATA);
  569. else
  570. writeb(sport->port.x_char, sport->port.membase + UARTDR);
  571. goto out;
  572. }
  573. if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {
  574. if (lpuart_is_32(sport))
  575. lpuart32_stop_tx(&sport->port);
  576. else
  577. lpuart_stop_tx(&sport->port);
  578. goto out;
  579. }
  580. if (lpuart_is_32(sport))
  581. lpuart32_transmit_buffer(sport);
  582. else
  583. lpuart_transmit_buffer(sport);
  584. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  585. uart_write_wakeup(&sport->port);
  586. out:
  587. spin_unlock_irqrestore(&sport->port.lock, flags);
  588. return IRQ_HANDLED;
  589. }
  590. static irqreturn_t lpuart_rxint(int irq, void *dev_id)
  591. {
  592. struct lpuart_port *sport = dev_id;
  593. unsigned int flg, ignored = 0;
  594. struct tty_port *port = &sport->port.state->port;
  595. unsigned long flags;
  596. unsigned char rx, sr;
  597. spin_lock_irqsave(&sport->port.lock, flags);
  598. while (!(readb(sport->port.membase + UARTSFIFO) & UARTSFIFO_RXEMPT)) {
  599. flg = TTY_NORMAL;
  600. sport->port.icount.rx++;
  601. /*
  602. * to clear the FE, OR, NF, FE, PE flags,
  603. * read SR1 then read DR
  604. */
  605. sr = readb(sport->port.membase + UARTSR1);
  606. rx = readb(sport->port.membase + UARTDR);
  607. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  608. continue;
  609. if (sr & (UARTSR1_PE | UARTSR1_OR | UARTSR1_FE)) {
  610. if (sr & UARTSR1_PE)
  611. sport->port.icount.parity++;
  612. else if (sr & UARTSR1_FE)
  613. sport->port.icount.frame++;
  614. if (sr & UARTSR1_OR)
  615. sport->port.icount.overrun++;
  616. if (sr & sport->port.ignore_status_mask) {
  617. if (++ignored > 100)
  618. goto out;
  619. continue;
  620. }
  621. sr &= sport->port.read_status_mask;
  622. if (sr & UARTSR1_PE)
  623. flg = TTY_PARITY;
  624. else if (sr & UARTSR1_FE)
  625. flg = TTY_FRAME;
  626. if (sr & UARTSR1_OR)
  627. flg = TTY_OVERRUN;
  628. #ifdef SUPPORT_SYSRQ
  629. sport->port.sysrq = 0;
  630. #endif
  631. }
  632. tty_insert_flip_char(port, rx, flg);
  633. }
  634. out:
  635. spin_unlock_irqrestore(&sport->port.lock, flags);
  636. tty_flip_buffer_push(port);
  637. return IRQ_HANDLED;
  638. }
  639. static irqreturn_t lpuart32_rxint(int irq, void *dev_id)
  640. {
  641. struct lpuart_port *sport = dev_id;
  642. unsigned int flg, ignored = 0;
  643. struct tty_port *port = &sport->port.state->port;
  644. unsigned long flags;
  645. unsigned long rx, sr;
  646. spin_lock_irqsave(&sport->port.lock, flags);
  647. while (!(lpuart32_read(&sport->port, UARTFIFO) & UARTFIFO_RXEMPT)) {
  648. flg = TTY_NORMAL;
  649. sport->port.icount.rx++;
  650. /*
  651. * to clear the FE, OR, NF, FE, PE flags,
  652. * read STAT then read DATA reg
  653. */
  654. sr = lpuart32_read(&sport->port, UARTSTAT);
  655. rx = lpuart32_read(&sport->port, UARTDATA);
  656. rx &= 0x3ff;
  657. if (uart_handle_sysrq_char(&sport->port, (unsigned char)rx))
  658. continue;
  659. if (sr & (UARTSTAT_PE | UARTSTAT_OR | UARTSTAT_FE)) {
  660. if (sr & UARTSTAT_PE)
  661. sport->port.icount.parity++;
  662. else if (sr & UARTSTAT_FE)
  663. sport->port.icount.frame++;
  664. if (sr & UARTSTAT_OR)
  665. sport->port.icount.overrun++;
  666. if (sr & sport->port.ignore_status_mask) {
  667. if (++ignored > 100)
  668. goto out;
  669. continue;
  670. }
  671. sr &= sport->port.read_status_mask;
  672. if (sr & UARTSTAT_PE)
  673. flg = TTY_PARITY;
  674. else if (sr & UARTSTAT_FE)
  675. flg = TTY_FRAME;
  676. if (sr & UARTSTAT_OR)
  677. flg = TTY_OVERRUN;
  678. #ifdef SUPPORT_SYSRQ
  679. sport->port.sysrq = 0;
  680. #endif
  681. }
  682. tty_insert_flip_char(port, rx, flg);
  683. }
  684. out:
  685. spin_unlock_irqrestore(&sport->port.lock, flags);
  686. tty_flip_buffer_push(port);
  687. return IRQ_HANDLED;
  688. }
  689. static irqreturn_t lpuart_int(int irq, void *dev_id)
  690. {
  691. struct lpuart_port *sport = dev_id;
  692. unsigned char sts;
  693. sts = readb(sport->port.membase + UARTSR1);
  694. if (sts & UARTSR1_RDRF)
  695. lpuart_rxint(irq, dev_id);
  696. if (sts & UARTSR1_TDRE)
  697. lpuart_txint(irq, dev_id);
  698. return IRQ_HANDLED;
  699. }
  700. static irqreturn_t lpuart32_int(int irq, void *dev_id)
  701. {
  702. struct lpuart_port *sport = dev_id;
  703. unsigned long sts, rxcount;
  704. sts = lpuart32_read(&sport->port, UARTSTAT);
  705. rxcount = lpuart32_read(&sport->port, UARTWATER);
  706. rxcount = rxcount >> UARTWATER_RXCNT_OFF;
  707. if (sts & UARTSTAT_RDRF || rxcount > 0)
  708. lpuart32_rxint(irq, dev_id);
  709. if ((sts & UARTSTAT_TDRE) &&
  710. !(lpuart32_read(&sport->port, UARTBAUD) & UARTBAUD_TDMAE))
  711. lpuart_txint(irq, dev_id);
  712. lpuart32_write(&sport->port, sts, UARTSTAT);
  713. return IRQ_HANDLED;
  714. }
  715. static void lpuart_copy_rx_to_tty(struct lpuart_port *sport)
  716. {
  717. struct tty_port *port = &sport->port.state->port;
  718. struct dma_tx_state state;
  719. enum dma_status dmastat;
  720. struct circ_buf *ring = &sport->rx_ring;
  721. unsigned long flags;
  722. int count = 0;
  723. unsigned char sr;
  724. sr = readb(sport->port.membase + UARTSR1);
  725. if (sr & (UARTSR1_PE | UARTSR1_FE)) {
  726. /* Read DR to clear the error flags */
  727. readb(sport->port.membase + UARTDR);
  728. if (sr & UARTSR1_PE)
  729. sport->port.icount.parity++;
  730. else if (sr & UARTSR1_FE)
  731. sport->port.icount.frame++;
  732. }
  733. async_tx_ack(sport->dma_rx_desc);
  734. spin_lock_irqsave(&sport->port.lock, flags);
  735. dmastat = dmaengine_tx_status(sport->dma_rx_chan,
  736. sport->dma_rx_cookie,
  737. &state);
  738. if (dmastat == DMA_ERROR) {
  739. dev_err(sport->port.dev, "Rx DMA transfer failed!\n");
  740. spin_unlock_irqrestore(&sport->port.lock, flags);
  741. return;
  742. }
  743. /* CPU claims ownership of RX DMA buffer */
  744. dma_sync_sg_for_cpu(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
  745. /*
  746. * ring->head points to the end of data already written by the DMA.
  747. * ring->tail points to the beginning of data to be read by the
  748. * framework.
  749. * The current transfer size should not be larger than the dma buffer
  750. * length.
  751. */
  752. ring->head = sport->rx_sgl.length - state.residue;
  753. BUG_ON(ring->head > sport->rx_sgl.length);
  754. /*
  755. * At this point ring->head may point to the first byte right after the
  756. * last byte of the dma buffer:
  757. * 0 <= ring->head <= sport->rx_sgl.length
  758. *
  759. * However ring->tail must always points inside the dma buffer:
  760. * 0 <= ring->tail <= sport->rx_sgl.length - 1
  761. *
  762. * Since we use a ring buffer, we have to handle the case
  763. * where head is lower than tail. In such a case, we first read from
  764. * tail to the end of the buffer then reset tail.
  765. */
  766. if (ring->head < ring->tail) {
  767. count = sport->rx_sgl.length - ring->tail;
  768. tty_insert_flip_string(port, ring->buf + ring->tail, count);
  769. ring->tail = 0;
  770. sport->port.icount.rx += count;
  771. }
  772. /* Finally we read data from tail to head */
  773. if (ring->tail < ring->head) {
  774. count = ring->head - ring->tail;
  775. tty_insert_flip_string(port, ring->buf + ring->tail, count);
  776. /* Wrap ring->head if needed */
  777. if (ring->head >= sport->rx_sgl.length)
  778. ring->head = 0;
  779. ring->tail = ring->head;
  780. sport->port.icount.rx += count;
  781. }
  782. dma_sync_sg_for_device(sport->port.dev, &sport->rx_sgl, 1,
  783. DMA_FROM_DEVICE);
  784. spin_unlock_irqrestore(&sport->port.lock, flags);
  785. tty_flip_buffer_push(port);
  786. mod_timer(&sport->lpuart_timer, jiffies + sport->dma_rx_timeout);
  787. }
  788. static void lpuart_dma_rx_complete(void *arg)
  789. {
  790. struct lpuart_port *sport = arg;
  791. lpuart_copy_rx_to_tty(sport);
  792. }
  793. static void lpuart_timer_func(struct timer_list *t)
  794. {
  795. struct lpuart_port *sport = from_timer(sport, t, lpuart_timer);
  796. lpuart_copy_rx_to_tty(sport);
  797. }
  798. static inline int lpuart_start_rx_dma(struct lpuart_port *sport)
  799. {
  800. struct dma_slave_config dma_rx_sconfig = {};
  801. struct circ_buf *ring = &sport->rx_ring;
  802. int ret, nent;
  803. int bits, baud;
  804. struct tty_struct *tty = tty_port_tty_get(&sport->port.state->port);
  805. struct ktermios *termios = &tty->termios;
  806. baud = tty_get_baud_rate(tty);
  807. bits = (termios->c_cflag & CSIZE) == CS7 ? 9 : 10;
  808. if (termios->c_cflag & PARENB)
  809. bits++;
  810. /*
  811. * Calculate length of one DMA buffer size to keep latency below
  812. * 10ms at any baud rate.
  813. */
  814. sport->rx_dma_rng_buf_len = (DMA_RX_TIMEOUT * baud / bits / 1000) * 2;
  815. sport->rx_dma_rng_buf_len = (1 << (fls(sport->rx_dma_rng_buf_len) - 1));
  816. if (sport->rx_dma_rng_buf_len < 16)
  817. sport->rx_dma_rng_buf_len = 16;
  818. ring->buf = kmalloc(sport->rx_dma_rng_buf_len, GFP_ATOMIC);
  819. if (!ring->buf) {
  820. dev_err(sport->port.dev, "Ring buf alloc failed\n");
  821. return -ENOMEM;
  822. }
  823. sg_init_one(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
  824. sg_set_buf(&sport->rx_sgl, ring->buf, sport->rx_dma_rng_buf_len);
  825. nent = dma_map_sg(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
  826. if (!nent) {
  827. dev_err(sport->port.dev, "DMA Rx mapping error\n");
  828. return -EINVAL;
  829. }
  830. dma_rx_sconfig.src_addr = sport->port.mapbase + UARTDR;
  831. dma_rx_sconfig.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  832. dma_rx_sconfig.src_maxburst = 1;
  833. dma_rx_sconfig.direction = DMA_DEV_TO_MEM;
  834. ret = dmaengine_slave_config(sport->dma_rx_chan, &dma_rx_sconfig);
  835. if (ret < 0) {
  836. dev_err(sport->port.dev,
  837. "DMA Rx slave config failed, err = %d\n", ret);
  838. return ret;
  839. }
  840. sport->dma_rx_desc = dmaengine_prep_dma_cyclic(sport->dma_rx_chan,
  841. sg_dma_address(&sport->rx_sgl),
  842. sport->rx_sgl.length,
  843. sport->rx_sgl.length / 2,
  844. DMA_DEV_TO_MEM,
  845. DMA_PREP_INTERRUPT);
  846. if (!sport->dma_rx_desc) {
  847. dev_err(sport->port.dev, "Cannot prepare cyclic DMA\n");
  848. return -EFAULT;
  849. }
  850. sport->dma_rx_desc->callback = lpuart_dma_rx_complete;
  851. sport->dma_rx_desc->callback_param = sport;
  852. sport->dma_rx_cookie = dmaengine_submit(sport->dma_rx_desc);
  853. dma_async_issue_pending(sport->dma_rx_chan);
  854. writeb(readb(sport->port.membase + UARTCR5) | UARTCR5_RDMAS,
  855. sport->port.membase + UARTCR5);
  856. return 0;
  857. }
  858. static void lpuart_dma_rx_free(struct uart_port *port)
  859. {
  860. struct lpuart_port *sport = container_of(port,
  861. struct lpuart_port, port);
  862. if (sport->dma_rx_chan)
  863. dmaengine_terminate_all(sport->dma_rx_chan);
  864. dma_unmap_sg(sport->port.dev, &sport->rx_sgl, 1, DMA_FROM_DEVICE);
  865. kfree(sport->rx_ring.buf);
  866. sport->rx_ring.tail = 0;
  867. sport->rx_ring.head = 0;
  868. sport->dma_rx_desc = NULL;
  869. sport->dma_rx_cookie = -EINVAL;
  870. }
  871. static int lpuart_config_rs485(struct uart_port *port,
  872. struct serial_rs485 *rs485)
  873. {
  874. struct lpuart_port *sport = container_of(port,
  875. struct lpuart_port, port);
  876. u8 modem = readb(sport->port.membase + UARTMODEM) &
  877. ~(UARTMODEM_TXRTSPOL | UARTMODEM_TXRTSE);
  878. writeb(modem, sport->port.membase + UARTMODEM);
  879. /* clear unsupported configurations */
  880. rs485->delay_rts_before_send = 0;
  881. rs485->delay_rts_after_send = 0;
  882. rs485->flags &= ~SER_RS485_RX_DURING_TX;
  883. if (rs485->flags & SER_RS485_ENABLED) {
  884. /* Enable auto RS-485 RTS mode */
  885. modem |= UARTMODEM_TXRTSE;
  886. /*
  887. * RTS needs to be logic HIGH either during transer _or_ after
  888. * transfer, other variants are not supported by the hardware.
  889. */
  890. if (!(rs485->flags & (SER_RS485_RTS_ON_SEND |
  891. SER_RS485_RTS_AFTER_SEND)))
  892. rs485->flags |= SER_RS485_RTS_ON_SEND;
  893. if (rs485->flags & SER_RS485_RTS_ON_SEND &&
  894. rs485->flags & SER_RS485_RTS_AFTER_SEND)
  895. rs485->flags &= ~SER_RS485_RTS_AFTER_SEND;
  896. /*
  897. * The hardware defaults to RTS logic HIGH while transfer.
  898. * Switch polarity in case RTS shall be logic HIGH
  899. * after transfer.
  900. * Note: UART is assumed to be active high.
  901. */
  902. if (rs485->flags & SER_RS485_RTS_ON_SEND)
  903. modem &= ~UARTMODEM_TXRTSPOL;
  904. else if (rs485->flags & SER_RS485_RTS_AFTER_SEND)
  905. modem |= UARTMODEM_TXRTSPOL;
  906. }
  907. /* Store the new configuration */
  908. sport->port.rs485 = *rs485;
  909. writeb(modem, sport->port.membase + UARTMODEM);
  910. return 0;
  911. }
  912. static unsigned int lpuart_get_mctrl(struct uart_port *port)
  913. {
  914. unsigned int temp = 0;
  915. unsigned char reg;
  916. reg = readb(port->membase + UARTMODEM);
  917. if (reg & UARTMODEM_TXCTSE)
  918. temp |= TIOCM_CTS;
  919. if (reg & UARTMODEM_RXRTSE)
  920. temp |= TIOCM_RTS;
  921. return temp;
  922. }
  923. static unsigned int lpuart32_get_mctrl(struct uart_port *port)
  924. {
  925. unsigned int temp = 0;
  926. unsigned long reg;
  927. reg = lpuart32_read(port, UARTMODIR);
  928. if (reg & UARTMODIR_TXCTSE)
  929. temp |= TIOCM_CTS;
  930. if (reg & UARTMODIR_RXRTSE)
  931. temp |= TIOCM_RTS;
  932. return temp;
  933. }
  934. static void lpuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  935. {
  936. unsigned char temp;
  937. struct lpuart_port *sport = container_of(port,
  938. struct lpuart_port, port);
  939. /* Make sure RXRTSE bit is not set when RS485 is enabled */
  940. if (!(sport->port.rs485.flags & SER_RS485_ENABLED)) {
  941. temp = readb(sport->port.membase + UARTMODEM) &
  942. ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
  943. if (mctrl & TIOCM_RTS)
  944. temp |= UARTMODEM_RXRTSE;
  945. if (mctrl & TIOCM_CTS)
  946. temp |= UARTMODEM_TXCTSE;
  947. writeb(temp, port->membase + UARTMODEM);
  948. }
  949. }
  950. static void lpuart32_set_mctrl(struct uart_port *port, unsigned int mctrl)
  951. {
  952. unsigned long temp;
  953. temp = lpuart32_read(port, UARTMODIR) &
  954. ~(UARTMODIR_RXRTSE | UARTMODIR_TXCTSE);
  955. if (mctrl & TIOCM_RTS)
  956. temp |= UARTMODIR_RXRTSE;
  957. if (mctrl & TIOCM_CTS)
  958. temp |= UARTMODIR_TXCTSE;
  959. lpuart32_write(port, temp, UARTMODIR);
  960. }
  961. static void lpuart_break_ctl(struct uart_port *port, int break_state)
  962. {
  963. unsigned char temp;
  964. temp = readb(port->membase + UARTCR2) & ~UARTCR2_SBK;
  965. if (break_state != 0)
  966. temp |= UARTCR2_SBK;
  967. writeb(temp, port->membase + UARTCR2);
  968. }
  969. static void lpuart32_break_ctl(struct uart_port *port, int break_state)
  970. {
  971. unsigned long temp;
  972. temp = lpuart32_read(port, UARTCTRL) & ~UARTCTRL_SBK;
  973. if (break_state != 0)
  974. temp |= UARTCTRL_SBK;
  975. lpuart32_write(port, temp, UARTCTRL);
  976. }
  977. static void lpuart_setup_watermark(struct lpuart_port *sport)
  978. {
  979. unsigned char val, cr2;
  980. unsigned char cr2_saved;
  981. cr2 = readb(sport->port.membase + UARTCR2);
  982. cr2_saved = cr2;
  983. cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_TE |
  984. UARTCR2_RIE | UARTCR2_RE);
  985. writeb(cr2, sport->port.membase + UARTCR2);
  986. val = readb(sport->port.membase + UARTPFIFO);
  987. writeb(val | UARTPFIFO_TXFE | UARTPFIFO_RXFE,
  988. sport->port.membase + UARTPFIFO);
  989. /* flush Tx and Rx FIFO */
  990. writeb(UARTCFIFO_TXFLUSH | UARTCFIFO_RXFLUSH,
  991. sport->port.membase + UARTCFIFO);
  992. /* explicitly clear RDRF */
  993. if (readb(sport->port.membase + UARTSR1) & UARTSR1_RDRF) {
  994. readb(sport->port.membase + UARTDR);
  995. writeb(UARTSFIFO_RXUF, sport->port.membase + UARTSFIFO);
  996. }
  997. writeb(0, sport->port.membase + UARTTWFIFO);
  998. writeb(1, sport->port.membase + UARTRWFIFO);
  999. /* Restore cr2 */
  1000. writeb(cr2_saved, sport->port.membase + UARTCR2);
  1001. }
  1002. static void lpuart32_setup_watermark(struct lpuart_port *sport)
  1003. {
  1004. unsigned long val, ctrl;
  1005. unsigned long ctrl_saved;
  1006. ctrl = lpuart32_read(&sport->port, UARTCTRL);
  1007. ctrl_saved = ctrl;
  1008. ctrl &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_TE |
  1009. UARTCTRL_RIE | UARTCTRL_RE);
  1010. lpuart32_write(&sport->port, ctrl, UARTCTRL);
  1011. /* enable FIFO mode */
  1012. val = lpuart32_read(&sport->port, UARTFIFO);
  1013. val |= UARTFIFO_TXFE | UARTFIFO_RXFE;
  1014. val |= UARTFIFO_TXFLUSH | UARTFIFO_RXFLUSH;
  1015. lpuart32_write(&sport->port, val, UARTFIFO);
  1016. /* set the watermark */
  1017. val = (0x1 << UARTWATER_RXWATER_OFF) | (0x0 << UARTWATER_TXWATER_OFF);
  1018. lpuart32_write(&sport->port, val, UARTWATER);
  1019. /* Restore cr2 */
  1020. lpuart32_write(&sport->port, ctrl_saved, UARTCTRL);
  1021. }
  1022. static void rx_dma_timer_init(struct lpuart_port *sport)
  1023. {
  1024. timer_setup(&sport->lpuart_timer, lpuart_timer_func, 0);
  1025. sport->lpuart_timer.expires = jiffies + sport->dma_rx_timeout;
  1026. add_timer(&sport->lpuart_timer);
  1027. }
  1028. static int lpuart_startup(struct uart_port *port)
  1029. {
  1030. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1031. unsigned long flags;
  1032. unsigned char temp;
  1033. /* determine FIFO size and enable FIFO mode */
  1034. temp = readb(sport->port.membase + UARTPFIFO);
  1035. sport->txfifo_size = 0x1 << (((temp >> UARTPFIFO_TXSIZE_OFF) &
  1036. UARTPFIFO_FIFOSIZE_MASK) + 1);
  1037. sport->port.fifosize = sport->txfifo_size;
  1038. sport->rxfifo_size = 0x1 << (((temp >> UARTPFIFO_RXSIZE_OFF) &
  1039. UARTPFIFO_FIFOSIZE_MASK) + 1);
  1040. spin_lock_irqsave(&sport->port.lock, flags);
  1041. lpuart_setup_watermark(sport);
  1042. temp = readb(sport->port.membase + UARTCR2);
  1043. temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
  1044. writeb(temp, sport->port.membase + UARTCR2);
  1045. if (sport->dma_rx_chan && !lpuart_start_rx_dma(sport)) {
  1046. /* set Rx DMA timeout */
  1047. sport->dma_rx_timeout = msecs_to_jiffies(DMA_RX_TIMEOUT);
  1048. if (!sport->dma_rx_timeout)
  1049. sport->dma_rx_timeout = 1;
  1050. sport->lpuart_dma_rx_use = true;
  1051. rx_dma_timer_init(sport);
  1052. } else {
  1053. sport->lpuart_dma_rx_use = false;
  1054. }
  1055. if (sport->dma_tx_chan && !lpuart_dma_tx_request(port)) {
  1056. init_waitqueue_head(&sport->dma_wait);
  1057. sport->lpuart_dma_tx_use = true;
  1058. temp = readb(port->membase + UARTCR5);
  1059. writeb(temp | UARTCR5_TDMAS, port->membase + UARTCR5);
  1060. } else {
  1061. sport->lpuart_dma_tx_use = false;
  1062. }
  1063. spin_unlock_irqrestore(&sport->port.lock, flags);
  1064. return 0;
  1065. }
  1066. static int lpuart32_startup(struct uart_port *port)
  1067. {
  1068. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1069. unsigned long flags;
  1070. unsigned long temp;
  1071. /* determine FIFO size */
  1072. temp = lpuart32_read(&sport->port, UARTFIFO);
  1073. sport->txfifo_size = 0x1 << (((temp >> UARTFIFO_TXSIZE_OFF) &
  1074. UARTFIFO_FIFOSIZE_MASK) - 1);
  1075. sport->rxfifo_size = 0x1 << (((temp >> UARTFIFO_RXSIZE_OFF) &
  1076. UARTFIFO_FIFOSIZE_MASK) - 1);
  1077. spin_lock_irqsave(&sport->port.lock, flags);
  1078. lpuart32_setup_watermark(sport);
  1079. temp = lpuart32_read(&sport->port, UARTCTRL);
  1080. temp |= (UARTCTRL_RIE | UARTCTRL_TIE | UARTCTRL_RE | UARTCTRL_TE);
  1081. temp |= UARTCTRL_ILIE;
  1082. lpuart32_write(&sport->port, temp, UARTCTRL);
  1083. spin_unlock_irqrestore(&sport->port.lock, flags);
  1084. return 0;
  1085. }
  1086. static void lpuart_shutdown(struct uart_port *port)
  1087. {
  1088. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1089. unsigned char temp;
  1090. unsigned long flags;
  1091. spin_lock_irqsave(&port->lock, flags);
  1092. /* disable Rx/Tx and interrupts */
  1093. temp = readb(port->membase + UARTCR2);
  1094. temp &= ~(UARTCR2_TE | UARTCR2_RE |
  1095. UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
  1096. writeb(temp, port->membase + UARTCR2);
  1097. spin_unlock_irqrestore(&port->lock, flags);
  1098. if (sport->lpuart_dma_rx_use) {
  1099. del_timer_sync(&sport->lpuart_timer);
  1100. lpuart_dma_rx_free(&sport->port);
  1101. }
  1102. if (sport->lpuart_dma_tx_use) {
  1103. if (wait_event_interruptible(sport->dma_wait,
  1104. !sport->dma_tx_in_progress) != false) {
  1105. sport->dma_tx_in_progress = false;
  1106. dmaengine_terminate_all(sport->dma_tx_chan);
  1107. }
  1108. lpuart_stop_tx(port);
  1109. }
  1110. }
  1111. static void lpuart32_shutdown(struct uart_port *port)
  1112. {
  1113. unsigned long temp;
  1114. unsigned long flags;
  1115. spin_lock_irqsave(&port->lock, flags);
  1116. /* disable Rx/Tx and interrupts */
  1117. temp = lpuart32_read(port, UARTCTRL);
  1118. temp &= ~(UARTCTRL_TE | UARTCTRL_RE |
  1119. UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
  1120. lpuart32_write(port, temp, UARTCTRL);
  1121. spin_unlock_irqrestore(&port->lock, flags);
  1122. }
  1123. static void
  1124. lpuart_set_termios(struct uart_port *port, struct ktermios *termios,
  1125. struct ktermios *old)
  1126. {
  1127. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1128. unsigned long flags;
  1129. unsigned char cr1, old_cr1, old_cr2, cr3, cr4, bdh, modem;
  1130. unsigned int baud;
  1131. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1132. unsigned int sbr, brfa;
  1133. cr1 = old_cr1 = readb(sport->port.membase + UARTCR1);
  1134. old_cr2 = readb(sport->port.membase + UARTCR2);
  1135. cr3 = readb(sport->port.membase + UARTCR3);
  1136. cr4 = readb(sport->port.membase + UARTCR4);
  1137. bdh = readb(sport->port.membase + UARTBDH);
  1138. modem = readb(sport->port.membase + UARTMODEM);
  1139. /*
  1140. * only support CS8 and CS7, and for CS7 must enable PE.
  1141. * supported mode:
  1142. * - (7,e/o,1)
  1143. * - (8,n,1)
  1144. * - (8,m/s,1)
  1145. * - (8,e/o,1)
  1146. */
  1147. while ((termios->c_cflag & CSIZE) != CS8 &&
  1148. (termios->c_cflag & CSIZE) != CS7) {
  1149. termios->c_cflag &= ~CSIZE;
  1150. termios->c_cflag |= old_csize;
  1151. old_csize = CS8;
  1152. }
  1153. if ((termios->c_cflag & CSIZE) == CS8 ||
  1154. (termios->c_cflag & CSIZE) == CS7)
  1155. cr1 = old_cr1 & ~UARTCR1_M;
  1156. if (termios->c_cflag & CMSPAR) {
  1157. if ((termios->c_cflag & CSIZE) != CS8) {
  1158. termios->c_cflag &= ~CSIZE;
  1159. termios->c_cflag |= CS8;
  1160. }
  1161. cr1 |= UARTCR1_M;
  1162. }
  1163. /*
  1164. * When auto RS-485 RTS mode is enabled,
  1165. * hardware flow control need to be disabled.
  1166. */
  1167. if (sport->port.rs485.flags & SER_RS485_ENABLED)
  1168. termios->c_cflag &= ~CRTSCTS;
  1169. if (termios->c_cflag & CRTSCTS) {
  1170. modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
  1171. } else {
  1172. termios->c_cflag &= ~CRTSCTS;
  1173. modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
  1174. }
  1175. if (termios->c_cflag & CSTOPB)
  1176. termios->c_cflag &= ~CSTOPB;
  1177. /* parity must be enabled when CS7 to match 8-bits format */
  1178. if ((termios->c_cflag & CSIZE) == CS7)
  1179. termios->c_cflag |= PARENB;
  1180. if ((termios->c_cflag & PARENB)) {
  1181. if (termios->c_cflag & CMSPAR) {
  1182. cr1 &= ~UARTCR1_PE;
  1183. if (termios->c_cflag & PARODD)
  1184. cr3 |= UARTCR3_T8;
  1185. else
  1186. cr3 &= ~UARTCR3_T8;
  1187. } else {
  1188. cr1 |= UARTCR1_PE;
  1189. if ((termios->c_cflag & CSIZE) == CS8)
  1190. cr1 |= UARTCR1_M;
  1191. if (termios->c_cflag & PARODD)
  1192. cr1 |= UARTCR1_PT;
  1193. else
  1194. cr1 &= ~UARTCR1_PT;
  1195. }
  1196. }
  1197. /* ask the core to calculate the divisor */
  1198. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1199. /*
  1200. * Need to update the Ring buffer length according to the selected
  1201. * baud rate and restart Rx DMA path.
  1202. *
  1203. * Since timer function acqures sport->port.lock, need to stop before
  1204. * acquring same lock because otherwise del_timer_sync() can deadlock.
  1205. */
  1206. if (old && sport->lpuart_dma_rx_use) {
  1207. del_timer_sync(&sport->lpuart_timer);
  1208. lpuart_dma_rx_free(&sport->port);
  1209. }
  1210. spin_lock_irqsave(&sport->port.lock, flags);
  1211. sport->port.read_status_mask = 0;
  1212. if (termios->c_iflag & INPCK)
  1213. sport->port.read_status_mask |= (UARTSR1_FE | UARTSR1_PE);
  1214. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1215. sport->port.read_status_mask |= UARTSR1_FE;
  1216. /* characters to ignore */
  1217. sport->port.ignore_status_mask = 0;
  1218. if (termios->c_iflag & IGNPAR)
  1219. sport->port.ignore_status_mask |= UARTSR1_PE;
  1220. if (termios->c_iflag & IGNBRK) {
  1221. sport->port.ignore_status_mask |= UARTSR1_FE;
  1222. /*
  1223. * if we're ignoring parity and break indicators,
  1224. * ignore overruns too (for real raw support).
  1225. */
  1226. if (termios->c_iflag & IGNPAR)
  1227. sport->port.ignore_status_mask |= UARTSR1_OR;
  1228. }
  1229. /* update the per-port timeout */
  1230. uart_update_timeout(port, termios->c_cflag, baud);
  1231. /* wait transmit engin complete */
  1232. while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
  1233. barrier();
  1234. /* disable transmit and receive */
  1235. writeb(old_cr2 & ~(UARTCR2_TE | UARTCR2_RE),
  1236. sport->port.membase + UARTCR2);
  1237. sbr = sport->port.uartclk / (16 * baud);
  1238. brfa = ((sport->port.uartclk - (16 * sbr * baud)) * 2) / baud;
  1239. bdh &= ~UARTBDH_SBR_MASK;
  1240. bdh |= (sbr >> 8) & 0x1F;
  1241. cr4 &= ~UARTCR4_BRFA_MASK;
  1242. brfa &= UARTCR4_BRFA_MASK;
  1243. writeb(cr4 | brfa, sport->port.membase + UARTCR4);
  1244. writeb(bdh, sport->port.membase + UARTBDH);
  1245. writeb(sbr & 0xFF, sport->port.membase + UARTBDL);
  1246. writeb(cr3, sport->port.membase + UARTCR3);
  1247. writeb(cr1, sport->port.membase + UARTCR1);
  1248. writeb(modem, sport->port.membase + UARTMODEM);
  1249. /* restore control register */
  1250. writeb(old_cr2, sport->port.membase + UARTCR2);
  1251. if (old && sport->lpuart_dma_rx_use) {
  1252. if (!lpuart_start_rx_dma(sport))
  1253. rx_dma_timer_init(sport);
  1254. else
  1255. sport->lpuart_dma_rx_use = false;
  1256. }
  1257. spin_unlock_irqrestore(&sport->port.lock, flags);
  1258. }
  1259. static void
  1260. lpuart32_serial_setbrg(struct lpuart_port *sport, unsigned int baudrate)
  1261. {
  1262. u32 sbr, osr, baud_diff, tmp_osr, tmp_sbr, tmp_diff, tmp;
  1263. u32 clk = sport->port.uartclk;
  1264. /*
  1265. * The idea is to use the best OSR (over-sampling rate) possible.
  1266. * Note, OSR is typically hard-set to 16 in other LPUART instantiations.
  1267. * Loop to find the best OSR value possible, one that generates minimum
  1268. * baud_diff iterate through the rest of the supported values of OSR.
  1269. *
  1270. * Calculation Formula:
  1271. * Baud Rate = baud clock / ((OSR+1) × SBR)
  1272. */
  1273. baud_diff = baudrate;
  1274. osr = 0;
  1275. sbr = 0;
  1276. for (tmp_osr = 4; tmp_osr <= 32; tmp_osr++) {
  1277. /* calculate the temporary sbr value */
  1278. tmp_sbr = (clk / (baudrate * tmp_osr));
  1279. if (tmp_sbr == 0)
  1280. tmp_sbr = 1;
  1281. /*
  1282. * calculate the baud rate difference based on the temporary
  1283. * osr and sbr values
  1284. */
  1285. tmp_diff = clk / (tmp_osr * tmp_sbr) - baudrate;
  1286. /* select best values between sbr and sbr+1 */
  1287. tmp = clk / (tmp_osr * (tmp_sbr + 1));
  1288. if (tmp_diff > (baudrate - tmp)) {
  1289. tmp_diff = baudrate - tmp;
  1290. tmp_sbr++;
  1291. }
  1292. if (tmp_diff <= baud_diff) {
  1293. baud_diff = tmp_diff;
  1294. osr = tmp_osr;
  1295. sbr = tmp_sbr;
  1296. if (!baud_diff)
  1297. break;
  1298. }
  1299. }
  1300. /* handle buadrate outside acceptable rate */
  1301. if (baud_diff > ((baudrate / 100) * 3))
  1302. dev_warn(sport->port.dev,
  1303. "unacceptable baud rate difference of more than 3%%\n");
  1304. tmp = lpuart32_read(&sport->port, UARTBAUD);
  1305. if ((osr > 3) && (osr < 8))
  1306. tmp |= UARTBAUD_BOTHEDGE;
  1307. tmp &= ~(UARTBAUD_OSR_MASK << UARTBAUD_OSR_SHIFT);
  1308. tmp |= (((osr-1) & UARTBAUD_OSR_MASK) << UARTBAUD_OSR_SHIFT);
  1309. tmp &= ~UARTBAUD_SBR_MASK;
  1310. tmp |= sbr & UARTBAUD_SBR_MASK;
  1311. tmp &= ~(UARTBAUD_TDMAE | UARTBAUD_RDMAE);
  1312. lpuart32_write(&sport->port, tmp, UARTBAUD);
  1313. }
  1314. static void
  1315. lpuart32_set_termios(struct uart_port *port, struct ktermios *termios,
  1316. struct ktermios *old)
  1317. {
  1318. struct lpuart_port *sport = container_of(port, struct lpuart_port, port);
  1319. unsigned long flags;
  1320. unsigned long ctrl, old_ctrl, modem;
  1321. unsigned int baud;
  1322. unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;
  1323. ctrl = old_ctrl = lpuart32_read(&sport->port, UARTCTRL);
  1324. modem = lpuart32_read(&sport->port, UARTMODIR);
  1325. /*
  1326. * only support CS8 and CS7, and for CS7 must enable PE.
  1327. * supported mode:
  1328. * - (7,e/o,1)
  1329. * - (8,n,1)
  1330. * - (8,m/s,1)
  1331. * - (8,e/o,1)
  1332. */
  1333. while ((termios->c_cflag & CSIZE) != CS8 &&
  1334. (termios->c_cflag & CSIZE) != CS7) {
  1335. termios->c_cflag &= ~CSIZE;
  1336. termios->c_cflag |= old_csize;
  1337. old_csize = CS8;
  1338. }
  1339. if ((termios->c_cflag & CSIZE) == CS8 ||
  1340. (termios->c_cflag & CSIZE) == CS7)
  1341. ctrl = old_ctrl & ~UARTCTRL_M;
  1342. if (termios->c_cflag & CMSPAR) {
  1343. if ((termios->c_cflag & CSIZE) != CS8) {
  1344. termios->c_cflag &= ~CSIZE;
  1345. termios->c_cflag |= CS8;
  1346. }
  1347. ctrl |= UARTCTRL_M;
  1348. }
  1349. if (termios->c_cflag & CRTSCTS) {
  1350. modem |= (UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
  1351. } else {
  1352. termios->c_cflag &= ~CRTSCTS;
  1353. modem &= ~(UARTMODEM_RXRTSE | UARTMODEM_TXCTSE);
  1354. }
  1355. if (termios->c_cflag & CSTOPB)
  1356. termios->c_cflag &= ~CSTOPB;
  1357. /* parity must be enabled when CS7 to match 8-bits format */
  1358. if ((termios->c_cflag & CSIZE) == CS7)
  1359. termios->c_cflag |= PARENB;
  1360. if ((termios->c_cflag & PARENB)) {
  1361. if (termios->c_cflag & CMSPAR) {
  1362. ctrl &= ~UARTCTRL_PE;
  1363. ctrl |= UARTCTRL_M;
  1364. } else {
  1365. ctrl |= UARTCR1_PE;
  1366. if ((termios->c_cflag & CSIZE) == CS8)
  1367. ctrl |= UARTCTRL_M;
  1368. if (termios->c_cflag & PARODD)
  1369. ctrl |= UARTCTRL_PT;
  1370. else
  1371. ctrl &= ~UARTCTRL_PT;
  1372. }
  1373. }
  1374. /* ask the core to calculate the divisor */
  1375. baud = uart_get_baud_rate(port, termios, old, 50, port->uartclk / 16);
  1376. spin_lock_irqsave(&sport->port.lock, flags);
  1377. sport->port.read_status_mask = 0;
  1378. if (termios->c_iflag & INPCK)
  1379. sport->port.read_status_mask |= (UARTSTAT_FE | UARTSTAT_PE);
  1380. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1381. sport->port.read_status_mask |= UARTSTAT_FE;
  1382. /* characters to ignore */
  1383. sport->port.ignore_status_mask = 0;
  1384. if (termios->c_iflag & IGNPAR)
  1385. sport->port.ignore_status_mask |= UARTSTAT_PE;
  1386. if (termios->c_iflag & IGNBRK) {
  1387. sport->port.ignore_status_mask |= UARTSTAT_FE;
  1388. /*
  1389. * if we're ignoring parity and break indicators,
  1390. * ignore overruns too (for real raw support).
  1391. */
  1392. if (termios->c_iflag & IGNPAR)
  1393. sport->port.ignore_status_mask |= UARTSTAT_OR;
  1394. }
  1395. /* update the per-port timeout */
  1396. uart_update_timeout(port, termios->c_cflag, baud);
  1397. /* wait transmit engin complete */
  1398. while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
  1399. barrier();
  1400. /* disable transmit and receive */
  1401. lpuart32_write(&sport->port, old_ctrl & ~(UARTCTRL_TE | UARTCTRL_RE),
  1402. UARTCTRL);
  1403. lpuart32_serial_setbrg(sport, baud);
  1404. lpuart32_write(&sport->port, modem, UARTMODIR);
  1405. lpuart32_write(&sport->port, ctrl, UARTCTRL);
  1406. /* restore control register */
  1407. spin_unlock_irqrestore(&sport->port.lock, flags);
  1408. }
  1409. static const char *lpuart_type(struct uart_port *port)
  1410. {
  1411. return "FSL_LPUART";
  1412. }
  1413. static void lpuart_release_port(struct uart_port *port)
  1414. {
  1415. /* nothing to do */
  1416. }
  1417. static int lpuart_request_port(struct uart_port *port)
  1418. {
  1419. return 0;
  1420. }
  1421. /* configure/autoconfigure the port */
  1422. static void lpuart_config_port(struct uart_port *port, int flags)
  1423. {
  1424. if (flags & UART_CONFIG_TYPE)
  1425. port->type = PORT_LPUART;
  1426. }
  1427. static int lpuart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1428. {
  1429. int ret = 0;
  1430. if (ser->type != PORT_UNKNOWN && ser->type != PORT_LPUART)
  1431. ret = -EINVAL;
  1432. if (port->irq != ser->irq)
  1433. ret = -EINVAL;
  1434. if (ser->io_type != UPIO_MEM)
  1435. ret = -EINVAL;
  1436. if (port->uartclk / 16 != ser->baud_base)
  1437. ret = -EINVAL;
  1438. if (port->iobase != ser->port)
  1439. ret = -EINVAL;
  1440. if (ser->hub6 != 0)
  1441. ret = -EINVAL;
  1442. return ret;
  1443. }
  1444. static const struct uart_ops lpuart_pops = {
  1445. .tx_empty = lpuart_tx_empty,
  1446. .set_mctrl = lpuart_set_mctrl,
  1447. .get_mctrl = lpuart_get_mctrl,
  1448. .stop_tx = lpuart_stop_tx,
  1449. .start_tx = lpuart_start_tx,
  1450. .stop_rx = lpuart_stop_rx,
  1451. .break_ctl = lpuart_break_ctl,
  1452. .startup = lpuart_startup,
  1453. .shutdown = lpuart_shutdown,
  1454. .set_termios = lpuart_set_termios,
  1455. .type = lpuart_type,
  1456. .request_port = lpuart_request_port,
  1457. .release_port = lpuart_release_port,
  1458. .config_port = lpuart_config_port,
  1459. .verify_port = lpuart_verify_port,
  1460. .flush_buffer = lpuart_flush_buffer,
  1461. #if defined(CONFIG_CONSOLE_POLL)
  1462. .poll_init = lpuart_poll_init,
  1463. .poll_get_char = lpuart_poll_get_char,
  1464. .poll_put_char = lpuart_poll_put_char,
  1465. #endif
  1466. };
  1467. static const struct uart_ops lpuart32_pops = {
  1468. .tx_empty = lpuart32_tx_empty,
  1469. .set_mctrl = lpuart32_set_mctrl,
  1470. .get_mctrl = lpuart32_get_mctrl,
  1471. .stop_tx = lpuart32_stop_tx,
  1472. .start_tx = lpuart32_start_tx,
  1473. .stop_rx = lpuart32_stop_rx,
  1474. .break_ctl = lpuart32_break_ctl,
  1475. .startup = lpuart32_startup,
  1476. .shutdown = lpuart32_shutdown,
  1477. .set_termios = lpuart32_set_termios,
  1478. .type = lpuart_type,
  1479. .request_port = lpuart_request_port,
  1480. .release_port = lpuart_release_port,
  1481. .config_port = lpuart_config_port,
  1482. .verify_port = lpuart_verify_port,
  1483. .flush_buffer = lpuart_flush_buffer,
  1484. #if defined(CONFIG_CONSOLE_POLL)
  1485. .poll_init = lpuart32_poll_init,
  1486. .poll_get_char = lpuart32_poll_get_char,
  1487. .poll_put_char = lpuart32_poll_put_char,
  1488. #endif
  1489. };
  1490. static struct lpuart_port *lpuart_ports[UART_NR];
  1491. #ifdef CONFIG_SERIAL_FSL_LPUART_CONSOLE
  1492. static void lpuart_console_putchar(struct uart_port *port, int ch)
  1493. {
  1494. while (!(readb(port->membase + UARTSR1) & UARTSR1_TDRE))
  1495. barrier();
  1496. writeb(ch, port->membase + UARTDR);
  1497. }
  1498. static void lpuart32_console_putchar(struct uart_port *port, int ch)
  1499. {
  1500. while (!(lpuart32_read(port, UARTSTAT) & UARTSTAT_TDRE))
  1501. barrier();
  1502. lpuart32_write(port, ch, UARTDATA);
  1503. }
  1504. static void
  1505. lpuart_console_write(struct console *co, const char *s, unsigned int count)
  1506. {
  1507. struct lpuart_port *sport = lpuart_ports[co->index];
  1508. unsigned char old_cr2, cr2;
  1509. unsigned long flags;
  1510. int locked = 1;
  1511. if (sport->port.sysrq || oops_in_progress)
  1512. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1513. else
  1514. spin_lock_irqsave(&sport->port.lock, flags);
  1515. /* first save CR2 and then disable interrupts */
  1516. cr2 = old_cr2 = readb(sport->port.membase + UARTCR2);
  1517. cr2 |= (UARTCR2_TE | UARTCR2_RE);
  1518. cr2 &= ~(UARTCR2_TIE | UARTCR2_TCIE | UARTCR2_RIE);
  1519. writeb(cr2, sport->port.membase + UARTCR2);
  1520. uart_console_write(&sport->port, s, count, lpuart_console_putchar);
  1521. /* wait for transmitter finish complete and restore CR2 */
  1522. while (!(readb(sport->port.membase + UARTSR1) & UARTSR1_TC))
  1523. barrier();
  1524. writeb(old_cr2, sport->port.membase + UARTCR2);
  1525. if (locked)
  1526. spin_unlock_irqrestore(&sport->port.lock, flags);
  1527. }
  1528. static void
  1529. lpuart32_console_write(struct console *co, const char *s, unsigned int count)
  1530. {
  1531. struct lpuart_port *sport = lpuart_ports[co->index];
  1532. unsigned long old_cr, cr;
  1533. unsigned long flags;
  1534. int locked = 1;
  1535. if (sport->port.sysrq || oops_in_progress)
  1536. locked = spin_trylock_irqsave(&sport->port.lock, flags);
  1537. else
  1538. spin_lock_irqsave(&sport->port.lock, flags);
  1539. /* first save CR2 and then disable interrupts */
  1540. cr = old_cr = lpuart32_read(&sport->port, UARTCTRL);
  1541. cr |= (UARTCTRL_TE | UARTCTRL_RE);
  1542. cr &= ~(UARTCTRL_TIE | UARTCTRL_TCIE | UARTCTRL_RIE);
  1543. lpuart32_write(&sport->port, cr, UARTCTRL);
  1544. uart_console_write(&sport->port, s, count, lpuart32_console_putchar);
  1545. /* wait for transmitter finish complete and restore CR2 */
  1546. while (!(lpuart32_read(&sport->port, UARTSTAT) & UARTSTAT_TC))
  1547. barrier();
  1548. lpuart32_write(&sport->port, old_cr, UARTCTRL);
  1549. if (locked)
  1550. spin_unlock_irqrestore(&sport->port.lock, flags);
  1551. }
  1552. /*
  1553. * if the port was already initialised (eg, by a boot loader),
  1554. * try to determine the current setup.
  1555. */
  1556. static void __init
  1557. lpuart_console_get_options(struct lpuart_port *sport, int *baud,
  1558. int *parity, int *bits)
  1559. {
  1560. unsigned char cr, bdh, bdl, brfa;
  1561. unsigned int sbr, uartclk, baud_raw;
  1562. cr = readb(sport->port.membase + UARTCR2);
  1563. cr &= UARTCR2_TE | UARTCR2_RE;
  1564. if (!cr)
  1565. return;
  1566. /* ok, the port was enabled */
  1567. cr = readb(sport->port.membase + UARTCR1);
  1568. *parity = 'n';
  1569. if (cr & UARTCR1_PE) {
  1570. if (cr & UARTCR1_PT)
  1571. *parity = 'o';
  1572. else
  1573. *parity = 'e';
  1574. }
  1575. if (cr & UARTCR1_M)
  1576. *bits = 9;
  1577. else
  1578. *bits = 8;
  1579. bdh = readb(sport->port.membase + UARTBDH);
  1580. bdh &= UARTBDH_SBR_MASK;
  1581. bdl = readb(sport->port.membase + UARTBDL);
  1582. sbr = bdh;
  1583. sbr <<= 8;
  1584. sbr |= bdl;
  1585. brfa = readb(sport->port.membase + UARTCR4);
  1586. brfa &= UARTCR4_BRFA_MASK;
  1587. uartclk = clk_get_rate(sport->clk);
  1588. /*
  1589. * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
  1590. */
  1591. baud_raw = uartclk / (16 * (sbr + brfa / 32));
  1592. if (*baud != baud_raw)
  1593. printk(KERN_INFO "Serial: Console lpuart rounded baud rate"
  1594. "from %d to %d\n", baud_raw, *baud);
  1595. }
  1596. static void __init
  1597. lpuart32_console_get_options(struct lpuart_port *sport, int *baud,
  1598. int *parity, int *bits)
  1599. {
  1600. unsigned long cr, bd;
  1601. unsigned int sbr, uartclk, baud_raw;
  1602. cr = lpuart32_read(&sport->port, UARTCTRL);
  1603. cr &= UARTCTRL_TE | UARTCTRL_RE;
  1604. if (!cr)
  1605. return;
  1606. /* ok, the port was enabled */
  1607. cr = lpuart32_read(&sport->port, UARTCTRL);
  1608. *parity = 'n';
  1609. if (cr & UARTCTRL_PE) {
  1610. if (cr & UARTCTRL_PT)
  1611. *parity = 'o';
  1612. else
  1613. *parity = 'e';
  1614. }
  1615. if (cr & UARTCTRL_M)
  1616. *bits = 9;
  1617. else
  1618. *bits = 8;
  1619. bd = lpuart32_read(&sport->port, UARTBAUD);
  1620. bd &= UARTBAUD_SBR_MASK;
  1621. sbr = bd;
  1622. uartclk = clk_get_rate(sport->clk);
  1623. /*
  1624. * baud = mod_clk/(16*(sbr[13]+(brfa)/32)
  1625. */
  1626. baud_raw = uartclk / (16 * sbr);
  1627. if (*baud != baud_raw)
  1628. printk(KERN_INFO "Serial: Console lpuart rounded baud rate"
  1629. "from %d to %d\n", baud_raw, *baud);
  1630. }
  1631. static int __init lpuart_console_setup(struct console *co, char *options)
  1632. {
  1633. struct lpuart_port *sport;
  1634. int baud = 115200;
  1635. int bits = 8;
  1636. int parity = 'n';
  1637. int flow = 'n';
  1638. /*
  1639. * check whether an invalid uart number has been specified, and
  1640. * if so, search for the first available port that does have
  1641. * console support.
  1642. */
  1643. if (co->index == -1 || co->index >= ARRAY_SIZE(lpuart_ports))
  1644. co->index = 0;
  1645. sport = lpuart_ports[co->index];
  1646. if (sport == NULL)
  1647. return -ENODEV;
  1648. if (options)
  1649. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1650. else
  1651. if (lpuart_is_32(sport))
  1652. lpuart32_console_get_options(sport, &baud, &parity, &bits);
  1653. else
  1654. lpuart_console_get_options(sport, &baud, &parity, &bits);
  1655. if (lpuart_is_32(sport))
  1656. lpuart32_setup_watermark(sport);
  1657. else
  1658. lpuart_setup_watermark(sport);
  1659. return uart_set_options(&sport->port, co, baud, parity, bits, flow);
  1660. }
  1661. static struct uart_driver lpuart_reg;
  1662. static struct console lpuart_console = {
  1663. .name = DEV_NAME,
  1664. .write = lpuart_console_write,
  1665. .device = uart_console_device,
  1666. .setup = lpuart_console_setup,
  1667. .flags = CON_PRINTBUFFER,
  1668. .index = -1,
  1669. .data = &lpuart_reg,
  1670. };
  1671. static struct console lpuart32_console = {
  1672. .name = DEV_NAME,
  1673. .write = lpuart32_console_write,
  1674. .device = uart_console_device,
  1675. .setup = lpuart_console_setup,
  1676. .flags = CON_PRINTBUFFER,
  1677. .index = -1,
  1678. .data = &lpuart_reg,
  1679. };
  1680. static void lpuart_early_write(struct console *con, const char *s, unsigned n)
  1681. {
  1682. struct earlycon_device *dev = con->data;
  1683. uart_console_write(&dev->port, s, n, lpuart_console_putchar);
  1684. }
  1685. static void lpuart32_early_write(struct console *con, const char *s, unsigned n)
  1686. {
  1687. struct earlycon_device *dev = con->data;
  1688. uart_console_write(&dev->port, s, n, lpuart32_console_putchar);
  1689. }
  1690. static int __init lpuart_early_console_setup(struct earlycon_device *device,
  1691. const char *opt)
  1692. {
  1693. if (!device->port.membase)
  1694. return -ENODEV;
  1695. device->con->write = lpuart_early_write;
  1696. return 0;
  1697. }
  1698. static int __init lpuart32_early_console_setup(struct earlycon_device *device,
  1699. const char *opt)
  1700. {
  1701. if (!device->port.membase)
  1702. return -ENODEV;
  1703. device->port.iotype = UPIO_MEM32BE;
  1704. device->con->write = lpuart32_early_write;
  1705. return 0;
  1706. }
  1707. static int __init lpuart32_imx_early_console_setup(struct earlycon_device *device,
  1708. const char *opt)
  1709. {
  1710. if (!device->port.membase)
  1711. return -ENODEV;
  1712. device->port.iotype = UPIO_MEM32;
  1713. device->port.membase += IMX_REG_OFF;
  1714. device->con->write = lpuart32_early_write;
  1715. return 0;
  1716. }
  1717. OF_EARLYCON_DECLARE(lpuart, "fsl,vf610-lpuart", lpuart_early_console_setup);
  1718. OF_EARLYCON_DECLARE(lpuart32, "fsl,ls1021a-lpuart", lpuart32_early_console_setup);
  1719. OF_EARLYCON_DECLARE(lpuart32, "fsl,imx7ulp-lpuart", lpuart32_imx_early_console_setup);
  1720. EARLYCON_DECLARE(lpuart, lpuart_early_console_setup);
  1721. EARLYCON_DECLARE(lpuart32, lpuart32_early_console_setup);
  1722. #define LPUART_CONSOLE (&lpuart_console)
  1723. #define LPUART32_CONSOLE (&lpuart32_console)
  1724. #else
  1725. #define LPUART_CONSOLE NULL
  1726. #define LPUART32_CONSOLE NULL
  1727. #endif
  1728. static struct uart_driver lpuart_reg = {
  1729. .owner = THIS_MODULE,
  1730. .driver_name = DRIVER_NAME,
  1731. .dev_name = DEV_NAME,
  1732. .nr = ARRAY_SIZE(lpuart_ports),
  1733. .cons = LPUART_CONSOLE,
  1734. };
  1735. static int lpuart_probe(struct platform_device *pdev)
  1736. {
  1737. const struct of_device_id *of_id = of_match_device(lpuart_dt_ids,
  1738. &pdev->dev);
  1739. const struct lpuart_soc_data *sdata = of_id->data;
  1740. struct device_node *np = pdev->dev.of_node;
  1741. struct lpuart_port *sport;
  1742. struct resource *res;
  1743. int ret;
  1744. sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
  1745. if (!sport)
  1746. return -ENOMEM;
  1747. pdev->dev.coherent_dma_mask = 0;
  1748. ret = of_alias_get_id(np, "serial");
  1749. if (ret < 0) {
  1750. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
  1751. return ret;
  1752. }
  1753. if (ret >= ARRAY_SIZE(lpuart_ports)) {
  1754. dev_err(&pdev->dev, "serial%d out of range\n", ret);
  1755. return -EINVAL;
  1756. }
  1757. sport->port.line = ret;
  1758. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  1759. sport->port.membase = devm_ioremap_resource(&pdev->dev, res);
  1760. if (IS_ERR(sport->port.membase))
  1761. return PTR_ERR(sport->port.membase);
  1762. sport->port.membase += sdata->reg_off;
  1763. sport->port.mapbase = res->start;
  1764. sport->port.dev = &pdev->dev;
  1765. sport->port.type = PORT_LPUART;
  1766. ret = platform_get_irq(pdev, 0);
  1767. if (ret < 0) {
  1768. dev_err(&pdev->dev, "cannot obtain irq\n");
  1769. return ret;
  1770. }
  1771. sport->port.irq = ret;
  1772. sport->port.iotype = sdata->iotype;
  1773. if (lpuart_is_32(sport))
  1774. sport->port.ops = &lpuart32_pops;
  1775. else
  1776. sport->port.ops = &lpuart_pops;
  1777. sport->port.flags = UPF_BOOT_AUTOCONF;
  1778. sport->port.rs485_config = lpuart_config_rs485;
  1779. sport->clk = devm_clk_get(&pdev->dev, "ipg");
  1780. if (IS_ERR(sport->clk)) {
  1781. ret = PTR_ERR(sport->clk);
  1782. dev_err(&pdev->dev, "failed to get uart clk: %d\n", ret);
  1783. return ret;
  1784. }
  1785. ret = clk_prepare_enable(sport->clk);
  1786. if (ret) {
  1787. dev_err(&pdev->dev, "failed to enable uart clk: %d\n", ret);
  1788. return ret;
  1789. }
  1790. sport->port.uartclk = clk_get_rate(sport->clk);
  1791. lpuart_ports[sport->port.line] = sport;
  1792. platform_set_drvdata(pdev, &sport->port);
  1793. if (lpuart_is_32(sport)) {
  1794. lpuart_reg.cons = LPUART32_CONSOLE;
  1795. ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart32_int, 0,
  1796. DRIVER_NAME, sport);
  1797. } else {
  1798. lpuart_reg.cons = LPUART_CONSOLE;
  1799. ret = devm_request_irq(&pdev->dev, sport->port.irq, lpuart_int, 0,
  1800. DRIVER_NAME, sport);
  1801. }
  1802. if (ret)
  1803. goto failed_irq_request;
  1804. ret = uart_add_one_port(&lpuart_reg, &sport->port);
  1805. if (ret)
  1806. goto failed_attach_port;
  1807. uart_get_rs485_mode(&pdev->dev, &sport->port.rs485);
  1808. if (sport->port.rs485.flags & SER_RS485_RX_DURING_TX)
  1809. dev_err(&pdev->dev, "driver doesn't support RX during TX\n");
  1810. if (sport->port.rs485.delay_rts_before_send ||
  1811. sport->port.rs485.delay_rts_after_send)
  1812. dev_err(&pdev->dev, "driver doesn't support RTS delays\n");
  1813. lpuart_config_rs485(&sport->port, &sport->port.rs485);
  1814. sport->dma_tx_chan = dma_request_slave_channel(sport->port.dev, "tx");
  1815. if (!sport->dma_tx_chan)
  1816. dev_info(sport->port.dev, "DMA tx channel request failed, "
  1817. "operating without tx DMA\n");
  1818. sport->dma_rx_chan = dma_request_slave_channel(sport->port.dev, "rx");
  1819. if (!sport->dma_rx_chan)
  1820. dev_info(sport->port.dev, "DMA rx channel request failed, "
  1821. "operating without rx DMA\n");
  1822. return 0;
  1823. failed_attach_port:
  1824. failed_irq_request:
  1825. clk_disable_unprepare(sport->clk);
  1826. return ret;
  1827. }
  1828. static int lpuart_remove(struct platform_device *pdev)
  1829. {
  1830. struct lpuart_port *sport = platform_get_drvdata(pdev);
  1831. uart_remove_one_port(&lpuart_reg, &sport->port);
  1832. clk_disable_unprepare(sport->clk);
  1833. if (sport->dma_tx_chan)
  1834. dma_release_channel(sport->dma_tx_chan);
  1835. if (sport->dma_rx_chan)
  1836. dma_release_channel(sport->dma_rx_chan);
  1837. return 0;
  1838. }
  1839. #ifdef CONFIG_PM_SLEEP
  1840. static int lpuart_suspend(struct device *dev)
  1841. {
  1842. struct lpuart_port *sport = dev_get_drvdata(dev);
  1843. unsigned long temp;
  1844. bool irq_wake;
  1845. if (lpuart_is_32(sport)) {
  1846. /* disable Rx/Tx and interrupts */
  1847. temp = lpuart32_read(&sport->port, UARTCTRL);
  1848. temp &= ~(UARTCTRL_TE | UARTCTRL_TIE | UARTCTRL_TCIE);
  1849. lpuart32_write(&sport->port, temp, UARTCTRL);
  1850. } else {
  1851. /* disable Rx/Tx and interrupts */
  1852. temp = readb(sport->port.membase + UARTCR2);
  1853. temp &= ~(UARTCR2_TE | UARTCR2_TIE | UARTCR2_TCIE);
  1854. writeb(temp, sport->port.membase + UARTCR2);
  1855. }
  1856. uart_suspend_port(&lpuart_reg, &sport->port);
  1857. /* uart_suspend_port() might set wakeup flag */
  1858. irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  1859. if (sport->lpuart_dma_rx_use) {
  1860. /*
  1861. * EDMA driver during suspend will forcefully release any
  1862. * non-idle DMA channels. If port wakeup is enabled or if port
  1863. * is console port or 'no_console_suspend' is set the Rx DMA
  1864. * cannot resume as as expected, hence gracefully release the
  1865. * Rx DMA path before suspend and start Rx DMA path on resume.
  1866. */
  1867. if (irq_wake) {
  1868. del_timer_sync(&sport->lpuart_timer);
  1869. lpuart_dma_rx_free(&sport->port);
  1870. }
  1871. /* Disable Rx DMA to use UART port as wakeup source */
  1872. writeb(readb(sport->port.membase + UARTCR5) & ~UARTCR5_RDMAS,
  1873. sport->port.membase + UARTCR5);
  1874. }
  1875. if (sport->lpuart_dma_tx_use) {
  1876. sport->dma_tx_in_progress = false;
  1877. dmaengine_terminate_all(sport->dma_tx_chan);
  1878. }
  1879. if (sport->port.suspended && !irq_wake)
  1880. clk_disable_unprepare(sport->clk);
  1881. return 0;
  1882. }
  1883. static int lpuart_resume(struct device *dev)
  1884. {
  1885. struct lpuart_port *sport = dev_get_drvdata(dev);
  1886. bool irq_wake = irqd_is_wakeup_set(irq_get_irq_data(sport->port.irq));
  1887. unsigned long temp;
  1888. if (sport->port.suspended && !irq_wake)
  1889. clk_prepare_enable(sport->clk);
  1890. if (lpuart_is_32(sport)) {
  1891. lpuart32_setup_watermark(sport);
  1892. temp = lpuart32_read(&sport->port, UARTCTRL);
  1893. temp |= (UARTCTRL_RIE | UARTCTRL_TIE | UARTCTRL_RE |
  1894. UARTCTRL_TE | UARTCTRL_ILIE);
  1895. lpuart32_write(&sport->port, temp, UARTCTRL);
  1896. } else {
  1897. lpuart_setup_watermark(sport);
  1898. temp = readb(sport->port.membase + UARTCR2);
  1899. temp |= (UARTCR2_RIE | UARTCR2_TIE | UARTCR2_RE | UARTCR2_TE);
  1900. writeb(temp, sport->port.membase + UARTCR2);
  1901. }
  1902. if (sport->lpuart_dma_rx_use) {
  1903. if (irq_wake) {
  1904. if (!lpuart_start_rx_dma(sport))
  1905. rx_dma_timer_init(sport);
  1906. else
  1907. sport->lpuart_dma_rx_use = false;
  1908. }
  1909. }
  1910. if (sport->dma_tx_chan && !lpuart_dma_tx_request(&sport->port)) {
  1911. init_waitqueue_head(&sport->dma_wait);
  1912. sport->lpuart_dma_tx_use = true;
  1913. writeb(readb(sport->port.membase + UARTCR5) |
  1914. UARTCR5_TDMAS, sport->port.membase + UARTCR5);
  1915. } else {
  1916. sport->lpuart_dma_tx_use = false;
  1917. }
  1918. uart_resume_port(&lpuart_reg, &sport->port);
  1919. return 0;
  1920. }
  1921. #endif
  1922. static SIMPLE_DEV_PM_OPS(lpuart_pm_ops, lpuart_suspend, lpuart_resume);
  1923. static struct platform_driver lpuart_driver = {
  1924. .probe = lpuart_probe,
  1925. .remove = lpuart_remove,
  1926. .driver = {
  1927. .name = "fsl-lpuart",
  1928. .of_match_table = lpuart_dt_ids,
  1929. .pm = &lpuart_pm_ops,
  1930. },
  1931. };
  1932. static int __init lpuart_serial_init(void)
  1933. {
  1934. int ret = uart_register_driver(&lpuart_reg);
  1935. if (ret)
  1936. return ret;
  1937. ret = platform_driver_register(&lpuart_driver);
  1938. if (ret)
  1939. uart_unregister_driver(&lpuart_reg);
  1940. return ret;
  1941. }
  1942. static void __exit lpuart_serial_exit(void)
  1943. {
  1944. platform_driver_unregister(&lpuart_driver);
  1945. uart_unregister_driver(&lpuart_reg);
  1946. }
  1947. module_init(lpuart_serial_init);
  1948. module_exit(lpuart_serial_exit);
  1949. MODULE_DESCRIPTION("Freescale lpuart serial port driver");
  1950. MODULE_LICENSE("GPL v2");