xref: /netbsd-src/sys/dev/cardbus/ahc_cardbus.c (revision 2de962bd804263c16657f586aa00f1704045df8e)
1 /*	$NetBSD: ahc_cardbus.c,v 1.24 2008/04/28 20:23:47 martin 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.24 2008/04/28 20:23:47 martin 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 	int	sc_intrline;		/* our interrupt line */
81 	cardbustag_t sc_tag;
82 
83 	int	sc_cbenable;		/* what CardBus access type to enable */
84 	int	sc_csr;			/* CSR bits */
85 	bus_size_t sc_size;
86 };
87 
88 int	ahc_cardbus_match(struct device *, struct cfdata *, void *);
89 void	ahc_cardbus_attach(struct device *, struct device *, void *);
90 int	ahc_cardbus_detach(struct device *, int);
91 int	ahc_activate(struct device *self, enum devact act);
92 
93 CFATTACH_DECL(ahc_cardbus, sizeof(struct ahc_cardbus_softc),
94     ahc_cardbus_match, ahc_cardbus_attach, ahc_cardbus_detach, ahc_activate);
95 
96 int
97 ahc_cardbus_match(struct device *parent, struct cfdata *match,
98     void *aux)
99 {
100 	struct cardbus_attach_args *ca = aux;
101 
102 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_ADP &&
103 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_ADP_APA1480)
104 		return (1);
105 
106 	return (0);
107 }
108 
109 void
110 ahc_cardbus_attach(struct device *parent, struct device *self,
111     void *aux)
112 {
113 	struct cardbus_attach_args *ca = aux;
114 	struct ahc_cardbus_softc *csc = device_private(self);
115 	struct ahc_softc *ahc = &csc->sc_ahc;
116 	cardbus_devfunc_t ct = ca->ca_ct;
117 	cardbus_chipset_tag_t cc = ct->ct_cc;
118 	cardbus_function_tag_t cf = ct->ct_cf;
119 	bus_space_tag_t bst;
120 	bus_space_handle_t bsh;
121 	pcireg_t reg;
122 	u_int sxfrctl1 = 0;
123 	u_char sblkctl;
124 
125 
126 	csc->sc_ct = ct;
127 	csc->sc_tag = ca->ca_tag;
128 	csc->sc_intrline = ca->ca_intrline;
129 
130 	printf(": Adaptec ADP-1480 SCSI\n");
131 
132 	/*
133 	 * Map the device.
134 	 */
135 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
136 	if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_MMBA,
137 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
138 	    &bst, &bsh, NULL, &csc->sc_size) == 0) {
139 		csc->sc_cbenable = CARDBUS_MEM_ENABLE;
140 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
141 	} else if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_IOBA,
142 	    PCI_MAPREG_TYPE_IO, 0, &bst, &bsh, NULL, &csc->sc_size) == 0) {
143 		csc->sc_cbenable = CARDBUS_IO_ENABLE;
144 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
145 	} else {
146 		printf("%s: unable to map device registers\n",
147 		    ahc_name(ahc));
148 		return;
149 	}
150 
151 	/* Make sure the right access type is on the CardBus bridge. */
152 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
153 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
154 
155 	/* Enable the appropriate bits in the PCI CSR. */
156 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
157 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
158 	reg |= csc->sc_csr;
159 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
160 
161 	/*
162 	 * Make sure the latency timer is set to some reasonable
163 	 * value.
164 	 */
165 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG);
166 	if (PCI_LATTIMER(reg) < 0x20) {
167 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
168 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
169 		cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg);
170 	}
171 
172 	ahc_set_name(ahc, device_xname(&ahc->sc_dev));
173 
174 	ahc->parent_dmat = ca->ca_dmat;
175 	ahc->tag = bst;
176 	ahc->bsh = bsh;
177 
178 	/*
179 	 * ADP-1480 is always an AIC-7860.
180 	 */
181 	ahc->chip = AHC_AIC7860 | AHC_PCI;
182 	ahc->features = AHC_AIC7860_FE|AHC_REMOVABLE;
183 	ahc->bugs |= AHC_TMODE_WIDEODD_BUG|AHC_CACHETHEN_BUG|AHC_PCI_MWI_BUG;
184 	if (PCI_REVISION(ca->ca_class) >= 1)
185 		ahc->bugs |= AHC_PCI_2_1_RETRY_BUG;
186 
187 	if (ahc_softc_init(ahc) != 0)
188 		return;
189 
190 	/*
191 	 * On all CardBus adapters, we allow SCB paging.
192 	 */
193 	ahc->flags = AHC_PAGESCBS;
194 
195 	ahc->channel = 'A';
196 
197 	ahc_intr_enable(ahc, FALSE);
198 
199 	ahc_reset(ahc);
200 
201 	/*
202 	 * Establish the interrupt.
203 	 */
204 	ahc->ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
205 	    ahc_intr, ahc);
206 	if (ahc->ih == NULL) {
207 		printf("%s: unable to establish interrupt at %d\n",
208 		    ahc_name(ahc), ca->ca_intrline);
209 		return;
210 	}
211 	printf("%s: interrupting at %d\n", ahc_name(ahc), ca->ca_intrline);
212 
213 	ahc->seep_config = malloc(sizeof(*ahc->seep_config),
214 				  M_DEVBUF, M_NOWAIT);
215 	if (ahc->seep_config == NULL)
216 		return;
217 
218 	ahc_check_extport(ahc, &sxfrctl1);
219 	/*
220 	 * Take the LED out of diagnostic mode.
221 	 */
222 	sblkctl = ahc_inb(ahc, SBLKCTL);
223 	ahc_outb(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
224 
225 	/*
226 	 * I don't know where this is set in the SEEPROM or by the
227 	 * BIOS, so we default to 100%.
228 	 */
229 	ahc_outb(ahc, DSPCISTATUS, DFTHRSH_100);
230 
231 	if (ahc->flags & AHC_USEDEFAULTS) {
232 		int our_id;
233 		/*
234 		 * Assume only one connector and always turn
235 		 * on termination.
236 		 */
237 		our_id = AHC_CARDBUS_DEFAULT_SCSI_ID;
238 		sxfrctl1 = STPWEN;
239 		ahc_outb(ahc, SCSICONF, our_id | ENSPCHK | RESET_SCSI);
240 		ahc->our_id = our_id;
241 	}
242 
243 	printf("%s: aic7860", ahc_name(ahc));
244 
245 	/*
246 	 * Record our termination setting for the
247 	 * generic initialization routine.
248 	 */
249         if ((sxfrctl1 & STPWEN) != 0)
250 		ahc->flags |= AHC_TERM_ENB_A;
251 
252 	if (ahc_init(ahc)) {
253 		ahc_free(ahc);
254 		return;
255 	}
256 
257 	ahc_attach(ahc);
258 }
259 
260 int
261 ahc_cardbus_detach(self, flags)
262 	struct device *self;
263 	int flags;
264 {
265 	struct ahc_cardbus_softc *csc = device_private(self);
266 	struct ahc_softc *ahc = &csc->sc_ahc;
267 
268 	int rv;
269 
270 	rv = ahc_detach((void *)ahc, flags);
271 	if (rv)
272 		return rv;
273 
274 	if (ahc->ih) {
275 		cardbus_intr_disestablish(csc->sc_ct->ct_cc,
276 					  csc->sc_ct->ct_cf, ahc->ih);
277 		ahc->ih = 0;
278 	}
279 
280 	if (csc->sc_cbenable) {
281 		if (csc->sc_cbenable == CARDBUS_MEM_ENABLE)
282 			Cardbus_mapreg_unmap(csc->sc_ct, AHC_CARDBUS_MMBA,
283 				ahc->tag, ahc->bsh, csc->sc_size);
284 		else if (csc->sc_cbenable == CARDBUS_IO_ENABLE)
285 			Cardbus_mapreg_unmap(csc->sc_ct, AHC_CARDBUS_IOBA,
286 				ahc->tag, ahc->bsh, csc->sc_size);
287 	csc->sc_cbenable = 0;
288 	}
289 
290 	return (0);
291 }
292 
293 
294 int
295 ahc_activate(struct device *self, enum devact act)
296 {
297 	struct ahc_cardbus_softc *csc = (void*)self;
298 	struct ahc_softc *ahc = &csc->sc_ahc;
299 	int s, rv = 0;
300 
301 	s = splhigh();
302 	switch (act) {
303 	case DVACT_ACTIVATE:
304 		rv = EOPNOTSUPP;
305 		break;
306 
307 	case DVACT_DEACTIVATE:
308 		if (ahc->sc_child != NULL)
309 			rv = config_deactivate(ahc->sc_child);
310 		break;
311 	}
312 	splx(s);
313 
314 	return (rv);
315 }
316