xref: /openbsd-src/sys/dev/pci/if_sf_pci.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: if_sf_pci.c,v 1.10 2013/08/07 01:06:37 bluhm 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/ip.h>
56 #include <netinet/if_ether.h>
57 #endif
58 
59 #include <net/if_media.h>
60 
61 #include <machine/bus.h>
62 #include <machine/intr.h>
63 
64 #include <dev/mii/miivar.h>
65 
66 #include <dev/ic/aic6915.h>
67 
68 #include <dev/pci/pcireg.h>
69 #include <dev/pci/pcivar.h>
70 #include <dev/pci/pcidevs.h>
71 
72 struct sf_pci_softc {
73 	struct sf_softc sc_starfire;	/* read Starfire softc */
74 
75 	/* PCI-specific goo. */
76 	void	*sc_ih;			/* interrupt handle */
77 };
78 
79 int	sf_pci_match(struct device *, void *, void *);
80 void	sf_pci_attach(struct device *, struct device *, void *);
81 
82 struct cfattach sf_pci_ca = {
83         sizeof(struct sf_pci_softc), sf_pci_match, sf_pci_attach
84 };
85 
86 const struct pci_matchid sf_pci_products[] = {
87 	{ PCI_VENDOR_ADP, PCI_PRODUCT_ADP_AIC6915 },
88 };
89 
90 int
91 sf_pci_match(struct device *parent, void *match, void *aux)
92 {
93 	return (pci_matchbyid((struct pci_attach_args *)aux, sf_pci_products,
94 	    nitems(sf_pci_products)));
95 }
96 
97 void
98 sf_pci_attach(struct device *parent, struct device *self, void *aux)
99 {
100 	struct sf_pci_softc *psc = (void *) self;
101 	struct sf_softc *sc = &psc->sc_starfire;
102 	struct pci_attach_args *pa = aux;
103 	pci_intr_handle_t ih;
104 	const char *intrstr = NULL;
105 	bus_space_tag_t iot, memt;
106 	bus_space_handle_t ioh, memh;
107 	int ioh_valid, memh_valid;
108 	bus_size_t iosize, memsize;
109 	pcireg_t reg;
110 
111 	pci_set_powerstate(pa->pa_pc, pa->pa_tag, PCI_PMCSR_STATE_D0);
112 
113 	/*
114 	 * Map the device.
115 	 */
116 	reg = pci_mapreg_type(pa->pa_pc, pa->pa_tag, SF_PCI_MEMBA);
117 	switch (reg) {
118 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT:
119 	case PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT:
120 		memh_valid = (pci_mapreg_map(pa, SF_PCI_MEMBA,
121 		    reg, 0, &memt, &memh, &memsize, NULL, 0) == 0);
122 		break;
123 	default:
124 		memh_valid = 0;
125 	}
126 
127 	ioh_valid = (pci_mapreg_map(pa,
128 	    (reg == (PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_64BIT)) ?
129 		SF_PCI_IOBA : SF_PCI_IOBA - 0x04,
130 	    PCI_MAPREG_TYPE_IO, 0,
131 	    &iot, &ioh, &iosize, NULL, 0) == 0);
132 
133 	if (memh_valid) {
134 		sc->sc_st = memt;
135 		sc->sc_sh = memh;
136 		sc->sc_iomapped = 0;
137 	} else if (ioh_valid) {
138 		sc->sc_st = iot;
139 		sc->sc_sh = ioh;
140 		sc->sc_iomapped = 1;
141 	} else {
142 		printf(": unable to map device registers\n");
143 		return;
144 	}
145 
146 	sc->sc_dmat = pa->pa_dmat;
147 
148 	/*
149 	 * Map and establish our interrupt.
150 	 */
151 	if (pci_intr_map(pa, &ih)) {
152 		printf(": unable to map interrupt\n");
153 		goto out;
154 	}
155 	intrstr = pci_intr_string(pa->pa_pc, ih);
156 	psc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_NET, sf_intr, sc,
157 	    self->dv_xname);
158 	if (psc->sc_ih == NULL) {
159 		printf(": unable to establish interrupt");
160 		if (intrstr != NULL)
161 			printf(" at %s", intrstr);
162 		printf("\n");
163 		goto out;
164 	}
165 	printf(": %s", intrstr);
166 
167 	/*
168 	 * Finish off the attach.
169 	 */
170 	sf_attach(sc);
171 	return;
172 
173  out:
174 	if (ioh_valid)
175 		bus_space_unmap(iot, ioh, iosize);
176 	if (memh_valid)
177 		bus_space_unmap(memt, memh, memsize);
178 }
179