Linux Audio

Check our new training course

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