xref: /openbsd-src/sys/dev/pci/twe_pci.c (revision 47911bd667ac77dc523b8a13ef40b012dbffa741)
1 /*	$OpenBSD: twe_pci.c,v 1.6 2002/03/14 01:27:00 millert Exp $	*/
2 
3 /*
4  * Copyright (c) 2000 Michael Shalayeff
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *      This product includes software developed by Michael Shalayeff.
18  * 4. The name of the author may not be used to endorse or promote products
19  *    derived from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
25  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
31  * THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/device.h>
39 
40 #include <dev/pci/pcidevs.h>
41 #include <dev/pci/pcivar.h>
42 
43 #include <machine/bus.h>
44 
45 #include <scsi/scsi_all.h>
46 #include <scsi/scsi_disk.h>
47 #include <scsi/scsiconf.h>
48 
49 #include <dev/ic/twereg.h>
50 #include <dev/ic/twevar.h>
51 
52 #define	TWE_BAR	0x10
53 
54 int	twe_pci_match(struct device *, void *, void *);
55 void	twe_pci_attach(struct device *, struct device *, void *);
56 
57 struct cfattach twe_pci_ca = {
58 	sizeof(struct twe_softc), twe_pci_match, twe_pci_attach
59 };
60 
61 int
62 twe_pci_match(parent, match, aux)
63 	struct device *parent;
64 	void *match;
65 	void *aux;
66 {
67 	struct pci_attach_args *pa = aux;
68 
69 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TRIWARE &&
70 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TRIWARE_ESCALADE ||
71 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TRIWARE_ESCALADE_ASIC))
72 		return 1;
73 
74 	return 0;
75 }
76 
77 void
78 twe_pci_attach(parent, self, aux)
79 	struct device *parent, *self;
80 	void *aux;
81 {
82 	struct twe_softc *sc = (struct twe_softc *)self;
83 	struct pci_attach_args *pa = aux;
84 	pci_intr_handle_t ih;
85 	const char *intrstr;
86 	bus_size_t size;
87 	pcireg_t csr;
88 
89 	if (pci_mapreg_map(pa, TWE_BAR, PCI_MAPREG_TYPE_IO, 0,
90 	    &sc->iot, &sc->ioh, NULL, &size, 0)) {
91 		printf(": can't map controller i/o space\n");
92 		return;
93 	}
94 	sc->dmat = pa->pa_dmat;
95 
96 	/* enable bus mastering (should not it be mi?) */
97 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
98 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
99 	    csr | PCI_COMMAND_MASTER_ENABLE);
100 
101 	if (pci_intr_map(pa, &ih)) {
102 		printf(": can't map interrupt\n");
103 		bus_space_unmap(sc->iot, sc->ioh, size);
104 		return;
105 	}
106 	intrstr = pci_intr_string(pa->pa_pc, ih);
107 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, twe_intr, sc,
108 	    sc->sc_dev.dv_xname);
109 	if (!sc->sc_ih) {
110 		printf(": can't establish interrupt");
111 		if (intrstr)
112 			printf(" at %s", intrstr);
113 		printf("\n");
114 		bus_space_unmap(sc->iot, sc->ioh, size);
115 	}
116 
117 	printf(": %s\n%s", intrstr, sc->sc_dev.dv_xname);
118 
119 	if (twe_attach(sc)) {
120 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
121 		sc->sc_ih = NULL;
122 		bus_space_unmap(sc->iot, sc->ioh, size);
123 	}
124 }
125