au1x00_uart.c

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

C
1,391
字号
/* *  Driver for 8250/16550-type serial ports * *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o. * *  Copyright (C) 2001 Russell King. * * 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. */#include <linux/config.h>#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/serial.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/mach-au1x00/au1000.h>#if defined(CONFIG_SERIAL_AU1X00_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)#define SUPPORT_SYSRQ#endif#include <linux/serial_core.h>#include "8250.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)static struct old_serial_port old_serial_port[] = {	{	.baud_base = 0,		.iomem_base = (u8 *)UART0_ADDR,		.irq = AU1000_UART0_INT,		.flags = STD_COM_FLAGS,		.iomem_reg_shift = 2,	}, {		.baud_base = 0,		.iomem_base = (u8 *)UART1_ADDR,		.irq = AU1000_UART1_INT,		.flags = STD_COM_FLAGS,		.iomem_reg_shift = 2	}, {		.baud_base = 0,		.iomem_base = (u8 *)UART2_ADDR,		.irq = AU1000_UART2_INT,		.flags = STD_COM_FLAGS,		.iomem_reg_shift = 2	}, {		.baud_base = 0,		.iomem_base = (u8 *)UART3_ADDR,		.irq = AU1000_UART3_INT,		.flags = STD_COM_FLAGS,		.iomem_reg_shift = 2	}};#define UART_NR	ARRAY_SIZE(old_serial_port)struct uart_8250_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_MAX_8250+1] = {	{ "unknown",	1,	0 },	{ "8250",	1,	0 },	{ "16450",	1,	0 },	{ "16550",	1,	0 },	/* PORT_16550A */	{ "AU1X00_UART",16,	UART_CLEAR_FIFO | UART_USE_FIFO },};static _INLINE_ unsigned int serial_in(struct uart_8250_port *up, int offset){	return au_readl((unsigned long)up->port.membase + offset);}static _INLINE_ voidserial_out(struct uart_8250_port *up, int offset, int value){	au_writel(value, (unsigned long)up->port.membase + offset);}#define serial_inp(up, offset)		serial_in(up, offset)#define serial_outp(up, offset, value)	serial_out(up, offset, value)/* * This routine is called by rs_init() to initialize a specific serial * port.  It determines what type of UART chip this serial port is * using: 8250, 16450, 16550, 16550A.  The important question is * whether or not this UART is a 16550A or not, since this will * determine whether or not we can use its FIFO features or not. */static void autoconfig(struct uart_8250_port *up, unsigned int probeflags){	unsigned char save_lcr, save_mcr;	unsigned long flags;	if (!up->port.iobase && !up->port.mapbase && !up->port.membase)		return;	DEBUG_AUTOCONF("ttyS%d: autoconf (0x%04x, 0x%08lx): ",			up->port.line, up->port.iobase, up->port.membase);	/*	 * We really do need global IRQs disabled here - we're going to	 * be frobbing the chips IRQ enable register to see if it exists.	 */	spin_lock_irqsave(&up->port.lock, flags);//	save_flags(flags); cli();	save_mcr = serial_in(up, UART_MCR);	save_lcr = serial_in(up, UART_LCR);	up->port.type = PORT_16550A;	serial_outp(up, UART_LCR, save_lcr);	up->port.fifosize = uart_config[up->port.type].dfl_xmit_fifo_size;	if (up->port.type == PORT_UNKNOWN)		goto out;	/*	 * Reset the UART.	 */	serial_outp(up, UART_MCR, save_mcr);	serial_outp(up, UART_FCR, (UART_FCR_ENABLE_FIFO |				     UART_FCR_CLEAR_RCVR |				     UART_FCR_CLEAR_XMIT));	serial_outp(up, UART_FCR, 0);	(void)serial_in(up, UART_RX);	serial_outp(up, UART_IER, 0); out:		spin_unlock_irqrestore(&up->port.lock, flags);//	restore_flags(flags);	DEBUG_AUTOCONF("type=%s\n", uart_config[up->port.type].name);}static void serial8250_stop_tx(struct uart_port *port, unsigned int tty_stop){	struct uart_8250_port *up = (struct uart_8250_port *)port;	if (up->ier & UART_IER_THRI) {		up->ier &= ~UART_IER_THRI;		serial_out(up, UART_IER, up->ier);	}}static void serial8250_start_tx(struct uart_port *port, unsigned int tty_start){	struct uart_8250_port *up = (struct uart_8250_port *)port;	if (!(up->ier & UART_IER_THRI)) {		up->ier |= UART_IER_THRI;		serial_out(up, UART_IER, up->ier);	}}static void serial8250_stop_rx(struct uart_port *port){	struct uart_8250_port *up = (struct uart_8250_port *)port;	up->ier &= ~UART_IER_RLSI;	up->port.read_status_mask &= ~UART_LSR_DR;	serial_out(up, UART_IER, up->ier);}static void serial8250_enable_ms(struct uart_port *port){	struct uart_8250_port *up = (struct uart_8250_port *)port;	up->ier |= UART_IER_MSI;	serial_out(up, UART_IER, up->ier);}static _INLINE_ voidreceive_chars(struct uart_8250_port *up, int *status, struct pt_regs *regs){	struct tty_struct *tty = up->port.info->tty;	unsigned char ch;	int max_count = 256;	do {		if (unlikely(tty->flip.count >= TTY_FLIPBUF_SIZE)) {			tty->flip.work.func((void *)tty);			if (tty->flip.count >= TTY_FLIPBUF_SIZE)				return; // if TTY_DONT_FLIP is set		}		ch = serial_inp(up, UART_RX);		*tty->flip.char_buf_ptr = ch;		*tty->flip.flag_buf_ptr = 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;#ifdef CONFIG_SERIAL_AU1X00_CONSOLE			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;			}#endif			if (*status & UART_LSR_BI) {				DEBUG_INTR("handling break....");				*tty->flip.flag_buf_ptr = TTY_BREAK;			} else if (*status & UART_LSR_PE)				*tty->flip.flag_buf_ptr = TTY_PARITY;			else if (*status & UART_LSR_FE)				*tty->flip.flag_buf_ptr = TTY_FRAME;		}		if (uart_handle_sysrq_char(&up->port, ch, regs))			goto ignore_char;		if ((*status & up->port.ignore_status_mask) == 0) {			tty->flip.flag_buf_ptr++;			tty->flip.char_buf_ptr++;			tty->flip.count++;		}		if ((*status & UART_LSR_OE) &&		    tty->flip.count < TTY_FLIPBUF_SIZE) {			/*			 * Overrun is special, since it's reported			 * immediately, and doesn't affect the current			 * character.			 */			*tty->flip.flag_buf_ptr = TTY_OVERRUN;			tty->flip.flag_buf_ptr++;			tty->flip.char_buf_ptr++;			tty->flip.count++;		}	ignore_char:		*status = serial_inp(up, UART_LSR);	} while ((*status & UART_LSR_DR) && (max_count-- > 0));	tty_flip_buffer_push(tty);}static _INLINE_ void transmit_chars(struct uart_8250_port *up){	struct circ_buf *xmit = &up->port.info->xmit;	int count;	if (up->port.x_char) {		serial_outp(up, UART_TX, up->port.x_char);		up->port.icount.tx++;		up->port.x_char = 0;		return;	}	if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {		serial8250_stop_tx(&up->port, 0);		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 (--count > 0);	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)		uart_write_wakeup(&up->port);	DEBUG_INTR("THRE...");	if (uart_circ_empty(xmit))		serial8250_stop_tx(&up->port, 0);}static _INLINE_ void check_modem_status(struct uart_8250_port *up){	int status;	status = serial_in(up, UART_MSR);	if ((status & UART_MSR_ANY_DELTA) == 0)		return;	if (status & UART_MSR_TERI)		up->port.icount.rng++;	if (status & UART_MSR_DDSR)		up->port.icount.dsr++;	if (status & UART_MSR_DDCD)		uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);	if (status & UART_MSR_DCTS)		uart_handle_cts_change(&up->port, status & UART_MSR_CTS);	wake_up_interruptible(&up->port.info->delta_msr_wait);}/* * This handles the interrupt from one port. */static inline voidserial8250_handle_port(struct uart_8250_port *up, struct pt_regs *regs){	unsigned int status = serial_inp(up, UART_LSR);	DEBUG_INTR("status = %x...", status);	if (status & UART_LSR_DR)		receive_chars(up, &status, regs);	check_modem_status(up);	if (status & UART_LSR_THRE)		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 serial8250_interrupt(int irq, void *dev_id, struct pt_regs *regs){	struct irq_info *i = dev_id;	struct list_head *l, *end = NULL;	int pass_counter = 0;	DEBUG_INTR("serial8250_interrupt(%d)...", irq);	spin_lock(&i->lock);	l = i->head;	do {		struct uart_8250_port *up;		unsigned int iir;		up = list_entry(l, struct uart_8250_port, list);		iir = serial_in(up, UART_IIR);		if (!(iir & UART_IIR_NO_INT)) {			spin_lock(&up->port.lock);			serial8250_handle_port(up, regs);			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 we hit this, we're dead. */			printk(KERN_ERR "serial8250: too much work for "				"irq%d\n", irq);			break;		}	} while (l != end);	spin_unlock(&i->lock);	DEBUG_INTR("end.\n");	/* FIXME! Was it really ours? */	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_8250_port *up){

⌨️ 快捷键说明

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