xref: /dragonfly/sys/dev/disk/buslogic/bt_pci.c (revision 9348a738)
1 /*-
2  * Product specific probe and attach routines for:
3  *      Buslogic BT946, BT948, BT956, BT958 SCSI controllers
4  *
5  * Copyright (c) 1995, 1997, 1998 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, this list of conditions, and the following disclaimer,
13  *    without modification, immediately at the beginning of the file.
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  * $FreeBSD: src/sys/dev/buslogic/bt_pci.c,v 1.25 2012/11/17 01:51:40 svnexp Exp $
30  */
31 
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/bus.h>
37 #include <sys/rman.h>
38 #include <sys/thread2.h>
39 
40 #include <bus/pci/pcireg.h>
41 #include <bus/pci/pcivar.h>
42 
43 #include "btreg.h"
44 
45 #define BT_PCI_IOADDR	PCIR_BAR(0)
46 #define BT_PCI_MEMADDR	PCIR_BAR(1)
47 
48 #define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER	0x1040104Bul
49 #define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC	0x0140104Bul
50 
51 static int
52 bt_pci_alloc_resources(device_t dev)
53 {
54 	int		command, type = 0, rid, zero;
55 	struct resource *regs = NULL;
56 	struct resource *irq = NULL;
57 
58 	command = pci_read_config(dev, PCIR_COMMAND, /*bytes*/1);
59 #if 0
60 	/* XXX Memory Mapped I/O seems to cause problems */
61 	if (command & PCIM_CMD_MEMEN) {
62 		type = SYS_RES_MEMORY;
63 		rid = BT_PCI_MEMADDR;
64 		regs = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
65 	}
66 #else
67 	if (!regs && (command & PCIM_CMD_PORTEN)) {
68 		type = SYS_RES_IOPORT;
69 		rid = BT_PCI_IOADDR;
70 		regs = bus_alloc_resource_any(dev, type, &rid, RF_ACTIVE);
71 	}
72 #endif
73 	if (!regs)
74 		return (ENOMEM);
75 
76 	zero = 0;
77 	irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &zero,
78 				     RF_ACTIVE | RF_SHAREABLE);
79 	if (!irq) {
80 		bus_release_resource(dev, type, rid, regs);
81 		return (ENOMEM);
82 	}
83 
84 	bt_init_softc(dev, regs, irq, 0);
85 
86 	return (0);
87 }
88 
89 static void
90 bt_pci_release_resources(device_t dev)
91 {
92 	struct bt_softc *bt = device_get_softc(dev);
93 
94 	if (bt->port)
95 		/* XXX can't cope with memory registers anyway */
96 		bus_release_resource(dev, SYS_RES_IOPORT,
97 				     BT_PCI_IOADDR, bt->port);
98 	if (bt->irq)
99 		bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
100 	bt_free_softc(dev);
101 }
102 
103 static int
104 bt_pci_probe(device_t dev)
105 {
106 	switch (pci_get_devid(dev)) {
107 		case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER:
108 		case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC:
109 			device_set_desc(dev,
110 			    "Buslogic Multi-Master SCSI Host Adapter");
111 			break;
112 		default:
113 			return (ENXIO);
114 	}
115 
116 	return (BUS_PROBE_DEFAULT);
117 }
118 
119 static int
120 bt_pci_attach(device_t dev)
121 {
122 	struct bt_softc   *bt = device_get_softc(dev);
123 	int		   error;
124 
125 	/* Initialize softc */
126 	error = bt_pci_alloc_resources(dev);
127 	if (error) {
128 		device_printf(dev, "can't allocate resources in bt_pci_attach\n");
129 		return error;
130 	}
131 
132 	/* Allocate a dmatag for our CCB DMA maps */
133 	/* XXX Should be a child of the PCI bus dma tag */
134 	if (bus_dma_tag_create(	/* parent	*/ NULL,
135 				/* alignemnt	*/ 1,
136 				/* boundary	*/ 0,
137 				/* lowaddr	*/ BUS_SPACE_MAXADDR_32BIT,
138 				/* highaddr	*/ BUS_SPACE_MAXADDR,
139 				/* filter	*/ NULL,
140 				/* filterarg	*/ NULL,
141 				/* maxsize	*/ BUS_SPACE_MAXSIZE_32BIT,
142 				/* nsegments	*/ ~0,
143 				/* maxsegsz	*/ BUS_SPACE_MAXSIZE_32BIT,
144 				/* flags	*/ 0,
145 				&bt->parent_dmat) != 0) {
146 		bt_pci_release_resources(dev);
147 		return (ENOMEM);
148 	}
149 
150 	if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
151 		bt_pci_release_resources(dev);
152 		return (ENXIO);
153 	}
154 
155 	error = bt_attach(dev);
156 
157 	if (error) {
158 		bt_pci_release_resources(dev);
159 		return (error);
160 	}
161 
162 	return (0);
163 }
164 
165 static device_method_t bt_pci_methods[] = {
166 	/* Device interface */
167 	DEVMETHOD(device_probe,		bt_pci_probe),
168 	DEVMETHOD(device_attach,	bt_pci_attach),
169 
170 	DEVMETHOD_END
171 };
172 
173 static driver_t bt_pci_driver = {
174 	"bt",
175 	bt_pci_methods,
176 	sizeof(struct bt_softc),
177 };
178 
179 static devclass_t bt_devclass;
180 
181 DRIVER_MODULE(bt, pci, bt_pci_driver, bt_devclass, NULL, NULL);
182 MODULE_DEPEND(bt, pci, 1, 1, 1);
183