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