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
27 * from: pnp.c,v 1.11 1999/05/06 22:11:19 peter Exp
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.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>
41 typedef struct _pnp_id {
47 struct pnp_set_config_arg {
48 int csn; /* Card number to configure */
49 int ldn; /* Logical device on card */
53 u_int32_t vendor_id; /* Vendor of the card */
54 u_int32_t logical_id; /* ID of the device with quirk */
56 #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */
61 struct pnp_quirk pnp_quirks[] = {
63 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
65 * XXX need to know the logical device id.
67 { 0x0100561e /* GRV0001 */, 0,
68 PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
75 * these entries are initialized using the autoconfig menu
76 * The struct is invalid (and must be initialized) if the first
77 * CSN is zero. The init code fills invalid entries with CSN 255
78 * which is not a supported value.
81 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
86 /* The READ_DATA port that we are using currently */
87 static int pnp_rd_port;
89 static void pnp_send_initiation_key(void);
90 static int pnp_get_serial(pnp_id *p);
91 static int pnp_isolation_protocol(device_t parent);
94 pnp_eisaformat(u_int32_t id)
96 u_int8_t *data = (u_int8_t *) &id;
98 const char hextoascii[] = "0123456789abcdef";
100 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
101 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
102 idbuf[2] = '@' + (data[1] & 0x1f);
103 idbuf[3] = hextoascii[(data[2] >> 4)];
104 idbuf[4] = hextoascii[(data[2] & 0xf)];
105 idbuf[5] = hextoascii[(data[3] >> 4)];
106 idbuf[6] = hextoascii[(data[3] & 0xf)];
112 pnp_write(int d, u_char r)
114 outb (_PNP_ADDRESS, d);
115 outb (_PNP_WRITE_DATA, r);
123 outb (_PNP_ADDRESS, d);
124 return (inb(3 | (pnp_rd_port <<2)));
130 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
134 pnp_send_initiation_key()
139 outb(_PNP_ADDRESS, 0);
140 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
143 outb(_PNP_ADDRESS, cur);
145 for (i = 1; i < 32; i++) {
146 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
147 outb(_PNP_ADDRESS, cur);
153 * Get the device's serial number. Returns 1 if the serial is valid.
156 pnp_get_serial(pnp_id *p)
158 int i, bit, valid = 0, sum = 0x6a;
159 u_char *data = (u_char *)p;
161 bzero(data, sizeof(char) * 9);
162 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
163 for (i = 0; i < 72; i++) {
164 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
165 DELAY(250); /* Delay 250 usec */
167 /* Can't Short Circuit the next evaluation, so 'and' is last */
168 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
169 DELAY(250); /* Delay 250 usec */
171 valid = valid || bit;
175 (((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);
218 * write_pnp_parms initializes a logical device with the parms
219 * in d, and then activates the board if the last parameter is 1.
223 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
227 pnp_write (SET_LDN, ldn );
228 i = pnp_read(SET_LDN) ;
230 printf("Warning: LDN %d does not exist\n", ldn);
232 for (i = 0; i < 8; i++) {
233 pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
234 pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
236 for (i = 0; i < 4; i++) {
237 pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
238 pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
239 pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
240 pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
241 pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
243 for (i = 0; i < 2; i++) {
244 pnp_write(IRQ_CONFIG + i*2 , d->irq[i] );
245 pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
246 pnp_write(DRQ_CONFIG + i, d->drq[i] );
249 * store parameters read into the current kernel
250 * so manual editing next time is easier
252 for (i = 0 ; i < MAX_PNP_LDN; i++) {
253 if (pnp_ldn_overrides[i].csn == d->csn &&
254 pnp_ldn_overrides[i].ldn == ldn) {
255 d->flags = pnp_ldn_overrides[i].flags ;
256 pnp_ldn_overrides[i] = *d ;
258 } else if (pnp_ldn_overrides[i].csn < 1 ||
259 pnp_ldn_overrides[i].csn == 255)
262 if (i== MAX_PNP_LDN && empty != -1)
263 pnp_ldn_overrides[empty] = *d;
266 * Here should really perform the range check, and
267 * return a failure if not successful.
269 pnp_write (IO_RANGE_CHECK, 0);
270 DELAY(1000); /* XXX is it really necessary ? */
271 pnp_write (ACTIVATE, d->enable ? 1 : 0);
272 DELAY(1000); /* XXX is it really necessary ? */
278 * This function is called after the bus has assigned resource
279 * locations for a logical device.
282 pnp_set_config(void *arg, struct isa_config *config, int enable)
284 int csn = ((struct pnp_set_config_arg *) arg)->csn;
285 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
289 * First put all cards into Sleep state with the initiation
290 * key, then put our card into Config state.
292 pnp_send_initiation_key();
293 pnp_write(PNP_WAKE, csn);
296 * Select our logical device so that we can program it.
298 pnp_write(PNP_SET_LDN, ldn);
301 * Now program the resources.
303 for (i = 0; i < config->ic_nmem; i++) {
304 u_int32_t start = config->ic_mem[i].ir_start;
305 u_int32_t size = config->ic_mem[i].ir_size;
307 panic("pnp_set_config: bogus memory assignment");
308 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
309 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
310 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
311 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
313 for (; i < ISA_NMEM; i++) {
314 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
315 pnp_write(PNP_MEM_BASE_LOW(i), 0);
316 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
317 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
320 for (i = 0; i < config->ic_nport; i++) {
321 u_int32_t start = config->ic_port[i].ir_start;
322 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
323 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
325 for (; i < ISA_NPORT; i++) {
326 pnp_write(PNP_IO_BASE_HIGH(i), 0);
327 pnp_write(PNP_IO_BASE_LOW(i), 0);
330 for (i = 0; i < config->ic_nirq; i++) {
331 int irq = ffs(config->ic_irqmask[i]) - 1;
332 pnp_write(PNP_IRQ_LEVEL(i), irq);
333 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
335 for (; i < ISA_NIRQ; i++) {
337 * IRQ 0 is not a valid interrupt selection and
338 * represents no interrupt selection.
340 pnp_write(PNP_IRQ_LEVEL(i), 0);
343 for (i = 0; i < config->ic_ndrq; i++) {
344 int drq = ffs(config->ic_drqmask[i]) - 1;
345 pnp_write(PNP_DMA_CHANNEL(i), drq);
347 for (; i < ISA_NDRQ; i++) {
349 * DMA channel 4, the cascade channel is used to
350 * indicate no DMA channel is active.
352 pnp_write(PNP_DMA_CHANNEL(i), 4);
355 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
358 * Wake everyone up again, we are finished.
360 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
364 * Process quirks for a logical device.. The card must be in Config state.
367 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn)
369 struct pnp_quirk *qp;
371 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
372 if (qp->vendor_id == vendor_id
373 && (qp->logical_id == 0
374 || qp->logical_id == logical_id)) {
376 case PNP_QUIRK_WRITE_REG:
377 pnp_write(PNP_SET_LDN, ldn);
378 pnp_write(qp->arg1, qp->arg2);
386 * Scan Resource Data for Logical Devices.
388 * This function exits as soon as it gets an error reading *ANY*
389 * Resource Data or it reaches the end of Resource Data. In the first
390 * case the return value will be TRUE, FALSE otherwise.
393 pnp_create_devices(device_t parent, pnp_id *p, int csn,
394 u_char *resources, int len)
396 u_char tag, *resp, *resinfo, *startres = 0;
397 int large_len, scanning = len, retval = FALSE;
398 u_int32_t logical_id;
402 struct pnp_set_config_arg *csnldn;
407 while (scanning > 0) {
410 if (PNP_RES_TYPE(tag) != 0) {
416 large_len = resp[0] + (resp[1] << 8);
419 if (scanning < large_len) {
425 scanning -= large_len;
427 if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
428 if (large_len > sizeof(buf) - 1)
429 large_len = sizeof(buf) - 1;
430 bcopy(resinfo, buf, large_len);
433 * Trim trailing spaces.
435 while (buf[large_len-1] == ' ')
437 buf[large_len] = '\0';
440 device_set_desc_copy(dev, desc);
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);
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);
476 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
478 device_set_desc_copy(dev, desc);
479 isa_set_vendorid(dev, p->vendor_id);
480 isa_set_serial(dev, p->serial);
481 isa_set_logicalid(dev, logical_id);
482 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
484 device_printf(parent,
491 ISA_SET_CONFIG_CALLBACK(parent, dev,
492 pnp_set_config, csnldn);
499 device_printf(parent,
500 "malformed resources\n");
504 pnp_parse_resources(dev, startres,
505 resinfo - startres - 1);
512 /* Skip this resource */
521 * Read 'amount' bytes of resources from the card, allocating memory
522 * as needed. If a buffer is already available, it should be passed in
523 * '*resourcesp' and its length in '*spacep'. The number of resource
524 * bytes already in the buffer should be passed in '*lenp'. The memory
525 * allocated will be returned in '*resourcesp' with its size and the
526 * number of bytes of resources in '*spacep' and '*lenp' respectively.
529 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
531 u_char *resources = *resourcesp;
538 resources = malloc(space, M_TEMP, M_NOWAIT);
543 if (len + amount > space) {
545 while (len + amount > space + extra)
547 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
550 bcopy(resources, newres, len);
551 free(resources, M_TEMP);
556 if (pnp_get_resource_info(resources + len, amount) != amount)
560 *resourcesp = resources;
568 * Read all resources from the card, allocating memory as needed. If a
569 * buffer is already available, it should be passed in '*resourcesp'
570 * and its length in '*spacep'. The memory allocated will be returned
571 * in '*resourcesp' with its size and the number of bytes of resources
572 * in '*spacep' and '*lenp' respectively.
575 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
577 u_char *resources = *resourcesp;
586 error = pnp_read_bytes(1, &resources, &space, &len);
589 tag = resources[len-1];
590 if (PNP_RES_TYPE(tag) == 0) {
592 * Small resource, read contents.
594 error = pnp_read_bytes(PNP_SRES_LEN(tag),
595 &resources, &space, &len);
598 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
602 * Large resource, read length and contents.
604 error = pnp_read_bytes(2, &resources, &space, &len);
607 error = pnp_read_bytes(resources[len-2]
608 + (resources[len-1] << 8),
609 &resources, &space, &len);
616 *resourcesp = resources;
623 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
624 * value (caller should try multiple READ_DATA locations before giving
625 * up). Upon exiting, all cards are aware that they should use
626 * pnp_rd_port as the READ_DATA port.
628 * In the first pass, a csn is assigned to each board and pnp_id's
629 * are saved to an array, pnp_devices. In the second pass, each
630 * card is woken up and the device configuration is called.
633 pnp_isolation_protocol(device_t parent)
638 u_char *resources = 0;
643 * Put all cards into the Sleep state so that we can clear
646 pnp_send_initiation_key();
649 * Clear the CSN for all cards.
651 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
654 * Move all cards to the Isolation state.
656 pnp_write(PNP_WAKE, 0);
659 * Tell them where the read point is going to be this time.
661 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
663 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
665 * Start the serial isolation protocol.
667 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
668 DELAY(1000); /* Delay 1 msec */
670 if (pnp_get_serial(&id)) {
672 * We have read the id from a card
673 * successfully. The card which won the
674 * isolation protocol will be in Isolation
675 * mode and all others will be in Sleep.
676 * Program the CSN of the isolated card
677 * (taking it to Config state) and read its
678 * resources, creating devices as we find
679 * logical devices on the card.
681 pnp_write(PNP_SET_CSN, csn);
682 error = pnp_read_resources(&resources,
687 pnp_create_devices(parent, &id, csn,
694 * Put this card back to the Sleep state and
695 * simultaneously move all cards which don't have a
696 * CSN yet to Isolation state.
698 pnp_write(PNP_WAKE, 0);
702 * Unless we have chosen the wrong read port, all cards will
703 * be in Sleep state. Put them back into WaitForKey for
704 * now. Their resources will be programmed later.
706 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
712 free(resources, M_TEMP);
721 * autoconfiguration of pnp devices. This routine just runs the
722 * isolation protocol over several ports, until one is successful.
724 * may be called more than once ?
729 pnp_identify(driver_t *driver, device_t parent)
734 if (pnp_ldn_overrides[0].csn == 0) {
736 printf("Initializing PnP override table\n");
737 bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
738 pnp_ldn_overrides[0].csn = 255 ;
742 /* Try various READ_DATA ports from 0x203-0x3ff */
743 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
745 printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
747 num_pnp_devs = pnp_isolation_protocol(parent);
753 static device_method_t pnp_methods[] = {
754 /* Device interface */
755 DEVMETHOD(device_identify, pnp_identify),
760 static driver_t pnp_driver = {
766 static devclass_t pnp_devclass;
768 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);