2 * ISA PNP 1.0 support + access to PCI configuration space
5 * - implement PNP card configuration (setting io bases etc)
6 * - write user program to drive PNP configuration...
7 * - extend PCI raw access to configuration space (writes, byte/short access?)
8 * - implement PCI access to memory/io space/BIOS ROM
9 * - use c->aux instead of performing lookup on each read/write?
12 #include "../port/lib.h"
17 #include "../port/error.h"
19 typedef struct Pnp Pnp;
20 typedef struct Card Card;
42 #define DPRINT if(pnp.debug) print
43 #define XPRINT if(1) print
61 #define TYPE(q) ((ulong)(q).path & 0x0F)
62 #define CSN(q) (((ulong)(q).path>>4) & 0xFF)
63 #define QID(c, t) (((c)<<4)|(t))
65 static Dirtab topdir[] = {
66 ".", { Qtopdir, 0, QTDIR }, 0, 0555,
67 "pnp", { Qpnpdir, 0, QTDIR }, 0, 0555,
68 "pci", { Qpcidir, 0, QTDIR }, 0, 0555,
71 static Dirtab pnpdir[] = {
72 ".", { Qpnpdir, 0, QTDIR }, 0, 0555,
73 "ctl", { Qpnpctl, 0, 0 }, 0, 0666,
77 static int wrconfig(Card*, char*);
81 0x6A, 0xB5, 0xDA, 0xED, 0xF6, 0xFB, 0x7D, 0xBE,
82 0xDF, 0x6F, 0x37, 0x1B, 0x0D, 0x86, 0xC3, 0x61,
83 0xB0, 0x58, 0x2C, 0x16, 0x8B, 0x45, 0xA2, 0xD1,
84 0xE8, 0x74, 0x3A, 0x9D, 0xCE, 0xE7, 0x73, 0x39,
94 /* Send initiation key, putting each card in Sleep state */
100 /* ensure each card's LFSR is reset */
104 /* send initiation key */
105 for (i = 0; i < 32; i++)
106 outb(Address, key[i]);
109 /* isolation protocol... */
118 return (r1 == 0x55) && (r2 == 0xaa);
122 isolate(int rddata, ulong *id1, ulong *id2)
127 outb(Address, 0x01); /* point to serial isolation register */
130 for(i = 0; i < 64; i++){
131 bit = readbit(rddata);
132 csum = (csum>>1) | (((csum&1) ^ ((csum>>1)&1) ^ bit)<<7);
134 *p = (*p>>1) | (bit<<7);
138 *p = (*p>>1) | (readbit(rddata)<<7);
140 *id1 = (id[3]<<24)|(id[2]<<16)|(id[1]<<8)|id[0];
141 *id2 = (id[7]<<24)|(id[6]<<16)|(id[5]<<8)|id[4];
145 DPRINT("pnp: bad checksum id1 %lux id2 %lux csum %x != %x\n", *id1, *id2, csum, id[8]); /**/
146 return id[8] == csum;
150 getresbyte(int rddata)
155 while ((inb(rddata) & 1) == 0)
156 if (tries++ > 1000000)
157 error("pnp: timeout waiting for resource data\n");
163 serial(ulong id1, ulong id2)
170 i2 = ((id1<<3)&24)+((id1>>13)&7);
172 x = (id1>>8)&0xff00|(id1>>24)&0x00ff;
173 if (i1 > 0 && i1 < 27 && i2 > 0 && i2 < 27 && i3 > 0 && i3 < 27 && (id1 & (1<<7)) == 0)
174 snprint(buf, sizeof(buf), "%c%c%c%.4lux.%lux", 'A'+i1-1, 'A'+i2-1, 'A'+i3-1, x, id2);
176 snprint(buf, sizeof(buf), "%.4lux%.4lux.%lux", (id1<<8)&0xff00|(id1>>8)&0x00ff, x, id2);
181 findcsn(int csn, int create, int dolock)
188 for(c = *l; c != nil; c = *l) {
196 *l = nc = malloc(sizeof(Card));
214 for(c = pnp.cards; c != nil; c = c->next) {
225 int i, n, x, ncfg, n1, n2;
229 x = getresbyte(rddata);
230 if((x & 0x80) == 0) {
232 for(i = 1; i < n; i++)
236 n1 = getresbyte(rddata);
237 n2 = getresbyte(rddata);
239 for (i = 3; i < n; i++)
249 /* look for cards, and assign them CSNs */
251 pnpscan(int rddata, int dawn)
257 initiation(); /* upsilon sigma */
258 cmd(0x02, 0x04+0x01); /* reset CSN on all cards and reset logical devices */
259 delay(1); /* delay after resetting cards */
261 cmd(0x03, 0); /* Wake all cards with a CSN of 0 */
262 cmd(0x00, rddata>>2); /* Set the READ_DATA port on all cards */
263 while(isolate(rddata, &id1, &id2)) {
264 for(c = pnp.cards; c != nil; c = c->next)
265 if(c->id1 == id1 && c->id2 == id2)
269 c = findcsn(csn, 1, 0);
273 else if(c->cfgstr != nil) {
274 if(!wrconfig(c, c->cfgstr))
275 print("pnp%d: bad cfg: %s\n", c->csn, c->cfgstr);
278 cmd(0x06, c->csn); /* set the card's csn */
280 print("pnp%d: %s\n", c->csn, serial(id1, id2));
281 c->ncfg = pnpncfg(rddata);
282 cmd(0x03, 0); /* Wake all cards with a CSN of 0, putting this card to sleep */
284 cmd(0x02, 0x02); /* return cards to Wait for Key state */
297 int csn, i1, i2, i3, x;
298 char *s, *p, buf[20];
301 memset(&isa, 0, sizeof(ISAConf));
303 if (isaconfig("pnp", 0, &isa) == 0)
305 if(isa.port < 0x203 || isa.port > 0x3ff)
307 for(csn = 1; csn < 256; csn++) {
308 snprint(buf, sizeof buf, "pnp%d", csn);
312 if(strlen(s) < 8 || s[7] != '.' || s[0] < 'A' || s[0] > 'Z' || s[1] < 'A' || s[1] > 'Z' || s[2] < 'A' || s[2] > 'Z') {
314 print("pnp%d: bad conf string %s\n", csn, s);
320 x = strtoul(&s[3], 0, 16);
321 id1 = (i1<<2)|((i2>>3)&3)|((i2&7)<<13)|(i3<<8)|((x&0xff)<<24)|((x&0xff00)<<8);
322 id2 = strtoul(&s[8], &p, 16);
329 c = findcsn(csn, 1, 0);
334 pnpscan(isa.port, 1);
338 csngen(Chan *c, int t, int csn, Card *cp, Dir *dp)
344 q = (Qid){QID(csn, Qcsnctl), 0, 0};
345 snprint(up->genbuf, sizeof up->genbuf, "csn%dctl", csn);
346 devdir(c, q, up->genbuf, 0, eve, 0664, dp);
349 q = (Qid){QID(csn, Qcsnraw), 0, 0};
350 snprint(up->genbuf, sizeof up->genbuf, "csn%draw", csn);
351 devdir(c, q, up->genbuf, cp->ncfg, eve, 0444, dp);
358 pcigen(Chan *c, int t, int tbdf, Dir *dp)
362 q = (Qid){BUSBDF(tbdf)|t, 0, 0};
365 snprint(up->genbuf, sizeof up->genbuf, "%d.%d.%dctl",
366 BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
367 devdir(c, q, up->genbuf, 0, eve, 0444, dp);
370 snprint(up->genbuf, sizeof up->genbuf, "%d.%d.%draw",
371 BUSBNO(tbdf), BUSDNO(tbdf), BUSFNO(tbdf));
372 devdir(c, q, up->genbuf, 128, eve, 0660, dp);
379 pnpgen(Chan *c, char *, Dirtab*, int, int s, Dir *dp)
386 switch(TYPE(c->qid)){
389 q = (Qid){QID(0, Qtopdir), 0, QTDIR};
390 snprint(up->genbuf, sizeof up->genbuf, "#%C", pnpdevtab.dc);
391 devdir(c, q, up->genbuf, 0, eve, 0555, dp);
394 return devgen(c, nil, topdir, nelem(topdir), s, dp);
397 q = (Qid){QID(0, Qtopdir), 0, QTDIR};
398 snprint(up->genbuf, sizeof up->genbuf, "#%C", pnpdevtab.dc);
399 devdir(c, q, up->genbuf, 0, eve, 0555, dp);
402 if(s < nelem(pnpdir)-1)
403 return devgen(c, nil, pnpdir, nelem(pnpdir), s, dp);
404 s -= nelem(pnpdir)-1;
407 while(s >= 2 && cp != nil) {
414 return csngen(c, s+Qcsnctl, cp->csn, cp, dp);
416 return devgen(c, nil, pnpdir, nelem(pnpdir), s, dp);
420 cp = findcsn(csn, 0, 1);
423 return csngen(c, TYPE(c->qid), csn, cp, dp);
426 q = (Qid){QID(0, Qtopdir), 0, QTDIR};
427 snprint(up->genbuf, sizeof up->genbuf, "#%C", pnpdevtab.dc);
428 devdir(c, q, up->genbuf, 0, eve, 0555, dp);
431 p = pcimatch(nil, 0, 0);
432 while(s >= 2 && p != nil) {
433 p = pcimatch(p, 0, 0);
438 return pcigen(c, s+Qpcictl, p->tbdf, dp);
441 tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((ulong)c->qid.path);
442 p = pcimatchtbdf(tbdf);
445 return pcigen(c, TYPE(c->qid), tbdf, dp);
453 pnpattach(char *spec)
455 return devattach(pnpdevtab.dc, spec);
459 pnpwalk(Chan* c, Chan *nc, char** name, int nname)
461 return devwalk(c, nc, name, nname, (Dirtab *)0, 0, pnpgen);
465 pnpstat(Chan* c, uchar* dp, int n)
467 return devstat(c, dp, n, (Dirtab *)0, 0L, pnpgen);
471 pnpopen(Chan *c, int omode)
473 c = devopen(c, omode, (Dirtab*)0, 0, pnpgen);
474 switch(TYPE(c->qid)){
487 pnpread(Chan *c, void *va, long n, vlong offset)
492 char buf[256], *ebuf, *w;
496 switch(TYPE(c->qid)){
500 return devdirread(c, a, n, (Dirtab *)0, 0L, pnpgen);
503 snprint(up->genbuf, sizeof up->genbuf, "enabled %#x\n",
506 snprint(up->genbuf, sizeof up->genbuf, "disabled\n");
507 return readstr(offset, a, n, up->genbuf);
510 cp = findcsn(csn, 0, 1);
513 if(offset+n > cp->ncfg)
514 n = cp->ncfg - offset;
517 cmd(0x03, csn); /* Wake up the card */
518 for(i = 0; i < offset+9; i++) /* 9 == skip serial + csum */
519 getresbyte(pnp.rddata);
520 for(i = 0; i < n; i++)
521 a[i] = getresbyte(pnp.rddata);
522 cmd(0x03, 0); /* Wake all cards with a CSN of 0, putting this card to sleep */
523 cmd(0x02, 0x02); /* return cards to Wait for Key state */
528 cp = findcsn(csn, 0, 1);
531 snprint(up->genbuf, sizeof up->genbuf, "%s\n",
532 serial(cp->id1, cp->id2));
533 return readstr(offset, a, n, up->genbuf);
535 tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((ulong)c->qid.path);
536 p = pcimatchtbdf(tbdf);
539 ebuf = buf+sizeof buf-1; /* -1 for newline */
540 w = seprint(buf, ebuf, "%.2x.%.2x.%.2x %.4x/%.4x %3d",
541 p->ccrb, p->ccru, p->ccrp, p->vid, p->did, p->intl);
542 for(i=0; i<nelem(p->mem); i++){
543 if(p->mem[i].size == 0)
545 w = seprint(w, ebuf, " %d:%.8lux %d", i, p->mem[i].bar, p->mem[i].size);
549 return readstr(offset, a, n, buf);
551 tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((ulong)c->qid.path);
552 p = pcimatchtbdf(tbdf);
560 if(!(r & 3) && n == 4){
565 if(!(r & 1) && n == 2){
570 for(i = 0; i < n; i++){
584 pnpwrite(Chan *c, void *va, long n, vlong offset)
599 switch(TYPE(c->qid)){
601 if(strncmp(buf, "port ", 5) == 0) {
602 port = strtoul(buf+5, 0, 0);
603 if(port < 0x203 || port > 0x3ff)
604 error("bad value for rddata port");
611 error("pnp port already set");
612 if(!pnpscan(port, 0))
613 error("no cards found");
617 else if(strncmp(buf, "debug ", 6) == 0)
618 pnp.debug = strtoul(buf+6, 0, 0);
624 cp = findcsn(csn, 0, 1);
627 if(!wrconfig(cp, buf))
631 tbdf = MKBUS(BusPCI, 0, 0, 0)|BUSBDF((ulong)c->qid.path);
632 p = pcimatchtbdf(tbdf);
640 if(!(r & 3) && n == 4){
645 if(!(r & 1) && n == 2){
650 for(i = 0; i < n; i++){
664 wrconfig(Card *c, char *cmd)
666 /* This should implement setting of I/O bases, etc */