aha_mca.c revision 51829
167761Smsmith/*-
267761Smsmith * Copyright (c) 1999 Matthew N. Dodd <winter@jurai.net>
367761Smsmith * All rights reserved.
467761Smsmith *
567761Smsmith * Redistribution and use in source and binary forms, with or without
667761Smsmith * modification, are permitted provided that the following conditions
767761Smsmith * are met:
867761Smsmith * 1. Redistributions of source code must retain the above copyright
967761Smsmith *    notice, this list of conditions and the following disclaimer.
1067761Smsmith * 2. Redistributions in binary form must reproduce the above copyright
1167761Smsmith *    notice, this list of conditions and the following disclaimer in the
1267761Smsmith *    documentation and/or other materials provided with the distribution.
1367761Smsmith *
1467761Smsmith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1567761Smsmith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1667761Smsmith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1767761Smsmith * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1867761Smsmith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1967761Smsmith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2067761Smsmith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2167761Smsmith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2267761Smsmith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2367761Smsmith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2467761Smsmith * SUCH DAMAGE.
2567761Smsmith *
2667761Smsmith * $FreeBSD: head/sys/dev/aha/aha_mca.c 51829 1999-10-01 03:12:15Z mdodd $
2767761Smsmith *
2867761Smsmith * Based on aha_isa.c
2967761Smsmith */
3069744Smsmith
3169744Smsmith#include <sys/types.h>
3269744Smsmith#include <sys/param.h>
3367761Smsmith#include <sys/systm.h>
3467761Smsmith#include <sys/kernel.h>
3567761Smsmith
3667761Smsmith#include <sys/module.h>
3770271Stakawata#include <sys/bus.h>
3870271Stakawata#include <machine/bus.h>
3970271Stakawata#include <machine/resource.h>
4070271Stakawata#include <sys/rman.h>
4170271Stakawata
4270271Stakawata#include <i386/isa/isa_dma.h>
4370271Stakawata
4470271Stakawata#include <dev/mca/mca_busreg.h>
4570271Stakawata#include <dev/mca/mca_busvar.h>
4670271Stakawata
4770271Stakawata#include <dev/aha/ahareg.h>
4870271Stakawata
4970271Stakawatastatic struct mca_ident aha_mca_devs[] = {
5070271Stakawata	{ 0x0f1f, "Adaptec AHA-1640 SCSI Adapter" },
5170271Stakawata	{ 0, NULL },
5270271Stakawata};
5370271Stakawata
5470271Stakawata#define AHA_MCA_IOPORT_POS		MCA_ADP_POS(MCA_POS1)
5570271Stakawata# define AHA_MCA_IOPORT_MASK1		0x07
5670271Stakawata# define AHA_MCA_IOPORT_MASK2		0xc0
5770271Stakawata# define AHA_MCA_IOPORT_SIZE		0x03
5870271Stakawata# define AHA_MCA_IOPORT(pos)		(0x30 + \
5970271Stakawata					(((u_int32_t)pos & \
6070271Stakawata						AHA_MCA_IOPORT_MASK1) << 8) + \
6169744Smsmith					(((u_int32_t)pos & \
6269744Smsmith						AHA_MCA_IOPORT_MASK2) >> 4))
6369744Smsmith
6469744Smsmith#define AHA_MCA_DRQ_POS			MCA_ADP_POS(MCA_POS3)
65# define AHA_MCA_DRQ_MASK		0x0f
66# define AHA_MCA_DRQ(pos)		(pos & AHA_MCA_DRQ_MASK)
67
68#define AHA_MCA_IRQ_POS			MCA_ADP_POS(MCA_POS2)
69# define AHA_MCA_IRQ_MASK		0x07
70# define AHA_MCA_IRQ(pos)		((pos & AHA_MCA_IRQ_MASK) + 8)
71
72/*
73 * Not needed as the board knows its config
74 * internally and the ID will be fetched
75 * via AOP_INQUIRE_SETUP_INFO command.
76 */
77#define AHA_MCA_SCSIID_POS		MCA_ADP_POS(MCA_POS2)
78#define AHA_MCA_SCSIID_MASK		0xe0
79#define AHA_MCA_SCSIID(pos)		((pos & AHA_MCA_SCSIID_MASK) >> 5)
80
81static int
82aha_mca_probe (device_t dev)
83{
84	const char *	desc;
85	mca_id_t	id = mca_get_id(dev);
86	u_int32_t	iobase = 0;
87	u_int32_t	iosize = 0;
88	u_int8_t	drq = 0;
89	u_int8_t	irq = 0;
90	u_int8_t	pos;
91
92	desc = mca_match_id(id, aha_mca_devs);
93	if (!desc)
94		return (ENXIO);
95	device_set_desc(dev, desc);
96
97	pos = mca_pos_read(dev, AHA_MCA_IOPORT_POS);
98	iobase = AHA_MCA_IOPORT(pos);
99	iosize = AHA_MCA_IOPORT_SIZE;
100
101	pos = mca_pos_read(dev, AHA_MCA_DRQ_POS);
102	drq = AHA_MCA_DRQ(pos);
103
104	pos = mca_pos_read(dev, AHA_MCA_IRQ_POS);
105	irq = AHA_MCA_IRQ(pos);
106
107	mca_add_iospace(dev, iobase, iosize);
108	mca_add_drq(dev, drq);
109	mca_add_irq(dev, irq);
110
111	/* We're going to attempt to probe it now, so mark it probed */
112	aha_mark_probed_iop(iobase);
113
114	aha_unit++;
115
116	return (0);
117}
118
119static int
120aha_mca_attach (device_t dev)
121{
122	struct aha_softc *	sc = NULL;
123	struct resource *	io = NULL;
124	struct resource *	irq = NULL;
125	struct resource *	drq = NULL;
126	int			error = 0;
127	int			unit = device_get_unit(dev);
128	int			rid;
129	void *			ih;
130
131	rid = 0;
132	io = bus_alloc_resource(dev, SYS_RES_IOPORT, &rid,
133				0, ~0, 1, RF_ACTIVE);
134	if (!io) {
135		device_printf(dev, "No I/O space?!\n");
136		error = ENOMEM;
137		goto bad;
138	}
139
140	rid = 0;
141	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid,
142				 0, ~0, 1, RF_ACTIVE);
143	if (irq == NULL) {
144		device_printf(dev, "No IRQ?!\n");
145		error = ENOMEM;
146		goto bad;
147	}
148
149	rid = 0;
150	drq = bus_alloc_resource(dev, SYS_RES_DRQ, &rid,
151				 0, ~0, 1, RF_ACTIVE);
152	if (drq == NULL) {
153		device_printf(dev, "No DRQ?!\n");
154		error = ENOMEM;
155		goto bad;
156	}
157
158	sc = aha_alloc(unit, rman_get_bustag(io), rman_get_bushandle(io));
159	if (sc == NULL) {
160		device_printf(dev, "aha_alloc() failed!\n");
161		error = ENOMEM;
162		goto bad;
163	}
164
165	error = aha_probe(sc);
166	if (error) {
167		device_printf(dev, "aha_probe() failed!\n");
168		goto bad;
169	}
170
171	error = aha_fetch_adapter_info(sc);
172	if (error) {
173		device_printf(dev, "aha_fetch_adapter_info() failed!\n");
174		goto bad;
175	}
176
177	isa_dmacascade(rman_get_start(drq));
178
179	error = bus_dma_tag_create(/* parent	*/	NULL,
180				   /* alignemnt	*/	1,
181				   /* boundary	*/	0,
182				   			BUS_SPACE_MAXADDR_24BIT,
183				   /* highaddr	*/	BUS_SPACE_MAXADDR,
184				   			NULL,
185							NULL,
186				   /* maxsize	*/	BUS_SPACE_MAXSIZE_24BIT,
187				   /* nsegments	*/	BUS_SPACE_UNRESTRICTED,
188				   /* maxsegsz	*/	BUS_SPACE_MAXSIZE_24BIT,
189				   /* flags	*/	0,
190							&sc->parent_dmat);
191	if (error) {
192		device_printf(dev, "bus_dma_tag_create() failed!\n");
193		goto bad;
194	}
195
196	error = aha_init(sc);
197	if (error) {
198		device_printf(dev, "aha_init() failed\n");
199		goto bad;
200	}
201
202	error = aha_attach(sc);
203	if (error) {
204		device_printf(dev, "aha_attach() failed\n");
205		goto bad;
206	}
207
208	error = bus_setup_intr(dev, irq, INTR_TYPE_CAM, aha_intr, sc, &ih);
209	if (error) {
210		device_printf(dev, "Unable to register interrupt handler\n");
211		goto bad;
212	}
213
214	return (0);
215
216bad:
217	aha_free(sc);
218
219	if (io)
220		bus_release_resource(dev, SYS_RES_IOPORT, 0, io);
221	if (irq)
222		bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
223	if (drq)
224		bus_release_resource(dev, SYS_RES_DRQ, 0, drq);
225
226	return (error);
227}
228
229static device_method_t aha_mca_methods[] = {
230	DEVMETHOD(device_probe,         aha_mca_probe),
231	DEVMETHOD(device_attach,        aha_mca_attach),
232
233	{ 0, 0 }
234};
235
236static driver_t aha_mca_driver = {
237	"aha",
238	aha_mca_methods,
239	1,
240/*
241	sizeof(struct aha_softc *),
242 */
243};
244
245static devclass_t aha_devclass;
246
247DRIVER_MODULE(aha, mca, aha_mca_driver, aha_devclass, 0, 0);
248