xref: /netbsd-src/sys/dev/pci/iop_pci.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: iop_pci.c,v 1.21 2007/10/19 12:00:50 ad Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000, 2001, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Andrew Doran.
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 /*
40  * PCI front-end for `iop' driver.
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: iop_pci.c,v 1.21 2007/10/19 12:00:50 ad Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/kernel.h>
49 #include <sys/device.h>
50 #include <sys/queue.h>
51 #include <sys/proc.h>
52 
53 #include <machine/endian.h>
54 #include <sys/bus.h>
55 
56 #include <dev/pci/pcidevs.h>
57 #include <dev/pci/pcivar.h>
58 
59 #include <dev/i2o/i2o.h>
60 #include <dev/i2o/iopreg.h>
61 #include <dev/i2o/iopio.h>
62 #include <dev/i2o/iopvar.h>
63 
64 #define	PCI_INTERFACE_I2O_POLLED	0x00
65 #define	PCI_INTERFACE_I2O_INTRDRIVEN	0x01
66 
67 static void	iop_pci_attach(struct device *, struct device *, void *);
68 static int	iop_pci_match(struct device *, struct cfdata *, void *);
69 
70 CFATTACH_DECL(iop_pci, sizeof(struct iop_softc),
71     iop_pci_match, iop_pci_attach, NULL, NULL);
72 
73 static int
74 iop_pci_match(struct device *parent, struct cfdata *match,
75     void *aux)
76 {
77 	struct pci_attach_args *pa;
78 	u_int product, vendor;
79 	pcireg_t reg;
80 
81 	pa = aux;
82 
83 	/*
84 	 * Look for an "intelligent I/O processor" that adheres to the I2O
85 	 * specification.  Ignore the device if it doesn't support interrupt
86 	 * driven operation.
87 	 */
88 	if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O &&
89 	    PCI_SUBCLASS(pa->pa_class) == PCI_SUBCLASS_I2O_STANDARD &&
90 	    PCI_INTERFACE(pa->pa_class) == PCI_INTERFACE_I2O_INTRDRIVEN)
91 		return (1);
92 
93 	/*
94 	 * Match boards that don't conform exactly to the spec.
95 	 */
96 	vendor = PCI_VENDOR(pa->pa_id);
97 	product = PCI_PRODUCT(pa->pa_id);
98 
99 	if (vendor == PCI_VENDOR_DPT &&
100 	    (product == PCI_PRODUCT_DPT_RAID_I2O ||
101 	    product == PCI_PRODUCT_DPT_RAID_2005S))
102 		return (1);
103 
104 	if (vendor == PCI_VENDOR_INTEL &&
105 	    (product == PCI_PRODUCT_INTEL_80960RM_2 ||
106 	    product == PCI_PRODUCT_INTEL_80960_RP)) {
107 		reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
108 		if (PCI_VENDOR(reg) == PCI_VENDOR_PROMISE)
109 			return (1);
110 	}
111 
112 	return (0);
113 }
114 
115 static void
116 iop_pci_attach(struct device *parent, struct device *self, void *aux)
117 {
118 	struct pci_attach_args *pa;
119 	struct iop_softc *sc;
120 	pci_chipset_tag_t pc;
121 	pci_intr_handle_t ih;
122 	const char *intrstr;
123 	pcireg_t reg;
124 	int i;
125 
126 	sc = (struct iop_softc *)self;
127 	pa = (struct pci_attach_args *)aux;
128 	pc = pa->pa_pc;
129 	printf(": ");
130 
131 	/*
132 	 * The kernel always uses the first memory mapping to communicate
133 	 * with the IOP.
134 	 */
135 	for (i = PCI_MAPREG_START; i < PCI_MAPREG_END; i += 4) {
136 		reg = pci_conf_read(pc, pa->pa_tag, i);
137 		if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_MEM) {
138 			sc->sc_memaddr = PCI_MAPREG_MEM_ADDR(reg);
139 			break;
140 		}
141 	}
142 	if (i == PCI_MAPREG_END) {
143 		printf("can't find mapping\n");
144 		return;
145 	}
146 
147 	/* Map the register window. */
148 	if (pci_mapreg_map(pa, i, PCI_MAPREG_TYPE_MEM, 0, &sc->sc_iot,
149 	    &sc->sc_ioh, NULL, NULL)) {
150 		printf("%s: can't map register window\n", sc->sc_dv.dv_xname);
151 		return;
152 	}
153 
154 	/* Map the 2nd register window. */
155 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_DPT &&
156 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_DPT_RAID_2005S) {
157 		i += 4;	/* next BAR */
158 		if (i == PCI_MAPREG_END) {
159 			printf("can't find mapping\n");
160 			return;
161 		}
162 
163 #if 0
164 		/* Should we check it? (see FreeBSD's asr driver) */
165 		reg = pci_conf_read(pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
166 		printf("subid %x, %x\n", PCI_VENDOR(reg), PCI_PRODUCT(reg));
167 #endif
168 		if (pci_mapreg_map(pa, i, PCI_MAPREG_TYPE_MEM, 0,
169 		    &sc->sc_msg_iot, &sc->sc_msg_ioh, NULL, NULL)) {
170 			printf("%s: can't map 2nd register window\n",
171 			    sc->sc_dv.dv_xname);
172 			return;
173 		}
174 	} else {
175 		/* iop devices other than 2005S */
176 		sc->sc_msg_iot = sc->sc_iot;
177 		sc->sc_msg_ioh = sc->sc_ioh;
178 	}
179 
180 	sc->sc_pcibus = pa->pa_bus;
181 	sc->sc_pcidev = pa->pa_device;
182 	sc->sc_dmat = pa->pa_dmat;
183 	sc->sc_bus_memt = pa->pa_memt;
184 	sc->sc_bus_iot = pa->pa_iot;
185 
186 	/* Enable the device. */
187 	reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
188 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
189 		       reg | PCI_COMMAND_MASTER_ENABLE);
190 
191 	/* Map and establish the interrupt.. */
192 	if (pci_intr_map(pa, &ih)) {
193 		printf("can't map interrupt\n");
194 		return;
195 	}
196 	intrstr = pci_intr_string(pc, ih);
197 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, iop_intr, sc);
198 	if (sc->sc_ih == NULL) {
199 		printf("can't establish interrupt");
200 		if (intrstr != NULL)
201 			printf(" at %s", intrstr);
202 		printf("\n");
203 		return;
204 	}
205 
206 	/* Attach to the bus-independent code. */
207 	iop_init(sc, intrstr);
208 }
209