Home | History | Annotate | Line # | Download | only in mvme
clock_pcctwo.c revision 1.8
      1  1.8      scw /*	$NetBSD: clock_pcctwo.c,v 1.8 2005/06/03 08:42:54 scw Exp $	*/
      2  1.1      scw 
      3  1.1      scw /*-
      4  1.1      scw  * Copyright (c) 1999, 2002 The NetBSD Foundation, Inc.
      5  1.1      scw  * All rights reserved.
      6  1.1      scw  *
      7  1.1      scw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1      scw  * by Steve C. Woodford.
      9  1.1      scw  *
     10  1.1      scw  * Redistribution and use in source and binary forms, with or without
     11  1.1      scw  * modification, are permitted provided that the following conditions
     12  1.1      scw  * are met:
     13  1.1      scw  * 1. Redistributions of source code must retain the above copyright
     14  1.1      scw  *    notice, this list of conditions and the following disclaimer.
     15  1.1      scw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      scw  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      scw  *    documentation and/or other materials provided with the distribution.
     18  1.1      scw  * 3. All advertising materials mentioning features or use of this software
     19  1.1      scw  *    must display the following acknowledgement:
     20  1.1      scw  *        This product includes software developed by the NetBSD
     21  1.1      scw  *        Foundation, Inc. and its contributors.
     22  1.1      scw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1      scw  *    contributors may be used to endorse or promote products derived
     24  1.1      scw  *    from this software without specific prior written permission.
     25  1.1      scw  *
     26  1.1      scw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1      scw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1      scw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1      scw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1      scw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1      scw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1      scw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1      scw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1      scw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1      scw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1      scw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1      scw  */
     38  1.1      scw 
     39  1.1      scw /*
     40  1.1      scw  * Glue for the Peripheral Channel Controller Two (PCCChip2) timers,
     41  1.1      scw  * the Memory Controller ASIC (MCchip, and the Mostek clock chip found
     42  1.1      scw  * on the MVME-1[67]7, MVME-1[67]2 and MVME-187 series of boards.
     43  1.1      scw  */
     44  1.5    lukem 
     45  1.5    lukem #include <sys/cdefs.h>
     46  1.8      scw __KERNEL_RCSID(0, "$NetBSD: clock_pcctwo.c,v 1.8 2005/06/03 08:42:54 scw Exp $");
     47  1.1      scw 
     48  1.1      scw #include <sys/param.h>
     49  1.1      scw #include <sys/kernel.h>
     50  1.1      scw #include <sys/systm.h>
     51  1.1      scw #include <sys/device.h>
     52  1.1      scw 
     53  1.1      scw #include <machine/psl.h>
     54  1.1      scw #include <machine/bus.h>
     55  1.1      scw 
     56  1.1      scw #include <dev/mvme/clockvar.h>
     57  1.1      scw #include <dev/mvme/pcctwovar.h>
     58  1.1      scw #include <dev/mvme/pcctworeg.h>
     59  1.1      scw 
     60  1.1      scw 
     61  1.6    perry int clock_pcctwo_match(struct device *, struct cfdata *, void *);
     62  1.6    perry void clock_pcctwo_attach(struct device *, struct device *, void *);
     63  1.1      scw 
     64  1.1      scw struct clock_pcctwo_softc {
     65  1.1      scw 	struct device sc_dev;
     66  1.1      scw 	struct clock_attach_args sc_clock_args;
     67  1.1      scw 	u_char sc_clock_lvl;
     68  1.1      scw };
     69  1.1      scw 
     70  1.7      scw CFATTACH_DECL(clock_pcctwo, sizeof(struct clock_pcctwo_softc),
     71  1.4  thorpej     clock_pcctwo_match, clock_pcctwo_attach, NULL, NULL);
     72  1.1      scw 
     73  1.1      scw extern struct cfdriver clock_cd;
     74  1.1      scw 
     75  1.6    perry static int clock_pcctwo_profintr(void *);
     76  1.6    perry static int clock_pcctwo_statintr(void *);
     77  1.6    perry static void clock_pcctwo_initclocks(void *, int, int);
     78  1.6    perry static long clock_pcctwo_microtime(void *);
     79  1.6    perry static void clock_pcctwo_shutdown(void *);
     80  1.1      scw 
     81  1.1      scw static struct clock_pcctwo_softc *clock_pcctwo_sc;
     82  1.1      scw 
     83  1.1      scw /* ARGSUSED */
     84  1.1      scw int
     85  1.1      scw clock_pcctwo_match(parent, cf, aux)
     86  1.1      scw 	struct device *parent;
     87  1.1      scw 	struct cfdata *cf;
     88  1.1      scw 	void *aux;
     89  1.1      scw {
     90  1.1      scw 	struct pcctwo_attach_args *pa = aux;
     91  1.1      scw 
     92  1.1      scw 	/* Only one clock, please. */
     93  1.1      scw 	if (clock_pcctwo_sc)
     94  1.1      scw 		return (0);
     95  1.1      scw 
     96  1.1      scw 	if (strcmp(pa->pa_name, clock_cd.cd_name))
     97  1.1      scw 		return (0);
     98  1.1      scw 
     99  1.1      scw 	pa->pa_ipl = cf->pcctwocf_ipl;
    100  1.1      scw 
    101  1.1      scw 	return (1);
    102  1.1      scw }
    103  1.1      scw 
    104  1.1      scw /* ARGSUSED */
    105  1.1      scw void
    106  1.1      scw clock_pcctwo_attach(parent, self, aux)
    107  1.1      scw 	struct device *parent;
    108  1.1      scw 	struct device *self;
    109  1.1      scw 	void *aux;
    110  1.1      scw {
    111  1.1      scw 	struct clock_pcctwo_softc *sc;
    112  1.1      scw 	struct pcctwo_attach_args *pa;
    113  1.1      scw 
    114  1.1      scw 	sc = clock_pcctwo_sc = (struct clock_pcctwo_softc *) self;
    115  1.1      scw 	pa = aux;
    116  1.1      scw 
    117  1.1      scw 	if (pa->pa_ipl != CLOCK_LEVEL)
    118  1.1      scw 		panic("clock_pcctwo_attach: wrong interrupt level");
    119  1.1      scw 
    120  1.1      scw 	sc->sc_clock_args.ca_arg = sc;
    121  1.1      scw 	sc->sc_clock_args.ca_initfunc = clock_pcctwo_initclocks;
    122  1.1      scw 	sc->sc_clock_args.ca_microtime = clock_pcctwo_microtime;
    123  1.1      scw 
    124  1.1      scw 	/* Do common portions of clock config. */
    125  1.1      scw 	clock_config(self, &sc->sc_clock_args, pcctwointr_evcnt(pa->pa_ipl));
    126  1.1      scw 
    127  1.1      scw 	/* Ensure our interrupts get disabled at shutdown time. */
    128  1.1      scw 	(void) shutdownhook_establish(clock_pcctwo_shutdown, NULL);
    129  1.1      scw 
    130  1.1      scw 	sc->sc_clock_lvl = (pa->pa_ipl & PCCTWO_ICR_LEVEL_MASK) |
    131  1.1      scw 	    PCCTWO_ICR_ICLR | PCCTWO_ICR_IEN;
    132  1.1      scw 
    133  1.1      scw 	/* Attach the interrupt handlers. */
    134  1.1      scw 	pcctwointr_establish(PCCTWOV_TIMER1, clock_pcctwo_profintr,
    135  1.1      scw 	    pa->pa_ipl, NULL, &clock_profcnt);
    136  1.1      scw 	pcctwointr_establish(PCCTWOV_TIMER2, clock_pcctwo_statintr,
    137  1.1      scw 	    pa->pa_ipl, NULL, &clock_statcnt);
    138  1.1      scw }
    139  1.1      scw 
    140  1.1      scw void
    141  1.8      scw clock_pcctwo_initclocks(arg, prof_us, stat_us)
    142  1.1      scw 	void *arg;
    143  1.8      scw 	int prof_us;
    144  1.8      scw 	int stat_us;
    145  1.1      scw {
    146  1.1      scw 	struct clock_pcctwo_softc *sc;
    147  1.1      scw 
    148  1.1      scw 	sc = arg;
    149  1.1      scw 
    150  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL, PCCTWO_TT_CTRL_COVF);
    151  1.1      scw 	pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER1_COUNTER, 0);
    152  1.1      scw 	pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER1_COMPARE,
    153  1.8      scw 	    PCCTWO_US2LIM(prof_us));
    154  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL,
    155  1.1      scw 	    PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
    156  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_ICSR, sc->sc_clock_lvl);
    157  1.1      scw 
    158  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL, PCCTWO_TT_CTRL_COVF);
    159  1.1      scw 	pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COUNTER, 0);
    160  1.1      scw 	pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COMPARE,
    161  1.8      scw 	    PCCTWO_US2LIM(stat_us));
    162  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL,
    163  1.1      scw 	    PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
    164  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR, sc->sc_clock_lvl);
    165  1.1      scw }
    166  1.1      scw 
    167  1.1      scw /* ARGSUSED */
    168  1.1      scw long
    169  1.1      scw clock_pcctwo_microtime(arg)
    170  1.1      scw 	void *arg;
    171  1.1      scw {
    172  1.1      scw 	static int ovfl_adj[] = {
    173  1.1      scw 		0,       10000,  20000,  30000,
    174  1.1      scw 		40000,   50000,  60000,  70000,
    175  1.1      scw 		80000,   90000, 100000, 110000,
    176  1.1      scw 		120000, 130000, 140000, 150000};
    177  1.1      scw 	u_int8_t cr;
    178  1.1      scw 	u_int32_t tc, tc2;
    179  1.1      scw 
    180  1.1      scw 	/*
    181  1.1      scw 	 * There's no way to latch the counter and overflow registers
    182  1.1      scw 	 * without pausing the clock, so compensate for the possible
    183  1.1      scw 	 * race by checking for counter wrap-around and re-reading the
    184  1.1      scw 	 * overflow counter if necessary.
    185  1.1      scw 	 *
    186  1.1      scw 	 * Note: This only works because we're called at splhigh().
    187  1.1      scw 	 */
    188  1.1      scw 	tc = pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER);
    189  1.1      scw 	cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
    190  1.1      scw 	if (tc > (tc2 = pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER))) {
    191  1.1      scw 		cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
    192  1.1      scw 		tc = tc2;
    193  1.1      scw 	}
    194  1.1      scw 
    195  1.1      scw 	return ((long) PCCTWO_LIM2US(tc) + ovfl_adj[PCCTWO_TT_CTRL_OVF(cr)]);
    196  1.1      scw }
    197  1.1      scw 
    198  1.1      scw int
    199  1.1      scw clock_pcctwo_profintr(frame)
    200  1.1      scw 	void *frame;
    201  1.1      scw {
    202  1.1      scw 	u_int8_t cr;
    203  1.1      scw 	u_int32_t tc;
    204  1.1      scw 	int s;
    205  1.1      scw 
    206  1.1      scw 	s = splhigh();
    207  1.1      scw 	tc = pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER);
    208  1.1      scw 	cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
    209  1.1      scw 	if (tc > pcc2_reg_read32(sys_pcctwo, PCC2REG_TIMER1_COUNTER))
    210  1.1      scw 		cr = pcc2_reg_read(sys_pcctwo, PCC2REG_TIMER1_CONTROL);
    211  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL,
    212  1.1      scw 	    PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
    213  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_ICSR,
    214  1.1      scw 	    clock_pcctwo_sc->sc_clock_lvl);
    215  1.1      scw 	splx(s);
    216  1.1      scw 
    217  1.1      scw 	for (cr = PCCTWO_TT_CTRL_OVF(cr); cr; cr--)
    218  1.1      scw 		hardclock(frame);
    219  1.1      scw 
    220  1.1      scw 	return (1);
    221  1.1      scw }
    222  1.1      scw 
    223  1.1      scw int
    224  1.1      scw clock_pcctwo_statintr(frame)
    225  1.1      scw 	void *frame;
    226  1.1      scw {
    227  1.1      scw 
    228  1.1      scw 	/* Disable the timer interrupt while we handle it. */
    229  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR, 0);
    230  1.1      scw 
    231  1.1      scw 	statclock((struct clockframe *) frame);
    232  1.1      scw 
    233  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL, PCCTWO_TT_CTRL_COVF);
    234  1.1      scw 	pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COUNTER, 0);
    235  1.1      scw 	pcc2_reg_write32(sys_pcctwo, PCC2REG_TIMER2_COMPARE,
    236  1.1      scw 	    PCCTWO_US2LIM(CLOCK_NEWINT(clock_statvar, clock_statmin)));
    237  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL,
    238  1.1      scw 	    PCCTWO_TT_CTRL_CEN | PCCTWO_TT_CTRL_COC | PCCTWO_TT_CTRL_COVF);
    239  1.1      scw 
    240  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR,
    241  1.1      scw 	    clock_pcctwo_sc->sc_clock_lvl);
    242  1.1      scw 
    243  1.1      scw 	return (1);
    244  1.1      scw }
    245  1.1      scw 
    246  1.1      scw /* ARGSUSED */
    247  1.1      scw void
    248  1.1      scw clock_pcctwo_shutdown(arg)
    249  1.1      scw 	void *arg;
    250  1.1      scw {
    251  1.1      scw 
    252  1.1      scw 	/* Make sure the timer interrupts are turned off. */
    253  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_CONTROL, PCCTWO_TT_CTRL_COVF);
    254  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER1_ICSR, 0);
    255  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_CONTROL, PCCTWO_TT_CTRL_COVF);
    256  1.1      scw 	pcc2_reg_write(sys_pcctwo, PCC2REG_TIMER2_ICSR, 0);
    257  1.1      scw }
    258