stm32-usart.c 32 KB

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