1/*	$OpenBSD: trm_pci.c,v 1.12 2022/04/16 19:19:59 naddy 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
57int	trm_pci_probe (struct device *, void *, void *);
58void	trm_pci_attach (struct device *, struct device *, void *);
59
60const 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};
67
68struct  cfdriver trm_cd = {
69        NULL, "trm", DV_DULL
70};
71
72const struct scsi_adapter trm_switch = {
73	trm_scsi_cmd, NULL, NULL, NULL, NULL
74};
75
76
77/*
78 * ------------------------------------------------------------
79 * Function : trm_pci_probe
80 * Purpose  : Check the slots looking for a board we recognize.
81 *            If we find one, note ti's address (slot) and call
82 *            the actual probe routine to check it out.
83 * Inputs   :
84 * ------------------------------------------------------------
85 */
86int
87trm_pci_probe(struct device *parent, void *match, void *aux)
88{
89	struct pci_attach_args *pa = aux;
90
91	if (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_TEKRAM2) {
92		switch (PCI_PRODUCT(pa->pa_id)) {
93		case PCI_PRODUCT_TEKRAM2_DC3X5U:
94			return 1;
95		}
96	}
97	return 0;
98}
99
100/*
101 * ------------------------------------------------------------
102 * Function : trm_pci_attach
103 * Purpose  :
104 * Inputs   :
105 * ------------------------------------------------------------
106 */
107void
108trm_pci_attach(struct device *parent, struct device *self, void *aux)
109{
110	struct pci_attach_args *pa = aux;
111	struct scsibus_attach_args saa;
112	bus_space_handle_t ioh;/* bus space handle */
113	pci_intr_handle_t ih;
114	struct trm_softc *sc = (void *)self;
115	bus_space_tag_t iot;   /* bus space tag    */
116	const char *intrstr;
117	int unit;
118
119	unit = sc->sc_device.dv_unit;
120
121	if (PCI_PRODUCT(pa->pa_id) != PCI_PRODUCT_TEKRAM2_DC3X5U)
122		return;
123
124	/*
125	 * mask for get correct base address of pci IO port
126	 */
127	if (pci_mapreg_map(pa, PCI_MAPREG_START, PCI_MAPREG_TYPE_IO, 0,
128	    &iot, &ioh, NULL, NULL, 0) != 0) {
129		printf("%s: unable to map registers\n", sc->sc_device.dv_xname);
130		return;
131	}
132
133	/*
134	 * test checksum of eeprom & initial "ACB" adapter control block
135	 */
136	sc->sc_iotag    = iot;
137	sc->sc_iohandle = ioh;
138	sc->sc_dmatag   = pa->pa_dmat;
139
140	if (trm_init(sc, unit) != 0) {
141		printf("%s: trm_init failed", sc->sc_device.dv_xname);
142		return;
143	}
144
145	/*
146	 *  Map and establish interrupt
147	 */
148	if (pci_intr_map(pa, &ih)) {
149		printf("%s: couldn't map interrupt\n", sc->sc_device.dv_xname);
150		return;
151	}
152	intrstr = pci_intr_string(pa->pa_pc, ih);
153
154	if (pci_intr_establish(pa->pa_pc, ih, IPL_BIO, trm_Interrupt, sc,
155	    sc->sc_device.dv_xname) == NULL) {
156		printf("\n%s: couldn't establish interrupt", sc->sc_device.dv_xname);
157		if (intrstr != NULL)
158			printf(" at %s", intrstr);
159		printf("\n");
160	} else {
161		if (intrstr != NULL)
162			printf(": %s\n", intrstr);
163
164		saa.saa_adapter_softc    = sc;
165		saa.saa_adapter_target   = sc->sc_AdaptSCSIID;
166		saa.saa_adapter          = &trm_switch;
167		saa.saa_adapter_buswidth = ((sc->sc_config & HCC_WIDE_CARD) == 0) ? 8:16;
168		saa.saa_luns		 = 8;
169		saa.saa_openings         = 30; /* So TagMask (32 bit integer) always has space */
170		saa.saa_pool		 = &sc->sc_iopool;
171		saa.saa_quirks = saa.saa_flags = 0;
172		saa.saa_wwpn = saa.saa_wwnn = 0;
173
174		config_found(&sc->sc_device, &saa, scsiprint);
175	}
176}
177