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