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