1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
6 #include <linux/console.h>
7 #include <linux/delay.h>
10 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/serial_core.h>
14 #include <linux/slab.h>
15 #include <linux/tty_flip.h>
16 #include <asm/serial.h>
18 #define DRIVER_NAME "esp32-uart"
19 #define DEV_NAME "ttyS"
22 #define ESP32_UART_TX_FIFO_SIZE 127
23 #define ESP32_UART_RX_FIFO_SIZE 127
25 #define UART_FIFO_REG 0x00
26 #define UART_INT_RAW_REG 0x04
27 #define UART_INT_ST_REG 0x08
28 #define UART_INT_ENA_REG 0x0c
29 #define UART_INT_CLR_REG 0x10
30 #define UART_RXFIFO_FULL_INT BIT(0)
31 #define UART_TXFIFO_EMPTY_INT BIT(1)
32 #define UART_BRK_DET_INT BIT(7)
33 #define UART_CLKDIV_REG 0x14
34 #define ESP32_UART_CLKDIV GENMASK(19, 0)
35 #define ESP32S3_UART_CLKDIV GENMASK(11, 0)
36 #define UART_CLKDIV_SHIFT 0
37 #define UART_CLKDIV_FRAG GENMASK(23, 20)
38 #define UART_STATUS_REG 0x1c
39 #define ESP32_UART_RXFIFO_CNT GENMASK(7, 0)
40 #define ESP32S3_UART_RXFIFO_CNT GENMASK(9, 0)
41 #define UART_RXFIFO_CNT_SHIFT 0
42 #define UART_DSRN BIT(13)
43 #define UART_CTSN BIT(14)
44 #define ESP32_UART_TXFIFO_CNT GENMASK(23, 16)
45 #define ESP32S3_UART_TXFIFO_CNT GENMASK(25, 16)
46 #define UART_TXFIFO_CNT_SHIFT 16
47 #define UART_CONF0_REG 0x20
48 #define UART_PARITY BIT(0)
49 #define UART_PARITY_EN BIT(1)
50 #define UART_BIT_NUM GENMASK(3, 2)
51 #define UART_BIT_NUM_5 0
52 #define UART_BIT_NUM_6 1
53 #define UART_BIT_NUM_7 2
54 #define UART_BIT_NUM_8 3
55 #define UART_STOP_BIT_NUM GENMASK(5, 4)
56 #define UART_STOP_BIT_NUM_1 1
57 #define UART_STOP_BIT_NUM_2 3
58 #define UART_SW_RTS BIT(6)
59 #define UART_SW_DTR BIT(7)
60 #define UART_LOOPBACK BIT(14)
61 #define UART_TX_FLOW_EN BIT(15)
62 #define UART_RTS_INV BIT(23)
63 #define UART_DTR_INV BIT(24)
64 #define UART_CONF1_REG 0x24
65 #define UART_RXFIFO_FULL_THRHD_SHIFT 0
66 #define ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT 8
67 #define ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT 10
68 #define ESP32_UART_RX_FLOW_EN BIT(23)
69 #define ESP32S3_UART_RX_FLOW_EN BIT(22)
70 #define ESP32S3_UART_CLK_CONF_REG 0x78
71 #define ESP32S3_UART_SCLK_DIV_B GENMASK(5, 0)
72 #define ESP32S3_UART_SCLK_DIV_A GENMASK(11, 6)
73 #define ESP32S3_UART_SCLK_DIV_NUM GENMASK(19, 12)
74 #define ESP32S3_UART_SCLK_SEL GENMASK(21, 20)
78 #define ESP32S3_UART_SCLK_EN BIT(22)
79 #define ESP32S3_UART_RST_CORE BIT(23)
80 #define ESP32S3_UART_TX_SCLK_EN BIT(24)
81 #define ESP32S3_UART_RX_SCLK_EN BIT(25)
82 #define ESP32S3_UART_TX_RST_CORE BIT(26)
83 #define ESP32S3_UART_RX_RST_CORE BIT(27)
85 #define ESP32S3_UART_CLK_CONF_DEFAULT \
86 (ESP32S3_UART_RX_SCLK_EN | \
87 ESP32S3_UART_TX_SCLK_EN | \
88 ESP32S3_UART_SCLK_EN | \
89 FIELD_PREP(ESP32S3_UART_SCLK_SEL, XTAL_CLK))
92 struct uart_port port;
96 struct esp32_uart_variant {
100 u32 txfifo_empty_thrhd_shift;
106 static const struct esp32_uart_variant esp32_variant = {
107 .clkdiv_mask = ESP32_UART_CLKDIV,
108 .rxfifo_cnt_mask = ESP32_UART_RXFIFO_CNT,
109 .txfifo_cnt_mask = ESP32_UART_TXFIFO_CNT,
110 .txfifo_empty_thrhd_shift = ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT,
111 .rx_flow_en = ESP32_UART_RX_FLOW_EN,
112 .type = "ESP32 UART",
115 static const struct esp32_uart_variant esp32s3_variant = {
116 .clkdiv_mask = ESP32S3_UART_CLKDIV,
117 .rxfifo_cnt_mask = ESP32S3_UART_RXFIFO_CNT,
118 .txfifo_cnt_mask = ESP32S3_UART_TXFIFO_CNT,
119 .txfifo_empty_thrhd_shift = ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT,
120 .rx_flow_en = ESP32S3_UART_RX_FLOW_EN,
121 .type = "ESP32S3 UART",
125 static const struct of_device_id esp32_uart_dt_ids[] = {
127 .compatible = "esp,esp32-uart",
128 .data = &esp32_variant,
130 .compatible = "esp,esp32s3-uart",
131 .data = &esp32s3_variant,
132 }, { /* sentinel */ }
134 MODULE_DEVICE_TABLE(of, esp32_uart_dt_ids);
136 static struct esp32_port *esp32_uart_ports[UART_NR];
138 static const struct esp32_uart_variant *port_variant(struct uart_port *port)
140 return port->private_data;
143 static void esp32_uart_write(struct uart_port *port, unsigned long reg, u32 v)
145 writel(v, port->membase + reg);
148 static u32 esp32_uart_read(struct uart_port *port, unsigned long reg)
150 return readl(port->membase + reg);
153 static u32 esp32_uart_tx_fifo_cnt(struct uart_port *port)
155 u32 status = esp32_uart_read(port, UART_STATUS_REG);
157 return (status & port_variant(port)->txfifo_cnt_mask) >> UART_TXFIFO_CNT_SHIFT;
160 static u32 esp32_uart_rx_fifo_cnt(struct uart_port *port)
162 u32 status = esp32_uart_read(port, UART_STATUS_REG);
164 return (status & port_variant(port)->rxfifo_cnt_mask) >> UART_RXFIFO_CNT_SHIFT;
167 /* return TIOCSER_TEMT when transmitter is not busy */
168 static unsigned int esp32_uart_tx_empty(struct uart_port *port)
170 return esp32_uart_tx_fifo_cnt(port) ? 0 : TIOCSER_TEMT;
173 static void esp32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
175 u32 conf0 = esp32_uart_read(port, UART_CONF0_REG);
177 conf0 &= ~(UART_LOOPBACK |
178 UART_SW_RTS | UART_RTS_INV |
179 UART_SW_DTR | UART_DTR_INV);
181 if (mctrl & TIOCM_RTS)
182 conf0 |= UART_SW_RTS;
183 if (mctrl & TIOCM_DTR)
184 conf0 |= UART_SW_DTR;
185 if (mctrl & TIOCM_LOOP)
186 conf0 |= UART_LOOPBACK;
188 esp32_uart_write(port, UART_CONF0_REG, conf0);
191 static unsigned int esp32_uart_get_mctrl(struct uart_port *port)
193 u32 status = esp32_uart_read(port, UART_STATUS_REG);
194 unsigned int ret = TIOCM_CAR;
196 if (status & UART_DSRN)
198 if (status & UART_CTSN)
204 static void esp32_uart_stop_tx(struct uart_port *port)
208 int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
209 int_ena &= ~UART_TXFIFO_EMPTY_INT;
210 esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
213 static void esp32_uart_rxint(struct uart_port *port)
215 struct tty_port *tty_port = &port->state->port;
216 u32 rx_fifo_cnt = esp32_uart_rx_fifo_cnt(port);
223 spin_lock_irqsave(&port->lock, flags);
225 for (i = 0; i < rx_fifo_cnt; ++i) {
226 u32 rx = esp32_uart_read(port, UART_FIFO_REG);
229 (esp32_uart_read(port, UART_INT_ST_REG) & UART_BRK_DET_INT)) {
230 esp32_uart_write(port, UART_INT_CLR_REG, UART_BRK_DET_INT);
232 uart_handle_break(port);
234 if (uart_handle_sysrq_char(port, (unsigned char)rx))
236 tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
240 spin_unlock_irqrestore(&port->lock, flags);
242 tty_flip_buffer_push(tty_port);
245 static void esp32_uart_put_char(struct uart_port *port, u8 c)
247 esp32_uart_write(port, UART_FIFO_REG, c);
250 static void esp32_uart_put_char_sync(struct uart_port *port, u8 c)
252 unsigned long timeout = jiffies + HZ;
254 while (esp32_uart_tx_fifo_cnt(port) >= ESP32_UART_TX_FIFO_SIZE) {
255 if (time_after(jiffies, timeout)) {
256 dev_warn(port->dev, "timeout waiting for TX FIFO\n");
261 esp32_uart_put_char(port, c);
264 static void esp32_uart_transmit_buffer(struct uart_port *port)
266 u32 tx_fifo_used = esp32_uart_tx_fifo_cnt(port);
267 unsigned int pending;
270 if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
273 pending = uart_port_tx_limited(port, ch,
274 ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
275 true, esp32_uart_put_char(port, ch),
280 int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
281 int_ena |= UART_TXFIFO_EMPTY_INT;
282 esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
286 static void esp32_uart_txint(struct uart_port *port)
288 esp32_uart_transmit_buffer(port);
291 static irqreturn_t esp32_uart_int(int irq, void *dev_id)
293 struct uart_port *port = dev_id;
296 status = esp32_uart_read(port, UART_INT_ST_REG);
298 if (status & (UART_RXFIFO_FULL_INT | UART_BRK_DET_INT))
299 esp32_uart_rxint(port);
300 if (status & UART_TXFIFO_EMPTY_INT)
301 esp32_uart_txint(port);
303 esp32_uart_write(port, UART_INT_CLR_REG, status);
305 return IRQ_RETVAL(status);
308 static void esp32_uart_start_tx(struct uart_port *port)
310 esp32_uart_transmit_buffer(port);
313 static void esp32_uart_stop_rx(struct uart_port *port)
317 int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
318 int_ena &= ~UART_RXFIFO_FULL_INT;
319 esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
322 static int esp32_uart_startup(struct uart_port *port)
326 struct esp32_port *sport = container_of(port, struct esp32_port, port);
328 ret = clk_prepare_enable(sport->clk);
332 ret = request_irq(port->irq, esp32_uart_int, 0, DRIVER_NAME, port);
334 clk_disable_unprepare(sport->clk);
338 spin_lock_irqsave(&port->lock, flags);
339 if (port_variant(port)->has_clkconf)
340 esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
341 ESP32S3_UART_CLK_CONF_DEFAULT);
342 esp32_uart_write(port, UART_CONF1_REG,
343 (1 << UART_RXFIFO_FULL_THRHD_SHIFT) |
344 (1 << port_variant(port)->txfifo_empty_thrhd_shift));
345 esp32_uart_write(port, UART_INT_CLR_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
346 esp32_uart_write(port, UART_INT_ENA_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
347 spin_unlock_irqrestore(&port->lock, flags);
352 static void esp32_uart_shutdown(struct uart_port *port)
354 struct esp32_port *sport = container_of(port, struct esp32_port, port);
356 esp32_uart_write(port, UART_INT_ENA_REG, 0);
357 free_irq(port->irq, port);
358 clk_disable_unprepare(sport->clk);
361 static bool esp32_uart_set_baud(struct uart_port *port, u32 baud)
363 u32 sclk = port->uartclk;
364 u32 div = sclk / baud;
366 if (port_variant(port)->has_clkconf) {
367 u32 sclk_div = div / port_variant(port)->clkdiv_mask;
369 if (div > port_variant(port)->clkdiv_mask) {
370 sclk /= (sclk_div + 1);
373 esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
374 FIELD_PREP(ESP32S3_UART_SCLK_DIV_NUM, sclk_div) |
375 ESP32S3_UART_CLK_CONF_DEFAULT);
378 if (div <= port_variant(port)->clkdiv_mask) {
379 u32 frag = (sclk * 16) / baud - div * 16;
381 esp32_uart_write(port, UART_CLKDIV_REG,
382 div | FIELD_PREP(UART_CLKDIV_FRAG, frag));
389 static void esp32_uart_set_termios(struct uart_port *port,
390 struct ktermios *termios,
391 const struct ktermios *old)
396 const u32 rx_flow_en = port_variant(port)->rx_flow_en;
397 u32 max_div = port_variant(port)->clkdiv_mask;
399 termios->c_cflag &= ~CMSPAR;
401 if (port_variant(port)->has_clkconf)
402 max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM);
404 baud = uart_get_baud_rate(port, termios, old,
405 port->uartclk / max_div,
408 spin_lock_irqsave(&port->lock, flags);
410 conf0 = esp32_uart_read(port, UART_CONF0_REG);
411 conf0 &= ~(UART_PARITY_EN | UART_PARITY | UART_BIT_NUM | UART_STOP_BIT_NUM);
413 conf1 = esp32_uart_read(port, UART_CONF1_REG);
414 conf1 &= ~rx_flow_en;
416 if (termios->c_cflag & PARENB) {
417 conf0 |= UART_PARITY_EN;
418 if (termios->c_cflag & PARODD)
419 conf0 |= UART_PARITY;
422 switch (termios->c_cflag & CSIZE) {
424 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_5);
427 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_6);
430 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_7);
433 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_8);
437 if (termios->c_cflag & CSTOPB)
438 conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_2);
440 conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_1);
442 if (termios->c_cflag & CRTSCTS)
445 esp32_uart_write(port, UART_CONF0_REG, conf0);
446 esp32_uart_write(port, UART_CONF1_REG, conf1);
449 esp32_uart_set_baud(port, baud);
450 uart_update_timeout(port, termios->c_cflag, baud);
452 if (esp32_uart_set_baud(port, 115200)) {
454 tty_termios_encode_baud_rate(termios, baud, baud);
455 uart_update_timeout(port, termios->c_cflag, baud);
458 "unable to set speed to %d baud or the default 115200\n",
462 spin_unlock_irqrestore(&port->lock, flags);
465 static const char *esp32_uart_type(struct uart_port *port)
467 return port_variant(port)->type;
470 /* configure/auto-configure the port */
471 static void esp32_uart_config_port(struct uart_port *port, int flags)
473 if (flags & UART_CONFIG_TYPE)
474 port->type = PORT_GENERIC;
477 #ifdef CONFIG_CONSOLE_POLL
478 static int esp32_uart_poll_init(struct uart_port *port)
480 struct esp32_port *sport = container_of(port, struct esp32_port, port);
482 return clk_prepare_enable(sport->clk);
485 static void esp32_uart_poll_put_char(struct uart_port *port, unsigned char c)
487 esp32_uart_put_char_sync(port, c);
490 static int esp32_uart_poll_get_char(struct uart_port *port)
492 if (esp32_uart_rx_fifo_cnt(port))
493 return esp32_uart_read(port, UART_FIFO_REG);
500 static const struct uart_ops esp32_uart_pops = {
501 .tx_empty = esp32_uart_tx_empty,
502 .set_mctrl = esp32_uart_set_mctrl,
503 .get_mctrl = esp32_uart_get_mctrl,
504 .stop_tx = esp32_uart_stop_tx,
505 .start_tx = esp32_uart_start_tx,
506 .stop_rx = esp32_uart_stop_rx,
507 .startup = esp32_uart_startup,
508 .shutdown = esp32_uart_shutdown,
509 .set_termios = esp32_uart_set_termios,
510 .type = esp32_uart_type,
511 .config_port = esp32_uart_config_port,
512 #ifdef CONFIG_CONSOLE_POLL
513 .poll_init = esp32_uart_poll_init,
514 .poll_put_char = esp32_uart_poll_put_char,
515 .poll_get_char = esp32_uart_poll_get_char,
519 static void esp32_uart_console_putchar(struct uart_port *port, u8 c)
521 esp32_uart_put_char_sync(port, c);
524 static void esp32_uart_string_write(struct uart_port *port, const char *s,
527 uart_console_write(port, s, count, esp32_uart_console_putchar);
531 esp32_uart_console_write(struct console *co, const char *s, unsigned int count)
533 struct esp32_port *sport = esp32_uart_ports[co->index];
534 struct uart_port *port = &sport->port;
540 else if (oops_in_progress)
541 locked = spin_trylock_irqsave(&port->lock, flags);
543 spin_lock_irqsave(&port->lock, flags);
545 esp32_uart_string_write(port, s, count);
548 spin_unlock_irqrestore(&port->lock, flags);
551 static int __init esp32_uart_console_setup(struct console *co, char *options)
553 struct esp32_port *sport;
561 * check whether an invalid uart number has been specified, and
562 * if so, search for the first available port that does have
565 if (co->index == -1 || co->index >= ARRAY_SIZE(esp32_uart_ports))
568 sport = esp32_uart_ports[co->index];
572 ret = clk_prepare_enable(sport->clk);
577 uart_parse_options(options, &baud, &parity, &bits, &flow);
579 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
582 static int esp32_uart_console_exit(struct console *co)
584 struct esp32_port *sport = esp32_uart_ports[co->index];
586 clk_disable_unprepare(sport->clk);
590 static struct uart_driver esp32_uart_reg;
591 static struct console esp32_uart_console = {
593 .write = esp32_uart_console_write,
594 .device = uart_console_device,
595 .setup = esp32_uart_console_setup,
596 .exit = esp32_uart_console_exit,
597 .flags = CON_PRINTBUFFER,
599 .data = &esp32_uart_reg,
602 static void esp32_uart_earlycon_putchar(struct uart_port *port, u8 c)
604 esp32_uart_put_char_sync(port, c);
607 static void esp32_uart_earlycon_write(struct console *con, const char *s,
610 struct earlycon_device *dev = con->data;
612 uart_console_write(&dev->port, s, n, esp32_uart_earlycon_putchar);
615 #ifdef CONFIG_CONSOLE_POLL
616 static int esp32_uart_earlycon_read(struct console *con, char *s, unsigned int n)
618 struct earlycon_device *dev = con->data;
619 unsigned int num_read = 0;
621 while (num_read < n) {
622 int c = esp32_uart_poll_get_char(&dev->port);
624 if (c == NO_POLL_CHAR)
632 static int __init esp32xx_uart_early_console_setup(struct earlycon_device *device,
635 if (!device->port.membase)
638 device->con->write = esp32_uart_earlycon_write;
639 #ifdef CONFIG_CONSOLE_POLL
640 device->con->read = esp32_uart_earlycon_read;
642 if (device->port.uartclk != BASE_BAUD * 16)
643 esp32_uart_set_baud(&device->port, device->baud);
648 static int __init esp32_uart_early_console_setup(struct earlycon_device *device,
651 device->port.private_data = (void *)&esp32_variant;
653 return esp32xx_uart_early_console_setup(device, options);
656 OF_EARLYCON_DECLARE(esp32uart, "esp,esp32-uart",
657 esp32_uart_early_console_setup);
659 static int __init esp32s3_uart_early_console_setup(struct earlycon_device *device,
662 device->port.private_data = (void *)&esp32s3_variant;
664 return esp32xx_uart_early_console_setup(device, options);
667 OF_EARLYCON_DECLARE(esp32s3uart, "esp,esp32s3-uart",
668 esp32s3_uart_early_console_setup);
670 static struct uart_driver esp32_uart_reg = {
671 .owner = THIS_MODULE,
672 .driver_name = DRIVER_NAME,
673 .dev_name = DEV_NAME,
674 .nr = ARRAY_SIZE(esp32_uart_ports),
675 .cons = &esp32_uart_console,
678 static int esp32_uart_probe(struct platform_device *pdev)
680 struct device_node *np = pdev->dev.of_node;
681 static const struct of_device_id *match;
682 struct uart_port *port;
683 struct esp32_port *sport;
684 struct resource *res;
687 match = of_match_device(esp32_uart_dt_ids, &pdev->dev);
691 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
697 ret = of_alias_get_id(np, "serial");
699 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
702 if (ret >= UART_NR) {
703 dev_err(&pdev->dev, "driver limited to %d serial ports\n", UART_NR);
709 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
713 port->mapbase = res->start;
714 port->membase = devm_ioremap_resource(&pdev->dev, res);
715 if (IS_ERR(port->membase))
716 return PTR_ERR(port->membase);
718 sport->clk = devm_clk_get(&pdev->dev, NULL);
719 if (IS_ERR(sport->clk))
720 return PTR_ERR(sport->clk);
722 port->uartclk = clk_get_rate(sport->clk);
723 port->dev = &pdev->dev;
724 port->type = PORT_GENERIC;
725 port->iotype = UPIO_MEM;
726 port->irq = platform_get_irq(pdev, 0);
727 port->ops = &esp32_uart_pops;
728 port->flags = UPF_BOOT_AUTOCONF;
730 port->fifosize = ESP32_UART_TX_FIFO_SIZE;
731 port->private_data = (void *)match->data;
733 esp32_uart_ports[port->line] = sport;
735 platform_set_drvdata(pdev, port);
737 return uart_add_one_port(&esp32_uart_reg, port);
740 static int esp32_uart_remove(struct platform_device *pdev)
742 struct uart_port *port = platform_get_drvdata(pdev);
744 uart_remove_one_port(&esp32_uart_reg, port);
750 static struct platform_driver esp32_uart_driver = {
751 .probe = esp32_uart_probe,
752 .remove = esp32_uart_remove,
755 .of_match_table = esp32_uart_dt_ids,
759 static int __init esp32_uart_init(void)
763 ret = uart_register_driver(&esp32_uart_reg);
767 ret = platform_driver_register(&esp32_uart_driver);
769 uart_unregister_driver(&esp32_uart_reg);
774 static void __exit esp32_uart_exit(void)
776 platform_driver_unregister(&esp32_uart_driver);
777 uart_unregister_driver(&esp32_uart_reg);
780 module_init(esp32_uart_init);
781 module_exit(esp32_uart_exit);
784 MODULE_LICENSE("GPL");