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