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