xref: /netbsd-src/sys/dev/ic/aic77xx.c (revision 454af1c0e885cbba6b59706391f770d646303f8c)
1*454af1c0Sdsl /*	$NetBSD: aic77xx.c,v 1.8 2009/03/14 15:36:17 dsl Exp $	*/
2085f6883Sfvdl 
3085f6883Sfvdl /*
4085f6883Sfvdl  * Common routines for AHA-27/284X and aic7770 motherboard SCSI controllers.
5085f6883Sfvdl  *
6085f6883Sfvdl  * Copyright (c) 1994, 1995, 1996, 1997, 1998 Justin T. Gibbs.
7085f6883Sfvdl  * All rights reserved.
8085f6883Sfvdl  *
9085f6883Sfvdl  * Redistribution and use in source and binary forms, with or without
10085f6883Sfvdl  * modification, are permitted provided that the following conditions
11085f6883Sfvdl  * are met:
12085f6883Sfvdl  * 1. Redistributions of source code must retain the above copyright
13085f6883Sfvdl  *    notice immediately at the beginning of the file, without modification,
14085f6883Sfvdl  *    this list of conditions, and the following disclaimer.
15085f6883Sfvdl  * 2. The name of the author may not be used to endorse or promote products
16085f6883Sfvdl  *    derived from this software without specific prior written permission.
17085f6883Sfvdl  *
18085f6883Sfvdl  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19085f6883Sfvdl  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20085f6883Sfvdl  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21085f6883Sfvdl  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
22085f6883Sfvdl  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23085f6883Sfvdl  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24085f6883Sfvdl  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25085f6883Sfvdl  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26085f6883Sfvdl  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27085f6883Sfvdl  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28085f6883Sfvdl  * SUCH DAMAGE.
29085f6883Sfvdl  *
30085f6883Sfvdl  * $FreeBSD: src/sys/dev/aic7xxx/ahc_eisa.c,v 1.15 2000/01/29 14:22:19 peter Exp $
31085f6883Sfvdl  */
32085f6883Sfvdl 
33a4bae8b0Slukem #include <sys/cdefs.h>
34*454af1c0Sdsl __KERNEL_RCSID(0, "$NetBSD: aic77xx.c,v 1.8 2009/03/14 15:36:17 dsl Exp $");
35a4bae8b0Slukem 
36085f6883Sfvdl #include <sys/param.h>
37085f6883Sfvdl #include <sys/systm.h>
38085f6883Sfvdl #include <sys/device.h>
39085f6883Sfvdl 
40a2a38285Sad #include <sys/bus.h>
41a2a38285Sad #include <sys/intr.h>
42085f6883Sfvdl 
43085f6883Sfvdl #include <dev/scsipi/scsi_all.h>
44085f6883Sfvdl #include <dev/scsipi/scsipi_all.h>
45085f6883Sfvdl #include <dev/scsipi/scsiconf.h>
46085f6883Sfvdl 
47ccadc2cbSfvdl #include <dev/ic/aic7xxx_osm.h>
48ccadc2cbSfvdl #include <dev/ic/aic7xxx_inline.h>
49085f6883Sfvdl #include <dev/ic/aic77xxreg.h>
50085f6883Sfvdl #include <dev/ic/aic77xxvar.h>
51085f6883Sfvdl 
52085f6883Sfvdl /*
53085f6883Sfvdl  * Return irq setting of the board, otherwise -1.
54085f6883Sfvdl  */
55085f6883Sfvdl int
ahc_aic77xx_irq(bus_space_tag_t iot,bus_space_handle_t ioh)56*454af1c0Sdsl ahc_aic77xx_irq(bus_space_tag_t iot, bus_space_handle_t ioh)
57085f6883Sfvdl {
58085f6883Sfvdl 	int irq;
59085f6883Sfvdl 	u_int8_t intdef;
60085f6883Sfvdl 	u_int8_t hcntrl;
61085f6883Sfvdl 
62085f6883Sfvdl 	/* Pause the card preseving the IRQ type */
63085f6883Sfvdl 	hcntrl = bus_space_read_1(iot, ioh, HCNTRL) & IRQMS;
64085f6883Sfvdl 	bus_space_write_1(iot, ioh, HCNTRL, hcntrl | PAUSE);
65085f6883Sfvdl 
66085f6883Sfvdl 	intdef = bus_space_read_1(iot, ioh, INTDEF);
67085f6883Sfvdl 	irq = (intdef & INTDEF_IRQ_MASK);
68085f6883Sfvdl 	switch (irq) {
69085f6883Sfvdl 	case 9:
70085f6883Sfvdl 	case 10:
71085f6883Sfvdl 	case 11:
72085f6883Sfvdl 	case 12:
73085f6883Sfvdl 	case 14:
74085f6883Sfvdl 	case 15:
75085f6883Sfvdl 		break;
76085f6883Sfvdl 	default:
77085f6883Sfvdl 		printf("ahc_aic77xx_irq: illegal irq setting %d\n", intdef);
78085f6883Sfvdl 		return (-1);
79085f6883Sfvdl 	}
80085f6883Sfvdl 
81085f6883Sfvdl 	return (irq);
82085f6883Sfvdl }
83085f6883Sfvdl 
84085f6883Sfvdl int
ahc_aic77xx_attach(struct ahc_softc * ahc)85*454af1c0Sdsl ahc_aic77xx_attach(struct ahc_softc *ahc)
86085f6883Sfvdl {
87085f6883Sfvdl 	u_int8_t sblkctl;
88085f6883Sfvdl 	u_int8_t sblkctl_orig;
89085f6883Sfvdl 	u_int8_t hostconf;
90085f6883Sfvdl 
91085f6883Sfvdl 	/*
92085f6883Sfvdl 	 * See if we have a Rev E or higher aic7770. Anything below a
93085f6883Sfvdl 	 * Rev E will have a R/O autoflush disable configuration bit.
94085f6883Sfvdl 	 */
95085f6883Sfvdl 	sblkctl_orig = ahc_inb(ahc, SBLKCTL);
96085f6883Sfvdl 	sblkctl = sblkctl_orig ^ AUTOFLUSHDIS;
97085f6883Sfvdl 	ahc_outb(ahc, SBLKCTL, sblkctl);
98085f6883Sfvdl 	sblkctl = ahc_inb(ahc, SBLKCTL);
99085f6883Sfvdl 	if (sblkctl != sblkctl_orig) {
100fd5c6329Stsutsui 		printf("%s: aic7770 >= Rev E: R/O autoflush enabled\n",
101fd5c6329Stsutsui 		    ahc_name(ahc));
102085f6883Sfvdl 		/*
103085f6883Sfvdl 		 * Ensure autoflush is enabled
104085f6883Sfvdl 		 */
105085f6883Sfvdl 		sblkctl &= ~AUTOFLUSHDIS;
106085f6883Sfvdl 		ahc_outb(ahc, SBLKCTL, sblkctl);
107fd5c6329Stsutsui 	}
108085f6883Sfvdl 
109085f6883Sfvdl 	/* Setup the FIFO threshold and the bus off time */
110085f6883Sfvdl 	hostconf = ahc_inb(ahc, HOSTCONF);
111085f6883Sfvdl 	ahc_outb(ahc, BUSSPD, hostconf & DFTHRSH);
112085f6883Sfvdl 	ahc_outb(ahc, BUSTIME, (hostconf << 2) & BOFF);
113085f6883Sfvdl 
114085f6883Sfvdl 	/*
115085f6883Sfvdl 	 * Generic aic7xxx initialization.
116085f6883Sfvdl 	 */
117085f6883Sfvdl 	if (ahc_init(ahc)) {
118085f6883Sfvdl 		/*
119085f6883Sfvdl 		 * The board's IRQ line is not yet enabled so it's safe
120085f6883Sfvdl 		 * to release the irq.
121085f6883Sfvdl 		 */
122085f6883Sfvdl 		return (ENXIO);
123085f6883Sfvdl 	}
124085f6883Sfvdl 
125085f6883Sfvdl 	/*
126085f6883Sfvdl 	 * Enable the board's BUS drivers
127085f6883Sfvdl 	 */
128085f6883Sfvdl 	ahc_outb(ahc, BCTL, ENABLE);
129085f6883Sfvdl 
130085f6883Sfvdl 	/* Attach sub-devices - always succeeds */
131085f6883Sfvdl 	ahc_attach(ahc);
132085f6883Sfvdl 
133085f6883Sfvdl 	return 0;
134085f6883Sfvdl }
135