aha_mca.c revision 122361
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 * Based on aha_isa.c
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/dev/aha/aha_mca.c 122361 2003-11-09 19:51:16Z imp $");
31
32#include <sys/types.h>
33#include <sys/param.h>
34#include <sys/kernel.h>
35#include <sys/lock.h>
36#include <sys/mutex.h>
37
38#include <sys/module.h>
39#include <sys/bus.h>
40#include <machine/bus.h>
41#include <machine/resource.h>
42#include <sys/rman.h>
43
44#include <i386/isa/isa_dma.h>
45
46#include <dev/mca/mca_busreg.h>
47#include <dev/mca/mca_busvar.h>
48
49#include <dev/aha/ahareg.h>
50
51static struct mca_ident aha_mca_devs[] = {
52	{ 0x0f1f, "Adaptec AHA-1640 SCSI Adapter" },
53	{ 0, NULL },
54};
55
56#define AHA_MCA_IOPORT_POS		MCA_ADP_POS(MCA_POS1)
57# define AHA_MCA_IOPORT_MASK1		0x07
58# define AHA_MCA_IOPORT_MASK2		0xc0
59# define AHA_MCA_IOPORT_SIZE		0x03
60# define AHA_MCA_IOPORT(pos)		(0x30 + \
61					(((uint32_t)pos & \
62						AHA_MCA_IOPORT_MASK1) << 8) + \
63					(((uint32_t)pos & \
64						AHA_MCA_IOPORT_MASK2) >> 4))
65
66#define AHA_MCA_DRQ_POS			MCA_ADP_POS(MCA_POS3)
67# define AHA_MCA_DRQ_MASK		0x0f
68# define AHA_MCA_DRQ(pos)		(pos & AHA_MCA_DRQ_MASK)
69
70#define AHA_MCA_IRQ_POS			MCA_ADP_POS(MCA_POS2)
71# define AHA_MCA_IRQ_MASK		0x07
72# define AHA_MCA_IRQ(pos)		((pos & AHA_MCA_IRQ_MASK) + 8)
73
74/*
75 * Not needed as the board knows its config
76 * internally and the ID will be fetched
77 * via AOP_INQUIRE_SETUP_INFO command.
78 */
79#define AHA_MCA_SCSIID_POS		MCA_ADP_POS(MCA_POS2)
80#define AHA_MCA_SCSIID_MASK		0xe0
81#define AHA_MCA_SCSIID(pos)		((pos & AHA_MCA_SCSIID_MASK) >> 5)
82
83static int
84aha_mca_probe (device_t dev)
85{
86	const char *	desc;
87	mca_id_t	id = mca_get_id(dev);
88	uint32_t	iobase = 0;
89	uint32_t	iosize = 0;
90	uint8_t	drq = 0;
91	uint8_t	irq = 0;
92	uint8_t	pos;
93
94	desc = mca_match_id(id, aha_mca_devs);
95	if (!desc)
96		return (ENXIO);
97	device_set_desc(dev, desc);
98
99	pos = mca_pos_read(dev, AHA_MCA_IOPORT_POS);
100	iobase = AHA_MCA_IOPORT(pos);
101	iosize = AHA_MCA_IOPORT_SIZE;
102
103	pos = mca_pos_read(dev, AHA_MCA_DRQ_POS);
104	drq = AHA_MCA_DRQ(pos);
105
106	pos = mca_pos_read(dev, AHA_MCA_IRQ_POS);
107	irq = AHA_MCA_IRQ(pos);
108
109	mca_add_iospace(dev, iobase, iosize);
110	mca_add_drq(dev, drq);
111	mca_add_irq(dev, irq);
112
113	return (0);
114}
115
116static int
117aha_mca_attach (device_t dev)
118{
119	struct aha_softc *	sc = device_get_softc(dev);
120	struct resource *	io = NULL;
121	struct resource *	irq = NULL;
122	struct resource *	drq = NULL;
123	int			error = 0;
124	int			unit = device_get_unit(dev);
125	int			rid;
126	void *			ih;
127
128	rid = 0;
129	io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
130				0, ~0, 1, RF_ACTIVE);
131	if (!io) {
132		device_printf(dev, "No I/O space?!\n");
133		error = ENOMEM;
134		goto bad;
135	}
136
137	rid = 0;
138	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
139				 0, ~0, 1, RF_ACTIVE);
140	if (irq == NULL) {
141		device_printf(dev, "No IRQ?!\n");
142		error = ENOMEM;
143		goto bad;
144	}
145
146	rid = 0;
147	drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
148				 0, ~0, 1, RF_ACTIVE);
149	if (drq == NULL) {
150		device_printf(dev, "No DRQ?!\n");
151		error = ENOMEM;
152		goto bad;
153	}
154
155	aha_alloc(sc, unit, rman_get_bustag(io), rman_get_bushandle(io));
156	error = aha_probe(sc);
157	if (error) {
158		device_printf(dev, "aha_probe() failed!\n");
159		goto bad;
160	}
161
162	error = aha_fetch_adapter_info(sc);
163	if (error) {
164		device_printf(dev, "aha_fetch_adapter_info() failed!\n");
165		goto bad;
166	}
167
168	isa_dmacascade(rman_get_start(drq));
169
170	error = bus_dma_tag_create(
171				/* parent	*/ NULL,
172				/* alignemnt	*/ 1,
173				/* boundary	*/ 0,
174				/* lowaddr	*/ BUS_SPACE_MAXADDR_24BIT,
175				/* highaddr	*/ BUS_SPACE_MAXADDR,
176				/* filter	*/ NULL,
177				/* filterarg	*/ NULL,
178				/* maxsize	*/ BUS_SPACE_MAXSIZE_24BIT,
179				/* nsegments	*/ ~0,
180				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_24BIT,
181				/* flags	*/ 0,
182				/* lockfunc	*/ busdma_lock_mutex,
183				/* lockarg	*/ &Giant,
184				&sc->parent_dmat);
185	if (error) {
186		device_printf(dev, "bus_dma_tag_create() failed!\n");
187		goto bad;
188	}
189
190	error = aha_init(sc);
191	if (error) {
192		device_printf(dev, "aha_init() failed\n");
193		goto bad;
194	}
195
196	error = aha_attach(sc);
197	if (error) {
198		device_printf(dev, "aha_attach() failed\n");
199		goto bad;
200	}
201
202	error = bus_setup_intr(dev, irq, INTR_TYPE_CAM|INTR_ENTROPY, aha_intr, sc, &ih);
203	if (error) {
204		device_printf(dev, "Unable to register interrupt handler\n");
205		goto bad;
206	}
207
208	return (0);
209
210bad:
211	aha_free(sc);
212
213	if (io)
214		bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
215	if (irq)
216		bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
217	if (drq)
218		bus_release_resource(dev, SYS_RES_DRQ, 0, drq);
219
220	return (error);
221}
222
223static device_method_t aha_mca_methods[] = {
224	DEVMETHOD(device_probe,         aha_mca_probe),
225	DEVMETHOD(device_attach,        aha_mca_attach),
226
227	{ 0, 0 }
228};
229
230static driver_t aha_mca_driver = {
231	"aha",
232	aha_mca_methods,
233	1,
234/*
235	sizeof(struct aha_softc *),
236 */
237};
238
239static devclass_t aha_devclass;
240
241DRIVER_MODULE(aha, mca, aha_mca_driver, aha_devclass, 0, 0);
242