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_eisaformat(uint32_t id)
110 static char idbuf[8];
111 const char hextoascii[] = "0123456789abcdef";
114 data = (uint8_t *)&id;
115 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
116 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
117 idbuf[2] = '@' + (data[1] & 0x1f);
118 idbuf[3] = hextoascii[(data[2] >> 4)];
119 idbuf[4] = hextoascii[(data[2] & 0xf)];
120 idbuf[5] = hextoascii[(data[3] >> 4)];
121 idbuf[6] = hextoascii[(data[3] & 0xf)];
127 pnp_write(int d, u_char r)
129 outb (_PNP_ADDRESS, d);
130 outb (_PNP_WRITE_DATA, r);
134 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
138 pnp_send_initiation_key()
143 outb(_PNP_ADDRESS, 0);
144 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
147 outb(_PNP_ADDRESS, cur);
149 for (i = 1; i < 32; i++) {
150 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
151 outb(_PNP_ADDRESS, cur);
157 * Get the device's serial number. Returns 1 if the serial is valid.
160 pnp_get_serial(pnp_id *p)
162 int i, bit, valid = 0, sum = 0x6a;
163 u_char *data = (u_char *)p;
165 bzero(data, sizeof(char) * 9);
166 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
167 for (i = 0; i < 72; i++) {
168 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
169 DELAY(250); /* Delay 250 usec */
171 /* Can't Short Circuit the next evaluation, so 'and' is last */
172 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
173 DELAY(250); /* Delay 250 usec */
175 valid = valid || bit;
178 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
179 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
182 valid = valid && (data[8] == sum);
188 * Fill's the buffer with resource info from the device.
189 * Returns the number of characters read.
192 pnp_get_resource_info(u_char *buffer, int len)
198 for (i = 0; i < len; i++) {
199 outb(_PNP_ADDRESS, PNP_STATUS);
200 for (j = 0; j < 100; j++) {
201 if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
206 printf("PnP device failed to report resource data\n");
209 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
210 temp = inb((pnp_rd_port << 2) | 0x3);
219 * This function is called after the bus has assigned resource
220 * locations for a logical device.
223 pnp_set_config(void *arg, struct isa_config *config, int enable)
225 int csn = ((struct pnp_set_config_arg *) arg)->csn;
226 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
230 * First put all cards into Sleep state with the initiation
231 * key, then put our card into Config state.
233 pnp_send_initiation_key();
234 pnp_write(PNP_WAKE, csn);
237 * Select our logical device so that we can program it.
239 pnp_write(PNP_SET_LDN, ldn);
242 * Constrain the number of resources we will try to program
244 if (config->ic_nmem > ISA_PNP_NMEM) {
245 printf("too many ISA memory ranges (%d > %d)\n",
246 config->ic_nmem, ISA_PNP_NMEM);
247 config->ic_nmem = ISA_PNP_NMEM;
249 if (config->ic_nport > ISA_PNP_NPORT) {
250 printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport,
252 config->ic_nport = ISA_PNP_NPORT;
254 if (config->ic_nirq > ISA_PNP_NIRQ) {
255 printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq,
257 config->ic_nirq = ISA_PNP_NIRQ;
259 if (config->ic_ndrq > ISA_PNP_NDRQ) {
260 printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq,
262 config->ic_ndrq = ISA_PNP_NDRQ;
266 * Now program the resources.
268 for (i = 0; i < config->ic_nmem; i++) {
272 /* XXX: should handle memory control register, 32 bit memory */
273 if (config->ic_mem[i].ir_size == 0) {
274 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
275 pnp_write(PNP_MEM_BASE_LOW(i), 0);
276 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
277 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
279 start = config->ic_mem[i].ir_start;
280 size = config->ic_mem[i].ir_size;
282 panic("pnp_set_config: bogus memory assignment");
283 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
284 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
285 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
286 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
289 for (; i < ISA_PNP_NMEM; i++) {
290 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
291 pnp_write(PNP_MEM_BASE_LOW(i), 0);
292 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
293 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
296 for (i = 0; i < config->ic_nport; i++) {
299 if (config->ic_port[i].ir_size == 0) {
300 pnp_write(PNP_IO_BASE_HIGH(i), 0);
301 pnp_write(PNP_IO_BASE_LOW(i), 0);
303 start = config->ic_port[i].ir_start;
304 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
305 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
308 for (; i < ISA_PNP_NPORT; i++) {
309 pnp_write(PNP_IO_BASE_HIGH(i), 0);
310 pnp_write(PNP_IO_BASE_LOW(i), 0);
313 for (i = 0; i < config->ic_nirq; i++) {
316 /* XXX: interrupt type */
317 if (config->ic_irqmask[i] == 0) {
318 pnp_write(PNP_IRQ_LEVEL(i), 0);
319 pnp_write(PNP_IRQ_TYPE(i), 2);
321 irq = ffs(config->ic_irqmask[i]) - 1;
322 pnp_write(PNP_IRQ_LEVEL(i), irq);
323 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
326 for (; i < ISA_PNP_NIRQ; i++) {
328 * IRQ 0 is not a valid interrupt selection and
329 * represents no interrupt selection.
331 pnp_write(PNP_IRQ_LEVEL(i), 0);
332 pnp_write(PNP_IRQ_TYPE(i), 2);
335 for (i = 0; i < config->ic_ndrq; i++) {
338 if (config->ic_drqmask[i] == 0) {
339 pnp_write(PNP_DMA_CHANNEL(i), 4);
341 drq = ffs(config->ic_drqmask[i]) - 1;
342 pnp_write(PNP_DMA_CHANNEL(i), drq);
345 for (; i < ISA_PNP_NDRQ; i++) {
347 * DMA channel 4, the cascade channel is used to
348 * indicate no DMA channel is active.
350 pnp_write(PNP_DMA_CHANNEL(i), 4);
353 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
356 * Wake everyone up again, we are finished.
358 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
362 * Process quirks for a logical device.. The card must be in Config state.
365 pnp_check_quirks(uint32_t vendor_id, uint32_t logical_id, int ldn,
366 struct isa_config *config)
368 struct pnp_quirk *qp;
370 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
371 if (qp->vendor_id == vendor_id
372 && (qp->logical_id == 0 || qp->logical_id == logical_id)) {
374 case PNP_QUIRK_WRITE_REG:
375 pnp_write(PNP_SET_LDN, ldn);
376 pnp_write(qp->arg1, qp->arg2);
378 case PNP_QUIRK_EXTRA_IO:
383 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
384 config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
385 config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
389 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
390 config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
391 config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
400 * Scan Resource Data for Logical Devices.
402 * This function exits as soon as it gets an error reading *ANY*
403 * Resource Data or it reaches the end of Resource Data. In the first
404 * case the return value will be TRUE, FALSE otherwise.
407 pnp_create_devices(device_t parent, pnp_id *p, int csn,
408 u_char *resources, int len)
410 u_char tag, *resp, *resinfo, *startres = NULL;
411 int large_len, scanning = len, retval = FALSE;
415 struct pnp_set_config_arg *csnldn;
420 while (scanning > 0) {
423 if (PNP_RES_TYPE(tag) != 0) {
429 large_len = resp[0] + (resp[1] << 8);
432 if (scanning < large_len) {
438 scanning -= large_len;
440 if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
443 * This is an optional device
444 * identifier string. Skip it
449 /* else mandately card identifier string */
450 if (large_len > sizeof(buf) - 1)
451 large_len = sizeof(buf) - 1;
452 bcopy(resinfo, buf, large_len);
455 * Trim trailing spaces.
457 while (buf[large_len-1] == ' ')
459 buf[large_len] = '\0';
468 if (scanning < PNP_SRES_LEN(tag)) {
473 resp += PNP_SRES_LEN(tag);
474 scanning -= PNP_SRES_LEN(tag);
476 switch (PNP_SRES_NUM(tag)) {
477 case PNP_TAG_LOGICAL_DEVICE:
479 * Parse the resources for the previous
480 * logical device (if any).
483 pnp_parse_resources(dev, startres,
484 resinfo - startres - 1, ldn);
490 * A new logical device. Scan for end of
493 bcopy(resinfo, &logical_id, 4);
494 pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
495 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
497 device_set_desc_copy(dev, desc);
499 device_set_desc_copy(dev,
500 pnp_eisaformat(logical_id));
501 isa_set_vendorid(dev, p->vendor_id);
502 isa_set_serial(dev, p->serial);
503 isa_set_logicalid(dev, logical_id);
504 isa_set_configattr(dev,
505 ISACFGATTR_CANDISABLE | ISACFGATTR_DYNAMIC);
506 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
508 device_printf(parent, "out of memory\n");
514 ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config,
516 isa_set_pnp_csn(dev, csn);
517 isa_set_pnp_ldn(dev, ldn);
524 device_printf(parent, "malformed resources\n");
528 pnp_parse_resources(dev, startres,
529 resinfo - startres - 1, ldn);
536 /* Skip this resource */
545 * Read 'amount' bytes of resources from the card, allocating memory
546 * as needed. If a buffer is already available, it should be passed in
547 * '*resourcesp' and its length in '*spacep'. The number of resource
548 * bytes already in the buffer should be passed in '*lenp'. The memory
549 * allocated will be returned in '*resourcesp' with its size and the
550 * number of bytes of resources in '*spacep' and '*lenp' respectively.
552 * XXX: Multiple problems here, we forget to free() stuff in one
553 * XXX: error return, and in another case we free (*resourcesp) but
554 * XXX: don't tell the caller.
557 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
559 u_char *resources = *resourcesp;
566 resources = malloc(space, M_TEMP, M_NOWAIT);
571 if (len + amount > space) {
573 while (len + amount > space + extra)
575 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
577 /* XXX: free resources */
580 bcopy(resources, newres, len);
581 free(resources, M_TEMP);
586 if (pnp_get_resource_info(resources + len, amount) != amount)
590 *resourcesp = resources;
598 * Read all resources from the card, allocating memory as needed. If a
599 * buffer is already available, it should be passed in '*resourcesp'
600 * and its length in '*spacep'. The memory allocated will be returned
601 * in '*resourcesp' with its size and the number of bytes of resources
602 * in '*spacep' and '*lenp' respectively.
605 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
607 u_char *resources = *resourcesp;
616 error = pnp_read_bytes(1, &resources, &space, &len);
619 tag = resources[len-1];
620 if (PNP_RES_TYPE(tag) == 0) {
622 * Small resource, read contents.
624 error = pnp_read_bytes(PNP_SRES_LEN(tag),
625 &resources, &space, &len);
628 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
632 * Large resource, read length and contents.
634 error = pnp_read_bytes(2, &resources, &space, &len);
637 error = pnp_read_bytes(resources[len-2]
638 + (resources[len-1] << 8), &resources, &space,
646 *resourcesp = resources;
653 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
654 * value (caller should try multiple READ_DATA locations before giving
655 * up). Upon exiting, all cards are aware that they should use
656 * pnp_rd_port as the READ_DATA port.
658 * In the first pass, a csn is assigned to each board and pnp_id's
659 * are saved to an array, pnp_devices. In the second pass, each
660 * card is woken up and the device configuration is called.
663 pnp_isolation_protocol(device_t parent)
668 u_char *resources = NULL;
673 * Put all cards into the Sleep state so that we can clear
676 pnp_send_initiation_key();
679 * Clear the CSN for all cards.
681 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
684 * Move all cards to the Isolation state.
686 pnp_write(PNP_WAKE, 0);
689 * Tell them where the read point is going to be this time.
691 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
693 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
695 * Start the serial isolation protocol.
697 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
698 DELAY(1000); /* Delay 1 msec */
700 if (pnp_get_serial(&id)) {
702 * We have read the id from a card
703 * successfully. The card which won the
704 * isolation protocol will be in Isolation
705 * mode and all others will be in Sleep.
706 * Program the CSN of the isolated card
707 * (taking it to Config state) and read its
708 * resources, creating devices as we find
709 * logical devices on the card.
711 pnp_write(PNP_SET_CSN, csn);
713 printf("Reading PnP configuration for %s.\n",
714 pnp_eisaformat(id.vendor_id));
715 error = pnp_read_resources(&resources, &space, &len);
718 pnp_create_devices(parent, &id, csn, resources, len);
724 * Put this card back to the Sleep state and
725 * simultaneously move all cards which don't have a
726 * CSN yet to Isolation state.
728 pnp_write(PNP_WAKE, 0);
732 * Unless we have chosen the wrong read port, all cards will
733 * be in Sleep state. Put them back into WaitForKey for
734 * now. Their resources will be programmed later.
736 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
742 free(resources, M_TEMP);
751 * autoconfiguration of pnp devices. This routine just runs the
752 * isolation protocol over several ports, until one is successful.
754 * may be called more than once ?
759 pnp_identify(driver_t *driver, device_t parent)
763 /* Try various READ_DATA ports from 0x203-0x3ff */
764 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
766 printf("pnp_identify: Trying Read_Port at %x\n",
767 (pnp_rd_port << 2) | 0x3);
769 num_pnp_devs = pnp_isolation_protocol(parent);
774 printf("PNP Identify complete\n");
777 static device_method_t pnp_methods[] = {
778 /* Device interface */
779 DEVMETHOD(device_identify, pnp_identify),
784 static driver_t pnp_driver = {
790 static devclass_t pnp_devclass;
792 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);