xref: /openbsd/sys/dev/pci/twe_pci.c (revision db3296cf)
1 /*	$OpenBSD: twe_pci.c,v 1.7 2003/06/02 19:24:23 mickey 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR OR HIS RELATIVES BE LIABLE FOR ANY DIRECT,
20  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22  * SERVICES; LOSS OF MIND, USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
24  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26  * THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/device.h>
34 
35 #include <dev/pci/pcidevs.h>
36 #include <dev/pci/pcivar.h>
37 
38 #include <machine/bus.h>
39 
40 #include <scsi/scsi_all.h>
41 #include <scsi/scsi_disk.h>
42 #include <scsi/scsiconf.h>
43 
44 #include <dev/ic/twereg.h>
45 #include <dev/ic/twevar.h>
46 
47 #define	TWE_BAR	0x10
48 
49 int	twe_pci_match(struct device *, void *, void *);
50 void	twe_pci_attach(struct device *, struct device *, void *);
51 
52 struct cfattach twe_pci_ca = {
53 	sizeof(struct twe_softc), twe_pci_match, twe_pci_attach
54 };
55 
56 int
57 twe_pci_match(parent, match, aux)
58 	struct device *parent;
59 	void *match;
60 	void *aux;
61 {
62 	struct pci_attach_args *pa = aux;
63 
64 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TRIWARE &&
65 	    (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TRIWARE_ESCALADE ||
66 	    PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_TRIWARE_ESCALADE_ASIC))
67 		return 1;
68 
69 	return 0;
70 }
71 
72 void
73 twe_pci_attach(parent, self, aux)
74 	struct device *parent, *self;
75 	void *aux;
76 {
77 	struct twe_softc *sc = (struct twe_softc *)self;
78 	struct pci_attach_args *pa = aux;
79 	pci_intr_handle_t ih;
80 	const char *intrstr;
81 	bus_size_t size;
82 	pcireg_t csr;
83 
84 	if (pci_mapreg_map(pa, TWE_BAR, PCI_MAPREG_TYPE_IO, 0,
85 	    &sc->iot, &sc->ioh, NULL, &size, 0)) {
86 		printf(": can't map controller i/o space\n");
87 		return;
88 	}
89 	sc->dmat = pa->pa_dmat;
90 
91 	/* enable bus mastering (should not it be mi?) */
92 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
93 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
94 	    csr | PCI_COMMAND_MASTER_ENABLE);
95 
96 	if (pci_intr_map(pa, &ih)) {
97 		printf(": can't map interrupt\n");
98 		bus_space_unmap(sc->iot, sc->ioh, size);
99 		return;
100 	}
101 	intrstr = pci_intr_string(pa->pa_pc, ih);
102 	sc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO, twe_intr, sc,
103 	    sc->sc_dev.dv_xname);
104 	if (!sc->sc_ih) {
105 		printf(": can't establish interrupt");
106 		if (intrstr)
107 			printf(" at %s", intrstr);
108 		printf("\n");
109 		bus_space_unmap(sc->iot, sc->ioh, size);
110 	}
111 
112 	printf(": %s\n%s", intrstr, sc->sc_dev.dv_xname);
113 
114 	if (twe_attach(sc)) {
115 		pci_intr_disestablish(pa->pa_pc, sc->sc_ih);
116 		sc->sc_ih = NULL;
117 		bus_space_unmap(sc->iot, sc->ioh, size);
118 	}
119 }
120