1/* $NetBSD: rtc.c,v 1.17 2011/07/01 18:50:41 dyoung Exp $ */ 2 3/* 4 * Copyright (c) 2001 Valeriy E. Ushakov 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30/* 31 * `rtc' is a DS1287A (== MC146818A) time-of-day clock at EBus. 32 * In Krups it's not used to store idprom so this driver doesn't 33 * support it. Don't know about other ms-IIep systems. 34 */ 35 36#include <sys/cdefs.h> 37__KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.17 2011/07/01 18:50:41 dyoung Exp $"); 38 39#include <sys/param.h> 40#include <sys/kernel.h> 41#include <sys/device.h> 42#include <sys/malloc.h> 43#include <sys/systm.h> 44#ifdef GPROF 45#include <sys/gmon.h> 46#endif 47 48#include <sys/bus.h> 49#include <machine/autoconf.h> 50 51#include <dev/clock_subr.h> 52#include <dev/ic/mc146818reg.h> 53 54#include <dev/ebus/ebusreg.h> 55#include <dev/ebus/ebusvar.h> 56 57struct rtc_ebus_softc { 58 bus_space_tag_t sc_bt; /* parent bus tag */ 59 bus_space_handle_t sc_bh; /* handle for registers */ 60}; 61 62static int rtcmatch_ebus(device_t, cfdata_t, void *); 63static void rtcattach_ebus(device_t, device_t, void *); 64 65CFATTACH_DECL_NEW(rtc_ebus, sizeof(struct rtc_ebus_softc), 66 rtcmatch_ebus, rtcattach_ebus, NULL, NULL); 67 68/* XXX: global TOD clock handle (sparc/clock.c) */ 69extern todr_chip_handle_t todr_handle; 70 71/* todr(9) methods */ 72static int rtc_gettime(todr_chip_handle_t, struct timeval *); 73static int rtc_settime(todr_chip_handle_t, struct timeval *); 74 75int rtc_auto_century_adjust = 1; /* XXX: do we ever want not to? */ 76 77 78/* 79 * MD read/write functions declared in mc146818reg.h 80 */ 81#define RTC_ADDR 0 82#define RTC_DATA 1 83 84u_int 85mc146818_read(void *cookie, u_int reg) 86{ 87 struct rtc_ebus_softc *sc = cookie; 88 89 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg); 90 return (bus_space_read_1(sc->sc_bt, sc->sc_bh, RTC_DATA)); 91} 92 93void 94mc146818_write(void *cookie, u_int reg, u_int datum) 95{ 96 struct rtc_ebus_softc *sc = cookie; 97 98 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_ADDR, reg); 99 bus_space_write_1(sc->sc_bt, sc->sc_bh, RTC_DATA, datum); 100} 101 102 103static int 104rtcmatch_ebus(device_t parent, cfdata_t cf, void *aux) 105{ 106 struct ebus_attach_args *ea = aux; 107 108 return (strcmp(cf->cf_name, ea->ea_name) == 0); 109} 110 111static void 112rtcattach_ebus(device_t parent, device_t self, void *aux) 113{ 114 struct rtc_ebus_softc *sc = device_private(self); 115 struct ebus_attach_args *ea = aux; 116 todr_chip_handle_t handle; 117 118 sc->sc_bt = ea->ea_bustag; 119 if (bus_space_map(sc->sc_bt, EBUS_ADDR_FROM_REG(&ea->ea_reg[0]), 120 ea->ea_reg[0].size, 0, &sc->sc_bh) != 0) 121 { 122 printf(": unable to map registers\n"); 123 return; 124 } 125 126 /* XXX: no "model" property in Krups */ 127 printf(": time-of-day clock\n"); 128 129 /* 130 * Turn interrupts off (clear MC_REGB_?IE bits), just in case 131 * (although they shouldn't be wired to an interrupt 132 * controller on sparcs). 133 */ 134 mc146818_write(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR); 135 136 /* setup our todr_handle */ 137 handle = malloc(ALIGN(sizeof(struct todr_chip_handle)), 138 M_DEVBUF, M_NOWAIT); 139 140 handle->cookie = sc; 141 handle->bus_cookie = NULL; /* unused */ 142 handle->todr_gettime = rtc_gettime; 143 handle->todr_settime = rtc_settime; 144 handle->todr_setwen = NULL; /* not necessary, no idprom to protect */ 145 146 todr_attach(handle); 147} 148 149 150/* 151 * Get time-of-day and convert to a `struct timeval' 152 * Return 0 on success; an error number otherwise. 153 */ 154static int 155rtc_gettime(todr_chip_handle_t handle, struct timeval *tv) 156{ 157 struct rtc_ebus_softc *sc = handle->cookie; 158 struct clock_ymdhms dt; 159 u_int year; 160 161 /* update in progress; spin loop */ 162 while (mc146818_read(sc, MC_REGA) & MC_REGA_UIP) 163 continue; 164 165 /* stop updates (XXX: do we need that???) */ 166 mc146818_write(sc, MC_REGB, 167 (mc146818_read(sc, MC_REGB) | MC_REGB_SET)); 168 169 /* read time */ 170 dt.dt_sec = mc146818_read(sc, MC_SEC); 171 dt.dt_min = mc146818_read(sc, MC_MIN); 172 dt.dt_hour = mc146818_read(sc, MC_HOUR); 173 dt.dt_day = mc146818_read(sc, MC_DOM); 174 dt.dt_mon = mc146818_read(sc, MC_MONTH); 175 year = mc146818_read(sc, MC_YEAR); 176 177 /* reenable updates */ 178 mc146818_write(sc, MC_REGB, 179 (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET)); 180 181 /* year in the century 0..99: adjust to AD */ 182 year += 1900; 183 if (year < POSIX_BASE_YEAR && rtc_auto_century_adjust != 0) 184 year += 100; 185 dt.dt_year = year; 186 187 /* simple sanity checks */ 188 if (dt.dt_mon > 12 || dt.dt_day > 31 189 || dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60) 190 return (ERANGE); 191 192 tv->tv_sec = clock_ymdhms_to_secs(&dt); 193 tv->tv_usec = 0; 194 return (0); 195} 196 197/* 198 * Set the time-of-day clock based on the value of the `struct timeval' arg. 199 * Return 0 on success; an error number otherwise. 200 */ 201static int 202rtc_settime(todr_chip_handle_t handle, struct timeval *tv) 203{ 204 struct rtc_ebus_softc *sc = handle->cookie; 205 struct clock_ymdhms dt; 206 u_int year; 207 208 clock_secs_to_ymdhms(tv->tv_sec, &dt); 209 210 year = dt.dt_year - 1900; 211 if (year >= 100 && rtc_auto_century_adjust != 0) 212 year -= 100; 213 214 /* stop updates */ 215 mc146818_write(sc, MC_REGB, 216 (mc146818_read(sc, MC_REGB) | MC_REGB_SET)); 217 218 mc146818_write(sc, MC_SEC, dt.dt_sec); 219 mc146818_write(sc, MC_MIN, dt.dt_min); 220 mc146818_write(sc, MC_HOUR, dt.dt_hour); 221 mc146818_write(sc, MC_DOW, dt.dt_wday + 1); 222 mc146818_write(sc, MC_DOM, dt.dt_day); 223 mc146818_write(sc, MC_MONTH, dt.dt_mon); 224 mc146818_write(sc, MC_YEAR, year); 225 226 /* reenable updates */ 227 mc146818_write(sc, MC_REGB, 228 (mc146818_read(sc, MC_REGB) & ~MC_REGB_SET)); 229 return (0); 230} 231