Home | History | Annotate | Line # | Download | only in obio
mkclock.c revision 1.1
      1 /*	$NetBSD: mkclock.c,v 1.1 2000/08/12 22:58:57 wdk Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Wayne Knowles
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/kernel.h>
     41 #include <sys/device.h>
     42 #include <sys/systm.h>
     43 
     44 #include <dev/clock_subr.h>
     45 
     46 #include <machine/cpu.h>
     47 #include <machine/mainboard.h>
     48 #include <machine/autoconf.h>
     49 #include <machine/sysconf.h>
     50 #include <machine/bus.h>
     51 
     52 #include <mipsco/obio/clockreg.h>
     53 
     54 struct	mkclock_softc {
     55         struct  device dev;
     56 	bus_space_tag_t	sc_bst;
     57 	bus_space_handle_t sc_bsh;
     58 };
     59 
     60 static int mkclock_match __P((struct device *, struct cfdata *, void *));
     61 static void mkclock_attach __P((struct device *, struct device *, void *));
     62 
     63 struct cfattach mkclock_ca = {
     64 	sizeof(struct mkclock_softc), mkclock_match, mkclock_attach
     65 };
     66 
     67 static struct mkclock_softc *mk0;
     68 
     69 void mkclock_read __P((struct clock_ymdhms *));
     70 void mkclock_write __P((struct clock_ymdhms *));
     71 
     72 int
     73 mkclock_match(parent, cf, aux)
     74 	struct device *parent;
     75 	struct cfdata *cf;
     76 	void *aux;
     77 {
     78 	return 1;
     79 }
     80 
     81 void
     82 mkclock_attach(parent, self, aux)
     83 	struct device *parent, *self;
     84 	void *aux;
     85 {
     86         struct mkclock_softc *sc = (void *)self;
     87 	struct confargs *ca = aux;
     88 
     89 	sc->sc_bst = ca->ca_bustag;
     90 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
     91 			  0x10000,
     92 			  BUS_SPACE_MAP_LINEAR,
     93 			  &sc->sc_bsh) != 0) {
     94 		printf(": cannot map registers\n");
     95 		return;
     96 	}
     97 
     98 	platform.write_todr = mkclock_write;
     99 	platform.read_todr  = mkclock_read;
    100 	mk0 = sc;
    101 	printf("\n");
    102 }
    103 
    104 static int
    105 mk_read(sc, reg)
    106 	struct mkclock_softc *sc;
    107 	int reg;
    108 {
    109 	u_int8_t val;
    110 
    111 	val = bus_space_read_1(sc->sc_bst, sc->sc_bsh, DATA_PORT + reg*4);
    112 	return FROMBCD(val);
    113 }
    114 
    115 static void
    116 mk_write(sc, reg, val)
    117 	struct mkclock_softc *sc;
    118 	int reg, val;
    119 {
    120 	bus_space_write_1(sc->sc_bst, sc->sc_bsh,
    121 			  DATA_PORT + reg*4, TOBCD(val));
    122 }
    123 
    124 void
    125 mkclock_read(dt)
    126 	struct clock_ymdhms *dt;
    127 {
    128 	struct mkclock_softc *sc = mk0;
    129 	int s = splclock();
    130 
    131 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, READ_CLOCK);
    132 	dt->dt_sec  = mk_read(sc, 0);
    133 	dt->dt_min  = mk_read(sc, 1);
    134 	dt->dt_hour = mk_read(sc, 2);
    135 	dt->dt_wday = mk_read(sc, 3);
    136 	dt->dt_day  = mk_read(sc, 4);
    137 	dt->dt_mon  = mk_read(sc, 5);
    138 	dt->dt_year = mk_read(sc, 6);
    139 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
    140 	splx(s);
    141 
    142 	dt->dt_year = dt->dt_year + (dt->dt_year >= 70 ? 1900 : 2000);
    143 }
    144 
    145 void
    146 mkclock_write(dt)
    147 	struct clock_ymdhms *dt;
    148 {
    149 	struct mkclock_softc *sc = mk0;
    150 	int year, s;
    151 
    152 	year = dt->dt_year % 100;
    153 
    154 	s = splclock();
    155 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, SET_CLOCK);
    156 	mk_write(sc, 0, dt->dt_sec);
    157 	mk_write(sc, 1, dt->dt_min);
    158 	mk_write(sc, 2, dt->dt_hour);
    159 	/* week day not set */
    160 	mk_write(sc, 4, dt->dt_day);
    161 	mk_write(sc, 5, dt->dt_mon);
    162 	mk_write(sc, 6, year);
    163 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_PORT, 0);
    164 	splx(s);
    165 }
    166