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