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