Home | History | Annotate | Line # | Download | only in tsarm
tsrtc.c revision 1.3.6.2
      1  1.3.6.1   mjf /*	$NetBSD: tsrtc.c,v 1.3.6.2 2008/06/02 13:22:03 mjf 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.3.6.1   mjf __KERNEL_RCSID(0, "$NetBSD: tsrtc.c,v 1.3.6.2 2008/06/02 13:22:03 mjf 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.3.6.1   mjf int	tsrtc_match(device_t, cfdata_t, void *);
     50  1.3.6.1   mjf 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.3.6.1   mjf CFATTACH_DECL_NEW(tsrtc, sizeof (struct tsrtc_softc),
     60      1.1  joff     tsrtc_match, tsrtc_attach, NULL, NULL);
     61      1.1  joff 
     62  1.3.6.1   mjf void	tsrtc_write(struct mc146818_softc *, u_int, u_int);
     63  1.3.6.1   mjf u_int	tsrtc_read(struct mc146818_softc *, u_int);
     64      1.1  joff 
     65      1.1  joff 
     66      1.1  joff int
     67  1.3.6.1   mjf 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.3.6.2   mjf 	struct tsrtc_softc tsrtc, *tsc;
     71  1.3.6.2   mjf 	struct mc146818_softc *sc;
     72      1.1  joff 	unsigned int t1, t2;
     73      1.1  joff 	static int found = -1;
     74      1.1  joff 
     75  1.3.6.1   mjf 	if (found != -1)
     76  1.3.6.1   mjf 		return found;
     77      1.1  joff 
     78  1.3.6.2   mjf 	tsc = &tsrtc;
     79  1.3.6.2   mjf 	sc = &tsc->sc_mc;
     80  1.3.6.2   mjf 	tsc->sc_iot = aa->ta_iot;
     81  1.3.6.2   mjf 	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX, 1, 0,
     82  1.3.6.2   mjf 		 &tsc->sc_idxh))
     83      1.1  joff 		return (0);
     84  1.3.6.2   mjf 	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT, 1, 0,
     85  1.3.6.2   mjf 		 &tsc->sc_dath))
     86      1.1  joff 		return (0);
     87      1.1  joff 
     88      1.1  joff 	/* Read from the seconds counter. */
     89  1.3.6.2   mjf 	t1 = FROMBCD(tsrtc_read(sc, MC_SEC));
     90      1.1  joff 	if (t1 > 59)
     91      1.1  joff 		goto unmap;
     92      1.1  joff 
     93      1.1  joff 	/* Wait, then look again. */
     94      1.1  joff 	DELAY(1100000);
     95  1.3.6.2   mjf 	t2 = FROMBCD(tsrtc_read(sc, MC_SEC));
     96      1.1  joff 	if (t2 > 59)
     97      1.1  joff 		goto unmap;
     98      1.1  joff 
     99      1.1  joff         /* If [1,2) seconds have passed since, call it a clock. */
    100      1.1  joff 	if ((t1 + 1) % 60 == t2 || (t1 + 2) % 60 == t2)
    101      1.1  joff 		found = 1;
    102      1.1  joff 
    103      1.1  joff  unmap:
    104  1.3.6.2   mjf 	bus_space_unmap(tsc->sc_iot, tsc->sc_idxh, 1);
    105  1.3.6.2   mjf 	bus_space_unmap(tsc->sc_iot, tsc->sc_dath, 1);
    106      1.1  joff 
    107      1.1  joff 	return (found);
    108      1.1  joff }
    109      1.1  joff 
    110      1.1  joff void
    111  1.3.6.1   mjf tsrtc_attach(device_t parent, device_t self, void *aux)
    112      1.1  joff {
    113  1.3.6.2   mjf 	struct tsrtc_softc *tsc = device_private(self);
    114  1.3.6.2   mjf 	struct mc146818_softc *sc = &tsc->sc_mc;
    115      1.1  joff 	struct tspld_attach_args *aa = aux;
    116      1.1  joff 
    117  1.3.6.1   mjf 	sc->sc_dev = self;
    118  1.3.6.2   mjf 	tsc->sc_iot = aa->ta_iot;
    119  1.3.6.2   mjf 	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCIDX,
    120  1.3.6.2   mjf 	    1, 0, &tsc->sc_idxh))
    121      1.1  joff 		panic("tsrtc_attach: couldn't map clock I/O space");
    122  1.3.6.2   mjf 	if (bus_space_map(tsc->sc_iot, TS7XXX_IO8_HWBASE + TS7XXX_RTCDAT,
    123  1.3.6.2   mjf 	    1, 0, &tsc->sc_dath))
    124      1.1  joff 		panic("tsrtc_attach: couldn't map clock I/O space");
    125      1.1  joff 
    126  1.3.6.2   mjf 	sc->sc_year0 = 2000;
    127  1.3.6.2   mjf 	sc->sc_mcread = tsrtc_read;
    128  1.3.6.2   mjf 	sc->sc_mcwrite = tsrtc_write;
    129  1.3.6.2   mjf 	sc->sc_flag = MC146818_BCD;
    130  1.3.6.2   mjf 	mc146818_attach(sc);
    131      1.1  joff 
    132  1.3.6.2   mjf 	aprint_normal("\n");
    133      1.1  joff 
    134  1.3.6.2   mjf 	(*sc->sc_mcwrite)(sc, MC_REGB, MC_REGB_24HR);
    135      1.1  joff }
    136      1.1  joff 
    137      1.1  joff void
    138  1.3.6.1   mjf tsrtc_write(struct mc146818_softc *mc_sc, u_int reg, u_int datum)
    139      1.1  joff {
    140      1.1  joff 	struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
    141      1.1  joff 
    142      1.1  joff 	bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
    143      1.1  joff 	bus_space_write_1(sc->sc_iot, sc->sc_dath, 0, datum);
    144      1.1  joff }
    145      1.1  joff 
    146      1.1  joff u_int
    147  1.3.6.1   mjf tsrtc_read(struct mc146818_softc *mc_sc, u_int reg)
    148      1.1  joff {
    149      1.1  joff 	struct tsrtc_softc *sc = (struct tsrtc_softc *)mc_sc;
    150      1.1  joff 	u_int datum;
    151      1.1  joff 
    152      1.1  joff 	bus_space_write_1(sc->sc_iot, sc->sc_idxh, 0, reg);
    153      1.1  joff 	datum = bus_space_read_1(sc->sc_iot, sc->sc_dath, 0);
    154      1.1  joff 
    155      1.1  joff 	return (datum);
    156      1.1  joff }
    157