1/*-
2 * Product specific probe and attach routines for:
3 * 	Buslogic BT74x SCSI controllers
4 *
5 * Copyright (c) 1995, 1998, 1999 Justin T. Gibbs
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 *    notice immediately at the beginning of the file, without modification,
13 *    this list of conditions, and the following disclaimer.
14 * 2. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD$");
33
34#include <sys/param.h>
35#include <sys/systm.h>
36#include <sys/kernel.h>
37#include <sys/module.h>
38#include <sys/lock.h>
39#include <sys/mutex.h>
40#include <sys/bus.h>
41
42#include <machine/bus.h>
43#include <machine/resource.h>
44#include <sys/rman.h>
45
46#include <dev/eisa/eisaconf.h>
47
48#include <dev/buslogic/btreg.h>
49
50#define EISA_DEVICE_ID_BUSLOGIC_74X_B	0x0ab34201
51#define EISA_DEVICE_ID_BUSLOGIC_74X_C	0x0ab34202
52#define EISA_DEVICE_ID_SDC3222B		0x0ab34281
53#define EISA_DEVICE_ID_SDC3222F		0x0ab34781
54#define EISA_DEVICE_ID_SDC3222WS	0x0ab34981
55#define EISA_DEVICE_ID_SDC3222WB	0x0ab34982
56#define	EISA_DEVICE_ID_AMI_4801		0x05a94801
57
58#define BT_IOSIZE		0x04		/* Move to central header */
59#define BT_EISA_IOSIZE		0x100
60#define	BT_EISA_SLOT_OFFSET	0xc00
61
62#define EISA_IOCONF			0x08C
63#define		PORTADDR		0x07
64#define			PORT_330	0x00
65#define			PORT_334	0x01
66#define			PORT_230	0x02
67#define			PORT_234	0x03
68#define			PORT_130	0x04
69#define			PORT_134	0x05
70#define		IRQ_CHANNEL		0xe0
71#define			INT_11		0x40
72#define			INT_10		0x20
73#define			INT_15		0xa0
74#define			INT_12		0x60
75#define			INT_14		0x80
76#define			INT_9		0x00
77
78#define EISA_IRQ_TYPE                   0x08D
79#define       LEVEL                     0x40
80
81/* Definitions for the AMI Series 48 controller */
82#define	AMI_EISA_IOSIZE			0x500	/* Two separate ranges?? */
83#define	AMI_EISA_SLOT_OFFSET		0x800
84#define	AMI_EISA_IOCONF			0x000
85#define		AMI_DMA_CHANNEL		0x03
86#define		AMI_IRQ_CHANNEL		0x1c
87#define			AMI_INT_15	0x14
88#define			AMI_INT_14	0x10
89#define			AMI_INT_12	0x0c
90#define			AMI_INT_11	0x00
91#define			AMI_INT_10	0x08
92#define			AMI_INT_9	0x04
93#define		AMI_BIOS_ADDR		0xe0
94
95#define	AMI_EISA_IOCONF1		0x001
96#define		AMI_PORTADDR		0x0e
97#define			AMI_PORT_334	0x08
98#define			AMI_PORT_330	0x00
99#define			AMI_PORT_234	0x0c
100#define			AMI_PORT_230	0x04
101#define			AMI_PORT_134	0x0a
102#define			AMI_PORT_130	0x02
103#define		AMI_IRQ_LEVEL		0x01
104
105
106#define	AMI_MISC2_OPTIONS		0x49E
107#define		AMI_ENABLE_ISA_DMA	0x08
108
109static const char *bt_match(eisa_id_t type);
110
111static int
112bt_eisa_alloc_resources(device_t dev)
113{
114	struct	bt_softc *bt = device_get_softc(dev);
115	int rid;
116	struct resource *port;
117	struct resource *irq;
118	int shared;
119
120	/*
121	 * XXX assumes that the iospace ranges are sorted in increasing
122	 * order.
123	 */
124	rid = 0;
125	port = bus_alloc_resource_any(dev, SYS_RES_IOPORT, &rid, RF_ACTIVE);
126	if (!port)
127		return (ENOMEM);
128
129	bt_init_softc(dev, port, 0, 0);
130
131	if (eisa_get_irq(dev) != -1) {
132		shared = bt->level_trigger_ints ? RF_SHAREABLE : 0;
133		rid = 0;
134		irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid,
135					     shared | RF_ACTIVE);
136		if (!irq) {
137			if (port)
138				bus_release_resource(dev, SYS_RES_IOPORT,
139						     0, port);
140			return (ENOMEM);
141		}
142	} else
143		irq = NULL;
144	bt->irq = irq;
145
146	return (0);
147}
148
149static void
150bt_eisa_release_resources(device_t dev)
151{
152	struct	bt_softc *bt = device_get_softc(dev);
153
154	if (bt->port)
155		bus_release_resource(dev, SYS_RES_IOPORT, 0, bt->port);
156	if (bt->irq)
157		bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
158	bt_free_softc(dev);
159}
160
161static const char*
162bt_match(eisa_id_t type)
163{
164	switch(type) {
165		case EISA_DEVICE_ID_BUSLOGIC_74X_B:
166			return ("Buslogic 74xB SCSI host adapter");
167		case EISA_DEVICE_ID_BUSLOGIC_74X_C:
168			return ("Buslogic 74xC SCSI host adapter");
169		case EISA_DEVICE_ID_SDC3222B:
170			return ("Storage Dimensions SDC3222B SCSI host adapter");
171		case EISA_DEVICE_ID_SDC3222F:
172			return ("Storage Dimensions SDC3222F SCSI host adapter");
173		case EISA_DEVICE_ID_SDC3222WS:
174			return ("Storage Dimensions SDC3222WS SCSI host adapter");
175		case EISA_DEVICE_ID_SDC3222WB:
176			return ("Storage Dimensions SDC3222WB SCSI host adapter");
177		case EISA_DEVICE_ID_AMI_4801:
178			return ("AMI Series 48 SCSI host adapter");
179		default:
180			break;
181	}
182	return (NULL);
183}
184
185static int
186bt_eisa_probe(device_t dev)
187{
188	const char *desc;
189	u_long iobase;
190	struct bt_probe_info info;
191	u_long port;
192	u_long iosize;
193	u_int  ioconf;
194	int    result;
195	int    shared;
196
197	desc = bt_match(eisa_get_id(dev));
198	if (!desc)
199		return (ENXIO);
200	device_set_desc(dev, desc);
201
202	iobase = (eisa_get_slot(dev) * EISA_SLOT_SIZE);
203	if (eisa_get_id(dev) == EISA_DEVICE_ID_AMI_4801) {
204		u_int ioconf1;
205
206		iobase += AMI_EISA_SLOT_OFFSET;
207		iosize = AMI_EISA_IOSIZE;
208		ioconf1 = inb(iobase + AMI_EISA_IOCONF1);
209		/* Determine "ISA" I/O port */
210		switch (ioconf1 & AMI_PORTADDR) {
211		case AMI_PORT_330:
212			port = 0x330;
213			break;
214		case AMI_PORT_334:
215			port = 0x334;
216			break;
217		case AMI_PORT_230:
218			port = 0x230;
219			break;
220		case AMI_PORT_234:
221			port = 0x234;
222			break;
223		case AMI_PORT_134:
224			port = 0x134;
225			break;
226		case AMI_PORT_130:
227			port = 0x130;
228			break;
229		default:
230			/* Disabled */
231			printf("bt: AMI EISA Adapter at "
232			       "slot %d has a disabled I/O "
233			       "port.  Cannot attach.\n",
234			       eisa_get_slot(dev));
235			return (ENXIO);
236		}
237		shared = (inb(iobase + AMI_EISA_IOCONF1) & AMI_IRQ_LEVEL) ?
238				EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
239	} else {
240		iobase += BT_EISA_SLOT_OFFSET;
241		iosize = BT_EISA_IOSIZE;
242
243		ioconf = inb(iobase + EISA_IOCONF);
244		/* Determine "ISA" I/O port */
245		switch (ioconf & PORTADDR) {
246		case PORT_330:
247			port = 0x330;
248			break;
249		case PORT_334:
250			port = 0x334;
251			break;
252		case PORT_230:
253			port = 0x230;
254			break;
255		case PORT_234:
256			port = 0x234;
257			break;
258		case PORT_130:
259			port = 0x130;
260			break;
261		case PORT_134:
262			port = 0x134;
263			break;
264		default:
265			/* Disabled */
266			printf("bt: Buslogic EISA Adapter at "
267			       "slot %d has a disabled I/O "
268			       "port.  Cannot attach.\n",
269			       eisa_get_slot(dev));
270			return (ENXIO);
271		}
272		shared = (inb(iobase + EISA_IRQ_TYPE) & LEVEL) ?
273				EISA_TRIGGER_LEVEL : EISA_TRIGGER_EDGE;
274	}
275	bt_mark_probed_iop(port);
276
277	/* Tell parent where our resources are going to be */
278	eisa_add_iospace(dev, iobase, iosize, RESVADDR_NONE);
279	eisa_add_iospace(dev, port, BT_IOSIZE, RESVADDR_NONE);
280
281	/* And allocate them */
282	bt_eisa_alloc_resources(dev);
283
284	if (bt_port_probe(dev, &info) != 0) {
285		printf("bt_eisa_probe: Probe failed for "
286		       "card at slot 0x%x\n", eisa_get_slot(dev));
287		result = ENXIO;
288	} else {
289		eisa_add_intr(dev, info.irq, shared);
290		result = BUS_PROBE_DEFAULT;
291	}
292	bt_eisa_release_resources(dev);
293
294	return (result);
295}
296
297static int
298bt_eisa_attach(device_t dev)
299{
300	struct bt_softc *bt = device_get_softc(dev);
301
302	/* Allocate resources */
303	bt_eisa_alloc_resources(dev);
304
305	/* Allocate a dmatag for our SCB DMA maps */
306	/* XXX Should be a child of the PCI bus dma tag */
307	if (bus_dma_tag_create( /* parent	*/ NULL,
308				/* alignment	*/ 1,
309				/* boundary	*/ 0,
310				/* lowaddr	*/ BUS_SPACE_MAXADDR_32BIT,
311				/* highaddr	*/ BUS_SPACE_MAXADDR,
312				/* filter	*/ NULL,
313				/* filterarg	*/ NULL,
314				/* maxsize	*/ BUS_SPACE_MAXSIZE_32BIT,
315				/* nsegments	*/ ~0,
316				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_32BIT,
317				/* flags	*/ 0,
318				/* lockfunc	*/ NULL,
319				/* lockarg,	*/ NULL,
320				&bt->parent_dmat) != 0) {
321		bt_eisa_release_resources(dev);
322		return (ENOMEM);
323	}
324
325	/*
326	 * Now that we know we own the resources we need, do the full
327	 * card initialization.
328	 */
329	if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
330		bt_eisa_release_resources(dev);
331		return (ENXIO);
332	}
333
334	/* Attach sub-devices - always succeeds (sets up intr) */
335	bt_attach(dev);
336
337	return 0;
338}
339
340static device_method_t bt_eisa_methods[] = {
341	/* Device interface */
342	DEVMETHOD(device_probe,		bt_eisa_probe),
343	DEVMETHOD(device_attach,	bt_eisa_attach),
344
345	{ 0, 0 }
346};
347
348static driver_t bt_eisa_driver = {
349	"bt",
350	bt_eisa_methods,
351	sizeof(struct bt_softc),
352};
353
354static devclass_t bt_devclass;
355
356DRIVER_MODULE(bt, eisa, bt_eisa_driver, bt_devclass, 0, 0);
357MODULE_DEPEND(bt, eisa, 1, 1, 1);
358