eppcic.c revision 1.1.4.2 1 1.1.4.2 christos /* $NetBSD: eppcic.c,v 1.1.4.2 2005/12/11 10:28:15 christos Exp $ */
2 1.1.4.2 christos
3 1.1.4.2 christos /*
4 1.1.4.2 christos * Copyright (c) 2005 HAMAJIMA Katsuomi. All rights reserved.
5 1.1.4.2 christos *
6 1.1.4.2 christos * Redistribution and use in source and binary forms, with or without
7 1.1.4.2 christos * modification, are permitted provided that the following conditions
8 1.1.4.2 christos * are met:
9 1.1.4.2 christos * 1. Redistributions of source code must retain the above copyright
10 1.1.4.2 christos * notice, this list of conditions and the following disclaimer.
11 1.1.4.2 christos * 2. Redistributions in binary form must reproduce the above copyright
12 1.1.4.2 christos * notice, this list of conditions and the following disclaimer in the
13 1.1.4.2 christos * documentation and/or other materials provided with the distribution.
14 1.1.4.2 christos *
15 1.1.4.2 christos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 1.1.4.2 christos * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 1.1.4.2 christos * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 1.1.4.2 christos * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.1.4.2 christos * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.1.4.2 christos * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 1.1.4.2 christos * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.1.4.2 christos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.1.4.2 christos * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1.4.2 christos * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1.4.2 christos * SUCH DAMAGE.
26 1.1.4.2 christos */
27 1.1.4.2 christos
28 1.1.4.2 christos #include <sys/cdefs.h>
29 1.1.4.2 christos __KERNEL_RCSID(0, "$NetBSD: eppcic.c,v 1.1.4.2 2005/12/11 10:28:15 christos Exp $");
30 1.1.4.2 christos
31 1.1.4.2 christos #include <sys/param.h>
32 1.1.4.2 christos #include <sys/systm.h>
33 1.1.4.2 christos #include <sys/kernel.h>
34 1.1.4.2 christos #include <sys/malloc.h>
35 1.1.4.2 christos #include <sys/device.h>
36 1.1.4.2 christos #include <sys/kthread.h>
37 1.1.4.2 christos #include <uvm/uvm_param.h>
38 1.1.4.2 christos #include <machine/bus.h>
39 1.1.4.2 christos #include <dev/pcmcia/pcmciareg.h>
40 1.1.4.2 christos #include <dev/pcmcia/pcmciavar.h>
41 1.1.4.2 christos #include <dev/pcmcia/pcmciachip.h>
42 1.1.4.2 christos #include <arm/ep93xx/epsocvar.h>
43 1.1.4.2 christos #include <arm/ep93xx/epgpiovar.h>
44 1.1.4.2 christos #include <arm/ep93xx/eppcicvar.h>
45 1.1.4.2 christos #include <arm/ep93xx/ep93xxreg.h>
46 1.1.4.2 christos #include <arm/ep93xx/epsmcreg.h>
47 1.1.4.2 christos #include "epled.h"
48 1.1.4.2 christos #if NEPLED > 0
49 1.1.4.2 christos #include <arm/ep93xx/epledvar.h>
50 1.1.4.2 christos #endif
51 1.1.4.2 christos
52 1.1.4.2 christos #include "epgpio.h"
53 1.1.4.2 christos #if NEPGPIO == 0
54 1.1.4.2 christos #error "epgpio requires in eppcic"
55 1.1.4.2 christos #endif
56 1.1.4.2 christos
57 1.1.4.2 christos #ifdef EPPCIC_DEBUG
58 1.1.4.2 christos int eppcic_debug = EPPCIC_DEBUG;
59 1.1.4.2 christos #define DPRINTFN(n,x) if (eppcic_debug>(n)) printf x;
60 1.1.4.2 christos #else
61 1.1.4.2 christos #define DPRINTFN(n,x)
62 1.1.4.2 christos #endif
63 1.1.4.2 christos
64 1.1.4.2 christos /* Mem & I/O */
65 1.1.4.2 christos #define SOCKET0_MCCD1 1 /* pin36/pin26 (negative) Card Detect 1 */
66 1.1.4.2 christos #define SOCKET0_MCCD2 2 /* pin67/pin25 (negative) Card Detect 2 */
67 1.1.4.2 christos #define SOCKET0_VS1 5 /* pin33/pin43 (negative) Voltage Sense 1 */
68 1.1.4.2 christos #define SOCKET0_VS2 7 /* pin57/pin40 (negative) Voltage Sense 2 */
69 1.1.4.2 christos /* Memory */
70 1.1.4.2 christos #define SOCKET0_WP 0 /* pin33/pin24 Write Protect */
71 1.1.4.2 christos #define SOCKET0_MCBVD1 3 /* pin63/pin46 Battery Voltage Detect 1 */
72 1.1.4.2 christos #define SOCKET0_MCBVD2 4 /* pin62/pin45 Battery Voltage Detect 2 */
73 1.1.4.2 christos #define SOCKET0_READY 6 /* pin16/pin37 Ready */
74 1.1.4.2 christos /* I/O */
75 1.1.4.2 christos #define SOCKET0_STSCHG 3 /* pin63/pin46 (negative) Status Change */
76 1.1.4.2 christos #define SOCKET0_SPKR 4 /* pin62/pin45 (negative) Speaker */
77 1.1.4.2 christos #define SOCKET0_IREQ 6 /* pin16/pin37 Interrupt Request */
78 1.1.4.2 christos
79 1.1.4.2 christos struct eppcic_handle {
80 1.1.4.2 christos int ph_socket; /* socket number */
81 1.1.4.2 christos struct eppcic_softc *ph_sc;
82 1.1.4.2 christos struct device *ph_card;
83 1.1.4.2 christos int (*ph_ih_func)(void *);
84 1.1.4.2 christos void *ph_ih_arg;
85 1.1.4.2 christos struct proc *ph_event_thread;
86 1.1.4.2 christos int ph_run; /* ktread running */
87 1.1.4.2 christos int ph_width; /* 8 or 16 */
88 1.1.4.2 christos int ph_vcc; /* 3 or 5 */
89 1.1.4.2 christos int ph_status[2]; /* cd1 and cd2 */
90 1.1.4.2 christos int ph_port; /* GPIO port */
91 1.1.4.2 christos int ph_cd[2]; /* card detect */
92 1.1.4.2 christos int ph_vs[2]; /* voltage sense */
93 1.1.4.2 christos int ph_ireq; /* interrupt request */
94 1.1.4.2 christos struct {
95 1.1.4.2 christos bus_size_t reg;
96 1.1.4.2 christos bus_addr_t base;
97 1.1.4.2 christos bus_size_t size;
98 1.1.4.2 christos } ph_space[3];
99 1.1.4.2 christos #define IO 0
100 1.1.4.2 christos #define COMMON 1
101 1.1.4.2 christos #define ATTRIBUTE 2
102 1.1.4.2 christos };
103 1.1.4.2 christos
104 1.1.4.2 christos static int eppcic_intr_carddetect(void *);
105 1.1.4.2 christos static int eppcic_intr_socket(void *);
106 1.1.4.2 christos static int eppcic_print(void *, const char *);
107 1.1.4.2 christos static void eppcic_create_event_thread(void *);
108 1.1.4.2 christos static void eppcic_event_thread(void *);
109 1.1.4.2 christos void eppcic_shutdown(void *);
110 1.1.4.2 christos
111 1.1.4.2 christos static int eppcic_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
112 1.1.4.2 christos struct pcmcia_mem_handle *);
113 1.1.4.2 christos static void eppcic_mem_free(pcmcia_chipset_handle_t,
114 1.1.4.2 christos struct pcmcia_mem_handle *);
115 1.1.4.2 christos static int eppcic_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
116 1.1.4.2 christos struct pcmcia_mem_handle *, bus_size_t *, int *);
117 1.1.4.2 christos static void eppcic_mem_unmap(pcmcia_chipset_handle_t, int);
118 1.1.4.2 christos static int eppcic_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
119 1.1.4.2 christos bus_size_t, struct pcmcia_io_handle *);
120 1.1.4.2 christos static void eppcic_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
121 1.1.4.2 christos static int eppcic_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
122 1.1.4.2 christos struct pcmcia_io_handle *, int *);
123 1.1.4.2 christos static void eppcic_io_unmap(pcmcia_chipset_handle_t, int);
124 1.1.4.2 christos static void *eppcic_intr_establish(pcmcia_chipset_handle_t,
125 1.1.4.2 christos struct pcmcia_function *,
126 1.1.4.2 christos int, int (*)(void *), void *);
127 1.1.4.2 christos static void eppcic_intr_disestablish(pcmcia_chipset_handle_t, void *);
128 1.1.4.2 christos static void eppcic_socket_enable(pcmcia_chipset_handle_t);
129 1.1.4.2 christos static void eppcic_socket_disable(pcmcia_chipset_handle_t);
130 1.1.4.2 christos static void eppcic_socket_settype(pcmcia_chipset_handle_t, int);
131 1.1.4.2 christos
132 1.1.4.2 christos static void eppcic_attach_socket(struct eppcic_handle *);
133 1.1.4.2 christos static void eppcic_config_socket(struct eppcic_handle *);
134 1.1.4.2 christos static int eppcic_get_voltage(struct eppcic_handle *);
135 1.1.4.2 christos static void eppcic_set_pcreg(struct eppcic_handle *, int);
136 1.1.4.2 christos
137 1.1.4.2 christos static struct pcmcia_chip_functions eppcic_functions = {
138 1.1.4.2 christos eppcic_mem_alloc, eppcic_mem_free,
139 1.1.4.2 christos eppcic_mem_map, eppcic_mem_unmap,
140 1.1.4.2 christos eppcic_io_alloc, eppcic_io_free,
141 1.1.4.2 christos eppcic_io_map, eppcic_io_unmap,
142 1.1.4.2 christos eppcic_intr_establish, eppcic_intr_disestablish,
143 1.1.4.2 christos eppcic_socket_enable, eppcic_socket_disable,
144 1.1.4.2 christos eppcic_socket_settype
145 1.1.4.2 christos };
146 1.1.4.2 christos
147 1.1.4.2 christos void
148 1.1.4.2 christos eppcic_attach_common(struct device *parent, struct device *self, void *aux,
149 1.1.4.2 christos eppcic_chipset_tag_t pcic)
150 1.1.4.2 christos {
151 1.1.4.2 christos struct eppcic_softc *sc = (struct eppcic_softc *)self;
152 1.1.4.2 christos struct epsoc_attach_args *sa = aux;
153 1.1.4.2 christos struct eppcic_handle *ph;
154 1.1.4.2 christos int reg;
155 1.1.4.2 christos int i;
156 1.1.4.2 christos
157 1.1.4.2 christos if (!sa->sa_gpio) {
158 1.1.4.2 christos printf("%s: epgpio requires\n", self->dv_xname);
159 1.1.4.2 christos return;
160 1.1.4.2 christos }
161 1.1.4.2 christos sc->sc_gpio = sa->sa_gpio;
162 1.1.4.2 christos sc->sc_iot = sa->sa_iot;
163 1.1.4.2 christos sc->sc_hclk = sa->sa_hclk;
164 1.1.4.2 christos sc->sc_pcic = pcic;
165 1.1.4.2 christos sc->sc_enable = 0;
166 1.1.4.2 christos if (bus_space_map(sa->sa_iot, sa->sa_addr,
167 1.1.4.2 christos sa->sa_size, 0, &sc->sc_ioh)){
168 1.1.4.2 christos printf("%s: Cannot map registers\n", self->dv_xname);
169 1.1.4.2 christos return;
170 1.1.4.2 christos }
171 1.1.4.2 christos printf("\n");
172 1.1.4.2 christos
173 1.1.4.2 christos #if NEPLED > 0
174 1.1.4.2 christos epled_green_on();
175 1.1.4.2 christos epled_red_off();
176 1.1.4.2 christos #endif
177 1.1.4.2 christos /* socket 0 */
178 1.1.4.2 christos if (!(ph = malloc(sizeof(struct eppcic_handle), M_DEVBUF, M_NOWAIT))) {
179 1.1.4.2 christos printf("%s: Cannot allocate memory\n", self->dv_xname);
180 1.1.4.2 christos return; /* ENOMEM */
181 1.1.4.2 christos }
182 1.1.4.2 christos sc->sc_ph[0] = ph;
183 1.1.4.2 christos ph->ph_sc = sc;
184 1.1.4.2 christos ph->ph_socket = 0;
185 1.1.4.2 christos ph->ph_port = PORT_F;
186 1.1.4.2 christos ph->ph_cd[0] = SOCKET0_MCCD1;
187 1.1.4.2 christos ph->ph_cd[1] = SOCKET0_MCCD2;
188 1.1.4.2 christos ph->ph_vs[0] = SOCKET0_VS1;
189 1.1.4.2 christos ph->ph_vs[1] = SOCKET0_VS2;
190 1.1.4.2 christos ph->ph_ireq = SOCKET0_IREQ;
191 1.1.4.2 christos ph->ph_space[IO].reg = EP93XX_PCMCIA0_IO;
192 1.1.4.2 christos ph->ph_space[IO].base = EP93XX_PCMCIA0_HWBASE + EP93XX_PCMCIA_IO;
193 1.1.4.2 christos ph->ph_space[IO].size = EP93XX_PCMCIA_IO_SIZE;
194 1.1.4.2 christos ph->ph_space[COMMON].reg = EP93XX_PCMCIA0_Common;
195 1.1.4.2 christos ph->ph_space[COMMON].base = EP93XX_PCMCIA0_HWBASE
196 1.1.4.2 christos + EP93XX_PCMCIA_COMMON;
197 1.1.4.2 christos ph->ph_space[COMMON].size = EP93XX_PCMCIA_COMMON_SIZE;
198 1.1.4.2 christos ph->ph_space[ATTRIBUTE].reg = EP93XX_PCMCIA0_Attribute;
199 1.1.4.2 christos ph->ph_space[ATTRIBUTE].base = EP93XX_PCMCIA0_HWBASE
200 1.1.4.2 christos + EP93XX_PCMCIA_ATTRIBUTE;
201 1.1.4.2 christos ph->ph_space[ATTRIBUTE].size = EP93XX_PCMCIA_ATTRIBUTE_SIZE;
202 1.1.4.2 christos eppcic_attach_socket(ph);
203 1.1.4.2 christos
204 1.1.4.2 christos reg = EP93XX_PCMCIA_WEN | (pcic->socket_type)(sc, 0);
205 1.1.4.2 christos bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_PCMCIA_Ctrl,
206 1.1.4.2 christos EP93XX_PCMCIA_RST | reg);
207 1.1.4.2 christos delay(10);
208 1.1.4.2 christos bus_space_write_4(sc->sc_iot, sc->sc_ioh, EP93XX_PCMCIA_Ctrl, reg);
209 1.1.4.2 christos delay(500);
210 1.1.4.2 christos
211 1.1.4.2 christos for (i = 0; i < EP93XX_PCMCIA_NSOCKET; i++)
212 1.1.4.2 christos eppcic_config_socket(sc->sc_ph[i]);
213 1.1.4.2 christos #if NEPLED > 0
214 1.1.4.2 christos epled_green_off();
215 1.1.4.2 christos #endif
216 1.1.4.2 christos }
217 1.1.4.2 christos
218 1.1.4.2 christos static void
219 1.1.4.2 christos eppcic_attach_socket(struct eppcic_handle *ph)
220 1.1.4.2 christos {
221 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
222 1.1.4.2 christos eppcic_chipset_tag_t pcic = sc->sc_pcic;
223 1.1.4.2 christos int wait;
224 1.1.4.2 christos
225 1.1.4.2 christos ph->ph_width = 16;
226 1.1.4.2 christos ph->ph_vcc = 3;
227 1.1.4.2 christos ph->ph_event_thread = NULL;
228 1.1.4.2 christos ph->ph_run = 0;
229 1.1.4.2 christos ph->ph_ih_func = NULL;
230 1.1.4.2 christos ph->ph_ih_arg = NULL;
231 1.1.4.2 christos epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
232 1.1.4.2 christos epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
233 1.1.4.2 christos epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_vs[0]);
234 1.1.4.2 christos epgpio_in(sc->sc_gpio, ph->ph_port, ph->ph_vs[1]);
235 1.1.4.2 christos ph->ph_status[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
236 1.1.4.2 christos ph->ph_status[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
237 1.1.4.2 christos wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
238 1.1.4.2 christos delay(wait);
239 1.1.4.2 christos eppcic_set_pcreg(ph, ph->ph_space[IO].reg);
240 1.1.4.2 christos eppcic_set_pcreg(ph, ph->ph_space[COMMON].reg);
241 1.1.4.2 christos eppcic_set_pcreg(ph, ph->ph_space[ATTRIBUTE].reg);
242 1.1.4.2 christos wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_ON);
243 1.1.4.2 christos delay(wait);
244 1.1.4.2 christos }
245 1.1.4.2 christos
246 1.1.4.2 christos static void
247 1.1.4.2 christos eppcic_config_socket(struct eppcic_handle *ph)
248 1.1.4.2 christos {
249 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
250 1.1.4.2 christos eppcic_chipset_tag_t pcic = sc->sc_pcic;
251 1.1.4.2 christos struct pcmciabus_attach_args paa;
252 1.1.4.2 christos int wait;
253 1.1.4.2 christos
254 1.1.4.2 christos paa.paa_busname = "pcmcia";
255 1.1.4.2 christos paa.pct = (pcmcia_chipset_tag_t)&eppcic_functions;
256 1.1.4.2 christos paa.pch = (pcmcia_chipset_handle_t)ph;
257 1.1.4.2 christos paa.iobase = ph->ph_space[IO].base;
258 1.1.4.2 christos paa.iosize = ph->ph_space[IO].size;
259 1.1.4.2 christos ph->ph_card = config_found_ia((void*)sc, "pcmciabus", &paa,
260 1.1.4.2 christos eppcic_print);
261 1.1.4.2 christos
262 1.1.4.2 christos epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_cd[0],
263 1.1.4.2 christos EDGE_TRIGGER | FALLING_EDGE | DEBOUNCE,
264 1.1.4.2 christos IPL_TTY, eppcic_intr_carddetect, ph);
265 1.1.4.2 christos epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_cd[1],
266 1.1.4.2 christos EDGE_TRIGGER | RISING_EDGE | DEBOUNCE,
267 1.1.4.2 christos IPL_TTY, eppcic_intr_carddetect, ph);
268 1.1.4.2 christos wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
269 1.1.4.2 christos delay(wait);
270 1.1.4.2 christos
271 1.1.4.2 christos kthread_create(eppcic_create_event_thread, ph);
272 1.1.4.2 christos }
273 1.1.4.2 christos
274 1.1.4.2 christos static int
275 1.1.4.2 christos eppcic_print(void *arg, const char *pnp)
276 1.1.4.2 christos {
277 1.1.4.2 christos return (UNCONF);
278 1.1.4.2 christos }
279 1.1.4.2 christos
280 1.1.4.2 christos static void
281 1.1.4.2 christos eppcic_create_event_thread(void *arg)
282 1.1.4.2 christos {
283 1.1.4.2 christos struct eppcic_handle *ph = arg;
284 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
285 1.1.4.2 christos
286 1.1.4.2 christos ph->ph_status[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
287 1.1.4.2 christos ph->ph_status[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
288 1.1.4.2 christos
289 1.1.4.2 christos DPRINTFN(1, ("eppcic_create_event_thread: cd1=%d, cd2=%d\n",ph->ph_status[0],ph->ph_status[1]));
290 1.1.4.2 christos
291 1.1.4.2 christos if (!(ph->ph_status[0] | ph->ph_status[1]))
292 1.1.4.2 christos pcmcia_card_attach(ph->ph_card);
293 1.1.4.2 christos
294 1.1.4.2 christos ph->ph_run = 1;
295 1.1.4.2 christos kthread_create1(eppcic_event_thread, ph, &ph->ph_event_thread,
296 1.1.4.2 christos "%s,%d", sc->sc_dev.dv_xname, ph->ph_socket);
297 1.1.4.2 christos }
298 1.1.4.2 christos
299 1.1.4.2 christos static void
300 1.1.4.2 christos eppcic_event_thread(void *arg)
301 1.1.4.2 christos {
302 1.1.4.2 christos struct eppcic_handle *ph = arg;
303 1.1.4.2 christos
304 1.1.4.2 christos for (;;) {
305 1.1.4.2 christos tsleep(ph, PWAIT, "CSC wait", 0);
306 1.1.4.2 christos if (!ph->ph_run)
307 1.1.4.2 christos break;
308 1.1.4.2 christos
309 1.1.4.2 christos DPRINTFN(1, ("eppcic_event_thread: cd1=%d, cd2=%d\n",ph->ph_status[0],ph->ph_status[1]));
310 1.1.4.2 christos
311 1.1.4.2 christos if (!ph->ph_status[0] && !ph->ph_status[1])
312 1.1.4.2 christos pcmcia_card_attach(ph->ph_card);
313 1.1.4.2 christos else if (ph->ph_status[0] && ph->ph_status[1])
314 1.1.4.2 christos pcmcia_card_detach(ph->ph_card, DETACH_FORCE);
315 1.1.4.2 christos }
316 1.1.4.2 christos
317 1.1.4.2 christos DPRINTFN(1, ("eppcic_event_thread: run=%d\n",ph->ph_run));
318 1.1.4.2 christos ph->ph_event_thread = NULL;
319 1.1.4.2 christos kthread_exit(0);
320 1.1.4.2 christos }
321 1.1.4.2 christos
322 1.1.4.2 christos void
323 1.1.4.2 christos eppcic_shutdown(void *arg)
324 1.1.4.2 christos {
325 1.1.4.2 christos struct eppcic_handle *ph = arg;
326 1.1.4.2 christos
327 1.1.4.2 christos DPRINTFN(1, ("eppcic_shutdown\n"));
328 1.1.4.2 christos ph->ph_run = 0;
329 1.1.4.2 christos wakeup(ph);
330 1.1.4.2 christos }
331 1.1.4.2 christos
332 1.1.4.2 christos static int
333 1.1.4.2 christos eppcic_intr_carddetect(void *arg)
334 1.1.4.2 christos {
335 1.1.4.2 christos struct eppcic_handle *ph = arg;
336 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
337 1.1.4.2 christos int nstatus[2];
338 1.1.4.2 christos
339 1.1.4.2 christos nstatus[0] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[0]);
340 1.1.4.2 christos nstatus[1] = epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_cd[1]);
341 1.1.4.2 christos
342 1.1.4.2 christos DPRINTFN(1, ("eppcic_intr: cd1=%#x, cd2=%#x\n",nstatus[0],nstatus[1]));
343 1.1.4.2 christos
344 1.1.4.2 christos if (nstatus[0] != ph->ph_status[0] || nstatus[1] != ph->ph_status[1])
345 1.1.4.2 christos wakeup(ph);
346 1.1.4.2 christos ph->ph_status[0] = nstatus[0];
347 1.1.4.2 christos ph->ph_status[1] = nstatus[1];
348 1.1.4.2 christos return 0;
349 1.1.4.2 christos }
350 1.1.4.2 christos
351 1.1.4.2 christos static int
352 1.1.4.2 christos eppcic_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
353 1.1.4.2 christos struct pcmcia_mem_handle *pmh)
354 1.1.4.2 christos {
355 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
356 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
357 1.1.4.2 christos
358 1.1.4.2 christos DPRINTFN(1, ("eppcic_mem_alloc: size=%#x\n",(unsigned)size));
359 1.1.4.2 christos
360 1.1.4.2 christos pmh->memt = sc->sc_iot;
361 1.1.4.2 christos return 0;
362 1.1.4.2 christos }
363 1.1.4.2 christos
364 1.1.4.2 christos static void
365 1.1.4.2 christos eppcic_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
366 1.1.4.2 christos {
367 1.1.4.2 christos DPRINTFN(1, ("eppcic_mem_free\n"));
368 1.1.4.2 christos }
369 1.1.4.2 christos
370 1.1.4.2 christos static int
371 1.1.4.2 christos eppcic_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
372 1.1.4.2 christos bus_size_t size, struct pcmcia_mem_handle *pmh,
373 1.1.4.2 christos bus_size_t *offsetp, int *windowp)
374 1.1.4.2 christos {
375 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
376 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
377 1.1.4.2 christos bus_addr_t pa;
378 1.1.4.2 christos int err;
379 1.1.4.2 christos
380 1.1.4.2 christos DPRINTFN(1, ("eppcic_mem_map: kind=%d, addr=%#x, size=%#x\n",kind,(unsigned)addr,(unsigned)size));
381 1.1.4.2 christos
382 1.1.4.2 christos pa = addr;
383 1.1.4.2 christos *offsetp = 0;
384 1.1.4.2 christos size = round_page(size);
385 1.1.4.2 christos pmh->realsize = size;
386 1.1.4.2 christos if (kind & PCMCIA_WIDTH_MEM8)
387 1.1.4.2 christos ph->ph_width = 8;
388 1.1.4.2 christos else
389 1.1.4.2 christos ph->ph_width = 16;
390 1.1.4.2 christos switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
391 1.1.4.2 christos case PCMCIA_MEM_ATTR:
392 1.1.4.2 christos eppcic_set_pcreg(ph, ph->ph_space[ATTRIBUTE].reg);
393 1.1.4.2 christos pa += ph->ph_space[ATTRIBUTE].base;
394 1.1.4.2 christos break;
395 1.1.4.2 christos case PCMCIA_MEM_COMMON:
396 1.1.4.2 christos eppcic_set_pcreg(ph, ph->ph_space[COMMON].reg);
397 1.1.4.2 christos pa += ph->ph_space[COMMON].base;
398 1.1.4.2 christos break;
399 1.1.4.2 christos default:
400 1.1.4.2 christos return -1;
401 1.1.4.2 christos }
402 1.1.4.2 christos
403 1.1.4.2 christos DPRINTFN(1, ("eppcic_mem_map: pa=%#x, *offsetp=%#x, size=%#x\n",(unsigned)pa,(unsigned)addr,(unsigned)size));
404 1.1.4.2 christos
405 1.1.4.2 christos if (!(err = bus_space_map(sc->sc_iot, pa, size, 0, &pmh->memh)))
406 1.1.4.2 christos *windowp = (int)pmh->memh;
407 1.1.4.2 christos return err;
408 1.1.4.2 christos }
409 1.1.4.2 christos
410 1.1.4.2 christos static void
411 1.1.4.2 christos eppcic_mem_unmap(pcmcia_chipset_handle_t pch, int window)
412 1.1.4.2 christos {
413 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
414 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
415 1.1.4.2 christos
416 1.1.4.2 christos DPRINTFN(1, ("eppcic_mem_unmap: window=%#x\n",window));
417 1.1.4.2 christos
418 1.1.4.2 christos bus_space_unmap(sc->sc_iot, (bus_addr_t)window, 0x400);
419 1.1.4.2 christos }
420 1.1.4.2 christos
421 1.1.4.2 christos static int
422 1.1.4.2 christos eppcic_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
423 1.1.4.2 christos bus_size_t align, struct pcmcia_io_handle *pih)
424 1.1.4.2 christos {
425 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
426 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
427 1.1.4.2 christos bus_addr_t pa;
428 1.1.4.2 christos
429 1.1.4.2 christos DPRINTFN(1, ("eppcic_io_alloc: start=%#x, size=%#x, align=%#x\n",(unsigned)start,(unsigned)size,(unsigned)align));
430 1.1.4.2 christos
431 1.1.4.2 christos pih->iot = sc->sc_iot;
432 1.1.4.2 christos pih->addr = start;
433 1.1.4.2 christos pih->size = size;
434 1.1.4.2 christos pa = pih->addr + ph->ph_space[IO].base;
435 1.1.4.2 christos return bus_space_map(sc->sc_iot, pa, size, 0, &pih->ioh);
436 1.1.4.2 christos }
437 1.1.4.2 christos
438 1.1.4.2 christos static void
439 1.1.4.2 christos eppcic_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
440 1.1.4.2 christos {
441 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
442 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
443 1.1.4.2 christos
444 1.1.4.2 christos DPRINTFN(1, ("eppcic_io_free\n"));
445 1.1.4.2 christos
446 1.1.4.2 christos bus_space_unmap(sc->sc_iot, pih->ioh, pih->size);
447 1.1.4.2 christos }
448 1.1.4.2 christos
449 1.1.4.2 christos static int
450 1.1.4.2 christos eppcic_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
451 1.1.4.2 christos bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
452 1.1.4.2 christos {
453 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
454 1.1.4.2 christos
455 1.1.4.2 christos DPRINTFN(1, ("eppcic_io_map: offset=%#x, size=%#x, width=%d",(unsigned)offset,(unsigned)size,width));
456 1.1.4.2 christos
457 1.1.4.2 christos switch (width) {
458 1.1.4.2 christos case PCMCIA_WIDTH_IO8:
459 1.1.4.2 christos DPRINTFN(1, ("(8bit)\n"));
460 1.1.4.2 christos ph->ph_width = 8;
461 1.1.4.2 christos break;
462 1.1.4.2 christos case PCMCIA_WIDTH_IO16:
463 1.1.4.2 christos case PCMCIA_WIDTH_AUTO: /* I don't understand how I check it */
464 1.1.4.2 christos DPRINTFN(1, ("(16bit)\n"));
465 1.1.4.2 christos ph->ph_width = 16;
466 1.1.4.2 christos break;
467 1.1.4.2 christos default:
468 1.1.4.2 christos DPRINTFN(1, ("(unknown)\n"));
469 1.1.4.2 christos return -1;
470 1.1.4.2 christos }
471 1.1.4.2 christos eppcic_set_pcreg(ph, ph->ph_space[IO].reg);
472 1.1.4.2 christos *windowp = 0; /* unused */
473 1.1.4.2 christos return 0;
474 1.1.4.2 christos }
475 1.1.4.2 christos
476 1.1.4.2 christos static void
477 1.1.4.2 christos eppcic_io_unmap(pcmcia_chipset_handle_t pch, int window)
478 1.1.4.2 christos {
479 1.1.4.2 christos DPRINTFN(1, ("eppcic_io_unmap: window=%#x\n",window));
480 1.1.4.2 christos }
481 1.1.4.2 christos
482 1.1.4.2 christos static void *
483 1.1.4.2 christos eppcic_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
484 1.1.4.2 christos int ipl, int (*ih_func)(void *), void *ih_arg)
485 1.1.4.2 christos {
486 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
487 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
488 1.1.4.2 christos
489 1.1.4.2 christos DPRINTFN(1, ("eppcic_intr_establish\n"));
490 1.1.4.2 christos
491 1.1.4.2 christos if (ph->ph_ih_func)
492 1.1.4.2 christos return 0;
493 1.1.4.2 christos
494 1.1.4.2 christos ph->ph_ih_func = ih_func;
495 1.1.4.2 christos ph->ph_ih_arg = ih_arg;
496 1.1.4.2 christos return epgpio_intr_establish(sc->sc_gpio, ph->ph_port, ph->ph_ireq,
497 1.1.4.2 christos LEVEL_SENSE | LOW_LEVEL,
498 1.1.4.2 christos ipl, eppcic_intr_socket, ph);
499 1.1.4.2 christos }
500 1.1.4.2 christos
501 1.1.4.2 christos static void
502 1.1.4.2 christos eppcic_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
503 1.1.4.2 christos {
504 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
505 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
506 1.1.4.2 christos
507 1.1.4.2 christos DPRINTFN(1, ("eppcic_intr_disestablish\n"));
508 1.1.4.2 christos
509 1.1.4.2 christos ph->ph_ih_func = NULL;
510 1.1.4.2 christos ph->ph_ih_arg = NULL;
511 1.1.4.2 christos epgpio_intr_disestablish(sc->sc_gpio, ph->ph_port, ph->ph_ireq);
512 1.1.4.2 christos }
513 1.1.4.2 christos
514 1.1.4.2 christos static int
515 1.1.4.2 christos eppcic_intr_socket(void *arg)
516 1.1.4.2 christos {
517 1.1.4.2 christos struct eppcic_handle *ph = arg;
518 1.1.4.2 christos int err = 0;
519 1.1.4.2 christos
520 1.1.4.2 christos if (ph->ph_ih_func) {
521 1.1.4.2 christos #if NEPLED > 0
522 1.1.4.2 christos epled_red_on();
523 1.1.4.2 christos #endif
524 1.1.4.2 christos err = (*ph->ph_ih_func)(ph->ph_ih_arg);
525 1.1.4.2 christos #if NEPLED > 0
526 1.1.4.2 christos epled_red_off();
527 1.1.4.2 christos #endif
528 1.1.4.2 christos }
529 1.1.4.2 christos return err;
530 1.1.4.2 christos }
531 1.1.4.2 christos
532 1.1.4.2 christos
533 1.1.4.2 christos static void
534 1.1.4.2 christos eppcic_socket_enable(pcmcia_chipset_handle_t pch)
535 1.1.4.2 christos {
536 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
537 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
538 1.1.4.2 christos eppcic_chipset_tag_t pcic = sc->sc_pcic;
539 1.1.4.2 christos int wait;
540 1.1.4.2 christos
541 1.1.4.2 christos DPRINTFN(1, ("eppcic_socket_enable\n"));
542 1.1.4.2 christos
543 1.1.4.2 christos wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_ON);
544 1.1.4.2 christos delay(wait);
545 1.1.4.2 christos #if NEPLED > 0
546 1.1.4.2 christos if (!sc->sc_enable++)
547 1.1.4.2 christos epled_green_on();
548 1.1.4.2 christos #endif
549 1.1.4.2 christos ph->ph_vcc = eppcic_get_voltage(ph);
550 1.1.4.2 christos }
551 1.1.4.2 christos
552 1.1.4.2 christos static void
553 1.1.4.2 christos eppcic_socket_disable(pcmcia_chipset_handle_t pch)
554 1.1.4.2 christos {
555 1.1.4.2 christos struct eppcic_handle *ph = (struct eppcic_handle *)pch;
556 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
557 1.1.4.2 christos eppcic_chipset_tag_t pcic = sc->sc_pcic;
558 1.1.4.2 christos int wait;
559 1.1.4.2 christos
560 1.1.4.2 christos DPRINTFN(1, ("eppcic_socket_disable\n"));
561 1.1.4.2 christos
562 1.1.4.2 christos wait = (pcic->power_ctl)(sc, ph->ph_socket, POWER_OFF);
563 1.1.4.2 christos delay(wait);
564 1.1.4.2 christos #if NEPLED > 0
565 1.1.4.2 christos if (!--sc->sc_enable)
566 1.1.4.2 christos epled_green_off();
567 1.1.4.2 christos #endif
568 1.1.4.2 christos }
569 1.1.4.2 christos
570 1.1.4.2 christos static void
571 1.1.4.2 christos eppcic_socket_settype(pcmcia_chipset_handle_t pch, int type)
572 1.1.4.2 christos {
573 1.1.4.2 christos DPRINTFN(1, ("eppcic_socket_settype: type=%d",type));
574 1.1.4.2 christos
575 1.1.4.2 christos switch (type) {
576 1.1.4.2 christos case PCMCIA_IFTYPE_MEMORY:
577 1.1.4.2 christos DPRINTFN(1, ("(Memory)\n"));
578 1.1.4.2 christos break;
579 1.1.4.2 christos case PCMCIA_IFTYPE_IO:
580 1.1.4.2 christos DPRINTFN(1, ("(I/O)\n"));
581 1.1.4.2 christos break;
582 1.1.4.2 christos default:
583 1.1.4.2 christos DPRINTFN(1, ("(unknown)\n"));
584 1.1.4.2 christos return;
585 1.1.4.2 christos }
586 1.1.4.2 christos }
587 1.1.4.2 christos
588 1.1.4.2 christos static int
589 1.1.4.2 christos eppcic_get_voltage(struct eppcic_handle *ph)
590 1.1.4.2 christos {
591 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
592 1.1.4.2 christos eppcic_chipset_tag_t pcic = sc->sc_pcic;
593 1.1.4.2 christos int cap, vcc = 0;
594 1.1.4.2 christos
595 1.1.4.2 christos cap = (pcic->power_capability)(sc, ph->ph_socket);
596 1.1.4.2 christos if (epgpio_read(sc->sc_gpio, ph->ph_port, ph->ph_vs[0])) {
597 1.1.4.2 christos if (cap | VCC_5V)
598 1.1.4.2 christos vcc = 5;
599 1.1.4.2 christos else
600 1.1.4.2 christos printf("%s: unsupported Vcc 5 Volts",
601 1.1.4.2 christos sc->sc_dev.dv_xname);
602 1.1.4.2 christos } else {
603 1.1.4.2 christos if (cap | VCC_3V)
604 1.1.4.2 christos vcc = 3;
605 1.1.4.2 christos else
606 1.1.4.2 christos printf("%s: unsupported Vcc 3.3 Volts",
607 1.1.4.2 christos sc->sc_dev.dv_xname);
608 1.1.4.2 christos }
609 1.1.4.2 christos DPRINTFN(1, ("eppcic_get_voltage: vs1=%d, vs2=%d (%dV)\n",epgpio_read_bit(sc->sc_gpio, ph->ph_port, ph->ph_vs[0]),epgpio_read_bit(sc->sc_gpio, ph->ph_port, ph->ph_vs[1]),vcc));
610 1.1.4.2 christos return vcc;
611 1.1.4.2 christos }
612 1.1.4.2 christos
613 1.1.4.2 christos #define EXTRA_DELAY 40
614 1.1.4.2 christos
615 1.1.4.2 christos static void
616 1.1.4.2 christos eppcic_set_pcreg(struct eppcic_handle *ph, int kind)
617 1.1.4.2 christos {
618 1.1.4.2 christos struct eppcic_softc *sc = ph->ph_sc;
619 1.1.4.2 christos int atiming, htiming, ptiming;
620 1.1.4.2 christos int period = 1000000000 / sc->sc_hclk;
621 1.1.4.2 christos int width;
622 1.1.4.2 christos
623 1.1.4.2 christos switch (ph->ph_width) {
624 1.1.4.2 christos case 8:
625 1.1.4.2 christos width = 0;
626 1.1.4.2 christos break;
627 1.1.4.2 christos case 16:
628 1.1.4.2 christos width = EP93XX_PCMCIA_WIDTH_16;
629 1.1.4.2 christos break;
630 1.1.4.2 christos default:
631 1.1.4.2 christos return;
632 1.1.4.2 christos }
633 1.1.4.2 christos switch (kind) {
634 1.1.4.2 christos case IO:
635 1.1.4.2 christos atiming = 165; htiming = 20; ptiming = 70;
636 1.1.4.2 christos break;
637 1.1.4.2 christos case COMMON:
638 1.1.4.2 christos #if linux_timing!=hamajima20050816
639 1.1.4.2 christos switch (ph->ph_vcc) {
640 1.1.4.2 christos case 3:
641 1.1.4.2 christos atiming = 465; htiming = 35; ptiming = 100;
642 1.1.4.2 christos break;
643 1.1.4.2 christos case 5:
644 1.1.4.2 christos atiming = 200; htiming = 20; ptiming = 30;
645 1.1.4.2 christos break;
646 1.1.4.2 christos default:
647 1.1.4.2 christos return;
648 1.1.4.2 christos }
649 1.1.4.2 christos break;
650 1.1.4.2 christos #endif
651 1.1.4.2 christos case ATTRIBUTE:
652 1.1.4.2 christos switch (ph->ph_vcc) {
653 1.1.4.2 christos case 3:
654 1.1.4.2 christos #if linux_timing!=hamajima20050816
655 1.1.4.2 christos atiming = 465; htiming = 35; ptiming = 100;
656 1.1.4.2 christos #else
657 1.1.4.2 christos atiming = 600; htiming = 35; ptiming = 100;
658 1.1.4.2 christos #endif
659 1.1.4.2 christos break;
660 1.1.4.2 christos case 5:
661 1.1.4.2 christos #if linux_timing!=hamajima20050816
662 1.1.4.2 christos atiming = 250; htiming = 20; ptiming = 30;
663 1.1.4.2 christos #else
664 1.1.4.2 christos atiming = 300; htiming = 20; ptiming = 30;
665 1.1.4.2 christos #endif
666 1.1.4.2 christos break;
667 1.1.4.2 christos default:
668 1.1.4.2 christos return;
669 1.1.4.2 christos }
670 1.1.4.2 christos break;
671 1.1.4.2 christos default:
672 1.1.4.2 christos return;
673 1.1.4.2 christos }
674 1.1.4.2 christos
675 1.1.4.2 christos #if linux_timing!=hamajima20050816
676 1.1.4.2 christos period = 1000000000 / 50000000;
677 1.1.4.2 christos width = EP93XX_PCMCIA_WIDTH_16;
678 1.1.4.2 christos #endif
679 1.1.4.2 christos
680 1.1.4.2 christos atiming = (atiming + EXTRA_DELAY) / period;
681 1.1.4.2 christos if (atiming>0xff)
682 1.1.4.2 christos atiming = 0xff;
683 1.1.4.2 christos htiming = ((htiming + EXTRA_DELAY) / period) + 1;
684 1.1.4.2 christos if (htiming>0xf)
685 1.1.4.2 christos htiming = 0xf;
686 1.1.4.2 christos ptiming = (ptiming + EXTRA_DELAY) / period;
687 1.1.4.2 christos if (ptiming>0xff)
688 1.1.4.2 christos ptiming = 0xff;
689 1.1.4.2 christos
690 1.1.4.2 christos DPRINTFN(1, ("eppcic_set_pcreg: width=%d, access=%d, hold=%d, pre-charge=%d\n",ph->ph_width,atiming,htiming,ptiming));
691 1.1.4.2 christos
692 1.1.4.2 christos bus_space_write_4(sc->sc_iot, sc->sc_ioh, ph->ph_space[kind].reg,
693 1.1.4.2 christos width
694 1.1.4.2 christos | (atiming<<EP93XX_PCMCIA_ACCESS_SHIFT)
695 1.1.4.2 christos | (htiming<<EP93XX_PCMCIA_HOLD_SHIFT)
696 1.1.4.2 christos | (ptiming<<EP93XX_PCMCIA_PRECHARGE_SHIFT));
697 1.1.4.2 christos }
698