gb225_pcic.c revision 1.1 1 1.1 bsh /*
2 1.1 bsh * Copyright (c) 2002, 2003, 2005 Genetec corp. All rights reserved.
3 1.1 bsh *
4 1.1 bsh * PCMCIA/CF support for TWINTAIL (G4255EB)
5 1.1 bsh * Written by Hiroyuki Bessho for Genetec corp.
6 1.1 bsh *
7 1.1 bsh * Redistribution and use in source and binary forms, with or without
8 1.1 bsh * modification, are permitted provided that the following conditions
9 1.1 bsh * are met:
10 1.1 bsh * 1. Redistributions of source code must retain the above copyright
11 1.1 bsh * notice, this list of conditions and the following disclaimer.
12 1.1 bsh * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bsh * notice, this list of conditions and the following disclaimer in the
14 1.1 bsh * documentation and/or other materials provided with the distribution.
15 1.1 bsh * 3. The name of Genetec corp. may not be used to endorse
16 1.1 bsh * or promote products derived from this software without specific prior
17 1.1 bsh * written permission.
18 1.1 bsh *
19 1.1 bsh * THIS SOFTWARE IS PROVIDED BY GENETEC CORP. ``AS IS'' AND
20 1.1 bsh * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 bsh * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 bsh * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GENETEC CORP.
23 1.1 bsh * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 bsh * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 bsh * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 bsh * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 bsh * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 bsh * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 bsh * POSSIBILITY OF SUCH DAMAGE.
30 1.1 bsh */
31 1.1 bsh
32 1.1 bsh #include <sys/types.h>
33 1.1 bsh #include <sys/param.h>
34 1.1 bsh #include <sys/systm.h>
35 1.1 bsh #include <sys/device.h>
36 1.1 bsh #include <sys/callout.h>
37 1.1 bsh #include <sys/kernel.h>
38 1.1 bsh #include <sys/kthread.h>
39 1.1 bsh #include <sys/malloc.h>
40 1.1 bsh #include <uvm/uvm.h>
41 1.1 bsh
42 1.1 bsh #include <machine/bus.h>
43 1.1 bsh #include <machine/intr.h>
44 1.1 bsh
45 1.1 bsh #include <dev/pcmcia/pcmciareg.h>
46 1.1 bsh #include <dev/pcmcia/pcmciavar.h>
47 1.1 bsh #include <dev/pcmcia/pcmciachip.h>
48 1.1 bsh
49 1.1 bsh #include <arch/arm/xscale/pxa2x0var.h>
50 1.1 bsh #include <arch/arm/xscale/pxa2x0reg.h>
51 1.1 bsh #include <arch/arm/sa11x0/sa11xx_pcicvar.h>
52 1.1 bsh #include <arch/evbarm/g42xxeb/g42xxeb_reg.h>
53 1.1 bsh #include <arch/evbarm/g42xxeb/g42xxeb_var.h>
54 1.1 bsh #include <arch/evbarm/g42xxeb/gb225reg.h>
55 1.1 bsh #include <arch/evbarm/g42xxeb/gb225var.h>
56 1.1 bsh
57 1.1 bsh
58 1.1 bsh //#define DONT_USE_CARD_DETECT_INTR
59 1.1 bsh
60 1.1 bsh #define PCMCIA_INT G42XXEB_INT_EXT1
61 1.1 bsh #define CF_INT G42XXEB_INT_EXT0
62 1.1 bsh
63 1.1 bsh #ifdef DEBUG
64 1.1 bsh #define DPRINTF(arg) printf arg
65 1.1 bsh #else
66 1.1 bsh #define DPRINTF(arg)
67 1.1 bsh #endif
68 1.1 bsh
69 1.1 bsh struct opcic_softc;
70 1.1 bsh
71 1.1 bsh struct opcic_socket {
72 1.1 bsh struct sapcic_socket ss; /* inherit socket for sa11x0 pcic */
73 1.1 bsh
74 1.1 bsh #if 0
75 1.1 bsh int voltage; /* card power voltage selected by
76 1.1 bsh upper layer */
77 1.1 bsh #endif
78 1.1 bsh };
79 1.1 bsh
80 1.1 bsh struct opcic_softc {
81 1.1 bsh struct sapcic_softc sc_pc; /* inherit SA11xx pcic */
82 1.1 bsh
83 1.1 bsh struct opcic_socket sc_socket[2];
84 1.1 bsh int sc_cards;
85 1.1 bsh bus_space_handle_t sc_memctl_ioh;
86 1.1 bsh };
87 1.1 bsh
88 1.1 bsh static int opcic_match(struct device *, struct cfdata *, void *);
89 1.1 bsh static void opcic_attach(struct device *, struct device *, void *);
90 1.1 bsh static int opcic_print(void *, const char *);
91 1.1 bsh static int opcic_submatch(struct device *, struct cfdata *, void *);
92 1.1 bsh
93 1.1 bsh static int opcic_read(struct sapcic_socket *, int);
94 1.1 bsh static void opcic_write(struct sapcic_socket *, int, int);
95 1.1 bsh static void opcic_set_power(struct sapcic_socket *, int);
96 1.1 bsh static void opcic_clear_intr(int);
97 1.1 bsh static void *opcic_intr_establish(struct sapcic_socket *, int,
98 1.1 bsh int (*)(void *), void *);
99 1.1 bsh static void opcic_intr_disestablish(struct sapcic_socket *, void *);
100 1.1 bsh #ifndef DONT_USE_CARD_DETECT_INTR
101 1.1 bsh static int opcic_card_detect(void *, int);
102 1.1 bsh #endif
103 1.1 bsh
104 1.1 bsh CFATTACH_DECL(opcic, sizeof(struct opcic_softc),
105 1.1 bsh opcic_match, opcic_attach, NULL, NULL);
106 1.1 bsh
107 1.1 bsh static struct sapcic_tag opcic_tag = {
108 1.1 bsh opcic_read,
109 1.1 bsh opcic_write,
110 1.1 bsh opcic_set_power,
111 1.1 bsh opcic_clear_intr,
112 1.1 bsh opcic_intr_establish,
113 1.1 bsh opcic_intr_disestablish,
114 1.1 bsh };
115 1.1 bsh
116 1.1 bsh
117 1.1 bsh #define HAVE_CARD(r) (((r)&CARDDET_DET)==0)
118 1.1 bsh
119 1.1 bsh static __inline uint8_t
120 1.1 bsh opcic_read_card_status(struct opcic_socket *so)
121 1.1 bsh {
122 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)(so->ss.sc);
123 1.1 bsh struct opio_softc *osc =
124 1.1 bsh (struct opio_softc *)(sc->sc_pc.sc_dev.dv_parent);
125 1.1 bsh
126 1.1 bsh return bus_space_read_1(osc->sc_iot, osc->sc_ioh,
127 1.1 bsh GB225_CFDET + 2 * so->ss.socket);
128 1.1 bsh
129 1.1 bsh }
130 1.1 bsh
131 1.1 bsh static int
132 1.1 bsh opcic_match(struct device *parent, struct cfdata *cf, void *aux)
133 1.1 bsh {
134 1.1 bsh return 1;
135 1.1 bsh }
136 1.1 bsh
137 1.1 bsh static void
138 1.1 bsh opcic_attach(struct device *parent, struct device *self, void *aux)
139 1.1 bsh {
140 1.1 bsh int i;
141 1.1 bsh struct pcmciabus_attach_args paa;
142 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)self;
143 1.1 bsh struct opio_softc *psc = (struct opio_softc *)parent;
144 1.1 bsh struct obio_softc *bsc = (struct obio_softc *)parent->dv_parent;
145 1.1 bsh bus_space_handle_t memctl_ioh = bsc->sc_memctl_ioh;
146 1.1 bsh bus_space_tag_t iot = psc->sc_iot;
147 1.1 bsh
148 1.1 bsh printf("\n");
149 1.1 bsh
150 1.1 bsh /* tell PXA2X0 that we have two sockets */
151 1.1 bsh #if 0
152 1.1 bsh bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR, MECR_NOS);
153 1.1 bsh #else
154 1.1 bsh bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR, MECR_CIT|MECR_NOS);
155 1.1 bsh #endif
156 1.1 bsh sc->sc_pc.sc_iot = psc->sc_iot;
157 1.1 bsh sc->sc_memctl_ioh = memctl_ioh;
158 1.1 bsh
159 1.1 bsh sc->sc_cards = 0;
160 1.1 bsh
161 1.1 bsh for(i = 0; i < 2; i++) {
162 1.1 bsh sc->sc_socket[i].ss.sc = &sc->sc_pc;
163 1.1 bsh sc->sc_socket[i].ss.socket = i;
164 1.1 bsh sc->sc_socket[i].ss.pcictag_cookie = NULL;
165 1.1 bsh sc->sc_socket[i].ss.pcictag = &opcic_tag;
166 1.1 bsh sc->sc_socket[i].ss.event_thread = NULL;
167 1.1 bsh sc->sc_socket[i].ss.event = 0;
168 1.1 bsh sc->sc_socket[i].ss.laststatus = CARDDET_NOCARD;
169 1.1 bsh sc->sc_socket[i].ss.shutdown = 0;
170 1.1 bsh
171 1.1 bsh sc->sc_socket[i].ss.power_capability =
172 1.1 bsh (SAPCIC_POWER_5V|SAPCIC_POWER_3V);
173 1.1 bsh
174 1.1 bsh bus_space_write_4(iot, memctl_ioh, MEMCTL_MCIO(i),
175 1.1 bsh MC_TIMING_VAL(1,1,1));
176 1.1 bsh #if 0
177 1.1 bsh bus_space_write_4(iot, memctl_ioh, MEMCTL_MCATT(i),
178 1.1 bsh MC_TIMING_VAL(31,31,31));
179 1.1 bsh #endif
180 1.1 bsh
181 1.1 bsh paa.paa_busname = "pcmcia";
182 1.1 bsh paa.pct = (pcmcia_chipset_tag_t)&sa11x0_pcmcia_functions;
183 1.1 bsh paa.pch = (pcmcia_chipset_handle_t)&sc->sc_socket[i].ss;
184 1.1 bsh paa.iobase = 0;
185 1.1 bsh paa.iosize = 0x4000000;
186 1.1 bsh
187 1.1 bsh sc->sc_socket[i].ss.pcmcia =
188 1.1 bsh (struct device *)config_found_sm(&sc->sc_pc.sc_dev,
189 1.1 bsh &paa, opcic_print, opcic_submatch);
190 1.1 bsh
191 1.1 bsh #ifndef DONT_USE_CARD_DETECT_INTR
192 1.1 bsh /* interrupt for card insertion/removal */
193 1.1 bsh opio_intr_establish(psc,
194 1.1 bsh i==0 ? OPIO_INTR_CF_INSERT : OPIO_INTR_PCMCIA_INSERT,
195 1.1 bsh IPL_BIO, opcic_card_detect, &sc->sc_socket[i]);
196 1.1 bsh #else
197 1.1 bsh bus_space_write_4(iot, ioh, MEMCTL_MECR, MECR_NOS | MECR_CIT);
198 1.1 bsh
199 1.1 bsh #endif
200 1.1 bsh
201 1.1 bsh /* schedule kthread creation */
202 1.1 bsh kthread_create(sapcic_kthread_create, &sc->sc_socket[i].ss);
203 1.1 bsh }
204 1.1 bsh
205 1.1 bsh }
206 1.1 bsh
207 1.1 bsh static int
208 1.1 bsh opcic_print(void *aux, const char *name)
209 1.1 bsh {
210 1.1 bsh return (UNCONF);
211 1.1 bsh }
212 1.1 bsh
213 1.1 bsh static int
214 1.1 bsh opcic_submatch(struct device *parent, struct cfdata *cf, void *aux)
215 1.1 bsh {
216 1.1 bsh return config_match(parent, cf, aux);
217 1.1 bsh }
218 1.1 bsh
219 1.1 bsh #ifndef DONT_USE_CARD_DETECT_INTR
220 1.1 bsh static int
221 1.1 bsh opcic_card_detect(void *arg, int val)
222 1.1 bsh {
223 1.1 bsh struct opcic_socket *socket = arg;
224 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)socket->ss.sc;
225 1.1 bsh bus_space_tag_t iot = sc->sc_pc.sc_iot;
226 1.1 bsh bus_space_handle_t memctl_ioh = sc->sc_memctl_ioh;
227 1.1 bsh int sock_no = socket->ss.socket;
228 1.1 bsh int last, s;
229 1.1 bsh
230 1.1 bsh s = splbio();
231 1.1 bsh last = sc->sc_cards;
232 1.1 bsh if (HAVE_CARD(val)) {
233 1.1 bsh sc->sc_cards |= 1<<sock_no;
234 1.1 bsh /* if it is the first card, turn on expansion memory
235 1.1 bsh * control. */
236 1.1 bsh if (last == 0)
237 1.1 bsh bus_space_write_4(iot, memctl_ioh, MEMCTL_MECR,
238 1.1 bsh MECR_NOS | MECR_CIT);
239 1.1 bsh }
240 1.1 bsh else {
241 1.1 bsh sc->sc_cards &= ~(1<<sock_no);
242 1.1 bsh /* if we loast all cards, turn off expansion memory
243 1.1 bsh * control. */
244 1.1 bsh #if 0
245 1.1 bsh if (sc->sc_cards == 0)
246 1.1 bsh bus_space_write_4(iot, memctl_ioh,
247 1.1 bsh MEMCTL_MECR, MECR_NOS);
248 1.1 bsh #endif
249 1.1 bsh }
250 1.1 bsh splx(s);
251 1.1 bsh
252 1.1 bsh DPRINTF(("%s: card %d %s\n", sc->sc_pc.sc_dev.dv_xname, sock_no,
253 1.1 bsh HAVE_CARD(val) ? "inserted" : "removed"));
254 1.1 bsh
255 1.1 bsh sapcic_intr(arg);
256 1.1 bsh
257 1.1 bsh return 1;
258 1.1 bsh }
259 1.1 bsh #endif /* DONT_USE_CARD_DETECT_INTR */
260 1.1 bsh
261 1.1 bsh static int
262 1.1 bsh opcic_read(struct sapcic_socket *__so, int which)
263 1.1 bsh {
264 1.1 bsh struct opcic_socket *so = (struct opcic_socket *)__so;
265 1.1 bsh uint8_t reg;
266 1.1 bsh
267 1.1 bsh reg = opcic_read_card_status(so);
268 1.1 bsh
269 1.1 bsh switch (which) {
270 1.1 bsh case SAPCIC_STATUS_CARD:
271 1.1 bsh return HAVE_CARD(reg) ?
272 1.1 bsh SAPCIC_CARD_VALID : SAPCIC_CARD_INVALID;
273 1.1 bsh
274 1.1 bsh case SAPCIC_STATUS_VS1:
275 1.1 bsh return (reg & CARDDET_NVS1) == 0;
276 1.1 bsh
277 1.1 bsh case SAPCIC_STATUS_VS2:
278 1.1 bsh return (reg & CARDDET_NVS2) == 0;
279 1.1 bsh
280 1.1 bsh case SAPCIC_STATUS_READY:
281 1.1 bsh return 1;
282 1.1 bsh
283 1.1 bsh default:
284 1.1 bsh panic("%s: bogus register", __FUNCTION__);
285 1.1 bsh }
286 1.1 bsh }
287 1.1 bsh
288 1.1 bsh static void
289 1.1 bsh opcic_write(struct sapcic_socket *__so, int which, int arg)
290 1.1 bsh {
291 1.1 bsh struct opcic_socket *so = (struct opcic_socket *)__so;
292 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)so->ss.sc;
293 1.1 bsh struct opio_softc *psc =
294 1.1 bsh (struct opio_softc *)sc->sc_pc.sc_dev.dv_parent;
295 1.1 bsh struct obio_softc *bsc =
296 1.1 bsh (struct obio_softc *)psc->sc_dev.dv_parent;
297 1.1 bsh
298 1.1 bsh switch (which) {
299 1.1 bsh case SAPCIC_CONTROL_RESET:
300 1.1 bsh obio_peripheral_reset(bsc, so->ss.socket, arg);
301 1.1 bsh delay(500*1000);
302 1.1 bsh break;
303 1.1 bsh
304 1.1 bsh case SAPCIC_CONTROL_LINEENABLE:
305 1.1 bsh break;
306 1.1 bsh
307 1.1 bsh case SAPCIC_CONTROL_WAITENABLE:
308 1.1 bsh break;
309 1.1 bsh
310 1.1 bsh case SAPCIC_CONTROL_POWERSELECT:
311 1.1 bsh #if 0
312 1.1 bsh so->voltage = arg;
313 1.1 bsh #endif
314 1.1 bsh break;
315 1.1 bsh
316 1.1 bsh default:
317 1.1 bsh panic("%s: bogus register", __FUNCTION__);
318 1.1 bsh }
319 1.1 bsh }
320 1.1 bsh
321 1.1 bsh static void
322 1.1 bsh opcic_set_power(struct sapcic_socket *__so, int arg)
323 1.1 bsh {
324 1.1 bsh struct opcic_socket *so = (struct opcic_socket *)__so;
325 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)so->ss.sc;
326 1.1 bsh struct opio_softc *psc =
327 1.1 bsh (struct opio_softc *)sc->sc_pc.sc_dev.dv_parent;
328 1.1 bsh int shift, save;
329 1.1 bsh volatile uint8_t *p;
330 1.1 bsh
331 1.1 bsh if( arg < 0 || SAPCIC_POWER_5V < arg )
332 1.1 bsh panic("sacpcic_set_power: bogus arg\n");
333 1.1 bsh
334 1.1 bsh DPRINTF(("card %d: DET=%x\n",
335 1.1 bsh so->ss.socket,
336 1.1 bsh bus_space_read_1(psc->sc_iot, psc->sc_ioh,
337 1.1 bsh GB225_CFDET + 2*so->ss.socket)));
338 1.1 bsh
339 1.1 bsh p = (volatile uint8_t *)bus_space_vaddr(psc->sc_iot, psc->sc_ioh)
340 1.1 bsh + GB225_CARDPOW;
341 1.1 bsh
342 1.1 bsh shift = 4 * !so->ss.socket;
343 1.1 bsh
344 1.1 bsh save = disable_interrupts(I32_bit);
345 1.1 bsh *p = (*p & ~(0x0f << shift)) | ((CARDPOW_VPPVCC | (arg<<2)) << shift);
346 1.1 bsh restore_interrupts(save);
347 1.1 bsh
348 1.1 bsh DPRINTF(("card %d power: %x\n", so->ss.socket, *p));
349 1.1 bsh }
350 1.1 bsh
351 1.1 bsh static void
352 1.1 bsh opcic_clear_intr(int arg)
353 1.1 bsh {
354 1.1 bsh }
355 1.1 bsh
356 1.1 bsh static void *
357 1.1 bsh opcic_intr_establish(struct sapcic_socket *so, int level,
358 1.1 bsh int (* ih_fun)(void *), void *ih_arg)
359 1.1 bsh {
360 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)so->sc;
361 1.1 bsh struct opio_softc *psc =
362 1.1 bsh (struct opio_softc *)sc->sc_pc.sc_dev.dv_parent;
363 1.1 bsh struct obio_softc *bsc =
364 1.1 bsh (struct obio_softc *)psc->sc_dev.dv_parent;
365 1.1 bsh int irq;
366 1.1 bsh
367 1.1 bsh DPRINTF(("opcic_intr_establish %d\n", so->socket));
368 1.1 bsh
369 1.1 bsh irq = so->socket ? PCMCIA_INT : CF_INT;
370 1.1 bsh return obio_intr_establish(bsc, irq, level, IST_EDGE_FALLING,
371 1.1 bsh ih_fun, ih_arg);
372 1.1 bsh }
373 1.1 bsh
374 1.1 bsh static void
375 1.1 bsh opcic_intr_disestablish(struct sapcic_socket *so, void *ih)
376 1.1 bsh {
377 1.1 bsh struct opcic_softc *sc = (struct opcic_softc *)so->sc;
378 1.1 bsh struct opio_softc *psc =
379 1.1 bsh (struct opio_softc *)sc->sc_pc.sc_dev.dv_parent;
380 1.1 bsh struct obio_softc *bsc =
381 1.1 bsh (struct obio_softc *)psc->sc_dev.dv_parent;
382 1.1 bsh int (* func)(void *) = ((struct obio_handler *)ih)->func;
383 1.1 bsh
384 1.1 bsh int irq = so->socket ? PCMCIA_INT : CF_INT;
385 1.1 bsh
386 1.1 bsh obio_intr_disestablish(bsc, irq, func);
387 1.1 bsh }
388