if_fxp_cardbus.c revision 1.11
1/*	$OpenBSD: if_fxp_cardbus.c,v 1.11 2005/05/16 01:36:25 brad 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 * 3. All advertising materials mentioning features or use of this software
20 *    must display the following acknowledgement:
21 *	This product includes software developed by the NetBSD
22 *	Foundation, Inc. and its contributors.
23 * 4. Neither the name of The NetBSD Foundation nor the names of its
24 *    contributors may be used to endorse or promote products derived
25 *    from this software without specific prior written permission.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 * POSSIBILITY OF SUCH DAMAGE.
38 */
39
40/*
41 * CardBus front-end for the Intel i82557 family of Ethernet chips.
42 */
43
44#include "bpfilter.h"
45
46#include <sys/param.h>
47#include <sys/systm.h>
48#include <sys/mbuf.h>
49#include <sys/socket.h>
50#include <sys/ioctl.h>
51#include <sys/errno.h>
52#include <sys/malloc.h>
53#include <sys/kernel.h>
54#include <sys/device.h>
55
56#include <net/if.h>
57#include <net/if_dl.h>
58#include <net/if_types.h>
59#include <net/if_media.h>
60
61#include <machine/endian.h>
62
63#if NBPFILTER > 0
64#include <net/bpf.h>
65#endif
66
67#ifdef INET
68#include <netinet/in.h>
69#include <netinet/in_systm.h>
70#include <netinet/in_var.h>
71#include <netinet/ip.h>
72#include <netinet/if_ether.h>
73#endif
74
75#include <machine/bus.h>
76#include <machine/intr.h>
77
78#include <dev/mii/miivar.h>
79
80#include <dev/ic/fxpreg.h>
81#include <dev/ic/fxpvar.h>
82
83#include <dev/pci/pcivar.h>
84#include <dev/pci/pcireg.h>
85#include <dev/pci/pcidevs.h>
86
87#include <dev/cardbus/cardbusvar.h>
88
89int fxp_cardbus_match(struct device *, void *, void *);
90void fxp_cardbus_attach(struct device *, struct device *, void *);
91int fxp_cardbus_detach(struct device *, int);
92void fxp_cardbus_setup(struct fxp_softc *);
93
94struct fxp_cardbus_softc {
95	struct fxp_softc sc;
96	cardbus_devfunc_t ct;
97	pcireg_t base0_reg;
98	pcireg_t base1_reg;
99	bus_size_t size;
100};
101
102struct cfattach fxp_cardbus_ca = {
103	sizeof(struct fxp_cardbus_softc), fxp_cardbus_match, fxp_cardbus_attach,
104	    fxp_cardbus_detach
105};
106
107const struct cardbus_matchid fxp_cardbus_devices[] = {
108	{ PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_82557 },
109};
110
111#ifdef CBB_DEBUG
112#define DPRINTF(X) printf X
113#else
114#define DPRINTF(X)
115#endif
116
117int
118fxp_cardbus_match(parent, match, aux)
119	struct device *parent;
120	void *match;
121	void *aux;
122{
123	return (cardbus_matchbyid((struct cardbus_attach_args *)aux,
124	    fxp_cardbus_devices,
125	    sizeof(fxp_cardbus_devices)/sizeof(fxp_cardbus_devices[0])));
126}
127
128void
129fxp_cardbus_attach(parent, self, aux)
130	struct device *parent, *self;
131	void *aux;
132{
133	static const char thisfunc[] = "fxp_cardbus_attach";
134
135	char intrstr[16];
136	struct fxp_softc *sc = (struct fxp_softc *) self;
137	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
138	struct cardbus_attach_args *ca = aux;
139	struct cardbus_softc *psc =
140	    (struct cardbus_softc *)sc->sc_dev.dv_parent;
141	cardbus_chipset_tag_t cc = psc->sc_cc;
142	cardbus_function_tag_t cf = psc->sc_cf;
143	bus_space_tag_t iot, memt;
144	bus_space_handle_t ioh, memh;
145
146	bus_addr_t adr;
147	bus_size_t size;
148
149	csc->ct = ca->ca_ct;
150
151	/*
152         * Map control/status registers.
153         */
154	if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE1_REG,
155	    PCI_MAPREG_TYPE_IO, 0, &iot, &ioh, &adr, &size) == 0) {
156		csc->base1_reg = adr | 1;
157		sc->sc_st = iot;
158		sc->sc_sh = ioh;
159		csc->size = size;
160	} else if (Cardbus_mapreg_map(csc->ct, CARDBUS_BASE0_REG,
161	    PCI_MAPREG_TYPE_MEM | PCI_MAPREG_MEM_TYPE_32BIT,
162	    0, &memt, &memh, &adr, &size) == 0) {
163		csc->base0_reg = adr;
164		sc->sc_st = memt;
165		sc->sc_sh = memh;
166		csc->size = size;
167	} else
168		panic("%s: failed to allocate mem and io space", thisfunc);
169
170	if (ca->ca_cis.cis1_info[0] && ca->ca_cis.cis1_info[1])
171		printf(": %s %s", ca->ca_cis.cis1_info[0],
172		    ca->ca_cis.cis1_info[1]);
173	else
174		printf("\n");
175
176	sc->sc_dmat = ca->ca_dmat;
177#if 0
178	sc->sc_enable = fxp_cardbus_enable;
179	sc->sc_disable = fxp_cardbus_disable;
180	sc->sc_enabled = 0;
181#endif
182
183	Cardbus_function_enable(csc->ct);
184
185	fxp_cardbus_setup(sc);
186
187	/* Map and establish the interrupt. */
188	sc->sc_ih = cardbus_intr_establish(cc, cf, psc->sc_intrline, IPL_NET,
189	    fxp_intr, sc);
190	if (NULL == sc->sc_ih) {
191		printf(": couldn't establish interrupt");
192		printf("at %d\n", ca->ca_intrline);
193		return;
194	}
195	snprintf(intrstr, sizeof(intrstr), "irq %d", ca->ca_intrline);
196
197	sc->sc_revision = PCI_REVISION(ca->ca_class);
198
199	fxp_attach_common(sc, intrstr);
200}
201
202void
203fxp_cardbus_setup(struct fxp_softc * sc)
204{
205	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) sc;
206	struct cardbus_softc *psc =
207	    (struct cardbus_softc *) sc->sc_dev.dv_parent;
208	cardbus_chipset_tag_t cc = psc->sc_cc;
209	cardbus_function_tag_t cf = psc->sc_cf;
210	pcireg_t command;
211
212	cardbustag_t tag = cardbus_make_tag(cc, cf, csc->ct->ct_bus,
213	    csc->ct->ct_dev, csc->ct->ct_func);
214
215	command = Cardbus_conf_read(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG);
216	if (csc->base0_reg) {
217		Cardbus_conf_write(csc->ct, tag,
218		    CARDBUS_BASE0_REG, csc->base0_reg);
219		(cf->cardbus_ctrl) (cc, CARDBUS_MEM_ENABLE);
220		command |= CARDBUS_COMMAND_MEM_ENABLE |
221		    CARDBUS_COMMAND_MASTER_ENABLE;
222	} else if (csc->base1_reg) {
223		Cardbus_conf_write(csc->ct, tag,
224		    CARDBUS_BASE1_REG, csc->base1_reg);
225		(cf->cardbus_ctrl) (cc, CARDBUS_IO_ENABLE);
226		command |= (CARDBUS_COMMAND_IO_ENABLE |
227		    CARDBUS_COMMAND_MASTER_ENABLE);
228	}
229
230	(cf->cardbus_ctrl) (cc, CARDBUS_BM_ENABLE);
231
232	/* enable the card */
233	Cardbus_conf_write(csc->ct, tag, CARDBUS_COMMAND_STATUS_REG, command);
234}
235
236int
237fxp_cardbus_detach(self, flags)
238	struct device *self;
239	int flags;
240{
241	struct fxp_softc *sc = (struct fxp_softc *) self;
242	struct fxp_cardbus_softc *csc = (struct fxp_cardbus_softc *) self;
243	struct cardbus_devfunc *ct = csc->ct;
244	int rv, reg;
245
246#ifdef DIAGNOSTIC
247	if (ct == NULL)
248		panic("%s: data structure lacks", sc->sc_dev.dv_xname);
249#endif
250
251	rv = fxp_detach(sc);
252	if (rv == 0) {
253		/*
254		 * Unhook the interrupt handler.
255		 */
256		cardbus_intr_disestablish(ct->ct_cc, ct->ct_cf, sc->sc_ih);
257
258		/*
259		 * release bus space and close window
260		 */
261		if (csc->base0_reg)
262			reg = CARDBUS_BASE0_REG;
263		else
264			reg = CARDBUS_BASE1_REG;
265		Cardbus_mapreg_unmap(ct, reg, sc->sc_st, sc->sc_sh, csc->size);
266	}
267	return (rv);
268}
269