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 * 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 },
74 /* Some NEC PnP cards have 9 bytes serial code. */
75 static pnp_id necids[] = {
76 {0x4180a3b8, 0xffffffff, 0x00}, /* PC-9801CB-B04 (NEC8041) */
77 {0x5181a3b8, 0xffffffff, 0x46}, /* PC-9821CB2-B04(NEC8151) */
78 {0x5182a3b8, 0xffffffff, 0xb8}, /* PC-9801-XX (NEC8251) */
79 {0x9181a3b8, 0xffffffff, 0x00}, /* PC-9801-120 (NEC8191) */
86 * these entries are initialized using the autoconfig menu
87 * The struct is invalid (and must be initialized) if the first
88 * CSN is zero. The init code fills invalid entries with CSN 255
89 * which is not a supported value.
92 struct pnp_cinfo pnp_ldn_overrides[MAX_PNP_LDN] = {
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(u_int32_t id)
107 u_int8_t *data = (u_int8_t *) &id;
108 static char idbuf[8];
109 const char hextoascii[] = "0123456789abcdef";
111 idbuf[0] = '@' + ((data[0] & 0x7c) >> 2);
112 idbuf[1] = '@' + (((data[0] & 0x3) << 3) + ((data[1] & 0xe0) >> 5));
113 idbuf[2] = '@' + (data[1] & 0x1f);
114 idbuf[3] = hextoascii[(data[2] >> 4)];
115 idbuf[4] = hextoascii[(data[2] & 0xf)];
116 idbuf[5] = hextoascii[(data[3] >> 4)];
117 idbuf[6] = hextoascii[(data[3] & 0xf)];
123 pnp_write(int d, u_char r)
125 outb (_PNP_ADDRESS, d);
126 outb (_PNP_WRITE_DATA, r);
134 outb (_PNP_ADDRESS, d);
135 return (inb(3 | (pnp_rd_port <<2)));
141 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
145 pnp_send_initiation_key()
150 outb(_PNP_ADDRESS, 0);
151 outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
154 outb(_PNP_ADDRESS, cur);
156 for (i = 1; i < 32; i++) {
157 cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
158 outb(_PNP_ADDRESS, cur);
164 * Get the device's serial number. Returns 1 if the serial is valid.
167 pnp_get_serial(pnp_id *p)
169 int i, bit, valid = 0, sum = 0x6a;
170 u_char *data = (u_char *)p;
172 bzero(data, sizeof(char) * 9);
173 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
174 for (i = 0; i < 72; i++) {
175 bit = inb((pnp_rd_port << 2) | 0x3) == 0x55;
176 DELAY(250); /* Delay 250 usec */
178 /* Can't Short Circuit the next evaluation, so 'and' is last */
179 bit = (inb((pnp_rd_port << 2) | 0x3) == 0xaa) && bit;
180 DELAY(250); /* Delay 250 usec */
182 valid = valid || bit;
186 (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
188 data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
191 valid = valid && (data[8] == sum);
197 * Fill's the buffer with resource info from the device.
198 * Returns the number of characters read.
201 pnp_get_resource_info(u_char *buffer, int len)
207 for (i = 0; i < len; i++) {
208 outb(_PNP_ADDRESS, PNP_STATUS);
209 for (j = 0; j < 100; j++) {
210 if ((inb((pnp_rd_port << 2) | 0x3)) & 0x1)
215 printf("PnP device failed to report resource data\n");
218 outb(_PNP_ADDRESS, PNP_RESOURCE_DATA);
219 temp = inb((pnp_rd_port << 2) | 0x3);
229 * write_pnp_parms initializes a logical device with the parms
230 * in d, and then activates the board if the last parameter is 1.
234 write_pnp_parms(struct pnp_cinfo *d, pnp_id *p, int ldn)
238 pnp_write (SET_LDN, ldn );
239 i = pnp_read(SET_LDN) ;
241 printf("Warning: LDN %d does not exist\n", ldn);
243 for (i = 0; i < 8; i++) {
244 pnp_write(IO_CONFIG_BASE + i * 2, d->ic_port[i] >> 8 );
245 pnp_write(IO_CONFIG_BASE + i * 2 + 1, d->ic_port[i] & 0xff );
247 for (i = 0; i < 4; i++) {
248 pnp_write(MEM_CONFIG + i*8, (d->ic_mem[i].base >> 16) & 0xff );
249 pnp_write(MEM_CONFIG + i*8+1, (d->ic_mem[i].base >> 8) & 0xff );
250 pnp_write(MEM_CONFIG + i*8+2, d->ic_mem[i].control & 0xff );
251 pnp_write(MEM_CONFIG + i*8+3, (d->ic_mem[i].range >> 16) & 0xff );
252 pnp_write(MEM_CONFIG + i*8+4, (d->ic_mem[i].range >> 8) & 0xff );
254 for (i = 0; i < 2; i++) {
255 pnp_write(IRQ_CONFIG + i*2 , d->irq[i] );
256 pnp_write(IRQ_CONFIG + i*2 + 1, d->irq_type[i] );
257 pnp_write(DRQ_CONFIG + i, d->drq[i] );
260 * store parameters read into the current kernel
261 * so manual editing next time is easier
263 for (i = 0 ; i < MAX_PNP_LDN; i++) {
264 if (pnp_ldn_overrides[i].csn == d->csn &&
265 pnp_ldn_overrides[i].ldn == ldn) {
266 d->flags = pnp_ldn_overrides[i].flags ;
267 pnp_ldn_overrides[i] = *d ;
269 } else if (pnp_ldn_overrides[i].csn < 1 ||
270 pnp_ldn_overrides[i].csn == 255)
273 if (i== MAX_PNP_LDN && empty != -1)
274 pnp_ldn_overrides[empty] = *d;
277 * Here should really perform the range check, and
278 * return a failure if not successful.
280 pnp_write (IO_RANGE_CHECK, 0);
281 DELAY(1000); /* XXX is it really necessary ? */
282 pnp_write (ACTIVATE, d->enable ? 1 : 0);
283 DELAY(1000); /* XXX is it really necessary ? */
289 * This function is called after the bus has assigned resource
290 * locations for a logical device.
293 pnp_set_config(void *arg, struct isa_config *config, int enable)
295 int csn = ((struct pnp_set_config_arg *) arg)->csn;
296 int ldn = ((struct pnp_set_config_arg *) arg)->ldn;
300 * First put all cards into Sleep state with the initiation
301 * key, then put our card into Config state.
303 pnp_send_initiation_key();
304 pnp_write(PNP_WAKE, csn);
307 * Select our logical device so that we can program it.
309 pnp_write(PNP_SET_LDN, ldn);
312 * Now program the resources.
314 for (i = 0; i < config->ic_nmem; i++) {
315 u_int32_t start = config->ic_mem[i].ir_start;
316 u_int32_t size = config->ic_mem[i].ir_size;
318 panic("pnp_set_config: bogus memory assignment");
319 pnp_write(PNP_MEM_BASE_HIGH(i), (start >> 16) & 0xff);
320 pnp_write(PNP_MEM_BASE_LOW(i), (start >> 8) & 0xff);
321 pnp_write(PNP_MEM_RANGE_HIGH(i), (size >> 16) & 0xff);
322 pnp_write(PNP_MEM_RANGE_LOW(i), (size >> 8) & 0xff);
324 for (; i < ISA_NMEM; i++) {
325 pnp_write(PNP_MEM_BASE_HIGH(i), 0);
326 pnp_write(PNP_MEM_BASE_LOW(i), 0);
327 pnp_write(PNP_MEM_RANGE_HIGH(i), 0);
328 pnp_write(PNP_MEM_RANGE_LOW(i), 0);
331 for (i = 0; i < config->ic_nport; i++) {
332 u_int32_t start = config->ic_port[i].ir_start;
333 pnp_write(PNP_IO_BASE_HIGH(i), (start >> 8) & 0xff);
334 pnp_write(PNP_IO_BASE_LOW(i), (start >> 0) & 0xff);
336 for (; i < ISA_NPORT; i++) {
337 pnp_write(PNP_IO_BASE_HIGH(i), 0);
338 pnp_write(PNP_IO_BASE_LOW(i), 0);
341 for (i = 0; i < config->ic_nirq; i++) {
342 int irq = ffs(config->ic_irqmask[i]) - 1;
343 pnp_write(PNP_IRQ_LEVEL(i), irq);
344 pnp_write(PNP_IRQ_TYPE(i), 2); /* XXX */
346 for (; i < ISA_NIRQ; i++) {
348 * IRQ 0 is not a valid interrupt selection and
349 * represents no interrupt selection.
351 pnp_write(PNP_IRQ_LEVEL(i), 0);
354 for (i = 0; i < config->ic_ndrq; i++) {
355 int drq = ffs(config->ic_drqmask[i]) - 1;
356 pnp_write(PNP_DMA_CHANNEL(i), drq);
358 for (; i < ISA_NDRQ; i++) {
360 * DMA channel 4, the cascade channel is used to
361 * indicate no DMA channel is active.
363 pnp_write(PNP_DMA_CHANNEL(i), 4);
366 pnp_write(PNP_ACTIVATE, enable ? 1 : 0);
369 * Wake everyone up again, we are finished.
371 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
375 * Process quirks for a logical device.. The card must be in Config state.
378 pnp_check_quirks(u_int32_t vendor_id, u_int32_t logical_id, int ldn)
380 struct pnp_quirk *qp;
382 for (qp = &pnp_quirks[0]; qp->vendor_id; qp++) {
383 if (qp->vendor_id == vendor_id
384 && (qp->logical_id == 0
385 || qp->logical_id == logical_id)) {
387 case PNP_QUIRK_WRITE_REG:
388 pnp_write(PNP_SET_LDN, ldn);
389 pnp_write(qp->arg1, qp->arg2);
397 * Scan Resource Data for Logical Devices.
399 * This function exits as soon as it gets an error reading *ANY*
400 * Resource Data or it reaches the end of Resource Data. In the first
401 * case the return value will be TRUE, FALSE otherwise.
404 pnp_create_devices(device_t parent, pnp_id *p, int csn,
405 u_char *resources, int len)
407 u_char tag, *resp, *resinfo, *startres = 0;
408 int large_len, scanning = len, retval = FALSE;
409 u_int32_t logical_id;
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) {
439 if (large_len > sizeof(buf) - 1)
440 large_len = sizeof(buf) - 1;
441 bcopy(resinfo, buf, large_len);
444 * Trim trailing spaces.
446 while (buf[large_len-1] == ' ')
448 buf[large_len] = '\0';
451 device_set_desc_copy(dev, desc);
459 if (scanning < PNP_SRES_LEN(tag)) {
464 resp += PNP_SRES_LEN(tag);
465 scanning -= PNP_SRES_LEN(tag);;
467 switch (PNP_SRES_NUM(tag)) {
468 case PNP_TAG_LOGICAL_DEVICE:
470 * Parse the resources for the previous
471 * logical device (if any).
474 pnp_parse_resources(dev, startres,
475 resinfo - startres - 1);
481 * A new logical device. Scan for end of
484 bcopy(resinfo, &logical_id, 4);
485 pnp_check_quirks(p->vendor_id, logical_id, ldn);
487 dev = BUS_ADD_CHILD(parent, ISA_ORDER_PNP, NULL, -1);
489 device_set_desc_copy(dev, desc);
490 isa_set_vendorid(dev, p->vendor_id);
491 isa_set_serial(dev, p->serial);
492 isa_set_logicalid(dev, logical_id);
493 csnldn = malloc(sizeof *csnldn, M_DEVBUF, M_NOWAIT);
495 device_printf(parent,
502 ISA_SET_CONFIG_CALLBACK(parent, dev,
503 pnp_set_config, csnldn);
510 device_printf(parent,
511 "malformed resources\n");
515 pnp_parse_resources(dev, startres,
516 resinfo - startres - 1);
523 /* Skip this resource */
532 * Read 'amount' bytes of resources from the card, allocating memory
533 * as needed. If a buffer is already available, it should be passed in
534 * '*resourcesp' and its length in '*spacep'. The number of resource
535 * bytes already in the buffer should be passed in '*lenp'. The memory
536 * allocated will be returned in '*resourcesp' with its size and the
537 * number of bytes of resources in '*spacep' and '*lenp' respectively.
540 pnp_read_bytes(int amount, u_char **resourcesp, int *spacep, int *lenp)
542 u_char *resources = *resourcesp;
549 resources = malloc(space, M_TEMP, M_NOWAIT);
554 if (len + amount > space) {
556 while (len + amount > space + extra)
558 newres = malloc(space + extra, M_TEMP, M_NOWAIT);
561 bcopy(resources, newres, len);
562 free(resources, M_TEMP);
567 if (pnp_get_resource_info(resources + len, amount) != amount)
571 *resourcesp = resources;
579 * Read all resources from the card, allocating memory as needed. If a
580 * buffer is already available, it should be passed in '*resourcesp'
581 * and its length in '*spacep'. The memory allocated will be returned
582 * in '*resourcesp' with its size and the number of bytes of resources
583 * in '*spacep' and '*lenp' respectively.
586 pnp_read_resources(u_char **resourcesp, int *spacep, int *lenp)
588 u_char *resources = *resourcesp;
597 error = pnp_read_bytes(1, &resources, &space, &len);
600 tag = resources[len-1];
601 if (PNP_RES_TYPE(tag) == 0) {
603 * Small resource, read contents.
605 error = pnp_read_bytes(PNP_SRES_LEN(tag),
606 &resources, &space, &len);
609 if (PNP_SRES_NUM(tag) == PNP_TAG_END)
613 * Large resource, read length and contents.
615 error = pnp_read_bytes(2, &resources, &space, &len);
618 error = pnp_read_bytes(resources[len-2]
619 + (resources[len-1] << 8),
620 &resources, &space, &len);
627 *resourcesp = resources;
634 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
635 * value (caller should try multiple READ_DATA locations before giving
636 * up). Upon exiting, all cards are aware that they should use
637 * pnp_rd_port as the READ_DATA port.
639 * In the first pass, a csn is assigned to each board and pnp_id's
640 * are saved to an array, pnp_devices. In the second pass, each
641 * card is woken up and the device configuration is called.
644 pnp_isolation_protocol(device_t parent)
649 u_char *resources = 0;
658 * Put all cards into the Sleep state so that we can clear
661 pnp_send_initiation_key();
664 * Clear the CSN for all cards.
666 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_RESET_CSN);
669 * Move all cards to the Isolation state.
671 pnp_write(PNP_WAKE, 0);
674 * Tell them where the read point is going to be this time.
676 pnp_write(PNP_SET_RD_DATA, pnp_rd_port);
678 for (csn = 1; csn < PNP_MAX_CARDS; csn++) {
680 * Start the serial isolation protocol.
682 outb(_PNP_ADDRESS, PNP_SERIAL_ISOLATION);
683 DELAY(1000); /* Delay 1 msec */
685 if (pnp_get_serial(&id)) {
687 * We have read the id from a card
688 * successfully. The card which won the
689 * isolation protocol will be in Isolation
690 * mode and all others will be in Sleep.
691 * Program the CSN of the isolated card
692 * (taking it to Config state) and read its
693 * resources, creating devices as we find
694 * logical devices on the card.
696 pnp_write(PNP_SET_CSN, csn);
699 printf("PnP Vendor ID = %x\n", id.vendor_id);
700 /* Check for NEC PnP (9 bytes serial). */
701 for (n = necpnp = 0; necids[n].vendor_id; n++) {
702 if (id.vendor_id == necids[n].vendor_id) {
709 printf("It seems to NEC-PnP card (%s).\n",
710 pnp_eisaformat(id.vendor_id));
711 /* Read dummy 9 bytes serial area. */
712 pnp_get_resource_info(buffer, 9);
715 printf("It seems to Normal-ISA-PnP card (%s).\n",
716 pnp_eisaformat(id.vendor_id));
719 printf("Reading PnP configuration for %s.\n",
720 pnp_eisaformat(id.vendor_id));
722 error = pnp_read_resources(&resources,
727 pnp_create_devices(parent, &id, csn,
734 * Put this card back to the Sleep state and
735 * simultaneously move all cards which don't have a
736 * CSN yet to Isolation state.
738 pnp_write(PNP_WAKE, 0);
742 * Unless we have chosen the wrong read port, all cards will
743 * be in Sleep state. Put them back into WaitForKey for
744 * now. Their resources will be programmed later.
746 pnp_write(PNP_CONFIG_CONTROL, PNP_CONFIG_CONTROL_WAIT_FOR_KEY);
752 free(resources, M_TEMP);
761 * autoconfiguration of pnp devices. This routine just runs the
762 * isolation protocol over several ports, until one is successful.
764 * may be called more than once ?
769 pnp_identify(driver_t *driver, device_t parent)
774 if (pnp_ldn_overrides[0].csn == 0) {
776 printf("Initializing PnP override table\n");
777 bzero (pnp_ldn_overrides, sizeof(pnp_ldn_overrides));
778 pnp_ldn_overrides[0].csn = 255 ;
782 /* Try various READ_DATA ports from 0x203-0x3ff */
783 for (pnp_rd_port = 0x80; (pnp_rd_port < 0xff); pnp_rd_port += 0x10) {
785 printf("Trying Read_Port at %x\n", (pnp_rd_port << 2) | 0x3);
787 num_pnp_devs = pnp_isolation_protocol(parent);
793 static device_method_t pnp_methods[] = {
794 /* Device interface */
795 DEVMETHOD(device_identify, pnp_identify),
800 static driver_t pnp_driver = {
806 static devclass_t pnp_devclass;
808 DRIVER_MODULE(pnp, isa, pnp_driver, pnp_devclass, 0, 0);