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