stm32-usart.c 34 KB

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