stm32-usart.c 29 KB

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