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