summaryrefslogtreecommitdiffstats
path: root/drivers/tty/serial/esp32_acm.c
blob: d4e8bdb1cdef192b39c4af73f4073f3191844aa8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// SPDX-License-Identifier: GPL-2.0-or-later

#include <linux/bitfield.h>
#include <linux/bits.h>
#include <linux/console.h>
#include <linux/delay.h>
#include <linux/io.h>
#include <linux/irq.h>
#include <linux/module.h>
#include <linux/of.h>
#include <linux/platform_device.h>
#include <linux/serial_core.h>
#include <linux/slab.h>
#include <linux/tty_flip.h>
#include <asm/serial.h>

#define DRIVER_NAME	"esp32s3-acm"
#define DEV_NAME	"ttyGS"
#define UART_NR		4

#define ESP32S3_ACM_TX_FIFO_SIZE	64

#define USB_SERIAL_JTAG_EP1_REG		0x00
#define USB_SERIAL_JTAG_EP1_CONF_REG	0x04
#define USB_SERIAL_JTAG_WR_DONE				BIT(0)
#define USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE		BIT(1)
#define USB_SERIAL_JTAG_INT_ST_REG	0x0c
#define USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ST	BIT(2)
#define USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ST		BIT(3)
#define USB_SERIAL_JTAG_INT_ENA_REG	0x10
#define USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA	BIT(2)
#define USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA		BIT(3)
#define USB_SERIAL_JTAG_INT_CLR_REG	0x14
#define USB_SERIAL_JTAG_IN_EP1_ST_REG	0x2c
#define USB_SERIAL_JTAG_IN_EP1_WR_ADDR			GENMASK(8, 2)
#define USB_SERIAL_JTAG_OUT_EP1_ST_REG	0x3c
#define USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT		GENMASK(22, 16)

static const struct of_device_id esp32s3_acm_dt_ids[] = {
	{
		.compatible = "esp,esp32s3-acm",
	}, { /* sentinel */ }
};
MODULE_DEVICE_TABLE(of, esp32s3_acm_dt_ids);

static struct uart_port *esp32s3_acm_ports[UART_NR];

static void esp32s3_acm_write(struct uart_port *port, unsigned long reg, u32 v)
{
	writel(v, port->membase + reg);
}

static u32 esp32s3_acm_read(struct uart_port *port, unsigned long reg)
{
	return readl(port->membase + reg);
}

static u32 esp32s3_acm_tx_fifo_free(struct uart_port *port)
{
	u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_CONF_REG);

	return status & USB_SERIAL_JTAG_SERIAL_IN_EP_DATA_FREE;
}

static u32 esp32s3_acm_tx_fifo_cnt(struct uart_port *port)
{
	u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_IN_EP1_ST_REG);

	return FIELD_GET(USB_SERIAL_JTAG_IN_EP1_WR_ADDR, status);
}

static u32 esp32s3_acm_rx_fifo_cnt(struct uart_port *port)
{
	u32 status = esp32s3_acm_read(port, USB_SERIAL_JTAG_OUT_EP1_ST_REG);

	return FIELD_GET(USB_SERIAL_JTAG_OUT_EP1_REC_DATA_CNT, status);
}

/* return TIOCSER_TEMT when transmitter is not busy */
static unsigned int esp32s3_acm_tx_empty(struct uart_port *port)
{
	return esp32s3_acm_tx_fifo_cnt(port) == 0 ? TIOCSER_TEMT : 0;
}

static void esp32s3_acm_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
}

static unsigned int esp32s3_acm_get_mctrl(struct uart_port *port)
{
	return TIOCM_CAR;
}

static void esp32s3_acm_stop_tx(struct uart_port *port)
{
	u32 int_ena;

	int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
	int_ena &= ~USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA;
	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
}

static void esp32s3_acm_rxint(struct uart_port *port)
{
	struct tty_port *tty_port = &port->state->port;
	u32 rx_fifo_cnt = esp32s3_acm_rx_fifo_cnt(port);
	unsigned long flags;
	u32 i;

	if (!rx_fifo_cnt)
		return;

	spin_lock_irqsave(&port->lock, flags);

	for (i = 0; i < rx_fifo_cnt; ++i) {
		u32 rx = esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);

		++port->icount.rx;
		tty_insert_flip_char(tty_port, rx, TTY_NORMAL);
	}
	spin_unlock_irqrestore(&port->lock, flags);

	tty_flip_buffer_push(tty_port);
}

static void esp32s3_acm_push(struct uart_port *port)
{
	if (esp32s3_acm_tx_fifo_free(port))
		esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_CONF_REG,
				  USB_SERIAL_JTAG_WR_DONE);
}

static void esp32s3_acm_put_char(struct uart_port *port, u8 c)
{
	esp32s3_acm_write(port, USB_SERIAL_JTAG_EP1_REG, c);
}

static void esp32s3_acm_put_char_sync(struct uart_port *port, u8 c)
{
	unsigned long timeout = jiffies + HZ;

	while (!esp32s3_acm_tx_fifo_free(port)) {
		if (time_after(jiffies, timeout)) {
			dev_warn(port->dev, "timeout waiting for TX FIFO\n");
			return;
		}
		cpu_relax();
	}
	esp32s3_acm_put_char(port, c);
	esp32s3_acm_push(port);
}

static void esp32s3_acm_transmit_buffer(struct uart_port *port)
{
	u32 tx_fifo_used;
	unsigned int pending;
	u8 ch;

	if (!esp32s3_acm_tx_fifo_free(port))
		return;

	tx_fifo_used = esp32s3_acm_tx_fifo_cnt(port);
	pending = uart_port_tx_limited(port, ch,
				       ESP32S3_ACM_TX_FIFO_SIZE - tx_fifo_used,
				       true, esp32s3_acm_put_char(port, ch),
				       ({}));
	if (pending) {
		u32 int_ena;

		int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
		int_ena |= USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ENA;
		esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
	}
	esp32s3_acm_push(port);
}

static void esp32s3_acm_txint(struct uart_port *port)
{
	esp32s3_acm_transmit_buffer(port);
}

static irqreturn_t esp32s3_acm_int(int irq, void *dev_id)
{
	struct uart_port *port = dev_id;
	u32 status;

	status = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ST_REG);
	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_CLR_REG, status);

	if (status & USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ST)
		esp32s3_acm_rxint(port);
	if (status & USB_SERIAL_JTAG_SERIAL_IN_EMPTY_INT_ST)
		esp32s3_acm_txint(port);

	return IRQ_RETVAL(status);
}

static void esp32s3_acm_start_tx(struct uart_port *port)
{
	esp32s3_acm_transmit_buffer(port);
}

static void esp32s3_acm_stop_rx(struct uart_port *port)
{
	u32 int_ena;

	int_ena = esp32s3_acm_read(port, USB_SERIAL_JTAG_INT_ENA_REG);
	int_ena &= ~USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA;
	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, int_ena);
}

static int esp32s3_acm_startup(struct uart_port *port)
{
	int ret;

	ret = request_irq(port->irq, esp32s3_acm_int, 0, DRIVER_NAME, port);
	if (ret)
		return ret;
	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG,
			  USB_SERIAL_JTAG_SERIAL_OUT_RECV_PKT_INT_ENA);

	return 0;
}

static void esp32s3_acm_shutdown(struct uart_port *port)
{
	esp32s3_acm_write(port, USB_SERIAL_JTAG_INT_ENA_REG, 0);
	free_irq(port->irq, port);
}

static void esp32s3_acm_set_termios(struct uart_port *port,
				    struct ktermios *termios,
				    const struct ktermios *old)
{
}

static const char *esp32s3_acm_type(struct uart_port *port)
{
	return "ESP32S3 ACM";
}

/* configure/auto-configure the port */
static void esp32s3_acm_config_port(struct uart_port *port, int flags)
{
	if (flags & UART_CONFIG_TYPE)
		port->type = PORT_GENERIC;
}

#ifdef CONFIG_CONSOLE_POLL
static void esp32s3_acm_poll_put_char(struct uart_port *port, unsigned char c)
{
	esp32s3_acm_put_char_sync(port, c);
}

static int esp32s3_acm_poll_get_char(struct uart_port *port)
{
	if (esp32s3_acm_rx_fifo_cnt(port))
		return esp32s3_acm_read(port, USB_SERIAL_JTAG_EP1_REG);
	else
		return NO_POLL_CHAR;
}
#endif

static const struct uart_ops esp32s3_acm_pops = {
	.tx_empty	= esp32s3_acm_tx_empty,
	.set_mctrl	= esp32s3_acm_set_mctrl,
	.get_mctrl	= esp32s3_acm_get_mctrl,
	.stop_tx	= esp32s3_acm_stop_tx,
	.start_tx	= esp32s3_acm_start_tx,
	.stop_rx	= esp32s3_acm_stop_rx,
	.startup	= esp32s3_acm_startup,
	.shutdown	= esp32s3_acm_shutdown,
	.set_termios	= esp32s3_acm_set_termios,
	.type		= esp32s3_acm_type,
	.config_port	= esp32s3_acm_config_port,
#ifdef CONFIG_CONSOLE_POLL
	.poll_put_char	= esp32s3_acm_poll_put_char,
	.poll_get_char	= esp32s3_acm_poll_get_char,
#endif
};

static void esp32s3_acm_string_write(struct uart_port *port, const char *s,
				     unsigned int count)
{
	uart_console_write(port, s, count, esp32s3_acm_put_char_sync);
}

static void
esp32s3_acm_console_write(struct console *co, const char *s, unsigned int count)
{
	struct uart_port *port = esp32s3_acm_ports[co->index];
	unsigned long flags;
	bool locked = true;

	if (port->sysrq)
		locked = false;
	else if (oops_in_progress)
		locked = spin_trylock_irqsave(&port->lock, flags);
	else
		spin_lock_irqsave(&port->lock, flags);

	esp32s3_acm_string_write(port, s, count);

	if (locked)
		spin_unlock_irqrestore(&port->lock, flags);
}

static struct uart_driver esp32s3_acm_reg;
static struct console esp32s3_acm_console = {
	.name		= DEV_NAME,
	.write		= esp32s3_acm_console_write,
	.device		= uart_console_device,
	.flags		= CON_PRINTBUFFER,
	.index		= -1,
	.data		= &esp32s3_acm_reg,
};

static void esp32s3_acm_earlycon_write(struct console *con, const char *s,
				      unsigned int n)
{
	struct earlycon_device *dev = con->data;

	uart_console_write(&dev->port, s, n, esp32s3_acm_put_char_sync);
}

#ifdef CONFIG_CONSOLE_POLL
static int esp32s3_acm_earlycon_read(struct console *con, char *s, unsigned int n)
{
	struct earlycon_device *dev = con->data;
	unsigned int num_read = 0;

	while (num_read < n) {
		int c = esp32s3_acm_poll_get_char(&dev->port);

		if (c == NO_POLL_CHAR)
			break;
		s[num_read++] = c;
	}
	return num_read;
}
#endif

static int __init esp32s3_acm_early_console_setup(struct earlycon_device *device,
						   const char *options)
{
	if (!device->port.membase)
		return -ENODEV;

	device->con->write = esp32s3_acm_earlycon_write;
#ifdef CONFIG_CONSOLE_POLL
	device->con->read = esp32s3_acm_earlycon_read;
#endif
	return 0;
}

OF_EARLYCON_DECLARE(esp32s3acm, "esp,esp32s3-acm",
		    esp32s3_acm_early_console_setup);

static struct uart_driver esp32s3_acm_reg = {
	.owner		= THIS_MODULE,
	.driver_name	= DRIVER_NAME,
	.dev_name	= DEV_NAME,
	.nr		= ARRAY_SIZE(esp32s3_acm_ports),
	.cons		= &esp32s3_acm_console,
};

static int esp32s3_acm_probe(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
	struct uart_port *port;
	struct resource *res;
	int ret;

	port = devm_kzalloc(&pdev->dev, sizeof(*port), GFP_KERNEL);
	if (!port)
		return -ENOMEM;

	ret = of_alias_get_id(np, "serial");
	if (ret < 0) {
		dev_err(&pdev->dev, "failed to get alias id, errno %d\n", ret);
		return ret;
	}
	if (ret >= UART_NR) {
		dev_err(&pdev->dev, "driver limited to %d serial ports\n",
			UART_NR);
		return -ENOMEM;
	}

	port->line = ret;

	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res)
		return -ENODEV;

	port->mapbase = res->start;
	port->membase = devm_ioremap_resource(&pdev->dev, res);
	if (IS_ERR(port->membase))
		return PTR_ERR(port->membase);

	port->dev = &pdev->dev;
	port->type = PORT_GENERIC;
	port->iotype = UPIO_MEM;
	port->irq = platform_get_irq(pdev, 0);
	port->ops = &esp32s3_acm_pops;
	port->flags = UPF_BOOT_AUTOCONF;
	port->has_sysrq = 1;
	port->fifosize = ESP32S3_ACM_TX_FIFO_SIZE;

	esp32s3_acm_ports[port->line] = port;

	platform_set_drvdata(pdev, port);

	return uart_add_one_port(&esp32s3_acm_reg, port);
}

static void esp32s3_acm_remove(struct platform_device *pdev)
{
	struct uart_port *port = platform_get_drvdata(pdev);

	uart_remove_one_port(&esp32s3_acm_reg, port);
}


static struct platform_driver esp32s3_acm_driver = {
	.probe		= esp32s3_acm_probe,
	.remove_new	= esp32s3_acm_remove,
	.driver		= {
		.name	= DRIVER_NAME,
		.of_match_table	= esp32s3_acm_dt_ids,
	},
};

static int __init esp32s3_acm_init(void)
{
	int ret;

	ret = uart_register_driver(&esp32s3_acm_reg);
	if (ret)
		return ret;

	ret = platform_driver_register(&esp32s3_acm_driver);
	if (ret)
		uart_unregister_driver(&esp32s3_acm_reg);

	return ret;
}

static void __exit esp32s3_acm_exit(void)
{
	platform_driver_unregister(&esp32s3_acm_driver);
	uart_unregister_driver(&esp32s3_acm_reg);
}

module_init(esp32s3_acm_init);
module_exit(esp32s3_acm_exit);

MODULE_AUTHOR("Max Filippov <jcmvbkbc@gmail.com>");
MODULE_LICENSE("GPL");