2 * Copyright (c) 1996, Sujal M. Patel
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * $FreeBSD: src/sys/isa/pnp.c,v 1.5.2.1 2002/10/14 09:31:09 nyan Exp $
27 * $DragonFly: src/sys/bus/isa/pnp.c,v 1.5 2004/04/07 05:54:32 dillon Exp $
28 * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/module.h>
36 #include <sys/malloc.h>
40 #include <machine/clock.h>
42 typedef struct _pnp_id {
48 struct pnp_set_config_arg {
49 int csn; /* Card number to configure */
50 int ldn; /* Logical device on card */
54 u_int32_t vendor_id; /* Vendor of the card */
55 u_int32_t logical_id; /* ID of the device with quirk */
61 #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */
62 #define PNP_QUIRK_EXTRA_IO 2 /* Has extra io ports */
64 struct pnp_quirk pnp_quirks[] = {
66 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
68 * XXX need to know the logical device id.
70 { 0x0100561e /* GRV0001 */, 0,
71 PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
73 * An emu8000 does not give us other than the first
76 { 0x0100561e /* GRV0001 */, 0,
77 PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
79 * An emu8000 does not give us other than the first
82 { 0x26008c0e /* SB16 */, 0x21008c0e,
83 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
84 { 0x42008c0e /* SB32(CTL0042) */, 0x21008c0e,
85 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
86 { 0x44008c0e /* SB32(CTL0044) */, 0x21008c0e,
87 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
88 { 0x49008c0e /* SB32(CTL0049) */, 0x21008c0e,
89 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
90 { 0xf1008c0e /* SB32(CTL00f1) */, 0x21008c0e,
91 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
92 { 0xc1008c0e /* SB64(CTL00c1) */, 0x22008c0e,
93 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
94 { 0xc5008c0e /* SB64(CTL00c5) */, 0x22008c0e,
95 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
96 { 0xe4008c0e /* SB64(CTL00e4) */, 0x22008c0e,
97 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
103 /* Some NEC PnP cards have 9 bytes serial code. */
104 static pnp_id necids[] = {
105 {0x4180a3b8, 0xffffffff, 0x00}, /* PC-9801CB-B04 (NEC8041) */
106 {0x5181a3b8, 0xffffffff, 0x46}, /* PC-9821CB2-B04(NEC8151) */
107 {0x5182a3b8, 0xffffffff, 0xb8}, /* PC-9801-XX (NEC8251) */
108 {0x9181a3b8, 0xffffffff, 0x00}, /* PC-9801-120 (NEC8191) */
115 * these entries are initialized using the autoconfig menu
116 * The struct is invalid (and must be initialized) if the first
117 * CSN is zero. The init code fills invalid entries with CSN 255
118 * which is not a supported value.
121 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
126 /* The READ_DATA port that we are using currently */
127 static int pnp_rd_port;
129 static void pnp_send_initiation_key(void);
130 static int pnp_get_serial(pnp_id *p);
131 static int pnp_isolation_protocol(device_t parent);
134 pnp_eisaformat(u_int32_t id)
136 u_int8_t *data = (u_int8_t *) &id;
137 static char idbuf[8];
138 const char hextoascii[] = "0123456789abcdef";
140 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
141 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
142 idbuf[2] = '@' + (data[1] & 0x1f);
143 idbuf[3] = hextoascii[(data[2] >> 4)];
144 idbuf[4] = hextoascii[(data[2] & 0xf)];
145 idbuf[5] = hextoascii[(data[3] >> 4)];
146 idbuf[6] = hextoascii[(data[3] & 0xf)];
152 pnp_write(int d, u_char r)
154 outb (_PNP_ADDRESS, d);
155 outb (_PNP_WRITE_DATA, r);
163 outb (_PNP_ADDRESS, d);
164 return (inb(3 | (pnp_rd_port <<2)));
170 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
174 pnp_send_initiation_key()
179 outb(_PNP_ADDRESS, 0);
180 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
183 outb(_PNP_ADDRESS, cur);
185 for (i = 1; i < 32; i++) {
186 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
187 outb(_PNP_ADDRESS, cur);
193 * Get the device's serial number. Returns 1 if the serial is valid.
196 pnp_get_serial(pnp_id *p)
198 int i, bit, valid = 0, sum = 0x6a;
199 u_char *data = (u_char *)p;
201 bzero(data, sizeof(char) * 9);
202 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
203 for (i = 0; i < 72; i++) {
204 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
205 DELAY(250); /* Delay 250 usec */
207 /* Can't Short Circuit the next evaluation, so 'and' is last */
208 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
209 DELAY(250); /* Delay 250 usec */
211 valid = valid || bit;
215 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
217 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
220 valid = valid && (data[8] == sum);
226 * Fill's the buffer with resource info from the device.
227 * Returns the number of characters read.
230 pnp_get_resource_info(u_char *buffer, int len)
236 for (i = 0; i < len; i++) {
237 outb(_PNP_ADDRESS, PNP_STATUS);
238 for (j = 0; j < 100; j++) {
239 if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
244 printf("PnP device failed to report resource data\n");
247 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
248 temp = inb((pnp_rd_port << 2) | 0x3);
258 * write_pnp_parms initializes a logical device with the parms
259 * in d, and then activates the board if the last parameter is 1.
263 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
267 pnp_write (SET_LDN, ldn );
268 i = pnp_read(SET_LDN) ;
270 printf("Warning: LDN %d does not exist\n", ldn);
272 for (i = 0; i < 8; i++) {
273 pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
274 pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
276 for (i = 0; i < 4; i++) {
277 pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
278 pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
279 pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
280 pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
281 pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
283 for (i = 0; i < 2; i++) {
284 pnp_write(IRQ_CONFIG + i*2 , d->irq[i] );
285 pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
286 pnp_write(DRQ_CONFIG + i, d->drq[i] );
289 * store parameters read into the current kernel
290 * so manual editing next time is easier
292 for (i = 0 ; i < MAX_PNP_LDN; i++) {
293 if (pnp_ldn_overrides[i].csn == d->csn &&
294 pnp_ldn_overrides[i].ldn == ldn) {
295 d->flags = pnp_ldn_overrides[i].flags ;
296 pnp_ldn_overrides[i] = *d ;
298 } else if (pnp_ldn_overrides[i].csn < 1 ||
299 pnp_ldn_overrides[i].csn == 255)
302 if (i== MAX_PNP_LDN && empty != -1)
303 pnp_ldn_overrides[empty] = *d;
306 * Here should really perform the range check, and
307 * return a failure if not successful.
309 pnp_write (IO_RANGE_CHECK, 0);
310 DELAY(1000); /* XXX is it really necessary ? */
311 pnp_write (ACTIVATE, d->enable ? 1 : 0);
312 DELAY(1000); /* XXX is it really necessary ? */
318 * This function is called after the bus has assigned resource
319 * locations for a logical device.
322 pnp_set_config(void *arg, struct isa_config *config, int enable)
324 int csn = ((struct pnp_set_config_arg *) arg)->csn;
325 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
329 * First put all cards into Sleep state with the initiation
330 * key, then put our card into Config state.
332 pnp_send_initiation_key();
333 pnp_write(PNP_WAKE, csn);
336 * Select our logical device so that we can program it.
338 pnp_write(PNP_SET_LDN, ldn);
341 * Now program the resources.
343 for (i = 0; i < config->ic_nmem; i++) {
344 u_int32_t start = config->ic_mem[i].ir_start;
345 u_int32_t size = config->ic_mem[i].ir_size;
347 panic("pnp_set_config: bogus memory assignment");
348 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
349 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
350 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
351 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
353 for (; i < ISA_NMEM; i++) {
354 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
355 pnp_write(PNP_MEM_BASE_LOW(i), 0);
356 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
357 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
360 for (i = 0; i < config->ic_nport; i++) {
361 u_int32_t start = config->ic_port[i].ir_start;
362 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
363 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
365 for (; i < ISA_NPORT; i++) {
366 pnp_write(PNP_IO_BASE_HIGH(i), 0);
367 pnp_write(PNP_IO_BASE_LOW(i), 0);
370 for (i = 0; i < config->ic_nirq; i++) {
371 int irq = ffs(config->ic_irqmask[i]) - 1;
372 pnp_write(PNP_IRQ_LEVEL(i), irq);
373 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
375 for (; i < ISA_NIRQ; i++) {
377 * IRQ 0 is not a valid interrupt selection and
378 * represents no interrupt selection.
380 pnp_write(PNP_IRQ_LEVEL(i), 0);
383 for (i = 0; i < config->ic_ndrq; i++) {
384 int drq = ffs(config->ic_drqmask[i]) - 1;
385 pnp_write(PNP_DMA_CHANNEL(i), drq);
387 for (; i < ISA_NDRQ; i++) {
389 * DMA channel 4, the cascade channel is used to
390 * indicate no DMA channel is active.
392 pnp_write(PNP_DMA_CHANNEL(i), 4);
395 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
398 * Wake everyone up again, we are finished.
400 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
404 * Process quirks for a logical device.. The card must be in Config state.
407 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id,
408 int ldn, struct isa_config *config)
410 struct pnp_quirk *qp;
412 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
413 if (qp->vendor_id == vendor_id
414 && (qp->logical_id == 0
415 || qp->logical_id == logical_id)) {
417 case PNP_QUIRK_WRITE_REG:
418 pnp_write(PNP_SET_LDN, ldn);
419 pnp_write(qp->arg1, qp->arg2);
421 case PNP_QUIRK_EXTRA_IO:
426 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
427 config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
428 config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
432 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
433 config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
434 config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
444 * Scan Resource Data for Logical Devices.
446 * This function exits as soon as it gets an error reading *ANY*
447 * Resource Data or it reaches the end of Resource Data. In the first
448 * case the return value will be TRUE, FALSE otherwise.
451 pnp_create_devices(device_t parent, pnp_id *p, int csn,
452 u_char *resources, int len)
454 u_char tag, *resp, *resinfo, *startres = 0;
455 int large_len, scanning = len, retval = FALSE;
456 u_int32_t logical_id;
460 struct pnp_set_config_arg *csnldn;
465 while (scanning > 0) {
468 if (PNP_RES_TYPE(tag) != 0) {
474 large_len = resp[0] + (resp[1] << 8);
477 if (scanning < large_len) {
483 scanning -= large_len;
485 if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
486 if (large_len > sizeof(buf) - 1)
487 large_len = sizeof(buf) - 1;
488 bcopy(resinfo, buf, large_len);
491 * Trim trailing spaces.
493 while (buf[large_len-1] == ' ')
495 buf[large_len] = '\0';
498 device_set_desc_copy(dev, desc);
506 if (scanning < PNP_SRES_LEN(tag)) {
511 resp += PNP_SRES_LEN(tag);
512 scanning -= PNP_SRES_LEN(tag);;
514 switch (PNP_SRES_NUM(tag)) {
515 case PNP_TAG_LOGICAL_DEVICE:
517 * Parse the resources for the previous
518 * logical device (if any).
521 pnp_parse_resources(dev, startres,
522 resinfo - startres - 1, ldn);
528 * A new logical device. Scan for end of
531 bcopy(resinfo, &logical_id, 4);
532 pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
534 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
536 device_set_desc_copy(dev, desc);
537 isa_set_vendorid(dev, p->vendor_id);
538 isa_set_serial(dev, p->serial);
539 isa_set_logicalid(dev, logical_id);
540 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_WAITOK);
543 ISA_SET_CONFIG_CALLBACK(parent, dev,
544 pnp_set_config, csnldn);
551 device_printf(parent,
552 "malformed resources\n");
556 pnp_parse_resources(dev, startres,
557 resinfo - startres - 1, ldn);
564 /* Skip this resource */
573 * Read 'amount' bytes of resources from the card, allocating memory
574 * as needed. If a buffer is already available, it should be passed in
575 * '*resourcesp' and its length in '*spacep'. The number of resource
576 * bytes already in the buffer should be passed in '*lenp'. The memory
577 * allocated will be returned in '*resourcesp' with its size and the
578 * number of bytes of resources in '*spacep' and '*lenp' respectively.
581 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
583 u_char *resources = *resourcesp;
590 resources = malloc(space, M_TEMP, M_WAITOK);
593 if (len + amount > space) {
595 while (len + amount > space + extra)
597 newres = malloc(space + extra, M_TEMP, M_WAITOK);
598 bcopy(resources, newres, len);
599 free(resources, M_TEMP);
604 if (pnp_get_resource_info(resources + len, amount) != amount)
608 *resourcesp = resources;
616 * Read all resources from the card, allocating memory as needed. If a
617 * buffer is already available, it should be passed in '*resourcesp'
618 * and its length in '*spacep'. The memory allocated will be returned
619 * in '*resourcesp' with its size and the number of bytes of resources
620 * in '*spacep' and '*lenp' respectively.
623 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
625 u_char *resources = *resourcesp;
634 error = pnp_read_bytes(1, &resources, &space, &len);
637 tag = resources[len-1];
638 if (PNP_RES_TYPE(tag) == 0) {
640 * Small resource, read contents.
642 error = pnp_read_bytes(PNP_SRES_LEN(tag),
643 &resources, &space, &len);
646 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
650 * Large resource, read length and contents.
652 error = pnp_read_bytes(2, &resources, &space, &len);
655 error = pnp_read_bytes(resources[len-2]
656 + (resources[len-1] << 8),
657 &resources, &space, &len);
664 *resourcesp = resources;
671 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
672 * value (caller should try multiple READ_DATA locations before giving
673 * up). Upon exiting, all cards are aware that they should use
674 * pnp_rd_port as the READ_DATA port.
676 * In the first pass, a csn is assigned to each board and pnp_id's
677 * are saved to an array, pnp_devices. In the second pass, each
678 * card is woken up and the device configuration is called.
681 pnp_isolation_protocol(device_t parent)
686 u_char *resources = 0;
695 * Put all cards into the Sleep state so that we can clear
698 pnp_send_initiation_key();
701 * Clear the CSN for all cards.
703 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
706 * Move all cards to the Isolation state.
708 pnp_write(PNP_WAKE, 0);
711 * Tell them where the read point is going to be this time.
713 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
715 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
717 * Start the serial isolation protocol.
719 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
720 DELAY(1000); /* Delay 1 msec */
722 if (pnp_get_serial(&id)) {
724 * We have read the id from a card
725 * successfully. The card which won the
726 * isolation protocol will be in Isolation
727 * mode and all others will be in Sleep.
728 * Program the CSN of the isolated card
729 * (taking it to Config state) and read its
730 * resources, creating devices as we find
731 * logical devices on the card.
733 pnp_write(PNP_SET_CSN, csn);
736 printf("PnP Vendor ID = %x\n", id.vendor_id);
737 /* Check for NEC PnP (9 bytes serial). */
738 for (n = necpnp = 0; necids[n].vendor_id; n++) {
739 if (id.vendor_id == necids[n].vendor_id) {
746 printf("It seems to NEC-PnP card (%s).\n",
747 pnp_eisaformat(id.vendor_id));
748 /* Read dummy 9 bytes serial area. */
749 pnp_get_resource_info(buffer, 9);
752 printf("It seems to Normal-ISA-PnP card (%s).\n",
753 pnp_eisaformat(id.vendor_id));
756 printf("Reading PnP configuration for %s.\n",
757 pnp_eisaformat(id.vendor_id));
759 error = pnp_read_resources(&resources,
764 pnp_create_devices(parent, &id, csn,
771 * Put this card back to the Sleep state and
772 * simultaneously move all cards which don't have a
773 * CSN yet to Isolation state.
775 pnp_write(PNP_WAKE, 0);
779 * Unless we have chosen the wrong read port, all cards will
780 * be in Sleep state. Put them back into WaitForKey for
781 * now. Their resources will be programmed later.
783 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
789 free(resources, M_TEMP);
798 * autoconfiguration of pnp devices. This routine just runs the
799 * isolation protocol over several ports, until one is successful.
801 * may be called more than once ?
806 pnp_identify(driver_t *driver, device_t parent)
811 if (pnp_ldn_overrides[0].csn == 0) {
813 printf("Initializing PnP override table\n");
814 bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
815 pnp_ldn_overrides[0].csn = 255 ;
819 /* Try various READ_DATA ports from 0x203-0x3ff */
820 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
822 printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
824 num_pnp_devs = pnp_isolation_protocol(parent);
830 static device_method_t pnp_methods[] = {
831 /* Device interface */
832 DEVMETHOD(device_identify, pnp_identify),
837 static driver_t pnp_driver = {
843 static devclass_t pnp_devclass;
845 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);