stm32-usart.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * Copyright (C) Maxime Coquelin 2015
  3. * Author: Maxime Coquelin <mcoquelin.stm32@gmail.com>
  4. * License terms: GNU General Public License (GPL), version 2
  5. *
  6. * Inspired by st-asc.c from STMicroelectronics (c)
  7. */
  8. #if defined(CONFIG_SERIAL_STM32_USART_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
  9. #define SUPPORT_SYSRQ
  10. #endif
  11. #include <linux/module.h>
  12. #include <linux/serial.h>
  13. #include <linux/console.h>
  14. #include <linux/sysrq.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/io.h>
  17. #include <linux/irq.h>
  18. #include <linux/tty.h>
  19. #include <linux/tty_flip.h>
  20. #include <linux/delay.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/pm_runtime.h>
  23. #include <linux/of.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/serial_core.h>
  26. #include <linux/clk.h>
  27. #define DRIVER_NAME "stm32-usart"
  28. /* Register offsets */
  29. #define USART_SR 0x00
  30. #define USART_DR 0x04
  31. #define USART_BRR 0x08
  32. #define USART_CR1 0x0c
  33. #define USART_CR2 0x10
  34. #define USART_CR3 0x14
  35. #define USART_GTPR 0x18
  36. /* USART_SR */
  37. #define USART_SR_PE BIT(0)
  38. #define USART_SR_FE BIT(1)
  39. #define USART_SR_NF BIT(2)
  40. #define USART_SR_ORE BIT(3)
  41. #define USART_SR_IDLE BIT(4)
  42. #define USART_SR_RXNE BIT(5)
  43. #define USART_SR_TC BIT(6)
  44. #define USART_SR_TXE BIT(7)
  45. #define USART_SR_LBD BIT(8)
  46. #define USART_SR_CTS BIT(9)
  47. #define USART_SR_ERR_MASK (USART_SR_LBD | USART_SR_ORE | \
  48. USART_SR_FE | USART_SR_PE)
  49. /* Dummy bits */
  50. #define USART_SR_DUMMY_RX BIT(16)
  51. /* USART_DR */
  52. #define USART_DR_MASK GENMASK(8, 0)
  53. /* USART_BRR */
  54. #define USART_BRR_DIV_F_MASK GENMASK(3, 0)
  55. #define USART_BRR_DIV_M_MASK GENMASK(15, 4)
  56. #define USART_BRR_DIV_M_SHIFT 4
  57. /* USART_CR1 */
  58. #define USART_CR1_SBK BIT(0)
  59. #define USART_CR1_RWU BIT(1)
  60. #define USART_CR1_RE BIT(2)
  61. #define USART_CR1_TE BIT(3)
  62. #define USART_CR1_IDLEIE BIT(4)
  63. #define USART_CR1_RXNEIE BIT(5)
  64. #define USART_CR1_TCIE BIT(6)
  65. #define USART_CR1_TXEIE BIT(7)
  66. #define USART_CR1_PEIE BIT(8)
  67. #define USART_CR1_PS BIT(9)
  68. #define USART_CR1_PCE BIT(10)
  69. #define USART_CR1_WAKE BIT(11)
  70. #define USART_CR1_M BIT(12)
  71. #define USART_CR1_UE BIT(13)
  72. #define USART_CR1_OVER8 BIT(15)
  73. #define USART_CR1_IE_MASK GENMASK(8, 4)
  74. /* USART_CR2 */
  75. #define USART_CR2_ADD_MASK GENMASK(3, 0)
  76. #define USART_CR2_LBDL BIT(5)
  77. #define USART_CR2_LBDIE BIT(6)
  78. #define USART_CR2_LBCL BIT(8)
  79. #define USART_CR2_CPHA BIT(9)
  80. #define USART_CR2_CPOL BIT(10)
  81. #define USART_CR2_CLKEN BIT(11)
  82. #define USART_CR2_STOP_2B BIT(13)
  83. #define USART_CR2_STOP_MASK GENMASK(13, 12)
  84. #define USART_CR2_LINEN BIT(14)
  85. /* USART_CR3 */
  86. #define USART_CR3_EIE BIT(0)
  87. #define USART_CR3_IREN BIT(1)
  88. #define USART_CR3_IRLP BIT(2)
  89. #define USART_CR3_HDSEL BIT(3)
  90. #define USART_CR3_NACK BIT(4)
  91. #define USART_CR3_SCEN BIT(5)
  92. #define USART_CR3_DMAR BIT(6)
  93. #define USART_CR3_DMAT BIT(7)
  94. #define USART_CR3_RTSE BIT(8)
  95. #define USART_CR3_CTSE BIT(9)
  96. #define USART_CR3_CTSIE BIT(10)
  97. #define USART_CR3_ONEBIT BIT(11)
  98. /* USART_GTPR */
  99. #define USART_GTPR_PSC_MASK GENMASK(7, 0)
  100. #define USART_GTPR_GT_MASK GENMASK(15, 8)
  101. #define DRIVER_NAME "stm32-usart"
  102. #define STM32_SERIAL_NAME "ttyS"
  103. #define STM32_MAX_PORTS 6
  104. struct stm32_port {
  105. struct uart_port port;
  106. struct clk *clk;
  107. bool hw_flow_control;
  108. };
  109. static struct stm32_port stm32_ports[STM32_MAX_PORTS];
  110. static struct uart_driver stm32_usart_driver;
  111. static void stm32_stop_tx(struct uart_port *port);
  112. static inline struct stm32_port *to_stm32_port(struct uart_port *port)
  113. {
  114. return container_of(port, struct stm32_port, port);
  115. }
  116. static void stm32_set_bits(struct uart_port *port, u32 reg, u32 bits)
  117. {
  118. u32 val;
  119. val = readl_relaxed(port->membase + reg);
  120. val |= bits;
  121. writel_relaxed(val, port->membase + reg);
  122. }
  123. static void stm32_clr_bits(struct uart_port *port, u32 reg, u32 bits)
  124. {
  125. u32 val;
  126. val = readl_relaxed(port->membase + reg);
  127. val &= ~bits;
  128. writel_relaxed(val, port->membase + reg);
  129. }
  130. static void stm32_receive_chars(struct uart_port *port)
  131. {
  132. struct tty_port *tport = &port->state->port;
  133. unsigned long c;
  134. u32 sr;
  135. char flag;
  136. if (port->irq_wake)
  137. pm_wakeup_event(tport->tty->dev, 0);
  138. while ((sr = readl_relaxed(port->membase + USART_SR)) & USART_SR_RXNE) {
  139. sr |= USART_SR_DUMMY_RX;
  140. c = readl_relaxed(port->membase + USART_DR);
  141. flag = TTY_NORMAL;
  142. port->icount.rx++;
  143. if (sr & USART_SR_ERR_MASK) {
  144. if (sr & USART_SR_LBD) {
  145. port->icount.brk++;
  146. if (uart_handle_break(port))
  147. continue;
  148. } else if (sr & USART_SR_ORE) {
  149. port->icount.overrun++;
  150. } else if (sr & USART_SR_PE) {
  151. port->icount.parity++;
  152. } else if (sr & USART_SR_FE) {
  153. port->icount.frame++;
  154. }
  155. sr &= port->read_status_mask;
  156. if (sr & USART_SR_LBD)
  157. flag = TTY_BREAK;
  158. else if (sr & USART_SR_PE)
  159. flag = TTY_PARITY;
  160. else if (sr & USART_SR_FE)
  161. flag = TTY_FRAME;
  162. }
  163. if (uart_handle_sysrq_char(port, c))
  164. continue;
  165. uart_insert_char(port, sr, USART_SR_ORE, c, flag);
  166. }
  167. spin_unlock(&port->lock);
  168. tty_flip_buffer_push(tport);
  169. spin_lock(&port->lock);
  170. }
  171. static void stm32_transmit_chars(struct uart_port *port)
  172. {
  173. struct circ_buf *xmit = &port->state->xmit;
  174. if (port->x_char) {
  175. writel_relaxed(port->x_char, port->membase + USART_DR);
  176. port->x_char = 0;
  177. port->icount.tx++;
  178. return;
  179. }
  180. if (uart_tx_stopped(port)) {
  181. stm32_stop_tx(port);
  182. return;
  183. }
  184. if (uart_circ_empty(xmit)) {
  185. stm32_stop_tx(port);
  186. return;
  187. }
  188. writel_relaxed(xmit->buf[xmit->tail], port->membase + USART_DR);
  189. xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
  190. port->icount.tx++;
  191. if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
  192. uart_write_wakeup(port);
  193. if (uart_circ_empty(xmit))
  194. stm32_stop_tx(port);
  195. }
  196. static irqreturn_t stm32_interrupt(int irq, void *ptr)
  197. {
  198. struct uart_port *port = ptr;
  199. u32 sr;
  200. spin_lock(&port->lock);
  201. sr = readl_relaxed(port->membase + USART_SR);
  202. if (sr & USART_SR_RXNE)
  203. stm32_receive_chars(port);
  204. if (sr & USART_SR_TXE)
  205. stm32_transmit_chars(port);
  206. spin_unlock(&port->lock);
  207. return IRQ_HANDLED;
  208. }
  209. static unsigned int stm32_tx_empty(struct uart_port *port)
  210. {
  211. return readl_relaxed(port->membase + USART_SR) & USART_SR_TXE;
  212. }
  213. static void stm32_set_mctrl(struct uart_port *port, unsigned int mctrl)
  214. {
  215. if ((mctrl & TIOCM_RTS) && (port->status & UPSTAT_AUTORTS))
  216. stm32_set_bits(port, USART_CR3, USART_CR3_RTSE);
  217. else
  218. stm32_clr_bits(port, USART_CR3, USART_CR3_RTSE);
  219. }
  220. static unsigned int stm32_get_mctrl(struct uart_port *port)
  221. {
  222. /* This routine is used to get signals of: DCD, DSR, RI, and CTS */
  223. return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
  224. }
  225. /* Transmit stop */
  226. static void stm32_stop_tx(struct uart_port *port)
  227. {
  228. stm32_clr_bits(port, USART_CR1, USART_CR1_TXEIE);
  229. }
  230. /* There are probably characters waiting to be transmitted. */
  231. static void stm32_start_tx(struct uart_port *port)
  232. {
  233. struct circ_buf *xmit = &port->state->xmit;
  234. if (uart_circ_empty(xmit))
  235. return;
  236. stm32_set_bits(port, USART_CR1, USART_CR1_TXEIE | USART_CR1_TE);
  237. }
  238. /* Throttle the remote when input buffer is about to overflow. */
  239. static void stm32_throttle(struct uart_port *port)
  240. {
  241. unsigned long flags;
  242. spin_lock_irqsave(&port->lock, flags);
  243. stm32_clr_bits(port, USART_CR1, USART_CR1_RXNEIE);
  244. spin_unlock_irqrestore(&port->lock, flags);
  245. }
  246. /* Unthrottle the remote, the input buffer can now accept data. */
  247. static void stm32_unthrottle(struct uart_port *port)
  248. {
  249. unsigned long flags;
  250. spin_lock_irqsave(&port->lock, flags);
  251. stm32_set_bits(port, USART_CR1, USART_CR1_RXNEIE);
  252. spin_unlock_irqrestore(&port->lock, flags);
  253. }
  254. /* Receive stop */
  255. static void stm32_stop_rx(struct uart_port *port)
  256. {
  257. stm32_clr_bits(port, USART_CR1, USART_CR1_RXNEIE);
  258. }
  259. /* Handle breaks - ignored by us */
  260. static void stm32_break_ctl(struct uart_port *port, int break_state)
  261. {
  262. }
  263. static int stm32_startup(struct uart_port *port)
  264. {
  265. const char *name = to_platform_device(port->dev)->name;
  266. u32 val;
  267. int ret;
  268. ret = request_irq(port->irq, stm32_interrupt, IRQF_NO_SUSPEND,
  269. name, port);
  270. if (ret)
  271. return ret;
  272. val = USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
  273. stm32_set_bits(port, USART_CR1, val);
  274. return 0;
  275. }
  276. static void stm32_shutdown(struct uart_port *port)
  277. {
  278. u32 val;
  279. val = USART_CR1_TXEIE | USART_CR1_RXNEIE | USART_CR1_TE | USART_CR1_RE;
  280. stm32_set_bits(port, USART_CR1, val);
  281. free_irq(port->irq, port);
  282. }
  283. static void stm32_set_termios(struct uart_port *port, struct ktermios *termios,
  284. struct ktermios *old)
  285. {
  286. struct stm32_port *stm32_port = to_stm32_port(port);
  287. unsigned int baud;
  288. u32 usartdiv, mantissa, fraction, oversampling;
  289. tcflag_t cflag = termios->c_cflag;
  290. u32 cr1, cr2, cr3;
  291. unsigned long flags;
  292. if (!stm32_port->hw_flow_control)
  293. cflag &= ~CRTSCTS;
  294. baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 8);
  295. spin_lock_irqsave(&port->lock, flags);
  296. /* Stop serial port and reset value */
  297. writel_relaxed(0, port->membase + USART_CR1);
  298. cr1 = USART_CR1_TE | USART_CR1_RE | USART_CR1_UE | USART_CR1_RXNEIE;
  299. cr2 = 0;
  300. cr3 = 0;
  301. if (cflag & CSTOPB)
  302. cr2 |= USART_CR2_STOP_2B;
  303. if (cflag & PARENB) {
  304. cr1 |= USART_CR1_PCE;
  305. if ((cflag & CSIZE) == CS8)
  306. cr1 |= USART_CR1_M;
  307. }
  308. if (cflag & PARODD)
  309. cr1 |= USART_CR1_PS;
  310. port->status &= ~(UPSTAT_AUTOCTS | UPSTAT_AUTORTS);
  311. if (cflag & CRTSCTS) {
  312. port->status |= UPSTAT_AUTOCTS | UPSTAT_AUTORTS;
  313. cr3 |= USART_CR3_CTSE;
  314. }
  315. usartdiv = DIV_ROUND_CLOSEST(port->uartclk, baud);
  316. /*
  317. * The USART supports 16 or 8 times oversampling.
  318. * By default we prefer 16 times oversampling, so that the receiver
  319. * has a better tolerance to clock deviations.
  320. * 8 times oversampling is only used to achieve higher speeds.
  321. */
  322. if (usartdiv < 16) {
  323. oversampling = 8;
  324. stm32_set_bits(port, USART_CR1, USART_CR1_OVER8);
  325. } else {
  326. oversampling = 16;
  327. stm32_clr_bits(port, USART_CR1, USART_CR1_OVER8);
  328. }
  329. mantissa = (usartdiv / oversampling) << USART_BRR_DIV_M_SHIFT;
  330. fraction = usartdiv % oversampling;
  331. writel_relaxed(mantissa | fraction, port->membase + USART_BRR);
  332. uart_update_timeout(port, cflag, baud);
  333. port->read_status_mask = USART_SR_ORE;
  334. if (termios->c_iflag & INPCK)
  335. port->read_status_mask |= USART_SR_PE | USART_SR_FE;
  336. if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
  337. port->read_status_mask |= USART_SR_LBD;
  338. /* Characters to ignore */
  339. port->ignore_status_mask = 0;
  340. if (termios->c_iflag & IGNPAR)
  341. port->ignore_status_mask = USART_SR_PE | USART_SR_FE;
  342. if (termios->c_iflag & IGNBRK) {
  343. port->ignore_status_mask |= USART_SR_LBD;
  344. /*
  345. * If we're ignoring parity and break indicators,
  346. * ignore overruns too (for real raw support).
  347. */
  348. if (termios->c_iflag & IGNPAR)
  349. port->ignore_status_mask |= USART_SR_ORE;
  350. }
  351. /* Ignore all characters if CREAD is not set */
  352. if ((termios->c_cflag & CREAD) == 0)
  353. port->ignore_status_mask |= USART_SR_DUMMY_RX;
  354. writel_relaxed(cr3, port->membase + USART_CR3);
  355. writel_relaxed(cr2, port->membase + USART_CR2);
  356. writel_relaxed(cr1, port->membase + USART_CR1);
  357. spin_unlock_irqrestore(&port->lock, flags);
  358. }
  359. static const char *stm32_type(struct uart_port *port)
  360. {
  361. return (port->type == PORT_STM32) ? DRIVER_NAME : NULL;
  362. }
  363. static void stm32_release_port(struct uart_port *port)
  364. {
  365. }
  366. static int stm32_request_port(struct uart_port *port)
  367. {
  368. return 0;
  369. }
  370. static void stm32_config_port(struct uart_port *port, int flags)
  371. {
  372. if (flags & UART_CONFIG_TYPE)
  373. port->type = PORT_STM32;
  374. }
  375. static int
  376. stm32_verify_port(struct uart_port *port, struct serial_struct *ser)
  377. {
  378. /* No user changeable parameters */
  379. return -EINVAL;
  380. }
  381. static void stm32_pm(struct uart_port *port, unsigned int state,
  382. unsigned int oldstate)
  383. {
  384. struct stm32_port *stm32port = container_of(port,
  385. struct stm32_port, port);
  386. unsigned long flags = 0;
  387. switch (state) {
  388. case UART_PM_STATE_ON:
  389. clk_prepare_enable(stm32port->clk);
  390. break;
  391. case UART_PM_STATE_OFF:
  392. spin_lock_irqsave(&port->lock, flags);
  393. stm32_clr_bits(port, USART_CR1, USART_CR1_UE);
  394. spin_unlock_irqrestore(&port->lock, flags);
  395. clk_disable_unprepare(stm32port->clk);
  396. break;
  397. }
  398. }
  399. static const struct uart_ops stm32_uart_ops = {
  400. .tx_empty = stm32_tx_empty,
  401. .set_mctrl = stm32_set_mctrl,
  402. .get_mctrl = stm32_get_mctrl,
  403. .stop_tx = stm32_stop_tx,
  404. .start_tx = stm32_start_tx,
  405. .throttle = stm32_throttle,
  406. .unthrottle = stm32_unthrottle,
  407. .stop_rx = stm32_stop_rx,
  408. .break_ctl = stm32_break_ctl,
  409. .startup = stm32_startup,
  410. .shutdown = stm32_shutdown,
  411. .set_termios = stm32_set_termios,
  412. .pm = stm32_pm,
  413. .type = stm32_type,
  414. .release_port = stm32_release_port,
  415. .request_port = stm32_request_port,
  416. .config_port = stm32_config_port,
  417. .verify_port = stm32_verify_port,
  418. };
  419. static int stm32_init_port(struct stm32_port *stm32port,
  420. struct platform_device *pdev)
  421. {
  422. struct uart_port *port = &stm32port->port;
  423. struct resource *res;
  424. int ret;
  425. port->iotype = UPIO_MEM;
  426. port->flags = UPF_BOOT_AUTOCONF;
  427. port->ops = &stm32_uart_ops;
  428. port->dev = &pdev->dev;
  429. port->irq = platform_get_irq(pdev, 0);
  430. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  431. port->membase = devm_ioremap_resource(&pdev->dev, res);
  432. if (IS_ERR(port->membase))
  433. return PTR_ERR(port->membase);
  434. port->mapbase = res->start;
  435. spin_lock_init(&port->lock);
  436. stm32port->clk = devm_clk_get(&pdev->dev, NULL);
  437. if (IS_ERR(stm32port->clk))
  438. return PTR_ERR(stm32port->clk);
  439. /* Ensure that clk rate is correct by enabling the clk */
  440. ret = clk_prepare_enable(stm32port->clk);
  441. if (ret)
  442. return ret;
  443. stm32port->port.uartclk = clk_get_rate(stm32port->clk);
  444. if (!stm32port->port.uartclk)
  445. ret = -EINVAL;
  446. clk_disable_unprepare(stm32port->clk);
  447. return ret;
  448. }
  449. static struct stm32_port *stm32_of_get_stm32_port(struct platform_device *pdev)
  450. {
  451. struct device_node *np = pdev->dev.of_node;
  452. int id;
  453. if (!np)
  454. return NULL;
  455. id = of_alias_get_id(np, "serial");
  456. if (id < 0)
  457. id = 0;
  458. if (WARN_ON(id >= STM32_MAX_PORTS))
  459. return NULL;
  460. stm32_ports[id].hw_flow_control = of_property_read_bool(np,
  461. "auto-flow-control");
  462. stm32_ports[id].port.line = id;
  463. return &stm32_ports[id];
  464. }
  465. #ifdef CONFIG_OF
  466. static const struct of_device_id stm32_match[] = {
  467. { .compatible = "st,stm32-usart", },
  468. { .compatible = "st,stm32-uart", },
  469. {},
  470. };
  471. MODULE_DEVICE_TABLE(of, stm32_match);
  472. #endif
  473. static int stm32_serial_probe(struct platform_device *pdev)
  474. {
  475. int ret;
  476. struct stm32_port *stm32port;
  477. stm32port = stm32_of_get_stm32_port(pdev);
  478. if (!stm32port)
  479. return -ENODEV;
  480. ret = stm32_init_port(stm32port, pdev);
  481. if (ret)
  482. return ret;
  483. ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port);
  484. if (ret)
  485. return ret;
  486. platform_set_drvdata(pdev, &stm32port->port);
  487. return 0;
  488. }
  489. static int stm32_serial_remove(struct platform_device *pdev)
  490. {
  491. struct uart_port *port = platform_get_drvdata(pdev);
  492. return uart_remove_one_port(&stm32_usart_driver, port);
  493. }
  494. #ifdef CONFIG_SERIAL_STM32_CONSOLE
  495. static void stm32_console_putchar(struct uart_port *port, int ch)
  496. {
  497. while (!(readl_relaxed(port->membase + USART_SR) & USART_SR_TXE))
  498. cpu_relax();
  499. writel_relaxed(ch, port->membase + USART_DR);
  500. }
  501. static void stm32_console_write(struct console *co, const char *s, unsigned cnt)
  502. {
  503. struct uart_port *port = &stm32_ports[co->index].port;
  504. unsigned long flags;
  505. u32 old_cr1, new_cr1;
  506. int locked = 1;
  507. local_irq_save(flags);
  508. if (port->sysrq)
  509. locked = 0;
  510. else if (oops_in_progress)
  511. locked = spin_trylock(&port->lock);
  512. else
  513. spin_lock(&port->lock);
  514. /* Save and disable interrupts */
  515. old_cr1 = readl_relaxed(port->membase + USART_CR1);
  516. new_cr1 = old_cr1 & ~USART_CR1_IE_MASK;
  517. writel_relaxed(new_cr1, port->membase + USART_CR1);
  518. uart_console_write(port, s, cnt, stm32_console_putchar);
  519. /* Restore interrupt state */
  520. writel_relaxed(old_cr1, port->membase + USART_CR1);
  521. if (locked)
  522. spin_unlock(&port->lock);
  523. local_irq_restore(flags);
  524. }
  525. static int stm32_console_setup(struct console *co, char *options)
  526. {
  527. struct stm32_port *stm32port;
  528. int baud = 9600;
  529. int bits = 8;
  530. int parity = 'n';
  531. int flow = 'n';
  532. if (co->index >= STM32_MAX_PORTS)
  533. return -ENODEV;
  534. stm32port = &stm32_ports[co->index];
  535. /*
  536. * This driver does not support early console initialization
  537. * (use ARM early printk support instead), so we only expect
  538. * this to be called during the uart port registration when the
  539. * driver gets probed and the port should be mapped at that point.
  540. */
  541. if (stm32port->port.mapbase == 0 || stm32port->port.membase == NULL)
  542. return -ENXIO;
  543. if (options)
  544. uart_parse_options(options, &baud, &parity, &bits, &flow);
  545. return uart_set_options(&stm32port->port, co, baud, parity, bits, flow);
  546. }
  547. static struct console stm32_console = {
  548. .name = STM32_SERIAL_NAME,
  549. .device = uart_console_device,
  550. .write = stm32_console_write,
  551. .setup = stm32_console_setup,
  552. .flags = CON_PRINTBUFFER,
  553. .index = -1,
  554. .data = &stm32_usart_driver,
  555. };
  556. #define STM32_SERIAL_CONSOLE (&stm32_console)
  557. #else
  558. #define STM32_SERIAL_CONSOLE NULL
  559. #endif /* CONFIG_SERIAL_STM32_CONSOLE */
  560. static struct uart_driver stm32_usart_driver = {
  561. .driver_name = DRIVER_NAME,
  562. .dev_name = STM32_SERIAL_NAME,
  563. .major = 0,
  564. .minor = 0,
  565. .nr = STM32_MAX_PORTS,
  566. .cons = STM32_SERIAL_CONSOLE,
  567. };
  568. static struct platform_driver stm32_serial_driver = {
  569. .probe = stm32_serial_probe,
  570. .remove = stm32_serial_remove,
  571. .driver = {
  572. .name = DRIVER_NAME,
  573. .of_match_table = of_match_ptr(stm32_match),
  574. },
  575. };
  576. static int __init usart_init(void)
  577. {
  578. static char banner[] __initdata = "STM32 USART driver initialized";
  579. int ret;
  580. pr_info("%s\n", banner);
  581. ret = uart_register_driver(&stm32_usart_driver);
  582. if (ret)
  583. return ret;
  584. ret = platform_driver_register(&stm32_serial_driver);
  585. if (ret)
  586. uart_unregister_driver(&stm32_usart_driver);
  587. return ret;
  588. }
  589. static void __exit usart_exit(void)
  590. {
  591. platform_driver_unregister(&stm32_serial_driver);
  592. uart_unregister_driver(&stm32_usart_driver);
  593. }
  594. module_init(usart_init);
  595. module_exit(usart_exit);
  596. MODULE_ALIAS("platform:" DRIVER_NAME);
  597. MODULE_DESCRIPTION("STMicroelectronics STM32 serial port driver");
  598. MODULE_LICENSE("GPL v2");