xref: /dragonfly/sys/dev/misc/orm/orm.c (revision 49781055)
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.5 2005/10/30 04:41:15 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 
40 #include <sys/module.h>
41 #include <sys/bus.h>
42 
43 #include <machine/bus.h>
44 #include <machine/resource.h>
45 #include <sys/rman.h>
46 
47 #include <bus/isa/isavar.h>
48 #include <bus/isa/pnpvar.h>
49 
50 #define	IOMEM_START	0x0a0000
51 #define	IOMEM_STEP	0x000800
52 #define	IOMEM_END	0x100000
53 
54 #define	ORM_ID	0x00004d3e
55 
56 static struct isa_pnp_id orm_ids[] = {
57 	{ ORM_ID,	NULL },		/* ORM0000 */
58 	{ 0,		NULL },
59 };
60 
61 #define MAX_ROMS	16
62 
63 struct orm_softc {
64 	int		rnum;
65 	int		rid[MAX_ROMS];
66 	struct resource *res[MAX_ROMS];
67 };
68 
69 static int
70 orm_probe(device_t dev)
71 {
72 	return (ISA_PNP_PROBE(device_get_parent(dev), dev, orm_ids));
73 }
74 
75 static int
76 orm_attach(device_t dev)
77 {
78 	return (0);
79 }
80 
81 static int
82 orm_identify(driver_t* driver, device_t parent)
83 {
84 	bus_space_handle_t	bh;
85 	bus_space_tag_t		bt;
86 	device_t		child;
87 	u_int32_t		chunk = IOMEM_START;
88 	struct resource		*res;
89 	int			rid;
90 	u_int32_t		rom_size;
91 	struct orm_softc	*sc;
92 	u_int8_t		buf[3];
93 
94 	/*
95 	 * rescanning the isa bus, do nothing
96 	 */
97 	if (device_get_state(parent) == DS_ATTACHED)
98 		return (0);
99 
100 	/*
101 	 * Otherwise see if it exists
102 	 */
103 	child = BUS_ADD_CHILD(parent, parent, ISA_ORDER_SENSITIVE, "orm", -1);
104 	device_set_driver(child, driver);
105 	isa_set_logicalid(child, ORM_ID);
106 	isa_set_vendorid(child, ORM_ID);
107 	sc = device_get_softc(child);
108 	sc->rnum = 0;
109 	while (chunk < IOMEM_END) {
110 		bus_set_resource(child, SYS_RES_MEMORY, sc->rnum, chunk,
111 		    IOMEM_STEP);
112 		rid = sc->rnum;
113 		res = bus_alloc_resource(child, SYS_RES_MEMORY, &rid, 0ul,
114 		    ~0ul, IOMEM_STEP, RF_ACTIVE);
115 		if (res == NULL) {
116 			bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
117 			chunk += IOMEM_STEP;
118 			continue;
119 		}
120 		bt = rman_get_bustag(res);
121 		bh = rman_get_bushandle(res);
122 		bus_space_read_region_1(bt, bh, 0, buf, sizeof(buf));
123 
124 		/*
125 		 * We need to release and delete the resource since we're
126 		 * changing its size, or the rom isn't there.  There
127 		 * is a checksum field in the ROM to prevent false
128 		 * positives.  However, some common hardware (IBM thinkpads)
129 		 * neglects to put a valid checksum in the ROM, so we do
130 		 * not double check the checksum here.  On the ISA bus
131 		 * areas that have no hardware read back as 0xff, so the
132 		 * tests to see if we have 0x55 followed by 0xaa are
133 		 * generally sufficient.
134 		 */
135 		bus_release_resource(child, SYS_RES_MEMORY, rid, res);
136 		bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
137 		if (buf[0] != 0x55 || buf[1] != 0xAA || (buf[2] & 0x03) != 0) {
138 			chunk += IOMEM_STEP;
139 			continue;
140 		}
141 		rom_size = buf[2] << 9;
142 		bus_set_resource(child, SYS_RES_MEMORY, sc->rnum, chunk,
143 		    rom_size);
144 		rid = sc->rnum;
145 		res = bus_alloc_resource(child, SYS_RES_MEMORY, &rid, 0ul,
146 		    ~0ul, rom_size, 0);
147 		if (res == NULL) {
148 			bus_delete_resource(child, SYS_RES_MEMORY, sc->rnum);
149 			chunk += IOMEM_STEP;
150 			continue;
151 		}
152 		sc->rid[sc->rnum] = rid;
153 		sc->res[sc->rnum] = res;
154 		sc->rnum++;
155 		chunk += rom_size;
156 	}
157 
158 	/*
159 	 * note: sc becomes invalid after we delete the child.
160 	 */
161 	if (sc->rnum == 0) {
162 		device_delete_child(parent, child);
163 		return (ENXIO);
164 	}
165 	if (sc->rnum == 1)
166 		device_set_desc(child, "Option ROM");
167 	else
168 		device_set_desc(child, "Option ROMs");
169 	return (0);
170 }
171 
172 static int
173 orm_detach(device_t dev)
174 {
175 	int			i;
176 	struct orm_softc	*sc = device_get_softc(dev);
177 
178 	for (i = 0; i < sc->rnum; i++)
179 		bus_release_resource(dev, SYS_RES_MEMORY, sc->rid[i],
180 		    sc->res[i]);
181 	return (0);
182 }
183 
184 static device_method_t orm_methods[] = {
185 	/* Device interface */
186 	DEVMETHOD(device_identify,	orm_identify),
187 	DEVMETHOD(device_probe,		orm_probe),
188 	DEVMETHOD(device_attach,	orm_attach),
189 	DEVMETHOD(device_detach,	orm_detach),
190 	{ 0, 0 }
191 };
192 
193 static driver_t orm_driver = {
194 	"orm",
195 	orm_methods,
196 	sizeof (struct orm_softc)
197 };
198 
199 static devclass_t orm_devclass;
200 
201 DRIVER_MODULE(orm, isa, orm_driver, orm_devclass, 0, 0);
202