Home | History | Annotate | Line # | Download | only in dev
aurtc.c revision 1.10
      1  1.10  gdamore /* $NetBSD: aurtc.c,v 1.10 2006/03/28 16:52:35 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.10  gdamore __KERNEL_RCSID(0, "$NetBSD: aurtc.c,v 1.10 2006/03/28 16:52:35 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 #define	REGVAL(x) (*(volatile uint32_t *)(MIPS_PHYS_TO_KSEG1(PC_BASE + (x))))
     88   1.9  gdamore #define	GETREG(x)	(REGVAL(x))
     89   1.9  gdamore #define	PUTREG(x,v)	(REGVAL(x) = (v))
     90   1.9  gdamore 
     91   1.9  gdamore struct aurtc_softc {
     92   1.9  gdamore 	struct device		sc_dev;
     93   1.9  gdamore 	struct todr_chip_handle	sc_tch;
     94   1.9  gdamore 	void			*sc_shutdownhook;
     95   1.9  gdamore };
     96   1.9  gdamore 
     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.9  gdamore static int	aurtc_gettime(todr_chip_handle_t, volatile struct timeval *);
    100   1.9  gdamore static int	aurtc_settime(todr_chip_handle_t, volatile struct timeval *);
    101   1.9  gdamore static int	aurtc_getcal(todr_chip_handle_t, int *);
    102   1.9  gdamore static int	aurtc_setcal(todr_chip_handle_t, int);
    103   1.9  gdamore static void	aurtc_shutdown(void *);
    104   1.1   simonb 
    105   1.9  gdamore 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.9  gdamore 	struct aurtc_softc *sc = (struct aurtc_softc *)self;
    123   1.1   simonb 
    124   1.9  gdamore 	printf(": Au1X00 programmable clock\n");
    125   1.9  gdamore 
    126   1.9  gdamore 	sc->sc_tch.cookie = sc;
    127   1.9  gdamore 	sc->sc_tch.bus_cookie = NULL;
    128   1.9  gdamore 	sc->sc_tch.todr_gettime = aurtc_gettime;
    129   1.9  gdamore 	sc->sc_tch.todr_settime = aurtc_settime;
    130   1.9  gdamore 	sc->sc_tch.todr_getcal = aurtc_getcal;
    131   1.9  gdamore 	sc->sc_tch.todr_setcal = aurtc_setcal;
    132   1.9  gdamore 	sc->sc_tch.todr_setwen = NULL;
    133   1.9  gdamore 
    134   1.9  gdamore 	sc->sc_shutdownhook = shutdownhook_establish(aurtc_shutdown, NULL);
    135  1.10  gdamore 
    136  1.10  gdamore 	todr_attach(&sc->sc_tch);
    137   1.1   simonb }
    138   1.1   simonb 
    139   1.9  gdamore /*
    140   1.9  gdamore  * Note that our RTC only has second resolution.
    141   1.9  gdamore  */
    142   1.9  gdamore 
    143   1.9  gdamore int
    144   1.9  gdamore aurtc_gettime(todr_chip_handle_t tch, volatile struct timeval *tv)
    145   1.1   simonb {
    146   1.9  gdamore 	int			s;
    147   1.1   simonb 
    148   1.9  gdamore 	s = splclock();
    149   1.9  gdamore 	tv->tv_sec = GETREG(PC_COUNTER_READ_0);
    150   1.9  gdamore 	splx(s);
    151   1.9  gdamore 	return 0;
    152   1.1   simonb }
    153   1.1   simonb 
    154   1.9  gdamore int
    155   1.9  gdamore aurtc_settime(todr_chip_handle_t tch, volatile struct timeval *tvp)
    156   1.1   simonb {
    157   1.9  gdamore 	int			s;
    158   1.9  gdamore 	struct timeval		tv;
    159   1.1   simonb 
    160   1.9  gdamore 	s = splclock();
    161   1.9  gdamore 	tv = *tvp;
    162   1.9  gdamore 	splx(s);
    163   1.1   simonb 
    164   1.9  gdamore 	/* wait for the clock register to be idle */
    165   1.9  gdamore 	while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) {
    166   1.9  gdamore 		continue;
    167   1.9  gdamore 	}
    168   1.1   simonb 
    169   1.9  gdamore 	PUTREG(PC_COUNTER_WRITE0, tv.tv_sec);
    170   1.9  gdamore 
    171   1.9  gdamore 	/*
    172   1.9  gdamore 	 * It could take a second or two for the clock change to take effect.
    173   1.9  gdamore 	 * We don't want to make settimeofday() take that long, so we don't
    174   1.9  gdamore 	 * wait here, but instead wait in flush.  This can have a bad effect
    175   1.9  gdamore 	 * for settimeofday() calls with a short window between them.
    176   1.9  gdamore 	 */
    177   1.9  gdamore 	return 0;
    178   1.1   simonb }
    179   1.1   simonb 
    180   1.9  gdamore int
    181   1.9  gdamore aurtc_getcal(todr_chip_handle_t tch, int *vp)
    182   1.1   simonb {
    183   1.8  gdamore 
    184   1.9  gdamore 	return EOPNOTSUPP;
    185   1.9  gdamore }
    186   1.1   simonb 
    187   1.9  gdamore int
    188   1.9  gdamore aurtc_setcal(todr_chip_handle_t tch, int v)
    189   1.9  gdamore {
    190   1.9  gdamore 
    191   1.9  gdamore 	return EOPNOTSUPP;
    192   1.9  gdamore }
    193   1.1   simonb 
    194   1.9  gdamore void
    195   1.9  gdamore aurtc_shutdown(void *arg)
    196   1.9  gdamore {
    197   1.1   simonb 
    198   1.9  gdamore 	/* wait for the clock register to be idle */
    199   1.9  gdamore 	while (GETREG(PC_COUNTER_CONTROL) & CC_C0S) {
    200   1.8  gdamore 		continue;
    201   1.9  gdamore 	}
    202   1.1   simonb }
    203