1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23 * Use is subject to license terms. 24 */ 25 26 27 #include <sys/types.h> 28 #include <sys/conf.h> 29 #include <sys/devops.h> 30 #include <sys/kmem.h> 31 #include <sys/open.h> 32 #include <sys/file.h> 33 #include <sys/note.h> 34 #include <sys/ddi.h> 35 #include <sys/sunddi.h> 36 37 #include <sys/modctl.h> 38 #include <sys/stat.h> 39 #include <sys/clock.h> 40 #include <sys/reboot.h> 41 #include <sys/machsystm.h> 42 #include <sys/poll.h> 43 #include <sys/pbio.h> 44 #include <sys/sysmacros.h> 45 46 /* Added for prom interface */ 47 #include <sys/promif.h> 48 #include <sys/promimpl.h> 49 50 #include <sys/i2c/misc/i2c_svc.h> 51 #include <sys/todds1337.h> 52 53 #define DS1337_DEVICE_TYPE "rtc" 54 55 /* 56 * Driver entry routines 57 */ 58 static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t); 59 static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t); 60 static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 61 62 /* 63 * tod_ops entry routines 64 */ 65 static timestruc_t todds1337_get(void); 66 static void todds1337_set(timestruc_t); 67 static uint_t todds1337_set_watchdog_timer(uint_t); 68 static uint_t todds1337_clear_watchdog_timer(void); 69 static void todds1337_set_power_alarm(timestruc_t); 70 static void todds1337_clear_power_alarm(void); 71 static int todds1337_setup_prom(); 72 static void todds1337_rele_prom(); 73 static int todds1337_prom_getdate(struct rtc_t *rtc); 74 static int todds1337_prom_setdate(struct rtc_t *rtc); 75 76 /* 77 * Local functions 78 */ 79 static int todds1337_read_rtc(struct rtc_t *); 80 static int todds1337_write_rtc(struct rtc_t *); 81 82 /* Anchor for soft state structure */ 83 static void *ds1337_statep; 84 static int instance = -1; 85 static int todds1337_attach_done = 0; 86 static kmutex_t todds1337_rd_lock; 87 static kmutex_t todds1337_alarm_lock; 88 static ihandle_t todds1337_ihandle = 0; 89 90 /* one second time out */ 91 #define I2C_CYCLIC_TIMEOUT 1000000000 92 uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT; 93 static int sync_clock_once = 1; 94 static struct rtc_t soft_rtc; 95 96 /* 97 * cp_ops structure 98 */ 99 static struct cb_ops ds1337_cbops = { 100 nodev, /* open */ 101 nodev, /* close */ 102 nodev, /* strategy */ 103 nodev, /* print */ 104 nodev, /* dump */ 105 nodev, /* read */ 106 nodev, /* write */ 107 nodev, /* ioctl */ 108 nodev, /* devmap */ 109 nodev, /* mmap */ 110 nodev, /* segmap */ 111 NULL, /* poll */ 112 ddi_prop_op, /* cb_prop_op */ 113 NULL, /* streamtab */ 114 D_NEW | D_MP, /* Driver compatibility flag */ 115 CB_REV, /* rev */ 116 nodev, /* int (*cb_aread)() */ 117 nodev /* int (*cb_awrite)() */ 118 }; 119 120 /* 121 * dev_ops structure 122 */ 123 static struct dev_ops ds1337_ops = { 124 DEVO_REV, /* devo_rev */ 125 0, /* refcnt - reference cnt always set to 0 */ 126 todds1337_getinfo, /* getinfo - Maybe requred */ 127 nulldev, /* identify */ 128 nulldev, /* probe */ 129 todds1337_attach, /* attach */ 130 todds1337_detach, /* detach */ 131 nodev, /* reset */ 132 &ds1337_cbops, /* cb_ops - ds1337 does not need this(?) */ 133 NULL, /* bus_ops */ 134 NULL, /* power */ 135 ddi_quiesce_not_needed, /* quiesce */ 136 }; 137 138 static struct modldrv todds1337_modldrv = { 139 &mod_driverops, /* Type of module. This one is a driver */ 140 "tod driver for DS1337 1.12", /* Name of the module. */ 141 &ds1337_ops, /* Pointer to dev_ops */ 142 }; 143 144 /* 145 * Module linkage structure 146 */ 147 static struct modlinkage todds1337_modlinkage = { 148 MODREV_1, 149 &todds1337_modldrv, 150 0 151 }; 152 153 int 154 _init(void) 155 { 156 int error; 157 158 if (strcmp(tod_module_name, "todds1337") == 0) { 159 if ((error = ddi_soft_state_init(&ds1337_statep, 160 sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) { 161 return (error); 162 } 163 164 tod_ops.tod_get = todds1337_get; 165 tod_ops.tod_set = todds1337_set; 166 tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer; 167 tod_ops.tod_clear_watchdog_timer = 168 todds1337_clear_watchdog_timer; 169 tod_ops.tod_set_power_alarm = todds1337_set_power_alarm; 170 tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm; 171 } 172 173 (void) todds1337_setup_prom(); 174 175 /* 176 * Install the module 177 */ 178 if ((error = mod_install(&todds1337_modlinkage)) != 0) { 179 if (strcmp(tod_module_name, "todds1337") == 0) { 180 ddi_soft_state_fini(&ds1337_statep); 181 } 182 todds1337_rele_prom(); 183 return (error); 184 } 185 mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL); 186 mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL); 187 188 return (0); 189 } 190 191 int 192 _fini(void) 193 { 194 int error = 0; 195 196 if (strcmp(tod_module_name, "todds1337") == 0) { 197 error = EBUSY; 198 } else { 199 if ((error = mod_remove(&todds1337_modlinkage)) == 0) { 200 mutex_destroy(&todds1337_rd_lock); 201 mutex_destroy(&todds1337_alarm_lock); 202 todds1337_rele_prom(); 203 } 204 } 205 206 return (error); 207 } 208 209 int 210 _info(struct modinfo *modinfop) 211 { 212 return (mod_info(&todds1337_modlinkage, modinfop)); 213 } 214 215 /* 216 * cyclical call to get tod. 217 */ 218 static void 219 todds1337_cyclic(void *arg) 220 { 221 222 (void) todds1337_read_rtc((struct rtc_t *)arg); 223 224 } 225 226 /* 227 * register ds1337 client device with i2c services, and 228 * allocate & initialize soft state structure. 229 */ 230 static int 231 todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 232 { 233 static ds1337_state_t *statep = NULL; 234 i2c_transfer_t *i2c_tp = NULL; 235 uint8_t tempVal = (uint8_t)0; 236 switch (cmd) { 237 case DDI_ATTACH: 238 break; 239 case DDI_RESUME: 240 return (DDI_SUCCESS); 241 default: 242 return (DDI_FAILURE); 243 } 244 245 if (instance != -1) { 246 cmn_err(CE_WARN, "todds1337_attach: wrong instance"); 247 return (DDI_FAILURE); 248 } 249 250 instance = ddi_get_instance(dip); 251 252 /* 253 * Allocate soft state structure 254 */ 255 if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) { 256 cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft " 257 "state"); 258 instance = -1; 259 return (DDI_FAILURE); 260 } 261 262 statep = ddi_get_soft_state(ds1337_statep, instance); 263 if (statep == NULL) { 264 cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft " 265 "state"); 266 instance = -1; 267 return (DDI_FAILURE); 268 } 269 270 statep->dip = dip; 271 272 if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) { 273 ddi_soft_state_free(ds1337_statep, instance); 274 cmn_err(CE_WARN, "todds1337_attach: cannot register i2c " 275 "client"); 276 instance = -1; 277 return (DDI_FAILURE); 278 } 279 280 /* check and initialize the oscillator */ 281 282 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 283 &i2c_tp, 1, 1, I2C_SLEEP); 284 i2c_tp->i2c_version = I2C_XFER_REV; 285 i2c_tp->i2c_flags = I2C_WR_RD; 286 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */ 287 i2c_tp->i2c_wlen = 1; 288 i2c_tp->i2c_rlen = 1; 289 290 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 291 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 292 i2c_client_unregister(statep->ds1337_i2c_hdl); 293 ddi_soft_state_free(ds1337_statep, instance); 294 cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 " 295 "status register"); 296 instance = -1; 297 return (DDI_FAILURE); 298 } 299 300 tempVal = i2c_tp->i2c_rbuf[0]; 301 302 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 303 304 /* 305 * Check Oscillator and initialize chip if OBP failed to do it 306 */ 307 308 if (tempVal & RTC_CTL_EOSC) { 309 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 310 2, 0, I2C_SLEEP); 311 i2c_tp->i2c_version = I2C_XFER_REV; 312 i2c_tp->i2c_flags = I2C_WR; 313 i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */ 314 i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 | 315 RTC_CTL_INTCN); 316 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) 317 != I2C_SUCCESS) { 318 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, 319 i2c_tp); 320 i2c_client_unregister(statep->ds1337_i2c_hdl); 321 ddi_soft_state_free(ds1337_statep, instance); 322 cmn_err(CE_WARN, "todds1337_attach: failed to write " 323 "DS1337 control register"); 324 instance = -1; 325 return (DDI_FAILURE); 326 } 327 328 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 329 330 /* 331 * Now reset the OSF flag in the Status register 332 */ 333 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 334 2, 0, I2C_SLEEP); 335 i2c_tp->i2c_version = I2C_XFER_REV; 336 i2c_tp->i2c_flags = I2C_WR; 337 i2c_tp->i2c_wbuf[0] = RTC_STATUS; 338 i2c_tp->i2c_wbuf[1] = (uchar_t)0; 339 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) 340 != I2C_SUCCESS) { 341 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, 342 i2c_tp); 343 i2c_client_unregister(statep->ds1337_i2c_hdl); 344 ddi_soft_state_free(ds1337_statep, instance); 345 cmn_err(CE_WARN, "todds1337_attach: failed to write " 346 "DS1337 status register"); 347 instance = -1; 348 return (DDI_FAILURE); 349 } 350 351 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 352 } 353 354 /* 355 * Create a periodical handler to read TOD. 356 */ 357 ASSERT(statep->cycid == NULL); 358 statep->cycid = ddi_periodic_add(todds1337_cyclic, &soft_rtc, 359 i2c_cyclic_timeout, DDI_IPL_1); 360 statep->state = TOD_ATTACHED; 361 todds1337_attach_done = 1; 362 ddi_report_dev(dip); 363 364 return (DDI_SUCCESS); 365 } 366 367 /*ARGSUSED*/ 368 static int 369 todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 370 { 371 /* 372 * Once attached, do not allow detach because the system constantly 373 * calling todds1337_get() to get the time. If the driver is detached 374 * and the system try to get the time, the system will have memory 375 * problem. 376 * 377 */ 378 switch (cmd) { 379 case DDI_SUSPEND: 380 return (DDI_SUCCESS); 381 default: 382 return (DDI_FAILURE); 383 } 384 } 385 386 /* *********************** tod_ops entry points ******************** */ 387 388 /* 389 * Read the current time from the DS1337 chip and convert to UNIX form. 390 * Should be called with tod_clock held. 391 */ 392 393 static timestruc_t 394 todds1337_get(void) 395 { 396 timestruc_t ts; 397 todinfo_t tod; 398 struct rtc_t rtc; 399 400 ASSERT(MUTEX_HELD(&tod_lock)); 401 402 if (sync_clock_once) { 403 (void) todds1337_read_rtc(&soft_rtc); 404 sync_clock_once = 0; 405 } else { 406 tod_fault_reset(); 407 return (hrestime); 408 } 409 410 bcopy(&soft_rtc, &rtc, sizeof (rtc)); 411 412 /* 413 * 00 - 68 = 2000 thru 2068 414 * 69-99 = 1969 thru 1999 415 */ 416 tod.tod_year = rtc.rtc_year; 417 if (rtc.rtc_year <= 68) 418 tod.tod_year += 100; 419 tod.tod_month = rtc.rtc_mon; 420 tod.tod_day = rtc.rtc_dom; 421 tod.tod_dow = rtc.rtc_dow; 422 tod.tod_hour = rtc.rtc_hrs; 423 tod.tod_min = rtc.rtc_min; 424 tod.tod_sec = rtc.rtc_sec; 425 426 ts.tv_sec = tod_to_utc(tod); 427 ts.tv_nsec = 0; 428 return (ts); 429 } 430 431 /* 432 * Program DS1337 with the specified time. 433 * Must be called with tod_lock held. The TOD 434 * chip supports date from 1969-2068 only. We must 435 * reject requests to set date below 1969. 436 */ 437 static void 438 todds1337_set(timestruc_t ts) 439 { 440 struct rtc_t rtc; 441 todinfo_t tod = utc_to_tod(ts.tv_sec); 442 int year; 443 444 445 ASSERT(MUTEX_HELD(&tod_lock)); 446 447 /* 448 * Year is base 1900, valid year range 1969-2068 449 */ 450 if ((tod.tod_year < 69) || (tod.tod_year > 168)) 451 return; 452 453 year = tod.tod_year; 454 if (year >= 100) 455 year -= 100; 456 457 rtc.rtc_year = (uint8_t)year; 458 rtc.rtc_mon = (uint8_t)tod.tod_month; 459 rtc.rtc_dom = (uint8_t)tod.tod_day; 460 rtc.rtc_dow = (uint8_t)tod.tod_dow; 461 rtc.rtc_hrs = (uint8_t)tod.tod_hour; 462 rtc.rtc_min = (uint8_t)tod.tod_min; 463 rtc.rtc_sec = (uint8_t)tod.tod_sec; 464 465 (void) todds1337_write_rtc(&rtc); 466 } 467 468 /* 469 * Program ds1337 registers for alarm to go off at the specified time. 470 * Alarm #1 is used (Alarm #2 stays idle) 471 */ 472 /* ARGSUSED */ 473 static void 474 todds1337_set_power_alarm(timestruc_t ts) 475 { 476 todinfo_t tod; 477 ds1337_state_t *statep = NULL; 478 i2c_transfer_t *i2c_tp = NULL; 479 uint8_t tmpval; 480 481 ASSERT(MUTEX_HELD(&tod_lock)); 482 483 if (!todds1337_attach_done) { 484 cmn_err(CE_WARN, "todds1337: driver not attached"); 485 return; 486 } 487 488 statep = ddi_get_soft_state(ds1337_statep, instance); 489 if (statep == NULL) { 490 cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed"); 491 return; 492 } 493 494 tod = utc_to_tod(ts.tv_sec); 495 496 /* 497 * i2c_transfe() may block; to avoid locking clock() which 498 * is running in interrupt context at PIL10 we temporarely exit 499 * the tod_mutex and enter alarm lock. Time/date and alarm hardware 500 * have non-interlsecting registers, it is safe to use different locks. 501 */ 502 mutex_exit(&tod_lock); 503 mutex_enter(&todds1337_alarm_lock); 504 505 /* 506 * Disable Power Alarm (A1IE) 507 */ 508 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 509 &i2c_tp, 1, 1, I2C_SLEEP); 510 i2c_tp->i2c_version = I2C_XFER_REV; 511 i2c_tp->i2c_flags = I2C_WR_RD; 512 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */ 513 514 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 515 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 516 cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read " 517 "DS1337 control register"); 518 mutex_exit(&todds1337_alarm_lock); 519 mutex_enter(&tod_lock); 520 return; 521 } 522 523 tmpval = i2c_tp->i2c_rbuf[0]; 524 525 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 526 527 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 528 &i2c_tp, 2, 0, I2C_SLEEP); 529 i2c_tp->i2c_version = I2C_XFER_REV; 530 i2c_tp->i2c_flags = I2C_WR; 531 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 532 i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE; 533 534 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 535 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 536 cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write " 537 "DS1337control register"); 538 mutex_exit(&todds1337_alarm_lock); 539 mutex_enter(&tod_lock); 540 return; 541 } 542 543 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 544 545 546 /* 547 * Write Alarm #1 registers 548 */ 549 550 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 551 &i2c_tp, 5, 0, I2C_SLEEP); 552 i2c_tp->i2c_version = I2C_XFER_REV; 553 i2c_tp->i2c_flags = I2C_WR; 554 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */ 555 i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec); 556 i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min); 557 i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour); 558 i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day); 559 560 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 561 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 562 cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write " 563 "DS1337 alarm registers"); 564 mutex_exit(&todds1337_alarm_lock); 565 mutex_enter(&tod_lock); 566 return; 567 } 568 569 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 570 571 572 /* 573 * Enable Power Alarm 574 */ 575 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 576 &i2c_tp, 2, 0, I2C_SLEEP); 577 i2c_tp->i2c_version = I2C_XFER_REV; 578 i2c_tp->i2c_flags = I2C_WR; 579 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 580 i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE; 581 582 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 583 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 584 cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable " 585 "DS1337 alarm"); 586 mutex_exit(&todds1337_alarm_lock); 587 mutex_enter(&tod_lock); 588 return; 589 } 590 591 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 592 593 mutex_exit(&todds1337_alarm_lock); 594 mutex_enter(&tod_lock); 595 } 596 597 /* ARGSUSED */ 598 static void 599 todds1337_clear_power_alarm(void) 600 { 601 ds1337_state_t *statep = NULL; 602 i2c_transfer_t *i2c_tp = NULL; 603 uint8_t tmpval; 604 605 ASSERT(MUTEX_HELD(&tod_lock)); 606 607 if (!todds1337_attach_done) { 608 cmn_err(CE_WARN, "todds1337: driver was not attached"); 609 return; 610 } 611 612 statep = ddi_get_soft_state(ds1337_statep, instance); 613 if (statep == NULL) { 614 cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed"); 615 return; 616 } 617 618 /* 619 * i2c_transfe() may block; to avoid locking clock() which 620 * is running in interrupt context at PIL10 we temporarely exit 621 * the tod_mutex and enter alarm lock. Time/date and alarm hardware 622 * have non-interlsecting registers, it is safe to use different locks. 623 */ 624 mutex_exit(&tod_lock); 625 mutex_enter(&todds1337_alarm_lock); 626 627 /* 628 * Disable Alarm #1 Interrupt 629 */ 630 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 631 &i2c_tp, 1, 1, I2C_SLEEP); 632 i2c_tp->i2c_version = I2C_XFER_REV; 633 i2c_tp->i2c_flags = I2C_WR_RD; 634 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */ 635 636 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 637 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 638 cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read " 639 "DS1337 control register"); 640 mutex_exit(&todds1337_alarm_lock); 641 mutex_enter(&tod_lock); 642 return; 643 } 644 645 tmpval = i2c_tp->i2c_rbuf[0]; 646 647 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 648 649 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 650 &i2c_tp, 2, 0, I2C_SLEEP); 651 i2c_tp->i2c_version = I2C_XFER_REV; 652 i2c_tp->i2c_flags = I2C_WR; 653 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 654 i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE; 655 656 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 657 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 658 cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write " 659 "DS1337 control register"); 660 mutex_exit(&todds1337_alarm_lock); 661 mutex_enter(&tod_lock); 662 return; 663 } 664 665 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 666 667 /* 668 * Reset Alarm #1 Flag 669 */ 670 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 671 &i2c_tp, 1, 1, I2C_SLEEP); 672 i2c_tp->i2c_version = I2C_XFER_REV; 673 i2c_tp->i2c_flags = I2C_WR_RD; 674 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */ 675 676 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 677 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 678 cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read " 679 "DS1337 status register"); 680 mutex_exit(&todds1337_alarm_lock); 681 mutex_enter(&tod_lock); 682 return; 683 } 684 685 tmpval = i2c_tp->i2c_rbuf[0]; 686 687 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 688 689 (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 690 &i2c_tp, 2, 0, I2C_SLEEP); 691 i2c_tp->i2c_version = I2C_XFER_REV; 692 i2c_tp->i2c_flags = I2C_WR; 693 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */ 694 i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F; 695 696 if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 697 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 698 cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write " 699 "DS1337 status register"); 700 mutex_exit(&todds1337_alarm_lock); 701 mutex_enter(&tod_lock); 702 return; 703 } 704 705 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 706 707 mutex_exit(&todds1337_alarm_lock); 708 mutex_enter(&tod_lock); 709 } 710 711 /* ARGSUSED */ 712 static uint_t 713 todds1337_set_watchdog_timer(uint_t timeoutval) 714 { 715 ASSERT(MUTEX_HELD(&tod_lock)); 716 return (0); 717 } 718 719 /* ARGSUSED */ 720 static uint_t 721 todds1337_clear_watchdog_timer(void) 722 { 723 ASSERT(MUTEX_HELD(&tod_lock)); 724 return (0); 725 } 726 727 /* ********************** Local functions ***************************** */ 728 729 static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1}; 730 static int 731 todds1337_read_rtc(struct rtc_t *rtc) 732 { 733 static ds1337_state_t *statep = NULL; 734 i2c_transfer_t *i2c_tp = NULL; 735 int i2c_cmd_status = I2C_FAILURE; 736 int counter = 4; 737 738 if (!todds1337_attach_done) { 739 return (todds1337_prom_getdate(rtc)); 740 } 741 742 statep = ddi_get_soft_state(ds1337_statep, instance); 743 if (statep == NULL) { 744 cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing"); 745 return (DDI_FAILURE); 746 } 747 748 mutex_enter(&todds1337_rd_lock); 749 750 /* 751 * Allocate 1 byte for write buffer and 7 bytes for read buffer to 752 * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year 753 */ 754 if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1, 755 7, I2C_SLEEP)) != I2C_SUCCESS) { 756 mutex_exit(&todds1337_rd_lock); 757 return (DDI_FAILURE); 758 } 759 760 do { 761 i2c_tp->i2c_version = I2C_XFER_REV; 762 i2c_tp->i2c_flags = I2C_WR_RD; 763 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */ 764 i2c_tp->i2c_wlen = 1; /* Write one byte address */ 765 i2c_tp->i2c_rlen = 7; /* Read 7 regs */ 766 767 if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 768 i2c_tp)) != I2C_SUCCESS) { 769 goto done; 770 } 771 /* for first read, need to get valid data */ 772 while (tod_read[0] == -1 && counter > 0) { 773 /* move data to static buffer */ 774 bcopy(i2c_tp->i2c_rbuf, tod_read, 7); 775 776 /* now read again */ 777 /* Start reading reg from 0x00 */ 778 i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; 779 i2c_tp->i2c_wlen = 1; /* Write one byte address */ 780 i2c_tp->i2c_rlen = 7; /* Read 7 regs */ 781 if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 782 i2c_tp)) != I2C_SUCCESS) { 783 goto done; 784 } 785 /* if they are not the same, then read again */ 786 if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) { 787 tod_read[0] = -1; 788 counter--; 789 } 790 } 791 792 } while (i2c_tp->i2c_rbuf[0] == 0x59 && 793 /* if seconds register is 0x59 (BCD), add data should match */ 794 bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 && 795 counter-- > 0); 796 797 if (counter < 0) 798 cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??"); 799 800 /* move data to static buffer */ 801 bcopy(i2c_tp->i2c_rbuf, tod_read, 7); 802 803 804 rtc->rtc_year = BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]); 805 rtc->rtc_mon = BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]); 806 rtc->rtc_dom = BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]); 807 rtc->rtc_dow = BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]); 808 rtc->rtc_hrs = BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]); 809 rtc->rtc_min = BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]); 810 rtc->rtc_sec = BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]); 811 812 done: 813 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 814 815 mutex_exit(&todds1337_rd_lock); 816 return (i2c_cmd_status); 817 } 818 819 820 static int 821 todds1337_write_rtc(struct rtc_t *rtc) 822 { 823 ds1337_state_t *statep = NULL; 824 i2c_transfer_t *i2c_tp = NULL; 825 int i2c_cmd_status = I2C_SUCCESS; 826 827 828 if (!todds1337_attach_done) { 829 return (todds1337_prom_setdate(rtc)); 830 } 831 832 statep = ddi_get_soft_state(ds1337_statep, instance); 833 if (statep == NULL) { 834 return (DDI_FAILURE); 835 } 836 837 if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl, 838 &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) { 839 return (i2c_cmd_status); 840 } 841 842 i2c_tp->i2c_version = I2C_XFER_REV; 843 i2c_tp->i2c_flags = I2C_WR; 844 i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; 845 i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec); 846 i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min); 847 i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs); 848 i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow); 849 i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom); 850 i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon); 851 i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year); 852 i2c_tp->i2c_wlen = 8; 853 854 if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 855 i2c_tp)) != I2C_SUCCESS) { 856 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 857 return (i2c_cmd_status); 858 } 859 860 tod_read[0] = -1; /* invalidate saved data from read routine */ 861 862 (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 863 864 return (i2c_cmd_status); 865 } 866 867 868 /*ARGSUSED*/ 869 static int 870 todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 871 void **result) 872 { 873 ds1337_state_t *softsp; 874 875 if (instance == -1) { 876 return (DDI_FAILURE); 877 } 878 879 switch (infocmd) { 880 case DDI_INFO_DEVT2DEVINFO: 881 if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) == 882 NULL) 883 return (DDI_FAILURE); 884 *result = (void *)softsp->dip; 885 return (DDI_SUCCESS); 886 case DDI_INFO_DEVT2INSTANCE: 887 *result = (void *)(uintptr_t)instance; 888 return (DDI_SUCCESS); 889 default: 890 return (DDI_FAILURE); 891 } 892 } 893 894 /* 895 * Finds the device node with device_type "rtc" and opens it to 896 * execute the get-time method 897 */ 898 static int 899 todds1337_setup_prom() 900 { 901 pnode_t todnode; 902 char tod1337_devpath[MAXNAMELEN]; 903 904 if ((todnode = prom_findnode_bydevtype(prom_rootnode(), 905 DS1337_DEVICE_TYPE)) == OBP_NONODE) 906 return (DDI_FAILURE); 907 908 /* 909 * We now have the phandle of the rtc node, we need to open the 910 * node and get the ihandle 911 */ 912 if (prom_phandle_to_path(todnode, tod1337_devpath, 913 sizeof (tod1337_devpath)) < 0) { 914 cmn_err(CE_WARN, "prom_phandle_to_path failed"); 915 return (DDI_FAILURE); 916 } 917 918 /* 919 * Now open the node and store it's ihandle 920 */ 921 if ((todds1337_ihandle = prom_open(tod1337_devpath)) == NULL) { 922 cmn_err(CE_WARN, "prom_open failed"); 923 return (DDI_FAILURE); 924 } 925 926 return (DDI_SUCCESS); 927 } 928 929 /* 930 * Closes the prom interface 931 */ 932 static void 933 todds1337_rele_prom() 934 { 935 (void) prom_close(todds1337_ihandle); 936 } 937 938 /* 939 * Read the date using "get-time" method in rtc node 940 * PROM returns 1969-1999 when reading 69-99 and 941 * 2000-2068 when reading 00-68 942 */ 943 static int 944 todds1337_prom_getdate(struct rtc_t *rtc) 945 { 946 int year; 947 cell_t ci[12]; 948 949 ci[0] = p1275_ptr2cell("call-method"); /* Service name */ 950 ci[1] = 2; /* # of arguments */ 951 ci[2] = 7; /* # of result cells */ 952 ci[3] = p1275_ptr2cell("get-time"); 953 ci[4] = p1275_ihandle2cell(todds1337_ihandle); 954 955 promif_preprom(); 956 (void) p1275_cif_handler(&ci); 957 promif_postprom(); 958 959 year = p1275_cell2int(ci[6]); 960 rtc->rtc_mon = p1275_cell2int(ci[7]); 961 rtc->rtc_dom = p1275_cell2int(ci[8]); 962 rtc->rtc_dow = 0; 963 rtc->rtc_hrs = p1275_cell2int(ci[9]); 964 rtc->rtc_min = p1275_cell2int(ci[10]); 965 rtc->rtc_sec = p1275_cell2int(ci[11]); 966 if (year >= 2000) 967 year -= 2000; 968 else 969 year -= 1900; 970 rtc->rtc_year = year; 971 972 return (DDI_SUCCESS); 973 } 974 975 /* 976 * Read the date using "set-time" method in rtc node 977 * For values 00 - 68, write 2000-2068, and for 69-99, 978 * write 1969-1999 979 */ 980 static int 981 todds1337_prom_setdate(struct rtc_t *rtc) 982 { 983 int year; 984 cell_t ci[12]; 985 986 year = rtc->rtc_year; 987 988 if ((year < 0) || (year > 99)) 989 return (DDI_FAILURE); 990 991 if (year <= 68) 992 year = rtc->rtc_year + 2000; 993 else 994 year = rtc->rtc_year + 1900; 995 996 ci[0] = p1275_ptr2cell("call-method"); /* Service name */ 997 ci[1] = 8; /* # of arguments */ 998 ci[2] = 0; /* # of result cells */ 999 ci[3] = p1275_ptr2cell("set-time"); 1000 ci[4] = p1275_ihandle2cell(todds1337_ihandle); 1001 ci[5] = p1275_int2cell(year); 1002 ci[6] = p1275_int2cell(rtc->rtc_mon); 1003 ci[7] = p1275_int2cell(rtc->rtc_dom); 1004 ci[8] = p1275_int2cell(rtc->rtc_hrs); 1005 ci[9] = p1275_int2cell(rtc->rtc_min); 1006 ci[10] = p1275_int2cell(rtc->rtc_sec); 1007 1008 promif_preprom(); 1009 (void) p1275_cif_handler(&ci); 1010 promif_postprom(); 1011 1012 return (DDI_SUCCESS); 1013 } 1014