终端设备驱动:
终端设备分类:串行端口终端(/dev/ttySn), 伪端口(/dev/pty), 控制台终端(/dev/ttyn, /dev/console) 。
终端设备驱动结构:包含tty核心,tty线路规程(以特殊的方式格式化从一个用户或硬件收到到数据,ppp或bluetooth),tty驱动。
tty设备发送数据流程:tty核心从用户获取数据---->tty线程规程驱动----->tty驱动----->硬件。接收数据,相反。
Linux系统的串口驱动与一般字符设备并一样,它采用层次化的架构,从而看做是一个串行系统来实现。
(1) 关注UART或其他底层串行硬件特征的底层驱动程序。
(2) 和底层驱动程序接口的TTY驱动程序。
(3) 加工用于和TTY驱动程序交换数据的线路规程。
下图描述了串行系统间的层次结构关系(s3c2440串口实现例),可以概括为:用户应用层 --> 线路规划层 --> TTY层 --> 底层驱动层 --> 物理硬件层
线路规程和TTY驱动程序是与硬件平台无关的,Linux源码中已经提供了实现,所以对于具体的平台,我们只需实现底层驱动程序即可,这也是我们最关心的。在s3c2440a中,主要由dirivers/serial/下的s3c2440.c和samsung.c实现。
Uart驱动程序主要围绕三个关键的数据结构展开(include/linux/serial_core.h中定义):
UART特定的驱动程序结构定义:struct uart_driver s3c24xx_uart_drv;
UART端口结构定义: struct uart_port s3c24xx_serial_ops;
UART相关操作函数结构定义: struct uart_ops s3c24xx_serial_ops;
tty设备驱动的主体工作是填充tty_driver结构体:
struct tty_driver {
int
magic;
/* magic number for this structure ,在alloc_tty_driver()中被初始化*/
struct kref kref;
/* Reference management */
struct cdev cdev;
//对应到字符设备cdev
struct module
*owner;
const char
*driver_name;
const char
*name;
//设备名
int
name_base;
/* offset of printed name */
int
major;
/* major device number */
int
minor_start;
/* start of minor device number */
int
minor_num;
/* number of *possible* devices */
int
num;
/* number of devices allocated */
short
type;
/* type of tty driver */
short
subtype;
/* subtype of tty driver */
struct ktermios init_termios; /* Initial termios */
int
flags;
/* tty driver flags */
struct proc_dir_entry *proc_entry; /* /proc fs entry */
struct tty_driver *other; /* only used for the PTY driver */
/*
* Pointer to the tty data structures
*/
struct tty_struct **ttys;
struct ktermios **termios;
struct ktermios **termios_locked;
void *driver_state;
/*
* Driver methods
*/
const struct tty_operations *ops;
struct list_head tty_drivers;
};
tty_operations结构体:
struct tty_operations {
struct tty_struct * (*lookup)(struct tty_driver *driver,
struct inode *inode, int idx);
int (*install)(struct tty_driver *driver, struct tty_struct *tty);
void (*remove)(struct tty_driver *driver, struct tty_struct *tty);
int (*open)(struct tty_struct * tty, struct file * filp);
void (*close)(struct tty_struct * tty, struct file * filp);
void (*shutdown)(struct tty_struct *tty);
void (*cleanup)(struct tty_struct *tty);
int (*write)(struct tty_struct * tty,
const unsigned char *buf, int count);
int (*put_char)(struct tty_struct *tty, unsigned char ch);
//单个字节写函数
void (*flush_chars)(struct tty_struct *tty);
//刷新数据到硬件
int (*write_room)(struct tty_struct *tty);
//指示有多少缓冲区空闲
int (*chars_in_buffer)(struct tty_struct *tty);
//指示缓冲区到长度
int (*ioctl)(struct tty_struct *tty, struct file * file,
//
unsigned int cmd, unsigned long arg);
long (*compat_ioctl)(struct tty_struct *tty, struct file * file,
unsigned int cmd, unsigned long arg);
void (*set_termios)(struct tty_struct *tty, struct ktermios * old);
void (*throttle)(struct tty_struct * tty);
//tty核心输入缓冲满时调用
void (*unthrottle)(struct tty_struct * tty);
//tty核心输入缓冲已被清空时调用
void (*stop)(struct tty_struct *tty);
//停止tty驱动发数据给设备
void (*start)(struct tty_struct *tty);
//恢复tty驱动发数据给设备
void (*hangup)(struct tty_struct *tty);
int (*break_ctl)(struct tty_struct *tty, int state);
void (*flush_buffer)(struct tty_struct *tty);
//刷新缓冲区,丢弃剩下的数据
void (*set_ldisc)(struct tty_struct *tty);
void (*wait_until_sent)(struct tty_struct *tty, int timeout);
void (*send_xchar)(struct tty_struct *tty, char ch);
int (*tiocmget)(struct tty_struct *tty, struct file *file);
int (*tiocmset)(struct tty_struct *tty, struct file *file,
unsigned int set, unsigned int clear);
int (*resize)(struct tty_struct *tty, struct winsize *ws);
int (*set_termiox)(struct tty_struct *tty, struct termiox *tnew);
int (*get_icount)(struct tty_struct *tty,
struct serial_icounter_struct *icount);
#ifdef CONFIG_CONSOLE_POLL
int (*poll_init)(struct tty_driver *driver, int line, char *options);
int (*poll_get_char)(struct tty_driver *driver, int line);
void (*poll_put_char)(struct tty_driver *driver, int line, char ch);
#endif
const struct file_operations *proc_fops;
};
1.分配tty驱动:
struct tty_driver *alloc_tty_driver(int lines);line为要分配到设备数量。
2.注册tty驱动:
int tty_register_driver(struct tty_driver *driver);
3.注销tty驱动:
int tty_unregister_driver(struct tty_driver *driver);
4.注册tty设备:
void tty_register_device(struct tty_driver *driver, unsigned index, struct device *device);用于注册关联于tty_driver的设备。
5.注销tty设备:
void tty_unregister_device(struct tty_driver *driver, unsigned index);
6.设置tty驱动操作:
void tty_set_operations(struct tty_driver *driver, struct tty_operations *op);
终端设备驱动应该包含:
1.模块加载和卸载函数:完成注册和注销tty_driver, 初始化和释放终端设备对应的tty_driver结构体成员及硬件资源。
2.实现tty_operations结构体中的函数:open, close, write,tiocmget, tiocmset等。
tty线路设置:
改变tty设备的线路设置或获取当前线路设置的两种方式:
1.调用用户空间的termios库函数。
int tcgetattr(int fd, struct termios *termios_p);
//获取终端设备的操作模式
int tcsetattr(int fd, int optional_actions, struct termios *termios_p);
//设置终端设备的操作模式
输入/输出波特率的获取和设置:
speed_t cfgetospeed(struct termios *termios_p);
//获得输出波特率
speed_t cfgetispeed(struct termios *termios_p);
//获得输入波特率
int cfsetospeed(struct termios *termios_p, speed_t speed);
//设置输出波特率
int cfsetispeed(struct termios *termios_p, speed_t speed);
//设置输入波特率
线路控制函数:
int tcdrain(int fd);
//等待所有输出都被发送
int tcflush(int fd, int queue_selector);
//flush输入/输出缓存
int tcflow(int fd, int action);
//对输入和输出流进行控制
int tcsendbreak(int fd, int duration);
//发送break
2.对tty设备节点进行ioctl()调用
termios库函数会被转化为对tty设备节点的ioctl()调用。
UART设备驱动:
linux内涵在serial_core.c文件中实现了UART设备的通用tty驱动层(串口核心层)。因此UART驱动的任务就变为实现serial_core.c中定义的一组uart_xxx接口。
串口核心层为串口设备驱动提供了3个结构体: uart_driver, uart_port, uart_ops。
struct uart_driver {
struct module
*owner;
const char
*driver_name;
const char
*dev_name;
int
major;
int
minor;
int
nr;
struct console
*cons;
/*
* these are private; the low level driver should not
* touch these; they should be initialised to NULL
*/
struct uart_state
*state;
struct tty_driver
*tty_driver;
};
UART驱动注册/注销函数:
int uart_register_driver(struct uart_driver *drv);
void uart_unregister_driver(struct uart_driver *drv);
这两个函数的实现,包含了tty_register_driver()和tty_unregister_driver()。
struct uart_port {
spinlock_t
lock;
/* port lock */
unsigned long
iobase;
/* in/out[bwl] */IO端口基地址
unsigned char __iomem
*membase;
/* read/write[bwl] */IO内存基地址
unsigned int
(*serial_in)(struct uart_port *, int);
void
(*serial_out)(struct uart_port *, int, int);
unsigned int
irq;
/* irq number */
unsigned long
irqflags;
/* irq flags */
unsigned int
uartclk;
/* base uart clock */
unsigned int
fifosize;
/* tx fifo size */
unsigned char
x_char;
/* xon/xoff char */
unsigned char
regshift;
/* reg offset shift */
unsigned char
iotype;
/* io access style */
unsigned char
unused1;
#define UPIO_PORT
(0)
#define UPIO_HUB6
(1)
#define UPIO_MEM
(2)
#define UPIO_MEM32
(3)
#define UPIO_AU
(4)
/* Au1x00 type IO */
#define UPIO_TSI
(5)
/* Tsi108/109 type IO */
#define UPIO_DWAPB
(6)
/* DesignWare APB UART */
#define UPIO_RM9000
(7)
/* RM9000 type IO */
unsigned int
read_status_mask;
/* driver specific */驱动特定的读状态掩码
unsigned int
ignore_status_mask;
/* driver specific */驱动特定的忽略状态掩码
struct uart_state
*state;
/* pointer to parent state */
struct uart_icount
icount;
/* statistics */
struct console
*cons;
/* struct console, if any */
#if defined(CONFIG_SERIAL_CORE_CONSOLE) || defined(SUPPORT_SYSRQ)
unsigned long
sysrq;
/* sysrq timeout */
#endif
upf_t
flags;
#define UPF_FOURPORT
((__force upf_t) (1 << 1))
#define UPF_SAK
((__force upf_t) (1 << 2))
#define UPF_SPD_MASK
((__force upf_t) (0x1030))
#define UPF_SPD_HI
((__force upf_t) (0x0010))
#define UPF_SPD_VHI
((__force upf_t) (0x0020))
#define UPF_SPD_CUST
((__force upf_t) (0x0030))
#define UPF_SPD_SHI
((__force upf_t) (0x1000))
#define UPF_SPD_WARP
((__force upf_t) (0x1010))
#define UPF_SKIP_TEST
((__force upf_t) (1 << 6))
#define UPF_AUTO_IRQ
((__force upf_t) (1 << 7))
#define UPF_HARDPPS_CD
((__force upf_t) (1 << 11))
#define UPF_LOW_LATENCY
((__force upf_t) (1 << 13))
#define UPF_BUGGY_UART
((__force upf_t) (1 << 14))
#define UPF_NO_TXEN_TEST
((__force upf_t) (1 << 15))
#define UPF_MAGIC_MULTIPLIER
((__force upf_t) (1 << 16))
#define UPF_CONS_FLOW
((__force upf_t) (1 << 23))
#define UPF_SHARE_IRQ
((__force upf_t) (1 << 24))
/* The exact UART type is known and should not be probed. */
#define UPF_FIXED_TYPE
((__force upf_t) (1 << 27))
#define UPF_BOOT_AUTOCONF
((__force upf_t) (1 << 28))
#define UPF_FIXED_PORT
((__force upf_t) (1 << 29))
#define UPF_DEAD
((__force upf_t) (1 << 30))
#define UPF_IOREMAP
((__force upf_t) (1 << 31))
#define UPF_CHANGE_MASK
((__force upf_t) (0x17fff))
#define UPF_USR_MASK
((__force upf_t) (UPF_SPD_MASK|UPF_LOW_LATENCY))
unsigned int
mctrl;
/* current modem ctrl settings */
unsigned int
timeout;
/* character-based timeout */
unsigned int
type;
/* port type */
const struct uart_ops
*ops;
unsigned int
custom_divisor;
unsigned int
line;
/* port index */
resource_size_t
mapbase;
/* for ioremap */
struct device
*dev;
/* parent device */
unsigned char
hub6;
/* this should be in the 8250 driver */
unsigned char
suspended;
unsigned char
unused[2];
void
*private_data;
/* generic platform data pointer */
};
uart_port用于描述一个UART端口到IO端口或IO内存地址,FIFO大小,端口类型等信息。
添加/删除端口函数:
int uart_add_one_port(struct uart_driver *drv, struct uart_port *port);
int uart_remove_one_port(struct uart_driver *drv, struct uart_port *port);
struct uart_ops {
unsigned int
(*tx_empty)(struct uart_port *);
void
(*set_mctrl)(struct uart_port *, unsigned int mctrl);
unsigned int
(*get_mctrl)(struct uart_port *);
void
(*stop_tx)(struct uart_port *);
void
(*start_tx)(struct uart_port *);
void
(*send_xchar)(struct uart_port *, char ch);
void
(*stop_rx)(struct uart_port *);
void
(*enable_ms)(struct uart_port *);
void
(*break_ctl)(struct uart_port *, int ctl);
int
(*startup)(struct uart_port *);
void
(*shutdown)(struct uart_port *);
void
(*flush_buffer)(struct uart_port *);
void
(*set_termios)(struct uart_port *, struct ktermios *new,
struct ktermios *old);
void
(*set_ldisc)(struct uart_port *);
void
(*pm)(struct uart_port *, unsigned int state,
unsigned int oldstate);
int
(*set_wake)(struct uart_port *, unsigned int state);
/*
* Return a string describing the type of the port
*/
const char *(*type)(struct uart_port *);
/*
* Release IO and memory resources used by the port.
* This includes iounmap if necessary.
*/
void
(*release_port)(struct uart_port *);
/*
* Request IO and memory resources used by the port.
* This includes iomapping the port if necessary.
*/
int
(*request_port)(struct uart_port *);
void
(*config_port)(struct uart_port *, int);
int
(*verify_port)(struct uart_port *, struct serial_struct *);
int
(*ioctl)(struct uart_port *, unsigned int, unsigned long);
#ifdef CONFIG_CONSOLE_POLL
void
(*poll_put_char)(struct uart_port *, unsigned char);
int
(*poll_get_char)(struct uart_port *);
#endif
};
uart_ops定义了对UART的一系列操作,包括发送,接收及线路设置等。
使用串口核心层这个通用串口tty驱动层的接口后,一个串口驱动要完成的主要工作就简单很多了。
s3c2440的UART驱动注释:
/* linux/drivers/serial/s3c2440.c*/
#include
#include
#include
#include
#include
#include //串口核心层头文件
#include
#include
#include
#include "samsung.h"
static int s3c2440_serial_setsource(struct uart_port *port,
struct s3c24xx_uart_clksrc *clk)
//设置时钟
{
unsigned long ucon = rd_regl(port, S3C2410_UCON); // ucon 内存地址=(port)->membase + S3C2410_UCON
/* todo - proper fclk<>nonfclk switch. */
ucon &= ~S3C2440_UCON_CLKMASK;
if (strcmp(clk->name, "uclk") == 0)
ucon |= S3C2440_UCON_UCLK;
else if (strcmp(clk->name, "pclk") == 0)
ucon |= S3C2440_UCON_PCLK;
else if (strcmp(clk->name, "fclk") == 0)
ucon |= S3C2440_UCON_FCLK;
else {
printk(KERN_ERR "unknown clock source %s\n", clk->name);
return -EINVAL;
}
wr_regl(port, S3C2410_UCON, ucon);
return 0;
}
static int s3c2440_serial_getsource(struct uart_port *port,
struct s3c24xx_uart_clksrc *clk)//读取时钟设置
{
unsigned long ucon = rd_regl(port, S3C2410_UCON);
unsigned long ucon0, ucon1, ucon2;
switch (ucon & S3C2440_UCON_CLKMASK) {
case S3C2440_UCON_UCLK:
clk->divisor = 1;
clk->name = "uclk";
break;
case S3C2440_UCON_PCLK:
case S3C2440_UCON_PCLK2:
clk->divisor = 1;
clk->name = "pclk";
break;
case S3C2440_UCON_FCLK:
/* the fun of calculating the uart divisors on
* the s3c2440 */
ucon0 = __raw_readl(S3C24XX_VA_UART0 + S3C2410_UCON);
ucon1 = __raw_readl(S3C24XX_VA_UART1 + S3C2410_UCON);
ucon2 = __raw_readl(S3C24XX_VA_UART2 + S3C2410_UCON);
printk("ucons: %08lx, %08lx, %08lx\n", ucon0, ucon1, ucon2);
ucon0 &= S3C2440_UCON0_DIVMASK;
ucon1 &= S3C2440_UCON1_DIVMASK;
ucon2 &= S3C2440_UCON2_DIVMASK;
if (ucon0 != 0) {
clk->divisor = ucon0 >> S3C2440_UCON_DIVSHIFT;
clk->divisor += 6;
} else if (ucon1 != 0) {
clk->divisor = ucon1 >> S3C2440_UCON_DIVSHIFT;
clk->divisor += 21;
} else if (ucon2 != 0) {
clk->divisor = ucon2 >> S3C2440_UCON_DIVSHIFT;
clk->divisor += 36;
} else {
/* manual calims 44, seems to be 9 */
clk->divisor = 9;
}
clk->name = "fclk";
break;
}
return 0;
}
static int s3c2440_serial_resetport(struct uart_port *port,
struct s3c2410_uartcfg *cfg)
{
unsigned long ucon = rd_regl(port, S3C2410_UCON);
dbg("s3c2440_serial_resetport: port=%p (%08lx), cfg=%p\n",
port, port->mapbase, cfg);
/* ensure we don't change the clock settings... */
ucon &= (S3C2440_UCON0_DIVMASK | (3<<10));
wr_regl(port, S3C2410_UCON, ucon | cfg->ucon);
wr_regl(port, S3C2410_ULCON, cfg->ulcon);
/* reset both fifos */
wr_regl(port, S3C2410_UFCON, cfg->ufcon | S3C2410_UFCON_RESETBOTH); //S3C2410_UFCON:FIFO控制寄存器
wr_regl(port, S3C2410_UFCON, cfg->ufcon);
return 0;
}
static struct s3c24xx_uart_info s3c2440_uart_inf = {
//针对S3C2440 UART的信息
.name
= "Samsung S3C2440 UART",
.type
= PORT_S3C2440,
.fifosize
= 64,
.rx_fifomask
= S3C2440_UFSTAT_RXMASK,
.rx_fifoshift
= S3C2440_UFSTAT_RXSHIFT,
.rx_fifofull
= S3C2440_UFSTAT_RXFULL,
.tx_fifofull
= S3C2440_UFSTAT_TXFULL,
.tx_fifomask
= S3C2440_UFSTAT_TXMASK,
.tx_fifoshift
= S3C2440_UFSTAT_TXSHIFT,
.get_clksrc
= s3c2440_serial_getsource,
.set_clksrc
= s3c2440_serial_setsource,
.reset_port
= s3c2440_serial_resetport,
};
/* device management */
static int s3c2440_serial_probe(struct platform_device *dev)//平台设备的探测函数
{
dbg("s3c2440_serial_probe: dev=%p\n", dev);
return s3c24xx_serial_probe(dev, &s3c2440_uart_inf); // 初始化端口并添加端口。
}
static struct platform_driver s3c2440_serial_driver = {
//初始化平台设备结构体
.probe
= s3c2440_serial_probe,
.remove
= __devexit_p(s3c24xx_serial_remove),
.driver
= {
.name
= "s3c2440-uart",
.owner
= THIS_MODULE,
},
};
s3c24xx_console_init(&s3c2440_serial_driver, &s3c2440_uart_inf); //宏定义, 调用samsung.c中的函数s3c24xx_serial_initconsole
static int __init s3c2440_serial_init(void)
{
return s3c24xx_serial_init(&s3c2440_serial_driver, &s3c2440_uart_inf);//调用平台设备的驱动注册函数platform_driver_register()
}
static void __exit s3c2440_serial_exit(void)
{
platform_driver_unregister(&s3c2440_serial_driver);
}
module_init(s3c2440_serial_init);
module_exit(s3c2440_serial_exit);
MODULE_DESCRIPTION("Samsung S3C2440,S3C2442 SoC Serial port driver");
MODULE_AUTHOR("Ben Dooks ");
MODULE_LICENSE("GPL v2");
MODULE_ALIAS("platform:s3c2440-uart");
samsung.c文件:
/* linux/drivers/serial/samsuing.c*/
#if defined(CONFIG_SERIAL_SAMSUNG_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
#define SUPPORT_SYSRQ
#endif
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include "samsung.h"
/* UART name and device definitions */
#define S3C24XX_SERIAL_NAME
"ttySAC"
#define S3C24XX_SERIAL_MAJOR
204
#define S3C24XX_SERIAL_MINOR
64
/* macros to change one thing to another */
#define tx_enabled(port) ((port)->unused[0])
#define rx_enabled(port) ((port)->unused[1])
/* flag to ignore all characters comming in */
#define RXSTAT_DUMMY_READ (0x10000000) //接收数据状态寄存器
static inline struct s3c24xx_uart_port *to_ourport(struct uart_port *port)
{
return container_of(port, struct s3c24xx_uart_port, port);
//根据port获取结构体本身的指针
}
/* translate a port to the device name */
static inline const char *s3c24xx_serial_portname(struct uart_port *port)
{
return to_platform_device(port->dev)->name; //to_platform_device(x) == container_of((x), struct platform_device, dev)
}//因此可以初始化platform_device的name
static int s3c24xx_serial_txempty_nofifo(struct uart_port *port)
{
return (rd_regl(port, S3C2410_UTRSTAT) & S3C2410_UTRSTAT_TXE);//rd_regl读S3C2410_UTRSTAT(发送接收状态寄存器)
}
//检查发送缓冲区是否为空, 非FIFO模式
static void s3c24xx_serial_rx_enable(struct uart_port *port)
//启动接收数据
{
unsigned long flags;
unsigned int ucon, ufcon;
int count = 10000;
spin_lock_irqsave(&port->lock, flags);
while (--count && !s3c24xx_serial_txempty_nofifo(port))
udelay(100);
ufcon = rd_regl(port, S3C2410_UFCON);
ufcon |= S3C2410_UFCON_RESETRX;
wr_regl(port, S3C2410_UFCON, ufcon);
ucon = rd_regl(port, S3C2410_UCON);
ucon |= S3C2410_UCON_RXIRQMODE; //设置接收数据中断
wr_regl(port, S3C2410_UCON, ucon);
rx_enabled(port) = 1;
spin_unlock_irqrestore(&port->lock, flags);
}
static void s3c24xx_serial_rx_disable(struct uart_port *port)
//禁止接收数据
{
unsigned long flags;
unsigned int ucon;
spin_lock_irqsave(&port->lock, flags);
ucon = rd_regl(port, S3C2410_UCON);
ucon &= ~S3C2410_UCON_RXIRQMODE;
wr_regl(port, S3C2410_UCON, ucon);
rx_enabled(port) = 0;
spin_unlock_irqrestore(&port->lock, flags);
}
static void s3c24xx_serial_stop_tx(struct uart_port *port)
//停止发送数据
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
if (tx_enabled(port)) {
disable_irq_nosync(ourport->tx_irq); // disable_irq_nosync 禁止中断立即返回 的。这样,使用后者将会更快,但是可能会让你的驱动程序处于竞态下。
// disable_irq 禁止中断,而且等待当前正在执行的中断处理程序完成.
tx_enabled(port) = 0;
if (port->flags & UPF_CONS_FLOW) // 判断 端口是否已 enabling flow control
s3c24xx_serial_rx_enable(port);
}
}
static void s3c24xx_serial_start_tx(struct uart_port *port)
//启动发送数据
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
if (!tx_enabled(port)) {
if (port->flags & UPF_CONS_FLOW)
s3c24xx_serial_rx_disable(port); // 启动发送数据中断,所以要禁止接收中断。
enable_irq(ourport->tx_irq);
tx_enabled(port) = 1;
}
}
static void s3c24xx_serial_stop_rx(struct uart_port *port)
// 停止接收数据
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
if (rx_enabled(port)) {
dbg("s3c24xx_serial_stop_rx: port=%p\n", port);
disable_irq_nosync(ourport->rx_irq);
rx_enabled(port) = 0;
}
}
static void s3c24xx_serial_enable_ms(struct uart_port *port)
{
}
static inline struct s3c24xx_uart_info *s3c24xx_port_to_info(struct uart_port *port)
{
return to_ourport(port)->info;//调用container_of, 获取s3c24xx_uart_info信息
}
static inline struct s3c2410_uartcfg *s3c24xx_port_to_cfg(struct uart_port *port)
{
if (port->dev == NULL)
return NULL;
return (struct s3c2410_uartcfg *)port->dev->platform_data; //
platform_data的获取在。。。。
}
static int s3c24xx_serial_rx_fifocnt(struct s3c24xx_uart_port *ourport,
unsigned long ufstat)//获取fifo中已有的数据大小
{
struct s3c24xx_uart_info *info = ourport->info;
if (ufstat & info->rx_fifofull)
return info->fifosize;
return (ufstat & info->rx_fifomask) >> info->rx_fifoshift;
}
/* ? - where has parity gone?? */
#define S3C2410_UERSTAT_PARITY (0x1000) // 奇偶校验错误
static irqreturn_t
s3c24xx_serial_rx_chars(int irq, void *dev_id)
//接收中断处理函数
{
struct s3c24xx_uart_port *ourport = dev_id;
struct uart_port *port = &ourport->port;
//获得uart_port
struct tty_struct *tty = port->state->port.tty;
//获得tty_struct
unsigned int ufcon, ch, flag, ufstat, uerstat;
int max_count = 64;
while (max_count-- > 0) {
ufcon = rd_regl(port, S3C2410_UFCON);
ufstat = rd_regl(port, S3C2410_UFSTAT);
if (s3c24xx_serial_rx_fifocnt(ourport, ufstat) == 0)
break;
uerstat = rd_regl(port, S3C2410_UERSTAT);
ch = rd_regb(port, S3C2410_URXH);
//从寄存器读出数据
if (port->flags & UPF_CONS_FLOW) {
int txe = s3c24xx_serial_txempty_nofifo(port);
if (rx_enabled(port)) {
//端口为使能接收状态
if (!txe) {
//发送缓冲区为空
rx_enabled(port) = 0;
continue;
}
} else {
//端口为禁止接收状态
if (txe) {
//发送缓冲区非空
ufcon |= S3C2410_UFCON_RESETRX;
wr_regl(port, S3C2410_UFCON, ufcon);
rx_enabled(port) = 1;
goto out;
}
continue;
}
}
/* insert the character into the buffer */
flag = TTY_NORMAL;
port->icount.rx++;
if (unlikely(uerstat & S3C2410_UERSTAT_ANY)) {
dbg("rxerr: port ch=0x%02x, rxs=0x%08x\n",
ch, uerstat);
/* check for break */
if (uerstat & S3C2410_UERSTAT_BREAK) {
dbg("break!\n");
port->icount.brk++;
if (uart_handle_break(port))
goto ignore_char;
}
if (uerstat & S3C2410_UERSTAT_FRAME)
port->icount.frame++;
if (uerstat & S3C2410_UERSTAT_OVERRUN)
port->icount.overrun++;
uerstat &= port->read_status_mask;
if (uerstat & S3C2410_UERSTAT_BREAK)
flag = TTY_BREAK;
else if (uerstat & S3C2410_UERSTAT_PARITY)
flag = TTY_PARITY;
else if (uerstat & (S3C2410_UERSTAT_FRAME |
S3C2410_UERSTAT_OVERRUN))
flag = TTY_FRAME;
}
if (uart_handle_sysrq_char(port, ch))
goto ignore_char;
uart_insert_char(port, uerstat, S3C2410_UERSTAT_OVERRUN, ch, flag);
//插入字符到tty设备的flip缓冲
ignore_char:
continue;
}
tty_flip_buffer_push(tty);
//刷新tty设备的flip缓冲
out:
return IRQ_HANDLED;
}
static irqreturn_t s3c24xx_serial_tx_chars(int irq, void *id)
//发送中断处理函数
{
struct s3c24xx_uart_port *ourport = id;
struct uart_port *port = &ourport->port;
struct circ_buf *xmit = &port->state->xmit;
//得到环形缓冲区
int count = 256;
//一次最多发送的字符数
if (port->x_char) {
//如果定义了xchar, 发送
wr_regb(port, S3C2410_UTXH, port->x_char);
port->icount.tx++;
port->x_char = 0;
goto out;
}
/* if there isnt anything more to transmit, or the uart is now
* stopped, disable the uart and exit
*/
if (uart_circ_empty(xmit) || uart_tx_stopped(port)) {
s3c24xx_serial_stop_tx(port);
goto out;
}
/* try and drain the buffer... */
while (!uart_circ_empty(xmit) && count-- > 0) {
if (rd_regl(port, S3C2410_UFSTAT) & ourport->info->tx_fifofull)
break;
wr_regb(port, S3C2410_UTXH, xmit->buf[xmit->tail]);
xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
port->icount.tx++;
}
if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)//如果环形缓冲区中剩余的字符少于WAKEUP_CHARS, 唤醒上层
uart_write_wakeup(port);
if (uart_circ_empty(xmit))
//如果发送环形buffer为空
s3c24xx_serial_stop_tx(port);
//停止发送
out:
return IRQ_HANDLED;
}
static unsigned int s3c24xx_serial_tx_empty(struct uart_port *port)//判断发送缓冲区/FIFO是否为空
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);//FIFO模式,检查UFSTATn寄存器
unsigned long ufstat = rd_regl(port, S3C2410_UFSTAT);
unsigned long ufcon = rd_regl(port, S3C2410_UFCON);
if (ufcon & S3C2410_UFCON_FIFOMODE) {
if ((ufstat & info->tx_fifomask) != 0 ||
//tx fifo数据非0
(ufstat & info->tx_fifofull))
//tx fifo满
return 0;
//非空返回0
return 1;
//空
}
return s3c24xx_serial_txempty_nofifo(port);
}
/* no modem control lines */
static unsigned int s3c24xx_serial_get_mctrl(struct uart_port *port)
{
unsigned int umstat = rd_regb(port, S3C2410_UMSTAT);
if (umstat & S3C2410_UMSTAT_CTS)
return TIOCM_CAR | TIOCM_DSR | TIOCM_CTS;
else
return TIOCM_CAR | TIOCM_DSR;
}
static void s3c24xx_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
{
/* todo - possibly remove AFC and do manual CTS */
}
static void s3c24xx_serial_break_ctl(struct uart_port *port, int break_state)
{
unsigned long flags;
unsigned int ucon;
spin_lock_irqsave(&port->lock, flags);
ucon = rd_regl(port, S3C2410_UCON);
if (break_state)
ucon |= S3C2410_UCON_SBREAK;
else
ucon &= ~S3C2410_UCON_SBREAK;
wr_regl(port, S3C2410_UCON, ucon);
spin_unlock_irqrestore(&port->lock, flags);
}
static void s3c24xx_serial_shutdown(struct uart_port *port)//释放中断,禁止发送和接收
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
if (ourport->tx_claimed) {
free_irq(ourport->tx_irq, ourport);
tx_enabled(port) = 0;
//设置发送使能状态为0
ourport->tx_claimed = 0;
}
if (ourport->rx_claimed) {
free_irq(ourport->rx_irq, ourport);
ourport->rx_claimed = 0;
rx_enabled(port) = 0;
//设置接收使能状态为0
}
}
static int s3c24xx_serial_startup(struct uart_port *port) //启动端口,申请端口的发送,接收中断,使能端口的发送和接收
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
int ret;
dbg("s3c24xx_serial_startup: port=%p (%08lx,%p)\n",
port->mapbase, port->membase);
rx_enabled(port) = 1;
//设置接收使能状态为1
ret = request_irq(ourport->rx_irq, s3c24xx_serial_rx_chars, 0,
//申请接收中断
s3c24xx_serial_portname(port), ourport);
if (ret != 0) {
printk(KERN_ERR "cannot get irq %d\n", ourport->rx_irq);
return ret;
}
ourport->rx_claimed = 1;
dbg("requesting tx irq...\n");
tx_enabled(port) = 1;
//设置发送使能状态为1
ret = request_irq(ourport->tx_irq, s3c24xx_serial_tx_chars, 0,//申请发送中断
s3c24xx_serial_portname(port), ourport);
if (ret) {
printk(KERN_ERR "cannot get irq %d\n", ourport->tx_irq);
goto err;
}
ourport->tx_claimed = 1;
dbg("s3c24xx_serial_startup ok\n");
/* the port reset code should have done the correct
* register setup for the port controls */
if (port->line == 2) {
s3c2410_gpio_cfgpin(S3C2410_GPH(6), S3C2410_GPH6_TXD2);
s3c2410_gpio_pullup(S3C2410_GPH(6), 1);
s3c2410_gpio_cfgpin(S3C2410_GPH(7), S3C2410_GPH7_RXD2);
s3c2410_gpio_pullup(S3C2410_GPH(7), 1);
}
return ret;
err:
s3c24xx_serial_shutdown(port);
return ret;
}
/* power power management control */
static void s3c24xx_serial_pm(struct uart_port *port, unsigned int level,
unsigned int old)
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
ourport->pm_level = level;
switch (level) {
case 3:
if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
clk_disable(ourport->baudclk);
clk_disable(ourport->clk);
break;
case 0:
clk_enable(ourport->clk);
if (!IS_ERR(ourport->baudclk) && ourport->baudclk != NULL)
clk_enable(ourport->baudclk);
break;
default:
printk(KERN_ERR "s3c24xx_serial: unknown pm %d\n", level);
}
}
/* baud rate calculation
*
* The UARTs on the S3C2410/S3C2440 can take their clocks from a number
* of different sources, including the peripheral clock ("pclk") and an
* external clock ("uclk"). The S3C2440 also adds the core clock ("fclk")
* with a programmable extra divisor.
*
* The following code goes through the clock sources, and calculates the
* baud clocks (and the resultant actual baud rates) and then tries to
* pick the closest one and select that.
*
*/
#define MAX_CLKS (8)
static struct s3c24xx_uart_clksrc tmp_clksrc = {
.name
= "pclk",
.min_baud
= 0,
.max_baud
= 0,
.divisor
= 1,
};
static inline int
s3c24xx_serial_getsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
return (info->get_clksrc)(port, c);
}
static inline int
s3c24xx_serial_setsource(struct uart_port *port, struct s3c24xx_uart_clksrc *c)
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
return (info->set_clksrc)(port, c);
}
struct baud_calc {
struct s3c24xx_uart_clksrc
*clksrc;
unsigned int
calc;
unsigned int
divslot;
unsigned int
quot;
struct clk
*src;
};
static int s3c24xx_serial_calcbaud(struct baud_calc *calc,
struct uart_port *port,
struct s3c24xx_uart_clksrc *clksrc,
unsigned int baud)
{
struct s3c24xx_uart_port *ourport = to_ourport(port);
unsigned long rate;
calc->src = clk_get(port->dev, clksrc->name);
if (calc->src == NULL || IS_ERR(calc->src))
return 0;
rate = clk_get_rate(calc->src);
rate /= clksrc->divisor;
calc->clksrc = clksrc;
if (ourport->info->has_divslot) {
unsigned long div = rate / baud;
/* The UDIVSLOT register on the newer UARTs allows us to
* get a divisor adjustment of 1/16th on the baud clock.
*
* We don't keep the UDIVSLOT value (the 16ths we calculated
* by not multiplying the baud by 16) as it is easy enough
* to recalculate.
*/
calc->quot = div / 16;
calc->calc = rate / div;
} else {
calc->quot = (rate + (8 * baud)) / (16 * baud);
calc->calc = (rate / (calc->quot * 16));
}
calc->quot--;
return 1;
}
static unsigned int s3c24xx_serial_getclk(struct uart_port *port,
struct s3c24xx_uart_clksrc **clksrc,
struct clk **clk,
unsigned int baud)
{
struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
struct s3c24xx_uart_clksrc *clkp;
struct baud_calc res[MAX_CLKS];
struct baud_calc *resptr, *best, *sptr;
int i;
clkp = cfg->clocks;
best = NULL;
if (cfg->clocks_size < 2) {
if (cfg->clocks_size == 0)
clkp = &tmp_clksrc;
/* check to see if we're sourcing fclk, and if so we're
* going to have to update the clock source
*/
if (strcmp(clkp->name, "fclk") == 0) {
struct s3c24xx_uart_clksrc src;
s3c24xx_serial_getsource(port, &src);
/* check that the port already using fclk, and if
* not, then re-select fclk
*/
if (strcmp(src.name, clkp->name) == 0) {
s3c24xx_serial_setsource(port, clkp);
s3c24xx_serial_getsource(port, &src);
}
clkp->divisor = src.divisor;
}
s3c24xx_serial_calcbaud(res, port, clkp, baud);
best = res;
resptr = best + 1;
} else {
resptr = res;
for (i = 0; i < cfg->clocks_size; i++, clkp++) {
if (s3c24xx_serial_calcbaud(resptr, port, clkp, baud))
resptr++;
}
}
/* ok, we now need to select the best clock we found */
if (!best) {
unsigned int deviation = (1<<30)|((1<<30)-1);
int calc_deviation;
for (sptr = res; sptr < resptr; sptr++) {
calc_deviation = baud - sptr->calc;
if (calc_deviation < 0)
calc_deviation = -calc_deviation;
if (calc_deviation < deviation) {
best = sptr;
deviation = calc_deviation;
}
}
}
/* store results to pass back */
*clksrc = best->clksrc;
*clk = best->src;
return best->quot;
}
/* udivslot_table[]
*
* This table takes the fractional value of the baud divisor and gives
* the recommended setting for the UDIVSLOT register.
*/
static u16 udivslot_table[16] = {
[0] = 0x0000,
[1] = 0x0080,
[2] = 0x0808,
[3] = 0x0888,
[4] = 0x2222,
[5] = 0x4924,
[6] = 0x4A52,
[7] = 0x54AA,
[8] = 0x5555,
[9] = 0xD555,
[10] = 0xD5D5,
[11] = 0xDDD5,
[12] = 0xDDDD,
[13] = 0xDFDD,
[14] = 0xDFDF,
[15] = 0xFFDF,
};
static void s3c24xx_serial_set_termios(struct uart_port *port,
//端口参数设置
struct ktermios *termios,
struct ktermios *old)
{
struct s3c2410_uartcfg *cfg = s3c24xx_port_to_cfg(port);
struct s3c24xx_uart_port *ourport = to_ourport(port);
struct s3c24xx_uart_clksrc *clksrc = NULL;
struct clk *clk = NULL;
unsigned long flags;
unsigned int baud, quot;
unsigned int ulcon;
unsigned int umcon;
unsigned int udivslot = 0;
/*
* We don't support modem control lines.
*/
termios->c_cflag &= ~(HUPCL | CMSPAR);
termios->c_cflag |= CLOCAL;
/*
* Ask the core to calculate the divisor for us.
*/
baud = uart_get_baud_rate(port, termios, old, 0, 115200*8);//请求计算分频,以便产生正确的波特率
if (baud == 38400 && (port->flags & UPF_SPD_MASK) == UPF_SPD_CUST)
quot = port->custom_divisor;
else
quot = s3c24xx_serial_getclk(port, &clksrc, &clk, baud);
/* check to see if we need to change clock source */
if (ourport->clksrc != clksrc || ourport->baudclk != clk) {
dbg("selecting clock %p\n", clk);
s3c24xx_serial_setsource(port, clksrc);
if (ourport->baudclk != NULL && !IS_ERR(ourport->baudclk)) {
clk_disable(ourport->baudclk);
ourport->baudclk = NULL;
}
clk_enable(clk);
ourport->clksrc = clksrc;
ourport->baudclk = clk;
ourport->baudclk_rate = clk ? clk_get_rate(clk) : 0;
}
if (ourport->info->has_divslot) {
unsigned int div = ourport->baudclk_rate / baud;
udivslot = udivslot_table[div & 15];
dbg("udivslot = %04x (div %d)\n", udivslot, div & 15);
}
switch (termios->c_cflag & CSIZE) {
//设置数据字长
case CS5:
dbg("config: 5bits/char\n");
ulcon = S3C2410_LCON_CS5;
break;
case CS6:
dbg("config: 6bits/char\n");
ulcon = S3C2410_LCON_CS6;
break;
case CS7:
dbg("config: 7bits/char\n");
ulcon = S3C2410_LCON_CS7;
break;
case CS8:
default:
dbg("config: 8bits/char\n");
ulcon = S3C2410_LCON_CS8;
break;
}
/* preserve original lcon IR settings */
ulcon |= (cfg->ulcon & S3C2410_LCON_IRM);
if (termios->c_cflag & CSTOPB)
ulcon |= S3C2410_LCON_STOPB; //
是否要求设置两个停止位(CSTOPB)
umcon = (termios->c_cflag & CRTSCTS) ? S3C2410_UMCOM_AFC : 0;
if (termios->c_cflag & PARENB) {
if (termios->c_cflag & PARODD)
ulcon |= S3C2410_LCON_PODD;
//奇校验
else
ulcon |= S3C2410_LCON_PEVEN;
//偶校验
} else {
ulcon |= S3C2410_LCON_PNONE;
//无校验
}
spin_lock_irqsave(&port->lock, flags);
dbg("setting ulcon to %08x, brddiv to %d, udivslot %08x\n",
ulcon, quot, udivslot);
wr_regl(port, S3C2410_ULCON, ulcon);
wr_regl(port, S3C2410_UBRDIV, quot);
wr_regl(port, S3C2410_UMCON, umcon);
if (ourport->info->has_divslot)
wr_regl(port, S3C2443_DIVSLOT, udivslot);
dbg("uart: ulcon = 0x%08x, ucon = 0x%08x, ufcon = 0x%08x\n",
rd_regl(port, S3C2410_ULCON),
rd_regl(port, S3C2410_UCON),
rd_regl(port, S3C2410_UFCON));
/*
* Update the per-port timeout.
*/
uart_update_timeout(port, termios->c_cflag, baud);
//更新端口的超时
/*
* Which character status flags are we interested in?
*/
port->read_status_mask = S3C2410_UERSTAT_OVERRUN;
if (termios->c_iflag & INPCK)
port->read_status_mask |= S3C2410_UERSTAT_FRAME | S3C2410_UERSTAT_PARITY;
/*
* Which character status flags should we ignore?
*/
port->ignore_status_mask = 0;
if (termios->c_iflag & IGNPAR)
port->ignore_status_mask |= S3C2410_UERSTAT_OVERRUN;
if (termios->c_iflag & IGNBRK && termios->c_iflag & IGNPAR)
port->ignore_status_mask |= S3C2410_UERSTAT_FRAME;
/*
* Ignore all characters if CREAD is not set.
*/
if ((termios->c_cflag & CREAD) == 0)
port->ignore_status_mask |= RXSTAT_DUMMY_READ;
spin_unlock_irqrestore(&port->lock, flags);
}
static const char *s3c24xx_serial_type(struct uart_port *port)
{
switch (port->type) {
case PORT_S3C2410:
return "S3C2410";
case PORT_S3C2440:
return "S3C2440";
case PORT_S3C2412:
return "S3C2412";
case PORT_S3C6400:
return "S3C6400/10";
default:
return NULL;
}
}
#define MAP_SIZE (0x100)
static void s3c24xx_serial_release_port(struct uart_port *port)
{
release_mem_region(port->mapbase, MAP_SIZE);
}
static int s3c24xx_serial_request_port(struct uart_port *port)
{
const char *name = s3c24xx_serial_portname(port);
return request_mem_region(port->mapbase, MAP_SIZE, name) ? 0 : -EBUSY;
}
static void s3c24xx_serial_config_port(struct uart_port *port, int flags)
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
if (flags & UART_CONFIG_TYPE &&
s3c24xx_serial_request_port(port) == 0)
port->type = info->type;
}
/*
* verify the new serial_struct (for TIOCSSERIAL).
*/
static int
s3c24xx_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
if (ser->type != PORT_UNKNOWN && ser->type != info->type)
return -EINVAL;
return 0;
}
#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
static struct console s3c24xx_serial_console;
#define S3C24XX_SERIAL_CONSOLE &s3c24xx_serial_console
#else
#define S3C24XX_SERIAL_CONSOLE NULL
#endif
static struct uart_ops s3c24xx_serial_ops = {
.pm
= s3c24xx_serial_pm,
.tx_empty
= s3c24xx_serial_tx_empty,
//发送缓冲区空
.get_mctrl
= s3c24xx_serial_get_mctrl,
//获取modem控制设置
.set_mctrl
= s3c24xx_serial_set_mctrl,
//设置modem控制
.stop_tx
= s3c24xx_serial_stop_tx,
//停止传输字符
.start_tx
= s3c24xx_serial_start_tx,
//开始传输字符
.stop_rx
= s3c24xx_serial_stop_rx,
//停止接收字符
.enable_ms
= s3c24xx_serial_enable_ms,
//modem状态中断时能
.break_ctl
= s3c24xx_serial_break_ctl,
//控制break信号的传输
.startup
= s3c24xx_serial_startup,
//启动端口
.shutdown
= s3c24xx_serial_shutdown,
//关闭端口
.set_termios
= s3c24xx_serial_set_termios,
//改变端口参数
.type
= s3c24xx_serial_type,
//返回描述特定端口的常量字符串指针
.release_port
= s3c24xx_serial_release_port,
//释放端口所占的内存和IO资源
.request_port
= s3c24xx_serial_request_port,
//申请端口所需的内存和IO资源
.config_port
= s3c24xx_serial_config_port,
//执行端口所需的自动配置步骤
.verify_port
= s3c24xx_serial_verify_port,
//验证新的串行端口信息
};
static struct uart_driver s3c24xx_uart_drv = {
.owner
= THIS_MODULE,
.dev_name
= "s3c2410_serial",
.nr
= CONFIG_SERIAL_SAMSUNG_UARTS,
.cons
= S3C24XX_SERIAL_CONSOLE,
.driver_name
= S3C24XX_SERIAL_NAME,
.major
= S3C24XX_SERIAL_MAJOR,
.minor
= S3C24XX_SERIAL_MINOR,
};
static struct s3c24xx_uart_port s3c24xx_serial_ports[CONFIG_SERIAL_SAMSUNG_UARTS] = {
[0] = {
.port = {
.lock
= __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[0].port.lock),
.iotype
= UPIO_MEM,
.irq
= IRQ_S3CUART_RX0,
// #define IRQ_S3CUART_RX0 S3C2410_IRQSUB(0) /* 70 */
// #define S3C2410_IRQSUB(x) S3C2410_IRQ((x)+54)
// #define S3C2410_IRQ(x) ((x) + S3C2410_CPUIRQ_OFFSET)
// #define S3C2410_CPUIRQ_OFFSET (16)
.uartclk
= 0,
.fifosize
= 16,
.ops
= &s3c24xx_serial_ops,
.flags
= UPF_BOOT_AUTOCONF,
.line
= 0,
//
/* UART port number */ 端口索引
}
},
[1] = {
.port = {
.lock
= __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[1].port.lock),
.iotype
= UPIO_MEM,
.irq
= IRQ_S3CUART_RX1,
//#define IRQ_S3CUART_RX1 S3C2410_IRQSUB(3) /* 73 */
.uartclk
= 0,
.fifosize
= 16,
.ops
= &s3c24xx_serial_ops,
.flags
= UPF_BOOT_AUTOCONF,
.line
= 1,
}
},
#if CONFIG_SERIAL_SAMSUNG_UARTS > 2
[2] = {
.port = {
.lock
= __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[2].port.lock),
.iotype
= UPIO_MEM,
.irq
= IRQ_S3CUART_RX2,
.uartclk
= 0,
.fifosize
= 16,
.ops
= &s3c24xx_serial_ops,
.flags
= UPF_BOOT_AUTOCONF,
.line
= 2,
}
},
#endif
#if CONFIG_SERIAL_SAMSUNG_UARTS > 3
[3] = {
.port = {
.lock
= __SPIN_LOCK_UNLOCKED(s3c24xx_serial_ports[3].port.lock),
.iotype
= UPIO_MEM,
.irq
= IRQ_S3CUART_RX3,
.uartclk
= 0,
.fifosize
= 16,
.ops
= &s3c24xx_serial_ops,
.flags
= UPF_BOOT_AUTOCONF,
.line
= 3,
}
}
#endif
};
/* s3c24xx_serial_resetport
*
* wrapper to call the specific reset for this port (reset the fifos
* and the settings)
*/
static inline int s3c24xx_serial_resetport(struct uart_port *port,
struct s3c2410_uartcfg *cfg)
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
return (info->reset_port)(port, cfg);
}
#ifdef CONFIG_CPU_FREQ
static int s3c24xx_serial_cpufreq_transition(struct notifier_block *nb,
unsigned long val, void *data)
{
struct s3c24xx_uart_port *port;
struct uart_port *uport;
port = container_of(nb, struct s3c24xx_uart_port, freq_transition);
uport = &port->port;
/* check to see if port is enabled */
if (port->pm_level != 0)
return 0;
/* try and work out if the baudrate is changing, we can detect
* a change in rate, but we do not have support for detecting
* a disturbance in the clock-rate over the change.
*/
if (IS_ERR(port->clk))
goto exit;
if (port->baudclk_rate == clk_get_rate(port->clk))
goto exit;
if (val == CPUFREQ_PRECHANGE) {
/* we should really shut the port down whilst the
* frequency change is in progress. */
} else if (val == CPUFREQ_POSTCHANGE) {
struct ktermios *termios;
struct tty_struct *tty;
if (uport->state == NULL)
goto exit;
tty = uport->state->port.tty;
if (tty == NULL)
goto exit;
termios = tty->termios;
if (termios == NULL) {
printk(KERN_WARNING "%s: no termios?\n", __func__);
goto exit;
}
s3c24xx_serial_set_termios(uport, termios, NULL);
}
exit:
return 0;
}
static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
{
port->freq_transition.notifier_call = s3c24xx_serial_cpufreq_transition;
return cpufreq_register_notifier(&port->freq_transition,
CPUFREQ_TRANSITION_NOTIFIER);
}
static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
{
cpufreq_unregister_notifier(&port->freq_transition,
CPUFREQ_TRANSITION_NOTIFIER);
}
#else
static inline int s3c24xx_serial_cpufreq_register(struct s3c24xx_uart_port *port)
{
return 0;
}
static inline void s3c24xx_serial_cpufreq_deregister(struct s3c24xx_uart_port *port)
{
}
#endif
/* s3c24xx_serial_init_port
*
* initialise a single serial port from the platform device given
*/
static int s3c24xx_serial_init_port(struct s3c24xx_uart_port *ourport,
struct s3c24xx_uart_info *info,
struct platform_device *platdev)
{
struct uart_port *port = &ourport->port;
struct s3c2410_uartcfg *cfg;
struct resource *res;
int ret;
dbg("s3c24xx_serial_init_port: port=%p, platdev=%p\n", port, platdev);
if (platdev == NULL)
return -ENODEV;
cfg = s3c24xx_dev_to_cfg(&platdev->dev);
if (port->mapbase != 0)
return 0;
if (cfg->hwport > CONFIG_SERIAL_SAMSUNG_UARTS) {
printk(KERN_ERR "%s: port %d bigger than %d\n", __func__,
cfg->hwport, CONFIG_SERIAL_SAMSUNG_UARTS);
return -ERANGE;
}
/* setup info for port */
port->dev
= &platdev->dev;
ourport->info
= info;
/* copy the info in from provided structure */
ourport->port.fifosize = info->fifosize;
dbg("s3c24xx_serial_init_port: %p (hw %d)...\n", port, cfg->hwport);
port->uartclk = 1;
if (cfg->uart_flags & UPF_CONS_FLOW) {
dbg("s3c24xx_serial_init_port: enabling flow control\n");
port->flags |= UPF_CONS_FLOW;
}
/* sort our the physical and virtual addresses for each UART */
res = platform_get_resource(platdev, IORESOURCE_MEM, 0); //遍历resource资源列表,返回类型一致的资源。
if (res == NULL) {
printk(KERN_ERR "failed to find memory resource for uart\n");
return -EINVAL;
}
dbg("resource %p (%lx..%lx)\n", res, res->start, res->end);
port->mapbase = res->start;
/* 设置串口的寄存器基地址(物理)*/
port->membase = S3C_VA_UART + res->start - (S3C_PA_UART & 0xfff00000);
ret = platform_get_irq(platdev, 0); // 调用platform_get_resource(platdev, IORESOURCE_IRQ, 0);返回第一次遇到的IORESOURCE_IRQ类型的resource的起始地址start。
if (ret < 0)
port->irq = 0;
else {
port->irq = ret;
ourport->rx_irq = ret;
ourport->tx_irq = ret + 1;
}
ret = platform_get_irq(platdev, 1);// 调用platform_get_resource(platdev, IORESOURCE_IRQ, 1);返回第一次遇到的IORESOURCE_IRQ类型的resource的起始地址start。
if (ret > 0)
ourport->tx_irq = ret;
ourport->clk
= clk_get(&platdev->dev, "uart");
dbg("port: map=%08x, mem=%08x, irq=%d (%d,%d), clock=%ld\n",
port->mapbase, port->membase, port->irq,
ourport->rx_irq, ourport->tx_irq, port->uartclk);
/* reset the fifos (and setup the uart) */
s3c24xx_serial_resetport(port, cfg);
return 0;
}
static ssize_t s3c24xx_serial_show_clksrc(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct uart_port *port = s3c24xx_dev_to_port(dev);
struct s3c24xx_uart_port *ourport = to_ourport(port);
return snprintf(buf, PAGE_SIZE, "* %s\n", ourport->clksrc->name);
}
static DEVICE_ATTR(clock_source, S_IRUGO, s3c24xx_serial_show_clksrc, NULL); //声明
dev_attr_clock_source属性,在s3c24xx_serial_probe中建立该属性文件。
/* Device driver serial port probe */
static int probe_index; //静态变量,端口探测索引。
int s3c24xx_serial_probe(struct platform_device *dev,
struct s3c24xx_uart_info *info)
{
struct s3c24xx_uart_port *ourport;
int ret;
dbg("s3c24xx_serial_probe(%p, %p) %d\n", dev, info, probe_index);
ourport = &s3c24xx_serial_ports[probe_index]; //s3c24xx_serial_ports为前面声明的s3c24xx_uart_port端口。
probe_index++;
dbg("%s: initialising port %p...\n", __func__, ourport);
ret = s3c24xx_serial_init_port(ourport, info, dev);
if (ret < 0)
goto probe_err;
dbg("%s: adding port\n", __func__);
uart_add_one_port(&s3c24xx_uart_drv, &ourport->port);
platform_set_drvdata(dev, &ourport->port);
ret = device_create_file(&dev->dev, &
dev_attr_clock_source);
if (ret < 0)
printk(KERN_ERR "%s: failed to add clksrc attr.\n", __func__);
ret = s3c24xx_serial_cpufreq_register(ourport);
if (ret < 0)
dev_err(&dev->dev, "failed to add cpufreq notifier\n");
return 0;
probe_err:
return ret;
}
EXPORT_SYMBOL_GPL(s3c24xx_serial_probe);
int __devexit s3c24xx_serial_remove(struct platform_device *dev)
{
struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
if (port) {
s3c24xx_serial_cpufreq_deregister(to_ourport(port));
device_remove_file(&dev->dev, &dev_attr_clock_source);
uart_remove_one_port(&s3c24xx_uart_drv, port);
}
return 0;
}
EXPORT_SYMBOL_GPL(s3c24xx_serial_remove);
/* UART power management code */
#ifdef CONFIG_PM
static int s3c24xx_serial_suspend(struct platform_device *dev, pm_message_t state)
{
struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
if (port)
uart_suspend_port(&s3c24xx_uart_drv, port);
return 0;
}
static int s3c24xx_serial_resume(struct platform_device *dev)
{
struct uart_port *port = s3c24xx_dev_to_port(&dev->dev);
struct s3c24xx_uart_port *ourport = to_ourport(port);
if (port) {
clk_enable(ourport->clk);
s3c24xx_serial_resetport(port, s3c24xx_port_to_cfg(port));
clk_disable(ourport->clk);
uart_resume_port(&s3c24xx_uart_drv, port);
}
return 0;
}
#endif
int s3c24xx_serial_init(struct platform_driver *drv,
struct s3c24xx_uart_info *info) //在serial/s3c2410.c中被调用。
{
dbg("s3c24xx_serial_init(%p,%p)\n", drv, info);
#ifdef CONFIG_PM
drv->suspend = s3c24xx_serial_suspend;
drv->resume = s3c24xx_serial_resume;
#endif
return platform_driver_register(drv);
}
EXPORT_SYMBOL_GPL(s3c24xx_serial_init);
/* module initialisation code */
static int
__init s3c24xx_serial_modinit(void)
{
int ret;
ret = uart_register_driver(&s3c24xx_uart_drv);
if (ret < 0) {
printk(KERN_ERR "failed to register UART driver\n");
return -1;
}
return 0;
}
static void
__exit s3c24xx_serial_modexit(void)
{
uart_unregister_driver(&s3c24xx_uart_drv);
}
module_init(s3c24xx_serial_modinit);
module_exit(s3c24xx_serial_modexit);
/* Console code */
#ifdef CONFIG_SERIAL_SAMSUNG_CONSOLE
static struct uart_port *cons_uart;
static int
s3c24xx_serial_console_txrdy(struct uart_port *port, unsigned int ufcon)
//发送数据就绪
{
struct s3c24xx_uart_info *info = s3c24xx_port_to_info(port);
unsigned long ufstat, utrstat;
if (ufcon & S3C2410_UFCON_FIFOMODE) {
// fifo模式
/* fifo mode - check amount of data in fifo registers... */
ufstat = rd_regl(port, S3C2410_UFSTAT);
return (ufstat & info->tx_fifofull) ? 0 : 1;
}
/* in non-fifo mode, we go and use the tx buffer empty */
utrstat = rd_regl(port, S3C2410_UTRSTAT);
return (utrstat & S3C2410_UTRSTAT_TXE) ? 1 : 0;
}
static void
s3c24xx_serial_console_putchar(struct uart_port *port, int ch)
//写入字符
{
unsigned int ufcon = rd_regl(cons_uart, S3C2410_UFCON);
while (!s3c24xx_serial_console_txrdy(port, ufcon))
barrier();
wr_regb(cons_uart, S3C2410_UTXH, ch);
}
static void
s3c24xx_serial_console_write(struct console *co, const char *s,
unsigned int count)) //写入count个字符;调用uart_console_write(struct uart_port *port, const char *s, unsigned int count, void (*putchar)(struct uart_port *, int))
{
uart_console_write(cons_uart, s, count, s3c24xx_serial_console_putchar);
}
static void __init
s3c24xx_serial_get_options(struct uart_port *port, int *baud,
int *parity, int *bits)
//获得端口的配置选项
{
struct s3c24xx_uart_clksrc clksrc;
struct clk *clk;
unsigned int ulcon;
unsigned int ucon;
unsigned int ubrdiv;
unsigned long rate;
ulcon = rd_regl(port, S3C2410_ULCON);
ucon = rd_regl(port, S3C2410_UCON);
ubrdiv = rd_regl(port, S3C2410_UBRDIV);
dbg("s3c24xx_serial_get_options: port=%p\n"
"registers: ulcon=%08x, ucon=%08x, ubdriv=%08x\n",
port, ulcon, ucon, ubrdiv);
if ((ucon & 0xf) != 0) {
/* consider the serial port configured if the tx/rx mode set */
switch (ulcon & S3C2410_LCON_CSMASK) {
//获取数据字长,ulcon的低2位表示。
case S3C2410_LCON_CS5:
// 0x0
*bits = 5;
break;
case S3C2410_LCON_CS6:
// 0x1
*bits = 6;
break;
case S3C2410_LCON_CS7
:// 0x 2
*bits = 7;
break;
default:
case S3C2410_LCON_CS8:
// 0x3
*bits = 8;
break;
}
switch (ulcon & S3C2410_LCON_PMASK) {//使用奇偶校验掩码,获取奇偶校验方式。
case S3C2410_LCON_PEVEN:
*parity = 'e';
break;
case S3C2410_LCON_PODD:
*parity = 'o';
break;
case S3C2410_LCON_PNONE:
default:
*parity = 'n';
}
/* now calculate the baud rate */
s3c24xx_serial_getsource(port, &clksrc);
clk = clk_get(port->dev, clksrc.name);
if (!IS_ERR(clk) && clk != NULL)
rate = clk_get_rate(clk) / clksrc.divisor;
else
rate = 1;
*baud = rate / (16 * (ubrdiv + 1)); // 通过 ubrdiv = rd_regl(port, S3C2410_UBRDIV);
dbg("calculated baud %d\n", *baud);
}
}
/* s3c24xx_serial_init_ports
*
* initialise the serial ports from the machine provided initialisation
* data.
*/
static int s3c24xx_serial_init_ports(struct s3c24xx_uart_info **info
) //初始化port
{
struct s3c24xx_uart_port *ptr = s3c24xx_serial_ports;
struct platform_device **platdev_ptr;
int i;
dbg("s3c24xx_serial_init_ports: initialising ports...\n");
platdev_ptr = s3c24xx_uart_devs;
for (i = 0; i < CONFIG_SERIAL_SAMSUNG_UARTS; i++, ptr++, platdev_ptr++) {
s3c24xx_serial_init_port(ptr, info[i], *platdev_ptr);
}
return 0;
}
static int __init
s3c24xx_serial_console_setup(struct console *co, char *options)
{
struct uart_port *port;
int baud = 9600;
int bits = 8;
int parity = 'n';
int flow = 'n';
dbg("s3c24xx_serial_console_setup: co=%p (%d), %s\n",
co, co->index, options);
/* is this a valid port */
if (co->index == -1 || co->index >= CONFIG_SERIAL_SAMSUNG_UARTS)
co->index = 0;
port = &s3c24xx_serial_ports[co->index].port;
/* is the port configured? */
if (port->mapbase == 0x0) {
co->index = 0;
port = &s3c24xx_serial_ports[co->index].port;
}
cons_uart = port;
dbg("s3c24xx_serial_console_setup: port=%p (%d)\n", port, co->index);
/*
* Check whether an invalid uart number has been specified, and
* if so, search for the first available port that does have
* console support.
*/
if (options)
uart_parse_options(options, &baud, &parity, &bits, &flow);
else
s3c24xx_serial_get_options(port, &baud, &parity, &bits);
dbg("s3c24xx_serial_console_setup: baud %d\n", baud);
return uart_set_options(port, co, baud, parity, bits, flow);
}
/* s3c24xx_serial_initconsole
*
* initialise the console from one of the uart drivers
*/
static struct console s3c24xx_serial_console = {
.name = S3C24XX_SERIAL_NAME,
.device = uart_console_device,
.flags = CON_PRINTBUFFER,
.index = -1,
.write = s3c24xx_serial_console_write,
.setup = s3c24xx_serial_console_setup
};
int s3c24xx_serial_initconsole(struct platform_driver *drv,
struct s3c24xx_uart_info **info)
{
struct platform_device *dev = s3c24xx_uart_devs[0];
dbg("s3c24xx_serial_initconsole\n");
/* select driver based on the cpu */
if (dev == NULL) {
printk(KERN_ERR "s3c24xx: no devices for console init\n");
return 0;
}
if (strcmp(dev->name, drv->driver.name) != 0)
return 0;
s3c24xx_serial_console.data = &s3c24xx_uart_drv;
s3c24xx_serial_init_ports(info);
register_console(&s3c24xx_serial_console);
return 0;
}
#endif /* CONFIG_SERIAL_SAMSUNG_CONSOLE */
MODULE_DESCRIPTION("Samsung SoC Serial port driver");
MODULE_AUTHOR("Ben Dooks ");
MODULE_LICENSE("GPL v2");