gayle_pcmcia.c revision 1.19 1 /* $NetBSD: gayle_pcmcia.c,v 1.19 2006/03/05 22:01:32 aymeric Exp $ */
2
3 /* public domain */
4
5 #include <sys/cdefs.h>
6 __KERNEL_RCSID(0, "$NetBSD: gayle_pcmcia.c,v 1.19 2006/03/05 22:01:32 aymeric Exp $");
7
8 /* PCMCIA front-end driver for A1200's and A600's. */
9
10 #include <sys/param.h>
11 #include <sys/device.h>
12 #include <sys/kernel.h>
13 #include <sys/kthread.h>
14 #include <sys/systm.h>
15
16 #include <uvm/uvm.h>
17
18 #include <dev/pcmcia/pcmciareg.h>
19 #include <dev/pcmcia/pcmciavar.h>
20
21 #include <machine/cpu.h>
22 #include <amiga/amiga/custom.h>
23 #include <amiga/amiga/device.h>
24 #include <amiga/amiga/gayle.h>
25 #include <amiga/amiga/isr.h>
26
27
28 /* There is one of these for each slot. And yes, there is only one slot. */
29 struct pccard_slot {
30 struct pccard_softc *sc; /* refer to `parent' */
31 int (*intr_func)(void *);
32 void * intr_arg;
33 struct device *card;
34 int flags;
35 #define SLOT_OCCUPIED 0x01
36 #define SLOT_NEW_CARD_EVENT 0x02
37 };
38
39 struct pccard_softc {
40 struct device sc_dev;
41 struct bus_space_tag io_space;
42 struct bus_space_tag attr_space;
43 struct bus_space_tag mem_space;
44 struct pccard_slot devs[1];
45 struct isr intr6;
46 struct isr intr2;
47 };
48
49 static int pccard_probe(struct device *, struct cfdata *, void *);
50 static void pccard_attach(struct device *, struct device *, void *);
51 static void pccard_attach_slot(struct pccard_slot *);
52 static int pccard_intr6(void *);
53 static int pccard_intr2(void *);
54 static void pccard_create_kthread(void *);
55 static void pccard_kthread(void *);
56
57 static int pcf_mem_alloc(pcmcia_chipset_handle_t, bus_size_t,
58 struct pcmcia_mem_handle *);
59 static void pcf_mem_free(pcmcia_chipset_handle_t, struct pcmcia_mem_handle *);
60 static int pcf_mem_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
61 struct pcmcia_mem_handle *, bus_addr_t *, int *);
62 static void pcf_mem_unmap(pcmcia_chipset_handle_t, int);
63 static int pcf_io_alloc(pcmcia_chipset_handle_t, bus_addr_t, bus_size_t,
64 bus_size_t, struct pcmcia_io_handle *);
65 static void pcf_io_free(pcmcia_chipset_handle_t, struct pcmcia_io_handle *);
66 static int pcf_io_map(pcmcia_chipset_handle_t, int, bus_addr_t, bus_size_t,
67 struct pcmcia_io_handle *, int *);
68 static void pcf_io_unmap(pcmcia_chipset_handle_t, int);
69 static void *pcf_intr_establish(pcmcia_chipset_handle_t,
70 struct pcmcia_function *, int, int (*)(void *), void *);
71 static void pcf_intr_disestablish(pcmcia_chipset_handle_t, void *);
72 static void pcf_socket_enable(pcmcia_chipset_handle_t);
73 static void pcf_socket_disable(pcmcia_chipset_handle_t);
74 static void pcf_socket_settype(pcmcia_chipset_handle_t, int);
75
76 static bsr(pcmio_bsr1, u_int8_t);
77 static bsw(pcmio_bsw1, u_int8_t);
78 static bsrm(pcmio_bsrm1, u_int8_t);
79 static bswm(pcmio_bswm1, u_int8_t);
80 static bsrm(pcmio_bsrr1, u_int8_t);
81 static bswm(pcmio_bswr1, u_int8_t);
82 static bssr(pcmio_bssr1, u_int8_t);
83 static bscr(pcmio_bscr1, u_int8_t);
84
85 CFATTACH_DECL(pccard, sizeof(struct pccard_softc),
86 pccard_probe, pccard_attach, NULL, NULL);
87
88 static struct pcmcia_chip_functions chip_functions = {
89 pcf_mem_alloc, pcf_mem_free,
90 pcf_mem_map, pcf_mem_unmap,
91 pcf_io_alloc, pcf_io_free,
92 pcf_io_map, pcf_io_unmap,
93 pcf_intr_establish, pcf_intr_disestablish,
94 pcf_socket_enable, pcf_socket_disable,
95 pcf_socket_settype
96 };
97
98 static struct amiga_bus_space_methods pcmio_bs_methods;
99
100 static u_int8_t *reset_card_reg;
101
102 static int
103 pccard_probe(struct device *dev, struct cfdata *cfd, void *aux)
104 {
105
106 return (/*is_a600() || */is_a1200()) && matchname(aux, "pccard");
107 }
108
109 static void
110 pccard_attach(struct device *parent, struct device *myself, void *aux)
111 {
112 struct pccard_softc *self = (struct pccard_softc *) myself;
113 struct pcmciabus_attach_args paa;
114 vaddr_t pcmcia_base;
115 vaddr_t i;
116 int ret;
117
118 printf("\n");
119
120 gayle_init();
121
122 ret = uvm_map(kernel_map, &pcmcia_base,
123 GAYLE_PCMCIA_END - GAYLE_PCMCIA_START, NULL,
124 UVM_UNKNOWN_OFFSET, 0,
125 UVM_MAPFLAG(UVM_PROT_NONE, UVM_PROT_NONE,
126 UVM_INH_NONE, UVM_ADV_RANDOM, 0));
127 if (ret != 0) {
128 printf("attach failed (no virtual memory)\n");
129 return;
130 }
131
132 for (i = GAYLE_PCMCIA_START; i < GAYLE_PCMCIA_END; i += PAGE_SIZE)
133 pmap_enter(kernel_map->pmap,
134 i - GAYLE_PCMCIA_START + pcmcia_base, i,
135 VM_PROT_READ | VM_PROT_WRITE, TRUE);
136 pmap_update(kernel_map->pmap);
137
138 /* override the one-byte access methods for I/O space */
139 pcmio_bs_methods = amiga_bus_stride_1;
140 pcmio_bs_methods.bsr1 = pcmio_bsr1;
141 pcmio_bs_methods.bsw1 = pcmio_bsw1;
142 pcmio_bs_methods.bsrm1 = pcmio_bsrm1;
143 pcmio_bs_methods.bswm1 = pcmio_bswm1;
144 pcmio_bs_methods.bsrr1 = pcmio_bsrr1;
145 pcmio_bs_methods.bswr1 = pcmio_bswr1;
146 pcmio_bs_methods.bssr1 = pcmio_bssr1;
147 pcmio_bs_methods.bscr1 = pcmio_bscr1;
148
149 reset_card_reg = (u_int8_t *) pcmcia_base +
150 (GAYLE_PCMCIA_RESET - GAYLE_PCMCIA_START);
151
152 self->io_space.base = (bus_addr_t) pcmcia_base +
153 (GAYLE_PCMCIA_IO_START - GAYLE_PCMCIA_START);
154 self->io_space.absm = &pcmio_bs_methods;
155
156 self->attr_space.base = (bus_addr_t) pcmcia_base +
157 (GAYLE_PCMCIA_ATTR_START - GAYLE_PCMCIA_START);
158 self->attr_space.absm = &amiga_bus_stride_1;
159
160 /* XXX we should check if the 4M of common memory are actually
161 * RAM or PCMCIA usable.
162 * For now, we just do as if the 4M were RAM and make common memory
163 * point to attribute memory, which is OK for some I/O cards.
164 */
165 self->mem_space.base = (bus_addr_t) pcmcia_base;
166 self->mem_space.absm = &amiga_bus_stride_1;
167
168 self->devs[0].sc = self;
169 self->devs[0].intr_func = NULL;
170 self->devs[0].intr_arg = NULL;
171 self->devs[0].flags = 0;
172
173 gayle.pcc_status = 0;
174 gayle.intreq = 0;
175 gayle.pcc_config = 0;
176 gayle.intena &= GAYLE_INT_IDE;
177
178 paa.paa_busname = "pcmcia";
179 paa.pct = &chip_functions;
180 paa.pch = &self->devs[0];
181 paa.iobase = 0;
182 paa.iosize = 0;
183 self->devs[0].card =
184 config_found(myself, &paa, simple_devprint);
185 if (self->devs[0].card == NULL) {
186 printf("attach failed, config_found() returned NULL\n");
187 pmap_remove(kernel_map->pmap, pcmcia_base,
188 pcmcia_base + (GAYLE_PCMCIA_END - GAYLE_PCMCIA_START));
189 pmap_update(kernel_map->pmap);
190 uvm_deallocate(kernel_map, pcmcia_base,
191 GAYLE_PCMCIA_END - GAYLE_PCMCIA_START);
192 return;
193 }
194
195 self->intr6.isr_intr = pccard_intr6;
196 self->intr6.isr_arg = self;
197 self->intr6.isr_ipl = 6;
198 add_isr(&self->intr6);
199
200 self->intr2.isr_intr = pccard_intr2;
201 self->intr2.isr_arg = self;
202 self->intr2.isr_ipl = 2;
203 add_isr(&self->intr2);
204
205 kthread_create(pccard_create_kthread, self);
206
207 gayle.intena |= GAYLE_INT_DETECT | GAYLE_INT_IREQ;
208
209 /* reset the card if it's already there */
210 if (gayle.pcc_status & GAYLE_CCMEM_DETECT) {
211 volatile u_int8_t x;
212 *reset_card_reg = 0x0;
213 delay(1000);
214 x = *reset_card_reg;
215 gayle.pcc_status = GAYLE_CCMEM_WP | GAYLE_CCIO_SPKR;
216 }
217
218 pccard_attach_slot(&self->devs[0]);
219 }
220
221 /* This is called as soon as it is possible to create a kernel thread */
222 static void
223 pccard_create_kthread(void *arg)
224 {
225 struct pccard_softc *self = arg;
226
227 if (kthread_create1(pccard_kthread, self, NULL, "pccard thread")) {
228 printf("%s: can't create kernel thread\n",
229 self->sc_dev.dv_xname);
230 panic("pccard kthread_create() failed");
231 }
232 }
233
234 static int
235 pccard_intr6(void *arg)
236 {
237 struct pccard_softc *self = arg;
238
239 if (gayle.intreq & GAYLE_INT_DETECT) {
240 gayle.intreq = GAYLE_INT_IDE | GAYLE_INT_STSCHG |
241 GAYLE_INT_SPKR | GAYLE_INT_WP | GAYLE_INT_IREQ;
242 self->devs[0].flags |= SLOT_NEW_CARD_EVENT;
243 return 1;
244 }
245 return 0;
246 }
247
248 static int
249 pccard_intr2(void *arg)
250 {
251 struct pccard_softc *self = arg;
252 struct pccard_slot *slot = &self->devs[0];
253
254 if (slot->flags & SLOT_NEW_CARD_EVENT) {
255 slot->flags &= ~SLOT_NEW_CARD_EVENT;
256
257 /* reset the registers */
258 gayle.intreq = GAYLE_INT_IDE | GAYLE_INT_DETECT;
259 gayle.pcc_status = GAYLE_CCMEM_WP | GAYLE_CCIO_SPKR;
260 gayle.pcc_config = 0;
261 pccard_attach_slot(&self->devs[0]);
262 } else {
263 int intreq = gayle.intreq &
264 (GAYLE_INT_STSCHG | GAYLE_INT_WP | GAYLE_INT_IREQ);
265 if (intreq) {
266 gayle.intreq = (intreq ^ 0x2c) | 0xc0;
267
268 return slot->flags & SLOT_OCCUPIED &&
269 slot->intr_func != NULL &&
270 slot->intr_func(slot->intr_arg);
271 }
272 }
273 return 0;
274 }
275
276 static void
277 pccard_kthread(void *arg)
278 {
279 struct pccard_softc *self = arg;
280 struct pccard_slot *slot = &self->devs[0];
281
282 for (;;) {
283 int s = spl2();
284
285 if (slot->flags & SLOT_NEW_CARD_EVENT) {
286 slot->flags &= ~SLOT_NEW_CARD_EVENT;
287 gayle.intreq = 0xc0;
288
289 /* reset the registers */
290 gayle.intreq = GAYLE_INT_IDE | GAYLE_INT_DETECT;
291 gayle.pcc_status = GAYLE_CCMEM_WP | GAYLE_CCIO_SPKR;
292 gayle.pcc_config = 0;
293 pccard_attach_slot(&self->devs[0]);
294 }
295 splx(s);
296
297 tsleep(slot, PWAIT, "pccthread", hz);
298 }
299 }
300
301 static void
302 pccard_attach_slot(struct pccard_slot *slot)
303 {
304
305 if (!(slot->flags & SLOT_OCCUPIED) &&
306 gayle.pcc_status & GAYLE_CCMEM_DETECT) {
307 if (pcmcia_card_attach(slot->card) == 0)
308 slot->flags |= SLOT_OCCUPIED;
309 }
310 }
311
312 static int
313 pcf_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t bsz,
314 struct pcmcia_mem_handle *pcmh)
315 {
316 struct pccard_slot *slot = (struct pccard_slot *) pch;
317
318 pcmh->memt = &slot->sc->attr_space;
319 pcmh->memh = pcmh->memt->base;
320 return 0;
321 }
322
323 static void
324 pcf_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *memh)
325 {
326 }
327
328 static int
329 pcf_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
330 bus_size_t size, struct pcmcia_mem_handle *pcmh,
331 bus_addr_t *offsetp, int *windowp)
332 {
333 struct pccard_slot *slot = (struct pccard_slot *) pch;
334
335 /* Ignore width requirements */
336 kind &= ~PCMCIA_WIDTH_MEM_MASK;
337
338 switch (kind) {
339 case PCMCIA_MEM_ATTR:
340 pcmh->memt = &slot->sc->attr_space;
341 break;
342 case PCMCIA_MEM_COMMON:
343 pcmh->memt = &slot->sc->mem_space;
344 break;
345 default:
346 /* This means that this code needs an update/a bugfix */
347 printf(__FILE__ ": unknown kind %d of PCMCIA memory\n", kind);
348 return 1;
349 }
350
351 bus_space_map(pcmh->memt, addr, size, 0, &pcmh->memh);
352 *offsetp = 0;
353 *windowp = 0; /* unused */
354
355 return 0;
356 }
357
358 static void
359 pcf_mem_unmap(pcmcia_chipset_handle_t pch, int win)
360 {
361 }
362
363 static int
364 pcf_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
365 bus_size_t align, struct pcmcia_io_handle *pcihp)
366 {
367 struct pccard_slot *slot = (struct pccard_slot *) pch;
368
369 pcihp->iot = &slot->sc->io_space;
370 pcihp->ioh = pcihp->iot->base;
371 return 0;
372 }
373
374 static void
375 pcf_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pcihp)
376 {
377 }
378
379 static int
380 pcf_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
381 bus_size_t size, struct pcmcia_io_handle *pcihp, int *windowp)
382 {
383 struct pccard_slot *slot = (struct pccard_slot *) pch;
384
385 pcihp->iot = &slot->sc->io_space;
386 bus_space_map(pcihp->iot, offset, size, 0, &pcihp->ioh);
387
388 *windowp = 0; /* unused */
389 return 0;
390 }
391
392 static void
393 pcf_io_unmap(pcmcia_chipset_handle_t pch, int win)
394 {
395 }
396
397 static void *
398 pcf_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
399 int ipl, int (*func)(void *), void *arg)
400 {
401 struct pccard_slot *slot = (struct pccard_slot *) pch;
402 int s;
403
404 s = splhigh();
405 if (slot->intr_func == NULL) {
406 slot->intr_func = func;
407 slot->intr_arg = arg;
408 } else {
409 /* if we are here, we need to put intrs into a list */
410 printf("ARGH! see " __FILE__ "\n");
411 slot = NULL;
412 }
413 splx(s);
414
415 return slot;
416 }
417
418 static void
419 pcf_intr_disestablish(pcmcia_chipset_handle_t pch, void *intr_handler)
420 {
421 struct pccard_slot *slot = (struct pccard_slot *) intr_handler;
422
423 if (slot != NULL) {
424 slot->intr_func = NULL;
425 slot->intr_arg = NULL;
426 }
427 }
428
429 static void
430 pcf_socket_enable(pcmcia_chipset_handle_t pch)
431 {
432 }
433
434 static void
435 pcf_socket_disable(pcmcia_chipset_handle_t pch)
436 {
437 }
438
439 static void
440 pcf_socket_settype(pcmcia_chipset_handle_t pch, int type) {
441 }
442
443 static u_int8_t
444 pcmio_bsr1(bus_space_handle_t h, bus_size_t o)
445 {
446
447 return *((volatile u_int8_t *) h + o + (o & 1 ? 0xffff : 0));
448 }
449
450 static void
451 pcmio_bsw1(bus_space_handle_t h, bus_size_t o, unsigned v)
452 {
453
454 *((volatile u_int8_t *) h + o + (o & 1 ? 0xffff : 0)) = v;
455 }
456
457 static void
458 pcmio_bsrm1(bus_space_handle_t h, bus_size_t o, u_int8_t *p, bus_size_t c)
459 {
460 volatile u_int8_t *src = (volatile u_int8_t *)
461 (h + o + (o & 1 ? 0xffff : 0));
462
463
464 /* XXX we can (should, must) optimize this if c >= 4 */
465 for (; c > 0; c--)
466 *p++ = *src;
467 }
468
469
470 static void
471 pcmio_bswm1(bus_space_handle_t h, bus_size_t o, const u_int8_t *p, bus_size_t c)
472 {
473 volatile u_int8_t *dst = (volatile u_int8_t *)
474 (h + o + (o & 1 ? 0xffff : 0));
475
476
477 /* XXX we can (should, must) optimize this if c >= 4 */
478 for (; c > 0; c--)
479 *dst = *p++;
480 }
481
482 static void
483 pcmio_bsrr1(bus_space_handle_t h, bus_size_t o, u_int8_t *p, bus_size_t c)
484 {
485 volatile u_int8_t *cp1;
486 volatile u_int8_t *cp2;
487 volatile u_int8_t *temp;
488
489 if (o & 1) {
490 cp1 = (volatile u_int8_t *) h + o + 0x10000;
491 cp2 = (volatile u_int8_t *) h + o;
492 } else {
493 cp1 = (volatile u_int8_t *) h + o;
494 cp2 = (volatile u_int8_t *) h + o + 0x10000 + 2;
495 }
496
497 /* XXX we can (should, must) optimize this if c >= 4 */
498 for (; c > 0; c--) {
499 *p++ = *cp1;
500 cp1 += 2;
501
502 /* swap pointers - hope gcc generates exg for this ;) */
503 temp = cp1;
504 cp1 = cp2;
505 cp2 = temp;
506 }
507 }
508
509
510 static void
511 pcmio_bswr1(bus_space_handle_t h, bus_size_t o, const u_int8_t *p, bus_size_t c)
512 {
513 volatile u_int8_t *cp1;
514 volatile u_int8_t *cp2;
515 volatile u_int8_t *temp;
516
517 if (o & 1) {
518 cp1 = (volatile u_int8_t *) h + o + 0x10000;
519 cp2 = (volatile u_int8_t *) h + o;
520 } else {
521 cp1 = (volatile u_int8_t *) h + o;
522 cp2 = (volatile u_int8_t *) h + o + 0x10000 + 2;
523 }
524
525 /* XXX we can (should, must) optimize this if c >= 4 */
526 for (; c > 0; c--) {
527 *cp1 = *p++;
528 cp1 += 2;
529
530 /* swap pointers - hope gcc generates exg for this ;) */
531 temp = cp1;
532 cp1 = cp2;
533 cp2 = temp;
534 }
535 }
536
537 void
538 pcmio_bssr1(bus_space_handle_t h, bus_size_t o, unsigned v, bus_size_t c)
539 {
540
541 panic("pcmio_bssr1 is not defined (" __FILE__ ")");
542 }
543
544 void
545 pcmio_bscr1(bus_space_handle_t h, bus_size_t o, bus_space_handle_t g,
546 bus_size_t q, bus_size_t c)
547 {
548
549 panic("pcmio_bscr1 is not defined (" __FILE__ ")");
550 }
551