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

📄 ds1302ppc.c

📁 linux下ppc对DS1302操作的驱动!对于时序很有帮助!
💻 C
📖 第 1 页 / 共 2 页
字号:
1 /*!***************************************************************************
  2 *!
  3 *! FILE NAME  : ds1302.c
  4 *!
  5 *! DESCRIPTION: Implements an interface for the DS1302 RTC through Etrax I/O
  6 *!
  7 *! Functions exported: ds1302_readreg, ds1302_writereg, ds1302_init
  8 *!
  9 *! ---------------------------------------------------------------------------
 10 *!
 11 *! (C) Copyright 1999-2007 Axis Communications AB, LUND, SWEDEN
 12 *!
 13 *!***************************************************************************/
 14 
 15 
 16 #include <linux/fs.h>
 17 #include <linux/init.h>
 18 #include <linux/mm.h>
 19 #include <linux/module.h>
 20 #include <linux/miscdevice.h>
 21 #include <linux/delay.h>
 22 #include <linux/bcd.h>
 23 #include <linux/capability.h>
 24 
 25 #include <asm/uaccess.h>
 26 #include <asm/system.h>
 27 #include <asm/arch/svinto.h>
 28 #include <asm/io.h>
 29 #include <asm/rtc.h>
 30 #include <asm/arch/io_interface_mux.h>
 31 
 32 #include "i2c.h"
 33 
 34 #define RTC_MAJOR_NR 121 /* local major, change later */
 35 
 36 static const char ds1302_name[] = "ds1302";
 37 
 38 /* The DS1302 might be connected to different bits on different products. 
 39  * It has three signals - SDA, SCL and RST. RST and SCL are always outputs,
 40  * but SDA can have a selected direction.
 41  * For now, only PORT_PB is hardcoded.
 42  */
 43 
 44 /* The RST bit may be on either the Generic Port or Port PB. */
 45 #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT
 46 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA,  port_g_data_shadow,  CONFIG_ETRAX_DS1302_RSTBIT, x)
 47 #define TK_RST_DIR(x)
 48 #else
 49 #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x)
 50 #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR,  port_pb_dir_shadow,  CONFIG_ETRAX_DS1302_RSTBIT, x)
 51 #endif
 52 
 53 
 54 #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x)
 55 #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x)
 56 
 57 #define TK_SDA_IN()   ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1)
 58 /* 1 is out, 0 is in */
 59 #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR,  port_pb_dir_shadow,  CONFIG_ETRAX_DS1302_SDABIT, x)
 60 #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR,  port_pb_dir_shadow,  CONFIG_ETRAX_DS1302_SCLBIT, x)
 61 
 62 
 63 /*
 64  * The reason for tempudelay and not udelay is that loops_per_usec
 65  * (used in udelay) is not set when functions here are called from time.c 
 66  */
 67 
 68 static void tempudelay(int usecs) 
 69 {
 70         volatile int loops;
 71 
 72         for(loops = usecs * 12; loops > 0; loops--)
 73                 /* nothing */;  
 74 }
 75 
 76 
 77 /* Send 8 bits. */
 78 static void
 79 out_byte(unsigned char x) 
 80 {
 81         int i;
 82         TK_SDA_DIR(1);
 83         for (i = 8; i--;) {
 84                 /* The chip latches incoming bits on the rising edge of SCL. */
 85                 TK_SCL_OUT(0);
 86                 TK_SDA_OUT(x & 1);
 87                 tempudelay(1);
 88                 TK_SCL_OUT(1);
 89                 tempudelay(1);
 90                 x >>= 1;
 91         }
 92         TK_SDA_DIR(0);
 93 }
 94 
 95 static unsigned char
 96 in_byte(void) 
 97 {
 98         unsigned char x = 0;
 99         int i;
100 
101         /* Read byte. Bits come LSB first, on the falling edge of SCL.
102          * Assume SDA is in input direction already.
103          */
104         TK_SDA_DIR(0);
105 
106         for (i = 8; i--;) {
107                 TK_SCL_OUT(0);
108                 tempudelay(1);
109                 x >>= 1;
110                 x |= (TK_SDA_IN() << 7);
111                 TK_SCL_OUT(1);
112                 tempudelay(1);
113         }
114 
115         return x;
116 }
117 
118 /* Prepares for a transaction by de-activating RST (active-low). */
119 
120 static void
121 start(void) 
122 {
123         TK_SCL_OUT(0);
124         tempudelay(1);
125         TK_RST_OUT(0);
126         tempudelay(5);
127         TK_RST_OUT(1);  
128 }
129 
130 /* Ends a transaction by taking RST active again. */
131 
132 static void
133 stop(void) 
134 {
135         tempudelay(2);
136         TK_RST_OUT(0);
137 }
138 
139 /* Enable writing. */
140 
141 static void
142 ds1302_wenable(void) 
143 {
144         start();        
145         out_byte(0x8e); /* Write control register  */
146         out_byte(0x00); /* Disable write protect bit 7 = 0 */
147         stop();
148 }
149 
150 /* Disable writing. */
151 
152 static void
153 ds1302_wdisable(void) 
154 {
155         start();
156         out_byte(0x8e); /* Write control register  */
157         out_byte(0x80); /* Disable write protect bit 7 = 0 */
158         stop();
159 }
160 
161 
162 
163 /* Read a byte from the selected register in the DS1302. */
164 
165 unsigned char
166 ds1302_readreg(int reg) 
167 {
168         unsigned char x;
169 
170         start();
171         out_byte(0x81 | (reg << 1)); /* read register */
172         x = in_byte();
173         stop();
174 
175         return x;
176 }
177 
178 /* Write a byte to the selected register. */
179 
180 void
181 ds1302_writereg(int reg, unsigned char val) 
182 {
183 #ifndef CONFIG_ETRAX_RTC_READONLY
184         int do_writereg = 1;
185 #else
186         int do_writereg = 0;
187 
188         if (reg == RTC_TRICKLECHARGER)
189                 do_writereg = 1;
190 #endif
191 
192         if (do_writereg) {
193                 ds1302_wenable();
194                 start();
195                 out_byte(0x80 | (reg << 1)); /* write register */
196                 out_byte(val);
197                 stop();
198                 ds1302_wdisable();
199         }
200 }
201 
202 void
203 get_rtc_time(struct rtc_time *rtc_tm) 
204 {
205         unsigned long flags;
206 
207         local_irq_save(flags);
208 
209         rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS);
210         rtc_tm->tm_min = CMOS_READ(RTC_MINUTES);
211         rtc_tm->tm_hour = CMOS_READ(RTC_HOURS);
212         rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH);
213         rtc_tm->tm_mon = CMOS_READ(RTC_MONTH);
214         rtc_tm->tm_year = CMOS_READ(RTC_YEAR);
215 
216         local_irq_restore(flags);
217         
218         BCD_TO_BIN(rtc_tm->tm_sec);
219         BCD_TO_BIN(rtc_tm->tm_min);
220         BCD_TO_BIN(rtc_tm->tm_hour);
221         BCD_TO_BIN(rtc_tm->tm_mday);
222         BCD_TO_BIN(rtc_tm->tm_mon);
223         BCD_TO_BIN(rtc_tm->tm_year);
224 
225         /*
226          * Account for differences between how the RTC uses the values
227          * and how they are defined in a struct rtc_time;
228          */
229 
230         if (rtc_tm->tm_year <= 69)
231                 rtc_tm->tm_year += 100;
232 
233         rtc_tm->tm_mon--;
234 }
235 
236 static unsigned char days_in_mo[] = 
237     {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
238 
239 /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */
240 
241 static int
242 rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
243           unsigned long arg) 
244 {
245         unsigned long flags;
246 
247         switch(cmd) {
248                 case RTC_RD_TIME:       /* read the time/date from RTC  */
249                 {
250                         struct rtc_time rtc_tm;
251                                                 
252                         memset(&rtc_tm, 0, sizeof (struct rtc_time));

⌨️ 快捷键说明

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