[BACK]Return to clock.c CVS log [TXT][DIR] Up to [local] / sys / arch / hp300 / stand / uboot

Annotation of sys/arch/hp300/stand/uboot/clock.c, Revision 1.1.1.1

1.1       nbrk        1: /*     $OpenBSD: clock.c,v 1.5 2006/08/17 06:31:10 miod Exp $  */
                      2: /*     $NetBSD: clock.c,v 1.3 1995/02/20 00:12:09 mycroft Exp $        */
                      3:
                      4: /*
                      5:  * Copyright (c) 1988 University of Utah.
                      6:  * Copyright (c) 1982, 1990, 1993
                      7:  *     The Regents of the University of California.  All rights reserved.
                      8:  *
                      9:  * This code is derived from software contributed to Berkeley by
                     10:  * the Systems Programming Group of the University of Utah Computer
                     11:  * Science Department.
                     12:  *
                     13:  * Redistribution and use in source and binary forms, with or without
                     14:  * modification, are permitted provided that the following conditions
                     15:  * are met:
                     16:  * 1. Redistributions of source code must retain the above copyright
                     17:  *    notice, this list of conditions and the following disclaimer.
                     18:  * 2. Redistributions in binary form must reproduce the above copyright
                     19:  *    notice, this list of conditions and the following disclaimer in the
                     20:  *    documentation and/or other materials provided with the distribution.
                     21:  * 3. Neither the name of the University nor the names of its contributors
                     22:  *    may be used to endorse or promote products derived from this software
                     23:  *    without specific prior written permission.
                     24:  *
                     25:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     26:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     27:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     28:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     29:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     30:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     31:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     32:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     33:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     34:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     35:  * SUCH DAMAGE.
                     36:  *
                     37:  * from: Utah $Hdr: clock.c 1.18 91/01/21$
                     38:  *
                     39:  *     @(#)clock.c     8.2 (Berkeley) 1/12/94
                     40:  */
                     41:
                     42: #include <sys/param.h>
                     43:
                     44: #include <lib/libsa/stand.h>
                     45:
                     46: #include "samachdep.h"
                     47: #include "hilreg.h"
                     48: #include <hp300/hp300/clockreg.h>
                     49:
                     50: extern void send_hil_cmd(struct hil_dev *, u_char, u_char *, u_char, u_char *);
                     51:
                     52: static int month_days[12] = {
                     53:        31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
                     54: };
                     55:
                     56: u_char bbc_registers[13];
                     57: struct hil_dev *bbcaddr = (struct hil_dev *)BBCADDR;
                     58:
                     59: int    bbc_to_gmt(u_long *);
                     60: void   read_bbc(void);
                     61: u_char read_bbc_reg(int);
                     62:
                     63: time_t
                     64: getsecs()
                     65: {
                     66:        static int bbcinited = 0;
                     67:        u_long timbuf = 0;
                     68:
                     69:        if (!bbc_to_gmt(&timbuf) && !bbcinited)
                     70:                printf("WARNING: bad date in battery clock\n");
                     71:        bbcinited = 1;
                     72:
                     73:        /* Battery clock does not store usec's, so forget about it. */
                     74:        return((time_t)timbuf);
                     75: }
                     76:
                     77: int
                     78: bbc_to_gmt(u_long *timbuf)
                     79: {
                     80:        int i;
                     81:        u_long tmp;
                     82:        int year, month, day, hour, min, sec;
                     83:
                     84:        read_bbc();
                     85:
                     86:        sec = bbc_to_decimal(1, 0);
                     87:        min = bbc_to_decimal(3, 2);
                     88:
                     89:        /*
                     90:         * Hours are different for some reason. Makes no sense really.
                     91:         */
                     92:        hour  = ((bbc_registers[5] & 0x03) * 10) + bbc_registers[4];
                     93:        day   = bbc_to_decimal(8, 7);
                     94:        month = bbc_to_decimal(10, 9);
                     95:        year  = bbc_to_decimal(12, 11) + 1900;
                     96:
                     97:        range_test(hour, 0, 23);
                     98:        range_test(day, 1, 31);
                     99:        range_test(month, 1, 12);
                    100:        range_test(year, STARTOFTIME, 2038);    /* 2038 is the end of time. */
                    101:
                    102:        tmp = 0;
                    103:
                    104:        for (i = STARTOFTIME; i < year; i++)
                    105:                tmp += days_in_year(i);
                    106:        if (leapyear(year) && month > FEBRUARY)
                    107:                tmp++;
                    108:
                    109:        for (i = 1; i < month; i++)
                    110:                tmp += days_in_month(i);
                    111:
                    112:        tmp += (day - 1);
                    113:        tmp = ((tmp * 24 + hour) * 60 + min) * 60 + sec;
                    114:
                    115:        *timbuf = tmp;
                    116:        return(1);
                    117: }
                    118:
                    119: void
                    120: read_bbc()
                    121: {
                    122:        int i, read_okay;
                    123:
                    124:        read_okay = 0;
                    125:        while (!read_okay) {
                    126:                read_okay = 1;
                    127:                for (i = 0; i <= NUM_BBC_REGS; i++)
                    128:                        bbc_registers[i] = read_bbc_reg(i);
                    129:                for (i = 0; i <= NUM_BBC_REGS; i++)
                    130:                        if (bbc_registers[i] != read_bbc_reg(i))
                    131:                                read_okay = 0;
                    132:        }
                    133: }
                    134:
                    135: u_char
                    136: read_bbc_reg(int reg)
                    137: {
                    138:        u_char data = reg;
                    139:
                    140:        if (bbcaddr) {
                    141: #if 0
                    142:                send_hil_cmd(bbcaddr, BBC_SET_REG, &data, 1, NULL);
                    143:                send_hil_cmd(bbcaddr, BBC_READ_REG, NULL, 0, &data);
                    144: #else
                    145:                HILWAIT(bbcaddr);
                    146:                bbcaddr->hil_cmd = BBC_SET_REG;
                    147:                HILWAIT(bbcaddr);
                    148:                bbcaddr->hil_data = data;
                    149:                HILWAIT(bbcaddr);
                    150:                bbcaddr->hil_cmd = BBC_READ_REG;
                    151:                HILDATAWAIT(bbcaddr);
                    152:                data = bbcaddr->hil_data;
                    153: #endif
                    154:        }
                    155:        return(data);
                    156: }

CVSweb