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