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 },
98 /* Some NEC PnP cards have 9 bytes serial code. */
99 static pnp_id necids
[] = {
100 {0x4180a3b8, 0xffffffff, 0x00}, /* PC-9801CB-B04 (NEC8041) */
101 {0x5181a3b8, 0xffffffff, 0x46}, /* PC-9821CB2-B04(NEC8151) */
102 {0x5182a3b8, 0xffffffff, 0xb8}, /* PC-9801-XX (NEC8251) */
103 {0x9181a3b8, 0xffffffff, 0x00}, /* PC-9801-120 (NEC8191) */
108 /* The READ_DATA port that we are using currently */
109 static int pnp_rd_port
;
111 static void pnp_send_initiation_key(void);
112 static int pnp_get_serial(pnp_id
*p
);
113 static int pnp_isolation_protocol(device_t parent
);
116 pnp_eisaformat(uint32_t id
)
119 static char idbuf
[8];
120 const char hextoascii
[] = "0123456789abcdef";
123 data
= (uint8_t *)&id
;
124 idbuf
[0] = '@' + ((data
[0] & 0x7c) >> 2);
125 idbuf
[1] = '@' + (((data
[0] & 0x3) << 3) + ((data
[1] & 0xe0) >> 5));
126 idbuf
[2] = '@' + (data
[1] & 0x1f);
127 idbuf
[3] = hextoascii
[(data
[2] >> 4)];
128 idbuf
[4] = hextoascii
[(data
[2] & 0xf)];
129 idbuf
[5] = hextoascii
[(data
[3] >> 4)];
130 idbuf
[6] = hextoascii
[(data
[3] & 0xf)];
136 pnp_write(int d
, u_char r
)
138 outb (_PNP_ADDRESS
, d
);
139 outb (_PNP_WRITE_DATA
, r
);
143 * Send Initiation LFSR as described in "Plug and Play ISA Specification",
147 pnp_send_initiation_key()
152 outb(_PNP_ADDRESS
, 0);
153 outb(_PNP_ADDRESS
, 0); /* yes, we do need it twice! */
156 outb(_PNP_ADDRESS
, cur
);
158 for (i
= 1; i
< 32; i
++) {
159 cur
= (cur
>> 1) | (((cur
^ (cur
>> 1)) << 7) & 0xff);
160 outb(_PNP_ADDRESS
, cur
);
166 * Get the device's serial number. Returns 1 if the serial is valid.
169 pnp_get_serial(pnp_id
*p
)
171 int i
, bit
, valid
= 0, sum
= 0x6a;
172 u_char
*data
= (u_char
*)p
;
174 bzero(data
, sizeof(char) * 9);
175 outb(_PNP_ADDRESS
, PNP_SERIAL_ISOLATION
);
176 for (i
= 0; i
< 72; i
++) {
177 bit
= inb((pnp_rd_port
<< 2) | 0x3) == 0x55;
178 DELAY(250); /* Delay 250 usec */
180 /* Can't Short Circuit the next evaluation, so 'and' is last */
181 bit
= (inb((pnp_rd_port
<< 2) | 0x3) == 0xaa) && bit
;
182 DELAY(250); /* Delay 250 usec */
184 valid
= valid
|| bit
;
187 (((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);
228 * This function is called after the bus has assigned resource
229 * locations for a logical device.
232 pnp_set_config(void *arg
, struct isa_config
*config
, int enable
)
234 int csn
= ((struct pnp_set_config_arg
*) arg
)->csn
;
235 int ldn
= ((struct pnp_set_config_arg
*) arg
)->ldn
;
239 * First put all cards into Sleep state with the initiation
240 * key, then put our card into Config state.
242 pnp_send_initiation_key();
243 pnp_write(PNP_WAKE
, csn
);
246 * Select our logical device so that we can program it.
248 pnp_write(PNP_SET_LDN
, ldn
);
251 * Constrain the number of resources we will try to program
253 if (config
->ic_nmem
> ISA_PNP_NMEM
) {
254 printf("too many ISA memory ranges (%d > %d)\n",
255 config
->ic_nmem
, ISA_PNP_NMEM
);
256 config
->ic_nmem
= ISA_PNP_NMEM
;
258 if (config
->ic_nport
> ISA_PNP_NPORT
) {
259 printf("too many ISA I/O ranges (%d > %d)\n", config
->ic_nport
,
261 config
->ic_nport
= ISA_PNP_NPORT
;
263 if (config
->ic_nirq
> ISA_PNP_NIRQ
) {
264 printf("too many ISA IRQs (%d > %d)\n", config
->ic_nirq
,
266 config
->ic_nirq
= ISA_PNP_NIRQ
;
268 if (config
->ic_ndrq
> ISA_PNP_NDRQ
) {
269 printf("too many ISA DRQs (%d > %d)\n", config
->ic_ndrq
,
271 config
->ic_ndrq
= ISA_PNP_NDRQ
;
275 * Now program the resources.
277 for (i
= 0; i
< config
->ic_nmem
; i
++) {
281 /* XXX: should handle memory control register, 32 bit memory */
282 if (config
->ic_mem
[i
].ir_size
== 0) {
283 pnp_write(PNP_MEM_BASE_HIGH(i
), 0);
284 pnp_write(PNP_MEM_BASE_LOW(i
), 0);
285 pnp_write(PNP_MEM_RANGE_HIGH(i
), 0);
286 pnp_write(PNP_MEM_RANGE_LOW(i
), 0);
288 start
= config
->ic_mem
[i
].ir_start
;
289 size
= config
->ic_mem
[i
].ir_size
;
291 panic("pnp_set_config: bogus memory assignment");
292 pnp_write(PNP_MEM_BASE_HIGH(i
), (start
>> 16) & 0xff);
293 pnp_write(PNP_MEM_BASE_LOW(i
), (start
>> 8) & 0xff);
294 pnp_write(PNP_MEM_RANGE_HIGH(i
), (size
>> 16) & 0xff);
295 pnp_write(PNP_MEM_RANGE_LOW(i
), (size
>> 8) & 0xff);
298 for (; i
< ISA_PNP_NMEM
; i
++) {
299 pnp_write(PNP_MEM_BASE_HIGH(i
), 0);
300 pnp_write(PNP_MEM_BASE_LOW(i
), 0);
301 pnp_write(PNP_MEM_RANGE_HIGH(i
), 0);
302 pnp_write(PNP_MEM_RANGE_LOW(i
), 0);
305 for (i
= 0; i
< config
->ic_nport
; i
++) {
308 if (config
->ic_port
[i
].ir_size
== 0) {
309 pnp_write(PNP_IO_BASE_HIGH(i
), 0);
310 pnp_write(PNP_IO_BASE_LOW(i
), 0);
312 start
= config
->ic_port
[i
].ir_start
;
313 pnp_write(PNP_IO_BASE_HIGH(i
), (start
>> 8) & 0xff);
314 pnp_write(PNP_IO_BASE_LOW(i
), (start
>> 0) & 0xff);
317 for (; i
< ISA_PNP_NPORT
; i
++) {
318 pnp_write(PNP_IO_BASE_HIGH(i
), 0);
319 pnp_write(PNP_IO_BASE_LOW(i
), 0);
322 for (i
= 0; i
< config
->ic_nirq
; i
++) {
325 /* XXX: interrupt type */
326 if (config
->ic_irqmask
[i
] == 0) {
327 pnp_write(PNP_IRQ_LEVEL(i
), 0);
328 pnp_write(PNP_IRQ_TYPE(i
), 2);
330 irq
= ffs(config
->ic_irqmask
[i
]) - 1;
331 pnp_write(PNP_IRQ_LEVEL(i
), irq
);
332 pnp_write(PNP_IRQ_TYPE(i
), 2); /* XXX */
335 for (; i
< ISA_PNP_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);
341 pnp_write(PNP_IRQ_TYPE(i
), 2);
344 for (i
= 0; i
< config
->ic_ndrq
; i
++) {
347 if (config
->ic_drqmask
[i
] == 0) {
348 pnp_write(PNP_DMA_CHANNEL(i
), 4);
350 drq
= ffs(config
->ic_drqmask
[i
]) - 1;
351 pnp_write(PNP_DMA_CHANNEL(i
), drq
);
354 for (; i
< ISA_PNP_NDRQ
; i
++) {
356 * DMA channel 4, the cascade channel is used to
357 * indicate no DMA channel is active.
359 pnp_write(PNP_DMA_CHANNEL(i
), 4);
362 pnp_write(PNP_ACTIVATE
, enable
? 1 : 0);
365 * Wake everyone up again, we are finished.
367 pnp_write(PNP_CONFIG_CONTROL
, PNP_CONFIG_CONTROL_WAIT_FOR_KEY
);
371 * Process quirks for a logical device.. The card must be in Config state.
374 pnp_check_quirks(uint32_t vendor_id
, uint32_t logical_id
, int ldn
,
375 struct isa_config
*config
)
377 struct pnp_quirk
*qp
;
379 for (qp
= &pnp_quirks
[0]; qp
->vendor_id
; qp
++) {
380 if (qp
->vendor_id
== vendor_id
381 && (qp
->logical_id
== 0 || qp
->logical_id
== logical_id
)) {
383 case PNP_QUIRK_WRITE_REG
:
384 pnp_write(PNP_SET_LDN
, ldn
);
385 pnp_write(qp
->arg1
, qp
->arg2
);
387 case PNP_QUIRK_EXTRA_IO
:
392 config
->ic_port
[config
->ic_nport
- 1] = config
->ic_port
[0];
393 config
->ic_port
[config
->ic_nport
- 1].ir_start
+= qp
->arg1
;
394 config
->ic_port
[config
->ic_nport
- 1].ir_end
+= qp
->arg1
;
398 config
->ic_port
[config
->ic_nport
- 1] = config
->ic_port
[0];
399 config
->ic_port
[config
->ic_nport
- 1].ir_start
+= qp
->arg2
;
400 config
->ic_port
[config
->ic_nport
- 1].ir_end
+= qp
->arg2
;
409 * Scan Resource Data for Logical Devices.
411 * This function exits as soon as it gets an error reading *ANY*
412 * Resource Data or it reaches the end of Resource Data. In the first
413 * case the return value will be TRUE, FALSE otherwise.
416 pnp_create_devices(device_t parent
, pnp_id
*p
, int csn
,
417 u_char
*resources
, int len
)
419 u_char tag
, *resp
, *resinfo
, *startres
= 0;
420 int large_len
, scanning
= len
, retval
= FALSE
;
424 struct pnp_set_config_arg
*csnldn
;
429 while (scanning
> 0) {
432 if (PNP_RES_TYPE(tag
) != 0) {
438 large_len
= resp
[0] + (resp
[1] << 8);
441 if (scanning
< large_len
) {
447 scanning
-= large_len
;
449 if (PNP_LRES_NUM(tag
) == PNP_TAG_ID_ANSI
) {
452 * This is an optional device
453 * indentifier string. Skipt it
458 /* else mandately card identifier string */
459 if (large_len
> sizeof(buf
) - 1)
460 large_len
= sizeof(buf
) - 1;
461 bcopy(resinfo
, buf
, large_len
);
464 * Trim trailing spaces.
466 while (buf
[large_len
-1] == ' ')
468 buf
[large_len
] = '\0';
477 if (scanning
< PNP_SRES_LEN(tag
)) {
482 resp
+= PNP_SRES_LEN(tag
);
483 scanning
-= PNP_SRES_LEN(tag
);;
485 switch (PNP_SRES_NUM(tag
)) {
486 case PNP_TAG_LOGICAL_DEVICE
:
488 * Parse the resources for the previous
489 * logical device (if any).
492 pnp_parse_resources(dev
, startres
,
493 resinfo
- startres
- 1, ldn
);
499 * A new logical device. Scan for end of
502 bcopy(resinfo
, &logical_id
, 4);
503 pnp_check_quirks(p
->vendor_id
, logical_id
, ldn
, NULL
);
504 dev
= BUS_ADD_CHILD(parent
, ISA_ORDER_PNP
, NULL
, -1);
506 device_set_desc_copy(dev
, desc
);
508 device_set_desc_copy(dev
,
509 pnp_eisaformat(logical_id
));
510 isa_set_vendorid(dev
, p
->vendor_id
);
511 isa_set_serial(dev
, p
->serial
);
512 isa_set_logicalid(dev
, logical_id
);
513 isa_set_configattr(dev
,
514 ISACFGATTR_CANDISABLE
| ISACFGATTR_DYNAMIC
);
515 csnldn
= malloc(sizeof *csnldn
, M_DEVBUF
, M_NOWAIT
);
517 device_printf(parent
, "out of memory\n");
523 ISA_SET_CONFIG_CALLBACK(parent
, dev
, pnp_set_config
,
531 device_printf(parent
, "malformed resources\n");
535 pnp_parse_resources(dev
, startres
,
536 resinfo
- startres
- 1, ldn
);
543 /* Skip this resource */
552 * Read 'amount' bytes of resources from the card, allocating memory
553 * as needed. If a buffer is already available, it should be passed in
554 * '*resourcesp' and its length in '*spacep'. The number of resource
555 * bytes already in the buffer should be passed in '*lenp'. The memory
556 * allocated will be returned in '*resourcesp' with its size and the
557 * number of bytes of resources in '*spacep' and '*lenp' respectively.
559 * XXX: Multiple problems here, we forget to free() stuff in one
560 * XXX: error return, and in another case we free (*resourcesp) but
561 * XXX: don't tell the caller.
564 pnp_read_bytes(int amount
, u_char
**resourcesp
, int *spacep
, int *lenp
)
566 u_char
*resources
= *resourcesp
;
573 resources
= malloc(space
, M_TEMP
, M_NOWAIT
);
578 if (len
+ amount
> space
) {
580 while (len
+ amount
> space
+ extra
)
582 newres
= malloc(space
+ extra
, M_TEMP
, M_NOWAIT
);
584 /* XXX: free resources */
587 bcopy(resources
, newres
, len
);
588 free(resources
, M_TEMP
);
593 if (pnp_get_resource_info(resources
+ len
, amount
) != amount
)
597 *resourcesp
= resources
;
605 * Read all resources from the card, allocating memory as needed. If a
606 * buffer is already available, it should be passed in '*resourcesp'
607 * and its length in '*spacep'. The memory allocated will be returned
608 * in '*resourcesp' with its size and the number of bytes of resources
609 * in '*spacep' and '*lenp' respectively.
612 pnp_read_resources(u_char
**resourcesp
, int *spacep
, int *lenp
)
614 u_char
*resources
= *resourcesp
;
623 error
= pnp_read_bytes(1, &resources
, &space
, &len
);
626 tag
= resources
[len
-1];
627 if (PNP_RES_TYPE(tag
) == 0) {
629 * Small resource, read contents.
631 error
= pnp_read_bytes(PNP_SRES_LEN(tag
),
632 &resources
, &space
, &len
);
635 if (PNP_SRES_NUM(tag
) == PNP_TAG_END
)
639 * Large resource, read length and contents.
641 error
= pnp_read_bytes(2, &resources
, &space
, &len
);
644 error
= pnp_read_bytes(resources
[len
-2]
645 + (resources
[len
-1] << 8), &resources
, &space
,
653 *resourcesp
= resources
;
660 * Run the isolation protocol. Use pnp_rd_port as the READ_DATA port
661 * value (caller should try multiple READ_DATA locations before giving
662 * up). Upon exiting, all cards are aware that they should use
663 * pnp_rd_port as the READ_DATA port.
665 * In the first pass, a csn is assigned to each board and pnp_id's
666 * are saved to an array, pnp_devices. In the second pass, each
667 * card is woken up and the device configuration is called.
670 pnp_isolation_protocol(device_t parent
)
675 u_char
*resources
= 0;
684 * Put all cards into the Sleep state so that we can clear
687 pnp_send_initiation_key();
690 * Clear the CSN for all cards.
692 pnp_write(PNP_CONFIG_CONTROL
, PNP_CONFIG_CONTROL_RESET_CSN
);
695 * Move all cards to the Isolation state.
697 pnp_write(PNP_WAKE
, 0);
700 * Tell them where the read point is going to be this time.
702 pnp_write(PNP_SET_RD_DATA
, pnp_rd_port
);
704 for (csn
= 1; csn
< PNP_MAX_CARDS
; csn
++) {
706 * Start the serial isolation protocol.
708 outb(_PNP_ADDRESS
, PNP_SERIAL_ISOLATION
);
709 DELAY(1000); /* Delay 1 msec */
711 if (pnp_get_serial(&id
)) {
713 * We have read the id from a card
714 * successfully. The card which won the
715 * isolation protocol will be in Isolation
716 * mode and all others will be in Sleep.
717 * Program the CSN of the isolated card
718 * (taking it to Config state) and read its
719 * resources, creating devices as we find
720 * logical devices on the card.
722 pnp_write(PNP_SET_CSN
, csn
);
725 printf("PnP Vendor ID = %x\n", id
.vendor_id
);
726 /* Check for NEC PnP (9 bytes serial). */
727 for (n
= necpnp
= 0; necids
[n
].vendor_id
; n
++) {
728 if (id
.vendor_id
== necids
[n
].vendor_id
) {
735 printf("An NEC-PnP card (%s).\n",
736 pnp_eisaformat(id
.vendor_id
));
737 /* Read dummy 9 bytes serial area. */
738 pnp_get_resource_info(buffer
, 9);
741 printf("A Normal-ISA-PnP card (%s).\n",
742 pnp_eisaformat(id
.vendor_id
));
745 printf("Reading PnP configuration for %s.\n",
746 pnp_eisaformat(id
.vendor_id
));
748 error
= pnp_read_resources(&resources
, &space
, &len
);
751 pnp_create_devices(parent
, &id
, csn
, resources
, len
);
757 * Put this card back to the Sleep state and
758 * simultaneously move all cards which don't have a
759 * CSN yet to Isolation state.
761 pnp_write(PNP_WAKE
, 0);
765 * Unless we have chosen the wrong read port, all cards will
766 * be in Sleep state. Put them back into WaitForKey for
767 * now. Their resources will be programmed later.
769 pnp_write(PNP_CONFIG_CONTROL
, PNP_CONFIG_CONTROL_WAIT_FOR_KEY
);
775 free(resources
, M_TEMP
);
784 * autoconfiguration of pnp devices. This routine just runs the
785 * isolation protocol over several ports, until one is successful.
787 * may be called more than once ?
792 pnp_identify(driver_t
*driver
, device_t parent
)
796 /* Try various READ_DATA ports from 0x203-0x3ff */
797 for (pnp_rd_port
= 0x80; (pnp_rd_port
< 0xff); pnp_rd_port
+= 0x10) {
799 printf("pnp_identify: Trying Read_Port at %x\n",
800 (pnp_rd_port
<< 2) | 0x3);
802 num_pnp_devs
= pnp_isolation_protocol(parent
);
807 printf("PNP Identify complete\n");
810 static device_method_t pnp_methods
[] = {
811 /* Device interface */
812 DEVMETHOD(device_identify
, pnp_identify
),
817 static driver_t pnp_driver
= {
823 static devclass_t pnp_devclass
;
825 DRIVER_MODULE(pnp
, isa
, pnp_driver
, pnp_devclass
, 0, 0);