diff options
author | Miod Vallat <miod@cvs.openbsd.org> | 2013-05-18 10:21:51 +0000 |
---|---|---|
committer | Miod Vallat <miod@cvs.openbsd.org> | 2013-05-18 10:21:51 +0000 |
commit | b2d3261255c0fbe10b4d5c2b79a46f1bd69f8694 (patch) | |
tree | 9f0763a1739a0ac5a871187e5319153c8149f335 /sys/arch/mvme88k | |
parent | b2827e96e46dffafad43bb4aaddff1140d390d2f (diff) |
Missing bits intended to go with the previous commit but lost in diff splitting:
Split inittodr/resettodr code from actual tod routines, to allow for
multiple tod routines in the near future.
Use the mi clock_ymdhms_to_secs and clock_secs_to_ymdhms routines.
Diffstat (limited to 'sys/arch/mvme88k')
-rw-r--r-- | sys/arch/mvme88k/dev/nvram.c | 194 |
1 files changed, 48 insertions, 146 deletions
diff --git a/sys/arch/mvme88k/dev/nvram.c b/sys/arch/mvme88k/dev/nvram.c index 7d220d5268f..41e0310cc1b 100644 --- a/sys/arch/mvme88k/dev/nvram.c +++ b/sys/arch/mvme88k/dev/nvram.c @@ -1,4 +1,4 @@ -/* $OpenBSD: nvram.c,v 1.34 2013/05/17 22:38:25 miod Exp $ */ +/* $OpenBSD: nvram.c,v 1.35 2013/05/18 10:21:50 miod Exp $ */ /* * Copyright (c) 1995 Theo de Raadt @@ -132,185 +132,87 @@ nvramattach(parent, self, args) sc->sc_ioh = ioh; printf(": MK48T0%lu\n", sc->sc_len / 1024); -} - -/* - * BCD to decimal and decimal to BCD. - */ -#define FROMBCD(x) (((x) >> 4) * 10 + ((x) & 0xf)) -#define TOBCD(x) (((x) / 10 * 16) + ((x) % 10)) - -#define SECYR (SECDAY * 365) -#define LEAPYEAR(y) (((y) & 3) == 0) -/* - * This code is defunct after 2068. - * Will Unix still be here then?? - */ -const int dayyr[12] = -{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334 }; + md_inittodr = nvram_inittodr; + md_resettodr = nvram_resettodr; +} -u_long -chiptotime(sec, min, hour, day, mon, year) - int sec, min, hour, day, mon, year; +time_t +chiptotime(struct clock_ymdhms *c) { - int days, yr; - - sec = FROMBCD(sec); - min = FROMBCD(min); - hour = FROMBCD(hour); - day = FROMBCD(day); - mon = FROMBCD(mon); - year = FROMBCD(year) + YEAR0; - - /* simple sanity checks */ - if (year>164 || mon<1 || mon>12 || day<1 || day>31) - return (0); - yr = 70; - days = 0; - - if (year < 70) { /* 2000 <= year */ - for (; yr < 100; yr++) /* deal with first 30 years */ - days += LEAPYEAR(yr) ? 366 : 365; - yr = 0; - } - - for (; yr < year; yr++) /* deal with years left */ - days += LEAPYEAR(yr) ? 366 : 365; - - days += dayyr[mon - 1] + day - 1; - - if (LEAPYEAR(yr) && mon > 2) - days++; - - /* now have days since Jan 1, 1970; the rest is easy... */ - return (days * SECDAY + hour * 3600 + min * 60 + sec); + c->dt_sec = FROMBCD(c->dt_sec); + c->dt_min = FROMBCD(c->dt_min); + c->dt_hour = FROMBCD(c->dt_hour); + c->dt_day = FROMBCD(c->dt_day); + c->dt_mon = FROMBCD(c->dt_mon); + c->dt_year = FROMBCD(c->dt_year) + YEAR0; + /* XXX 2-digit year => Y2070 problem */ + if (c->dt_year < POSIX_BASE_YEAR) + c->dt_year += 100; + + return clock_ymdhms_to_secs(c); } -struct chiptime { - int sec; - int min; - int hour; - int wday; - int day; - int mon; - int year; -}; - -void timetochip(struct chiptime *c); - void -timetochip(c) - struct chiptime *c; +timetochip(struct clock_ymdhms *c) { - time_t t, t2, t3, now = time_second; - - /* January 1 1970 was a Thursday (4 in unix wdays) */ - /* compute the days since the epoch */ - t2 = now / SECDAY; - - t3 = (t2 + 4) % 7; /* day of week */ - c->wday = TOBCD(t3 + 1); - - /* compute the year */ - t = 69; - while (t2 >= 0) { /* whittle off years */ - t3 = t2; - t++; - t2 -= LEAPYEAR(t) ? 366 : 365; - } - c->year = t; - - /* t3 = month + day; separate */ - t = LEAPYEAR(t); - for (t2 = 1; t2 < 12; t2++) - if (t3 < (dayyr[t2] + ((t && (t2 > 1)) ? 1:0))) - break; - - /* t2 is month */ - c->mon = t2; - c->day = t3 - dayyr[t2 - 1] + 1; - if (t && t2 > 2) - c->day--; - - /* the rest is easy */ - t = now % SECDAY; - c->hour = t / 3600; - t %= 3600; - c->min = t / 60; - c->sec = t % 60; - - c->sec = TOBCD(c->sec); - c->min = TOBCD(c->min); - c->hour = TOBCD(c->hour); - c->day = TOBCD(c->day); - c->mon = TOBCD(c->mon); - c->year = TOBCD((c->year - YEAR0) % 100); + clock_secs_to_ymdhms(time_second, c); + + c->dt_sec = TOBCD(c->dt_sec); + c->dt_min = TOBCD(c->dt_min); + c->dt_hour = TOBCD(c->dt_hour); + c->dt_day = TOBCD(c->dt_day); + c->dt_mon = TOBCD(c->dt_mon); + c->dt_year = TOBCD(c->dt_year % 100); } -/* - * Set up the system's time, given a `reasonable' time value. - */ - -void -inittodr(time_t base) +time_t +nvram_inittodr() { - struct nvramsoftc *sc = (struct nvramsoftc *) nvram_cd.cd_devs[0]; - int sec, min, hour, day, mon, year; - int badbase = 0, waszero = base == 0; - struct timespec ts; - - ts.tv_sec = ts.tv_nsec = 0; - - if (base < 35 * SECYR) { - /* - * If base is 0, assume filesystem time is just unknown - * in stead of preposterous. Don't bark. - */ - if (base != 0) - printf("WARNING: preposterous time in file system\n"); - /* not going to use it anyway, if the chip is readable */ - base = 39 * SECYR; - badbase = 1; - } + struct nvramsoftc *sc = nvram_cd.cd_devs[0]; + struct clock_ymdhms c; - if (brdtyp == BRD_188) { + switch (brdtyp) { +#ifdef MVME188 + case BRD_188: bus_space_write_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_CSR << 2), CLK_READ | bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_CSR << 2))); - sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, + c.dt_sec = bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_SEC << 2)) & 0xff; - min = bus_space_read_4(sc->sc_iot, sc->sc_ioh, + c.dt_min = bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_MIN << 2)) & 0xff; - hour = bus_space_read_4(sc->sc_iot, sc->sc_ioh, + c.dt_hour = bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_HOUR << 2)) & 0xff; - day = bus_space_read_4(sc->sc_iot, sc->sc_ioh, + c.dt_day = bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_DAY << 2)) & 0xff; - mon = bus_space_read_4(sc->sc_iot, sc->sc_ioh, + c.dt_mon = bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_MONTH << 2)) & 0xff; - year = bus_space_read_4(sc->sc_iot, sc->sc_ioh, + c.dt_year = bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_YEAR << 2)) & 0xff; bus_space_write_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_CSR << 2), bus_space_read_4(sc->sc_iot, sc->sc_ioh, sc->sc_regs + (CLK_CSR << 2)) & ~CLK_READ); - } else { + break; +#endif + default: bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_CSR, CLK_READ | bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_CSR)); - sec = bus_space_read_1(sc->sc_iot, sc->sc_ioh, + c.dt_sec = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_SEC); - min = bus_space_read_1(sc->sc_iot, sc->sc_ioh, + c.dt_min = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_MIN); - hour = bus_space_read_1(sc->sc_iot, sc->sc_ioh, + c.dt_hour = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_HOUR); - day = bus_space_read_1(sc->sc_iot, sc->sc_ioh, + c.dt_day = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_DAY); - mon = bus_space_read_1(sc->sc_iot, sc->sc_ioh, + c.dt_mon = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_MONTH); - year = bus_space_read_1(sc->sc_iot, sc->sc_ioh, + c.dt_year = bus_space_read_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_YEAR); bus_space_write_1(sc->sc_iot, sc->sc_ioh, sc->sc_regs + CLK_CSR, |