m32r_sio.c

来自「linux 内核源代码」· C语言 代码 · 共 1,193 行 · 第 1/2 页

C
1,193
字号
/* *  m32r_sio.c * *  Driver for M32R serial ports * *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. *  Based on drivers/serial/8250.c. * *  Copyright (C) 2001  Russell King. *  Copyright (C) 2004  Hirokazu Takata <takata at linux-m32r.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. *//* * A note about mapbase / membase * *  mapbase is the physical address of the IO port.  Currently, we don't *  support this very well, and it may well be dropped from this driver *  in future.  As such, mapbase should be NULL. * *  membase is an 'ioremapped' cookie.  This is compatible with the old *  serial.c driver, and is currently the preferred form. */#if defined(CONFIG_SERIAL_M32R_SIO_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)#define SUPPORT_SYSRQ#endif#include <linux/module.h>#include <linux/tty.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/console.h>#include <linux/sysrq.h>#include <linux/serial.h>#include <linux/serialP.h>#include <linux/delay.h>#include <asm/m32r.h>#include <asm/io.h>#include <asm/irq.h>#define PORT_M32R_BASE	PORT_M32R_SIO#define PORT_INDEX(x)	(x - PORT_M32R_BASE + 1)#define BAUD_RATE	115200#include <linux/serial_core.h>#include "m32r_sio.h"#include "m32r_sio_reg.h"/* * Debugging. */#if 0#define DEBUG_AUTOCONF(fmt...)	printk(fmt)#else#define DEBUG_AUTOCONF(fmt...)	do { } while (0)#endif#if 0#define DEBUG_INTR(fmt...)	printk(fmt)#else#define DEBUG_INTR(fmt...)	do { } while (0)#endif#define PASS_LIMIT	256/* * We default to IRQ0 for the "no irq" hack.   Some * machine types want others as well - they're free * to redefine this in their header file. */#define is_real_interrupt(irq)	((irq) != 0)#define BASE_BAUD	115200/* Standard COM flags */#define STD_COM_FLAGS (UPF_BOOT_AUTOCONF | UPF_SKIP_TEST)/* * SERIAL_PORT_DFNS tells us about built-in ports that have no * standard enumeration mechanism.   Platforms that can find all * serial ports via mechanisms like ACPI or PCI need not supply it. */#if defined(CONFIG_PLAT_USRV)#define SERIAL_PORT_DFNS						\       /* UART  CLK     PORT   IRQ            FLAGS */			\	{ 0, BASE_BAUD, 0x3F8, PLD_IRQ_UART0, STD_COM_FLAGS }, /* ttyS0 */ \	{ 0, BASE_BAUD, 0x2F8, PLD_IRQ_UART1, STD_COM_FLAGS }, /* ttyS1 */#else /* !CONFIG_PLAT_USRV */#if defined(CONFIG_SERIAL_M32R_PLDSIO)#define SERIAL_PORT_DFNS						\	{ 0, BASE_BAUD, ((unsigned long)PLD_ESIO0CR), PLD_IRQ_SIO0_RCV,	\	  STD_COM_FLAGS }, /* ttyS0 */#else#define SERIAL_PORT_DFNS						\	{ 0, BASE_BAUD, M32R_SIO_OFFSET, M32R_IRQ_SIO0_R,		\	  STD_COM_FLAGS }, /* ttyS0 */#endif#endif /* !CONFIG_PLAT_USRV */static struct old_serial_port old_serial_port[] = {	SERIAL_PORT_DFNS};#define UART_NR	ARRAY_SIZE(old_serial_port)struct uart_sio_port {	struct uart_port	port;	struct timer_list	timer;		/* "no irq" timer */	struct list_head	list;		/* ports on this IRQ */	unsigned short		rev;	unsigned char		acr;	unsigned char		ier;	unsigned char		lcr;	unsigned char		mcr_mask;	/* mask of user bits */	unsigned char		mcr_force;	/* mask of forced bits */	unsigned char		lsr_break_flag;	/*	 * We provide a per-port pm hook.	 */	void			(*pm)(struct uart_port *port,				      unsigned int state, unsigned int old);};struct irq_info {	spinlock_t		lock;	struct list_head	*head;};static struct irq_info irq_lists[NR_IRQS];/* * Here we define the default xmit fifo size used for each type of UART. */static const struct serial_uart_config uart_config[] = {	[PORT_UNKNOWN] = {		.name			= "unknown",		.dfl_xmit_fifo_size	= 1,		.flags			= 0,	},	[PORT_INDEX(PORT_M32R_SIO)] = {		.name			= "M32RSIO",		.dfl_xmit_fifo_size	= 1,		.flags			= 0,	},};#ifdef CONFIG_SERIAL_M32R_PLDSIO#define __sio_in(x) inw((unsigned long)(x))#define __sio_out(v,x) outw((v),(unsigned long)(x))static inline void sio_set_baud_rate(unsigned long baud){	unsigned short sbaud;	sbaud = (boot_cpu_data.bus_clock / (baud * 4))-1;	__sio_out(sbaud, PLD_ESIO0BAUR);}static void sio_reset(void){	unsigned short tmp;	tmp = __sio_in(PLD_ESIO0RXB);	tmp = __sio_in(PLD_ESIO0RXB);	tmp = __sio_in(PLD_ESIO0CR);	sio_set_baud_rate(BAUD_RATE);	__sio_out(0x0300, PLD_ESIO0CR);	__sio_out(0x0003, PLD_ESIO0CR);}static void sio_init(void){	unsigned short tmp;	tmp = __sio_in(PLD_ESIO0RXB);	tmp = __sio_in(PLD_ESIO0RXB);	tmp = __sio_in(PLD_ESIO0CR);	__sio_out(0x0300, PLD_ESIO0CR);	__sio_out(0x0003, PLD_ESIO0CR);}static void sio_error(int *status){	printk("SIO0 error[%04x]\n", *status);	do {		sio_init();	} while ((*status = __sio_in(PLD_ESIO0CR)) != 3);}#else /* not CONFIG_SERIAL_M32R_PLDSIO */#define __sio_in(x) inl(x)#define __sio_out(v,x) outl((v),(x))static inline void sio_set_baud_rate(unsigned long baud){	unsigned long i, j;	i = boot_cpu_data.bus_clock / (baud * 16);	j = (boot_cpu_data.bus_clock - (i * baud * 16)) / baud;	i -= 1;	j = (j + 1) >> 1;	__sio_out(i, M32R_SIO0_BAUR_PORTL);	__sio_out(j, M32R_SIO0_RBAUR_PORTL);}static void sio_reset(void){	__sio_out(0x00000300, M32R_SIO0_CR_PORTL);	/* init status */	__sio_out(0x00000800, M32R_SIO0_MOD1_PORTL);	/* 8bit        */	__sio_out(0x00000080, M32R_SIO0_MOD0_PORTL);	/* 1stop non   */	sio_set_baud_rate(BAUD_RATE);	__sio_out(0x00000000, M32R_SIO0_TRCR_PORTL);	__sio_out(0x00000003, M32R_SIO0_CR_PORTL);	/* RXCEN */}static void sio_init(void){	unsigned int tmp;	tmp = __sio_in(M32R_SIO0_RXB_PORTL);	tmp = __sio_in(M32R_SIO0_RXB_PORTL);	tmp = __sio_in(M32R_SIO0_STS_PORTL);	__sio_out(0x00000003, M32R_SIO0_CR_PORTL);}static void sio_error(int *status){	printk("SIO0 error[%04x]\n", *status);	do {		sio_init();	} while ((*status = __sio_in(M32R_SIO0_CR_PORTL)) != 3);}#endif /* CONFIG_SERIAL_M32R_PLDSIO */static unsigned int sio_in(struct uart_sio_port *up, int offset){	return __sio_in(up->port.iobase + offset);}static void sio_out(struct uart_sio_port *up, int offset, int value){	__sio_out(value, up->port.iobase + offset);}static unsigned int serial_in(struct uart_sio_port *up, int offset){	if (!offset)		return 0;	return __sio_in(offset);}static void serial_out(struct uart_sio_port *up, int offset, int value){	if (!offset)		return;	__sio_out(value, offset);}static void m32r_sio_stop_tx(struct uart_port *port){	struct uart_sio_port *up = (struct uart_sio_port *)port;	if (up->ier & UART_IER_THRI) {		up->ier &= ~UART_IER_THRI;		serial_out(up, UART_IER, up->ier);	}}static void m32r_sio_start_tx(struct uart_port *port){#ifdef CONFIG_SERIAL_M32R_PLDSIO	struct uart_sio_port *up = (struct uart_sio_port *)port;	struct circ_buf *xmit = &up->port.info->xmit;	if (!(up->ier & UART_IER_THRI)) {		up->ier |= UART_IER_THRI;		serial_out(up, UART_IER, up->ier);		serial_out(up, UART_TX, xmit->buf[xmit->tail]);		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);		up->port.icount.tx++;	}	while((serial_in(up, UART_LSR) & UART_EMPTY) != UART_EMPTY);#else	struct uart_sio_port *up = (struct uart_sio_port *)port;	if (!(up->ier & UART_IER_THRI)) {		up->ier |= UART_IER_THRI;		serial_out(up, UART_IER, up->ier);	}#endif}static void m32r_sio_stop_rx(struct uart_port *port){	struct uart_sio_port *up = (struct uart_sio_port *)port;	up->ier &= ~UART_IER_RLSI;	up->port.read_status_mask &= ~UART_LSR_DR;	serial_out(up, UART_IER, up->ier);}static void m32r_sio_enable_ms(struct uart_port *port){	struct uart_sio_port *up = (struct uart_sio_port *)port;	up->ier |= UART_IER_MSI;	serial_out(up, UART_IER, up->ier);}static void receive_chars(struct uart_sio_port *up, int *status){	struct tty_struct *tty = up->port.info->tty;	unsigned char ch;	unsigned char flag;	int max_count = 256;	do {		ch = sio_in(up, SIORXB);		flag = TTY_NORMAL;		up->port.icount.rx++;		if (unlikely(*status & (UART_LSR_BI | UART_LSR_PE |				       UART_LSR_FE | UART_LSR_OE))) {			/*			 * For statistics only			 */			if (*status & UART_LSR_BI) {				*status &= ~(UART_LSR_FE | UART_LSR_PE);				up->port.icount.brk++;				/*				 * We do the SysRQ and SAK checking				 * here because otherwise the break				 * may get masked by ignore_status_mask				 * or read_status_mask.				 */				if (uart_handle_break(&up->port))					goto ignore_char;			} else if (*status & UART_LSR_PE)				up->port.icount.parity++;			else if (*status & UART_LSR_FE)				up->port.icount.frame++;			if (*status & UART_LSR_OE)				up->port.icount.overrun++;			/*			 * Mask off conditions which should be ingored.			 */			*status &= up->port.read_status_mask;			if (up->port.line == up->port.cons->index) {				/* Recover the break flag from console xmit */				*status |= up->lsr_break_flag;				up->lsr_break_flag = 0;			}			if (*status & UART_LSR_BI) {				DEBUG_INTR("handling break....");				flag = TTY_BREAK;			} else if (*status & UART_LSR_PE)				flag = TTY_PARITY;			else if (*status & UART_LSR_FE)				flag = TTY_FRAME;		}		if (uart_handle_sysrq_char(&up->port, ch))			goto ignore_char;		if ((*status & up->port.ignore_status_mask) == 0)			tty_insert_flip_char(tty, ch, flag);		if (*status & UART_LSR_OE) {			/*			 * Overrun is special, since it's reported			 * immediately, and doesn't affect the current			 * character.			 */			tty_insert_flip_char(tty, 0, TTY_OVERRUN);		}	ignore_char:		*status = serial_in(up, UART_LSR);	} while ((*status & UART_LSR_DR) && (max_count-- > 0));	tty_flip_buffer_push(tty);}static void transmit_chars(struct uart_sio_port *up){	struct circ_buf *xmit = &up->port.info->xmit;	int count;	if (up->port.x_char) {#ifndef CONFIG_SERIAL_M32R_PLDSIO	/* XXX */		serial_out(up, UART_TX, up->port.x_char);#endif		up->port.icount.tx++;		up->port.x_char = 0;		return;	}	if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {		m32r_sio_stop_tx(&up->port);		return;	}	count = up->port.fifosize;	do {		serial_out(up, UART_TX, xmit->buf[xmit->tail]);		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);		up->port.icount.tx++;		if (uart_circ_empty(xmit))			break;		while (!serial_in(up, UART_LSR) & UART_LSR_THRE);	} while (--count > 0);	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)		uart_write_wakeup(&up->port);	DEBUG_INTR("THRE...");	if (uart_circ_empty(xmit))		m32r_sio_stop_tx(&up->port);}/* * This handles the interrupt from one port. */static inline void m32r_sio_handle_port(struct uart_sio_port *up,	unsigned int status){	DEBUG_INTR("status = %x...", status);	if (status & 0x04)		receive_chars(up, &status);	if (status & 0x01)		transmit_chars(up);}/* * This is the serial driver's interrupt routine. * * Arjan thinks the old way was overly complex, so it got simplified. * Alan disagrees, saying that need the complexity to handle the weird * nature of ISA shared interrupts.  (This is a special exception.) * * In order to handle ISA shared interrupts properly, we need to check * that all ports have been serviced, and therefore the ISA interrupt * line has been de-asserted. * * This means we need to loop through all ports. checking that they * don't have an interrupt pending. */static irqreturn_t m32r_sio_interrupt(int irq, void *dev_id){	struct irq_info *i = dev_id;	struct list_head *l, *end = NULL;	int pass_counter = 0;	DEBUG_INTR("m32r_sio_interrupt(%d)...", irq);#ifdef CONFIG_SERIAL_M32R_PLDSIO//	if (irq == PLD_IRQ_SIO0_SND)//		irq = PLD_IRQ_SIO0_RCV;#else	if (irq == M32R_IRQ_SIO0_S)		irq = M32R_IRQ_SIO0_R;#endif	spin_lock(&i->lock);	l = i->head;	do {		struct uart_sio_port *up;		unsigned int sts;		up = list_entry(l, struct uart_sio_port, list);		sts = sio_in(up, SIOSTS);		if (sts & 0x5) {			spin_lock(&up->port.lock);			m32r_sio_handle_port(up, sts);			spin_unlock(&up->port.lock);			end = NULL;		} else if (end == NULL)			end = l;		l = l->next;		if (l == i->head && pass_counter++ > PASS_LIMIT) {			if (sts & 0xe0)				sio_error(&sts);			break;		}	} while (l != end);	spin_unlock(&i->lock);	DEBUG_INTR("end.\n");	return IRQ_HANDLED;}/* * To support ISA shared interrupts, we need to have one interrupt * handler that ensures that the IRQ line has been deasserted * before returning.  Failing to do this will result in the IRQ * line being stuck active, and, since ISA irqs are edge triggered, * no more IRQs will be seen. */static void serial_do_unlink(struct irq_info *i, struct uart_sio_port *up){	spin_lock_irq(&i->lock);	if (!list_empty(i->head)) {		if (i->head == &up->list)			i->head = i->head->next;		list_del(&up->list);	} else {		BUG_ON(i->head != &up->list);		i->head = NULL;	}	spin_unlock_irq(&i->lock);}static int serial_link_irq_chain(struct uart_sio_port *up){	struct irq_info *i = irq_lists + up->port.irq;	int ret, irq_flags = 0;	spin_lock_irq(&i->lock);	if (i->head) {		list_add(&up->list, i->head);		spin_unlock_irq(&i->lock);		ret = 0;	} else {		INIT_LIST_HEAD(&up->list);		i->head = &up->list;		spin_unlock_irq(&i->lock);		ret = request_irq(up->port.irq, m32r_sio_interrupt,				  irq_flags, "SIO0-RX", i);		ret |= request_irq(up->port.irq + 1, m32r_sio_interrupt,				  irq_flags, "SIO0-TX", i);		if (ret < 0)			serial_do_unlink(i, up);	}	return ret;}static void serial_unlink_irq_chain(struct uart_sio_port *up){	struct irq_info *i = irq_lists + up->port.irq;	BUG_ON(i->head == NULL);	if (list_empty(i->head)) {		free_irq(up->port.irq, i);		free_irq(up->port.irq + 1, i);	}	serial_do_unlink(i, up);}/* * This function is used to handle ports that do not have an interrupt. */static void m32r_sio_timeout(unsigned long data){	struct uart_sio_port *up = (struct uart_sio_port *)data;	unsigned int timeout;	unsigned int sts;	sts = sio_in(up, SIOSTS);	if (sts & 0x5) {		spin_lock(&up->port.lock);		m32r_sio_handle_port(up, sts);		spin_unlock(&up->port.lock);	}	timeout = up->port.timeout;

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?