xref: /netbsd-src/sys/dev/pci/if_fxp_pci.c (revision 3b435a73967be44dfb4a27315acd72bfacde430c)
1 /*	$NetBSD: if_fxp_pci.c,v 1.1 1999/06/20 16:35:40 thorpej Exp $	*/
2 
3 /*-
4  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9  * NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * PCI bus front-end for the Intel i82557 fast Ethernet controller
42  * driver.  Works with Intel Etherexpress Pro 10+, 100B, 100+ cards.
43  */
44 
45 #include "opt_inet.h"
46 #include "opt_ns.h"
47 #include "bpfilter.h"
48 #include "rnd.h"
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/mbuf.h>
53 #include <sys/malloc.h>
54 #include <sys/kernel.h>
55 #include <sys/socket.h>
56 #include <sys/ioctl.h>
57 #include <sys/errno.h>
58 #include <sys/device.h>
59 
60 #if NRND > 0
61 #include <sys/rnd.h>
62 #endif
63 
64 #include <net/if.h>
65 #include <net/if_dl.h>
66 #include <net/if_media.h>
67 #include <net/if_ether.h>
68 
69 #if NBPFILTER > 0
70 #include <net/bpf.h>
71 #endif
72 
73 #ifdef INET
74 #include <netinet/in.h>
75 #include <netinet/if_inarp.h>
76 #endif
77 
78 #ifdef NS
79 #include <netns/ns.h>
80 #include <netns/ns_if.h>
81 #endif
82 
83 #include <machine/bus.h>
84 #include <machine/intr.h>
85 
86 #include <dev/mii/miivar.h>
87 
88 #include <dev/ic/i82557reg.h>
89 #include <dev/ic/i82557var.h>
90 
91 #include <dev/pci/pcivar.h>
92 #include <dev/pci/pcireg.h>
93 #include <dev/pci/pcidevs.h>
94 
95 int	fxp_pci_match __P((struct device *, struct cfdata *, void *));
96 void	fxp_pci_attach __P((struct device *, struct device *, void *));
97 
98 struct cfattach fxp_pci_ca = {
99 	sizeof(struct fxp_softc), fxp_pci_match, fxp_pci_attach
100 };
101 
102 int
103 fxp_pci_match(parent, match, aux)
104 	struct device *parent;
105 	struct cfdata *match;
106 	void *aux;
107 {
108 	struct pci_attach_args *pa = aux;
109 
110 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL)
111 		return (0);
112 
113 	switch (PCI_PRODUCT(pa->pa_id)) {
114 	case PCI_PRODUCT_INTEL_82557:
115 		return (1);
116 	}
117 
118 	return (0);
119 }
120 
121 void
122 fxp_pci_attach(parent, self, aux)
123 	struct device *parent, *self;
124 	void *aux;
125 {
126 	struct fxp_softc *sc = (struct fxp_softc *)self;
127 	struct pci_attach_args *pa = aux;
128 	pci_chipset_tag_t pc = pa->pa_pc;
129 	pci_intr_handle_t ih;
130 	const char *intrstr = NULL;
131 	bus_space_tag_t iot, memt;
132 	bus_space_handle_t ioh, memh;
133 	int ioh_valid, memh_valid;
134 	bus_addr_t addr;
135 	bus_size_t size;
136 	int flags;
137 
138 	/*
139 	 * Map control/status registers.
140 	 */
141 	ioh_valid = (pci_mapreg_map(pa, FXP_PCI_IOBA,
142 	    PCI_MAPREG_TYPE_IO, 0,
143 	    &iot, &ioh, NULL, NULL) == 0);
144 
145 	/*
146 	 * Version 2.1 of the PCI spec, page 196, "Address Maps":
147 	 *
148 	 *	Prefetchable
149 	 *
150 	 *	Set to one if there are no side effects on reads, the
151 	 *	device returns all bytes regardless of the byte enables,
152 	 *	and host bridges can merge processor writes into this
153 	 *	range without causing errors.  Bit must be set to zero
154 	 *	otherwise.
155 	 *
156 	 * The 82557 incorrectly sets the "prefetchable" bit, resulting
157 	 * in errors on systems which will do merged reads and writes.
158 	 * These errors manifest themselves as all-bits-set when reading
159 	 * from the EEPROM or other < 4 byte registers.
160 	 *
161 	 * We must work around this problem by always forcing the mapping
162 	 * for memory space to be uncacheable.  On systems which cannot
163 	 * create an uncacheable mapping (because the firmware mapped it
164 	 * into only cacheable/prefetchable space due to the "prefetchable"
165 	 * bit), we can fall back onto i/o mapped access.
166 	 */
167 	memh_valid = 0;
168 	memt = pa->pa_memt;
169 	if (((pa->pa_flags & PCI_FLAGS_MEM_ENABLED) != 0) &&
170 	    pci_mapreg_info(pa->pa_pc, pa->pa_tag, FXP_PCI_MMBA,
171 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT,
172 	    &addr, &size, &flags) == 0) {
173 		flags &= ~BUS_SPACE_MAP_CACHEABLE;
174 		if (bus_space_map(memt, addr, size, flags, &memh) == 0)
175 			memh_valid = 1;
176 	}
177 
178 	if (memh_valid) {
179 		sc->sc_st = memt;
180 		sc->sc_sh = memh;
181 	} else if (ioh_valid) {
182 		sc->sc_st = iot;
183 		sc->sc_sh = ioh;
184 	} else {
185 		printf(": unable to map device registers\n");
186 		return;
187 	}
188 
189 	sc->sc_dmat = pa->pa_dmat;
190 
191 	/*
192 	 * XXX Perhaps report '557, '558, '559 based on revision?
193 	 */
194 	printf(": Intel i82557 Ethernet, rev %d\n",
195 	    PCI_REVISION(pa->pa_class));
196 
197 	/* Make sure bus-mastering is enabled. */
198 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
199 	    pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG) |
200 	    PCI_COMMAND_MASTER_ENABLE);
201 
202 	/*
203 	 * Map and establish our interrupt.
204 	 */
205 	if (pci_intr_map(pc, pa->pa_intrtag, pa->pa_intrpin,
206 	    pa->pa_intrline, &ih)) {
207 		printf("%s: couldn't map interrupt\n", sc->sc_dev.dv_xname);
208 		return;
209 	}
210 	intrstr = pci_intr_string(pc, ih);
211 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_NET, fxp_intr, sc);
212 	if (sc->sc_ih == NULL) {
213 		printf("%s: couldn't establish interrupt",
214 		    sc->sc_dev.dv_xname);
215 		if (intrstr != NULL)
216 			printf(" at %s", intrstr);
217 		printf("\n");
218 		return;
219 	}
220 	printf("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr);
221 
222 	/* Finish off the attach. */
223 	fxp_attach(sc);
224 }
225