xref: /dragonfly/sys/dev/misc/orm/orm.c (revision 768af85b)
1 /*-
2  * Copyright (c) 2000 Nikolai Saoukh
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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
24  * SUCH DAMAGE.
25  *
26  *	$FreeBSD: src/sys/isa/orm.c,v 1.1.2.1 2001/06/19 05:48:29 imp Exp $
27  *	$DragonFly: src/sys/dev/misc/orm/orm.c,v 1.6 2006/10/25 20:55:54 dillon Exp $
28  */
29 
30 /*
31  * Driver to take care of holes in ISA I/O memory occupied
32  * by option rom(s)
33  */
34 
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/socket.h>
39 #include <sys/module.h>
40 #include <sys/bus.h>
41 #include <sys/rman.h>
42 
43 #include <bus/isa/isavar.h>
44 #include <bus/isa/pnpvar.h>
45 
46 #define	IOMEM_START	0x0a0000
47 #define	IOMEM_STEP	0x000800
48 #define	IOMEM_END	0x100000
49 
50 #define	ORM_ID	0x00004d3e
51 
52 static struct isa_pnp_id orm_ids[] = {
53 	{ ORM_ID,	NULL },		/* ORM0000 */
54 	{ 0,		NULL },
55 };
56 
57 #define MAX_ROMS	16
58 
59 struct orm_softc {
60 	int		rnum;
61 	int		rid[MAX_ROMS];
62 	struct resource *res[MAX_ROMS];
63 };
64 
65 static int
66 orm_probe(device_t dev)
67 {
68 	return (ISA_PNP_PROBE(device_get_parent(dev), dev, orm_ids));
69 }
70 
71 static int
72 orm_attach(device_t dev)
73 {
74 	return (0);
75 }
76 
77 static int
78 orm_identify(driver_t* driver, device_t parent)
79 {
80 	bus_space_handle_t	bh;
81 	bus_space_tag_t		bt;
82 	device_t		child;
83 	u_int32_t		chunk = IOMEM_START;
84 	struct resource		*res;
85 	int			rid;
86 	u_int32_t		rom_size;
87 	struct orm_softc	*sc;
88 	u_int8_t		buf[3];
89 
90 	/*
91 	 * rescanning the isa bus, do nothing
92 	 */
93 	if (device_get_state(parent) == DS_ATTACHED)
94 		return (0);
95 
96 	/*
97 	 * Otherwise see if it exists
98 	 */
99 	child = BUS_ADD_CHILD(parent, 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 (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(child, SYS_RES_MEMORY, &rid, 0ul,
110 		    ~0ul, IOMEM_STEP, 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(child, SYS_RES_MEMORY, &rid, 0ul,
142 		    ~0ul, rom_size, 0);
143 		if (res == NULL) {
144 			bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
145 			chunk += IOMEM_STEP;
146 			continue;
147 		}
148 		sc->rid[sc->rnum] = rid;
149 		sc->res[sc->rnum] = res;
150 		sc->rnum++;
151 		chunk += rom_size;
152 	}
153 
154 	/*
155 	 * note: sc becomes invalid after we delete the child.
156 	 */
157 	if (sc->rnum == 0) {
158 		device_delete_child(parent, child);
159 		return (ENXIO);
160 	}
161 	if (sc->rnum == 1)
162 		device_set_desc(child, "Option ROM");
163 	else
164 		device_set_desc(child, "Option ROMs");
165 	return (0);
166 }
167 
168 static int
169 orm_detach(device_t dev)
170 {
171 	int			i;
172 	struct orm_softc	*sc = device_get_softc(dev);
173 
174 	for (i = 0; i < sc->rnum; i++)
175 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid[i],
176 		    sc->res[i]);
177 	return (0);
178 }
179 
180 static device_method_t orm_methods[] = {
181 	/* Device interface */
182 	DEVMETHOD(device_identify,	orm_identify),
183 	DEVMETHOD(device_probe,		orm_probe),
184 	DEVMETHOD(device_attach,	orm_attach),
185 	DEVMETHOD(device_detach,	orm_detach),
186 	{ 0, 0 }
187 };
188 
189 static driver_t orm_driver = {
190 	"orm",
191 	orm_methods,
192 	sizeof (struct orm_softc)
193 };
194 
195 static devclass_t orm_devclass;
196 
197 DRIVER_MODULE(orm, isa, orm_driver, orm_devclass, 0, 0);
198