Linux Audio

Check our new training course

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