xref: /netbsd/sys/dev/pcmcia/if_cs_pcmcia.c (revision bf9ec67e)
1 /* $NetBSD: if_cs_pcmcia.c,v 1.2 2001/11/26 19:17:07 yamt Exp $ */
2 
3 /*-
4  * Copyright (c)2001 YAMAMOTO Takashi,
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: if_cs_pcmcia.c,v 1.2 2001/11/26 19:17:07 yamt Exp $");
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/device.h>
35 #include <sys/socket.h>
36 #include <sys/queue.h>
37 
38 #include "rnd.h"
39 #if NRND > 0
40 #include <sys/rnd.h>
41 #endif
42 
43 #include <net/if.h>
44 #include <net/if_ether.h>
45 #include <net/if_media.h>
46 
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49 
50 #include <dev/pcmcia/pcmciareg.h>
51 #include <dev/pcmcia/pcmciavar.h>
52 #include <dev/pcmcia/pcmciadevs.h>
53 
54 #include <dev/ic/cs89x0reg.h>
55 #include <dev/ic/cs89x0var.h>
56 
57 #define DEVNAME(sc) ((sc)->sc_dev.dv_xname)
58 
59 struct cs_pcmcia_softc;
60 
61 static int cs_pcmcia_match(struct device *, struct cfdata *, void *);
62 static void cs_pcmcia_attach(struct device *, struct device *, void *);
63 static int cs_pcmcia_detach(struct device *, int);
64 static int cs_pcmcia_enable(struct cs_softc *);
65 static void cs_pcmcia_disable(struct cs_softc *);
66 
67 struct cs_pcmcia_softc {
68 	struct cs_softc sc_cs; /* real "cs" softc */
69 
70 	struct pcmcia_io_handle sc_pcioh;
71 	struct pcmcia_function *sc_pf;
72 	int sc_io_window;
73 	int sc_flags;
74 };
75 
76 #define CS_PCMCIA_FLAGS_IO_ALLOCATED 1
77 #define CS_PCMCIA_FLAGS_IO_MAPPED 2
78 
79 struct cfattach cs_pcmcia_ca = {
80 	sizeof(struct cs_pcmcia_softc),
81 	cs_pcmcia_match,
82 	cs_pcmcia_attach,
83 	cs_pcmcia_detach,
84 	cs_activate
85 };
86 
87 static int
88 cs_pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
89 {
90 	struct pcmcia_attach_args *pa = aux;
91 
92 	if (pa->card->manufacturer == PCMCIA_VENDOR_IBM
93 		&& pa->card->product == PCMCIA_PRODUCT_IBM_ETHERJET)
94 		return 1;
95 
96 	return 0;
97 }
98 
99 static void
100 cs_pcmcia_attach(struct device *parent, struct device *self, void *aux)
101 {
102 	struct cs_pcmcia_softc *psc = (void *)self;
103 	struct cs_softc *sc = (void *)&psc->sc_cs;
104 	struct pcmcia_attach_args *pa = aux;
105 	struct pcmcia_config_entry *cfe;
106 	struct pcmcia_function *pf;
107 	char devinfo[256];
108 
109 	/* Print out what we are. */
110 	pcmcia_devinfo(&pa->pf->sc->card, 0, devinfo, sizeof(devinfo));
111 	printf(": %s\n", devinfo);
112 
113 	pf = psc->sc_pf = pa->pf;
114 
115 	cfe = SIMPLEQ_FIRST(&pa->pf->cfe_head);
116 
117 	if (cfe->num_iospace != 1) {
118 		printf("%s: unexpected number of iospace(%d)\n",
119 			DEVNAME(sc), cfe->num_iospace);
120 		goto fail;
121 	}
122 
123 	if (cfe->iospace[0].length < CS8900_IOSIZE) {
124 		printf("%s: unexpected iosize(%lu)\n",
125 			DEVNAME(sc), cfe->iospace[0].length);
126 		goto fail;
127 	}
128 
129 	if (cfe->num_memspace != 0) {
130 		printf("%s: unexpected number of memspace(%d)\n",
131 			DEVNAME(sc), cfe->num_memspace);
132 		goto fail;
133 	}
134 
135 	if (pcmcia_io_alloc(pf, cfe->iospace[0].start,
136 		cfe->iospace[0].length, cfe->iospace[0].length,
137 		&psc->sc_pcioh) != 0) {
138 		printf("%s: can't allocate i/o space %lx:%lx\n", DEVNAME(sc),
139 			cfe->iospace[0].start, cfe->iospace[0].length);
140 		goto fail;
141 	}
142 	psc->sc_flags |= CS_PCMCIA_FLAGS_IO_ALLOCATED;
143 
144 	sc->sc_iot = psc->sc_pcioh.iot;
145 	sc->sc_ioh = psc->sc_pcioh.ioh;
146 	sc->sc_irq = -1;
147 #define CS_PCMCIA_HACK_FOR_CARDBUS
148 #ifdef CS_PCMCIA_HACK_FOR_CARDBUS
149 	/*
150 	 * XXX is there a generic way to know if it's a cardbus or not?
151 	 */
152 	sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK;
153 #endif
154 	sc->sc_enable = cs_pcmcia_enable;
155 	sc->sc_disable = cs_pcmcia_disable;
156 
157 	pcmcia_function_init(pa->pf, cfe);
158 	if (cs_pcmcia_enable(sc))
159 		goto fail;
160 
161 	/* chip attach */
162 	if (cs_attach(sc, 0, 0, 0, 0))
163 		goto fail;
164 
165 	cs_pcmcia_disable(sc);
166 
167 	return;
168 
169 fail:
170 	cs_pcmcia_detach((struct device *)psc, 0);
171 
172 	return;
173 }
174 
175 static int
176 cs_pcmcia_detach(struct device *self, int flags)
177 {
178 	struct cs_pcmcia_softc *psc = (void *)self;
179 	struct cs_softc *sc = &psc->sc_cs;
180 	struct pcmcia_function *pf = psc->sc_pf;
181 	int rv;
182 
183 	rv = cs_detach(sc);
184 	if (rv)
185 		return rv;
186 
187 	cs_pcmcia_disable(sc);
188 
189 	if (psc->sc_flags & CS_PCMCIA_FLAGS_IO_ALLOCATED) {
190 		pcmcia_io_free(pf, &psc->sc_pcioh);
191 		psc->sc_flags &= ~CS_PCMCIA_FLAGS_IO_ALLOCATED;
192 	}
193 
194 	return 0;
195 }
196 
197 static int
198 cs_pcmcia_enable(struct cs_softc *sc)
199 {
200 	struct cs_pcmcia_softc *psc = (void *)sc;
201 	struct pcmcia_function *pf = psc->sc_pf;
202 
203 	if (pcmcia_io_map(pf, PCMCIA_WIDTH_AUTO, 0, psc->sc_pcioh.size,
204 		&psc->sc_pcioh, &psc->sc_io_window) != 0) {
205 		printf("%s: can't map i/o space\n", DEVNAME(sc));
206 		goto fail;
207 	}
208 	psc->sc_flags |= CS_PCMCIA_FLAGS_IO_MAPPED;
209 
210 	if (pcmcia_function_enable(pf)) {
211 		printf("%s: can't enable function\n", DEVNAME(sc));
212 		goto fail;
213 	}
214 
215 	sc->sc_ih = pcmcia_intr_establish(pf, IPL_NET, cs_intr, sc);
216 	if (sc->sc_ih == 0) {
217 		printf("%s: can't establish interrupt\n", DEVNAME(sc));
218 		goto fail;
219 	}
220 
221 	return 0;
222 
223 fail:
224 	return EIO;
225 }
226 
227 static void
228 cs_pcmcia_disable(struct cs_softc *sc)
229 {
230 	struct cs_pcmcia_softc *psc = (void *)sc;
231 	struct pcmcia_function *pf = psc->sc_pf;
232 
233 	if (sc->sc_ih != 0) {
234 		pcmcia_intr_disestablish(pf, sc->sc_ih);
235 		sc->sc_ih = 0;
236 	}
237 
238 	pcmcia_function_disable(pf);
239 
240 	if (psc->sc_flags & CS_PCMCIA_FLAGS_IO_MAPPED) {
241 		pcmcia_io_unmap(pf, psc->sc_io_window);
242 		psc->sc_flags &= ~CS_PCMCIA_FLAGS_IO_MAPPED;
243 	}
244 }
245