stm32-usart.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477
  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@st.com>
  7. *
  8. * Inspired by st-asc.c from STMicroelectronics (c)
  9. */
  10. #if defined(CONFIG_SERIAL_STM32_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  11. #define SUPPORT_SYSRQ
  12. #endif
  13. #include <linux/clk.h>
  14. #include <linux/console.h>
  15. #include <linux/delay.h>
  16. #include <linux/dma-direction.h>
  17. #include <linux/dmaengine.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/io.h>
  20. #include <linux/iopoll.h>
  21. #include <linux/irq.h>
  22. #include <linux/module.h>
  23. #include <linux/of.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/pinctrl/consumer.h>
  26. #include <linux/platform_device.h>
  27. #include <linux/pm_runtime.h>
  28. #include <linux/pm_wakeirq.h>
  29. #include <linux/serial_core.h>
  30. #include <linux/serial.h>
  31. #include <linux/spinlock.h>
  32. #include <linux/sysrq.h>
  33. #include <linux/tty_flip.h>
  34. #include <linux/tty.h>
  35. #include "stm32-usart.h"
  36. static void stm32_stop_tx(struct uart_port *port);
  37. static void stm32_transmit_chars(struct uart_port *port);
  38. static inline struct stm32_port *to_stm32_port(struct uart_port *port)
  39. {
  40. return container_of(port, struct stm32_port, port);
  41. }
  42. static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
  43. {
  44. u32 val;
  45. val = readl_relaxed(port->membase + reg);
  46. val |= bits;
  47. writel_relaxed(val, port->membase + reg);
  48. }
  49. static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
  50. {
  51. u32 val;
  52. val = readl_relaxed(port->membase + reg);
  53. val &= ~bits;
  54. writel_relaxed(val, port->membase + reg);
  55. }
  56. static void stm32_config_reg_rs485(u32 *cr1, u32 *cr3, u32 delay_ADE,
  57. u32 delay_DDE, u32 baud)
  58. {
  59. u32 rs485_deat_dedt;
  60. u32 rs485_deat_dedt_max = (USART_CR1_DEAT_MASK >> USART_CR1_DEAT_SHIFT);
  61. bool over8;
  62. *cr3 |= USART_CR3_DEM;
  63. over8 = *cr1 & USART_CR1_OVER8;
  64. if (over8)
  65. rs485_deat_dedt = delay_ADE * baud * 8;
  66. else
  67. rs485_deat_dedt = delay_ADE * baud * 16;
  68. rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
  69. rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
  70. rs485_deat_dedt_max : rs485_deat_dedt;
  71. rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEAT_SHIFT) &
  72. USART_CR1_DEAT_MASK;
  73. *cr1 |= rs485_deat_dedt;
  74. if (over8)
  75. rs485_deat_dedt = delay_DDE * baud * 8;
  76. else
  77. rs485_deat_dedt = delay_DDE * baud * 16;
  78. rs485_deat_dedt = DIV_ROUND_CLOSEST(rs485_deat_dedt, 1000);
  79. rs485_deat_dedt = rs485_deat_dedt > rs485_deat_dedt_max ?
  80. rs485_deat_dedt_max : rs485_deat_dedt;
  81. rs485_deat_dedt = (rs485_deat_dedt << USART_CR1_DEDT_SHIFT) &
  82. USART_CR1_DEDT_MASK;
  83. *cr1 |= rs485_deat_dedt;
  84. }
  85. static int stm32_config_rs485(struct uart_port *port,
  86. struct serial_rs485 *rs485conf)
  87. {
  88. struct stm32_port *stm32_port = to_stm32_port(port);
  89. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  90. struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  91. u32 usartdiv, baud, cr1, cr3;
  92. bool over8;
  93. stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  94. port->rs485 = *rs485conf;
  95. rs485conf->flags |= SER_RS485_RX_DURING_TX;
  96. if (rs485conf->flags & SER_RS485_ENABLED) {
  97. cr1 = readl_relaxed(port->membase + ofs->cr1);
  98. cr3 = readl_relaxed(port->membase + ofs->cr3);
  99. usartdiv = readl_relaxed(port->membase + ofs->brr);
  100. usartdiv = usartdiv & GENMASK(15, 0);
  101. over8 = cr1 & USART_CR1_OVER8;
  102. if (over8)
  103. usartdiv = usartdiv | (usartdiv & GENMASK(4, 0))
  104. << USART_BRR_04_R_SHIFT;
  105. baud = DIV_ROUND_CLOSEST(port->uartclk, usartdiv);
  106. stm32_config_reg_rs485(&cr1, &cr3,
  107. rs485conf->delay_rts_before_send,
  108. rs485conf->delay_rts_after_send, baud);
  109. if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
  110. cr3 &= ~USART_CR3_DEP;
  111. rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
  112. } else {
  113. cr3 |= USART_CR3_DEP;
  114. rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  115. }
  116. writel_relaxed(cr3, port->membase + ofs->cr3);
  117. writel_relaxed(cr1, port->membase + ofs->cr1);
  118. } else {
  119. stm32_clr_bits(port, ofs->cr3, USART_CR3_DEM | USART_CR3_DEP);
  120. stm32_clr_bits(port, ofs->cr1,
  121. USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
  122. }
  123. stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  124. return 0;
  125. }
  126. static int stm32_init_rs485(struct uart_port *port,
  127. struct platform_device *pdev)
  128. {
  129. struct serial_rs485 *rs485conf = &port->rs485;
  130. rs485conf->flags = 0;
  131. rs485conf->delay_rts_before_send = 0;
  132. rs485conf->delay_rts_after_send = 0;
  133. if (!pdev->dev.of_node)
  134. return -ENODEV;
  135. uart_get_rs485_mode(&pdev->dev, rs485conf);
  136. return 0;
  137. }
  138. static int stm32_pending_rx(struct uart_port *port, u32 *sr, int *last_res,
  139. bool threaded)
  140. {
  141. struct stm32_port *stm32_port = to_stm32_port(port);
  142. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  143. enum dma_status status;
  144. struct dma_tx_state state;
  145. *sr = readl_relaxed(port->membase + ofs->isr);
  146. if (threaded && stm32_port->rx_ch) {
  147. status = dmaengine_tx_status(stm32_port->rx_ch,
  148. stm32_port->rx_ch->cookie,
  149. &state);
  150. if ((status == DMA_IN_PROGRESS) &&
  151. (*last_res != state.residue))
  152. return 1;
  153. else
  154. return 0;
  155. } else if (*sr & USART_SR_RXNE) {
  156. return 1;
  157. }
  158. return 0;
  159. }
  160. static unsigned long stm32_get_char(struct uart_port *port, u32 *sr,
  161. int *last_res)
  162. {
  163. struct stm32_port *stm32_port = to_stm32_port(port);
  164. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  165. unsigned long c;
  166. if (stm32_port->rx_ch) {
  167. c = stm32_port->rx_buf[RX_BUF_L - (*last_res)--];
  168. if ((*last_res) == 0)
  169. *last_res = RX_BUF_L;
  170. } else {
  171. c = readl_relaxed(port->membase + ofs->rdr);
  172. /* apply RDR data mask */
  173. c &= stm32_port->rdr_mask;
  174. }
  175. return c;
  176. }
  177. static void stm32_receive_chars(struct uart_port *port, bool threaded)
  178. {
  179. struct tty_port *tport = &port->state->port;
  180. struct stm32_port *stm32_port = to_stm32_port(port);
  181. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  182. unsigned long c;
  183. u32 sr;
  184. char flag;
  185. if (irqd_is_wakeup_set(irq_get_irq_data(port->irq)))
  186. pm_wakeup_event(tport->tty->dev, 0);
  187. while (stm32_pending_rx(port, &sr, &stm32_port->last_res, threaded)) {
  188. sr |= USART_SR_DUMMY_RX;
  189. flag = TTY_NORMAL;
  190. /*
  191. * Status bits has to be cleared before reading the RDR:
  192. * In FIFO mode, reading the RDR will pop the next data
  193. * (if any) along with its status bits into the SR.
  194. * Not doing so leads to misalignement between RDR and SR,
  195. * and clear status bits of the next rx data.
  196. *
  197. * Clear errors flags for stm32f7 and stm32h7 compatible
  198. * devices. On stm32f4 compatible devices, the error bit is
  199. * cleared by the sequence [read SR - read DR].
  200. */
  201. if ((sr & USART_SR_ERR_MASK) && ofs->icr != UNDEF_REG)
  202. writel_relaxed(sr & USART_SR_ERR_MASK,
  203. port->membase + ofs->icr);
  204. c = stm32_get_char(port, &sr, &stm32_port->last_res);
  205. port->icount.rx++;
  206. if (sr & USART_SR_ERR_MASK) {
  207. if (sr & USART_SR_ORE) {
  208. port->icount.overrun++;
  209. } else if (sr & USART_SR_PE) {
  210. port->icount.parity++;
  211. } else if (sr & USART_SR_FE) {
  212. /* Break detection if character is null */
  213. if (!c) {
  214. port->icount.brk++;
  215. if (uart_handle_break(port))
  216. continue;
  217. } else {
  218. port->icount.frame++;
  219. }
  220. }
  221. sr &= port->read_status_mask;
  222. if (sr & USART_SR_PE) {
  223. flag = TTY_PARITY;
  224. } else if (sr & USART_SR_FE) {
  225. if (!c)
  226. flag = TTY_BREAK;
  227. else
  228. flag = TTY_FRAME;
  229. }
  230. }
  231. if (uart_handle_sysrq_char(port, c))
  232. continue;
  233. uart_insert_char(port, sr, USART_SR_ORE, c, flag);
  234. }
  235. spin_unlock(&port->lock);
  236. tty_flip_buffer_push(tport);
  237. spin_lock(&port->lock);
  238. }
  239. static void stm32_tx_dma_complete(void *arg)
  240. {
  241. struct uart_port *port = arg;
  242. struct stm32_port *stm32port = to_stm32_port(port);
  243. struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  244. stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
  245. stm32port->tx_dma_busy = false;
  246. /* Let's see if we have pending data to send */
  247. stm32_transmit_chars(port);
  248. }
  249. static void stm32_tx_interrupt_enable(struct uart_port *port)
  250. {
  251. struct stm32_port *stm32_port = to_stm32_port(port);
  252. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  253. /*
  254. * Enables TX FIFO threashold irq when FIFO is enabled,
  255. * or TX empty irq when FIFO is disabled
  256. */
  257. if (stm32_port->fifoen)
  258. stm32_set_bits(port, ofs->cr3, USART_CR3_TXFTIE);
  259. else
  260. stm32_set_bits(port, ofs->cr1, USART_CR1_TXEIE);
  261. }
  262. static void stm32_tx_interrupt_disable(struct uart_port *port)
  263. {
  264. struct stm32_port *stm32_port = to_stm32_port(port);
  265. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  266. if (stm32_port->fifoen)
  267. stm32_clr_bits(port, ofs->cr3, USART_CR3_TXFTIE);
  268. else
  269. stm32_clr_bits(port, ofs->cr1, USART_CR1_TXEIE);
  270. }
  271. static void stm32_transmit_chars_pio(struct uart_port *port)
  272. {
  273. struct stm32_port *stm32_port = to_stm32_port(port);
  274. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  275. struct circ_buf *xmit = &port->state->xmit;
  276. if (stm32_port->tx_dma_busy) {
  277. stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
  278. stm32_port->tx_dma_busy = false;
  279. }
  280. while (!uart_circ_empty(xmit)) {
  281. /* Check that TDR is empty before filling FIFO */
  282. if (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
  283. break;
  284. writel_relaxed(xmit->buf[xmit->tail], port->membase + ofs->tdr);
  285. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  286. port->icount.tx++;
  287. }
  288. /* rely on TXE irq (mask or unmask) for sending remaining data */
  289. if (uart_circ_empty(xmit))
  290. stm32_tx_interrupt_disable(port);
  291. else
  292. stm32_tx_interrupt_enable(port);
  293. }
  294. static void stm32_transmit_chars_dma(struct uart_port *port)
  295. {
  296. struct stm32_port *stm32port = to_stm32_port(port);
  297. struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  298. struct circ_buf *xmit = &port->state->xmit;
  299. struct dma_async_tx_descriptor *desc = NULL;
  300. dma_cookie_t cookie;
  301. unsigned int count, i;
  302. if (stm32port->tx_dma_busy)
  303. return;
  304. stm32port->tx_dma_busy = true;
  305. count = uart_circ_chars_pending(xmit);
  306. if (count > TX_BUF_L)
  307. count = TX_BUF_L;
  308. if (xmit->tail < xmit->head) {
  309. memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], count);
  310. } else {
  311. size_t one = UART_XMIT_SIZE - xmit->tail;
  312. size_t two;
  313. if (one > count)
  314. one = count;
  315. two = count - one;
  316. memcpy(&stm32port->tx_buf[0], &xmit->buf[xmit->tail], one);
  317. if (two)
  318. memcpy(&stm32port->tx_buf[one], &xmit->buf[0], two);
  319. }
  320. desc = dmaengine_prep_slave_single(stm32port->tx_ch,
  321. stm32port->tx_dma_buf,
  322. count,
  323. DMA_MEM_TO_DEV,
  324. DMA_PREP_INTERRUPT);
  325. if (!desc) {
  326. for (i = count; i > 0; i--)
  327. stm32_transmit_chars_pio(port);
  328. return;
  329. }
  330. desc->callback = stm32_tx_dma_complete;
  331. desc->callback_param = port;
  332. /* Push current DMA TX transaction in the pending queue */
  333. cookie = dmaengine_submit(desc);
  334. /* Issue pending DMA TX requests */
  335. dma_async_issue_pending(stm32port->tx_ch);
  336. stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
  337. xmit->tail = (xmit->tail + count) & (UART_XMIT_SIZE - 1);
  338. port->icount.tx += count;
  339. }
  340. static void stm32_transmit_chars(struct uart_port *port)
  341. {
  342. struct stm32_port *stm32_port = to_stm32_port(port);
  343. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  344. struct circ_buf *xmit = &port->state->xmit;
  345. if (port->x_char) {
  346. if (stm32_port->tx_dma_busy)
  347. stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
  348. writel_relaxed(port->x_char, port->membase + ofs->tdr);
  349. port->x_char = 0;
  350. port->icount.tx++;
  351. if (stm32_port->tx_dma_busy)
  352. stm32_set_bits(port, ofs->cr3, USART_CR3_DMAT);
  353. return;
  354. }
  355. if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
  356. stm32_tx_interrupt_disable(port);
  357. return;
  358. }
  359. if (ofs->icr == UNDEF_REG)
  360. stm32_clr_bits(port, ofs->isr, USART_SR_TC);
  361. else
  362. writel_relaxed(USART_ICR_TCCF, port->membase + ofs->icr);
  363. if (stm32_port->tx_ch)
  364. stm32_transmit_chars_dma(port);
  365. else
  366. stm32_transmit_chars_pio(port);
  367. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  368. uart_write_wakeup(port);
  369. if (uart_circ_empty(xmit))
  370. stm32_tx_interrupt_disable(port);
  371. }
  372. static irqreturn_t stm32_interrupt(int irq, void *ptr)
  373. {
  374. struct uart_port *port = ptr;
  375. struct stm32_port *stm32_port = to_stm32_port(port);
  376. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  377. u32 sr;
  378. spin_lock(&port->lock);
  379. sr = readl_relaxed(port->membase + ofs->isr);
  380. if ((sr & USART_SR_RTOF) && ofs->icr != UNDEF_REG)
  381. writel_relaxed(USART_ICR_RTOCF,
  382. port->membase + ofs->icr);
  383. if ((sr & USART_SR_WUF) && (ofs->icr != UNDEF_REG))
  384. writel_relaxed(USART_ICR_WUCF,
  385. port->membase + ofs->icr);
  386. if ((sr & USART_SR_RXNE) && !(stm32_port->rx_ch))
  387. stm32_receive_chars(port, false);
  388. if ((sr & USART_SR_TXE) && !(stm32_port->tx_ch))
  389. stm32_transmit_chars(port);
  390. spin_unlock(&port->lock);
  391. if (stm32_port->rx_ch)
  392. return IRQ_WAKE_THREAD;
  393. else
  394. return IRQ_HANDLED;
  395. }
  396. static irqreturn_t stm32_threaded_interrupt(int irq, void *ptr)
  397. {
  398. struct uart_port *port = ptr;
  399. struct stm32_port *stm32_port = to_stm32_port(port);
  400. spin_lock(&port->lock);
  401. if (stm32_port->rx_ch)
  402. stm32_receive_chars(port, true);
  403. spin_unlock(&port->lock);
  404. return IRQ_HANDLED;
  405. }
  406. static unsigned int stm32_tx_empty(struct uart_port *port)
  407. {
  408. struct stm32_port *stm32_port = to_stm32_port(port);
  409. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  410. if (readl_relaxed(port->membase + ofs->isr) & USART_SR_TC)
  411. return TIOCSER_TEMT;
  412. return 0;
  413. }
  414. static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
  415. {
  416. struct stm32_port *stm32_port = to_stm32_port(port);
  417. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  418. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  419. stm32_set_bits(port, ofs->cr3, USART_CR3_RTSE);
  420. else
  421. stm32_clr_bits(port, ofs->cr3, USART_CR3_RTSE);
  422. }
  423. static unsigned int stm32_get_mctrl(struct uart_port *port)
  424. {
  425. /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
  426. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  427. }
  428. /* Transmit stop */
  429. static void stm32_stop_tx(struct uart_port *port)
  430. {
  431. stm32_tx_interrupt_disable(port);
  432. }
  433. /* There are probably characters waiting to be transmitted. */
  434. static void stm32_start_tx(struct uart_port *port)
  435. {
  436. struct circ_buf *xmit = &port->state->xmit;
  437. if (uart_circ_empty(xmit))
  438. return;
  439. stm32_transmit_chars(port);
  440. }
  441. /* Throttle the remote when input buffer is about to overflow. */
  442. static void stm32_throttle(struct uart_port *port)
  443. {
  444. struct stm32_port *stm32_port = to_stm32_port(port);
  445. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  446. unsigned long flags;
  447. spin_lock_irqsave(&port->lock, flags);
  448. stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
  449. if (stm32_port->cr3_irq)
  450. stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
  451. spin_unlock_irqrestore(&port->lock, flags);
  452. }
  453. /* Unthrottle the remote, the input buffer can now accept data. */
  454. static void stm32_unthrottle(struct uart_port *port)
  455. {
  456. struct stm32_port *stm32_port = to_stm32_port(port);
  457. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  458. unsigned long flags;
  459. spin_lock_irqsave(&port->lock, flags);
  460. stm32_set_bits(port, ofs->cr1, stm32_port->cr1_irq);
  461. if (stm32_port->cr3_irq)
  462. stm32_set_bits(port, ofs->cr3, stm32_port->cr3_irq);
  463. spin_unlock_irqrestore(&port->lock, flags);
  464. }
  465. /* Receive stop */
  466. static void stm32_stop_rx(struct uart_port *port)
  467. {
  468. struct stm32_port *stm32_port = to_stm32_port(port);
  469. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  470. stm32_clr_bits(port, ofs->cr1, stm32_port->cr1_irq);
  471. if (stm32_port->cr3_irq)
  472. stm32_clr_bits(port, ofs->cr3, stm32_port->cr3_irq);
  473. }
  474. /* Handle breaks - ignored by us */
  475. static void stm32_break_ctl(struct uart_port *port, int break_state)
  476. {
  477. }
  478. static int stm32_startup(struct uart_port *port)
  479. {
  480. struct stm32_port *stm32_port = to_stm32_port(port);
  481. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  482. const char *name = to_platform_device(port->dev)->name;
  483. u32 val;
  484. int ret;
  485. ret = request_threaded_irq(port->irq, stm32_interrupt,
  486. stm32_threaded_interrupt,
  487. IRQF_NO_SUSPEND, name, port);
  488. if (ret)
  489. return ret;
  490. /* RX FIFO Flush */
  491. if (ofs->rqr != UNDEF_REG)
  492. stm32_set_bits(port, ofs->rqr, USART_RQR_RXFRQ);
  493. /* Tx and RX FIFO configuration */
  494. if (stm32_port->fifoen) {
  495. val = readl_relaxed(port->membase + ofs->cr3);
  496. val &= ~(USART_CR3_TXFTCFG_MASK | USART_CR3_RXFTCFG_MASK);
  497. val |= USART_CR3_TXFTCFG_HALF << USART_CR3_TXFTCFG_SHIFT;
  498. val |= USART_CR3_RXFTCFG_HALF << USART_CR3_RXFTCFG_SHIFT;
  499. writel_relaxed(val, port->membase + ofs->cr3);
  500. }
  501. /* RX FIFO enabling */
  502. val = stm32_port->cr1_irq | USART_CR1_RE;
  503. if (stm32_port->fifoen)
  504. val |= USART_CR1_FIFOEN;
  505. stm32_set_bits(port, ofs->cr1, val);
  506. return 0;
  507. }
  508. static void stm32_shutdown(struct uart_port *port)
  509. {
  510. struct stm32_port *stm32_port = to_stm32_port(port);
  511. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  512. struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  513. u32 val, isr;
  514. int ret;
  515. val = USART_CR1_TXEIE | USART_CR1_TE;
  516. val |= stm32_port->cr1_irq | USART_CR1_RE;
  517. val |= BIT(cfg->uart_enable_bit);
  518. if (stm32_port->fifoen)
  519. val |= USART_CR1_FIFOEN;
  520. ret = readl_relaxed_poll_timeout(port->membase + ofs->isr,
  521. isr, (isr & USART_SR_TC),
  522. 10, 100000);
  523. if (ret)
  524. dev_err(port->dev, "transmission complete not set\n");
  525. stm32_clr_bits(port, ofs->cr1, val);
  526. free_irq(port->irq, port);
  527. }
  528. static unsigned int stm32_get_databits(struct ktermios *termios)
  529. {
  530. unsigned int bits;
  531. tcflag_t cflag = termios->c_cflag;
  532. switch (cflag & CSIZE) {
  533. /*
  534. * CSIZE settings are not necessarily supported in hardware.
  535. * CSIZE unsupported configurations are handled here to set word length
  536. * to 8 bits word as default configuration and to print debug message.
  537. */
  538. case CS5:
  539. bits = 5;
  540. break;
  541. case CS6:
  542. bits = 6;
  543. break;
  544. case CS7:
  545. bits = 7;
  546. break;
  547. /* default including CS8 */
  548. default:
  549. bits = 8;
  550. break;
  551. }
  552. return bits;
  553. }
  554. static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
  555. struct ktermios *old)
  556. {
  557. struct stm32_port *stm32_port = to_stm32_port(port);
  558. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  559. struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  560. struct serial_rs485 *rs485conf = &port->rs485;
  561. unsigned int baud, bits;
  562. u32 usartdiv, mantissa, fraction, oversampling;
  563. tcflag_t cflag = termios->c_cflag;
  564. u32 cr1, cr2, cr3, isr;
  565. unsigned long flags;
  566. int ret;
  567. if (!stm32_port->hw_flow_control)
  568. cflag &= ~CRTSCTS;
  569. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
  570. spin_lock_irqsave(&port->lock, flags);
  571. ret = readl_relaxed_poll_timeout_atomic(port->membase + ofs->isr,
  572. isr,
  573. (isr & USART_SR_TC),
  574. 10, 100000);
  575. /* Send the TC error message only when ISR_TC is not set. */
  576. if (ret)
  577. dev_err(port->dev, "Transmission is not complete\n");
  578. /* Stop serial port and reset value */
  579. writel_relaxed(0, port->membase + ofs->cr1);
  580. /* flush RX & TX FIFO */
  581. if (ofs->rqr != UNDEF_REG)
  582. stm32_set_bits(port, ofs->rqr,
  583. USART_RQR_TXFRQ | USART_RQR_RXFRQ);
  584. cr1 = USART_CR1_TE | USART_CR1_RE;
  585. if (stm32_port->fifoen)
  586. cr1 |= USART_CR1_FIFOEN;
  587. cr2 = 0;
  588. cr3 = readl_relaxed(port->membase + ofs->cr3);
  589. cr3 &= USART_CR3_TXFTIE | USART_CR3_RXFTCFG_MASK | USART_CR3_RXFTIE
  590. | USART_CR3_TXFTCFG_MASK;
  591. if (cflag & CSTOPB)
  592. cr2 |= USART_CR2_STOP_2B;
  593. bits = stm32_get_databits(termios);
  594. stm32_port->rdr_mask = (BIT(bits) - 1);
  595. if (cflag & PARENB) {
  596. bits++;
  597. cr1 |= USART_CR1_PCE;
  598. }
  599. /*
  600. * Word length configuration:
  601. * CS8 + parity, 9 bits word aka [M1:M0] = 0b01
  602. * CS7 or (CS6 + parity), 7 bits word aka [M1:M0] = 0b10
  603. * CS8 or (CS7 + parity), 8 bits word aka [M1:M0] = 0b00
  604. * M0 and M1 already cleared by cr1 initialization.
  605. */
  606. if (bits == 9)
  607. cr1 |= USART_CR1_M0;
  608. else if ((bits == 7) && cfg->has_7bits_data)
  609. cr1 |= USART_CR1_M1;
  610. else if (bits != 8)
  611. dev_dbg(port->dev, "Unsupported data bits config: %u bits\n"
  612. , bits);
  613. if (ofs->rtor != UNDEF_REG && (stm32_port->rx_ch ||
  614. stm32_port->fifoen)) {
  615. if (cflag & CSTOPB)
  616. bits = bits + 3; /* 1 start bit + 2 stop bits */
  617. else
  618. bits = bits + 2; /* 1 start bit + 1 stop bit */
  619. /* RX timeout irq to occur after last stop bit + bits */
  620. stm32_port->cr1_irq = USART_CR1_RTOIE;
  621. writel_relaxed(bits, port->membase + ofs->rtor);
  622. cr2 |= USART_CR2_RTOEN;
  623. /* Not using dma, enable fifo threshold irq */
  624. if (!stm32_port->rx_ch)
  625. stm32_port->cr3_irq = USART_CR3_RXFTIE;
  626. }
  627. cr1 |= stm32_port->cr1_irq;
  628. cr3 |= stm32_port->cr3_irq;
  629. if (cflag & PARODD)
  630. cr1 |= USART_CR1_PS;
  631. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  632. if (cflag & CRTSCTS) {
  633. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  634. cr3 |= USART_CR3_CTSE | USART_CR3_RTSE;
  635. }
  636. usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
  637. /*
  638. * The USART supports 16 or 8 times oversampling.
  639. * By default we prefer 16 times oversampling, so that the receiver
  640. * has a better tolerance to clock deviations.
  641. * 8 times oversampling is only used to achieve higher speeds.
  642. */
  643. if (usartdiv < 16) {
  644. oversampling = 8;
  645. cr1 |= USART_CR1_OVER8;
  646. stm32_set_bits(port, ofs->cr1, USART_CR1_OVER8);
  647. } else {
  648. oversampling = 16;
  649. cr1 &= ~USART_CR1_OVER8;
  650. stm32_clr_bits(port, ofs->cr1, USART_CR1_OVER8);
  651. }
  652. mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
  653. fraction = usartdiv % oversampling;
  654. writel_relaxed(mantissa | fraction, port->membase + ofs->brr);
  655. uart_update_timeout(port, cflag, baud);
  656. port->read_status_mask = USART_SR_ORE;
  657. if (termios->c_iflag & INPCK)
  658. port->read_status_mask |= USART_SR_PE | USART_SR_FE;
  659. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  660. port->read_status_mask |= USART_SR_FE;
  661. /* Characters to ignore */
  662. port->ignore_status_mask = 0;
  663. if (termios->c_iflag & IGNPAR)
  664. port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
  665. if (termios->c_iflag & IGNBRK) {
  666. port->ignore_status_mask |= USART_SR_FE;
  667. /*
  668. * If we're ignoring parity and break indicators,
  669. * ignore overruns too (for real raw support).
  670. */
  671. if (termios->c_iflag & IGNPAR)
  672. port->ignore_status_mask |= USART_SR_ORE;
  673. }
  674. /* Ignore all characters if CREAD is not set */
  675. if ((termios->c_cflag & CREAD) == 0)
  676. port->ignore_status_mask |= USART_SR_DUMMY_RX;
  677. if (stm32_port->rx_ch)
  678. cr3 |= USART_CR3_DMAR;
  679. if (rs485conf->flags & SER_RS485_ENABLED) {
  680. stm32_config_reg_rs485(&cr1, &cr3,
  681. rs485conf->delay_rts_before_send,
  682. rs485conf->delay_rts_after_send, baud);
  683. if (rs485conf->flags & SER_RS485_RTS_ON_SEND) {
  684. cr3 &= ~USART_CR3_DEP;
  685. rs485conf->flags &= ~SER_RS485_RTS_AFTER_SEND;
  686. } else {
  687. cr3 |= USART_CR3_DEP;
  688. rs485conf->flags |= SER_RS485_RTS_AFTER_SEND;
  689. }
  690. } else {
  691. cr3 &= ~(USART_CR3_DEM | USART_CR3_DEP);
  692. cr1 &= ~(USART_CR1_DEDT_MASK | USART_CR1_DEAT_MASK);
  693. }
  694. writel_relaxed(cr3, port->membase + ofs->cr3);
  695. writel_relaxed(cr2, port->membase + ofs->cr2);
  696. writel_relaxed(cr1, port->membase + ofs->cr1);
  697. stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  698. spin_unlock_irqrestore(&port->lock, flags);
  699. }
  700. static const char *stm32_type(struct uart_port *port)
  701. {
  702. return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
  703. }
  704. static void stm32_release_port(struct uart_port *port)
  705. {
  706. }
  707. static int stm32_request_port(struct uart_port *port)
  708. {
  709. return 0;
  710. }
  711. static void stm32_config_port(struct uart_port *port, int flags)
  712. {
  713. if (flags & UART_CONFIG_TYPE)
  714. port->type = PORT_STM32;
  715. }
  716. static int
  717. stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
  718. {
  719. /* No user changeable parameters */
  720. return -EINVAL;
  721. }
  722. static void stm32_pm(struct uart_port *port, unsigned int state,
  723. unsigned int oldstate)
  724. {
  725. struct stm32_port *stm32port = container_of(port,
  726. struct stm32_port, port);
  727. struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  728. struct stm32_usart_config *cfg = &stm32port->info->cfg;
  729. unsigned long flags = 0;
  730. switch (state) {
  731. case UART_PM_STATE_ON:
  732. pm_runtime_get_sync(port->dev);
  733. break;
  734. case UART_PM_STATE_OFF:
  735. spin_lock_irqsave(&port->lock, flags);
  736. stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  737. spin_unlock_irqrestore(&port->lock, flags);
  738. pm_runtime_put_sync(port->dev);
  739. break;
  740. }
  741. }
  742. static const struct uart_ops stm32_uart_ops = {
  743. .tx_empty = stm32_tx_empty,
  744. .set_mctrl = stm32_set_mctrl,
  745. .get_mctrl = stm32_get_mctrl,
  746. .stop_tx = stm32_stop_tx,
  747. .start_tx = stm32_start_tx,
  748. .throttle = stm32_throttle,
  749. .unthrottle = stm32_unthrottle,
  750. .stop_rx = stm32_stop_rx,
  751. .break_ctl = stm32_break_ctl,
  752. .startup = stm32_startup,
  753. .shutdown = stm32_shutdown,
  754. .set_termios = stm32_set_termios,
  755. .pm = stm32_pm,
  756. .type = stm32_type,
  757. .release_port = stm32_release_port,
  758. .request_port = stm32_request_port,
  759. .config_port = stm32_config_port,
  760. .verify_port = stm32_verify_port,
  761. };
  762. static int stm32_init_port(struct stm32_port *stm32port,
  763. struct platform_device *pdev)
  764. {
  765. struct uart_port *port = &stm32port->port;
  766. struct resource *res;
  767. int ret;
  768. port->iotype = UPIO_MEM;
  769. port->flags = UPF_BOOT_AUTOCONF;
  770. port->ops = &stm32_uart_ops;
  771. port->dev = &pdev->dev;
  772. port->fifosize = stm32port->info->cfg.fifosize;
  773. ret = platform_get_irq(pdev, 0);
  774. if (ret <= 0)
  775. return ret ? : -ENODEV;
  776. port->irq = ret;
  777. port->rs485_config = stm32_config_rs485;
  778. stm32_init_rs485(port, pdev);
  779. if (stm32port->info->cfg.has_wakeup) {
  780. stm32port->wakeirq = platform_get_irq_optional(pdev, 1);
  781. if (stm32port->wakeirq <= 0 && stm32port->wakeirq != -ENXIO)
  782. return stm32port->wakeirq ? : -ENODEV;
  783. }
  784. stm32port->fifoen = stm32port->info->cfg.has_fifo;
  785. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  786. port->membase = devm_ioremap_resource(&pdev->dev, res);
  787. if (IS_ERR(port->membase))
  788. return PTR_ERR(port->membase);
  789. port->mapbase = res->start;
  790. spin_lock_init(&port->lock);
  791. stm32port->clk = devm_clk_get(&pdev->dev, NULL);
  792. if (IS_ERR(stm32port->clk))
  793. return PTR_ERR(stm32port->clk);
  794. /* Ensure that clk rate is correct by enabling the clk */
  795. ret = clk_prepare_enable(stm32port->clk);
  796. if (ret)
  797. return ret;
  798. stm32port->port.uartclk = clk_get_rate(stm32port->clk);
  799. if (!stm32port->port.uartclk) {
  800. clk_disable_unprepare(stm32port->clk);
  801. ret = -EINVAL;
  802. }
  803. return ret;
  804. }
  805. static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
  806. {
  807. struct device_node *np = pdev->dev.of_node;
  808. int id;
  809. if (!np)
  810. return NULL;
  811. id = of_alias_get_id(np, "serial");
  812. if (id < 0) {
  813. dev_err(&pdev->dev, "failed to get alias id, errno %d\n", id);
  814. return NULL;
  815. }
  816. if (WARN_ON(id >= STM32_MAX_PORTS))
  817. return NULL;
  818. stm32_ports[id].hw_flow_control = of_property_read_bool(np,
  819. "st,hw-flow-ctrl");
  820. stm32_ports[id].port.line = id;
  821. stm32_ports[id].cr1_irq = USART_CR1_RXNEIE;
  822. stm32_ports[id].cr3_irq = 0;
  823. stm32_ports[id].last_res = RX_BUF_L;
  824. return &stm32_ports[id];
  825. }
  826. #ifdef CONFIG_OF
  827. static const struct of_device_id stm32_match[] = {
  828. { .compatible = "st,stm32-uart", .data = &stm32f4_info},
  829. { .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
  830. { .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
  831. {},
  832. };
  833. MODULE_DEVICE_TABLE(of, stm32_match);
  834. #endif
  835. static int stm32_of_dma_rx_probe(struct stm32_port *stm32port,
  836. struct platform_device *pdev)
  837. {
  838. struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  839. struct uart_port *port = &stm32port->port;
  840. struct device *dev = &pdev->dev;
  841. struct dma_slave_config config;
  842. struct dma_async_tx_descriptor *desc = NULL;
  843. dma_cookie_t cookie;
  844. int ret;
  845. /* Request DMA RX channel */
  846. stm32port->rx_ch = dma_request_slave_channel(dev, "rx");
  847. if (!stm32port->rx_ch) {
  848. dev_info(dev, "rx dma alloc failed\n");
  849. return -ENODEV;
  850. }
  851. stm32port->rx_buf = dma_alloc_coherent(&pdev->dev, RX_BUF_L,
  852. &stm32port->rx_dma_buf,
  853. GFP_KERNEL);
  854. if (!stm32port->rx_buf) {
  855. ret = -ENOMEM;
  856. goto alloc_err;
  857. }
  858. /* Configure DMA channel */
  859. memset(&config, 0, sizeof(config));
  860. config.src_addr = port->mapbase + ofs->rdr;
  861. config.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  862. ret = dmaengine_slave_config(stm32port->rx_ch, &config);
  863. if (ret < 0) {
  864. dev_err(dev, "rx dma channel config failed\n");
  865. ret = -ENODEV;
  866. goto config_err;
  867. }
  868. /* Prepare a DMA cyclic transaction */
  869. desc = dmaengine_prep_dma_cyclic(stm32port->rx_ch,
  870. stm32port->rx_dma_buf,
  871. RX_BUF_L, RX_BUF_P, DMA_DEV_TO_MEM,
  872. DMA_PREP_INTERRUPT);
  873. if (!desc) {
  874. dev_err(dev, "rx dma prep cyclic failed\n");
  875. ret = -ENODEV;
  876. goto config_err;
  877. }
  878. /* No callback as dma buffer is drained on usart interrupt */
  879. desc->callback = NULL;
  880. desc->callback_param = NULL;
  881. /* Push current DMA transaction in the pending queue */
  882. cookie = dmaengine_submit(desc);
  883. /* Issue pending DMA requests */
  884. dma_async_issue_pending(stm32port->rx_ch);
  885. return 0;
  886. config_err:
  887. dma_free_coherent(&pdev->dev,
  888. RX_BUF_L, stm32port->rx_buf,
  889. stm32port->rx_dma_buf);
  890. alloc_err:
  891. dma_release_channel(stm32port->rx_ch);
  892. stm32port->rx_ch = NULL;
  893. return ret;
  894. }
  895. static int stm32_of_dma_tx_probe(struct stm32_port *stm32port,
  896. struct platform_device *pdev)
  897. {
  898. struct stm32_usart_offsets *ofs = &stm32port->info->ofs;
  899. struct uart_port *port = &stm32port->port;
  900. struct device *dev = &pdev->dev;
  901. struct dma_slave_config config;
  902. int ret;
  903. stm32port->tx_dma_busy = false;
  904. /* Request DMA TX channel */
  905. stm32port->tx_ch = dma_request_slave_channel(dev, "tx");
  906. if (!stm32port->tx_ch) {
  907. dev_info(dev, "tx dma alloc failed\n");
  908. return -ENODEV;
  909. }
  910. stm32port->tx_buf = dma_alloc_coherent(&pdev->dev, TX_BUF_L,
  911. &stm32port->tx_dma_buf,
  912. GFP_KERNEL);
  913. if (!stm32port->tx_buf) {
  914. ret = -ENOMEM;
  915. goto alloc_err;
  916. }
  917. /* Configure DMA channel */
  918. memset(&config, 0, sizeof(config));
  919. config.dst_addr = port->mapbase + ofs->tdr;
  920. config.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
  921. ret = dmaengine_slave_config(stm32port->tx_ch, &config);
  922. if (ret < 0) {
  923. dev_err(dev, "tx dma channel config failed\n");
  924. ret = -ENODEV;
  925. goto config_err;
  926. }
  927. return 0;
  928. config_err:
  929. dma_free_coherent(&pdev->dev,
  930. TX_BUF_L, stm32port->tx_buf,
  931. stm32port->tx_dma_buf);
  932. alloc_err:
  933. dma_release_channel(stm32port->tx_ch);
  934. stm32port->tx_ch = NULL;
  935. return ret;
  936. }
  937. static int stm32_serial_probe(struct platform_device *pdev)
  938. {
  939. const struct of_device_id *match;
  940. struct stm32_port *stm32port;
  941. int ret;
  942. stm32port = stm32_of_get_stm32_port(pdev);
  943. if (!stm32port)
  944. return -ENODEV;
  945. match = of_match_device(stm32_match, &pdev->dev);
  946. if (match && match->data)
  947. stm32port->info = (struct stm32_usart_info *)match->data;
  948. else
  949. return -EINVAL;
  950. ret = stm32_init_port(stm32port, pdev);
  951. if (ret)
  952. return ret;
  953. if (stm32port->wakeirq > 0) {
  954. ret = device_init_wakeup(&pdev->dev, true);
  955. if (ret)
  956. goto err_uninit;
  957. ret = dev_pm_set_dedicated_wake_irq(&pdev->dev,
  958. stm32port->wakeirq);
  959. if (ret)
  960. goto err_nowup;
  961. device_set_wakeup_enable(&pdev->dev, false);
  962. }
  963. ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
  964. if (ret)
  965. goto err_wirq;
  966. ret = stm32_of_dma_rx_probe(stm32port, pdev);
  967. if (ret)
  968. dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n");
  969. ret = stm32_of_dma_tx_probe(stm32port, pdev);
  970. if (ret)
  971. dev_info(&pdev->dev, "interrupt mode used for tx (no dma)\n");
  972. platform_set_drvdata(pdev, &stm32port->port);
  973. pm_runtime_get_noresume(&pdev->dev);
  974. pm_runtime_set_active(&pdev->dev);
  975. pm_runtime_enable(&pdev->dev);
  976. pm_runtime_put_sync(&pdev->dev);
  977. return 0;
  978. err_wirq:
  979. if (stm32port->wakeirq > 0)
  980. dev_pm_clear_wake_irq(&pdev->dev);
  981. err_nowup:
  982. if (stm32port->wakeirq > 0)
  983. device_init_wakeup(&pdev->dev, false);
  984. err_uninit:
  985. clk_disable_unprepare(stm32port->clk);
  986. return ret;
  987. }
  988. static int stm32_serial_remove(struct platform_device *pdev)
  989. {
  990. struct uart_port *port = platform_get_drvdata(pdev);
  991. struct stm32_port *stm32_port = to_stm32_port(port);
  992. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  993. int err;
  994. pm_runtime_get_sync(&pdev->dev);
  995. stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAR);
  996. if (stm32_port->rx_ch)
  997. dma_release_channel(stm32_port->rx_ch);
  998. if (stm32_port->rx_dma_buf)
  999. dma_free_coherent(&pdev->dev,
  1000. RX_BUF_L, stm32_port->rx_buf,
  1001. stm32_port->rx_dma_buf);
  1002. stm32_clr_bits(port, ofs->cr3, USART_CR3_DMAT);
  1003. if (stm32_port->tx_ch)
  1004. dma_release_channel(stm32_port->tx_ch);
  1005. if (stm32_port->tx_dma_buf)
  1006. dma_free_coherent(&pdev->dev,
  1007. TX_BUF_L, stm32_port->tx_buf,
  1008. stm32_port->tx_dma_buf);
  1009. if (stm32_port->wakeirq > 0) {
  1010. dev_pm_clear_wake_irq(&pdev->dev);
  1011. device_init_wakeup(&pdev->dev, false);
  1012. }
  1013. clk_disable_unprepare(stm32_port->clk);
  1014. err = uart_remove_one_port(&stm32_usart_driver, port);
  1015. pm_runtime_disable(&pdev->dev);
  1016. pm_runtime_put_noidle(&pdev->dev);
  1017. return err;
  1018. }
  1019. #ifdef CONFIG_SERIAL_STM32_CONSOLE
  1020. static void stm32_console_putchar(struct uart_port *port, int ch)
  1021. {
  1022. struct stm32_port *stm32_port = to_stm32_port(port);
  1023. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1024. while (!(readl_relaxed(port->membase + ofs->isr) & USART_SR_TXE))
  1025. cpu_relax();
  1026. writel_relaxed(ch, port->membase + ofs->tdr);
  1027. }
  1028. static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
  1029. {
  1030. struct uart_port *port = &stm32_ports[co->index].port;
  1031. struct stm32_port *stm32_port = to_stm32_port(port);
  1032. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1033. struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  1034. unsigned long flags;
  1035. u32 old_cr1, new_cr1;
  1036. int locked = 1;
  1037. local_irq_save(flags);
  1038. if (port->sysrq)
  1039. locked = 0;
  1040. else if (oops_in_progress)
  1041. locked = spin_trylock(&port->lock);
  1042. else
  1043. spin_lock(&port->lock);
  1044. /* Save and disable interrupts, enable the transmitter */
  1045. old_cr1 = readl_relaxed(port->membase + ofs->cr1);
  1046. new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
  1047. new_cr1 |= USART_CR1_TE | BIT(cfg->uart_enable_bit);
  1048. writel_relaxed(new_cr1, port->membase + ofs->cr1);
  1049. uart_console_write(port, s, cnt, stm32_console_putchar);
  1050. /* Restore interrupt state */
  1051. writel_relaxed(old_cr1, port->membase + ofs->cr1);
  1052. if (locked)
  1053. spin_unlock(&port->lock);
  1054. local_irq_restore(flags);
  1055. }
  1056. static int stm32_console_setup(struct console *co, char *options)
  1057. {
  1058. struct stm32_port *stm32port;
  1059. int baud = 9600;
  1060. int bits = 8;
  1061. int parity = 'n';
  1062. int flow = 'n';
  1063. if (co->index >= STM32_MAX_PORTS)
  1064. return -ENODEV;
  1065. stm32port = &stm32_ports[co->index];
  1066. /*
  1067. * This driver does not support early console initialization
  1068. * (use ARM early printk support instead), so we only expect
  1069. * this to be called during the uart port registration when the
  1070. * driver gets probed and the port should be mapped at that point.
  1071. */
  1072. if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
  1073. return -ENXIO;
  1074. if (options)
  1075. uart_parse_options(options, &baud, &parity, &bits, &flow);
  1076. return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
  1077. }
  1078. static struct console stm32_console = {
  1079. .name = STM32_SERIAL_NAME,
  1080. .device = uart_console_device,
  1081. .write = stm32_console_write,
  1082. .setup = stm32_console_setup,
  1083. .flags = CON_PRINTBUFFER,
  1084. .index = -1,
  1085. .data = &stm32_usart_driver,
  1086. };
  1087. #define STM32_SERIAL_CONSOLE (&stm32_console)
  1088. #else
  1089. #define STM32_SERIAL_CONSOLE NULL
  1090. #endif /* CONFIG_SERIAL_STM32_CONSOLE */
  1091. static struct uart_driver stm32_usart_driver = {
  1092. .driver_name = DRIVER_NAME,
  1093. .dev_name = STM32_SERIAL_NAME,
  1094. .major = 0,
  1095. .minor = 0,
  1096. .nr = STM32_MAX_PORTS,
  1097. .cons = STM32_SERIAL_CONSOLE,
  1098. };
  1099. static void __maybe_unused stm32_serial_enable_wakeup(struct uart_port *port,
  1100. bool enable)
  1101. {
  1102. struct stm32_port *stm32_port = to_stm32_port(port);
  1103. struct stm32_usart_offsets *ofs = &stm32_port->info->ofs;
  1104. struct stm32_usart_config *cfg = &stm32_port->info->cfg;
  1105. u32 val;
  1106. if (stm32_port->wakeirq <= 0)
  1107. return;
  1108. if (enable) {
  1109. stm32_clr_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  1110. stm32_set_bits(port, ofs->cr1, USART_CR1_UESM);
  1111. val = readl_relaxed(port->membase + ofs->cr3);
  1112. val &= ~USART_CR3_WUS_MASK;
  1113. /* Enable Wake up interrupt from low power on start bit */
  1114. val |= USART_CR3_WUS_START_BIT | USART_CR3_WUFIE;
  1115. writel_relaxed(val, port->membase + ofs->cr3);
  1116. stm32_set_bits(port, ofs->cr1, BIT(cfg->uart_enable_bit));
  1117. } else {
  1118. stm32_clr_bits(port, ofs->cr1, USART_CR1_UESM);
  1119. }
  1120. }
  1121. static int __maybe_unused stm32_serial_suspend(struct device *dev)
  1122. {
  1123. struct uart_port *port = dev_get_drvdata(dev);
  1124. uart_suspend_port(&stm32_usart_driver, port);
  1125. if (device_may_wakeup(dev))
  1126. stm32_serial_enable_wakeup(port, true);
  1127. else
  1128. stm32_serial_enable_wakeup(port, false);
  1129. pinctrl_pm_select_sleep_state(dev);
  1130. return 0;
  1131. }
  1132. static int __maybe_unused stm32_serial_resume(struct device *dev)
  1133. {
  1134. struct uart_port *port = dev_get_drvdata(dev);
  1135. pinctrl_pm_select_default_state(dev);
  1136. if (device_may_wakeup(dev))
  1137. stm32_serial_enable_wakeup(port, false);
  1138. return uart_resume_port(&stm32_usart_driver, port);
  1139. }
  1140. static int __maybe_unused stm32_serial_runtime_suspend(struct device *dev)
  1141. {
  1142. struct uart_port *port = dev_get_drvdata(dev);
  1143. struct stm32_port *stm32port = container_of(port,
  1144. struct stm32_port, port);
  1145. clk_disable_unprepare(stm32port->clk);
  1146. return 0;
  1147. }
  1148. static int __maybe_unused stm32_serial_runtime_resume(struct device *dev)
  1149. {
  1150. struct uart_port *port = dev_get_drvdata(dev);
  1151. struct stm32_port *stm32port = container_of(port,
  1152. struct stm32_port, port);
  1153. return clk_prepare_enable(stm32port->clk);
  1154. }
  1155. static const struct dev_pm_ops stm32_serial_pm_ops = {
  1156. SET_RUNTIME_PM_OPS(stm32_serial_runtime_suspend,
  1157. stm32_serial_runtime_resume, NULL)
  1158. SET_SYSTEM_SLEEP_PM_OPS(stm32_serial_suspend, stm32_serial_resume)
  1159. };
  1160. static struct platform_driver stm32_serial_driver = {
  1161. .probe = stm32_serial_probe,
  1162. .remove = stm32_serial_remove,
  1163. .driver = {
  1164. .name = DRIVER_NAME,
  1165. .pm = &stm32_serial_pm_ops,
  1166. .of_match_table = of_match_ptr(stm32_match),
  1167. },
  1168. };
  1169. static int __init usart_init(void)
  1170. {
  1171. static char banner[] __initdata = "STM32 USART driver initialized";
  1172. int ret;
  1173. pr_info("%s\n", banner);
  1174. ret = uart_register_driver(&stm32_usart_driver);
  1175. if (ret)
  1176. return ret;
  1177. ret = platform_driver_register(&stm32_serial_driver);
  1178. if (ret)
  1179. uart_unregister_driver(&stm32_usart_driver);
  1180. return ret;
  1181. }
  1182. static void __exit usart_exit(void)
  1183. {
  1184. platform_driver_unregister(&stm32_serial_driver);
  1185. uart_unregister_driver(&stm32_usart_driver);
  1186. }
  1187. module_init(usart_init);
  1188. module_exit(usart_exit);
  1189. MODULE_ALIAS("platform:" DRIVER_NAME);
  1190. MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
  1191. MODULE_LICENSE("GPL v2");