xref: /netbsd-src/sys/dev/pci/bha_pci.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1 /*	$NetBSD: bha_pci.c,v 1.33 2008/04/28 20:23:54 martin 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.33 2008/04/28 20:23:54 martin 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 it's address (slot) and call
56  * the actual probe routine to check it out.
57  */
58 static int
59 bha_pci_match(struct device *parent, struct cfdata *match,
60     void *aux)
61 {
62 	struct pci_attach_args *pa = aux;
63 	bus_space_tag_t iot;
64 	bus_space_handle_t ioh;
65 	bus_size_t iosize;
66 	int rv;
67 
68 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BUSLOGIC)
69 		return (0);
70 
71 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BUSLOGIC_MULTIMASTER_NC &&
72 	    PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BUSLOGIC_MULTIMASTER)
73 		return (0);
74 
75 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &iot, &ioh,
76 	    NULL, &iosize))
77 		return (0);
78 
79 	rv = bha_find(iot, ioh);
80 
81 	bus_space_unmap(iot, ioh, iosize);
82 
83 	return (rv);
84 }
85 
86 /*
87  * Attach all the sub-devices we can find
88  */
89 static void
90 bha_pci_attach(struct device *parent, struct device *self, void *aux)
91 {
92 	struct pci_attach_args *pa = aux;
93 	struct bha_softc *sc = (void *)self;
94 	bus_space_tag_t iot;
95 	bus_space_handle_t ioh;
96 	pci_chipset_tag_t pc = pa->pa_pc;
97 	pci_intr_handle_t ih;
98 	pcireg_t csr;
99 	const char *model, *intrstr;
100 
101 	aprint_naive(": SCSI controller\n");
102 
103 	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BUSLOGIC_MULTIMASTER_NC)
104 		model = "BusLogic 9xxC SCSI";
105 	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BUSLOGIC_MULTIMASTER)
106 		model = "BusLogic 9xxC SCSI";
107 	else
108 		model = "unknown model!";
109 	aprint_normal(": %s\n", model);
110 
111 	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &iot, &ioh,
112 	    NULL, NULL)) {
113 		aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
114 		return;
115 	}
116 
117 	sc->sc_iot = iot;
118 	sc->sc_ioh = ioh;
119 	sc->sc_dmat = pa->pa_dmat;
120 	if (!bha_find(iot, ioh))
121 		panic("bha_pci_attach: bha_find failed");
122 
123 	sc->sc_dmaflags = 0;
124 
125 	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
126 	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
127 	    csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE);
128 
129 	if (pci_intr_map(pa, &ih)) {
130 		aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
131 		return;
132 	}
133 	intrstr = pci_intr_string(pc, ih);
134 	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, bha_intr, sc);
135 	if (sc->sc_ih == NULL) {
136 		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
137 		if (intrstr != NULL)
138 			aprint_normal(" at %s", intrstr);
139 		aprint_normal("\n");
140 		return;
141 	}
142 	aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
143 
144 	bha_attach(sc);
145 
146 	bha_disable_isacompat(sc);
147 }
148 
149 CFATTACH_DECL(bha_pci, sizeof(struct bha_softc),
150     bha_pci_match, bha_pci_attach, NULL, NULL);
151