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