xref: /freebsd/sys/dev/aic7xxx/ahc_isa.c (revision f4b37ed0f8b307b1f3f0f630ca725d68f1dff30d)
1 /*-
2  * FreeBSD, VLB/ISA product support functions
3  *
4  * Copyright (c) 2004 Justin T. Gibbs.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions, and the following disclaimer,
12  *    without modification.
13  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
14  *    substantially similar to the "NO WARRANTY" disclaimer below
15  *    ("Disclaimer") and any redistribution must be conditioned upon
16  *    including a substantially similar Disclaimer requirement for further
17  *    binary redistribution.
18  * 3. Neither the names of the above-listed copyright holders nor the names
19  *    of any contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * NO WARRANTY
23  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
26  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
31  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
32  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGES.
34  *
35  * $Id$
36  */
37 
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 #include <dev/aic7xxx/aic7xxx_osm.h>
42 
43 #include <sys/limits.h>		/* For CHAR_BIT*/
44 #include <isa/isavar.h>		/* For ISA attach glue */
45 
46 
47 static struct aic7770_identity *ahc_isa_find_device(bus_space_tag_t tag,
48 						    bus_space_handle_t bsh);
49 static void			ahc_isa_identify(driver_t *driver,
50 						 device_t parent);
51 static int			ahc_isa_probe(device_t dev);
52 static int			ahc_isa_attach(device_t dev);
53 
54 /*
55  * Perform an EISA probe of the address with the addition
56  * of a "priming" step.  The 284X requires priming (a write
57  * to offset 0x80, the first EISA ID register) to ensure it
58  * is not mistaken as an EISA card.  Once we have the ID,
59  * lookup the controller in the aic7770 table of supported
60  * devices.
61  */
62 static struct aic7770_identity *
63 ahc_isa_find_device(bus_space_tag_t tag, bus_space_handle_t bsh) {
64 	uint32_t  id;
65 	u_int	  id_size;
66 	int	  i;
67 
68 	id = 0;
69 	id_size = sizeof(id);
70 	for (i = 0; i < id_size; i++) {
71 		bus_space_write_1(tag, bsh, 0x80, 0x80 + i);
72 		id |= bus_space_read_1(tag, bsh, 0x80 + i)
73 		   << ((id_size - i - 1) * CHAR_BIT);
74 	}
75 
76 	return (aic7770_find_device(id));
77 }
78 
79 static void
80 ahc_isa_identify(driver_t *driver, device_t parent)
81 {
82 	int slot;
83 	int max_slot;
84 
85 	max_slot = 14;
86 	for (slot = 0; slot <= max_slot; slot++) {
87 		struct aic7770_identity *entry;
88 		bus_space_tag_t	    tag;
89 		bus_space_handle_t  bsh;
90 		struct resource	   *regs;
91 		uint32_t	    iobase;
92 		int		    rid;
93 
94 		rid = 0;
95 		iobase = (slot * AHC_EISA_SLOT_SIZE) + AHC_EISA_SLOT_OFFSET;
96 		regs = bus_alloc_resource(parent, SYS_RES_IOPORT, &rid,
97 					  iobase, iobase, AHC_EISA_IOSIZE,
98 					  RF_ACTIVE);
99 		if (regs == NULL) {
100 			if (bootverbose)
101 				printf("ahc_isa_identify %d: ioport 0x%x "
102 				       "alloc failed\n", slot, iobase);
103 			continue;
104 		}
105 
106 		tag = rman_get_bustag(regs);
107 		bsh = rman_get_bushandle(regs);
108 
109 		entry = ahc_isa_find_device(tag, bsh);
110 		if (entry != NULL) {
111 			device_t child;
112 
113 			child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE,
114 					      "ahc", -1);
115 			if (child != NULL) {
116 				device_set_driver(child, driver);
117 				bus_set_resource(child, SYS_RES_IOPORT,
118 						 0, iobase, AHC_EISA_IOSIZE);
119 			}
120 		}
121 		bus_release_resource(parent, SYS_RES_IOPORT, rid, regs);
122 	}
123 }
124 
125 static int
126 ahc_isa_probe(device_t dev)
127 {
128 	struct	  aic7770_identity *entry;
129 	bus_space_tag_t	    tag;
130 	bus_space_handle_t  bsh;
131 	struct	  resource *regs;
132 	struct	  resource *irq;
133 	uint32_t  iobase;
134 	u_int	  intdef;
135 	u_int	  hcntrl;
136 	int	  irq_num;
137 	int	  error;
138 	int	  zero;
139 
140 	error = ENXIO;
141 	zero = 0;
142 	regs = NULL;
143 	irq = NULL;
144 
145 	/* Skip probes for ISA PnP devices */
146 	if (isa_get_logicalid(dev) != 0)
147 		return (error);
148 
149 	regs = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &zero, RF_ACTIVE);
150 	if (regs == NULL) {
151 		device_printf(dev, "No resources allocated.\n");
152 		return (ENOMEM);
153 	}
154 
155 	iobase = rman_get_start(regs);
156 	tag = rman_get_bustag(regs);
157 	bsh = rman_get_bushandle(regs);
158 
159 	entry = ahc_isa_find_device(tag, bsh);
160 	if (entry == NULL)
161 		goto cleanup;
162 
163 	/* Pause the card preseving the IRQ type */
164 	hcntrl = bus_space_read_1(tag, bsh, HCNTRL) & IRQMS;
165 	bus_space_write_1(tag, bsh, HCNTRL, hcntrl | PAUSE);
166 	while ((bus_space_read_1(tag, bsh, HCNTRL) & PAUSE) == 0)
167 		;
168 
169 	/* Make sure we have a valid interrupt vector */
170 	intdef = bus_space_read_1(tag, bsh, INTDEF);
171 	irq_num = intdef & VECTOR;
172 	switch (irq_num) {
173 	case 9:
174 	case 10:
175 	case 11:
176 	case 12:
177 	case 14:
178 	case 15:
179 		break;
180 	default:
181 		device_printf(dev, "@0x%x: illegal irq setting %d\n",
182 			      iobase, irq_num);
183 		goto cleanup;
184 	}
185 
186 	if (bus_set_resource(dev, SYS_RES_IRQ, zero, irq_num, 1) != 0)
187 		goto cleanup;
188 
189 	/*
190 	 * The 284X only supports edge triggered interrupts,
191 	 * so do not claim RF_SHAREABLE.
192 	 */
193 	irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &zero,
194 				     0 /*!(RF_ACTIVE|RF_SHAREABLE)*/);
195 	if (irq != NULL) {
196 		error = 0;
197 		device_set_desc(dev, entry->name);
198 	} else
199 		device_printf(dev, "@0x%x: irq %d allocation failed\n",
200 			      iobase, irq_num);
201 
202 cleanup:
203 	if (regs != NULL) {
204 		bus_release_resource(dev, SYS_RES_IOPORT, zero, regs);
205 		regs = NULL;
206 	}
207 
208 	if (irq != NULL) {
209 		bus_release_resource(dev, SYS_RES_IRQ, zero, irq);
210 		irq = NULL;
211 	}
212 
213 	return (error);
214 }
215 
216 static int
217 ahc_isa_attach(device_t dev)
218 {
219 	struct	 aic7770_identity *entry;
220 	bus_space_tag_t	    tag;
221 	bus_space_handle_t  bsh;
222 	struct	  resource *regs;
223 	struct	  ahc_softc *ahc;
224 	char	 *name;
225 	int	  zero;
226 	int	  error;
227 
228 	zero = 0;
229 	regs = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &zero, RF_ACTIVE);
230 	if (regs == NULL)
231 		return (ENOMEM);
232 
233 	tag = rman_get_bustag(regs);
234 	bsh = rman_get_bushandle(regs);
235 	entry = ahc_isa_find_device(tag, bsh);
236 	bus_release_resource(dev, SYS_RES_IOPORT, zero, regs);
237 	if (entry == NULL)
238 		return (ENODEV);
239 
240 	/*
241 	 * Allocate a softc for this card and
242 	 * set it up for attachment by our
243 	 * common detect routine.
244 	 */
245 	name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT);
246 	if (name == NULL)
247 		return (ENOMEM);
248 	strcpy(name, device_get_nameunit(dev));
249 	ahc = ahc_alloc(dev, name);
250 	if (ahc == NULL)
251 		return (ENOMEM);
252 
253 	ahc_set_unit(ahc, device_get_unit(dev));
254 
255 	/* Allocate a dmatag for our SCB DMA maps */
256 	error = aic_dma_tag_create(ahc, /*parent*/bus_get_dma_tag(dev),
257 				   /*alignment*/1, /*boundary*/0,
258 				   /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
259 				   /*highaddr*/BUS_SPACE_MAXADDR,
260 				   /*filter*/NULL, /*filterarg*/NULL,
261 				   /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
262 				   /*nsegments*/AHC_NSEG,
263 				   /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
264 				   /*flags*/0,
265 				   &ahc->parent_dmat);
266 
267 	if (error != 0) {
268 		printf("ahc_isa_attach: Could not allocate DMA tag "
269 		       "- error %d\n", error);
270 		ahc_free(ahc);
271 		return (ENOMEM);
272 	}
273 	ahc->dev_softc = dev;
274 	error = aic7770_config(ahc, entry, /*unused ioport arg*/0);
275 	if (error != 0) {
276 		ahc_free(ahc);
277 		return (error);
278 	}
279 
280 	ahc_attach(ahc);
281 	return (0);
282 }
283 
284 static device_method_t ahc_isa_device_methods[] = {
285 	/* Device interface */
286 	DEVMETHOD(device_identify,      ahc_isa_identify),
287 	DEVMETHOD(device_probe,		ahc_isa_probe),
288 	DEVMETHOD(device_attach,	ahc_isa_attach),
289 	DEVMETHOD(device_detach,	ahc_detach),
290 	{ 0, 0 }
291 };
292 
293 static driver_t ahc_isa_driver = {
294 	"ahc",
295 	ahc_isa_device_methods,
296 	sizeof(struct ahc_softc)
297 };
298 
299 DRIVER_MODULE(ahc_isa, isa, ahc_isa_driver, ahc_devclass, 0, 0);
300 MODULE_DEPEND(ahc_isa, ahc, 1, 1, 1);
301 MODULE_VERSION(ahc_isa, 1);
302