Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_ost.c revision 1.3
      1 /*	$NetBSD: sa11x0_ost.c,v 1.3 2001/11/22 18:34:34 thorpej 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/types.h>
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/kernel.h>
     44 #include <sys/time.h>
     45 #include <sys/device.h>
     46 
     47 #include <machine/bus.h>
     48 #include <machine/intr.h>
     49 #include <machine/cpufunc.h>
     50 #include <arm/arm32/katelib.h>
     51 #include <arm/sa11x0/sa11x0_reg.h>
     52 #include <arm/sa11x0/sa11x0_var.h>
     53 #include <arm/sa11x0/sa11x0_ostreg.h>
     54 
     55 static int	saost_match(struct device *, struct cfdata *, void *);
     56 static void	saost_attach(struct device *, struct device *, void *);
     57 
     58 int		gettick(void);
     59 static int	clockintr(void *);
     60 static int	statintr(void *);
     61 void		rtcinit(void);
     62 
     63 struct saost_softc {
     64 	struct device		sc_dev;
     65 	bus_addr_t		sc_baseaddr;
     66 	bus_space_tag_t		sc_iot;
     67 	bus_space_handle_t	sc_ioh;
     68 
     69 	u_int32_t	sc_clock_count;
     70 	u_int32_t	sc_statclock_count;
     71 	u_int32_t	sc_statclock_step;
     72 };
     73 
     74 static struct saost_softc *saost_sc = NULL;
     75 
     76 #define TIMER_FREQUENCY         3686400         /* 3.6864MHz */
     77 #define TICKS_PER_MICROSECOND   (TIMER_FREQUENCY/1000000)
     78 
     79 #ifndef STATHZ
     80 #define STATHZ	64
     81 #endif
     82 
     83 struct cfattach saost_ca = {
     84 	sizeof(struct saost_softc), saost_match, saost_attach
     85 };
     86 
     87 static int
     88 saost_match(parent, match, aux)
     89 	struct device *parent;
     90 	struct cfdata *match;
     91 	void *aux;
     92 {
     93 	return (1);
     94 }
     95 
     96 void
     97 saost_attach(parent, self, aux)
     98 	struct device *parent;
     99 	struct device *self;
    100 	void *aux;
    101 {
    102 	struct saost_softc *sc = (struct saost_softc*)self;
    103 	struct sa11x0_attach_args *sa = aux;
    104 
    105 	printf("\n");
    106 
    107 	sc->sc_iot = sa->sa_iot;
    108 	sc->sc_baseaddr = sa->sa_addr;
    109 
    110 	saost_sc = sc;
    111 
    112 	if(bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0,
    113 			&sc->sc_ioh))
    114 		panic("%s: Cannot map registers\n", self->dv_xname);
    115 
    116 	/* disable all channel and clear interrupt status */
    117 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 0);
    118 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
    119 
    120 	printf("%s: SA-11x0 OS Timer\n",  sc->sc_dev.dv_xname);
    121 }
    122 
    123 static int
    124 clockintr(arg)
    125 	void *arg;
    126 {
    127 	struct clockframe *frame = arg;
    128 	u_int32_t oscr, nextmatch, oldmatch;
    129 	int s;
    130 
    131 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    132 			SAOST_SR, 1);
    133 
    134 	/* schedule next clock intr */
    135 	oldmatch = saost_sc->sc_clock_count;
    136 	nextmatch = oldmatch + TIMER_FREQUENCY / hz;
    137 
    138 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    139 			  nextmatch);
    140 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    141 				SAOST_CR);
    142 
    143 	if ((nextmatch > oldmatch &&
    144 	     (oscr > nextmatch || oscr < oldmatch)) ||
    145 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    146 		/*
    147 		 * we couldn't set the matching register in time.
    148 		 * just set it to some value so that next interrupt happens.
    149 		 * XXX is it possible to compansate lost interrupts?
    150 		 */
    151 
    152 		s = splhigh();
    153 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    154 					SAOST_CR);
    155 		nextmatch = oscr + 10;
    156 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    157 				  SAOST_MR0, nextmatch);
    158 		splx(s);
    159 	}
    160 
    161 	saost_sc->sc_clock_count = nextmatch;
    162 	hardclock(frame);
    163 
    164 	return(1);
    165 }
    166 
    167 static int
    168 statintr(arg)
    169 	void *arg;
    170 {
    171 	struct clockframe *frame = arg;
    172 	u_int32_t oscr, nextmatch, oldmatch;
    173 	int s;
    174 
    175 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    176 			SAOST_SR, 2);
    177 
    178 	/* schedule next clock intr */
    179 	oldmatch = saost_sc->sc_statclock_count;
    180 	nextmatch = oldmatch + saost_sc->sc_statclock_step;
    181 
    182 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    183 			  nextmatch);
    184 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    185 				SAOST_CR);
    186 
    187 	if ((nextmatch > oldmatch &&
    188 	     (oscr > nextmatch || oscr < oldmatch)) ||
    189 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    190 		/*
    191 		 * we couldn't set the matching register in time.
    192 		 * just set it to some value so that next interrupt happens.
    193 		 * XXX is it possible to compansate lost interrupts?
    194 		 */
    195 
    196 		s = splhigh();
    197 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    198 					SAOST_CR);
    199 		nextmatch = oscr + 10;
    200 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    201 				  SAOST_MR1, nextmatch);
    202 		splx(s);
    203 	}
    204 
    205 	saost_sc->sc_statclock_count = nextmatch;
    206 	statclock(frame);
    207 
    208 	return(1);
    209 }
    210 
    211 
    212 void
    213 setstatclockrate(hz)
    214 	int hz;
    215 {
    216 	u_int32_t count;
    217 
    218 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / hz;
    219 	count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
    220 	count += saost_sc->sc_statclock_step;
    221 	saost_sc->sc_statclock_count = count;
    222 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    223 			SAOST_MR1, count);
    224 }
    225 
    226 void
    227 cpu_initclocks()
    228 {
    229 	stathz = STATHZ;
    230 	profhz = stathz;
    231 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
    232 
    233 	printf("clock: hz=%d stathz = %d\n", hz, stathz);
    234 
    235 	/* Zero the counter value */
    236 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
    237 
    238 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    239 	saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
    240 	saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
    241 
    242 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
    243 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    244 			  saost_sc->sc_clock_count);
    245 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    246 			  saost_sc->sc_statclock_count);
    247 
    248 	sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
    249 	sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
    250 }
    251 
    252 int
    253 gettick()
    254 {
    255 	int counter;
    256 	u_int savedints;
    257 	savedints = disable_interrupts(I32_bit);
    258 
    259 	counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    260 			SAOST_CR);
    261 
    262 	restore_interrupts(savedints);
    263 	return counter;
    264 }
    265 
    266 void
    267 microtime(tvp)
    268 	register struct timeval *tvp;
    269 {
    270 	int s = splhigh();
    271 	int tm;
    272 	int deltatm;
    273 	static struct timeval lasttime;
    274 
    275 	tm = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    276 			SAOST_CR);
    277 
    278 	deltatm = saost_sc->sc_clock_count - tm;
    279 
    280 #ifdef DEBUG
    281 	printf("deltatm = %d\n",deltatm);
    282 #endif
    283 
    284 	*tvp = time;
    285 	tvp->tv_usec++;		/* XXX */
    286 	while (tvp->tv_usec >= 1000000) {
    287 		tvp->tv_sec++;
    288 		tvp->tv_usec -= 1000000;
    289 	}
    290 
    291 	if (tvp->tv_sec == lasttime.tv_sec &&
    292 		tvp->tv_usec <= lasttime.tv_usec &&
    293 		(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
    294 	{
    295 		tvp->tv_sec++;
    296 		tvp->tv_usec -= 1000000;
    297 	}
    298 	lasttime = *tvp;
    299 	splx(s);
    300 }
    301 
    302 void
    303 delay(usecs)
    304 	u_int usecs;
    305 {
    306 	u_int32_t tick, otick, delta;
    307 	int j, csec, usec;
    308 
    309 	csec = usecs / 10000;
    310 	usec = usecs % 10000;
    311 
    312 	usecs = (TIMER_FREQUENCY / 100) * csec
    313 	    + (TIMER_FREQUENCY / 100) * usec / 10000;
    314 
    315 	if (! saost_sc) {
    316 		/* clock isn't initialized yet */
    317 		for(; usecs > 0; usecs--)
    318 			for(j = 100; j > 0; j--)
    319 				;
    320 		return;
    321 	}
    322 
    323 	otick = gettick();
    324 
    325 	while (1) {
    326 		for(j = 100; j > 0; j--)
    327 			;
    328 		tick = gettick();
    329 		delta = tick - otick;
    330 		if (delta > usecs)
    331 			break;
    332 		usecs -= delta;
    333 		otick = tick;
    334 	}
    335 }
    336 
    337 void
    338 resettodr()
    339 {
    340 }
    341 
    342 void
    343 inittodr(base)
    344 	time_t base;
    345 {
    346 	time.tv_sec = base;
    347 	time.tv_usec = 0;
    348 }
    349