xref: /openbsd-src/sys/dev/pci/if_sf_pci.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: if_sf_pci.c,v 1.7 2008/06/26 05:42:17 ray Exp $	*/
2 /*	$NetBSD: if_sf_pci.c,v 1.10 2006/06/17 23:34:27 christos Exp $	*/
3 
4 /*-
5  * Copyright (c) 2001 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Jason R. Thorpe.
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  *
20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 /*
34  * PCI bus front-end for the Adaptec AIC-6915 (``Starfire'')
35  * 10/100 Ethernet controller.
36  */
37 
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/mbuf.h>
41 #include <sys/malloc.h>
42 #include <sys/kernel.h>
43 #include <sys/socket.h>
44 #include <sys/ioctl.h>
45 #include <sys/errno.h>
46 #include <sys/device.h>
47 
48 #include <net/if.h>
49 #include <net/if_dl.h>
50 #include <net/if_types.h>
51 
52 #ifdef INET
53 #include <netinet/in.h>
54 #include <netinet/in_systm.h>
55 #include <netinet/in_var.h>
56 #include <netinet/ip.h>
57 #include <netinet/if_ether.h>
58 #endif
59 
60 #include <net/if_media.h>
61 
62 #include <machine/bus.h>
63 #include <machine/intr.h>
64 
65 #include <dev/mii/miivar.h>
66 
67 #include <dev/ic/aic6915.h>
68 
69 #include <dev/pci/pcireg.h>
70 #include <dev/pci/pcivar.h>
71 #include <dev/pci/pcidevs.h>
72 
73 struct sf_pci_softc {
74 	struct sf_softc sc_starfire;	/* read Starfire softc */
75 
76 	/* PCI-specific goo. */
77 	void	*sc_ih;			/* interrupt handle */
78 };
79 
80 int	sf_pci_match(struct device *, void *, void *);
81 void	sf_pci_attach(struct device *, struct device *, void *);
82 
83 struct cfattach sf_pci_ca = {
84         sizeof(struct sf_pci_softc), sf_pci_match, sf_pci_attach
85 };
86 
87 const struct pci_matchid sf_pci_products[] = {
88 	{ PCI_VENDOR_ADP, PCI_PRODUCT_ADP_AIC6915 },
89 };
90 
91 int
92 sf_pci_match(struct device *parent, void *match, void *aux)
93 {
94 	return (pci_matchbyid((struct pci_attach_args *)aux, sf_pci_products,
95 	    sizeof(sf_pci_products)/sizeof(sf_pci_products[0])));
96 }
97 
98 void
99 sf_pci_attach(struct device *parent, struct device *self, void *aux)
100 {
101 	struct sf_pci_softc *psc = (void *) self;
102 	struct sf_softc *sc = &psc->sc_starfire;
103 	struct pci_attach_args *pa = aux;
104 	pci_intr_handle_t ih;
105 	const char *intrstr = NULL;
106 	bus_space_tag_t iot, memt;
107 	bus_space_handle_t ioh, memh;
108 	int state, ioh_valid, memh_valid;
109 	bus_size_t iosize, memsize;
110 	pcireg_t reg;
111 
112 	state = pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
113 	if (state == PCI_PMCSR_STATE_D3) {
114 		printf(": unable to wake up from power state D3, "
115 		    "reboot required.\n");
116 		return;
117 	}
118 
119 	/*
120 	 * Map the device.
121 	 */
122 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
123 	switch (reg) {
124 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
125 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
126 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
127 		    reg, 0, &memt, &memh, &memsize, NULL, 0) == 0);
128 		break;
129 	default:
130 		memh_valid = 0;
131 	}
132 
133 	ioh_valid = (pci_mapreg_map(pa,
134 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
135 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
136 	    PCI_MAPREG_TYPE_IO, 0,
137 	    &iot, &ioh, &iosize, NULL, 0) == 0);
138 
139 	if (memh_valid) {
140 		sc->sc_st = memt;
141 		sc->sc_sh = memh;
142 		sc->sc_iomapped = 0;
143 	} else if (ioh_valid) {
144 		sc->sc_st = iot;
145 		sc->sc_sh = ioh;
146 		sc->sc_iomapped = 1;
147 	} else {
148 		printf(": unable to map device registers\n");
149 		return;
150 	}
151 
152 	sc->sc_dmat = pa->pa_dmat;
153 
154 	/*
155 	 * Map and establish our interrupt.
156 	 */
157 	if (pci_intr_map(pa, &ih)) {
158 		printf(": unable to map interrupt\n");
159 		goto out;
160 	}
161 	intrstr = pci_intr_string(pa->pa_pc, ih);
162 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, sf_intr, sc,
163 	    self->dv_xname);
164 	if (psc->sc_ih == NULL) {
165 		printf(": unable to establish interrupt");
166 		if (intrstr != NULL)
167 			printf(" at %s", intrstr);
168 		printf("\n");
169 		goto out;
170 	}
171 	printf(": %s", intrstr);
172 
173 	/*
174 	 * Finish off the attach.
175 	 */
176 	sf_attach(sc);
177 	return;
178 
179  out:
180 	if (ioh_valid)
181 		bus_space_unmap(iot, ioh, iosize);
182 	if (memh_valid)
183 		bus_space_unmap(memt, memh, memsize);
184 }
185