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