1 /* $NetBSD: bha_pci.c,v 1.41 2016/10/13 17:11:09 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: bha_pci.c,v 1.41 2016/10/13 17:11:09 jdolecek Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/device.h>
38
39 #include <sys/bus.h>
40 #include <sys/intr.h>
41
42 #include <dev/scsipi/scsipi_all.h>
43 #include <dev/scsipi/scsiconf.h>
44
45 #include <dev/pci/pcivar.h>
46 #include <dev/pci/pcidevs.h>
47
48 #include <dev/ic/bhareg.h>
49 #include <dev/ic/bhavar.h>
50
51 #define PCI_CBIO 0x10
52
53 /*
54 * Check the slots looking for a board we recognise
55 * If we find one, note its address (slot) and call
56 * the actual probe routine to check it out.
57 */
58 static int
bha_pci_match(device_t parent,cfdata_t match,void * aux)59 bha_pci_match(device_t parent, cfdata_t match, void *aux)
60 {
61 struct pci_attach_args *pa = aux;
62 bus_space_tag_t iot;
63 bus_space_handle_t ioh;
64 bus_size_t iosize;
65 int rv;
66
67 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BUSLOGIC)
68 return (0);
69
70 if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BUSLOGIC_MULTIMASTER_NC &&
71 PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BUSLOGIC_MULTIMASTER)
72 return (0);
73
74 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &iot, &ioh,
75 NULL, &iosize))
76 return (0);
77
78 rv = bha_find(iot, ioh);
79
80 bus_space_unmap(iot, ioh, iosize);
81
82 return (rv);
83 }
84
85 /*
86 * Attach all the sub-devices we can find
87 */
88 static void
bha_pci_attach(device_t parent,device_t self,void * aux)89 bha_pci_attach(device_t parent, device_t self, void *aux)
90 {
91 struct pci_attach_args *pa = aux;
92 struct bha_softc *sc = device_private(self);
93 bus_space_tag_t iot;
94 bus_space_handle_t ioh;
95 pci_chipset_tag_t pc = pa->pa_pc;
96 pci_intr_handle_t ih;
97 pcireg_t csr;
98 const char *model, *intrstr;
99 char intrbuf[PCI_INTRSTR_LEN];
100
101 sc->sc_dev = self;
102
103 aprint_naive(": SCSI controller\n");
104
105 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BUSLOGIC_MULTIMASTER_NC)
106 model = "BusLogic 9xxC SCSI";
107 else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BUSLOGIC_MULTIMASTER)
108 model = "BusLogic 9xxC SCSI";
109 else
110 model = "unknown model!";
111 aprint_normal(": %s\n", model);
112
113 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &iot, &ioh,
114 NULL, NULL)) {
115 aprint_error_dev(sc->sc_dev, "unable to map device registers\n");
116 return;
117 }
118
119 sc->sc_iot = iot;
120 sc->sc_ioh = ioh;
121 sc->sc_dmat = pa->pa_dmat;
122 if (!bha_find(iot, ioh))
123 panic("bha_pci_attach: bha_find failed");
124
125 sc->sc_dmaflags = 0;
126
127 csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
128 pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
129 csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE);
130
131 if (pci_intr_map(pa, &ih)) {
132 aprint_error_dev(sc->sc_dev, "couldn't map interrupt\n");
133 return;
134 }
135 intrstr = pci_intr_string(pc, ih, intrbuf, sizeof(intrbuf));
136 sc->sc_ih = pci_intr_establish_xname(pc, ih, IPL_BIO, bha_intr, sc,
137 device_xname(sc->sc_dev));
138 if (sc->sc_ih == NULL) {
139 aprint_error_dev(sc->sc_dev, "couldn't establish interrupt");
140 if (intrstr != NULL)
141 aprint_error(" at %s", intrstr);
142 aprint_error("\n");
143 return;
144 }
145 aprint_normal_dev(sc->sc_dev, "interrupting at %s\n", intrstr);
146
147 bha_attach(sc);
148
149 bha_disable_isacompat(sc);
150 }
151
152 CFATTACH_DECL_NEW(bha_pci, sizeof(struct bha_softc),
153 bha_pci_match, bha_pci_attach, NULL, NULL);
154