2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1996, Sujal M. Patel
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
39 #include <sys/endian.h>
40 #include <sys/malloc.h>
41 #include <isa/isavar.h>
42 #include <isa/pnpreg.h>
43 #include <isa/pnpvar.h>
44 #include <machine/bus.h>
46 typedef struct _pnp_id {
52 struct pnp_set_config_arg {
53 int csn; /* Card number to configure */
54 int ldn; /* Logical device on card */
58 uint32_t vendor_id; /* Vendor of the card */
59 uint32_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 */
67 struct pnp_quirk pnp_quirks[] = {
69 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
71 * XXX need to know the logical device id.
73 { 0x0100561e /* GRV0001 */, 0,
74 PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
76 * An emu8000 does not give us other than the first
79 { 0x26008c0e /* SB16 */, 0x21008c0e,
80 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
81 { 0x42008c0e /* SB32(CTL0042) */, 0x21008c0e,
82 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
83 { 0x44008c0e /* SB32(CTL0044) */, 0x21008c0e,
84 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
85 { 0x49008c0e /* SB32(CTL0049) */, 0x21008c0e,
86 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
87 { 0xf1008c0e /* SB32(CTL00f1) */, 0x21008c0e,
88 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
89 { 0xc1008c0e /* SB64(CTL00c1) */, 0x22008c0e,
90 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
91 { 0xc5008c0e /* SB64(CTL00c5) */, 0x22008c0e,
92 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
93 { 0xe4008c0e /* SB64(CTL00e4) */, 0x22008c0e,
94 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
99 /* The READ_DATA port that we are using currently */
100 static int pnp_rd_port;
102 static void pnp_send_initiation_key(void);
103 static int pnp_get_serial(pnp_id *p);
104 static int pnp_isolation_protocol(device_t parent);
107 pnp_write(int d, u_char r)
109 outb (_PNP_ADDRESS, d);
110 outb (_PNP_WRITE_DATA, r);
114 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
118 pnp_send_initiation_key()
123 outb(_PNP_ADDRESS, 0);
124 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
127 outb(_PNP_ADDRESS, cur);
129 for (i = 1; i < 32; i++) {
130 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
131 outb(_PNP_ADDRESS, cur);
137 * Get the device's serial number. Returns 1 if the serial is valid.
140 pnp_get_serial(pnp_id *p)
142 int i, bit, valid = 0, sum = 0x6a;
143 u_char *data = (u_char *)p;
145 bzero(data, sizeof(char) * 9);
146 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
147 for (i = 0; i < 72; i++) {
148 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
149 DELAY(250); /* Delay 250 usec */
151 /* Can't Short Circuit the next evaluation, so 'and' is last */
152 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
153 DELAY(250); /* Delay 250 usec */
155 valid = valid || bit;
158 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
159 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
162 valid = valid && (data[8] == sum);
168 * Fill's the buffer with resource info from the device.
169 * Returns the number of characters read.
172 pnp_get_resource_info(u_char *buffer, int len)
178 for (i = 0; i < len; i++) {
179 outb(_PNP_ADDRESS, PNP_STATUS);
180 for (j = 0; j < 100; j++) {
181 if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
186 printf("PnP device failed to report resource data\n");
189 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
190 temp = inb((pnp_rd_port << 2) | 0x3);
199 * This function is called after the bus has assigned resource
200 * locations for a logical device.
203 pnp_set_config(void *arg, struct isa_config *config, int enable)
205 int csn = ((struct pnp_set_config_arg *) arg)->csn;
206 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
210 * First put all cards into Sleep state with the initiation
211 * key, then put our card into Config state.
213 pnp_send_initiation_key();
214 pnp_write(PNP_WAKE, csn);
217 * Select our logical device so that we can program it.
219 pnp_write(PNP_SET_LDN, ldn);
222 * Constrain the number of resources we will try to program
224 if (config->ic_nmem > ISA_PNP_NMEM) {
225 printf("too many ISA memory ranges (%d > %d)\n",
226 config->ic_nmem, ISA_PNP_NMEM);
227 config->ic_nmem = ISA_PNP_NMEM;
229 if (config->ic_nport > ISA_PNP_NPORT) {
230 printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport,
232 config->ic_nport = ISA_PNP_NPORT;
234 if (config->ic_nirq > ISA_PNP_NIRQ) {
235 printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq,
237 config->ic_nirq = ISA_PNP_NIRQ;
239 if (config->ic_ndrq > ISA_PNP_NDRQ) {
240 printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq,
242 config->ic_ndrq = ISA_PNP_NDRQ;
246 * Now program the resources.
248 for (i = 0; i < config->ic_nmem; i++) {
252 /* XXX: should handle memory control register, 32 bit memory */
253 if (config->ic_mem[i].ir_size == 0) {
254 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
255 pnp_write(PNP_MEM_BASE_LOW(i), 0);
256 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
257 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
259 start = config->ic_mem[i].ir_start;
260 size = config->ic_mem[i].ir_size;
262 panic("pnp_set_config: bogus memory assignment");
263 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
264 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
265 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
266 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
269 for (; i < ISA_PNP_NMEM; i++) {
270 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
271 pnp_write(PNP_MEM_BASE_LOW(i), 0);
272 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
273 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
276 for (i = 0; i < config->ic_nport; i++) {
279 if (config->ic_port[i].ir_size == 0) {
280 pnp_write(PNP_IO_BASE_HIGH(i), 0);
281 pnp_write(PNP_IO_BASE_LOW(i), 0);
283 start = config->ic_port[i].ir_start;
284 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
285 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
288 for (; i < ISA_PNP_NPORT; i++) {
289 pnp_write(PNP_IO_BASE_HIGH(i), 0);
290 pnp_write(PNP_IO_BASE_LOW(i), 0);
293 for (i = 0; i < config->ic_nirq; i++) {
296 /* XXX: interrupt type */
297 if (config->ic_irqmask[i] == 0) {
298 pnp_write(PNP_IRQ_LEVEL(i), 0);
299 pnp_write(PNP_IRQ_TYPE(i), 2);
301 irq = ffs(config->ic_irqmask[i]) - 1;
302 pnp_write(PNP_IRQ_LEVEL(i), irq);
303 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
306 for (; i < ISA_PNP_NIRQ; i++) {
308 * IRQ 0 is not a valid interrupt selection and
309 * represents no interrupt selection.
311 pnp_write(PNP_IRQ_LEVEL(i), 0);
312 pnp_write(PNP_IRQ_TYPE(i), 2);
315 for (i = 0; i < config->ic_ndrq; i++) {
318 if (config->ic_drqmask[i] == 0) {
319 pnp_write(PNP_DMA_CHANNEL(i), 4);
321 drq = ffs(config->ic_drqmask[i]) - 1;
322 pnp_write(PNP_DMA_CHANNEL(i), drq);
325 for (; i < ISA_PNP_NDRQ; i++) {
327 * DMA channel 4, the cascade channel is used to
328 * indicate no DMA channel is active.
330 pnp_write(PNP_DMA_CHANNEL(i), 4);
333 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
336 * Wake everyone up again, we are finished.
338 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
342 * Process quirks for a logical device.. The card must be in Config state.
345 pnp_check_quirks(uint32_t vendor_id, uint32_t logical_id, int ldn,
346 struct isa_config *config)
348 struct pnp_quirk *qp;
350 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
351 if (qp->vendor_id == vendor_id
352 && (qp->logical_id == 0 || qp->logical_id == logical_id)) {
354 case PNP_QUIRK_WRITE_REG:
355 pnp_write(PNP_SET_LDN, ldn);
356 pnp_write(qp->arg1, qp->arg2);
358 case PNP_QUIRK_EXTRA_IO:
363 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
364 config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
365 config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
369 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
370 config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
371 config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
380 * Scan Resource Data for Logical Devices.
382 * This function exits as soon as it gets an error reading *ANY*
383 * Resource Data or it reaches the end of Resource Data. In the first
384 * case the return value will be TRUE, FALSE otherwise.
387 pnp_create_devices(device_t parent, pnp_id *p, int csn,
388 u_char *resources, int len)
390 u_char tag, *resp, *resinfo, *startres = NULL;
391 int large_len, scanning = len, retval = FALSE;
395 struct pnp_set_config_arg *csnldn;
400 while (scanning > 0) {
403 if (PNP_RES_TYPE(tag) != 0) {
409 large_len = resp[0] + (resp[1] << 8);
412 if (scanning < large_len) {
418 scanning -= large_len;
420 if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
423 * This is an optional device
424 * identifier string. Skip it
429 /* else mandately card identifier string */
430 if (large_len > sizeof(buf) - 1)
431 large_len = sizeof(buf) - 1;
432 bcopy(resinfo, buf, large_len);
435 * Trim trailing spaces.
437 while (buf[large_len-1] == ' ')
439 buf[large_len] = '\0';
448 if (scanning < PNP_SRES_LEN(tag)) {
453 resp += PNP_SRES_LEN(tag);
454 scanning -= PNP_SRES_LEN(tag);
456 switch (PNP_SRES_NUM(tag)) {
457 case PNP_TAG_LOGICAL_DEVICE:
459 * Parse the resources for the previous
460 * logical device (if any).
463 pnp_parse_resources(dev, startres,
464 resinfo - startres - 1, ldn);
470 * A new logical device. Scan for end of
473 bcopy(resinfo, &logical_id, 4);
474 pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
475 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
477 device_set_desc_copy(dev, desc);
479 device_set_desc_copy(dev,
480 pnp_eisaformat(logical_id));
481 isa_set_vendorid(dev, p->vendor_id);
482 isa_set_serial(dev, p->serial);
483 isa_set_logicalid(dev, logical_id);
484 isa_set_configattr(dev,
485 ISACFGATTR_CANDISABLE | ISACFGATTR_DYNAMIC);
486 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
488 device_printf(parent, "out of memory\n");
494 ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config,
496 isa_set_pnp_csn(dev, csn);
497 isa_set_pnp_ldn(dev, ldn);
504 device_printf(parent, "malformed resources\n");
508 pnp_parse_resources(dev, startres,
509 resinfo - startres - 1, ldn);
516 /* Skip this resource */
525 * Read 'amount' bytes of resources from the card, allocating memory
526 * as needed. If a buffer is already available, it should be passed in
527 * '*resourcesp' and its length in '*spacep'. The number of resource
528 * bytes already in the buffer should be passed in '*lenp'. The memory
529 * allocated will be returned in '*resourcesp' with its size and the
530 * number of bytes of resources in '*spacep' and '*lenp' respectively.
532 * XXX: Multiple problems here, we forget to free() stuff in one
533 * XXX: error return, and in another case we free (*resourcesp) but
534 * XXX: don't tell the caller.
537 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
539 u_char *resources = *resourcesp;
546 resources = malloc(space, M_TEMP, M_NOWAIT);
551 if (len + amount > space) {
553 while (len + amount > space + extra)
555 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
557 /* XXX: free resources */
560 bcopy(resources, newres, len);
561 free(resources, M_TEMP);
566 if (pnp_get_resource_info(resources + len, amount) != amount)
570 *resourcesp = resources;
578 * Read all resources from the card, allocating memory as needed. If a
579 * buffer is already available, it should be passed in '*resourcesp'
580 * and its length in '*spacep'. The memory allocated will be returned
581 * in '*resourcesp' with its size and the number of bytes of resources
582 * in '*spacep' and '*lenp' respectively.
585 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
587 u_char *resources = *resourcesp;
596 error = pnp_read_bytes(1, &resources, &space, &len);
599 tag = resources[len-1];
600 if (PNP_RES_TYPE(tag) == 0) {
602 * Small resource, read contents.
604 error = pnp_read_bytes(PNP_SRES_LEN(tag),
605 &resources, &space, &len);
608 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
612 * Large resource, read length and contents.
614 error = pnp_read_bytes(2, &resources, &space, &len);
617 error = pnp_read_bytes(resources[len-2]
618 + (resources[len-1] << 8), &resources, &space,
626 *resourcesp = resources;
633 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
634 * value (caller should try multiple READ_DATA locations before giving
635 * up). Upon exiting, all cards are aware that they should use
636 * pnp_rd_port as the READ_DATA port.
638 * In the first pass, a csn is assigned to each board and pnp_id's
639 * are saved to an array, pnp_devices. In the second pass, each
640 * card is woken up and the device configuration is called.
643 pnp_isolation_protocol(device_t parent)
648 u_char *resources = NULL;
653 * Put all cards into the Sleep state so that we can clear
656 pnp_send_initiation_key();
659 * Clear the CSN for all cards.
661 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
664 * Move all cards to the Isolation state.
666 pnp_write(PNP_WAKE, 0);
669 * Tell them where the read point is going to be this time.
671 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
673 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
675 * Start the serial isolation protocol.
677 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
678 DELAY(1000); /* Delay 1 msec */
680 if (pnp_get_serial(&id)) {
682 * We have read the id from a card
683 * successfully. The card which won the
684 * isolation protocol will be in Isolation
685 * mode and all others will be in Sleep.
686 * Program the CSN of the isolated card
687 * (taking it to Config state) and read its
688 * resources, creating devices as we find
689 * logical devices on the card.
691 pnp_write(PNP_SET_CSN, csn);
693 printf("Reading PnP configuration for %s.\n",
694 pnp_eisaformat(id.vendor_id));
695 error = pnp_read_resources(&resources, &space, &len);
698 pnp_create_devices(parent, &id, csn, resources, len);
704 * Put this card back to the Sleep state and
705 * simultaneously move all cards which don't have a
706 * CSN yet to Isolation state.
708 pnp_write(PNP_WAKE, 0);
712 * Unless we have chosen the wrong read port, all cards will
713 * be in Sleep state. Put them back into WaitForKey for
714 * now. Their resources will be programmed later.
716 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
722 free(resources, M_TEMP);
731 * autoconfiguration of pnp devices. This routine just runs the
732 * isolation protocol over several ports, until one is successful.
734 * may be called more than once ?
739 pnp_identify(driver_t *driver, device_t parent)
743 /* Try various READ_DATA ports from 0x203-0x3ff */
744 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
746 printf("pnp_identify: Trying Read_Port at %x\n",
747 (pnp_rd_port << 2) | 0x3);
749 num_pnp_devs = pnp_isolation_protocol(parent);
754 printf("PNP Identify complete\n");
757 static device_method_t pnp_methods[] = {
758 /* Device interface */
759 DEVMETHOD(device_identify, pnp_identify),
764 static driver_t pnp_driver = {
770 static devclass_t pnp_devclass;
772 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);