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_acm.c
blobbb7cc65427f0b756ecc535a7cfd468aab472e867
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 #include <linux/bitfield.h>
4 #include <linux/bits.h>
5 #include <linux/console.h>
6 #include <linux/delay.h>
7 #include <linux/io.h>
8 #include <linux/irq.h>
9 #include <linux/module.h>
10 #include <linux/of.h>
11 #include <linux/platform_device.h>
12 #include <linux/serial_core.h>
13 #include <linux/slab.h>
14 #include <linux/tty_flip.h>
15 #include <asm/serial.h>
17 #define DRIVER_NAME "esp32s3-acm"
18 #define DEV_NAME "ttyGS"
19 #define UART_NR 4
21 #define ESP32S3_ACM_TX_FIFO_SIZE 64
23 #define USB_SERIAL_JTAG_EP1_REG 0x00
24 #define USB_SERIAL_JTAG_EP1_CONF_REG 0x04
25 #define USB_SERIAL_JTAG_WR_DONE BIT(0)
26 #define USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE BIT(1)
27 #define USB_SERIAL_JTAG_INT_ST_REG 0x0c
28 #define USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ST BIT(2)
29 #define USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ST BIT(3)
30 #define USB_SERIAL_JTAG_INT_ENA_REG 0x10
31 #define USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA BIT(2)
32 #define USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA BIT(3)
33 #define USB_SERIAL_JTAG_INT_CLR_REG 0x14
34 #define USB_SERIAL_JTAG_IN_EP1_ST_REG 0x2c
35 #define USB_SERIAL_JTAG_IN_EP1_WR_ADDR GENMASK(8, 2)
36 #define USB_SERIAL_JTAG_OUT_EP1_ST_REG 0x3c
37 #define USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT GENMASK(22, 16)
39 static const struct of_device_id esp32s3_acm_dt_ids[] = {
41 .compatible = "esp,esp32s3-acm",
42 }, { /* sentinel */ }
44 MODULE_DEVICE_TABLE(of, esp32s3_acm_dt_ids);
46 static struct uart_port *esp32s3_acm_ports[UART_NR];
48 static void esp32s3_acm_write(struct uart_port *port, unsigned long reg, u32 v)
50 writel(v, port->membase + reg);
53 static u32 esp32s3_acm_read(struct uart_port *port, unsigned long reg)
55 return readl(port->membase + reg);
58 static u32 esp32s3_acm_tx_fifo_free(struct uart_port *port)
60 u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_CONF_REG);
62 return status & USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE;
65 static u32 esp32s3_acm_tx_fifo_cnt(struct uart_port *port)
67 u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_IN_EP1_ST_REG);
69 return FIELD_GET(USB_SERIAL_JTAG_IN_EP1_WR_ADDR, status);
72 static u32 esp32s3_acm_rx_fifo_cnt(struct uart_port *port)
74 u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_OUT_EP1_ST_REG);
76 return FIELD_GET(USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT, status);
79 /* return TIOCSER_TEMT when transmitter is not busy */
80 static unsigned int esp32s3_acm_tx_empty(struct uart_port *port)
82 return esp32s3_acm_tx_fifo_cnt(port) == 0 ? TIOCSER_TEMT : 0;
85 static void esp32s3_acm_set_mctrl(struct uart_port *port, unsigned int mctrl)
89 static unsigned int esp32s3_acm_get_mctrl(struct uart_port *port)
91 return TIOCM_CAR;
94 static void esp32s3_acm_stop_tx(struct uart_port *port)
96 u32 int_ena;
98 int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
99 int_ena &= ~USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA;
100 esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
103 static void esp32s3_acm_rxint(struct uart_port *port)
105 struct tty_port *tty_port = &port->state->port;
106 u32 rx_fifo_cnt = esp32s3_acm_rx_fifo_cnt(port);
107 unsigned long flags;
108 u32 i;
110 if (!rx_fifo_cnt)
111 return;
113 spin_lock_irqsave(&port->lock, flags);
115 for (i = 0; i < rx_fifo_cnt; ++i) {
116 u32 rx = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);
118 ++port->icount.rx;
119 tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
121 spin_unlock_irqrestore(&port->lock, flags);
123 tty_flip_buffer_push(tty_port);
126 static void esp32s3_acm_push(struct uart_port *port)
128 if (esp32s3_acm_tx_fifo_free(port))
129 esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_CONF_REG,
130 USB_SERIAL_JTAG_WR_DONE);
133 static void esp32s3_acm_put_char(struct uart_port *port, u8 c)
135 esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_REG, c);
138 static void esp32s3_acm_put_char_sync(struct uart_port *port, u8 c)
140 unsigned long timeout = jiffies + HZ;
142 while (!esp32s3_acm_tx_fifo_free(port)) {
143 if (time_after(jiffies, timeout)) {
144 dev_warn(port->dev, "timeout waiting for TX FIFO\n");
145 return;
147 cpu_relax();
149 esp32s3_acm_put_char(port, c);
150 esp32s3_acm_push(port);
153 static void esp32s3_acm_transmit_buffer(struct uart_port *port)
155 u32 tx_fifo_used;
156 unsigned int pending;
157 u8 ch;
159 if (!esp32s3_acm_tx_fifo_free(port))
160 return;
162 tx_fifo_used = esp32s3_acm_tx_fifo_cnt(port);
163 pending = uart_port_tx_limited(port, ch,
164 ESP32S3_ACM_TX_FIFO_SIZE - tx_fifo_used,
165 true, esp32s3_acm_put_char(port, ch),
166 ({}));
167 if (pending) {
168 u32 int_ena;
170 int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
171 int_ena |= USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA;
172 esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
174 esp32s3_acm_push(port);
177 static void esp32s3_acm_txint(struct uart_port *port)
179 esp32s3_acm_transmit_buffer(port);
182 static irqreturn_t esp32s3_acm_int(int irq, void *dev_id)
184 struct uart_port *port = dev_id;
185 u32 status;
187 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ST_REG);
188 esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_CLR_REG, status);
190 if (status & USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ST)
191 esp32s3_acm_rxint(port);
192 if (status & USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ST)
193 esp32s3_acm_txint(port);
195 return IRQ_RETVAL(status);
198 static void esp32s3_acm_start_tx(struct uart_port *port)
200 esp32s3_acm_transmit_buffer(port);
203 static void esp32s3_acm_stop_rx(struct uart_port *port)
205 u32 int_ena;
207 int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
208 int_ena &= ~USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA;
209 esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
212 static int esp32s3_acm_startup(struct uart_port *port)
214 int ret;
216 ret = request_irq(port->irq, esp32s3_acm_int, 0, DRIVER_NAME, port);
217 if (ret)
218 return ret;
219 esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG,
220 USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA);
222 return 0;
225 static void esp32s3_acm_shutdown(struct uart_port *port)
227 esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, 0);
228 free_irq(port->irq, port);
231 static void esp32s3_acm_set_termios(struct uart_port *port,
232 struct ktermios *termios,
233 const struct ktermios *old)
237 static const char *esp32s3_acm_type(struct uart_port *port)
239 return "ESP32S3 ACM";
242 /* configure/auto-configure the port */
243 static void esp32s3_acm_config_port(struct uart_port *port, int flags)
245 if (flags & UART_CONFIG_TYPE)
246 port->type = PORT_GENERIC;
249 #ifdef CONFIG_CONSOLE_POLL
250 static void esp32s3_acm_poll_put_char(struct uart_port *port, unsigned char c)
252 esp32s3_acm_put_char_sync(port, c);
255 static int esp32s3_acm_poll_get_char(struct uart_port *port)
257 if (esp32s3_acm_rx_fifo_cnt(port))
258 return esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);
259 else
260 return NO_POLL_CHAR;
262 #endif
264 static const struct uart_ops esp32s3_acm_pops = {
265 .tx_empty = esp32s3_acm_tx_empty,
266 .set_mctrl = esp32s3_acm_set_mctrl,
267 .get_mctrl = esp32s3_acm_get_mctrl,
268 .stop_tx = esp32s3_acm_stop_tx,
269 .start_tx = esp32s3_acm_start_tx,
270 .stop_rx = esp32s3_acm_stop_rx,
271 .startup = esp32s3_acm_startup,
272 .shutdown = esp32s3_acm_shutdown,
273 .set_termios = esp32s3_acm_set_termios,
274 .type = esp32s3_acm_type,
275 .config_port = esp32s3_acm_config_port,
276 #ifdef CONFIG_CONSOLE_POLL
277 .poll_put_char = esp32s3_acm_poll_put_char,
278 .poll_get_char = esp32s3_acm_poll_get_char,
279 #endif
282 static void esp32s3_acm_string_write(struct uart_port *port, const char *s,
283 unsigned int count)
285 uart_console_write(port, s, count, esp32s3_acm_put_char_sync);
288 static void
289 esp32s3_acm_console_write(struct console *co, const char *s, unsigned int count)
291 struct uart_port *port = esp32s3_acm_ports[co->index];
292 unsigned long flags;
293 bool locked = true;
295 if (port->sysrq)
296 locked = false;
297 else if (oops_in_progress)
298 locked = spin_trylock_irqsave(&port->lock, flags);
299 else
300 spin_lock_irqsave(&port->lock, flags);
302 esp32s3_acm_string_write(port, s, count);
304 if (locked)
305 spin_unlock_irqrestore(&port->lock, flags);
308 static struct uart_driver esp32s3_acm_reg;
309 static struct console esp32s3_acm_console = {
310 .name = DEV_NAME,
311 .write = esp32s3_acm_console_write,
312 .device = uart_console_device,
313 .flags = CON_PRINTBUFFER,
314 .index = -1,
315 .data = &esp32s3_acm_reg,
318 static void esp32s3_acm_earlycon_write(struct console *con, const char *s,
319 unsigned int n)
321 struct earlycon_device *dev = con->data;
323 uart_console_write(&dev->port, s, n, esp32s3_acm_put_char_sync);
326 #ifdef CONFIG_CONSOLE_POLL
327 static int esp32s3_acm_earlycon_read(struct console *con, char *s, unsigned int n)
329 struct earlycon_device *dev = con->data;
330 unsigned int num_read = 0;
332 while (num_read < n) {
333 int c = esp32s3_acm_poll_get_char(&dev->port);
335 if (c == NO_POLL_CHAR)
336 break;
337 s[num_read++] = c;
339 return num_read;
341 #endif
343 static int __init esp32s3_acm_early_console_setup(struct earlycon_device *device,
344 const char *options)
346 if (!device->port.membase)
347 return -ENODEV;
349 device->con->write = esp32s3_acm_earlycon_write;
350 #ifdef CONFIG_CONSOLE_POLL
351 device->con->read = esp32s3_acm_earlycon_read;
352 #endif
353 return 0;
356 OF_EARLYCON_DECLARE(esp32s3acm, "esp,esp32s3-acm",
357 esp32s3_acm_early_console_setup);
359 static struct uart_driver esp32s3_acm_reg = {
360 .owner = THIS_MODULE,
361 .driver_name = DRIVER_NAME,
362 .dev_name = DEV_NAME,
363 .nr = ARRAY_SIZE(esp32s3_acm_ports),
364 .cons = &esp32s3_acm_console,
367 static int esp32s3_acm_probe(struct platform_device *pdev)
369 struct device_node *np = pdev->dev.of_node;
370 struct uart_port *port;
371 struct resource *res;
372 int ret;
374 port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
375 if (!port)
376 return -ENOMEM;
378 ret = of_alias_get_id(np, "serial");
379 if (ret < 0) {
380 dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
381 return ret;
383 if (ret >= UART_NR) {
384 dev_err(&pdev->dev, "driver limited to %d serial ports\n",
385 UART_NR);
386 return -ENOMEM;
389 port->line = ret;
391 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
392 if (!res)
393 return -ENODEV;
395 port->mapbase = res->start;
396 port->membase = devm_ioremap_resource(&pdev->dev, res);
397 if (IS_ERR(port->membase))
398 return PTR_ERR(port->membase);
400 port->dev = &pdev->dev;
401 port->type = PORT_GENERIC;
402 port->iotype = UPIO_MEM;
403 port->irq = platform_get_irq(pdev, 0);
404 port->ops = &esp32s3_acm_pops;
405 port->flags = UPF_BOOT_AUTOCONF;
406 port->has_sysrq = 1;
407 port->fifosize = ESP32S3_ACM_TX_FIFO_SIZE;
409 esp32s3_acm_ports[port->line] = port;
411 platform_set_drvdata(pdev, port);
413 return uart_add_one_port(&esp32s3_acm_reg, port);
416 static void esp32s3_acm_remove(struct platform_device *pdev)
418 struct uart_port *port = platform_get_drvdata(pdev);
420 uart_remove_one_port(&esp32s3_acm_reg, port);
424 static struct platform_driver esp32s3_acm_driver = {
425 .probe = esp32s3_acm_probe,
426 .remove = esp32s3_acm_remove,
427 .driver = {
428 .name = DRIVER_NAME,
429 .of_match_table = esp32s3_acm_dt_ids,
433 static int __init esp32s3_acm_init(void)
435 int ret;
437 ret = uart_register_driver(&esp32s3_acm_reg);
438 if (ret)
439 return ret;
441 ret = platform_driver_register(&esp32s3_acm_driver);
442 if (ret)
443 uart_unregister_driver(&esp32s3_acm_reg);
445 return ret;
448 static void __exit esp32s3_acm_exit(void)
450 platform_driver_unregister(&esp32s3_acm_driver);
451 uart_unregister_driver(&esp32s3_acm_reg);
454 module_init(esp32s3_acm_init);
455 module_exit(esp32s3_acm_exit);
457 MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
458 MODULE_DESCRIPTION("Espressif ESP32 USB ACM gadget support");
459 MODULE_LICENSE("GPL");