xref: /linux/drivers/tty/serial/esp32_acm.c (revision d8b78066f4c9c8ec898bd4246fc4db31d476dd88)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 
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>
16 
17 #define DRIVER_NAME	"esp32s3-acm"
18 #define DEV_NAME	"ttyGS"
19 #define UART_NR		4
20 
21 #define ESP32S3_ACM_TX_FIFO_SIZE	64
22 
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)
38 
39 static const struct of_device_id esp32s3_acm_dt_ids[] = {
40 	{
41 		.compatible = "esp,esp32s3-acm",
42 	}, { /* sentinel */ }
43 };
44 MODULE_DEVICE_TABLE(of, esp32s3_acm_dt_ids);
45 
46 static struct uart_port *esp32s3_acm_ports[UART_NR];
47 
esp32s3_acm_write(struct uart_port * port,unsigned long reg,u32 v)48 static void esp32s3_acm_write(struct uart_port *port, unsigned long reg, u32 v)
49 {
50 	writel(v, port->membase + reg);
51 }
52 
esp32s3_acm_read(struct uart_port * port,unsigned long reg)53 static u32 esp32s3_acm_read(struct uart_port *port, unsigned long reg)
54 {
55 	return readl(port->membase + reg);
56 }
57 
esp32s3_acm_tx_fifo_free(struct uart_port * port)58 static u32 esp32s3_acm_tx_fifo_free(struct uart_port *port)
59 {
60 	u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_CONF_REG);
61 
62 	return status & USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE;
63 }
64 
esp32s3_acm_tx_fifo_cnt(struct uart_port * port)65 static u32 esp32s3_acm_tx_fifo_cnt(struct uart_port *port)
66 {
67 	u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_IN_EP1_ST_REG);
68 
69 	return FIELD_GET(USB_SERIAL_JTAG_IN_EP1_WR_ADDR, status);
70 }
71 
esp32s3_acm_rx_fifo_cnt(struct uart_port * port)72 static u32 esp32s3_acm_rx_fifo_cnt(struct uart_port *port)
73 {
74 	u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_OUT_EP1_ST_REG);
75 
76 	return FIELD_GET(USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT, status);
77 }
78 
79 /* return TIOCSER_TEMT when transmitter is not busy */
esp32s3_acm_tx_empty(struct uart_port * port)80 static unsigned int esp32s3_acm_tx_empty(struct uart_port *port)
81 {
82 	return esp32s3_acm_tx_fifo_cnt(port) == 0 ? TIOCSER_TEMT : 0;
83 }
84 
esp32s3_acm_set_mctrl(struct uart_port * port,unsigned int mctrl)85 static void esp32s3_acm_set_mctrl(struct uart_port *port, unsigned int mctrl)
86 {
87 }
88 
esp32s3_acm_get_mctrl(struct uart_port * port)89 static unsigned int esp32s3_acm_get_mctrl(struct uart_port *port)
90 {
91 	return TIOCM_CAR;
92 }
93 
esp32s3_acm_stop_tx(struct uart_port * port)94 static void esp32s3_acm_stop_tx(struct uart_port *port)
95 {
96 	u32 int_ena;
97 
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);
101 }
102 
esp32s3_acm_rxint(struct uart_port * port)103 static void esp32s3_acm_rxint(struct uart_port *port)
104 {
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;
109 
110 	if (!rx_fifo_cnt)
111 		return;
112 
113 	spin_lock_irqsave(&port->lock, flags);
114 
115 	for (i = 0; i < rx_fifo_cnt; ++i) {
116 		u32 rx = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);
117 
118 		++port->icount.rx;
119 		tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
120 	}
121 	spin_unlock_irqrestore(&port->lock, flags);
122 
123 	tty_flip_buffer_push(tty_port);
124 }
125 
esp32s3_acm_push(struct uart_port * port)126 static void esp32s3_acm_push(struct uart_port *port)
127 {
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);
131 }
132 
esp32s3_acm_put_char(struct uart_port * port,u8 c)133 static void esp32s3_acm_put_char(struct uart_port *port, u8 c)
134 {
135 	esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_REG, c);
136 }
137 
esp32s3_acm_put_char_sync(struct uart_port * port,u8 c)138 static void esp32s3_acm_put_char_sync(struct uart_port *port, u8 c)
139 {
140 	unsigned long timeout = jiffies + HZ;
141 
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;
146 		}
147 		cpu_relax();
148 	}
149 	esp32s3_acm_put_char(port, c);
150 	esp32s3_acm_push(port);
151 }
152 
esp32s3_acm_transmit_buffer(struct uart_port * port)153 static void esp32s3_acm_transmit_buffer(struct uart_port *port)
154 {
155 	u32 tx_fifo_used;
156 	unsigned int pending;
157 	u8 ch;
158 
159 	if (!esp32s3_acm_tx_fifo_free(port))
160 		return;
161 
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;
169 
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);
173 	}
174 	esp32s3_acm_push(port);
175 }
176 
esp32s3_acm_txint(struct uart_port * port)177 static void esp32s3_acm_txint(struct uart_port *port)
178 {
179 	esp32s3_acm_transmit_buffer(port);
180 }
181 
esp32s3_acm_int(int irq,void * dev_id)182 static irqreturn_t esp32s3_acm_int(int irq, void *dev_id)
183 {
184 	struct uart_port *port = dev_id;
185 	u32 status;
186 
187 	status = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ST_REG);
188 	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_CLR_REG, status);
189 
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);
194 
195 	return IRQ_RETVAL(status);
196 }
197 
esp32s3_acm_start_tx(struct uart_port * port)198 static void esp32s3_acm_start_tx(struct uart_port *port)
199 {
200 	esp32s3_acm_transmit_buffer(port);
201 }
202 
esp32s3_acm_stop_rx(struct uart_port * port)203 static void esp32s3_acm_stop_rx(struct uart_port *port)
204 {
205 	u32 int_ena;
206 
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);
210 }
211 
esp32s3_acm_startup(struct uart_port * port)212 static int esp32s3_acm_startup(struct uart_port *port)
213 {
214 	int ret;
215 
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);
221 
222 	return 0;
223 }
224 
esp32s3_acm_shutdown(struct uart_port * port)225 static void esp32s3_acm_shutdown(struct uart_port *port)
226 {
227 	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, 0);
228 	free_irq(port->irq, port);
229 }
230 
esp32s3_acm_set_termios(struct uart_port * port,struct ktermios * termios,const struct ktermios * old)231 static void esp32s3_acm_set_termios(struct uart_port *port,
232 				    struct ktermios *termios,
233 				    const struct ktermios *old)
234 {
235 }
236 
esp32s3_acm_type(struct uart_port * port)237 static const char *esp32s3_acm_type(struct uart_port *port)
238 {
239 	return "ESP32S3 ACM";
240 }
241 
242 /* configure/auto-configure the port */
esp32s3_acm_config_port(struct uart_port * port,int flags)243 static void esp32s3_acm_config_port(struct uart_port *port, int flags)
244 {
245 	if (flags & UART_CONFIG_TYPE)
246 		port->type = PORT_GENERIC;
247 }
248 
249 #ifdef CONFIG_CONSOLE_POLL
esp32s3_acm_poll_put_char(struct uart_port * port,unsigned char c)250 static void esp32s3_acm_poll_put_char(struct uart_port *port, unsigned char c)
251 {
252 	esp32s3_acm_put_char_sync(port, c);
253 }
254 
esp32s3_acm_poll_get_char(struct uart_port * port)255 static int esp32s3_acm_poll_get_char(struct uart_port *port)
256 {
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;
261 }
262 #endif
263 
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
280 };
281 
esp32s3_acm_string_write(struct uart_port * port,const char * s,unsigned int count)282 static void esp32s3_acm_string_write(struct uart_port *port, const char *s,
283 				     unsigned int count)
284 {
285 	uart_console_write(port, s, count, esp32s3_acm_put_char_sync);
286 }
287 
288 static void
esp32s3_acm_console_write(struct console * co,const char * s,unsigned int count)289 esp32s3_acm_console_write(struct console *co, const char *s, unsigned int count)
290 {
291 	struct uart_port *port = esp32s3_acm_ports[co->index];
292 	unsigned long flags;
293 	bool locked = true;
294 
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);
301 
302 	esp32s3_acm_string_write(port, s, count);
303 
304 	if (locked)
305 		spin_unlock_irqrestore(&port->lock, flags);
306 }
307 
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,
316 };
317 
esp32s3_acm_earlycon_write(struct console * con,const char * s,unsigned int n)318 static void esp32s3_acm_earlycon_write(struct console *con, const char *s,
319 				      unsigned int n)
320 {
321 	struct earlycon_device *dev = con->data;
322 
323 	uart_console_write(&dev->port, s, n, esp32s3_acm_put_char_sync);
324 }
325 
326 #ifdef CONFIG_CONSOLE_POLL
esp32s3_acm_earlycon_read(struct console * con,char * s,unsigned int n)327 static int esp32s3_acm_earlycon_read(struct console *con, char *s, unsigned int n)
328 {
329 	struct earlycon_device *dev = con->data;
330 	unsigned int num_read = 0;
331 
332 	while (num_read < n) {
333 		int c = esp32s3_acm_poll_get_char(&dev->port);
334 
335 		if (c == NO_POLL_CHAR)
336 			break;
337 		s[num_read++] = c;
338 	}
339 	return num_read;
340 }
341 #endif
342 
esp32s3_acm_early_console_setup(struct earlycon_device * device,const char * options)343 static int __init esp32s3_acm_early_console_setup(struct earlycon_device *device,
344 						   const char *options)
345 {
346 	if (!device->port.membase)
347 		return -ENODEV;
348 
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;
354 }
355 
356 OF_EARLYCON_DECLARE(esp32s3acm, "esp,esp32s3-acm",
357 		    esp32s3_acm_early_console_setup);
358 
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,
365 };
366 
esp32s3_acm_probe(struct platform_device * pdev)367 static int esp32s3_acm_probe(struct platform_device *pdev)
368 {
369 	struct device_node *np = pdev->dev.of_node;
370 	struct uart_port *port;
371 	struct resource *res;
372 	int ret;
373 
374 	port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
375 	if (!port)
376 		return -ENOMEM;
377 
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;
382 	}
383 	if (ret >= UART_NR) {
384 		dev_err(&pdev->dev, "driver limited to %d serial ports\n",
385 			UART_NR);
386 		return -ENOMEM;
387 	}
388 
389 	port->line = ret;
390 
391 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
392 	if (!res)
393 		return -ENODEV;
394 
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);
399 
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;
408 
409 	esp32s3_acm_ports[port->line] = port;
410 
411 	platform_set_drvdata(pdev, port);
412 
413 	return uart_add_one_port(&esp32s3_acm_reg, port);
414 }
415 
esp32s3_acm_remove(struct platform_device * pdev)416 static void esp32s3_acm_remove(struct platform_device *pdev)
417 {
418 	struct uart_port *port = platform_get_drvdata(pdev);
419 
420 	uart_remove_one_port(&esp32s3_acm_reg, port);
421 }
422 
423 
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,
430 	},
431 };
432 
esp32s3_acm_init(void)433 static int __init esp32s3_acm_init(void)
434 {
435 	int ret;
436 
437 	ret = uart_register_driver(&esp32s3_acm_reg);
438 	if (ret)
439 		return ret;
440 
441 	ret = platform_driver_register(&esp32s3_acm_driver);
442 	if (ret)
443 		uart_unregister_driver(&esp32s3_acm_reg);
444 
445 	return ret;
446 }
447 
esp32s3_acm_exit(void)448 static void __exit esp32s3_acm_exit(void)
449 {
450 	platform_driver_unregister(&esp32s3_acm_driver);
451 	uart_unregister_driver(&esp32s3_acm_reg);
452 }
453 
454 module_init(esp32s3_acm_init);
455 module_exit(esp32s3_acm_exit);
456 
457 MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
458 MODULE_DESCRIPTION("Espressif ESP32 USB ACM gadget support");
459 MODULE_LICENSE("GPL");
460