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