157aea934SToomas Soome /*
2199767f8SToomas Soome * Copyright (c) 1998 Michael Smith <msmith@freebsd.org>
3199767f8SToomas Soome * All rights reserved.
4199767f8SToomas Soome *
5199767f8SToomas Soome * Redistribution and use in source and binary forms, with or without
6199767f8SToomas Soome * modification, are permitted provided that the following conditions
7199767f8SToomas Soome * are met:
8199767f8SToomas Soome * 1. Redistributions of source code must retain the above copyright
9199767f8SToomas Soome * notice, this list of conditions and the following disclaimer.
10199767f8SToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
11199767f8SToomas Soome * notice, this list of conditions and the following disclaimer in the
12199767f8SToomas Soome * documentation and/or other materials provided with the distribution.
13199767f8SToomas Soome *
14199767f8SToomas Soome * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15199767f8SToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16199767f8SToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17199767f8SToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18199767f8SToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19199767f8SToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20199767f8SToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21199767f8SToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22199767f8SToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23199767f8SToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24199767f8SToomas Soome * SUCH DAMAGE.
25199767f8SToomas Soome */
26199767f8SToomas Soome
27199767f8SToomas Soome #include <sys/cdefs.h>
28199767f8SToomas Soome
29199767f8SToomas Soome /*
30199767f8SToomas Soome * PnP BIOS enumerator.
31199767f8SToomas Soome */
32199767f8SToomas Soome
33199767f8SToomas Soome #include <stand.h>
34199767f8SToomas Soome #include <machine/stdarg.h>
35199767f8SToomas Soome #include <bootstrap.h>
36199767f8SToomas Soome #include <isapnp.h>
37199767f8SToomas Soome #include <btxv86.h>
38199767f8SToomas Soome
39199767f8SToomas Soome
40199767f8SToomas Soome static int biospnp_init(void);
41199767f8SToomas Soome static void biospnp_enumerate(void);
42199767f8SToomas Soome
43199767f8SToomas Soome struct pnphandler biospnphandler =
44199767f8SToomas Soome {
45199767f8SToomas Soome "PnP BIOS",
46199767f8SToomas Soome biospnp_enumerate
47199767f8SToomas Soome };
48199767f8SToomas Soome
49199767f8SToomas Soome struct pnp_ICstructure
50199767f8SToomas Soome {
51199767f8SToomas Soome u_int8_t pnp_signature[4];
52199767f8SToomas Soome u_int8_t pnp_version;
53199767f8SToomas Soome u_int8_t pnp_length;
54199767f8SToomas Soome u_int16_t pnp_BIOScontrol;
55199767f8SToomas Soome u_int8_t pnp_checksum;
56199767f8SToomas Soome u_int32_t pnp_eventflag;
57199767f8SToomas Soome u_int16_t pnp_rmip;
58199767f8SToomas Soome u_int16_t pnp_rmcs;
59199767f8SToomas Soome u_int16_t pnp_pmip;
60199767f8SToomas Soome u_int32_t pnp_pmcs;
61199767f8SToomas Soome u_int8_t pnp_OEMdev[4];
62199767f8SToomas Soome u_int16_t pnp_rmds;
63199767f8SToomas Soome u_int32_t pnp_pmds;
64199767f8SToomas Soome } __packed;
65199767f8SToomas Soome
66*55fea89dSDan Cross struct pnp_devNode
67199767f8SToomas Soome {
68199767f8SToomas Soome u_int16_t dn_size;
69199767f8SToomas Soome u_int8_t dn_handle;
70199767f8SToomas Soome u_int8_t dn_id[4];
71199767f8SToomas Soome u_int8_t dn_type[3];
72199767f8SToomas Soome u_int16_t dn_attrib;
73199767f8SToomas Soome u_int8_t dn_data[1];
74199767f8SToomas Soome } __packed;
75199767f8SToomas Soome
76199767f8SToomas Soome struct pnp_isaConfiguration
77199767f8SToomas Soome {
78199767f8SToomas Soome u_int8_t ic_revision;
79199767f8SToomas Soome u_int8_t ic_nCSN;
80199767f8SToomas Soome u_int16_t ic_rdport;
81199767f8SToomas Soome u_int16_t ic_reserved;
82199767f8SToomas Soome } __packed;
83199767f8SToomas Soome
84199767f8SToomas Soome static struct pnp_ICstructure *pnp_Icheck = NULL;
85199767f8SToomas Soome static u_int16_t pnp_NumNodes;
86199767f8SToomas Soome static u_int16_t pnp_NodeSize;
87199767f8SToomas Soome
88199767f8SToomas Soome static void biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn);
89199767f8SToomas Soome static int biospnp_call(int func, const char *fmt, ...);
90199767f8SToomas Soome
91199767f8SToomas Soome #define vsegofs(vptr) (((u_int32_t)VTOPSEG(vptr) << 16) + VTOPOFF(vptr))
92199767f8SToomas Soome
93199767f8SToomas Soome typedef void v86bios_t(u_int32_t, u_int32_t, u_int32_t, u_int32_t);
94199767f8SToomas Soome v86bios_t *v86bios = (v86bios_t *)v86int;
95199767f8SToomas Soome
96199767f8SToomas Soome #define biospnp_f00(NumNodes, NodeSize) biospnp_call(0x00, "ll", NumNodes, NodeSize)
97199767f8SToomas Soome #define biospnp_f01(Node, devNodeBuffer, Control) biospnp_call(0x01, "llw", Node, devNodeBuffer, Control)
98199767f8SToomas Soome #define biospnp_f40(Configuration) biospnp_call(0x40, "l", Configuration)
99199767f8SToomas Soome
100199767f8SToomas Soome /* PnP BIOS return codes */
101199767f8SToomas Soome #define PNP_SUCCESS 0x00
102199767f8SToomas Soome #define PNP_FUNCTION_NOT_SUPPORTED 0x80
103199767f8SToomas Soome
104199767f8SToomas Soome /*
105199767f8SToomas Soome * Initialisation: locate the PnP BIOS, test that we can call it.
106199767f8SToomas Soome * Returns nonzero if the PnP BIOS is not usable on this system.
107199767f8SToomas Soome */
108199767f8SToomas Soome static int
biospnp_init(void)109199767f8SToomas Soome biospnp_init(void)
110199767f8SToomas Soome {
111199767f8SToomas Soome struct pnp_isaConfiguration icfg;
112199767f8SToomas Soome char *sigptr;
113199767f8SToomas Soome int result;
114*55fea89dSDan Cross
115199767f8SToomas Soome /* Search for the $PnP signature */
116199767f8SToomas Soome pnp_Icheck = NULL;
117199767f8SToomas Soome for (sigptr = PTOV(0xf0000); sigptr < PTOV(0xfffff); sigptr += 16)
118199767f8SToomas Soome if (!bcmp(sigptr, "$PnP", 4)) {
119199767f8SToomas Soome pnp_Icheck = (struct pnp_ICstructure *)sigptr;
120199767f8SToomas Soome break;
121199767f8SToomas Soome }
122*55fea89dSDan Cross
123199767f8SToomas Soome /* No signature, no BIOS */
124199767f8SToomas Soome if (pnp_Icheck == NULL)
125199767f8SToomas Soome return(1);
126199767f8SToomas Soome
127199767f8SToomas Soome /*
128199767f8SToomas Soome * Fetch the system table parameters as a test of the BIOS
129199767f8SToomas Soome */
130199767f8SToomas Soome result = biospnp_f00(vsegofs(&pnp_NumNodes), vsegofs(&pnp_NodeSize));
131199767f8SToomas Soome if (result != PNP_SUCCESS) {
132199767f8SToomas Soome return(1);
133199767f8SToomas Soome }
134199767f8SToomas Soome
135199767f8SToomas Soome /*
136*55fea89dSDan Cross * Look for the PnP ISA configuration table
137199767f8SToomas Soome */
138199767f8SToomas Soome result = biospnp_f40(vsegofs(&icfg));
139199767f8SToomas Soome switch (result) {
140199767f8SToomas Soome case PNP_SUCCESS:
141199767f8SToomas Soome /* If the BIOS found some PnP devices, take its hint for the read port */
142199767f8SToomas Soome if ((icfg.ic_revision == 1) && (icfg.ic_nCSN > 0))
143199767f8SToomas Soome isapnp_readport = icfg.ic_rdport;
144199767f8SToomas Soome break;
145199767f8SToomas Soome case PNP_FUNCTION_NOT_SUPPORTED:
146199767f8SToomas Soome /* The BIOS says there is no ISA bus (should we trust that this works?) */
147199767f8SToomas Soome printf("PnP BIOS claims no ISA bus\n");
148199767f8SToomas Soome isapnp_readport = -1;
149199767f8SToomas Soome break;
150199767f8SToomas Soome }
151199767f8SToomas Soome return(0);
152199767f8SToomas Soome }
153199767f8SToomas Soome
154199767f8SToomas Soome static void
biospnp_enumerate(void)155199767f8SToomas Soome biospnp_enumerate(void)
156199767f8SToomas Soome {
157199767f8SToomas Soome u_int8_t Node;
158199767f8SToomas Soome struct pnp_devNode *devNodeBuffer;
159199767f8SToomas Soome int result;
160199767f8SToomas Soome struct pnpinfo *pi;
161199767f8SToomas Soome int count;
162199767f8SToomas Soome
163199767f8SToomas Soome /* Init/check state */
164199767f8SToomas Soome if (biospnp_init())
165199767f8SToomas Soome return;
166199767f8SToomas Soome
167199767f8SToomas Soome devNodeBuffer = (struct pnp_devNode *)alloca(pnp_NodeSize);
168199767f8SToomas Soome Node = 0;
169199767f8SToomas Soome count = 1000;
170199767f8SToomas Soome while((Node != 0xff) && (count-- > 0)) {
171199767f8SToomas Soome result = biospnp_f01(vsegofs(&Node), vsegofs(devNodeBuffer), 0x1);
172199767f8SToomas Soome if (result != PNP_SUCCESS) {
173199767f8SToomas Soome printf("PnP BIOS node %d: error 0x%x\n", Node, result);
174199767f8SToomas Soome } else {
175199767f8SToomas Soome pi = pnp_allocinfo();
176199767f8SToomas Soome pnp_addident(pi, pnp_eisaformat(devNodeBuffer->dn_id));
177199767f8SToomas Soome biospnp_scanresdata(pi, devNodeBuffer);
178199767f8SToomas Soome pnp_addinfo(pi);
179199767f8SToomas Soome }
180199767f8SToomas Soome }
181199767f8SToomas Soome }
182199767f8SToomas Soome
183199767f8SToomas Soome /*
184199767f8SToomas Soome * Scan the resource data in the node's data area for compatible device IDs
185199767f8SToomas Soome * and descriptions.
186199767f8SToomas Soome */
187199767f8SToomas Soome static void
biospnp_scanresdata(struct pnpinfo * pi,struct pnp_devNode * dn)188199767f8SToomas Soome biospnp_scanresdata(struct pnpinfo *pi, struct pnp_devNode *dn)
189199767f8SToomas Soome {
190199767f8SToomas Soome u_int tag, i, rlen, dlen;
191199767f8SToomas Soome u_int8_t *p;
192199767f8SToomas Soome char *str;
193199767f8SToomas Soome
194199767f8SToomas Soome p = dn->dn_data; /* point to resource data */
195199767f8SToomas Soome dlen = dn->dn_size - (p - (u_int8_t *)dn); /* length of resource data */
196199767f8SToomas Soome
197199767f8SToomas Soome for (i = 0; i < dlen; i+= rlen) {
198199767f8SToomas Soome tag = p[i];
199199767f8SToomas Soome i++;
200199767f8SToomas Soome if (PNP_RES_TYPE(tag) == 0) {
201199767f8SToomas Soome rlen = PNP_SRES_LEN(tag);
202199767f8SToomas Soome /* small resource */
203199767f8SToomas Soome switch (PNP_SRES_NUM(tag)) {
204199767f8SToomas Soome
205199767f8SToomas Soome case COMP_DEVICE_ID:
206199767f8SToomas Soome /* got a compatible device ID */
207199767f8SToomas Soome pnp_addident(pi, pnp_eisaformat(p + i));
208199767f8SToomas Soome break;
209*55fea89dSDan Cross
210199767f8SToomas Soome case END_TAG:
211199767f8SToomas Soome return;
212199767f8SToomas Soome }
213199767f8SToomas Soome } else {
214199767f8SToomas Soome /* large resource */
215199767f8SToomas Soome rlen = *(u_int16_t *)(p + i);
216199767f8SToomas Soome i += sizeof(u_int16_t);
217*55fea89dSDan Cross
218199767f8SToomas Soome switch(PNP_LRES_NUM(tag)) {
219199767f8SToomas Soome
220199767f8SToomas Soome case ID_STRING_ANSI:
221199767f8SToomas Soome str = malloc(rlen + 1);
222199767f8SToomas Soome bcopy(p + i, str, rlen);
223199767f8SToomas Soome str[rlen] = 0;
224199767f8SToomas Soome if (pi->pi_desc == NULL) {
225199767f8SToomas Soome pi->pi_desc = str;
226199767f8SToomas Soome } else {
227199767f8SToomas Soome free(str);
228199767f8SToomas Soome }
229199767f8SToomas Soome break;
230199767f8SToomas Soome }
231199767f8SToomas Soome }
232199767f8SToomas Soome }
233199767f8SToomas Soome }
234199767f8SToomas Soome
235199767f8SToomas Soome
236199767f8SToomas Soome /*
237199767f8SToomas Soome * Make a 16-bit realmode PnP BIOS call.
238199767f8SToomas Soome *
239199767f8SToomas Soome * The first argument passed is the function number, the last is the
240*55fea89dSDan Cross * BIOS data segment selector. Intermediate arguments may be 16 or
241199767f8SToomas Soome * 32 bytes in length, and are described by the format string.
242199767f8SToomas Soome *
243199767f8SToomas Soome * Arguments to the BIOS functions must be packed on the stack, hence
244199767f8SToomas Soome * this evil.
245199767f8SToomas Soome */
246199767f8SToomas Soome static int
biospnp_call(int func,const char * fmt,...)247199767f8SToomas Soome biospnp_call(int func, const char *fmt, ...)
248199767f8SToomas Soome {
249199767f8SToomas Soome va_list ap;
250199767f8SToomas Soome const char *p;
25157aea934SToomas Soome uint8_t *argp;
25257aea934SToomas Soome uint16_t int16;
25357aea934SToomas Soome uint32_t args[4];
25457aea934SToomas Soome uint32_t i;
255199767f8SToomas Soome
256199767f8SToomas Soome /* function number first */
25757aea934SToomas Soome argp = (uint8_t *)args;
25857aea934SToomas Soome int16 = func;
25957aea934SToomas Soome bcopy(&int16, argp, sizeof (int16));
26057aea934SToomas Soome argp += sizeof(uint16_t);
261199767f8SToomas Soome
262199767f8SToomas Soome /* take args according to format */
263199767f8SToomas Soome va_start(ap, fmt);
264199767f8SToomas Soome for (p = fmt; *p != 0; p++) {
265199767f8SToomas Soome switch(*p) {
266199767f8SToomas Soome case 'w':
26757aea934SToomas Soome i = va_arg(ap, uint32_t);
26857aea934SToomas Soome int16 = i;
26957aea934SToomas Soome bcopy(&int16, argp, sizeof (int16));
27057aea934SToomas Soome argp += sizeof (uint16_t);
271199767f8SToomas Soome break;
272*55fea89dSDan Cross
273199767f8SToomas Soome case 'l':
27457aea934SToomas Soome i = va_arg(ap, uint32_t);
27557aea934SToomas Soome bcopy(&i, argp, sizeof (i));
27657aea934SToomas Soome argp += sizeof (uint32_t);
277199767f8SToomas Soome break;
278199767f8SToomas Soome }
279199767f8SToomas Soome }
280199767f8SToomas Soome va_end(ap);
281199767f8SToomas Soome
282199767f8SToomas Soome /* BIOS segment last */
28357aea934SToomas Soome int16 = pnp_Icheck->pnp_rmds;
28457aea934SToomas Soome bcopy(&int16, argp, sizeof (int16));
28557aea934SToomas Soome argp += sizeof(uint16_t);
286199767f8SToomas Soome
287199767f8SToomas Soome /* prepare for call */
288*55fea89dSDan Cross v86.ctl = V86_ADDR | V86_CALLF;
28957aea934SToomas Soome v86.addr = ((uint32_t)pnp_Icheck->pnp_rmcs << 16) + pnp_Icheck->pnp_rmip;
290*55fea89dSDan Cross
291199767f8SToomas Soome /* call with packed stack and return */
292199767f8SToomas Soome v86bios(args[0], args[1], args[2], args[3]);
29357aea934SToomas Soome return (v86.eax & 0xffff);
294199767f8SToomas Soome }
295