Linux Audio

Check our new training course

Loading...
Note: File does not exist in v6.2.
  1// SPDX-License-Identifier: GPL-2.0+
  2/*
  3 *  Driver for KS8695 serial ports
  4 *
  5 *  Based on drivers/serial/serial_amba.c, by Kam Lee.
  6 *
  7 *  Copyright 2002-2005 Micrel Inc.
  8 */
  9#include <linux/module.h>
 10#include <linux/tty.h>
 11#include <linux/tty_flip.h>
 12#include <linux/ioport.h>
 13#include <linux/init.h>
 14#include <linux/serial.h>
 15#include <linux/console.h>
 16#include <linux/sysrq.h>
 17#include <linux/device.h>
 18
 19#include <asm/io.h>
 20#include <asm/irq.h>
 21#include <asm/mach/irq.h>
 22
 23#include <mach/regs-uart.h>
 24#include <mach/regs-irq.h>
 25
 26#if defined(CONFIG_SERIAL_KS8695_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
 27#define SUPPORT_SYSRQ
 28#endif
 29
 30#include <linux/serial_core.h>
 31
 32
 33#define SERIAL_KS8695_MAJOR	204
 34#define SERIAL_KS8695_MINOR	16
 35#define SERIAL_KS8695_DEVNAME	"ttyAM"
 36
 37#define SERIAL_KS8695_NR	1
 38
 39/*
 40 * Access macros for the KS8695 UART
 41 */
 42#define UART_GET_CHAR(p)	(__raw_readl((p)->membase + KS8695_URRB) & 0xFF)
 43#define UART_PUT_CHAR(p, c)	__raw_writel((c), (p)->membase + KS8695_URTH)
 44#define UART_GET_FCR(p)		__raw_readl((p)->membase + KS8695_URFC)
 45#define UART_PUT_FCR(p, c)	__raw_writel((c), (p)->membase + KS8695_URFC)
 46#define UART_GET_MSR(p)		__raw_readl((p)->membase + KS8695_URMS)
 47#define UART_GET_LSR(p)		__raw_readl((p)->membase + KS8695_URLS)
 48#define UART_GET_LCR(p)		__raw_readl((p)->membase + KS8695_URLC)
 49#define UART_PUT_LCR(p, c)	__raw_writel((c), (p)->membase + KS8695_URLC)
 50#define UART_GET_MCR(p)		__raw_readl((p)->membase + KS8695_URMC)
 51#define UART_PUT_MCR(p, c)	__raw_writel((c), (p)->membase + KS8695_URMC)
 52#define UART_GET_BRDR(p)	__raw_readl((p)->membase + KS8695_URBD)
 53#define UART_PUT_BRDR(p, c)	__raw_writel((c), (p)->membase + KS8695_URBD)
 54
 55#define KS8695_CLR_TX_INT()	__raw_writel(1 << KS8695_IRQ_UART_TX, KS8695_IRQ_VA + KS8695_INTST)
 56
 57#define UART_DUMMY_LSR_RX	0x100
 58#define UART_PORT_SIZE		(KS8695_USR - KS8695_URRB + 4)
 59
 60static inline int tx_enabled(struct uart_port *port)
 61{
 62	return port->unused[0] & 1;
 63}
 64
 65static inline int rx_enabled(struct uart_port *port)
 66{
 67	return port->unused[0] & 2;
 68}
 69
 70static inline int ms_enabled(struct uart_port *port)
 71{
 72	return port->unused[0] & 4;
 73}
 74
 75static inline void ms_enable(struct uart_port *port, int enabled)
 76{
 77	if(enabled)
 78		port->unused[0] |= 4;
 79	else
 80		port->unused[0] &= ~4;
 81}
 82
 83static inline void rx_enable(struct uart_port *port, int enabled)
 84{
 85	if(enabled)
 86		port->unused[0] |= 2;
 87	else
 88		port->unused[0] &= ~2;
 89}
 90
 91static inline void tx_enable(struct uart_port *port, int enabled)
 92{
 93	if(enabled)
 94		port->unused[0] |= 1;
 95	else
 96		port->unused[0] &= ~1;
 97}
 98
 99
100#ifdef SUPPORT_SYSRQ
101static struct console ks8695_console;
102#endif
103
104static void ks8695uart_stop_tx(struct uart_port *port)
105{
106	if (tx_enabled(port)) {
107		/* use disable_irq_nosync() and not disable_irq() to avoid self
108		 * imposed deadlock by not waiting for irq handler to end,
109		 * since this ks8695uart_stop_tx() is called from interrupt context.
110		 */
111		disable_irq_nosync(KS8695_IRQ_UART_TX);
112		tx_enable(port, 0);
113	}
114}
115
116static void ks8695uart_start_tx(struct uart_port *port)
117{
118	if (!tx_enabled(port)) {
119		enable_irq(KS8695_IRQ_UART_TX);
120		tx_enable(port, 1);
121	}
122}
123
124static void ks8695uart_stop_rx(struct uart_port *port)
125{
126	if (rx_enabled(port)) {
127		disable_irq(KS8695_IRQ_UART_RX);
128		rx_enable(port, 0);
129	}
130}
131
132static void ks8695uart_enable_ms(struct uart_port *port)
133{
134	if (!ms_enabled(port)) {
135		enable_irq(KS8695_IRQ_UART_MODEM_STATUS);
136		ms_enable(port,1);
137	}
138}
139
140static void ks8695uart_disable_ms(struct uart_port *port)
141{
142	if (ms_enabled(port)) {
143		disable_irq(KS8695_IRQ_UART_MODEM_STATUS);
144		ms_enable(port,0);
145	}
146}
147
148static irqreturn_t ks8695uart_rx_chars(int irq, void *dev_id)
149{
150	struct uart_port *port = dev_id;
151	unsigned int status, ch, lsr, flg, max_count = 256;
152
153	status = UART_GET_LSR(port);		/* clears pending LSR interrupts */
154	while ((status & URLS_URDR) && max_count--) {
155		ch = UART_GET_CHAR(port);
156		flg = TTY_NORMAL;
157
158		port->icount.rx++;
159
160		/*
161		 * Note that the error handling code is
162		 * out of the main execution path
163		 */
164		lsr = UART_GET_LSR(port) | UART_DUMMY_LSR_RX;
165		if (unlikely(lsr & (URLS_URBI | URLS_URPE | URLS_URFE | URLS_URROE))) {
166			if (lsr & URLS_URBI) {
167				lsr &= ~(URLS_URFE | URLS_URPE);
168				port->icount.brk++;
169				if (uart_handle_break(port))
170					goto ignore_char;
171			}
172			if (lsr & URLS_URPE)
173				port->icount.parity++;
174			if (lsr & URLS_URFE)
175				port->icount.frame++;
176			if (lsr & URLS_URROE)
177				port->icount.overrun++;
178
179			lsr &= port->read_status_mask;
180
181			if (lsr & URLS_URBI)
182				flg = TTY_BREAK;
183			else if (lsr & URLS_URPE)
184				flg = TTY_PARITY;
185			else if (lsr & URLS_URFE)
186				flg = TTY_FRAME;
187		}
188
189		if (uart_handle_sysrq_char(port, ch))
190			goto ignore_char;
191
192		uart_insert_char(port, lsr, URLS_URROE, ch, flg);
193
194ignore_char:
195		status = UART_GET_LSR(port);
196	}
197	tty_flip_buffer_push(&port->state->port);
198
199	return IRQ_HANDLED;
200}
201
202
203static irqreturn_t ks8695uart_tx_chars(int irq, void *dev_id)
204{
205	struct uart_port *port = dev_id;
206	struct circ_buf *xmit = &port->state->xmit;
207	unsigned int count;
208
209	if (port->x_char) {
210		KS8695_CLR_TX_INT();
211		UART_PUT_CHAR(port, port->x_char);
212		port->icount.tx++;
213		port->x_char = 0;
214		return IRQ_HANDLED;
215	}
216
217	if (uart_tx_stopped(port) || uart_circ_empty(xmit)) {
218		ks8695uart_stop_tx(port);
219		return IRQ_HANDLED;
220	}
221
222	count = 16;	/* fifo size */
223	while (!uart_circ_empty(xmit) && (count-- > 0)) {
224		KS8695_CLR_TX_INT();
225		UART_PUT_CHAR(port, xmit->buf[xmit->tail]);
226
227		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
228		port->icount.tx++;
229	}
230
231	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
232		uart_write_wakeup(port);
233
234	if (uart_circ_empty(xmit))
235		ks8695uart_stop_tx(port);
236
237	return IRQ_HANDLED;
238}
239
240static irqreturn_t ks8695uart_modem_status(int irq, void *dev_id)
241{
242	struct uart_port *port = dev_id;
243	unsigned int status;
244
245	/*
246	 * clear modem interrupt by reading MSR
247	 */
248	status = UART_GET_MSR(port);
249
250	if (status & URMS_URDDCD)
251		uart_handle_dcd_change(port, status & URMS_URDDCD);
252
253	if (status & URMS_URDDST)
254		port->icount.dsr++;
255
256	if (status & URMS_URDCTS)
257		uart_handle_cts_change(port, status & URMS_URDCTS);
258
259	if (status & URMS_URTERI)
260		port->icount.rng++;
261
262	wake_up_interruptible(&port->state->port.delta_msr_wait);
263
264	return IRQ_HANDLED;
265}
266
267static unsigned int ks8695uart_tx_empty(struct uart_port *port)
268{
269	return (UART_GET_LSR(port) & URLS_URTE) ? TIOCSER_TEMT : 0;
270}
271
272static unsigned int ks8695uart_get_mctrl(struct uart_port *port)
273{
274	unsigned int result = 0;
275	unsigned int status;
276
277	status = UART_GET_MSR(port);
278	if (status & URMS_URDCD)
279		result |= TIOCM_CAR;
280	if (status & URMS_URDSR)
281		result |= TIOCM_DSR;
282	if (status & URMS_URCTS)
283		result |= TIOCM_CTS;
284	if (status & URMS_URRI)
285		result |= TIOCM_RI;
286
287	return result;
288}
289
290static void ks8695uart_set_mctrl(struct uart_port *port, u_int mctrl)
291{
292	unsigned int mcr;
293
294	mcr = UART_GET_MCR(port);
295	if (mctrl & TIOCM_RTS)
296		mcr |= URMC_URRTS;
297	else
298		mcr &= ~URMC_URRTS;
299
300	if (mctrl & TIOCM_DTR)
301		mcr |= URMC_URDTR;
302	else
303		mcr &= ~URMC_URDTR;
304
305	UART_PUT_MCR(port, mcr);
306}
307
308static void ks8695uart_break_ctl(struct uart_port *port, int break_state)
309{
310	unsigned int lcr;
311
312	lcr = UART_GET_LCR(port);
313
314	if (break_state == -1)
315		lcr |= URLC_URSBC;
316	else
317		lcr &= ~URLC_URSBC;
318
319	UART_PUT_LCR(port, lcr);
320}
321
322static int ks8695uart_startup(struct uart_port *port)
323{
324	int retval;
325
326	irq_modify_status(KS8695_IRQ_UART_TX, IRQ_NOREQUEST, IRQ_NOAUTOEN);
327	tx_enable(port, 0);
328	rx_enable(port, 1);
329	ms_enable(port, 1);
330
331	/*
332	 * Allocate the IRQ
333	 */
334	retval = request_irq(KS8695_IRQ_UART_TX, ks8695uart_tx_chars, 0, "UART TX", port);
335	if (retval)
336		goto err_tx;
337
338	retval = request_irq(KS8695_IRQ_UART_RX, ks8695uart_rx_chars, 0, "UART RX", port);
339	if (retval)
340		goto err_rx;
341
342	retval = request_irq(KS8695_IRQ_UART_LINE_STATUS, ks8695uart_rx_chars, 0, "UART LineStatus", port);
343	if (retval)
344		goto err_ls;
345
346	retval = request_irq(KS8695_IRQ_UART_MODEM_STATUS, ks8695uart_modem_status, 0, "UART ModemStatus", port);
347	if (retval)
348		goto err_ms;
349
350	return 0;
351
352err_ms:
353	free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
354err_ls:
355	free_irq(KS8695_IRQ_UART_RX, port);
356err_rx:
357	free_irq(KS8695_IRQ_UART_TX, port);
358err_tx:
359	return retval;
360}
361
362static void ks8695uart_shutdown(struct uart_port *port)
363{
364	/*
365	 * Free the interrupt
366	 */
367	free_irq(KS8695_IRQ_UART_RX, port);
368	free_irq(KS8695_IRQ_UART_TX, port);
369	free_irq(KS8695_IRQ_UART_MODEM_STATUS, port);
370	free_irq(KS8695_IRQ_UART_LINE_STATUS, port);
371
372	/* disable break condition and fifos */
373	UART_PUT_LCR(port, UART_GET_LCR(port) & ~URLC_URSBC);
374	UART_PUT_FCR(port, UART_GET_FCR(port) & ~URFC_URFE);
375}
376
377static void ks8695uart_set_termios(struct uart_port *port, struct ktermios *termios, struct ktermios *old)
378{
379	unsigned int lcr, fcr = 0;
380	unsigned long flags;
381	unsigned int baud, quot;
382
383	/*
384	 * Ask the core to calculate the divisor for us.
385	 */
386	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
387	quot = uart_get_divisor(port, baud);
388
389	switch (termios->c_cflag & CSIZE) {
390	case CS5:
391		lcr = URCL_5;
392		break;
393	case CS6:
394		lcr = URCL_6;
395		break;
396	case CS7:
397		lcr = URCL_7;
398		break;
399	default:
400		lcr = URCL_8;
401		break;
402	}
403
404	/* stop bits */
405	if (termios->c_cflag & CSTOPB)
406		lcr |= URLC_URSB;
407
408	/* parity */
409	if (termios->c_cflag & PARENB) {
410		if (termios->c_cflag & CMSPAR) {	/* Mark or Space parity */
411			if (termios->c_cflag & PARODD)
412				lcr |= URPE_MARK;
413			else
414				lcr |= URPE_SPACE;
415		}
416		else if (termios->c_cflag & PARODD)
417			lcr |= URPE_ODD;
418		else
419			lcr |= URPE_EVEN;
420	}
421
422	if (port->fifosize > 1)
423		fcr = URFC_URFRT_8 | URFC_URTFR | URFC_URRFR | URFC_URFE;
424
425	spin_lock_irqsave(&port->lock, flags);
426
427	/*
428	 * Update the per-port timeout.
429	 */
430	uart_update_timeout(port, termios->c_cflag, baud);
431
432	port->read_status_mask = URLS_URROE;
433	if (termios->c_iflag & INPCK)
434		port->read_status_mask |= (URLS_URFE | URLS_URPE);
435	if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
436		port->read_status_mask |= URLS_URBI;
437
438	/*
439	 * Characters to ignore
440	 */
441	port->ignore_status_mask = 0;
442	if (termios->c_iflag & IGNPAR)
443		port->ignore_status_mask |= (URLS_URFE | URLS_URPE);
444	if (termios->c_iflag & IGNBRK) {
445		port->ignore_status_mask |= URLS_URBI;
446		/*
447		 * If we're ignoring parity and break indicators,
448		 * ignore overruns too (for real raw support).
449		 */
450		if (termios->c_iflag & IGNPAR)
451			port->ignore_status_mask |= URLS_URROE;
452	}
453
454	/*
455	 * Ignore all characters if CREAD is not set.
456	 */
457	if ((termios->c_cflag & CREAD) == 0)
458		port->ignore_status_mask |= UART_DUMMY_LSR_RX;
459
460	/* first, disable everything */
461	if (UART_ENABLE_MS(port, termios->c_cflag))
462		ks8695uart_enable_ms(port);
463	else
464		ks8695uart_disable_ms(port);
465
466	/* Set baud rate */
467	UART_PUT_BRDR(port, quot);
468
469	UART_PUT_LCR(port, lcr);
470	UART_PUT_FCR(port, fcr);
471
472	spin_unlock_irqrestore(&port->lock, flags);
473}
474
475static const char *ks8695uart_type(struct uart_port *port)
476{
477	return port->type == PORT_KS8695 ? "KS8695" : NULL;
478}
479
480/*
481 * Release the memory region(s) being used by 'port'
482 */
483static void ks8695uart_release_port(struct uart_port *port)
484{
485	release_mem_region(port->mapbase, UART_PORT_SIZE);
486}
487
488/*
489 * Request the memory region(s) being used by 'port'
490 */
491static int ks8695uart_request_port(struct uart_port *port)
492{
493	return request_mem_region(port->mapbase, UART_PORT_SIZE,
494			"serial_ks8695") != NULL ? 0 : -EBUSY;
495}
496
497/*
498 * Configure/autoconfigure the port.
499 */
500static void ks8695uart_config_port(struct uart_port *port, int flags)
501{
502	if (flags & UART_CONFIG_TYPE) {
503		port->type = PORT_KS8695;
504		ks8695uart_request_port(port);
505	}
506}
507
508/*
509 * verify the new serial_struct (for TIOCSSERIAL).
510 */
511static int ks8695uart_verify_port(struct uart_port *port, struct serial_struct *ser)
512{
513	int ret = 0;
514
515	if (ser->type != PORT_UNKNOWN && ser->type != PORT_KS8695)
516		ret = -EINVAL;
517	if (ser->irq != port->irq)
518		ret = -EINVAL;
519	if (ser->baud_base < 9600)
520		ret = -EINVAL;
521	return ret;
522}
523
524static struct uart_ops ks8695uart_pops = {
525	.tx_empty	= ks8695uart_tx_empty,
526	.set_mctrl	= ks8695uart_set_mctrl,
527	.get_mctrl	= ks8695uart_get_mctrl,
528	.stop_tx	= ks8695uart_stop_tx,
529	.start_tx	= ks8695uart_start_tx,
530	.stop_rx	= ks8695uart_stop_rx,
531	.enable_ms	= ks8695uart_enable_ms,
532	.break_ctl	= ks8695uart_break_ctl,
533	.startup	= ks8695uart_startup,
534	.shutdown	= ks8695uart_shutdown,
535	.set_termios	= ks8695uart_set_termios,
536	.type		= ks8695uart_type,
537	.release_port	= ks8695uart_release_port,
538	.request_port	= ks8695uart_request_port,
539	.config_port	= ks8695uart_config_port,
540	.verify_port	= ks8695uart_verify_port,
541};
542
543static struct uart_port ks8695uart_ports[SERIAL_KS8695_NR] = {
544	{
545		.membase	= KS8695_UART_VA,
546		.mapbase	= KS8695_UART_PA,
547		.iotype		= SERIAL_IO_MEM,
548		.irq		= KS8695_IRQ_UART_TX,
549		.uartclk	= KS8695_CLOCK_RATE * 16,
550		.fifosize	= 16,
551		.ops		= &ks8695uart_pops,
552		.flags		= UPF_BOOT_AUTOCONF,
553		.line		= 0,
554	}
555};
556
557#ifdef CONFIG_SERIAL_KS8695_CONSOLE
558static void ks8695_console_putchar(struct uart_port *port, int ch)
559{
560	while (!(UART_GET_LSR(port) & URLS_URTHRE))
561		barrier();
562
563	UART_PUT_CHAR(port, ch);
564}
565
566static void ks8695_console_write(struct console *co, const char *s, u_int count)
567{
568	struct uart_port *port = ks8695uart_ports + co->index;
569
570	uart_console_write(port, s, count, ks8695_console_putchar);
571}
572
573static void __init ks8695_console_get_options(struct uart_port *port, int *baud, int *parity, int *bits)
574{
575	unsigned int lcr;
576
577	lcr = UART_GET_LCR(port);
578
579	switch (lcr & URLC_PARITY) {
580		case URPE_ODD:
581			*parity = 'o';
582			break;
583		case URPE_EVEN:
584			*parity = 'e';
585			break;
586		default:
587			*parity = 'n';
588	}
589
590	switch (lcr & URLC_URCL) {
591		case URCL_5:
592			*bits = 5;
593			break;
594		case URCL_6:
595			*bits = 6;
596			break;
597		case URCL_7:
598			*bits = 7;
599			break;
600		default:
601			*bits = 8;
602	}
603
604	*baud = port->uartclk / (UART_GET_BRDR(port) & 0x0FFF);
605	*baud /= 16;
606	*baud &= 0xFFFFFFF0;
607}
608
609static int __init ks8695_console_setup(struct console *co, char *options)
610{
611	struct uart_port *port;
612	int baud = 115200;
613	int bits = 8;
614	int parity = 'n';
615	int flow = 'n';
616
617	/*
618	 * Check whether an invalid uart number has been specified, and
619	 * if so, search for the first available port that does have
620	 * console support.
621	 */
622	port = uart_get_console(ks8695uart_ports, SERIAL_KS8695_NR, co);
623
624	if (options)
625		uart_parse_options(options, &baud, &parity, &bits, &flow);
626	else
627		ks8695_console_get_options(port, &baud, &parity, &bits);
628
629	return uart_set_options(port, co, baud, parity, bits, flow);
630}
631
632static struct uart_driver ks8695_reg;
633
634static struct console ks8695_console = {
635	.name		= SERIAL_KS8695_DEVNAME,
636	.write		= ks8695_console_write,
637	.device		= uart_console_device,
638	.setup		= ks8695_console_setup,
639	.flags		= CON_PRINTBUFFER,
640	.index		= -1,
641	.data		= &ks8695_reg,
642};
643
644static int __init ks8695_console_init(void)
645{
646	add_preferred_console(SERIAL_KS8695_DEVNAME, 0, NULL);
647	register_console(&ks8695_console);
648	return 0;
649}
650
651console_initcall(ks8695_console_init);
652
653#define KS8695_CONSOLE	&ks8695_console
654#else
655#define KS8695_CONSOLE	NULL
656#endif
657
658static struct uart_driver ks8695_reg = {
659	.owner			= THIS_MODULE,
660	.driver_name		= "serial_ks8695",
661	.dev_name		= SERIAL_KS8695_DEVNAME,
662	.major			= SERIAL_KS8695_MAJOR,
663	.minor			= SERIAL_KS8695_MINOR,
664	.nr			= SERIAL_KS8695_NR,
665	.cons			= KS8695_CONSOLE,
666};
667
668static int __init ks8695uart_init(void)
669{
670	int i, ret;
671
672	printk(KERN_INFO "Serial: Micrel KS8695 UART driver\n");
673
674	ret = uart_register_driver(&ks8695_reg);
675	if (ret)
676		return ret;
677
678	for (i = 0; i < SERIAL_KS8695_NR; i++)
679		uart_add_one_port(&ks8695_reg, &ks8695uart_ports[0]);
680
681	return 0;
682}
683
684static void __exit ks8695uart_exit(void)
685{
686	int i;
687
688	for (i = 0; i < SERIAL_KS8695_NR; i++)
689		uart_remove_one_port(&ks8695_reg, &ks8695uart_ports[0]);
690	uart_unregister_driver(&ks8695_reg);
691}
692
693module_init(ks8695uart_init);
694module_exit(ks8695uart_exit);
695
696MODULE_DESCRIPTION("KS8695 serial port driver");
697MODULE_AUTHOR("Micrel Inc.");
698MODULE_LICENSE("GPL");