dpt_pci.c revision 104710
152284Sobrien/*-
2169689Skan * Copyright (c) 2000 Matthew N. Dodd <winter@jurai.net>
352284Sobrien * All rights reserved.
4132718Skan *
552284Sobrien * Copyright (c) 1997 Simon Shapiro
6132718Skan * All Rights Reserved
752284Sobrien *
852284Sobrien * Redistribution and use in source and binary forms, with or without
952284Sobrien * modification, are permitted provided that the following conditions
1052284Sobrien * are met:
11132718Skan * 1. Redistributions of source code must retain the above copyright
1252284Sobrien *    notice, this list of conditions and the following disclaimer.
1352284Sobrien * 2. Redistributions in binary form must reproduce the above copyright
1452284Sobrien *    notice, this list of conditions and the following disclaimer in the
1552284Sobrien *    documentation and/or other materials provided with the distribution.
1652284Sobrien *
17132718Skan * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18169689Skan * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19169689Skan * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2052284Sobrien * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2152284Sobrien * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2252284Sobrien * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2352284Sobrien * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2452284Sobrien * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2590075Sobrien * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2690075Sobrien * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2752284Sobrien * SUCH DAMAGE.
2852284Sobrien *
2952284Sobrien *	$FreeBSD: head/sys/dev/dpt/dpt_pci.c 104710 2002-10-09 08:54:32Z peter $
30117395Skan */
3152284Sobrien
3252284Sobrien#include <sys/param.h>
3352284Sobrien#include <sys/systm.h>
3452284Sobrien#include <sys/kernel.h>
3552284Sobrien#include <sys/module.h>
3652284Sobrien#include <sys/bus.h>
3752284Sobrien
3852284Sobrien#include <machine/bus_memio.h>
3952284Sobrien#include <machine/bus_pio.h>
4052284Sobrien#include <machine/bus.h>
4152284Sobrien#include <machine/resource.h>
4252284Sobrien#include <sys/rman.h>
4352284Sobrien
4452284Sobrien#include <pci/pcireg.h>
4552284Sobrien#include <pci/pcivar.h>
4652284Sobrien
4752284Sobrien#include <cam/scsi/scsi_all.h>
4852284Sobrien
4952284Sobrien#include <dev/dpt/dpt.h>
5052284Sobrien
5152284Sobrien#define	DPT_VENDOR_ID		0x1044
5252284Sobrien#define	DPT_DEVICE_ID		0xa400
5352284Sobrien
5452284Sobrien#define	DPT_PCI_IOADDR		PCIR_MAPS		/* I/O Address */
5552284Sobrien#define	DPT_PCI_MEMADDR		(PCIR_MAPS + 4)		/* Mem I/O Address */
56169689Skan
5752284Sobrien#define	ISA_PRIMARY_WD_ADDRESS	0x1f8
5852284Sobrien
5952284Sobrienstatic int	dpt_pci_probe	(device_t);
6052284Sobrienstatic int	dpt_pci_attach	(device_t);
6152284Sobrien
62169689Skanstatic int
6352284Sobriendpt_pci_probe (device_t dev)
6490075Sobrien{
65	if ((pci_get_vendor(dev) == DPT_VENDOR_ID) &&
66	    (pci_get_device(dev) == DPT_DEVICE_ID)) {
67		device_set_desc(dev, "DPT Caching SCSI RAID Controller");
68		return (0);
69	}
70	return (ENXIO);
71}
72
73static int
74dpt_pci_attach (device_t dev)
75{
76	dpt_softc_t *	dpt;
77	struct resource *io = 0;
78	struct resource *irq = 0;
79	int		s;
80	int		rid;
81	void *		ih;
82	int		error = 0;
83
84	int		iotype = 0;
85	u_int32_t	command;
86
87	command = pci_read_config(dev, PCIR_COMMAND, /*bytes*/1);
88
89#ifdef DPT_ALLOW_MMIO
90	if ((command & PCIM_CMD_MEMEN) != 0) {
91		rid = DPT_PCI_MEMADDR;
92		iotype = SYS_RES_MEMORY;
93		io = bus_alloc_resource(dev, iotype, &rid, 0, ~0, 1, RF_ACTIVE);
94	}
95#endif
96	if (io == NULL && (command &  PCIM_CMD_PORTEN) != 0) {
97		rid = DPT_PCI_IOADDR;
98		iotype = SYS_RES_IOPORT;
99		io = bus_alloc_resource(dev, iotype, &rid, 0, ~0, 1, RF_ACTIVE);
100	}
101
102	if (io == NULL) {
103		device_printf(dev, "can't allocate register resources\n");
104		error = ENOMEM;
105		goto bad;
106	}
107
108	rid = 0;
109	irq = bus_alloc_resource(dev, SYS_RES_IRQ, &rid, 0, ~0, 1,
110				 RF_ACTIVE | RF_SHAREABLE);
111	if (!irq) {
112		device_printf(dev, "No irq?!\n");
113		error = ENOMEM;
114		goto bad;
115	}
116
117	/* Ensure busmastering is enabled */
118	command |= PCIM_CMD_BUSMASTEREN;
119	pci_write_config(dev, PCIR_COMMAND, command, /*bytes*/1);
120
121	if (rman_get_start(io) == (ISA_PRIMARY_WD_ADDRESS - 0x10)) {
122#ifdef DPT_DEBUG_WARN
123		device_printf(dev, "Mapped as an IDE controller.  "
124				   "Disabling SCSI setup\n");
125#endif
126		error = ENXIO;
127		goto bad;
128	}
129
130	/* Device registers are offset 0x10 into the register window.  FEH */
131	dpt = dpt_alloc(dev, rman_get_bustag(io), rman_get_bushandle(io) + 0x10);
132	if (dpt == NULL) {
133		error = ENXIO;
134		goto bad;
135	}
136
137	/* Allocate a dmatag representing the capabilities of this attachment */
138	/* XXX Should be a child of the PCI bus dma tag */
139	if (bus_dma_tag_create(	/* parent    */	NULL,
140				/* alignemnt */	1,
141				/* boundary  */	0,
142				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
143				/* highaddr  */	BUS_SPACE_MAXADDR,
144				/* filter    */	NULL,
145				/* filterarg */	NULL,
146				/* maxsize   */	BUS_SPACE_MAXSIZE_32BIT,
147				/* nsegments */	~0,
148				/* maxsegsz  */	BUS_SPACE_MAXSIZE_32BIT,
149				/* flags     */	0,
150				&dpt->parent_dmat) != 0) {
151		dpt_free(dpt);
152		error = ENXIO;
153		goto bad;
154	}
155
156	s = splcam();
157
158	if (dpt_init(dpt) != 0) {
159		dpt_free(dpt);
160		error = ENXIO;
161		goto bad;
162	}
163
164	/* Register with the XPT */
165	dpt_attach(dpt);
166
167	splx(s);
168
169	if (bus_setup_intr(dev, irq, INTR_TYPE_CAM | INTR_ENTROPY, dpt_intr,
170		 	   dpt, &ih)) {
171		device_printf(dev, "Unable to register interrupt handler\n");
172		error = ENXIO;
173		goto bad;
174	}
175
176	return (error);
177
178bad:
179	if (io)
180		bus_release_resource(dev, iotype, 0, io);
181	if (irq)
182		bus_release_resource(dev, SYS_RES_IRQ, 0, irq);
183
184	return (error);
185}
186
187static device_method_t dpt_pci_methods[] = {
188	/* Device interface */
189	DEVMETHOD(device_probe,         dpt_pci_probe),
190	DEVMETHOD(device_attach,        dpt_pci_attach),
191
192	{ 0, 0 }
193};
194
195static driver_t dpt_pci_driver = {
196	"dpt",
197	dpt_pci_methods,
198	sizeof(dpt_softc_t),
199};
200
201static devclass_t dpt_devclass;
202
203DRIVER_MODULE(dpt, pci, dpt_pci_driver, dpt_devclass, 0, 0);
204