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