17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 570e456aaSrameshc * Common Development and Distribution License (the "License"). 670e456aaSrameshc * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 2270e456aaSrameshc * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include <sys/types.h> 297c478bd9Sstevel@tonic-gate #include <sys/conf.h> 307c478bd9Sstevel@tonic-gate #include <sys/devops.h> 317c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 327c478bd9Sstevel@tonic-gate #include <sys/open.h> 337c478bd9Sstevel@tonic-gate #include <sys/file.h> 347c478bd9Sstevel@tonic-gate #include <sys/note.h> 357c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 367c478bd9Sstevel@tonic-gate #include <sys/sunddi.h> 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 397c478bd9Sstevel@tonic-gate #include <sys/stat.h> 407c478bd9Sstevel@tonic-gate #include <sys/clock.h> 417c478bd9Sstevel@tonic-gate #include <sys/reboot.h> 427c478bd9Sstevel@tonic-gate #include <sys/machsystm.h> 437c478bd9Sstevel@tonic-gate #include <sys/poll.h> 447c478bd9Sstevel@tonic-gate #include <sys/pbio.h> 457c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate /* Added for prom interface */ 487c478bd9Sstevel@tonic-gate #include <sys/promif.h> 497c478bd9Sstevel@tonic-gate #include <sys/promimpl.h> 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h> 527c478bd9Sstevel@tonic-gate #include <sys/todds1337.h> 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate #define DS1337_DEVICE_TYPE "rtc" 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate /* 577c478bd9Sstevel@tonic-gate * Driver entry routines 587c478bd9Sstevel@tonic-gate */ 597c478bd9Sstevel@tonic-gate static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t); 607c478bd9Sstevel@tonic-gate static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t); 617c478bd9Sstevel@tonic-gate static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * tod_ops entry routines 657c478bd9Sstevel@tonic-gate */ 667c478bd9Sstevel@tonic-gate static timestruc_t todds1337_get(void); 677c478bd9Sstevel@tonic-gate static void todds1337_set(timestruc_t); 687c478bd9Sstevel@tonic-gate static uint_t todds1337_set_watchdog_timer(uint_t); 697c478bd9Sstevel@tonic-gate static uint_t todds1337_clear_watchdog_timer(void); 707c478bd9Sstevel@tonic-gate static void todds1337_set_power_alarm(timestruc_t); 717c478bd9Sstevel@tonic-gate static void todds1337_clear_power_alarm(void); 727c478bd9Sstevel@tonic-gate static int todds1337_setup_prom(); 737c478bd9Sstevel@tonic-gate static void todds1337_rele_prom(); 747c478bd9Sstevel@tonic-gate static int todds1337_prom_getdate(struct rtc_t *rtc); 757c478bd9Sstevel@tonic-gate static int todds1337_prom_setdate(struct rtc_t *rtc); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate /* 787c478bd9Sstevel@tonic-gate * Local functions 797c478bd9Sstevel@tonic-gate */ 807c478bd9Sstevel@tonic-gate static int todds1337_read_rtc(struct rtc_t *); 817c478bd9Sstevel@tonic-gate static int todds1337_write_rtc(struct rtc_t *); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* Anchor for soft state structure */ 847c478bd9Sstevel@tonic-gate static void *ds1337_statep; 857c478bd9Sstevel@tonic-gate static int instance = -1; 867c478bd9Sstevel@tonic-gate static int todds1337_attach_done = 0; 877c478bd9Sstevel@tonic-gate static kmutex_t todds1337_rd_lock; 887c478bd9Sstevel@tonic-gate static kmutex_t todds1337_alarm_lock; 897c478bd9Sstevel@tonic-gate static ihandle_t todds1337_ihandle = 0; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate /* one second time out */ 927c478bd9Sstevel@tonic-gate #define I2C_CYCLIC_TIMEOUT 1000000000 937c478bd9Sstevel@tonic-gate uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT; 947c478bd9Sstevel@tonic-gate static int sync_clock_once = 1; 957c478bd9Sstevel@tonic-gate static struct rtc_t soft_rtc; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * cp_ops structure 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate static struct cb_ops ds1337_cbops = { 1017c478bd9Sstevel@tonic-gate nodev, /* open */ 1027c478bd9Sstevel@tonic-gate nodev, /* close */ 1037c478bd9Sstevel@tonic-gate nodev, /* strategy */ 1047c478bd9Sstevel@tonic-gate nodev, /* print */ 1057c478bd9Sstevel@tonic-gate nodev, /* dump */ 1067c478bd9Sstevel@tonic-gate nodev, /* read */ 1077c478bd9Sstevel@tonic-gate nodev, /* write */ 1087c478bd9Sstevel@tonic-gate nodev, /* ioctl */ 1097c478bd9Sstevel@tonic-gate nodev, /* devmap */ 1107c478bd9Sstevel@tonic-gate nodev, /* mmap */ 1117c478bd9Sstevel@tonic-gate nodev, /* segmap */ 1127c478bd9Sstevel@tonic-gate NULL, /* poll */ 1137c478bd9Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 1147c478bd9Sstevel@tonic-gate NULL, /* streamtab */ 1157c478bd9Sstevel@tonic-gate D_NEW | D_MP, /* Driver compatibility flag */ 1167c478bd9Sstevel@tonic-gate CB_REV, /* rev */ 1177c478bd9Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 1187c478bd9Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 1197c478bd9Sstevel@tonic-gate }; 1207c478bd9Sstevel@tonic-gate 1217c478bd9Sstevel@tonic-gate /* 1227c478bd9Sstevel@tonic-gate * dev_ops structure 1237c478bd9Sstevel@tonic-gate */ 1247c478bd9Sstevel@tonic-gate static struct dev_ops ds1337_ops = { 1257c478bd9Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 1267c478bd9Sstevel@tonic-gate 0, /* refcnt - reference cnt always set to 0 */ 1277c478bd9Sstevel@tonic-gate todds1337_getinfo, /* getinfo - Maybe requred */ 1287c478bd9Sstevel@tonic-gate nulldev, /* identify */ 1297c478bd9Sstevel@tonic-gate nulldev, /* probe */ 1307c478bd9Sstevel@tonic-gate todds1337_attach, /* attach */ 1317c478bd9Sstevel@tonic-gate todds1337_detach, /* detach */ 1327c478bd9Sstevel@tonic-gate nodev, /* reset */ 1337c478bd9Sstevel@tonic-gate &ds1337_cbops, /* cb_ops - ds1337 does not need this(?) */ 1347c478bd9Sstevel@tonic-gate NULL 1357c478bd9Sstevel@tonic-gate }; 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate static struct modldrv todds1337_modldrv = { 1387c478bd9Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 1397c478bd9Sstevel@tonic-gate "tod driver for DS1337 %I%", /* Name of the module. */ 1407c478bd9Sstevel@tonic-gate &ds1337_ops, /* Pointer to dev_ops */ 1417c478bd9Sstevel@tonic-gate }; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* 1447c478bd9Sstevel@tonic-gate * Module linkage structure 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate static struct modlinkage todds1337_modlinkage = { 1477c478bd9Sstevel@tonic-gate MODREV_1, 1487c478bd9Sstevel@tonic-gate &todds1337_modldrv, 1497c478bd9Sstevel@tonic-gate 0 1507c478bd9Sstevel@tonic-gate }; 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate int 1537c478bd9Sstevel@tonic-gate _init(void) 1547c478bd9Sstevel@tonic-gate { 1557c478bd9Sstevel@tonic-gate int error; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate if (strcmp(tod_module_name, "todds1337") == 0) { 1587c478bd9Sstevel@tonic-gate if ((error = ddi_soft_state_init(&ds1337_statep, 1597c478bd9Sstevel@tonic-gate sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) { 1607c478bd9Sstevel@tonic-gate return (error); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate tod_ops.tod_get = todds1337_get; 1647c478bd9Sstevel@tonic-gate tod_ops.tod_set = todds1337_set; 1657c478bd9Sstevel@tonic-gate tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer; 1667c478bd9Sstevel@tonic-gate tod_ops.tod_clear_watchdog_timer = 1677c478bd9Sstevel@tonic-gate todds1337_clear_watchdog_timer; 1687c478bd9Sstevel@tonic-gate tod_ops.tod_set_power_alarm = todds1337_set_power_alarm; 1697c478bd9Sstevel@tonic-gate tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm; 1707c478bd9Sstevel@tonic-gate } 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate (void) todds1337_setup_prom(); 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * Install the module 1767c478bd9Sstevel@tonic-gate */ 1777c478bd9Sstevel@tonic-gate if ((error = mod_install(&todds1337_modlinkage)) != 0) { 17870e456aaSrameshc if (strcmp(tod_module_name, "todds1337") == 0) { 1797c478bd9Sstevel@tonic-gate ddi_soft_state_fini(&ds1337_statep); 18070e456aaSrameshc } 18170e456aaSrameshc todds1337_rele_prom(); 1827c478bd9Sstevel@tonic-gate return (error); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL); 1857c478bd9Sstevel@tonic-gate mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL); 1867c478bd9Sstevel@tonic-gate 1877c478bd9Sstevel@tonic-gate return (0); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate int 1917c478bd9Sstevel@tonic-gate _fini(void) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate int error = 0; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate if (strcmp(tod_module_name, "todds1337") == 0) { 1967c478bd9Sstevel@tonic-gate error = EBUSY; 1977c478bd9Sstevel@tonic-gate } else { 1987c478bd9Sstevel@tonic-gate if ((error = mod_remove(&todds1337_modlinkage)) == 0) { 1997c478bd9Sstevel@tonic-gate mutex_destroy(&todds1337_rd_lock); 2007c478bd9Sstevel@tonic-gate mutex_destroy(&todds1337_alarm_lock); 2017c478bd9Sstevel@tonic-gate todds1337_rele_prom(); 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate return (error); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate int 2097c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop) 2107c478bd9Sstevel@tonic-gate { 2117c478bd9Sstevel@tonic-gate return (mod_info(&todds1337_modlinkage, modinfop)); 2127c478bd9Sstevel@tonic-gate } 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate /* 2157c478bd9Sstevel@tonic-gate * cyclical call to get tod. 2167c478bd9Sstevel@tonic-gate */ 2177c478bd9Sstevel@tonic-gate static void 2187c478bd9Sstevel@tonic-gate todds1337_cyclic(void *arg) 2197c478bd9Sstevel@tonic-gate { 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate (void) todds1337_read_rtc((struct rtc_t *)arg); 2227c478bd9Sstevel@tonic-gate 2237c478bd9Sstevel@tonic-gate } 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * register ds1337 client device with i2c services, and 2277c478bd9Sstevel@tonic-gate * allocate & initialize soft state structure. 2287c478bd9Sstevel@tonic-gate */ 2297c478bd9Sstevel@tonic-gate static int 2307c478bd9Sstevel@tonic-gate todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2317c478bd9Sstevel@tonic-gate { 2327c478bd9Sstevel@tonic-gate static ds1337_state_t *statep = NULL; 2337c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 2347c478bd9Sstevel@tonic-gate uint8_t tempVal = (uint8_t)0; 2357c478bd9Sstevel@tonic-gate switch (cmd) { 2367c478bd9Sstevel@tonic-gate case DDI_ATTACH: 2377c478bd9Sstevel@tonic-gate break; 2387c478bd9Sstevel@tonic-gate case DDI_RESUME: 2397c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 2407c478bd9Sstevel@tonic-gate default: 2417c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (instance != -1) { 2457c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: wrong instance"); 2467c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate instance = ddi_get_instance(dip); 2507c478bd9Sstevel@tonic-gate 2517c478bd9Sstevel@tonic-gate /* 2527c478bd9Sstevel@tonic-gate * Allocate soft state structure 2537c478bd9Sstevel@tonic-gate */ 2547c478bd9Sstevel@tonic-gate if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) { 2557c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft " 2567c478bd9Sstevel@tonic-gate "state"); 2577c478bd9Sstevel@tonic-gate instance = -1; 2587c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2597c478bd9Sstevel@tonic-gate } 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 2627c478bd9Sstevel@tonic-gate if (statep == NULL) { 2637c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft " 2647c478bd9Sstevel@tonic-gate "state"); 2657c478bd9Sstevel@tonic-gate instance = -1; 2667c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate statep->dip = dip; 2707c478bd9Sstevel@tonic-gate 2717c478bd9Sstevel@tonic-gate if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) { 2727c478bd9Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 2737c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot register i2c " 2747c478bd9Sstevel@tonic-gate "client"); 2757c478bd9Sstevel@tonic-gate instance = -1; 2767c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2777c478bd9Sstevel@tonic-gate } 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate /* check and initialize the oscillator */ 2807c478bd9Sstevel@tonic-gate 2817c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 2827c478bd9Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 2837c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 2847c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 2857c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */ 2867c478bd9Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; 2877c478bd9Sstevel@tonic-gate i2c_tp->i2c_rlen = 1; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 2907c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 2917c478bd9Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl); 2927c478bd9Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 2937c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 " 2947c478bd9Sstevel@tonic-gate "status register"); 2957c478bd9Sstevel@tonic-gate instance = -1; 2967c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate tempVal = i2c_tp->i2c_rbuf[0]; 3007c478bd9Sstevel@tonic-gate 3017c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate /* 3047c478bd9Sstevel@tonic-gate * Check Oscillator and initialize chip if OBP failed to do it 3057c478bd9Sstevel@tonic-gate */ 3067c478bd9Sstevel@tonic-gate 3077c478bd9Sstevel@tonic-gate if (tempVal & RTC_CTL_EOSC) { 3087c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 3097c478bd9Sstevel@tonic-gate 2, 0, I2C_SLEEP); 3107c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 3117c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 3127c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */ 3137c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 | 3147c478bd9Sstevel@tonic-gate RTC_CTL_INTCN); 3157c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) 3167c478bd9Sstevel@tonic-gate != I2C_SUCCESS) { 3177c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, 3187c478bd9Sstevel@tonic-gate i2c_tp); 3197c478bd9Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl); 3207c478bd9Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 3217c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to write " 3227c478bd9Sstevel@tonic-gate "DS1337 control register"); 3237c478bd9Sstevel@tonic-gate instance = -1; 3247c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3257c478bd9Sstevel@tonic-gate } 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate /* 3307c478bd9Sstevel@tonic-gate * Now reset the OSF flag in the Status register 3317c478bd9Sstevel@tonic-gate */ 3327c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 3337c478bd9Sstevel@tonic-gate 2, 0, I2C_SLEEP); 3347c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 3357c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 3367c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = RTC_STATUS; 3377c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = (uchar_t)0; 3387c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) 3397c478bd9Sstevel@tonic-gate != I2C_SUCCESS) { 3407c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, 3417c478bd9Sstevel@tonic-gate i2c_tp); 3427c478bd9Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl); 3437c478bd9Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 3447c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to write " 3457c478bd9Sstevel@tonic-gate "DS1337 status register"); 3467c478bd9Sstevel@tonic-gate instance = -1; 3477c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3487c478bd9Sstevel@tonic-gate } 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 3517c478bd9Sstevel@tonic-gate } 3527c478bd9Sstevel@tonic-gate 353*dd4eeefdSeota /* 354*dd4eeefdSeota * Create a periodical handler to read TOD. 355*dd4eeefdSeota */ 356*dd4eeefdSeota ASSERT(statep->cycid == NULL); 357*dd4eeefdSeota statep->cycid = ddi_periodic_add(todds1337_cyclic, &soft_rtc, 358*dd4eeefdSeota i2c_cyclic_timeout, DDI_IPL_1); 3597c478bd9Sstevel@tonic-gate statep->state = TOD_ATTACHED; 3607c478bd9Sstevel@tonic-gate todds1337_attach_done = 1; 3617c478bd9Sstevel@tonic-gate ddi_report_dev(dip); 3627c478bd9Sstevel@tonic-gate 3637c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3647c478bd9Sstevel@tonic-gate } 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 3677c478bd9Sstevel@tonic-gate static int 3687c478bd9Sstevel@tonic-gate todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3697c478bd9Sstevel@tonic-gate { 3707c478bd9Sstevel@tonic-gate /* 3717c478bd9Sstevel@tonic-gate * Once attached, do not allow detach because the system constantly 3727c478bd9Sstevel@tonic-gate * calling todds1337_get() to get the time. If the driver is detached 3737c478bd9Sstevel@tonic-gate * and the system try to get the time, the system will have memory 3747c478bd9Sstevel@tonic-gate * problem. 3757c478bd9Sstevel@tonic-gate * 3767c478bd9Sstevel@tonic-gate */ 3777c478bd9Sstevel@tonic-gate switch (cmd) { 3787c478bd9Sstevel@tonic-gate case DDI_SUSPEND: 3797c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 3807c478bd9Sstevel@tonic-gate default: 3817c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 3827c478bd9Sstevel@tonic-gate } 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */ 3867c478bd9Sstevel@tonic-gate 3877c478bd9Sstevel@tonic-gate /* 3887c478bd9Sstevel@tonic-gate * Read the current time from the DS1337 chip and convert to UNIX form. 3897c478bd9Sstevel@tonic-gate * Should be called with tod_clock held. 3907c478bd9Sstevel@tonic-gate */ 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate static timestruc_t 3937c478bd9Sstevel@tonic-gate todds1337_get(void) 3947c478bd9Sstevel@tonic-gate { 3957c478bd9Sstevel@tonic-gate timestruc_t ts; 3967c478bd9Sstevel@tonic-gate todinfo_t tod; 3977c478bd9Sstevel@tonic-gate struct rtc_t rtc; 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 4007c478bd9Sstevel@tonic-gate 4017c478bd9Sstevel@tonic-gate if (sync_clock_once) { 4027c478bd9Sstevel@tonic-gate (void) todds1337_read_rtc(&soft_rtc); 4037c478bd9Sstevel@tonic-gate sync_clock_once = 0; 4047c478bd9Sstevel@tonic-gate } else { 4057c478bd9Sstevel@tonic-gate tod_fault_reset(); 4067c478bd9Sstevel@tonic-gate return (hrestime); 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate bcopy(&soft_rtc, &rtc, sizeof (rtc)); 4107c478bd9Sstevel@tonic-gate 4117c478bd9Sstevel@tonic-gate /* 4127c478bd9Sstevel@tonic-gate * 00 - 68 = 2000 thru 2068 4137c478bd9Sstevel@tonic-gate * 69-99 = 1969 thru 1999 4147c478bd9Sstevel@tonic-gate */ 4157c478bd9Sstevel@tonic-gate tod.tod_year = rtc.rtc_year; 4167c478bd9Sstevel@tonic-gate if (rtc.rtc_year <= 68) 4177c478bd9Sstevel@tonic-gate tod.tod_year += 100; 4187c478bd9Sstevel@tonic-gate tod.tod_month = rtc.rtc_mon; 4197c478bd9Sstevel@tonic-gate tod.tod_day = rtc.rtc_dom; 4207c478bd9Sstevel@tonic-gate tod.tod_dow = rtc.rtc_dow; 4217c478bd9Sstevel@tonic-gate tod.tod_hour = rtc.rtc_hrs; 4227c478bd9Sstevel@tonic-gate tod.tod_min = rtc.rtc_min; 4237c478bd9Sstevel@tonic-gate tod.tod_sec = rtc.rtc_sec; 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate ts.tv_sec = tod_to_utc(tod); 4267c478bd9Sstevel@tonic-gate ts.tv_nsec = 0; 4277c478bd9Sstevel@tonic-gate return (ts); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate 4307c478bd9Sstevel@tonic-gate /* 4317c478bd9Sstevel@tonic-gate * Program DS1337 with the specified time. 4327c478bd9Sstevel@tonic-gate * Must be called with tod_lock held. The TOD 4337c478bd9Sstevel@tonic-gate * chip supports date from 1969-2068 only. We must 4347c478bd9Sstevel@tonic-gate * reject requests to set date below 1969. 4357c478bd9Sstevel@tonic-gate */ 4367c478bd9Sstevel@tonic-gate static void 4377c478bd9Sstevel@tonic-gate todds1337_set(timestruc_t ts) 4387c478bd9Sstevel@tonic-gate { 4397c478bd9Sstevel@tonic-gate struct rtc_t rtc; 4407c478bd9Sstevel@tonic-gate todinfo_t tod = utc_to_tod(ts.tv_sec); 4417c478bd9Sstevel@tonic-gate int year; 4427c478bd9Sstevel@tonic-gate 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate /* 4477c478bd9Sstevel@tonic-gate * Year is base 1900, valid year range 1969-2068 4487c478bd9Sstevel@tonic-gate */ 4497c478bd9Sstevel@tonic-gate if ((tod.tod_year < 69) || (tod.tod_year > 168)) 4507c478bd9Sstevel@tonic-gate return; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate year = tod.tod_year; 4537c478bd9Sstevel@tonic-gate if (year >= 100) 4547c478bd9Sstevel@tonic-gate year -= 100; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate rtc.rtc_year = (uint8_t)year; 4577c478bd9Sstevel@tonic-gate rtc.rtc_mon = (uint8_t)tod.tod_month; 4587c478bd9Sstevel@tonic-gate rtc.rtc_dom = (uint8_t)tod.tod_day; 4597c478bd9Sstevel@tonic-gate rtc.rtc_dow = (uint8_t)tod.tod_dow; 4607c478bd9Sstevel@tonic-gate rtc.rtc_hrs = (uint8_t)tod.tod_hour; 4617c478bd9Sstevel@tonic-gate rtc.rtc_min = (uint8_t)tod.tod_min; 4627c478bd9Sstevel@tonic-gate rtc.rtc_sec = (uint8_t)tod.tod_sec; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate (void) todds1337_write_rtc(&rtc); 4657c478bd9Sstevel@tonic-gate } 4667c478bd9Sstevel@tonic-gate 4677c478bd9Sstevel@tonic-gate /* 4687c478bd9Sstevel@tonic-gate * Program ds1337 registers for alarm to go off at the specified time. 4697c478bd9Sstevel@tonic-gate * Alarm #1 is used (Alarm #2 stays idle) 4707c478bd9Sstevel@tonic-gate */ 4717c478bd9Sstevel@tonic-gate /* ARGSUSED */ 4727c478bd9Sstevel@tonic-gate static void 4737c478bd9Sstevel@tonic-gate todds1337_set_power_alarm(timestruc_t ts) 4747c478bd9Sstevel@tonic-gate { 4757c478bd9Sstevel@tonic-gate todinfo_t tod; 4767c478bd9Sstevel@tonic-gate ds1337_state_t *statep = NULL; 4777c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 4787c478bd9Sstevel@tonic-gate uint8_t tmpval; 4797c478bd9Sstevel@tonic-gate 4807c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate if (!todds1337_attach_done) { 4837c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: driver not attached"); 4847c478bd9Sstevel@tonic-gate return; 4857c478bd9Sstevel@tonic-gate } 4867c478bd9Sstevel@tonic-gate 4877c478bd9Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 4887c478bd9Sstevel@tonic-gate if (statep == NULL) { 4897c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed"); 4907c478bd9Sstevel@tonic-gate return; 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate tod = utc_to_tod(ts.tv_sec); 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate /* 4967c478bd9Sstevel@tonic-gate * i2c_transfe() may block; to avoid locking clock() which 4977c478bd9Sstevel@tonic-gate * is running in interrupt context at PIL10 we temporarely exit 4987c478bd9Sstevel@tonic-gate * the tod_mutex and enter alarm lock. Time/date and alarm hardware 4997c478bd9Sstevel@tonic-gate * have non-interlsecting registers, it is safe to use different locks. 5007c478bd9Sstevel@tonic-gate */ 5017c478bd9Sstevel@tonic-gate mutex_exit(&tod_lock); 5027c478bd9Sstevel@tonic-gate mutex_enter(&todds1337_alarm_lock); 5037c478bd9Sstevel@tonic-gate 5047c478bd9Sstevel@tonic-gate /* 5057c478bd9Sstevel@tonic-gate * Disable Power Alarm (A1IE) 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5087c478bd9Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 5097c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5107c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 5117c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */ 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5147c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5157c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read " 5167c478bd9Sstevel@tonic-gate "DS1337 control register"); 5177c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5187c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 5197c478bd9Sstevel@tonic-gate return; 5207c478bd9Sstevel@tonic-gate } 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0]; 5237c478bd9Sstevel@tonic-gate 5247c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5257c478bd9Sstevel@tonic-gate 5267c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5277c478bd9Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 5287c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5297c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 5307c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 5317c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE; 5327c478bd9Sstevel@tonic-gate 5337c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5347c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5357c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write " 5367c478bd9Sstevel@tonic-gate "DS1337control register"); 5377c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5387c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 5397c478bd9Sstevel@tonic-gate return; 5407c478bd9Sstevel@tonic-gate } 5417c478bd9Sstevel@tonic-gate 5427c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate /* 5467c478bd9Sstevel@tonic-gate * Write Alarm #1 registers 5477c478bd9Sstevel@tonic-gate */ 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5507c478bd9Sstevel@tonic-gate &i2c_tp, 5, 0, I2C_SLEEP); 5517c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5527c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 5537c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */ 5547c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec); 5557c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min); 5567c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour); 5577c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day); 5587c478bd9Sstevel@tonic-gate 5597c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5607c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5617c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write " 5627c478bd9Sstevel@tonic-gate "DS1337 alarm registers"); 5637c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5647c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 5657c478bd9Sstevel@tonic-gate return; 5667c478bd9Sstevel@tonic-gate } 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5697c478bd9Sstevel@tonic-gate 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * Enable Power Alarm 5737c478bd9Sstevel@tonic-gate */ 5747c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5757c478bd9Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 5767c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5777c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 5787c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 5797c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE; 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5827c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5837c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable " 5847c478bd9Sstevel@tonic-gate "DS1337 alarm"); 5857c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5867c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 5877c478bd9Sstevel@tonic-gate return; 5887c478bd9Sstevel@tonic-gate } 5897c478bd9Sstevel@tonic-gate 5907c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5917c478bd9Sstevel@tonic-gate 5927c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5937c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 5947c478bd9Sstevel@tonic-gate } 5957c478bd9Sstevel@tonic-gate 5967c478bd9Sstevel@tonic-gate /* ARGSUSED */ 5977c478bd9Sstevel@tonic-gate static void 5987c478bd9Sstevel@tonic-gate todds1337_clear_power_alarm(void) 5997c478bd9Sstevel@tonic-gate { 6007c478bd9Sstevel@tonic-gate ds1337_state_t *statep = NULL; 6017c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 6027c478bd9Sstevel@tonic-gate uint8_t tmpval; 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate if (!todds1337_attach_done) { 6077c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: driver was not attached"); 6087c478bd9Sstevel@tonic-gate return; 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 6127c478bd9Sstevel@tonic-gate if (statep == NULL) { 6137c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed"); 6147c478bd9Sstevel@tonic-gate return; 6157c478bd9Sstevel@tonic-gate } 6167c478bd9Sstevel@tonic-gate 6177c478bd9Sstevel@tonic-gate /* 6187c478bd9Sstevel@tonic-gate * i2c_transfe() may block; to avoid locking clock() which 6197c478bd9Sstevel@tonic-gate * is running in interrupt context at PIL10 we temporarely exit 6207c478bd9Sstevel@tonic-gate * the tod_mutex and enter alarm lock. Time/date and alarm hardware 6217c478bd9Sstevel@tonic-gate * have non-interlsecting registers, it is safe to use different locks. 6227c478bd9Sstevel@tonic-gate */ 6237c478bd9Sstevel@tonic-gate mutex_exit(&tod_lock); 6247c478bd9Sstevel@tonic-gate mutex_enter(&todds1337_alarm_lock); 6257c478bd9Sstevel@tonic-gate 6267c478bd9Sstevel@tonic-gate /* 6277c478bd9Sstevel@tonic-gate * Disable Alarm #1 Interrupt 6287c478bd9Sstevel@tonic-gate */ 6297c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6307c478bd9Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 6317c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6327c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 6337c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */ 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6367c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6377c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read " 6387c478bd9Sstevel@tonic-gate "DS1337 control register"); 6397c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 6407c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 6417c478bd9Sstevel@tonic-gate return; 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0]; 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6477c478bd9Sstevel@tonic-gate 6487c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6497c478bd9Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 6507c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6517c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 6527c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 6537c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE; 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6567c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6577c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write " 6587c478bd9Sstevel@tonic-gate "DS1337 control register"); 6597c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 6607c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 6617c478bd9Sstevel@tonic-gate return; 6627c478bd9Sstevel@tonic-gate } 6637c478bd9Sstevel@tonic-gate 6647c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6657c478bd9Sstevel@tonic-gate 6667c478bd9Sstevel@tonic-gate /* 6677c478bd9Sstevel@tonic-gate * Reset Alarm #1 Flag 6687c478bd9Sstevel@tonic-gate */ 6697c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6707c478bd9Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 6717c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6727c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 6737c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */ 6747c478bd9Sstevel@tonic-gate 6757c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6767c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6777c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read " 6787c478bd9Sstevel@tonic-gate "DS1337 status register"); 6797c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 6807c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 6817c478bd9Sstevel@tonic-gate return; 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0]; 6857c478bd9Sstevel@tonic-gate 6867c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6897c478bd9Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 6907c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6917c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 6927c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */ 6937c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F; 6947c478bd9Sstevel@tonic-gate 6957c478bd9Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6967c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6977c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write " 6987c478bd9Sstevel@tonic-gate "DS1337 status register"); 6997c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 7007c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 7017c478bd9Sstevel@tonic-gate return; 7027c478bd9Sstevel@tonic-gate } 7037c478bd9Sstevel@tonic-gate 7047c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 7077c478bd9Sstevel@tonic-gate mutex_enter(&tod_lock); 7087c478bd9Sstevel@tonic-gate } 7097c478bd9Sstevel@tonic-gate 7107c478bd9Sstevel@tonic-gate /* ARGSUSED */ 7117c478bd9Sstevel@tonic-gate static uint_t 7127c478bd9Sstevel@tonic-gate todds1337_set_watchdog_timer(uint_t timeoutval) 7137c478bd9Sstevel@tonic-gate { 7147c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 7157c478bd9Sstevel@tonic-gate return (0); 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate /* ARGSUSED */ 7197c478bd9Sstevel@tonic-gate static uint_t 7207c478bd9Sstevel@tonic-gate todds1337_clear_watchdog_timer(void) 7217c478bd9Sstevel@tonic-gate { 7227c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 7237c478bd9Sstevel@tonic-gate return (0); 7247c478bd9Sstevel@tonic-gate } 7257c478bd9Sstevel@tonic-gate 7267c478bd9Sstevel@tonic-gate /* ********************** Local functions ***************************** */ 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1}; 7297c478bd9Sstevel@tonic-gate static int 7307c478bd9Sstevel@tonic-gate todds1337_read_rtc(struct rtc_t *rtc) 7317c478bd9Sstevel@tonic-gate { 7327c478bd9Sstevel@tonic-gate static ds1337_state_t *statep = NULL; 7337c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 7347c478bd9Sstevel@tonic-gate int i2c_cmd_status = I2C_FAILURE; 7357c478bd9Sstevel@tonic-gate int counter = 4; 7367c478bd9Sstevel@tonic-gate 7377c478bd9Sstevel@tonic-gate if (!todds1337_attach_done) { 7387c478bd9Sstevel@tonic-gate return (todds1337_prom_getdate(rtc)); 7397c478bd9Sstevel@tonic-gate } 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 7427c478bd9Sstevel@tonic-gate if (statep == NULL) { 7437c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing"); 7447c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 7457c478bd9Sstevel@tonic-gate } 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate mutex_enter(&todds1337_rd_lock); 7487c478bd9Sstevel@tonic-gate 7497c478bd9Sstevel@tonic-gate /* 7507c478bd9Sstevel@tonic-gate * Allocate 1 byte for write buffer and 7 bytes for read buffer to 7517c478bd9Sstevel@tonic-gate * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year 7527c478bd9Sstevel@tonic-gate */ 7537c478bd9Sstevel@tonic-gate if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1, 7547c478bd9Sstevel@tonic-gate 7, I2C_SLEEP)) != I2C_SUCCESS) { 7557c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_rd_lock); 7567c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 7577c478bd9Sstevel@tonic-gate } 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate do { 7607c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 7617c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 7627c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */ 7637c478bd9Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; /* Write one byte address */ 7647c478bd9Sstevel@tonic-gate i2c_tp->i2c_rlen = 7; /* Read 7 regs */ 7657c478bd9Sstevel@tonic-gate 7667c478bd9Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 7677c478bd9Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) { 7687c478bd9Sstevel@tonic-gate goto done; 7697c478bd9Sstevel@tonic-gate } 7707c478bd9Sstevel@tonic-gate /* for first read, need to get valid data */ 7717c478bd9Sstevel@tonic-gate while (tod_read[0] == -1 && counter > 0) { 7727c478bd9Sstevel@tonic-gate /* move data to static buffer */ 7737c478bd9Sstevel@tonic-gate bcopy(i2c_tp->i2c_rbuf, tod_read, 7); 7747c478bd9Sstevel@tonic-gate 7757c478bd9Sstevel@tonic-gate /* now read again */ 7767c478bd9Sstevel@tonic-gate /* Start reading reg from 0x00 */ 7777c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; 7787c478bd9Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; /* Write one byte address */ 7797c478bd9Sstevel@tonic-gate i2c_tp->i2c_rlen = 7; /* Read 7 regs */ 7807c478bd9Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 7817c478bd9Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) { 7827c478bd9Sstevel@tonic-gate goto done; 7837c478bd9Sstevel@tonic-gate } 7847c478bd9Sstevel@tonic-gate /* if they are not the same, then read again */ 7857c478bd9Sstevel@tonic-gate if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) { 7867c478bd9Sstevel@tonic-gate tod_read[0] = -1; 7877c478bd9Sstevel@tonic-gate counter--; 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate } 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate } while (i2c_tp->i2c_rbuf[0] == 0x59 && 7927c478bd9Sstevel@tonic-gate /* if seconds register is 0x59 (BCD), add data should match */ 7937c478bd9Sstevel@tonic-gate bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 && 7947c478bd9Sstevel@tonic-gate counter-- > 0); 7957c478bd9Sstevel@tonic-gate 7967c478bd9Sstevel@tonic-gate if (counter < 0) 7977c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??"); 7987c478bd9Sstevel@tonic-gate 7997c478bd9Sstevel@tonic-gate /* move data to static buffer */ 8007c478bd9Sstevel@tonic-gate bcopy(i2c_tp->i2c_rbuf, tod_read, 7); 8017c478bd9Sstevel@tonic-gate 8027c478bd9Sstevel@tonic-gate 8037c478bd9Sstevel@tonic-gate rtc->rtc_year = BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]); 8047c478bd9Sstevel@tonic-gate rtc->rtc_mon = BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]); 8057c478bd9Sstevel@tonic-gate rtc->rtc_dom = BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]); 8067c478bd9Sstevel@tonic-gate rtc->rtc_dow = BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]); 8077c478bd9Sstevel@tonic-gate rtc->rtc_hrs = BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]); 8087c478bd9Sstevel@tonic-gate rtc->rtc_min = BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]); 8097c478bd9Sstevel@tonic-gate rtc->rtc_sec = BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]); 8107c478bd9Sstevel@tonic-gate 8117c478bd9Sstevel@tonic-gate done: 8127c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 8137c478bd9Sstevel@tonic-gate 8147c478bd9Sstevel@tonic-gate mutex_exit(&todds1337_rd_lock); 8157c478bd9Sstevel@tonic-gate return (i2c_cmd_status); 8167c478bd9Sstevel@tonic-gate } 8177c478bd9Sstevel@tonic-gate 8187c478bd9Sstevel@tonic-gate 8197c478bd9Sstevel@tonic-gate static int 8207c478bd9Sstevel@tonic-gate todds1337_write_rtc(struct rtc_t *rtc) 8217c478bd9Sstevel@tonic-gate { 8227c478bd9Sstevel@tonic-gate ds1337_state_t *statep = NULL; 8237c478bd9Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 8247c478bd9Sstevel@tonic-gate int i2c_cmd_status = I2C_SUCCESS; 8257c478bd9Sstevel@tonic-gate 8267c478bd9Sstevel@tonic-gate 8277c478bd9Sstevel@tonic-gate if (!todds1337_attach_done) { 8287c478bd9Sstevel@tonic-gate return (todds1337_prom_setdate(rtc)); 8297c478bd9Sstevel@tonic-gate } 8307c478bd9Sstevel@tonic-gate 8317c478bd9Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 8327c478bd9Sstevel@tonic-gate if (statep == NULL) { 8337c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 8347c478bd9Sstevel@tonic-gate } 8357c478bd9Sstevel@tonic-gate 8367c478bd9Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl, 8377c478bd9Sstevel@tonic-gate &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) { 8387c478bd9Sstevel@tonic-gate return (i2c_cmd_status); 8397c478bd9Sstevel@tonic-gate } 8407c478bd9Sstevel@tonic-gate 8417c478bd9Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 8427c478bd9Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 8437c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; 8447c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec); 8457c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min); 8467c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs); 8477c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow); 8487c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom); 8497c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon); 8507c478bd9Sstevel@tonic-gate i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year); 8517c478bd9Sstevel@tonic-gate i2c_tp->i2c_wlen = 8; 8527c478bd9Sstevel@tonic-gate 8537c478bd9Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 8547c478bd9Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) { 8557c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 8567c478bd9Sstevel@tonic-gate return (i2c_cmd_status); 8577c478bd9Sstevel@tonic-gate } 8587c478bd9Sstevel@tonic-gate 8597c478bd9Sstevel@tonic-gate tod_read[0] = -1; /* invalidate saved data from read routine */ 8607c478bd9Sstevel@tonic-gate 8617c478bd9Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 8627c478bd9Sstevel@tonic-gate 8637c478bd9Sstevel@tonic-gate return (i2c_cmd_status); 8647c478bd9Sstevel@tonic-gate } 8657c478bd9Sstevel@tonic-gate 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 8687c478bd9Sstevel@tonic-gate static int 8697c478bd9Sstevel@tonic-gate todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 8707c478bd9Sstevel@tonic-gate void **result) 8717c478bd9Sstevel@tonic-gate { 8727c478bd9Sstevel@tonic-gate ds1337_state_t *softsp; 8737c478bd9Sstevel@tonic-gate 8747c478bd9Sstevel@tonic-gate if (instance == -1) { 8757c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 8767c478bd9Sstevel@tonic-gate } 8777c478bd9Sstevel@tonic-gate 8787c478bd9Sstevel@tonic-gate switch (infocmd) { 8797c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 8807c478bd9Sstevel@tonic-gate if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) == 8817c478bd9Sstevel@tonic-gate NULL) 8827c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 8837c478bd9Sstevel@tonic-gate *result = (void *)softsp->dip; 8847c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 8857c478bd9Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 886b0b35aceSmike_s *result = (void *)(uintptr_t)instance; 8877c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 8887c478bd9Sstevel@tonic-gate default: 8897c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 8907c478bd9Sstevel@tonic-gate } 8917c478bd9Sstevel@tonic-gate } 8927c478bd9Sstevel@tonic-gate 8937c478bd9Sstevel@tonic-gate /* 8947c478bd9Sstevel@tonic-gate * Finds the device node with device_type "rtc" and opens it to 8957c478bd9Sstevel@tonic-gate * execute the get-time method 8967c478bd9Sstevel@tonic-gate */ 8977c478bd9Sstevel@tonic-gate static int 8987c478bd9Sstevel@tonic-gate todds1337_setup_prom() 8997c478bd9Sstevel@tonic-gate { 900fa9e4066Sahrens pnode_t todnode; 9017c478bd9Sstevel@tonic-gate char tod1337_devpath[MAXNAMELEN]; 9027c478bd9Sstevel@tonic-gate 9037c478bd9Sstevel@tonic-gate if ((todnode = prom_findnode_bydevtype(prom_rootnode(), 9047c478bd9Sstevel@tonic-gate DS1337_DEVICE_TYPE)) == OBP_NONODE) 9057c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 9067c478bd9Sstevel@tonic-gate 9077c478bd9Sstevel@tonic-gate /* 9087c478bd9Sstevel@tonic-gate * We now have the phandle of the rtc node, we need to open the 9097c478bd9Sstevel@tonic-gate * node and get the ihandle 9107c478bd9Sstevel@tonic-gate */ 9117c478bd9Sstevel@tonic-gate if (prom_phandle_to_path(todnode, tod1337_devpath, 9127c478bd9Sstevel@tonic-gate sizeof (tod1337_devpath)) < 0) { 9137c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "prom_phandle_to_path failed"); 9147c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 9157c478bd9Sstevel@tonic-gate } 9167c478bd9Sstevel@tonic-gate 9177c478bd9Sstevel@tonic-gate /* 9187c478bd9Sstevel@tonic-gate * Now open the node and store it's ihandle 9197c478bd9Sstevel@tonic-gate */ 9207c478bd9Sstevel@tonic-gate if ((todds1337_ihandle = prom_open(tod1337_devpath)) == NULL) { 9217c478bd9Sstevel@tonic-gate cmn_err(CE_WARN, "prom_open failed"); 9227c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 9237c478bd9Sstevel@tonic-gate } 9247c478bd9Sstevel@tonic-gate 9257c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 9267c478bd9Sstevel@tonic-gate } 9277c478bd9Sstevel@tonic-gate 9287c478bd9Sstevel@tonic-gate /* 9297c478bd9Sstevel@tonic-gate * Closes the prom interface 9307c478bd9Sstevel@tonic-gate */ 9317c478bd9Sstevel@tonic-gate static void 9327c478bd9Sstevel@tonic-gate todds1337_rele_prom() 9337c478bd9Sstevel@tonic-gate { 9347c478bd9Sstevel@tonic-gate (void) prom_close(todds1337_ihandle); 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate 9377c478bd9Sstevel@tonic-gate /* 9387c478bd9Sstevel@tonic-gate * Read the date using "get-time" method in rtc node 9397c478bd9Sstevel@tonic-gate * PROM returns 1969-1999 when reading 69-99 and 9407c478bd9Sstevel@tonic-gate * 2000-2068 when reading 00-68 9417c478bd9Sstevel@tonic-gate */ 9427c478bd9Sstevel@tonic-gate static int 9437c478bd9Sstevel@tonic-gate todds1337_prom_getdate(struct rtc_t *rtc) 9447c478bd9Sstevel@tonic-gate { 9457c478bd9Sstevel@tonic-gate int year; 9467c478bd9Sstevel@tonic-gate cell_t ci[12]; 9477c478bd9Sstevel@tonic-gate 9487c478bd9Sstevel@tonic-gate ci[0] = p1275_ptr2cell("call-method"); /* Service name */ 9497c478bd9Sstevel@tonic-gate ci[1] = 2; /* # of arguments */ 9507c478bd9Sstevel@tonic-gate ci[2] = 7; /* # of result cells */ 9517c478bd9Sstevel@tonic-gate ci[3] = p1275_ptr2cell("get-time"); 9527c478bd9Sstevel@tonic-gate ci[4] = p1275_ihandle2cell(todds1337_ihandle); 9537c478bd9Sstevel@tonic-gate 9547c478bd9Sstevel@tonic-gate promif_preprom(); 9557c478bd9Sstevel@tonic-gate (void) p1275_cif_handler(&ci); 9567c478bd9Sstevel@tonic-gate promif_postprom(); 9577c478bd9Sstevel@tonic-gate 9587c478bd9Sstevel@tonic-gate year = p1275_cell2int(ci[6]); 9597c478bd9Sstevel@tonic-gate rtc->rtc_mon = p1275_cell2int(ci[7]); 9607c478bd9Sstevel@tonic-gate rtc->rtc_dom = p1275_cell2int(ci[8]); 9617c478bd9Sstevel@tonic-gate rtc->rtc_dow = 0; 9627c478bd9Sstevel@tonic-gate rtc->rtc_hrs = p1275_cell2int(ci[9]); 9637c478bd9Sstevel@tonic-gate rtc->rtc_min = p1275_cell2int(ci[10]); 9647c478bd9Sstevel@tonic-gate rtc->rtc_sec = p1275_cell2int(ci[11]); 9657c478bd9Sstevel@tonic-gate if (year >= 2000) 9667c478bd9Sstevel@tonic-gate year -= 2000; 9677c478bd9Sstevel@tonic-gate else 9687c478bd9Sstevel@tonic-gate year -= 1900; 9697c478bd9Sstevel@tonic-gate rtc->rtc_year = year; 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 9727c478bd9Sstevel@tonic-gate } 9737c478bd9Sstevel@tonic-gate 9747c478bd9Sstevel@tonic-gate /* 9757c478bd9Sstevel@tonic-gate * Read the date using "set-time" method in rtc node 9767c478bd9Sstevel@tonic-gate * For values 00 - 68, write 2000-2068, and for 69-99, 9777c478bd9Sstevel@tonic-gate * write 1969-1999 9787c478bd9Sstevel@tonic-gate */ 9797c478bd9Sstevel@tonic-gate static int 9807c478bd9Sstevel@tonic-gate todds1337_prom_setdate(struct rtc_t *rtc) 9817c478bd9Sstevel@tonic-gate { 9827c478bd9Sstevel@tonic-gate int year; 9837c478bd9Sstevel@tonic-gate cell_t ci[12]; 9847c478bd9Sstevel@tonic-gate 9857c478bd9Sstevel@tonic-gate year = rtc->rtc_year; 9867c478bd9Sstevel@tonic-gate 9877c478bd9Sstevel@tonic-gate if ((year < 0) || (year > 99)) 9887c478bd9Sstevel@tonic-gate return (DDI_FAILURE); 9897c478bd9Sstevel@tonic-gate 9907c478bd9Sstevel@tonic-gate if (year <= 68) 9917c478bd9Sstevel@tonic-gate year = rtc->rtc_year + 2000; 9927c478bd9Sstevel@tonic-gate else 9937c478bd9Sstevel@tonic-gate year = rtc->rtc_year + 1900; 9947c478bd9Sstevel@tonic-gate 9957c478bd9Sstevel@tonic-gate ci[0] = p1275_ptr2cell("call-method"); /* Service name */ 9967c478bd9Sstevel@tonic-gate ci[1] = 8; /* # of arguments */ 9977c478bd9Sstevel@tonic-gate ci[2] = 0; /* # of result cells */ 9987c478bd9Sstevel@tonic-gate ci[3] = p1275_ptr2cell("set-time"); 9997c478bd9Sstevel@tonic-gate ci[4] = p1275_ihandle2cell(todds1337_ihandle); 10007c478bd9Sstevel@tonic-gate ci[5] = p1275_int2cell(year); 10017c478bd9Sstevel@tonic-gate ci[6] = p1275_int2cell(rtc->rtc_mon); 10027c478bd9Sstevel@tonic-gate ci[7] = p1275_int2cell(rtc->rtc_dom); 10037c478bd9Sstevel@tonic-gate ci[8] = p1275_int2cell(rtc->rtc_hrs); 10047c478bd9Sstevel@tonic-gate ci[9] = p1275_int2cell(rtc->rtc_min); 10057c478bd9Sstevel@tonic-gate ci[10] = p1275_int2cell(rtc->rtc_sec); 10067c478bd9Sstevel@tonic-gate 10077c478bd9Sstevel@tonic-gate promif_preprom(); 10087c478bd9Sstevel@tonic-gate (void) p1275_cif_handler(&ci); 10097c478bd9Sstevel@tonic-gate promif_postprom(); 10107c478bd9Sstevel@tonic-gate 10117c478bd9Sstevel@tonic-gate return (DDI_SUCCESS); 10127c478bd9Sstevel@tonic-gate } 1013