stm32-usart.c 58 KB

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