stm32-usart.c 26 KB

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