Home | History | Annotate | Line # | Download | only in pci
pci_1000a.c revision 1.21
      1 /* $NetBSD: pci_1000a.c,v 1.21 2009/03/14 14:45:53 dsl Exp $ */
      2 
      3 /*
      4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is based on pci_kn20aa.c, written by Chris G. Demetriou at
      8  * Carnegie-Mellon University. Platform support for Noritake, Pintake, and
      9  * Corelle by Ross Harvey with copyright assignment by permission of Avalon
     10  * Computer Systems, Inc.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  *
     21  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     23  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     24  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     25  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     31  * POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 1995, 1996 Carnegie-Mellon University.
     36  * All rights reserved.
     37  *
     38  * Author: Chris G. Demetriou
     39  *
     40  * Permission to use, copy, modify and distribute this software and
     41  * its documentation is hereby granted, provided that both the copyright
     42  * notice and this permission notice appear in all copies of the
     43  * software, derivative works or modified versions, and any portions
     44  * thereof, and that both notices appear in supporting documentation.
     45  *
     46  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
     47  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
     48  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
     49  *
     50  * Carnegie Mellon requests users of this software to return to
     51  *
     52  *  Software Distribution Coordinator  or  Software.Distribution (at) CS.CMU.EDU
     53  *  School of Computer Science
     54  *  Carnegie Mellon University
     55  *  Pittsburgh PA 15213-3890
     56  *
     57  * any improvements or extensions that they make and grant Carnegie the
     58  * rights to redistribute these changes.
     59  */
     60 
     61 #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     62 
     63 __KERNEL_RCSID(0, "$NetBSD: pci_1000a.c,v 1.21 2009/03/14 14:45:53 dsl Exp $");
     64 
     65 #include <sys/types.h>
     66 #include <sys/param.h>
     67 #include <sys/time.h>
     68 #include <sys/systm.h>
     69 #include <sys/errno.h>
     70 #include <sys/malloc.h>
     71 #include <sys/device.h>
     72 #include <sys/syslog.h>
     73 
     74 #include <uvm/uvm_extern.h>
     75 
     76 #include <machine/autoconf.h>
     77 
     78 #include <dev/pci/pcireg.h>
     79 #include <dev/pci/pcivar.h>
     80 
     81 #include <alpha/pci/pci_1000a.h>
     82 
     83 #include "sio.h"
     84 #if NSIO > 0 || NPCEB > 0
     85 #include <alpha/pci/siovar.h>
     86 #endif
     87 
     88 #define	PCI_NIRQ	32
     89 #define	PCI_STRAY_MAX	5
     90 
     91 #define IMR2IRQ(bn) ((bn) - 1)
     92 #define IRQ2IMR(irq) ((irq) + 1)
     93 
     94 static bus_space_tag_t mystery_icu_iot;
     95 static bus_space_handle_t mystery_icu_ioh[2];
     96 
     97 int	dec_1000a_intr_map(struct pci_attach_args *,
     98 	    pci_intr_handle_t *);
     99 const char *dec_1000a_intr_string(void *, pci_intr_handle_t);
    100 const struct evcnt *dec_1000a_intr_evcnt(void *, pci_intr_handle_t);
    101 void	*dec_1000a_intr_establish(void *, pci_intr_handle_t,
    102 	    int, int (*func)(void *), void *);
    103 void	dec_1000a_intr_disestablish(void *, void *);
    104 
    105 struct alpha_shared_intr *dec_1000a_pci_intr;
    106 
    107 static void dec_1000a_iointr(void *arg, unsigned long vec);
    108 static void dec_1000a_enable_intr(int irq);
    109 static void dec_1000a_disable_intr(int irq);
    110 static void pci_1000a_imi(void);
    111 static pci_chipset_tag_t pc_tag;
    112 
    113 void
    114 pci_1000a_pickintr(core, iot, memt, pc)
    115 	void *core;
    116 	bus_space_tag_t iot, memt;
    117 	pci_chipset_tag_t pc;
    118 {
    119 	char *cp;
    120 	int i;
    121 
    122 	mystery_icu_iot = iot;
    123 
    124 	pc_tag = pc;
    125 	if (bus_space_map(iot, 0x54a, 2, 0, mystery_icu_ioh + 0)
    126 	||  bus_space_map(iot, 0x54c, 2, 0, mystery_icu_ioh + 1))
    127 		panic("pci_1000a_pickintr");
    128         pc->pc_intr_v = core;
    129         pc->pc_intr_map = dec_1000a_intr_map;
    130         pc->pc_intr_string = dec_1000a_intr_string;
    131 	pc->pc_intr_evcnt = dec_1000a_intr_evcnt;
    132         pc->pc_intr_establish = dec_1000a_intr_establish;
    133         pc->pc_intr_disestablish = dec_1000a_intr_disestablish;
    134 
    135 	pc->pc_pciide_compat_intr_establish = NULL;
    136 
    137 	dec_1000a_pci_intr = alpha_shared_intr_alloc(PCI_NIRQ, 8);
    138 	for (i = 0; i < PCI_NIRQ; i++) {
    139 		alpha_shared_intr_set_maxstrays(dec_1000a_pci_intr, i,
    140 		    PCI_STRAY_MAX);
    141 
    142 		cp = alpha_shared_intr_string(dec_1000a_pci_intr, i);
    143 		sprintf(cp, "irq %d", i);
    144 		evcnt_attach_dynamic(alpha_shared_intr_evcnt(
    145 		    dec_1000a_pci_intr, i), EVCNT_TYPE_INTR, NULL,
    146 		    "dec_1000a", cp);
    147 	}
    148 
    149 	pci_1000a_imi();
    150 #if NSIO > 0 || NPCEB > 0
    151 	sio_intr_setup(pc, iot);
    152 #endif
    153 }
    154 
    155 int
    156 dec_1000a_intr_map(pa, ihp)
    157 	struct pci_attach_args *pa;
    158         pci_intr_handle_t *ihp;
    159 {
    160 	pcitag_t bustag = pa->pa_intrtag;
    161 	int buspin = pa->pa_intrpin;
    162 	pci_chipset_tag_t pc = pa->pa_pc;
    163 	int imrbit, device;
    164 	/*
    165 	 * Get bit number in mystery ICU imr
    166 	 */
    167 	static const signed char imrmap[][4] = {
    168 #		define	IRQSPLIT(o) { (o), (o)+1, (o)+16, (o)+16+1 }
    169 #		define	IRQNONE		 { 0, 0, 0, 0 }
    170 		/*  0  */ { 1, 0, 0, 0 },	/* Noritake and Pintake */
    171 		/*  1  */ IRQSPLIT(8),
    172 		/*  2  */ IRQSPLIT(10),
    173 		/*  3  */ IRQSPLIT(12),
    174 		/*  4  */ IRQSPLIT(14),
    175 		/*  5  */ { 1, 0, 0, 0 },	/* Corelle */
    176 		/*  6  */ { 10, 0, 0, 0 },	/* Corelle */
    177 		/*  7  */ IRQNONE,
    178 		/*  8  */ { 1, 0, 0, 0 },	/* isp behind ppb */
    179 		/*  9  */ IRQNONE,
    180 		/* 10  */ IRQNONE,
    181 		/* 11  */ IRQSPLIT(2),
    182 		/* 12  */ IRQSPLIT(4),
    183 		/* 13  */ IRQSPLIT(6),
    184 		/* 14  */ IRQSPLIT(8)		/* Corelle */
    185 	};
    186 
    187 	if (buspin == 0)	/* No IRQ used. */
    188 		return 1;
    189 	if (!(1 <= buspin && buspin <= 4))
    190 		goto bad;
    191 	pci_decompose_tag(pc, bustag, NULL, &device, NULL);
    192 	if (0 <= device && device < sizeof imrmap / sizeof imrmap[0]) {
    193 		if (device == 0)
    194 			printf("dec_1000a_intr_map: ?! UNEXPECTED DEV 0\n");
    195 		imrbit = imrmap[device][buspin - 1];
    196 		if (imrbit) {
    197 			*ihp = IMR2IRQ(imrbit);
    198 			return 0;
    199 		}
    200 	}
    201 bad:	printf("dec_1000a_intr_map: can't map dev %d pin %d\n", device, buspin);
    202 	return 1;
    203 }
    204 
    205 const char *
    206 dec_1000a_intr_string(ccv, ih)
    207 	void *ccv;
    208 	pci_intr_handle_t ih;
    209 {
    210 	static const char irqmsg_fmt[] = "dec_1000a irq %ld";
    211         static char irqstr[sizeof irqmsg_fmt];
    212 
    213 
    214         if (ih >= PCI_NIRQ)
    215                 panic("dec_1000a_intr_string: bogus dec_1000a IRQ 0x%lx", ih);
    216 
    217         snprintf(irqstr, sizeof irqstr, irqmsg_fmt, ih);
    218         return (irqstr);
    219 }
    220 
    221 const struct evcnt *
    222 dec_1000a_intr_evcnt(ccv, ih)
    223 	void *ccv;
    224 	pci_intr_handle_t ih;
    225 {
    226 
    227 	if (ih >= PCI_NIRQ)
    228 		panic("dec_1000a_intr_evcnt: bogus dec_1000a IRQ 0x%lx", ih);
    229 
    230 	return (alpha_shared_intr_evcnt(dec_1000a_pci_intr, ih));
    231 }
    232 
    233 void *
    234 dec_1000a_intr_establish(ccv, ih, level, func, arg)
    235         void *ccv, *arg;
    236         pci_intr_handle_t ih;
    237         int level;
    238         int (*func)(void *);
    239 {
    240 	void *cookie;
    241 
    242         if (ih >= PCI_NIRQ)
    243                 panic("dec_1000a_intr_establish: IRQ too high, 0x%lx", ih);
    244 
    245 	cookie = alpha_shared_intr_establish(dec_1000a_pci_intr, ih, IST_LEVEL,
    246 	    level, func, arg, "dec_1000a irq");
    247 
    248 	if (cookie != NULL &&
    249 	    alpha_shared_intr_firstactive(dec_1000a_pci_intr, ih)) {
    250 		scb_set(0x900 + SCB_IDXTOVEC(ih), dec_1000a_iointr, NULL,
    251 		    level);
    252 		dec_1000a_enable_intr(ih);
    253 	}
    254 	return (cookie);
    255 }
    256 
    257 void
    258 dec_1000a_intr_disestablish(ccv, cookie)
    259         void *ccv, *cookie;
    260 {
    261 	struct alpha_shared_intrhand *ih = cookie;
    262 	unsigned int irq = ih->ih_num;
    263 	int s;
    264 
    265 	s = splhigh();
    266 
    267 	alpha_shared_intr_disestablish(dec_1000a_pci_intr, cookie,
    268 	    "dec_1000a irq");
    269 	if (alpha_shared_intr_isactive(dec_1000a_pci_intr, irq) == 0) {
    270 		dec_1000a_disable_intr(irq);
    271 		alpha_shared_intr_set_dfltsharetype(dec_1000a_pci_intr, irq,
    272 		    IST_NONE);
    273 		scb_free(0x900 + SCB_IDXTOVEC(irq));
    274 	}
    275 
    276 	splx(s);
    277 }
    278 
    279 static void
    280 dec_1000a_iointr(framep, vec)
    281 	void *framep;
    282 	unsigned long vec;
    283 {
    284 	int irq;
    285 
    286 	irq = SCB_VECTOIDX(vec - 0x900);
    287 
    288 	if (!alpha_shared_intr_dispatch(dec_1000a_pci_intr, irq)) {
    289 		alpha_shared_intr_stray(dec_1000a_pci_intr, irq,
    290 		    "dec_1000a irq");
    291 		if (ALPHA_SHARED_INTR_DISABLE(dec_1000a_pci_intr, irq))
    292 			dec_1000a_disable_intr(irq);
    293 	} else
    294 		alpha_shared_intr_reset_strays(dec_1000a_pci_intr, irq);
    295 }
    296 
    297 /*
    298  * Read and write the mystery ICU IMR registers
    299  */
    300 
    301 #define	IR(h) bus_space_read_2(mystery_icu_iot, mystery_icu_ioh[h], 0)
    302 #define	IW(h, v) bus_space_write_2(mystery_icu_iot, mystery_icu_ioh[h], 0, (v))
    303 
    304 /*
    305  * Enable and disable interrupts at the ICU level
    306  */
    307 
    308 static void
    309 dec_1000a_enable_intr(irq)
    310 	int irq;
    311 {
    312 	int imrval = IRQ2IMR(irq);
    313 	int i = imrval >= 16;
    314 
    315 	IW(i, IR(i) | 1 << (imrval & 0xf));
    316 }
    317 
    318 static void
    319 dec_1000a_disable_intr(irq)
    320 	int irq;
    321 {
    322 	int imrval = IRQ2IMR(irq);
    323 	int i = imrval >= 16;
    324 
    325 	IW(i, IR(i) & ~(1 << (imrval & 0xf)));
    326 }
    327 /*
    328  * Initialize mystery ICU
    329  */
    330 static void
    331 pci_1000a_imi()
    332 {
    333 	IW(0, IR(0) & 1);
    334 	IW(1, IR(0) & 3);
    335 }
    336