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