xref: /freebsd/sys/dev/proto/proto_bus_isa.c (revision 7a7741af18d6c8a804cc643cb7ecda9d730c6aa6)
1 /*-
2  * Copyright (c) 2015 Marcel Moolenaar
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 ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25 
26 #include <sys/param.h>
27 #include <sys/systm.h>
28 #include <sys/bus.h>
29 #include <sys/conf.h>
30 #include <sys/kernel.h>
31 #include <sys/module.h>
32 #include <machine/bus.h>
33 #include <sys/rman.h>
34 #include <machine/resource.h>
35 
36 #include <isa/isavar.h>
37 #include <isa/pnpvar.h>
38 
39 #include <dev/proto/proto.h>
40 
41 static int proto_isa_probe(device_t dev);
42 static int proto_isa_attach(device_t dev);
43 
44 static device_method_t proto_isa_methods[] = {
45 	/* Device interface */
46 	DEVMETHOD(device_probe,		proto_isa_probe),
47 	DEVMETHOD(device_attach,	proto_isa_attach),
48 	DEVMETHOD(device_detach,	proto_detach),
49 	DEVMETHOD_END
50 };
51 
52 static driver_t proto_isa_driver = {
53 	proto_driver_name,
54 	proto_isa_methods,
55 	sizeof(struct proto_softc),
56 };
57 
58 static char proto_isa_prefix[] = "isa";
59 static char **proto_isa_devnames;
60 
61 static int
62 proto_isa_probe(device_t dev)
63 {
64 	struct resource *res;
65 	int rid, type;
66 
67 	rid = 0;
68 	type = SYS_RES_IOPORT;
69 	res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
70 	if (res == NULL) {
71 		type = SYS_RES_MEMORY;
72 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
73 	}
74 	if (res == NULL)
75 		return (ENODEV);
76 
77 	device_set_descf(dev, "%s:%#jx", proto_isa_prefix, rman_get_start(res));
78 	bus_release_resource(dev, type, rid, res);
79 	return (proto_probe(dev, proto_isa_prefix, &proto_isa_devnames));
80 }
81 
82 static int
83 proto_isa_alloc(device_t dev, int type, int nrids)
84 {
85 	struct resource *res;
86 	struct proto_softc *sc;
87 	int count, rid;
88 
89 	sc = device_get_softc(dev);
90 	count = 0;
91 	for (rid = 0; rid < nrids; rid++) {
92 		res = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
93 		if (res == NULL)
94 			break;
95 		proto_add_resource(sc, type, rid, res);
96 		count++;
97 	}
98 	if (type == SYS_RES_DRQ && count > 0)
99 		proto_add_resource(sc, PROTO_RES_BUSDMA, 0, NULL);
100 	return (count);
101 }
102 
103 static int
104 proto_isa_attach(device_t dev)
105 {
106 
107 	proto_isa_alloc(dev, SYS_RES_IRQ, ISA_NIRQ);
108 	proto_isa_alloc(dev, SYS_RES_DRQ, ISA_NDRQ);
109 	proto_isa_alloc(dev, SYS_RES_IOPORT, ISA_NPORT);
110 	proto_isa_alloc(dev, SYS_RES_MEMORY, ISA_NMEM);
111 	return (proto_attach(dev));
112 }
113 
114 DRIVER_MODULE(proto, acpi, proto_isa_driver, NULL, NULL);
115 DRIVER_MODULE(proto, isa, proto_isa_driver, NULL, NULL);
116