Home | History | Annotate | Line # | Download | only in sa11x0
sa11x0_ost.c revision 1.19
      1 /*	$NetBSD: sa11x0_ost.c,v 1.19 2006/09/24 15:36:34 peter 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.19 2006/09/24 15:36:34 peter 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/timetc.h>
     49 #include <sys/device.h>
     50 
     51 #include <machine/bus.h>
     52 #include <machine/intr.h>
     53 
     54 #include <arm/cpufunc.h>
     55 
     56 #include <arm/arm32/katelib.h>
     57 
     58 #include <arm/sa11x0/sa11x0_reg.h>
     59 #include <arm/sa11x0/sa11x0_var.h>
     60 #include <arm/sa11x0/sa11x0_ostreg.h>
     61 
     62 static int	saost_match(struct device *, struct cfdata *, void *);
     63 static void	saost_attach(struct device *, struct device *, void *);
     64 
     65 #ifdef __HAVE_TIMECOUNTER
     66 static void	saost_tc_init(void);
     67 #endif /* __HAVE_TIMECOUNTER */
     68 
     69 static uint32_t	gettick(void);
     70 static int	clockintr(void *);
     71 static int	statintr(void *);
     72 void		rtcinit(void);
     73 
     74 struct saost_softc {
     75 	struct device		sc_dev;
     76 	bus_addr_t		sc_baseaddr;
     77 	bus_space_tag_t		sc_iot;
     78 	bus_space_handle_t	sc_ioh;
     79 
     80 	uint32_t	sc_clock_count;
     81 	uint32_t	sc_statclock_count;
     82 	uint32_t	sc_statclock_step;
     83 };
     84 
     85 static struct saost_softc *saost_sc = NULL;
     86 
     87 #define TIMER_FREQUENCY         3686400         /* 3.6864MHz */
     88 #define TICKS_PER_MICROSECOND   (TIMER_FREQUENCY/1000000)
     89 
     90 #ifndef STATHZ
     91 #define STATHZ	64
     92 #endif
     93 
     94 CFATTACH_DECL(saost, sizeof(struct saost_softc),
     95     saost_match, saost_attach, NULL, NULL);
     96 
     97 static int
     98 saost_match(struct device *parent, struct cfdata *match, void *aux)
     99 {
    100 
    101 	return 1;
    102 }
    103 
    104 void
    105 saost_attach(struct device *parent, struct device *self, 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(void *arg)
    130 {
    131 	struct clockframe *frame = arg;
    132 	uint32_t oscr, nextmatch, oldmatch;
    133 	int s;
    134 
    135 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    136 			SAOST_SR, 1);
    137 
    138 	/* schedule next clock intr */
    139 	oldmatch = saost_sc->sc_clock_count;
    140 	nextmatch = oldmatch + TIMER_FREQUENCY / hz;
    141 
    142 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    143 			  nextmatch);
    144 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    145 				SAOST_CR);
    146 
    147 	if ((nextmatch > oldmatch &&
    148 	     (oscr > nextmatch || oscr < oldmatch)) ||
    149 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    150 		/*
    151 		 * we couldn't set the matching register in time.
    152 		 * just set it to some value so that next interrupt happens.
    153 		 * XXX is it possible to compensate lost interrupts?
    154 		 */
    155 
    156 		s = splhigh();
    157 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    158 					SAOST_CR);
    159 		nextmatch = oscr + 10;
    160 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    161 				  SAOST_MR0, nextmatch);
    162 		splx(s);
    163 	}
    164 
    165 	saost_sc->sc_clock_count = nextmatch;
    166 	hardclock(frame);
    167 
    168 	return 1;
    169 }
    170 
    171 static int
    172 statintr(void *arg)
    173 {
    174 	struct clockframe *frame = arg;
    175 	uint32_t oscr, nextmatch, oldmatch;
    176 	int s;
    177 
    178 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    179 			SAOST_SR, 2);
    180 
    181 	/* schedule next clock intr */
    182 	oldmatch = saost_sc->sc_statclock_count;
    183 	nextmatch = oldmatch + saost_sc->sc_statclock_step;
    184 
    185 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    186 			  nextmatch);
    187 	oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    188 				SAOST_CR);
    189 
    190 	if ((nextmatch > oldmatch &&
    191 	     (oscr > nextmatch || oscr < oldmatch)) ||
    192 	    (nextmatch < oldmatch && oscr > nextmatch && oscr < oldmatch)) {
    193 		/*
    194 		 * we couldn't set the matching register in time.
    195 		 * just set it to some value so that next interrupt happens.
    196 		 * XXX is it possible to compensate lost interrupts?
    197 		 */
    198 
    199 		s = splhigh();
    200 		oscr = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    201 					SAOST_CR);
    202 		nextmatch = oscr + 10;
    203 		bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    204 				  SAOST_MR1, nextmatch);
    205 		splx(s);
    206 	}
    207 
    208 	saost_sc->sc_statclock_count = nextmatch;
    209 	statclock(frame);
    210 
    211 	return 1;
    212 }
    213 
    214 
    215 void
    216 setstatclockrate(int schz)
    217 {
    218 	uint32_t count;
    219 
    220 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / schz;
    221 	count = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR);
    222 	count += saost_sc->sc_statclock_step;
    223 	saost_sc->sc_statclock_count = count;
    224 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    225 			SAOST_MR1, count);
    226 }
    227 
    228 void
    229 cpu_initclocks(void)
    230 {
    231 	stathz = STATHZ;
    232 	profhz = stathz;
    233 	saost_sc->sc_statclock_step = TIMER_FREQUENCY / stathz;
    234 
    235 	printf("clock: hz=%d stathz=%d\n", hz, stathz);
    236 
    237 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    238 	saost_sc->sc_clock_count = TIMER_FREQUENCY / hz;
    239 	saost_sc->sc_statclock_count = TIMER_FREQUENCY / stathz;
    240 
    241 	sa11x0_intr_establish(0, 26, 1, IPL_CLOCK, clockintr, 0);
    242 	sa11x0_intr_establish(0, 27, 1, IPL_CLOCK, statintr, 0);
    243 
    244 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_SR, 0xf);
    245 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_IR, 3);
    246 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR0,
    247 			  saost_sc->sc_clock_count);
    248 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_MR1,
    249 			  saost_sc->sc_statclock_count);
    250 
    251 	/* Zero the counter value */
    252 	bus_space_write_4(saost_sc->sc_iot, saost_sc->sc_ioh, SAOST_CR, 0);
    253 
    254 #ifdef __HAVE_TIMECOUNTER
    255 	saost_tc_init();
    256 #endif /* __HAVE_TIMECOUNTER */
    257 }
    258 
    259 #ifdef __HAVE_TIMECOUNTER
    260 static u_int
    261 saost_tc_get_timecount(struct timecounter *tc)
    262 {
    263 	return (u_int)gettick();
    264 }
    265 
    266 static void
    267 saost_tc_init(void)
    268 {
    269 	static struct timecounter saost_tc = {
    270 		.tc_get_timecount = saost_tc_get_timecount,
    271 		.tc_frequency = TIMER_FREQUENCY,
    272 		.tc_counter_mask = ~0,
    273 		.tc_name = "saost_count",
    274 		.tc_quality = 100,
    275 	};
    276 
    277 	tc_init(&saost_tc);
    278 }
    279 #endif /* __HAVE_TIMECOUNTER */
    280 
    281 static uint32_t
    282 gettick(void)
    283 {
    284 	int counter;
    285 	u_int savedints;
    286 	savedints = disable_interrupts(I32_bit);
    287 
    288 	counter = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    289 			SAOST_CR);
    290 
    291 	restore_interrupts(savedints);
    292 	return counter;
    293 }
    294 
    295 #ifndef __HAVE_TIMECOUNTER
    296 void
    297 microtime(struct timeval *tvp)
    298 {
    299 	int s, tm, deltatm;
    300 	static struct timeval lasttime;
    301 
    302 	if (saost_sc == NULL) {
    303 		tvp->tv_sec = 0;
    304 		tvp->tv_usec = 0;
    305 		return;
    306 	}
    307 
    308 	s = splhigh();
    309 	tm = bus_space_read_4(saost_sc->sc_iot, saost_sc->sc_ioh,
    310 			SAOST_CR);
    311 
    312 	deltatm = saost_sc->sc_clock_count - tm;
    313 
    314 #ifdef OST_DEBUG
    315 	printf("deltatm = %d\n",deltatm);
    316 #endif
    317 
    318 	*tvp = time;
    319 	tvp->tv_usec++;		/* XXX */
    320 	while (tvp->tv_usec >= 1000000) {
    321 		tvp->tv_sec++;
    322 		tvp->tv_usec -= 1000000;
    323 	}
    324 
    325 	if (tvp->tv_sec == lasttime.tv_sec &&
    326 		tvp->tv_usec <= lasttime.tv_usec &&
    327 		(tvp->tv_usec = lasttime.tv_usec + 1) >= 1000000)
    328 	{
    329 		tvp->tv_sec++;
    330 		tvp->tv_usec -= 1000000;
    331 	}
    332 	lasttime = *tvp;
    333 	splx(s);
    334 }
    335 #endif /* !__HAVE_TIMECOUNTER */
    336 
    337 void
    338 delay(u_int usecs)
    339 {
    340 	uint32_t xtick, otick, delta;
    341 	int j, csec, usec;
    342 
    343 	csec = usecs / 10000;
    344 	usec = usecs % 10000;
    345 
    346 	usecs = (TIMER_FREQUENCY / 100) * csec
    347 	    + (TIMER_FREQUENCY / 100) * usec / 10000;
    348 
    349 	if (!saost_sc) {
    350 		/* clock isn't initialized yet */
    351 		for (; usecs > 0; usecs--)
    352 			for (j = 100; j > 0; j--)
    353 				continue;
    354 		return;
    355 	}
    356 
    357 	otick = gettick();
    358 
    359 	while (1) {
    360 		for (j = 100; j > 0; j--)
    361 			continue;
    362 		xtick = gettick();
    363 		delta = xtick - otick;
    364 		if (delta > usecs)
    365 			break;
    366 		usecs -= delta;
    367 		otick = xtick;
    368 	}
    369 }
    370 
    371 #ifndef __HAVE_GENERIC_TODR
    372 void
    373 resettodr(void)
    374 {
    375 }
    376 
    377 void
    378 inittodr(time_t base)
    379 {
    380 	time.tv_sec = base;
    381 	time.tv_usec = 0;
    382 }
    383 #endif /* !__HAVE_GENERIC_TODR */
    384