wdc_isa.c revision 1.21 1 /* $NetBSD: wdc_isa.c,v 1.21 2001/06/07 06:33:48 leo 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/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/device.h>
43 #include <sys/malloc.h>
44
45 #include <machine/bus.h>
46 #include <machine/intr.h>
47
48 #include <dev/isa/isavar.h>
49 #include <dev/isa/isadmavar.h>
50
51 #include <dev/ata/atavar.h>
52 #include <dev/ic/wdcvar.h>
53
54 #define WDC_ISA_REG_NPORTS 8
55 #define WDC_ISA_AUXREG_OFFSET 0x206
56 #define WDC_ISA_AUXREG_NPORTS 1 /* XXX "fdc" owns ports 0x3f7/0x377 */
57
58 /* options passed via the 'flags' config keyword */
59 #define WDC_OPTIONS_32 0x01 /* try to use 32bit data I/O */
60 #define WDC_OPTIONS_SINGLE_DRIVE 0x02 /* Don't probe second drive */
61 #define WDC_OPTIONS_ATA_NOSTREAM 0x04
62 #define WDC_OPTIONS_ATAPI_NOSTREAM 0x08
63
64 struct wdc_isa_softc {
65 struct wdc_softc sc_wdcdev;
66 struct channel_softc *wdc_chanptr;
67 struct channel_softc wdc_channel;
68 isa_chipset_tag_t sc_ic;
69 void *sc_ih;
70 int sc_drq;
71 };
72
73 int wdc_isa_probe __P((struct device *, struct cfdata *, void *));
74 void wdc_isa_attach __P((struct device *, struct device *, void *));
75
76 struct cfattach wdc_isa_ca = {
77 sizeof(struct wdc_isa_softc), wdc_isa_probe, wdc_isa_attach
78 };
79
80 static void wdc_isa_dma_setup __P((struct wdc_isa_softc *));
81 static int wdc_isa_dma_init __P((void*, int, int, void *, size_t, int));
82 static void wdc_isa_dma_start __P((void*, int, int));
83 static int wdc_isa_dma_finish __P((void*, int, int, int));
84
85 int
86 wdc_isa_probe(parent, match, aux)
87 struct device *parent;
88 struct cfdata *match;
89 void *aux;
90 {
91 struct channel_softc ch;
92 struct isa_attach_args *ia = aux;
93 int result = 0;
94
95 memset(&ch, 0, sizeof(ch));
96
97 ch.cmd_iot = ia->ia_iot;
98 if (bus_space_map(ch.cmd_iot, ia->ia_iobase, WDC_ISA_REG_NPORTS, 0,
99 &ch.cmd_ioh))
100 goto out;
101
102 ch.ctl_iot = ia->ia_iot;
103 if (bus_space_map(ch.ctl_iot, ia->ia_iobase + WDC_ISA_AUXREG_OFFSET,
104 WDC_ISA_AUXREG_NPORTS, 0, &ch.ctl_ioh))
105 goto outunmap;
106
107 result = wdcprobe(&ch);
108 if (result) {
109 ia->ia_iosize = WDC_ISA_REG_NPORTS;
110 ia->ia_msize = 0;
111 }
112
113 bus_space_unmap(ch.ctl_iot, ch.ctl_ioh, WDC_ISA_AUXREG_NPORTS);
114 outunmap:
115 bus_space_unmap(ch.cmd_iot, ch.cmd_ioh, WDC_ISA_REG_NPORTS);
116 out:
117 return (result);
118 }
119
120 void
121 wdc_isa_attach(parent, self, aux)
122 struct device *parent, *self;
123 void *aux;
124 {
125 struct wdc_isa_softc *sc = (void *)self;
126 struct isa_attach_args *ia = aux;
127
128 printf("\n");
129
130 sc->wdc_channel.cmd_iot = ia->ia_iot;
131 sc->wdc_channel.ctl_iot = ia->ia_iot;
132 sc->sc_ic = ia->ia_ic;
133 if (bus_space_map(sc->wdc_channel.cmd_iot, ia->ia_iobase,
134 WDC_ISA_REG_NPORTS, 0, &sc->wdc_channel.cmd_ioh) ||
135 bus_space_map(sc->wdc_channel.ctl_iot,
136 ia->ia_iobase + WDC_ISA_AUXREG_OFFSET, WDC_ISA_AUXREG_NPORTS,
137 0, &sc->wdc_channel.ctl_ioh)) {
138 printf("%s: couldn't map registers\n",
139 sc->sc_wdcdev.sc_dev.dv_xname);
140 }
141 sc->wdc_channel.data32iot = sc->wdc_channel.cmd_iot;
142 sc->wdc_channel.data32ioh = sc->wdc_channel.cmd_ioh;
143
144 sc->sc_ih = isa_intr_establish(ia->ia_ic, ia->ia_irq, IST_EDGE,
145 IPL_BIO, wdcintr, &sc->wdc_channel);
146
147 if (ia->ia_drq != DRQUNK) {
148 sc->sc_drq = ia->ia_drq;
149
150 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DMA;
151 sc->sc_wdcdev.dma_arg = sc;
152 sc->sc_wdcdev.dma_init = wdc_isa_dma_init;
153 sc->sc_wdcdev.dma_start = wdc_isa_dma_start;
154 sc->sc_wdcdev.dma_finish = wdc_isa_dma_finish;
155 wdc_isa_dma_setup(sc);
156 }
157 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_PREATA;
158 if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_32)
159 sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA32;
160 if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_SINGLE_DRIVE)
161 sc->sc_wdcdev.cap |= WDC_CAPABILITY_SINGLE_DRIVE;
162 if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags & WDC_OPTIONS_ATA_NOSTREAM)
163 sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATA_NOSTREAM;
164 if (sc->sc_wdcdev.sc_dev.dv_cfdata->cf_flags
165 & WDC_OPTIONS_ATAPI_NOSTREAM)
166 sc->sc_wdcdev.cap |= WDC_CAPABILITY_ATAPI_NOSTREAM;
167 sc->sc_wdcdev.PIO_cap = 0;
168 sc->wdc_chanptr = &sc->wdc_channel;
169 sc->sc_wdcdev.channels = &sc->wdc_chanptr;
170 sc->sc_wdcdev.nchannels = 1;
171 sc->wdc_channel.channel = 0;
172 sc->wdc_channel.wdc = &sc->sc_wdcdev;
173 sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
174 M_DEVBUF, M_NOWAIT);
175 if (sc->wdc_channel.ch_queue == NULL) {
176 printf("%s: can't allocate memory for command queue",
177 sc->sc_wdcdev.sc_dev.dv_xname);
178 return;
179 }
180 wdcattach(&sc->wdc_channel);
181 }
182
183 static void
184 wdc_isa_dma_setup(sc)
185 struct wdc_isa_softc *sc;
186 {
187 bus_size_t maxsize;
188
189 if ((maxsize = isa_dmamaxsize(sc->sc_ic, sc->sc_drq)) < MAXPHYS) {
190 printf("%s: max DMA size %lu is less than required %d\n",
191 sc->sc_wdcdev.sc_dev.dv_xname, (u_long)maxsize, MAXPHYS);
192 sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
193 return;
194 }
195
196 if (isa_dmamap_create(sc->sc_ic, sc->sc_drq,
197 MAXPHYS, BUS_DMA_NOWAIT|BUS_DMA_ALLOCNOW)) {
198 printf("%s: can't create map for drq %d\n",
199 sc->sc_wdcdev.sc_dev.dv_xname, sc->sc_drq);
200 sc->sc_wdcdev.cap &= ~WDC_CAPABILITY_DMA;
201 }
202 }
203
204 static int
205 wdc_isa_dma_init(v, channel, drive, databuf, datalen, read)
206 void *v;
207 void *databuf;
208 size_t datalen;
209 int read;
210 {
211 struct wdc_isa_softc *sc = v;
212
213 isa_dmastart(sc->sc_ic, sc->sc_drq, databuf, datalen, NULL,
214 (read ? DMAMODE_READ : DMAMODE_WRITE) | DMAMODE_DEMAND,
215 BUS_DMA_NOWAIT);
216 return 0;
217 }
218
219 static void
220 wdc_isa_dma_start(v, channel, drive)
221 void *v;
222 int channel, drive;
223 {
224 /* nothing to do */
225 }
226
227 static int
228 wdc_isa_dma_finish(v, channel, drive, read)
229 void *v;
230 int channel, drive;
231 int read;
232 {
233 struct wdc_isa_softc *sc = v;
234
235 isa_dmadone(sc->sc_ic, sc->sc_drq);
236 return 0;
237 }
238