xref: /openbmc/linux/drivers/tty/serial/liteuart.c (revision 5602cf99dcdcc0bf8f9a5979b7443fbe46686995)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * LiteUART serial controller (LiteX) Driver
4  *
5  * Copyright (C) 2019-2020 Antmicro <www.antmicro.com>
6  */
7 
8 #include <linux/bits.h>
9 #include <linux/console.h>
10 #include <linux/interrupt.h>
11 #include <linux/litex.h>
12 #include <linux/module.h>
13 #include <linux/of.h>
14 #include <linux/of_address.h>
15 #include <linux/of_platform.h>
16 #include <linux/serial.h>
17 #include <linux/serial_core.h>
18 #include <linux/slab.h>
19 #include <linux/timer.h>
20 #include <linux/tty_flip.h>
21 #include <linux/xarray.h>
22 
23 /*
24  * CSRs definitions (base address offsets + width)
25  *
26  * The definitions below are true for LiteX SoC configured for 8-bit CSR Bus,
27  * 32-bit aligned.
28  *
29  * Supporting other configurations might require new definitions or a more
30  * generic way of indexing the LiteX CSRs.
31  *
32  * For more details on how CSRs are defined and handled in LiteX, see comments
33  * in the LiteX SoC Driver: drivers/soc/litex/litex_soc_ctrl.c
34  */
35 #define OFF_RXTX	0x00
36 #define OFF_TXFULL	0x04
37 #define OFF_RXEMPTY	0x08
38 #define OFF_EV_STATUS	0x0c
39 #define OFF_EV_PENDING	0x10
40 #define OFF_EV_ENABLE	0x14
41 
42 /* events */
43 #define EV_TX		BIT(0)
44 #define EV_RX		BIT(1)
45 
46 struct liteuart_port {
47 	struct uart_port port;
48 	struct timer_list timer;
49 	u32 id;
50 	u8 irq_reg;
51 };
52 
53 #define to_liteuart_port(port)	container_of(port, struct liteuart_port, port)
54 
55 static DEFINE_XARRAY_FLAGS(liteuart_array, XA_FLAGS_ALLOC);
56 
57 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
58 static struct console liteuart_console;
59 #endif
60 
61 static struct uart_driver liteuart_driver = {
62 	.owner = THIS_MODULE,
63 	.driver_name = KBUILD_MODNAME,
64 	.dev_name = "ttyLXU",
65 	.major = 0,
66 	.minor = 0,
67 	.nr = CONFIG_SERIAL_LITEUART_MAX_PORTS,
68 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
69 	.cons = &liteuart_console,
70 #endif
71 };
72 
73 static void liteuart_putchar(struct uart_port *port, unsigned char ch)
74 {
75 	while (litex_read8(port->membase + OFF_TXFULL))
76 		cpu_relax();
77 
78 	litex_write8(port->membase + OFF_RXTX, ch);
79 }
80 
81 static void liteuart_update_irq_reg(struct uart_port *port, bool set, u8 mask)
82 {
83 	struct liteuart_port *uart = to_liteuart_port(port);
84 
85 	if (set)
86 		uart->irq_reg |= mask;
87 	else
88 		uart->irq_reg &= ~mask;
89 
90 	if (port->irq)
91 		litex_write8(port->membase + OFF_EV_ENABLE, uart->irq_reg);
92 }
93 
94 static void liteuart_stop_tx(struct uart_port *port)
95 {
96 }
97 
98 static void liteuart_start_tx(struct uart_port *port)
99 {
100 	struct circ_buf *xmit = &port->state->xmit;
101 	unsigned char ch;
102 
103 	if (unlikely(port->x_char)) {
104 		litex_write8(port->membase + OFF_RXTX, port->x_char);
105 		port->icount.tx++;
106 		port->x_char = 0;
107 	} else if (!uart_circ_empty(xmit)) {
108 		while (xmit->head != xmit->tail) {
109 			ch = xmit->buf[xmit->tail];
110 			uart_xmit_advance(port, 1);
111 			liteuart_putchar(port, ch);
112 		}
113 	}
114 
115 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
116 		uart_write_wakeup(port);
117 }
118 
119 static void liteuart_stop_rx(struct uart_port *port)
120 {
121 	struct liteuart_port *uart = to_liteuart_port(port);
122 
123 	/* just delete timer */
124 	del_timer(&uart->timer);
125 }
126 
127 static void liteuart_rx_chars(struct uart_port *port)
128 {
129 	unsigned char __iomem *membase = port->membase;
130 	u8 ch;
131 
132 	while (!litex_read8(membase + OFF_RXEMPTY)) {
133 		ch = litex_read8(membase + OFF_RXTX);
134 		port->icount.rx++;
135 
136 		/* necessary for RXEMPTY to refresh its value */
137 		litex_write8(membase + OFF_EV_PENDING, EV_RX);
138 
139 		/* no overflow bits in status */
140 		if (!(uart_handle_sysrq_char(port, ch)))
141 			uart_insert_char(port, 1, 0, ch, TTY_NORMAL);
142 	}
143 
144 	tty_flip_buffer_push(&port->state->port);
145 }
146 
147 static irqreturn_t liteuart_interrupt(int irq, void *data)
148 {
149 	struct liteuart_port *uart = data;
150 	struct uart_port *port = &uart->port;
151 	unsigned long flags;
152 	u8 isr;
153 
154 	/*
155 	 * if polling, the context would be "in_serving_softirq", so use
156 	 * irq[save|restore] spin_lock variants to cover all possibilities
157 	 */
158 	spin_lock_irqsave(&port->lock, flags);
159 	isr = litex_read8(port->membase + OFF_EV_PENDING) & uart->irq_reg;
160 	if (isr & EV_RX)
161 		liteuart_rx_chars(port);
162 	spin_unlock_irqrestore(&port->lock, flags);
163 
164 	return IRQ_RETVAL(isr);
165 }
166 
167 static void liteuart_timer(struct timer_list *t)
168 {
169 	struct liteuart_port *uart = from_timer(uart, t, timer);
170 	struct uart_port *port = &uart->port;
171 
172 	liteuart_interrupt(0, port);
173 	mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
174 }
175 
176 static unsigned int liteuart_tx_empty(struct uart_port *port)
177 {
178 	/* not really tx empty, just checking if tx is not full */
179 	if (!litex_read8(port->membase + OFF_TXFULL))
180 		return TIOCSER_TEMT;
181 
182 	return 0;
183 }
184 
185 static void liteuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
186 {
187 	/* modem control register is not present in LiteUART */
188 }
189 
190 static unsigned int liteuart_get_mctrl(struct uart_port *port)
191 {
192 	return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
193 }
194 
195 static int liteuart_startup(struct uart_port *port)
196 {
197 	struct liteuart_port *uart = to_liteuart_port(port);
198 	unsigned long flags;
199 	int ret;
200 
201 	if (port->irq) {
202 		ret = request_irq(port->irq, liteuart_interrupt, 0,
203 				  KBUILD_MODNAME, uart);
204 		if (ret) {
205 			dev_warn(port->dev,
206 				"line %d irq %d failed: switch to polling\n",
207 				port->line, port->irq);
208 			port->irq = 0;
209 		}
210 	}
211 
212 	spin_lock_irqsave(&port->lock, flags);
213 	/* only enabling rx irqs during startup */
214 	liteuart_update_irq_reg(port, true, EV_RX);
215 	spin_unlock_irqrestore(&port->lock, flags);
216 
217 	if (!port->irq) {
218 		timer_setup(&uart->timer, liteuart_timer, 0);
219 		mod_timer(&uart->timer, jiffies + uart_poll_timeout(port));
220 	}
221 
222 	return 0;
223 }
224 
225 static void liteuart_shutdown(struct uart_port *port)
226 {
227 	struct liteuart_port *uart = to_liteuart_port(port);
228 	unsigned long flags;
229 
230 	spin_lock_irqsave(&port->lock, flags);
231 	liteuart_update_irq_reg(port, false, EV_RX | EV_TX);
232 	spin_unlock_irqrestore(&port->lock, flags);
233 
234 	if (port->irq)
235 		free_irq(port->irq, port);
236 	else
237 		del_timer_sync(&uart->timer);
238 }
239 
240 static void liteuart_set_termios(struct uart_port *port, struct ktermios *new,
241 				 const struct ktermios *old)
242 {
243 	unsigned int baud;
244 	unsigned long flags;
245 
246 	spin_lock_irqsave(&port->lock, flags);
247 
248 	/* update baudrate */
249 	baud = uart_get_baud_rate(port, new, old, 0, 460800);
250 	uart_update_timeout(port, new->c_cflag, baud);
251 
252 	spin_unlock_irqrestore(&port->lock, flags);
253 }
254 
255 static const char *liteuart_type(struct uart_port *port)
256 {
257 	return "liteuart";
258 }
259 
260 static void liteuart_config_port(struct uart_port *port, int flags)
261 {
262 	/*
263 	 * Driver core for serial ports forces a non-zero value for port type.
264 	 * Write an arbitrary value here to accommodate the serial core driver,
265 	 * as ID part of UAPI is redundant.
266 	 */
267 	port->type = 1;
268 }
269 
270 static int liteuart_verify_port(struct uart_port *port,
271 				struct serial_struct *ser)
272 {
273 	if (port->type != PORT_UNKNOWN && ser->type != 1)
274 		return -EINVAL;
275 
276 	return 0;
277 }
278 
279 static const struct uart_ops liteuart_ops = {
280 	.tx_empty	= liteuart_tx_empty,
281 	.set_mctrl	= liteuart_set_mctrl,
282 	.get_mctrl	= liteuart_get_mctrl,
283 	.stop_tx	= liteuart_stop_tx,
284 	.start_tx	= liteuart_start_tx,
285 	.stop_rx	= liteuart_stop_rx,
286 	.startup	= liteuart_startup,
287 	.shutdown	= liteuart_shutdown,
288 	.set_termios	= liteuart_set_termios,
289 	.type		= liteuart_type,
290 	.config_port	= liteuart_config_port,
291 	.verify_port	= liteuart_verify_port,
292 };
293 
294 static int liteuart_probe(struct platform_device *pdev)
295 {
296 	struct liteuart_port *uart;
297 	struct uart_port *port;
298 	struct xa_limit limit;
299 	int dev_id, ret;
300 
301 	/* look for aliases; auto-enumerate for free index if not found */
302 	dev_id = of_alias_get_id(pdev->dev.of_node, "serial");
303 	if (dev_id < 0)
304 		limit = XA_LIMIT(0, CONFIG_SERIAL_LITEUART_MAX_PORTS);
305 	else
306 		limit = XA_LIMIT(dev_id, dev_id);
307 
308 	uart = devm_kzalloc(&pdev->dev, sizeof(struct liteuart_port), GFP_KERNEL);
309 	if (!uart)
310 		return -ENOMEM;
311 
312 	ret = xa_alloc(&liteuart_array, &dev_id, uart, limit, GFP_KERNEL);
313 	if (ret)
314 		return ret;
315 
316 	uart->id = dev_id;
317 	port = &uart->port;
318 
319 	/* get membase */
320 	port->membase = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
321 	if (IS_ERR(port->membase)) {
322 		ret = PTR_ERR(port->membase);
323 		goto err_erase_id;
324 	}
325 
326 	ret = platform_get_irq_optional(pdev, 0);
327 	if (ret < 0 && ret != -ENXIO)
328 		return ret;
329 	if (ret > 0)
330 		port->irq = ret;
331 
332 	/* values not from device tree */
333 	port->dev = &pdev->dev;
334 	port->iotype = UPIO_MEM;
335 	port->flags = UPF_BOOT_AUTOCONF;
336 	port->ops = &liteuart_ops;
337 	port->fifosize = 16;
338 	port->type = PORT_UNKNOWN;
339 	port->line = dev_id;
340 	spin_lock_init(&port->lock);
341 
342 	platform_set_drvdata(pdev, port);
343 
344 	ret = uart_add_one_port(&liteuart_driver, &uart->port);
345 	if (ret)
346 		goto err_erase_id;
347 
348 	return 0;
349 
350 err_erase_id:
351 	xa_erase(&liteuart_array, uart->id);
352 
353 	return ret;
354 }
355 
356 static int liteuart_remove(struct platform_device *pdev)
357 {
358 	struct uart_port *port = platform_get_drvdata(pdev);
359 	struct liteuart_port *uart = to_liteuart_port(port);
360 
361 	uart_remove_one_port(&liteuart_driver, port);
362 	xa_erase(&liteuart_array, uart->id);
363 
364 	return 0;
365 }
366 
367 static const struct of_device_id liteuart_of_match[] = {
368 	{ .compatible = "litex,liteuart" },
369 	{}
370 };
371 MODULE_DEVICE_TABLE(of, liteuart_of_match);
372 
373 static struct platform_driver liteuart_platform_driver = {
374 	.probe = liteuart_probe,
375 	.remove = liteuart_remove,
376 	.driver = {
377 		.name = KBUILD_MODNAME,
378 		.of_match_table = liteuart_of_match,
379 	},
380 };
381 
382 #ifdef CONFIG_SERIAL_LITEUART_CONSOLE
383 
384 static void liteuart_console_write(struct console *co, const char *s,
385 	unsigned int count)
386 {
387 	struct liteuart_port *uart;
388 	struct uart_port *port;
389 	unsigned long flags;
390 
391 	uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
392 	port = &uart->port;
393 
394 	spin_lock_irqsave(&port->lock, flags);
395 	uart_console_write(port, s, count, liteuart_putchar);
396 	spin_unlock_irqrestore(&port->lock, flags);
397 }
398 
399 static int liteuart_console_setup(struct console *co, char *options)
400 {
401 	struct liteuart_port *uart;
402 	struct uart_port *port;
403 	int baud = 115200;
404 	int bits = 8;
405 	int parity = 'n';
406 	int flow = 'n';
407 
408 	uart = (struct liteuart_port *)xa_load(&liteuart_array, co->index);
409 	if (!uart)
410 		return -ENODEV;
411 
412 	port = &uart->port;
413 	if (!port->membase)
414 		return -ENODEV;
415 
416 	if (options)
417 		uart_parse_options(options, &baud, &parity, &bits, &flow);
418 
419 	return uart_set_options(port, co, baud, parity, bits, flow);
420 }
421 
422 static struct console liteuart_console = {
423 	.name = KBUILD_MODNAME,
424 	.write = liteuart_console_write,
425 	.device = uart_console_device,
426 	.setup = liteuart_console_setup,
427 	.flags = CON_PRINTBUFFER,
428 	.index = -1,
429 	.data = &liteuart_driver,
430 };
431 
432 static int __init liteuart_console_init(void)
433 {
434 	register_console(&liteuart_console);
435 
436 	return 0;
437 }
438 console_initcall(liteuart_console_init);
439 
440 static void early_liteuart_write(struct console *console, const char *s,
441 				    unsigned int count)
442 {
443 	struct earlycon_device *device = console->data;
444 	struct uart_port *port = &device->port;
445 
446 	uart_console_write(port, s, count, liteuart_putchar);
447 }
448 
449 static int __init early_liteuart_setup(struct earlycon_device *device,
450 				       const char *options)
451 {
452 	if (!device->port.membase)
453 		return -ENODEV;
454 
455 	device->con->write = early_liteuart_write;
456 	return 0;
457 }
458 
459 OF_EARLYCON_DECLARE(liteuart, "litex,liteuart", early_liteuart_setup);
460 #endif /* CONFIG_SERIAL_LITEUART_CONSOLE */
461 
462 static int __init liteuart_init(void)
463 {
464 	int res;
465 
466 	res = uart_register_driver(&liteuart_driver);
467 	if (res)
468 		return res;
469 
470 	res = platform_driver_register(&liteuart_platform_driver);
471 	if (res)
472 		uart_unregister_driver(&liteuart_driver);
473 
474 	return res;
475 }
476 
477 static void __exit liteuart_exit(void)
478 {
479 	platform_driver_unregister(&liteuart_platform_driver);
480 	uart_unregister_driver(&liteuart_driver);
481 }
482 
483 module_init(liteuart_init);
484 module_exit(liteuart_exit);
485 
486 MODULE_AUTHOR("Antmicro <www.antmicro.com>");
487 MODULE_DESCRIPTION("LiteUART serial driver");
488 MODULE_LICENSE("GPL v2");
489 MODULE_ALIAS("platform:liteuart");
490