xref: /netbsd-src/sys/dev/pcmcia/if_cs_pcmcia.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /* $NetBSD: if_cs_pcmcia.c,v 1.12 2005/12/11 12:23:23 christos 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.12 2005/12/11 12:23:23 christos 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 int cs_pcmcia_validate_config(struct pcmcia_config_entry *);
63 static void cs_pcmcia_attach(struct device *, struct device *, void *);
64 static int cs_pcmcia_detach(struct device *, int);
65 static int cs_pcmcia_enable(struct cs_softc *);
66 static void cs_pcmcia_disable(struct cs_softc *);
67 
68 struct cs_pcmcia_softc {
69 	struct cs_softc sc_cs; /* real "cs" softc */
70 
71 	struct pcmcia_function *sc_pf;
72 
73 	int sc_state;
74 #define	CS_PCMCIA_ATTACHED	3
75 };
76 
77 CFATTACH_DECL(cs_pcmcia, sizeof(struct cs_pcmcia_softc),
78     cs_pcmcia_match, cs_pcmcia_attach, cs_pcmcia_detach, cs_activate);
79 
80 static int
81 cs_pcmcia_match(struct device *parent, struct cfdata *match, void *aux)
82 {
83 	struct pcmcia_attach_args *pa = aux;
84 
85 	if (pa->manufacturer == PCMCIA_VENDOR_IBM &&
86 	    pa->product == PCMCIA_PRODUCT_IBM_ETHERJET)
87 		return (1);
88 	return (0);
89 }
90 
91 static int
92 cs_pcmcia_validate_config(struct pcmcia_config_entry *cfe)
93 {
94 	if (cfe->iftype != PCMCIA_IFTYPE_IO ||
95 	    cfe->num_memspace != 0 ||
96 	    cfe->num_iospace != 1 ||
97 	    cfe->iospace[0].length < CS8900_IOSIZE)
98 		return (EINVAL);
99 	return (0);
100 }
101 
102 static void
103 cs_pcmcia_attach(struct device *parent, struct device *self, void *aux)
104 {
105 	struct cs_pcmcia_softc *psc = (void *)self;
106 	struct cs_softc *sc = (void *)&psc->sc_cs;
107 	struct pcmcia_attach_args *pa = aux;
108 	struct pcmcia_config_entry *cfe;
109 	struct pcmcia_function *pf;
110 	int error;
111 
112 	pf = psc->sc_pf = pa->pf;
113 
114 	error = pcmcia_function_configure(pa->pf, cs_pcmcia_validate_config);
115 	if (error) {
116 		aprint_error("%s: configure failed, error=%d\n", self->dv_xname,
117 		    error);
118 		return;
119 	}
120 
121 	cfe = pf->cfe;
122 	sc->sc_iot = cfe->iospace[0].handle.iot;
123 	sc->sc_ioh = cfe->iospace[0].handle.ioh;
124 	sc->sc_irq = -1;
125 #define CS_PCMCIA_HACK_FOR_CARDBUS
126 #ifdef CS_PCMCIA_HACK_FOR_CARDBUS
127 	/*
128 	 * XXX is there a generic way to know if it's a cardbus or not?
129 	 */
130 	sc->sc_cfgflags |= CFGFLG_CARDBUS_HACK;
131 #endif
132 
133 	error = cs_pcmcia_enable(sc);
134 	if (error)
135 		goto fail;
136 
137 	sc->sc_enable = cs_pcmcia_enable;
138 	sc->sc_disable = cs_pcmcia_disable;
139 
140 	/* chip attach */
141 	error = cs_attach(sc, 0, 0, 0, 0);
142 	if (error)
143 		goto fail2;
144 
145 	cs_pcmcia_disable(sc);
146 	psc->sc_state = CS_PCMCIA_ATTACHED;
147 	return;
148 
149 fail2:
150 	cs_pcmcia_disable(sc);
151 fail:
152 	pcmcia_function_unconfigure(pf);
153 }
154 
155 static int
156 cs_pcmcia_detach(struct device *self, int flags)
157 {
158 	struct cs_pcmcia_softc *psc = (void *)self;
159 	struct cs_softc *sc = &psc->sc_cs;
160 	int error;
161 
162 	if (psc->sc_state != CS_PCMCIA_ATTACHED)
163 		return (0);
164 
165 	error = cs_detach(sc);
166 	if (error)
167 		return (error);
168 
169 	pcmcia_function_unconfigure(psc->sc_pf);
170 
171 	return (0);
172 }
173 
174 static int
175 cs_pcmcia_enable(struct cs_softc *sc)
176 {
177 	struct cs_pcmcia_softc *psc = (void *)sc;
178 	int error;
179 
180 	sc->sc_ih = pcmcia_intr_establish(psc->sc_pf, IPL_NET, cs_intr, sc);
181 	if (!sc->sc_ih)
182 		return (EIO);
183 
184 	error = pcmcia_function_enable(psc->sc_pf);
185 	if (error) {
186 		pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
187 		sc->sc_ih = 0;
188 	}
189 
190 	return (error);
191 }
192 
193 static void
194 cs_pcmcia_disable(struct cs_softc *sc)
195 {
196 	struct cs_pcmcia_softc *psc = (void *)sc;
197 
198 	pcmcia_function_disable(psc->sc_pf);
199 	pcmcia_intr_disestablish(psc->sc_pf, sc->sc_ih);
200 	sc->sc_ih = 0;
201 }
202