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/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"
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)
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))
93 struct uart_port port
;
97 struct esp32_uart_variant
{
101 u32 txfifo_empty_thrhd_shift
;
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",
126 static const struct of_device_id esp32_uart_dt_ids
[] = {
128 .compatible
= "esp,esp32-uart",
129 .data
= &esp32_variant
,
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
)
199 if (status
& UART_CTSN
)
205 static void esp32_uart_stop_tx(struct uart_port
*port
)
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
);
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
);
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
);
233 uart_handle_break(port
);
235 if (uart_handle_sysrq_char(port
, (unsigned char)rx
))
237 tty_insert_flip_char(tty_port
, rx
, TTY_NORMAL
);
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");
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
;
271 if (tx_fifo_used
>= ESP32_UART_TX_FIFO_SIZE
)
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
),
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
;
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
)
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
)
327 struct esp32_port
*sport
= container_of(port
, struct esp32_port
, port
);
329 ret
= clk_prepare_enable(sport
->clk
);
333 ret
= request_irq(port
->irq
, esp32_uart_int
, 0, DRIVER_NAME
, port
);
335 clk_disable_unprepare(sport
->clk
);
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
);
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);
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
));
390 static void esp32_uart_set_termios(struct uart_port
*port
,
391 struct ktermios
*termios
,
392 const struct ktermios
*old
)
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
,
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
) {
425 conf0
|= FIELD_PREP(UART_BIT_NUM
, UART_BIT_NUM_5
);
428 conf0
|= FIELD_PREP(UART_BIT_NUM
, UART_BIT_NUM_6
);
431 conf0
|= FIELD_PREP(UART_BIT_NUM
, UART_BIT_NUM_7
);
434 conf0
|= FIELD_PREP(UART_BIT_NUM
, UART_BIT_NUM_8
);
438 if (termios
->c_cflag
& CSTOPB
)
439 conf0
|= FIELD_PREP(UART_STOP_BIT_NUM
, UART_STOP_BIT_NUM_2
);
441 conf0
|= FIELD_PREP(UART_STOP_BIT_NUM
, UART_STOP_BIT_NUM_1
);
443 if (termios
->c_cflag
& CRTSCTS
)
446 esp32_uart_write(port
, UART_CONF0_REG
, conf0
);
447 esp32_uart_write(port
, UART_CONF1_REG
, conf1
);
450 esp32_uart_set_baud(port
, baud
);
451 uart_update_timeout(port
, termios
->c_cflag
, baud
);
453 if (esp32_uart_set_baud(port
, 115200)) {
455 tty_termios_encode_baud_rate(termios
, baud
, baud
);
456 uart_update_timeout(port
, termios
->c_cflag
, baud
);
459 "unable to set speed to %d baud or the default 115200\n",
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
);
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
,
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
,
528 uart_console_write(port
, s
, count
, esp32_uart_console_putchar
);
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
;
541 else if (oops_in_progress
)
542 locked
= spin_trylock_irqsave(&port
->lock
, flags
);
544 spin_lock_irqsave(&port
->lock
, flags
);
546 esp32_uart_string_write(port
, s
, count
);
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
;
562 * check whether an invalid uart number has been specified, and
563 * if so, search for the first available port that does have
566 if (co
->index
== -1 || co
->index
>= ARRAY_SIZE(esp32_uart_ports
))
569 sport
= esp32_uart_ports
[co
->index
];
573 ret
= clk_prepare_enable(sport
->clk
);
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
);
591 static struct uart_driver esp32_uart_reg
;
592 static struct console esp32_uart_console
= {
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
,
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
,
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
)
633 static int __init
esp32xx_uart_early_console_setup(struct earlycon_device
*device
,
636 if (!device
->port
.membase
)
639 device
->con
->write
= esp32_uart_earlycon_write
;
640 #ifdef CONFIG_CONSOLE_POLL
641 device
->con
->read
= esp32_uart_earlycon_read
;
643 if (device
->port
.uartclk
!= BASE_BAUD
* 16)
644 esp32_uart_set_baud(&device
->port
, device
->baud
);
649 static int __init
esp32_uart_early_console_setup(struct earlycon_device
*device
,
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
,
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
;
687 sport
= devm_kzalloc(&pdev
->dev
, sizeof(*sport
), GFP_KERNEL
);
693 ret
= of_alias_get_id(np
, "serial");
695 dev_err(&pdev
->dev
, "failed to get alias id, errno %d\n", ret
);
698 if (ret
>= UART_NR
) {
699 dev_err(&pdev
->dev
, "driver limited to %d serial ports\n", UART_NR
);
705 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
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
;
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
,
749 .of_match_table
= esp32_uart_dt_ids
,
753 static int __init
esp32_uart_init(void)
757 ret
= uart_register_driver(&esp32_uart_reg
);
761 ret
= platform_driver_register(&esp32_uart_driver
);
763 uart_unregister_driver(&esp32_uart_reg
);
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");