xref: /netbsd-src/sys/dev/isa/wdc_isa.c (revision 9fbd88883c38d0c0fbfcbe66d76fe6b0fab3f9de)
1 /*	$NetBSD: wdc_isa.c,v 1.24 2002/01/07 21:47:13 thorpej Exp $ */
2 
3 /*-
4  * Copyright (c) 1998 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Onno van der Linden.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *        This product includes software developed by the NetBSD
21  *        Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: wdc_isa.c,v 1.24 2002/01/07 21:47:13 thorpej Exp $");
41 
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/device.h>
45 #include <sys/malloc.h>
46 
47 #include <machine/bus.h>
48 #include <machine/intr.h>
49 
50 #include <dev/isa/isavar.h>
51 #include <dev/isa/isadmavar.h>
52 
53 #include <dev/ata/atavar.h>
54 #include <dev/ic/wdcvar.h>
55 
56 #define	WDC_ISA_REG_NPORTS	8
57 #define	WDC_ISA_AUXREG_OFFSET	0x206
58 #define	WDC_ISA_AUXREG_NPORTS	1 /* XXX "fdc" owns ports 0x3f7/0x377 */
59 
60 /* options passed via the 'flags' config keyword */
61 #define WDC_OPTIONS_32			0x01 /* try to use 32bit data I/O */
62 #define WDC_OPTIONS_SINGLE_DRIVE	0x02 /* Don't probe second drive */
63 #define WDC_OPTIONS_ATA_NOSTREAM	0x04
64 #define WDC_OPTIONS_ATAPI_NOSTREAM	0x08
65 
66 struct wdc_isa_softc {
67 	struct	wdc_softc sc_wdcdev;
68 	struct	channel_softc *wdc_chanptr;
69 	struct	channel_softc wdc_channel;
70 	isa_chipset_tag_t sc_ic;
71 	void	*sc_ih;
72 	int	sc_drq;
73 };
74 
75 int	wdc_isa_probe	__P((struct device *, struct cfdata *, void *));
76 void	wdc_isa_attach	__P((struct device *, struct device *, void *));
77 
78 struct cfattach wdc_isa_ca = {
79 	sizeof(struct wdc_isa_softc), wdc_isa_probe, wdc_isa_attach
80 };
81 
82 static void	wdc_isa_dma_setup __P((struct wdc_isa_softc *));
83 static int	wdc_isa_dma_init __P((void*, int, int, void *, size_t, int));
84 static void 	wdc_isa_dma_start __P((void*, int, int));
85 static int	wdc_isa_dma_finish __P((void*, int, int, int));
86 
87 int
88 wdc_isa_probe(parent, match, aux)
89 	struct device *parent;
90 	struct cfdata *match;
91 	void *aux;
92 {
93 	struct channel_softc ch;
94 	struct isa_attach_args *ia = aux;
95 	int result = 0;
96 
97 	if (ia->ia_nio < 1)
98 		return (0);
99 	if (ia->ia_nirq < 1)
100 		return (0);
101 
102 	if (ISA_DIRECT_CONFIG(ia))
103 		return (0);
104 
105 	if (ia->ia_io[0].ir_addr == ISACF_PORT_DEFAULT)
106 		return (0);
107 	if (ia->ia_irq[0].ir_irq == ISACF_IRQ_DEFAULT)
108 		return (0);
109 
110 	memset(&ch, 0, sizeof(ch));
111 
112 	ch.cmd_iot = ia->ia_iot;
113 
114 	if (bus_space_map(ch.cmd_iot, ia->ia_io[0].ir_addr,
115 	    WDC_ISA_REG_NPORTS, 0, &ch.cmd_ioh))
116 		goto out;
117 
118 	ch.ctl_iot = ia->ia_iot;
119 	if (bus_space_map(ch.ctl_iot, ia->ia_io[0].ir_addr +
120 	    WDC_ISA_AUXREG_OFFSET, WDC_ISA_AUXREG_NPORTS, 0, &ch.ctl_ioh))
121 		goto outunmap;
122 
123 	result = wdcprobe(&ch);
124 	if (result) {
125 		ia->ia_nio = 1;
126 		ia->ia_io[0].ir_size = WDC_ISA_REG_NPORTS;
127 
128 		ia->ia_nirq = 1;
129 
130 		ia->ia_niomem = 0;
131 	}
132 
133 	bus_space_unmap(ch.ctl_iot, ch.ctl_ioh, WDC_ISA_AUXREG_NPORTS);
134 outunmap:
135 	bus_space_unmap(ch.cmd_iot, ch.cmd_ioh, WDC_ISA_REG_NPORTS);
136 out:
137 	return (result);
138 }
139 
140 void
141 wdc_isa_attach(parent, self, aux)
142 	struct device *parent, *self;
143 	void *aux;
144 {
145 	struct wdc_isa_softc *sc = (void *)self;
146 	struct isa_attach_args *ia = aux;
147 
148 	printf("\n");
149 
150 	sc->wdc_channel.cmd_iot = ia->ia_iot;
151 	sc->wdc_channel.ctl_iot = ia->ia_iot;
152 	sc->sc_ic = ia->ia_ic;
153 	if (bus_space_map(sc->wdc_channel.cmd_iot, ia->ia_io[0].ir_addr,
154 	    WDC_ISA_REG_NPORTS, 0, &sc->wdc_channel.cmd_ioh) ||
155 	    bus_space_map(sc->wdc_channel.ctl_iot,
156 	      ia->ia_io[0].ir_addr + WDC_ISA_AUXREG_OFFSET,
157 	      WDC_ISA_AUXREG_NPORTS, 0, &sc->wdc_channel.ctl_ioh)) {
158 		printf("%s: couldn't map registers\n",
159 		    sc->sc_wdcdev.sc_dev.dv_xname);
160 	}
161 	sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
162 	sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
163 
164 	sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq[0].ir_irq,
165 	    IST_EDGE, IPL_BIO, wdcintr, &sc->wdc_channel);
166 
167 	if (ia->ia_ndrq > 0 && ia->ia_drq[0].ir_drq != ISACF_DRQ_DEFAULT) {
168 		sc->sc_drq = ia->ia_drq[0].ir_drq;
169 
170 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
171 		sc->sc_wdcdev.dma_arg = sc;
172 		sc->sc_wdcdev.dma_init = wdc_isa_dma_init;
173 		sc->sc_wdcdev.dma_start = wdc_isa_dma_start;
174 		sc->sc_wdcdev.dma_finish = wdc_isa_dma_finish;
175 		wdc_isa_dma_setup(sc);
176 	}
177 	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_PREATA;
178 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_32)
179 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
180 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_SINGLE_DRIVE)
181 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
182 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_ATA_NOSTREAM)
183 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATA_NOSTREAM;
184 	if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags
185 						& WDC_OPTIONS_ATAPI_NOSTREAM)
186 		sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATAPI_NOSTREAM;
187 	sc->sc_wdcdev.PIO_cap = 0;
188 	sc->wdc_chanptr = &sc->wdc_channel;
189 	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
190 	sc->sc_wdcdev.nchannels = 1;
191 	sc->wdc_channel.channel = 0;
192 	sc->wdc_channel.wdc = &sc->sc_wdcdev;
193 	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
194 	    M_DEVBUF, M_NOWAIT);
195 	if (sc->wdc_channel.ch_queue == NULL) {
196 		printf("%s: can't allocate memory for command queue",
197 		sc->sc_wdcdev.sc_dev.dv_xname);
198 		return;
199 	}
200 	wdcattach(&sc->wdc_channel);
201 }
202 
203 static void
204 wdc_isa_dma_setup(sc)
205 	struct wdc_isa_softc *sc;
206 {
207 	bus_size_t maxsize;
208 
209 	if ((maxsize = isa_dmamaxsize(sc->sc_ic, sc->sc_drq)) < MAXPHYS) {
210 		printf("%s: max DMA size %lu is less than required %d\n",
211 		    sc->sc_wdcdev.sc_dev.dv_xname, (u_long)maxsize, MAXPHYS);
212 		sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
213 		return;
214 	}
215 
216 	if (isa_dmamap_create(sc->sc_ic, sc->sc_drq,
217 	    MAXPHYS, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
218 		printf("%s: can't create map for drq %d\n",
219 		    sc->sc_wdcdev.sc_dev.dv_xname, sc->sc_drq);
220 		sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
221 	}
222 }
223 
224 static int
225 wdc_isa_dma_init(v, channel, drive, databuf, datalen, read)
226 	void *v;
227 	void *databuf;
228 	size_t datalen;
229 	int read;
230 {
231 	struct wdc_isa_softc *sc = v;
232 
233 	isa_dmastart(sc->sc_ic, sc->sc_drq, databuf, datalen, NULL,
234 	    (read ? DMAMODE_READ : DMAMODE_WRITE) | DMAMODE_DEMAND,
235 	    BUS_DMA_NOWAIT);
236 	return 0;
237 }
238 
239 static void
240 wdc_isa_dma_start(v, channel, drive)
241 	void *v;
242 	int channel, drive;
243 {
244 	/* nothing to do */
245 }
246 
247 static int
248 wdc_isa_dma_finish(v, channel, drive, read)
249 	void *v;
250 	int channel, drive;
251 	int read;
252 {
253 	struct wdc_isa_softc *sc = v;
254 
255 	isa_dmadone(sc->sc_ic, sc->sc_drq);
256 	return 0;
257 }
258