xref: /netbsd-src/sys/dev/cardbus/ahc_cardbus.c (revision aa73cae19608873cc4d1f712c4a0f8f8435f1ffa)
1 /*	$NetBSD: ahc_cardbus.c,v 1.16 2005/02/27 00:26:59 perry Exp $	*/
2 
3 /*-
4  * Copyright (c) 2000 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  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*
41  * CardBus front-end for the Adaptec AIC-7xxx family of SCSI controllers.
42  *
43  * TODO:
44  *	- power management
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: ahc_cardbus.c,v 1.16 2005/02/27 00:26:59 perry Exp $");
49 
50 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/malloc.h>
53 #include <sys/kernel.h>
54 #include <sys/queue.h>
55 #include <sys/device.h>
56 
57 #include <machine/bus.h>
58 #include <machine/intr.h>
59 
60 #include <dev/scsipi/scsi_all.h>
61 #include <dev/scsipi/scsipi_all.h>
62 #include <dev/scsipi/scsiconf.h>
63 
64 #include <dev/pci/pcireg.h>
65 
66 #include <dev/cardbus/cardbusvar.h>
67 #include <dev/pci/pcidevs.h>
68 
69 #include <dev/ic/aic7xxx_osm.h>
70 #include <dev/ic/aic7xxx_inline.h>
71 
72 
73 #define	AHC_CARDBUS_IOBA	0x10
74 #define	AHC_CARDBUS_MMBA	0x14
75 
76 struct ahc_cardbus_softc {
77 	struct ahc_softc sc_ahc;	/* real AHC */
78 
79 	/* CardBus-specific goo. */
80 	cardbus_devfunc_t sc_ct;	/* our CardBus devfuncs */
81 	int	sc_intrline;		/* our interrupt line */
82 	cardbustag_t sc_tag;
83 
84 	int	sc_cbenable;		/* what CardBus access type to enable */
85 	int	sc_csr;			/* CSR bits */
86 	bus_size_t sc_size;
87 };
88 
89 int	ahc_cardbus_match(struct device *, struct cfdata *, void *);
90 void	ahc_cardbus_attach(struct device *, struct device *, void *);
91 int	ahc_cardbus_detach(struct device *, int);
92 int	ahc_activate(struct device *self, enum devact act);
93 
94 CFATTACH_DECL(ahc_cardbus, sizeof(struct ahc_cardbus_softc),
95     ahc_cardbus_match, ahc_cardbus_attach, ahc_cardbus_detach, ahc_activate);
96 
97 int
98 ahc_cardbus_match(parent, match, aux)
99 	struct device *parent;
100 	struct cfdata *match;
101 	void *aux;
102 {
103 	struct cardbus_attach_args *ca = aux;
104 
105 	if (CARDBUS_VENDOR(ca->ca_id) == PCI_VENDOR_ADP &&
106 	    CARDBUS_PRODUCT(ca->ca_id) == PCI_PRODUCT_ADP_APA1480)
107 		return (1);
108 
109 	return (0);
110 }
111 
112 void
113 ahc_cardbus_attach(parent, self, aux)
114 	struct device *parent, *self;
115 	void *aux;
116 {
117 	struct cardbus_attach_args *ca = aux;
118 	struct ahc_cardbus_softc *csc = (void *) self;
119 	struct ahc_softc *ahc = &csc->sc_ahc;
120 	cardbus_devfunc_t ct = ca->ca_ct;
121 	cardbus_chipset_tag_t cc = ct->ct_cc;
122 	cardbus_function_tag_t cf = ct->ct_cf;
123 	bus_space_tag_t bst;
124 	bus_space_handle_t bsh;
125 	pcireg_t reg;
126 	u_int sxfrctl1 = 0;
127 	u_char sblkctl;
128 
129 
130 	csc->sc_ct = ct;
131 	csc->sc_tag = ca->ca_tag;
132 	csc->sc_intrline = ca->ca_intrline;
133 
134 	printf(": Adaptec ADP-1480 SCSI\n");
135 
136 	/*
137 	 * Map the device.
138 	 */
139 	csc->sc_csr = PCI_COMMAND_MASTER_ENABLE;
140 	if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_MMBA,
141 	    PCI_MAPREG_TYPE_MEM|PCI_MAPREG_MEM_TYPE_32BIT, 0,
142 	    &bst, &bsh, NULL, &csc->sc_size) == 0) {
143 		csc->sc_cbenable = CARDBUS_MEM_ENABLE;
144 		csc->sc_csr |= PCI_COMMAND_MEM_ENABLE;
145 	} else if (Cardbus_mapreg_map(csc->sc_ct, AHC_CARDBUS_IOBA,
146 	    PCI_MAPREG_TYPE_IO, 0, &bst, &bsh, NULL, &csc->sc_size) == 0) {
147 		csc->sc_cbenable = CARDBUS_IO_ENABLE;
148 		csc->sc_csr |= PCI_COMMAND_IO_ENABLE;
149 	} else {
150 		printf("%s: unable to map device registers\n",
151 		    ahc_name(ahc));
152 		return;
153 	}
154 
155 	/* Make sure the right access type is on the CardBus bridge. */
156 	(*ct->ct_cf->cardbus_ctrl)(cc, csc->sc_cbenable);
157 	(*ct->ct_cf->cardbus_ctrl)(cc, CARDBUS_BM_ENABLE);
158 
159 	/* Enable the appropriate bits in the PCI CSR. */
160 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG);
161 	reg &= ~(PCI_COMMAND_IO_ENABLE|PCI_COMMAND_MEM_ENABLE);
162 	reg |= csc->sc_csr;
163 	cardbus_conf_write(cc, cf, ca->ca_tag, PCI_COMMAND_STATUS_REG, reg);
164 
165 	/*
166 	 * Make sure the latency timer is set to some reasonable
167 	 * value.
168 	 */
169 	reg = cardbus_conf_read(cc, cf, ca->ca_tag, PCI_BHLC_REG);
170 	if (PCI_LATTIMER(reg) < 0x20) {
171 		reg &= ~(PCI_LATTIMER_MASK << PCI_LATTIMER_SHIFT);
172 		reg |= (0x20 << PCI_LATTIMER_SHIFT);
173 		cardbus_conf_write(cc, cf, ca->ca_tag, PCI_BHLC_REG, reg);
174 	}
175 
176 	ahc_set_name(ahc, ahc->sc_dev.dv_xname);
177 
178 	ahc->parent_dmat = ca->ca_dmat;
179 	ahc->tag = bst;
180 	ahc->bsh = bsh;
181 
182 	/*
183 	 * ADP-1480 is always an AIC-7860.
184 	 */
185 	ahc->chip = AHC_AIC7860 | AHC_PCI;
186 	ahc->features = AHC_AIC7860_FE|AHC_REMOVABLE;
187 	ahc->bugs |= AHC_TMODE_WIDEODD_BUG|AHC_CACHETHEN_BUG|AHC_PCI_MWI_BUG;
188 	if (PCI_REVISION(ca->ca_class) >= 1)
189 		ahc->bugs |= AHC_PCI_2_1_RETRY_BUG;
190 
191 	if (ahc_softc_init(ahc) != 0)
192 		return;
193 
194 	/*
195 	 * On all CardBus adapters, we allow SCB paging.
196 	 */
197 	ahc->flags = AHC_PAGESCBS;
198 
199 	ahc->channel = 'A';
200 
201 	ahc_intr_enable(ahc, FALSE);
202 
203 	ahc_reset(ahc);
204 
205 	/*
206 	 * Establish the interrupt.
207 	 */
208 	ahc->ih = cardbus_intr_establish(cc, cf, ca->ca_intrline, IPL_BIO,
209 	    ahc_intr, ahc);
210 	if (ahc->ih == NULL) {
211 		printf("%s: unable to establish interrupt at %d\n",
212 		    ahc_name(ahc), ca->ca_intrline);
213 		return;
214 	}
215 	printf("%s: interrupting at %d\n", ahc_name(ahc), ca->ca_intrline);
216 
217 	ahc->seep_config = malloc(sizeof(*ahc->seep_config),
218 				  M_DEVBUF, M_NOWAIT);
219 	if (ahc->seep_config == NULL)
220 		return;
221 
222 	ahc_check_extport(ahc, &sxfrctl1);
223 	/*
224 	 * Take the LED out of diagnostic mode.
225 	 */
226 	sblkctl = ahc_inb(ahc, SBLKCTL);
227 	ahc_outb(ahc, SBLKCTL, (sblkctl & ~(DIAGLEDEN|DIAGLEDON)));
228 
229 	/*
230 	 * I don't know where this is set in the SEEPROM or by the
231 	 * BIOS, so we default to 100%.
232 	 */
233 	ahc_outb(ahc, DSPCISTATUS, DFTHRSH_100);
234 
235 	if (ahc->flags & AHC_USEDEFAULTS) {
236 		/*
237 		 * We can't "use defaults", as we have no way
238 		 * of knowing what default settings hould be.
239 		 */
240 		printf("%s: CardBus device requires an SEEPROM\n",
241 		    ahc_name(ahc));
242 		return;
243 	}
244 
245 	printf("%s: aic7860", ahc_name(ahc));
246 
247 	/*
248 	 * Record our termination setting for the
249 	 * generic initialization routine.
250 	 */
251         if ((sxfrctl1 & STPWEN) != 0)
252 		ahc->flags |= AHC_TERM_ENB_A;
253 
254 	if (ahc_init(ahc)) {
255 		ahc_free(ahc);
256 		return;
257 	}
258 
259 	ahc_attach(ahc);
260 }
261 
262 int
263 ahc_cardbus_detach(self, flags)
264 	struct device *self;
265 	int flags;
266 {
267 	struct ahc_cardbus_softc *csc = (void*)self;
268 	struct ahc_softc *ahc = &csc->sc_ahc;
269 
270 	int rv;
271 
272 	rv = ahc_detach((void *)ahc, flags);
273 	if (rv)
274 		return rv;
275 
276 	if (ahc->ih) {
277 		cardbus_intr_disestablish(csc->sc_ct->ct_cc,
278 					  csc->sc_ct->ct_cf, ahc->ih);
279 		ahc->ih = 0;
280 	}
281 
282 	if (csc->sc_cbenable) {
283 		if (csc->sc_cbenable == CARDBUS_MEM_ENABLE)
284 			Cardbus_mapreg_unmap(csc->sc_ct, AHC_CARDBUS_MMBA,
285 				ahc->tag, ahc->bsh, csc->sc_size);
286 		else if (csc->sc_cbenable == CARDBUS_IO_ENABLE)
287 			Cardbus_mapreg_unmap(csc->sc_ct, AHC_CARDBUS_IOBA,
288 				ahc->tag, ahc->bsh, csc->sc_size);
289 	csc->sc_cbenable = 0;
290 	}
291 
292 	return (0);
293 }
294 
295 
296 int
297 ahc_activate(struct device *self, enum devact act)
298 {
299 	struct ahc_cardbus_softc *csc = (void*)self;
300 	struct ahc_softc *ahc = &csc->sc_ahc;
301 	int s, rv = 0;
302 
303 	s = splhigh();
304 	switch (act) {
305 	case DVACT_ACTIVATE:
306 		rv = EOPNOTSUPP;
307 		break;
308 
309 	case DVACT_DEACTIVATE:
310 		if (ahc->sc_child != NULL)
311 			rv = config_deactivate(ahc->sc_child);
312 		break;
313 	}
314 	splx(s);
315 
316 	return (rv);
317 }
318