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