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