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