1c79df70eSToomas Soome /*
2199767f8SToomas Soome * Copyright (c) 1998, Michael Smith
3199767f8SToomas Soome * Copyright (c) 1996, Sujal M. Patel
4199767f8SToomas Soome * All rights reserved.
5199767f8SToomas Soome *
6199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
7199767f8SToomas Soome * modification, are permitted provided that the following conditions
8199767f8SToomas Soome * are met:
9199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
10199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
11199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
12199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
13199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
14199767f8SToomas Soome *
15199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25199767f8SToomas Soome * SUCH DAMAGE.
26199767f8SToomas Soome */
27199767f8SToomas Soome
28199767f8SToomas Soome #include <sys/cdefs.h>
29199767f8SToomas Soome
30199767f8SToomas Soome /*
31199767f8SToomas Soome * Machine-independant ISA PnP enumerator implementing a subset of the
32199767f8SToomas Soome * ISA PnP specification.
33199767f8SToomas Soome */
34199767f8SToomas Soome #include <stand.h>
35199767f8SToomas Soome #include <string.h>
36199767f8SToomas Soome #include <bootstrap.h>
37199767f8SToomas Soome #include <isapnp.h>
38199767f8SToomas Soome
3913bac698SToomas Soome #define inb(x) (archsw.arch_isainb((x)))
4013bac698SToomas Soome #define outb(x, y) (archsw.arch_isaoutb((x), (y)))
41199767f8SToomas Soome
42199767f8SToomas Soome static void isapnp_write(int d, int r);
43199767f8SToomas Soome static void isapnp_send_Initiation_LFSR(void);
4413bac698SToomas Soome static int isapnp_get_serial(uint8_t *p);
45199767f8SToomas Soome static int isapnp_isolation_protocol(void);
46199767f8SToomas Soome static void isapnp_enumerate(void);
47199767f8SToomas Soome
48199767f8SToomas Soome /* PnP read data port */
49199767f8SToomas Soome int isapnp_readport = 0;
50199767f8SToomas Soome
5113bac698SToomas Soome #define _PNP_ID_LEN 9
52199767f8SToomas Soome
53199767f8SToomas Soome struct pnphandler isapnphandler =
54199767f8SToomas Soome {
5513bac698SToomas Soome "ISA bus",
5613bac698SToomas Soome isapnp_enumerate
57199767f8SToomas Soome };
58199767f8SToomas Soome
59199767f8SToomas Soome static void
isapnp_write(int d,int r)60199767f8SToomas Soome isapnp_write(int d, int r)
61199767f8SToomas Soome {
6213bac698SToomas Soome outb(_PNP_ADDRESS, d);
6313bac698SToomas Soome outb(_PNP_WRITE_DATA, r);
64199767f8SToomas Soome }
65199767f8SToomas Soome
66199767f8SToomas Soome /*
67199767f8SToomas Soome * Send Initiation LFSR as described in "Plug and Play ISA Specification",
68199767f8SToomas Soome * Intel May 94.
69199767f8SToomas Soome */
70199767f8SToomas Soome static void
isapnp_send_Initiation_LFSR(void)71199767f8SToomas Soome isapnp_send_Initiation_LFSR(void)
72199767f8SToomas Soome {
7313bac698SToomas Soome int cur, i;
74199767f8SToomas Soome
7513bac698SToomas Soome /* Reset the LSFR */
7613bac698SToomas Soome outb(_PNP_ADDRESS, 0);
7713bac698SToomas Soome outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */
78199767f8SToomas Soome
7913bac698SToomas Soome cur = 0x6a;
80199767f8SToomas Soome outb(_PNP_ADDRESS, cur);
8113bac698SToomas Soome
8213bac698SToomas Soome for (i = 1; i < 32; i++) {
8313bac698SToomas Soome cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);
8413bac698SToomas Soome outb(_PNP_ADDRESS, cur);
8513bac698SToomas Soome }
86199767f8SToomas Soome }
87199767f8SToomas Soome
88199767f8SToomas Soome /*
89199767f8SToomas Soome * Get the device's serial number. Returns 1 if the serial is valid.
90199767f8SToomas Soome */
91199767f8SToomas Soome static int
isapnp_get_serial(uint8_t * data)9213bac698SToomas Soome isapnp_get_serial(uint8_t *data)
93199767f8SToomas Soome {
9413bac698SToomas Soome int i, bit, valid = 0, sum = 0x6a;
95199767f8SToomas Soome
9613bac698SToomas Soome bzero(data, _PNP_ID_LEN);
9713bac698SToomas Soome outb(_PNP_ADDRESS, SERIAL_ISOLATION);
9813bac698SToomas Soome for (i = 0; i < 72; i++) {
9913bac698SToomas Soome bit = inb(isapnp_readport) == 0x55;
10013bac698SToomas Soome delay(250); /* Delay 250 usec */
101199767f8SToomas Soome
10213bac698SToomas Soome /* Can't Short Circuit the next evaluation, so 'and' is last */
10313bac698SToomas Soome bit = (inb(isapnp_readport) == 0xaa) && bit;
10413bac698SToomas Soome delay(250); /* Delay 250 usec */
105199767f8SToomas Soome
10613bac698SToomas Soome valid = valid || bit;
107199767f8SToomas Soome
10813bac698SToomas Soome if (i < 64) {
10913bac698SToomas Soome sum = (sum >> 1) |
11013bac698SToomas Soome (((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);
11113bac698SToomas Soome }
112199767f8SToomas Soome
11313bac698SToomas Soome data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);
11413bac698SToomas Soome }
115199767f8SToomas Soome
11613bac698SToomas Soome valid = valid && (data[8] == sum);
117199767f8SToomas Soome
11813bac698SToomas Soome return (valid);
119199767f8SToomas Soome }
120199767f8SToomas Soome
121199767f8SToomas Soome /*
122199767f8SToomas Soome * Fills the buffer with resource info from the device.
123199767f8SToomas Soome * Returns nonzero if the device fails to report
124199767f8SToomas Soome */
125199767f8SToomas Soome static int
isapnp_get_resource_info(uint8_t * buffer,int len)12613bac698SToomas Soome isapnp_get_resource_info(uint8_t *buffer, int len)
127199767f8SToomas Soome {
12813bac698SToomas Soome int i, j;
12913bac698SToomas Soome uchar_t temp;
13013bac698SToomas Soome
13113bac698SToomas Soome for (i = 0; i < len; i++) {
13213bac698SToomas Soome outb(_PNP_ADDRESS, STATUS);
13313bac698SToomas Soome for (j = 0; j < 100; j++) {
13413bac698SToomas Soome if ((inb(isapnp_readport)) & 0x1)
13513bac698SToomas Soome break;
13613bac698SToomas Soome delay(1);
13713bac698SToomas Soome }
13813bac698SToomas Soome if (j == 100) {
13913bac698SToomas Soome printf("PnP device failed to report resource data\n");
14013bac698SToomas Soome return (1);
14113bac698SToomas Soome }
14213bac698SToomas Soome outb(_PNP_ADDRESS, RESOURCE_DATA);
14313bac698SToomas Soome temp = inb(isapnp_readport);
14413bac698SToomas Soome if (buffer != NULL)
14513bac698SToomas Soome buffer[i] = temp;
14613bac698SToomas Soome }
14713bac698SToomas Soome return (0);
148199767f8SToomas Soome }
149199767f8SToomas Soome
150199767f8SToomas Soome /*
151199767f8SToomas Soome * Scan Resource Data for useful information.
152199767f8SToomas Soome *
153199767f8SToomas Soome * We scan the resource data for compatible device IDs and
154199767f8SToomas Soome * identifier strings; we only take the first identifier string
155199767f8SToomas Soome * and assume it's for the card as a whole.
156199767f8SToomas Soome *
157199767f8SToomas Soome * Returns 0 if the scan completed OK, nonzero on error.
158199767f8SToomas Soome */
159199767f8SToomas Soome static int
isapnp_scan_resdata(struct pnpinfo * pi)160199767f8SToomas Soome isapnp_scan_resdata(struct pnpinfo *pi)
161199767f8SToomas Soome {
16213bac698SToomas Soome uchar_t tag, resinfo[8];
16313bac698SToomas Soome uint_t limit;
16413bac698SToomas Soome size_t large_len;
16513bac698SToomas Soome uchar_t *str;
16613bac698SToomas Soome
16713bac698SToomas Soome limit = 1000;
16813bac698SToomas Soome while ((limit-- > 0) && !isapnp_get_resource_info(&tag, 1)) {
16913bac698SToomas Soome if (PNP_RES_TYPE(tag) == 0) {
17013bac698SToomas Soome /* Small resource */
17113bac698SToomas Soome switch (PNP_SRES_NUM(tag)) {
17213bac698SToomas Soome case COMP_DEVICE_ID:
17313bac698SToomas Soome /* Got a compatible device id resource */
17413bac698SToomas Soome if (isapnp_get_resource_info(resinfo,
17513bac698SToomas Soome PNP_SRES_LEN(tag)))
17613bac698SToomas Soome return (1);
17713bac698SToomas Soome pnp_addident(pi, pnp_eisaformat(resinfo));
17813bac698SToomas Soome return (0);
17913bac698SToomas Soome
18013bac698SToomas Soome case END_TAG:
18113bac698SToomas Soome return (0);
18213bac698SToomas Soome
18313bac698SToomas Soome default:
18413bac698SToomas Soome /* Skip this resource */
18513bac698SToomas Soome if (isapnp_get_resource_info(NULL,
18613bac698SToomas Soome PNP_SRES_LEN(tag)))
18713bac698SToomas Soome return (1);
18813bac698SToomas Soome break;
18913bac698SToomas Soome }
190199767f8SToomas Soome } else {
19113bac698SToomas Soome /* Large resource */
19213bac698SToomas Soome if (isapnp_get_resource_info(resinfo, 2))
19313bac698SToomas Soome return (1);
19413bac698SToomas Soome
19513bac698SToomas Soome large_len = resinfo[1];
19613bac698SToomas Soome large_len = (large_len << 8) + resinfo[0];
19713bac698SToomas Soome
19813bac698SToomas Soome switch (PNP_LRES_NUM(tag)) {
19913bac698SToomas Soome case ID_STRING_ANSI:
20013bac698SToomas Soome str = malloc(large_len + 1);
20113bac698SToomas Soome if (isapnp_get_resource_info(str,
20213bac698SToomas Soome (ssize_t)large_len)) {
20313bac698SToomas Soome free(str);
20413bac698SToomas Soome return (1);
20513bac698SToomas Soome }
20613bac698SToomas Soome str[large_len] = 0;
20713bac698SToomas Soome if (pi->pi_desc == NULL) {
20813bac698SToomas Soome pi->pi_desc = (char *)str;
20913bac698SToomas Soome } else {
21013bac698SToomas Soome free(str);
21113bac698SToomas Soome }
21213bac698SToomas Soome break;
21313bac698SToomas Soome
21413bac698SToomas Soome default:
21513bac698SToomas Soome /* Large resource, skip it */
21613bac698SToomas Soome if (isapnp_get_resource_info(NULL,
21713bac698SToomas Soome (ssize_t)large_len))
21813bac698SToomas Soome return (1);
21913bac698SToomas Soome }
220199767f8SToomas Soome }
221199767f8SToomas Soome }
22213bac698SToomas Soome return (1);
223199767f8SToomas Soome }
224199767f8SToomas Soome
225199767f8SToomas Soome /*
226199767f8SToomas Soome * Run the isolation protocol. Upon exiting, all cards are aware that
227199767f8SToomas Soome * they should use isapnp_readport as the READ_DATA port.
228199767f8SToomas Soome */
229199767f8SToomas Soome static int
isapnp_isolation_protocol(void)230199767f8SToomas Soome isapnp_isolation_protocol(void)
231199767f8SToomas Soome {
23213bac698SToomas Soome int csn;
23313bac698SToomas Soome struct pnpinfo *pi;
23413bac698SToomas Soome uint8_t cardid[_PNP_ID_LEN];
23513bac698SToomas Soome int ndevs;
23613bac698SToomas Soome
237199767f8SToomas Soome isapnp_send_Initiation_LFSR();
23813bac698SToomas Soome ndevs = 0;
23913bac698SToomas Soome
24013bac698SToomas Soome isapnp_write(CONFIG_CONTROL, 0x04); /* Reset CSN for All Cards */
24113bac698SToomas Soome
24213bac698SToomas Soome for (csn = 1; ; csn++) {
24313bac698SToomas Soome /* Wake up cards without a CSN (ie. all of them) */
24413bac698SToomas Soome isapnp_write(WAKE, 0);
24513bac698SToomas Soome isapnp_write(SET_RD_DATA, (isapnp_readport >> 2));
24613bac698SToomas Soome outb(_PNP_ADDRESS, SERIAL_ISOLATION);
24713bac698SToomas Soome delay(1000); /* Delay 1 msec */
24813bac698SToomas Soome
24913bac698SToomas Soome if (isapnp_get_serial(cardid)) {
25013bac698SToomas Soome isapnp_write(SET_CSN, csn);
25113bac698SToomas Soome pi = pnp_allocinfo();
25213bac698SToomas Soome ndevs++;
25313bac698SToomas Soome pnp_addident(pi, pnp_eisaformat(cardid));
25413bac698SToomas Soome /*
25513bac698SToomas Soome * scan the card obtaining all the identifiers it holds
25613bac698SToomas Soome */
25713bac698SToomas Soome if (isapnp_scan_resdata(pi)) {
25813bac698SToomas Soome /* error getting data, ignore */
25913bac698SToomas Soome pnp_freeinfo(pi);
26013bac698SToomas Soome } else {
26113bac698SToomas Soome pnp_addinfo(pi);
26213bac698SToomas Soome }
26313bac698SToomas Soome } else {
26413bac698SToomas Soome break;
26513bac698SToomas Soome }
26613bac698SToomas Soome }
26713bac698SToomas Soome /* Move all cards to wait-for-key state */
26813bac698SToomas Soome while (--csn > 0) {
26913bac698SToomas Soome isapnp_send_Initiation_LFSR();
27013bac698SToomas Soome isapnp_write(WAKE, csn);
27113bac698SToomas Soome isapnp_write(CONFIG_CONTROL, 0x02);
27213bac698SToomas Soome delay(1000); /* XXX is it really necessary ? */
27313bac698SToomas Soome csn--;
27413bac698SToomas Soome }
27513bac698SToomas Soome return (ndevs);
276199767f8SToomas Soome }
277199767f8SToomas Soome
278199767f8SToomas Soome /*
279199767f8SToomas Soome * Locate ISA-PnP devices and populate the supplied list.
280199767f8SToomas Soome */
281199767f8SToomas Soome static void
isapnp_enumerate(void)28213bac698SToomas Soome isapnp_enumerate(void)
283199767f8SToomas Soome {
28413bac698SToomas Soome int pnp_rd_port;
28513bac698SToomas Soome
28613bac698SToomas Soome /* Check for I/O port access */
28713bac698SToomas Soome if ((archsw.arch_isainb == NULL) || (archsw.arch_isaoutb == NULL))
28813bac698SToomas Soome return;
28913bac698SToomas Soome
29013bac698SToomas Soome /*
29113bac698SToomas Soome * Validate a possibly-suggested read port value. If the autoscan failed
29213bac698SToomas Soome * last time, this will return us to autoscan mode again.
29313bac698SToomas Soome */
29413bac698SToomas Soome if ((isapnp_readport > 0) &&
29513bac698SToomas Soome (((isapnp_readport < 0x203) ||
29613bac698SToomas Soome (isapnp_readport > 0x3ff) ||
29713bac698SToomas Soome (isapnp_readport & 0x3) != 0x3))) {
29813bac698SToomas Soome /* invalid, go look for ourselves */
29913bac698SToomas Soome isapnp_readport = 0;
30013bac698SToomas Soome }
30113bac698SToomas Soome
30213bac698SToomas Soome if (isapnp_readport < 0) {
30313bac698SToomas Soome /* someone is telling us there is no ISA in the system */
30413bac698SToomas Soome return;
30513bac698SToomas Soome } else if (isapnp_readport > 0) {
30613bac698SToomas Soome /*
30713bac698SToomas Soome * Someone has told us where the port is/should be,
30813bac698SToomas Soome * or we found one last time.
30913bac698SToomas Soome */
31013bac698SToomas Soome isapnp_isolation_protocol();
31113bac698SToomas Soome } else {
31213bac698SToomas Soome /* No clues, look for it ourselves */
31313bac698SToomas Soome for (pnp_rd_port = 0x80; pnp_rd_port < 0xff;
31413bac698SToomas Soome pnp_rd_port += 0x10) {
31513bac698SToomas Soome /* Look for something, quit when we find it */
31613bac698SToomas Soome isapnp_readport = (pnp_rd_port << 2) | 0x3;
31713bac698SToomas Soome if (isapnp_isolation_protocol() > 0)
31813bac698SToomas Soome break;
31913bac698SToomas Soome }
320199767f8SToomas Soome }
321199767f8SToomas Soome }
322