xref: /openbsd-src/sys/dev/pci/trm_pci.c (revision b725ae7711052a2233e31a66fefb8a752c388d7a)
1 /*	$OpenBSD: trm_pci.c,v 1.1 2002/02/18 01:55:30 krw Exp $
2  * ------------------------------------------------------------
3  *       O.S     : OpenBSD
4  *    FILE NAME  : trm_pci.c
5  *         BY    : Erich Chen     (erich@tekram.com.tw)
6  *    Description: Device Driver for Tekram DC395U/UW/F,DC315/U
7  *                 PCI SCSI Bus Master Host Adapter
8  *                 (SCSI chip set used Tekram ASIC TRM-S1040)
9  * (C)Copyright 1995-1999 Tekram Technology Co., Ltd.
10  * (C)Copyright 2001-2002 Ashley R. Martens and Kenneth R. Westerback
11  * ------------------------------------------------------------
12  *    HISTORY:
13  *
14  *  REV#   DATE          NAME           DESCRIPTION
15  *  1.00   05/01/99      ERICH CHEN     First released for NetBSD 1.4.x
16  *  1.01   00/00/00      MARTIN AKESSON Port to OpenBSD 2.8
17  *  1.02   Sep 19, 2001  ASHLEY MARTENS Cleanup and formatting
18  * ------------------------------------------------------------
19  *
20  * Redistribution and use in source and binary forms, with or without
21  * modification, are permitted provided that the following conditions
22  * are met:
23  * 1. Redistributions of source code must retain the above copyright
24  *    notice, this list of conditions and the following disclaimer.
25  * 2. Redistributions in binary form must reproduce the above copyright
26  *    notice, this list of conditions and the following disclaimer in the
27  *    documentation and/or other materials provided with the distribution.
28  * 3. The name of the author may not be used to endorse or promote products
29  *    derived from this software without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
32  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
33  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
34  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
37  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
38  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
40  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
41  *
42  * ------------------------------------------------------------
43  */
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/device.h>
48 
49 #include <dev/pci/pcidevs.h>
50 #include <dev/pci/pcivar.h>
51 
52 #include <scsi/scsi_all.h>
53 #include <scsi/scsiconf.h>
54 
55 #include <dev/ic/trm.h>
56 
57 int	trm_pci_probe (struct device *, void *, void *);
58 void	trm_pci_attach (struct device *, struct device *, void *);
59 
60 struct  cfattach trm_pci_ca = {
61 	sizeof(struct trm_softc),
62 	trm_pci_probe,
63 	trm_pci_attach,
64 	NULL,			/* Detach */
65 	NULL,			/* Activate */
66 	NULL			/* ZeroRef */
67 };
68 
69 
70 /*
71  * ------------------------------------------------------------
72  * Function : trm_pci_probe
73  * Purpose  : Check the slots looking for a board we recognize.
74  *            If we find one, note ti's address (slot) and call
75  *            the actual probe routine to check it out.
76  * Inputs   :
77  * ------------------------------------------------------------
78  */
79 int
80 trm_pci_probe(struct device *parent, void *match, void *aux)
81 {
82 	struct pci_attach_args *pa = aux;
83 
84 	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TEKRAM2) {
85 		switch (PCI_PRODUCT(pa->pa_id)) {
86 		case PCI_PRODUCT_TEKRAM2_DC3X5U:
87 			return 1;
88 		}
89 	}
90 	return 0;
91 }
92 
93 /*
94  * ------------------------------------------------------------
95  * Function : trm_pci_attach
96  * Purpose  :
97  * Inputs   :
98  * ------------------------------------------------------------
99  */
100 void
101 trm_pci_attach(struct device *parent, struct device *self, void *aux)
102 {
103 	struct pci_attach_args *pa = aux;
104 	bus_space_handle_t ioh;/* bus space handle */
105 	pci_intr_handle_t ih;
106 	struct trm_softc *sc = (void *)self;
107 	bus_space_tag_t iot;   /* bus space tag    */
108 	const char *intrstr;
109 	pcireg_t command;
110 	int unit;
111 
112 	unit = sc->sc_device.dv_unit;
113 
114 	/*
115 	 * These cards do not allow memory mapped accesses.
116 	 * pa_pc:  chipset tag
117 	 * pa_tag: pci tag
118 	 */
119 	command = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
120 	command |= PCI_COMMAND_IO_ENABLE | PCI_COMMAND_MASTER_ENABLE;
121 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG, command);
122 
123 	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_TEKRAM2_DC3X5U)
124 		return;
125 
126 	/*
127 	 * mask for get correct base address of pci IO port
128 	 */
129 	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
130 	    &iot, &ioh, NULL, NULL, 0) != 0) {
131 		printf("%s: unable to map registers\n", sc->sc_device.dv_xname);
132 		return;
133 	}
134 
135 	/*
136 	 * test checksum of eeprom & initial "ACB" adapter control block
137 	 */
138 	sc->sc_iotag    = iot;
139 	sc->sc_iohandle = ioh;
140 	sc->sc_dmatag   = pa->pa_dmat;
141 
142 	if (trm_init(sc, unit) != 0) {
143 		printf("%s: trm_init failed", sc->sc_device.dv_xname);
144 		return;
145 	}
146 
147 	/*
148 	 *  Map and establish interrupt
149 	 */
150 	if (pci_intr_map(pa, &ih)) {
151 		printf("%s: couldn't map interrupt\n", sc->sc_device.dv_xname);
152 		return;
153 	}
154 	intrstr = pci_intr_string(pa->pa_pc, ih);
155 
156 	if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, trm_Interrupt, sc,
157 	    sc->sc_device.dv_xname) == NULL) {
158 		printf("\n%s: couldn't establish interrupt", sc->sc_device.dv_xname);
159 		if (intrstr != NULL)
160 			printf(" at %s", intrstr);
161 		printf("\n");
162 	} else {
163 		if (intrstr != NULL)
164 			printf(": %s\n", intrstr);
165 
166 		/* Tell SCSI layer about our SCSI bus */
167 		config_found(&sc->sc_device, &sc->sc_link, scsiprint);
168 	}
169 }
170