Home | History | Annotate | Line # | Download | only in dev
rtc.c revision 1.8
      1  1.8   dyoung /*	$NetBSD: rtc.c,v 1.8 2011/07/01 18:48:37 dyoung Exp $	*/
      2  1.1      mrg 
      3  1.1      mrg /*
      4  1.1      mrg  * Copyright (c) 1992, 1993
      5  1.1      mrg  *	The Regents of the University of California.  All rights reserved.
      6  1.1      mrg  * Copyright (c) 1994 Gordon W. Ross
      7  1.1      mrg  * Copyright (c) 1993 Adam Glass
      8  1.1      mrg  * Copyright (c) 1996 Paul Kranenburg
      9  1.1      mrg  * Copyright (c) 1996
     10  1.1      mrg  * 	The President and Fellows of Harvard College. All rights reserved.
     11  1.1      mrg  *
     12  1.1      mrg  * This software was developed by the Computer Systems Engineering group
     13  1.1      mrg  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
     14  1.1      mrg  * contributed to Berkeley.
     15  1.1      mrg  *
     16  1.1      mrg  * All advertising materials mentioning features or use of this software
     17  1.1      mrg  * must display the following acknowledgement:
     18  1.1      mrg  *	This product includes software developed by Harvard University.
     19  1.1      mrg  *	This product includes software developed by the University of
     20  1.1      mrg  *	California, Lawrence Berkeley Laboratory.
     21  1.1      mrg  *
     22  1.1      mrg  * Redistribution and use in source and binary forms, with or without
     23  1.1      mrg  * modification, are permitted provided that the following conditions
     24  1.1      mrg  * are met:
     25  1.1      mrg  *
     26  1.1      mrg  * 1. Redistributions of source code must retain the above copyright
     27  1.1      mrg  *    notice, this list of conditions and the following disclaimer.
     28  1.1      mrg  * 2. Redistributions in binary form must reproduce the above copyright
     29  1.1      mrg  *    notice, this list of conditions and the following disclaimer in the
     30  1.1      mrg  *    documentation and/or other materials provided with the distribution.
     31  1.1      mrg  * 3. All advertising materials mentioning features or use of this software
     32  1.1      mrg  *    must display the following acknowledgement:
     33  1.1      mrg  *	This product includes software developed by the University of
     34  1.1      mrg  *	California, Berkeley and its contributors.
     35  1.1      mrg  *	This product includes software developed by Paul Kranenburg.
     36  1.1      mrg  *	This product includes software developed by Harvard University.
     37  1.1      mrg  * 4. Neither the name of the University nor the names of its contributors
     38  1.1      mrg  *    may be used to endorse or promote products derived from this software
     39  1.1      mrg  *    without specific prior written permission.
     40  1.1      mrg  *
     41  1.1      mrg  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     42  1.1      mrg  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     43  1.1      mrg  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     44  1.1      mrg  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     45  1.1      mrg  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     46  1.1      mrg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     47  1.1      mrg  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     48  1.1      mrg  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     49  1.1      mrg  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     50  1.1      mrg  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     51  1.1      mrg  * SUCH DAMAGE.
     52  1.1      mrg  *
     53  1.1      mrg  *	@(#)clock.c	8.1 (Berkeley) 6/11/93
     54  1.1      mrg  * from: NetBSD: clock.c,v 1.80 2006/09/03 22:27:45 gdamore Exp
     55  1.1      mrg  *
     56  1.1      mrg  */
     57  1.1      mrg 
     58  1.1      mrg #include <sys/cdefs.h>
     59  1.8   dyoung __KERNEL_RCSID(0, "$NetBSD: rtc.c,v 1.8 2011/07/01 18:48:37 dyoung Exp $");
     60  1.1      mrg 
     61  1.1      mrg /*
     62  1.1      mrg  * Clock driver for 'rtc' - mc146818 driver.
     63  1.1      mrg  */
     64  1.1      mrg 
     65  1.1      mrg #include <sys/param.h>
     66  1.1      mrg #include <sys/kernel.h>
     67  1.1      mrg #include <sys/device.h>
     68  1.1      mrg #include <sys/proc.h>
     69  1.1      mrg 
     70  1.8   dyoung #include <sys/bus.h>
     71  1.1      mrg #include <machine/autoconf.h>
     72  1.1      mrg 
     73  1.1      mrg #include <dev/clock_subr.h>
     74  1.1      mrg #include <dev/ic/mc146818reg.h>
     75  1.1      mrg #include <dev/ic/mc146818var.h>
     76  1.1      mrg 
     77  1.1      mrg #include <dev/ebus/ebusreg.h>
     78  1.1      mrg #include <dev/ebus/ebusvar.h>
     79  1.1      mrg 
     80  1.4  tsutsui static int	rtc_ebus_match(device_t, cfdata_t, void *);
     81  1.4  tsutsui static void	rtc_ebus_attach(device_t, device_t, void *);
     82  1.1      mrg 
     83  1.4  tsutsui CFATTACH_DECL_NEW(rtc_ebus, sizeof(struct mc146818_softc),
     84  1.2  tsutsui     rtc_ebus_match, rtc_ebus_attach, NULL, NULL);
     85  1.1      mrg 
     86  1.1      mrg u_int rtc_read_reg(struct mc146818_softc *, u_int);
     87  1.1      mrg void rtc_write_reg(struct mc146818_softc *, u_int, u_int);
     88  1.1      mrg u_int rtc_getcent(struct mc146818_softc *);
     89  1.1      mrg void rtc_setcent(struct mc146818_softc *, u_int);
     90  1.1      mrg 
     91  1.1      mrg static int
     92  1.4  tsutsui rtc_ebus_match(device_t parent, cfdata_t cf, void *aux)
     93  1.1      mrg {
     94  1.1      mrg 	struct ebus_attach_args *ea = aux;
     95  1.1      mrg 
     96  1.1      mrg 	return (strcmp("rtc", ea->ea_name) == 0);
     97  1.1      mrg }
     98  1.1      mrg 
     99  1.1      mrg /*
    100  1.1      mrg  * `rtc' is a ds1287 on an ebus (actually an isa bus, but we use the
    101  1.1      mrg  * ebus driver for isa.)  So we can use ebus_wenable() but need to do
    102  1.1      mrg  * different attach work and use different todr routines.  It does not
    103  1.1      mrg  * incorporate an IDPROM.
    104  1.1      mrg  */
    105  1.1      mrg 
    106  1.1      mrg /*
    107  1.1      mrg  * XXX the stupid ds1287 is not mapped directly but uses an address
    108  1.1      mrg  * and a data reg so we cannot access the stuuupid thing w/o having
    109  1.1      mrg  * write access to the registers.
    110  1.1      mrg  *
    111  1.1      mrg  * XXXX We really need to mutex register access!
    112  1.1      mrg  */
    113  1.1      mrg #define	RTC_ADDR	0
    114  1.1      mrg #define	RTC_DATA	1
    115  1.1      mrg u_int
    116  1.1      mrg rtc_read_reg(struct mc146818_softc *sc, u_int reg)
    117  1.1      mrg {
    118  1.1      mrg 
    119  1.1      mrg 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_ADDR, reg);
    120  1.1      mrg 	return (bus_space_read_1(sc->sc_bst, sc->sc_bsh, RTC_DATA));
    121  1.1      mrg }
    122  1.1      mrg void
    123  1.1      mrg rtc_write_reg(struct mc146818_softc *sc, u_int reg, u_int val)
    124  1.1      mrg {
    125  1.1      mrg 
    126  1.1      mrg 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_ADDR, reg);
    127  1.1      mrg 	bus_space_write_1(sc->sc_bst, sc->sc_bsh, RTC_DATA, val);
    128  1.1      mrg }
    129  1.1      mrg 
    130  1.1      mrg /* ARGSUSED */
    131  1.1      mrg static void
    132  1.4  tsutsui rtc_ebus_attach(device_t parent, device_t self, void *aux)
    133  1.1      mrg {
    134  1.4  tsutsui 	struct mc146818_softc *sc = device_private(self);
    135  1.1      mrg 	struct ebus_attach_args *ea = aux;
    136  1.1      mrg 	char *model;
    137  1.1      mrg 	int sz;
    138  1.1      mrg 
    139  1.5  tsutsui 	sc->sc_dev = self;
    140  1.1      mrg 	sc->sc_bst = ea->ea_bustag;
    141  1.1      mrg 
    142  1.1      mrg 	/* hard code to 8K? */
    143  1.1      mrg 	sz = ea->ea_reg[0].size;
    144  1.1      mrg 
    145  1.1      mrg 	if (bus_space_map(sc->sc_bst,
    146  1.1      mrg 			 EBUS_ADDR_FROM_REG(&ea->ea_reg[0]),
    147  1.6      mrg 			 sz, 0,
    148  1.1      mrg 			 &sc->sc_bsh) != 0) {
    149  1.4  tsutsui 		aprint_error(": can't map register\n");
    150  1.1      mrg 		return;
    151  1.1      mrg 	}
    152  1.1      mrg 
    153  1.1      mrg 	model = prom_getpropstring(ea->ea_node, "model");
    154  1.1      mrg #ifdef DIAGNOSTIC
    155  1.1      mrg 	if (model == NULL)
    156  1.1      mrg 		panic("clockattach_rtc: no model property");
    157  1.1      mrg #endif
    158  1.1      mrg 
    159  1.1      mrg 	/* Our TOD clock year 0 is 0 */
    160  1.1      mrg 	sc->sc_year0 = 0;
    161  1.1      mrg 	sc->sc_flag = MC146818_NO_CENT_ADJUST;
    162  1.1      mrg 	sc->sc_mcread = rtc_read_reg;
    163  1.1      mrg 	sc->sc_mcwrite = rtc_write_reg;
    164  1.1      mrg 	sc->sc_getcent = rtc_getcent;
    165  1.1      mrg 	sc->sc_setcent = rtc_setcent;
    166  1.1      mrg 	mc146818_attach(sc);
    167  1.1      mrg 
    168  1.4  tsutsui 	aprint_normal(": %s\n", model);
    169  1.7      mrg 	aprint_naive(": Clock\n");
    170  1.1      mrg 
    171  1.1      mrg 	/*
    172  1.1      mrg 	 * Turn interrupts off, just in case. (Although they shouldn't
    173  1.1      mrg 	 * be wired to an interrupt controller on sparcs).
    174  1.1      mrg 	 */
    175  1.1      mrg 	rtc_write_reg(sc, MC_REGB, MC_REGB_BINARY | MC_REGB_24HR);
    176  1.1      mrg 
    177  1.1      mrg 	/*
    178  1.1      mrg 	 * Apparently on some machines the TOD registers are on the same
    179  1.1      mrg 	 * physical page as the COM registers.  So we won't protect them.
    180  1.1      mrg 	 */
    181  1.1      mrg 	/*sc->sc_handle.todr_setwen = NULL;*/
    182  1.1      mrg }
    183  1.1      mrg 
    184  1.1      mrg /*
    185  1.1      mrg  * MD mc146818 RTC todr routines.
    186  1.1      mrg  */
    187  1.1      mrg 
    188  1.1      mrg /* Loooks like Sun stores the century info somewhere in CMOS RAM */
    189  1.1      mrg #define MC_CENT 0x32
    190  1.1      mrg 
    191  1.1      mrg u_int
    192  1.4  tsutsui rtc_getcent(struct mc146818_softc *sc)
    193  1.1      mrg {
    194  1.1      mrg 
    195  1.1      mrg 	return rtc_read_reg(sc, MC_CENT);
    196  1.1      mrg }
    197  1.1      mrg void
    198  1.4  tsutsui rtc_setcent(struct mc146818_softc *sc, u_int cent)
    199  1.1      mrg {
    200  1.1      mrg 
    201  1.1      mrg 	rtc_write_reg(sc, MC_CENT, cent);
    202  1.1      mrg }
    203