Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_ost.c revision 1.11
      1 /*	$NetBSD: sa11x0_ost.c,v 1.11 2003/07/15 00:24:51 lukem Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Mark Brinicombe.
      5  * Copyright (c) 1997 Causality Limited.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by IWAMOTO Toshihiro and Ichiro FUKUHARA.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 __KERNEL_RCSID(0, "$NetBSD: sa11x0_ost.c,v 1.11 2003/07/15 00:24:51 lukem Exp $");
     42 
     43 #include <sys/types.h>
     44 #include <sys/param.h>
     45 #include <sys/systm.h>
     46 #include <sys/kernel.h>
     47 #include <sys/time.h>
     48 #include <sys/device.h>
     49 
     50 #include <machine/bus.h>
     51 #include <machine/intr.h>
     52 
     53 #include <arm/cpufunc.h>
     54 
     55 #include <arm/arm32/katelib.h>
     56 
     57 #include <arm/sa11x0/sa11x0_reg.h>
     58 #include <arm/sa11x0/sa11x0_var.h>
     59 #include <arm/sa11x0/sa11x0_ostreg.h>
     60 
     61 static int	saost_match(struct device *, struct cfdata *, void *);
     62 static void	saost_attach(struct device *, struct device *, void *);
     63 
     64 int		gettick(void);
     65 static int	clockintr(void *);
     66 static int	statintr(void *);
     67 void		rtcinit(void);
     68 
     69 struct saost_softc {
     70 	struct device		sc_dev;
     71 	bus_addr_t		sc_baseaddr;
     72 	bus_space_tag_t		sc_iot;
     73 	bus_space_handle_t	sc_ioh;
     74 
     75 	u_int32_t	sc_clock_count;
     76 	u_int32_t	sc_statclock_count;
     77 	u_int32_t	sc_statclock_step;
     78 };
     79 
     80 static struct saost_softc *saost_sc = NULL;
     81 
     82 #define TIMER_FREQUENCY         3686400         /* 3.6864MHz */
     83 #define TICKS_PER_MICROSECOND   (TIMER_FREQUENCY/1000000)
     84 
     85 #ifndef STATHZ
     86 #define STATHZ	64
     87 #endif
     88 
     89 CFATTACH_DECL(saost, sizeof(struct saost_softc),
     90     saost_match, saost_attach, NULL, NULL);
     91 
     92 static int
     93 saost_match(parent, match, aux)
     94 	struct device *parent;
     95 	struct cfdata *match;
     96 	void *aux;
     97 {
     98 	return (1);
     99 }
    100 
    101 void
    102 saost_attach(parent, self, aux)
    103 	struct device *parent;
    104 	struct device *self;
    105 	void *aux;
    106 {
    107 	struct saost_softc *sc = (struct saost_softc*)self;
    108 	struct sa11x0_attach_args *sa = aux;
    109 
    110 	printf("\n");
    111 
    112 	sc->sc_iot = sa->sa_iot;
    113 	sc->sc_baseaddr = sa->sa_addr;
    114 
    115 	saost_sc = sc;
    116 
    117 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    118 			&sc->sc_ioh))
    119 		panic("%s: Cannot map registers", self->dv_xname);
    120 
    121 	/* disable all channel and clear interrupt status */
    122 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 0);
    123 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
    124 
    125 	printf("%s: SA-11x0 OS Timer\n",  sc->sc_dev.dv_xname);
    126 }
    127 
    128 static int
    129 clockintr(arg)
    130 	void *arg;
    131 {
    132 	struct clockframe *frame = arg;
    133 	u_int32_t oscr, nextmatch, oldmatch;
    134 	int s;
    135 
    136 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    137 			SAOST_SR, 1);
    138 
    139 	/* schedule next clock intr */
    140 	oldmatch = saost_sc->sc_clock_count;
    141 	nextmatch = oldmatch + TIMER_FREQUENCY / hz;
    142 
    143 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    144 			  nextmatch);
    145 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    146 				SAOST_CR);
    147 
    148 	if ((nextmatch > oldmatch &&
    149 	     (oscr > nextmatch || oscr < oldmatch)) ||
    150 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    151 		/*
    152 		 * we couldn't set the matching register in time.
    153 		 * just set it to some value so that next interrupt happens.
    154 		 * XXX is it possible to compansate lost interrupts?
    155 		 */
    156 
    157 		s = splhigh();
    158 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    159 					SAOST_CR);
    160 		nextmatch = oscr + 10;
    161 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    162 				  SAOST_MR0, nextmatch);
    163 		splx(s);
    164 	}
    165 
    166 	saost_sc->sc_clock_count = nextmatch;
    167 	hardclock(frame);
    168 
    169 	return(1);
    170 }
    171 
    172 static int
    173 statintr(arg)
    174 	void *arg;
    175 {
    176 	struct clockframe *frame = arg;
    177 	u_int32_t oscr, nextmatch, oldmatch;
    178 	int s;
    179 
    180 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    181 			SAOST_SR, 2);
    182 
    183 	/* schedule next clock intr */
    184 	oldmatch = saost_sc->sc_statclock_count;
    185 	nextmatch = oldmatch + saost_sc->sc_statclock_step;
    186 
    187 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    188 			  nextmatch);
    189 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    190 				SAOST_CR);
    191 
    192 	if ((nextmatch > oldmatch &&
    193 	     (oscr > nextmatch || oscr < oldmatch)) ||
    194 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    195 		/*
    196 		 * we couldn't set the matching register in time.
    197 		 * just set it to some value so that next interrupt happens.
    198 		 * XXX is it possible to compansate lost interrupts?
    199 		 */
    200 
    201 		s = splhigh();
    202 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    203 					SAOST_CR);
    204 		nextmatch = oscr + 10;
    205 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    206 				  SAOST_MR1, nextmatch);
    207 		splx(s);
    208 	}
    209 
    210 	saost_sc->sc_statclock_count = nextmatch;
    211 	statclock(frame);
    212 
    213 	return(1);
    214 }
    215 
    216 
    217 void
    218 setstatclockrate(hz)
    219 	int hz;
    220 {
    221 	u_int32_t count;
    222 
    223 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / hz;
    224 	count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
    225 	count += saost_sc->sc_statclock_step;
    226 	saost_sc->sc_statclock_count = count;
    227 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    228 			SAOST_MR1, count);
    229 }
    230 
    231 void
    232 cpu_initclocks()
    233 {
    234 	stathz = STATHZ;
    235 	profhz = stathz;
    236 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
    237 
    238 	printf("clock: hz=%d stathz = %d\n", hz, stathz);
    239 
    240 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    241 	saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
    242 	saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
    243 
    244 	sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
    245 	sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
    246 
    247 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
    248 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
    249 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    250 			  saost_sc->sc_clock_count);
    251 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    252 			  saost_sc->sc_statclock_count);
    253 
    254 	/* Zero the counter value */
    255 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
    256 }
    257 
    258 int
    259 gettick()
    260 {
    261 	int counter;
    262 	u_int savedints;
    263 	savedints = disable_interrupts(I32_bit);
    264 
    265 	counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    266 			SAOST_CR);
    267 
    268 	restore_interrupts(savedints);
    269 	return counter;
    270 }
    271 
    272 void
    273 microtime(tvp)
    274 	register struct timeval *tvp;
    275 {
    276 	int s, tm, deltatm;
    277 	static struct timeval lasttime;
    278 
    279 	if(saost_sc == NULL) {
    280 		tvp->tv_sec = 0;
    281 		tvp->tv_usec = 0;
    282 		return;
    283 	}
    284 
    285 	s = splhigh();
    286 	tm = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    287 			SAOST_CR);
    288 
    289 	deltatm = saost_sc->sc_clock_count - tm;
    290 
    291 #ifdef OST_DEBUG
    292 	printf("deltatm = %d\n",deltatm);
    293 #endif
    294 
    295 	*tvp = time;
    296 	tvp->tv_usec++;		/* XXX */
    297 	while (tvp->tv_usec >= 1000000) {
    298 		tvp->tv_sec++;
    299 		tvp->tv_usec -= 1000000;
    300 	}
    301 
    302 	if (tvp->tv_sec == lasttime.tv_sec &&
    303 		tvp->tv_usec <= lasttime.tv_usec &&
    304 		(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
    305 	{
    306 		tvp->tv_sec++;
    307 		tvp->tv_usec -= 1000000;
    308 	}
    309 	lasttime = *tvp;
    310 	splx(s);
    311 }
    312 
    313 void
    314 delay(usecs)
    315 	u_int usecs;
    316 {
    317 	u_int32_t tick, otick, delta;
    318 	int j, csec, usec;
    319 
    320 	csec = usecs / 10000;
    321 	usec = usecs % 10000;
    322 
    323 	usecs = (TIMER_FREQUENCY / 100) * csec
    324 	    + (TIMER_FREQUENCY / 100) * usec / 10000;
    325 
    326 	if (! saost_sc) {
    327 		/* clock isn't initialized yet */
    328 		for(; usecs > 0; usecs--)
    329 			for(j = 100; j > 0; j--)
    330 				;
    331 		return;
    332 	}
    333 
    334 	otick = gettick();
    335 
    336 	while (1) {
    337 		for(j = 100; j > 0; j--)
    338 			;
    339 		tick = gettick();
    340 		delta = tick - otick;
    341 		if (delta > usecs)
    342 			break;
    343 		usecs -= delta;
    344 		otick = tick;
    345 	}
    346 }
    347 
    348 void
    349 resettodr()
    350 {
    351 }
    352 
    353 void
    354 inittodr(base)
    355 	time_t base;
    356 {
    357 	time.tv_sec = base;
    358 	time.tv_usec = 0;
    359 }
    360