sh-sci.c

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

C
1,633
字号
/* * drivers/serial/sh-sci.c * * SuperH on-chip serial module support.  (SCI with no FIFO / with FIFO) * *  Copyright (C) 2002, 2003  Paul Mundt * * based off of the old drivers/char/sh-sci.c by: * *   Copyright (C) 1999, 2000  Niibe Yutaka *   Copyright (C) 2000  Sugioka Toshinobu *   Modified to support multiple serial ports. Stuart Menefy (May 2000). *   Modified to support SecureEdge. David McCullough (2002) *   Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003). * * This file is subject to the terms and conditions of the GNU General Public * License.  See the file "COPYING" in the main directory of this archive * for more details. */#define DEBUG#include <linux/config.h>#include <linux/module.h>#include <linux/errno.h>#include <linux/signal.h>#include <linux/sched.h>#include <linux/timer.h>#include <linux/interrupt.h>#include <linux/tty.h>#include <linux/tty_flip.h>#include <linux/serial.h>#include <linux/major.h>#include <linux/string.h>#include <linux/sysrq.h>#include <linux/fcntl.h>#include <linux/ptrace.h>#include <linux/ioport.h>#include <linux/mm.h>#include <linux/slab.h>#include <linux/init.h>#include <linux/delay.h>#include <linux/console.h>#ifdef CONFIG_CPU_FREQ#include <linux/notifier.h>#include <linux/cpufreq.h>#endif#include <asm/system.h>#include <asm/io.h>#include <asm/irq.h>#include <asm/uaccess.h>#include <asm/bitops.h>#include <linux/generic_serial.h>#ifdef CONFIG_SH_STANDARD_BIOS#include <asm/sh_bios.h>#endif#if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)#define SUPPORT_SYSRQ#endif#include "sh-sci.h"#ifdef CONFIG_SH_KGDB#include <asm/kgdb.h>static int kgdb_get_char(struct sci_port *port);static void kgdb_put_char(struct sci_port *port, char c);static void kgdb_handle_error(struct sci_port *port);static struct sci_port *kgdb_sci_port;#endif /* CONFIG_SH_KGDB */#ifdef CONFIG_SERIAL_SH_SCI_CONSOLEstatic struct sci_port *serial_console_port = 0;#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE *//* Function prototypes */static void sci_stop_tx(struct uart_port *port, unsigned int tty_stop);static void sci_start_tx(struct uart_port *port, unsigned int tty_start);static void sci_start_rx(struct uart_port *port, unsigned int tty_start);static void sci_stop_rx(struct uart_port *port);static int sci_request_irq(struct sci_port *port);static void sci_free_irq(struct sci_port *port);static struct sci_port sci_ports[SCI_NPORTS];static struct uart_driver sci_uart_driver;#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)static void handle_error(struct uart_port *port){				/* Clear error flags */	sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));}static int get_char(struct uart_port *port){	unsigned long flags;	unsigned short status;	int c;	local_irq_save(flags);        do {		status = sci_in(port, SCxSR);		if (status & SCxSR_ERRORS(port)) {			handle_error(port);			continue;		}	} while (!(status & SCxSR_RDxF(port)));	c = sci_in(port, SCxRDR);	sci_in(port, SCxSR);            /* Dummy read */	sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));	local_irq_restore(flags);	return c;}/* Taken from sh-stub.c of GDB 4.18 */static const char hexchars[] = "0123456789abcdef";static __inline__ char highhex(int  x){	return hexchars[(x >> 4) & 0xf];}static __inline__ char lowhex(int  x){	return hexchars[x & 0xf];}#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB *//* * Send the packet in buffer.  The host gets one chance to read it. * This routine does not wait for a positive acknowledge. */#ifdef CONFIG_SERIAL_SH_SCI_CONSOLEstatic void put_char(struct uart_port *port, char c){	unsigned long flags;	unsigned short status;	local_irq_save(flags);	do {		status = sci_in(port, SCxSR);	} while (!(status & SCxSR_TDxE(port)));	sci_out(port, SCxTDR, c);	sci_in(port, SCxSR);            /* Dummy read */	sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));	local_irq_restore(flags);}static void put_string(struct sci_port *sci_port, const char *buffer, int count){	struct uart_port *port = &sci_port->port;	const unsigned char *p = buffer;	int i;#if defined(CONFIG_SH_STANDARD_BIOS) || defined(CONFIG_SH_KGDB)	int checksum;	int usegdb=0;#ifdef CONFIG_SH_STANDARD_BIOS    	/* This call only does a trap the first time it is	 * called, and so is safe to do here unconditionally	 */	usegdb |= sh_bios_in_gdb_mode();#endif#ifdef CONFIG_SH_KGDB	usegdb |= (kgdb_in_gdb_mode && (port == kgdb_sci_port));#endif	if (usegdb) {	    /*  $<packet info>#<checksum>. */	    do {		unsigned char c;		put_char(port, '$');		put_char(port, 'O'); /* 'O'utput to console */		checksum = 'O';		for (i=0; i<count; i++) { /* Don't use run length encoding */			int h, l;			c = *p++;			h = highhex(c);			l = lowhex(c);			put_char(port, h);			put_char(port, l);			checksum += h + l;		}		put_char(port, '#');		put_char(port, highhex(checksum));		put_char(port, lowhex(checksum));	    } while  (get_char(port) != '+');	} else#endif /* CONFIG_SH_STANDARD_BIOS || CONFIG_SH_KGDB */	for (i=0; i<count; i++) {		if (*p == 10)			put_char(port, '\r');		put_char(port, *p++);	}}#endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */#ifdef CONFIG_SH_KGDB/* Is the SCI ready, ie is there a char waiting? */static int kgdb_is_char_ready(struct sci_port *port){        unsigned short status = sci_in(port, SCxSR);        if (status & (SCxSR_ERRORS(port) | SCxSR_BRK(port)))                kgdb_handle_error(port);        return (status & SCxSR_RDxF(port));}/* Write a char */static void kgdb_put_char(struct sci_port *port, char c){        unsigned short status;        do                status = sci_in(port, SCxSR);        while (!(status & SCxSR_TDxE(port)));        sci_out(port, SCxTDR, c);        sci_in(port, SCxSR);    /* Dummy read */        sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));}/* Get a char if there is one, else ret -1 */static int kgdb_get_char(struct sci_port *port){        int c;        if (kgdb_is_char_ready(port) == 0)                c = -1;        else {                c = sci_in(port, SCxRDR);                sci_in(port, SCxSR);    /* Dummy read */                sci_out(port, SCxSR, SCxSR_RDxF_CLEAR(port));        }        return c;}/* Called from kgdbstub.c to get a character, i.e. is blocking */static int kgdb_sci_getchar(void){        volatile int c;        /* Keep trying to read a character, this could be neater */        while ((c = kgdb_get_char(kgdb_sci_port)) < 0);        return c;}/* Called from kgdbstub.c to put a character, just a wrapper */static void kgdb_sci_putchar(int c){        kgdb_put_char(kgdb_sci_port, c);}/* Clear any errors on the SCI */static void kgdb_handle_error(struct sci_port *port){        sci_out(port, SCxSR, SCxSR_ERROR_CLEAR(port));  /* Clear error flags */}/* Breakpoint if there's a break sent on the serial port */static void kgdb_break_interrupt(int irq, void *ptr, struct pt_regs *regs){        struct sci_port *port = ptr;        unsigned short status = sci_in(port, SCxSR);        if (status & SCxSR_BRK(port)) {                /* Break into the debugger if a break is detected */                BREAKPOINT();                /* Clear */                sci_out(port, SCxSR, SCxSR_BREAK_CLEAR(port));        }}#endif /* CONFIG_SH_KGDB */#if defined(__H8300S__)enum { sci_disable, sci_enable };static void h8300_sci_enable(struct uart_port* port, unsigned int ctrl){	volatile unsigned char *mstpcrl=(volatile unsigned char *)MSTPCRL;	int ch = (port->mapbase  - SMR0) >> 3;	unsigned char mask = 1 << (ch+1);	if (ctrl == sci_disable) {		*mstpcrl |= mask;	} else {		*mstpcrl &= ~mask;	}}#endif#if defined(SCI_ONLY) || defined(SCI_AND_SCIF)#if defined(__H8300H__) || defined(__H8300S__)static void sci_init_pins_sci(struct uart_port* port, unsigned int cflag){	int ch = (port->mapbase - SMR0) >> 3;	/* set DDR regs */	H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].rx,H8300_GPIO_INPUT);	H8300_GPIO_DDR(h8300_sci_pins[ch].port,h8300_sci_pins[ch].tx,H8300_GPIO_OUTPUT);	/* tx mark output*/	H8300_SCI_DR(ch) |= h8300_sci_pins[ch].tx;}#elsestatic void sci_init_pins_sci(struct uart_port *port, unsigned int cflag){}#endif#endif#if defined(SCIF_ONLY) || defined(SCI_AND_SCIF)#if defined(CONFIG_CPU_SH3)/* For SH7707, SH7709, SH7709A, SH7729, SH7300*/static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag){	unsigned int fcr_val = 0;#if !defined(CONFIG_CPU_SUBTYPE_SH7300) /* SH7300 doesn't use RTS/CTS */	{		unsigned short data;		/* We need to set SCPCR to enable RTS/CTS */		data = ctrl_inw(SCPCR);		/* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/		ctrl_outw(data&0x0fcf, SCPCR);	}	if (cflag & CRTSCTS)		fcr_val |= SCFCR_MCE;	else {		unsigned short data;		/* We need to set SCPCR to enable RTS/CTS */		data = ctrl_inw(SCPCR);		/* Clear out SCP7MD1,0, SCP4MD1,0,		   Set SCP6MD1,0 = {01} (output)  */		ctrl_outw((data&0x0fcf)|0x1000, SCPCR);		data = ctrl_inb(SCPDR);		/* Set /RTS2 (bit6) = 0 */		ctrl_outb(data&0xbf, SCPDR);	}#endif	sci_out(port, SCFCR, fcr_val);}static void sci_init_pins_irda(struct uart_port *port, unsigned int cflag){	unsigned int fcr_val = 0;	if (cflag & CRTSCTS)		fcr_val |= SCFCR_MCE;	sci_out(port, SCFCR, fcr_val);}#else/* For SH7750 */static void sci_init_pins_scif(struct uart_port *port, unsigned int cflag){	unsigned int fcr_val = 0;	if (cflag & CRTSCTS) {		fcr_val |= SCFCR_MCE;	} else {		ctrl_outw(0x0080, SCSPTR2); /* Set RTS = 1 */	}	sci_out(port, SCFCR, fcr_val);}#endif#endif /* SCIF_ONLY || SCI_AND_SCIF *//* ********************************************************************** * *                   the interrupt related routines                       * * ********************************************************************** */static void sci_transmit_chars(struct uart_port *port){	struct circ_buf *xmit = &port->info->xmit;	unsigned int stopped = uart_tx_stopped(port);	unsigned long flags;	unsigned short status;	unsigned short ctrl;	int count, txroom;	status = sci_in(port, SCxSR);	if (!(status & SCxSR_TDxE(port))) {		local_irq_save(flags);		ctrl = sci_in(port, SCSCR);		if (uart_circ_empty(xmit)) {			ctrl &= ~SCI_CTRL_FLAGS_TIE;		} else {			ctrl |= SCI_CTRL_FLAGS_TIE;		}		sci_out(port, SCSCR, ctrl);		local_irq_restore(flags);		return;	}#if !defined(SCI_ONLY)	if (port->type == PORT_SCIF) {		txroom = SCIF_TXROOM_MAX - (sci_in(port, SCFDR)>>8);	} else {		txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;	}#else	txroom = (sci_in(port, SCxSR) & SCI_TDRE)?1:0;#endif	count = txroom;	do {		unsigned char c;		if (port->x_char) {			c = port->x_char;			port->x_char = 0;		} else if (!uart_circ_empty(xmit) && !stopped) {			c = xmit->buf[xmit->tail];			xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);		} else {			break;		}		sci_out(port, SCxTDR, c);		port->icount.tx++;	} while (--count > 0);	sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)		uart_write_wakeup(port);	if (uart_circ_empty(xmit)) {		sci_stop_tx(port, 0);	} else {		local_irq_save(flags);		ctrl = sci_in(port, SCSCR);#if !defined(SCI_ONLY)		if (port->type == PORT_SCIF) {			sci_in(port, SCxSR); /* Dummy read */			sci_out(port, SCxSR, SCxSR_TDxE_CLEAR(port));		}#endif		ctrl |= SCI_CTRL_FLAGS_TIE;		sci_out(port, SCSCR, ctrl);		local_irq_restore(flags);	}}/* On SH3, SCIF may read end-of-break as a space->mark char */#define STEPFN(c)  ({int __c=(c); (((__c-1)|(__c)) == -1); })static inline void sci_receive_chars(struct uart_port *port,				     struct pt_regs *regs){	struct tty_struct *tty = port->info->tty;	int i, count, copied = 0;	unsigned short status;	status = sci_in(port, SCxSR);	if (!(status & SCxSR_RDxF(port)))		return;	while (1) {#if !defined(SCI_ONLY)		if (port->type == PORT_SCIF) {			count = sci_in(port, SCFDR)&SCIF_RFDC_MASK ;		} else {			count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;		}#else		count = (sci_in(port, SCxSR)&SCxSR_RDxF(port))?1:0;#endif		/* Don't copy more bytes than there is room for in the buffer */		if (tty->flip.count + count > TTY_FLIPBUF_SIZE)			count = TTY_FLIPBUF_SIZE - tty->flip.count;		/* If for any reason we can't copy more data, we're done! */		if (count == 0)			break;		if (port->type == PORT_SCI) {			char c = sci_in(port, SCxRDR);                       if(((struct sci_port *)port)->break_flag			    || uart_handle_sysrq_char(port, c, regs)) {				count = 0;			} else {			    tty->flip.char_buf_ptr[0] = c;			    tty->flip.flag_buf_ptr[0] = TTY_NORMAL;			}		} else {			for (i=0; i<count; i++) {				char c = sci_in(port, SCxRDR);				status = sci_in(port, SCxSR);#if defined(CONFIG_CPU_SH3)				/* Skip "chars" during break */				if (((struct sci_port *)port)->break_flag) {					if ((c == 0) &&					    (status & SCxSR_FER(port))) {						count--; i--;						continue;					}					/* Nonzero => end-of-break */					pr_debug("scif: debounce<%02x>\n", c);					((struct sci_port *)port)->break_flag = 0;					if (STEPFN(c)) {						count--; i--;						continue;					}				}#endif /* CONFIG_CPU_SH3 */				if (uart_handle_sysrq_char(port, c, regs)) {					count--; i--;					continue;				}				/* Store data and status */				tty->flip.char_buf_ptr[i] = c;				if (status&SCxSR_FER(port)) {

⌨️ 快捷键说明

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