Annotation of sys/arch/mvme68k/dev/nvram.c, Revision 1.1
1.1 ! nbrk 1: /* $OpenBSD: nvram.c,v 1.17 2006/06/21 14:49:33 deraadt Exp $ */
! 2:
! 3: /*
! 4: * Copyright (c) 1995 Theo de Raadt
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: * 1. Redistributions of source code must retain the above copyright
! 10: * notice, this list of conditions and the following disclaimer.
! 11: * 2. Redistributions in binary form must reproduce the above copyright
! 12: * notice, this list of conditions and the following disclaimer in the
! 13: * documentation and/or other materials provided with the distribution.
! 14: *
! 15: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
! 16: * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
! 17: * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
! 18: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
! 19: * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
! 20: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
! 21: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
! 22: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
! 23: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
! 24: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
! 25: * SUCH DAMAGE.
! 26: */
! 27:
! 28: /*
! 29: * 8/22/2000 BH Cleaned up year 2000 problems with calendar hardware.
! 30: * This code will break again in 2068 or so - come dance on my grave.
! 31: */
! 32:
! 33: #include <sys/param.h>
! 34: #include <sys/systm.h>
! 35: #include <sys/kernel.h>
! 36: #include <sys/ioctl.h>
! 37: #include <sys/device.h>
! 38:
! 39: #include <machine/autoconf.h>
! 40: #include <machine/conf.h>
! 41: #include <machine/cpu.h>
! 42: #include <machine/mioctl.h>
! 43: #include <machine/psl.h>
! 44:
! 45: #include <uvm/uvm_extern.h>
! 46:
! 47: #include <mvme68k/dev/memdevs.h>
! 48: #include <mvme68k/dev/nvramreg.h>
! 49:
! 50: #if defined(GPROF)
! 51: #include <sys/gmon.h>
! 52: #endif
! 53:
! 54: struct nvramsoftc {
! 55: struct device sc_dev;
! 56: paddr_t sc_paddr;
! 57: vaddr_t sc_vaddr;
! 58: int sc_len;
! 59: struct clockreg *sc_regs;
! 60: };
! 61:
! 62: void nvramattach(struct device *, struct device *, void *);
! 63: int nvrammatch(struct device *, void *, void *);
! 64:
! 65: struct cfattach nvram_ca = {
! 66: sizeof(struct nvramsoftc), nvrammatch, nvramattach
! 67: };
! 68:
! 69: struct cfdriver nvram_cd = {
! 70: NULL, "nvram", DV_DULL
! 71: };
! 72:
! 73: int
! 74: nvrammatch(parent, vcf, args)
! 75: struct device *parent;
! 76: void *vcf, *args;
! 77: {
! 78: struct confargs *ca = args;
! 79:
! 80: /*X*/ if (ca->ca_vaddr == (vaddr_t)-1)
! 81: /*X*/ return (1);
! 82: return (!badvaddr(ca->ca_vaddr, 1));
! 83: }
! 84:
! 85: void
! 86: nvramattach(parent, self, args)
! 87: struct device *parent, *self;
! 88: void *args;
! 89: {
! 90: struct confargs *ca = args;
! 91: struct nvramsoftc *sc = (struct nvramsoftc *)self;
! 92:
! 93: sc->sc_paddr = ca->ca_paddr;
! 94: sc->sc_vaddr = (vaddr_t)ca->ca_vaddr;
! 95:
! 96: sc->sc_len = MK48T08_SIZE;
! 97: if (cputyp == CPU_147)
! 98: sc->sc_len = MK48T02_SIZE;
! 99:
! 100:
! 101: /*X*/ if (sc->sc_vaddr == -1)
! 102: /*X*/ sc->sc_vaddr = mapiodev(sc->sc_paddr, MAX(sc->sc_len, NBPG));
! 103: /*X*/ if (sc->sc_vaddr == 0)
! 104: /*X*/ panic("failed to map!");
! 105:
! 106: sc->sc_regs = (struct clockreg *)(sc->sc_vaddr + sc->sc_len -
! 107: sizeof(struct clockreg));
! 108:
! 109: printf(": MK48T0%d len %d\n", sc->sc_len / 1024, sc->sc_len);
! 110: }
! 111:
! 112: /*
! 113: * Return the best possible estimate of the time in the timeval
! 114: * to which tvp points. We do this by returning the current time
! 115: * plus the amount of time since the last clock interrupt (clock.c:clkread).
! 116: *
! 117: * Check that this time is no less than any previously-reported time,
! 118: * which could happen around the time of a clock adjustment. Just for fun,
! 119: * we guarantee that the time will be greater than the value obtained by a
! 120: * previous call.
! 121: */
! 122: void
! 123: microtime(tvp)
! 124: register struct timeval *tvp;
! 125: {
! 126: int s = splhigh();
! 127: static struct timeval lasttime;
! 128:
! 129: *tvp = time;
! 130: while (tvp->tv_usec >= 1000000) {
! 131: tvp->tv_sec++;
! 132: tvp->tv_usec -= 1000000;
! 133: }
! 134: if (tvp->tv_sec == lasttime.tv_sec &&
! 135: tvp->tv_usec <= lasttime.tv_usec &&
! 136: (tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000) {
! 137: tvp->tv_sec++;
! 138: tvp->tv_usec -= 1000000;
! 139: }
! 140: lasttime = *tvp;
! 141: splx(s);
! 142: }
! 143:
! 144: /*
! 145: * BCD to decimal and decimal to BCD.
! 146: */
! 147: #define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf))
! 148: #define TOBCD(x) (((x) / 10 * 16) + ((x) % 10))
! 149:
! 150: #define SECYR (SECDAY * 365)
! 151: #define LEAPYEAR(y) (((y) & 3) == 0)
! 152:
! 153: /*
! 154: * This code is defunct after 2068.
! 155: * Will Unix still be here then??
! 156: */
! 157: const short dayyr[12] =
! 158: { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
! 159:
! 160: struct chiptime {
! 161: int sec;
! 162: int min;
! 163: int hour;
! 164: int wday;
! 165: int day;
! 166: int mon;
! 167: int year;
! 168: };
! 169:
! 170: u_long chiptotime(int, int, int, int, int, int);
! 171: void timetochip(struct chiptime *);
! 172:
! 173: u_long
! 174: chiptotime(sec, min, hour, day, mon, year)
! 175: register int sec, min, hour, day, mon, year;
! 176: {
! 177: register int days, yr;
! 178:
! 179: sec = FROMBCD(sec);
! 180: min = FROMBCD(min);
! 181: hour = FROMBCD(hour);
! 182: day = FROMBCD(day);
! 183: mon = FROMBCD(mon);
! 184: year = FROMBCD(year) + YEAR0;
! 185:
! 186: /* simple sanity checks */
! 187: if (year>164 || mon<1 || mon>12 || day<1 || day>31)
! 188: return (0);
! 189: yr = 70;
! 190: days = 0;
! 191:
! 192: if (year < 70) { /* 2000 <= year */
! 193: for (; yr < 100; yr++) /* deal with first 30 years */
! 194: days += LEAPYEAR(yr) ? 366 : 365;
! 195: yr = 0;
! 196: }
! 197:
! 198: for (; yr < year; yr++) /* deal with years left */
! 199: days += LEAPYEAR(yr) ? 366 : 365;
! 200:
! 201: days += dayyr[mon - 1] + day - 1;
! 202:
! 203: if (LEAPYEAR(yr) && mon > 2)
! 204: days++;
! 205:
! 206: /* now have days since Jan 1, 1970; the rest is easy... */
! 207: return (days * SECDAY + hour * 3600 + min * 60 + sec);
! 208: }
! 209:
! 210: void
! 211: timetochip(c)
! 212: struct chiptime *c;
! 213: {
! 214: int t, t2, t3, now = time.tv_sec;
! 215:
! 216: /* January 1 1970 was a Thursday (4 in unix wdays) */
! 217: /* compute the days since the epoch */
! 218: t2 = now / SECDAY;
! 219:
! 220: t3 = (t2 + 4) % 7; /* day of week */
! 221: c->wday = TOBCD(t3 + 1);
! 222:
! 223: /* compute the year */
! 224: t = 69;
! 225: while (t2 >= 0) { /* whittle off years */
! 226: t3 = t2;
! 227: t++;
! 228: t2 -= LEAPYEAR(t) ? 366 : 365;
! 229: }
! 230: c->year = t;
! 231:
! 232: /* t3 = month + day; separate */
! 233: t = LEAPYEAR(t);
! 234: for (t2 = 1; t2 < 12; t2++)
! 235: if (t3 < (dayyr[t2] + ((t && (t2 > 1)) ? 1:0)))
! 236: break;
! 237:
! 238: /* t2 is month */
! 239: c->mon = t2;
! 240: c->day = t3 - dayyr[t2 - 1] + 1;
! 241: if (t && t2 > 2)
! 242: c->day--;
! 243:
! 244: /* the rest is easy */
! 245: t = now % SECDAY;
! 246: c->hour = t / 3600;
! 247: t %= 3600;
! 248: c->min = t / 60;
! 249: c->sec = t % 60;
! 250:
! 251: c->sec = TOBCD(c->sec);
! 252: c->min = TOBCD(c->min);
! 253: c->hour = TOBCD(c->hour);
! 254: c->day = TOBCD(c->day);
! 255: c->mon = TOBCD(c->mon);
! 256: c->year = TOBCD((c->year - YEAR0) % 100);
! 257: }
! 258:
! 259: /*
! 260: * Set up the system's time, given a `reasonable' time value.
! 261: */
! 262: void
! 263: inittodr(base)
! 264: time_t base;
! 265: {
! 266: struct nvramsoftc *sc = (struct nvramsoftc *) nvram_cd.cd_devs[0];
! 267: register struct clockreg *cl = sc->sc_regs;
! 268: int sec, min, hour, day, mon, year;
! 269: int badbase = 0, waszero = base == 0;
! 270:
! 271: if (base < 5 * SECYR) {
! 272: /*
! 273: * If base is 0, assume filesystem time is just unknown
! 274: * instead of preposterous. Don't bark.
! 275: */
! 276: if (base != 0)
! 277: printf("WARNING: preposterous time in file system\n");
! 278: /* not going to use it anyway, if the chip is readable */
! 279: base = 21*SECYR + 186*SECDAY + SECDAY/2;
! 280: badbase = 1;
! 281: }
! 282: cl->cl_csr |= CLK_READ; /* enable read (stop time) */
! 283: sec = cl->cl_sec;
! 284: min = cl->cl_min;
! 285: hour = cl->cl_hour;
! 286: day = cl->cl_mday;
! 287: mon = cl->cl_month;
! 288: year = cl->cl_year;
! 289: cl->cl_csr &= ~CLK_READ; /* time wears on */
! 290: if ((time.tv_sec = chiptotime(sec, min, hour, day, mon, year)) == 0) {
! 291: printf("WARNING: bad date in nvram");
! 292: /*
! 293: * Believe the time in the file system for lack of
! 294: * anything better, resetting the clock.
! 295: */
! 296: time.tv_sec = base;
! 297: if (!badbase)
! 298: resettodr();
! 299: } else {
! 300: int deltat = time.tv_sec - base;
! 301:
! 302: if (deltat < 0)
! 303: deltat = -deltat;
! 304: if (waszero || deltat < 2 * SECDAY)
! 305: return;
! 306: printf("WARNING: clock %s %d days",
! 307: time.tv_sec < base ? "lost" : "gained", deltat / SECDAY);
! 308: }
! 309: printf(" -- CHECK AND RESET THE DATE!\n");
! 310: }
! 311:
! 312: /*
! 313: * Reset the clock based on the current time.
! 314: * Used when the current clock is preposterous, when the time is changed,
! 315: * and when rebooting. Do nothing if the time is not yet known, e.g.,
! 316: * when crashing during autoconfig.
! 317: */
! 318: void
! 319: resettodr()
! 320: {
! 321: struct nvramsoftc *sc = (struct nvramsoftc *) nvram_cd.cd_devs[0];
! 322: register struct clockreg *cl = sc->sc_regs;
! 323: struct chiptime c;
! 324:
! 325: if (!time.tv_sec || cl == NULL)
! 326: return;
! 327: timetochip(&c);
! 328: cl->cl_csr |= CLK_WRITE; /* enable write */
! 329: cl->cl_sec = c.sec;
! 330: cl->cl_min = c.min;
! 331: cl->cl_hour = c.hour;
! 332: cl->cl_wday = c.wday;
! 333: cl->cl_mday = c.day;
! 334: cl->cl_month = c.mon;
! 335: cl->cl_year = c.year;
! 336: cl->cl_csr &= ~CLK_WRITE; /* load them up */
! 337: }
! 338:
! 339: /*ARGSUSED*/
! 340: int
! 341: nvramopen(dev, flag, mode, p)
! 342: dev_t dev;
! 343: int flag, mode;
! 344: struct proc *p;
! 345: {
! 346: if (minor(dev) >= nvram_cd.cd_ndevs ||
! 347: nvram_cd.cd_devs[minor(dev)] == NULL)
! 348: return (ENODEV);
! 349: return (0);
! 350: }
! 351:
! 352: /*ARGSUSED*/
! 353: int
! 354: nvramclose(dev, flag, mode, p)
! 355: dev_t dev;
! 356: int flag, mode;
! 357: struct proc *p;
! 358: {
! 359:
! 360: return (0);
! 361: }
! 362:
! 363: /*ARGSUSED*/
! 364: int
! 365: nvramioctl(dev, cmd, data, flag, p)
! 366: dev_t dev;
! 367: u_long cmd;
! 368: caddr_t data;
! 369: int flag;
! 370: struct proc *p;
! 371: {
! 372: int unit = minor(dev);
! 373: struct nvramsoftc *sc = (struct nvramsoftc *) nvram_cd.cd_devs[unit];
! 374: int error = 0;
! 375:
! 376: switch (cmd) {
! 377: case MIOCGSIZ:
! 378: *(int *)data = sc->sc_len;
! 379: break;
! 380: default:
! 381: error = ENOTTY;
! 382: break;
! 383: }
! 384: return (error);
! 385: }
! 386:
! 387: /*ARGSUSED*/
! 388: int
! 389: nvramrw(dev, uio, flags)
! 390: dev_t dev;
! 391: struct uio *uio;
! 392: int flags;
! 393: {
! 394: int unit = minor(dev);
! 395: struct nvramsoftc *sc = (struct nvramsoftc *) nvram_cd.cd_devs[unit];
! 396:
! 397: return (memdevrw(sc->sc_vaddr, sc->sc_len, uio, flags));
! 398: }
! 399:
! 400: /*
! 401: * If the NVRAM is of the 2K variety, an extra 2K of who-knows-what
! 402: * will also be mmap'd, due to NBPG being 4K. On the MVME147 the NVRAM
! 403: * repeats, so userland gets two copies back-to-back.
! 404: */
! 405: paddr_t
! 406: nvrammmap(dev, off, prot)
! 407: dev_t dev;
! 408: off_t off;
! 409: int prot;
! 410: {
! 411: int unit = minor(dev);
! 412: struct nvramsoftc *sc = (struct nvramsoftc *) nvram_cd.cd_devs[unit];
! 413:
! 414: if (minor(dev) != 0)
! 415: return (-1);
! 416:
! 417: /* allow access only in RAM */
! 418: if (off < 0 || off > sc->sc_len)
! 419: return (-1);
! 420: return (atop(sc->sc_paddr + off));
! 421: }
CVSweb