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.2 2003/06/17 04:28:40 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>
37 #include <isa/isavar.h>
38 #include <isa/pnpreg.h>
39 #include <isa/pnpvar.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 */
57 #define PNP_QUIRK_WRITE_REG 1 /* Need to write a pnp register */
62 struct pnp_quirk pnp_quirks[] = {
64 * The Gravis UltraSound needs register 0xf2 to be set to 0xff
66 * XXX need to know the logical device id.
68 { 0x0100561e /* GRV0001 */, 0,
69 PNP_QUIRK_WRITE_REG, 0xf2, 0xff },
75 /* Some NEC PnP cards have 9 bytes serial code. */
76 static pnp_id necids[] = {
77 {0x4180a3b8, 0xffffffff, 0x00}, /* PC-9801CB-B04 (NEC8041) */
78 {0x5181a3b8, 0xffffffff, 0x46}, /* PC-9821CB2-B04(NEC8151) */
79 {0x5182a3b8, 0xffffffff, 0xb8}, /* PC-9801-XX (NEC8251) */
80 {0x9181a3b8, 0xffffffff, 0x00}, /* PC-9801-120 (NEC8191) */
87 * these entries are initialized using the autoconfig menu
88 * The struct is invalid (and must be initialized) if the first
89 * CSN is zero. The init code fills invalid entries with CSN 255
90 * which is not a supported value.
93 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
98 /* The READ_DATA port that we are using currently */
99 static int pnp_rd_port;
101 static void pnp_send_initiation_key(void);
102 static int pnp_get_serial(pnp_id *p);
103 static int pnp_isolation_protocol(device_t parent);
106 pnp_eisaformat(u_int32_t id)
108 u_int8_t *data = (u_int8_t *) &id;
109 static char idbuf[8];
110 const char hextoascii[] = "0123456789abcdef";
112 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
113 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
114 idbuf[2] = '@' + (data[1] & 0x1f);
115 idbuf[3] = hextoascii[(data[2] >> 4)];
116 idbuf[4] = hextoascii[(data[2] & 0xf)];
117 idbuf[5] = hextoascii[(data[3] >> 4)];
118 idbuf[6] = hextoascii[(data[3] & 0xf)];
124 pnp_write(int d, u_char r)
126 outb (_PNP_ADDRESS, d);
127 outb (_PNP_WRITE_DATA, r);
135 outb (_PNP_ADDRESS, d);
136 return (inb(3 | (pnp_rd_port <<2)));
142 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
146 pnp_send_initiation_key()
151 outb(_PNP_ADDRESS, 0);
152 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
155 outb(_PNP_ADDRESS, cur);
157 for (i = 1; i < 32; i++) {
158 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
159 outb(_PNP_ADDRESS, cur);
165 * Get the device's serial number. Returns 1 if the serial is valid.
168 pnp_get_serial(pnp_id *p)
170 int i, bit, valid = 0, sum = 0x6a;
171 u_char *data = (u_char *)p;
173 bzero(data, sizeof(char) * 9);
174 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
175 for (i = 0; i < 72; i++) {
176 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
177 DELAY(250); /* Delay 250 usec */
179 /* Can't Short Circuit the next evaluation, so 'and' is last */
180 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
181 DELAY(250); /* Delay 250 usec */
183 valid = valid || bit;
187 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
189 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
192 valid = valid && (data[8] == sum);
198 * Fill's the buffer with resource info from the device.
199 * Returns the number of characters read.
202 pnp_get_resource_info(u_char *buffer, int len)
208 for (i = 0; i < len; i++) {
209 outb(_PNP_ADDRESS, PNP_STATUS);
210 for (j = 0; j < 100; j++) {
211 if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
216 printf("PnP device failed to report resource data\n");
219 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
220 temp = inb((pnp_rd_port << 2) | 0x3);
230 * write_pnp_parms initializes a logical device with the parms
231 * in d, and then activates the board if the last parameter is 1.
235 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
239 pnp_write (SET_LDN, ldn );
240 i = pnp_read(SET_LDN) ;
242 printf("Warning: LDN %d does not exist\n", ldn);
244 for (i = 0; i < 8; i++) {
245 pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
246 pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
248 for (i = 0; i < 4; i++) {
249 pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
250 pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
251 pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
252 pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
253 pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
255 for (i = 0; i < 2; i++) {
256 pnp_write(IRQ_CONFIG + i*2 , d->irq[i] );
257 pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
258 pnp_write(DRQ_CONFIG + i, d->drq[i] );
261 * store parameters read into the current kernel
262 * so manual editing next time is easier
264 for (i = 0 ; i < MAX_PNP_LDN; i++) {
265 if (pnp_ldn_overrides[i].csn == d->csn &&
266 pnp_ldn_overrides[i].ldn == ldn) {
267 d->flags = pnp_ldn_overrides[i].flags ;
268 pnp_ldn_overrides[i] = *d ;
270 } else if (pnp_ldn_overrides[i].csn < 1 ||
271 pnp_ldn_overrides[i].csn == 255)
274 if (i== MAX_PNP_LDN && empty != -1)
275 pnp_ldn_overrides[empty] = *d;
278 * Here should really perform the range check, and
279 * return a failure if not successful.
281 pnp_write (IO_RANGE_CHECK, 0);
282 DELAY(1000); /* XXX is it really necessary ? */
283 pnp_write (ACTIVATE, d->enable ? 1 : 0);
284 DELAY(1000); /* XXX is it really necessary ? */
290 * This function is called after the bus has assigned resource
291 * locations for a logical device.
294 pnp_set_config(void *arg, struct isa_config *config, int enable)
296 int csn = ((struct pnp_set_config_arg *) arg)->csn;
297 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
301 * First put all cards into Sleep state with the initiation
302 * key, then put our card into Config state.
304 pnp_send_initiation_key();
305 pnp_write(PNP_WAKE, csn);
308 * Select our logical device so that we can program it.
310 pnp_write(PNP_SET_LDN, ldn);
313 * Now program the resources.
315 for (i = 0; i < config->ic_nmem; i++) {
316 u_int32_t start = config->ic_mem[i].ir_start;
317 u_int32_t size = config->ic_mem[i].ir_size;
319 panic("pnp_set_config: bogus memory assignment");
320 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
321 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
322 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
323 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
325 for (; i < ISA_NMEM; i++) {
326 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
327 pnp_write(PNP_MEM_BASE_LOW(i), 0);
328 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
329 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
332 for (i = 0; i < config->ic_nport; i++) {
333 u_int32_t start = config->ic_port[i].ir_start;
334 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
335 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
337 for (; i < ISA_NPORT; i++) {
338 pnp_write(PNP_IO_BASE_HIGH(i), 0);
339 pnp_write(PNP_IO_BASE_LOW(i), 0);
342 for (i = 0; i < config->ic_nirq; i++) {
343 int irq = ffs(config->ic_irqmask[i]) - 1;
344 pnp_write(PNP_IRQ_LEVEL(i), irq);
345 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
347 for (; i < ISA_NIRQ; i++) {
349 * IRQ 0 is not a valid interrupt selection and
350 * represents no interrupt selection.
352 pnp_write(PNP_IRQ_LEVEL(i), 0);
355 for (i = 0; i < config->ic_ndrq; i++) {
356 int drq = ffs(config->ic_drqmask[i]) - 1;
357 pnp_write(PNP_DMA_CHANNEL(i), drq);
359 for (; i < ISA_NDRQ; i++) {
361 * DMA channel 4, the cascade channel is used to
362 * indicate no DMA channel is active.
364 pnp_write(PNP_DMA_CHANNEL(i), 4);
367 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
370 * Wake everyone up again, we are finished.
372 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
376 * Process quirks for a logical device.. The card must be in Config state.
379 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn)
381 struct pnp_quirk *qp;
383 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
384 if (qp->vendor_id == vendor_id
385 && (qp->logical_id == 0
386 || qp->logical_id == logical_id)) {
388 case PNP_QUIRK_WRITE_REG:
389 pnp_write(PNP_SET_LDN, ldn);
390 pnp_write(qp->arg1, 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 = 0;
409 int large_len, scanning = len, retval = FALSE;
410 u_int32_t logical_id;
414 struct pnp_set_config_arg *csnldn;
419 while (scanning > 0) {
422 if (PNP_RES_TYPE(tag) != 0) {
428 large_len = resp[0] + (resp[1] << 8);
431 if (scanning < large_len) {
437 scanning -= large_len;
439 if (PNP_LRES_NUM(tag) == PNP_TAG_ID_ANSI) {
440 if (large_len > sizeof(buf) - 1)
441 large_len = sizeof(buf) - 1;
442 bcopy(resinfo, buf, large_len);
445 * Trim trailing spaces.
447 while (buf[large_len-1] == ' ')
449 buf[large_len] = '\0';
452 device_set_desc_copy(dev, desc);
460 if (scanning < PNP_SRES_LEN(tag)) {
465 resp += PNP_SRES_LEN(tag);
466 scanning -= PNP_SRES_LEN(tag);;
468 switch (PNP_SRES_NUM(tag)) {
469 case PNP_TAG_LOGICAL_DEVICE:
471 * Parse the resources for the previous
472 * logical device (if any).
475 pnp_parse_resources(dev, startres,
476 resinfo - startres - 1);
482 * A new logical device. Scan for end of
485 bcopy(resinfo, &logical_id, 4);
486 pnp_check_quirks(p->vendor_id, logical_id, ldn);
488 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
490 device_set_desc_copy(dev, desc);
491 isa_set_vendorid(dev, p->vendor_id);
492 isa_set_serial(dev, p->serial);
493 isa_set_logicalid(dev, logical_id);
494 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
496 device_printf(parent,
503 ISA_SET_CONFIG_CALLBACK(parent, dev,
504 pnp_set_config, csnldn);
511 device_printf(parent,
512 "malformed resources\n");
516 pnp_parse_resources(dev, startres,
517 resinfo - startres - 1);
524 /* Skip this resource */
533 * Read 'amount' bytes of resources from the card, allocating memory
534 * as needed. If a buffer is already available, it should be passed in
535 * '*resourcesp' and its length in '*spacep'. The number of resource
536 * bytes already in the buffer should be passed in '*lenp'. The memory
537 * allocated will be returned in '*resourcesp' with its size and the
538 * number of bytes of resources in '*spacep' and '*lenp' respectively.
541 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
543 u_char *resources = *resourcesp;
550 resources = malloc(space, M_TEMP, M_NOWAIT);
555 if (len + amount > space) {
557 while (len + amount > space + extra)
559 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
562 bcopy(resources, newres, len);
563 free(resources, M_TEMP);
568 if (pnp_get_resource_info(resources + len, amount) != amount)
572 *resourcesp = resources;
580 * Read all resources from the card, allocating memory as needed. If a
581 * buffer is already available, it should be passed in '*resourcesp'
582 * and its length in '*spacep'. The memory allocated will be returned
583 * in '*resourcesp' with its size and the number of bytes of resources
584 * in '*spacep' and '*lenp' respectively.
587 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
589 u_char *resources = *resourcesp;
598 error = pnp_read_bytes(1, &resources, &space, &len);
601 tag = resources[len-1];
602 if (PNP_RES_TYPE(tag) == 0) {
604 * Small resource, read contents.
606 error = pnp_read_bytes(PNP_SRES_LEN(tag),
607 &resources, &space, &len);
610 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
614 * Large resource, read length and contents.
616 error = pnp_read_bytes(2, &resources, &space, &len);
619 error = pnp_read_bytes(resources[len-2]
620 + (resources[len-1] << 8),
621 &resources, &space, &len);
628 *resourcesp = resources;
635 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
636 * value (caller should try multiple READ_DATA locations before giving
637 * up). Upon exiting, all cards are aware that they should use
638 * pnp_rd_port as the READ_DATA port.
640 * In the first pass, a csn is assigned to each board and pnp_id's
641 * are saved to an array, pnp_devices. In the second pass, each
642 * card is woken up and the device configuration is called.
645 pnp_isolation_protocol(device_t parent)
650 u_char *resources = 0;
659 * Put all cards into the Sleep state so that we can clear
662 pnp_send_initiation_key();
665 * Clear the CSN for all cards.
667 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
670 * Move all cards to the Isolation state.
672 pnp_write(PNP_WAKE, 0);
675 * Tell them where the read point is going to be this time.
677 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
679 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
681 * Start the serial isolation protocol.
683 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
684 DELAY(1000); /* Delay 1 msec */
686 if (pnp_get_serial(&id)) {
688 * We have read the id from a card
689 * successfully. The card which won the
690 * isolation protocol will be in Isolation
691 * mode and all others will be in Sleep.
692 * Program the CSN of the isolated card
693 * (taking it to Config state) and read its
694 * resources, creating devices as we find
695 * logical devices on the card.
697 pnp_write(PNP_SET_CSN, csn);
700 printf("PnP Vendor ID = %x\n", id.vendor_id);
701 /* Check for NEC PnP (9 bytes serial). */
702 for (n = necpnp = 0; necids[n].vendor_id; n++) {
703 if (id.vendor_id == necids[n].vendor_id) {
710 printf("It seems to NEC-PnP card (%s).\n",
711 pnp_eisaformat(id.vendor_id));
712 /* Read dummy 9 bytes serial area. */
713 pnp_get_resource_info(buffer, 9);
716 printf("It seems to Normal-ISA-PnP card (%s).\n",
717 pnp_eisaformat(id.vendor_id));
720 printf("Reading PnP configuration for %s.\n",
721 pnp_eisaformat(id.vendor_id));
723 error = pnp_read_resources(&resources,
728 pnp_create_devices(parent, &id, csn,
735 * Put this card back to the Sleep state and
736 * simultaneously move all cards which don't have a
737 * CSN yet to Isolation state.
739 pnp_write(PNP_WAKE, 0);
743 * Unless we have chosen the wrong read port, all cards will
744 * be in Sleep state. Put them back into WaitForKey for
745 * now. Their resources will be programmed later.
747 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
753 free(resources, M_TEMP);
762 * autoconfiguration of pnp devices. This routine just runs the
763 * isolation protocol over several ports, until one is successful.
765 * may be called more than once ?
770 pnp_identify(driver_t *driver, device_t parent)
775 if (pnp_ldn_overrides[0].csn == 0) {
777 printf("Initializing PnP override table\n");
778 bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
779 pnp_ldn_overrides[0].csn = 255 ;
783 /* Try various READ_DATA ports from 0x203-0x3ff */
784 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
786 printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
788 num_pnp_devs = pnp_isolation_protocol(parent);
794 static device_method_t pnp_methods[] = {
795 /* Device interface */
796 DEVMETHOD(device_identify, pnp_identify),
801 static driver_t pnp_driver = {
807 static devclass_t pnp_devclass;
809 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);