xref: /freebsd/sys/dev/aic7xxx/ahc_isa.c (revision 61e21613)
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 #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 static struct aic7770_identity *ahc_isa_find_device(bus_space_tag_t tag,
47 						    bus_space_handle_t bsh);
48 static void			ahc_isa_identify(driver_t *driver,
49 						 device_t parent);
50 static int			ahc_isa_probe(device_t dev);
51 static int			ahc_isa_attach(device_t dev);
52 
53 /*
54  * Perform an EISA probe of the address with the addition
55  * of a "priming" step.  The 284X requires priming (a write
56  * to offset 0x80, the first EISA ID register) to ensure it
57  * is not mistaken as an EISA card.  Once we have the ID,
58  * lookup the controller in the aic7770 table of supported
59  * devices.
60  */
61 static struct aic7770_identity *
62 ahc_isa_find_device(bus_space_tag_t tag, bus_space_handle_t bsh) {
63 	uint32_t  id;
64 	u_int	  id_size;
65 	int	  i;
66 
67 	id = 0;
68 	id_size = sizeof(id);
69 	for (i = 0; i < id_size; i++) {
70 		bus_space_write_1(tag, bsh, 0x80, 0x80 + i);
71 		id |= bus_space_read_1(tag, bsh, 0x80 + i)
72 		   << ((id_size - i - 1) * CHAR_BIT);
73 	}
74 
75 	return (aic7770_find_device(id));
76 }
77 
78 static void
79 ahc_isa_identify(driver_t *driver, device_t parent)
80 {
81 	int slot;
82 	int max_slot;
83 
84 	max_slot = 14;
85 	for (slot = 0; slot <= max_slot; slot++) {
86 		struct aic7770_identity *entry;
87 		bus_space_tag_t	    tag;
88 		bus_space_handle_t  bsh;
89 		struct resource	   *regs;
90 		uint32_t	    iobase;
91 		int		    rid;
92 
93 		rid = 0;
94 		iobase = (slot * AHC_EISA_SLOT_SIZE) + AHC_EISA_SLOT_OFFSET;
95 		regs = bus_alloc_resource(parent, SYS_RES_IOPORT, &rid,
96 					  iobase, iobase, AHC_EISA_IOSIZE,
97 					  RF_ACTIVE);
98 		if (regs == NULL) {
99 			if (bootverbose)
100 				printf("ahc_isa_identify %d: ioport 0x%x "
101 				       "alloc failed\n", slot, iobase);
102 			continue;
103 		}
104 
105 		tag = rman_get_bustag(regs);
106 		bsh = rman_get_bushandle(regs);
107 
108 		entry = ahc_isa_find_device(tag, bsh);
109 		if (entry != NULL) {
110 			device_t child;
111 
112 			child = BUS_ADD_CHILD(parent, ISA_ORDER_SPECULATIVE,
113 					      "ahc", -1);
114 			if (child != NULL) {
115 				device_set_driver(child, driver);
116 				bus_set_resource(child, SYS_RES_IOPORT,
117 						 0, iobase, AHC_EISA_IOSIZE);
118 			}
119 		}
120 		bus_release_resource(parent, SYS_RES_IOPORT, rid, regs);
121 	}
122 }
123 
124 static int
125 ahc_isa_probe(device_t dev)
126 {
127 	struct	  aic7770_identity *entry;
128 	bus_space_tag_t	    tag;
129 	bus_space_handle_t  bsh;
130 	struct	  resource *regs;
131 	struct	  resource *irq;
132 	uint32_t  iobase;
133 	u_int	  intdef;
134 	u_int	  hcntrl;
135 	int	  irq_num;
136 	int	  error;
137 	int	  zero;
138 
139 	error = ENXIO;
140 	zero = 0;
141 	regs = NULL;
142 	irq = NULL;
143 
144 	/* Skip probes for ISA PnP devices */
145 	if (isa_get_logicalid(dev) != 0)
146 		return (error);
147 
148 	regs = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &zero, RF_ACTIVE);
149 	if (regs == NULL) {
150 		device_printf(dev, "No resources allocated.\n");
151 		return (ENOMEM);
152 	}
153 
154 	iobase = rman_get_start(regs);
155 	tag = rman_get_bustag(regs);
156 	bsh = rman_get_bushandle(regs);
157 
158 	entry = ahc_isa_find_device(tag, bsh);
159 	if (entry == NULL)
160 		goto cleanup;
161 
162 	/* Pause the card preseving the IRQ type */
163 	hcntrl = bus_space_read_1(tag, bsh, HCNTRL) & IRQMS;
164 	bus_space_write_1(tag, bsh, HCNTRL, hcntrl | PAUSE);
165 	while ((bus_space_read_1(tag, bsh, HCNTRL) & PAUSE) == 0)
166 		;
167 
168 	/* Make sure we have a valid interrupt vector */
169 	intdef = bus_space_read_1(tag, bsh, INTDEF);
170 	irq_num = intdef & VECTOR;
171 	switch (irq_num) {
172 	case 9:
173 	case 10:
174 	case 11:
175 	case 12:
176 	case 14:
177 	case 15:
178 		break;
179 	default:
180 		device_printf(dev, "@0x%x: illegal irq setting %d\n",
181 			      iobase, irq_num);
182 		goto cleanup;
183 	}
184 
185 	if (bus_set_resource(dev, SYS_RES_IRQ, zero, irq_num, 1) != 0)
186 		goto cleanup;
187 
188 	/*
189 	 * The 284X only supports edge triggered interrupts,
190 	 * so do not claim RF_SHAREABLE.
191 	 */
192 	irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &zero,
193 				     0 /*!(RF_ACTIVE|RF_SHAREABLE)*/);
194 	if (irq != NULL) {
195 		error = 0;
196 		device_set_desc(dev, entry->name);
197 	} else
198 		device_printf(dev, "@0x%x: irq %d allocation failed\n",
199 			      iobase, irq_num);
200 
201 cleanup:
202 	if (regs != NULL) {
203 		bus_release_resource(dev, SYS_RES_IOPORT, zero, regs);
204 		regs = NULL;
205 	}
206 
207 	if (irq != NULL) {
208 		bus_release_resource(dev, SYS_RES_IRQ, zero, irq);
209 		irq = NULL;
210 	}
211 
212 	return (error);
213 }
214 
215 static int
216 ahc_isa_attach(device_t dev)
217 {
218 	struct	 aic7770_identity *entry;
219 	bus_space_tag_t	    tag;
220 	bus_space_handle_t  bsh;
221 	struct	  resource *regs;
222 	struct	  ahc_softc *ahc;
223 	char	 *name;
224 	int	  zero;
225 	int	  error;
226 
227 	zero = 0;
228 	regs = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &zero, RF_ACTIVE);
229 	if (regs == NULL)
230 		return (ENOMEM);
231 
232 	tag = rman_get_bustag(regs);
233 	bsh = rman_get_bushandle(regs);
234 	entry = ahc_isa_find_device(tag, bsh);
235 	bus_release_resource(dev, SYS_RES_IOPORT, zero, regs);
236 	if (entry == NULL)
237 		return (ENODEV);
238 
239 	/*
240 	 * Allocate a softc for this card and
241 	 * set it up for attachment by our
242 	 * common detect routine.
243 	 */
244 	name = malloc(strlen(device_get_nameunit(dev)) + 1, M_DEVBUF, M_NOWAIT);
245 	if (name == NULL)
246 		return (ENOMEM);
247 	strcpy(name, device_get_nameunit(dev));
248 	ahc = ahc_alloc(dev, name);
249 	if (ahc == NULL)
250 		return (ENOMEM);
251 
252 	ahc_set_unit(ahc, device_get_unit(dev));
253 
254 	/* Allocate a dmatag for our SCB DMA maps */
255 	error = aic_dma_tag_create(ahc, /*parent*/bus_get_dma_tag(dev),
256 				   /*alignment*/1, /*boundary*/0,
257 				   /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
258 				   /*highaddr*/BUS_SPACE_MAXADDR,
259 				   /*filter*/NULL, /*filterarg*/NULL,
260 				   /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
261 				   /*nsegments*/AHC_NSEG,
262 				   /*maxsegsz*/AHC_MAXTRANSFER_SIZE,
263 				   /*flags*/0,
264 				   &ahc->parent_dmat);
265 
266 	if (error != 0) {
267 		printf("ahc_isa_attach: Could not allocate DMA tag "
268 		       "- error %d\n", error);
269 		ahc_free(ahc);
270 		return (ENOMEM);
271 	}
272 	ahc->dev_softc = dev;
273 	error = aic7770_config(ahc, entry, /*unused ioport arg*/0);
274 	if (error != 0) {
275 		ahc_free(ahc);
276 		return (error);
277 	}
278 
279 	ahc_attach(ahc);
280 	return (0);
281 }
282 
283 static device_method_t ahc_isa_device_methods[] = {
284 	/* Device interface */
285 	DEVMETHOD(device_identify,      ahc_isa_identify),
286 	DEVMETHOD(device_probe,		ahc_isa_probe),
287 	DEVMETHOD(device_attach,	ahc_isa_attach),
288 	DEVMETHOD(device_detach,	ahc_detach),
289 	{ 0, 0 }
290 };
291 
292 static driver_t ahc_isa_driver = {
293 	"ahc",
294 	ahc_isa_device_methods,
295 	sizeof(struct ahc_softc)
296 };
297 
298 DRIVER_MODULE(ahc_isa, isa, ahc_isa_driver, 0, 0);
299 MODULE_DEPEND(ahc_isa, ahc, 1, 1, 1);
300 MODULE_VERSION(ahc_isa, 1);
301