stm32-usart.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) Maxime Coquelin 2015
  4. * Copyright (C) STMicroelectronics SA 2017
  5. * Authors: Maxime Coquelin <mcoquelin.stm32@gmail.com>
  6. * Gerald Baeza <gerald.baeza@foss.st.com>
  7. * Erwan Le Ray <erwan.leray@foss.st.com>
  8. *
  9. * Inspired by st-asc.c from STMicroelectronics (c)
  10. */
  11. #include <linux/bitfield.h>
  12. #include <linux/clk.h>
  13. #include <linux/console.h>
  14. #include <linux/delay.h>
  15. #include <linux/dma-direction.h>
  16. #include <linux/dmaengine.h>
  17. #include <linux/dma-mapping.h>
  18. #include <linux/io.h>
  19. #include <linux/iopoll.h>
  20. #include <linux/irq.h>
  21. #include <linux/module.h>
  22. #include <linux/of.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/pinctrl/consumer.h>
  25. #include <linux/platform_device.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/pm_wakeirq.h>
  28. #include <linux/serial_core.h>
  29. #include <linux/serial.h>
  30. #include <linux/spinlock.h>
  31. #include <linux/sysrq.h>
  32. #include <linux/tty_flip.h>
  33. #include <linux/tty.h>
  34. #include "serial_mctrl_gpio.h"
  35. #include "stm32-usart.h"
  36. /* Register offsets */
  37. static struct stm32_usart_info __maybe_unused stm32f4_info = {
  38. .ofs = {
  39. .isr = 0x00,
  40. .rdr = 0x04,
  41. .tdr = 0x04,
  42. .brr = 0x08,
  43. .cr1 = 0x0c,
  44. .cr2 = 0x10,
  45. .cr3 = 0x14,
  46. .gtpr = 0x18,
  47. .rtor = UNDEF_REG,
  48. .rqr = UNDEF_REG,
  49. .icr = UNDEF_REG,
  50. .presc = UNDEF_REG,
  51. .hwcfgr1 = UNDEF_REG,
  52. },
  53. .cfg = {
  54. .uart_enable_bit = 13,
  55. .has_7bits_data = false,
  56. }
  57. };
  58. static struct stm32_usart_info __maybe_unused stm32f7_info = {
  59. .ofs = {
  60. .cr1 = 0x00,
  61. .cr2 = 0x04,
  62. .cr3 = 0x08,
  63. .brr = 0x0c,
  64. .gtpr = 0x10,
  65. .rtor = 0x14,
  66. .rqr = 0x18,
  67. .isr = 0x1c,
  68. .icr = 0x20,
  69. .rdr = 0x24,
  70. .tdr = 0x28,
  71. .presc = UNDEF_REG,
  72. .hwcfgr1 = UNDEF_REG,
  73. },
  74. .cfg = {
  75. .uart_enable_bit = 0,
  76. .has_7bits_data = true,
  77. .has_swap = true,
  78. }
  79. };
  80. static struct stm32_usart_info __maybe_unused stm32h7_info = {
  81. .ofs = {
  82. .cr1 = 0x00,
  83. .cr2 = 0x04,
  84. .cr3 = 0x08,
  85. .brr = 0x0c,
  86. .gtpr = 0x10,
  87. .rtor = 0x14,
  88. .rqr = 0x18,
  89. .isr = 0x1c,
  90. .icr = 0x20,
  91. .rdr = 0x24,
  92. .tdr = 0x28,
  93. .presc = 0x2c,
  94. .hwcfgr1 = 0x3f0,
  95. },
  96. .cfg = {
  97. .uart_enable_bit = 0,
  98. .has_7bits_data = true,
  99. .has_swap = true,
  100. .has_wakeup = true,
  101. .has_fifo = true,
  102. }
  103. };
  104. static void stm32_usart_stop_tx(struct uart_port *port);
  105. static void stm32_usart_transmit_chars(struct uart_port *port);
  106. static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch);
  107. static inline struct stm32_port *to_stm32_port(struct uart_port *port)
  108. {
  109. return container_of(port, struct stm32_port, port);
  110. }
  111. static void stm32_usart_set_bits(struct uart_port *port, u32 reg, u32 bits)
  112. {
  113. u32 val;
  114. val = readl_relaxed(port->membase + reg);
  115. val |= bits;
  116. writel_relaxed(val, port->membase + reg);
  117. }
  118. static void stm32_usart_clr_bits(struct uart_port *port, u32 reg, u32 bits)
  119. {
  120. u32 val;
  121. val = readl_relaxed(port->membase + reg);
  122. val &= ~bits;
  123. writel_relaxed(val, port->membase + reg);
  124. }
  125. static unsigned int stm32_usart_tx_empty(struct uart_port *port)
  126. {
  127. struct stm32_port *stm32_port = to_stm32_port(port);
  128. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  129. if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
  130. return TIOCSER_TEMT;
  131. return 0;
  132. }
  133. static void stm32_usart_rs485_rts_enable(struct uart_port *port)
  134. {
  135. struct stm32_port *stm32_port = to_stm32_port(port);
  136. struct serial_rs485 *rs485conf = &port->rs485;
  137. if (stm32_port->hw_flow_control ||
  138. !(rs485conf->flags & SER_RS485_ENABLED))
  139. return;
  140. if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
  141. mctrl_gpio_set(stm32_port->gpios,
  142. stm32_port->port.mctrl | TIOCM_RTS);
  143. } else {
  144. mctrl_gpio_set(stm32_port->gpios,
  145. stm32_port->port.mctrl & ~TIOCM_RTS);
  146. }
  147. }
  148. static void stm32_usart_rs485_rts_disable(struct uart_port *port)
  149. {
  150. struct stm32_port *stm32_port = to_stm32_port(port);
  151. struct serial_rs485 *rs485conf = &port->rs485;
  152. if (stm32_port->hw_flow_control ||
  153. !(rs485conf->flags & SER_RS485_ENABLED))
  154. return;
  155. if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
  156. mctrl_gpio_set(stm32_port->gpios,
  157. stm32_port->port.mctrl & ~TIOCM_RTS);
  158. } else {
  159. mctrl_gpio_set(stm32_port->gpios,
  160. stm32_port->port.mctrl | TIOCM_RTS);
  161. }
  162. }
  163. static void stm32_usart_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
  164. u32 delay_DDE, u32 baud)
  165. {
  166. u32 rs485_deat_dedt;
  167. u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
  168. bool over8;
  169. *cr3 |= USART_CR3_DEM;
  170. over8 = *cr1 & USART_CR1_OVER8;
  171. *cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
  172. if (over8)
  173. rs485_deat_dedt = delay_ADE * baud * 8;
  174. else
  175. rs485_deat_dedt = delay_ADE * baud * 16;
  176. rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
  177. rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
  178. rs485_deat_dedt_max : rs485_deat_dedt;
  179. rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
  180. USART_CR1_DEAT_MASK;
  181. *cr1 |= rs485_deat_dedt;
  182. if (over8)
  183. rs485_deat_dedt = delay_DDE * baud * 8;
  184. else
  185. rs485_deat_dedt = delay_DDE * baud * 16;
  186. rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
  187. rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
  188. rs485_deat_dedt_max : rs485_deat_dedt;
  189. rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
  190. USART_CR1_DEDT_MASK;
  191. *cr1 |= rs485_deat_dedt;
  192. }
  193. static int stm32_usart_config_rs485(struct uart_port *port, struct ktermios *termios,
  194. struct serial_rs485 *rs485conf)
  195. {
  196. struct stm32_port *stm32_port = to_stm32_port(port);
  197. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  198. const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  199. u32 usartdiv, baud, cr1, cr3;
  200. bool over8;
  201. stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  202. if (rs485conf->flags & SER_RS485_ENABLED) {
  203. cr1 = readl_relaxed(port->membase + ofs->cr1);
  204. cr3 = readl_relaxed(port->membase + ofs->cr3);
  205. usartdiv = readl_relaxed(port->membase + ofs->brr);
  206. usartdiv = usartdiv & GENMASK(15, 0);
  207. over8 = cr1 & USART_CR1_OVER8;
  208. if (over8)
  209. usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
  210. << USART_BRR_04_R_SHIFT;
  211. baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
  212. stm32_usart_config_reg_rs485(&cr1, &cr3,
  213. rs485conf->delay_rts_before_send,
  214. rs485conf->delay_rts_after_send,
  215. baud);
  216. if (rs485conf->flags & SER_RS485_RTS_ON_SEND)
  217. cr3 &= ~USART_CR3_DEP;
  218. else
  219. cr3 |= USART_CR3_DEP;
  220. writel_relaxed(cr3, port->membase + ofs->cr3);
  221. writel_relaxed(cr1, port->membase + ofs->cr1);
  222. if (!port->rs485_rx_during_tx_gpio)
  223. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  224. } else {
  225. stm32_usart_clr_bits(port, ofs->cr3,
  226. USART_CR3_DEM | USART_CR3_DEP);
  227. stm32_usart_clr_bits(port, ofs->cr1,
  228. USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
  229. }
  230. stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  231. /* Adjust RTS polarity in case it's driven in software */
  232. if (stm32_usart_tx_empty(port))
  233. stm32_usart_rs485_rts_disable(port);
  234. else
  235. stm32_usart_rs485_rts_enable(port);
  236. return 0;
  237. }
  238. static int stm32_usart_init_rs485(struct uart_port *port,
  239. struct platform_device *pdev)
  240. {
  241. struct serial_rs485 *rs485conf = &port->rs485;
  242. rs485conf->flags = 0;
  243. rs485conf->delay_rts_before_send = 0;
  244. rs485conf->delay_rts_after_send = 0;
  245. if (!pdev->dev.of_node)
  246. return -ENODEV;
  247. return uart_get_rs485_mode(port);
  248. }
  249. static bool stm32_usart_rx_dma_started(struct stm32_port *stm32_port)
  250. {
  251. return stm32_port->rx_ch ? stm32_port->rx_dma_busy : false;
  252. }
  253. static void stm32_usart_rx_dma_terminate(struct stm32_port *stm32_port)
  254. {
  255. dmaengine_terminate_async(stm32_port->rx_ch);
  256. stm32_port->rx_dma_busy = false;
  257. }
  258. static int stm32_usart_dma_pause_resume(struct stm32_port *stm32_port,
  259. struct dma_chan *chan,
  260. enum dma_status expected_status,
  261. int dmaengine_pause_or_resume(struct dma_chan *),
  262. bool stm32_usart_xx_dma_started(struct stm32_port *),
  263. void stm32_usart_xx_dma_terminate(struct stm32_port *))
  264. {
  265. struct uart_port *port = &stm32_port->port;
  266. enum dma_status dma_status;
  267. int ret;
  268. if (!stm32_usart_xx_dma_started(stm32_port))
  269. return -EPERM;
  270. dma_status = dmaengine_tx_status(chan, chan->cookie, NULL);
  271. if (dma_status != expected_status)
  272. return -EAGAIN;
  273. ret = dmaengine_pause_or_resume(chan);
  274. if (ret) {
  275. dev_err(port->dev, "DMA failed with error code: %d\n", ret);
  276. stm32_usart_xx_dma_terminate(stm32_port);
  277. }
  278. return ret;
  279. }
  280. static int stm32_usart_rx_dma_pause(struct stm32_port *stm32_port)
  281. {
  282. return stm32_usart_dma_pause_resume(stm32_port, stm32_port->rx_ch,
  283. DMA_IN_PROGRESS, dmaengine_pause,
  284. stm32_usart_rx_dma_started,
  285. stm32_usart_rx_dma_terminate);
  286. }
  287. static int stm32_usart_rx_dma_resume(struct stm32_port *stm32_port)
  288. {
  289. return stm32_usart_dma_pause_resume(stm32_port, stm32_port->rx_ch,
  290. DMA_PAUSED, dmaengine_resume,
  291. stm32_usart_rx_dma_started,
  292. stm32_usart_rx_dma_terminate);
  293. }
  294. /* Return true when data is pending (in pio mode), and false when no data is pending. */
  295. static bool stm32_usart_pending_rx_pio(struct uart_port *port, u32 *sr)
  296. {
  297. struct stm32_port *stm32_port = to_stm32_port(port);
  298. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  299. *sr = readl_relaxed(port->membase + ofs->isr);
  300. /* Get pending characters in RDR or FIFO */
  301. if (*sr & USART_SR_RXNE) {
  302. /* Get all pending characters from the RDR or the FIFO when using interrupts */
  303. if (!stm32_usart_rx_dma_started(stm32_port))
  304. return true;
  305. /* Handle only RX data errors when using DMA */
  306. if (*sr & USART_SR_ERR_MASK)
  307. return true;
  308. }
  309. return false;
  310. }
  311. static u8 stm32_usart_get_char_pio(struct uart_port *port)
  312. {
  313. struct stm32_port *stm32_port = to_stm32_port(port);
  314. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  315. unsigned long c;
  316. c = readl_relaxed(port->membase + ofs->rdr);
  317. /* Apply RDR data mask */
  318. c &= stm32_port->rdr_mask;
  319. return c;
  320. }
  321. static unsigned int stm32_usart_receive_chars_pio(struct uart_port *port)
  322. {
  323. struct stm32_port *stm32_port = to_stm32_port(port);
  324. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  325. unsigned int size = 0;
  326. u32 sr;
  327. u8 c, flag;
  328. while (stm32_usart_pending_rx_pio(port, &sr)) {
  329. sr |= USART_SR_DUMMY_RX;
  330. flag = TTY_NORMAL;
  331. /*
  332. * Status bits has to be cleared before reading the RDR:
  333. * In FIFO mode, reading the RDR will pop the next data
  334. * (if any) along with its status bits into the SR.
  335. * Not doing so leads to misalignement between RDR and SR,
  336. * and clear status bits of the next rx data.
  337. *
  338. * Clear errors flags for stm32f7 and stm32h7 compatible
  339. * devices. On stm32f4 compatible devices, the error bit is
  340. * cleared by the sequence [read SR - read DR].
  341. */
  342. if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
  343. writel_relaxed(sr & USART_SR_ERR_MASK,
  344. port->membase + ofs->icr);
  345. c = stm32_usart_get_char_pio(port);
  346. port->icount.rx++;
  347. size++;
  348. if (sr & USART_SR_ERR_MASK) {
  349. if (sr & USART_SR_ORE) {
  350. port->icount.overrun++;
  351. } else if (sr & USART_SR_PE) {
  352. port->icount.parity++;
  353. } else if (sr & USART_SR_FE) {
  354. /* Break detection if character is null */
  355. if (!c) {
  356. port->icount.brk++;
  357. if (uart_handle_break(port))
  358. continue;
  359. } else {
  360. port->icount.frame++;
  361. }
  362. }
  363. sr &= port->read_status_mask;
  364. if (sr & USART_SR_PE) {
  365. flag = TTY_PARITY;
  366. } else if (sr & USART_SR_FE) {
  367. if (!c)
  368. flag = TTY_BREAK;
  369. else
  370. flag = TTY_FRAME;
  371. }
  372. }
  373. if (uart_prepare_sysrq_char(port, c))
  374. continue;
  375. uart_insert_char(port, sr, USART_SR_ORE, c, flag);
  376. }
  377. return size;
  378. }
  379. static void stm32_usart_push_buffer_dma(struct uart_port *port, unsigned int dma_size)
  380. {
  381. struct stm32_port *stm32_port = to_stm32_port(port);
  382. struct tty_port *ttyport = &stm32_port->port.state->port;
  383. unsigned char *dma_start;
  384. int dma_count, i;
  385. dma_start = stm32_port->rx_buf + (RX_BUF_L - stm32_port->last_res);
  386. /*
  387. * Apply rdr_mask on buffer in order to mask parity bit.
  388. * This loop is useless in cs8 mode because DMA copies only
  389. * 8 bits and already ignores parity bit.
  390. */
  391. if (!(stm32_port->rdr_mask == (BIT(8) - 1)))
  392. for (i = 0; i < dma_size; i++)
  393. *(dma_start + i) &= stm32_port->rdr_mask;
  394. dma_count = tty_insert_flip_string(ttyport, dma_start, dma_size);
  395. port->icount.rx += dma_count;
  396. if (dma_count != dma_size)
  397. port->icount.buf_overrun++;
  398. stm32_port->last_res -= dma_count;
  399. if (stm32_port->last_res == 0)
  400. stm32_port->last_res = RX_BUF_L;
  401. }
  402. static unsigned int stm32_usart_receive_chars_dma(struct uart_port *port)
  403. {
  404. struct stm32_port *stm32_port = to_stm32_port(port);
  405. unsigned int dma_size, size = 0;
  406. /* DMA buffer is configured in cyclic mode and handles the rollback of the buffer. */
  407. if (stm32_port->rx_dma_state.residue > stm32_port->last_res) {
  408. /* Conditional first part: from last_res to end of DMA buffer */
  409. dma_size = stm32_port->last_res;
  410. stm32_usart_push_buffer_dma(port, dma_size);
  411. size = dma_size;
  412. }
  413. dma_size = stm32_port->last_res - stm32_port->rx_dma_state.residue;
  414. stm32_usart_push_buffer_dma(port, dma_size);
  415. size += dma_size;
  416. return size;
  417. }
  418. static unsigned int stm32_usart_receive_chars(struct uart_port *port, bool force_dma_flush)
  419. {
  420. struct stm32_port *stm32_port = to_stm32_port(port);
  421. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  422. enum dma_status rx_dma_status;
  423. u32 sr;
  424. unsigned int size = 0;
  425. if (stm32_usart_rx_dma_started(stm32_port) || force_dma_flush) {
  426. rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
  427. stm32_port->rx_ch->cookie,
  428. &stm32_port->rx_dma_state);
  429. if (rx_dma_status == DMA_IN_PROGRESS ||
  430. rx_dma_status == DMA_PAUSED) {
  431. /* Empty DMA buffer */
  432. size = stm32_usart_receive_chars_dma(port);
  433. sr = readl_relaxed(port->membase + ofs->isr);
  434. if (sr & USART_SR_ERR_MASK) {
  435. /* Disable DMA request line */
  436. stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
  437. /* Switch to PIO mode to handle the errors */
  438. size += stm32_usart_receive_chars_pio(port);
  439. /* Switch back to DMA mode */
  440. stm32_usart_set_bits(port, ofs->cr3, USART_CR3_DMAR);
  441. }
  442. } else {
  443. /* Disable RX DMA */
  444. stm32_usart_rx_dma_terminate(stm32_port);
  445. /* Fall back to interrupt mode */
  446. dev_dbg(port->dev, "DMA error, fallback to irq mode\n");
  447. size = stm32_usart_receive_chars_pio(port);
  448. }
  449. } else {
  450. size = stm32_usart_receive_chars_pio(port);
  451. }
  452. return size;
  453. }
  454. static void stm32_usart_rx_dma_complete(void *arg)
  455. {
  456. struct uart_port *port = arg;
  457. struct tty_port *tport = &port->state->port;
  458. unsigned int size;
  459. unsigned long flags;
  460. uart_port_lock_irqsave(port, &flags);
  461. size = stm32_usart_receive_chars(port, false);
  462. uart_unlock_and_check_sysrq_irqrestore(port, flags);
  463. if (size)
  464. tty_flip_buffer_push(tport);
  465. }
  466. static int stm32_usart_rx_dma_start_or_resume(struct uart_port *port)
  467. {
  468. struct stm32_port *stm32_port = to_stm32_port(port);
  469. struct dma_async_tx_descriptor *desc;
  470. enum dma_status rx_dma_status;
  471. int ret;
  472. if (stm32_port->throttled)
  473. return 0;
  474. if (stm32_port->rx_dma_busy) {
  475. rx_dma_status = dmaengine_tx_status(stm32_port->rx_ch,
  476. stm32_port->rx_ch->cookie,
  477. NULL);
  478. if (rx_dma_status == DMA_IN_PROGRESS)
  479. return 0;
  480. if (rx_dma_status == DMA_PAUSED && !stm32_usart_rx_dma_resume(stm32_port))
  481. return 0;
  482. dev_err(port->dev, "DMA failed : status error.\n");
  483. stm32_usart_rx_dma_terminate(stm32_port);
  484. }
  485. stm32_port->rx_dma_busy = true;
  486. stm32_port->last_res = RX_BUF_L;
  487. /* Prepare a DMA cyclic transaction */
  488. desc = dmaengine_prep_dma_cyclic(stm32_port->rx_ch,
  489. stm32_port->rx_dma_buf,
  490. RX_BUF_L, RX_BUF_P,
  491. DMA_DEV_TO_MEM,
  492. DMA_PREP_INTERRUPT);
  493. if (!desc) {
  494. dev_err(port->dev, "rx dma prep cyclic failed\n");
  495. stm32_port->rx_dma_busy = false;
  496. return -ENODEV;
  497. }
  498. desc->callback = stm32_usart_rx_dma_complete;
  499. desc->callback_param = port;
  500. /* Push current DMA transaction in the pending queue */
  501. ret = dma_submit_error(dmaengine_submit(desc));
  502. if (ret) {
  503. dmaengine_terminate_sync(stm32_port->rx_ch);
  504. stm32_port->rx_dma_busy = false;
  505. return ret;
  506. }
  507. /* Issue pending DMA requests */
  508. dma_async_issue_pending(stm32_port->rx_ch);
  509. return 0;
  510. }
  511. static void stm32_usart_tx_dma_terminate(struct stm32_port *stm32_port)
  512. {
  513. dmaengine_terminate_async(stm32_port->tx_ch);
  514. stm32_port->tx_dma_busy = false;
  515. }
  516. static bool stm32_usart_tx_dma_started(struct stm32_port *stm32_port)
  517. {
  518. /*
  519. * We cannot use the function "dmaengine_tx_status" to know the
  520. * status of DMA. This function does not show if the "dma complete"
  521. * callback of the DMA transaction has been called. So we prefer
  522. * to use "tx_dma_busy" flag to prevent dual DMA transaction at the
  523. * same time.
  524. */
  525. return stm32_port->tx_dma_busy;
  526. }
  527. static int stm32_usart_tx_dma_pause(struct stm32_port *stm32_port)
  528. {
  529. return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
  530. DMA_IN_PROGRESS, dmaengine_pause,
  531. stm32_usart_tx_dma_started,
  532. stm32_usart_tx_dma_terminate);
  533. }
  534. static int stm32_usart_tx_dma_resume(struct stm32_port *stm32_port)
  535. {
  536. return stm32_usart_dma_pause_resume(stm32_port, stm32_port->tx_ch,
  537. DMA_PAUSED, dmaengine_resume,
  538. stm32_usart_tx_dma_started,
  539. stm32_usart_tx_dma_terminate);
  540. }
  541. static void stm32_usart_tx_dma_complete(void *arg)
  542. {
  543. struct uart_port *port = arg;
  544. struct stm32_port *stm32port = to_stm32_port(port);
  545. unsigned long flags;
  546. stm32_usart_tx_dma_terminate(stm32port);
  547. /* Let's see if we have pending data to send */
  548. uart_port_lock_irqsave(port, &flags);
  549. stm32_usart_transmit_chars(port);
  550. uart_port_unlock_irqrestore(port, flags);
  551. }
  552. static void stm32_usart_tx_interrupt_enable(struct uart_port *port)
  553. {
  554. struct stm32_port *stm32_port = to_stm32_port(port);
  555. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  556. /*
  557. * Enables TX FIFO threashold irq when FIFO is enabled,
  558. * or TX empty irq when FIFO is disabled
  559. */
  560. if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
  561. stm32_usart_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
  562. else
  563. stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
  564. }
  565. static void stm32_usart_tc_interrupt_enable(struct uart_port *port)
  566. {
  567. struct stm32_port *stm32_port = to_stm32_port(port);
  568. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  569. stm32_usart_set_bits(port, ofs->cr1, USART_CR1_TCIE);
  570. }
  571. static void stm32_usart_tx_interrupt_disable(struct uart_port *port)
  572. {
  573. struct stm32_port *stm32_port = to_stm32_port(port);
  574. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  575. if (stm32_port->fifoen && stm32_port->txftcfg >= 0)
  576. stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
  577. else
  578. stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
  579. }
  580. static void stm32_usart_tc_interrupt_disable(struct uart_port *port)
  581. {
  582. struct stm32_port *stm32_port = to_stm32_port(port);
  583. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  584. stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_TCIE);
  585. }
  586. static void stm32_usart_transmit_chars_pio(struct uart_port *port)
  587. {
  588. struct stm32_port *stm32_port = to_stm32_port(port);
  589. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  590. struct tty_port *tport = &port->state->port;
  591. while (1) {
  592. unsigned char ch;
  593. /* Check that TDR is empty before filling FIFO */
  594. if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
  595. break;
  596. if (!uart_fifo_get(port, &ch))
  597. break;
  598. writel_relaxed(ch, port->membase + ofs->tdr);
  599. }
  600. /* rely on TXE irq (mask or unmask) for sending remaining data */
  601. if (kfifo_is_empty(&tport->xmit_fifo))
  602. stm32_usart_tx_interrupt_disable(port);
  603. else
  604. stm32_usart_tx_interrupt_enable(port);
  605. }
  606. static void stm32_usart_transmit_chars_dma(struct uart_port *port)
  607. {
  608. struct stm32_port *stm32port = to_stm32_port(port);
  609. struct tty_port *tport = &port->state->port;
  610. struct dma_async_tx_descriptor *desc = NULL;
  611. unsigned int count;
  612. int ret;
  613. if (stm32_usart_tx_dma_started(stm32port)) {
  614. ret = stm32_usart_tx_dma_resume(stm32port);
  615. if (ret < 0 && ret != -EAGAIN)
  616. goto fallback_err;
  617. return;
  618. }
  619. count = kfifo_out_peek(&tport->xmit_fifo, &stm32port->tx_buf[0],
  620. TX_BUF_L);
  621. desc = dmaengine_prep_slave_single(stm32port->tx_ch,
  622. stm32port->tx_dma_buf,
  623. count,
  624. DMA_MEM_TO_DEV,
  625. DMA_PREP_INTERRUPT);
  626. if (!desc)
  627. goto fallback_err;
  628. /*
  629. * Set "tx_dma_busy" flag. This flag will be released when
  630. * dmaengine_terminate_async will be called. This flag helps
  631. * transmit_chars_dma not to start another DMA transaction
  632. * if the callback of the previous is not yet called.
  633. */
  634. stm32port->tx_dma_busy = true;
  635. desc->callback = stm32_usart_tx_dma_complete;
  636. desc->callback_param = port;
  637. /* Push current DMA TX transaction in the pending queue */
  638. /* DMA no yet started, safe to free resources */
  639. ret = dma_submit_error(dmaengine_submit(desc));
  640. if (ret) {
  641. dev_err(port->dev, "DMA failed with error code: %d\n", ret);
  642. stm32_usart_tx_dma_terminate(stm32port);
  643. goto fallback_err;
  644. }
  645. /* Issue pending DMA TX requests */
  646. dma_async_issue_pending(stm32port->tx_ch);
  647. uart_xmit_advance(port, count);
  648. return;
  649. fallback_err:
  650. stm32_usart_transmit_chars_pio(port);
  651. }
  652. static void stm32_usart_transmit_chars(struct uart_port *port)
  653. {
  654. struct stm32_port *stm32_port = to_stm32_port(port);
  655. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  656. struct tty_port *tport = &port->state->port;
  657. u32 isr;
  658. int ret;
  659. if (!stm32_port->hw_flow_control &&
  660. port->rs485.flags & SER_RS485_ENABLED &&
  661. (port->x_char ||
  662. !(kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)))) {
  663. stm32_usart_tc_interrupt_disable(port);
  664. stm32_usart_rs485_rts_enable(port);
  665. }
  666. if (port->x_char) {
  667. /* dma terminate may have been called in case of dma pause failure */
  668. stm32_usart_tx_dma_pause(stm32_port);
  669. /* Check that TDR is empty before filling FIFO */
  670. ret =
  671. readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
  672. isr,
  673. (isr & USART_SR_TXE),
  674. 10, 1000);
  675. if (ret)
  676. dev_warn(port->dev, "1 character may be erased\n");
  677. writel_relaxed(port->x_char, port->membase + ofs->tdr);
  678. port->x_char = 0;
  679. port->icount.tx++;
  680. /* dma terminate may have been called in case of dma resume failure */
  681. stm32_usart_tx_dma_resume(stm32_port);
  682. return;
  683. }
  684. if (kfifo_is_empty(&tport->xmit_fifo) || uart_tx_stopped(port)) {
  685. stm32_usart_tx_interrupt_disable(port);
  686. return;
  687. }
  688. if (ofs->icr == UNDEF_REG)
  689. stm32_usart_clr_bits(port, ofs->isr, USART_SR_TC);
  690. else
  691. writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
  692. if (stm32_port->tx_ch)
  693. stm32_usart_transmit_chars_dma(port);
  694. else
  695. stm32_usart_transmit_chars_pio(port);
  696. if (kfifo_len(&tport->xmit_fifo) < WAKEUP_CHARS)
  697. uart_write_wakeup(port);
  698. if (kfifo_is_empty(&tport->xmit_fifo)) {
  699. stm32_usart_tx_interrupt_disable(port);
  700. if (!stm32_port->hw_flow_control &&
  701. port->rs485.flags & SER_RS485_ENABLED) {
  702. stm32_usart_tc_interrupt_enable(port);
  703. }
  704. }
  705. }
  706. static irqreturn_t stm32_usart_interrupt(int irq, void *ptr)
  707. {
  708. struct uart_port *port = ptr;
  709. struct tty_port *tport = &port->state->port;
  710. struct stm32_port *stm32_port = to_stm32_port(port);
  711. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  712. u32 sr;
  713. unsigned int size;
  714. irqreturn_t ret = IRQ_NONE;
  715. sr = readl_relaxed(port->membase + ofs->isr);
  716. if (!stm32_port->hw_flow_control &&
  717. port->rs485.flags & SER_RS485_ENABLED &&
  718. (sr & USART_SR_TC)) {
  719. stm32_usart_tc_interrupt_disable(port);
  720. stm32_usart_rs485_rts_disable(port);
  721. ret = IRQ_HANDLED;
  722. }
  723. if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG) {
  724. writel_relaxed(USART_ICR_RTOCF,
  725. port->membase + ofs->icr);
  726. ret = IRQ_HANDLED;
  727. }
  728. if ((sr & USART_SR_WUF) && ofs->icr != UNDEF_REG) {
  729. /* Clear wake up flag and disable wake up interrupt */
  730. writel_relaxed(USART_ICR_WUCF,
  731. port->membase + ofs->icr);
  732. stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
  733. if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
  734. pm_wakeup_event(tport->tty->dev, 0);
  735. ret = IRQ_HANDLED;
  736. }
  737. /*
  738. * rx errors in dma mode has to be handled ASAP to avoid overrun as the DMA request
  739. * line has been masked by HW and rx data are stacking in FIFO.
  740. */
  741. if (!stm32_port->throttled) {
  742. if (((sr & USART_SR_RXNE) && !stm32_usart_rx_dma_started(stm32_port)) ||
  743. ((sr & USART_SR_ERR_MASK) && stm32_usart_rx_dma_started(stm32_port))) {
  744. uart_port_lock(port);
  745. size = stm32_usart_receive_chars(port, false);
  746. uart_unlock_and_check_sysrq(port);
  747. if (size)
  748. tty_flip_buffer_push(tport);
  749. ret = IRQ_HANDLED;
  750. }
  751. }
  752. if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch)) {
  753. uart_port_lock(port);
  754. stm32_usart_transmit_chars(port);
  755. uart_port_unlock(port);
  756. ret = IRQ_HANDLED;
  757. }
  758. /* Receiver timeout irq for DMA RX */
  759. if (stm32_usart_rx_dma_started(stm32_port) && !stm32_port->throttled) {
  760. uart_port_lock(port);
  761. size = stm32_usart_receive_chars(port, false);
  762. uart_unlock_and_check_sysrq(port);
  763. if (size)
  764. tty_flip_buffer_push(tport);
  765. ret = IRQ_HANDLED;
  766. }
  767. return ret;
  768. }
  769. static void stm32_usart_set_mctrl(struct uart_port *port, unsigned int mctrl)
  770. {
  771. struct stm32_port *stm32_port = to_stm32_port(port);
  772. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  773. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  774. stm32_usart_set_bits(port, ofs->cr3, USART_CR3_RTSE);
  775. else
  776. stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
  777. mctrl_gpio_set(stm32_port->gpios, mctrl);
  778. }
  779. static unsigned int stm32_usart_get_mctrl(struct uart_port *port)
  780. {
  781. struct stm32_port *stm32_port = to_stm32_port(port);
  782. unsigned int ret;
  783. /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
  784. ret = TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  785. return mctrl_gpio_get(stm32_port->gpios, &ret);
  786. }
  787. static void stm32_usart_enable_ms(struct uart_port *port)
  788. {
  789. mctrl_gpio_enable_ms(to_stm32_port(port)->gpios);
  790. }
  791. static void stm32_usart_disable_ms(struct uart_port *port)
  792. {
  793. mctrl_gpio_disable_ms_sync(to_stm32_port(port)->gpios);
  794. }
  795. /* Transmit stop */
  796. static void stm32_usart_stop_tx(struct uart_port *port)
  797. {
  798. struct stm32_port *stm32_port = to_stm32_port(port);
  799. stm32_usart_tx_interrupt_disable(port);
  800. /* dma terminate may have been called in case of dma pause failure */
  801. stm32_usart_tx_dma_pause(stm32_port);
  802. stm32_usart_rs485_rts_disable(port);
  803. }
  804. /* There are probably characters waiting to be transmitted. */
  805. static void stm32_usart_start_tx(struct uart_port *port)
  806. {
  807. struct tty_port *tport = &port->state->port;
  808. if (kfifo_is_empty(&tport->xmit_fifo) && !port->x_char)
  809. return;
  810. stm32_usart_rs485_rts_enable(port);
  811. stm32_usart_transmit_chars(port);
  812. }
  813. /* Flush the transmit buffer. */
  814. static void stm32_usart_flush_buffer(struct uart_port *port)
  815. {
  816. struct stm32_port *stm32_port = to_stm32_port(port);
  817. if (stm32_port->tx_ch)
  818. stm32_usart_tx_dma_terminate(stm32_port);
  819. }
  820. /* Throttle the remote when input buffer is about to overflow. */
  821. static void stm32_usart_throttle(struct uart_port *port)
  822. {
  823. struct stm32_port *stm32_port = to_stm32_port(port);
  824. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  825. unsigned long flags;
  826. uart_port_lock_irqsave(port, &flags);
  827. /*
  828. * Pause DMA transfer, so the RX data gets queued into the FIFO.
  829. * Hardware flow control is triggered when RX FIFO is full.
  830. */
  831. stm32_usart_rx_dma_pause(stm32_port);
  832. stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
  833. if (stm32_port->cr3_irq)
  834. stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
  835. stm32_port->throttled = true;
  836. uart_port_unlock_irqrestore(port, flags);
  837. }
  838. /* Unthrottle the remote, the input buffer can now accept data. */
  839. static void stm32_usart_unthrottle(struct uart_port *port)
  840. {
  841. struct stm32_port *stm32_port = to_stm32_port(port);
  842. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  843. unsigned long flags;
  844. uart_port_lock_irqsave(port, &flags);
  845. stm32_usart_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
  846. if (stm32_port->cr3_irq)
  847. stm32_usart_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
  848. stm32_port->throttled = false;
  849. /*
  850. * Switch back to DMA mode (resume DMA).
  851. * Hardware flow control is stopped when FIFO is not full any more.
  852. */
  853. if (stm32_port->rx_ch)
  854. stm32_usart_rx_dma_start_or_resume(port);
  855. uart_port_unlock_irqrestore(port, flags);
  856. }
  857. /* Receive stop */
  858. static void stm32_usart_stop_rx(struct uart_port *port)
  859. {
  860. struct stm32_port *stm32_port = to_stm32_port(port);
  861. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  862. /* Disable DMA request line. */
  863. stm32_usart_rx_dma_pause(stm32_port);
  864. stm32_usart_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
  865. if (stm32_port->cr3_irq)
  866. stm32_usart_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
  867. }
  868. static void stm32_usart_break_ctl(struct uart_port *port, int break_state)
  869. {
  870. struct stm32_port *stm32_port = to_stm32_port(port);
  871. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  872. unsigned long flags;
  873. uart_port_lock_irqsave(port, &flags);
  874. if (break_state)
  875. stm32_usart_set_bits(port, ofs->rqr, USART_RQR_SBKRQ);
  876. else
  877. stm32_usart_clr_bits(port, ofs->rqr, USART_RQR_SBKRQ);
  878. uart_port_unlock_irqrestore(port, flags);
  879. }
  880. static int stm32_usart_startup(struct uart_port *port)
  881. {
  882. struct stm32_port *stm32_port = to_stm32_port(port);
  883. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  884. const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  885. const char *name = to_platform_device(port->dev)->name;
  886. u32 val;
  887. int ret;
  888. ret = request_irq(port->irq, stm32_usart_interrupt,
  889. IRQF_NO_SUSPEND, name, port);
  890. if (ret)
  891. return ret;
  892. if (stm32_port->swap) {
  893. val = readl_relaxed(port->membase + ofs->cr2);
  894. val |= USART_CR2_SWAP;
  895. writel_relaxed(val, port->membase + ofs->cr2);
  896. }
  897. stm32_port->throttled = false;
  898. /* RX FIFO Flush */
  899. if (ofs->rqr != UNDEF_REG)
  900. writel_relaxed(USART_RQR_RXFRQ, port->membase + ofs->rqr);
  901. if (stm32_port->rx_ch) {
  902. ret = stm32_usart_rx_dma_start_or_resume(port);
  903. if (ret) {
  904. free_irq(port->irq, port);
  905. return ret;
  906. }
  907. }
  908. /* RX enabling */
  909. val = stm32_port->cr1_irq | USART_CR1_RE | BIT(cfg->uart_enable_bit);
  910. stm32_usart_set_bits(port, ofs->cr1, val);
  911. return 0;
  912. }
  913. static void stm32_usart_shutdown(struct uart_port *port)
  914. {
  915. struct stm32_port *stm32_port = to_stm32_port(port);
  916. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  917. const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  918. u32 val, isr;
  919. int ret;
  920. if (stm32_usart_tx_dma_started(stm32_port))
  921. stm32_usart_tx_dma_terminate(stm32_port);
  922. if (stm32_port->tx_ch)
  923. stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
  924. /* Disable modem control interrupts */
  925. stm32_usart_disable_ms(port);
  926. val = USART_CR1_TXEIE | USART_CR1_TE;
  927. val |= stm32_port->cr1_irq | USART_CR1_RE;
  928. val |= BIT(cfg->uart_enable_bit);
  929. if (stm32_port->fifoen)
  930. val |= USART_CR1_FIFOEN;
  931. ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
  932. isr, (isr & USART_SR_TC),
  933. 10, 100000);
  934. /* Send the TC error message only when ISR_TC is not set */
  935. if (ret)
  936. dev_err(port->dev, "Transmission is not complete\n");
  937. /* Disable RX DMA. */
  938. if (stm32_port->rx_ch) {
  939. stm32_usart_rx_dma_terminate(stm32_port);
  940. dmaengine_synchronize(stm32_port->rx_ch);
  941. }
  942. /* flush RX & TX FIFO */
  943. if (ofs->rqr != UNDEF_REG)
  944. writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
  945. port->membase + ofs->rqr);
  946. stm32_usart_clr_bits(port, ofs->cr1, val);
  947. free_irq(port->irq, port);
  948. }
  949. static const unsigned int stm32_usart_presc_val[] = {1, 2, 4, 6, 8, 10, 12, 16, 32, 64, 128, 256};
  950. static void stm32_usart_set_termios(struct uart_port *port,
  951. struct ktermios *termios,
  952. const struct ktermios *old)
  953. {
  954. struct stm32_port *stm32_port = to_stm32_port(port);
  955. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  956. const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  957. struct serial_rs485 *rs485conf = &port->rs485;
  958. unsigned int baud, bits, uart_clk, uart_clk_pres;
  959. u32 usartdiv, mantissa, fraction, oversampling;
  960. tcflag_t cflag = termios->c_cflag;
  961. u32 cr1, cr2, cr3, isr, brr, presc;
  962. unsigned long flags;
  963. int ret;
  964. if (!stm32_port->hw_flow_control)
  965. cflag &= ~CRTSCTS;
  966. uart_clk = clk_get_rate(stm32_port->clk);
  967. baud = uart_get_baud_rate(port, termios, old, 0, uart_clk / 8);
  968. uart_port_lock_irqsave(port, &flags);
  969. ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
  970. isr,
  971. (isr & USART_SR_TC),
  972. 10, 100000);
  973. /* Send the TC error message only when ISR_TC is not set. */
  974. if (ret)
  975. dev_err(port->dev, "Transmission is not complete\n");
  976. /* Stop serial port and reset value */
  977. writel_relaxed(0, port->membase + ofs->cr1);
  978. /* flush RX & TX FIFO */
  979. if (ofs->rqr != UNDEF_REG)
  980. writel_relaxed(USART_RQR_TXFRQ | USART_RQR_RXFRQ,
  981. port->membase + ofs->rqr);
  982. cr1 = USART_CR1_TE | USART_CR1_RE;
  983. if (stm32_port->fifoen)
  984. cr1 |= USART_CR1_FIFOEN;
  985. cr2 = stm32_port->swap ? USART_CR2_SWAP : 0;
  986. /* Tx and RX FIFO configuration */
  987. cr3 = readl_relaxed(port->membase + ofs->cr3);
  988. cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTIE;
  989. if (stm32_port->fifoen) {
  990. if (stm32_port->txftcfg >= 0)
  991. cr3 |= stm32_port->txftcfg << USART_CR3_TXFTCFG_SHIFT;
  992. if (stm32_port->rxftcfg >= 0)
  993. cr3 |= stm32_port->rxftcfg << USART_CR3_RXFTCFG_SHIFT;
  994. }
  995. if (cflag & CSTOPB)
  996. cr2 |= USART_CR2_STOP_2B;
  997. bits = tty_get_char_size(cflag);
  998. stm32_port->rdr_mask = (BIT(bits) - 1);
  999. if (cflag & PARENB) {
  1000. bits++;
  1001. cr1 |= USART_CR1_PCE;
  1002. }
  1003. /*
  1004. * Word length configuration:
  1005. * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
  1006. * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
  1007. * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
  1008. * M0 and M1 already cleared by cr1 initialization.
  1009. */
  1010. if (bits == 9) {
  1011. cr1 |= USART_CR1_M0;
  1012. } else if ((bits == 7) && cfg->has_7bits_data) {
  1013. cr1 |= USART_CR1_M1;
  1014. } else if (bits != 8) {
  1015. dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
  1016. , bits);
  1017. cflag &= ~CSIZE;
  1018. cflag |= CS8;
  1019. termios->c_cflag = cflag;
  1020. bits = 8;
  1021. if (cflag & PARENB) {
  1022. bits++;
  1023. cr1 |= USART_CR1_M0;
  1024. }
  1025. }
  1026. if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
  1027. (stm32_port->fifoen &&
  1028. stm32_port->rxftcfg >= 0))) {
  1029. if (cflag & CSTOPB)
  1030. bits = bits + 3; /* 1 start bit + 2 stop bits */
  1031. else
  1032. bits = bits + 2; /* 1 start bit + 1 stop bit */
  1033. /* RX timeout irq to occur after last stop bit + bits */
  1034. stm32_port->cr1_irq = USART_CR1_RTOIE;
  1035. writel_relaxed(bits, port->membase + ofs->rtor);
  1036. cr2 |= USART_CR2_RTOEN;
  1037. /*
  1038. * Enable fifo threshold irq in two cases, either when there is no DMA, or when
  1039. * wake up over usart, from low power until the DMA gets re-enabled by resume.
  1040. */
  1041. stm32_port->cr3_irq = USART_CR3_RXFTIE;
  1042. }
  1043. cr1 |= stm32_port->cr1_irq;
  1044. cr3 |= stm32_port->cr3_irq;
  1045. if (cflag & PARODD)
  1046. cr1 |= USART_CR1_PS;
  1047. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  1048. if (cflag & CRTSCTS) {
  1049. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  1050. cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
  1051. }
  1052. for (presc = 0; presc <= USART_PRESC_MAX; presc++) {
  1053. uart_clk_pres = DIV_ROUND_CLOSEST(uart_clk, stm32_usart_presc_val[presc]);
  1054. usartdiv = DIV_ROUND_CLOSEST(uart_clk_pres, baud);
  1055. /*
  1056. * The USART supports 16 or 8 times oversampling.
  1057. * By default we prefer 16 times oversampling, so that the receiver
  1058. * has a better tolerance to clock deviations.
  1059. * 8 times oversampling is only used to achieve higher speeds.
  1060. */
  1061. if (usartdiv < 16) {
  1062. oversampling = 8;
  1063. cr1 |= USART_CR1_OVER8;
  1064. stm32_usart_set_bits(port, ofs->cr1, USART_CR1_OVER8);
  1065. } else {
  1066. oversampling = 16;
  1067. cr1 &= ~USART_CR1_OVER8;
  1068. stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
  1069. }
  1070. mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
  1071. fraction = usartdiv % oversampling;
  1072. brr = mantissa | fraction;
  1073. if (FIELD_FIT(USART_BRR_MASK, brr)) {
  1074. if (ofs->presc != UNDEF_REG) {
  1075. port->uartclk = uart_clk_pres;
  1076. writel_relaxed(presc, port->membase + ofs->presc);
  1077. } else if (presc) {
  1078. /* We need a prescaler but we don't have it (STM32F4, STM32F7) */
  1079. dev_err(port->dev,
  1080. "unable to set baudrate, input clock is too high");
  1081. }
  1082. break;
  1083. } else if (presc == USART_PRESC_MAX) {
  1084. /* Even with prescaler and brr at max value we can't set baudrate */
  1085. dev_err(port->dev, "unable to set baudrate, input clock is too high");
  1086. break;
  1087. }
  1088. }
  1089. writel_relaxed(brr, port->membase + ofs->brr);
  1090. uart_update_timeout(port, cflag, baud);
  1091. port->read_status_mask = USART_SR_ORE;
  1092. if (termios->c_iflag & INPCK)
  1093. port->read_status_mask |= USART_SR_PE | USART_SR_FE;
  1094. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  1095. port->read_status_mask |= USART_SR_FE;
  1096. /* Characters to ignore */
  1097. port->ignore_status_mask = 0;
  1098. if (termios->c_iflag & IGNPAR)
  1099. port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
  1100. if (termios->c_iflag & IGNBRK) {
  1101. port->ignore_status_mask |= USART_SR_FE;
  1102. /*
  1103. * If we're ignoring parity and break indicators,
  1104. * ignore overruns too (for real raw support).
  1105. */
  1106. if (termios->c_iflag & IGNPAR)
  1107. port->ignore_status_mask |= USART_SR_ORE;
  1108. }
  1109. /* Ignore all characters if CREAD is not set */
  1110. if ((termios->c_cflag & CREAD) == 0)
  1111. port->ignore_status_mask |= USART_SR_DUMMY_RX;
  1112. if (stm32_port->rx_ch) {
  1113. /*
  1114. * Setup DMA to collect only valid data and enable error irqs.
  1115. * This also enables break reception when using DMA.
  1116. */
  1117. cr1 |= USART_CR1_PEIE;
  1118. cr3 |= USART_CR3_EIE;
  1119. cr3 |= USART_CR3_DMAR;
  1120. cr3 |= USART_CR3_DDRE;
  1121. }
  1122. if (stm32_port->tx_ch)
  1123. cr3 |= USART_CR3_DMAT;
  1124. if (rs485conf->flags & SER_RS485_ENABLED) {
  1125. stm32_usart_config_reg_rs485(&cr1, &cr3,
  1126. rs485conf->delay_rts_before_send,
  1127. rs485conf->delay_rts_after_send,
  1128. baud);
  1129. if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
  1130. cr3 &= ~USART_CR3_DEP;
  1131. rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
  1132. } else {
  1133. cr3 |= USART_CR3_DEP;
  1134. rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  1135. }
  1136. } else {
  1137. cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
  1138. cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
  1139. }
  1140. /* Configure wake up from low power on start bit detection */
  1141. if (stm32_port->wakeup_src) {
  1142. cr3 &= ~USART_CR3_WUS_MASK;
  1143. cr3 |= USART_CR3_WUS_START_BIT;
  1144. }
  1145. writel_relaxed(cr3, port->membase + ofs->cr3);
  1146. writel_relaxed(cr2, port->membase + ofs->cr2);
  1147. writel_relaxed(cr1, port->membase + ofs->cr1);
  1148. stm32_usart_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  1149. uart_port_unlock_irqrestore(port, flags);
  1150. /* Handle modem control interrupts */
  1151. if (UART_ENABLE_MS(port, termios->c_cflag))
  1152. stm32_usart_enable_ms(port);
  1153. else
  1154. stm32_usart_disable_ms(port);
  1155. }
  1156. static const char *stm32_usart_type(struct uart_port *port)
  1157. {
  1158. return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
  1159. }
  1160. static void stm32_usart_release_port(struct uart_port *port)
  1161. {
  1162. }
  1163. static int stm32_usart_request_port(struct uart_port *port)
  1164. {
  1165. return 0;
  1166. }
  1167. static void stm32_usart_config_port(struct uart_port *port, int flags)
  1168. {
  1169. if (flags & UART_CONFIG_TYPE)
  1170. port->type = PORT_STM32;
  1171. }
  1172. static int
  1173. stm32_usart_verify_port(struct uart_port *port, struct serial_struct *ser)
  1174. {
  1175. /* No user changeable parameters */
  1176. return -EINVAL;
  1177. }
  1178. static void stm32_usart_pm(struct uart_port *port, unsigned int state,
  1179. unsigned int oldstate)
  1180. {
  1181. struct stm32_port *stm32port = container_of(port,
  1182. struct stm32_port, port);
  1183. const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  1184. const struct stm32_usart_config *cfg = &stm32port->info->cfg;
  1185. unsigned long flags;
  1186. switch (state) {
  1187. case UART_PM_STATE_ON:
  1188. pm_runtime_get_sync(port->dev);
  1189. break;
  1190. case UART_PM_STATE_OFF:
  1191. uart_port_lock_irqsave(port, &flags);
  1192. stm32_usart_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  1193. uart_port_unlock_irqrestore(port, flags);
  1194. pm_runtime_put_sync(port->dev);
  1195. break;
  1196. }
  1197. }
  1198. #if defined(CONFIG_CONSOLE_POLL)
  1199. /* Callbacks for characters polling in debug context (i.e. KGDB). */
  1200. static int stm32_usart_poll_init(struct uart_port *port)
  1201. {
  1202. struct stm32_port *stm32_port = to_stm32_port(port);
  1203. return clk_prepare_enable(stm32_port->clk);
  1204. }
  1205. static int stm32_usart_poll_get_char(struct uart_port *port)
  1206. {
  1207. struct stm32_port *stm32_port = to_stm32_port(port);
  1208. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1209. if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_RXNE))
  1210. return NO_POLL_CHAR;
  1211. return readl_relaxed(port->membase + ofs->rdr) & stm32_port->rdr_mask;
  1212. }
  1213. static void stm32_usart_poll_put_char(struct uart_port *port, unsigned char ch)
  1214. {
  1215. stm32_usart_console_putchar(port, ch);
  1216. }
  1217. #endif /* CONFIG_CONSOLE_POLL */
  1218. static const struct uart_ops stm32_uart_ops = {
  1219. .tx_empty = stm32_usart_tx_empty,
  1220. .set_mctrl = stm32_usart_set_mctrl,
  1221. .get_mctrl = stm32_usart_get_mctrl,
  1222. .stop_tx = stm32_usart_stop_tx,
  1223. .start_tx = stm32_usart_start_tx,
  1224. .throttle = stm32_usart_throttle,
  1225. .unthrottle = stm32_usart_unthrottle,
  1226. .stop_rx = stm32_usart_stop_rx,
  1227. .enable_ms = stm32_usart_enable_ms,
  1228. .break_ctl = stm32_usart_break_ctl,
  1229. .startup = stm32_usart_startup,
  1230. .shutdown = stm32_usart_shutdown,
  1231. .flush_buffer = stm32_usart_flush_buffer,
  1232. .set_termios = stm32_usart_set_termios,
  1233. .pm = stm32_usart_pm,
  1234. .type = stm32_usart_type,
  1235. .release_port = stm32_usart_release_port,
  1236. .request_port = stm32_usart_request_port,
  1237. .config_port = stm32_usart_config_port,
  1238. .verify_port = stm32_usart_verify_port,
  1239. #if defined(CONFIG_CONSOLE_POLL)
  1240. .poll_init = stm32_usart_poll_init,
  1241. .poll_get_char = stm32_usart_poll_get_char,
  1242. .poll_put_char = stm32_usart_poll_put_char,
  1243. #endif /* CONFIG_CONSOLE_POLL */
  1244. };
  1245. struct stm32_usart_thresh_ratio {
  1246. int mul;
  1247. int div;
  1248. };
  1249. static const struct stm32_usart_thresh_ratio stm32h7_usart_fifo_thresh_cfg[] = {
  1250. {1, 8}, {1, 4}, {1, 2}, {3, 4}, {7, 8}, {1, 1} };
  1251. static int stm32_usart_get_thresh_value(u32 fifo_size, int index)
  1252. {
  1253. return fifo_size * stm32h7_usart_fifo_thresh_cfg[index].mul /
  1254. stm32h7_usart_fifo_thresh_cfg[index].div;
  1255. }
  1256. static int stm32_usart_get_ftcfg(struct platform_device *pdev, struct stm32_port *stm32port,
  1257. const char *p, int *ftcfg)
  1258. {
  1259. const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  1260. u32 bytes, i, cfg8;
  1261. int fifo_size;
  1262. if (WARN_ON(ofs->hwcfgr1 == UNDEF_REG))
  1263. return 1;
  1264. cfg8 = FIELD_GET(USART_HWCFGR1_CFG8,
  1265. readl_relaxed(stm32port->port.membase + ofs->hwcfgr1));
  1266. /* On STM32H7, hwcfgr is not present, so returned value will be 0 */
  1267. fifo_size = cfg8 ? 1 << cfg8 : STM32H7_USART_FIFO_SIZE;
  1268. /* DT option to get RX & TX FIFO threshold (default to half fifo size) */
  1269. if (of_property_read_u32(pdev->dev.of_node, p, &bytes))
  1270. bytes = fifo_size / 2;
  1271. if (bytes < stm32_usart_get_thresh_value(fifo_size, 0)) {
  1272. *ftcfg = -EINVAL;
  1273. return fifo_size;
  1274. }
  1275. for (i = 0; i < ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg); i++) {
  1276. if (stm32_usart_get_thresh_value(fifo_size, i) >= bytes)
  1277. break;
  1278. }
  1279. if (i >= ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg))
  1280. i = ARRAY_SIZE(stm32h7_usart_fifo_thresh_cfg) - 1;
  1281. dev_dbg(&pdev->dev, "%s set to %d/%d bytes\n", p,
  1282. stm32_usart_get_thresh_value(fifo_size, i), fifo_size);
  1283. *ftcfg = i;
  1284. return fifo_size;
  1285. }
  1286. static void stm32_usart_deinit_port(struct stm32_port *stm32port)
  1287. {
  1288. clk_disable_unprepare(stm32port->clk);
  1289. }
  1290. static const struct serial_rs485 stm32_rs485_supported = {
  1291. .flags = SER_RS485_ENABLED | SER_RS485_RTS_ON_SEND | SER_RS485_RTS_AFTER_SEND |
  1292. SER_RS485_RX_DURING_TX,
  1293. .delay_rts_before_send = 1,
  1294. .delay_rts_after_send = 1,
  1295. };
  1296. static int stm32_usart_init_port(struct stm32_port *stm32port,
  1297. struct platform_device *pdev)
  1298. {
  1299. struct uart_port *port = &stm32port->port;
  1300. struct resource *res;
  1301. int ret, irq;
  1302. irq = platform_get_irq(pdev, 0);
  1303. if (irq < 0)
  1304. return irq;
  1305. port->iotype = UPIO_MEM;
  1306. port->flags = UPF_BOOT_AUTOCONF;
  1307. port->ops = &stm32_uart_ops;
  1308. port->dev = &pdev->dev;
  1309. port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_STM32_CONSOLE);
  1310. port->irq = irq;
  1311. port->rs485_config = stm32_usart_config_rs485;
  1312. port->rs485_supported = stm32_rs485_supported;
  1313. ret = stm32_usart_init_rs485(port, pdev);
  1314. if (ret)
  1315. return ret;
  1316. stm32port->wakeup_src = stm32port->info->cfg.has_wakeup &&
  1317. of_property_read_bool(pdev->dev.of_node, "wakeup-source");
  1318. stm32port->swap = stm32port->info->cfg.has_swap &&
  1319. of_property_read_bool(pdev->dev.of_node, "rx-tx-swap");
  1320. port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
  1321. if (IS_ERR(port->membase))
  1322. return PTR_ERR(port->membase);
  1323. port->mapbase = res->start;
  1324. spin_lock_init(&port->lock);
  1325. stm32port->clk = devm_clk_get(&pdev->dev, NULL);
  1326. if (IS_ERR(stm32port->clk))
  1327. return PTR_ERR(stm32port->clk);
  1328. /* Ensure that clk rate is correct by enabling the clk */
  1329. ret = clk_prepare_enable(stm32port->clk);
  1330. if (ret)
  1331. return ret;
  1332. stm32port->port.uartclk = clk_get_rate(stm32port->clk);
  1333. if (!stm32port->port.uartclk) {
  1334. ret = -EINVAL;
  1335. goto err_clk;
  1336. }
  1337. stm32port->fifoen = stm32port->info->cfg.has_fifo;
  1338. if (stm32port->fifoen) {
  1339. stm32_usart_get_ftcfg(pdev, stm32port, "rx-threshold", &stm32port->rxftcfg);
  1340. port->fifosize = stm32_usart_get_ftcfg(pdev, stm32port, "tx-threshold",
  1341. &stm32port->txftcfg);
  1342. } else {
  1343. port->fifosize = 1;
  1344. }
  1345. stm32port->gpios = mctrl_gpio_init(&stm32port->port, 0);
  1346. if (IS_ERR(stm32port->gpios)) {
  1347. ret = PTR_ERR(stm32port->gpios);
  1348. goto err_clk;
  1349. }
  1350. /*
  1351. * Both CTS/RTS gpios and "st,hw-flow-ctrl" (deprecated) or "uart-has-rtscts"
  1352. * properties should not be specified.
  1353. */
  1354. if (stm32port->hw_flow_control) {
  1355. if (mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_CTS) ||
  1356. mctrl_gpio_to_gpiod(stm32port->gpios, UART_GPIO_RTS)) {
  1357. dev_err(&pdev->dev, "Conflicting RTS/CTS config\n");
  1358. ret = -EINVAL;
  1359. goto err_clk;
  1360. }
  1361. }
  1362. return ret;
  1363. err_clk:
  1364. clk_disable_unprepare(stm32port->clk);
  1365. return ret;
  1366. }
  1367. static struct stm32_port *stm32_usart_of_get_port(struct platform_device *pdev)
  1368. {
  1369. struct device_node *np = pdev->dev.of_node;
  1370. int id;
  1371. if (!np)
  1372. return NULL;
  1373. id = of_alias_get_id(np, "serial");
  1374. if (id < 0) {
  1375. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
  1376. return NULL;
  1377. }
  1378. if (WARN_ON(id >= STM32_MAX_PORTS))
  1379. return NULL;
  1380. stm32_ports[id].hw_flow_control =
  1381. of_property_read_bool (np, "st,hw-flow-ctrl") /*deprecated*/ ||
  1382. of_property_read_bool (np, "uart-has-rtscts");
  1383. stm32_ports[id].port.line = id;
  1384. stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
  1385. stm32_ports[id].cr3_irq = 0;
  1386. stm32_ports[id].last_res = RX_BUF_L;
  1387. return &stm32_ports[id];
  1388. }
  1389. #ifdef CONFIG_OF
  1390. static const struct of_device_id stm32_match[] = {
  1391. { .compatible = "st,stm32-uart", .data = &stm32f4_info},
  1392. { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
  1393. { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
  1394. {},
  1395. };
  1396. MODULE_DEVICE_TABLE(of, stm32_match);
  1397. #endif
  1398. static void stm32_usart_of_dma_rx_remove(struct stm32_port *stm32port,
  1399. struct platform_device *pdev)
  1400. {
  1401. if (stm32port->rx_buf)
  1402. dma_free_coherent(&pdev->dev, RX_BUF_L, stm32port->rx_buf,
  1403. stm32port->rx_dma_buf);
  1404. }
  1405. static int stm32_usart_of_dma_rx_probe(struct stm32_port *stm32port,
  1406. struct platform_device *pdev)
  1407. {
  1408. const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  1409. struct uart_port *port = &stm32port->port;
  1410. struct device *dev = &pdev->dev;
  1411. struct dma_slave_config config;
  1412. int ret;
  1413. stm32port->rx_buf = dma_alloc_coherent(dev, RX_BUF_L,
  1414. &stm32port->rx_dma_buf,
  1415. GFP_KERNEL);
  1416. if (!stm32port->rx_buf)
  1417. return -ENOMEM;
  1418. /* Configure DMA channel */
  1419. memset(&config, 0, sizeof(config));
  1420. config.src_addr = port->mapbase + ofs->rdr;
  1421. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1422. ret = dmaengine_slave_config(stm32port->rx_ch, &config);
  1423. if (ret < 0) {
  1424. dev_err(dev, "rx dma channel config failed\n");
  1425. stm32_usart_of_dma_rx_remove(stm32port, pdev);
  1426. return ret;
  1427. }
  1428. return 0;
  1429. }
  1430. static void stm32_usart_of_dma_tx_remove(struct stm32_port *stm32port,
  1431. struct platform_device *pdev)
  1432. {
  1433. if (stm32port->tx_buf)
  1434. dma_free_coherent(&pdev->dev, TX_BUF_L, stm32port->tx_buf,
  1435. stm32port->tx_dma_buf);
  1436. }
  1437. static int stm32_usart_of_dma_tx_probe(struct stm32_port *stm32port,
  1438. struct platform_device *pdev)
  1439. {
  1440. const struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  1441. struct uart_port *port = &stm32port->port;
  1442. struct device *dev = &pdev->dev;
  1443. struct dma_slave_config config;
  1444. int ret;
  1445. stm32port->tx_buf = dma_alloc_coherent(dev, TX_BUF_L,
  1446. &stm32port->tx_dma_buf,
  1447. GFP_KERNEL);
  1448. if (!stm32port->tx_buf)
  1449. return -ENOMEM;
  1450. /* Configure DMA channel */
  1451. memset(&config, 0, sizeof(config));
  1452. config.dst_addr = port->mapbase + ofs->tdr;
  1453. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  1454. ret = dmaengine_slave_config(stm32port->tx_ch, &config);
  1455. if (ret < 0) {
  1456. dev_err(dev, "tx dma channel config failed\n");
  1457. stm32_usart_of_dma_tx_remove(stm32port, pdev);
  1458. return ret;
  1459. }
  1460. return 0;
  1461. }
  1462. static int stm32_usart_serial_probe(struct platform_device *pdev)
  1463. {
  1464. struct stm32_port *stm32port;
  1465. int ret;
  1466. stm32port = stm32_usart_of_get_port(pdev);
  1467. if (!stm32port)
  1468. return -ENODEV;
  1469. stm32port->info = of_device_get_match_data(&pdev->dev);
  1470. if (!stm32port->info)
  1471. return -EINVAL;
  1472. stm32port->rx_ch = dma_request_chan(&pdev->dev, "rx");
  1473. if (PTR_ERR(stm32port->rx_ch) == -EPROBE_DEFER)
  1474. return -EPROBE_DEFER;
  1475. /* Fall back in interrupt mode for any non-deferral error */
  1476. if (IS_ERR(stm32port->rx_ch))
  1477. stm32port->rx_ch = NULL;
  1478. stm32port->tx_ch = dma_request_chan(&pdev->dev, "tx");
  1479. if (PTR_ERR(stm32port->tx_ch) == -EPROBE_DEFER) {
  1480. ret = -EPROBE_DEFER;
  1481. goto err_dma_rx;
  1482. }
  1483. /* Fall back in interrupt mode for any non-deferral error */
  1484. if (IS_ERR(stm32port->tx_ch))
  1485. stm32port->tx_ch = NULL;
  1486. ret = stm32_usart_init_port(stm32port, pdev);
  1487. if (ret)
  1488. goto err_dma_tx;
  1489. if (stm32port->wakeup_src) {
  1490. device_set_wakeup_capable(&pdev->dev, true);
  1491. ret = dev_pm_set_wake_irq(&pdev->dev, stm32port->port.irq);
  1492. if (ret)
  1493. goto err_deinit_port;
  1494. }
  1495. if (stm32port->rx_ch && stm32_usart_of_dma_rx_probe(stm32port, pdev)) {
  1496. /* Fall back in interrupt mode */
  1497. dma_release_channel(stm32port->rx_ch);
  1498. stm32port->rx_ch = NULL;
  1499. }
  1500. if (stm32port->tx_ch && stm32_usart_of_dma_tx_probe(stm32port, pdev)) {
  1501. /* Fall back in interrupt mode */
  1502. dma_release_channel(stm32port->tx_ch);
  1503. stm32port->tx_ch = NULL;
  1504. }
  1505. if (!stm32port->rx_ch)
  1506. dev_info(&pdev->dev, "interrupt mode for rx (no dma)\n");
  1507. if (!stm32port->tx_ch)
  1508. dev_info(&pdev->dev, "interrupt mode for tx (no dma)\n");
  1509. platform_set_drvdata(pdev, &stm32port->port);
  1510. pm_runtime_get_noresume(&pdev->dev);
  1511. pm_runtime_set_active(&pdev->dev);
  1512. pm_runtime_enable(&pdev->dev);
  1513. ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
  1514. if (ret)
  1515. goto err_port;
  1516. pm_runtime_put_sync(&pdev->dev);
  1517. return 0;
  1518. err_port:
  1519. pm_runtime_disable(&pdev->dev);
  1520. pm_runtime_set_suspended(&pdev->dev);
  1521. pm_runtime_put_noidle(&pdev->dev);
  1522. if (stm32port->tx_ch)
  1523. stm32_usart_of_dma_tx_remove(stm32port, pdev);
  1524. if (stm32port->rx_ch)
  1525. stm32_usart_of_dma_rx_remove(stm32port, pdev);
  1526. if (stm32port->wakeup_src)
  1527. dev_pm_clear_wake_irq(&pdev->dev);
  1528. err_deinit_port:
  1529. if (stm32port->wakeup_src)
  1530. device_set_wakeup_capable(&pdev->dev, false);
  1531. stm32_usart_deinit_port(stm32port);
  1532. err_dma_tx:
  1533. if (stm32port->tx_ch)
  1534. dma_release_channel(stm32port->tx_ch);
  1535. err_dma_rx:
  1536. if (stm32port->rx_ch)
  1537. dma_release_channel(stm32port->rx_ch);
  1538. return ret;
  1539. }
  1540. static void stm32_usart_serial_remove(struct platform_device *pdev)
  1541. {
  1542. struct uart_port *port = platform_get_drvdata(pdev);
  1543. struct stm32_port *stm32_port = to_stm32_port(port);
  1544. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1545. u32 cr3;
  1546. pm_runtime_get_sync(&pdev->dev);
  1547. uart_remove_one_port(&stm32_usart_driver, port);
  1548. pm_runtime_disable(&pdev->dev);
  1549. pm_runtime_set_suspended(&pdev->dev);
  1550. pm_runtime_put_noidle(&pdev->dev);
  1551. stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_PEIE);
  1552. if (stm32_port->tx_ch) {
  1553. stm32_usart_of_dma_tx_remove(stm32_port, pdev);
  1554. dma_release_channel(stm32_port->tx_ch);
  1555. }
  1556. if (stm32_port->rx_ch) {
  1557. stm32_usart_of_dma_rx_remove(stm32_port, pdev);
  1558. dma_release_channel(stm32_port->rx_ch);
  1559. }
  1560. cr3 = readl_relaxed(port->membase + ofs->cr3);
  1561. cr3 &= ~USART_CR3_EIE;
  1562. cr3 &= ~USART_CR3_DMAR;
  1563. cr3 &= ~USART_CR3_DMAT;
  1564. cr3 &= ~USART_CR3_DDRE;
  1565. writel_relaxed(cr3, port->membase + ofs->cr3);
  1566. if (stm32_port->wakeup_src) {
  1567. dev_pm_clear_wake_irq(&pdev->dev);
  1568. device_init_wakeup(&pdev->dev, false);
  1569. }
  1570. stm32_usart_deinit_port(stm32_port);
  1571. }
  1572. static void __maybe_unused stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
  1573. {
  1574. struct stm32_port *stm32_port = to_stm32_port(port);
  1575. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1576. u32 isr;
  1577. int ret;
  1578. ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr, isr,
  1579. (isr & USART_SR_TXE), 100,
  1580. STM32_USART_TIMEOUT_USEC);
  1581. if (ret != 0) {
  1582. dev_err(port->dev, "Error while sending data in UART TX : %d\n", ret);
  1583. return;
  1584. }
  1585. writel_relaxed(ch, port->membase + ofs->tdr);
  1586. }
  1587. #ifdef CONFIG_SERIAL_STM32_CONSOLE
  1588. static void stm32_usart_console_write(struct console *co, const char *s,
  1589. unsigned int cnt)
  1590. {
  1591. struct uart_port *port = &stm32_ports[co->index].port;
  1592. struct stm32_port *stm32_port = to_stm32_port(port);
  1593. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1594. const struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  1595. unsigned long flags;
  1596. u32 old_cr1, new_cr1;
  1597. int locked = 1;
  1598. if (oops_in_progress)
  1599. locked = uart_port_trylock_irqsave(port, &flags);
  1600. else
  1601. uart_port_lock_irqsave(port, &flags);
  1602. /* Save and disable interrupts, enable the transmitter */
  1603. old_cr1 = readl_relaxed(port->membase + ofs->cr1);
  1604. new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
  1605. new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
  1606. writel_relaxed(new_cr1, port->membase + ofs->cr1);
  1607. uart_console_write(port, s, cnt, stm32_usart_console_putchar);
  1608. /* Restore interrupt state */
  1609. writel_relaxed(old_cr1, port->membase + ofs->cr1);
  1610. if (locked)
  1611. uart_port_unlock_irqrestore(port, flags);
  1612. }
  1613. static int stm32_usart_console_setup(struct console *co, char *options)
  1614. {
  1615. struct stm32_port *stm32port;
  1616. int baud = 9600;
  1617. int bits = 8;
  1618. int parity = 'n';
  1619. int flow = 'n';
  1620. if (co->index >= STM32_MAX_PORTS)
  1621. return -ENODEV;
  1622. stm32port = &stm32_ports[co->index];
  1623. /*
  1624. * This driver does not support early console initialization
  1625. * (use ARM early printk support instead), so we only expect
  1626. * this to be called during the uart port registration when the
  1627. * driver gets probed and the port should be mapped at that point.
  1628. */
  1629. if (stm32port->port.mapbase == 0 || !stm32port->port.membase)
  1630. return -ENXIO;
  1631. if (options)
  1632. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1633. return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
  1634. }
  1635. static struct console stm32_console = {
  1636. .name = STM32_SERIAL_NAME,
  1637. .device = uart_console_device,
  1638. .write = stm32_usart_console_write,
  1639. .setup = stm32_usart_console_setup,
  1640. .flags = CON_PRINTBUFFER,
  1641. .index = -1,
  1642. .data = &stm32_usart_driver,
  1643. };
  1644. #define STM32_SERIAL_CONSOLE (&stm32_console)
  1645. #else
  1646. #define STM32_SERIAL_CONSOLE NULL
  1647. #endif /* CONFIG_SERIAL_STM32_CONSOLE */
  1648. #ifdef CONFIG_SERIAL_EARLYCON
  1649. static void early_stm32_usart_console_putchar(struct uart_port *port, unsigned char ch)
  1650. {
  1651. struct stm32_usart_info *info = port->private_data;
  1652. while (!(readl_relaxed(port->membase + info->ofs.isr) & USART_SR_TXE))
  1653. cpu_relax();
  1654. writel_relaxed(ch, port->membase + info->ofs.tdr);
  1655. }
  1656. static void early_stm32_serial_write(struct console *console, const char *s, unsigned int count)
  1657. {
  1658. struct earlycon_device *device = console->data;
  1659. struct uart_port *port = &device->port;
  1660. uart_console_write(port, s, count, early_stm32_usart_console_putchar);
  1661. }
  1662. static int __init early_stm32_h7_serial_setup(struct earlycon_device *device, const char *options)
  1663. {
  1664. if (!(device->port.membase || device->port.iobase))
  1665. return -ENODEV;
  1666. device->port.private_data = &stm32h7_info;
  1667. device->con->write = early_stm32_serial_write;
  1668. return 0;
  1669. }
  1670. static int __init early_stm32_f7_serial_setup(struct earlycon_device *device, const char *options)
  1671. {
  1672. if (!(device->port.membase || device->port.iobase))
  1673. return -ENODEV;
  1674. device->port.private_data = &stm32f7_info;
  1675. device->con->write = early_stm32_serial_write;
  1676. return 0;
  1677. }
  1678. static int __init early_stm32_f4_serial_setup(struct earlycon_device *device, const char *options)
  1679. {
  1680. if (!(device->port.membase || device->port.iobase))
  1681. return -ENODEV;
  1682. device->port.private_data = &stm32f4_info;
  1683. device->con->write = early_stm32_serial_write;
  1684. return 0;
  1685. }
  1686. OF_EARLYCON_DECLARE(stm32, "st,stm32h7-uart", early_stm32_h7_serial_setup);
  1687. OF_EARLYCON_DECLARE(stm32, "st,stm32f7-uart", early_stm32_f7_serial_setup);
  1688. OF_EARLYCON_DECLARE(stm32, "st,stm32-uart", early_stm32_f4_serial_setup);
  1689. #endif /* CONFIG_SERIAL_EARLYCON */
  1690. static struct uart_driver stm32_usart_driver = {
  1691. .driver_name = DRIVER_NAME,
  1692. .dev_name = STM32_SERIAL_NAME,
  1693. .major = 0,
  1694. .minor = 0,
  1695. .nr = STM32_MAX_PORTS,
  1696. .cons = STM32_SERIAL_CONSOLE,
  1697. };
  1698. static int __maybe_unused stm32_usart_serial_en_wakeup(struct uart_port *port,
  1699. bool enable)
  1700. {
  1701. struct stm32_port *stm32_port = to_stm32_port(port);
  1702. const struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1703. struct tty_port *tport = &port->state->port;
  1704. int ret;
  1705. unsigned int size = 0;
  1706. unsigned long flags;
  1707. if (!stm32_port->wakeup_src || !tty_port_initialized(tport))
  1708. return 0;
  1709. /*
  1710. * Enable low-power wake-up and wake-up irq if argument is set to
  1711. * "enable", disable low-power wake-up and wake-up irq otherwise
  1712. */
  1713. if (enable) {
  1714. stm32_usart_set_bits(port, ofs->cr1, USART_CR1_UESM);
  1715. stm32_usart_set_bits(port, ofs->cr3, USART_CR3_WUFIE);
  1716. mctrl_gpio_enable_irq_wake(stm32_port->gpios);
  1717. /*
  1718. * When DMA is used for reception, it must be disabled before
  1719. * entering low-power mode and re-enabled when exiting from
  1720. * low-power mode.
  1721. */
  1722. if (stm32_port->rx_ch) {
  1723. uart_port_lock_irqsave(port, &flags);
  1724. /* Poll data from DMA RX buffer if any */
  1725. if (!stm32_usart_rx_dma_pause(stm32_port))
  1726. size += stm32_usart_receive_chars(port, true);
  1727. stm32_usart_rx_dma_terminate(stm32_port);
  1728. uart_unlock_and_check_sysrq_irqrestore(port, flags);
  1729. if (size)
  1730. tty_flip_buffer_push(tport);
  1731. }
  1732. /* Poll data from RX FIFO if any */
  1733. stm32_usart_receive_chars(port, false);
  1734. } else {
  1735. if (stm32_port->rx_ch) {
  1736. ret = stm32_usart_rx_dma_start_or_resume(port);
  1737. if (ret)
  1738. return ret;
  1739. }
  1740. mctrl_gpio_disable_irq_wake(stm32_port->gpios);
  1741. stm32_usart_clr_bits(port, ofs->cr1, USART_CR1_UESM);
  1742. stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_WUFIE);
  1743. }
  1744. return 0;
  1745. }
  1746. static int __maybe_unused stm32_usart_serial_suspend(struct device *dev)
  1747. {
  1748. struct uart_port *port = dev_get_drvdata(dev);
  1749. int ret;
  1750. uart_suspend_port(&stm32_usart_driver, port);
  1751. if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
  1752. ret = stm32_usart_serial_en_wakeup(port, true);
  1753. if (ret)
  1754. return ret;
  1755. }
  1756. /*
  1757. * When "no_console_suspend" is enabled, keep the pinctrl default state
  1758. * and rely on bootloader stage to restore this state upon resume.
  1759. * Otherwise, apply the idle or sleep states depending on wakeup
  1760. * capabilities.
  1761. */
  1762. if (console_suspend_enabled || !uart_console(port)) {
  1763. if (device_may_wakeup(dev) || device_wakeup_path(dev))
  1764. pinctrl_pm_select_idle_state(dev);
  1765. else
  1766. pinctrl_pm_select_sleep_state(dev);
  1767. }
  1768. return 0;
  1769. }
  1770. static int __maybe_unused stm32_usart_serial_resume(struct device *dev)
  1771. {
  1772. struct uart_port *port = dev_get_drvdata(dev);
  1773. int ret;
  1774. pinctrl_pm_select_default_state(dev);
  1775. if (device_may_wakeup(dev) || device_wakeup_path(dev)) {
  1776. ret = stm32_usart_serial_en_wakeup(port, false);
  1777. if (ret)
  1778. return ret;
  1779. }
  1780. return uart_resume_port(&stm32_usart_driver, port);
  1781. }
  1782. static int __maybe_unused stm32_usart_runtime_suspend(struct device *dev)
  1783. {
  1784. struct uart_port *port = dev_get_drvdata(dev);
  1785. struct stm32_port *stm32port = container_of(port,
  1786. struct stm32_port, port);
  1787. clk_disable_unprepare(stm32port->clk);
  1788. return 0;
  1789. }
  1790. static int __maybe_unused stm32_usart_runtime_resume(struct device *dev)
  1791. {
  1792. struct uart_port *port = dev_get_drvdata(dev);
  1793. struct stm32_port *stm32port = container_of(port,
  1794. struct stm32_port, port);
  1795. return clk_prepare_enable(stm32port->clk);
  1796. }
  1797. static const struct dev_pm_ops stm32_serial_pm_ops = {
  1798. SET_RUNTIME_PM_OPS(stm32_usart_runtime_suspend,
  1799. stm32_usart_runtime_resume, NULL)
  1800. SET_SYSTEM_SLEEP_PM_OPS(stm32_usart_serial_suspend,
  1801. stm32_usart_serial_resume)
  1802. };
  1803. static struct platform_driver stm32_serial_driver = {
  1804. .probe = stm32_usart_serial_probe,
  1805. .remove = stm32_usart_serial_remove,
  1806. .driver = {
  1807. .name = DRIVER_NAME,
  1808. .pm = &stm32_serial_pm_ops,
  1809. .of_match_table = of_match_ptr(stm32_match),
  1810. },
  1811. };
  1812. static int __init stm32_usart_init(void)
  1813. {
  1814. static char banner[] __initdata = "STM32 USART driver initialized";
  1815. int ret;
  1816. pr_info("%s\n", banner);
  1817. ret = uart_register_driver(&stm32_usart_driver);
  1818. if (ret)
  1819. return ret;
  1820. ret = platform_driver_register(&stm32_serial_driver);
  1821. if (ret)
  1822. uart_unregister_driver(&stm32_usart_driver);
  1823. return ret;
  1824. }
  1825. static void __exit stm32_usart_exit(void)
  1826. {
  1827. platform_driver_unregister(&stm32_serial_driver);
  1828. uart_unregister_driver(&stm32_usart_driver);
  1829. }
  1830. module_init(stm32_usart_init);
  1831. module_exit(stm32_usart_exit);
  1832. MODULE_ALIAS("platform:" DRIVER_NAME);
  1833. MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
  1834. MODULE_LICENSE("GPL v2");