📄 ds1302_file.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.12 2002/04/10 15:35:25 johana*! Moved probe function closer to init function and marked it __init.*!*! 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.12 2002/04/10 15:35:25 johana 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 <asm/uaccess.h>#include <asm/system.h>//#include <asm/svinto.h>#include <asm/io.h>#include <asm/rtc.h>#define RTC_MAJOR_NR 126 /* local major, change later */#define RTC_DATA 0x00100000 //GPIO20#define RTC_RST 0x00400000 //GPIO22#define RTC_CLK 0x00000001 //GPIO0static const char ds1302_name[] = "ds1302";/* The DS1302 might be connected to different bits on different products. * It has three signals - SDA, SCL and RST. RST and SCL are always outputs, * but SDA can have a selected direction. * For now, only PORT_PB is hardcoded. *//* The RST bit may be on either the Generic Port or Port PB. */#define TK_SDA_IN() ((GPLR0 >> 20) & 1)/* * The reason for tempudelay and not udelay is that loops_per_usec * (used in udelay) is not set when functions here are called from time.c */static void tempudelay(int usecs) { volatile int loops; for(loops = usecs * 12; loops > 0; loops--) /* nothing */; }/* Send 8 bits. */static voidout_byte(unsigned char x) { int i; GPDR0|=RTC_DATA; for (i = 8; i--;) { /* The chip latches incoming bits on the rising edge of SCL. */ GPCR0|=RTC_CLK; if(x & 1) GPSR0|=RTC_DATA; else GPCR0|=RTC_DATA; tempudelay(1); GPSR0|=RTC_CLK; tempudelay(1); x >>= 1; } GPDR0&=~RTC_DATA;}static unsigned charin_byte(void) { unsigned char x = 0; int i; /* Read byte. Bits come LSB first, on the falling edge of SCL. * Assume SDA is in input direction already. */ GPDR0&=~RTC_DATA; for (i = 8; i--;) { GPCR0|=RTC_CLK; tempudelay(1); x >>= 1; x |= (TK_SDA_IN() << 7); GPSR0|=RTC_CLK; tempudelay(1); } return x;}/* Prepares for a transaction by de-activating RST (active-low). */static voidstart(void) { GPCR0|=RTC_CLK; tempudelay(1); GPCR0|=RTC_RST; tempudelay(5); GPSR0|=RTC_RST; }/* Ends a transaction by taking RST active again. */static voidstop(void) { tempudelay(2); GPCR0|=RTC_RST;}/* Enable writing. */static voidds1302_wenable(void) { start(); out_byte(0x8e); /* Write control register */ out_byte(0x00); /* Disable write protect bit 7 = 0 */ stop();}/* Disable writing. */static voidds1302_wdisable(void) { start(); out_byte(0x8e); /* Write control register */ out_byte(0x80); /* Disable write protect bit 7 = 0 */ stop();}/* Read a byte from the selected register in the DS1302. */unsigned chards1302_readreg(int reg) { unsigned char x; start(); out_byte(0x81 | (reg << 1)); /* read register */ x = in_byte(); stop(); return x;}/* Write a byte to the selected register. */voidds1302_writereg(int reg, unsigned char val) { ds1302_wenable(); start(); out_byte(0x80 | (reg << 1)); /* write register */ out_byte(val); stop(); ds1302_wdisable();}voidget_rtc_time(struct rtc_time *rtc_tm) { unsigned long flags; save_flags(flags); cli(); rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS); rtc_tm->tm_min = CMOS_READ(RTC_MINUTES); rtc_tm->tm_hour = CMOS_READ(RTC_HOURS); rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH); rtc_tm->tm_mon = CMOS_READ(RTC_MONTH); rtc_tm->tm_year = CMOS_READ(RTC_YEAR); restore_flags(flags); BCD_TO_BIN(rtc_tm->tm_sec); BCD_TO_BIN(rtc_tm->tm_min); BCD_TO_BIN(rtc_tm->tm_hour); BCD_TO_BIN(rtc_tm->tm_mday); BCD_TO_BIN(rtc_tm->tm_mon); BCD_TO_BIN(rtc_tm->tm_year); /* * Account for differences between how the RTC uses the values * and how they are defined in a struct rtc_time; */ if (rtc_tm->tm_year <= 69) rtc_tm->tm_year += 100; rtc_tm->tm_mon--;}static 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). */static intrtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, unsigned long arg) { unsigned long flags; switch(cmd) { case RTC_RD_TIME: /* read the time/date from RTC */ { struct rtc_time rtc_tm; get_rtc_time(&rtc_tm); if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time))) return -EFAULT; return 0; } case RTC_SET_TIME: /* set the RTC */ { struct rtc_time rtc_tm; unsigned char mon, day, hrs, min, sec, leap_yr; unsigned char save_control, save_freq_select; unsigned int yrs; if (!capable(CAP_SYS_TIME)) return -EPERM; if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) return -EFAULT; yrs = rtc_tm.tm_year + 1900; mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ day = rtc_tm.tm_mday; hrs = rtc_tm.tm_hour; min = rtc_tm.tm_min; sec = rtc_tm.tm_sec; if ((yrs < 1970) || (yrs > 2069)) return -EINVAL; leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); if ((mon > 12) || (day == 0)) return -EINVAL; if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) return -EINVAL; if ((hrs >= 24) || (min >= 60) || (sec >= 60)) return -EINVAL; if (yrs >= 2000) yrs -= 2000; /* RTC (0, 1, ... 69) */ else yrs -= 1900; /* RTC (70, 71, ... 99) */ BIN_TO_BCD(sec); BIN_TO_BCD(min); BIN_TO_BCD(hrs); BIN_TO_BCD(day); BIN_TO_BCD(mon); BIN_TO_BCD(yrs); save_flags(flags); cli(); CMOS_WRITE(yrs, RTC_YEAR); CMOS_WRITE(mon, RTC_MONTH); CMOS_WRITE(day, RTC_DAY_OF_MONTH); CMOS_WRITE(hrs, RTC_HOURS); CMOS_WRITE(min, RTC_MINUTES); CMOS_WRITE(sec, RTC_SECONDS); restore_flags(flags); /* Notice that at this point, the RTC is updated but * the kernel is still running with the old time. * You need to set that separately with settimeofday * or adjtimex. */ return 0; } case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */ { int tcs_val; unsigned char save_control, save_freq_select; if (!capable(CAP_SYS_TIME)) return -EPERM; if(copy_from_user(&tcs_val, (int*)arg, sizeof(int))) return -EFAULT; tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F); ds1302_writereg(RTC_TRICKLECHARGER, tcs_val); return 0; } default: return -ENOIOCTLCMD; }}intget_rtc_status(char *buf) { char *p; struct rtc_time tm; p = buf; get_rtc_time(&tm); /* * There is no way to tell if the luser has the RTC set for local * time or for Universal Standard Time (GMT). Probably local though. */ p += sprintf(p, "rtc_time\t: %02d:%02d:%02d\n" "rtc_date\t: %04d-%02d-%02d\n", tm.tm_hour, tm.tm_min, tm.tm_sec, tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); return p - buf;}static int rtc_open(struct inode *inode, struct file *file){ printk("g_chdrv device %s opened\n",ds1302_name); return 0;}/* The various file operations we support. */static struct file_operations rtc_fops = { owner: THIS_MODULE, open: rtc_open, ioctl: rtc_ioctl, }; /* Probe for the chip by writing something to its RAM and try reading it back. */#define MAGIC_PATTERN 0x42static int __initds1302_probe(void) { int retval, res; GAFR0_L&=0xfffffffc;
GAFR0_U&=0xffffccff; printk("%x\n",GAFR0_U); GPDR0|=(RTC_RST |RTC_CLK); GPDR0&=~RTC_DATA; /* Try to talk to timekeeper. */ ds1302_wenable(); start(); out_byte(0xc0); /* write RAM byte 0 */ out_byte(MAGIC_PATTERN); /* write something magic */ start(); out_byte(0xc1); /* read RAM byte 0 */ if((res = in_byte())==MAGIC_PATTERN) { char buf[100]; stop(); ds1302_wdisable(); printk("%s: RTC found.\n", ds1302_name); printk("%s: SDA, SCL, RST on GPIO:20, GPIO:0, GPIO:22\n", ds1302_name); get_rtc_status(buf); printk(buf); retval = 1; } else { stop(); printk("%s: RTC not found.\n", ds1302_name); retval = 0; } printk("res=%x\n",res); return retval;}/* Just probe for the RTC and register the device to handle the ioctl needed. */int __initds1302_init(void) { if (!ds1302_probe()) { return -1; } if (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; } return 0;}module_init(ds1302_init);MODULE_LICENSE("GPL");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -