84e1b24ed3f308b01726d695cb0c31f59ef62533
[dragonfly.git] / sys / dev / disk / buslogic / bt_pci.c
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.11 2000/01/17 12:38:00 nyan Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bus.h>
36 #include <sys/rman.h>
37 #include <sys/thread2.h>
38
39 #include <bus/pci/pcireg.h>
40 #include <bus/pci/pcivar.h>
41
42 #include "btreg.h"
43
44 #define BT_PCI_IOADDR   PCIR_MAPS
45 #define BT_PCI_MEMADDR  PCIR_MAPS + 4
46
47 #define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER      0x1040104Bul
48 #define PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC   0x0140104Bul
49 #define PCI_DEVICE_ID_BUSLOGIC_FLASHPOINT       0x8130104Bul
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(dev, type, &rid,
65                                           0, ~0, 1, RF_ACTIVE);
66         }
67 #else
68         if (!regs && (command & PCIM_CMD_PORTEN)) {
69                 type = SYS_RES_IOPORT;
70                 rid = BT_PCI_IOADDR;
71                 regs = bus_alloc_resource(dev, type, &rid,
72                                           0, ~0, 1, RF_ACTIVE);
73         }
74 #endif
75         if (!regs)
76                 return (ENOMEM);
77         
78         zero = 0;
79         irq = bus_alloc_resource(dev, SYS_RES_IRQ, &zero,
80                                  0, ~0, 1, RF_ACTIVE | RF_SHAREABLE);
81         if (!irq) {
82                 bus_release_resource(dev, type, rid, regs);
83                 return (ENOMEM);
84         }
85
86         bt_init_softc(dev, regs, irq, 0);
87
88         return (0);
89 }
90
91 static void
92 bt_pci_release_resources(device_t dev)
93 {
94         struct bt_softc *bt = device_get_softc(dev);
95
96         if (bt->port)
97                 /* XXX can't cope with memory registers anyway */
98                 bus_release_resource(dev, SYS_RES_IOPORT,
99                                      BT_PCI_IOADDR, bt->port);
100         if (bt->irq)
101                 bus_release_resource(dev, SYS_RES_IRQ, 0, bt->irq);
102         bt_free_softc(dev);
103 }
104
105 static int
106 bt_pci_probe(device_t dev)
107 {
108         switch (pci_get_devid(dev)) {
109                 case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER:
110                 case PCI_DEVICE_ID_BUSLOGIC_MULTIMASTER_NC:
111                 {
112                         struct bt_softc   *bt = device_get_softc(dev);
113                         pci_info_data_t pci_info;
114                         int error;
115
116                         error = bt_pci_alloc_resources(dev);
117                         if (error)
118                                 return (error);
119
120                         /*
121                          * Determine if an ISA compatible I/O port has been
122                          * enabled.  If so, record the port so it will not
123                          * be probed by our ISA probe.  If the PCI I/O port
124                          * was not set to the compatibility port, disable it.
125                          */
126                         error = bt_cmd(bt, BOP_INQUIRE_PCI_INFO,
127                                        /*param*/NULL, /*paramlen*/0,
128                                        (u_int8_t*)&pci_info, sizeof(pci_info),
129                                        DEFAULT_CMD_TIMEOUT);
130                         if (error == 0
131                          && pci_info.io_port < BIO_DISABLED) {
132                                 bt_mark_probed_bio(pci_info.io_port);
133                                 if (rman_get_start(bt->port) !=
134                                     bt_iop_from_bio(pci_info.io_port)) {
135                                         u_int8_t new_addr;
136
137                                         new_addr = BIO_DISABLED;
138                                         bt_cmd(bt, BOP_MODIFY_IO_ADDR,
139                                                /*param*/&new_addr,
140                                                /*paramlen*/1, /*reply_buf*/NULL,
141                                                /*reply_len*/0,
142                                                DEFAULT_CMD_TIMEOUT);
143                                 }
144                         }
145                         bt_pci_release_resources(dev);
146                         device_set_desc(dev, "Buslogic Multi-Master SCSI Host Adapter");
147                         return (0);
148                 }
149                 default:
150                         break;
151         }
152
153         return (ENXIO);
154 }
155
156 static int
157 bt_pci_attach(device_t dev)
158 {
159         struct bt_softc   *bt = device_get_softc(dev);
160         int                error;
161
162         /* Initialize softc */
163         error = bt_pci_alloc_resources(dev);
164         if (error) {
165                 device_printf(dev, "can't allocate resources in bt_pci_attach\n");
166                 return error;
167         }
168
169         /* Allocate a dmatag for our CCB DMA maps */
170         /* XXX Should be a child of the PCI bus dma tag */
171         if (bus_dma_tag_create(/*parent*/NULL, /*alignemnt*/1, /*boundary*/0,
172                                /*lowaddr*/BUS_SPACE_MAXADDR_32BIT,
173                                /*highaddr*/BUS_SPACE_MAXADDR,
174                                /*filter*/NULL, /*filterarg*/NULL,
175                                /*maxsize*/BUS_SPACE_MAXSIZE_32BIT,
176                                /*nsegments*/BUS_SPACE_UNRESTRICTED,
177                                /*maxsegsz*/BUS_SPACE_MAXSIZE_32BIT,
178                                /*flags*/0, &bt->parent_dmat) != 0) {
179                 bt_pci_release_resources(dev);
180                 return (ENOMEM);
181         }
182
183         /*
184          * Protect ourself from spurrious interrupts during
185          * intialization and attach.  We should really rely
186          * on interrupts during attach, but we don't have
187          * access to our interrupts during ISA probes, so until
188          * that changes, we mask our interrupts during attach
189          * too.
190          */
191         crit_enter();
192
193         if (bt_probe(dev) || bt_fetch_adapter_info(dev) || bt_init(dev)) {
194                 bt_pci_release_resources(dev);
195                 crit_exit();
196                 return (ENXIO);
197         }
198
199         error = bt_attach(dev);
200         crit_exit();
201
202         if (error) {
203                 bt_pci_release_resources(dev);
204                 return (error);
205         }
206
207         return (0);
208 }
209
210 static device_method_t bt_pci_methods[] = {
211         /* Device interface */
212         DEVMETHOD(device_probe,         bt_pci_probe),
213         DEVMETHOD(device_attach,        bt_pci_attach),
214
215         { 0, 0 }
216 };
217
218 static driver_t bt_pci_driver = {
219         "bt",
220         bt_pci_methods,
221         sizeof(struct bt_softc),
222 };
223
224 static devclass_t bt_devclass;
225
226 DRIVER_MODULE(bt, pci, bt_pci_driver, bt_devclass, NULL, NULL);