1 /* $NetBSD: mk48txx.c,v 1.10 2002/02/23 17:18:56 scw 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 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the NetBSD 20 * Foundation, Inc. and its contributors. 21 * 4. Neither the name of The NetBSD Foundation nor the names of its 22 * contributors may be used to endorse or promote products derived 23 * from this software without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 26 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 27 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 29 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 35 * POSSIBILITY OF SUCH DAMAGE. 36 */ 37 38 /* 39 * Mostek MK48T02, MK48T08, MK48T59 time-of-day chip subroutines. 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: mk48txx.c,v 1.10 2002/02/23 17:18:56 scw Exp $"); 44 45 #include <sys/param.h> 46 #include <sys/malloc.h> 47 #include <sys/systm.h> 48 #include <sys/errno.h> 49 50 #include <machine/bus.h> 51 #include <dev/clock_subr.h> 52 #include <dev/ic/mk48txxreg.h> 53 54 55 struct mk48txx { 56 bus_space_tag_t mk_bt; /* bus tag & handle */ 57 bus_space_handle_t mk_bh; /* */ 58 bus_size_t mk_nvramsz; /* Size of NVRAM on the chip */ 59 bus_size_t mk_clkoffset; /* Offset in NVRAM to clock bits */ 60 u_int mk_year0; /* What year is represented on the system 61 by the chip's year counter at 0 */ 62 mk48txx_nvrd_t mk_nvrd; /* NVRAM/RTC read function */ 63 mk48txx_nvwr_t mk_nvwr; /* NVRAM/RTC write function */ 64 }; 65 66 int mk48txx_gettime(todr_chip_handle_t, struct timeval *); 67 int mk48txx_settime(todr_chip_handle_t, struct timeval *); 68 int mk48txx_getcal(todr_chip_handle_t, int *); 69 int mk48txx_setcal(todr_chip_handle_t, int); 70 u_int8_t mk48txx_def_nvrd(bus_space_tag_t, bus_space_handle_t, int); 71 void mk48txx_def_nvwr(bus_space_tag_t, bus_space_handle_t, int, u_int8_t); 72 73 int mk48txx_auto_century_adjust = 1; 74 75 struct { 76 const char *name; 77 bus_size_t nvramsz; 78 bus_size_t clkoff; 79 int flags; 80 #define MK48TXX_EXT_REGISTERS 1 /* Has extended register set */ 81 } mk48txx_models[] = { 82 { "mk48t02", MK48T02_CLKSZ, MK48T02_CLKOFF, 0 }, 83 { "mk48t08", MK48T08_CLKSZ, MK48T08_CLKOFF, 0 }, 84 { "mk48t18", MK48T18_CLKSZ, MK48T18_CLKOFF, 0 }, 85 { "mk48t59", MK48T59_CLKSZ, MK48T59_CLKOFF, MK48TXX_EXT_REGISTERS }, 86 }; 87 88 todr_chip_handle_t 89 mk48txx_attach(bt, bh, model, year0, nvread, nvwrite) 90 bus_space_tag_t bt; 91 bus_space_handle_t bh; 92 const char *model; 93 int year0; 94 mk48txx_nvrd_t nvread; 95 mk48txx_nvwr_t nvwrite; 96 { 97 todr_chip_handle_t handle; 98 struct mk48txx *mk; 99 bus_size_t nvramsz, clkoff; 100 int sz; 101 int i; 102 103 printf(": %s", model); 104 105 i = sizeof(mk48txx_models)/sizeof(mk48txx_models[0]); 106 while (--i >= 0) { 107 if (strcmp(model, mk48txx_models[i].name) == 0) { 108 nvramsz = mk48txx_models[i].nvramsz; 109 clkoff = mk48txx_models[i].clkoff; 110 break; 111 } 112 } 113 if (i < 0) { 114 printf(": unsupported model"); 115 return (NULL); 116 } 117 118 sz = ALIGN(sizeof(struct todr_chip_handle)) + sizeof(struct mk48txx); 119 handle = malloc(sz, M_DEVBUF, M_NOWAIT); 120 mk = (struct mk48txx *)((u_long)handle + 121 ALIGN(sizeof(struct todr_chip_handle))); 122 handle->cookie = mk; 123 handle->todr_gettime = mk48txx_gettime; 124 handle->todr_settime = mk48txx_settime; 125 handle->todr_getcal = mk48txx_getcal; 126 handle->todr_setcal = mk48txx_setcal; 127 handle->todr_setwen = NULL; 128 mk->mk_bt = bt; 129 mk->mk_bh = bh; 130 mk->mk_nvramsz = nvramsz; 131 mk->mk_clkoffset = clkoff; 132 mk->mk_year0 = year0; 133 134 if (nvread == NULL) 135 nvread = mk48txx_def_nvrd; 136 if (nvwrite == NULL) 137 nvwrite = mk48txx_def_nvwr; 138 139 mk->mk_nvrd = nvread; 140 mk->mk_nvwr = nvwrite; 141 142 return (handle); 143 } 144 145 /* 146 * Get time-of-day and convert to a `struct timeval' 147 * Return 0 on success; an error number otherwise. 148 */ 149 int 150 mk48txx_gettime(handle, tv) 151 todr_chip_handle_t handle; 152 struct timeval *tv; 153 { 154 struct mk48txx *mk = handle->cookie; 155 bus_space_tag_t bt = mk->mk_bt; 156 bus_space_handle_t bh = mk->mk_bh; 157 bus_size_t clkoff = mk->mk_clkoffset; 158 struct clock_ymdhms dt; 159 int year; 160 u_int8_t csr; 161 162 todr_wenable(handle, 1); 163 164 /* enable read (stop time) */ 165 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR); 166 csr |= MK48TXX_CSR_READ; 167 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr); 168 169 dt.dt_sec = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ISEC)); 170 dt.dt_min = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IMIN)); 171 dt.dt_hour = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IHOUR)); 172 dt.dt_day = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IDAY)); 173 dt.dt_wday = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IWDAY)); 174 dt.dt_mon = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IMON)); 175 year = FROMBCD((*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_IYEAR)); 176 177 year += mk->mk_year0; 178 if (year < POSIX_BASE_YEAR && mk48txx_auto_century_adjust != 0) 179 year += 100; 180 181 dt.dt_year = year; 182 183 /* time wears on */ 184 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR); 185 csr &= ~MK48TXX_CSR_READ; 186 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr); 187 todr_wenable(handle, 0); 188 189 /* simple sanity checks */ 190 if (dt.dt_mon > 12 || dt.dt_day > 31 || 191 dt.dt_hour >= 24 || dt.dt_min >= 60 || dt.dt_sec >= 60) 192 return (1); 193 194 tv->tv_sec = clock_ymdhms_to_secs(&dt); 195 tv->tv_usec = 0; 196 return (0); 197 } 198 199 /* 200 * Set the time-of-day clock based on the value of the `struct timeval' arg. 201 * Return 0 on success; an error number otherwise. 202 */ 203 int 204 mk48txx_settime(handle, tv) 205 todr_chip_handle_t handle; 206 struct timeval *tv; 207 { 208 struct mk48txx *mk = handle->cookie; 209 bus_space_tag_t bt = mk->mk_bt; 210 bus_space_handle_t bh = mk->mk_bh; 211 bus_size_t clkoff = mk->mk_clkoffset; 212 struct clock_ymdhms dt; 213 u_int8_t csr; 214 int year; 215 216 /* Note: we ignore `tv_usec' */ 217 clock_secs_to_ymdhms(tv->tv_sec, &dt); 218 219 year = dt.dt_year - mk->mk_year0; 220 if (year > 99 && mk48txx_auto_century_adjust != 0) 221 year -= 100; 222 223 todr_wenable(handle, 1); 224 /* enable write */ 225 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR); 226 csr |= MK48TXX_CSR_WRITE; 227 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr); 228 229 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ISEC, TOBCD(dt.dt_sec)); 230 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IMIN, TOBCD(dt.dt_min)); 231 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IHOUR, TOBCD(dt.dt_hour)); 232 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IWDAY, TOBCD(dt.dt_wday)); 233 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IDAY, TOBCD(dt.dt_day)); 234 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IMON, TOBCD(dt.dt_mon)); 235 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_IYEAR, TOBCD(year)); 236 237 /* load them up */ 238 csr = (*mk->mk_nvrd)(bt, bh, clkoff + MK48TXX_ICSR); 239 csr &= ~MK48TXX_CSR_WRITE; 240 (*mk->mk_nvwr)(bt, bh, clkoff + MK48TXX_ICSR, csr); 241 todr_wenable(handle, 0); 242 return (0); 243 } 244 245 int 246 mk48txx_getcal(handle, vp) 247 todr_chip_handle_t handle; 248 int *vp; 249 { 250 return (EOPNOTSUPP); 251 } 252 253 int 254 mk48txx_setcal(handle, v) 255 todr_chip_handle_t handle; 256 int v; 257 { 258 return (EOPNOTSUPP); 259 } 260 261 int 262 mk48txx_get_nvram_size(handle, vp) 263 todr_chip_handle_t handle; 264 bus_size_t *vp; 265 { 266 struct mk48txx *mk = handle->cookie; 267 *vp = mk->mk_nvramsz; 268 return (0); 269 } 270 271 u_int8_t 272 mk48txx_def_nvrd(bus_space_tag_t bt, bus_space_handle_t bh, int off) 273 { 274 275 return (bus_space_read_1(bt, bh, off)); 276 } 277 278 void 279 mk48txx_def_nvwr(bus_space_tag_t bt, bus_space_handle_t bh, int off, u_int8_t v) 280 { 281 282 bus_space_write_1(bt, bh, off, v); 283 } 284