Home | History | Annotate | Line # | Download | only in tsarm
tsrtc.c revision 1.1
      1 /*	$NetBSD: tsrtc.c,v 1.1 2004/12/27 02:41:54 joff Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
      5  * All rights reserved.
      6  *
      7  * Author: Chris G. Demetriou
      8  *
      9  * Permission to use, copy, modify and distribute this software and
     10  * its documentation is hereby granted, provided that both the copyright
     11  * notice and this permission notice appear in all copies of the
     12  * software, derivative works or modified versions, and any portions
     13  * thereof, and that both notices appear in supporting documentation.
     14  *
     15  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     16  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     17  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     18  *
     19  * Carnegie Mellon requests users of this software to return to
     20  *
     21  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     22  *  School of Computer Science
     23  *  Carnegie Mellon University
     24  *  Pittsburgh PA 15213-3890
     25  *
     26  * any improvements or extensions that they make and grant Carnegie the
     27  * rights to redistribute these changes.
     28  */
     29 
     30 #include <sys/cdefs.h>
     31 __KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.1 2004/12/27 02:41:54 joff Exp $");
     32 
     33 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     34 
     35 #include <sys/param.h>
     36 #include <sys/kernel.h>
     37 #include <sys/systm.h>
     38 #include <sys/device.h>
     39 
     40 #include <machine/bus.h>
     41 
     42 #include <dev/clock_subr.h>
     43 #include <dev/ic/mc146818reg.h>
     44 #include <dev/ic/mc146818var.h>
     45 
     46 #include <evbarm/tsarm/tspldvar.h>
     47 #include <evbarm/tsarm/tsarmreg.h>
     48 
     49 int	tsrtc_match __P((struct device *, struct cfdata *, void *));
     50 void	tsrtc_attach __P((struct device *, struct device *, void *));
     51 
     52 struct tsrtc_softc {
     53 	struct mc146818_softc	sc_mc;
     54 	bus_space_tag_t		sc_iot;
     55 	bus_space_handle_t	sc_dath;
     56 	bus_space_handle_t	sc_idxh;
     57 };
     58 
     59 CFATTACH_DECL(tsrtc, sizeof (struct tsrtc_softc),
     60     tsrtc_match, tsrtc_attach, NULL, NULL);
     61 
     62 void	tsrtc_write __P((struct mc146818_softc *, u_int, u_int));
     63 u_int	tsrtc_read __P((struct mc146818_softc *, u_int));
     64 
     65 
     66 int
     67 tsrtc_match(parent, match, aux)
     68 	struct device *parent;
     69 	struct cfdata *match;
     70 	void *aux;
     71 {
     72 	struct tspld_attach_args *aa = aux;
     73 	struct tsrtc_softc tsrtc, *sc;
     74 	unsigned int t1, t2;
     75 	static int found = -1;
     76 
     77 	if (found != -1) return found;
     78 
     79 	sc = &tsrtc;
     80 	sc->sc_iot = aa->ta_iot;
     81 	if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0,
     82 		 &sc->sc_idxh))
     83 		return (0);
     84 	if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0,
     85 		 &sc->sc_dath))
     86 		return (0);
     87 
     88 	/* Read from the seconds counter. */
     89 	t1 = FROMBCD(tsrtc_read((struct mc146818_softc *)sc, MC_SEC));
     90 	if (t1 > 59)
     91 		goto unmap;
     92 
     93 	/* Wait, then look again. */
     94 	DELAY(1100000);
     95 	t2 = FROMBCD(tsrtc_read((struct mc146818_softc *)sc, MC_SEC));
     96 	if (t2 > 59)
     97 		goto unmap;
     98 
     99         /* If [1,2) seconds have passed since, call it a clock. */
    100 	if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
    101 		found = 1;
    102 
    103  unmap:
    104 	bus_space_unmap(sc->sc_iot, sc->sc_idxh, 1);
    105 	bus_space_unmap(sc->sc_iot, sc->sc_dath, 1);
    106 
    107 	return (found);
    108 }
    109 
    110 void
    111 tsrtc_attach(parent, self, aux)
    112 	struct device *parent, *self;
    113 	void *aux;
    114 {
    115 	struct tsrtc_softc *sc = (void *)self;
    116 	struct tspld_attach_args *aa = aux;
    117 
    118 	sc->sc_iot = aa->ta_iot;
    119 	if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX,
    120 	    1, 0, &sc->sc_idxh))
    121 		panic("tsrtc_attach: couldn't map clock I/O space");
    122 	if (bus_space_map(sc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT,
    123 	    1, 0, &sc->sc_dath))
    124 		panic("tsrtc_attach: couldn't map clock I/O space");
    125 
    126 	sc->sc_mc.sc_year0 = 2000;
    127 	sc->sc_mc.sc_mcread = tsrtc_read;
    128 	sc->sc_mc.sc_mcwrite = tsrtc_write;
    129 	sc->sc_mc.sc_flag = MC146818_BCD;
    130 	mc146818_attach(&sc->sc_mc);
    131 
    132 	printf("\n");
    133 
    134 	(*sc->sc_mc.sc_mcwrite)((struct mc146818_softc *)sc, MC_REGB,
    135 		MC_REGB_24HR);
    136 
    137 	todr_attach(&sc->sc_mc.sc_handle);
    138 }
    139 
    140 void
    141 tsrtc_write(mc_sc, reg, datum)
    142 	struct mc146818_softc *mc_sc;
    143 	u_int reg, datum;
    144 {
    145 	struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
    146 
    147 	bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
    148 	bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum);
    149 }
    150 
    151 u_int
    152 tsrtc_read(mc_sc, reg)
    153 	struct mc146818_softc *mc_sc;
    154 	u_int reg;
    155 {
    156 	struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
    157 	u_int datum;
    158 
    159 	bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
    160 	datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0);
    161 
    162 	return (datum);
    163 }
    164