1 /*-
2  * Copyright (c) 2000 Matthew N. Dodd <winter@jurai.net>
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  */
27 
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD: stable/9/sys/dev/dpt/dpt_isa.c 251164 2013-05-30 21:54:48Z scottl $");
30 
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/kernel.h>
34 #include <sys/lock.h>
35 #include <sys/module.h>
36 #include <sys/mutex.h>
37 #include <sys/bus.h>
38 
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <sys/rman.h>
42 
43 #include <isa/isavar.h>
44 
45 #include <cam/scsi/scsi_all.h>
46 
47 #include <dev/dpt/dpt.h>
48 
49 #ifdef notyet
50 static void	dpt_isa_identify	(driver_t *, device_t);
51 #endif
52 static int	dpt_isa_probe		(device_t);
53 static int	dpt_isa_attach		(device_t);
54 static int	dpt_isa_detach		(device_t);
55 
56 static int	dpt_isa_valid_irq	(int);
57 static int	dpt_isa_valid_ioport	(int);
58 
59 static int
dpt_isa_valid_irq(int irq)60 dpt_isa_valid_irq (int irq)
61 {
62 	switch (irq) {
63 		case 11:
64 		case 12:
65 		case 14:
66 		case 15:
67 			return (0);
68 		default:
69 			return (1);
70 	};
71 	return (1);
72 }
73 
74 static int
dpt_isa_valid_ioport(int ioport)75 dpt_isa_valid_ioport (int ioport)
76 {
77 	switch (ioport) {
78 		case 0x170:
79 		case 0x1f0:
80 		case 0x230:
81 		case 0x330:
82 			return (0);
83 		default:
84 			return (1);
85 	};
86 	return (1);
87 }
88 
89 #ifdef notyet
90 static void
dpt_isa_identify(driver_t * driver,device_t parent)91 dpt_isa_identify (driver_t *driver, device_t parent)
92 {
93 	device_t	child;
94 	dpt_conf_t *	conf;
95 	int		isa_bases[] = { 0x1f0, 0x170, 0x330, 0x230, 0 };
96 	int		i;
97 
98 	for (i = 0; isa_bases[i]; i++) {
99 		conf = dpt_pio_get_conf(isa_bases[i]);
100 	        if (!conf) {
101 			if (bootverbose)
102 				device_printf(parent, "dpt: dpt_pio_get_conf(%x) failed.\n",
103 					isa_bases[i]);
104 			continue;
105 		}
106 
107 		child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE, "dpt", -1);
108 		if (child == 0) {
109 			device_printf(parent, "dpt: BUS_ADD_CHILD() failed!\n");
110 			continue;
111 		}
112 		device_set_driver(child, driver);
113 		bus_set_resource(child, SYS_RES_IOPORT, 0, isa_bases[i], 0x9);
114 	}
115 	return;
116 }
117 #endif
118 
119 static int
dpt_isa_probe(device_t dev)120 dpt_isa_probe (device_t dev)
121 {
122 	dpt_conf_t *	conf;
123 	u_int32_t	io_base;
124 
125 	/* No pnp support */
126 	if (isa_get_vendorid(dev))
127 		return (ENXIO);
128 
129 	if ((io_base = bus_get_resource_start(dev, SYS_RES_IOPORT, 0)) == 0)
130 		return (ENXIO);
131 
132 	if (dpt_isa_valid_ioport(io_base))
133 		;
134 
135 	conf = dpt_pio_get_conf(io_base);
136 	if (!conf) {
137 		printf("dpt: dpt_pio_get_conf() failed.\n");
138 		return (ENXIO);
139 	}
140 
141 	if (dpt_isa_valid_irq(conf->IRQ))
142 		;
143 
144 	device_set_desc(dev, "ISA DPT SCSI controller");
145 	bus_set_resource(dev, SYS_RES_IRQ, 0, conf->IRQ, 1);
146 	bus_set_resource(dev, SYS_RES_DRQ, 0, ((8 - conf->DMA_channel) & 7), 1);
147 
148 	return 0;
149 }
150 
151 static int
dpt_isa_attach(device_t dev)152 dpt_isa_attach (device_t dev)
153 {
154 	dpt_softc_t *	dpt;
155 	int		error = 0;
156 
157 	dpt = device_get_softc(dev);
158 	dpt->dev = dev;
159 	dpt_alloc(dev);
160 
161 	dpt->io_rid = 0;
162 	dpt->io_type = SYS_RES_IOPORT;
163 	dpt->irq_rid = 0;
164 
165 	error = dpt_alloc_resources(dev);
166 	if (error) {
167 		goto bad;
168 	}
169 
170 	dpt->drq_rid = 0;
171 	dpt->drq_res = bus_alloc_resource_any(dev, SYS_RES_DRQ, &dpt->drq_rid,
172 					RF_ACTIVE);
173 	if (!dpt->drq_res) {
174 		device_printf(dev, "No DRQ!\n");
175 		error = ENOMEM;
176 		goto bad;
177 	}
178 	isa_dma_acquire(rman_get_start(dpt->drq_res));
179 	isa_dmacascade(rman_get_start(dpt->drq_res));
180 
181 	/* Allocate a dmatag representing the capabilities of this attachment */
182 	if (bus_dma_tag_create( /* parent    */	bus_get_dma_tag(dev),
183 				/* alignemnt */	1,
184 				/* boundary  */	0,
185 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
186 				/* highaddr  */	BUS_SPACE_MAXADDR,
187 				/* filter    */	NULL,
188 				/* filterarg */	NULL,
189 				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
190 				/* nsegments */	~0,
191 				/* maxsegsz  */	BUS_SPACE_MAXSIZE_32BIT,
192 				/* flags     */	0,
193 				/* lockfunc  */ NULL,
194 				/* lockarg   */ NULL,
195 				&dpt->parent_dmat) != 0) {
196 		error = ENXIO;
197 		goto bad;
198 	}
199 
200 	if (dpt_init(dpt) != 0) {
201 		error = ENXIO;
202 		goto bad;
203 	}
204 
205 	/* Register with the XPT */
206 	dpt_attach(dpt);
207 
208 	if (bus_setup_intr(dev, dpt->irq_res, INTR_TYPE_CAM | INTR_ENTROPY |
209 	    INTR_MPSAFE, NULL, dpt_intr, dpt, &dpt->ih)) {
210 		device_printf(dev, "Unable to register interrupt handler\n");
211 		error = ENXIO;
212 		goto bad;
213 	}
214 
215 	return (error);
216 
217  bad:
218 	if (dpt->drq_res) {
219 		isa_dma_release(rman_get_start(dpt->drq_res));
220 	}
221 
222 	dpt_release_resources(dev);
223 
224 	if (dpt)
225 		dpt_free(dpt);
226 
227 	return (error);
228 }
229 
230 static int
dpt_isa_detach(device_t dev)231 dpt_isa_detach (device_t dev)
232 {
233 	dpt_softc_t *	dpt;
234 	int		dma;
235 	int		error;
236 
237 	dpt = device_get_softc(dev);
238 
239 	dma = rman_get_start(dpt->drq_res);
240 	error = dpt_detach(dev);
241 	isa_dma_release(dma);
242 
243 	return (error);
244 }
245 
246 
247 static device_method_t dpt_isa_methods[] = {
248 	/* Device interface */
249 #ifdef notyet
250 	DEVMETHOD(device_identify,	dpt_isa_identify),
251 #endif
252 	DEVMETHOD(device_probe,		dpt_isa_probe),
253 	DEVMETHOD(device_attach,	dpt_isa_attach),
254 	DEVMETHOD(device_detach,	dpt_isa_detach),
255 
256 	{ 0, 0 }
257 };
258 
259 static driver_t dpt_isa_driver = {
260 	"dpt",
261 	dpt_isa_methods,
262 	sizeof(dpt_softc_t),
263 };
264 
265 DRIVER_MODULE(dpt, isa, dpt_isa_driver, dpt_devclass, 0, 0);
266 MODULE_DEPEND(dpt, isa, 1, 1, 1);
267 MODULE_DEPEND(dpt, cam, 1, 1, 1);
268