aha_mca.c revision 117126
1/*-
2 * Copyright (c) 1999 Matthew N. Dodd <winter@jurai.net>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 * SUCH DAMAGE.
25 *
26 * $FreeBSD: head/sys/dev/aha/aha_mca.c 117126 2003-07-01 15:52:06Z scottl $
27 *
28 * Based on aha_isa.c
29 */
30
31#include <sys/types.h>
32#include <sys/param.h>
33#include <sys/kernel.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36
37#include <sys/module.h>
38#include <sys/bus.h>
39#include <machine/bus.h>
40#include <machine/resource.h>
41#include <sys/rman.h>
42
43#include <i386/isa/isa_dma.h>
44
45#include <dev/mca/mca_busreg.h>
46#include <dev/mca/mca_busvar.h>
47
48#include <dev/aha/ahareg.h>
49
50static struct mca_ident aha_mca_devs[] = {
51	{ 0x0f1f, "Adaptec AHA-1640 SCSI Adapter" },
52	{ 0, NULL },
53};
54
55#define AHA_MCA_IOPORT_POS		MCA_ADP_POS(MCA_POS1)
56# define AHA_MCA_IOPORT_MASK1		0x07
57# define AHA_MCA_IOPORT_MASK2		0xc0
58# define AHA_MCA_IOPORT_SIZE		0x03
59# define AHA_MCA_IOPORT(pos)		(0x30 + \
60					(((u_int32_t)pos & \
61						AHA_MCA_IOPORT_MASK1) << 8) + \
62					(((u_int32_t)pos & \
63						AHA_MCA_IOPORT_MASK2) >> 4))
64
65#define AHA_MCA_DRQ_POS			MCA_ADP_POS(MCA_POS3)
66# define AHA_MCA_DRQ_MASK		0x0f
67# define AHA_MCA_DRQ(pos)		(pos & AHA_MCA_DRQ_MASK)
68
69#define AHA_MCA_IRQ_POS			MCA_ADP_POS(MCA_POS2)
70# define AHA_MCA_IRQ_MASK		0x07
71# define AHA_MCA_IRQ(pos)		((pos & AHA_MCA_IRQ_MASK) + 8)
72
73/*
74 * Not needed as the board knows its config
75 * internally and the ID will be fetched
76 * via AOP_INQUIRE_SETUP_INFO command.
77 */
78#define AHA_MCA_SCSIID_POS		MCA_ADP_POS(MCA_POS2)
79#define AHA_MCA_SCSIID_MASK		0xe0
80#define AHA_MCA_SCSIID(pos)		((pos & AHA_MCA_SCSIID_MASK) >> 5)
81
82static int
83aha_mca_probe (device_t dev)
84{
85	const char *	desc;
86	mca_id_t	id = mca_get_id(dev);
87	u_int32_t	iobase = 0;
88	u_int32_t	iosize = 0;
89	u_int8_t	drq = 0;
90	u_int8_t	irq = 0;
91	u_int8_t	pos;
92
93	desc = mca_match_id(id, aha_mca_devs);
94	if (!desc)
95		return (ENXIO);
96	device_set_desc(dev, desc);
97
98	pos = mca_pos_read(dev, AHA_MCA_IOPORT_POS);
99	iobase = AHA_MCA_IOPORT(pos);
100	iosize = AHA_MCA_IOPORT_SIZE;
101
102	pos = mca_pos_read(dev, AHA_MCA_DRQ_POS);
103	drq = AHA_MCA_DRQ(pos);
104
105	pos = mca_pos_read(dev, AHA_MCA_IRQ_POS);
106	irq = AHA_MCA_IRQ(pos);
107
108	mca_add_iospace(dev, iobase, iosize);
109	mca_add_drq(dev, drq);
110	mca_add_irq(dev, irq);
111
112	aha_unit++;
113
114	return (0);
115}
116
117static int
118aha_mca_attach (device_t dev)
119{
120	struct aha_softc *	sc = NULL;
121	struct resource *	io = NULL;
122	struct resource *	irq = NULL;
123	struct resource *	drq = NULL;
124	int			error = 0;
125	int			unit = device_get_unit(dev);
126	int			rid;
127	void *			ih;
128
129	rid = 0;
130	io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
131				0, ~0, 1, RF_ACTIVE);
132	if (!io) {
133		device_printf(dev, "No I/O space?!\n");
134		error = ENOMEM;
135		goto bad;
136	}
137
138	rid = 0;
139	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
140				 0, ~0, 1, RF_ACTIVE);
141	if (irq == NULL) {
142		device_printf(dev, "No IRQ?!\n");
143		error = ENOMEM;
144		goto bad;
145	}
146
147	rid = 0;
148	drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
149				 0, ~0, 1, RF_ACTIVE);
150	if (drq == NULL) {
151		device_printf(dev, "No DRQ?!\n");
152		error = ENOMEM;
153		goto bad;
154	}
155
156	sc = aha_alloc(unit, rman_get_bustag(io), rman_get_bushandle(io));
157	if (sc == NULL) {
158		device_printf(dev, "aha_alloc() failed!\n");
159		error = ENOMEM;
160		goto bad;
161	}
162
163	error = aha_probe(sc);
164	if (error) {
165		device_printf(dev, "aha_probe() failed!\n");
166		goto bad;
167	}
168
169	error = aha_fetch_adapter_info(sc);
170	if (error) {
171		device_printf(dev, "aha_fetch_adapter_info() failed!\n");
172		goto bad;
173	}
174
175	isa_dmacascade(rman_get_start(drq));
176
177	error = bus_dma_tag_create(
178				/* parent	*/ NULL,
179				/* alignemnt	*/ 1,
180				/* boundary	*/ 0,
181				/* lowaddr	*/ BUS_SPACE_MAXADDR_24BIT,
182				/* highaddr	*/ BUS_SPACE_MAXADDR,
183				/* filter	*/ NULL,
184				/* filterarg	*/ NULL,
185				/* maxsize	*/ BUS_SPACE_MAXSIZE_24BIT,
186				/* nsegments	*/ ~0,
187				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
188				/* flags	*/ 0,
189				/* lockfunc	*/ busdma_lock_mutex,
190				/* lockarg	*/ &Giant,
191				&sc->parent_dmat);
192	if (error) {
193		device_printf(dev, "bus_dma_tag_create() failed!\n");
194		goto bad;
195	}
196
197	error = aha_init(sc);
198	if (error) {
199		device_printf(dev, "aha_init() failed\n");
200		goto bad;
201	}
202
203	error = aha_attach(sc);
204	if (error) {
205		device_printf(dev, "aha_attach() failed\n");
206		goto bad;
207	}
208
209	error = bus_setup_intr(dev, irq, INTR_TYPE_CAM|INTR_ENTROPY, aha_intr, sc, &ih);
210	if (error) {
211		device_printf(dev, "Unable to register interrupt handler\n");
212		goto bad;
213	}
214
215	return (0);
216
217bad:
218	aha_free(sc);
219
220	if (io)
221		bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
222	if (irq)
223		bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
224	if (drq)
225		bus_release_resource(dev, SYS_RES_DRQ, 0, drq);
226
227	return (error);
228}
229
230static device_method_t aha_mca_methods[] = {
231	DEVMETHOD(device_probe,         aha_mca_probe),
232	DEVMETHOD(device_attach,        aha_mca_attach),
233
234	{ 0, 0 }
235};
236
237static driver_t aha_mca_driver = {
238	"aha",
239	aha_mca_methods,
240	1,
241/*
242	sizeof(struct aha_softc *),
243 */
244};
245
246static devclass_t aha_devclass;
247
248DRIVER_MODULE(aha, mca, aha_mca_driver, aha_devclass, 0, 0);
249