1106121Sdes/*	$NetBSD: bha_pci.c,v 1.36 2009/05/12 08:23:00 cegger Exp $	*/
2124208Sdes
3124208Sdes/*-
4124208Sdes * Copyright (c) 1998 The NetBSD Foundation, Inc.
5106121Sdes * All rights reserved.
6106121Sdes *
7106121Sdes * This code is derived from software contributed to The NetBSD Foundation
8106121Sdes * by Charles M. Hannum.
9106121Sdes *
10106121Sdes * Redistribution and use in source and binary forms, with or without
11106121Sdes * modification, are permitted provided that the following conditions
12106121Sdes * are met:
13106121Sdes * 1. Redistributions of source code must retain the above copyright
14106121Sdes *    notice, this list of conditions and the following disclaimer.
15106121Sdes * 2. Redistributions in binary form must reproduce the above copyright
16106121Sdes *    notice, this list of conditions and the following disclaimer in the
17106121Sdes *    documentation and/or other materials provided with the distribution.
18106121Sdes *
19106121Sdes * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20106121Sdes * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21106121Sdes * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22106121Sdes * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23106121Sdes * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24106121Sdes * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25106121Sdes * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26137015Sdes * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27124208Sdes * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28106121Sdes * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29106121Sdes * POSSIBILITY OF SUCH DAMAGE.
30106121Sdes */
31106121Sdes
32106121Sdes#include <sys/cdefs.h>
33106121Sdes__KERNEL_RCSID(0, "$NetBSD: bha_pci.c,v 1.36 2009/05/12 08:23:00 cegger Exp $");
34106121Sdes
35106121Sdes#include <sys/param.h>
36106121Sdes#include <sys/systm.h>
37106121Sdes#include <sys/device.h>
38106121Sdes
39106121Sdes#include <sys/bus.h>
40106121Sdes#include <sys/intr.h>
41106121Sdes
42106121Sdes#include <dev/scsipi/scsipi_all.h>
43137015Sdes#include <dev/scsipi/scsiconf.h>
44106121Sdes
45106121Sdes#include <dev/pci/pcivar.h>
46137015Sdes#include <dev/pci/pcidevs.h>
47106121Sdes
48106121Sdes#include <dev/ic/bhareg.h>
49106121Sdes#include <dev/ic/bhavar.h>
50106121Sdes
51106121Sdes#define	PCI_CBIO	0x10
52106121Sdes
53137015Sdes/*
54106121Sdes * Check the slots looking for a board we recognise
55137015Sdes * If we find one, note it's address (slot) and call
56106121Sdes * the actual probe routine to check it out.
57137015Sdes */
58106121Sdesstatic int
59106121Sdesbha_pci_match(device_t parent, cfdata_t match, void *aux)
60106121Sdes{
61106121Sdes	struct pci_attach_args *pa = aux;
62106121Sdes	bus_space_tag_t iot;
63106121Sdes	bus_space_handle_t ioh;
64137015Sdes	bus_size_t iosize;
65106121Sdes	int rv;
66106121Sdes
67106121Sdes	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_BUSLOGIC)
68106121Sdes		return (0);
69106121Sdes
70106121Sdes	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BUSLOGIC_MULTIMASTER_NC &&
71106121Sdes	    PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_BUSLOGIC_MULTIMASTER)
72106121Sdes		return (0);
73106121Sdes
74106121Sdes	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &iot, &ioh,
75106121Sdes	    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 */
88static void
89bha_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
100	aprint_naive(": SCSI controller\n");
101
102	if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BUSLOGIC_MULTIMASTER_NC)
103		model = "BusLogic 9xxC SCSI";
104	else if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_BUSLOGIC_MULTIMASTER)
105		model = "BusLogic 9xxC SCSI";
106	else
107		model = "unknown model!";
108	aprint_normal(": %s\n", model);
109
110	if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0, &iot, &ioh,
111	    NULL, NULL)) {
112		aprint_error_dev(&sc->sc_dev, "unable to map device registers\n");
113		return;
114	}
115
116	sc->sc_iot = iot;
117	sc->sc_ioh = ioh;
118	sc->sc_dmat = pa->pa_dmat;
119	if (!bha_find(iot, ioh))
120		panic("bha_pci_attach: bha_find failed");
121
122	sc->sc_dmaflags = 0;
123
124	csr = pci_conf_read(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
125	pci_conf_write(pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
126	    csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE);
127
128	if (pci_intr_map(pa, &ih)) {
129		aprint_error_dev(&sc->sc_dev, "couldn't map interrupt\n");
130		return;
131	}
132	intrstr = pci_intr_string(pc, ih);
133	sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, bha_intr, sc);
134	if (sc->sc_ih == NULL) {
135		aprint_error_dev(&sc->sc_dev, "couldn't establish interrupt");
136		if (intrstr != NULL)
137			aprint_error(" at %s", intrstr);
138		aprint_error("\n");
139		return;
140	}
141	aprint_normal_dev(&sc->sc_dev, "interrupting at %s\n", intrstr);
142
143	bha_attach(sc);
144
145	bha_disable_isacompat(sc);
146}
147
148CFATTACH_DECL(bha_pci, sizeof(struct bha_softc),
149    bha_pci_match, bha_pci_attach, NULL, NULL);
150