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