Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | *! $Log: ds1302.c,v $ |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 10 | *! Revision 1.18 2005/01/24 09:11:26 mikaelam |
| 11 | *! Minor changes to get DS1302 RTC chip driver to work |
| 12 | *! |
| 13 | *! Revision 1.17 2005/01/05 06:11:22 starvik |
| 14 | *! No need to do local_irq_disable after local_irq_save. |
| 15 | *! |
| 16 | *! Revision 1.16 2004/12/13 12:21:52 starvik |
| 17 | *! Added I/O and DMA allocators from Linux 2.4 |
| 18 | *! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | *! Revision 1.14 2004/08/24 06:48:43 starvik |
| 20 | *! Whitespace cleanup |
| 21 | *! |
| 22 | *! Revision 1.13 2004/05/28 09:26:59 starvik |
| 23 | *! Modified I2C initialization to work in 2.6. |
| 24 | *! |
| 25 | *! Revision 1.12 2004/05/14 07:58:03 starvik |
| 26 | *! Merge of changes from 2.4 |
| 27 | *! |
| 28 | *! Revision 1.10 2004/02/04 09:25:12 starvik |
| 29 | *! Merge of Linux 2.6.2 |
| 30 | *! |
| 31 | *! Revision 1.9 2003/07/04 08:27:37 starvik |
| 32 | *! Merge of Linux 2.5.74 |
| 33 | *! |
| 34 | *! Revision 1.8 2003/04/09 05:20:47 starvik |
| 35 | *! Merge of Linux 2.5.67 |
| 36 | *! |
| 37 | *! Revision 1.6 2003/01/09 14:42:51 starvik |
| 38 | *! Merge of Linux 2.5.55 |
| 39 | *! |
| 40 | *! Revision 1.4 2002/12/11 13:13:57 starvik |
| 41 | *! Added arch/ to v10 specific includes |
| 42 | *! Added fix from Linux 2.4 in serial.c (flush_to_flip_buffer) |
| 43 | *! |
| 44 | *! Revision 1.3 2002/11/20 11:56:10 starvik |
| 45 | *! Merge of Linux 2.5.48 |
| 46 | *! |
| 47 | *! Revision 1.2 2002/11/18 13:16:06 starvik |
| 48 | *! Linux 2.5 port of latest 2.4 drivers |
| 49 | *! |
| 50 | *! Revision 1.15 2002/10/11 16:14:33 johana |
| 51 | *! Added CONFIG_ETRAX_DS1302_TRICKLE_CHARGE and initial setting of the |
| 52 | *! trcklecharge register. |
| 53 | *! |
| 54 | *! Revision 1.14 2002/10/10 12:15:38 magnusmn |
| 55 | *! Added support for having the RST signal on bit g0 |
| 56 | *! |
| 57 | *! Revision 1.13 2002/05/29 15:16:08 johana |
| 58 | *! Removed unused variables. |
| 59 | *! |
| 60 | *! Revision 1.12 2002/04/10 15:35:25 johana |
| 61 | *! Moved probe function closer to init function and marked it __init. |
| 62 | *! |
| 63 | *! Revision 1.11 2001/06/14 12:35:52 jonashg |
| 64 | *! The ATA hack is back. It is unfortunately the only way to set g27 to output. |
| 65 | *! |
| 66 | *! Revision 1.9 2001/06/14 10:00:14 jonashg |
| 67 | *! No need for tempudelay to be inline anymore (had to adjust the usec to |
| 68 | *! loops conversion because of this to make it slow enough to be a udelay). |
| 69 | *! |
| 70 | *! Revision 1.8 2001/06/14 08:06:32 jonashg |
| 71 | *! Made tempudelay delay usecs (well, just a tad more). |
| 72 | *! |
| 73 | *! Revision 1.7 2001/06/13 14:18:11 jonashg |
| 74 | *! Only allow processes with SYS_TIME capability to set time and charge. |
| 75 | *! |
| 76 | *! Revision 1.6 2001/06/12 15:22:07 jonashg |
| 77 | *! * Made init function __init. |
| 78 | *! * Parameter to out_byte() is unsigned char. |
| 79 | *! * The magic number 42 has got a name. |
| 80 | *! * Removed comment about /proc (nothing is exported there). |
| 81 | *! |
| 82 | *! Revision 1.5 2001/06/12 14:35:13 jonashg |
| 83 | *! Gave the module a name and added it to printk's. |
| 84 | *! |
| 85 | *! Revision 1.4 2001/05/31 14:53:40 jonashg |
| 86 | *! Made tempudelay() inline so that the watchdog doesn't reset (see |
| 87 | *! function comment). |
| 88 | *! |
| 89 | *! Revision 1.3 2001/03/26 16:03:06 bjornw |
| 90 | *! Needs linux/config.h |
| 91 | *! |
| 92 | *! Revision 1.2 2001/03/20 19:42:00 bjornw |
| 93 | *! Use the ETRAX prefix on the DS1302 options |
| 94 | *! |
| 95 | *! Revision 1.1 2001/03/20 09:13:50 magnusmn |
| 96 | *! Linux 2.4 port |
| 97 | *! |
| 98 | *! Revision 1.10 2000/07/05 15:38:23 bjornw |
| 99 | *! Dont update kernel time when a RTC_SET_TIME is done |
| 100 | *! |
| 101 | *! Revision 1.9 2000/03/02 15:42:59 macce |
| 102 | *! * Hack to make RTC work on all 2100/2400 |
| 103 | *! |
| 104 | *! Revision 1.8 2000/02/23 16:59:18 torbjore |
| 105 | *! added setup of R_GEN_CONFIG when RTC is connected to the generic port. |
| 106 | *! |
| 107 | *! Revision 1.7 2000/01/17 15:51:43 johana |
| 108 | *! Added RTC_SET_CHARGE ioctl to enable trickle charger. |
| 109 | *! |
| 110 | *! Revision 1.6 1999/10/27 13:19:47 bjornw |
| 111 | *! Added update_xtime_from_cmos which reads back the updated RTC into the kernel. |
| 112 | *! /dev/rtc calls it now. |
| 113 | *! |
| 114 | *! Revision 1.5 1999/10/27 12:39:37 bjornw |
| 115 | *! Disabled superuser check. Anyone can now set the time. |
| 116 | *! |
| 117 | *! Revision 1.4 1999/09/02 13:27:46 pkj |
| 118 | *! Added shadow for R_PORT_PB_CONFIG. |
| 119 | *! Renamed port_g_shadow to port_g_data_shadow. |
| 120 | *! |
| 121 | *! Revision 1.3 1999/09/02 08:28:06 pkj |
| 122 | *! Made it possible to select either port PB or the generic port for the RST |
| 123 | *! signal line to the DS1302 RTC. |
| 124 | *! Also make sure the RST bit is configured as output on Port PB (if used). |
| 125 | *! |
| 126 | *! Revision 1.2 1999/09/01 14:47:20 bjornw |
| 127 | *! Added support for /dev/rtc operations with ioctl RD_TIME and SET_TIME to read |
| 128 | *! and set the date. Register as major 121. |
| 129 | *! |
| 130 | *! Revision 1.1 1999/09/01 09:45:29 bjornw |
| 131 | *! Implemented a DS1302 RTC driver. |
| 132 | *! |
| 133 | *! |
| 134 | *! --------------------------------------------------------------------------- |
| 135 | *! |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 136 | *! (C) Copyright 1999, 2000, 2001, 2002, 2003, 2004 Axis Communications AB, LUND, SWEDEN |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | *! |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 138 | *! $Id: ds1302.c,v 1.18 2005/01/24 09:11:26 mikaelam Exp $ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | *! |
| 140 | *!***************************************************************************/ |
| 141 | |
| 142 | #include <linux/config.h> |
| 143 | |
| 144 | #include <linux/fs.h> |
| 145 | #include <linux/init.h> |
| 146 | #include <linux/mm.h> |
| 147 | #include <linux/module.h> |
| 148 | #include <linux/miscdevice.h> |
| 149 | #include <linux/delay.h> |
| 150 | #include <linux/bcd.h> |
| 151 | |
| 152 | #include <asm/uaccess.h> |
| 153 | #include <asm/system.h> |
| 154 | #include <asm/arch/svinto.h> |
| 155 | #include <asm/io.h> |
| 156 | #include <asm/rtc.h> |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 157 | #include <asm/arch/io_interface_mux.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
| 159 | #define RTC_MAJOR_NR 121 /* local major, change later */ |
| 160 | |
| 161 | static const char ds1302_name[] = "ds1302"; |
| 162 | |
| 163 | /* The DS1302 might be connected to different bits on different products. |
| 164 | * It has three signals - SDA, SCL and RST. RST and SCL are always outputs, |
| 165 | * but SDA can have a selected direction. |
| 166 | * For now, only PORT_PB is hardcoded. |
| 167 | */ |
| 168 | |
| 169 | /* The RST bit may be on either the Generic Port or Port PB. */ |
| 170 | #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT |
| 171 | #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_G_DATA, port_g_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x) |
| 172 | #define TK_RST_DIR(x) |
| 173 | #else |
| 174 | #define TK_RST_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x) |
| 175 | #define TK_RST_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_RSTBIT, x) |
| 176 | #endif |
| 177 | |
| 178 | |
| 179 | #define TK_SDA_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SDABIT, x) |
| 180 | #define TK_SCL_OUT(x) REG_SHADOW_SET(R_PORT_PB_DATA, port_pb_data_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x) |
| 181 | |
| 182 | #define TK_SDA_IN() ((*R_PORT_PB_READ >> CONFIG_ETRAX_DS1302_SDABIT) & 1) |
| 183 | /* 1 is out, 0 is in */ |
| 184 | #define TK_SDA_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SDABIT, x) |
| 185 | #define TK_SCL_DIR(x) REG_SHADOW_SET(R_PORT_PB_DIR, port_pb_dir_shadow, CONFIG_ETRAX_DS1302_SCLBIT, x) |
| 186 | |
| 187 | |
| 188 | /* |
| 189 | * The reason for tempudelay and not udelay is that loops_per_usec |
| 190 | * (used in udelay) is not set when functions here are called from time.c |
| 191 | */ |
| 192 | |
| 193 | static void tempudelay(int usecs) |
| 194 | { |
| 195 | volatile int loops; |
| 196 | |
| 197 | for(loops = usecs * 12; loops > 0; loops--) |
| 198 | /* nothing */; |
| 199 | } |
| 200 | |
| 201 | |
| 202 | /* Send 8 bits. */ |
| 203 | static void |
| 204 | out_byte(unsigned char x) |
| 205 | { |
| 206 | int i; |
| 207 | TK_SDA_DIR(1); |
| 208 | for (i = 8; i--;) { |
| 209 | /* The chip latches incoming bits on the rising edge of SCL. */ |
| 210 | TK_SCL_OUT(0); |
| 211 | TK_SDA_OUT(x & 1); |
| 212 | tempudelay(1); |
| 213 | TK_SCL_OUT(1); |
| 214 | tempudelay(1); |
| 215 | x >>= 1; |
| 216 | } |
| 217 | TK_SDA_DIR(0); |
| 218 | } |
| 219 | |
| 220 | static unsigned char |
| 221 | in_byte(void) |
| 222 | { |
| 223 | unsigned char x = 0; |
| 224 | int i; |
| 225 | |
| 226 | /* Read byte. Bits come LSB first, on the falling edge of SCL. |
| 227 | * Assume SDA is in input direction already. |
| 228 | */ |
| 229 | TK_SDA_DIR(0); |
| 230 | |
| 231 | for (i = 8; i--;) { |
| 232 | TK_SCL_OUT(0); |
| 233 | tempudelay(1); |
| 234 | x >>= 1; |
| 235 | x |= (TK_SDA_IN() << 7); |
| 236 | TK_SCL_OUT(1); |
| 237 | tempudelay(1); |
| 238 | } |
| 239 | |
| 240 | return x; |
| 241 | } |
| 242 | |
| 243 | /* Prepares for a transaction by de-activating RST (active-low). */ |
| 244 | |
| 245 | static void |
| 246 | start(void) |
| 247 | { |
| 248 | TK_SCL_OUT(0); |
| 249 | tempudelay(1); |
| 250 | TK_RST_OUT(0); |
| 251 | tempudelay(5); |
| 252 | TK_RST_OUT(1); |
| 253 | } |
| 254 | |
| 255 | /* Ends a transaction by taking RST active again. */ |
| 256 | |
| 257 | static void |
| 258 | stop(void) |
| 259 | { |
| 260 | tempudelay(2); |
| 261 | TK_RST_OUT(0); |
| 262 | } |
| 263 | |
| 264 | /* Enable writing. */ |
| 265 | |
| 266 | static void |
| 267 | ds1302_wenable(void) |
| 268 | { |
| 269 | start(); |
| 270 | out_byte(0x8e); /* Write control register */ |
| 271 | out_byte(0x00); /* Disable write protect bit 7 = 0 */ |
| 272 | stop(); |
| 273 | } |
| 274 | |
| 275 | /* Disable writing. */ |
| 276 | |
| 277 | static void |
| 278 | ds1302_wdisable(void) |
| 279 | { |
| 280 | start(); |
| 281 | out_byte(0x8e); /* Write control register */ |
| 282 | out_byte(0x80); /* Disable write protect bit 7 = 0 */ |
| 283 | stop(); |
| 284 | } |
| 285 | |
| 286 | |
| 287 | |
| 288 | /* Read a byte from the selected register in the DS1302. */ |
| 289 | |
| 290 | unsigned char |
| 291 | ds1302_readreg(int reg) |
| 292 | { |
| 293 | unsigned char x; |
| 294 | |
| 295 | start(); |
| 296 | out_byte(0x81 | (reg << 1)); /* read register */ |
| 297 | x = in_byte(); |
| 298 | stop(); |
| 299 | |
| 300 | return x; |
| 301 | } |
| 302 | |
| 303 | /* Write a byte to the selected register. */ |
| 304 | |
| 305 | void |
| 306 | ds1302_writereg(int reg, unsigned char val) |
| 307 | { |
| 308 | #ifndef CONFIG_ETRAX_RTC_READONLY |
| 309 | int do_writereg = 1; |
| 310 | #else |
| 311 | int do_writereg = 0; |
| 312 | |
| 313 | if (reg == RTC_TRICKLECHARGER) |
| 314 | do_writereg = 1; |
| 315 | #endif |
| 316 | |
| 317 | if (do_writereg) { |
| 318 | ds1302_wenable(); |
| 319 | start(); |
| 320 | out_byte(0x80 | (reg << 1)); /* write register */ |
| 321 | out_byte(val); |
| 322 | stop(); |
| 323 | ds1302_wdisable(); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | void |
| 328 | get_rtc_time(struct rtc_time *rtc_tm) |
| 329 | { |
| 330 | unsigned long flags; |
| 331 | |
| 332 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
| 334 | rtc_tm->tm_sec = CMOS_READ(RTC_SECONDS); |
| 335 | rtc_tm->tm_min = CMOS_READ(RTC_MINUTES); |
| 336 | rtc_tm->tm_hour = CMOS_READ(RTC_HOURS); |
| 337 | rtc_tm->tm_mday = CMOS_READ(RTC_DAY_OF_MONTH); |
| 338 | rtc_tm->tm_mon = CMOS_READ(RTC_MONTH); |
| 339 | rtc_tm->tm_year = CMOS_READ(RTC_YEAR); |
| 340 | |
| 341 | local_irq_restore(flags); |
| 342 | |
| 343 | BCD_TO_BIN(rtc_tm->tm_sec); |
| 344 | BCD_TO_BIN(rtc_tm->tm_min); |
| 345 | BCD_TO_BIN(rtc_tm->tm_hour); |
| 346 | BCD_TO_BIN(rtc_tm->tm_mday); |
| 347 | BCD_TO_BIN(rtc_tm->tm_mon); |
| 348 | BCD_TO_BIN(rtc_tm->tm_year); |
| 349 | |
| 350 | /* |
| 351 | * Account for differences between how the RTC uses the values |
| 352 | * and how they are defined in a struct rtc_time; |
| 353 | */ |
| 354 | |
| 355 | if (rtc_tm->tm_year <= 69) |
| 356 | rtc_tm->tm_year += 100; |
| 357 | |
| 358 | rtc_tm->tm_mon--; |
| 359 | } |
| 360 | |
| 361 | static unsigned char days_in_mo[] = |
| 362 | {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; |
| 363 | |
| 364 | /* ioctl that supports RTC_RD_TIME and RTC_SET_TIME (read and set time/date). */ |
| 365 | |
| 366 | static int |
| 367 | rtc_ioctl(struct inode *inode, struct file *file, unsigned int cmd, |
| 368 | unsigned long arg) |
| 369 | { |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 370 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | |
| 372 | switch(cmd) { |
| 373 | case RTC_RD_TIME: /* read the time/date from RTC */ |
| 374 | { |
| 375 | struct rtc_time rtc_tm; |
| 376 | |
| 377 | memset(&rtc_tm, 0, sizeof (struct rtc_time)); |
| 378 | get_rtc_time(&rtc_tm); |
| 379 | if (copy_to_user((struct rtc_time*)arg, &rtc_tm, sizeof(struct rtc_time))) |
| 380 | return -EFAULT; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | case RTC_SET_TIME: /* set the RTC */ |
| 385 | { |
| 386 | struct rtc_time rtc_tm; |
| 387 | unsigned char mon, day, hrs, min, sec, leap_yr; |
| 388 | unsigned int yrs; |
| 389 | |
| 390 | if (!capable(CAP_SYS_TIME)) |
| 391 | return -EPERM; |
| 392 | |
| 393 | if (copy_from_user(&rtc_tm, (struct rtc_time*)arg, sizeof(struct rtc_time))) |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 394 | return -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
| 396 | yrs = rtc_tm.tm_year + 1900; |
| 397 | mon = rtc_tm.tm_mon + 1; /* tm_mon starts at zero */ |
| 398 | day = rtc_tm.tm_mday; |
| 399 | hrs = rtc_tm.tm_hour; |
| 400 | min = rtc_tm.tm_min; |
| 401 | sec = rtc_tm.tm_sec; |
| 402 | |
| 403 | |
| 404 | if ((yrs < 1970) || (yrs > 2069)) |
| 405 | return -EINVAL; |
| 406 | |
| 407 | leap_yr = ((!(yrs % 4) && (yrs % 100)) || !(yrs % 400)); |
| 408 | |
| 409 | if ((mon > 12) || (day == 0)) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | if (day > (days_in_mo[mon] + ((mon == 2) && leap_yr))) |
| 413 | return -EINVAL; |
| 414 | |
| 415 | if ((hrs >= 24) || (min >= 60) || (sec >= 60)) |
| 416 | return -EINVAL; |
| 417 | |
| 418 | if (yrs >= 2000) |
| 419 | yrs -= 2000; /* RTC (0, 1, ... 69) */ |
| 420 | else |
| 421 | yrs -= 1900; /* RTC (70, 71, ... 99) */ |
| 422 | |
| 423 | BIN_TO_BCD(sec); |
| 424 | BIN_TO_BCD(min); |
| 425 | BIN_TO_BCD(hrs); |
| 426 | BIN_TO_BCD(day); |
| 427 | BIN_TO_BCD(mon); |
| 428 | BIN_TO_BCD(yrs); |
| 429 | |
| 430 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | CMOS_WRITE(yrs, RTC_YEAR); |
| 432 | CMOS_WRITE(mon, RTC_MONTH); |
| 433 | CMOS_WRITE(day, RTC_DAY_OF_MONTH); |
| 434 | CMOS_WRITE(hrs, RTC_HOURS); |
| 435 | CMOS_WRITE(min, RTC_MINUTES); |
| 436 | CMOS_WRITE(sec, RTC_SECONDS); |
| 437 | local_irq_restore(flags); |
| 438 | |
| 439 | /* Notice that at this point, the RTC is updated but |
| 440 | * the kernel is still running with the old time. |
| 441 | * You need to set that separately with settimeofday |
| 442 | * or adjtimex. |
| 443 | */ |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | case RTC_SET_CHARGE: /* set the RTC TRICKLE CHARGE register */ |
| 448 | { |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 449 | int tcs_val; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
| 451 | if (!capable(CAP_SYS_TIME)) |
| 452 | return -EPERM; |
| 453 | |
| 454 | if(copy_from_user(&tcs_val, (int*)arg, sizeof(int))) |
| 455 | return -EFAULT; |
| 456 | |
| 457 | tcs_val = RTC_TCR_PATTERN | (tcs_val & 0x0F); |
| 458 | ds1302_writereg(RTC_TRICKLECHARGER, tcs_val); |
| 459 | return 0; |
| 460 | } |
| 461 | case RTC_VLOW_RD: |
| 462 | { |
| 463 | /* TODO: |
| 464 | * Implement voltage low detection support |
| 465 | */ |
| 466 | printk(KERN_WARNING "DS1302: RTC Voltage Low detection" |
| 467 | " is not supported\n"); |
| 468 | return 0; |
| 469 | } |
| 470 | case RTC_VLOW_SET: |
| 471 | { |
| 472 | /* TODO: |
| 473 | * Nothing to do since Voltage Low detection is not supported |
| 474 | */ |
| 475 | return 0; |
| 476 | } |
| 477 | default: |
| 478 | return -ENOIOCTLCMD; |
| 479 | } |
| 480 | } |
| 481 | |
| 482 | static void |
| 483 | print_rtc_status(void) |
| 484 | { |
| 485 | struct rtc_time tm; |
| 486 | |
| 487 | get_rtc_time(&tm); |
| 488 | |
| 489 | /* |
| 490 | * There is no way to tell if the luser has the RTC set for local |
| 491 | * time or for Universal Standard Time (GMT). Probably local though. |
| 492 | */ |
| 493 | |
| 494 | printk(KERN_INFO "rtc_time\t: %02d:%02d:%02d\n", |
| 495 | tm.tm_hour, tm.tm_min, tm.tm_sec); |
| 496 | printk(KERN_INFO "rtc_date\t: %04d-%02d-%02d\n", |
| 497 | tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday); |
| 498 | } |
| 499 | |
| 500 | /* The various file operations we support. */ |
| 501 | |
| 502 | static struct file_operations rtc_fops = { |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 503 | .owner = THIS_MODULE, |
| 504 | .ioctl = rtc_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | }; |
| 506 | |
| 507 | /* Probe for the chip by writing something to its RAM and try reading it back. */ |
| 508 | |
| 509 | #define MAGIC_PATTERN 0x42 |
| 510 | |
| 511 | static int __init |
| 512 | ds1302_probe(void) |
| 513 | { |
| 514 | int retval, res; |
| 515 | |
| 516 | TK_RST_DIR(1); |
| 517 | TK_SCL_DIR(1); |
| 518 | TK_SDA_DIR(0); |
| 519 | |
| 520 | /* Try to talk to timekeeper. */ |
| 521 | |
| 522 | ds1302_wenable(); |
| 523 | start(); |
| 524 | out_byte(0xc0); /* write RAM byte 0 */ |
| 525 | out_byte(MAGIC_PATTERN); /* write something magic */ |
| 526 | start(); |
| 527 | out_byte(0xc1); /* read RAM byte 0 */ |
| 528 | |
| 529 | if((res = in_byte()) == MAGIC_PATTERN) { |
| 530 | stop(); |
| 531 | ds1302_wdisable(); |
| 532 | printk(KERN_INFO "%s: RTC found.\n", ds1302_name); |
| 533 | printk(KERN_INFO "%s: SDA, SCL, RST on PB%i, PB%i, %s%i\n", |
| 534 | ds1302_name, |
| 535 | CONFIG_ETRAX_DS1302_SDABIT, |
| 536 | CONFIG_ETRAX_DS1302_SCLBIT, |
| 537 | #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT |
| 538 | "GENIO", |
| 539 | #else |
| 540 | "PB", |
| 541 | #endif |
| 542 | CONFIG_ETRAX_DS1302_RSTBIT); |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 543 | print_rtc_status(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 544 | retval = 1; |
| 545 | } else { |
| 546 | stop(); |
| 547 | retval = 0; |
| 548 | } |
| 549 | |
| 550 | return retval; |
| 551 | } |
| 552 | |
| 553 | |
| 554 | /* Just probe for the RTC and register the device to handle the ioctl needed. */ |
| 555 | |
| 556 | int __init |
| 557 | ds1302_init(void) |
| 558 | { |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 559 | #ifdef CONFIG_ETRAX_I2C |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 560 | i2c_init(); |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 561 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
| 563 | if (!ds1302_probe()) { |
| 564 | #ifdef CONFIG_ETRAX_DS1302_RST_ON_GENERIC_PORT |
| 565 | #if CONFIG_ETRAX_DS1302_RSTBIT == 27 |
| 566 | /* |
| 567 | * The only way to set g27 to output is to enable ATA. |
| 568 | * |
| 569 | * Make sure that R_GEN_CONFIG is setup correct. |
| 570 | */ |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 571 | /* Allocating the ATA interface will grab almost all |
| 572 | * pins in I/O groups a, b, c and d. A consequence of |
| 573 | * allocating the ATA interface is that the fixed |
| 574 | * interfaces shared RAM, parallel port 0, parallel |
| 575 | * port 1, parallel port W, SCSI-8 port 0, SCSI-8 port |
| 576 | * 1, SCSI-W, serial port 2, serial port 3, |
| 577 | * synchronous serial port 3 and USB port 2 and almost |
| 578 | * all GPIO pins on port g cannot be used. |
| 579 | */ |
| 580 | if (cris_request_io_interface(if_ata, "ds1302/ATA")) { |
| 581 | printk(KERN_WARNING "ds1302: Failed to get IO interface\n"); |
| 582 | return -1; |
| 583 | } |
| 584 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | #elif CONFIG_ETRAX_DS1302_RSTBIT == 0 |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 586 | if (cris_io_interface_allocate_pins(if_gpio_grp_a, |
| 587 | 'g', |
| 588 | CONFIG_ETRAX_DS1302_RSTBIT, |
| 589 | CONFIG_ETRAX_DS1302_RSTBIT)) { |
| 590 | printk(KERN_WARNING "ds1302: Failed to get IO interface\n"); |
| 591 | return -1; |
| 592 | } |
| 593 | |
| 594 | /* Set the direction of this bit to out. */ |
| 595 | genconfig_shadow = ((genconfig_shadow & |
| 596 | ~IO_MASK(R_GEN_CONFIG, g0dir)) | |
| 597 | (IO_STATE(R_GEN_CONFIG, g0dir, out))); |
| 598 | *R_GEN_CONFIG = genconfig_shadow; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | #endif |
| 600 | if (!ds1302_probe()) { |
| 601 | printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name); |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 602 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | } |
| 604 | #else |
| 605 | printk(KERN_WARNING "%s: RTC not found.\n", ds1302_name); |
Mikael Starvik | 7e92042 | 2005-07-27 11:44:34 -0700 | [diff] [blame^] | 606 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | #endif |
| 608 | } |
| 609 | /* Initialise trickle charger */ |
| 610 | ds1302_writereg(RTC_TRICKLECHARGER, |
| 611 | RTC_TCR_PATTERN |(CONFIG_ETRAX_DS1302_TRICKLE_CHARGE & 0x0F)); |
| 612 | /* Start clock by resetting CLOCK_HALT */ |
| 613 | ds1302_writereg(RTC_SECONDS, (ds1302_readreg(RTC_SECONDS) & 0x7F)); |
| 614 | return 0; |
| 615 | } |
| 616 | |
| 617 | static int __init ds1302_register(void) |
| 618 | { |
| 619 | ds1302_init(); |
| 620 | if (register_chrdev(RTC_MAJOR_NR, ds1302_name, &rtc_fops)) { |
| 621 | printk(KERN_INFO "%s: unable to get major %d for rtc\n", |
| 622 | ds1302_name, RTC_MAJOR_NR); |
| 623 | return -1; |
| 624 | } |
| 625 | return 0; |
| 626 | |
| 627 | } |
| 628 | |
| 629 | module_init(ds1302_register); |