xref: /linux/drivers/tty/serial/apbuart.c (revision eacaf5ae747f7dead6cc268de17a7382d79031fc)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  *  Driver for GRLIB serial ports (APBUART)
4  *
5  *  Based on linux/drivers/serial/amba.c
6  *
7  *  Copyright (C) 2000 Deep Blue Solutions Ltd.
8  *  Copyright (C) 2003 Konrad Eisele <eiselekd@web.de>
9  *  Copyright (C) 2006 Daniel Hellstrom <daniel@gaisler.com>, Aeroflex Gaisler AB
10  *  Copyright (C) 2008 Gilead Kutnick <kutnickg@zin-tech.com>
11  *  Copyright (C) 2009 Kristoffer Glembo <kristoffer@gaisler.com>, Aeroflex Gaisler AB
12  */
13 
14 #include <linux/module.h>
15 #include <linux/tty.h>
16 #include <linux/tty_flip.h>
17 #include <linux/ioport.h>
18 #include <linux/init.h>
19 #include <linux/serial.h>
20 #include <linux/console.h>
21 #include <linux/sysrq.h>
22 #include <linux/kthread.h>
23 #include <linux/device.h>
24 #include <linux/of.h>
25 #include <linux/platform_device.h>
26 #include <linux/io.h>
27 #include <linux/serial_core.h>
28 #include <asm/irq.h>
29 
30 #include "apbuart.h"
31 
32 #define SERIAL_APBUART_MAJOR	TTY_MAJOR
33 #define SERIAL_APBUART_MINOR	64
34 #define UART_DUMMY_RSR_RX	0x8000	/* for ignore all read */
35 
36 static void apbuart_tx_chars(struct uart_port *port);
37 
38 static void apbuart_stop_tx(struct uart_port *port)
39 {
40 	unsigned int cr;
41 
42 	cr = UART_GET_CTRL(port);
43 	cr &= ~UART_CTRL_TI;
44 	UART_PUT_CTRL(port, cr);
45 }
46 
47 static void apbuart_start_tx(struct uart_port *port)
48 {
49 	unsigned int cr;
50 
51 	cr = UART_GET_CTRL(port);
52 	cr |= UART_CTRL_TI;
53 	UART_PUT_CTRL(port, cr);
54 
55 	if (UART_GET_STATUS(port) & UART_STATUS_THE)
56 		apbuart_tx_chars(port);
57 }
58 
59 static void apbuart_stop_rx(struct uart_port *port)
60 {
61 	unsigned int cr;
62 
63 	cr = UART_GET_CTRL(port);
64 	cr &= ~(UART_CTRL_RI);
65 	UART_PUT_CTRL(port, cr);
66 }
67 
68 static void apbuart_rx_chars(struct uart_port *port)
69 {
70 	unsigned int status, rsr;
71 	unsigned int max_chars = port->fifosize;
72 	u8 ch, flag;
73 
74 	status = UART_GET_STATUS(port);
75 
76 	while (UART_RX_DATA(status) && (max_chars--)) {
77 
78 		ch = UART_GET_CHAR(port);
79 		flag = TTY_NORMAL;
80 
81 		port->icount.rx++;
82 
83 		rsr = UART_GET_STATUS(port) | UART_DUMMY_RSR_RX;
84 		UART_PUT_STATUS(port, 0);
85 		if (rsr & UART_STATUS_ERR) {
86 
87 			if (rsr & UART_STATUS_BR) {
88 				rsr &= ~(UART_STATUS_FE | UART_STATUS_PE);
89 				port->icount.brk++;
90 				if (uart_handle_break(port))
91 					goto ignore_char;
92 			} else if (rsr & UART_STATUS_PE) {
93 				port->icount.parity++;
94 			} else if (rsr & UART_STATUS_FE) {
95 				port->icount.frame++;
96 			}
97 			if (rsr & UART_STATUS_OE)
98 				port->icount.overrun++;
99 
100 			rsr &= port->read_status_mask;
101 
102 			if (rsr & UART_STATUS_PE)
103 				flag = TTY_PARITY;
104 			else if (rsr & UART_STATUS_FE)
105 				flag = TTY_FRAME;
106 		}
107 
108 		if (uart_handle_sysrq_char(port, ch))
109 			goto ignore_char;
110 
111 		uart_insert_char(port, rsr, UART_STATUS_OE, ch, flag);
112 
113 
114 	      ignore_char:
115 		status = UART_GET_STATUS(port);
116 	}
117 
118 	tty_flip_buffer_push(&port->state->port);
119 }
120 
121 static void apbuart_tx_chars(struct uart_port *port)
122 {
123 	u8 ch;
124 
125 	uart_port_tx_limited(port, ch, port->fifosize,
126 		true,
127 		UART_PUT_CHAR(port, ch),
128 		({}));
129 }
130 
131 static irqreturn_t apbuart_int(int irq, void *dev_id)
132 {
133 	struct uart_port *port = dev_id;
134 	unsigned int status;
135 
136 	uart_port_lock(port);
137 
138 	status = UART_GET_STATUS(port);
139 	if (status & UART_STATUS_DR)
140 		apbuart_rx_chars(port);
141 	if (status & UART_STATUS_THE)
142 		apbuart_tx_chars(port);
143 
144 	uart_port_unlock(port);
145 
146 	return IRQ_HANDLED;
147 }
148 
149 static unsigned int apbuart_tx_empty(struct uart_port *port)
150 {
151 	unsigned int status = UART_GET_STATUS(port);
152 	return status & UART_STATUS_THE ? TIOCSER_TEMT : 0;
153 }
154 
155 static unsigned int apbuart_get_mctrl(struct uart_port *port)
156 {
157 	/* The GRLIB APBUART handles flow control in hardware */
158 	return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
159 }
160 
161 static void apbuart_set_mctrl(struct uart_port *port, unsigned int mctrl)
162 {
163 	/* The GRLIB APBUART handles flow control in hardware */
164 }
165 
166 static void apbuart_break_ctl(struct uart_port *port, int break_state)
167 {
168 	/* We don't support sending break */
169 }
170 
171 static int apbuart_startup(struct uart_port *port)
172 {
173 	int retval;
174 	unsigned int cr;
175 
176 	/* Allocate the IRQ */
177 	retval = request_irq(port->irq, apbuart_int, 0, "apbuart", port);
178 	if (retval)
179 		return retval;
180 
181 	/* Finally, enable interrupts */
182 	cr = UART_GET_CTRL(port);
183 	UART_PUT_CTRL(port,
184 		      cr | UART_CTRL_RE | UART_CTRL_TE |
185 		      UART_CTRL_RI | UART_CTRL_TI);
186 
187 	return 0;
188 }
189 
190 static void apbuart_shutdown(struct uart_port *port)
191 {
192 	unsigned int cr;
193 
194 	/* disable all interrupts, disable the port */
195 	cr = UART_GET_CTRL(port);
196 	UART_PUT_CTRL(port,
197 		      cr & ~(UART_CTRL_RE | UART_CTRL_TE |
198 			     UART_CTRL_RI | UART_CTRL_TI));
199 
200 	/* Free the interrupt */
201 	free_irq(port->irq, port);
202 }
203 
204 static void apbuart_set_termios(struct uart_port *port,
205 				struct ktermios *termios, const struct ktermios *old)
206 {
207 	unsigned int cr;
208 	unsigned long flags;
209 	unsigned int baud, quot;
210 
211 	/* Ask the core to calculate the divisor for us. */
212 	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk / 16);
213 
214 	/* uart_get_divisor calc a *16 uart freq, apbuart is *8 */
215 	quot = (uart_get_divisor(port, baud)) * 2;
216 	cr = UART_GET_CTRL(port);
217 	cr &= ~(UART_CTRL_PE | UART_CTRL_PS);
218 
219 	if (termios->c_cflag & PARENB) {
220 		cr |= UART_CTRL_PE;
221 		if ((termios->c_cflag & PARODD))
222 			cr |= UART_CTRL_PS;
223 	}
224 
225 	/* Enable flow control. */
226 	if (termios->c_cflag & CRTSCTS)
227 		cr |= UART_CTRL_FL;
228 
229 	uart_port_lock_irqsave(port, &flags);
230 
231 	/* Update the per-port timeout. */
232 	uart_update_timeout(port, termios->c_cflag, baud);
233 
234 	port->read_status_mask = UART_STATUS_OE;
235 	if (termios->c_iflag & INPCK)
236 		port->read_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
237 
238 	/* Characters to ignore */
239 	port->ignore_status_mask = 0;
240 	if (termios->c_iflag & IGNPAR)
241 		port->ignore_status_mask |= UART_STATUS_FE | UART_STATUS_PE;
242 
243 	/* Ignore all characters if CREAD is not set. */
244 	if ((termios->c_cflag & CREAD) == 0)
245 		port->ignore_status_mask |= UART_DUMMY_RSR_RX;
246 
247 	/* Set baud rate */
248 	quot -= 1;
249 	UART_PUT_SCAL(port, quot);
250 	UART_PUT_CTRL(port, cr);
251 
252 	uart_port_unlock_irqrestore(port, flags);
253 }
254 
255 static const char *apbuart_type(struct uart_port *port)
256 {
257 	return port->type == PORT_APBUART ? "GRLIB/APBUART" : NULL;
258 }
259 
260 static void apbuart_release_port(struct uart_port *port)
261 {
262 	release_mem_region(port->mapbase, 0x100);
263 }
264 
265 static int apbuart_request_port(struct uart_port *port)
266 {
267 	return request_mem_region(port->mapbase, 0x100, "grlib-apbuart")
268 	    != NULL ? 0 : -EBUSY;
269 	return 0;
270 }
271 
272 /* Configure/autoconfigure the port */
273 static void apbuart_config_port(struct uart_port *port, int flags)
274 {
275 	if (flags & UART_CONFIG_TYPE) {
276 		port->type = PORT_APBUART;
277 		apbuart_request_port(port);
278 	}
279 }
280 
281 /* Verify the new serial_struct (for TIOCSSERIAL) */
282 static int apbuart_verify_port(struct uart_port *port,
283 			       struct serial_struct *ser)
284 {
285 	int ret = 0;
286 	if (ser->type != PORT_UNKNOWN && ser->type != PORT_APBUART)
287 		ret = -EINVAL;
288 	if (ser->irq < 0 || ser->irq >= NR_IRQS)
289 		ret = -EINVAL;
290 	if (ser->baud_base < 9600)
291 		ret = -EINVAL;
292 	return ret;
293 }
294 
295 static const struct uart_ops grlib_apbuart_ops = {
296 	.tx_empty = apbuart_tx_empty,
297 	.set_mctrl = apbuart_set_mctrl,
298 	.get_mctrl = apbuart_get_mctrl,
299 	.stop_tx = apbuart_stop_tx,
300 	.start_tx = apbuart_start_tx,
301 	.stop_rx = apbuart_stop_rx,
302 	.break_ctl = apbuart_break_ctl,
303 	.startup = apbuart_startup,
304 	.shutdown = apbuart_shutdown,
305 	.set_termios = apbuart_set_termios,
306 	.type = apbuart_type,
307 	.release_port = apbuart_release_port,
308 	.request_port = apbuart_request_port,
309 	.config_port = apbuart_config_port,
310 	.verify_port = apbuart_verify_port,
311 };
312 
313 static struct uart_port grlib_apbuart_ports[UART_NR];
314 static struct device_node *grlib_apbuart_nodes[UART_NR];
315 
316 static int apbuart_scan_fifo_size(struct uart_port *port, int portnumber)
317 {
318 	int ctrl, loop = 0;
319 	int status;
320 	int fifosize;
321 	unsigned long flags;
322 
323 	ctrl = UART_GET_CTRL(port);
324 
325 	/*
326 	 * Enable the transceiver and wait for it to be ready to send data.
327 	 * Clear interrupts so that this process will not be externally
328 	 * interrupted in the middle (which can cause the transceiver to
329 	 * drain prematurely).
330 	 */
331 
332 	local_irq_save(flags);
333 
334 	UART_PUT_CTRL(port, ctrl | UART_CTRL_TE);
335 
336 	while (!UART_TX_READY(UART_GET_STATUS(port)))
337 		loop++;
338 
339 	/*
340 	 * Disable the transceiver so data isn't actually sent during the
341 	 * actual test.
342 	 */
343 
344 	UART_PUT_CTRL(port, ctrl & ~(UART_CTRL_TE));
345 
346 	fifosize = 1;
347 	UART_PUT_CHAR(port, 0);
348 
349 	/*
350 	 * So long as transmitting a character increments the tranceivier FIFO
351 	 * length the FIFO must be at least that big. These bytes will
352 	 * automatically drain off of the FIFO.
353 	 */
354 
355 	status = UART_GET_STATUS(port);
356 	while (((status >> 20) & 0x3F) == fifosize) {
357 		fifosize++;
358 		UART_PUT_CHAR(port, 0);
359 		status = UART_GET_STATUS(port);
360 	}
361 
362 	fifosize--;
363 
364 	UART_PUT_CTRL(port, ctrl);
365 	local_irq_restore(flags);
366 
367 	if (fifosize == 0)
368 		fifosize = 1;
369 
370 	return fifosize;
371 }
372 
373 static void apbuart_flush_fifo(struct uart_port *port)
374 {
375 	int i;
376 
377 	for (i = 0; i < port->fifosize; i++)
378 		UART_GET_CHAR(port);
379 }
380 
381 
382 /* ======================================================================== */
383 /* Console driver, if enabled                                               */
384 /* ======================================================================== */
385 
386 #ifdef CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE
387 
388 static void apbuart_console_putchar(struct uart_port *port, unsigned char ch)
389 {
390 	unsigned int status;
391 	do {
392 		status = UART_GET_STATUS(port);
393 	} while (!UART_TX_READY(status));
394 	UART_PUT_CHAR(port, ch);
395 }
396 
397 static void
398 apbuart_console_write(struct console *co, const char *s, unsigned int count)
399 {
400 	struct uart_port *port = &grlib_apbuart_ports[co->index];
401 	unsigned int status, old_cr, new_cr;
402 
403 	/* First save the CR then disable the interrupts */
404 	old_cr = UART_GET_CTRL(port);
405 	new_cr = old_cr & ~(UART_CTRL_RI | UART_CTRL_TI);
406 	UART_PUT_CTRL(port, new_cr);
407 
408 	uart_console_write(port, s, count, apbuart_console_putchar);
409 
410 	/*
411 	 *      Finally, wait for transmitter to become empty
412 	 *      and restore the TCR
413 	 */
414 	do {
415 		status = UART_GET_STATUS(port);
416 	} while (!UART_TX_READY(status));
417 	UART_PUT_CTRL(port, old_cr);
418 }
419 
420 static void __init
421 apbuart_console_get_options(struct uart_port *port, int *baud,
422 			    int *parity, int *bits)
423 {
424 	if (UART_GET_CTRL(port) & (UART_CTRL_RE | UART_CTRL_TE)) {
425 
426 		unsigned int quot, status;
427 		status = UART_GET_STATUS(port);
428 
429 		*parity = 'n';
430 		if (status & UART_CTRL_PE) {
431 			if ((status & UART_CTRL_PS) == 0)
432 				*parity = 'e';
433 			else
434 				*parity = 'o';
435 		}
436 
437 		*bits = 8;
438 		quot = UART_GET_SCAL(port) / 8;
439 		*baud = port->uartclk / (16 * (quot + 1));
440 	}
441 }
442 
443 static int __init apbuart_console_setup(struct console *co, char *options)
444 {
445 	struct uart_port *port;
446 	int baud = 38400;
447 	int bits = 8;
448 	int parity = 'n';
449 	int flow = 'n';
450 
451 	pr_debug("apbuart_console_setup co=%p, co->index=%i, options=%s\n",
452 		 co, co->index, options);
453 
454 	/*
455 	 * Check whether an invalid uart number has been specified, and
456 	 * if so, search for the first available port that does have
457 	 * console support.
458 	 */
459 	if (co->index >= grlib_apbuart_port_nr)
460 		co->index = 0;
461 
462 	port = &grlib_apbuart_ports[co->index];
463 
464 	spin_lock_init(&port->lock);
465 
466 	if (options)
467 		uart_parse_options(options, &baud, &parity, &bits, &flow);
468 	else
469 		apbuart_console_get_options(port, &baud, &parity, &bits);
470 
471 	return uart_set_options(port, co, baud, parity, bits, flow);
472 }
473 
474 static struct uart_driver grlib_apbuart_driver;
475 
476 static struct console grlib_apbuart_console = {
477 	.name = "ttyS",
478 	.write = apbuart_console_write,
479 	.device = uart_console_device,
480 	.setup = apbuart_console_setup,
481 	.flags = CON_PRINTBUFFER,
482 	.index = -1,
483 	.data = &grlib_apbuart_driver,
484 };
485 
486 
487 static int grlib_apbuart_configure(void);
488 
489 static int __init apbuart_console_init(void)
490 {
491 	if (grlib_apbuart_configure())
492 		return -ENODEV;
493 	register_console(&grlib_apbuart_console);
494 	return 0;
495 }
496 
497 console_initcall(apbuart_console_init);
498 
499 #define APBUART_CONSOLE	(&grlib_apbuart_console)
500 #else
501 #define APBUART_CONSOLE	NULL
502 #endif
503 
504 static struct uart_driver grlib_apbuart_driver = {
505 	.owner = THIS_MODULE,
506 	.driver_name = "serial_apbuart",
507 	.dev_name = "ttyS",
508 	.major = SERIAL_APBUART_MAJOR,
509 	.minor = SERIAL_APBUART_MINOR,
510 	.nr = UART_NR,
511 	.cons = APBUART_CONSOLE,
512 };
513 
514 
515 /* ======================================================================== */
516 /* OF Platform Driver                                                       */
517 /* ======================================================================== */
518 
519 static int apbuart_probe(struct platform_device *op)
520 {
521 	int i;
522 	struct uart_port *port = NULL;
523 
524 	for (i = 0; i < grlib_apbuart_port_nr; i++) {
525 		if (op->dev.of_node == grlib_apbuart_nodes[i])
526 			break;
527 	}
528 
529 	port = &grlib_apbuart_ports[i];
530 	port->dev = &op->dev;
531 	port->irq = op->archdata.irqs[0];
532 
533 	uart_add_one_port(&grlib_apbuart_driver, (struct uart_port *) port);
534 
535 	apbuart_flush_fifo((struct uart_port *) port);
536 
537 	printk(KERN_INFO "grlib-apbuart at 0x%llx, irq %d\n",
538 	       (unsigned long long) port->mapbase, port->irq);
539 	return 0;
540 }
541 
542 static const struct of_device_id apbuart_match[] = {
543 	{
544 	 .name = "GAISLER_APBUART",
545 	 },
546 	{
547 	 .name = "01_00c",
548 	 },
549 	{},
550 };
551 MODULE_DEVICE_TABLE(of, apbuart_match);
552 
553 static struct platform_driver grlib_apbuart_of_driver = {
554 	.probe = apbuart_probe,
555 	.driver = {
556 		.name = "grlib-apbuart",
557 		.of_match_table = apbuart_match,
558 	},
559 };
560 
561 
562 static int __init grlib_apbuart_configure(void)
563 {
564 	struct device_node *np;
565 	int line = 0;
566 
567 	for_each_matching_node(np, apbuart_match) {
568 		const int *ampopts;
569 		const u32 *freq_hz;
570 		const struct amba_prom_registers *regs;
571 		struct uart_port *port;
572 		unsigned long addr;
573 
574 		ampopts = of_get_property(np, "ampopts", NULL);
575 		if (ampopts && (*ampopts == 0))
576 			continue; /* Ignore if used by another OS instance */
577 		regs = of_get_property(np, "reg", NULL);
578 		/* Frequency of APB Bus is frequency of UART */
579 		freq_hz = of_get_property(np, "freq", NULL);
580 
581 		if (!regs || !freq_hz || (*freq_hz == 0))
582 			continue;
583 
584 		grlib_apbuart_nodes[line] = np;
585 
586 		addr = regs->phys_addr;
587 
588 		port = &grlib_apbuart_ports[line];
589 
590 		port->mapbase = addr;
591 		port->membase = ioremap(addr, sizeof(struct grlib_apbuart_regs_map));
592 		port->irq = 0;
593 		port->iotype = UPIO_MEM;
594 		port->ops = &grlib_apbuart_ops;
595 		port->has_sysrq = IS_ENABLED(CONFIG_SERIAL_GRLIB_GAISLER_APBUART_CONSOLE);
596 		port->flags = UPF_BOOT_AUTOCONF;
597 		port->line = line;
598 		port->uartclk = *freq_hz;
599 		port->fifosize = apbuart_scan_fifo_size((struct uart_port *) port, line);
600 		line++;
601 
602 		/* We support maximum UART_NR uarts ... */
603 		if (line == UART_NR)
604 			break;
605 	}
606 
607 	grlib_apbuart_driver.nr = grlib_apbuart_port_nr = line;
608 	return line ? 0 : -ENODEV;
609 }
610 
611 static int __init grlib_apbuart_init(void)
612 {
613 	int ret;
614 
615 	/* Find all APBUARTS in device the tree and initialize their ports */
616 	ret = grlib_apbuart_configure();
617 	if (ret)
618 		return ret;
619 
620 	printk(KERN_INFO "Serial: GRLIB APBUART driver\n");
621 
622 	ret = uart_register_driver(&grlib_apbuart_driver);
623 
624 	if (ret) {
625 		printk(KERN_ERR "%s: uart_register_driver failed (%i)\n",
626 		       __FILE__, ret);
627 		return ret;
628 	}
629 
630 	ret = platform_driver_register(&grlib_apbuart_of_driver);
631 	if (ret) {
632 		printk(KERN_ERR
633 		       "%s: platform_driver_register failed (%i)\n",
634 		       __FILE__, ret);
635 		uart_unregister_driver(&grlib_apbuart_driver);
636 		return ret;
637 	}
638 
639 	return ret;
640 }
641 
642 static void __exit grlib_apbuart_exit(void)
643 {
644 	int i;
645 
646 	for (i = 0; i < grlib_apbuart_port_nr; i++)
647 		uart_remove_one_port(&grlib_apbuart_driver,
648 				     &grlib_apbuart_ports[i]);
649 
650 	uart_unregister_driver(&grlib_apbuart_driver);
651 	platform_driver_unregister(&grlib_apbuart_of_driver);
652 }
653 
654 module_init(grlib_apbuart_init);
655 module_exit(grlib_apbuart_exit);
656 
657 MODULE_AUTHOR("Aeroflex Gaisler AB");
658 MODULE_DESCRIPTION("GRLIB APBUART serial driver");
659 MODULE_VERSION("2.1");
660 MODULE_LICENSE("GPL");
661