sa1100.c

来自「Linux Kernel 2.6.9 for OMAP1710」· C语言 代码 · 共 958 行 · 第 1/2 页

C
958
字号
/* *  linux/drivers/char/sa1100.c * *  Driver for SA11x0 serial ports * *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. * *  Copyright (C) 2000 Deep Blue Solutions Ltd. * * 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. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA * *  $Id: sa1100.c,v 1.50 2002/07/29 14:41:04 rmk Exp $ * */#include <linux/config.h>#include <linux/module.h>#include <linux/tty.h>#include <linux/ioport.h>#include <linux/init.h>#include <linux/serial.h>#include <linux/console.h>#include <linux/sysrq.h>#include <linux/device.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/hardware.h>#include <asm/mach/serial_sa1100.h>#if defined(CONFIG_SERIAL_SA1100_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)#define SUPPORT_SYSRQ#endif#include <linux/serial_core.h>/* We've been assigned a range on the "Low-density serial ports" major */#define SERIAL_SA1100_MAJOR	204#define MINOR_START		5#define NR_PORTS		3#define SA1100_ISR_PASS_LIMIT	256/* * Convert from ignore_status_mask or read_status_mask to UTSR[01] */#define SM_TO_UTSR0(x)	((x) & 0xff)#define SM_TO_UTSR1(x)	((x) >> 8)#define UTSR0_TO_SM(x)	((x))#define UTSR1_TO_SM(x)	((x) << 8)#define UART_GET_UTCR0(sport)	__raw_readl((sport)->port.membase + UTCR0)#define UART_GET_UTCR1(sport)	__raw_readl((sport)->port.membase + UTCR1)#define UART_GET_UTCR2(sport)	__raw_readl((sport)->port.membase + UTCR2)#define UART_GET_UTCR3(sport)	__raw_readl((sport)->port.membase + UTCR3)#define UART_GET_UTSR0(sport)	__raw_readl((sport)->port.membase + UTSR0)#define UART_GET_UTSR1(sport)	__raw_readl((sport)->port.membase + UTSR1)#define UART_GET_CHAR(sport)	__raw_readl((sport)->port.membase + UTDR)#define UART_PUT_UTCR0(sport,v)	__raw_writel((v),(sport)->port.membase + UTCR0)#define UART_PUT_UTCR1(sport,v)	__raw_writel((v),(sport)->port.membase + UTCR1)#define UART_PUT_UTCR2(sport,v)	__raw_writel((v),(sport)->port.membase + UTCR2)#define UART_PUT_UTCR3(sport,v)	__raw_writel((v),(sport)->port.membase + UTCR3)#define UART_PUT_UTSR0(sport,v)	__raw_writel((v),(sport)->port.membase + UTSR0)#define UART_PUT_UTSR1(sport,v)	__raw_writel((v),(sport)->port.membase + UTSR1)#define UART_PUT_CHAR(sport,v)	__raw_writel((v),(sport)->port.membase + UTDR)/* * This is the size of our serial port register set. */#define UART_PORT_SIZE	0x24/* * This determines how often we check the modem status signals * for any change.  They generally aren't connected to an IRQ * so we have to poll them.  We also check immediately before * filling the TX fifo incase CTS has been dropped. */#define MCTRL_TIMEOUT	(250*HZ/1000)struct sa1100_port {	struct uart_port	port;	struct timer_list	timer;	unsigned int		old_status;};/* * Handle any change of modem status signal since we were last called. */static void sa1100_mctrl_check(struct sa1100_port *sport){	unsigned int status, changed;	status = sport->port.ops->get_mctrl(&sport->port);	changed = status ^ sport->old_status;	if (changed == 0)		return;	sport->old_status = status;	if (changed & TIOCM_RI)		sport->port.icount.rng++;	if (changed & TIOCM_DSR)		sport->port.icount.dsr++;	if (changed & TIOCM_CAR)		uart_handle_dcd_change(&sport->port, status & TIOCM_CAR);	if (changed & TIOCM_CTS)		uart_handle_cts_change(&sport->port, status & TIOCM_CTS);	wake_up_interruptible(&sport->port.info->delta_msr_wait);}/* * This is our per-port timeout handler, for checking the * modem status signals. */static void sa1100_timeout(unsigned long data){	struct sa1100_port *sport = (struct sa1100_port *)data;	unsigned long flags;	if (sport->port.info) {		spin_lock_irqsave(&sport->port.lock, flags);		sa1100_mctrl_check(sport);		spin_unlock_irqrestore(&sport->port.lock, flags);		mod_timer(&sport->timer, jiffies + MCTRL_TIMEOUT);	}}/* * interrupts disabled on entry */static void sa1100_stop_tx(struct uart_port *port, unsigned int tty_stop){	struct sa1100_port *sport = (struct sa1100_port *)port;	u32 utcr3;	utcr3 = UART_GET_UTCR3(sport);	UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_TIE);	sport->port.read_status_mask &= ~UTSR0_TO_SM(UTSR0_TFS);}/* * interrupts may not be disabled on entry */static void sa1100_start_tx(struct uart_port *port, unsigned int tty_start){	struct sa1100_port *sport = (struct sa1100_port *)port;	unsigned long flags;	u32 utcr3;	spin_lock_irqsave(&sport->port.lock, flags);	utcr3 = UART_GET_UTCR3(sport);	sport->port.read_status_mask |= UTSR0_TO_SM(UTSR0_TFS);	UART_PUT_UTCR3(sport, utcr3 | UTCR3_TIE);	spin_unlock_irqrestore(&sport->port.lock, flags);}/* * Interrupts enabled */static void sa1100_stop_rx(struct uart_port *port){	struct sa1100_port *sport = (struct sa1100_port *)port;	u32 utcr3;	utcr3 = UART_GET_UTCR3(sport);	UART_PUT_UTCR3(sport, utcr3 & ~UTCR3_RIE);}/* * Set the modem control timer to fire immediately. */static void sa1100_enable_ms(struct uart_port *port){	struct sa1100_port *sport = (struct sa1100_port *)port;	mod_timer(&sport->timer, jiffies);}static voidsa1100_rx_chars(struct sa1100_port *sport, struct pt_regs *regs){	struct tty_struct *tty = sport->port.info->tty;	unsigned int status, ch, flg, ignored = 0;	status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |		 UTSR0_TO_SM(UART_GET_UTSR0(sport));	while (status & UTSR1_TO_SM(UTSR1_RNE)) {		ch = UART_GET_CHAR(sport);		if (tty->flip.count >= TTY_FLIPBUF_SIZE)			goto ignore_char;		sport->port.icount.rx++;		flg = TTY_NORMAL;		/*		 * note that the error handling code is		 * out of the main execution path		 */		if (status & UTSR1_TO_SM(UTSR1_PRE | UTSR1_FRE | UTSR1_ROR))			goto handle_error;		if (uart_handle_sysrq_char(&sport->port, ch, regs))			goto ignore_char;	error_return:		*tty->flip.flag_buf_ptr++ = flg;		*tty->flip.char_buf_ptr++ = ch;		tty->flip.count++;	ignore_char:		status = UTSR1_TO_SM(UART_GET_UTSR1(sport)) |			 UTSR0_TO_SM(UART_GET_UTSR0(sport));	} out:	tty_flip_buffer_push(tty);	return; handle_error:	if (status & UTSR1_TO_SM(UTSR1_PRE))		sport->port.icount.parity++;	else if (status & UTSR1_TO_SM(UTSR1_FRE))		sport->port.icount.frame++;	if (status & UTSR1_TO_SM(UTSR1_ROR))		sport->port.icount.overrun++;	if (status & sport->port.ignore_status_mask) {		if (++ignored > 100)			goto out;		goto ignore_char;	}	status &= sport->port.read_status_mask;	if (status & UTSR1_TO_SM(UTSR1_PRE))		flg = TTY_PARITY;	else if (status & UTSR1_TO_SM(UTSR1_FRE))		flg = TTY_FRAME;	if (status & UTSR1_TO_SM(UTSR1_ROR)) {		/*		 * overrun does *not* affect the character		 * we read from the FIFO		 */		*tty->flip.flag_buf_ptr++ = flg;		*tty->flip.char_buf_ptr++ = ch;		tty->flip.count++;		if (tty->flip.count >= TTY_FLIPBUF_SIZE)			goto ignore_char;		ch = 0;		flg = TTY_OVERRUN;	}#ifdef SUPPORT_SYSRQ	sport->port.sysrq = 0;#endif	goto error_return;}static void sa1100_tx_chars(struct sa1100_port *sport){	struct circ_buf *xmit = &sport->port.info->xmit;	if (sport->port.x_char) {		UART_PUT_CHAR(sport, sport->port.x_char);		sport->port.icount.tx++;		sport->port.x_char = 0;		return;	}	/*	 * Check the modem control lines before	 * transmitting anything.	 */	sa1100_mctrl_check(sport);	if (uart_circ_empty(xmit) || uart_tx_stopped(&sport->port)) {		sa1100_stop_tx(&sport->port, 0);		return;	}	/*	 * Tried using FIFO (not checking TNF) for fifo fill:	 * still had the '4 bytes repeated' problem.	 */	while (UART_GET_UTSR1(sport) & UTSR1_TNF) {		UART_PUT_CHAR(sport, xmit->buf[xmit->tail]);		xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);		sport->port.icount.tx++;		if (uart_circ_empty(xmit))			break;	}	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)		uart_write_wakeup(&sport->port);	if (uart_circ_empty(xmit))		sa1100_stop_tx(&sport->port, 0);}static irqreturn_t sa1100_int(int irq, void *dev_id, struct pt_regs *regs){	struct sa1100_port *sport = dev_id;	unsigned int status, pass_counter = 0;	spin_lock(&sport->port.lock);	status = UART_GET_UTSR0(sport);	status &= SM_TO_UTSR0(sport->port.read_status_mask) | ~UTSR0_TFS;	do {		if (status & (UTSR0_RFS | UTSR0_RID)) {			/* Clear the receiver idle bit, if set */			if (status & UTSR0_RID)				UART_PUT_UTSR0(sport, UTSR0_RID);			sa1100_rx_chars(sport, regs);		}		/* Clear the relevant break bits */		if (status & (UTSR0_RBB | UTSR0_REB))			UART_PUT_UTSR0(sport, status & (UTSR0_RBB | UTSR0_REB));		if (status & UTSR0_RBB)			sport->port.icount.brk++;		if (status & UTSR0_REB)			uart_handle_break(&sport->port);		if (status & UTSR0_TFS)			sa1100_tx_chars(sport);		if (pass_counter++ > SA1100_ISR_PASS_LIMIT)			break;		status = UART_GET_UTSR0(sport);		status &= SM_TO_UTSR0(sport->port.read_status_mask) |			  ~UTSR0_TFS;	} while (status & (UTSR0_TFS | UTSR0_RFS | UTSR0_RID));	spin_unlock(&sport->port.lock);	return IRQ_HANDLED;}/* * Return TIOCSER_TEMT when transmitter is not busy. */static unsigned int sa1100_tx_empty(struct uart_port *port){	struct sa1100_port *sport = (struct sa1100_port *)port;	return UART_GET_UTSR1(sport) & UTSR1_TBY ? 0 : TIOCSER_TEMT;}static unsigned int sa1100_get_mctrl(struct uart_port *port){	return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;}static void sa1100_set_mctrl(struct uart_port *port, unsigned int mctrl){}/* * Interrupts always disabled. */static void sa1100_break_ctl(struct uart_port *port, int break_state){	struct sa1100_port *sport = (struct sa1100_port *)port;	unsigned long flags;	unsigned int utcr3;	spin_lock_irqsave(&sport->port.lock, flags);	utcr3 = UART_GET_UTCR3(sport);	if (break_state == -1)		utcr3 |= UTCR3_BRK;	else		utcr3 &= ~UTCR3_BRK;	UART_PUT_UTCR3(sport, utcr3);	spin_unlock_irqrestore(&sport->port.lock, flags);}static int sa1100_startup(struct uart_port *port){	struct sa1100_port *sport = (struct sa1100_port *)port;	int retval;	/*	 * Allocate the IRQ	 */	retval = request_irq(sport->port.irq, sa1100_int, 0,			     "sa11x0-uart", sport);	if (retval)		return retval;	/*	 * Finally, clear and enable interrupts	 */	UART_PUT_UTSR0(sport, -1);	UART_PUT_UTCR3(sport, UTCR3_RXE | UTCR3_TXE | UTCR3_RIE);	/*	 * Enable modem status interrupts	 */	spin_lock_irq(&sport->port.lock);	sa1100_enable_ms(&sport->port);	spin_unlock_irq(&sport->port.lock);	return 0;}static void sa1100_shutdown(struct uart_port *port){	struct sa1100_port *sport = (struct sa1100_port *)port;	/*	 * Stop our timer.	 */	del_timer_sync(&sport->timer);	/*	 * Free the interrupt	 */	free_irq(sport->port.irq, sport);	/*	 * Disable all interrupts, port and break condition.	 */	UART_PUT_UTCR3(sport, 0);}static voidsa1100_set_termios(struct uart_port *port, struct termios *termios,		   struct termios *old){	struct sa1100_port *sport = (struct sa1100_port *)port;	unsigned long flags;	unsigned int utcr0, old_utcr3, baud, quot;	unsigned int old_csize = old ? old->c_cflag & CSIZE : CS8;	/*	 * We only support CS7 and CS8.	 */	while ((termios->c_cflag & CSIZE) != CS7 &&	       (termios->c_cflag & CSIZE) != CS8) {		termios->c_cflag &= ~CSIZE;		termios->c_cflag |= old_csize;		old_csize = CS8;	}	if ((termios->c_cflag & CSIZE) == CS8)		utcr0 = UTCR0_DSS;	else		utcr0 = 0;	if (termios->c_cflag & CSTOPB)		utcr0 |= UTCR0_SBS;	if (termios->c_cflag & PARENB) {		utcr0 |= UTCR0_PE;		if (!(termios->c_cflag & PARODD))			utcr0 |= UTCR0_OES;	}	/*	 * Ask the core to calculate the divisor for us.	 */	baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16); 	quot = uart_get_divisor(port, baud);	spin_lock_irqsave(&sport->port.lock, flags);

⌨️ 快捷键说明

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