Home | History | Annotate | Line # | Download | only in s3c2xx0
      1  1.18     rin /* $NetBSD: s3c2800_clk.c,v 1.18 2020/05/29 12:30:39 rin Exp $ */
      2   1.1     bsh 
      3   1.1     bsh /*
      4   1.1     bsh  * Copyright (c) 2002 Fujitsu Component Limited
      5   1.1     bsh  * Copyright (c) 2002 Genetec Corporation
      6   1.1     bsh  * All rights reserved.
      7   1.1     bsh  *
      8   1.1     bsh  * Redistribution and use in source and binary forms, with or without
      9   1.1     bsh  * modification, are permitted provided that the following conditions
     10   1.1     bsh  * are met:
     11   1.1     bsh  * 1. Redistributions of source code must retain the above copyright
     12   1.1     bsh  *    notice, this list of conditions and the following disclaimer.
     13   1.1     bsh  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.1     bsh  *    notice, this list of conditions and the following disclaimer in the
     15   1.1     bsh  *    documentation and/or other materials provided with the distribution.
     16   1.1     bsh  * 3. Neither the name of The Fujitsu Component Limited nor the name of
     17   1.1     bsh  *    Genetec corporation may not be used to endorse or promote products
     18   1.1     bsh  *    derived from this software without specific prior written permission.
     19   1.1     bsh  *
     20   1.1     bsh  * THIS SOFTWARE IS PROVIDED BY FUJITSU COMPONENT LIMITED AND GENETEC
     21   1.1     bsh  * CORPORATION ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     22   1.1     bsh  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     23   1.1     bsh  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     24   1.1     bsh  * DISCLAIMED.  IN NO EVENT SHALL FUJITSU COMPONENT LIMITED OR GENETEC
     25   1.1     bsh  * CORPORATION BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     26   1.1     bsh  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     27   1.1     bsh  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
     28   1.1     bsh  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
     29   1.1     bsh  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     30   1.1     bsh  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     31   1.1     bsh  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     32   1.1     bsh  * SUCH DAMAGE.
     33   1.1     bsh  */
     34   1.1     bsh 
     35   1.1     bsh 
     36   1.5   lukem #include <sys/cdefs.h>
     37  1.18     rin __KERNEL_RCSID(0, "$NetBSD: s3c2800_clk.c,v 1.18 2020/05/29 12:30:39 rin Exp $");
     38   1.5   lukem 
     39   1.1     bsh #include <sys/param.h>
     40   1.1     bsh #include <sys/systm.h>
     41   1.1     bsh #include <sys/kernel.h>
     42  1.12   joerg #include <sys/atomic.h>
     43   1.1     bsh #include <sys/time.h>
     44  1.12   joerg #include <sys/timetc.h>
     45   1.1     bsh 
     46  1.16  dyoung #include <sys/bus.h>
     47   1.1     bsh #include <machine/intr.h>
     48   1.1     bsh #include <arm/cpufunc.h>
     49   1.1     bsh 
     50   1.1     bsh #include <arm/s3c2xx0/s3c2800reg.h>
     51   1.1     bsh #include <arm/s3c2xx0/s3c2800var.h>
     52   1.1     bsh 
     53   1.1     bsh 
     54   1.1     bsh #ifndef STATHZ
     55   1.1     bsh #define STATHZ	64
     56   1.1     bsh #endif
     57   1.1     bsh 
     58   1.4     bsh #define TIMER_FREQUENCY(pclk) ((pclk)/32) /* divider=1/32 */
     59   1.1     bsh 
     60   1.1     bsh static unsigned int timer0_reload_value;
     61   1.1     bsh static unsigned int timer0_prescaler;
     62   1.4     bsh static unsigned int timer0_mseccount;
     63   1.4     bsh 
     64   1.4     bsh #define usec_to_counter(t)	\
     65   1.4     bsh 	((timer0_mseccount*(t))/1000)
     66   1.1     bsh 
     67   1.4     bsh #define counter_to_usec(c,pclk)	\
     68   1.4     bsh 	(((c)*timer0_prescaler*1000)/(TIMER_FREQUENCY(pclk)/1000))
     69   1.1     bsh 
     70  1.12   joerg static u_int	s3c2800_get_timecount(struct timecounter *);
     71  1.12   joerg 
     72  1.12   joerg static struct timecounter s3c2800_timecounter = {
     73  1.18     rin 	.tc_get_timecount = s3c2800_get_timecount,
     74  1.18     rin 	.tc_counter_mask = 0xffffffff,
     75  1.18     rin 	.tc_name = "s3c2800",
     76  1.18     rin 	.tc_quality = 100,
     77  1.12   joerg };
     78  1.12   joerg 
     79  1.12   joerg static volatile uint32_t s3c2800_base;
     80  1.12   joerg 
     81  1.12   joerg static u_int
     82  1.12   joerg s3c2800_get_timecount(struct timecounter *tc)
     83   1.1     bsh {
     84   1.1     bsh 	struct s3c2800_softc *sc = (struct s3c2800_softc *) s3c2xx0_softc;
     85  1.12   joerg 	int save, int_pend0, int_pend1, count;
     86   1.1     bsh 
     87   1.1     bsh 	save = disable_interrupts(I32_bit);
     88   1.1     bsh 
     89   1.1     bsh  again:
     90   1.1     bsh 	int_pend0 = S3C2800_INT_TIMER0 &
     91   1.1     bsh 	    bus_space_read_4(sc->sc_sx.sc_iot, sc->sc_sx.sc_intctl_ioh,
     92   1.1     bsh 		INTCTL_SRCPND);
     93   1.1     bsh 	count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_tmr0_ioh,
     94   1.1     bsh 	    TIMER_TMCNT);
     95   1.1     bsh 
     96   1.1     bsh 	for (;;){
     97   1.1     bsh 
     98   1.1     bsh 		int_pend1 = S3C2800_INT_TIMER0 &
     99   1.1     bsh 		    bus_space_read_4(sc->sc_sx.sc_iot, sc->sc_sx.sc_intctl_ioh,
    100   1.1     bsh 			INTCTL_SRCPND);
    101   1.1     bsh 		if( int_pend0 == int_pend1 )
    102   1.1     bsh 			break;
    103   1.1     bsh 
    104   1.1     bsh 		/*
    105   1.1     bsh 		 * Down counter reached to zero while we were reading
    106   1.1     bsh 		 * timer values. do it again to get consistent values.
    107   1.1     bsh 		 */
    108   1.1     bsh 		int_pend0 = int_pend1;
    109   1.1     bsh 		count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_tmr0_ioh,
    110   1.1     bsh 		    TIMER_TMCNT);
    111   1.1     bsh 	}
    112   1.1     bsh 
    113   1.1     bsh 	if( __predict_false(count > timer0_reload_value) ){
    114   1.1     bsh 		/*
    115   1.1     bsh 		 * Buggy Hardware Warning --- sometimes timer counter
    116   1.1     bsh 		 * reads bogus value like 0xffff.  I guess it happens when
    117   1.1     bsh 		 * the timer is reloaded.
    118   1.1     bsh 		 */
    119   1.1     bsh #if 0
    120   1.1     bsh 		printf( "Bogus value from timer counter: %d\n", count );
    121   1.1     bsh #endif
    122   1.1     bsh 		goto again;
    123   1.1     bsh 	}
    124   1.1     bsh 
    125   1.1     bsh 	restore_interrupts(save);
    126   1.1     bsh 
    127  1.12   joerg 	if (int_pend1)
    128  1.12   joerg 		count -= timer0_reload_value;
    129   1.1     bsh 
    130  1.12   joerg 	return s3c2800_base - count;
    131   1.1     bsh }
    132   1.1     bsh 
    133   1.9   perry static inline int
    134   1.1     bsh read_timer(struct s3c2800_softc *sc)
    135   1.1     bsh {
    136   1.1     bsh 	int count;
    137   1.1     bsh 
    138   1.1     bsh 	do {
    139   1.1     bsh 		count = bus_space_read_2(sc->sc_sx.sc_iot, sc->sc_tmr0_ioh,
    140   1.1     bsh 		    TIMER_TMCNT);
    141   1.1     bsh 	} while ( __predict_false(count > timer0_reload_value) );
    142   1.1     bsh 
    143   1.1     bsh 	return count;
    144   1.1     bsh }
    145   1.1     bsh 
    146   1.1     bsh /*
    147   1.1     bsh  * delay:
    148   1.1     bsh  *
    149   1.1     bsh  *	Delay for at least N microseconds.
    150   1.1     bsh  */
    151   1.1     bsh void
    152   1.1     bsh delay(u_int n)
    153   1.1     bsh {
    154   1.1     bsh 	struct s3c2800_softc *sc = (struct s3c2800_softc *) s3c2xx0_softc;
    155   1.1     bsh 	int v0, v1, delta;
    156   1.4     bsh 	u_int ucnt;
    157   1.1     bsh 
    158   1.1     bsh 	if ( timer0_reload_value == 0 ){
    159   1.1     bsh 		/* not initialized yet */
    160   1.1     bsh 		while ( n-- > 0 ){
    161   1.1     bsh 			int m;
    162   1.1     bsh 
    163   1.1     bsh 			for (m=0; m<100; ++m )
    164   1.1     bsh 				;
    165   1.1     bsh 		}
    166   1.1     bsh 		return;
    167   1.1     bsh 	}
    168   1.1     bsh 
    169   1.1     bsh 	/* read down counter */
    170   1.1     bsh 	v0 = read_timer(sc);
    171   1.1     bsh 
    172   1.4     bsh 	ucnt = usec_to_counter(n);
    173   1.4     bsh 
    174   1.4     bsh 	while( ucnt > 0 ) {
    175   1.1     bsh 		v1 = read_timer(sc);
    176   1.1     bsh 		delta = v0 - v1;
    177   1.4     bsh 		if ( delta < 0 )
    178   1.1     bsh 			delta += timer0_reload_value;
    179   1.1     bsh #ifdef DEBUG
    180   1.1     bsh 		if (delta < 0 || delta > timer0_reload_value)
    181   1.1     bsh 			panic("wrong value from timer counter");
    182   1.1     bsh #endif
    183   1.1     bsh 
    184   1.4     bsh 		if((u_int)delta < ucnt){
    185   1.4     bsh 			ucnt -= (u_int)delta;
    186   1.4     bsh 			v0 = v1;
    187   1.4     bsh 		}
    188   1.4     bsh 		else {
    189   1.4     bsh 			ucnt = 0;
    190   1.4     bsh 		}
    191   1.1     bsh 	}
    192   1.1     bsh 	/*NOTREACHED*/
    193   1.1     bsh }
    194   1.4     bsh 
    195   1.1     bsh void
    196   1.7      he setstatclockrate(int newhz)
    197   1.1     bsh {
    198   1.1     bsh }
    199   1.1     bsh 
    200  1.12   joerg static int
    201  1.12   joerg hardintr(void *arg)
    202  1.12   joerg {
    203  1.13   joerg 	atomic_add_32(&s3c2800_base, timer0_reload_value);
    204   1.1     bsh 
    205  1.12   joerg 	hardclock((struct clockframe *)arg);
    206  1.12   joerg 
    207  1.12   joerg 	return 1;
    208  1.12   joerg }
    209   1.1     bsh 
    210  1.13   joerg static int
    211  1.13   joerg statintr(void *arg)
    212  1.13   joerg {
    213  1.13   joerg 	statclock((struct clockframe *)arg);
    214  1.13   joerg 
    215  1.13   joerg 	return 1;
    216  1.13   joerg }
    217  1.13   joerg 
    218   1.1     bsh void
    219  1.15  cegger cpu_initclocks(void)
    220   1.1     bsh {
    221   1.4     bsh 	struct s3c2800_softc *sc = (struct s3c2800_softc *)s3c2xx0_softc;
    222   1.1     bsh 	long tc;
    223   1.1     bsh 	int prescaler;
    224   1.4     bsh 	int pclk = s3c2xx0_softc->sc_pclk;
    225   1.1     bsh 
    226   1.1     bsh 	stathz = STATHZ;
    227   1.1     bsh 	profhz = stathz;
    228   1.1     bsh 
    229   1.1     bsh #define calc_time_constant(hz)					\
    230   1.1     bsh 	do {							\
    231   1.1     bsh 		prescaler = 1;					\
    232   1.1     bsh 		do {						\
    233   1.1     bsh 			++prescaler;				\
    234   1.4     bsh 			tc = TIMER_FREQUENCY(pclk) /(hz)/ prescaler;	\
    235   1.1     bsh 		} while( tc > 65536 );				\
    236   1.1     bsh 	} while(0)
    237   1.1     bsh 
    238   1.1     bsh 
    239   1.1     bsh 
    240   1.1     bsh 	/* Use the channels 0 and 1 for hardclock and statclock, respectively */
    241   1.4     bsh 	bus_space_write_4(sc->sc_sx.sc_iot, sc->sc_tmr0_ioh, TIMER_TMCON, 0);
    242   1.4     bsh 	bus_space_write_4(sc->sc_sx.sc_iot, sc->sc_tmr1_ioh, TIMER_TMCON, 0);
    243   1.4     bsh 
    244   1.1     bsh 	calc_time_constant(hz);
    245   1.1     bsh 	bus_space_write_4(sc->sc_sx.sc_iot, sc->sc_tmr0_ioh, TIMER_TMDAT,
    246   1.1     bsh 	    ((prescaler - 1) << 16) | (tc - 1));
    247   1.1     bsh 	timer0_prescaler = prescaler;
    248   1.1     bsh 	timer0_reload_value = tc;
    249   1.4     bsh 	timer0_mseccount = TIMER_FREQUENCY(pclk)/timer0_prescaler/1000 ;
    250   1.1     bsh 
    251   1.1     bsh 	printf("clock: hz=%d stathz = %d PCLK=%d prescaler=%d tc=%ld\n",
    252   1.4     bsh 	    hz, stathz, pclk, prescaler, tc);
    253   1.1     bsh 
    254   1.1     bsh 	calc_time_constant(stathz);
    255   1.1     bsh 	bus_space_write_4(sc->sc_sx.sc_iot, sc->sc_tmr1_ioh, TIMER_TMDAT,
    256   1.1     bsh 	    ((prescaler - 1) << 16) | (tc - 1));
    257   1.1     bsh 
    258   1.1     bsh 
    259   1.4     bsh 	s3c2800_intr_establish(S3C2800_INT_TIMER0, IPL_CLOCK,
    260   1.4     bsh 			       IST_NONE, hardintr, 0);
    261  1.11    matt 	s3c2800_intr_establish(S3C2800_INT_TIMER1, IPL_HIGH,
    262   1.4     bsh 			       IST_NONE, statintr, 0);
    263   1.1     bsh 
    264   1.1     bsh 	/* start timers */
    265   1.4     bsh 	bus_space_write_4(sc->sc_sx.sc_iot, sc->sc_tmr0_ioh, TIMER_TMCON,
    266   1.4     bsh 	    TMCON_MUX_DIV32|TMCON_INTENA|TMCON_ENABLE);
    267   1.4     bsh 	bus_space_write_4(sc->sc_sx.sc_iot, sc->sc_tmr1_ioh, TIMER_TMCON,
    268   1.4     bsh 	    TMCON_MUX_DIV4|TMCON_INTENA|TMCON_ENABLE);
    269   1.1     bsh 
    270   1.1     bsh 	/* stop timer2 */
    271   1.1     bsh 	{
    272   1.1     bsh 		bus_space_handle_t tmp_ioh;
    273   1.1     bsh 
    274   1.1     bsh 		bus_space_map(sc->sc_sx.sc_iot, S3C2800_TIMER2_BASE,
    275   1.1     bsh 		    S3C2800_TIMER_SIZE, 0, &tmp_ioh);
    276   1.1     bsh 
    277   1.1     bsh 		bus_space_write_4(sc->sc_sx.sc_iot, tmp_ioh,
    278   1.1     bsh 		    TIMER_TMCON, 0);
    279   1.1     bsh 
    280   1.1     bsh 		bus_space_unmap(sc->sc_sx.sc_iot, tmp_ioh,
    281   1.1     bsh 		    S3C2800_TIMER_SIZE);
    282   1.1     bsh 
    283   1.1     bsh 	}
    284  1.12   joerg 
    285  1.14     bsh 	s3c2800_timecounter.tc_frequency = TIMER_FREQUENCY(pclk) / timer0_prescaler;
    286  1.12   joerg 	tc_init(&s3c2800_timecounter);
    287   1.1     bsh }
    288