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