📄 ds1302c.c
字号:
/*!****************************************************************************!*! FILE NAME : ds1302.c*!*! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O*!*! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init, get_rtc_status*!*! $Log: ds1302.c,v $*! Revision 1.11 2001/06/14 12:35:52 jonashg*! The ATA hack is back. It is unfortunately the only way to set g27 to output.*!*! Revision 1.9 2001/06/14 10:00:14 jonashg*! No need for tempudelay to be inline anymore (had to adjust the usec to*! loops conversion because of this to make it slow enough to be a udelay).*!*! Revision 1.8 2001/06/14 08:06:32 jonashg*! Made tempudelay delay usecs (well, just a tad more).*!*! Revision 1.7 2001/06/13 14:18:11 jonashg*! Only allow processes with SYS_TIME capability to set time and charge.*!*! Revision 1.6 2001/06/12 15:22:07 jonashg*! * Made init function __init.*! * Parameter to out_byte() is unsigned char.*! * The magic number 42 has got a name.*! * Removed comment about /proc (nothing is exported there).*!*! Revision 1.5 2001/06/12 14:35:13 jonashg*! Gave the module a name and added it to printk's.*!*! Revision 1.4 2001/05/31 14:53:40 jonashg*! Made tempudelay() inline so that the watchdog doesn't reset (see*! function comment).*!*! Revision 1.3 2001/03/26 16:03:06 bjornw*! Needs linux/config.h*!*! Revision 1.2 2001/03/20 19:42:00 bjornw*! Use the ETRAX prefix on the DS1302 options*!*! Revision 1.1 2001/03/20 09:13:50 magnusmn*! Linux 2.4 port*!*! Revision 1.10 2000/07/05 15:38:23 bjornw*! Dont update kernel time when a RTC_SET_TIME is done*!*! Revision 1.9 2000/03/02 15:42:59 macce*! * Hack to make RTC work on all 2100/2400*!*! Revision 1.8 2000/02/23 16:59:18 torbjore*! added setup of R_GEN_CONFIG when RTC is connected to the generic port.*!*! Revision 1.7 2000/01/17 15:51:43 johana*! Added RTC_SET_CHARGE ioctl to enable trickle charger.*!*! Revision 1.6 1999/10/27 13:19:47 bjornw*! Added update_xtime_from_cmos which reads back the updated RTC into the kernel.*! /dev/rtc calls it now.*!*! Revision 1.5 1999/10/27 12:39:37 bjornw*! Disabled superuser check. Anyone can now set the time.*!*! Revision 1.4 1999/09/02 13:27:46 pkj*! Added shadow for R_PORT_PB_CONFIG.*! Renamed port_g_shadow to port_g_data_shadow.*!*! Revision 1.3 1999/09/02 08:28:06 pkj*! Made it possible to select either port PB or the generic port for the RST*! signal line to the DS1302 RTC.*! Also make sure the RST bit is configured as output on Port PB (if used).*!*! Revision 1.2 1999/09/01 14:47:20 bjornw*! Added support for /dev/rtc operations with ioctl RD_TIME and SET_TIME to read*! and set the date. Register as major 121.*!*! Revision 1.1 1999/09/01 09:45:29 bjornw*! Implemented a DS1302 RTC driver.*!*!*! ---------------------------------------------------------------------------*!*! (C) Copyright 1999, 2000, 2001 Axis Communications AB, LUND, SWEDEN*!*! $Id: ds1302.c,v 1.11 2001/06/14 12:35:52 jonashg Exp $*!*!***************************************************************************//*#include <linux/config.h>#include <linux/fs.h>#include <linux/init.h>#include <linux/mm.h>#include <linux/module.h>#include <linux/miscdevice.h>#include <linux/delay.h>#include <linux/blkdev.h>#include <asm/uaccess.h>#include <asm/system.h>//#include <asm/svinto.h>#include <asm/io.h>*/#include <linux/fs.h>
#include <linux/iobuf.h>
#include <linux/major.h>
#include <linux/blkdev.h>
#include <linux/capability.h>
#include <linux/smp_lock.h>
#include <asm/io.h>
#include <asm/uaccess.h>
#include <linux/module.h>//#include <asm/rtc.h>//#include <linux/rtc.h>//#include "ds1302c.h"#define RTCC_RD_TIME 0x01#define RTCC_SET_TIME 0x02#define RTCC_SET_CHARGE 0x03int dsc1302_open(struct inode*, struct file *);
int dsc1302_close(struct inode*, struct file *);
int rtcc_ioctl(struct inode*inode,struct file *flip,unsigned int cmd,unsigned long arg);#define DS1302_RST GPIO_F7#define DS1302_SCL GPIO_F4#define DS1302_SDA GPIO_F6#define INN 0#define OUTT 1//#define RTC_MAJOR_NR 121 /* local major, change later */static int RTCC_MAJOR_NR=0;static devfs_handle_t devfs_handle;//static const char dsc1302_name[] = "ds1302c";/* Probe for the chip by writing something to its RAM and try reading it back. *///#define MAGIC_PATTERN 0x42static unsigned char days_in_mo[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};/* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */int rtcc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { unsigned long flags; switch(cmd) { case RTCC_RD_TIME: /* read the time/date from RTC */ { printk("RTC READ....\n"); return 0; } case RTCC_SET_TIME: /* set the RTC */ { printk("RTC WRITE....\n"); return 0; } case RTCC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */ { return 0; } default: return -ENOIOCTLCMD; }}/* The various file operations we support. */static struct file_operations rtcc_fops = { ioctl: rtcc_ioctl, open: dsc1302_open, release: dsc1302_close,}; /* Just probe for the RTC and register the device to handle the ioctl needed. */int __init dsc1302_init(void) { int ret; //set_gpio_ctrl(GPIO_MODE_OUT | DS1302_SCL); //set_gpio_ctrl(GPIO_MODE_OUT | DS1302_RST); //set_gpio_pullup(DS1302_SCL); //set_gpio_pullup(DS1302_RST);// if (!ds1302_probe()) {//#ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT /* * The only way to set g27 to output is to enable ATA. * * Make sure that R_GEN_CONFIG is setup correct. */ // genconfig_shadow = ((genconfig_shadow & // ~IO_MASK(R_GEN_CONFIG, ata)) // | //(IO_STATE(R_GEN_CONFIG, ata, select))); // *R_GEN_CONFIG = genconfig_shadow; //if (!ds1302_probe()) //return -1;//#else // printk("probe fail....\n"); // return -1;//#endif //} /* if (ret=register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) { printk(KERN_INFO "%s: unable to get major %d for rtc\n", ds1302_name, RTC_MAJOR_NR); return -1; */ ret = register_chrdev(0, "ds1302c", &rtcc_fops);
if (ret < 0) {
printk("Driver register error!\n");
return ret; }
RTCC_MAJOR_NR=ret; devfs_handle = devfs_register(NULL, "ds1302c", DEVFS_FL_DEFAULT,RTCC_MAJOR_NR, 0, S_IFCHR | S_IRUSR | S_IWUSR,&rtcc_fops, NULL); return 0;}int dsc1302_release(struct inode*inode, struct file *filp)
{
devfs_unregister(devfs_handle);
unregister_chrdev(RTCC_MAJOR_NR, "ds1302c");
}int dsc1302_open(struct inode*inode, struct file *filp)
{
return 0;
}int dsc1302_close(struct inode*inode, struct file *filp)
{
return 0;
}module_init(dsc1302_init); //用户加载该驱动时执行insmod gpio_driv.o就会自动调用gpio_init函数,它是驱动
//的入口点,相当于应用程序的main函数。
module_exit(dsc1302_release); //用户卸载该驱动rmmod gpio_driv时执行
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -