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