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