Home | History | Annotate | Line # | Download | only in dev
plumicu.c revision 1.7
      1 /*	$NetBSD: plumicu.c,v 1.7 2002/10/02 05:26:46 thorpej Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 1999, 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by UCHIYAMA Yasushi.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/device.h>
     42 
     43 #include <machine/bus.h>
     44 #include <machine/intr.h>
     45 
     46 #include <hpcmips/tx/tx39var.h>
     47 #include <hpcmips/dev/plumvar.h>
     48 #include <hpcmips/dev/plumicuvar.h>
     49 #include <hpcmips/dev/plumicureg.h>
     50 
     51 #ifdef PLUMICUDEBUG
     52 #define	DPRINTF(arg) printf arg
     53 #else
     54 #define	DPRINTF(arg)
     55 #endif
     56 
     57 int plumicu_match(struct device *, struct cfdata *, void *);
     58 void plumicu_attach(struct device *, struct device *, void *);
     59 int plumicu_intr(void *);
     60 
     61 static __inline__ void plum_di(plum_chipset_tag_t);
     62 static __inline__ void plum_ei(plum_chipset_tag_t);
     63 
     64 const struct plum_intr_ctrl {
     65 	plumreg_t	ic_ackpat1;
     66 	plumreg_t	ic_ackpat2;	int	ic_ackreg2;
     67 	plumreg_t	ic_ienpat;	int	ic_ienreg;
     68 	plumreg_t	ic_senpat;	int	ic_senreg;
     69 } pi_ctrl[PLUM_INTR_MAX] = {
     70 	[PLUM_INT_C1IO]	= {PLUM_INT_INTSTA_PCCINT,
     71 			   PLUM_INT_PCCINTS_C1IO,	PLUM_INT_PCCINTS_REG,
     72 			   PLUM_INT_PCCIEN_IENC1IO,	PLUM_INT_PCCIEN_REG,
     73 			   PLUM_INT_PCCIEN_SENC1IO,	PLUM_INT_PCCIEN_REG
     74 	},
     75 	[PLUM_INT_C1RI] = {PLUM_INT_INTSTA_PCCINT,
     76 			   PLUM_INT_PCCINTS_C1RI,	PLUM_INT_PCCINTS_REG,
     77 			   PLUM_INT_PCCIEN_IENC1RI,	PLUM_INT_PCCIEN_REG,
     78 			   PLUM_INT_PCCIEN_SENC1RI,	PLUM_INT_PCCIEN_REG
     79 	},
     80 	[PLUM_INT_C1SC] = {PLUM_INT_INTSTA_C1SCINT,	0, 0, 0, 0, 0, 0},
     81 	[PLUM_INT_C2IO] = {PLUM_INT_INTSTA_PCCINT,
     82 			   PLUM_INT_PCCINTS_C2IO,	PLUM_INT_PCCINTS_REG,
     83 			   PLUM_INT_PCCIEN_IENC2IO,	PLUM_INT_PCCIEN_REG,
     84 			   PLUM_INT_PCCIEN_SENC2IO,	PLUM_INT_PCCIEN_REG
     85 	},
     86 	[PLUM_INT_C2RI] = {PLUM_INT_INTSTA_PCCINT,
     87 			   PLUM_INT_PCCINTS_C2RI,	PLUM_INT_PCCINTS_REG,
     88 			   PLUM_INT_PCCIEN_IENC2RI,	PLUM_INT_PCCIEN_REG,
     89 			   PLUM_INT_PCCIEN_SENC2RI,	PLUM_INT_PCCIEN_REG
     90 	},
     91 	[PLUM_INT_C2SC] = {PLUM_INT_INTSTA_C2SCINT,	0, 0, 0, 0, 0, 0},
     92 	[PLUM_INT_DISP] = {PLUM_INT_INTSTA_DISPINT,	0, 0, 0, 0, 0, 0},
     93 	[PLUM_INT_USB]  = {PLUM_INT_INTSTA_USBINT,
     94 			   0, 0,
     95 			   PLUM_INT_USBINTEN_IEN,	PLUM_INT_USBINTEN_REG,
     96 			   0, 0
     97 	},
     98 	[PLUM_INT_USBWAKE] = {PLUM_INT_INTSTA_USBWAKE,
     99 			   0, 0,
    100 			   PLUM_INT_USBINTEN_WIEN,	PLUM_INT_USBINTEN_REG,
    101 			   0, 0
    102 	},
    103 	[PLUM_INT_SM]	= {PLUM_INT_INTSTA_SMINT,
    104 			   0, 0,
    105 			   PLUM_INT_SMIEN,		PLUM_INT_SMIEN_REG,
    106 			   0, 0
    107 	},
    108 	[PLUM_INT_EXT5IO0] = {PLUM_INT_INTSTA_EXTINT,
    109 			   PLUM_INT_EXTINTS_IO5INT0,	PLUM_INT_EXTINTS_REG,
    110 			   PLUM_INT_EXTIEN_IENIO5INT0,	PLUM_INT_EXTIEN_REG,
    111 			   PLUM_INT_EXTIEN_SENIO5INT0,	PLUM_INT_EXTIEN_REG,
    112 	},
    113 	[PLUM_INT_EXT5IO1] = {PLUM_INT_INTSTA_EXTINT,
    114 			   PLUM_INT_EXTINTS_IO5INT1,	PLUM_INT_EXTINTS_REG,
    115 			   PLUM_INT_EXTIEN_IENIO5INT1,	PLUM_INT_EXTIEN_REG,
    116 			   PLUM_INT_EXTIEN_SENIO5INT1,	PLUM_INT_EXTIEN_REG,
    117 	},
    118 	[PLUM_INT_EXT5IO2] = {PLUM_INT_INTSTA_EXTINT,
    119 			   PLUM_INT_EXTINTS_IO5INT2,	PLUM_INT_EXTINTS_REG,
    120 			   PLUM_INT_EXTIEN_IENIO5INT2,	PLUM_INT_EXTIEN_REG,
    121 			   PLUM_INT_EXTIEN_SENIO5INT2,	PLUM_INT_EXTIEN_REG,
    122 	},
    123 	[PLUM_INT_EXT5IO3] = {PLUM_INT_INTSTA_EXTINT,
    124 			   PLUM_INT_EXTINTS_IO5INT3,	PLUM_INT_EXTINTS_REG,
    125 			   PLUM_INT_EXTIEN_IENIO5INT0,	PLUM_INT_EXTIEN_REG,
    126 			   PLUM_INT_EXTIEN_SENIO5INT0,	PLUM_INT_EXTIEN_REG,
    127 	},
    128 	[PLUM_INT_EXT3IO0] = {PLUM_INT_INTSTA_EXTINT,
    129 			   PLUM_INT_EXTINTS_IO3INT0,	PLUM_INT_EXTINTS_REG,
    130 			   PLUM_INT_EXTIEN_IENIO3INT0,	PLUM_INT_EXTIEN_REG,
    131 			   PLUM_INT_EXTIEN_SENIO3INT0,	PLUM_INT_EXTIEN_REG,
    132 	},
    133 	[PLUM_INT_EXT3IO1] = {PLUM_INT_INTSTA_EXTINT,
    134 			   PLUM_INT_EXTINTS_IO3INT1,	PLUM_INT_EXTINTS_REG,
    135 			   PLUM_INT_EXTIEN_IENIO3INT1,	PLUM_INT_EXTIEN_REG,
    136 			   PLUM_INT_EXTIEN_SENIO3INT1,	PLUM_INT_EXTIEN_REG,
    137 	}
    138 };
    139 
    140 struct plum_intr_entry {
    141 	int pi_enabled;
    142 	int pi_line;
    143 	int (*pi_fun)(void *);
    144 	void *pi_arg;
    145 	const struct plum_intr_ctrl *pi_ctrl;
    146 };
    147 
    148 struct plumicu_softc {
    149 	struct	device		sc_dev;
    150 	plum_chipset_tag_t	sc_pc;
    151 	bus_space_tag_t		sc_regt;
    152 	bus_space_handle_t	sc_regh;
    153 	void			*sc_ih;
    154 	int			sc_enable_count;
    155 	struct plum_intr_entry  sc_intr[PLUM_INTR_MAX];
    156 };
    157 
    158 CFATTACH_DECL(plumicu, sizeof(struct plumicu_softc),
    159     plumicu_match, plumicu_attach, NULL, NULL);
    160 
    161 #ifdef PLUMICUDEBUG
    162 void plumicu_dump(struct plumicu_softc *);
    163 #endif
    164 
    165 int
    166 plumicu_match(struct device *parent, struct cfdata *cf, void *aux)
    167 {
    168 
    169 	return (2); /* 1st attach group */
    170 }
    171 
    172 void
    173 plumicu_attach(struct device *parent, struct device *self, void *aux)
    174 {
    175 	struct plum_attach_args *pa = aux;
    176 	struct plumicu_softc *sc = (void*)self;
    177 	const struct plum_intr_ctrl *pic;
    178 	bus_space_tag_t regt;
    179 	bus_space_handle_t regh;
    180 	plumreg_t reg;
    181 	int i;
    182 
    183 	sc->sc_pc	= pa->pa_pc;
    184 	sc->sc_regt	= pa->pa_regt;
    185 
    186 	/* map plum2 interrupt controller register space */
    187 	if (bus_space_map(sc->sc_regt, PLUM_INT_REGBASE,
    188 			  PLUM_INT_REGSIZE, 0, &sc->sc_regh)) {
    189 		printf(":interrupt register map failed\n");
    190 		return;
    191 	}
    192 #ifdef PLUMICUDEBUG
    193 	plumicu_dump(sc);
    194 #endif
    195 	/* disable all interrupt */
    196 	regt = sc->sc_regt;
    197 	regh = sc->sc_regh;
    198 	for (i = 0; i < PLUM_INTR_MAX; i++) {
    199 		pic = &pi_ctrl[i];
    200 		if (pic->ic_ienreg) {
    201 			reg = plum_conf_read(regt, regh, pic->ic_ienreg);
    202 			reg &= ~pic->ic_ienpat;
    203 			plum_conf_write(regt, regh, pic->ic_ienreg, reg);
    204 		}
    205 		if (pic->ic_senreg) {
    206 			reg = plum_conf_read(regt, regh, pic->ic_senreg);
    207 			reg &= ~pic->ic_senpat;
    208 			plum_conf_write(regt, regh, pic->ic_senreg, reg);
    209 		}
    210 	}
    211 
    212 	/* register handle to plum_chipset_tag */
    213 	plum_conf_register_intr(sc->sc_pc, (void*)sc);
    214 
    215 	/* disable interrupt redirect to TX39 core */
    216 	plum_di(sc->sc_pc);
    217 
    218 	if (!(sc->sc_ih = tx_intr_establish(sc->sc_pc->pc_tc, pa->pa_irq,
    219 					    IST_EDGE, IPL_BIO,
    220 					    plumicu_intr, sc))) {
    221 		printf(": can't establish interrupt\n");
    222 	}
    223 	printf("\n");
    224 }
    225 
    226 __inline__ void
    227 plum_di(plum_chipset_tag_t pc)
    228 {
    229 	struct plumicu_softc *sc = pc->pc_intrt;
    230 
    231 	plum_conf_write(sc->sc_regt, sc->sc_regh, PLUM_INT_INTIEN_REG, 0);
    232 }
    233 
    234 __inline__ void
    235 plum_ei(plum_chipset_tag_t pc)
    236 {
    237 	struct plumicu_softc *sc = pc->pc_intrt;
    238 
    239 	plum_conf_write(sc->sc_regt, sc->sc_regh, PLUM_INT_INTIEN_REG,
    240 			PLUM_INT_INTIEN);
    241 }
    242 
    243 void*
    244 plum_intr_establish(plum_chipset_tag_t pc, int line, int mode, int level,
    245     int (*ih_fun)(void *), void *ih_arg)
    246 {
    247 	struct plumicu_softc *sc = pc->pc_intrt;
    248 	bus_space_tag_t regt = sc->sc_regt;
    249 	bus_space_handle_t regh = sc->sc_regh;
    250 	plumreg_t reg;
    251 	struct plum_intr_entry *pi;
    252 
    253 	if (!LEGAL_PRUM_INTR(line)) {
    254 		panic("plum_intr_establish: bogus interrupt line");
    255 	}
    256 
    257 	pi = &sc->sc_intr[line];
    258 	pi->pi_line = line;
    259 	pi->pi_fun  = ih_fun;
    260 	pi->pi_arg  = ih_arg;
    261 	pi->pi_ctrl = &pi_ctrl[line];
    262 
    263 	/* Enable interrupt */
    264 
    265 	/* status enable */
    266 	if (pi->pi_ctrl->ic_senreg) {
    267 		reg = plum_conf_read(regt, regh, pi->pi_ctrl->ic_senreg);
    268 		reg |= pi->pi_ctrl->ic_senpat;
    269 		plum_conf_write(regt, regh, pi->pi_ctrl->ic_senreg, reg);
    270 	}
    271 	/* interrupt enable */
    272 	if (pi->pi_ctrl->ic_ienreg) {
    273 		reg = plum_conf_read(regt, regh, pi->pi_ctrl->ic_ienreg);
    274 		reg |= pi->pi_ctrl->ic_ienpat;
    275 		plum_conf_write(regt, regh, pi->pi_ctrl->ic_ienreg, reg);
    276 	}
    277 
    278 	/* Enable redirect to TX39 core */
    279 	DPRINTF(("plum_intr_establish: %d (count=%d)\n", line,
    280 		 sc->sc_enable_count));
    281 
    282 	if (sc->sc_enable_count++ == 0)
    283 		plum_ei(pc);
    284 
    285 	pi->pi_enabled = 1;
    286 
    287 	return (ih_fun);
    288 }
    289 
    290 void
    291 plum_intr_disestablish(plum_chipset_tag_t pc, void *arg)
    292 {
    293 	struct plumicu_softc *sc = pc->pc_intrt;
    294 	bus_space_tag_t regt = sc->sc_regt;
    295 	bus_space_handle_t regh = sc->sc_regh;
    296 	plumreg_t reg;
    297 	struct plum_intr_entry *pi;
    298 	int i;
    299 
    300 	sc = pc->pc_intrt;
    301 
    302 	for (i = 0; i < PLUM_INTR_MAX; i++) {
    303 		pi = &sc->sc_intr[i];
    304 		if (pi->pi_fun != arg)
    305 			continue;
    306 		DPRINTF(("plum_intr_disestablish: %d (count=%d)\n",
    307 			 pi->pi_line, sc->sc_enable_count - 1));
    308 		goto found;
    309 	}
    310 	panic("plum_intr_disestablish: can't find entry.");
    311 	/* NOTREACHED */
    312  found:
    313 	pi->pi_enabled = 0;
    314 	/* Disable interrupt */
    315 	if (pi->pi_ctrl->ic_ienreg) {
    316 		reg = plum_conf_read(regt, regh, pi->pi_ctrl->ic_ienreg);
    317 		reg &= ~(pi->pi_ctrl->ic_ienpat);
    318 		plum_conf_write(regt, regh, pi->pi_ctrl->ic_ienreg, reg);
    319 	}
    320 	if (pi->pi_ctrl->ic_senreg) {
    321 		reg = plum_conf_read(regt, regh, pi->pi_ctrl->ic_senreg);
    322 		reg &= ~(pi->pi_ctrl->ic_senpat);
    323 		plum_conf_write(regt, regh, pi->pi_ctrl->ic_senreg, reg);
    324 	}
    325 
    326 	/* Disable/Enable interrupt redirect to TX39 core */
    327 	if (--sc->sc_enable_count == 0)
    328 		plum_di(pc);
    329 }
    330 
    331 int
    332 plumicu_intr(void *arg)
    333 {
    334 	struct plumicu_softc *sc = arg;
    335 	bus_space_tag_t regt = sc->sc_regt;
    336 	bus_space_handle_t regh = sc->sc_regh;
    337 	plumreg_t reg1, reg2, reg_ext, reg_pccard;
    338 	int i;
    339 
    340 	plum_di(sc->sc_pc);
    341 	/* read level 1 status */
    342 	reg1 = plum_conf_read(regt, regh, PLUM_INT_INTSTA_REG);
    343 
    344 	/* read level 2 status and acknowledge */
    345 	reg_ext = plum_conf_read(regt, regh, PLUM_INT_EXTINTS_REG);
    346 	plum_conf_write(regt, regh, PLUM_INT_EXTINTS_REG, reg_ext);
    347 
    348 	reg_pccard = plum_conf_read(regt, regh, PLUM_INT_PCCINTS_REG);
    349 	plum_conf_write(regt, regh, PLUM_INT_PCCINTS_REG, reg_pccard);
    350 
    351 	for (i = 0; i < PLUM_INTR_MAX; i++) {
    352 		register struct plum_intr_entry *pi;
    353 		register const struct plum_intr_ctrl *pic = &pi_ctrl[i];
    354 
    355 		if (!(pic->ic_ackpat1 & reg1))
    356 			continue;
    357 
    358 		pi = &sc->sc_intr[i];
    359 		if (!pi->pi_enabled)
    360 			continue;
    361 
    362 		if (pic->ic_ackreg2 == 0) {
    363 			(*pi->pi_fun)(pi->pi_arg);
    364 			continue;
    365 		}
    366 
    367 		reg2 = pic->ic_ackreg2 == PLUM_INT_PCCINTS_REG
    368 			? reg_pccard : reg_ext;
    369 
    370 		if (pic->ic_ackpat2 & reg2)
    371 			(*pi->pi_fun)(pi->pi_arg);
    372 	}
    373 	plum_ei(sc->sc_pc);
    374 
    375 	return (0);
    376 }
    377 
    378 #ifdef PLUMICUDEBUG
    379 void
    380 plumicu_dump(struct plumicu_softc *sc)
    381 {
    382 	bus_space_tag_t regt = sc->sc_regt;
    383 	bus_space_handle_t regh = sc->sc_regh;
    384 	plumreg_t reg;
    385 
    386 	printf("status:");
    387 	reg = plum_conf_read(regt, regh, PLUM_INT_INTSTA_REG);
    388 	dbg_bit_print(reg);
    389 	printf("ExtIO\n");
    390 	printf("status:");
    391 	reg = plum_conf_read(regt, regh, PLUM_INT_EXTINTS_REG);
    392 	dbg_bit_print(reg);
    393 	printf("enable:");
    394 	reg = plum_conf_read(regt, regh, PLUM_INT_EXTIEN_REG);
    395  	dbg_bit_print(reg);
    396 
    397 }
    398 #endif /* PLUMICUDEBUG */
    399