Home | History | Annotate | Line # | Download | only in dev
aurtc.c revision 1.7.8.3
      1  1.7.8.3     yamt /* $NetBSD: aurtc.c,v 1.7.8.3 2006/09/14 12:31:12 yamt Exp $ */
      2  1.7.8.2     yamt 
      3  1.7.8.2     yamt /*-
      4  1.7.8.2     yamt  * Copyright (c) 2006 Itronix Inc.
      5  1.7.8.2     yamt  * All rights reserved.
      6  1.7.8.2     yamt  *
      7  1.7.8.2     yamt  * Written by Garrett D'Amore for Itronix Inc.
      8  1.7.8.2     yamt  *
      9  1.7.8.2     yamt  * Redistribution and use in source and binary forms, with or without
     10  1.7.8.2     yamt  * modification, are permitted provided that the following conditions
     11  1.7.8.2     yamt  * are met:
     12  1.7.8.2     yamt  * 1. Redistributions of source code must retain the above copyright
     13  1.7.8.2     yamt  *    notice, this list of conditions and the following disclaimer.
     14  1.7.8.2     yamt  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.7.8.2     yamt  *    notice, this list of conditions and the following disclaimer in the
     16  1.7.8.2     yamt  *    documentation and/or other materials provided with the distribution.
     17  1.7.8.2     yamt  * 3. The name of Itronix Inc. may not be used to endorse
     18  1.7.8.2     yamt  *    or promote products derived from this software without specific
     19  1.7.8.2     yamt  *    prior written permission.
     20  1.7.8.2     yamt  *
     21  1.7.8.2     yamt  * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND
     22  1.7.8.2     yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  1.7.8.2     yamt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  1.7.8.2     yamt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY
     25  1.7.8.2     yamt  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     26  1.7.8.2     yamt  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     27  1.7.8.2     yamt  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     28  1.7.8.2     yamt  * ON ANY THEORY OF LIABILITY, WHETHER IN
     29  1.7.8.2     yamt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  1.7.8.2     yamt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  1.7.8.2     yamt  * POSSIBILITY OF SUCH DAMAGE.
     32  1.7.8.2     yamt  */
     33      1.1   simonb 
     34      1.1   simonb /*
     35      1.1   simonb  * Copyright 2002 Wasabi Systems, Inc.
     36      1.1   simonb  * All rights reserved.
     37      1.1   simonb  *
     38      1.1   simonb  * Written by Simon Burge for Wasabi Systems, Inc.
     39      1.1   simonb  *
     40      1.1   simonb  * Redistribution and use in source and binary forms, with or without
     41      1.1   simonb  * modification, are permitted provided that the following conditions
     42      1.1   simonb  * are met:
     43      1.1   simonb  * 1. Redistributions of source code must retain the above copyright
     44      1.1   simonb  *    notice, this list of conditions and the following disclaimer.
     45      1.1   simonb  * 2. Redistributions in binary form must reproduce the above copyright
     46      1.1   simonb  *    notice, this list of conditions and the following disclaimer in the
     47      1.1   simonb  *    documentation and/or other materials provided with the distribution.
     48      1.1   simonb  * 3. All advertising materials mentioning features or use of this software
     49      1.1   simonb  *    must display the following acknowledgement:
     50      1.1   simonb  *      This product includes software developed for the NetBSD Project by
     51      1.1   simonb  *      Wasabi Systems, Inc.
     52      1.1   simonb  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
     53      1.1   simonb  *    or promote products derived from this software without specific prior
     54      1.1   simonb  *    written permission.
     55      1.1   simonb  *
     56      1.1   simonb  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
     57      1.1   simonb  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     58      1.1   simonb  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     59      1.1   simonb  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
     60      1.1   simonb  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     61      1.1   simonb  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     62      1.1   simonb  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     63      1.1   simonb  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     64      1.1   simonb  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     65      1.1   simonb  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     66      1.1   simonb  * POSSIBILITY OF SUCH DAMAGE.
     67      1.1   simonb  */
     68      1.6    lukem 
     69  1.7.8.2     yamt 
     70      1.6    lukem #include <sys/cdefs.h>
     71  1.7.8.3     yamt __KERNEL_RCSID(0, "$NetBSD: aurtc.c,v 1.7.8.3 2006/09/14 12:31:12 yamt Exp $");
     72      1.1   simonb 
     73      1.1   simonb #include <sys/param.h>
     74      1.1   simonb #include <sys/systm.h>
     75  1.7.8.2     yamt #include <sys/errno.h>
     76      1.1   simonb #include <sys/device.h>
     77  1.7.8.2     yamt #include <sys/proc.h>
     78      1.1   simonb 
     79      1.1   simonb #include <dev/clock_subr.h>
     80      1.1   simonb 
     81  1.7.8.2     yamt #include <machine/bus.h>
     82  1.7.8.2     yamt 
     83      1.1   simonb #include <mips/alchemy/include/aureg.h>
     84  1.7.8.2     yamt #include <mips/alchemy/include/auvar.h>
     85      1.1   simonb #include <mips/alchemy/include/aubusvar.h>
     86      1.1   simonb 
     87  1.7.8.2     yamt #define	REGVAL(x) (*(volatile uint32_t *)(MIPS_PHYS_TO_KSEG1(PC_BASE + (x))))
     88  1.7.8.2     yamt #define	GETREG(x)	(REGVAL(x))
     89  1.7.8.2     yamt #define	PUTREG(x,v)	(REGVAL(x) = (v))
     90  1.7.8.2     yamt 
     91  1.7.8.2     yamt struct aurtc_softc {
     92  1.7.8.2     yamt 	struct device		sc_dev;
     93  1.7.8.2     yamt 	struct todr_chip_handle	sc_tch;
     94  1.7.8.2     yamt 	void			*sc_shutdownhook;
     95  1.7.8.2     yamt };
     96  1.7.8.2     yamt 
     97      1.1   simonb static int	aurtc_match(struct device *, struct cfdata *, void *);
     98      1.1   simonb static void	aurtc_attach(struct device *, struct device *, void *);
     99  1.7.8.2     yamt static int	aurtc_gettime(todr_chip_handle_t, volatile struct timeval *);
    100  1.7.8.2     yamt static int	aurtc_settime(todr_chip_handle_t, volatile struct timeval *);
    101  1.7.8.2     yamt static void	aurtc_shutdown(void *);
    102      1.1   simonb 
    103  1.7.8.2     yamt CFATTACH_DECL(aurtc, sizeof (struct aurtc_softc),
    104      1.5  thorpej     aurtc_match, aurtc_attach, NULL, NULL);
    105      1.1   simonb 
    106      1.1   simonb int
    107      1.1   simonb aurtc_match(struct device *parent, struct cfdata *match, void *aux)
    108      1.1   simonb {
    109      1.1   simonb 	struct aubus_attach_args *aa = aux;
    110      1.1   simonb 
    111      1.2  thorpej 	if (strcmp(aa->aa_name, match->cf_name) == 0)
    112      1.1   simonb 		return (1);
    113      1.1   simonb 
    114      1.1   simonb 	return (0);
    115      1.1   simonb }
    116      1.1   simonb 
    117      1.1   simonb void
    118      1.1   simonb aurtc_attach(struct device *parent, struct device *self, void *aux)
    119      1.1   simonb {
    120  1.7.8.2     yamt 	struct aurtc_softc *sc = (struct aurtc_softc *)self;
    121      1.1   simonb 
    122  1.7.8.2     yamt 	printf(": Au1X00 programmable clock\n");
    123  1.7.8.2     yamt 
    124  1.7.8.2     yamt 	sc->sc_tch.cookie = sc;
    125  1.7.8.2     yamt 	sc->sc_tch.bus_cookie = NULL;
    126  1.7.8.2     yamt 	sc->sc_tch.todr_gettime = aurtc_gettime;
    127  1.7.8.2     yamt 	sc->sc_tch.todr_settime = aurtc_settime;
    128  1.7.8.2     yamt 	sc->sc_tch.todr_setwen = NULL;
    129      1.1   simonb 
    130  1.7.8.2     yamt 	sc->sc_shutdownhook = shutdownhook_establish(aurtc_shutdown, NULL);
    131      1.1   simonb 
    132  1.7.8.2     yamt 	todr_attach(&sc->sc_tch);
    133      1.1   simonb }
    134      1.1   simonb 
    135      1.1   simonb /*
    136  1.7.8.2     yamt  * Note that our RTC only has second resolution.
    137      1.1   simonb  */
    138      1.1   simonb 
    139  1.7.8.2     yamt int
    140  1.7.8.2     yamt aurtc_gettime(todr_chip_handle_t tch, volatile struct timeval *tv)
    141      1.1   simonb {
    142  1.7.8.2     yamt 	int			s;
    143  1.7.8.2     yamt 
    144  1.7.8.2     yamt 	s = splclock();
    145  1.7.8.2     yamt 	tv->tv_sec = GETREG(PC_COUNTER_READ_0);
    146  1.7.8.2     yamt 	splx(s);
    147  1.7.8.2     yamt 	return 0;
    148  1.7.8.2     yamt }
    149  1.7.8.2     yamt 
    150  1.7.8.2     yamt int
    151  1.7.8.2     yamt aurtc_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    152  1.7.8.2     yamt {
    153  1.7.8.2     yamt 	int			s;
    154  1.7.8.2     yamt 	struct timeval		tv;
    155  1.7.8.2     yamt 
    156  1.7.8.2     yamt 	s = splclock();
    157  1.7.8.2     yamt 	tv = *tvp;
    158  1.7.8.2     yamt 	splx(s);
    159      1.1   simonb 
    160  1.7.8.2     yamt 	/* wait for the clock register to be idle */
    161  1.7.8.2     yamt 	while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) {
    162  1.7.8.2     yamt 		continue;
    163  1.7.8.2     yamt 	}
    164      1.1   simonb 
    165  1.7.8.2     yamt 	PUTREG(PC_COUNTER_WRITE0, tv.tv_sec);
    166      1.1   simonb 
    167  1.7.8.2     yamt 	/*
    168  1.7.8.2     yamt 	 * It could take a second or two for the clock change to take effect.
    169  1.7.8.2     yamt 	 * We don't want to make settimeofday() take that long, so we don't
    170  1.7.8.2     yamt 	 * wait here, but instead wait in flush.  This can have a bad effect
    171  1.7.8.2     yamt 	 * for settimeofday() calls with a short window between them.
    172  1.7.8.2     yamt 	 */
    173  1.7.8.2     yamt 	return 0;
    174      1.1   simonb }
    175      1.1   simonb 
    176  1.7.8.2     yamt void
    177  1.7.8.2     yamt aurtc_shutdown(void *arg)
    178  1.7.8.2     yamt {
    179  1.7.8.2     yamt 
    180  1.7.8.2     yamt 	/* wait for the clock register to be idle */
    181  1.7.8.2     yamt 	while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) {
    182  1.7.8.1     yamt 		continue;
    183  1.7.8.2     yamt 	}
    184      1.1   simonb }
    185