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