xref: /openbsd/sys/dev/cardbus/if_fxp_cardbus.c (revision 404b540a)
1 /*	$OpenBSD: if_fxp_cardbus.c,v 1.23 2009/06/02 16:50:20 jsg Exp $ */
2 /*	$NetBSD: if_fxp_cardbus.c,v 1.12 2000/05/08 18:23:36 thorpej Exp $	*/
3 
4 /*
5  * Copyright (c) 1999 The NetBSD Foundation, Inc.
6  * All rights reserved.
7  *
8  * This code is derived from software contributed to The NetBSD Foundation
9  * by Johan Danielsson.
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  * CardBus front-end for the Intel i8255x family of Ethernet chips.
35  */
36 
37 #include "bpfilter.h"
38 
39 #include <sys/param.h>
40 #include <sys/systm.h>
41 #include <sys/mbuf.h>
42 #include <sys/socket.h>
43 #include <sys/ioctl.h>
44 #include <sys/errno.h>
45 #include <sys/malloc.h>
46 #include <sys/kernel.h>
47 #include <sys/timeout.h>
48 #include <sys/device.h>
49 
50 #include <net/if.h>
51 #include <net/if_dl.h>
52 #include <net/if_types.h>
53 #include <net/if_media.h>
54 
55 #include <machine/endian.h>
56 
57 #if NBPFILTER > 0
58 #include <net/bpf.h>
59 #endif
60 
61 #ifdef INET
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/in_var.h>
65 #include <netinet/ip.h>
66 #include <netinet/if_ether.h>
67 #endif
68 
69 #include <machine/bus.h>
70 #include <machine/intr.h>
71 
72 #include <dev/mii/miivar.h>
73 
74 #include <dev/ic/fxpreg.h>
75 #include <dev/ic/fxpvar.h>
76 
77 #include <dev/pci/pcivar.h>
78 #include <dev/pci/pcireg.h>
79 #include <dev/pci/pcidevs.h>
80 
81 #include <dev/cardbus/cardbusvar.h>
82 
83 int fxp_cardbus_match(struct device *, void *, void *);
84 void fxp_cardbus_attach(struct device *, struct device *, void *);
85 int fxp_cardbus_detach(struct device *, int);
86 void fxp_cardbus_setup(struct fxp_softc *);
87 
88 struct fxp_cardbus_softc {
89 	struct fxp_softc sc;
90 	cardbus_devfunc_t ct;
91 	cardbustag_t ct_tag;
92 	pcireg_t base0_reg;
93 	pcireg_t base1_reg;
94 	bus_size_t size;
95 };
96 
97 struct cfattach fxp_cardbus_ca = {
98 	sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach,
99 	    fxp_cardbus_detach
100 };
101 
102 const struct cardbus_matchid fxp_cardbus_devices[] = {
103 	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_8255x },
104 };
105 
106 #ifdef CBB_DEBUG
107 #define DPRINTF(X) printf X
108 #else
109 #define DPRINTF(X)
110 #endif
111 
112 int
113 fxp_cardbus_match(struct device *parent, void *match, void *aux)
114 {
115 	return (cardbus_matchbyid((struct cardbus_attach_args *)aux,
116 	    fxp_cardbus_devices,
117 	    sizeof(fxp_cardbus_devices)/sizeof(fxp_cardbus_devices[0])));
118 }
119 
120 void
121 fxp_cardbus_attach(struct device *parent, struct device *self, void *aux)
122 {
123 	char intrstr[16];
124 	struct fxp_softc *sc = (struct fxp_softc *) self;
125 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
126 	struct cardbus_attach_args *ca = aux;
127 	struct cardbus_softc *psc =
128 	    (struct cardbus_softc *)sc->sc_dev.dv_parent;
129 	cardbus_chipset_tag_t cc = psc->sc_cc;
130 	cardbus_function_tag_t cf = psc->sc_cf;
131 	bus_space_tag_t iot, memt;
132 	bus_space_handle_t ioh, memh;
133 
134 	bus_addr_t adr;
135 	bus_size_t size;
136 
137 	csc->ct = ca->ca_ct;
138 
139 	/*
140 	 * Map control/status registers.
141 	 */
142 	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
143 	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
144 		csc->base1_reg = adr | 1;
145 		sc->sc_st = iot;
146 		sc->sc_sh = ioh;
147 		csc->size = size;
148 	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
149 	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
150 	    0, &memt, &memh, &adr, &size) == 0) {
151 		csc->base0_reg = adr;
152 		sc->sc_st = memt;
153 		sc->sc_sh = memh;
154 		csc->size = size;
155 	} else
156 		panic("%s: failed to allocate mem and io space", __func__);
157 
158 	sc->sc_dmat = ca->ca_dmat;
159 #if 0
160 	sc->sc_enable = fxp_cardbus_enable;
161 	sc->sc_disable = fxp_cardbus_disable;
162 	sc->sc_enabled = 0;
163 #endif
164 
165 	Cardbus_function_enable(csc->ct);
166 
167 	fxp_cardbus_setup(sc);
168 
169 	/* Map and establish the interrupt. */
170 	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
171 	    fxp_intr, sc, sc->sc_dev.dv_xname);
172 	if (NULL == sc->sc_ih) {
173 		printf(": couldn't establish interrupt");
174 		printf("at %d\n", ca->ca_intrline);
175 		return;
176 	}
177 	snprintf(intrstr, sizeof(intrstr), "irq %d", ca->ca_intrline);
178 
179 	sc->sc_revision = PCI_REVISION(ca->ca_class);
180 
181 	fxp_attach(sc, intrstr);
182 }
183 
184 void
185 fxp_cardbus_setup(struct fxp_softc *sc)
186 {
187 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
188 	struct cardbus_softc *psc =
189 	    (struct cardbus_softc *) sc->sc_dev.dv_parent;
190 	cardbus_chipset_tag_t cc = psc->sc_cc;
191 	cardbus_function_tag_t cf = psc->sc_cf;
192 	pcireg_t command;
193 
194 	csc->ct_tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
195 	    csc->ct->ct_dev, csc->ct->ct_func);
196 
197 	command = cardbus_conf_read(cc, cf, csc->ct_tag, CARDBUS_COMMAND_STATUS_REG);
198 	if (csc->base0_reg) {
199 		cardbus_conf_write(cc, cf, csc->ct_tag, CARDBUS_BASE0_REG, csc->base0_reg);
200 		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
201 		command |= CARDBUS_COMMAND_MEM_ENABLE |
202 		    CARDBUS_COMMAND_MASTER_ENABLE;
203 	} else if (csc->base1_reg) {
204 		cardbus_conf_write(cc, cf, csc->ct_tag, CARDBUS_BASE1_REG, csc->base1_reg);
205 		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
206 		command |= (CARDBUS_COMMAND_IO_ENABLE |
207 		    CARDBUS_COMMAND_MASTER_ENABLE);
208 	}
209 
210 	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
211 
212 	/* enable the card */
213 	cardbus_conf_write(cc, cf, csc->ct_tag, CARDBUS_COMMAND_STATUS_REG, command);
214 }
215 
216 int
217 fxp_cardbus_detach(struct device *self, int flags)
218 {
219 	struct fxp_softc *sc = (struct fxp_softc *) self;
220 	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
221 	struct cardbus_devfunc *ct = csc->ct;
222 	int rv, reg;
223 
224 #ifdef DIAGNOSTIC
225 	if (ct == NULL)
226 		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
227 #endif
228 
229 	rv = fxp_detach(sc);
230 	if (rv == 0) {
231 		/*
232 		 * Unhook the interrupt handler.
233 		 */
234 		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
235 
236 		/*
237 		 * release bus space and close window
238 		 */
239 		if (csc->base0_reg)
240 			reg = CARDBUS_BASE0_REG;
241 		else
242 			reg = CARDBUS_BASE1_REG;
243 		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
244 	}
245 	return (rv);
246 }
247