⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uart.c

📁 microwindows移植到S3C44B0的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
/* *  UART driver for 68360 CPM SCC or SMC *  Copyright (c) 2000 D. Jeff Dionne <jeff@uclinux.org>, *  Copyright (c) 2000 Michael Leslie <mleslie@lineo.ca> *  Copyright (c) 1997 Dan Malek <dmalek@jlc.net> * * I used the serial.c driver as the framework for this driver. * Give credit to those guys. * The original code was written for the MBX860 board.  I tried to make * it generic, but there may be some assumptions in the structures that * have to be fixed later. * To save porting time, I did not bother to change any object names * that are not accessed outside of this file. * It still needs lots of work........When it was easy, I included code * to support the SCCs, but this has never been tested, nor is it complete. * Only the SCCs support modem control, so that is not complete either. * * This module exports the following rs232 io functions: * *	int rs_360_init(void); */#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/serialP.h> #include <linux/major.h>#include <linux/string.h>#include <linux/fcntl.h>#include <linux/ptrace.h>#include <linux/mm.h>#include <linux/malloc.h>/* #include <linux/init.h> */#include <linux/delay.h>/* #include <asm/uaccess.h> */#include <asm/irq.h>#include <linux/compatmac.h>#include <asm/m68360.h>/* #include <asm/360_immap.h> */ /* more hierarchical than m68360_quicc.h */#include "commproc.h" #ifdef CONFIG_KGDBextern void breakpoint(void);extern void set_debug_traps(void);extern int  kgdb_output_string (const char* s, unsigned int count);#endif/* #ifdef CONFIG_SERIAL_CONSOLE */ /* This seems to be a post 2.0 thing - mles */#include <linux/console.h>/* this defines the index into rs_table for the port to use */#ifndef CONFIG_SERIAL_CONSOLE_PORT#define CONFIG_SERIAL_CONSOLE_PORT	1 /* ie SMC2 - note USE_SMC2 must be defined */#endif/* #endif */#if 0/* SCC2 for console */#undef CONFIG_SERIAL_CONSOLE_PORT#define CONFIG_SERIAL_CONSOLE_PORT	2#endif#define TX_WAKEUP	ASYNC_SHARE_IRQstatic char *serial_name = "CPM UART driver";static char *serial_version = "0.03";static DECLARE_TASK_QUEUE(tq_serial);static struct tty_driver serial_driver, callout_driver;static int serial_refcount;int serial_console_setup(struct console *co, char *options);/* * Serial driver configuration section.  Here are the various options: */#define SERIAL_PARANOIA_CHECK#define CONFIG_SERIAL_NOPAUSE_IO#define SERIAL_DO_RESTART/* Set of debugging defines */#undef SERIAL_DEBUG_INTR#undef SERIAL_DEBUG_OPEN#undef SERIAL_DEBUG_FLOW#undef SERIAL_DEBUG_RS_WAIT_UNTIL_SENT#define _INLINE_ inline  #define DBG_CNT(s)/* We overload some of the items in the data structure to meet our * needs.  For example, the port address is the CPM parameter ram * offset for the SCC or SMC.  The maximum number of ports is 4 SCCs and * 2 SMCs.  The "hub6" field is used to indicate the channel number, with * a flag indicating SCC or SMC, and the number is used as an index into * the CPM parameter area for this device. * The "type" field is currently set to 0, for PORT_UNKNOWN.  It is * not currently used.  I should probably use it to indicate the port * type of SMC or SCC. * The SMCs do not support any modem control signals. */#define smc_scc_num	hub6#define NUM_IS_SCC	((int)0x00010000)#define PORT_NUM(P)	((P) & 0x0000ffff)#if defined (CONFIG_UCQUICC)volatile extern void *_periph_base;/* sipex transceiver *   mode bits for       are on pins * *    SCC2                d16..19 *    SCC3                d20..23 *    SCC4                d24..27 */#define SIPEX_MODE(n,m) ((m & 0x0f)<<(16+4*(n-1)))static uint sipex_mode_bits = 0x00000000;#endif/* There is no `serial_state' defined back here in 2.0. * Try to get by with serial_struct *//* #define serial_state serial_struct *//* 2.4 -> 2.0 portability problem: async_icount in 2.4 has a few * extras: */#if 0struct async_icount_24 {	__u32   cts, dsr, rng, dcd, tx, rx;	__u32   frame, parity, overrun, brk;	__u32   buf_overrun;} icount;#endif#if 0struct serial_state {        int     magic;        int     baud_base;        unsigned long   port;        int     irq;        int     flags;        int     hub6;        int     type;        int     line;        int     revision;       /* Chip revision (950) */        int     xmit_fifo_size;        int     custom_divisor;        int     count;        u8      *iomem_base;        u16     iomem_reg_shift;        unsigned short  close_delay;        unsigned short  closing_wait; /* time to wait before closing */        struct async_icount_24     icount;         struct termios          normal_termios;        struct termios          callout_termios;        int     io_type;        struct async_struct *info;};#endif#define SSTATE_MAGIC 0x5302/* SMC2 is sometimes used for low performance TDM interfaces.  Define * this as 1 if you want SMC2 as a serial port UART managed by this driver. * Define this as 0 if you wish to use SMC2 for something else. */#define USE_SMC2 1#if 0/* Define SCC to ttySx mapping. */#define SCC_NUM_BASE	(USE_SMC2 + 1)	/* SCC base tty "number" *//* Define which SCC is the first one to use for a serial port.  These * are 0-based numbers, i.e. this assumes the first SCC (SCC1) is used * for Ethernet, and the first available SCC for serial UART is SCC2. * NOTE:  IF YOU CHANGE THIS, you have to change the PROFF_xxx and * interrupt vectors in the table below to match. */#define SCC_IDX_BASE	1	/* table index */#endif/* Processors other than the 860 only get SMCs configured by default. * Either they don't have SCCs or they are allocated somewhere else. * Of course, there are now 860s without some SCCs, so we will need to * address that someday. * The Embedded Planet Multimedia I/O cards use TDM interfaces to the * stereo codec parts, and we use SMC2 to help support that. */static struct serial_state rs_table[] = {/*  type   line   PORT           IRQ       FLAGS  smc_scc_num (F.K.A. hub6) */	{  0,     0, PRSLOT_SMC1, CPMVEC_SMC1,   0,    0 }    /* SMC1 ttyS0 */#if USE_SMC2	,{ 0,     0, PRSLOT_SMC2, CPMVEC_SMC2,   0,    1 }     /* SMC2 ttyS1 */#endif#if defined(CONFIG_M68360_SCC_UART)	,{ 0,     0, PRSLOT_SCC2, CPMVEC_SCC2,   0, (NUM_IS_SCC | 1) }    /* SCC2 ttyS2 */	,{ 0,     0, PRSLOT_SCC3, CPMVEC_SCC3,   0, (NUM_IS_SCC | 2) }    /* SCC3 ttyS3 */	,{ 0,     0, PRSLOT_SCC4, CPMVEC_SCC4,   0, (NUM_IS_SCC | 3) }    /* SCC4 ttyS4 */#endif};#define NR_PORTS	(sizeof(rs_table)/sizeof(struct serial_state))static struct tty_struct *serial_table[NR_PORTS];static struct termios *serial_termios[NR_PORTS];static struct termios *serial_termios_locked[NR_PORTS];/* The number of buffer descriptors and their sizes. */#define RX_NUM_FIFO	4#define RX_BUF_SIZE	32#define TX_NUM_FIFO	4#define TX_BUF_SIZE	32#define CONSOLE_NUM_FIFO 2#define CONSOLE_BUF_SIZE 4char *console_fifos[CONSOLE_NUM_FIFO * CONSOLE_BUF_SIZE];#ifndef MIN#define MIN(a,b)	((a) < (b) ? (a) : (b))#endif/* The async_struct in serial.h does not really give us what we * need, so define our own here. */typedef struct serial_info {	int			magic;	int			flags;	struct serial_state		*state; 	/* struct serial_struct	*state; */ 	/* struct async_struct	*state; */		struct tty_struct 	*tty;	int			read_status_mask;	int			ignore_status_mask;	int			timeout;	int			line;	int			x_char;	/* xon/xoff character */	int			close_delay;	unsigned short		closing_wait;	unsigned short		closing_wait2;	unsigned long		event;	unsigned long		last_active;	int			blocked_open; /* # of blocked opens */	long			session; /* Session of opening process */	long			pgrp; /* pgrp of opening process */	struct tq_struct	tqueue;	struct tq_struct	tqueue_hangup; 	wait_queue_head_t	open_wait;  	wait_queue_head_t	close_wait; /*	struct wait_queue     *open_wait; *//*	struct wait_queue	   *close_wait;i */	/* CPM Buffer Descriptor pointers.	*/	QUICC_BD			*rx_bd_base;	QUICC_BD			*rx_cur;	QUICC_BD			*tx_bd_base;	QUICC_BD			*tx_cur;} ser_info_t;/* since kmalloc_init() does not get called until much after this initialization: */static ser_info_t  quicc_ser_info[NR_PORTS];static char rx_buf_pool[NR_PORTS * RX_NUM_FIFO * RX_BUF_SIZE];static char tx_buf_pool[NR_PORTS * TX_NUM_FIFO * TX_BUF_SIZE];static void change_speed(ser_info_t *info);static void rs_360_wait_until_sent(struct tty_struct *tty, int timeout);static inline int serial_paranoia_check(ser_info_t *info,					kdev_t device, const char *routine){#ifdef SERIAL_PARANOIA_CHECK	static const char *badmagic =		"Warning: bad magic number for serial struct (%s) in %s\n";	static const char *badinfo =		"Warning: null async_struct for (%s) in %s\n";	if (!info) {		printk(badinfo, kdevname(device), routine);		return 1;	}	if (info->magic != SERIAL_MAGIC) {		printk(badmagic, kdevname(device), routine);		return 1;	}#endif	return 0;}/* * This is used to figure out the divisor speeds and the timeouts, * indexed by the termio value.  The generic CPM functions are responsible * for setting and assigning baud rate generators for us. */static int baud_table[] = {	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,	9600, 19200, 38400, 57600, 115200, 230400, 460800, 0 };/* This sucks. There is a better way: */#if defined(CONFIG_CONSOLE_9600)  #define CONSOLE_BAUDRATE 9600#elif defined(CONFIG_CONSOLE_19200)  #define CONSOLE_BAUDRATE 19200#elif defined(CONFIG_CONSOLE_115200)  #define CONSOLE_BAUDRATE 115200#else#error "console baud rate undefined"#endif/* * ------------------------------------------------------------ * rs_stop() and rs_start() * * This routines are called before setting or resetting tty->stopped. * They enable or disable transmitter interrupts, as necessary. * ------------------------------------------------------------ */static void rs_360_stop(struct tty_struct *tty){	ser_info_t *info = (ser_info_t *)tty->driver_data;	int	idx;	unsigned long flags; 	volatile struct scc_regs *sccp; 	volatile struct smc_regs *smcp;	if (serial_paranoia_check(info, tty->device, "rs_stop"))		return;		save_flags(flags); cli();	idx = PORT_NUM(info->state->smc_scc_num);	if (info->state->smc_scc_num & NUM_IS_SCC) {		sccp = &pquicc->scc_regs[idx];		sccp->scc_sccm &= ~UART_SCCM_TX;	} else {		/* smcp = &cpmp->cp_smc[idx]; */		smcp = &pquicc->smc_regs[idx];		smcp->smc_smcm &= ~SMCM_TX;	}	restore_flags(flags);}static void rs_360_start(struct tty_struct *tty){	ser_info_t *info = (ser_info_t *)tty->driver_data;	int	idx;	unsigned long flags;	volatile struct scc_regs *sccp;	volatile struct smc_regs *smcp;	if (serial_paranoia_check(info, tty->device, "rs_stop"))		return;		save_flags(flags); cli();	idx = PORT_NUM(info->state->smc_scc_num);	if (info->state->smc_scc_num & NUM_IS_SCC) {		sccp = &pquicc->scc_regs[idx];		sccp->scc_sccm |= UART_SCCM_TX;	} else {		smcp = &pquicc->smc_regs[idx];		smcp->smc_smcm |= SMCM_TX;	}	restore_flags(flags);}/* * ---------------------------------------------------------------------- * * Here starts the interrupt handling routines.  All of the following * subroutines are declared as inline and are folded into * rs_interrupt().  They were separated out for readability's sake. * * Note: rs_interrupt() is a "fast" interrupt, which means that it * runs with interrupts turned off.  People who may want to modify * rs_interrupt() should try to keep the interrupt handler as fast as * possible.  After you are done making modifications, it is not a bad * idea to do: *  * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c * * and look at the resulting assemble code in serial.s. * * 				- Ted Ts'o (tytso@mit.edu), 7-Mar-93 * ----------------------------------------------------------------------- *//* * This routine is used by the interrupt handler to schedule * processing in the software interrupt portion of the driver. */static _INLINE_ void rs_sched_event(ser_info_t *info,				  int event){	info->event |= 1 << event;	queue_task(&info->tqueue, &tq_serial);	mark_bh(SERIAL_BH);}static _INLINE_ void receive_chars(ser_info_t *info){	struct tty_struct *tty = info->tty;	unsigned char ch, *cp;	/*int	ignored = 0;*/	int	i;	ushort	status;	 struct	async_icount *icount; 	/* struct	async_icount_24 *icount; */	volatile QUICC_BD	*bdp;	icount = &info->state->icount;	/* Just loop through the closed BDs and copy the characters into	 * the buffer.	 */	bdp = info->rx_cur;	for (;;) {		if (bdp->status & BD_SC_EMPTY)	/* If this one is empty */			break;			/*   we are all done */		/* The read status mask tell us what we should do with		 * incoming characters, especially if errors occur.		 * One special case is the use of BD_SC_EMPTY.  If		 * this is not set, we are supposed to be ignoring		 * inputs.  In this case, just mark the buffer empty and		 * continue.		 */		if (!(info->read_status_mask & BD_SC_EMPTY)) {			bdp->status |= BD_SC_EMPTY;			bdp->status &=				~(BD_SC_BR | BD_SC_FR | BD_SC_PR | BD_SC_OV);			if (bdp->status & BD_SC_WRAP)				bdp = info->rx_bd_base;			else				bdp++;			continue;		}		/* Get the number of characters and the buffer pointer.		*/		i = bdp->length;		/* cp = (unsigned char *)__va(bdp->buf); */		cp = (char *)bdp->buf;		status = bdp->status;		/* Check to see if there is room in the tty buffer for		 * the characters in our BD buffer.  If not, we exit		 * now, leaving the BD with the characters.  We'll pick		 * them up again on the next receive interrupt (which could		 * be a timeout).		 */		if ((tty->flip.count + i) >= TTY_FLIPBUF_SIZE)			break;		while (i-- > 0) {			ch = *cp++;			*tty->flip.char_buf_ptr = ch;			icount->rx++;#ifdef SERIAL_DEBUG_INTR			printk("DR%02x:%02x...", ch, status);#endif			*tty->flip.flag_buf_ptr = 0;			if (status & (BD_SC_BR | BD_SC_FR |				       BD_SC_PR | BD_SC_OV)) {				/*				 * For statistics only				 */				if (status & BD_SC_BR)					icount->brk++;				else if (status & BD_SC_PR)					icount->parity++;				else if (status & BD_SC_FR)					icount->frame++;				if (status & BD_SC_OV)					icount->overrun++;				/*				 * Now check to see if character should be				 * ignored, and mask off conditions which				 * should be ignored.				if (status & info->ignore_status_mask) {					if (++ignored > 100)						break;					continue;				}				 */				status &= info->read_status_mask;						if (status & (BD_SC_BR)) {#ifdef SERIAL_DEBUG_INTR					printk("handling break....");#endif

⌨️ 快捷键说明

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