1 /* $NetBSD: mk48txx.c,v 1.28 2020/01/01 19:24:03 thorpej Exp $ */ 2 /*- 3 * Copyright (c) 2000 The NetBSD Foundation, Inc. 4 * All rights reserved. 5 * 6 * This code is derived from software contributed to The NetBSD Foundation 7 * by Paul Kranenburg. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 2. Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in the 16 * documentation and/or other materials provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 /* 32 * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines. 33 */ 34 35 #include <sys/cdefs.h> 36 __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.28 2020/01/01 19:24:03 thorpej Exp $"); 37 38 #include <sys/param.h> 39 #include <sys/systm.h> 40 #include <sys/device.h> 41 #include <sys/errno.h> 42 43 #include <sys/bus.h> 44 #include <dev/clock_subr.h> 45 #include <dev/ic/mk48txxreg.h> 46 #include <dev/ic/mk48txxvar.h> 47 48 int mk48txx_gettime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *); 49 int mk48txx_settime_ymdhms(todr_chip_handle_t, struct clock_ymdhms *); 50 uint8_t mk48txx_def_nvrd(struct mk48txx_softc *, int); 51 void mk48txx_def_nvwr(struct mk48txx_softc *, int, uint8_t); 52 53 const struct { 54 const char *name; 55 bus_size_t nvramsz; 56 bus_size_t clkoff; 57 int flags; 58 #define MK48TXX_EXT_REGISTERS 1 /* Has extended register set */ 59 } mk48txx_models[] = { 60 { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 }, 61 { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 }, 62 { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 }, 63 { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS }, 64 { "ds1553", DS1553_CLKSZ, DS1553_CLKOFF, MK48TXX_EXT_REGISTERS }, 65 }; 66 67 void 68 mk48txx_attach(struct mk48txx_softc *sc) 69 { 70 todr_chip_handle_t handle; 71 int i; 72 73 aprint_normal(": %s", sc->sc_model); 74 75 i = __arraycount(mk48txx_models); 76 while (--i >= 0) { 77 if (strcmp(sc->sc_model, mk48txx_models[i].name) == 0) 78 break; 79 } 80 if (i < 0) 81 panic("%s: unsupported model", __func__); 82 83 sc->sc_nvramsz = mk48txx_models[i].nvramsz; 84 sc->sc_clkoffset = mk48txx_models[i].clkoff; 85 86 handle = &sc->sc_handle; 87 handle->cookie = sc; 88 handle->todr_gettime = NULL; 89 handle->todr_settime = NULL; 90 handle->todr_gettime_ymdhms = mk48txx_gettime_ymdhms; 91 handle->todr_settime_ymdhms = mk48txx_settime_ymdhms; 92 93 if (sc->sc_nvrd == NULL) 94 sc->sc_nvrd = mk48txx_def_nvrd; 95 if (sc->sc_nvwr == NULL) 96 sc->sc_nvwr = mk48txx_def_nvwr; 97 98 todr_attach(handle); 99 } 100 101 /* 102 * Get time-of-day and convert to a `struct timeval' 103 * Return 0 on success; an error number otherwise. 104 */ 105 int 106 mk48txx_gettime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt) 107 { 108 struct mk48txx_softc *sc; 109 bus_size_t clkoff; 110 int year; 111 uint8_t csr; 112 113 sc = handle->cookie; 114 clkoff = sc->sc_clkoffset; 115 116 /* enable read (stop time) */ 117 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR); 118 csr |= MK48TXX_CSR_READ; 119 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr); 120 121 dt->dt_sec = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_ISEC)); 122 dt->dt_min = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMIN)); 123 dt->dt_hour = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IHOUR)); 124 dt->dt_day = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IDAY)); 125 dt->dt_wday = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IWDAY)); 126 dt->dt_mon = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IMON)); 127 year = bcdtobin((*sc->sc_nvrd)(sc, clkoff + MK48TXX_IYEAR)); 128 129 if (sc->sc_flag & MK48TXX_HAVE_CENT_REG) { 130 year += 100*bcdtobin(csr & MK48TXX_CSR_CENT_MASK); 131 } else { 132 year += sc->sc_year0; 133 if (year < POSIX_BASE_YEAR && 134 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0) 135 year += 100; 136 } 137 138 dt->dt_year = year; 139 140 /* time wears on */ 141 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR); 142 csr &= ~MK48TXX_CSR_READ; 143 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr); 144 145 return 0; 146 } 147 148 /* 149 * Set the time-of-day clock based on the value of the `struct timeval' arg. 150 * Return 0 on success; an error number otherwise. 151 */ 152 int 153 mk48txx_settime_ymdhms(todr_chip_handle_t handle, struct clock_ymdhms *dt) 154 { 155 struct mk48txx_softc *sc; 156 bus_size_t clkoff; 157 uint8_t csr; 158 int year; 159 int cent; 160 161 sc = handle->cookie; 162 clkoff = sc->sc_clkoffset; 163 164 if ((sc->sc_flag & MK48TXX_HAVE_CENT_REG) == 0) { 165 cent = 0; 166 year = dt->dt_year - sc->sc_year0; 167 if (year > 99 && 168 (sc->sc_flag & MK48TXX_NO_CENT_ADJUST) == 0) 169 year -= 100; 170 } else { 171 cent = dt->dt_year / 100; 172 year = dt->dt_year % 100; 173 } 174 175 /* enable write */ 176 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR); 177 csr |= MK48TXX_CSR_WRITE; 178 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr); 179 180 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ISEC, bintobcd(dt->dt_sec)); 181 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMIN, bintobcd(dt->dt_min)); 182 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IHOUR, bintobcd(dt->dt_hour)); 183 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IWDAY, bintobcd(dt->dt_wday)); 184 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IDAY, bintobcd(dt->dt_day)); 185 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IMON, bintobcd(dt->dt_mon)); 186 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_IYEAR, bintobcd(year)); 187 188 /* 189 * If we have a century register and the century has changed 190 * update it. 191 */ 192 if ((sc->sc_flag & MK48TXX_HAVE_CENT_REG) 193 && (csr & MK48TXX_CSR_CENT_MASK) != bintobcd(cent)) { 194 csr &= ~MK48TXX_CSR_CENT_MASK; 195 csr |= MK48TXX_CSR_CENT_MASK & bintobcd(cent); 196 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr); 197 } 198 199 /* load them up */ 200 csr = (*sc->sc_nvrd)(sc, clkoff + MK48TXX_ICSR); 201 csr &= ~MK48TXX_CSR_WRITE; 202 (*sc->sc_nvwr)(sc, clkoff + MK48TXX_ICSR, csr); 203 204 return 0; 205 } 206 207 int 208 mk48txx_get_nvram_size(todr_chip_handle_t handle, bus_size_t *vp) 209 { 210 struct mk48txx_softc *sc; 211 212 sc = handle->cookie; 213 *vp = sc->sc_nvramsz; 214 return 0; 215 } 216 217 uint8_t 218 mk48txx_def_nvrd(struct mk48txx_softc *sc, int off) 219 { 220 221 return bus_space_read_1(sc->sc_bst, sc->sc_bsh, off); 222 } 223 224 void 225 mk48txx_def_nvwr(struct mk48txx_softc *sc, int off, uint8_t v) 226 { 227 228 bus_space_write_1(sc->sc_bst, sc->sc_bsh, off, v); 229 } 230