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 * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
37 #include <sys/endian.h>
38 #include <sys/malloc.h>
39 #include <isa/isavar.h>
40 #include <isa/pnpreg.h>
41 #include <isa/pnpvar.h>
42 #include <machine/bus.h>
44 typedef struct _pnp_id {
50 struct pnp_set_config_arg {
51 int csn; /* Card number to configure */
52 int ldn; /* Logical device on card */
56 uint32_t vendor_id; /* Vendor of the card */
57 uint32_t logical_id; /* ID of the device with quirk */
59 #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */
60 #define PNP_QUIRK_EXTRA_IO 2 /* Has extra io ports */
65 struct pnp_quirk pnp_quirks[] = {
67 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
69 * XXX need to know the logical device id.
71 { 0x0100561e /* GRV0001 */, 0,
72 PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
74 * An emu8000 does not give us other than the first
77 { 0x26008c0e /* SB16 */, 0x21008c0e,
78 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
79 { 0x42008c0e /* SB32(CTL0042) */, 0x21008c0e,
80 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
81 { 0x44008c0e /* SB32(CTL0044) */, 0x21008c0e,
82 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
83 { 0x49008c0e /* SB32(CTL0049) */, 0x21008c0e,
84 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
85 { 0xf1008c0e /* SB32(CTL00f1) */, 0x21008c0e,
86 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
87 { 0xc1008c0e /* SB64(CTL00c1) */, 0x22008c0e,
88 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
89 { 0xc5008c0e /* SB64(CTL00c5) */, 0x22008c0e,
90 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
91 { 0xe4008c0e /* SB64(CTL00e4) */, 0x22008c0e,
92 PNP_QUIRK_EXTRA_IO, 0x400, 0x800 },
97 /* The READ_DATA port that we are using currently */
98 static int pnp_rd_port;
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);
105 pnp_eisaformat(uint32_t id)
108 static char idbuf[8];
109 const char hextoascii[] = "0123456789abcdef";
112 data = (uint8_t *)&id;
113 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
114 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
115 idbuf[2] = '@' + (data[1] & 0x1f);
116 idbuf[3] = hextoascii[(data[2] >> 4)];
117 idbuf[4] = hextoascii[(data[2] & 0xf)];
118 idbuf[5] = hextoascii[(data[3] >> 4)];
119 idbuf[6] = hextoascii[(data[3] & 0xf)];
125 pnp_write(int d, u_char r)
127 outb (_PNP_ADDRESS, d);
128 outb (_PNP_WRITE_DATA, r);
132 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
136 pnp_send_initiation_key()
141 outb(_PNP_ADDRESS, 0);
142 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
145 outb(_PNP_ADDRESS, cur);
147 for (i = 1; i < 32; i++) {
148 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
149 outb(_PNP_ADDRESS, cur);
155 * Get the device's serial number. Returns 1 if the serial is valid.
158 pnp_get_serial(pnp_id *p)
160 int i, bit, valid = 0, sum = 0x6a;
161 u_char *data = (u_char *)p;
163 bzero(data, sizeof(char) * 9);
164 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
165 for (i = 0; i < 72; i++) {
166 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
167 DELAY(250); /* Delay 250 usec */
169 /* Can't Short Circuit the next evaluation, so 'and' is last */
170 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
171 DELAY(250); /* Delay 250 usec */
173 valid = valid || bit;
176 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
177 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
180 valid = valid && (data[8] == sum);
186 * Fill's the buffer with resource info from the device.
187 * Returns the number of characters read.
190 pnp_get_resource_info(u_char *buffer, int len)
196 for (i = 0; i < len; i++) {
197 outb(_PNP_ADDRESS, PNP_STATUS);
198 for (j = 0; j < 100; j++) {
199 if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
204 printf("PnP device failed to report resource data\n");
207 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
208 temp = inb((pnp_rd_port << 2) | 0x3);
217 * This function is called after the bus has assigned resource
218 * locations for a logical device.
221 pnp_set_config(void *arg, struct isa_config *config, int enable)
223 int csn = ((struct pnp_set_config_arg *) arg)->csn;
224 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
228 * First put all cards into Sleep state with the initiation
229 * key, then put our card into Config state.
231 pnp_send_initiation_key();
232 pnp_write(PNP_WAKE, csn);
235 * Select our logical device so that we can program it.
237 pnp_write(PNP_SET_LDN, ldn);
240 * Constrain the number of resources we will try to program
242 if (config->ic_nmem > ISA_PNP_NMEM) {
243 printf("too many ISA memory ranges (%d > %d)\n",
244 config->ic_nmem, ISA_PNP_NMEM);
245 config->ic_nmem = ISA_PNP_NMEM;
247 if (config->ic_nport > ISA_PNP_NPORT) {
248 printf("too many ISA I/O ranges (%d > %d)\n", config->ic_nport,
250 config->ic_nport = ISA_PNP_NPORT;
252 if (config->ic_nirq > ISA_PNP_NIRQ) {
253 printf("too many ISA IRQs (%d > %d)\n", config->ic_nirq,
255 config->ic_nirq = ISA_PNP_NIRQ;
257 if (config->ic_ndrq > ISA_PNP_NDRQ) {
258 printf("too many ISA DRQs (%d > %d)\n", config->ic_ndrq,
260 config->ic_ndrq = ISA_PNP_NDRQ;
264 * Now program the resources.
266 for (i = 0; i < config->ic_nmem; i++) {
270 /* XXX: should handle memory control register, 32 bit memory */
271 if (config->ic_mem[i].ir_size == 0) {
272 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
273 pnp_write(PNP_MEM_BASE_LOW(i), 0);
274 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
275 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
277 start = config->ic_mem[i].ir_start;
278 size = config->ic_mem[i].ir_size;
280 panic("pnp_set_config: bogus memory assignment");
281 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
282 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
283 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
284 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
287 for (; i < ISA_PNP_NMEM; i++) {
288 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
289 pnp_write(PNP_MEM_BASE_LOW(i), 0);
290 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
291 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
294 for (i = 0; i < config->ic_nport; i++) {
297 if (config->ic_port[i].ir_size == 0) {
298 pnp_write(PNP_IO_BASE_HIGH(i), 0);
299 pnp_write(PNP_IO_BASE_LOW(i), 0);
301 start = config->ic_port[i].ir_start;
302 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
303 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
306 for (; i < ISA_PNP_NPORT; i++) {
307 pnp_write(PNP_IO_BASE_HIGH(i), 0);
308 pnp_write(PNP_IO_BASE_LOW(i), 0);
311 for (i = 0; i < config->ic_nirq; i++) {
314 /* XXX: interrupt type */
315 if (config->ic_irqmask[i] == 0) {
316 pnp_write(PNP_IRQ_LEVEL(i), 0);
317 pnp_write(PNP_IRQ_TYPE(i), 2);
319 irq = ffs(config->ic_irqmask[i]) - 1;
320 pnp_write(PNP_IRQ_LEVEL(i), irq);
321 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
324 for (; i < ISA_PNP_NIRQ; i++) {
326 * IRQ 0 is not a valid interrupt selection and
327 * represents no interrupt selection.
329 pnp_write(PNP_IRQ_LEVEL(i), 0);
330 pnp_write(PNP_IRQ_TYPE(i), 2);
333 for (i = 0; i < config->ic_ndrq; i++) {
336 if (config->ic_drqmask[i] == 0) {
337 pnp_write(PNP_DMA_CHANNEL(i), 4);
339 drq = ffs(config->ic_drqmask[i]) - 1;
340 pnp_write(PNP_DMA_CHANNEL(i), drq);
343 for (; i < ISA_PNP_NDRQ; i++) {
345 * DMA channel 4, the cascade channel is used to
346 * indicate no DMA channel is active.
348 pnp_write(PNP_DMA_CHANNEL(i), 4);
351 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
354 * Wake everyone up again, we are finished.
356 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
360 * Process quirks for a logical device.. The card must be in Config state.
363 pnp_check_quirks(uint32_t vendor_id, uint32_t logical_id, int ldn,
364 struct isa_config *config)
366 struct pnp_quirk *qp;
368 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
369 if (qp->vendor_id == vendor_id
370 && (qp->logical_id == 0 || qp->logical_id == logical_id)) {
372 case PNP_QUIRK_WRITE_REG:
373 pnp_write(PNP_SET_LDN, ldn);
374 pnp_write(qp->arg1, qp->arg2);
376 case PNP_QUIRK_EXTRA_IO:
381 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
382 config->ic_port[config->ic_nport - 1].ir_start += qp->arg1;
383 config->ic_port[config->ic_nport - 1].ir_end += qp->arg1;
387 config->ic_port[config->ic_nport - 1] = config->ic_port[0];
388 config->ic_port[config->ic_nport - 1].ir_start += qp->arg2;
389 config->ic_port[config->ic_nport - 1].ir_end += qp->arg2;
398 * Scan Resource Data for Logical Devices.
400 * This function exits as soon as it gets an error reading *ANY*
401 * Resource Data or it reaches the end of Resource Data. In the first
402 * case the return value will be TRUE, FALSE otherwise.
405 pnp_create_devices(device_t parent, pnp_id *p, int csn,
406 u_char *resources, int len)
408 u_char tag, *resp, *resinfo, *startres = NULL;
409 int large_len, scanning = len, retval = FALSE;
413 struct pnp_set_config_arg *csnldn;
418 while (scanning > 0) {
421 if (PNP_RES_TYPE(tag) != 0) {
427 large_len = resp[0] + (resp[1] << 8);
430 if (scanning < large_len) {
436 scanning -= large_len;
438 if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
441 * This is an optional device
442 * identifier string. Skip it
447 /* else mandately card identifier string */
448 if (large_len > sizeof(buf) - 1)
449 large_len = sizeof(buf) - 1;
450 bcopy(resinfo, buf, large_len);
453 * Trim trailing spaces.
455 while (buf[large_len-1] == ' ')
457 buf[large_len] = '\0';
466 if (scanning < PNP_SRES_LEN(tag)) {
471 resp += PNP_SRES_LEN(tag);
472 scanning -= PNP_SRES_LEN(tag);
474 switch (PNP_SRES_NUM(tag)) {
475 case PNP_TAG_LOGICAL_DEVICE:
477 * Parse the resources for the previous
478 * logical device (if any).
481 pnp_parse_resources(dev, startres,
482 resinfo - startres - 1, ldn);
488 * A new logical device. Scan for end of
491 bcopy(resinfo, &logical_id, 4);
492 pnp_check_quirks(p->vendor_id, logical_id, ldn, NULL);
493 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
495 device_set_desc_copy(dev, desc);
497 device_set_desc_copy(dev,
498 pnp_eisaformat(logical_id));
499 isa_set_vendorid(dev, p->vendor_id);
500 isa_set_serial(dev, p->serial);
501 isa_set_logicalid(dev, logical_id);
502 isa_set_configattr(dev,
503 ISACFGATTR_CANDISABLE | ISACFGATTR_DYNAMIC);
504 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
506 device_printf(parent, "out of memory\n");
512 ISA_SET_CONFIG_CALLBACK(parent, dev, pnp_set_config,
514 isa_set_pnp_csn(dev, csn);
515 isa_set_pnp_ldn(dev, ldn);
522 device_printf(parent, "malformed resources\n");
526 pnp_parse_resources(dev, startres,
527 resinfo - startres - 1, ldn);
534 /* Skip this resource */
543 * Read 'amount' bytes of resources from the card, allocating memory
544 * as needed. If a buffer is already available, it should be passed in
545 * '*resourcesp' and its length in '*spacep'. The number of resource
546 * bytes already in the buffer should be passed in '*lenp'. The memory
547 * allocated will be returned in '*resourcesp' with its size and the
548 * number of bytes of resources in '*spacep' and '*lenp' respectively.
550 * XXX: Multiple problems here, we forget to free() stuff in one
551 * XXX: error return, and in another case we free (*resourcesp) but
552 * XXX: don't tell the caller.
555 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
557 u_char *resources = *resourcesp;
564 resources = malloc(space, M_TEMP, M_NOWAIT);
569 if (len + amount > space) {
571 while (len + amount > space + extra)
573 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
575 /* XXX: free resources */
578 bcopy(resources, newres, len);
579 free(resources, M_TEMP);
584 if (pnp_get_resource_info(resources + len, amount) != amount)
588 *resourcesp = resources;
596 * Read all resources from the card, allocating memory as needed. If a
597 * buffer is already available, it should be passed in '*resourcesp'
598 * and its length in '*spacep'. The memory allocated will be returned
599 * in '*resourcesp' with its size and the number of bytes of resources
600 * in '*spacep' and '*lenp' respectively.
603 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
605 u_char *resources = *resourcesp;
614 error = pnp_read_bytes(1, &resources, &space, &len);
617 tag = resources[len-1];
618 if (PNP_RES_TYPE(tag) == 0) {
620 * Small resource, read contents.
622 error = pnp_read_bytes(PNP_SRES_LEN(tag),
623 &resources, &space, &len);
626 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
630 * Large resource, read length and contents.
632 error = pnp_read_bytes(2, &resources, &space, &len);
635 error = pnp_read_bytes(resources[len-2]
636 + (resources[len-1] << 8), &resources, &space,
644 *resourcesp = resources;
651 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
652 * value (caller should try multiple READ_DATA locations before giving
653 * up). Upon exiting, all cards are aware that they should use
654 * pnp_rd_port as the READ_DATA port.
656 * In the first pass, a csn is assigned to each board and pnp_id's
657 * are saved to an array, pnp_devices. In the second pass, each
658 * card is woken up and the device configuration is called.
661 pnp_isolation_protocol(device_t parent)
666 u_char *resources = NULL;
671 * Put all cards into the Sleep state so that we can clear
674 pnp_send_initiation_key();
677 * Clear the CSN for all cards.
679 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
682 * Move all cards to the Isolation state.
684 pnp_write(PNP_WAKE, 0);
687 * Tell them where the read point is going to be this time.
689 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
691 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
693 * Start the serial isolation protocol.
695 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
696 DELAY(1000); /* Delay 1 msec */
698 if (pnp_get_serial(&id)) {
700 * We have read the id from a card
701 * successfully. The card which won the
702 * isolation protocol will be in Isolation
703 * mode and all others will be in Sleep.
704 * Program the CSN of the isolated card
705 * (taking it to Config state) and read its
706 * resources, creating devices as we find
707 * logical devices on the card.
709 pnp_write(PNP_SET_CSN, csn);
711 printf("Reading PnP configuration for %s.\n",
712 pnp_eisaformat(id.vendor_id));
713 error = pnp_read_resources(&resources, &space, &len);
716 pnp_create_devices(parent, &id, csn, resources, len);
722 * Put this card back to the Sleep state and
723 * simultaneously move all cards which don't have a
724 * CSN yet to Isolation state.
726 pnp_write(PNP_WAKE, 0);
730 * Unless we have chosen the wrong read port, all cards will
731 * be in Sleep state. Put them back into WaitForKey for
732 * now. Their resources will be programmed later.
734 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
740 free(resources, M_TEMP);
749 * autoconfiguration of pnp devices. This routine just runs the
750 * isolation protocol over several ports, until one is successful.
752 * may be called more than once ?
757 pnp_identify(driver_t *driver, device_t parent)
761 /* Try various READ_DATA ports from 0x203-0x3ff */
762 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
764 printf("pnp_identify: Trying Read_Port at %x\n",
765 (pnp_rd_port << 2) | 0x3);
767 num_pnp_devs = pnp_isolation_protocol(parent);
772 printf("PNP Identify complete\n");
775 static device_method_t pnp_methods[] = {
776 /* Device interface */
777 DEVMETHOD(device_identify, pnp_identify),
782 static driver_t pnp_driver = {
788 static devclass_t pnp_devclass;
790 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);