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

📄 serial_8250.c

📁 16c554的驱动
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
 *  linux/drivers/char/serial_port4.c
 *
 *  Driver for S3C44B0  expand serial ports 
 *
 *  Based on drivers/char/serial.c, by Linus Torvalds, Theodore Ts'o.
 *
 *  Copyright 1999 ARM Limited
 *  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: s3c44b0.c,v 1.9.2.1 2001/11/27 17:35:39 rmk Exp $
 *
 * This is a generic driver for ARM S3C44B0-type serial ports.  They
 * have a lot of 16550-like features, but are not register compatable.
 * Note that although they do have CTS, DCD and DSR inputs, they do
 * not have an RI input, nor do they have DTR or RTS outputs.  If
 * required, these have to be supplied via some other means (eg, GPIO)
 * and hooked into this driver.
 */
#include <linux/config.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/signal.h>
#include <linux/sched.h>
#include <linux/interrupt.h>
#include <linux/tty.h>
#include <linux/tty_flip.h>
#include <linux/major.h>   //定义了设备的设备号
#include <linux/string.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/circ_buf.h>
#include <linux/serial.h>
#include <linux/console.h>
#include <linux/sysrq.h>
#include "serial_core.h"

#include <asm/system.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
#include <asm/bitops.h>
#include "serial_port4.h"

#if defined(CONFIG_SERIAL_PORT4_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)  //在autoconf.h中定义
#define SUPPORT_SYSRQ
#endif


#include "hardware.h" //<asm/hardware.h>

#define UART_NR		4

#define SERIAL_PORT4_MAJOR	6   //定义模块的主设备号(动态分配)
#define SERIAL_PORT4_MINOR	64 //定义模块的次设备号
#define SERIAL_PORT4_NR	UART_NR  //定义串口数目

#define CALLOUT_PORT4_NAME	"cua"
#define CALLOUT_PORT4_MAJOR	7
#define CALLOUT_PORT4_MINOR	64
#define CALLOUT_PORT4_NR	UART_NR

static struct tty_driver normal, callout;
static struct tty_struct *port4_table[UART_NR];  //在tty.h里定义;tty_driver、termios均在此结构中
static struct termios *port4_termios[UART_NR], *port4_termios_locked[UART_NR];
#ifdef SUPPORT_SYSRQ
static struct console port4_console;
#endif


/*
 * Access macros for the st16c554 UARTs
 
 */
 #define RHR		((port)->iobase + (void *)&UA_RHR - (void *)&UA_RHR)  //定义寄存器地址
#define THR		((port)->iobase + (void *)&UA_THR - (void *)&UA_RHR)
#define DLL		((port)->iobase + (void *)&UA_DLL - (void *)&UA_RHR)
#define DLM		((port)->iobase + (void *)&UA_DLM - (void *)&UA_RHR)
#define IER		((port)->iobase + (void *)&UA_IER - (void *)&UA_RHR)
#define FCR		((port)->iobase + (void *)&UA_FCR - (void *)&UA_RHR)
#define ISR		((port)->iobase + (void *)&UA_ISR - (void *)&UA_RHR)
#define LCR		((port)->iobase + (void *)&UA_LCR - (void *)&UA_RHR)
#define MCR		((port)->iobase + (void *)&UA_MCR - (void *)&UA_RHR)
#define LSR		((port)->iobase + (void *)&UA_LSR - (void *)&UA_RHR)
#define MSR		((port)->iobase + (void *)&UA_MSR - (void *)&UA_RHR)
#define SPR		((port)->iobase + (void *)&UA_SPR - (void *)&UA_RHR)

#define TEMP		0x20  //发送fifo为空
#define ALL_TEMP 0x60  //发送寄存器和发送fifo均为空
#define TEN     0xFD    //禁止发送数据中断标志

//#define UART_OE		1
//#define UART_PE		2
//#define UART_FE		3
//#define UART_BE		4
//#define UART_ANY_ERR	(UART_OE|UART_PE|UART_FE)

//#define UART_RTS	1
//#define UART_CTS	1
#define LSR_ENABLE	0x10    //产生break signal
#define CONFIG_PORT4_CLK  29491200

#define LCR_CS5	0
#define LCR_CS6	1
#define LCR_CS7	2
#define LCR_CS8	3
#define LCR_STOPB	4
#define LCR_PARITY	0x08
#define LCR_PEVEN	0x10

#define FCR_TRIGGER_LEVEL 14      //触发级别
#ifndef EXTINPND
#define EXTINPND 0x01d20054       //外部中断请求寄存器
#endif
//#define TINPND 0x01e00004      


static void port4_stop_tx(struct uart_port *port, u_int from_tty)  //串口禁止发送函数
{
	unsigned char status;
	status=(inb(IER)&TEN);   //禁止发送数据中断
	outb(status,IER);
}

static void port4_start_tx(struct uart_port *port, u_int nonempty, u_int from_tty)  //串口使能发送函数
{
	unsigned char status;
	
	if(nonempty){
	outb(0xc5,FCR);    //设置触发级别为14,使能fifo,并清除发送fifo里的内容
	status=(inb(IER)|0x02);   //使能发送数据准备好中断(fifo为空)
	outb(status,IER);
	}
}

static void port4_stop_rx(struct uart_port *port)//串口禁止接收函数
{ 
	unsigned char status=(inb(IER)&0xFE);   //禁止接收数据中断
	outb(status,IER);
}

static void port4_enable_ms(struct uart_port *port) //调制解调使能函数
{
}

static void port4_rx_int(int irq, void *dev_id, struct pt_regs *regs)   //串口接收中断处理函数
{	struct uart_info *info = dev_id;       
	struct tty_struct *tty = info->tty;
	unsigned int  uer, ch, flg, ignored = 0;
	unsigned char status;
	struct uart_port *port = info->port;
	
	status = inb(LSR);
	while(status&0x01){                     //RFCNT_MASK为0x0f,将发送fifo中的个数清零
	//	uer = inl(UERSTAT);                         //读出出错状态
		ch = inb(RHR);                             //接收数据

		if(tty->flip.count >= TTY_FLIPBUF_SIZE)
			goto ignore_char;
		port->icount.rx++;     

		flg = TTY_NORMAL;      

	//	if(uer&UART_ANY_ERR)      //检验接收错误
		//	goto handle_error;
		if(uart_handle_sysrq_char(info, 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 = inb(LSR);
	}
//out:
	tty_flip_buffer_push(tty);//将当前 flip 缓冲中的数据推给用户
	return;

//handle_error:
//	if(uer&UART_PE)
	//	port->icount.parity++;
//	else if(uer&UART_FE)
//		port->icount.frame++;
//	if(uer&UART_OE)
	//	port->icount.overrun++;

//if(uer&port->ignore_status_mask){
//		if( ++ignored > 100)
	//		goto out;
	//	goto ignore_char;
	//}
	//uer &= port->read_status_mask;

	//if(uer&UART_PE)
//		flg = TTY_PARITY;
//	else if(uer&UART_FE)
	//	flg = TTY_FRAME;

   //     if (uer&UART_OE) {
		/*
		 * CHECK: does overrun affect the current character?
		 * ASSUMPTION: it does not.
		 */
    //            *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
        info->sysrq = 0;
#endif
        goto error_return;
	}

static void port4_tx_int(int irq, void *dev_id, struct pt_regs *regs) //串口发送中断处理函数
{	struct uart_info *info = dev_id;
	int count;
	unsigned char status1;
	struct uart_port *port = info->port;
	if(irq=21)
		outb(0x01,EXTINPND);

	if (port->x_char) {
		outb(port->x_char, THR); //把要发送的数据放在THR里
		port->icount.tx++;
		port->x_char = 0;     //取0的目的是不重复发送一个数据
		return;
	}
	if (info->xmit.head == info->xmit.tail//当info发送队列满或tty停止,则禁止串口发送数据
	    || info->tty->stopped
	    || info->tty->hw_stopped) {
		port4_stop_tx(port, 0);
		return;
	}
//#define FIFOSIZE 16
//count=FIFOSIZE;
    status1 = inb(LSR);
   while((status1&TEMP)==0){   //当发送FIFO不满的时候
		outb(info->xmit.buf[info->xmit.tail], THR);//把info的发送队列里的数据放在THR里
		#define UART_XMIT_SIZE 1024
		info->xmit.tail = (info->xmit.tail + 1) & (UART_XMIT_SIZE - 1);//UART_XMIT_SIZE定义在serial.h里,为1024
		port->icount.tx++;       //port->icount.tx是接着上面计数的
    	//count--;
		if (info->xmit.head == info->xmit.tail)  //表明info发送队列已满,不能再发送了
			break;
	};
#define WAKEUP_CHRS 256
#define EVT_WRITE_WAKEUP 0
	if (CIRC_CNT(info->xmit.head,
		     info->xmit.tail,
		     UART_XMIT_SIZE) < WAKEUP_CHARS)   //WAKEUP_CHARS定义在serial.h里。为256;CIRC_CNT函数定义在circ_buf.h里
		uart_event(info, EVT_WRITE_WAKEUP);//EVT_WRITE_WAKEUP定义在serial_core.h里,值为0; 

	if (info->xmit.head == info->xmit.tail)
		port4_stop_tx(info->port, 0);

}

static u_int port4_tx_empty(struct uart_port *port)//该函数将检查参数port所对应的发送队列是否为空
{
	return (!(inb(LSR)&0x60)) ?  0:TIOCSER_TEMT; 
}

static u_int port4_get_mctrl(struct uart_port *port) //该函数对调制解调控制
{
	//return (inl(UMSTAT)&UART_CTS) ? TIOCM_CTS : 0;
}

static void port4_set_mctrl(struct uart_port *port, u_int mctrl) //该函数对调制解调设制
{
	//unsigned int status;

	//status = inl(UMCON);

	//if(mctrl & TIOCM_RTS)
	//	status |= UART_RTS;
	//else
	//	status &= ~UART_RTS;

	//outl(status, UMCON);
}

//控制暂停信号的传输。如果break_state不为零,中断信号将被传输。
//当有凭借为零的break_state产生的其他调用时,该信号应终止。
static void port4_break_ctl(struct uart_port *port, int break_state)
{unsigned char status;
	//int statue;

	status = inb(LSR);
	if(break_state)
		status |=LSR_ENABLE ;
	else
		status &= ~LSR_ENABLE;
	outb(status, LSR);
	

}
static int port4_startup(struct uart_port *port, struct uart_info *info) //申请中断
{
	int retv;
	unsigned int status;

retv = request_irq(port->irq, port4_tx_int, 0,"port4_uart_tx", info);
	if(retv)
		return retv;
	retv = request_irq(port->irq, port4_rx_int, 0,"port4_uart_rx", info);
	if(retv){
		free_irq(port->irq, info);
		return retv;
	}

	outb(0xc7, FCR);
	//outl(0xc4, ISR);
	//INT_ENABLE(port->irq);
	status=inb(IER)|0x01;
	outb(status,IER);

	return 0;
}

static void port4_shutdown(struct uart_port *port, struct uart_info *info)
{
	free_irq(port->irq, info);
	//free_irq(port->irq+4, info);
	//outl(0x00, IER);
}

static void port4_change_speed(struct uart_port *port, u_int cflag, u_int iflag, u_int quot)
{
  unsigned int flags;
	unsigned char lcr = 0;
	//#define CSIZE 

	switch(cflag&CSIZE){

⌨️ 快捷键说明

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