/*-1* Copyright (c) 1998, Michael Smith2* Copyright (c) 1996, Sujal M. Patel3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*/2627/*28* Machine-independent ISA PnP enumerator implementing a subset of the29* ISA PnP specification.30*/31#include <stand.h>32#include <string.h>33#include <bootstrap.h>34#include <isapnp.h>3536#define inb(x) (archsw.arch_isainb((x)))37#define outb(x,y) (archsw.arch_isaoutb((x),(y)))3839static void isapnp_write(int d, int r);40static void isapnp_send_Initiation_LFSR(void);41static int isapnp_get_serial(uint8_t *p);42static int isapnp_isolation_protocol(void);43static void isapnp_enumerate(void);4445/* PnP read data port */46int isapnp_readport = 0;4748#define _PNP_ID_LEN 94950struct pnphandler isapnphandler =51{52"ISA bus",53isapnp_enumerate54};5556static void57isapnp_write(int d, int r)58{59outb (_PNP_ADDRESS, d);60outb (_PNP_WRITE_DATA, r);61}6263/*64* Send Initiation LFSR as described in "Plug and Play ISA Specification",65* Intel May 94.66*/67static void68isapnp_send_Initiation_LFSR(void)69{70int cur, i;7172/* Reset the LSFR */73outb(_PNP_ADDRESS, 0);74outb(_PNP_ADDRESS, 0); /* yes, we do need it twice! */7576cur = 0x6a;77outb(_PNP_ADDRESS, cur);7879for (i = 1; i < 32; i++) {80cur = (cur >> 1) | (((cur ^ (cur >> 1)) << 7) & 0xff);81outb(_PNP_ADDRESS, cur);82}83}8485/*86* Get the device's serial number. Returns 1 if the serial is valid.87*/88static int89isapnp_get_serial(uint8_t *data)90{91int i, bit, valid = 0, sum = 0x6a;9293bzero(data, _PNP_ID_LEN);94outb(_PNP_ADDRESS, SERIAL_ISOLATION);95for (i = 0; i < 72; i++) {96bit = inb(isapnp_readport) == 0x55;97delay(250); /* Delay 250 usec */9899/* Can't Short Circuit the next evaluation, so 'and' is last */100bit = (inb(isapnp_readport) == 0xaa) && bit;101delay(250); /* Delay 250 usec */102103valid = valid || bit;104105if (i < 64)106sum = (sum >> 1) |107(((sum ^ (sum >> 1) ^ bit) << 7) & 0xff);108109data[i / 8] = (data[i / 8] >> 1) | (bit ? 0x80 : 0);110}111112valid = valid && (data[8] == sum);113114return valid;115}116117/*118* Fills the buffer with resource info from the device.119* Returns nonzero if the device fails to report120*/121static int122isapnp_get_resource_info(uint8_t *buffer, int len)123{124int i, j;125u_char temp;126127for (i = 0; i < len; i++) {128outb(_PNP_ADDRESS, STATUS);129for (j = 0; j < 100; j++) {130if ((inb(isapnp_readport)) & 0x1)131break;132delay(1);133}134if (j == 100) {135printf("PnP device failed to report resource data\n");136return(1);137}138outb(_PNP_ADDRESS, RESOURCE_DATA);139temp = inb(isapnp_readport);140if (buffer != NULL)141buffer[i] = temp;142}143return(0);144}145146/*147* Scan Resource Data for useful information.148*149* We scan the resource data for compatible device IDs and150* identifier strings; we only take the first identifier string151* and assume it's for the card as a whole.152*153* Returns 0 if the scan completed OK, nonzero on error.154*/155static int156isapnp_scan_resdata(struct pnpinfo *pi)157{158u_char tag, resinfo[8];159u_int limit;160size_t large_len;161u_char *str;162163limit = 1000;164while ((limit-- > 0) && !isapnp_get_resource_info(&tag, 1)) {165if (PNP_RES_TYPE(tag) == 0) {166/* Small resource */167switch (PNP_SRES_NUM(tag)) {168169case COMP_DEVICE_ID:170/* Got a compatible device id resource */171if (isapnp_get_resource_info(resinfo, PNP_SRES_LEN(tag)))172return(1);173pnp_addident(pi, pnp_eisaformat(resinfo));174175case END_TAG:176return(0);177break;178179default:180/* Skip this resource */181if (isapnp_get_resource_info(NULL, PNP_SRES_LEN(tag)))182return(1);183break;184}185} else {186/* Large resource */187if (isapnp_get_resource_info(resinfo, 2))188return(1);189190large_len = resinfo[1];191large_len = (large_len << 8) + resinfo[0];192193switch(PNP_LRES_NUM(tag)) {194195case ID_STRING_ANSI:196str = malloc(large_len + 1);197if (isapnp_get_resource_info(str, (ssize_t)large_len)) {198free(str);199return(1);200}201str[large_len] = 0;202if (pi->pi_desc == NULL) {203pi->pi_desc = (char *)str;204} else {205free(str);206}207break;208209default:210/* Large resource, skip it */211if (isapnp_get_resource_info(NULL, (ssize_t)large_len))212return(1);213}214}215}216return(1);217}218219/*220* Run the isolation protocol. Upon exiting, all cards are aware that221* they should use isapnp_readport as the READ_DATA port.222*/223static int224isapnp_isolation_protocol(void)225{226int csn;227struct pnpinfo *pi;228uint8_t cardid[_PNP_ID_LEN];229int ndevs;230231isapnp_send_Initiation_LFSR();232ndevs = 0;233234isapnp_write(CONFIG_CONTROL, 0x04); /* Reset CSN for All Cards */235236for (csn = 1; ; csn++) {237/* Wake up cards without a CSN (ie. all of them) */238isapnp_write(WAKE, 0);239isapnp_write(SET_RD_DATA, (isapnp_readport >> 2));240outb(_PNP_ADDRESS, SERIAL_ISOLATION);241delay(1000); /* Delay 1 msec */242243if (isapnp_get_serial(cardid)) {244isapnp_write(SET_CSN, csn);245pi = pnp_allocinfo();246ndevs++;247pnp_addident(pi, pnp_eisaformat(cardid));248/* scan the card obtaining all the identifiers it holds */249if (isapnp_scan_resdata(pi)) {250pnp_freeinfo(pi); /* error getting data, ignore */251} else {252pnp_addinfo(pi);253}254} else {255break;256}257}258/* Move all cards to wait-for-key state */259while (--csn > 0) {260isapnp_send_Initiation_LFSR();261isapnp_write(WAKE, csn);262isapnp_write(CONFIG_CONTROL, 0x02);263delay(1000); /* XXX is it really necessary ? */264csn--;265}266return(ndevs);267}268269/*270* Locate ISA-PnP devices and populate the supplied list.271*/272static void273isapnp_enumerate(void)274{275int pnp_rd_port;276277/* Check for I/O port access */278if ((archsw.arch_isainb == NULL) || (archsw.arch_isaoutb == NULL))279return;280281/*282* Validate a possibly-suggested read port value. If the autoscan failed283* last time, this will return us to autoscan mode again.284*/285if ((isapnp_readport > 0) &&286(((isapnp_readport < 0x203) ||287(isapnp_readport > 0x3ff) ||288(isapnp_readport & 0x3) != 0x3)))289/* invalid, go look for ourselves */290isapnp_readport = 0;291292if (isapnp_readport < 0) {293/* someone is telling us there is no ISA in the system */294return;295296} else if (isapnp_readport > 0) {297/* Someone has told us where the port is/should be, or we found one last time */298isapnp_isolation_protocol();299300} else {301/* No clues, look for it ourselves */302for (pnp_rd_port = 0x80; pnp_rd_port < 0xff; pnp_rd_port += 0x10) {303/* Look for something, quit when we find it */304isapnp_readport = (pnp_rd_port << 2) | 0x3;305if (isapnp_isolation_protocol() > 0)306break;307}308}309}310311312