gayle_pcmcia.c revision 1.17 1 /* $NetBSD: gayle_pcmcia.c,v 1.17 2004/09/13 15:14:12 drochner Exp $ */
2
3 /* public domain */
4
5 #include <sys/cdefs.h>
6 __KERNEL_RCSID(0, "$NetBSD: gayle_pcmcia.c,v 1.17 2004/09/13 15:14:12 drochner 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 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 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 = GAYLE_PCMCIA_START;
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 - GAYLE_PCMCIA_START +
150 GAYLE_PCMCIA_RESET;
151
152 self->io_space.base = (u_int) pcmcia_base - GAYLE_PCMCIA_START +
153 GAYLE_PCMCIA_IO_START;
154 self->io_space.absm = &pcmio_bs_methods;
155
156 self->attr_space.base = (u_int) pcmcia_base - GAYLE_PCMCIA_START +
157 GAYLE_PCMCIA_ATTR_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 = (u_int) 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_sm() returned NULL\n");
187 return;
188 }
189
190 self->intr6.isr_intr = pccard_intr6;
191 self->intr6.isr_arg = self;
192 self->intr6.isr_ipl = 6;
193 add_isr(&self->intr6);
194
195 self->intr2.isr_intr = pccard_intr2;
196 self->intr2.isr_arg = self;
197 self->intr2.isr_ipl = 2;
198 add_isr(&self->intr2);
199
200 kthread_create(pccard_create_kthread, self);
201
202 gayle.intena |= GAYLE_INT_DETECT | GAYLE_INT_IREQ;
203
204 /* reset the card if it's already there */
205 if (gayle.pcc_status & GAYLE_CCMEM_DETECT) {
206 volatile u_int8_t x;
207 *reset_card_reg = 0x0;
208 delay(1000);
209 x = *reset_card_reg;
210 gayle.pcc_status = GAYLE_CCMEM_WP | GAYLE_CCIO_SPKR;
211 }
212
213 pccard_attach_slot(&self->devs[0]);
214 }
215
216 /* This is called as soon as it is possible to create a kernel thread */
217 static void
218 pccard_create_kthread(void *arg)
219 {
220 struct pccard_softc *self = arg;
221
222 if (kthread_create1(pccard_kthread, self, NULL, "pccard thread")) {
223 printf("%s: can't create kernel thread\n",
224 self->sc_dev.dv_xname);
225 panic("pccard kthread_create() failed");
226 }
227 }
228
229 static int
230 pccard_intr6(void *arg)
231 {
232 struct pccard_softc *self = arg;
233
234 if (gayle.intreq & GAYLE_INT_DETECT) {
235 gayle.intreq = GAYLE_INT_IDE | GAYLE_INT_STSCHG |
236 GAYLE_INT_SPKR | GAYLE_INT_WP | GAYLE_INT_IREQ;
237 self->devs[0].flags |= SLOT_NEW_CARD_EVENT;
238 return 1;
239 }
240 return 0;
241 }
242
243 static int
244 pccard_intr2(void *arg)
245 {
246 struct pccard_softc *self = arg;
247 struct pccard_slot *slot = &self->devs[0];
248
249 if (slot->flags & SLOT_NEW_CARD_EVENT) {
250 slot->flags &= ~SLOT_NEW_CARD_EVENT;
251
252 /* reset the registers */
253 gayle.intreq = GAYLE_INT_IDE | GAYLE_INT_DETECT;
254 gayle.pcc_status = GAYLE_CCMEM_WP | GAYLE_CCIO_SPKR;
255 gayle.pcc_config = 0;
256 pccard_attach_slot(&self->devs[0]);
257 } else {
258 int intreq = gayle.intreq &
259 (GAYLE_INT_STSCHG | GAYLE_INT_WP | GAYLE_INT_IREQ);
260 if (intreq) {
261 gayle.intreq = (intreq ^ 0x2c) | 0xc0;
262
263 return slot->flags & SLOT_OCCUPIED &&
264 slot->intr_func != NULL &&
265 slot->intr_func(slot->intr_arg);
266 }
267 }
268 return 0;
269 }
270
271 static void
272 pccard_kthread(void *arg)
273 {
274 struct pccard_softc *self = arg;
275 struct pccard_slot *slot = &self->devs[0];
276
277 for (;;) {
278 int s = spl2();
279
280 if (slot->flags & SLOT_NEW_CARD_EVENT) {
281 slot->flags &= ~SLOT_NEW_CARD_EVENT;
282 gayle.intreq = 0xc0;
283
284 /* reset the registers */
285 gayle.intreq = GAYLE_INT_IDE | GAYLE_INT_DETECT;
286 gayle.pcc_status = GAYLE_CCMEM_WP | GAYLE_CCIO_SPKR;
287 gayle.pcc_config = 0;
288 pccard_attach_slot(&self->devs[0]);
289 }
290 splx(s);
291
292 tsleep(slot, PWAIT, "pccthread", hz);
293 }
294 }
295
296 static void
297 pccard_attach_slot(struct pccard_slot *slot)
298 {
299
300 if (!(slot->flags & SLOT_OCCUPIED) &&
301 gayle.pcc_status & GAYLE_CCMEM_DETECT) {
302 if (pcmcia_card_attach(slot->card) == 0)
303 slot->flags |= SLOT_OCCUPIED;
304 }
305 }
306
307 static int
308 pcf_mem_alloc(pcmcia_chipset_handle_t pch, bus_size_t bsz,
309 struct pcmcia_mem_handle *pcmh)
310 {
311 struct pccard_slot *slot = (struct pccard_slot *) pch;
312
313 pcmh->memt = &slot->sc->attr_space;
314 pcmh->memh = pcmh->memt->base;
315 return 0;
316 }
317
318 static void
319 pcf_mem_free(pcmcia_chipset_handle_t pch, struct pcmcia_mem_handle *memh)
320 {
321 }
322
323 static int
324 pcf_mem_map(pcmcia_chipset_handle_t pch, int kind, bus_addr_t addr,
325 bus_size_t size, struct pcmcia_mem_handle *pcmh,
326 bus_addr_t *offsetp, int *windowp)
327 {
328 struct pccard_slot *slot = (struct pccard_slot *) pch;
329
330 /* Ignore width requirements */
331 kind &= ~PCMCIA_WIDTH_MEM_MASK;
332
333 switch (kind) {
334 case PCMCIA_MEM_ATTR:
335 pcmh->memt = &slot->sc->attr_space;
336 break;
337 case PCMCIA_MEM_COMMON:
338 pcmh->memt = &slot->sc->mem_space;
339 break;
340 default:
341 /* This means that this code needs an update/a bugfix */
342 printf(__FILE__ ": unknown kind %d of PCMCIA memory\n", kind);
343 return 1;
344 }
345
346 bus_space_map(pcmh->memt, addr, size, 0, &pcmh->memh);
347 *offsetp = 0;
348 *windowp = 0; /* unused */
349
350 return 0;
351 }
352
353 static void
354 pcf_mem_unmap(pcmcia_chipset_handle_t pch, int win)
355 {
356 }
357
358 static int
359 pcf_io_alloc(pcmcia_chipset_handle_t pch, bus_addr_t start, bus_size_t size,
360 bus_size_t align, struct pcmcia_io_handle *pcihp)
361 {
362 struct pccard_slot *slot = (struct pccard_slot *) pch;
363
364 pcihp->iot = &slot->sc->io_space;
365 pcihp->ioh = pcihp->iot->base;
366 return 0;
367 }
368
369 static void
370 pcf_io_free(pcmcia_chipset_handle_t pch, struct pcmcia_io_handle *pcihp)
371 {
372 }
373
374 static int
375 pcf_io_map(pcmcia_chipset_handle_t pch, int width, bus_addr_t offset,
376 bus_size_t size, struct pcmcia_io_handle *pcihp, int *windowp)
377 {
378 struct pccard_slot *slot = (struct pccard_slot *) pch;
379
380 pcihp->iot = &slot->sc->io_space;
381 bus_space_map(pcihp->iot, offset, size, 0, &pcihp->ioh);
382
383 *windowp = 0; /* unused */
384 return 0;
385 }
386
387 static void
388 pcf_io_unmap(pcmcia_chipset_handle_t pch, int win)
389 {
390 }
391
392 static void *
393 pcf_intr_establish(pcmcia_chipset_handle_t pch, struct pcmcia_function *pf,
394 int ipl, int (*func)(void *), void *arg)
395 {
396 struct pccard_slot *slot = (struct pccard_slot *) pch;
397 int s;
398
399 s = splhigh();
400 if (slot->intr_func == NULL) {
401 slot->intr_func = func;
402 slot->intr_arg = arg;
403 } else {
404 /* if we are here, we need to put intrs into a list */
405 printf("ARGH! see " __FILE__ "\n");
406 slot = NULL;
407 }
408 splx(s);
409
410 return slot;
411 }
412
413 static void
414 pcf_intr_disestablish(pcmcia_chipset_handle_t pch, void *intr_handler)
415 {
416 struct pccard_slot *slot = (struct pccard_slot *) intr_handler;
417
418 if (slot != NULL) {
419 slot->intr_func = NULL;
420 slot->intr_arg = NULL;
421 }
422 }
423
424 static void
425 pcf_socket_enable(pcmcia_chipset_handle_t pch)
426 {
427 }
428
429 static void
430 pcf_socket_disable(pcmcia_chipset_handle_t pch)
431 {
432 }
433
434 static void
435 pcf_socket_settype(pcmcia_chipset_handle_t pch, int type) {
436 }
437
438 static u_int8_t
439 pcmio_bsr1(bus_space_handle_t h, bus_size_t o)
440 {
441
442 return *((volatile u_int8_t *) h + o + (o & 1 ? 0xffff : 0));
443 }
444
445 static void
446 pcmio_bsw1(bus_space_handle_t h, bus_size_t o, unsigned v)
447 {
448
449 *((volatile u_int8_t *) h + o + (o & 1 ? 0xffff : 0)) = v;
450 }
451
452 static void
453 pcmio_bsrm1(bus_space_handle_t h, bus_size_t o, u_int8_t *p, bus_size_t c)
454 {
455 volatile u_int8_t *src = (volatile u_int8_t *)
456 (h + o + (o & 1 ? 0xffff : 0));
457
458
459 /* XXX we can (should, must) optimize this if c >= 4 */
460 for (; c > 0; c--)
461 *p++ = *src;
462 }
463
464
465 static void
466 pcmio_bswm1(bus_space_handle_t h, bus_size_t o, const u_int8_t *p, bus_size_t c)
467 {
468 volatile u_int8_t *dst = (volatile u_int8_t *)
469 (h + o + (o & 1 ? 0xffff : 0));
470
471
472 /* XXX we can (should, must) optimize this if c >= 4 */
473 for (; c > 0; c--)
474 *dst = *p++;
475 }
476
477 static void
478 pcmio_bsrr1(bus_space_handle_t h, bus_size_t o, u_int8_t *p, bus_size_t c)
479 {
480 volatile u_int8_t *cp1;
481 volatile u_int8_t *cp2;
482 volatile u_int8_t *temp;
483
484 if (o & 1) {
485 cp1 = (volatile u_int8_t *) h + o + 0x10000;
486 cp2 = (volatile u_int8_t *) h + o;
487 } else {
488 cp1 = (volatile u_int8_t *) h + o;
489 cp2 = (volatile u_int8_t *) h + o + 0x10000 + 2;
490 }
491
492 /* XXX we can (should, must) optimize this if c >= 4 */
493 for (; c > 0; c--) {
494 *p++ = *cp1;
495 cp1 += 2;
496
497 /* swap pointers - hope gcc generates exg for this ;) */
498 temp = cp1;
499 cp1 = cp2;
500 cp2 = temp;
501 }
502 }
503
504
505 static void
506 pcmio_bswr1(bus_space_handle_t h, bus_size_t o, const u_int8_t *p, bus_size_t c)
507 {
508 volatile u_int8_t *cp1;
509 volatile u_int8_t *cp2;
510 volatile u_int8_t *temp;
511
512 if (o & 1) {
513 cp1 = (volatile u_int8_t *) h + o + 0x10000;
514 cp2 = (volatile u_int8_t *) h + o;
515 } else {
516 cp1 = (volatile u_int8_t *) h + o;
517 cp2 = (volatile u_int8_t *) h + o + 0x10000 + 2;
518 }
519
520 /* XXX we can (should, must) optimize this if c >= 4 */
521 for (; c > 0; c--) {
522 *cp1 = *p++;
523 cp1 += 2;
524
525 /* swap pointers - hope gcc generates exg for this ;) */
526 temp = cp1;
527 cp1 = cp2;
528 cp2 = temp;
529 }
530 }
531
532 void
533 pcmio_bssr1(bus_space_handle_t h, bus_size_t o, unsigned v, bus_size_t c)
534 {
535
536 panic("pcmio_bssr1 is not defined (" __FILE__ ")");
537 }
538
539 void
540 pcmio_bscr1(bus_space_handle_t h, bus_size_t o, bus_space_handle_t g,
541 bus_size_t q, bus_size_t c)
542 {
543
544 panic("pcmio_bscr1 is not defined (" __FILE__ ")");
545 }
546