sa11xx_pcic.c revision 1.7 1 /* $NetBSD: sa11xx_pcic.c,v 1.7 2006/03/04 17:22:06 peter Exp $ */
2
3 /*
4 * Copyright (c) 2001 IWAMOTO Toshihiro. All rights reserved.
5 * Copyright (c) 1997 Marc Horowitz. 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 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by Marc Horowitz.
18 * 4. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * Common code for SA11x0 based PCMCIA modules
35 */
36
37 #include <sys/cdefs.h>
38 __KERNEL_RCSID(0, "$NetBSD: sa11xx_pcic.c,v 1.7 2006/03/04 17:22:06 peter Exp $");
39
40 #include <sys/types.h>
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/device.h>
44 #include <sys/callout.h>
45 #include <sys/kernel.h>
46 #include <sys/kthread.h>
47 #include <sys/malloc.h>
48 #include <uvm/uvm.h>
49
50 #include <machine/bus.h>
51 #include <machine/intr.h>
52
53 #include <dev/pcmcia/pcmciareg.h>
54 #include <dev/pcmcia/pcmciavar.h>
55 #include <dev/pcmcia/pcmciachip.h>
56
57 #include <arm/sa11x0/sa11x0_reg.h>
58 #include <arm/sa11x0/sa11x0_var.h>
59 #include <arm/sa11x0/sa11xx_pcicreg.h>
60 #include <arm/sa11x0/sa11xx_pcicvar.h>
61
62 static int sapcic_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
63 struct pcmcia_mem_handle *);
64 static void sapcic_mem_free(pcmcia_chipset_handle_t,
65 struct pcmcia_mem_handle *);
66 static int sapcic_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t,
67 bus_size_t, struct pcmcia_mem_handle *,
68 bus_addr_t *, int *);
69 static void sapcic_mem_unmap(pcmcia_chipset_handle_t, int);
70 static int sapcic_io_alloc(pcmcia_chipset_handle_t, bus_addr_t,
71 bus_size_t, bus_size_t,
72 struct pcmcia_io_handle *);
73 static void sapcic_io_free(pcmcia_chipset_handle_t,
74 struct pcmcia_io_handle *);
75 static int sapcic_io_map(pcmcia_chipset_handle_t, int,
76 bus_addr_t, bus_size_t,
77 struct pcmcia_io_handle *, int *);
78 static void sapcic_io_unmap(pcmcia_chipset_handle_t, int);
79 static void *sapcic_intr_establish(pcmcia_chipset_handle_t,
80 struct pcmcia_function *, int,
81 int (*)(void *), void *);
82 static void sapcic_intr_disestablish(pcmcia_chipset_handle_t,
83 void *);
84 static void sapcic_socket_enable(pcmcia_chipset_handle_t);
85 static void sapcic_socket_disable(pcmcia_chipset_handle_t);
86 static void sapcic_socket_settype(pcmcia_chipset_handle_t, int);
87
88 static void sapcic_event_thread(void *);
89
90 static void sapcic_delay(int, const char *);
91
92 #ifdef DEBUG
93 #define DPRINTF(arg) printf arg
94 #else
95 #define DPRINTF(arg)
96 #endif
97
98 struct pcmcia_chip_functions sa11x0_pcmcia_functions = {
99 sapcic_mem_alloc,
100 sapcic_mem_free,
101 sapcic_mem_map,
102 sapcic_mem_unmap,
103
104 sapcic_io_alloc,
105 sapcic_io_free,
106 sapcic_io_map,
107 sapcic_io_unmap,
108
109 sapcic_intr_establish,
110 sapcic_intr_disestablish,
111
112 sapcic_socket_enable,
113 sapcic_socket_disable,
114 sapcic_socket_settype,
115 };
116
117
118 void
119 sapcic_kthread_create(void *arg)
120 {
121 struct sapcic_socket *so = arg;
122
123 /* XXX attach card if already present */
124
125 so->laststatus =(so->pcictag->read)(so, SAPCIC_STATUS_CARD);
126 if (so->laststatus == SAPCIC_CARD_VALID) {
127 printf("%s: card present\n",
128 so->sc->sc_dev.dv_xname);
129
130 pcmcia_card_attach(so->pcmcia);
131 }
132
133 if (kthread_create1(sapcic_event_thread, so, &so->event_thread,
134 "%s,%d", so->sc->sc_dev.dv_xname, so->socket)) {
135 printf("%s: unable to create event thread for socket %d\n",
136 so->sc->sc_dev.dv_xname, so->socket);
137 panic("sapcic_kthread_create");
138 }
139 }
140
141 static void
142 sapcic_event_thread(void *arg)
143 {
144 struct sapcic_socket *so = arg;
145 int newstatus, s;
146
147 while (so->shutdown == 0) {
148 /*
149 * Serialize event processing on the PCIC. We may
150 * sleep while we hold this lock.
151 */
152 (void) lockmgr(&so->sc->sc_lock, LK_EXCLUSIVE, NULL);
153
154 /* sleep .25s to be enqueued chatterling interrupts */
155 (void) tsleep((caddr_t)sapcic_event_thread, PWAIT,
156 "pcicss", hz/4);
157
158 s = splhigh();
159 so->event = 0;
160
161 /* we don't rely on interrupt type */
162 newstatus = (so->pcictag->read)(so, SAPCIC_STATUS_CARD);
163 splx(s);
164
165 if (so->laststatus == newstatus) {
166 /*
167 * No events to process; release the PCIC lock.
168 */
169 (void) lockmgr(&so->sc->sc_lock, LK_RELEASE, NULL);
170 (void) tsleep(&so->event, PWAIT, "pcicev", hz);
171 continue;
172 }
173
174 so->laststatus = newstatus;
175 switch (newstatus) {
176 case SAPCIC_CARD_VALID:
177 printf("%s: insertion event\n",
178 so->sc->sc_dev.dv_xname);
179
180 pcmcia_card_attach(so->pcmcia);
181 break;
182
183 case SAPCIC_CARD_INVALID:
184 printf("%s: removal event\n",
185 so->sc->sc_dev.dv_xname);
186
187 pcmcia_card_detach(so->pcmcia, DETACH_FORCE);
188 break;
189
190 default:
191 panic("sapcic_event_thread: unknown status %d",
192 newstatus);
193 }
194
195 (void) lockmgr(&so->sc->sc_lock, LK_RELEASE, NULL);
196 }
197
198 so->event_thread = NULL;
199
200 /* In case parent is waiting for us to exit. */
201 wakeup(so->sc);
202
203 kthread_exit(0);
204 }
205
206 static void
207 sapcic_delay(int timo, const char *wmesg)
208 {
209 #ifdef DIAGNOSTIC
210 if (curlwp == NULL)
211 panic("sapcic_delay: called in interrupt context");
212 #endif
213
214 tsleep(sapcic_delay, PWAIT, wmesg, roundup(timo * hz, 1000) / 1000);
215 }
216
217 int
218 sapcic_intr(void *arg)
219 {
220 struct sapcic_socket *so = arg;
221
222 so->event++;
223 (so->pcictag->clear_intr)(so->socket);
224 wakeup(&so->event);
225 return 1;
226 }
227
228 static int
229 sapcic_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t size,
230 struct pcmcia_mem_handle *pmh)
231 {
232 struct sapcic_socket *so = pch;
233
234 /* All we need is bus space tag */
235 memset(pmh, 0, sizeof(*pmh));
236 pmh->memt = so->sc->sc_iot;
237 return (0);
238 }
239
240
241 static void
242 sapcic_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *pmh)
243 {
244 }
245
246 static int
247 sapcic_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t card_addr,
248 bus_size_t size, struct pcmcia_mem_handle *pmh, bus_addr_t *offsetp,
249 int *windowp)
250 {
251 struct sapcic_socket *so = pch;
252 int error;
253 bus_addr_t pa;
254
255 pa = trunc_page(card_addr);
256 *offsetp = card_addr - pa;
257 size = round_page(card_addr + size) - pa;
258 pmh->realsize = size;
259
260 pa += SAPCIC_BASE_OFFSET;
261 pa += SAPCIC_SOCKET_OFFSET * so->socket;
262
263 switch (kind & ~PCMCIA_WIDTH_MEM_MASK) {
264 case PCMCIA_MEM_ATTR:
265 pa += SAPCIC_ATTR_OFFSET;
266 break;
267 case PCMCIA_MEM_COMMON:
268 pa += SAPCIC_COMMON_OFFSET;
269 break;
270 default:
271 panic("sapcic_mem_map: bogus kind");
272 }
273
274 error = bus_space_map(so->sc->sc_iot, pa, size, 0, &pmh->memh);
275 if (! error)
276 *windowp = (int)pmh->memh;
277 return (error);
278 }
279
280 static void
281 sapcic_mem_unmap(pcmcia_chipset_handle_t pch, int window)
282 {
283 struct sapcic_socket *so = pch;
284
285 bus_space_unmap(so->sc->sc_iot, (bus_addr_t)window, 4096); /* XXX */
286 }
287
288 static int
289 sapcic_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start,
290 bus_size_t size, bus_size_t align, struct pcmcia_io_handle *pih)
291 {
292 struct sapcic_socket *so = pch;
293 int error;
294 bus_addr_t pa;
295
296 memset(pih, 0, sizeof(*pih));
297 pih->iot = so->sc->sc_iot;
298 pih->addr = start;
299 pih->size = size;
300
301 pa = pih->addr;
302 pa += SAPCIC_BASE_OFFSET;
303 pa += SAPCIC_SOCKET_OFFSET * so->socket;
304
305 DPRINTF(("sapcic_io_alloc: %x %x\n", (unsigned int)pa,
306 (unsigned int)size));
307 /* XXX Are we ignoring alignment constraints? */
308 error = bus_space_map(so->sc->sc_iot, pa, size, 0, &pih->ioh);
309
310 return (error);
311 }
312
313 static void
314 sapcic_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pih)
315 {
316 struct sapcic_socket *so = pch;
317
318 bus_space_unmap(so->sc->sc_iot, pih->ioh, pih->size);
319 }
320
321 static int
322 sapcic_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
323 bus_size_t size, struct pcmcia_io_handle *pih, int *windowp)
324 {
325 return (0);
326 }
327
328 static void sapcic_io_unmap(pcmcia_chipset_handle_t pch, int window)
329 {
330 }
331
332 static void *
333 sapcic_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
334 int ipl, int (*fct)(void *), void *arg)
335 {
336 struct sapcic_socket *so = pch;
337
338 /* XXX need to check if something should be done here */
339
340 return ((so->pcictag->intr_establish)(so, ipl, fct, arg));
341 }
342
343 static void
344 sapcic_intr_disestablish(pcmcia_chipset_handle_t pch, void *ih)
345 {
346 struct sapcic_socket *so = pch;
347
348 ((so->pcictag->intr_disestablish)(so, ih));
349 }
350
351 static void
352 sapcic_socket_enable(pcmcia_chipset_handle_t pch)
353 {
354 struct sapcic_socket *so = pch;
355 int i;
356
357 #if defined(DIAGNOSTIC) && defined(notyet)
358 if (so->flags & PCIC_FLAG_ENABLED)
359 printf("sapcic_socket_enable: enabling twice\n");
360 #endif
361
362 /* disable interrupts */
363
364 /* power down the socket to reset it, clear the card reset pin */
365 (so->pcictag->set_power)(so, SAPCIC_POWER_OFF);
366
367 /*
368 * wait 300ms until power fails (Tpf). Then, wait 100ms since
369 * we are changing Vcc (Toff).
370 */
371 sapcic_delay(300 + 100, "pccen0");
372
373 /* power up the socket */
374 /* XXX voltage selection should be done in PCMCIA code */
375 if (so->power_capability & SAPCIC_POWER_5V) {
376 (so->pcictag->set_power)(so, SAPCIC_POWER_5V);
377 (so->pcictag->write)(so, SAPCIC_CONTROL_POWERSELECT,
378 SAPCIC_POWER_5V);
379 } else {
380 (so->pcictag->set_power)(so, SAPCIC_POWER_3V);
381 (so->pcictag->write)(so, SAPCIC_CONTROL_POWERSELECT,
382 SAPCIC_POWER_3V);
383 }
384
385 /* enable PCMCIA control lines */
386 (so->pcictag->write)(so, SAPCIC_CONTROL_LINEENABLE, 1);
387
388 /*
389 * wait 100ms until power raise (Tpr) and 20ms to become
390 * stable (Tsu(Vcc)).
391 *
392 * some machines require some more time to be settled
393 * (300ms is added here).
394 */
395 sapcic_delay(100 + 20 + 300, "pccen1");
396
397 /* honor nWAIT signal */
398 (so->pcictag->write)(so, SAPCIC_CONTROL_WAITENABLE, 1);
399 /* now make sure we have reset# active */
400 (so->pcictag->write)(so, SAPCIC_CONTROL_RESET, 1);
401
402 /*
403 * hold RESET at least 10us, this is a min allow for slop in
404 * delay routine.
405 */
406 delay(20);
407
408 /* clear the reset flag */
409 (so->pcictag->write)(so, SAPCIC_CONTROL_RESET, 0);
410
411 /* wait 20ms as per pc card standard (r2.01) section 4.3.6 */
412 sapcic_delay(20, "pccen2");
413
414 /* wait for the chip to finish initializing */
415 sapcic_delay(10, "pccen3");
416 for(i = 100; i; i--) {
417 if ((so->pcictag->read)(so, SAPCIC_STATUS_READY))
418 break;
419 sapcic_delay(100, "pccen4");
420 }
421 DPRINTF(("sapcic_socket_enable: wait ready %d\n", 100 - i));
422
423 /* finally enable the interrupt */
424
425 }
426
427 static void
428 sapcic_socket_disable(pcmcia_chipset_handle_t pch)
429 {
430 struct sapcic_socket *so = pch;
431
432 /* XXX mask card interrupts */
433
434 /* power down the card */
435 (so->pcictag->set_power)(so, SAPCIC_POWER_OFF);
436
437 /* float controller lines */
438 (so->pcictag->write)(so, SAPCIC_CONTROL_LINEENABLE, 0);
439 }
440
441 static void
442 sapcic_socket_settype(pcmcia_chipset_handle_t pch, int type)
443 {
444
445 /* XXX nothing to do */
446 }
447