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

📄 riscom8.c

📁 powerpc内核mpc8241linux系统下char驱动程序
💻 C
📖 第 1 页 / 共 4 页
字号:
/* *      linux/drivers/char/riscom.c  -- RISCom/8 multiport serial driver. * *      Copyright (C) 1994-1996  Dmitry Gorodchanin (pgmdsg@ibi.com) * *      This code is loosely based on the Linux serial driver, written by *      Linus Torvalds, Theodore T'so and others. The RISCom/8 card  *      programming info was obtained from various drivers for other OSes  *	(FreeBSD, ISC, etc), but no source code from those drivers were  *	directly included in this driver. * * *      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., 675 Mass Ave, Cambridge, MA 02139, USA. * *	Revision 1.0  */#include <linux/module.h>#include <asm/io.h>#include <linux/kernel.h>#include <linux/sched.h>#include <linux/ioport.h>#include <linux/interrupt.h>#include <linux/errno.h>#include <linux/tty.h>#include <linux/mm.h>#include <linux/serial.h>#include <linux/fcntl.h>#include <linux/major.h>#include <linux/init.h>#include <asm/uaccess.h>#include "riscom8.h"#include "riscom8_reg.h"/* Am I paranoid or not ? ;-) */#define RISCOM_PARANOIA_CHECK/*  * Crazy InteliCom/8 boards sometimes has swapped CTS & DSR signals. * You can slightly speed up things by #undefing the following option, * if you are REALLY sure that your board is correct one.  */#define RISCOM_BRAIN_DAMAGED_CTS/*  * The following defines are mostly for testing purposes. But if you need * some nice reporting in your syslog, you can define them also. */#undef RC_REPORT_FIFO#undef RC_REPORT_OVERRUN#define RISCOM_LEGAL_FLAGS \	(ASYNC_HUP_NOTIFY   | ASYNC_SAK          | ASYNC_SPLIT_TERMIOS   | \	 ASYNC_SPD_HI       | ASYNC_SPEED_VHI    | ASYNC_SESSION_LOCKOUT | \	 ASYNC_PGRP_LOCKOUT | ASYNC_CALLOUT_NOHUP)#ifndef MIN#define MIN(a,b) ((a) < (b) ? (a) : (b))#endifDECLARE_TASK_QUEUE(tq_riscom);#define RISCOM_TYPE_NORMAL	1#define RISCOM_TYPE_CALLOUT	2static struct riscom_board * IRQ_to_board[16] = { NULL, } ;static struct tty_driver riscom_driver, riscom_callout_driver;static int    riscom_refcount = 0;static struct tty_struct * riscom_table[RC_NBOARD * RC_NPORT] = { NULL, };static struct termios * riscom_termios[RC_NBOARD * RC_NPORT] = { NULL, };static struct termios * riscom_termios_locked[RC_NBOARD * RC_NPORT] = { NULL, };static unsigned char * tmp_buf = NULL;static struct semaphore tmp_buf_sem = MUTEX;static unsigned long baud_table[] =  {	0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,	9600, 19200, 38400, 57600, 76800, 0, };static struct riscom_board rc_board[RC_NBOARD] =  {	{ 0, RC_IOBASE1, 0, },	{ 0, RC_IOBASE2, 0, },	{ 0, RC_IOBASE3, 0, },	{ 0, RC_IOBASE4, 0, },};static struct riscom_port rc_port[RC_NBOARD * RC_NPORT] =  {	{ 0, },};		/* RISCom/8 I/O ports addresses (without address translation) */static unsigned short rc_ioport[] =  {#if 1		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c,#else		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0a, 0x0b, 0x0c, 0x10,	0x11, 0x12, 0x18, 0x28, 0x31, 0x32, 0x39, 0x3a, 0x40, 0x41, 0x61, 0x62,	0x63, 0x64, 0x6b, 0x70, 0x71, 0x78, 0x7a, 0x7b, 0x7f, 0x100, 0x101#endif	};#define RC_NIOPORT	(sizeof(rc_ioport) / sizeof(rc_ioport[0]))static inline int rc_paranoia_check(struct riscom_port const * port,				    kdev_t device, const char *routine){#ifdef RISCOM_PARANOIA_CHECK	static const char *badmagic =		"rc: Warning: bad riscom port magic number for device %s in %s\n";	static const char *badinfo =		"rc: Warning: null riscom port for device %s in %s\n";	if (!port) {		printk(badinfo, kdevname(device), routine);		return 1;	}	if (port->magic != RISCOM8_MAGIC) {		printk(badmagic, kdevname(device), routine);		return 1;	}#endif	return 0;}/* *  *  Service functions for RISCom/8 driver. *  *//* Get board number from pointer */extern inline int board_No (struct riscom_board const * bp){	return bp - rc_board;}/* Get port number from pointer */extern inline int port_No (struct riscom_port const * port){	return RC_PORT(port - rc_port); }/* Get pointer to board from pointer to port */extern inline struct riscom_board * port_Board(struct riscom_port const * port){	return &rc_board[RC_BOARD(port - rc_port)];}/* Input Byte from CL CD180 register */extern inline unsigned char rc_in(struct riscom_board const * bp, unsigned short reg){	return inb(bp->base + RC_TO_ISA(reg));}/* Output Byte to CL CD180 register */extern inline void rc_out(struct riscom_board const * bp, unsigned short reg,			  unsigned char val){	outb(val, bp->base + RC_TO_ISA(reg));}/* Wait for Channel Command Register ready */extern inline void rc_wait_CCR(struct riscom_board const * bp){	unsigned long delay;	/* FIXME: need something more descriptive then 100000 :) */	for (delay = 100000; delay; delay--) 		if (!rc_in(bp, CD180_CCR))			return;		printk("rc%d: Timeout waiting for CCR.\n", board_No(bp));}/* *  RISCom/8 probe functions. */extern inline int rc_check_io_range(struct riscom_board * const bp){	int i;		for (i = 0; i < RC_NIOPORT; i++)  		if (check_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1))  {			printk("rc%d: Skipping probe at 0x%03x. I/O address in use.\n",			       board_No(bp), bp->base);			return 1;		}	return 0;}extern inline void rc_request_io_range(struct riscom_board * const bp){	int i;		for (i = 0; i < RC_NIOPORT; i++) 		request_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1, "RISCom/8" );}extern inline void rc_release_io_range(struct riscom_board * const bp){	int i;		for (i = 0; i < RC_NIOPORT; i++)  		release_region(RC_TO_ISA(rc_ioport[i]) + bp->base, 1);}	/* Must be called with enabled interrupts */extern inline void rc_long_delay(unsigned long delay){	unsigned long i;		for (i = jiffies + delay; time_after(i,jiffies); ) ;}/* Reset and setup CD180 chip */__initfunc(static void rc_init_CD180(struct riscom_board const * bp)){	unsigned long flags;		save_flags(flags); cli();	rc_out(bp, RC_CTOUT, 0);     	           /* Clear timeout             */	rc_wait_CCR(bp);			   /* Wait for CCR ready        */	rc_out(bp, CD180_CCR, CCR_HARDRESET);      /* Reset CD180 chip          */	sti();	rc_long_delay(HZ/20);                      /* Delay 0.05 sec            */	cli();	rc_out(bp, CD180_GIVR, RC_ID);             /* Set ID for this chip      */	rc_out(bp, CD180_GICR, 0);                 /* Clear all bits            */	rc_out(bp, CD180_PILR1, RC_ACK_MINT);      /* Prio for modem intr       */	rc_out(bp, CD180_PILR2, RC_ACK_TINT);      /* Prio for transmitter intr */	rc_out(bp, CD180_PILR3, RC_ACK_RINT);      /* Prio for receiver intr    */		/* Setting up prescaler. We need 4 ticks per 1 ms */	rc_out(bp, CD180_PPRH, (RC_OSCFREQ/(1000000/RISCOM_TPS)) >> 8);	rc_out(bp, CD180_PPRL, (RC_OSCFREQ/(1000000/RISCOM_TPS)) & 0xff);		restore_flags(flags);}/* Main probing routine, also sets irq. */__initfunc(static int rc_probe(struct riscom_board *bp)){	unsigned char val1, val2;	int irqs = 0;	int retries;		bp->irq = 0;	if (rc_check_io_range(bp))		return 1;		/* Are the I/O ports here ? */	rc_out(bp, CD180_PPRL, 0x5a);	outb(0xff, 0x80);	val1 = rc_in(bp, CD180_PPRL);	rc_out(bp, CD180_PPRL, 0xa5);	outb(0x00, 0x80);	val2 = rc_in(bp, CD180_PPRL);		if ((val1 != 0x5a) || (val2 != 0xa5))  {		printk("rc%d: RISCom/8 Board at 0x%03x not found.\n",		       board_No(bp), bp->base);		return 1;	}		/* It's time to find IRQ for this board */	for (retries = 0; retries < 5 && irqs <= 0; retries++)  {		irqs = probe_irq_on();		rc_init_CD180(bp);	       		/* Reset CD180 chip       */		rc_out(bp, CD180_CAR, 2);               /* Select port 2          */		rc_wait_CCR(bp);		rc_out(bp, CD180_CCR, CCR_TXEN);        /* Enable transmitter     */		rc_out(bp, CD180_IER, IER_TXRDY);       /* Enable tx empty intr   */		rc_long_delay(HZ/20);	       				irqs = probe_irq_off(irqs);		val1 = rc_in(bp, RC_BSR);		/* Get Board Status reg   */		val2 = rc_in(bp, RC_ACK_TINT);          /* ACK interrupt          */		rc_init_CD180(bp);	       		/* Reset CD180 again      */			if ((val1 & RC_BSR_TINT) || (val2 != (RC_ID | GIVR_IT_TX)))  {			printk("rc%d: RISCom/8 Board at 0x%03x not found.\n",			       board_No(bp), bp->base);			return 1;		}	}		if (irqs <= 0)  {		printk("rc%d: Can't find IRQ for RISCom/8 board at 0x%03x.\n",		       board_No(bp), bp->base);		return 1;	}	rc_request_io_range(bp);	bp->irq = irqs;	bp->flags |= RC_BOARD_PRESENT;		printk("rc%d: RISCom/8 Rev. %c board detected at 0x%03x, IRQ %d.\n",	       board_No(bp),	       (rc_in(bp, CD180_GFRCR) & 0x0f) + 'A',   /* Board revision */	       bp->base, bp->irq);		return 0;}/*  *  *  Interrupt processing routines. *  */extern inline void rc_mark_event(struct riscom_port * port, int event){	/*          * I'm not quite happy with current scheme all serial	 * drivers use their own BH routine.         * It seems this easily can be done with one BH routine	 * serving for all serial drivers.	 * For now I must introduce another one - RISCOM8_BH.	 * Still hope this will be changed in near future.         */	set_bit(event, &port->event);	queue_task(&port->tqueue, &tq_riscom);	mark_bh(RISCOM8_BH);}extern inline struct riscom_port * rc_get_port(struct riscom_board const * bp,					       unsigned char const * what){	unsigned char channel;	struct riscom_port * port;		channel = rc_in(bp, CD180_GICR) >> GICR_CHAN_OFF;	if (channel < CD180_NCH)  {		port = &rc_port[board_No(bp) * RC_NPORT + channel];		if (port->flags & ASYNC_INITIALIZED)  {			return port;		}	}	printk("rc%d: %s interrupt from invalid port %d\n", 	       board_No(bp), what, channel);	return NULL;}extern inline void rc_receive_exc(struct riscom_board const * bp){	struct riscom_port *port;	struct tty_struct *tty;	unsigned char status;	unsigned char ch;		if (!(port = rc_get_port(bp, "Receive")))		return;	tty = port->tty;	if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {		printk("rc%d: port %d: Working around flip buffer overflow.\n",		       board_No(bp), port_No(port));		return;	}	#ifdef RC_REPORT_OVERRUN		status = rc_in(bp, CD180_RCSR);	if (status & RCSR_OE)  {		port->overrun++;#if 0				printk("rc%d: port %d: Overrun. Total %ld overruns.\n", 		       board_No(bp), port_No(port), port->overrun);#endif			}	status &= port->mark_mask;#else		status = rc_in(bp, CD180_RCSR) & port->mark_mask;#endif		ch = rc_in(bp, CD180_RDR);	if (!status)  {		return;	}	if (status & RCSR_TOUT)  {		printk("rc%d: port %d: Receiver timeout. Hardware problems ?\n", 		       board_No(bp), port_No(port));		return;			} else if (status & RCSR_BREAK)  {		printk("rc%d: port %d: Handling break...\n",		       board_No(bp), port_No(port));		*tty->flip.flag_buf_ptr++ = TTY_BREAK;		if (port->flags & ASYNC_SAK)			do_SAK(tty);			} else if (status & RCSR_PE) 		*tty->flip.flag_buf_ptr++ = TTY_PARITY;		else if (status & RCSR_FE) 		*tty->flip.flag_buf_ptr++ = TTY_FRAME;	        else if (status & RCSR_OE)		*tty->flip.flag_buf_ptr++ = TTY_OVERRUN;		else		*tty->flip.flag_buf_ptr++ = 0;		*tty->flip.char_buf_ptr++ = ch;	tty->flip.count++;	queue_task(&tty->flip.tqueue, &tq_timer);}extern inline void rc_receive(struct riscom_board const * bp){	struct riscom_port *port;	struct tty_struct *tty;	unsigned char count;		if (!(port = rc_get_port(bp, "Receive")))		return;		tty = port->tty;		count = rc_in(bp, CD180_RDCR);	#ifdef RC_REPORT_FIFO	port->hits[count > 8 ? 9 : count]++;#endif			while (count--)  {		if (tty->flip.count >= TTY_FLIPBUF_SIZE)  {			printk("rc%d: port %d: Working around flip buffer overflow.\n",			       board_No(bp), port_No(port));			break;		}		*tty->flip.char_buf_ptr++ = rc_in(bp, CD180_RDR);		*tty->flip.flag_buf_ptr++ = 0;		tty->flip.count++;	}	queue_task(&tty->flip.tqueue, &tq_timer);}extern inline void rc_transmit(struct riscom_board const * bp){	struct riscom_port *port;	struct tty_struct *tty;	unsigned char count;			if (!(port = rc_get_port(bp, "Transmit")))		return;		tty = port->tty;		if (port->IER & IER_TXEMPTY)  {		/* FIFO drained */		rc_out(bp, CD180_CAR, port_No(port));		port->IER &= ~IER_TXEMPTY;		rc_out(bp, CD180_IER, port->IER);		return;	}		if ((port->xmit_cnt <= 0 && !port->break_length)	    || tty->stopped || tty->hw_stopped)  {		rc_out(bp, CD180_CAR, port_No(port));

⌨️ 快捷键说明

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