mk48txx.c revision 201004
1/*- 2 * Copyright (c) 2000 The NetBSD Foundation, Inc. 3 * All rights reserved. 4 * 5 * This code is derived from software contributed to The NetBSD Foundation 6 * by Paul Kranenburg. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $NetBSD: mk48txx.c,v 1.25 2008/04/28 20:23:50 martin Exp $ 30 */ 31 32#include <sys/cdefs.h> 33__FBSDID("$FreeBSD: head/sys/dev/mk48txx/mk48txx.c 201004 2009-12-25 21:41:05Z marius $"); 34 35/* 36 * Mostek MK48T02, MK48T08, MK48T18, MK48T59 time-of-day chip subroutines 37 */ 38 39#include <sys/param.h> 40#include <sys/systm.h> 41#include <sys/bus.h> 42#include <sys/clock.h> 43#include <sys/eventhandler.h> 44#include <sys/lock.h> 45#include <sys/mutex.h> 46#include <sys/watchdog.h> 47 48#include <machine/bus.h> 49 50#include <dev/mk48txx/mk48txxreg.h> 51#include <dev/mk48txx/mk48txxvar.h> 52 53#include "clock_if.h" 54 55static uint8_t mk48txx_def_nvrd(device_t dev, int off); 56static void mk48txx_def_nvwr(device_t dev, int off, uint8_t v); 57static void mk48txx_watchdog(void *arg, u_int cmd, int *error); 58 59static const struct { 60 const char *name; 61 bus_size_t nvramsz; 62 bus_size_t clkoff; 63 u_int flags; 64#define MK48TXX_EXT_REGISTERS 1 /* Has extended register set. */ 65} const mk48txx_models[] = { 66 { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 }, 67 { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 }, 68 { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 }, 69 { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS }, 70}; 71 72int 73mk48txx_attach(device_t dev) 74{ 75 struct mk48txx_softc *sc; 76 int i; 77 uint8_t wday; 78 79 sc = device_get_softc(dev); 80 81 if (mtx_initialized(&sc->sc_mtx) == 0) { 82 device_printf(dev, "%s: mutex not initialized\n", __func__); 83 return (ENXIO); 84 } 85 86 device_printf(dev, "model %s", sc->sc_model); 87 i = sizeof(mk48txx_models) / sizeof(mk48txx_models[0]); 88 while (--i >= 0) { 89 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0) { 90 break; 91 } 92 } 93 if (i < 0) { 94 device_printf(dev, " (unsupported)\n"); 95 return (ENXIO); 96 } 97 printf("\n"); 98 sc->sc_nvramsz = mk48txx_models[i].nvramsz; 99 sc->sc_clkoffset = mk48txx_models[i].clkoff; 100 101 if (sc->sc_nvrd == NULL) 102 sc->sc_nvrd = mk48txx_def_nvrd; 103 if (sc->sc_nvwr == NULL) 104 sc->sc_nvwr = mk48txx_def_nvwr; 105 106 if (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS) { 107 mtx_lock(&sc->sc_mtx); 108 if ((*sc->sc_nvrd)(dev, sc->sc_clkoffset + MK48TXX_FLAGS) & 109 MK48TXX_FLAGS_BL) { 110 mtx_unlock(&sc->sc_mtx); 111 device_printf(dev, "%s: battery low\n", __func__); 112 return (ENXIO); 113 } 114 mtx_unlock(&sc->sc_mtx); 115 } 116 117 if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) { 118 /* 119 * Use MK48TXX_WDAY_CB instead of manually adjusting the 120 * century. 121 */ 122 if (!(mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) { 123 device_printf(dev, "%s: no century bit\n", __func__); 124 return (ENXIO); 125 } else { 126 mtx_lock(&sc->sc_mtx); 127 wday = (*sc->sc_nvrd) 128 (dev, sc->sc_clkoffset + MK48TXX_IWDAY); 129 wday |= MK48TXX_WDAY_CEB; 130 (*sc->sc_nvwr) 131 (dev, sc->sc_clkoffset + MK48TXX_IWDAY, wday); 132 mtx_unlock(&sc->sc_mtx); 133 } 134 } 135 136 clock_register(dev, 1000000); /* 1 second resolution */ 137 138 if ((sc->sc_flag & MK48TXX_WDOG_REGISTER) && 139 (mk48txx_models[i].flags & MK48TXX_EXT_REGISTERS)) { 140 sc->sc_wet = EVENTHANDLER_REGISTER(watchdog_list, 141 mk48txx_watchdog, dev, 0); 142 device_printf(dev, 143 "watchdog registered, timeout interval max. 128 sec\n"); 144 } 145 146 return (0); 147} 148 149/* 150 * Get time-of-day and convert to a `struct timespec' 151 * Return 0 on success; an error number otherwise. 152 */ 153int 154mk48txx_gettime(device_t dev, struct timespec *ts) 155{ 156 struct mk48txx_softc *sc; 157 bus_size_t clkoff; 158 struct clocktime ct; 159 int year; 160 uint8_t csr; 161 162 sc = device_get_softc(dev); 163 clkoff = sc->sc_clkoffset; 164 165 mtx_lock(&sc->sc_mtx); 166 /* enable read (stop time) */ 167 csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR); 168 csr |= MK48TXX_CSR_READ; 169 (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr); 170 171#define FROMREG(reg, mask) ((*sc->sc_nvrd)(dev, clkoff + (reg)) & (mask)) 172 173 ct.nsec = 0; 174 ct.sec = FROMBCD(FROMREG(MK48TXX_ISEC, MK48TXX_SEC_MASK)); 175 ct.min = FROMBCD(FROMREG(MK48TXX_IMIN, MK48TXX_MIN_MASK)); 176 ct.hour = FROMBCD(FROMREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK)); 177 ct.day = FROMBCD(FROMREG(MK48TXX_IDAY, MK48TXX_DAY_MASK)); 178 /* Map dow from 1 - 7 to 0 - 6; FROMBCD() isn't necessary here. */ 179 ct.dow = FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK) - 1; 180 ct.mon = FROMBCD(FROMREG(MK48TXX_IMON, MK48TXX_MON_MASK)); 181 year = FROMBCD(FROMREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK)); 182 year += sc->sc_year0; 183 if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) 184 year += (FROMREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB) >> 185 MK48TXX_WDAY_CB_SHIFT) * 100; 186 else if (year < POSIX_BASE_YEAR) 187 year += 100; 188 189#undef FROMREG 190 191 ct.year = year; 192 193 /* time wears on */ 194 csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR); 195 csr &= ~MK48TXX_CSR_READ; 196 (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr); 197 mtx_unlock(&sc->sc_mtx); 198 199 return (clock_ct_to_ts(&ct, ts)); 200} 201 202/* 203 * Set the time-of-day clock based on the value of the `struct timespec' arg. 204 * Return 0 on success; an error number otherwise. 205 */ 206int 207mk48txx_settime(device_t dev, struct timespec *ts) 208{ 209 struct mk48txx_softc *sc; 210 bus_size_t clkoff; 211 struct clocktime ct; 212 uint8_t csr; 213 int cent, year; 214 215 sc = device_get_softc(dev); 216 clkoff = sc->sc_clkoffset; 217 218 /* Accuracy is only one second. */ 219 if (ts->tv_nsec >= 500000000) 220 ts->tv_sec++; 221 ts->tv_nsec = 0; 222 clock_ts_to_ct(ts, &ct); 223 224 mtx_lock(&sc->sc_mtx); 225 /* enable write */ 226 csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR); 227 csr |= MK48TXX_CSR_WRITE; 228 (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr); 229 230#define TOREG(reg, mask, val) \ 231 ((*sc->sc_nvwr)(dev, clkoff + (reg), \ 232 ((*sc->sc_nvrd)(dev, clkoff + (reg)) & ~(mask)) | \ 233 ((val) & (mask)))) 234 235 TOREG(MK48TXX_ISEC, MK48TXX_SEC_MASK, TOBCD(ct.sec)); 236 TOREG(MK48TXX_IMIN, MK48TXX_MIN_MASK, TOBCD(ct.min)); 237 TOREG(MK48TXX_IHOUR, MK48TXX_HOUR_MASK, TOBCD(ct.hour)); 238 /* Map dow from 0 - 6 to 1 - 7; TOBCD() isn't necessary here. */ 239 TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_MASK, ct.dow + 1); 240 TOREG(MK48TXX_IDAY, MK48TXX_DAY_MASK, TOBCD(ct.day)); 241 TOREG(MK48TXX_IMON, MK48TXX_MON_MASK, TOBCD(ct.mon)); 242 243 year = ct.year - sc->sc_year0; 244 if (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) { 245 cent = year / 100; 246 TOREG(MK48TXX_IWDAY, MK48TXX_WDAY_CB, 247 cent << MK48TXX_WDAY_CB_SHIFT); 248 year -= cent * 100; 249 } else if (year > 99) 250 year -= 100; 251 TOREG(MK48TXX_IYEAR, MK48TXX_YEAR_MASK, TOBCD(year)); 252 253#undef TOREG 254 255 /* load them up */ 256 csr = (*sc->sc_nvrd)(dev, clkoff + MK48TXX_ICSR); 257 csr &= ~MK48TXX_CSR_WRITE; 258 (*sc->sc_nvwr)(dev, clkoff + MK48TXX_ICSR, csr); 259 mtx_unlock(&sc->sc_mtx); 260 return (0); 261} 262 263static uint8_t 264mk48txx_def_nvrd(device_t dev, int off) 265{ 266 struct mk48txx_softc *sc; 267 268 sc = device_get_softc(dev); 269 return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, off)); 270} 271 272static void 273mk48txx_def_nvwr(device_t dev, int off, uint8_t v) 274{ 275 struct mk48txx_softc *sc; 276 277 sc = device_get_softc(dev); 278 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v); 279} 280 281static void 282mk48txx_watchdog(void *arg, u_int cmd, int *error) 283{ 284 device_t dev; 285 struct mk48txx_softc *sc; 286 uint8_t t, wdog; 287 288 dev = arg; 289 sc = device_get_softc(dev); 290 291 t = cmd & WD_INTERVAL; 292 if (t >= 26 && t <= 37) { 293 wdog = 0; 294 if (t <= WD_TO_2SEC) { 295 wdog |= MK48TXX_WDOG_RB_1_16; 296 t -= 26; 297 } else if (t <= WD_TO_8SEC) { 298 wdog |= MK48TXX_WDOG_RB_1_4; 299 t -= WD_TO_250MS; 300 } else if (t <= WD_TO_32SEC) { 301 wdog |= MK48TXX_WDOG_RB_1; 302 t -= WD_TO_1SEC; 303 } else { 304 wdog |= MK48TXX_WDOG_RB_4; 305 t -= WD_TO_4SEC; 306 } 307 wdog |= (min(1 << t, 308 MK48TXX_WDOG_BMB_MASK >> MK48TXX_WDOG_BMB_SHIFT)) << 309 MK48TXX_WDOG_BMB_SHIFT; 310 if (sc->sc_flag & MK48TXX_WDOG_ENABLE_WDS) 311 wdog |= MK48TXX_WDOG_WDS; 312 *error = 0; 313 } else { 314 wdog = 0; 315 } 316 mtx_lock(&sc->sc_mtx); 317 (*sc->sc_nvwr)(dev, sc->sc_clkoffset + MK48TXX_WDOG, wdog); 318 mtx_unlock(&sc->sc_mtx); 319} 320