Initial import from FreeBSD RELENG_4:
[games.git] / sys / bus / isa / pnp.c
1 /*
2  * Copyright (c) 1996, Sujal M. Patel
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      $FreeBSD: src/sys/isa/pnp.c,v 1.5.2.1 2002/10/14 09:31:09 nyan Exp $
27  *      from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
28  */
29
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/bus.h>
35 #include <sys/malloc.h>
36 #include <isa/isavar.h>
37 #include <isa/pnpreg.h>
38 #include <isa/pnpvar.h>
39 #include <machine/clock.h>
40
41 typedef struct _pnp_id {
42         u_int32_t vendor_id;
43         u_int32_t serial;
44         u_char checksum;
45 } pnp_id;
46
47 struct pnp_set_config_arg {
48         int     csn;            /* Card number to configure */
49         int     ldn;            /* Logical device on card */
50 };
51
52 struct pnp_quirk {
53         u_int32_t vendor_id;    /* Vendor of the card */
54         u_int32_t logical_id;   /* ID of the device with quirk */
55         int     type;
56 #define PNP_QUIRK_WRITE_REG     1 /* Need to write a pnp register  */
57         int     arg1;
58         int     arg2;
59 };
60
61 struct pnp_quirk pnp_quirks[] = {
62         /*
63          * The Gravis UltraSound needs register 0xf2 to be set to 0xff
64          * to enable power.
65          * XXX need to know the logical device id.
66          */
67         { 0x0100561e /* GRV0001 */,     0,
68           PNP_QUIRK_WRITE_REG,  0xf2,    0xff },
69
70         { 0 }
71 };
72
73 #ifdef PC98
74 /* Some NEC PnP cards have 9 bytes serial code. */
75 static pnp_id necids[] = {
76         {0x4180a3b8, 0xffffffff, 0x00}, /* PC-9801CB-B04 (NEC8041) */
77         {0x5181a3b8, 0xffffffff, 0x46}, /* PC-9821CB2-B04(NEC8151) */
78         {0x5182a3b8, 0xffffffff, 0xb8}, /* PC-9801-XX    (NEC8251) */
79         {0x9181a3b8, 0xffffffff, 0x00}, /* PC-9801-120   (NEC8191) */
80         {0, 0, 0}
81 };
82 #endif
83
84 #if 0
85 /*
86  * these entries are initialized using the autoconfig menu
87  * The struct is invalid (and must be initialized) if the first
88  * CSN is zero. The init code fills invalid entries with CSN 255
89  * which is not a supported value.
90  */
91
92 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
93     { 0 }
94 };
95 #endif
96
97 /* The READ_DATA port that we are using currently */
98 static int pnp_rd_port;
99
100 static void   pnp_send_initiation_key(void);
101 static int    pnp_get_serial(pnp_id *p);
102 static int    pnp_isolation_protocol(device_t parent);
103
104 char *
105 pnp_eisaformat(u_int32_t id)
106 {
107         u_int8_t *data = (u_int8_t *) &id;
108         static char idbuf[8];
109         const char  hextoascii[] = "0123456789abcdef";
110
111         idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
112         idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
113         idbuf[2] = '@' + (data[1] & 0x1f);
114         idbuf[3] = hextoascii[(data[2] >> 4)];
115         idbuf[4] = hextoascii[(data[2] & 0xf)];
116         idbuf[5] = hextoascii[(data[3] >> 4)];
117         idbuf[6] = hextoascii[(data[3] & 0xf)];
118         idbuf[7] = 0;
119         return(idbuf);
120 }
121
122 static void
123 pnp_write(int d, u_char r)
124 {
125         outb (_PNP_ADDRESS, d);
126         outb (_PNP_WRITE_DATA, r);
127 }
128
129 #if 0
130
131 static u_char
132 pnp_read(int d)
133 {
134         outb (_PNP_ADDRESS, d);
135         return (inb(3 | (pnp_rd_port <<2)));
136 }
137
138 #endif
139
140 /*
141  * Send Initiation LFSR as described in "Plug and Play ISA Specification",
142  * Intel May 94.
143  */
144 static void
145 pnp_send_initiation_key()
146 {
147         int cur, i;
148
149         /* Reset the LSFR */
150         outb(_PNP_ADDRESS, 0);
151         outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
152
153         cur = 0x6a;
154         outb(_PNP_ADDRESS, cur);
155
156         for (i = 1; i < 32; i++) {
157                 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
158                 outb(_PNP_ADDRESS, cur);
159         }
160 }
161
162
163 /*
164  * Get the device's serial number.  Returns 1 if the serial is valid.
165  */
166 static int
167 pnp_get_serial(pnp_id *p)
168 {
169         int i, bit, valid = 0, sum = 0x6a;
170         u_char *data = (u_char *)p;
171
172         bzero(data, sizeof(char) * 9);
173         outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
174         for (i = 0; i < 72; i++) {
175                 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
176                 DELAY(250);     /* Delay 250 usec */
177
178                 /* Can't Short Circuit the next evaluation, so 'and' is last */
179                 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
180                 DELAY(250);     /* Delay 250 usec */
181
182                 valid = valid || bit;
183
184                 if (i < 64)
185                         sum = (sum >> 1) |
186                                 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
187
188                 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
189         }
190
191         valid = valid && (data[8] == sum);
192
193         return valid;
194 }
195
196 /*
197  * Fill's the buffer with resource info from the device.
198  * Returns the number of characters read.
199  */
200 static int
201 pnp_get_resource_info(u_char *buffer, int len)
202 {
203         int i, j, count;
204         u_char temp;
205
206         count = 0;
207         for (i = 0; i < len; i++) {
208                 outb(_PNP_ADDRESS, PNP_STATUS);
209                 for (j = 0; j < 100; j++) {
210                         if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
211                                 break;
212                         DELAY(1);
213                 }
214                 if (j == 100) {
215                         printf("PnP device failed to report resource data\n");
216                         return count;
217                 }
218                 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
219                 temp = inb((pnp_rd_port << 2) | 0x3);
220                 if (buffer != NULL)
221                         buffer[i] = temp;
222                 count++;
223         }
224         return count;
225 }
226
227 #if 0
228 /*
229  * write_pnp_parms initializes a logical device with the parms
230  * in d, and then activates the board if the last parameter is 1.
231  */
232
233 static int
234 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
235 {
236     int i, empty = -1 ;
237
238     pnp_write (SET_LDN, ldn );
239     i = pnp_read(SET_LDN) ;
240     if (i != ldn) {
241         printf("Warning: LDN %d does not exist\n", ldn);
242     }
243     for (i = 0; i < 8; i++) {
244         pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
245         pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
246     }
247     for (i = 0; i < 4; i++) {
248         pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
249         pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
250         pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
251         pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
252         pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
253     }
254     for (i = 0; i < 2; i++) {
255         pnp_write(IRQ_CONFIG + i*2    , d->irq[i] );
256         pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
257         pnp_write(DRQ_CONFIG + i, d->drq[i] );
258     }
259     /*
260      * store parameters read into the current kernel
261      * so manual editing next time is easier
262      */
263     for (i = 0 ; i < MAX_PNP_LDN; i++) {
264         if (pnp_ldn_overrides[i].csn == d->csn &&
265                 pnp_ldn_overrides[i].ldn == ldn) {
266             d->flags = pnp_ldn_overrides[i].flags ;
267             pnp_ldn_overrides[i] = *d ;
268             break ;
269         } else if (pnp_ldn_overrides[i].csn < 1 ||
270                 pnp_ldn_overrides[i].csn == 255)
271             empty = i ;
272     }
273     if (i== MAX_PNP_LDN && empty != -1)
274         pnp_ldn_overrides[empty] = *d;
275
276     /*
277      * Here should really perform the range check, and
278      * return a failure if not successful.
279      */
280     pnp_write (IO_RANGE_CHECK, 0);
281     DELAY(1000); /* XXX is it really necessary ? */
282     pnp_write (ACTIVATE, d->enable ? 1 : 0);
283     DELAY(1000); /* XXX is it really necessary ? */
284     return 1 ;
285 }
286 #endif
287
288 /*
289  * This function is called after the bus has assigned resource
290  * locations for a logical device.
291  */
292 static void
293 pnp_set_config(void *arg, struct isa_config *config, int enable)
294 {
295         int csn = ((struct pnp_set_config_arg *) arg)->csn;
296         int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
297         int i;
298
299         /*
300          * First put all cards into Sleep state with the initiation
301          * key, then put our card into Config state.
302          */
303         pnp_send_initiation_key();
304         pnp_write(PNP_WAKE, csn);
305
306         /*
307          * Select our logical device so that we can program it.
308          */
309         pnp_write(PNP_SET_LDN, ldn);
310
311         /*
312          * Now program the resources.
313          */
314         for (i = 0; i < config->ic_nmem; i++) {
315                 u_int32_t start = config->ic_mem[i].ir_start;
316                 u_int32_t size =  config->ic_mem[i].ir_size;
317                 if (start & 0xff)
318                         panic("pnp_set_config: bogus memory assignment");
319                 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
320                 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
321                 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
322                 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
323         }
324         for (; i < ISA_NMEM; i++) {
325                 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
326                 pnp_write(PNP_MEM_BASE_LOW(i), 0);
327                 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
328                 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
329         }
330
331         for (i = 0; i < config->ic_nport; i++) {
332                 u_int32_t start = config->ic_port[i].ir_start;
333                 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
334                 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
335         }
336         for (; i < ISA_NPORT; i++) {
337                 pnp_write(PNP_IO_BASE_HIGH(i), 0);
338                 pnp_write(PNP_IO_BASE_LOW(i), 0);
339         }
340
341         for (i = 0; i < config->ic_nirq; i++) {
342                 int irq = ffs(config->ic_irqmask[i]) - 1;
343                 pnp_write(PNP_IRQ_LEVEL(i), irq);
344                 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
345         }
346         for (; i < ISA_NIRQ; i++) {
347                 /*
348                  * IRQ 0 is not a valid interrupt selection and
349                  * represents no interrupt selection.
350                  */
351                 pnp_write(PNP_IRQ_LEVEL(i), 0);
352         }               
353
354         for (i = 0; i < config->ic_ndrq; i++) {
355                 int drq = ffs(config->ic_drqmask[i]) - 1;
356                 pnp_write(PNP_DMA_CHANNEL(i), drq);
357         }
358         for (; i < ISA_NDRQ; i++) {
359                 /*
360                  * DMA channel 4, the cascade channel is used to
361                  * indicate no DMA channel is active.
362                  */
363                 pnp_write(PNP_DMA_CHANNEL(i), 4);
364         }               
365
366         pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
367
368         /*
369          * Wake everyone up again, we are finished.
370          */
371         pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
372 }
373
374 /*
375  * Process quirks for a logical device.. The card must be in Config state.
376  */
377 static void
378 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn)
379 {
380         struct pnp_quirk *qp;
381
382         for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
383                 if (qp->vendor_id == vendor_id
384                     && (qp->logical_id == 0
385                         || qp->logical_id == logical_id)) {
386                         switch (qp->type) {
387                         case PNP_QUIRK_WRITE_REG:
388                                 pnp_write(PNP_SET_LDN, ldn);
389                                 pnp_write(qp->arg1, qp->arg2);
390                                 break;
391                         }
392                 }
393         }
394 }
395
396 /*
397  * Scan Resource Data for Logical Devices.
398  *
399  * This function exits as soon as it gets an error reading *ANY*
400  * Resource Data or it reaches the end of Resource Data.  In the first
401  * case the return value will be TRUE, FALSE otherwise.
402  */
403 static int
404 pnp_create_devices(device_t parent, pnp_id *p, int csn,
405                    u_char *resources, int len)
406 {
407         u_char tag, *resp, *resinfo, *startres = 0;
408         int large_len, scanning = len, retval = FALSE;
409         u_int32_t logical_id;
410         u_int32_t compat_id;
411         device_t dev = 0;
412         int ldn = 0;
413         struct pnp_set_config_arg *csnldn;
414         char buf[100];
415         char *desc = 0;
416
417         resp = resources;
418         while (scanning > 0) {
419                 tag = *resp++;
420                 scanning--;
421                 if (PNP_RES_TYPE(tag) != 0) {
422                         /* Large resource */
423                         if (scanning < 2) {
424                                 scanning = 0;
425                                 continue;
426                         }
427                         large_len = resp[0] + (resp[1] << 8);
428                         resp += 2;
429
430                         if (scanning < large_len) {
431                                 scanning = 0;
432                                 continue;
433                         }
434                         resinfo = resp;
435                         resp += large_len;
436                         scanning -= large_len;
437
438                         if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
439                                 if (large_len > sizeof(buf) - 1)
440                                         large_len = sizeof(buf) - 1;
441                                 bcopy(resinfo, buf, large_len);
442
443                                 /*
444                                  * Trim trailing spaces.
445                                  */
446                                 while (buf[large_len-1] == ' ')
447                                         large_len--;
448                                 buf[large_len] = '\0';
449                                 desc = buf;
450                                 if (dev)
451                                         device_set_desc_copy(dev, desc);
452                                 continue;
453                         }
454
455                         continue;
456                 }
457                 
458                 /* Small resource */
459                 if (scanning < PNP_SRES_LEN(tag)) {
460                         scanning = 0;
461                         continue;
462                 }
463                 resinfo = resp;
464                 resp += PNP_SRES_LEN(tag);
465                 scanning -= PNP_SRES_LEN(tag);;
466                         
467                 switch (PNP_SRES_NUM(tag)) {
468                 case PNP_TAG_LOGICAL_DEVICE:
469                         /*
470                          * Parse the resources for the previous
471                          * logical device (if any).
472                          */
473                         if (startres) {
474                                 pnp_parse_resources(dev, startres,
475                                                     resinfo - startres - 1);
476                                 dev = 0;
477                                 startres = 0;
478                         }
479
480                         /* 
481                          * A new logical device. Scan for end of
482                          * resources.
483                          */
484                         bcopy(resinfo, &logical_id, 4);
485                         pnp_check_quirks(p->vendor_id, logical_id, ldn);
486                         compat_id = 0;
487                         dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
488                         if (desc)
489                                 device_set_desc_copy(dev, desc);
490                         isa_set_vendorid(dev, p->vendor_id);
491                         isa_set_serial(dev, p->serial);
492                         isa_set_logicalid(dev, logical_id);
493                         csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
494                         if (!csnldn) {
495                                 device_printf(parent,
496                                               "out of memory\n");
497                                 scanning = 0;
498                                 break;
499                         }
500                         csnldn->csn = csn;
501                         csnldn->ldn = ldn;
502                         ISA_SET_CONFIG_CALLBACK(parent, dev,
503                                                 pnp_set_config, csnldn);
504                         ldn++;
505                         startres = resp;
506                         break;
507                     
508                 case PNP_TAG_END:
509                         if (!startres) {
510                                 device_printf(parent,
511                                               "malformed resources\n");
512                                 scanning = 0;
513                                 break;
514                         }
515                         pnp_parse_resources(dev, startres,
516                                             resinfo - startres - 1);
517                         dev = 0;
518                         startres = 0;
519                         scanning = 0;
520                         break;
521
522                 default:
523                         /* Skip this resource */
524                         break;
525                 }
526         }
527
528         return retval;
529 }
530
531 /*
532  * Read 'amount' bytes of resources from the card, allocating memory
533  * as needed. If a buffer is already available, it should be passed in
534  * '*resourcesp' and its length in '*spacep'. The number of resource
535  * bytes already in the buffer should be passed in '*lenp'. The memory
536  * allocated will be returned in '*resourcesp' with its size and the
537  * number of bytes of resources in '*spacep' and '*lenp' respectively.
538  */
539 static int
540 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
541 {
542         u_char *resources = *resourcesp;
543         u_char *newres;
544         int space = *spacep;
545         int len = *lenp;
546
547         if (space == 0) {
548                 space = 1024;
549                 resources = malloc(space, M_TEMP, M_NOWAIT);
550                 if (!resources)
551                         return ENOMEM;
552         }
553         
554         if (len + amount > space) {
555                 int extra = 1024;
556                 while (len + amount > space + extra)
557                         extra += 1024;
558                 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
559                 if (!newres)
560                         return ENOMEM;
561                 bcopy(resources, newres, len);
562                 free(resources, M_TEMP);
563                 resources = newres;
564                 space += extra;
565         }
566
567         if (pnp_get_resource_info(resources + len, amount) != amount)
568                 return EINVAL;
569         len += amount;
570
571         *resourcesp = resources;
572         *spacep = space;
573         *lenp = len;
574
575         return 0;
576 }
577
578 /*
579  * Read all resources from the card, allocating memory as needed. If a
580  * buffer is already available, it should be passed in '*resourcesp'
581  * and its length in '*spacep'. The memory allocated will be returned
582  * in '*resourcesp' with its size and the number of bytes of resources
583  * in '*spacep' and '*lenp' respectively.
584  */
585 static int
586 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
587 {
588         u_char *resources = *resourcesp;
589         int space = *spacep;
590         int len = 0;
591         int error, done;
592         u_char tag;
593
594         error = 0;
595         done = 0;
596         while (!done) {
597                 error = pnp_read_bytes(1, &resources, &space, &len);
598                 if (error)
599                         goto out;
600                 tag = resources[len-1];
601                 if (PNP_RES_TYPE(tag) == 0) {
602                         /*
603                          * Small resource, read contents.
604                          */
605                         error = pnp_read_bytes(PNP_SRES_LEN(tag),
606                                                &resources, &space, &len);
607                         if (error)
608                                 goto out;
609                         if (PNP_SRES_NUM(tag) == PNP_TAG_END)
610                                 done = 1;
611                 } else {
612                         /*
613                          * Large resource, read length and contents.
614                          */
615                         error = pnp_read_bytes(2, &resources, &space, &len);
616                         if (error)
617                                 goto out;
618                         error = pnp_read_bytes(resources[len-2]
619                                                + (resources[len-1] << 8),
620                                                &resources, &space, &len);
621                         if (error)
622                                 goto out;
623                 }
624         }
625
626  out:
627         *resourcesp = resources;
628         *spacep = space;
629         *lenp = len;
630         return error;
631 }
632
633 /*
634  * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
635  * value (caller should try multiple READ_DATA locations before giving
636  * up). Upon exiting, all cards are aware that they should use
637  * pnp_rd_port as the READ_DATA port.
638  *
639  * In the first pass, a csn is assigned to each board and pnp_id's
640  * are saved to an array, pnp_devices. In the second pass, each
641  * card is woken up and the device configuration is called.
642  */
643 static int
644 pnp_isolation_protocol(device_t parent)
645 {
646         int csn;
647         pnp_id id;
648         int found = 0, len;
649         u_char *resources = 0;
650         int space = 0;
651         int error;
652 #ifdef PC98
653         int n, necpnp;
654         u_char buffer[10];
655 #endif
656
657         /*
658          * Put all cards into the Sleep state so that we can clear
659          * their CSNs.
660          */
661         pnp_send_initiation_key();
662
663         /*
664          * Clear the CSN for all cards.
665          */
666         pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
667
668         /*
669          * Move all cards to the Isolation state.
670          */
671         pnp_write(PNP_WAKE, 0);
672
673         /*
674          * Tell them where the read point is going to be this time.
675          */
676         pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
677
678         for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
679                 /*
680                  * Start the serial isolation protocol.
681                  */
682                 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
683                 DELAY(1000);    /* Delay 1 msec */
684
685                 if (pnp_get_serial(&id)) {
686                         /*
687                          * We have read the id from a card
688                          * successfully. The card which won the
689                          * isolation protocol will be in Isolation
690                          * mode and all others will be in Sleep.
691                          * Program the CSN of the isolated card
692                          * (taking it to Config state) and read its
693                          * resources, creating devices as we find
694                          * logical devices on the card.
695                          */
696                         pnp_write(PNP_SET_CSN, csn);
697 #ifdef PC98
698                         if (bootverbose)
699                                 printf("PnP Vendor ID = %x\n", id.vendor_id);
700                         /* Check for NEC PnP (9 bytes serial). */
701                         for (n = necpnp = 0; necids[n].vendor_id; n++) {
702                                 if (id.vendor_id == necids[n].vendor_id) {
703                                         necpnp = 1;
704                                         break;
705                                 }
706                         }
707                         if (necpnp) {
708                                 if (bootverbose)
709                                         printf("It seems to NEC-PnP card (%s).\n",
710                                                pnp_eisaformat(id.vendor_id));
711                                 /*  Read dummy 9 bytes serial area. */
712                                 pnp_get_resource_info(buffer, 9);
713                         } else {
714                                 if (bootverbose)
715                                         printf("It seems to Normal-ISA-PnP card (%s).\n",
716                                                pnp_eisaformat(id.vendor_id));
717                         }
718                         if (bootverbose)
719                                 printf("Reading PnP configuration for %s.\n",
720                                        pnp_eisaformat(id.vendor_id));
721 #endif
722                         error = pnp_read_resources(&resources,
723                                                    &space,
724                                                    &len);
725                         if (error)
726                                 break;
727                         pnp_create_devices(parent, &id, csn,
728                                            resources, len);
729                         found++;
730                 } else
731                         break;
732
733                 /*
734                  * Put this card back to the Sleep state and
735                  * simultaneously move all cards which don't have a
736                  * CSN yet to Isolation state.
737                  */
738                 pnp_write(PNP_WAKE, 0);
739         }
740
741         /*
742          * Unless we have chosen the wrong read port, all cards will
743          * be in Sleep state. Put them back into WaitForKey for
744          * now. Their resources will be programmed later.
745          */
746         pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
747
748         /*
749          * Cleanup.
750          */
751         if (resources)
752                 free(resources, M_TEMP);
753
754         return found;
755 }
756
757
758 /*
759  * pnp_identify()
760  *
761  * autoconfiguration of pnp devices. This routine just runs the
762  * isolation protocol over several ports, until one is successful.
763  *
764  * may be called more than once ?
765  *
766  */
767
768 static void
769 pnp_identify(driver_t *driver, device_t parent)
770 {
771         int num_pnp_devs;
772
773 #if 0
774         if (pnp_ldn_overrides[0].csn == 0) {
775                 if (bootverbose)
776                         printf("Initializing PnP override table\n");
777                 bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
778                 pnp_ldn_overrides[0].csn = 255 ;
779         }
780 #endif
781
782         /* Try various READ_DATA ports from 0x203-0x3ff */
783         for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
784                 if (bootverbose)
785                         printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
786
787                 num_pnp_devs = pnp_isolation_protocol(parent);
788                 if (num_pnp_devs)
789                         break;
790         }
791 }
792
793 static device_method_t pnp_methods[] = {
794         /* Device interface */
795         DEVMETHOD(device_identify,      pnp_identify),
796
797         { 0, 0 }
798 };
799
800 static driver_t pnp_driver = {
801         "pnp",
802         pnp_methods,
803         1,                      /* no softc */
804 };
805
806 static devclass_t pnp_devclass;
807
808 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);