1 /* $NetBSD: ahc_cardbus.c,v 1.38 2019/11/10 21:16:34 chs Exp $ */
2
3 /*-
4 * Copyright (c) 2000, 2005 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 * NASA Ames Research Center.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * CardBus front-end for the Adaptec AIC-7xxx family of SCSI controllers.
35 *
36 * TODO:
37 * - power management
38 */
39
40 #include <sys/cdefs.h>
41 __KERNEL_RCSID(0, "$NetBSD: ahc_cardbus.c,v 1.38 2019/11/10 21:16:34 chs Exp $");
42
43 #include "opt_ahc_cardbus.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/malloc.h>
48 #include <sys/kernel.h>
49 #include <sys/queue.h>
50 #include <sys/device.h>
51
52 #include <sys/bus.h>
53 #include <sys/intr.h>
54
55 #include <dev/scsipi/scsi_all.h>
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsiconf.h>
58
59 #include <dev/pci/pcireg.h>
60
61 #include <dev/cardbus/cardbusvar.h>
62 #include <dev/pci/pcidevs.h>
63
64 #include <dev/ic/aic7xxx_osm.h>
65 #include <dev/ic/aic7xxx_inline.h>
66
67
68 #ifndef AHC_CARDBUS_DEFAULT_SCSI_ID
69 #define AHC_CARDBUS_DEFAULT_SCSI_ID 0x7
70 #endif
71
72 #define AHC_CARDBUS_IOBA 0x10
73 #define AHC_CARDBUS_MMBA 0x14
74
75 struct ahc_cardbus_softc {
76 struct ahc_softc sc_ahc; /* real AHC */
77
78 /* CardBus-specific goo. */
79 cardbus_devfunc_t sc_ct; /* our CardBus devfuncs */
80 pcitag_t sc_tag;
81
82 int sc_bar;
83 pcireg_t sc_csr;
84 bus_size_t sc_size;
85 };
86
87 int ahc_cardbus_match(device_t, cfdata_t, void *);
88 void ahc_cardbus_attach(device_t, device_t, void *);
89 int ahc_cardbus_detach(device_t, int);
90
91 CFATTACH_DECL_NEW(ahc_cardbus, sizeof(struct ahc_cardbus_softc),
92 ahc_cardbus_match, ahc_cardbus_attach, ahc_cardbus_detach, NULL);
93
94 int
ahc_cardbus_match(device_t parent,cfdata_t match,void * aux)95 ahc_cardbus_match(device_t parent, cfdata_t match, void *aux)
96 {
97 struct cardbus_attach_args *ca = aux;
98
99 if (PCI_VENDOR(ca->ca_id) == PCI_VENDOR_ADP &&
100 PCI_PRODUCT(ca->ca_id) == PCI_PRODUCT_ADP_APA1480)
101 return (1);
102
103 return (0);
104 }
105
106 void
ahc_cardbus_attach(device_t parent,device_t self,void * aux)107 ahc_cardbus_attach(device_t parent, device_t self, void *aux)
108 {
109 struct cardbus_attach_args *ca = aux;
110 struct ahc_cardbus_softc *csc = device_private(self);
111 struct ahc_softc *ahc = &csc->sc_ahc;
112 cardbus_devfunc_t ct = ca->ca_ct;
113 bus_space_tag_t bst;
114 bus_space_handle_t bsh;
115 pcireg_t reg;
116 u_int sxfrctl1 = 0;
117 u_char sblkctl;
118
119
120 ahc->sc_dev = self;
121 csc->sc_ct = ct;
122 csc->sc_tag = ca->ca_tag;
123
124 printf(": Adaptec ADP-1480 SCSI\n");
125
126 /*
127 * Map the device.
128 */
129 csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
130 if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_MMBA,
131 PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
132 &bst, &bsh, NULL, &csc->sc_size) == 0) {
133 csc->sc_bar = AHC_CARDBUS_MMBA;
134 csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
135 } else if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_IOBA,
136 PCI_MAPREG_TYPE_IO, 0, &bst, &bsh, NULL, &csc->sc_size) == 0) {
137 csc->sc_bar = AHC_CARDBUS_IOBA;
138 csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
139 } else {
140 csc->sc_bar = 0;
141 aprint_error("%s: unable to map device registers\n",
142 ahc_name(ahc));
143 return;
144 }
145
146 /* Enable the appropriate bits in the PCI CSR. */
147 reg = Cardbus_conf_read(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG);
148 reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
149 reg |= csc->sc_csr;
150 Cardbus_conf_write(ct, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
151
152 /*
153 * Make sure the latency timer is set to some reasonable
154 * value.
155 */
156 reg = Cardbus_conf_read(ct, ca->ca_tag, PCI_BHLC_REG);
157 if (PCI_LATTIMER(reg) < 0x20) {
158 reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
159 reg |= (0x20 << PCI_LATTIMER_SHIFT);
160 Cardbus_conf_write(ct, ca->ca_tag, PCI_BHLC_REG, reg);
161 }
162
163 ahc_set_name(ahc, device_xname(ahc->sc_dev));
164
165 ahc->parent_dmat = ca->ca_dmat;
166 ahc->tag = bst;
167 ahc->bsh = bsh;
168
169 /*
170 * ADP-1480 is always an AIC-7860.
171 */
172 ahc->chip = AHC_AIC7860 | AHC_PCI;
173 ahc->features = AHC_AIC7860_FE|AHC_REMOVABLE;
174 ahc->bugs |= AHC_TMODE_WIDEODD_BUG|AHC_CACHETHEN_BUG|AHC_PCI_MWI_BUG;
175 if (PCI_REVISION(ca->ca_class) >= 1)
176 ahc->bugs |= AHC_PCI_2_1_RETRY_BUG;
177
178 if (ahc_softc_init(ahc) != 0)
179 return;
180
181 /*
182 * On all CardBus adapters, we allow SCB paging.
183 */
184 ahc->flags = AHC_PAGESCBS;
185
186 ahc->channel = 'A';
187
188 ahc_intr_enable(ahc, FALSE);
189
190 ahc_reset(ahc);
191
192 /*
193 * Establish the interrupt.
194 */
195 ahc->ih = Cardbus_intr_establish(ct, IPL_BIO, ahc_intr, ahc);
196 if (ahc->ih == NULL) {
197 aprint_error("%s: unable to establish interrupt\n",
198 ahc_name(ahc));
199 return;
200 }
201
202 ahc->seep_config = malloc(sizeof(*ahc->seep_config),
203 M_DEVBUF, M_WAITOK);
204 ahc_check_extport(ahc, &sxfrctl1);
205 /*
206 * Take the LED out of diagnostic mode.
207 */
208 sblkctl = ahc_inb(ahc, SBLKCTL);
209 ahc_outb(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
210
211 /*
212 * I don't know where this is set in the SEEPROM or by the
213 * BIOS, so we default to 100%.
214 */
215 ahc_outb(ahc, DSPCISTATUS, DFTHRSH_100);
216
217 if (ahc->flags & AHC_USEDEFAULTS) {
218 int our_id;
219 /*
220 * Assume only one connector and always turn
221 * on termination.
222 */
223 our_id = AHC_CARDBUS_DEFAULT_SCSI_ID;
224 sxfrctl1 = STPWEN;
225 ahc_outb(ahc, SCSICONF, our_id | ENSPCHK | RESET_SCSI);
226 ahc->our_id = our_id;
227 }
228
229 /*
230 * Record our termination setting for the
231 * generic initialization routine.
232 */
233 if ((sxfrctl1 & STPWEN) != 0)
234 ahc->flags |= AHC_TERM_ENB_A;
235
236 if (ahc_init(ahc)) {
237 ahc_free(ahc);
238 return;
239 }
240
241 ahc_attach(ahc);
242 }
243
244 int
ahc_cardbus_detach(device_t self,int flags)245 ahc_cardbus_detach(device_t self, int flags)
246 {
247 struct ahc_cardbus_softc *csc = device_private(self);
248 struct ahc_softc *ahc = &csc->sc_ahc;
249
250 int rv;
251
252 rv = ahc_detach(ahc, flags);
253 if (rv)
254 return rv;
255
256 if (ahc->ih) {
257 Cardbus_intr_disestablish(csc->sc_ct, ahc->ih);
258 ahc->ih = 0;
259 }
260
261 if (csc->sc_bar != 0) {
262 Cardbus_mapreg_unmap(csc->sc_ct, csc->sc_bar,
263 ahc->tag, ahc->bsh, csc->sc_size);
264 csc->sc_bar = 0;
265 }
266
267 return (0);
268 }
269