Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / tty / serial / esp32_uart.c
blob667c2198a03a51e9b88fa760868a123601994897
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
5 #include <linux/clk.h>
6 #include <linux/console.h>
7 #include <linux/delay.h>
8 #include <linux/io.h>
9 #include <linux/irq.h>
10 #include <linux/module.h>
11 #include <linux/of.h>
12 #include <linux/platform_device.h>
13 #include <linux/property.h>
14 #include <linux/serial_core.h>
15 #include <linux/slab.h>
16 #include <linux/tty_flip.h>
17 #include <asm/serial.h>
19 #define DRIVER_NAME "esp32-uart"
20 #define DEV_NAME "ttyS"
21 #define UART_NR 3
23 #define ESP32_UART_TX_FIFO_SIZE 127
24 #define ESP32_UART_RX_FIFO_SIZE 127
26 #define UART_FIFO_REG 0x00
27 #define UART_INT_RAW_REG 0x04
28 #define UART_INT_ST_REG 0x08
29 #define UART_INT_ENA_REG 0x0c
30 #define UART_INT_CLR_REG 0x10
31 #define UART_RXFIFO_FULL_INT BIT(0)
32 #define UART_TXFIFO_EMPTY_INT BIT(1)
33 #define UART_BRK_DET_INT BIT(7)
34 #define UART_CLKDIV_REG 0x14
35 #define ESP32_UART_CLKDIV GENMASK(19, 0)
36 #define ESP32S3_UART_CLKDIV GENMASK(11, 0)
37 #define UART_CLKDIV_SHIFT 0
38 #define UART_CLKDIV_FRAG GENMASK(23, 20)
39 #define UART_STATUS_REG 0x1c
40 #define ESP32_UART_RXFIFO_CNT GENMASK(7, 0)
41 #define ESP32S3_UART_RXFIFO_CNT GENMASK(9, 0)
42 #define UART_RXFIFO_CNT_SHIFT 0
43 #define UART_DSRN BIT(13)
44 #define UART_CTSN BIT(14)
45 #define ESP32_UART_TXFIFO_CNT GENMASK(23, 16)
46 #define ESP32S3_UART_TXFIFO_CNT GENMASK(25, 16)
47 #define UART_TXFIFO_CNT_SHIFT 16
48 #define UART_CONF0_REG 0x20
49 #define UART_PARITY BIT(0)
50 #define UART_PARITY_EN BIT(1)
51 #define UART_BIT_NUM GENMASK(3, 2)
52 #define UART_BIT_NUM_5 0
53 #define UART_BIT_NUM_6 1
54 #define UART_BIT_NUM_7 2
55 #define UART_BIT_NUM_8 3
56 #define UART_STOP_BIT_NUM GENMASK(5, 4)
57 #define UART_STOP_BIT_NUM_1 1
58 #define UART_STOP_BIT_NUM_2 3
59 #define UART_SW_RTS BIT(6)
60 #define UART_SW_DTR BIT(7)
61 #define UART_LOOPBACK BIT(14)
62 #define UART_TX_FLOW_EN BIT(15)
63 #define UART_RTS_INV BIT(23)
64 #define UART_DTR_INV BIT(24)
65 #define UART_CONF1_REG 0x24
66 #define UART_RXFIFO_FULL_THRHD_SHIFT 0
67 #define ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT 8
68 #define ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT 10
69 #define ESP32_UART_RX_FLOW_EN BIT(23)
70 #define ESP32S3_UART_RX_FLOW_EN BIT(22)
71 #define ESP32S3_UART_CLK_CONF_REG 0x78
72 #define ESP32S3_UART_SCLK_DIV_B GENMASK(5, 0)
73 #define ESP32S3_UART_SCLK_DIV_A GENMASK(11, 6)
74 #define ESP32S3_UART_SCLK_DIV_NUM GENMASK(19, 12)
75 #define ESP32S3_UART_SCLK_SEL GENMASK(21, 20)
76 #define APB_CLK 1
77 #define RC_FAST_CLK 2
78 #define XTAL_CLK 3
79 #define ESP32S3_UART_SCLK_EN BIT(22)
80 #define ESP32S3_UART_RST_CORE BIT(23)
81 #define ESP32S3_UART_TX_SCLK_EN BIT(24)
82 #define ESP32S3_UART_RX_SCLK_EN BIT(25)
83 #define ESP32S3_UART_TX_RST_CORE BIT(26)
84 #define ESP32S3_UART_RX_RST_CORE BIT(27)
86 #define ESP32S3_UART_CLK_CONF_DEFAULT \
87 (ESP32S3_UART_RX_SCLK_EN | \
88 ESP32S3_UART_TX_SCLK_EN | \
89 ESP32S3_UART_SCLK_EN | \
90 FIELD_PREP(ESP32S3_UART_SCLK_SEL, XTAL_CLK))
92 struct esp32_port {
93 struct uart_port port;
94 struct clk *clk;
97 struct esp32_uart_variant {
98 u32 clkdiv_mask;
99 u32 rxfifo_cnt_mask;
100 u32 txfifo_cnt_mask;
101 u32 txfifo_empty_thrhd_shift;
102 u32 rx_flow_en;
103 const char *type;
104 bool has_clkconf;
107 static const struct esp32_uart_variant esp32_variant = {
108 .clkdiv_mask = ESP32_UART_CLKDIV,
109 .rxfifo_cnt_mask = ESP32_UART_RXFIFO_CNT,
110 .txfifo_cnt_mask = ESP32_UART_TXFIFO_CNT,
111 .txfifo_empty_thrhd_shift = ESP32_UART_TXFIFO_EMPTY_THRHD_SHIFT,
112 .rx_flow_en = ESP32_UART_RX_FLOW_EN,
113 .type = "ESP32 UART",
116 static const struct esp32_uart_variant esp32s3_variant = {
117 .clkdiv_mask = ESP32S3_UART_CLKDIV,
118 .rxfifo_cnt_mask = ESP32S3_UART_RXFIFO_CNT,
119 .txfifo_cnt_mask = ESP32S3_UART_TXFIFO_CNT,
120 .txfifo_empty_thrhd_shift = ESP32S3_UART_TXFIFO_EMPTY_THRHD_SHIFT,
121 .rx_flow_en = ESP32S3_UART_RX_FLOW_EN,
122 .type = "ESP32S3 UART",
123 .has_clkconf = true,
126 static const struct of_device_id esp32_uart_dt_ids[] = {
128 .compatible = "esp,esp32-uart",
129 .data = &esp32_variant,
130 }, {
131 .compatible = "esp,esp32s3-uart",
132 .data = &esp32s3_variant,
133 }, { /* sentinel */ }
135 MODULE_DEVICE_TABLE(of, esp32_uart_dt_ids);
137 static struct esp32_port *esp32_uart_ports[UART_NR];
139 static const struct esp32_uart_variant *port_variant(struct uart_port *port)
141 return port->private_data;
144 static void esp32_uart_write(struct uart_port *port, unsigned long reg, u32 v)
146 writel(v, port->membase + reg);
149 static u32 esp32_uart_read(struct uart_port *port, unsigned long reg)
151 return readl(port->membase + reg);
154 static u32 esp32_uart_tx_fifo_cnt(struct uart_port *port)
156 u32 status = esp32_uart_read(port, UART_STATUS_REG);
158 return (status & port_variant(port)->txfifo_cnt_mask) >> UART_TXFIFO_CNT_SHIFT;
161 static u32 esp32_uart_rx_fifo_cnt(struct uart_port *port)
163 u32 status = esp32_uart_read(port, UART_STATUS_REG);
165 return (status & port_variant(port)->rxfifo_cnt_mask) >> UART_RXFIFO_CNT_SHIFT;
168 /* return TIOCSER_TEMT when transmitter is not busy */
169 static unsigned int esp32_uart_tx_empty(struct uart_port *port)
171 return esp32_uart_tx_fifo_cnt(port) ? 0 : TIOCSER_TEMT;
174 static void esp32_uart_set_mctrl(struct uart_port *port, unsigned int mctrl)
176 u32 conf0 = esp32_uart_read(port, UART_CONF0_REG);
178 conf0 &= ~(UART_LOOPBACK |
179 UART_SW_RTS | UART_RTS_INV |
180 UART_SW_DTR | UART_DTR_INV);
182 if (mctrl & TIOCM_RTS)
183 conf0 |= UART_SW_RTS;
184 if (mctrl & TIOCM_DTR)
185 conf0 |= UART_SW_DTR;
186 if (mctrl & TIOCM_LOOP)
187 conf0 |= UART_LOOPBACK;
189 esp32_uart_write(port, UART_CONF0_REG, conf0);
192 static unsigned int esp32_uart_get_mctrl(struct uart_port *port)
194 u32 status = esp32_uart_read(port, UART_STATUS_REG);
195 unsigned int ret = TIOCM_CAR;
197 if (status & UART_DSRN)
198 ret |= TIOCM_DSR;
199 if (status & UART_CTSN)
200 ret |= TIOCM_CTS;
202 return ret;
205 static void esp32_uart_stop_tx(struct uart_port *port)
207 u32 int_ena;
209 int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
210 int_ena &= ~UART_TXFIFO_EMPTY_INT;
211 esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
214 static void esp32_uart_rxint(struct uart_port *port)
216 struct tty_port *tty_port = &port->state->port;
217 u32 rx_fifo_cnt = esp32_uart_rx_fifo_cnt(port);
218 unsigned long flags;
219 u32 i;
221 if (!rx_fifo_cnt)
222 return;
224 spin_lock_irqsave(&port->lock, flags);
226 for (i = 0; i < rx_fifo_cnt; ++i) {
227 u32 rx = esp32_uart_read(port, UART_FIFO_REG);
229 if (!rx &&
230 (esp32_uart_read(port, UART_INT_ST_REG) & UART_BRK_DET_INT)) {
231 esp32_uart_write(port, UART_INT_CLR_REG, UART_BRK_DET_INT);
232 ++port->icount.brk;
233 uart_handle_break(port);
234 } else {
235 if (uart_handle_sysrq_char(port, (unsigned char)rx))
236 continue;
237 tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
238 ++port->icount.rx;
241 spin_unlock_irqrestore(&port->lock, flags);
243 tty_flip_buffer_push(tty_port);
246 static void esp32_uart_put_char(struct uart_port *port, u8 c)
248 esp32_uart_write(port, UART_FIFO_REG, c);
251 static void esp32_uart_put_char_sync(struct uart_port *port, u8 c)
253 unsigned long timeout = jiffies + HZ;
255 while (esp32_uart_tx_fifo_cnt(port) >= ESP32_UART_TX_FIFO_SIZE) {
256 if (time_after(jiffies, timeout)) {
257 dev_warn(port->dev, "timeout waiting for TX FIFO\n");
258 return;
260 cpu_relax();
262 esp32_uart_put_char(port, c);
265 static void esp32_uart_transmit_buffer(struct uart_port *port)
267 u32 tx_fifo_used = esp32_uart_tx_fifo_cnt(port);
268 unsigned int pending;
269 u8 ch;
271 if (tx_fifo_used >= ESP32_UART_TX_FIFO_SIZE)
272 return;
274 pending = uart_port_tx_limited(port, ch,
275 ESP32_UART_TX_FIFO_SIZE - tx_fifo_used,
276 true, esp32_uart_put_char(port, ch),
277 ({}));
278 if (pending) {
279 u32 int_ena;
281 int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
282 int_ena |= UART_TXFIFO_EMPTY_INT;
283 esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
287 static void esp32_uart_txint(struct uart_port *port)
289 esp32_uart_transmit_buffer(port);
292 static irqreturn_t esp32_uart_int(int irq, void *dev_id)
294 struct uart_port *port = dev_id;
295 u32 status;
297 status = esp32_uart_read(port, UART_INT_ST_REG);
299 if (status & (UART_RXFIFO_FULL_INT | UART_BRK_DET_INT))
300 esp32_uart_rxint(port);
301 if (status & UART_TXFIFO_EMPTY_INT)
302 esp32_uart_txint(port);
304 esp32_uart_write(port, UART_INT_CLR_REG, status);
306 return IRQ_RETVAL(status);
309 static void esp32_uart_start_tx(struct uart_port *port)
311 esp32_uart_transmit_buffer(port);
314 static void esp32_uart_stop_rx(struct uart_port *port)
316 u32 int_ena;
318 int_ena = esp32_uart_read(port, UART_INT_ENA_REG);
319 int_ena &= ~UART_RXFIFO_FULL_INT;
320 esp32_uart_write(port, UART_INT_ENA_REG, int_ena);
323 static int esp32_uart_startup(struct uart_port *port)
325 int ret = 0;
326 unsigned long flags;
327 struct esp32_port *sport = container_of(port, struct esp32_port, port);
329 ret = clk_prepare_enable(sport->clk);
330 if (ret)
331 return ret;
333 ret = request_irq(port->irq, esp32_uart_int, 0, DRIVER_NAME, port);
334 if (ret) {
335 clk_disable_unprepare(sport->clk);
336 return ret;
339 spin_lock_irqsave(&port->lock, flags);
340 if (port_variant(port)->has_clkconf)
341 esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
342 ESP32S3_UART_CLK_CONF_DEFAULT);
343 esp32_uart_write(port, UART_CONF1_REG,
344 (1 << UART_RXFIFO_FULL_THRHD_SHIFT) |
345 (1 << port_variant(port)->txfifo_empty_thrhd_shift));
346 esp32_uart_write(port, UART_INT_CLR_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
347 esp32_uart_write(port, UART_INT_ENA_REG, UART_RXFIFO_FULL_INT | UART_BRK_DET_INT);
348 spin_unlock_irqrestore(&port->lock, flags);
350 return ret;
353 static void esp32_uart_shutdown(struct uart_port *port)
355 struct esp32_port *sport = container_of(port, struct esp32_port, port);
357 esp32_uart_write(port, UART_INT_ENA_REG, 0);
358 free_irq(port->irq, port);
359 clk_disable_unprepare(sport->clk);
362 static bool esp32_uart_set_baud(struct uart_port *port, u32 baud)
364 u32 sclk = port->uartclk;
365 u32 div = sclk / baud;
367 if (port_variant(port)->has_clkconf) {
368 u32 sclk_div = div / port_variant(port)->clkdiv_mask;
370 if (div > port_variant(port)->clkdiv_mask) {
371 sclk /= (sclk_div + 1);
372 div = sclk / baud;
374 esp32_uart_write(port, ESP32S3_UART_CLK_CONF_REG,
375 FIELD_PREP(ESP32S3_UART_SCLK_DIV_NUM, sclk_div) |
376 ESP32S3_UART_CLK_CONF_DEFAULT);
379 if (div <= port_variant(port)->clkdiv_mask) {
380 u32 frag = (sclk * 16) / baud - div * 16;
382 esp32_uart_write(port, UART_CLKDIV_REG,
383 div | FIELD_PREP(UART_CLKDIV_FRAG, frag));
384 return true;
387 return false;
390 static void esp32_uart_set_termios(struct uart_port *port,
391 struct ktermios *termios,
392 const struct ktermios *old)
394 unsigned long flags;
395 u32 conf0, conf1;
396 u32 baud;
397 const u32 rx_flow_en = port_variant(port)->rx_flow_en;
398 u32 max_div = port_variant(port)->clkdiv_mask;
400 termios->c_cflag &= ~CMSPAR;
402 if (port_variant(port)->has_clkconf)
403 max_div *= FIELD_MAX(ESP32S3_UART_SCLK_DIV_NUM);
405 baud = uart_get_baud_rate(port, termios, old,
406 port->uartclk / max_div,
407 port->uartclk / 16);
409 spin_lock_irqsave(&port->lock, flags);
411 conf0 = esp32_uart_read(port, UART_CONF0_REG);
412 conf0 &= ~(UART_PARITY_EN | UART_PARITY | UART_BIT_NUM | UART_STOP_BIT_NUM);
414 conf1 = esp32_uart_read(port, UART_CONF1_REG);
415 conf1 &= ~rx_flow_en;
417 if (termios->c_cflag & PARENB) {
418 conf0 |= UART_PARITY_EN;
419 if (termios->c_cflag & PARODD)
420 conf0 |= UART_PARITY;
423 switch (termios->c_cflag & CSIZE) {
424 case CS5:
425 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_5);
426 break;
427 case CS6:
428 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_6);
429 break;
430 case CS7:
431 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_7);
432 break;
433 case CS8:
434 conf0 |= FIELD_PREP(UART_BIT_NUM, UART_BIT_NUM_8);
435 break;
438 if (termios->c_cflag & CSTOPB)
439 conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_2);
440 else
441 conf0 |= FIELD_PREP(UART_STOP_BIT_NUM, UART_STOP_BIT_NUM_1);
443 if (termios->c_cflag & CRTSCTS)
444 conf1 |= rx_flow_en;
446 esp32_uart_write(port, UART_CONF0_REG, conf0);
447 esp32_uart_write(port, UART_CONF1_REG, conf1);
449 if (baud) {
450 esp32_uart_set_baud(port, baud);
451 uart_update_timeout(port, termios->c_cflag, baud);
452 } else {
453 if (esp32_uart_set_baud(port, 115200)) {
454 baud = 115200;
455 tty_termios_encode_baud_rate(termios, baud, baud);
456 uart_update_timeout(port, termios->c_cflag, baud);
457 } else {
458 dev_warn(port->dev,
459 "unable to set speed to %d baud or the default 115200\n",
460 baud);
463 spin_unlock_irqrestore(&port->lock, flags);
466 static const char *esp32_uart_type(struct uart_port *port)
468 return port_variant(port)->type;
471 /* configure/auto-configure the port */
472 static void esp32_uart_config_port(struct uart_port *port, int flags)
474 if (flags & UART_CONFIG_TYPE)
475 port->type = PORT_GENERIC;
478 #ifdef CONFIG_CONSOLE_POLL
479 static int esp32_uart_poll_init(struct uart_port *port)
481 struct esp32_port *sport = container_of(port, struct esp32_port, port);
483 return clk_prepare_enable(sport->clk);
486 static void esp32_uart_poll_put_char(struct uart_port *port, unsigned char c)
488 esp32_uart_put_char_sync(port, c);
491 static int esp32_uart_poll_get_char(struct uart_port *port)
493 if (esp32_uart_rx_fifo_cnt(port))
494 return esp32_uart_read(port, UART_FIFO_REG);
495 else
496 return NO_POLL_CHAR;
499 #endif
501 static const struct uart_ops esp32_uart_pops = {
502 .tx_empty = esp32_uart_tx_empty,
503 .set_mctrl = esp32_uart_set_mctrl,
504 .get_mctrl = esp32_uart_get_mctrl,
505 .stop_tx = esp32_uart_stop_tx,
506 .start_tx = esp32_uart_start_tx,
507 .stop_rx = esp32_uart_stop_rx,
508 .startup = esp32_uart_startup,
509 .shutdown = esp32_uart_shutdown,
510 .set_termios = esp32_uart_set_termios,
511 .type = esp32_uart_type,
512 .config_port = esp32_uart_config_port,
513 #ifdef CONFIG_CONSOLE_POLL
514 .poll_init = esp32_uart_poll_init,
515 .poll_put_char = esp32_uart_poll_put_char,
516 .poll_get_char = esp32_uart_poll_get_char,
517 #endif
520 static void esp32_uart_console_putchar(struct uart_port *port, u8 c)
522 esp32_uart_put_char_sync(port, c);
525 static void esp32_uart_string_write(struct uart_port *port, const char *s,
526 unsigned int count)
528 uart_console_write(port, s, count, esp32_uart_console_putchar);
531 static void
532 esp32_uart_console_write(struct console *co, const char *s, unsigned int count)
534 struct esp32_port *sport = esp32_uart_ports[co->index];
535 struct uart_port *port = &sport->port;
536 unsigned long flags;
537 bool locked = true;
539 if (port->sysrq)
540 locked = false;
541 else if (oops_in_progress)
542 locked = spin_trylock_irqsave(&port->lock, flags);
543 else
544 spin_lock_irqsave(&port->lock, flags);
546 esp32_uart_string_write(port, s, count);
548 if (locked)
549 spin_unlock_irqrestore(&port->lock, flags);
552 static int __init esp32_uart_console_setup(struct console *co, char *options)
554 struct esp32_port *sport;
555 int baud = 115200;
556 int bits = 8;
557 int parity = 'n';
558 int flow = 'n';
559 int ret;
562 * check whether an invalid uart number has been specified, and
563 * if so, search for the first available port that does have
564 * console support.
566 if (co->index == -1 || co->index >= ARRAY_SIZE(esp32_uart_ports))
567 co->index = 0;
569 sport = esp32_uart_ports[co->index];
570 if (!sport)
571 return -ENODEV;
573 ret = clk_prepare_enable(sport->clk);
574 if (ret)
575 return ret;
577 if (options)
578 uart_parse_options(options, &baud, &parity, &bits, &flow);
580 return uart_set_options(&sport->port, co, baud, parity, bits, flow);
583 static int esp32_uart_console_exit(struct console *co)
585 struct esp32_port *sport = esp32_uart_ports[co->index];
587 clk_disable_unprepare(sport->clk);
588 return 0;
591 static struct uart_driver esp32_uart_reg;
592 static struct console esp32_uart_console = {
593 .name = DEV_NAME,
594 .write = esp32_uart_console_write,
595 .device = uart_console_device,
596 .setup = esp32_uart_console_setup,
597 .exit = esp32_uart_console_exit,
598 .flags = CON_PRINTBUFFER,
599 .index = -1,
600 .data = &esp32_uart_reg,
603 static void esp32_uart_earlycon_putchar(struct uart_port *port, u8 c)
605 esp32_uart_put_char_sync(port, c);
608 static void esp32_uart_earlycon_write(struct console *con, const char *s,
609 unsigned int n)
611 struct earlycon_device *dev = con->data;
613 uart_console_write(&dev->port, s, n, esp32_uart_earlycon_putchar);
616 #ifdef CONFIG_CONSOLE_POLL
617 static int esp32_uart_earlycon_read(struct console *con, char *s, unsigned int n)
619 struct earlycon_device *dev = con->data;
620 unsigned int num_read = 0;
622 while (num_read < n) {
623 int c = esp32_uart_poll_get_char(&dev->port);
625 if (c == NO_POLL_CHAR)
626 break;
627 s[num_read++] = c;
629 return num_read;
631 #endif
633 static int __init esp32xx_uart_early_console_setup(struct earlycon_device *device,
634 const char *options)
636 if (!device->port.membase)
637 return -ENODEV;
639 device->con->write = esp32_uart_earlycon_write;
640 #ifdef CONFIG_CONSOLE_POLL
641 device->con->read = esp32_uart_earlycon_read;
642 #endif
643 if (device->port.uartclk != BASE_BAUD * 16)
644 esp32_uart_set_baud(&device->port, device->baud);
646 return 0;
649 static int __init esp32_uart_early_console_setup(struct earlycon_device *device,
650 const char *options)
652 device->port.private_data = (void *)&esp32_variant;
654 return esp32xx_uart_early_console_setup(device, options);
657 OF_EARLYCON_DECLARE(esp32uart, "esp,esp32-uart",
658 esp32_uart_early_console_setup);
660 static int __init esp32s3_uart_early_console_setup(struct earlycon_device *device,
661 const char *options)
663 device->port.private_data = (void *)&esp32s3_variant;
665 return esp32xx_uart_early_console_setup(device, options);
668 OF_EARLYCON_DECLARE(esp32s3uart, "esp,esp32s3-uart",
669 esp32s3_uart_early_console_setup);
671 static struct uart_driver esp32_uart_reg = {
672 .owner = THIS_MODULE,
673 .driver_name = DRIVER_NAME,
674 .dev_name = DEV_NAME,
675 .nr = ARRAY_SIZE(esp32_uart_ports),
676 .cons = &esp32_uart_console,
679 static int esp32_uart_probe(struct platform_device *pdev)
681 struct device_node *np = pdev->dev.of_node;
682 struct uart_port *port;
683 struct esp32_port *sport;
684 struct resource *res;
685 int ret;
687 sport = devm_kzalloc(&pdev->dev, sizeof(*sport), GFP_KERNEL);
688 if (!sport)
689 return -ENOMEM;
691 port = &sport->port;
693 ret = of_alias_get_id(np, "serial");
694 if (ret < 0) {
695 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
696 return ret;
698 if (ret >= UART_NR) {
699 dev_err(&pdev->dev, "driver limited to %d serial ports\n", UART_NR);
700 return -ENOMEM;
703 port->line = ret;
705 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706 if (!res)
707 return -ENODEV;
709 port->mapbase = res->start;
710 port->membase = devm_ioremap_resource(&pdev->dev, res);
711 if (IS_ERR(port->membase))
712 return PTR_ERR(port->membase);
714 sport->clk = devm_clk_get(&pdev->dev, NULL);
715 if (IS_ERR(sport->clk))
716 return PTR_ERR(sport->clk);
718 port->uartclk = clk_get_rate(sport->clk);
719 port->dev = &pdev->dev;
720 port->type = PORT_GENERIC;
721 port->iotype = UPIO_MEM;
722 port->irq = platform_get_irq(pdev, 0);
723 port->ops = &esp32_uart_pops;
724 port->flags = UPF_BOOT_AUTOCONF;
725 port->has_sysrq = 1;
726 port->fifosize = ESP32_UART_TX_FIFO_SIZE;
727 port->private_data = (void *)device_get_match_data(&pdev->dev);
729 esp32_uart_ports[port->line] = sport;
731 platform_set_drvdata(pdev, port);
733 return uart_add_one_port(&esp32_uart_reg, port);
736 static void esp32_uart_remove(struct platform_device *pdev)
738 struct uart_port *port = platform_get_drvdata(pdev);
740 uart_remove_one_port(&esp32_uart_reg, port);
744 static struct platform_driver esp32_uart_driver = {
745 .probe = esp32_uart_probe,
746 .remove = esp32_uart_remove,
747 .driver = {
748 .name = DRIVER_NAME,
749 .of_match_table = esp32_uart_dt_ids,
753 static int __init esp32_uart_init(void)
755 int ret;
757 ret = uart_register_driver(&esp32_uart_reg);
758 if (ret)
759 return ret;
761 ret = platform_driver_register(&esp32_uart_driver);
762 if (ret)
763 uart_unregister_driver(&esp32_uart_reg);
765 return ret;
768 static void __exit esp32_uart_exit(void)
770 platform_driver_unregister(&esp32_uart_driver);
771 uart_unregister_driver(&esp32_uart_reg);
774 module_init(esp32_uart_init);
775 module_exit(esp32_uart_exit);
777 MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
778 MODULE_DESCRIPTION("Espressif ESP32 UART support");
779 MODULE_LICENSE("GPL");