2 cardbus and pcmcia (grmph) support.
5 #include "../port/lib.h"
9 #include "../port/error.h"
14 #pragma varargck type "T" int
16 #define MAP(x,o) (Rmap + (x)*0x8 + o)
23 TI_1251A_did = 0xAC1D,
27 Ricoh_475_did = 0x0475,
28 Ricoh_476_did = 0x0476,
29 Ricoh_478_did = 0x0478,
32 O2_OZ711M3_did = 0x7134,
34 Nslots = 4, /* Maximum number of CardBus slots to use */
42 TI1131xSC = 0x80, /* system control */
43 TI122X_SC_INTRTIE = 1 << 29,
44 TI12xxIM = 0x8c, /* */
45 TI1131xCC = 0x91, /* card control */
46 TI113X_CC_RIENB = 1 << 7,
47 TI113X_CC_ZVENABLE = 1 << 6,
48 TI113X_CC_PCI_IRQ_ENA = 1 << 5,
49 TI113X_CC_PCI_IREQ = 1 << 4,
50 TI113X_CC_PCI_CSC = 1 << 3,
51 TI113X_CC_SPKROUTEN = 1 << 1,
52 TI113X_CC_IFG = 1 << 0,
53 TI1131xDC = 0x92, /* device control */
56 typedef struct Variant Variant;
63 static Variant variant[] = {
64 { Ricoh_vid, Ricoh_475_did, "Ricoh 475 PCI/Cardbus bridge", },
65 { Ricoh_vid, Ricoh_476_did, "Ricoh 476 PCI/Cardbus bridge", },
66 { Ricoh_vid, Ricoh_478_did, "Ricoh 478 PCI/Cardbus bridge", },
67 { TI_vid, TI_1131_did, "TI PCI-1131 Cardbus Controller", },
68 { TI_vid, TI_1250_did, "TI PCI-1250 Cardbus Controller", },
69 { TI_vid, TI_1450_did, "TI PCI-1450 Cardbus Controller", },
70 { TI_vid, TI_1251A_did, "TI PCI-1251A Cardbus Controller", },
71 { TI_vid, TI_1420_did, "TI PCI-1420 Cardbus Controller", },
72 { O2_vid, O2_OZ711M3_did, "O2Micro OZ711M3 MemoryCardBus", },
75 /* Cardbus registers */
99 PciPCR_Master = 1 << 2,
119 * Intel 82365SL PCIC controller for the PCMCIA or
120 * Cirrus Logic PD6710/PD6720 which is mostly register compatible
127 Rid= 0x0, /* identification and revision */
128 Ris= 0x1, /* interface status */
129 Rpc= 0x2, /* power control */
130 Foutena= (1<<7), /* output enable */
131 Fautopower= (1<<5), /* automatic power switching */
132 Fcardena= (1<<4), /* PC card enable */
133 Rigc= 0x3, /* interrupt and general control */
134 Fiocard= (1<<5), /* I/O card (vs memory) */
135 Fnotreset= (1<<6), /* reset if not set */
136 FSMIena= (1<<4), /* enable change interrupt on SMI */
137 Rcsc= 0x4, /* card status change */
138 Rcscic= 0x5, /* card status change interrupt config */
139 Fchangeena= (1<<3), /* card changed */
140 Fbwarnena= (1<<1), /* card battery warning */
141 Fbdeadena= (1<<0), /* card battery dead */
142 Rwe= 0x6, /* address window enable */
143 Fmem16= (1<<5), /* use A23-A12 to decode address */
144 Rio= 0x7, /* I/O control */
145 Fwidth16= (1<<0), /* 16 bit data width */
146 Fiocs16= (1<<1), /* IOCS16 determines data width */
147 Fzerows= (1<<2), /* zero wait state */
148 Ftiming= (1<<3), /* timing register to use */
149 Riobtm0lo= 0x8, /* I/O address 0 start low byte */
150 Riobtm0hi= 0x9, /* I/O address 0 start high byte */
151 Riotop0lo= 0xa, /* I/O address 0 stop low byte */
152 Riotop0hi= 0xb, /* I/O address 0 stop high byte */
153 Riobtm1lo= 0xc, /* I/O address 1 start low byte */
154 Riobtm1hi= 0xd, /* I/O address 1 start high byte */
155 Riotop1lo= 0xe, /* I/O address 1 stop low byte */
156 Riotop1hi= 0xf, /* I/O address 1 stop high byte */
157 Rmap= 0x10, /* map 0 */
160 * CL-PD67xx extension registers
162 Rmisc1= 0x16, /* misc control 1 */
169 Rfifo= 0x17, /* fifo control */
170 Fflush= (1<<7), /* flush fifo */
171 Rmisc2= 0x1E, /* misc control 2 */
172 Flowpow= (1<<1), /* low power mode */
173 Rchipinfo= 0x1F, /* chip information */
174 Ratactl= 0x26, /* ATA control */
177 * offsets into the system memory address maps
179 Mbtmlo= 0x0, /* System mem addr mapping start low byte */
180 Mbtmhi= 0x1, /* System mem addr mapping start high byte */
181 F16bit= (1<<7), /* 16-bit wide data path */
182 Mtoplo= 0x2, /* System mem addr mapping stop low byte */
183 Mtophi= 0x3, /* System mem addr mapping stop high byte */
184 Ftimer1= (1<<6), /* timer set 1 */
185 Mofflo= 0x4, /* Card memory offset address low byte */
186 Moffhi= 0x5, /* Card memory offset address high byte */
187 Fregactive= (1<<6), /* attribute memory */
190 * configuration registers - they start at an offset in attribute
191 * memory found in the CIS.
194 Creset= (1<<7), /* reset device */
195 Clevel= (1<<6), /* level sensitive interrupt line */
199 * read and crack the card information structure enough to set
200 * important parameters like power
202 /* cis memory walking */
203 typedef struct Cisdat Cisdat;
211 typedef struct Pcminfo Pcminfo;
213 char verstr[512]; /* Version string */
214 PCMmap mmap[4]; /* maps, last is always for the kernel */
215 ulong conf_addr; /* Config address */
216 uchar conf_present; /* Config register present */
217 int nctab; /* In use configuration tables */
218 PCMconftab ctab[8]; /* Configuration tables */
219 PCMconftab *defctab; /* Default conftab */
221 int port; /* Actual port usage */
222 int irq; /* Actual IRQ usage */
225 typedef struct Cardbus Cardbus;
228 Variant *variant; /* Which CardBus chipset */
229 Pcidev *pci; /* The bridge itself */
230 ulong *regs; /* Cardbus registers */
231 int ltype; /* Legacy type */
232 int lindex; /* Legacy port index address */
233 int ldata; /* Legacy port data address */
234 int lbase; /* Base register for this socket */
236 int state; /* Current state of card */
237 int type; /* Type of card */
238 Pcminfo linfo; /* PCMCIA slot info */
240 int special; /* card is allocated to a driver */
242 int refs; /* Number of refs to slot */
243 Lock refslock; /* inc/dev ref lock */
246 static int managerstarted;
250 Mgran= (1<<Mshift), /* granularity of maps */
251 Mmask= ~(Mgran-1), /* mask for address bits important to the chip */
254 static Cardbus cbslots[Nslots];
257 static ulong exponent[8] = {
258 1, 10, 100, 1000, 10000, 100000, 1000000, 10000000,
261 static ulong vmant[16] = {
262 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80, 90,
265 static ulong mantissa[16] = {
266 0, 10, 12, 13, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 70, 80,
269 static char Enocard[] = "No card in slot";
277 static Cmdtab pccardctlmsg[] =
283 static int powerup(Cardbus *);
284 static void configure(Cardbus *);
285 static void powerdown(Cardbus *cb);
286 static void unconfigure(Cardbus *cb);
288 static void i82365probe(Cardbus *cb, int lindex, int ldata);
289 static void i82365configure(Cardbus *cb);
290 static PCMmap *isamap(Cardbus *cb, ulong offset, int len, int attr);
291 static void isaunmap(PCMmap* m);
292 static uchar rdreg(Cardbus *cb, int index);
293 static void wrreg(Cardbus *cb, int index, uchar val);
294 static int readc(Cisdat *cis, uchar *x);
295 static void tvers1(Cardbus *cb, Cisdat *cis, int );
296 static void tcfig(Cardbus *cb, Cisdat *cis, int );
297 static void tentry(Cardbus *cb, Cisdat *cis, int );
298 static int vcode(int volt);
299 static int pccard_pcmspecial(char *idstr, ISAConf *isa);
300 static void pccard_pcmspecialclose(int slotno);
309 static char *messages[] = {
310 [CardDetected] "CardDetected",
311 [CardPowered] "CardPowered",
312 [CardEjected] "CardEjected",
313 [CardConfigured] "CardConfigured",
323 static char *states[] = {
324 [SlotEmpty] "SlotEmpty",
325 [SlotFull] "SlotFull",
326 [SlotPowered] "SlotPowered",
327 [SlotConfigured] "SlotConfigured",
331 engine(Cardbus *cb, int message)
334 print("engine(%ld): %s(%s)\n", cb - cbslots,
335 states[cb->state], messages[message]);
341 cb->state = SlotFull;
348 print("#Y%ld: Invalid message %s in SlotEmpty state\n",
349 cb - cbslots, messages[message]);
358 cb->state = SlotPowered;
362 cb->state = SlotEmpty;
367 print("#Y%ld: Invalid message %s in SlotFull state\n",
368 cb - cbslots, messages[message]);
377 cb->state = SlotConfigured;
380 cb->state = SlotEmpty;
385 print("#Y%ld: Invalid message %s in SlotPowered state\n",
386 cb - cbslots, messages[message]);
395 cb->state = SlotEmpty;
401 print("#Y%ld: Invalid message %s in SlotConfigured state\n",
402 cb - cbslots, messages[message]);
410 qengine(Cardbus *cb, int message)
417 typedef struct Events Events;
424 static Events events[NUMEVENTS];
425 static Rendez revents;
429 iengine(Cardbus *cb, int message)
431 if (nevents >= NUMEVENTS) {
432 print("#Y: Too many events queued, discarding request\n");
436 events[nevents].cb = cb;
437 events[nevents].message = message;
450 processevents(void *)
456 sleep(&revents, (int (*)(void *))eventoccured, nil);
463 message = events[0].message;
466 memmove(events, &events[1], nevents * sizeof(Events));
471 qengine(cb, message);
476 cbinterrupt(Ureg *, void *)
480 for (i = 0; i != nslots; i++) {
481 Cardbus *cb = &cbslots[i];
484 event = cb->regs[SocketEvent];
485 if(!(event & (SE_POWER|SE_CCD)))
487 state = cb->regs[SocketState];
488 rdreg(cb, Rcsc); /* Ack the interrupt */
491 print("#Y%ld: interrupt: event %.8lX, state %.8lX, (%s)\n",
492 cb - cbslots, event, state, states[cb->state]);
494 if (event & SE_CCD) {
495 cb->regs[SocketEvent] |= SE_CCD; /* Ack interrupt */
496 if (state & SE_CCD) {
497 if (cb->state != SlotEmpty) {
498 print("#Y: take cardejected interrupt\n");
499 iengine(cb, CardEjected);
503 iengine(cb, CardDetected);
506 if (event & SE_POWER) {
507 cb->regs[SocketEvent] |= SE_POWER; /* Ack interrupt */
508 iengine(cb, CardPowered);
516 static int initialized;
527 if((p=getconf("pccard0")) && strncmp(p, "disabled", 8)==0)
533 /* Allocate legacy space */
534 if (ioalloc(LegacyAddr, 2, 0, "i82365.0") < 0)
535 print("#Y: WARNING: Cannot allocate legacy ports\n");
537 /* Find all CardBus controllers */
540 while ((pci = pcimatch(pci, 0, 0)) != nil) {
546 if(pci->ccrb != 6 || pci->ccru != 7)
548 for (i = 0; i != nelem(variant); i++)
549 if (pci->vid == variant[i].vid && pci->did == variant[i].did)
551 if (i == nelem(variant))
554 /* initialize this slot */
559 cb->variant = &variant[i];
561 if (pci->vid != TI_vid) {
563 * Gross hack, needs a fix. Inherit the mappings from
564 * 9load for the TIs (pb)
566 pcicfgw32(pci, PciCBMBR0, 0xffffffff);
567 pcicfgw32(pci, PciCBMLR0, 0);
568 pcicfgw32(pci, PciCBMBR1, 0xffffffff);
569 pcicfgw32(pci, PciCBMLR1, 0);
570 pcicfgw32(pci, PciCBIBR0, 0xffffffff);
571 pcicfgw32(pci, PciCBILR0, 0);
572 pcicfgw32(pci, PciCBIBR1, 0xffffffff);
573 pcicfgw32(pci, PciCBILR1, 0);
576 /* Set up PCI bus numbers if needed. */
577 if (pcicfgr8(pci, PciSBN) == 0) {
578 static int busbase = 0x20;
580 pcicfgw8(pci, PciSBN, busbase);
581 pcicfgw8(pci, PciUBN, busbase + 2);
585 /* Patch up intl if needed. */
586 if ((pin = pcicfgr8(pci, PciINTP)) != 0 &&
587 (pci->intl == 0xff || pci->intl == 0)) {
588 pci->intl = pciipin(nil, pin);
589 pcicfgw8(pci, PciINTL, pci->intl);
591 if (pci->intl == 0xff || pci->intl == 0)
592 print("#Y%ld: No interrupt?\n", cb - cbslots);
595 /* Don't you love standards! */
596 if (pci->vid == TI_vid) {
597 if (pci->did <= TI_1131_did) {
600 cc = pcicfgr8(pci, TI1131xCC);
601 cc &= ~(TI113X_CC_PCI_IRQ_ENA |
605 cc |= TI113X_CC_PCI_IRQ_ENA |
608 pcicfgw8(pci, TI1131xCC, cc);
610 /* PCI interrupts only */
611 pcicfgw8(pci, TI1131xDC,
612 pcicfgr8(pci, TI1131xDC) & ~6);
614 /* CSC ints to PCI bus. */
615 wrreg(cb, Rigc, rdreg(cb, Rigc) | 0x10);
617 else if (pci->did == TI_1250_did) {
618 print("No support yet for the TI_1250_did, prod pb\n");
620 else if (pci->did == TI_1420_did) {
621 /* Disable Vcc protection */
622 pcicfgw32(cb->pci, 0x80,
623 pcicfgr32(cb->pci, 0x80) | (1 << 21));
626 pcicfgw16(cb->pci, PciPMC, pcicfgr16(cb->pci, PciPMC) & ~3);
628 if (pci->vid == O2_vid) {
630 print("writing O2 config\n");
631 pcicfgw8(cb->pci, 0x94, 0xCA);
632 pcicfgw8(cb->pci, 0xD4, 0xCA);
635 if (intl != 0xff && intl != pci->intl)
636 intrenable(pci->intl, cbinterrupt, cb, pci->tbdf, "cardbus");
639 if ((baddr = pcicfgr32(cb->pci, PciBAR0)) == 0) {
640 int size = (pci->did == Ricoh_478_did)? 0x10000: 0x1000;
642 baddr = upaalloc(size, size);
643 baddrva = vmap(baddr, size);
644 pcicfgw32(cb->pci, PciBAR0, baddr);
645 cb->regs = (ulong *)baddrva;
648 cb->regs = (ulong *)vmap(baddr, 4096);
649 cb->state = SlotEmpty;
651 /* Don't really know what to do with this... */
652 i82365probe(cb, LegacyAddr, LegacyAddr + 1);
654 print("#Y%ld: %s, %.8ulX intl %d\n", cb - cbslots,
655 variant[i].name, baddr, pci->intl);
663 _pcmspecial = pccard_pcmspecial;
664 _pcmspecialclose = pccard_pcmspecialclose;
666 for (i = 0; i != nslots; i++) {
667 Cardbus *cb = &cbslots[i];
669 if ((cb->regs[SocketState] & SE_CCD) == 0)
670 engine(cb, CardDetected);
673 delay(500); /* Allow time for power up */
675 for (i = 0; i != nslots; i++) {
676 Cardbus *cb = &cbslots[i];
678 if (cb->regs[SocketState] & SE_POWER)
679 engine(cb, CardPowered);
681 /* Ack and enable interrupts on all events */
682 // cb->regs[SocketEvent] = cb->regs[SocketEvent];
683 cb->regs[SocketMask] |= 0xF;
684 wrreg(cb, Rcscic, 0xC);
694 state = cb->regs[SocketState];
695 if (state & SS_PC16) {
697 print("#Y%ld: Probed a PC16 card, powering up card\n",
700 memset(&cb->linfo, 0, sizeof(Pcminfo));
702 /* power up and unreset, wait's are empirical (???) */
703 wrreg(cb, Rpc, Fautopower|Foutena|Fcardena);
707 wrreg(cb, Rigc, Fnotreset);
716 if (state & SS_NOTCARD) {
717 print("#Y%ld: No card inserted\n", cb - cbslots);
721 if ((state & SS_3V) == 0 && (state & SS_5V) == 0) {
722 print("#Y%ld: Unsupported voltage, powering down card!\n",
724 cb->regs[SocketControl] = 0;
729 print("#Y%ld: card %spowered at %d volt\n", cb - cbslots,
730 (state & SS_POWER)? "": "not ",
731 (state & SS_3V)? 3: (state & SS_5V)? 5: -1);
734 * and make sure the secondary bus is not in reset.
736 cb->regs[SocketControl] = (state & SS_5V)? SC_5V: SC_3V;
738 bcr = pcicfgr16(cb->pci, PciBCR);
740 pcicfgw16(cb->pci, PciBCR, bcr);
752 powerdown(Cardbus *cb)
756 if (cb->type == PC16) {
758 wrreg(cb, Rpc, 0); /* turn off card power */
759 wrreg(cb, Rwe, 0); /* no windows */
765 bcr = pcicfgr16(cb->pci, PciBCR);
767 pcicfgw16(cb->pci, PciBCR, bcr);
768 cb->regs[SocketControl] = 0;
773 configure(Cardbus *cb)
778 ulong membase, iobase, memlen, iolen, rombase, romlen;
781 print("configuring slot %ld (%s)\n", cb - cbslots, states[cb->state]);
782 if (cb->state == SlotConfigured)
784 engine(cb, CardConfigured);
786 delay(50); /* Emperically established */
788 if (cb->type == PC16) {
793 /* Scan the CardBus for new PCI devices */
794 pciscan(pcicfgr8(cb->pci, PciSBN), &cb->pci->bridge);
797 * size the devices on the bus, reserve a minimum for devices arriving later,
798 * allow for ROM space, allocate space, and set the cardbus mapping registers
800 pcibussize(cb->pci->bridge, &memlen, &iolen); /* TO DO: need initial alignments */
803 for(pci = cb->pci->bridge; pci != nil; pci = pci->list){
804 size = pcibarsize(pci, PciEBAR0);
807 pci->rom.size = size;
814 iobase = ioreserve(~0, iolen, 0, "cardbus");
815 pcicfgw32(cb->pci, PciCBIBR0, iobase);
816 pcicfgw32(cb->pci, PciCBILR0, iobase + iolen-1);
817 pcicfgw32(cb->pci, PciCBIBR1, 0);
818 pcicfgw32(cb->pci, PciCBILR1, 0);
822 if(memlen < 1*1024*1024)
823 memlen = 1*1024*1024;
824 membase = upaalloc(memlen, 4*1024*1024); /* TO DO: better alignment */
825 pcicfgw32(cb->pci, PciCBMBR0, membase);
826 pcicfgw32(cb->pci, PciCBMLR0, membase + memlen-1);
827 pcicfgw32(cb->pci, PciCBMBR1, 0);
828 pcicfgw32(cb->pci, PciCBMLR1, 0);
830 // pcibussize(cb->pci->bridge, &membase, &iobase); /* now assign them */
833 for(pci = cb->pci->bridge; pci != nil; pci = pci->list){
834 r = pcicfgr16(pci, PciPCR);
835 r &= ~(PciPCR_IO|PciPCR_MEM);
836 pcicfgw16(pci, PciPCR, r);
839 * Treat the found device as an ordinary PCI card.
840 * It seems that the CIS is not always present in
842 * XXX, need to support multifunction cards
844 for(i = 0; i < Nbars; i++) {
845 if(pci->mem[i].size == 0)
847 bar = pci->mem[i].bar;
852 pci->mem[i].bar = bar;
853 pcicfgw32(pci, PciBAR0 + 4*i, bar);
855 print("%T mem[%d] %8.8lux %d\n", pci->tbdf, i, bar, pci->mem[i].size);
856 if(bar & 0x80){ /* TO DO: enable prefetch */
861 if((size = pcibarsize(pci, PciEBAR0)) > 0) { /* TO DO: can this be done by pci.c? */
862 pci->rom.bar = rombase;
863 pci->rom.size = size;
865 pcicfgw32(pci, PciEBAR0, pci->rom.bar);
868 /* Set the basic PCI registers for the device */
869 pci->pcr = pcicfgr16(pci, PciPCR);
870 pci->pcr |= PciPCR_IO|PciPCR_MEM|PciPCR_Master;
873 pcicfgw16(pci, PciPCR, pci->pcr);
874 pcicfgw8(pci, PciCLS, pci->cls);
875 pcicfgw8(pci, PciLTR, pci->ltr);
877 if (pcicfgr8(pci, PciINTP)) {
878 pci->intl = pcicfgr8(cb->pci, PciINTL);
879 pcicfgw8(pci, PciINTL, pci->intl);
881 /* Route interrupts to INTA#/B# */
882 pcicfgw16(cb->pci, PciBCR,
883 pcicfgr16(cb->pci, PciBCR) & ~(1 << 7));
889 unconfigure(Cardbus *cb)
892 int i, ioindex, memindex, r;
894 if (cb->type == PC16) {
895 print("#Y%d: Don't know how to unconfigure a PC16 card\n",
896 (int)(cb - cbslots));
898 memset(&cb->linfo, 0, sizeof(Pcminfo));
902 pci = cb->pci->bridge;
904 return; /* Not configured */
905 cb->pci->bridge = nil;
907 memindex = ioindex = 0;
911 for (i = 0; i != Nbars; i++) {
912 if (pci->mem[i].size == 0)
914 if (pci->mem[i].bar & 1) {
915 iofree(pci->mem[i].bar & ~1);
916 pcicfgw16(cb->pci, PciCBIBR0 + ioindex * 8,
918 pcicfgw16(cb->pci, PciCBILR0 + ioindex * 8, 0);
923 upafree(pci->mem[i].bar & ~0xF, pci->mem[i].size);
924 pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8, (ulong)-1);
925 pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8, 0);
926 r = pcicfgr16(cb->pci, PciBCR);
927 r &= ~(1 << (8 + memindex));
928 pcicfgw16(cb->pci, PciBCR, r);
932 if (pci->rom.bar && memindex < 2) {
933 upafree(pci->rom.bar & ~0xF, pci->rom.size);
934 pcicfgw32(cb->pci, PciCBMBR0 + memindex * 8, (ulong)-1);
935 pcicfgw32(cb->pci, PciCBMLR0 + memindex * 8, 0);
946 i82365configure(Cardbus *cb)
954 * Read all tuples in attribute space.
956 m = isamap(cb, 0, 0, 1);
960 cis.cisbase = KADDR(m->isa);
965 /* loop through all the tuples */
968 if(readc(&cis, &type) != 1)
972 if(readc(&cis, &link) != 1)
979 tvers1(cb, &cis, type);
982 tcfig(cb, &cis, type);
985 tentry(cb, &cis, type);
991 cis.cispos = this + (2+link);
997 * look for a card whose version contains 'idstr'
1000 pccard_pcmspecial(char *idstr, ISAConf *isa)
1003 PCMconftab *ct, *et;
1009 for (i = 0; i != nslots; i++) {
1013 if (cb->state == SlotConfigured &&
1016 strstr(cb->linfo.verstr, idstr))
1023 print("#Y: %s not found\n", idstr);
1030 * configure the PCMslot for IO. We assume very heavily that we can read
1031 * configuration info from the CIS. If not, we won't set up correctly.
1037 et = &pi->ctab[pi->nctab];
1039 for(i = 0; i < isa->nopt; i++){
1043 if(strncmp(isa->opt[i], "index=", 6))
1045 index = strtol(&isa->opt[i][6], &cp, 0);
1046 if(cp == &isa->opt[i][6] || index >= pi->nctab) {
1048 print("#Y%d: Cannot find index %d in conf table\n",
1049 (int)(cb - cbslots), index);
1052 ct = &pi->ctab[index];
1058 /* assume default is right */
1064 /* try for best match */
1066 || ct->io[0].start != isa->port || ((1<<irq) & ct->irqs) == 0){
1067 for(t = pi->ctab; t < et; t++)
1069 && t->io[0].start == isa->port
1070 && ((1<<irq) & t->irqs)){
1075 if(ct->nio == 0 || ((1<<irq) & ct->irqs) == 0){
1076 for(t = pi->ctab; t < et; t++)
1077 if(t->nio && ((1<<irq) & t->irqs)){
1083 for(t = pi->ctab; t < et; t++)
1091 if(ct == et || ct->nio == 0) {
1093 print("#Y%d: No configuration?\n", (int)(cb - cbslots));
1096 if(isa->port == 0 && ct->io[0].start == 0) {
1098 print("#Y%d: No part or start address\n", (int)(cb - cbslots));
1102 cb->special = 1; /* taken */
1104 /* route interrupts */
1106 wrreg(cb, Rigc, irq | Fnotreset | Fiocard);
1108 /* set power and enable device */
1109 x = vcode(ct->vpp1);
1110 wrreg(cb, Rpc, x|Fautopower|Foutena|Fcardena);
1112 /* 16-bit data path */
1114 x = Ftiming|Fiocs16|Fwidth16;
1117 if(ct->nio == 2 && ct->io[1].start)
1122 * enable io port map 0
1123 * the 'top' register value includes the last valid address
1126 isa->port = ct->io[0].start;
1127 we = rdreg(cb, Rwe);
1128 wrreg(cb, Riobtm0lo, isa->port);
1129 wrreg(cb, Riobtm0hi, isa->port>>8);
1130 i = isa->port+ct->io[0].len-1;
1131 wrreg(cb, Riotop0lo, i);
1132 wrreg(cb, Riotop0hi, i>>8);
1134 if(ct->nio == 2 && ct->io[1].start){
1135 wrreg(cb, Riobtm1lo, ct->io[1].start);
1136 wrreg(cb, Riobtm1hi, ct->io[1].start>>8);
1137 i = ct->io[1].start+ct->io[1].len-1;
1138 wrreg(cb, Riotop1lo, i);
1139 wrreg(cb, Riotop1hi, i>>8);
1144 /* only touch Rconfig if it is present */
1145 if(pi->conf_present & (1<<Rconfig)){
1149 m = isamap(cb, pi->conf_addr + Rconfig, 1, 1);
1150 p = KADDR(m->isa + pi->conf_addr + Rconfig - m->ca);
1152 /* set configuration and interrupt type */
1154 if(ct->irqtype & 0x20)
1162 pi->port = isa->port;
1166 print("#Y%ld: %s irq %d, port %lX\n", cb - cbslots, pi->verstr, isa->irq, isa->port);
1167 return (int)(cb - cbslots);
1171 pccard_pcmspecialclose(int slotno)
1173 Cardbus *cb = &cbslots[slotno];
1175 wrreg(cb, Rwe, 0); /* no windows */
1180 pccardattach(char *spec)
1182 if (!managerstarted) {
1184 kproc("cardbus", processevents, nil);
1186 return devattach('Y', spec);
1197 #define SLOTNO(c) ((ulong)((c->qid.path>>8)&0xff))
1198 #define TYPE(c) ((ulong)(c->qid.path&0xff))
1199 #define QID(s,t) (((s)<<8)|(t))
1202 pccardgen(Chan *c, char*, Dirtab *, int , int i, Dir *dp)
1210 mkqid(&qid, Qdir, 0, QTDIR);
1211 devdir(c, qid, "#Y", 0, eve, 0555, dp);
1216 if(i >= Nents * nslots) return -1;
1220 qid.path = QID(slotno, Qctl);
1221 snprint(up->genbuf, sizeof up->genbuf, "cb%dctl", slotno);
1224 /* Entries for memory regions. I'll implement them when
1229 devdir(c, qid, up->genbuf, len, eve, 0660, dp);
1234 pccardwalk(Chan *c, Chan *nc, char **name, int nname)
1236 return devwalk(c, nc, name, nname, 0, 0, pccardgen);
1240 pccardstat(Chan *c, uchar *db, int n)
1242 return devstat(c, db, n, 0, 0, pccardgen);
1246 increfp(Cardbus *cb)
1248 lock(&cb->refslock);
1250 unlock(&cb->refslock);
1254 decrefp(Cardbus *cb)
1256 lock(&cb->refslock);
1258 unlock(&cb->refslock);
1262 pccardopen(Chan *c, int omode)
1264 if (c->qid.type & QTDIR){
1268 increfp(&cbslots[SLOTNO(c)]);
1269 c->mode = openmode(omode);
1276 pccardclose(Chan *c)
1279 if((c->qid.type & QTDIR) == 0)
1280 decrefp(&cbslots[SLOTNO(c)]);
1284 pccardread(Chan *c, void *a, long n, vlong offset)
1292 return devdirread(c, a, n, 0, 0, pccardgen);
1295 buf = p = smalloc(READSTR);
1299 cb = &cbslots[SLOTNO(c)];
1301 p = seprint(p, e, "slot %ld: %s; ", cb - cbslots, states[cb->state]);
1305 seprint(p, e, "\n");
1309 if (cb->pci->bridge) {
1310 Pcidev *pci = cb->pci->bridge;
1314 p = seprint(p, e, "%.4uX %.4uX; irq %d\n",
1315 pci->vid, pci->did, pci->intl);
1316 for (i = 0; i != Nbars; i++)
1317 if (pci->mem[i].size)
1319 "\tmem[%d] %.8ulX (%.8uX)\n",
1323 p = seprint(p, e, "\tROM %.8ulX (%.8uX)\n",
1324 pci->rom.bar, pci->rom.size);
1331 if (cb->state == SlotConfigured) {
1332 Pcminfo *pi = &cb->linfo;
1334 p = seprint(p, e, "%s port %X; irq %d;\n",
1335 pi->verstr, pi->port,
1337 for (i = 0; i != pi->nctab; i++) {
1343 "\tconfiguration[%d] irqs %.4uX; vpp %d, %d; %s\n",
1344 i, ct->irqs, ct->vpp1, ct->vpp2,
1345 (ct == pi->defctab)? "(default);": "");
1346 for (j = 0; j != ct->nio; j++)
1347 if (ct->io[j].len > 0)
1348 p = seprint(p, e, "\t\tio[%d] %.8ulX %uld\n",
1349 j, ct->io[j].start, ct->io[j].len);
1356 n = readstr(offset, a, n, buf);
1364 pccardwrite(Chan *c, void *v, long n, vlong)
1376 cb = &cbslots[SLOTNO(c)];
1378 cbf = parsecmd(v, n);
1383 ct = lookupcmd(cbf, pccardctlmsg, nelem(pccardctlmsg));
1387 device += chartorune(&r, device);
1388 if ((n = devno(r, 1)) >= 0 && devtab[n]->config)
1389 devtab[n]->config(0, device, nil);
1390 qengine(cb, CardEjected);
1393 if ((cb->regs[SocketState] & SS_CCD) == 0)
1394 qengine(cb, CardDetected);
1404 Dev pccarddevtab = {
1426 isamap(Cardbus *cb, ulong offset, int len, int attr)
1436 /* convert offset to granularity */
1439 e = ROUND(offset+len, Mgran);
1443 /* look for a map that covers the right area */
1444 we = rdreg(cb, Rwe);
1447 for(m = pi->mmap; m < &pi->mmap[nelem(pi->mmap)]; m++){
1450 if(offset >= m->ca && e <= m->cea){
1456 if(nm == 0 && m->ref == 0)
1463 /* if isa space isn't big enough, free it and get more */
1466 umbfree(m->isa, m->len);
1469 m->isa = PADDR(umbmalloc(0, len, Mgran));
1471 print("isamap: out of isa space\n");
1477 /* set up new map */
1479 m->cea = m->ca + m->len;
1483 wrreg(cb, Rwe, we & ~bit); /* disable map before changing it */
1484 wrreg(cb, MAP(i, Mbtmlo), m->isa>>Mshift);
1485 wrreg(cb, MAP(i, Mbtmhi), (m->isa>>(Mshift+8)) | F16bit);
1486 wrreg(cb, MAP(i, Mtoplo), (m->isa+m->len-1)>>Mshift);
1487 wrreg(cb, MAP(i, Mtophi), ((m->isa+m->len-1)>>(Mshift+8)));
1489 offset &= (1<<25)-1;
1491 wrreg(cb, MAP(i, Mofflo), offset);
1492 wrreg(cb, MAP(i, Moffhi), (offset>>8) | (attr ? Fregactive : 0));
1493 wrreg(cb, Rwe, we | bit); /* enable map */
1506 * reading and writing card registers
1509 rdreg(Cardbus *cb, int index)
1511 outb(cb->lindex, cb->lbase + index);
1512 return inb(cb->ldata);
1516 wrreg(Cardbus *cb, int index, uchar val)
1518 outb(cb->lindex, cb->lbase + index);
1519 outb(cb->ldata, val);
1523 readc(Cisdat *cis, uchar *x)
1525 if(cis->cispos >= cis->cislen)
1527 *x = cis->cisbase[cis->cisskip*cis->cispos];
1533 getlong(Cisdat *cis, int size)
1540 for(i = 0; i < size; i++){
1541 if(readc(cis, &c) != 1)
1549 tcfig(Cardbus *cb, Cisdat *cis, int )
1551 uchar size, rasize, rmsize;
1555 if(readc(cis, &size) != 1)
1557 rasize = (size&0x3) + 1;
1558 rmsize = ((size>>2)&0xf) + 1;
1559 if(readc(cis, &last) != 1)
1563 pi->conf_addr = getlong(cis, rasize);
1564 pi->conf_present = getlong(cis, rmsize);
1568 tvers1(Cardbus *cb, Cisdat *cis, int )
1570 uchar c, major, minor, last;
1575 if(readc(cis, &major) != 1)
1577 if(readc(cis, &minor) != 1)
1580 for(i = 0; i < sizeof(pi->verstr) - 1; i++){
1581 if(readc(cis, &c) != 1)
1589 if(c == ';' && last == ';')
1598 microvolt(Cisdat *cis)
1604 if(readc(cis, &c) != 1)
1606 exp = exponent[c&0x7];
1607 microvolts = vmant[(c>>3)&0xf]*exp;
1609 if(readc(cis, &c) != 1)
1613 break; /* high impedence when sleeping */
1616 microvolts = 0; /* no connection */
1620 microvolts += exp*(c&0x7f);
1627 nanoamps(Cisdat *cis)
1632 if(readc(cis, &c) != 1)
1634 nanoamps = exponent[c&0x7]*vmant[(c>>3)&0xf];
1636 if(readc(cis, &c) != 1)
1638 if(c == 0x7d || c == 0x7e || c == 0x7f)
1645 * only nominal voltage (feature 1) is important for config,
1646 * other features must read card to stay in sync.
1655 if(readc(cis, &feature) != 1)
1658 mv = microvolt(cis);
1675 ttiming(Cisdat *cis, int scale)
1680 if(readc(cis, &unscaled) != 1)
1682 nanosecs = (mantissa[(unscaled>>3)&0xf]*exponent[unscaled&7])/10;
1683 nanosecs = nanosecs * exponent[scale];
1688 timing(Cisdat *cis, PCMconftab *ct)
1692 if(readc(cis, &c) != 1)
1696 ct->maxwait = ttiming(cis, i); /* max wait */
1699 ct->readywait = ttiming(cis, i); /* max ready/busy wait */
1702 ct->otherwait = ttiming(cis, i); /* reserved wait */
1706 iospaces(Cisdat *cis, PCMconftab *ct)
1712 if(readc(cis, &c) != 1)
1715 ct->bit16 = ((c>>5)&3) >= 2;
1717 ct->io[0].start = 0;
1718 ct->io[0].len = 1<<(c&0x1f);
1723 if(readc(cis, &c) != 1)
1727 * For each of the range descriptions read the
1728 * start address and the length (value is length-1).
1731 for(i = 0; i < nio; i++){
1732 ct->io[i].start = getlong(cis, (c>>4)&0x3);
1733 ct->io[i].len = getlong(cis, (c>>6)&0x3)+1;
1739 irq(Cisdat *cis, PCMconftab *ct)
1743 if(readc(cis, &c) != 1)
1745 ct->irqtype = c & 0xe0;
1747 ct->irqs = getlong(cis, 2);
1749 ct->irqs = 1<<(c&0xf);
1750 ct->irqs &= 0xDEB8; /* levels available to card */
1754 memspace(Cisdat *cis, int asize, int lsize, int host)
1756 ulong haddress, address, len;
1758 len = getlong(cis, lsize)*256;
1759 address = getlong(cis, asize)*256;
1762 haddress = getlong(cis, asize)*256;
1768 tentry(Cardbus *cb, Cisdat *cis, int )
1770 uchar c, i, feature;
1775 if(pi->nctab >= nelem(pi->ctab))
1777 if(readc(cis, &c) != 1)
1779 ct = &pi->ctab[pi->nctab++];
1781 /* copy from last default config */
1785 ct->index = c & 0x3f;
1787 /* is this the new default? */
1791 /* memory wait specified? */
1793 if(readc(cis, &i) != 1)
1799 if(readc(cis, &feature) != 1)
1801 switch(feature&0x3){
1803 ct->vpp1 = ct->vpp2 = power(cis);
1807 ct->vpp1 = ct->vpp2 = power(cis);
1811 ct->vpp1 = power(cis);
1812 ct->vpp2 = power(cis);
1823 switch((feature>>5)&0x3){
1825 memspace(cis, 0, 2, 0);
1828 memspace(cis, 2, 2, 0);
1831 if(readc(cis, &c) != 1)
1833 for(i = 0; i <= (c&0x7); i++)
1834 memspace(cis, (c>>5)&0x3, (c>>3)&0x3, c&0x80);
1840 i82365probe(Cardbus *cb, int lindex, int ldata)
1843 int dev = 0; /* According to the Ricoh spec 00->3F _and_ 80->BF seem
1844 to be the same socket A (ditto for B). */
1846 outb(lindex, Rid + (dev<<7));
1848 if((id & 0xf0) != 0x80)
1849 return; /* not a memory & I/O card */
1850 if((id & 0x0f) == 0x00)
1851 return; /* no revision number, not possible */
1853 cb->lindex = lindex;
1855 cb->ltype = Ti82365;
1856 cb->lbase = (int)(cb - cbslots) * 0x40;
1862 /* could be a cirrus */
1863 outb(cb->lindex, Rchipinfo + (dev<<7));
1866 if((c & 0xc0) != 0xc0)
1869 if((c & 0xc0) != 0x00)
1872 cb->ltype = Tpd6720;
1874 cb->ltype = Tpd6710;
1877 /* low power mode */
1878 outb(cb->lindex, Rmisc2 + (dev<<7));
1880 outb(cb->ldata, c & ~Flowpow);
1885 /* if it's not a Cirrus, it could be a Vadem... */
1886 if(cb->ltype == Ti82365){
1887 /* unlock the Vadem extended regs */
1888 outb(cb->lindex, 0x0E + (dev<<7));
1889 outb(cb->lindex, 0x37 + (dev<<7));
1891 /* make the id register show the Vadem id */
1892 outb(cb->lindex, 0x3A + (dev<<7));
1894 outb(cb->ldata, c|0xC0);
1895 outb(cb->lindex, Rid + (dev<<7));
1900 /* go back to Intel compatible id */
1901 outb(cb->lindex, 0x3A + (dev<<7));
1903 outb(cb->ldata, c & ~0xC0);