xref: /freebsd/sys/x86/isa/orm.c (revision 37c8ee8847faa53432809cae2ecc11b80c4eab2f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2000 Nikolai Saoukh
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /*
33  * Driver to take care of holes in ISA I/O memory occupied
34  * by option rom(s)
35  */
36 
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/socket.h>
41 
42 #include <sys/module.h>
43 #include <sys/bus.h>
44 
45 #include <machine/bus.h>
46 #include <machine/resource.h>
47 #include <sys/rman.h>
48 
49 #include <isa/isavar.h>
50 #include <isa/pnpvar.h>
51 
52 #define	IOMEM_START	0x0a0000
53 #define	IOMEM_STEP	0x000800
54 #define	IOMEM_END	0x100000
55 
56 #define	ORM_ID	0x00004d3e
57 
58 static struct isa_pnp_id orm_ids[] = {
59 	{ ORM_ID,	NULL },		/* ORM0000 */
60 	{ 0,		NULL },
61 };
62 
63 #define MAX_ROMS	32
64 
65 struct orm_softc {
66 	int		rnum;
67 	int		rid[MAX_ROMS];
68 	struct resource *res[MAX_ROMS];
69 };
70 
71 static int
72 orm_probe(device_t dev)
73 {
74 	return (ISA_PNP_PROBE(device_get_parent(dev), dev, orm_ids));
75 }
76 
77 static int
78 orm_attach(device_t dev)
79 {
80 	return (0);
81 }
82 
83 static void
84 orm_identify(driver_t* driver, device_t parent)
85 {
86 	bus_space_handle_t	bh;
87 	bus_space_tag_t		bt;
88 	device_t		child;
89 	u_int32_t		chunk = IOMEM_START;
90 	struct resource		*res;
91 	int			rid;
92 	u_int32_t		rom_size;
93 	struct orm_softc	*sc;
94 	u_int8_t		buf[3];
95 
96 	if (resource_disabled("orm", 0))
97 		return;
98 
99 	child = BUS_ADD_CHILD(parent, ISA_ORDER_SENSITIVE, "orm", -1);
100 	device_set_driver(child, driver);
101 	isa_set_logicalid(child, ORM_ID);
102 	isa_set_vendorid(child, ORM_ID);
103 	sc = device_get_softc(child);
104 	sc->rnum = 0;
105 	while (sc->rnum < MAX_ROMS && chunk < IOMEM_END) {
106 		bus_set_resource(child, SYS_RES_MEMORY, sc->rnum, chunk,
107 		    IOMEM_STEP);
108 		rid = sc->rnum;
109 		res = bus_alloc_resource_any(child, SYS_RES_MEMORY, &rid,
110 		    RF_ACTIVE);
111 		if (res == NULL) {
112 			bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
113 			chunk += IOMEM_STEP;
114 			continue;
115 		}
116 		bt = rman_get_bustag(res);
117 		bh = rman_get_bushandle(res);
118 		bus_space_read_region_1(bt, bh, 0, buf, sizeof(buf));
119 
120 		/*
121 		 * We need to release and delete the resource since we're
122 		 * changing its size, or the rom isn't there.  There
123 		 * is a checksum field in the ROM to prevent false
124 		 * positives.  However, some common hardware (IBM thinkpads)
125 		 * neglects to put a valid checksum in the ROM, so we do
126 		 * not double check the checksum here.  On the ISA bus
127 		 * areas that have no hardware read back as 0xff, so the
128 		 * tests to see if we have 0x55 followed by 0xaa are
129 		 * generally sufficient.
130 		 */
131 		bus_release_resource(child, SYS_RES_MEMORY, rid, res);
132 		bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
133 		if (buf[0] != 0x55 || buf[1] != 0xAA || (buf[2] & 0x03) != 0) {
134 			chunk += IOMEM_STEP;
135 			continue;
136 		}
137 		rom_size = buf[2] << 9;
138 		bus_set_resource(child, SYS_RES_MEMORY, sc->rnum, chunk,
139 		    rom_size);
140 		rid = sc->rnum;
141 		res = bus_alloc_resource_any(child, SYS_RES_MEMORY, &rid, 0);
142 		if (res == NULL) {
143 			bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
144 			chunk += IOMEM_STEP;
145 			continue;
146 		}
147 		sc->rid[sc->rnum] = rid;
148 		sc->res[sc->rnum] = res;
149 		sc->rnum++;
150 		chunk += rom_size;
151 	}
152 
153 	if (sc->rnum == 0)
154 		device_delete_child(parent, child);
155 	else if (sc->rnum == 1)
156 		device_set_desc(child, "ISA Option ROM");
157 	else
158 		device_set_desc(child, "ISA Option ROMs");
159 }
160 
161 static int
162 orm_detach(device_t dev)
163 {
164 	int			i;
165 	struct orm_softc	*sc = device_get_softc(dev);
166 
167 	for (i = 0; i < sc->rnum; i++)
168 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid[i],
169 		    sc->res[i]);
170 	return (0);
171 }
172 
173 static device_method_t orm_methods[] = {
174 	/* Device interface */
175 	DEVMETHOD(device_identify,	orm_identify),
176 	DEVMETHOD(device_probe,		orm_probe),
177 	DEVMETHOD(device_attach,	orm_attach),
178 	DEVMETHOD(device_detach,	orm_detach),
179 	{ 0, 0 }
180 };
181 
182 static driver_t orm_driver = {
183 	"orm",
184 	orm_methods,
185 	sizeof (struct orm_softc)
186 };
187 
188 DRIVER_MODULE(orm, isa, orm_driver, 0, 0);
189 ISA_PNP_INFO(orm_ids);
190