10Sstevel@tonic-gate /* 20Sstevel@tonic-gate * CDDL HEADER START 30Sstevel@tonic-gate * 40Sstevel@tonic-gate * The contents of this file are subject to the terms of the 53728Srameshc * Common Development and Distribution License (the "License"). 63728Srameshc * You may not use this file except in compliance with the License. 70Sstevel@tonic-gate * 80Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 90Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 100Sstevel@tonic-gate * See the License for the specific language governing permissions 110Sstevel@tonic-gate * and limitations under the License. 120Sstevel@tonic-gate * 130Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 140Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 150Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 160Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 170Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 180Sstevel@tonic-gate * 190Sstevel@tonic-gate * CDDL HEADER END 200Sstevel@tonic-gate */ 210Sstevel@tonic-gate /* 22*7656SSherry.Moore@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate 270Sstevel@tonic-gate #include <sys/types.h> 280Sstevel@tonic-gate #include <sys/conf.h> 290Sstevel@tonic-gate #include <sys/devops.h> 300Sstevel@tonic-gate #include <sys/kmem.h> 310Sstevel@tonic-gate #include <sys/open.h> 320Sstevel@tonic-gate #include <sys/file.h> 330Sstevel@tonic-gate #include <sys/note.h> 340Sstevel@tonic-gate #include <sys/ddi.h> 350Sstevel@tonic-gate #include <sys/sunddi.h> 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include <sys/modctl.h> 380Sstevel@tonic-gate #include <sys/stat.h> 390Sstevel@tonic-gate #include <sys/clock.h> 400Sstevel@tonic-gate #include <sys/reboot.h> 410Sstevel@tonic-gate #include <sys/machsystm.h> 420Sstevel@tonic-gate #include <sys/poll.h> 430Sstevel@tonic-gate #include <sys/pbio.h> 440Sstevel@tonic-gate #include <sys/sysmacros.h> 450Sstevel@tonic-gate 460Sstevel@tonic-gate /* Added for prom interface */ 470Sstevel@tonic-gate #include <sys/promif.h> 480Sstevel@tonic-gate #include <sys/promimpl.h> 490Sstevel@tonic-gate 500Sstevel@tonic-gate #include <sys/i2c/misc/i2c_svc.h> 510Sstevel@tonic-gate #include <sys/todds1337.h> 520Sstevel@tonic-gate 530Sstevel@tonic-gate #define DS1337_DEVICE_TYPE "rtc" 540Sstevel@tonic-gate 550Sstevel@tonic-gate /* 560Sstevel@tonic-gate * Driver entry routines 570Sstevel@tonic-gate */ 580Sstevel@tonic-gate static int todds1337_attach(dev_info_t *, ddi_attach_cmd_t); 590Sstevel@tonic-gate static int todds1337_detach(dev_info_t *, ddi_detach_cmd_t); 600Sstevel@tonic-gate static int todds1337_getinfo(dev_info_t *, ddi_info_cmd_t, void *, void **); 610Sstevel@tonic-gate 620Sstevel@tonic-gate /* 630Sstevel@tonic-gate * tod_ops entry routines 640Sstevel@tonic-gate */ 650Sstevel@tonic-gate static timestruc_t todds1337_get(void); 660Sstevel@tonic-gate static void todds1337_set(timestruc_t); 670Sstevel@tonic-gate static uint_t todds1337_set_watchdog_timer(uint_t); 680Sstevel@tonic-gate static uint_t todds1337_clear_watchdog_timer(void); 690Sstevel@tonic-gate static void todds1337_set_power_alarm(timestruc_t); 700Sstevel@tonic-gate static void todds1337_clear_power_alarm(void); 710Sstevel@tonic-gate static int todds1337_setup_prom(); 720Sstevel@tonic-gate static void todds1337_rele_prom(); 730Sstevel@tonic-gate static int todds1337_prom_getdate(struct rtc_t *rtc); 740Sstevel@tonic-gate static int todds1337_prom_setdate(struct rtc_t *rtc); 750Sstevel@tonic-gate 760Sstevel@tonic-gate /* 770Sstevel@tonic-gate * Local functions 780Sstevel@tonic-gate */ 790Sstevel@tonic-gate static int todds1337_read_rtc(struct rtc_t *); 800Sstevel@tonic-gate static int todds1337_write_rtc(struct rtc_t *); 810Sstevel@tonic-gate 820Sstevel@tonic-gate /* Anchor for soft state structure */ 830Sstevel@tonic-gate static void *ds1337_statep; 840Sstevel@tonic-gate static int instance = -1; 850Sstevel@tonic-gate static int todds1337_attach_done = 0; 860Sstevel@tonic-gate static kmutex_t todds1337_rd_lock; 870Sstevel@tonic-gate static kmutex_t todds1337_alarm_lock; 880Sstevel@tonic-gate static ihandle_t todds1337_ihandle = 0; 890Sstevel@tonic-gate 900Sstevel@tonic-gate /* one second time out */ 910Sstevel@tonic-gate #define I2C_CYCLIC_TIMEOUT 1000000000 920Sstevel@tonic-gate uint_t i2c_cyclic_timeout = I2C_CYCLIC_TIMEOUT; 930Sstevel@tonic-gate static int sync_clock_once = 1; 940Sstevel@tonic-gate static struct rtc_t soft_rtc; 950Sstevel@tonic-gate 960Sstevel@tonic-gate /* 970Sstevel@tonic-gate * cp_ops structure 980Sstevel@tonic-gate */ 990Sstevel@tonic-gate static struct cb_ops ds1337_cbops = { 1000Sstevel@tonic-gate nodev, /* open */ 1010Sstevel@tonic-gate nodev, /* close */ 1020Sstevel@tonic-gate nodev, /* strategy */ 1030Sstevel@tonic-gate nodev, /* print */ 1040Sstevel@tonic-gate nodev, /* dump */ 1050Sstevel@tonic-gate nodev, /* read */ 1060Sstevel@tonic-gate nodev, /* write */ 1070Sstevel@tonic-gate nodev, /* ioctl */ 1080Sstevel@tonic-gate nodev, /* devmap */ 1090Sstevel@tonic-gate nodev, /* mmap */ 1100Sstevel@tonic-gate nodev, /* segmap */ 1110Sstevel@tonic-gate NULL, /* poll */ 1120Sstevel@tonic-gate ddi_prop_op, /* cb_prop_op */ 1130Sstevel@tonic-gate NULL, /* streamtab */ 1140Sstevel@tonic-gate D_NEW | D_MP, /* Driver compatibility flag */ 1150Sstevel@tonic-gate CB_REV, /* rev */ 1160Sstevel@tonic-gate nodev, /* int (*cb_aread)() */ 1170Sstevel@tonic-gate nodev /* int (*cb_awrite)() */ 1180Sstevel@tonic-gate }; 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * dev_ops structure 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate static struct dev_ops ds1337_ops = { 1240Sstevel@tonic-gate DEVO_REV, /* devo_rev */ 1250Sstevel@tonic-gate 0, /* refcnt - reference cnt always set to 0 */ 1260Sstevel@tonic-gate todds1337_getinfo, /* getinfo - Maybe requred */ 1270Sstevel@tonic-gate nulldev, /* identify */ 1280Sstevel@tonic-gate nulldev, /* probe */ 1290Sstevel@tonic-gate todds1337_attach, /* attach */ 1300Sstevel@tonic-gate todds1337_detach, /* detach */ 1310Sstevel@tonic-gate nodev, /* reset */ 1320Sstevel@tonic-gate &ds1337_cbops, /* cb_ops - ds1337 does not need this(?) */ 133*7656SSherry.Moore@Sun.COM NULL, /* bus_ops */ 134*7656SSherry.Moore@Sun.COM NULL, /* power */ 135*7656SSherry.Moore@Sun.COM ddi_quiesce_not_needed, /* quiesce */ 1360Sstevel@tonic-gate }; 1370Sstevel@tonic-gate 1380Sstevel@tonic-gate static struct modldrv todds1337_modldrv = { 1390Sstevel@tonic-gate &mod_driverops, /* Type of module. This one is a driver */ 140*7656SSherry.Moore@Sun.COM "tod driver for DS1337 1.12", /* Name of the module. */ 1410Sstevel@tonic-gate &ds1337_ops, /* Pointer to dev_ops */ 1420Sstevel@tonic-gate }; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* 1450Sstevel@tonic-gate * Module linkage structure 1460Sstevel@tonic-gate */ 1470Sstevel@tonic-gate static struct modlinkage todds1337_modlinkage = { 1480Sstevel@tonic-gate MODREV_1, 1490Sstevel@tonic-gate &todds1337_modldrv, 1500Sstevel@tonic-gate 0 1510Sstevel@tonic-gate }; 1520Sstevel@tonic-gate 1530Sstevel@tonic-gate int 1540Sstevel@tonic-gate _init(void) 1550Sstevel@tonic-gate { 1560Sstevel@tonic-gate int error; 1570Sstevel@tonic-gate 1580Sstevel@tonic-gate if (strcmp(tod_module_name, "todds1337") == 0) { 1590Sstevel@tonic-gate if ((error = ddi_soft_state_init(&ds1337_statep, 1605107Seota sizeof (ds1337_state_t), 0)) != DDI_SUCCESS) { 1610Sstevel@tonic-gate return (error); 1620Sstevel@tonic-gate } 1630Sstevel@tonic-gate 1640Sstevel@tonic-gate tod_ops.tod_get = todds1337_get; 1650Sstevel@tonic-gate tod_ops.tod_set = todds1337_set; 1660Sstevel@tonic-gate tod_ops.tod_set_watchdog_timer = todds1337_set_watchdog_timer; 1670Sstevel@tonic-gate tod_ops.tod_clear_watchdog_timer = 1680Sstevel@tonic-gate todds1337_clear_watchdog_timer; 1690Sstevel@tonic-gate tod_ops.tod_set_power_alarm = todds1337_set_power_alarm; 1700Sstevel@tonic-gate tod_ops.tod_clear_power_alarm = todds1337_clear_power_alarm; 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate (void) todds1337_setup_prom(); 1740Sstevel@tonic-gate 1750Sstevel@tonic-gate /* 1760Sstevel@tonic-gate * Install the module 1770Sstevel@tonic-gate */ 1780Sstevel@tonic-gate if ((error = mod_install(&todds1337_modlinkage)) != 0) { 1793728Srameshc if (strcmp(tod_module_name, "todds1337") == 0) { 1803728Srameshc ddi_soft_state_fini(&ds1337_statep); 1813728Srameshc } 1823728Srameshc todds1337_rele_prom(); 1830Sstevel@tonic-gate return (error); 1840Sstevel@tonic-gate } 1850Sstevel@tonic-gate mutex_init(&todds1337_rd_lock, NULL, MUTEX_DEFAULT, NULL); 1860Sstevel@tonic-gate mutex_init(&todds1337_alarm_lock, NULL, MUTEX_DEFAULT, NULL); 1870Sstevel@tonic-gate 1880Sstevel@tonic-gate return (0); 1890Sstevel@tonic-gate } 1900Sstevel@tonic-gate 1910Sstevel@tonic-gate int 1920Sstevel@tonic-gate _fini(void) 1930Sstevel@tonic-gate { 1940Sstevel@tonic-gate int error = 0; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate if (strcmp(tod_module_name, "todds1337") == 0) { 1970Sstevel@tonic-gate error = EBUSY; 1980Sstevel@tonic-gate } else { 1990Sstevel@tonic-gate if ((error = mod_remove(&todds1337_modlinkage)) == 0) { 2000Sstevel@tonic-gate mutex_destroy(&todds1337_rd_lock); 2010Sstevel@tonic-gate mutex_destroy(&todds1337_alarm_lock); 2020Sstevel@tonic-gate todds1337_rele_prom(); 2030Sstevel@tonic-gate } 2040Sstevel@tonic-gate } 2050Sstevel@tonic-gate 2060Sstevel@tonic-gate return (error); 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate 2090Sstevel@tonic-gate int 2100Sstevel@tonic-gate _info(struct modinfo *modinfop) 2110Sstevel@tonic-gate { 2120Sstevel@tonic-gate return (mod_info(&todds1337_modlinkage, modinfop)); 2130Sstevel@tonic-gate } 2140Sstevel@tonic-gate 2150Sstevel@tonic-gate /* 2160Sstevel@tonic-gate * cyclical call to get tod. 2170Sstevel@tonic-gate */ 2180Sstevel@tonic-gate static void 2190Sstevel@tonic-gate todds1337_cyclic(void *arg) 2200Sstevel@tonic-gate { 2210Sstevel@tonic-gate 2220Sstevel@tonic-gate (void) todds1337_read_rtc((struct rtc_t *)arg); 2230Sstevel@tonic-gate 2240Sstevel@tonic-gate } 2250Sstevel@tonic-gate 2260Sstevel@tonic-gate /* 2270Sstevel@tonic-gate * register ds1337 client device with i2c services, and 2280Sstevel@tonic-gate * allocate & initialize soft state structure. 2290Sstevel@tonic-gate */ 2300Sstevel@tonic-gate static int 2310Sstevel@tonic-gate todds1337_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 2320Sstevel@tonic-gate { 2330Sstevel@tonic-gate static ds1337_state_t *statep = NULL; 2340Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 2350Sstevel@tonic-gate uint8_t tempVal = (uint8_t)0; 2360Sstevel@tonic-gate switch (cmd) { 2370Sstevel@tonic-gate case DDI_ATTACH: 2380Sstevel@tonic-gate break; 2390Sstevel@tonic-gate case DDI_RESUME: 2400Sstevel@tonic-gate return (DDI_SUCCESS); 2410Sstevel@tonic-gate default: 2420Sstevel@tonic-gate return (DDI_FAILURE); 2430Sstevel@tonic-gate } 2440Sstevel@tonic-gate 2450Sstevel@tonic-gate if (instance != -1) { 2460Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: wrong instance"); 2470Sstevel@tonic-gate return (DDI_FAILURE); 2480Sstevel@tonic-gate } 2490Sstevel@tonic-gate 2500Sstevel@tonic-gate instance = ddi_get_instance(dip); 2510Sstevel@tonic-gate 2520Sstevel@tonic-gate /* 2530Sstevel@tonic-gate * Allocate soft state structure 2540Sstevel@tonic-gate */ 2550Sstevel@tonic-gate if (ddi_soft_state_zalloc(ds1337_statep, instance) != DDI_SUCCESS) { 2560Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot allocate soft " 2570Sstevel@tonic-gate "state"); 2580Sstevel@tonic-gate instance = -1; 2590Sstevel@tonic-gate return (DDI_FAILURE); 2600Sstevel@tonic-gate } 2610Sstevel@tonic-gate 2620Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 2630Sstevel@tonic-gate if (statep == NULL) { 2640Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot acquire soft " 2650Sstevel@tonic-gate "state"); 2660Sstevel@tonic-gate instance = -1; 2670Sstevel@tonic-gate return (DDI_FAILURE); 2680Sstevel@tonic-gate } 2690Sstevel@tonic-gate 2700Sstevel@tonic-gate statep->dip = dip; 2710Sstevel@tonic-gate 2720Sstevel@tonic-gate if (i2c_client_register(dip, &statep->ds1337_i2c_hdl) != I2C_SUCCESS) { 2730Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 2740Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: cannot register i2c " 2750Sstevel@tonic-gate "client"); 2760Sstevel@tonic-gate instance = -1; 2770Sstevel@tonic-gate return (DDI_FAILURE); 2780Sstevel@tonic-gate } 2790Sstevel@tonic-gate 2800Sstevel@tonic-gate /* check and initialize the oscillator */ 2810Sstevel@tonic-gate 2820Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 2830Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 2840Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 2850Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 2860Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */ 2870Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; 2880Sstevel@tonic-gate i2c_tp->i2c_rlen = 1; 2890Sstevel@tonic-gate 2900Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 2910Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 2920Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl); 2930Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 2940Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to read DS1337 " 2950Sstevel@tonic-gate "status register"); 2960Sstevel@tonic-gate instance = -1; 2970Sstevel@tonic-gate return (DDI_FAILURE); 2980Sstevel@tonic-gate } 2990Sstevel@tonic-gate 3000Sstevel@tonic-gate tempVal = i2c_tp->i2c_rbuf[0]; 3010Sstevel@tonic-gate 3020Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 3030Sstevel@tonic-gate 3040Sstevel@tonic-gate /* 3050Sstevel@tonic-gate * Check Oscillator and initialize chip if OBP failed to do it 3060Sstevel@tonic-gate */ 3070Sstevel@tonic-gate 3080Sstevel@tonic-gate if (tempVal & RTC_CTL_EOSC) { 3090Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 3100Sstevel@tonic-gate 2, 0, I2C_SLEEP); 3110Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 3120Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 3130Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = RTC_CTL; /* Write Control register */ 3140Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = (uchar_t)(RTC_CTL_RS2 | RTC_CTL_RS1 | 3150Sstevel@tonic-gate RTC_CTL_INTCN); 3160Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) 3170Sstevel@tonic-gate != I2C_SUCCESS) { 3180Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, 3190Sstevel@tonic-gate i2c_tp); 3200Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl); 3210Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 3220Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to write " 3230Sstevel@tonic-gate "DS1337 control register"); 3240Sstevel@tonic-gate instance = -1; 3250Sstevel@tonic-gate return (DDI_FAILURE); 3260Sstevel@tonic-gate } 3270Sstevel@tonic-gate 3280Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 3290Sstevel@tonic-gate 3300Sstevel@tonic-gate /* 3310Sstevel@tonic-gate * Now reset the OSF flag in the Status register 3320Sstevel@tonic-gate */ 3330Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 3340Sstevel@tonic-gate 2, 0, I2C_SLEEP); 3350Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 3360Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 3370Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = RTC_STATUS; 3380Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = (uchar_t)0; 3390Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) 3400Sstevel@tonic-gate != I2C_SUCCESS) { 3410Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, 3420Sstevel@tonic-gate i2c_tp); 3430Sstevel@tonic-gate i2c_client_unregister(statep->ds1337_i2c_hdl); 3440Sstevel@tonic-gate ddi_soft_state_free(ds1337_statep, instance); 3450Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_attach: failed to write " 3460Sstevel@tonic-gate "DS1337 status register"); 3470Sstevel@tonic-gate instance = -1; 3480Sstevel@tonic-gate return (DDI_FAILURE); 3490Sstevel@tonic-gate } 3500Sstevel@tonic-gate 3510Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 3520Sstevel@tonic-gate } 3530Sstevel@tonic-gate 3545107Seota /* 3555107Seota * Create a periodical handler to read TOD. 3565107Seota */ 3575107Seota ASSERT(statep->cycid == NULL); 3585107Seota statep->cycid = ddi_periodic_add(todds1337_cyclic, &soft_rtc, 3595107Seota i2c_cyclic_timeout, DDI_IPL_1); 3600Sstevel@tonic-gate statep->state = TOD_ATTACHED; 3610Sstevel@tonic-gate todds1337_attach_done = 1; 3620Sstevel@tonic-gate ddi_report_dev(dip); 3630Sstevel@tonic-gate 3640Sstevel@tonic-gate return (DDI_SUCCESS); 3650Sstevel@tonic-gate } 3660Sstevel@tonic-gate 3670Sstevel@tonic-gate /*ARGSUSED*/ 3680Sstevel@tonic-gate static int 3690Sstevel@tonic-gate todds1337_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 3700Sstevel@tonic-gate { 3710Sstevel@tonic-gate /* 3720Sstevel@tonic-gate * Once attached, do not allow detach because the system constantly 3730Sstevel@tonic-gate * calling todds1337_get() to get the time. If the driver is detached 3740Sstevel@tonic-gate * and the system try to get the time, the system will have memory 3750Sstevel@tonic-gate * problem. 3760Sstevel@tonic-gate * 3770Sstevel@tonic-gate */ 3780Sstevel@tonic-gate switch (cmd) { 3790Sstevel@tonic-gate case DDI_SUSPEND: 3800Sstevel@tonic-gate return (DDI_SUCCESS); 3810Sstevel@tonic-gate default: 3820Sstevel@tonic-gate return (DDI_FAILURE); 3830Sstevel@tonic-gate } 3840Sstevel@tonic-gate } 3850Sstevel@tonic-gate 3860Sstevel@tonic-gate /* *********************** tod_ops entry points ******************** */ 3870Sstevel@tonic-gate 3880Sstevel@tonic-gate /* 3890Sstevel@tonic-gate * Read the current time from the DS1337 chip and convert to UNIX form. 3900Sstevel@tonic-gate * Should be called with tod_clock held. 3910Sstevel@tonic-gate */ 3920Sstevel@tonic-gate 3930Sstevel@tonic-gate static timestruc_t 3940Sstevel@tonic-gate todds1337_get(void) 3950Sstevel@tonic-gate { 3960Sstevel@tonic-gate timestruc_t ts; 3970Sstevel@tonic-gate todinfo_t tod; 3980Sstevel@tonic-gate struct rtc_t rtc; 3990Sstevel@tonic-gate 4000Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 4010Sstevel@tonic-gate 4020Sstevel@tonic-gate if (sync_clock_once) { 4030Sstevel@tonic-gate (void) todds1337_read_rtc(&soft_rtc); 4040Sstevel@tonic-gate sync_clock_once = 0; 4050Sstevel@tonic-gate } else { 4060Sstevel@tonic-gate tod_fault_reset(); 4070Sstevel@tonic-gate return (hrestime); 4080Sstevel@tonic-gate } 4090Sstevel@tonic-gate 4100Sstevel@tonic-gate bcopy(&soft_rtc, &rtc, sizeof (rtc)); 4110Sstevel@tonic-gate 4120Sstevel@tonic-gate /* 4130Sstevel@tonic-gate * 00 - 68 = 2000 thru 2068 4140Sstevel@tonic-gate * 69-99 = 1969 thru 1999 4150Sstevel@tonic-gate */ 4160Sstevel@tonic-gate tod.tod_year = rtc.rtc_year; 4170Sstevel@tonic-gate if (rtc.rtc_year <= 68) 4180Sstevel@tonic-gate tod.tod_year += 100; 4190Sstevel@tonic-gate tod.tod_month = rtc.rtc_mon; 4200Sstevel@tonic-gate tod.tod_day = rtc.rtc_dom; 4210Sstevel@tonic-gate tod.tod_dow = rtc.rtc_dow; 4220Sstevel@tonic-gate tod.tod_hour = rtc.rtc_hrs; 4230Sstevel@tonic-gate tod.tod_min = rtc.rtc_min; 4240Sstevel@tonic-gate tod.tod_sec = rtc.rtc_sec; 4250Sstevel@tonic-gate 4260Sstevel@tonic-gate ts.tv_sec = tod_to_utc(tod); 4270Sstevel@tonic-gate ts.tv_nsec = 0; 4280Sstevel@tonic-gate return (ts); 4290Sstevel@tonic-gate } 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate /* 4320Sstevel@tonic-gate * Program DS1337 with the specified time. 4330Sstevel@tonic-gate * Must be called with tod_lock held. The TOD 4340Sstevel@tonic-gate * chip supports date from 1969-2068 only. We must 4350Sstevel@tonic-gate * reject requests to set date below 1969. 4360Sstevel@tonic-gate */ 4370Sstevel@tonic-gate static void 4380Sstevel@tonic-gate todds1337_set(timestruc_t ts) 4390Sstevel@tonic-gate { 4400Sstevel@tonic-gate struct rtc_t rtc; 4410Sstevel@tonic-gate todinfo_t tod = utc_to_tod(ts.tv_sec); 4420Sstevel@tonic-gate int year; 4430Sstevel@tonic-gate 4440Sstevel@tonic-gate 4450Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 4460Sstevel@tonic-gate 4470Sstevel@tonic-gate /* 4480Sstevel@tonic-gate * Year is base 1900, valid year range 1969-2068 4490Sstevel@tonic-gate */ 4500Sstevel@tonic-gate if ((tod.tod_year < 69) || (tod.tod_year > 168)) 4510Sstevel@tonic-gate return; 4520Sstevel@tonic-gate 4530Sstevel@tonic-gate year = tod.tod_year; 4540Sstevel@tonic-gate if (year >= 100) 4550Sstevel@tonic-gate year -= 100; 4560Sstevel@tonic-gate 4570Sstevel@tonic-gate rtc.rtc_year = (uint8_t)year; 4580Sstevel@tonic-gate rtc.rtc_mon = (uint8_t)tod.tod_month; 4590Sstevel@tonic-gate rtc.rtc_dom = (uint8_t)tod.tod_day; 4600Sstevel@tonic-gate rtc.rtc_dow = (uint8_t)tod.tod_dow; 4610Sstevel@tonic-gate rtc.rtc_hrs = (uint8_t)tod.tod_hour; 4620Sstevel@tonic-gate rtc.rtc_min = (uint8_t)tod.tod_min; 4630Sstevel@tonic-gate rtc.rtc_sec = (uint8_t)tod.tod_sec; 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate (void) todds1337_write_rtc(&rtc); 4660Sstevel@tonic-gate } 4670Sstevel@tonic-gate 4680Sstevel@tonic-gate /* 4690Sstevel@tonic-gate * Program ds1337 registers for alarm to go off at the specified time. 4700Sstevel@tonic-gate * Alarm #1 is used (Alarm #2 stays idle) 4710Sstevel@tonic-gate */ 4720Sstevel@tonic-gate /* ARGSUSED */ 4730Sstevel@tonic-gate static void 4740Sstevel@tonic-gate todds1337_set_power_alarm(timestruc_t ts) 4750Sstevel@tonic-gate { 4760Sstevel@tonic-gate todinfo_t tod; 4770Sstevel@tonic-gate ds1337_state_t *statep = NULL; 4780Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 4790Sstevel@tonic-gate uint8_t tmpval; 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 4820Sstevel@tonic-gate 4830Sstevel@tonic-gate if (!todds1337_attach_done) { 4840Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: driver not attached"); 4850Sstevel@tonic-gate return; 4860Sstevel@tonic-gate } 4870Sstevel@tonic-gate 4880Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 4890Sstevel@tonic-gate if (statep == NULL) { 4900Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failed"); 4910Sstevel@tonic-gate return; 4920Sstevel@tonic-gate } 4930Sstevel@tonic-gate 4940Sstevel@tonic-gate tod = utc_to_tod(ts.tv_sec); 4950Sstevel@tonic-gate 4960Sstevel@tonic-gate /* 4970Sstevel@tonic-gate * i2c_transfe() may block; to avoid locking clock() which 4980Sstevel@tonic-gate * is running in interrupt context at PIL10 we temporarely exit 4990Sstevel@tonic-gate * the tod_mutex and enter alarm lock. Time/date and alarm hardware 5000Sstevel@tonic-gate * have non-interlsecting registers, it is safe to use different locks. 5010Sstevel@tonic-gate */ 5020Sstevel@tonic-gate mutex_exit(&tod_lock); 5030Sstevel@tonic-gate mutex_enter(&todds1337_alarm_lock); 5040Sstevel@tonic-gate 5050Sstevel@tonic-gate /* 5060Sstevel@tonic-gate * Disable Power Alarm (A1IE) 5070Sstevel@tonic-gate */ 5080Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5090Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 5100Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5110Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 5120Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */ 5130Sstevel@tonic-gate 5140Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5150Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5160Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to read " 5170Sstevel@tonic-gate "DS1337 control register"); 5180Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5190Sstevel@tonic-gate mutex_enter(&tod_lock); 5200Sstevel@tonic-gate return; 5210Sstevel@tonic-gate } 5220Sstevel@tonic-gate 5230Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0]; 5240Sstevel@tonic-gate 5250Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5260Sstevel@tonic-gate 5270Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5280Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 5290Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5300Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 5310Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 5320Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE; 5330Sstevel@tonic-gate 5340Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5350Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5360Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write " 5370Sstevel@tonic-gate "DS1337control register"); 5380Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5390Sstevel@tonic-gate mutex_enter(&tod_lock); 5400Sstevel@tonic-gate return; 5410Sstevel@tonic-gate } 5420Sstevel@tonic-gate 5430Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5440Sstevel@tonic-gate 5450Sstevel@tonic-gate 5460Sstevel@tonic-gate /* 5470Sstevel@tonic-gate * Write Alarm #1 registers 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate 5500Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5510Sstevel@tonic-gate &i2c_tp, 5, 0, I2C_SLEEP); 5520Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5530Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 5540Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_ALARM_SEC; /* Alarm #1 Seconds */ 5550Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(tod.tod_sec); 5560Sstevel@tonic-gate i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(tod.tod_min); 5570Sstevel@tonic-gate i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(tod.tod_hour); 5580Sstevel@tonic-gate i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(tod.tod_day); 5590Sstevel@tonic-gate 5600Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5610Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5620Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to write " 5630Sstevel@tonic-gate "DS1337 alarm registers"); 5640Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5650Sstevel@tonic-gate mutex_enter(&tod_lock); 5660Sstevel@tonic-gate return; 5670Sstevel@tonic-gate } 5680Sstevel@tonic-gate 5690Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5700Sstevel@tonic-gate 5710Sstevel@tonic-gate 5720Sstevel@tonic-gate /* 5730Sstevel@tonic-gate * Enable Power Alarm 5740Sstevel@tonic-gate */ 5750Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 5760Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 5770Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 5780Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 5790Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 5800Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval | RTC_CTL_A1IE; 5810Sstevel@tonic-gate 5820Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 5830Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5840Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_set_power_alarm: failed to enable " 5850Sstevel@tonic-gate "DS1337 alarm"); 5860Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5870Sstevel@tonic-gate mutex_enter(&tod_lock); 5880Sstevel@tonic-gate return; 5890Sstevel@tonic-gate } 5900Sstevel@tonic-gate 5910Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 5920Sstevel@tonic-gate 5930Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 5940Sstevel@tonic-gate mutex_enter(&tod_lock); 5950Sstevel@tonic-gate } 5960Sstevel@tonic-gate 5970Sstevel@tonic-gate /* ARGSUSED */ 5980Sstevel@tonic-gate static void 5990Sstevel@tonic-gate todds1337_clear_power_alarm(void) 6000Sstevel@tonic-gate { 6010Sstevel@tonic-gate ds1337_state_t *statep = NULL; 6020Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 6030Sstevel@tonic-gate uint8_t tmpval; 6040Sstevel@tonic-gate 6050Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 6060Sstevel@tonic-gate 6070Sstevel@tonic-gate if (!todds1337_attach_done) { 6080Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: driver was not attached"); 6090Sstevel@tonic-gate return; 6100Sstevel@tonic-gate } 6110Sstevel@tonic-gate 6120Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 6130Sstevel@tonic-gate if (statep == NULL) { 6140Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state has failed"); 6150Sstevel@tonic-gate return; 6160Sstevel@tonic-gate } 6170Sstevel@tonic-gate 6180Sstevel@tonic-gate /* 6190Sstevel@tonic-gate * i2c_transfe() may block; to avoid locking clock() which 6200Sstevel@tonic-gate * is running in interrupt context at PIL10 we temporarely exit 6210Sstevel@tonic-gate * the tod_mutex and enter alarm lock. Time/date and alarm hardware 6220Sstevel@tonic-gate * have non-interlsecting registers, it is safe to use different locks. 6230Sstevel@tonic-gate */ 6240Sstevel@tonic-gate mutex_exit(&tod_lock); 6250Sstevel@tonic-gate mutex_enter(&todds1337_alarm_lock); 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate /* 6280Sstevel@tonic-gate * Disable Alarm #1 Interrupt 6290Sstevel@tonic-gate */ 6300Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6310Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 6320Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6330Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 6340Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Read Control register */ 6350Sstevel@tonic-gate 6360Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6370Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6380Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read " 6390Sstevel@tonic-gate "DS1337 control register"); 6400Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 6410Sstevel@tonic-gate mutex_enter(&tod_lock); 6420Sstevel@tonic-gate return; 6430Sstevel@tonic-gate } 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0]; 6460Sstevel@tonic-gate 6470Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6480Sstevel@tonic-gate 6490Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6500Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 6510Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6520Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 6530Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_CTL; /* Write Control register */ 6540Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_CTL_A1IE; 6550Sstevel@tonic-gate 6560Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6570Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6580Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write " 6590Sstevel@tonic-gate "DS1337 control register"); 6600Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 6610Sstevel@tonic-gate mutex_enter(&tod_lock); 6620Sstevel@tonic-gate return; 6630Sstevel@tonic-gate } 6640Sstevel@tonic-gate 6650Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6660Sstevel@tonic-gate 6670Sstevel@tonic-gate /* 6680Sstevel@tonic-gate * Reset Alarm #1 Flag 6690Sstevel@tonic-gate */ 6700Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6710Sstevel@tonic-gate &i2c_tp, 1, 1, I2C_SLEEP); 6720Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6730Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 6740Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Read Status register */ 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6770Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6780Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to read " 6790Sstevel@tonic-gate "DS1337 status register"); 6800Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 6810Sstevel@tonic-gate mutex_enter(&tod_lock); 6820Sstevel@tonic-gate return; 6830Sstevel@tonic-gate } 6840Sstevel@tonic-gate 6850Sstevel@tonic-gate tmpval = i2c_tp->i2c_rbuf[0]; 6860Sstevel@tonic-gate 6870Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate (void) i2c_transfer_alloc(statep->ds1337_i2c_hdl, 6900Sstevel@tonic-gate &i2c_tp, 2, 0, I2C_SLEEP); 6910Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 6920Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 6930Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_STATUS; /* Write Status register */ 6940Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = tmpval & ~RTC_STATUS_A1F; 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate if ((i2c_transfer(statep->ds1337_i2c_hdl, i2c_tp)) != I2C_SUCCESS) { 6970Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 6980Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337_clear_power_alarm: failed to write " 6990Sstevel@tonic-gate "DS1337 status register"); 7000Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 7010Sstevel@tonic-gate mutex_enter(&tod_lock); 7020Sstevel@tonic-gate return; 7030Sstevel@tonic-gate } 7040Sstevel@tonic-gate 7050Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 7060Sstevel@tonic-gate 7070Sstevel@tonic-gate mutex_exit(&todds1337_alarm_lock); 7080Sstevel@tonic-gate mutex_enter(&tod_lock); 7090Sstevel@tonic-gate } 7100Sstevel@tonic-gate 7110Sstevel@tonic-gate /* ARGSUSED */ 7120Sstevel@tonic-gate static uint_t 7130Sstevel@tonic-gate todds1337_set_watchdog_timer(uint_t timeoutval) 7140Sstevel@tonic-gate { 7150Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 7160Sstevel@tonic-gate return (0); 7170Sstevel@tonic-gate } 7180Sstevel@tonic-gate 7190Sstevel@tonic-gate /* ARGSUSED */ 7200Sstevel@tonic-gate static uint_t 7210Sstevel@tonic-gate todds1337_clear_watchdog_timer(void) 7220Sstevel@tonic-gate { 7230Sstevel@tonic-gate ASSERT(MUTEX_HELD(&tod_lock)); 7240Sstevel@tonic-gate return (0); 7250Sstevel@tonic-gate } 7260Sstevel@tonic-gate 7270Sstevel@tonic-gate /* ********************** Local functions ***************************** */ 7280Sstevel@tonic-gate 7290Sstevel@tonic-gate static char tod_read[7] = {-1, -1, -1, -1, -1, -1, -1}; 7300Sstevel@tonic-gate static int 7310Sstevel@tonic-gate todds1337_read_rtc(struct rtc_t *rtc) 7320Sstevel@tonic-gate { 7330Sstevel@tonic-gate static ds1337_state_t *statep = NULL; 7340Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 7350Sstevel@tonic-gate int i2c_cmd_status = I2C_FAILURE; 7360Sstevel@tonic-gate int counter = 4; 7370Sstevel@tonic-gate 7380Sstevel@tonic-gate if (!todds1337_attach_done) { 7390Sstevel@tonic-gate return (todds1337_prom_getdate(rtc)); 7400Sstevel@tonic-gate } 7410Sstevel@tonic-gate 7420Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 7430Sstevel@tonic-gate if (statep == NULL) { 7440Sstevel@tonic-gate cmn_err(CE_WARN, "todds1337: ddi_get_soft_state failing"); 7450Sstevel@tonic-gate return (DDI_FAILURE); 7460Sstevel@tonic-gate } 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate mutex_enter(&todds1337_rd_lock); 7490Sstevel@tonic-gate 7500Sstevel@tonic-gate /* 7510Sstevel@tonic-gate * Allocate 1 byte for write buffer and 7 bytes for read buffer to 7520Sstevel@tonic-gate * to accomodate sec, min, hrs, dayOfWeek, dayOfMonth, year 7530Sstevel@tonic-gate */ 7540Sstevel@tonic-gate if ((i2c_transfer_alloc(statep->ds1337_i2c_hdl, &i2c_tp, 1, 7550Sstevel@tonic-gate 7, I2C_SLEEP)) != I2C_SUCCESS) { 7560Sstevel@tonic-gate mutex_exit(&todds1337_rd_lock); 7570Sstevel@tonic-gate return (DDI_FAILURE); 7580Sstevel@tonic-gate } 7590Sstevel@tonic-gate 7600Sstevel@tonic-gate do { 7610Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 7620Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR_RD; 7630Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; /* Start from 0x00 */ 7640Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; /* Write one byte address */ 7650Sstevel@tonic-gate i2c_tp->i2c_rlen = 7; /* Read 7 regs */ 7660Sstevel@tonic-gate 7670Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 7680Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) { 7690Sstevel@tonic-gate goto done; 7700Sstevel@tonic-gate } 7715107Seota /* for first read, need to get valid data */ 7725107Seota while (tod_read[0] == -1 && counter > 0) { 7735107Seota /* move data to static buffer */ 7740Sstevel@tonic-gate bcopy(i2c_tp->i2c_rbuf, tod_read, 7); 7750Sstevel@tonic-gate 7760Sstevel@tonic-gate /* now read again */ 7770Sstevel@tonic-gate /* Start reading reg from 0x00 */ 7780Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)0x00; 7790Sstevel@tonic-gate i2c_tp->i2c_wlen = 1; /* Write one byte address */ 7800Sstevel@tonic-gate i2c_tp->i2c_rlen = 7; /* Read 7 regs */ 7810Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 7820Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) { 7835107Seota goto done; 7840Sstevel@tonic-gate } 7850Sstevel@tonic-gate /* if they are not the same, then read again */ 7860Sstevel@tonic-gate if (bcmp(tod_read, i2c_tp->i2c_rbuf, 7) != 0) { 7875107Seota tod_read[0] = -1; 7885107Seota counter--; 7890Sstevel@tonic-gate } 7905107Seota } 7910Sstevel@tonic-gate 7920Sstevel@tonic-gate } while (i2c_tp->i2c_rbuf[0] == 0x59 && 7930Sstevel@tonic-gate /* if seconds register is 0x59 (BCD), add data should match */ 7945107Seota bcmp(&tod_read[1], &i2c_tp->i2c_rbuf[1], 6) != 0 && 7955107Seota counter-- > 0); 7960Sstevel@tonic-gate 7970Sstevel@tonic-gate if (counter < 0) 7985107Seota cmn_err(CE_WARN, "i2ctod: TOD Chip failed ??"); 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate /* move data to static buffer */ 8010Sstevel@tonic-gate bcopy(i2c_tp->i2c_rbuf, tod_read, 7); 8020Sstevel@tonic-gate 8030Sstevel@tonic-gate 8040Sstevel@tonic-gate rtc->rtc_year = BCD_TO_BYTE(i2c_tp->i2c_rbuf[6]); 8050Sstevel@tonic-gate rtc->rtc_mon = BCD_TO_BYTE(i2c_tp->i2c_rbuf[5]); 8060Sstevel@tonic-gate rtc->rtc_dom = BCD_TO_BYTE(i2c_tp->i2c_rbuf[4]); 8070Sstevel@tonic-gate rtc->rtc_dow = BCD_TO_BYTE(i2c_tp->i2c_rbuf[3]); 8080Sstevel@tonic-gate rtc->rtc_hrs = BCD_TO_BYTE(i2c_tp->i2c_rbuf[2]); 8090Sstevel@tonic-gate rtc->rtc_min = BCD_TO_BYTE(i2c_tp->i2c_rbuf[1]); 8100Sstevel@tonic-gate rtc->rtc_sec = BCD_TO_BYTE(i2c_tp->i2c_rbuf[0]); 8110Sstevel@tonic-gate 8120Sstevel@tonic-gate done: 8130Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 8140Sstevel@tonic-gate 8150Sstevel@tonic-gate mutex_exit(&todds1337_rd_lock); 8160Sstevel@tonic-gate return (i2c_cmd_status); 8170Sstevel@tonic-gate } 8180Sstevel@tonic-gate 8190Sstevel@tonic-gate 8200Sstevel@tonic-gate static int 8210Sstevel@tonic-gate todds1337_write_rtc(struct rtc_t *rtc) 8220Sstevel@tonic-gate { 8230Sstevel@tonic-gate ds1337_state_t *statep = NULL; 8240Sstevel@tonic-gate i2c_transfer_t *i2c_tp = NULL; 8250Sstevel@tonic-gate int i2c_cmd_status = I2C_SUCCESS; 8260Sstevel@tonic-gate 8270Sstevel@tonic-gate 8280Sstevel@tonic-gate if (!todds1337_attach_done) { 8290Sstevel@tonic-gate return (todds1337_prom_setdate(rtc)); 8300Sstevel@tonic-gate } 8310Sstevel@tonic-gate 8320Sstevel@tonic-gate statep = ddi_get_soft_state(ds1337_statep, instance); 8330Sstevel@tonic-gate if (statep == NULL) { 8340Sstevel@tonic-gate return (DDI_FAILURE); 8350Sstevel@tonic-gate } 8360Sstevel@tonic-gate 8370Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer_alloc(statep->ds1337_i2c_hdl, 8380Sstevel@tonic-gate &i2c_tp, 8, 0, I2C_SLEEP)) != I2C_SUCCESS) { 8390Sstevel@tonic-gate return (i2c_cmd_status); 8400Sstevel@tonic-gate } 8410Sstevel@tonic-gate 8420Sstevel@tonic-gate i2c_tp->i2c_version = I2C_XFER_REV; 8430Sstevel@tonic-gate i2c_tp->i2c_flags = I2C_WR; 8440Sstevel@tonic-gate i2c_tp->i2c_wbuf[0] = (uchar_t)RTC_SEC; 8450Sstevel@tonic-gate i2c_tp->i2c_wbuf[1] = BYTE_TO_BCD(rtc->rtc_sec); 8460Sstevel@tonic-gate i2c_tp->i2c_wbuf[2] = BYTE_TO_BCD(rtc->rtc_min); 8470Sstevel@tonic-gate i2c_tp->i2c_wbuf[3] = BYTE_TO_BCD(rtc->rtc_hrs); 8480Sstevel@tonic-gate i2c_tp->i2c_wbuf[4] = BYTE_TO_BCD(rtc->rtc_dow); 8490Sstevel@tonic-gate i2c_tp->i2c_wbuf[5] = BYTE_TO_BCD(rtc->rtc_dom); 8500Sstevel@tonic-gate i2c_tp->i2c_wbuf[6] = BYTE_TO_BCD(rtc->rtc_mon); 8510Sstevel@tonic-gate i2c_tp->i2c_wbuf[7] = BYTE_TO_BCD(rtc->rtc_year); 8520Sstevel@tonic-gate i2c_tp->i2c_wlen = 8; 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate if ((i2c_cmd_status = i2c_transfer(statep->ds1337_i2c_hdl, 8550Sstevel@tonic-gate i2c_tp)) != I2C_SUCCESS) { 8560Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 8570Sstevel@tonic-gate return (i2c_cmd_status); 8580Sstevel@tonic-gate } 8590Sstevel@tonic-gate 8600Sstevel@tonic-gate tod_read[0] = -1; /* invalidate saved data from read routine */ 8610Sstevel@tonic-gate 8620Sstevel@tonic-gate (void) i2c_transfer_free(statep->ds1337_i2c_hdl, i2c_tp); 8630Sstevel@tonic-gate 8640Sstevel@tonic-gate return (i2c_cmd_status); 8650Sstevel@tonic-gate } 8660Sstevel@tonic-gate 8670Sstevel@tonic-gate 8680Sstevel@tonic-gate /*ARGSUSED*/ 8690Sstevel@tonic-gate static int 8700Sstevel@tonic-gate todds1337_getinfo(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, 8710Sstevel@tonic-gate void **result) 8720Sstevel@tonic-gate { 8730Sstevel@tonic-gate ds1337_state_t *softsp; 8740Sstevel@tonic-gate 8750Sstevel@tonic-gate if (instance == -1) { 8760Sstevel@tonic-gate return (DDI_FAILURE); 8770Sstevel@tonic-gate } 8780Sstevel@tonic-gate 8790Sstevel@tonic-gate switch (infocmd) { 8800Sstevel@tonic-gate case DDI_INFO_DEVT2DEVINFO: 8810Sstevel@tonic-gate if ((softsp = ddi_get_soft_state(ds1337_statep, instance)) == 8820Sstevel@tonic-gate NULL) 8830Sstevel@tonic-gate return (DDI_FAILURE); 8840Sstevel@tonic-gate *result = (void *)softsp->dip; 8850Sstevel@tonic-gate return (DDI_SUCCESS); 8860Sstevel@tonic-gate case DDI_INFO_DEVT2INSTANCE: 8871004Smike_s *result = (void *)(uintptr_t)instance; 8880Sstevel@tonic-gate return (DDI_SUCCESS); 8890Sstevel@tonic-gate default: 8900Sstevel@tonic-gate return (DDI_FAILURE); 8910Sstevel@tonic-gate } 8920Sstevel@tonic-gate } 8930Sstevel@tonic-gate 8940Sstevel@tonic-gate /* 8950Sstevel@tonic-gate * Finds the device node with device_type "rtc" and opens it to 8960Sstevel@tonic-gate * execute the get-time method 8970Sstevel@tonic-gate */ 8980Sstevel@tonic-gate static int 8990Sstevel@tonic-gate todds1337_setup_prom() 9000Sstevel@tonic-gate { 901789Sahrens pnode_t todnode; 9020Sstevel@tonic-gate char tod1337_devpath[MAXNAMELEN]; 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate if ((todnode = prom_findnode_bydevtype(prom_rootnode(), 9050Sstevel@tonic-gate DS1337_DEVICE_TYPE)) == OBP_NONODE) 9060Sstevel@tonic-gate return (DDI_FAILURE); 9070Sstevel@tonic-gate 9080Sstevel@tonic-gate /* 9090Sstevel@tonic-gate * We now have the phandle of the rtc node, we need to open the 9100Sstevel@tonic-gate * node and get the ihandle 9110Sstevel@tonic-gate */ 9120Sstevel@tonic-gate if (prom_phandle_to_path(todnode, tod1337_devpath, 9130Sstevel@tonic-gate sizeof (tod1337_devpath)) < 0) { 9140Sstevel@tonic-gate cmn_err(CE_WARN, "prom_phandle_to_path failed"); 9150Sstevel@tonic-gate return (DDI_FAILURE); 9160Sstevel@tonic-gate } 9170Sstevel@tonic-gate 9180Sstevel@tonic-gate /* 9190Sstevel@tonic-gate * Now open the node and store it's ihandle 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate if ((todds1337_ihandle = prom_open(tod1337_devpath)) == NULL) { 9220Sstevel@tonic-gate cmn_err(CE_WARN, "prom_open failed"); 9230Sstevel@tonic-gate return (DDI_FAILURE); 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate 9260Sstevel@tonic-gate return (DDI_SUCCESS); 9270Sstevel@tonic-gate } 9280Sstevel@tonic-gate 9290Sstevel@tonic-gate /* 9300Sstevel@tonic-gate * Closes the prom interface 9310Sstevel@tonic-gate */ 9320Sstevel@tonic-gate static void 9330Sstevel@tonic-gate todds1337_rele_prom() 9340Sstevel@tonic-gate { 9350Sstevel@tonic-gate (void) prom_close(todds1337_ihandle); 9360Sstevel@tonic-gate } 9370Sstevel@tonic-gate 9380Sstevel@tonic-gate /* 9390Sstevel@tonic-gate * Read the date using "get-time" method in rtc node 9400Sstevel@tonic-gate * PROM returns 1969-1999 when reading 69-99 and 9410Sstevel@tonic-gate * 2000-2068 when reading 00-68 9420Sstevel@tonic-gate */ 9430Sstevel@tonic-gate static int 9440Sstevel@tonic-gate todds1337_prom_getdate(struct rtc_t *rtc) 9450Sstevel@tonic-gate { 9460Sstevel@tonic-gate int year; 9470Sstevel@tonic-gate cell_t ci[12]; 9480Sstevel@tonic-gate 9490Sstevel@tonic-gate ci[0] = p1275_ptr2cell("call-method"); /* Service name */ 9500Sstevel@tonic-gate ci[1] = 2; /* # of arguments */ 9510Sstevel@tonic-gate ci[2] = 7; /* # of result cells */ 9520Sstevel@tonic-gate ci[3] = p1275_ptr2cell("get-time"); 9530Sstevel@tonic-gate ci[4] = p1275_ihandle2cell(todds1337_ihandle); 9540Sstevel@tonic-gate 9550Sstevel@tonic-gate promif_preprom(); 9560Sstevel@tonic-gate (void) p1275_cif_handler(&ci); 9570Sstevel@tonic-gate promif_postprom(); 9580Sstevel@tonic-gate 9590Sstevel@tonic-gate year = p1275_cell2int(ci[6]); 9600Sstevel@tonic-gate rtc->rtc_mon = p1275_cell2int(ci[7]); 9610Sstevel@tonic-gate rtc->rtc_dom = p1275_cell2int(ci[8]); 9620Sstevel@tonic-gate rtc->rtc_dow = 0; 9630Sstevel@tonic-gate rtc->rtc_hrs = p1275_cell2int(ci[9]); 9640Sstevel@tonic-gate rtc->rtc_min = p1275_cell2int(ci[10]); 9650Sstevel@tonic-gate rtc->rtc_sec = p1275_cell2int(ci[11]); 9660Sstevel@tonic-gate if (year >= 2000) 9670Sstevel@tonic-gate year -= 2000; 9680Sstevel@tonic-gate else 9690Sstevel@tonic-gate year -= 1900; 9700Sstevel@tonic-gate rtc->rtc_year = year; 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate return (DDI_SUCCESS); 9730Sstevel@tonic-gate } 9740Sstevel@tonic-gate 9750Sstevel@tonic-gate /* 9760Sstevel@tonic-gate * Read the date using "set-time" method in rtc node 9770Sstevel@tonic-gate * For values 00 - 68, write 2000-2068, and for 69-99, 9780Sstevel@tonic-gate * write 1969-1999 9790Sstevel@tonic-gate */ 9800Sstevel@tonic-gate static int 9810Sstevel@tonic-gate todds1337_prom_setdate(struct rtc_t *rtc) 9820Sstevel@tonic-gate { 9830Sstevel@tonic-gate int year; 9840Sstevel@tonic-gate cell_t ci[12]; 9850Sstevel@tonic-gate 9860Sstevel@tonic-gate year = rtc->rtc_year; 9870Sstevel@tonic-gate 9880Sstevel@tonic-gate if ((year < 0) || (year > 99)) 9890Sstevel@tonic-gate return (DDI_FAILURE); 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate if (year <= 68) 9920Sstevel@tonic-gate year = rtc->rtc_year + 2000; 9930Sstevel@tonic-gate else 9940Sstevel@tonic-gate year = rtc->rtc_year + 1900; 9950Sstevel@tonic-gate 9960Sstevel@tonic-gate ci[0] = p1275_ptr2cell("call-method"); /* Service name */ 9970Sstevel@tonic-gate ci[1] = 8; /* # of arguments */ 9980Sstevel@tonic-gate ci[2] = 0; /* # of result cells */ 9990Sstevel@tonic-gate ci[3] = p1275_ptr2cell("set-time"); 10000Sstevel@tonic-gate ci[4] = p1275_ihandle2cell(todds1337_ihandle); 10010Sstevel@tonic-gate ci[5] = p1275_int2cell(year); 10020Sstevel@tonic-gate ci[6] = p1275_int2cell(rtc->rtc_mon); 10030Sstevel@tonic-gate ci[7] = p1275_int2cell(rtc->rtc_dom); 10040Sstevel@tonic-gate ci[8] = p1275_int2cell(rtc->rtc_hrs); 10050Sstevel@tonic-gate ci[9] = p1275_int2cell(rtc->rtc_min); 10060Sstevel@tonic-gate ci[10] = p1275_int2cell(rtc->rtc_sec); 10070Sstevel@tonic-gate 10080Sstevel@tonic-gate promif_preprom(); 10090Sstevel@tonic-gate (void) p1275_cif_handler(&ci); 10100Sstevel@tonic-gate promif_postprom(); 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate return (DDI_SUCCESS); 10130Sstevel@tonic-gate } 1014