pciide_common.c revision 1.72 1 1.72 andvar /* $NetBSD: pciide_common.c,v 1.72 2025/04/16 17:53:04 andvar Exp $ */
2 1.1 bouyer
3 1.1 bouyer
4 1.1 bouyer /*
5 1.1 bouyer * Copyright (c) 1999, 2000, 2001, 2003 Manuel Bouyer.
6 1.1 bouyer *
7 1.1 bouyer * Redistribution and use in source and binary forms, with or without
8 1.1 bouyer * modification, are permitted provided that the following conditions
9 1.1 bouyer * are met:
10 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
11 1.1 bouyer * notice, this list of conditions and the following disclaimer.
12 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
14 1.1 bouyer * documentation and/or other materials provided with the distribution.
15 1.1 bouyer *
16 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.26 perry * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.1 bouyer *
27 1.1 bouyer */
28 1.1 bouyer
29 1.1 bouyer
30 1.1 bouyer /*
31 1.1 bouyer * Copyright (c) 1996, 1998 Christopher G. Demetriou. All rights reserved.
32 1.1 bouyer *
33 1.1 bouyer * Redistribution and use in source and binary forms, with or without
34 1.1 bouyer * modification, are permitted provided that the following conditions
35 1.1 bouyer * are met:
36 1.1 bouyer * 1. Redistributions of source code must retain the above copyright
37 1.1 bouyer * notice, this list of conditions and the following disclaimer.
38 1.1 bouyer * 2. Redistributions in binary form must reproduce the above copyright
39 1.1 bouyer * notice, this list of conditions and the following disclaimer in the
40 1.1 bouyer * documentation and/or other materials provided with the distribution.
41 1.1 bouyer * 3. All advertising materials mentioning features or use of this software
42 1.1 bouyer * must display the following acknowledgement:
43 1.1 bouyer * This product includes software developed by Christopher G. Demetriou
44 1.1 bouyer * for the NetBSD Project.
45 1.1 bouyer * 4. The name of the author may not be used to endorse or promote products
46 1.1 bouyer * derived from this software without specific prior written permission
47 1.1 bouyer *
48 1.1 bouyer * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
49 1.1 bouyer * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
50 1.1 bouyer * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
51 1.1 bouyer * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
52 1.1 bouyer * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
53 1.1 bouyer * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
54 1.1 bouyer * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
55 1.1 bouyer * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
56 1.1 bouyer * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
57 1.1 bouyer * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
58 1.1 bouyer */
59 1.1 bouyer
60 1.1 bouyer /*
61 1.1 bouyer * PCI IDE controller driver.
62 1.1 bouyer *
63 1.1 bouyer * Author: Christopher G. Demetriou, March 2, 1998 (derived from NetBSD
64 1.1 bouyer * sys/dev/pci/ppb.c, revision 1.16).
65 1.1 bouyer *
66 1.1 bouyer * See "PCI IDE Controller Specification, Revision 1.0 3/4/94" and
67 1.1 bouyer * "Programming Interface for Bus Master IDE Controller, Revision 1.0
68 1.1 bouyer * 5/16/94" from the PCI SIG.
69 1.1 bouyer *
70 1.1 bouyer */
71 1.1 bouyer
72 1.1 bouyer #include <sys/cdefs.h>
73 1.72 andvar __KERNEL_RCSID(0, "$NetBSD: pciide_common.c,v 1.72 2025/04/16 17:53:04 andvar Exp $");
74 1.1 bouyer
75 1.1 bouyer #include <sys/param.h>
76 1.1 bouyer
77 1.1 bouyer #include <dev/pci/pcireg.h>
78 1.1 bouyer #include <dev/pci/pcivar.h>
79 1.1 bouyer #include <dev/pci/pcidevs.h>
80 1.1 bouyer #include <dev/pci/pciidereg.h>
81 1.1 bouyer #include <dev/pci/pciidevar.h>
82 1.1 bouyer
83 1.3 fvdl #include <dev/ic/wdcreg.h>
84 1.3 fvdl
85 1.16 thorpej #ifdef ATADEBUG
86 1.72 andvar #ifndef ATADEBUG_PCIIDE_MASK
87 1.72 andvar #define ATADEBUG_PCIIDE_MASK 0
88 1.72 andvar #endif
89 1.72 andvar int atadebug_pciide_mask = ATADEBUG_PCIIDE_MASK;
90 1.1 bouyer #endif
91 1.1 bouyer
92 1.33 itohy #if NATA_DMA
93 1.26 perry static const char dmaerrfmt[] =
94 1.1 bouyer "%s:%d: unable to %s table DMA map for drive %d, error=%d\n";
95 1.33 itohy #endif
96 1.1 bouyer
97 1.1 bouyer /* Default product description for devices not known from this controller */
98 1.1 bouyer const struct pciide_product_desc default_product_desc = {
99 1.1 bouyer 0,
100 1.1 bouyer 0,
101 1.1 bouyer "Generic PCI IDE controller",
102 1.1 bouyer default_chip_map,
103 1.26 perry };
104 1.1 bouyer
105 1.1 bouyer const struct pciide_product_desc *
106 1.39 dsl pciide_lookup_product(pcireg_t id, const struct pciide_product_desc *pp)
107 1.1 bouyer {
108 1.1 bouyer for (; pp->chip_map != NULL; pp++)
109 1.1 bouyer if (PCI_PRODUCT(id) == pp->ide_product)
110 1.1 bouyer break;
111 1.26 perry
112 1.1 bouyer if (pp->chip_map == NULL)
113 1.1 bouyer return NULL;
114 1.1 bouyer return pp;
115 1.1 bouyer }
116 1.1 bouyer
117 1.1 bouyer void
118 1.61 msaitoh pciide_common_attach(struct pciide_softc *sc, const struct pci_attach_args *pa,
119 1.61 msaitoh const struct pciide_product_desc *pp)
120 1.1 bouyer {
121 1.1 bouyer pci_chipset_tag_t pc = pa->pa_pc;
122 1.1 bouyer pcitag_t tag = pa->pa_tag;
123 1.33 itohy #if NATA_DMA
124 1.1 bouyer pcireg_t csr;
125 1.33 itohy #endif
126 1.52 drochner const char *displaydev = NULL;
127 1.52 drochner int dontprint = 0;
128 1.1 bouyer
129 1.1 bouyer sc->sc_pci_id = pa->pa_id;
130 1.1 bouyer if (pp == NULL) {
131 1.1 bouyer /* should only happen for generic pciide devices */
132 1.1 bouyer sc->sc_pp = &default_product_desc;
133 1.1 bouyer } else {
134 1.1 bouyer sc->sc_pp = pp;
135 1.52 drochner /* if ide_name == NULL, printf is done in chip-specific map */
136 1.52 drochner if (pp->ide_name)
137 1.52 drochner displaydev = pp->ide_name;
138 1.52 drochner else
139 1.52 drochner dontprint = 1;
140 1.1 bouyer }
141 1.1 bouyer
142 1.52 drochner if (dontprint) {
143 1.52 drochner aprint_naive("disk controller\n");
144 1.52 drochner aprint_normal("\n"); /* ??? */
145 1.52 drochner } else
146 1.52 drochner pci_aprint_devinfo_fancy(pa, "disk controller", displaydev, 1);
147 1.1 bouyer
148 1.1 bouyer sc->sc_pc = pa->pa_pc;
149 1.1 bouyer sc->sc_tag = pa->pa_tag;
150 1.1 bouyer
151 1.33 itohy #if NATA_DMA
152 1.1 bouyer /* Set up DMA defaults; these might be adjusted by chip_map. */
153 1.1 bouyer sc->sc_dma_maxsegsz = IDEDMA_BYTE_COUNT_MAX;
154 1.1 bouyer sc->sc_dma_boundary = IDEDMA_BYTE_COUNT_ALIGN;
155 1.33 itohy #endif
156 1.1 bouyer
157 1.16 thorpej #ifdef ATADEBUG
158 1.16 thorpej if (atadebug_pciide_mask & DEBUG_PROBE)
159 1.1 bouyer pci_conf_print(sc->sc_pc, sc->sc_tag, NULL);
160 1.1 bouyer #endif
161 1.1 bouyer sc->sc_pp->chip_map(sc, pa);
162 1.1 bouyer
163 1.33 itohy #if NATA_DMA
164 1.1 bouyer if (sc->sc_dma_ok) {
165 1.1 bouyer csr = pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG);
166 1.1 bouyer csr |= PCI_COMMAND_MASTER_ENABLE;
167 1.1 bouyer pci_conf_write(pc, tag, PCI_COMMAND_STATUS_REG, csr);
168 1.1 bouyer }
169 1.33 itohy #endif
170 1.16 thorpej ATADEBUG_PRINT(("pciide: command/status register=%x\n",
171 1.1 bouyer pci_conf_read(pc, tag, PCI_COMMAND_STATUS_REG)), DEBUG_PROBE);
172 1.1 bouyer }
173 1.1 bouyer
174 1.44 jakllsch int
175 1.44 jakllsch pciide_common_detach(struct pciide_softc *sc, int flags)
176 1.44 jakllsch {
177 1.44 jakllsch struct pciide_channel *cp;
178 1.44 jakllsch struct ata_channel *wdc_cp;
179 1.44 jakllsch struct wdc_regs *wdr;
180 1.44 jakllsch int channel, drive;
181 1.44 jakllsch int rv;
182 1.44 jakllsch
183 1.44 jakllsch rv = wdcdetach(sc->sc_wdcdev.sc_atac.atac_dev, flags);
184 1.44 jakllsch if (rv)
185 1.44 jakllsch return rv;
186 1.44 jakllsch
187 1.44 jakllsch for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
188 1.44 jakllsch channel++) {
189 1.44 jakllsch cp = &sc->pciide_channels[channel];
190 1.44 jakllsch wdc_cp = &cp->ata_channel;
191 1.44 jakllsch wdr = CHAN_TO_WDC_REGS(wdc_cp);
192 1.44 jakllsch
193 1.44 jakllsch if (wdc_cp->ch_flags & ATACH_DISABLED)
194 1.44 jakllsch continue;
195 1.44 jakllsch
196 1.44 jakllsch if (wdr->cmd_ios != 0)
197 1.44 jakllsch bus_space_unmap(wdr->cmd_iot,
198 1.44 jakllsch wdr->cmd_baseioh, wdr->cmd_ios);
199 1.44 jakllsch if (cp->compat != 0) {
200 1.44 jakllsch if (wdr->ctl_ios != 0)
201 1.44 jakllsch bus_space_unmap(wdr->ctl_iot,
202 1.44 jakllsch wdr->ctl_ioh, wdr->ctl_ios);
203 1.44 jakllsch } else {
204 1.44 jakllsch if (cp->ctl_ios != 0)
205 1.44 jakllsch bus_space_unmap(wdr->ctl_iot,
206 1.44 jakllsch cp->ctl_baseioh, cp->ctl_ios);
207 1.44 jakllsch }
208 1.44 jakllsch
209 1.57 bouyer for (drive = 0; drive < sc->sc_wdcdev.wdc_maxdrives; drive++) {
210 1.48 dholland #if NATA_DMA
211 1.44 jakllsch pciide_dma_table_teardown(sc, channel, drive);
212 1.48 dholland #endif
213 1.44 jakllsch }
214 1.44 jakllsch }
215 1.44 jakllsch
216 1.48 dholland #if NATA_DMA
217 1.44 jakllsch if (sc->sc_dma_ios != 0)
218 1.44 jakllsch bus_space_unmap(sc->sc_dma_iot, sc->sc_dma_ioh, sc->sc_dma_ios);
219 1.44 jakllsch if (sc->sc_ba5_ss != 0)
220 1.45 jakllsch bus_space_unmap(sc->sc_ba5_st, sc->sc_ba5_sh, sc->sc_ba5_ss);
221 1.48 dholland #endif
222 1.44 jakllsch
223 1.44 jakllsch return 0;
224 1.44 jakllsch }
225 1.44 jakllsch
226 1.46 jakllsch int
227 1.46 jakllsch pciide_detach(device_t self, int flags)
228 1.46 jakllsch {
229 1.46 jakllsch struct pciide_softc *sc = device_private(self);
230 1.46 jakllsch struct pciide_channel *cp;
231 1.46 jakllsch int channel;
232 1.46 jakllsch #ifndef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_DISESTABLISH
233 1.46 jakllsch bool has_compat_chan;
234 1.46 jakllsch
235 1.46 jakllsch has_compat_chan = false;
236 1.46 jakllsch for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
237 1.46 jakllsch channel++) {
238 1.46 jakllsch cp = &sc->pciide_channels[channel];
239 1.46 jakllsch if (cp->compat != 0) {
240 1.46 jakllsch has_compat_chan = true;
241 1.46 jakllsch }
242 1.46 jakllsch }
243 1.46 jakllsch
244 1.46 jakllsch if (has_compat_chan != false)
245 1.46 jakllsch return EBUSY;
246 1.46 jakllsch #endif
247 1.46 jakllsch
248 1.46 jakllsch for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
249 1.46 jakllsch channel++) {
250 1.46 jakllsch cp = &sc->pciide_channels[channel];
251 1.46 jakllsch if (cp->compat != 0)
252 1.58 jakllsch if (cp->ih != NULL) {
253 1.46 jakllsch pciide_unmap_compat_intr(sc->sc_pc, cp, channel);
254 1.58 jakllsch cp->ih = NULL;
255 1.58 jakllsch }
256 1.46 jakllsch }
257 1.46 jakllsch
258 1.58 jakllsch if (sc->sc_pci_ih != NULL) {
259 1.46 jakllsch pci_intr_disestablish(sc->sc_pc, sc->sc_pci_ih);
260 1.58 jakllsch sc->sc_pci_ih = NULL;
261 1.58 jakllsch }
262 1.46 jakllsch
263 1.46 jakllsch return pciide_common_detach(sc, flags);
264 1.46 jakllsch }
265 1.46 jakllsch
266 1.1 bouyer /* tell whether the chip is enabled or not */
267 1.1 bouyer int
268 1.49 dyoung pciide_chipen(struct pciide_softc *sc, const struct pci_attach_args *pa)
269 1.1 bouyer {
270 1.1 bouyer pcireg_t csr;
271 1.1 bouyer
272 1.51 dyoung if ((pa->pa_flags & PCI_FLAGS_IO_OKAY) == 0) {
273 1.38 cube aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
274 1.50 dyoung "I/O access disabled at bridge\n");
275 1.50 dyoung return 0;
276 1.50 dyoung }
277 1.50 dyoung csr = pci_conf_read(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG);
278 1.50 dyoung if ((csr & PCI_COMMAND_IO_ENABLE) == 0) {
279 1.50 dyoung aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
280 1.50 dyoung "I/O access disabled at device\n");
281 1.1 bouyer return 0;
282 1.1 bouyer }
283 1.1 bouyer return 1;
284 1.1 bouyer }
285 1.1 bouyer
286 1.1 bouyer void
287 1.61 msaitoh pciide_mapregs_compat(const struct pci_attach_args *pa,
288 1.61 msaitoh struct pciide_channel *cp, int compatchan)
289 1.1 bouyer {
290 1.19 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
291 1.17 thorpej struct ata_channel *wdc_cp = &cp->ata_channel;
292 1.19 thorpej struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
293 1.3 fvdl int i;
294 1.1 bouyer
295 1.1 bouyer cp->compat = 1;
296 1.1 bouyer
297 1.17 thorpej wdr->cmd_iot = pa->pa_iot;
298 1.17 thorpej if (bus_space_map(wdr->cmd_iot, PCIIDE_COMPAT_CMD_BASE(compatchan),
299 1.17 thorpej PCIIDE_COMPAT_CMD_SIZE, 0, &wdr->cmd_baseioh) != 0) {
300 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
301 1.38 cube "couldn't map %s channel cmd regs\n", cp->name);
302 1.1 bouyer goto bad;
303 1.1 bouyer }
304 1.44 jakllsch wdr->cmd_ios = PCIIDE_COMPAT_CMD_SIZE;
305 1.1 bouyer
306 1.17 thorpej wdr->ctl_iot = pa->pa_iot;
307 1.17 thorpej if (bus_space_map(wdr->ctl_iot, PCIIDE_COMPAT_CTL_BASE(compatchan),
308 1.17 thorpej PCIIDE_COMPAT_CTL_SIZE, 0, &wdr->ctl_ioh) != 0) {
309 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
310 1.38 cube "couldn't map %s channel ctl regs\n", cp->name);
311 1.44 jakllsch bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
312 1.1 bouyer goto bad;
313 1.1 bouyer }
314 1.44 jakllsch wdr->ctl_ios = PCIIDE_COMPAT_CTL_SIZE;
315 1.1 bouyer
316 1.3 fvdl for (i = 0; i < WDC_NREG; i++) {
317 1.17 thorpej if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
318 1.17 thorpej i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
319 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
320 1.38 cube "couldn't subregion %s channel cmd regs\n",
321 1.38 cube cp->name);
322 1.3 fvdl goto bad;
323 1.3 fvdl }
324 1.3 fvdl }
325 1.63 jdolecek wdc_init_shadow_regs(wdr);
326 1.17 thorpej wdr->data32iot = wdr->cmd_iot;
327 1.17 thorpej wdr->data32ioh = wdr->cmd_iohs[0];
328 1.1 bouyer return;
329 1.1 bouyer
330 1.1 bouyer bad:
331 1.17 thorpej cp->ata_channel.ch_flags |= ATACH_DISABLED;
332 1.1 bouyer return;
333 1.1 bouyer }
334 1.1 bouyer
335 1.1 bouyer void
336 1.49 dyoung pciide_mapregs_native(const struct pci_attach_args *pa,
337 1.44 jakllsch struct pciide_channel *cp, int (*pci_intr)(void *))
338 1.1 bouyer {
339 1.19 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
340 1.17 thorpej struct ata_channel *wdc_cp = &cp->ata_channel;
341 1.19 thorpej struct wdc_regs *wdr = CHAN_TO_WDC_REGS(wdc_cp);
342 1.1 bouyer const char *intrstr;
343 1.1 bouyer pci_intr_handle_t intrhandle;
344 1.3 fvdl int i;
345 1.60 christos char intrbuf[PCI_INTRSTR_LEN];
346 1.1 bouyer
347 1.1 bouyer cp->compat = 0;
348 1.1 bouyer
349 1.1 bouyer if (sc->sc_pci_ih == NULL) {
350 1.1 bouyer if (pci_intr_map(pa, &intrhandle) != 0) {
351 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
352 1.38 cube "couldn't map native-PCI interrupt\n");
353 1.1 bouyer goto bad;
354 1.26 perry }
355 1.60 christos intrstr = pci_intr_string(pa->pa_pc, intrhandle, intrbuf, sizeof(intrbuf));
356 1.62 jdolecek sc->sc_pci_ih = pci_intr_establish_xname(pa->pa_pc,
357 1.62 jdolecek intrhandle, IPL_BIO, pci_intr, sc,
358 1.62 jdolecek device_xname(sc->sc_wdcdev.sc_atac.atac_dev));
359 1.1 bouyer if (sc->sc_pci_ih != NULL) {
360 1.38 cube aprint_normal_dev(sc->sc_wdcdev.sc_atac.atac_dev,
361 1.38 cube "using %s for native-PCI interrupt\n",
362 1.1 bouyer intrstr ? intrstr : "unknown interrupt");
363 1.1 bouyer } else {
364 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
365 1.38 cube "couldn't establish native-PCI interrupt");
366 1.1 bouyer if (intrstr != NULL)
367 1.38 cube aprint_error(" at %s", intrstr);
368 1.38 cube aprint_error("\n");
369 1.1 bouyer goto bad;
370 1.1 bouyer }
371 1.1 bouyer }
372 1.1 bouyer cp->ih = sc->sc_pci_ih;
373 1.8 thorpej if (pci_mapreg_map(pa, PCIIDE_REG_CMD_BASE(wdc_cp->ch_channel),
374 1.1 bouyer PCI_MAPREG_TYPE_IO, 0,
375 1.44 jakllsch &wdr->cmd_iot, &wdr->cmd_baseioh, NULL, &wdr->cmd_ios) != 0) {
376 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
377 1.38 cube "couldn't map %s channel cmd regs\n", cp->name);
378 1.1 bouyer goto bad;
379 1.1 bouyer }
380 1.1 bouyer
381 1.8 thorpej if (pci_mapreg_map(pa, PCIIDE_REG_CTL_BASE(wdc_cp->ch_channel),
382 1.1 bouyer PCI_MAPREG_TYPE_IO, 0,
383 1.44 jakllsch &wdr->ctl_iot, &cp->ctl_baseioh, NULL, &cp->ctl_ios) != 0) {
384 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
385 1.38 cube "couldn't map %s channel ctl regs\n", cp->name);
386 1.44 jakllsch bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
387 1.1 bouyer goto bad;
388 1.1 bouyer }
389 1.1 bouyer /*
390 1.1 bouyer * In native mode, 4 bytes of I/O space are mapped for the control
391 1.1 bouyer * register, the control register is at offset 2. Pass the generic
392 1.1 bouyer * code a handle for only one byte at the right offset.
393 1.1 bouyer */
394 1.17 thorpej if (bus_space_subregion(wdr->ctl_iot, cp->ctl_baseioh, 2, 1,
395 1.17 thorpej &wdr->ctl_ioh) != 0) {
396 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
397 1.38 cube "unable to subregion %s channel ctl regs\n", cp->name);
398 1.44 jakllsch bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, wdr->cmd_ios);
399 1.44 jakllsch bus_space_unmap(wdr->cmd_iot, cp->ctl_baseioh, cp->ctl_ios);
400 1.1 bouyer goto bad;
401 1.1 bouyer }
402 1.1 bouyer
403 1.3 fvdl for (i = 0; i < WDC_NREG; i++) {
404 1.17 thorpej if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh, i,
405 1.17 thorpej i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
406 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
407 1.38 cube "couldn't subregion %s channel cmd regs\n",
408 1.38 cube cp->name);
409 1.3 fvdl goto bad;
410 1.3 fvdl }
411 1.3 fvdl }
412 1.63 jdolecek wdc_init_shadow_regs(wdr);
413 1.17 thorpej wdr->data32iot = wdr->cmd_iot;
414 1.17 thorpej wdr->data32ioh = wdr->cmd_iohs[0];
415 1.1 bouyer return;
416 1.1 bouyer
417 1.1 bouyer bad:
418 1.17 thorpej cp->ata_channel.ch_flags |= ATACH_DISABLED;
419 1.1 bouyer return;
420 1.1 bouyer }
421 1.1 bouyer
422 1.33 itohy #if NATA_DMA
423 1.1 bouyer void
424 1.49 dyoung pciide_mapreg_dma(struct pciide_softc *sc, const struct pci_attach_args *pa)
425 1.1 bouyer {
426 1.1 bouyer pcireg_t maptype;
427 1.1 bouyer bus_addr_t addr;
428 1.3 fvdl struct pciide_channel *pc;
429 1.3 fvdl int reg, chan;
430 1.3 fvdl bus_size_t size;
431 1.1 bouyer
432 1.1 bouyer /*
433 1.1 bouyer * Map DMA registers
434 1.1 bouyer *
435 1.1 bouyer * Note that sc_dma_ok is the right variable to test to see if
436 1.1 bouyer * DMA can be done. If the interface doesn't support DMA,
437 1.1 bouyer * sc_dma_ok will never be non-zero. If the DMA regs couldn't
438 1.1 bouyer * be mapped, it'll be zero. I.e., sc_dma_ok will only be
439 1.1 bouyer * non-zero if the interface supports DMA and the registers
440 1.1 bouyer * could be mapped.
441 1.1 bouyer *
442 1.1 bouyer * XXX Note that despite the fact that the Bus Master IDE specs
443 1.1 bouyer * XXX say that "The bus master IDE function uses 16 bytes of IO
444 1.1 bouyer * XXX space," some controllers (at least the United
445 1.1 bouyer * XXX Microelectronics UM8886BF) place it in memory space.
446 1.1 bouyer */
447 1.1 bouyer maptype = pci_mapreg_type(pa->pa_pc, pa->pa_tag,
448 1.1 bouyer PCIIDE_REG_BUS_MASTER_DMA);
449 1.1 bouyer
450 1.1 bouyer switch (maptype) {
451 1.1 bouyer case PCI_MAPREG_TYPE_IO:
452 1.1 bouyer sc->sc_dma_ok = (pci_mapreg_info(pa->pa_pc, pa->pa_tag,
453 1.1 bouyer PCIIDE_REG_BUS_MASTER_DMA, PCI_MAPREG_TYPE_IO,
454 1.1 bouyer &addr, NULL, NULL) == 0);
455 1.1 bouyer if (sc->sc_dma_ok == 0) {
456 1.36 ad aprint_verbose(
457 1.1 bouyer ", but unused (couldn't query registers)");
458 1.1 bouyer break;
459 1.1 bouyer }
460 1.1 bouyer if ((sc->sc_pp->ide_flags & IDE_16BIT_IOSPACE)
461 1.1 bouyer && addr >= 0x10000) {
462 1.1 bouyer sc->sc_dma_ok = 0;
463 1.36 ad aprint_verbose(
464 1.1 bouyer ", but unused (registers at unsafe address "
465 1.1 bouyer "%#lx)", (unsigned long)addr);
466 1.1 bouyer break;
467 1.1 bouyer }
468 1.1 bouyer /* FALLTHROUGH */
469 1.26 perry
470 1.1 bouyer case PCI_MAPREG_MEM_TYPE_32BIT:
471 1.1 bouyer sc->sc_dma_ok = (pci_mapreg_map(pa,
472 1.1 bouyer PCIIDE_REG_BUS_MASTER_DMA, maptype, 0,
473 1.44 jakllsch &sc->sc_dma_iot, &sc->sc_dma_ioh, NULL, &sc->sc_dma_ios)
474 1.44 jakllsch == 0);
475 1.1 bouyer sc->sc_dmat = pa->pa_dmat;
476 1.1 bouyer if (sc->sc_dma_ok == 0) {
477 1.36 ad aprint_verbose(", but unused (couldn't map registers)");
478 1.1 bouyer } else {
479 1.1 bouyer sc->sc_wdcdev.dma_arg = sc;
480 1.1 bouyer sc->sc_wdcdev.dma_init = pciide_dma_init;
481 1.1 bouyer sc->sc_wdcdev.dma_start = pciide_dma_start;
482 1.1 bouyer sc->sc_wdcdev.dma_finish = pciide_dma_finish;
483 1.1 bouyer }
484 1.1 bouyer
485 1.38 cube if (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
486 1.1 bouyer PCIIDE_OPTIONS_NODMA) {
487 1.36 ad aprint_verbose(
488 1.1 bouyer ", but unused (forced off by config file)");
489 1.1 bouyer sc->sc_dma_ok = 0;
490 1.71 thorpej } else {
491 1.71 thorpej bool disable;
492 1.71 thorpej
493 1.71 thorpej if (prop_dictionary_get_bool(
494 1.71 thorpej device_properties(sc->sc_wdcdev.sc_atac.atac_dev),
495 1.71 thorpej "pciide-disable-dma", &disable) && disable) {
496 1.71 thorpej aprint_verbose(
497 1.71 thorpej ", but unused (disabled by platform)");
498 1.71 thorpej sc->sc_dma_ok = 0;
499 1.71 thorpej }
500 1.1 bouyer }
501 1.1 bouyer break;
502 1.1 bouyer
503 1.1 bouyer default:
504 1.1 bouyer sc->sc_dma_ok = 0;
505 1.36 ad aprint_verbose(
506 1.1 bouyer ", but unsupported register maptype (0x%x)", maptype);
507 1.1 bouyer }
508 1.3 fvdl
509 1.12 bouyer if (sc->sc_dma_ok == 0)
510 1.12 bouyer return;
511 1.12 bouyer
512 1.3 fvdl /*
513 1.3 fvdl * Set up the default handles for the DMA registers.
514 1.3 fvdl * Just reserve 32 bits for each handle, unless space
515 1.3 fvdl * doesn't permit it.
516 1.3 fvdl */
517 1.3 fvdl for (chan = 0; chan < PCIIDE_NUM_CHANNELS; chan++) {
518 1.3 fvdl pc = &sc->pciide_channels[chan];
519 1.3 fvdl for (reg = 0; reg < IDEDMA_NREGS; reg++) {
520 1.3 fvdl size = 4;
521 1.3 fvdl if (size > (IDEDMA_SCH_OFFSET - reg))
522 1.3 fvdl size = IDEDMA_SCH_OFFSET - reg;
523 1.3 fvdl if (bus_space_subregion(sc->sc_dma_iot, sc->sc_dma_ioh,
524 1.3 fvdl IDEDMA_SCH_OFFSET * chan + reg, size,
525 1.3 fvdl &pc->dma_iohs[reg]) != 0) {
526 1.3 fvdl sc->sc_dma_ok = 0;
527 1.36 ad aprint_verbose(", but can't subregion offset %d "
528 1.3 fvdl "size %lu", reg, (u_long)size);
529 1.3 fvdl return;
530 1.3 fvdl }
531 1.3 fvdl }
532 1.3 fvdl }
533 1.1 bouyer }
534 1.33 itohy #endif /* NATA_DMA */
535 1.1 bouyer
536 1.1 bouyer int
537 1.39 dsl pciide_compat_intr(void *arg)
538 1.1 bouyer {
539 1.1 bouyer struct pciide_channel *cp = arg;
540 1.1 bouyer
541 1.1 bouyer #ifdef DIAGNOSTIC
542 1.1 bouyer /* should only be called for a compat channel */
543 1.1 bouyer if (cp->compat == 0)
544 1.1 bouyer panic("pciide compat intr called for non-compat chan %p", cp);
545 1.1 bouyer #endif
546 1.17 thorpej return (wdcintr(&cp->ata_channel));
547 1.1 bouyer }
548 1.1 bouyer
549 1.1 bouyer int
550 1.39 dsl pciide_pci_intr(void *arg)
551 1.1 bouyer {
552 1.1 bouyer struct pciide_softc *sc = arg;
553 1.1 bouyer struct pciide_channel *cp;
554 1.17 thorpej struct ata_channel *wdc_cp;
555 1.1 bouyer int i, rv, crv;
556 1.1 bouyer
557 1.1 bouyer rv = 0;
558 1.20 thorpej for (i = 0; i < sc->sc_wdcdev.sc_atac.atac_nchannels; i++) {
559 1.1 bouyer cp = &sc->pciide_channels[i];
560 1.17 thorpej wdc_cp = &cp->ata_channel;
561 1.1 bouyer
562 1.1 bouyer /* If a compat channel skip. */
563 1.1 bouyer if (cp->compat)
564 1.1 bouyer continue;
565 1.1 bouyer
566 1.64 jdolecek /* if this channel not waiting for intr, skip */
567 1.64 jdolecek if ((wdc_cp->ch_flags & ATACH_IRQ_WAIT) == 0)
568 1.64 jdolecek continue;
569 1.64 jdolecek
570 1.1 bouyer crv = wdcintr(wdc_cp);
571 1.1 bouyer if (crv == 0)
572 1.1 bouyer ; /* leave rv alone */
573 1.1 bouyer else if (crv == 1)
574 1.1 bouyer rv = 1; /* claim the intr */
575 1.1 bouyer else if (rv == 0) /* crv should be -1 in this case */
576 1.1 bouyer rv = crv; /* if we've done no better, take it */
577 1.1 bouyer }
578 1.1 bouyer return (rv);
579 1.1 bouyer }
580 1.1 bouyer
581 1.33 itohy #if NATA_DMA
582 1.1 bouyer void
583 1.39 dsl pciide_channel_dma_setup(struct pciide_channel *cp)
584 1.1 bouyer {
585 1.21 thorpej int drive, s;
586 1.19 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
587 1.1 bouyer struct ata_drive_datas *drvp;
588 1.1 bouyer
589 1.57 bouyer KASSERT(cp->ata_channel.ch_ndrives != 0);
590 1.17 thorpej
591 1.57 bouyer for (drive = 0; drive < cp->ata_channel.ch_ndrives; drive++) {
592 1.17 thorpej drvp = &cp->ata_channel.ch_drive[drive];
593 1.1 bouyer /* If no drive, skip */
594 1.57 bouyer if (drvp->drive_type == ATA_DRIVET_NONE)
595 1.1 bouyer continue;
596 1.1 bouyer /* setup DMA if needed */
597 1.57 bouyer if (((drvp->drive_flags & ATA_DRIVE_DMA) == 0 &&
598 1.57 bouyer (drvp->drive_flags & ATA_DRIVE_UDMA) == 0) ||
599 1.1 bouyer sc->sc_dma_ok == 0) {
600 1.21 thorpej s = splbio();
601 1.57 bouyer drvp->drive_flags &= ~(ATA_DRIVE_DMA | ATA_DRIVE_UDMA);
602 1.21 thorpej splx(s);
603 1.1 bouyer continue;
604 1.1 bouyer }
605 1.17 thorpej if (pciide_dma_table_setup(sc, cp->ata_channel.ch_channel,
606 1.8 thorpej drive) != 0) {
607 1.1 bouyer /* Abort DMA setup */
608 1.21 thorpej s = splbio();
609 1.57 bouyer drvp->drive_flags &= ~(ATA_DRIVE_DMA | ATA_DRIVE_UDMA);
610 1.21 thorpej splx(s);
611 1.1 bouyer continue;
612 1.1 bouyer }
613 1.1 bouyer }
614 1.1 bouyer }
615 1.1 bouyer
616 1.24 briggs #define NIDEDMA_TABLES(sc) \
617 1.66 riastrad (MAXPHYS/(uimin((sc)->sc_dma_maxsegsz, PAGE_SIZE)) + 1)
618 1.24 briggs
619 1.1 bouyer int
620 1.40 dsl pciide_dma_table_setup(struct pciide_softc *sc, int channel, int drive)
621 1.1 bouyer {
622 1.44 jakllsch int error;
623 1.1 bouyer const bus_size_t dma_table_size =
624 1.24 briggs sizeof(struct idedma_table) * NIDEDMA_TABLES(sc);
625 1.1 bouyer struct pciide_dma_maps *dma_maps =
626 1.1 bouyer &sc->pciide_channels[channel].dma_maps[drive];
627 1.1 bouyer
628 1.1 bouyer /* If table was already allocated, just return */
629 1.1 bouyer if (dma_maps->dma_table)
630 1.1 bouyer return 0;
631 1.1 bouyer
632 1.1 bouyer /* Allocate memory for the DMA tables and map it */
633 1.1 bouyer if ((error = bus_dmamem_alloc(sc->sc_dmat, dma_table_size,
634 1.44 jakllsch IDEDMA_TBL_ALIGN, IDEDMA_TBL_ALIGN, &dma_maps->dmamap_table_seg,
635 1.44 jakllsch 1, &dma_maps->dmamap_table_nseg, BUS_DMA_NOWAIT)) != 0) {
636 1.38 cube aprint_error(dmaerrfmt,
637 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
638 1.38 cube "allocate", drive, error);
639 1.1 bouyer return error;
640 1.1 bouyer }
641 1.44 jakllsch if ((error = bus_dmamem_map(sc->sc_dmat, &dma_maps->dmamap_table_seg,
642 1.44 jakllsch dma_maps->dmamap_table_nseg, dma_table_size,
643 1.37 christos (void **)&dma_maps->dma_table,
644 1.1 bouyer BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
645 1.38 cube aprint_error(dmaerrfmt,
646 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
647 1.38 cube "map", drive, error);
648 1.1 bouyer return error;
649 1.1 bouyer }
650 1.16 thorpej ATADEBUG_PRINT(("pciide_dma_table_setup: table at %p len %lu, "
651 1.1 bouyer "phy 0x%lx\n", dma_maps->dma_table, (u_long)dma_table_size,
652 1.44 jakllsch (unsigned long)dma_maps->dmamap_table_seg.ds_addr), DEBUG_PROBE);
653 1.1 bouyer /* Create and load table DMA map for this disk */
654 1.1 bouyer if ((error = bus_dmamap_create(sc->sc_dmat, dma_table_size,
655 1.1 bouyer 1, dma_table_size, IDEDMA_TBL_ALIGN, BUS_DMA_NOWAIT,
656 1.1 bouyer &dma_maps->dmamap_table)) != 0) {
657 1.38 cube aprint_error(dmaerrfmt,
658 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
659 1.38 cube "create", drive, error);
660 1.1 bouyer return error;
661 1.1 bouyer }
662 1.1 bouyer if ((error = bus_dmamap_load(sc->sc_dmat,
663 1.1 bouyer dma_maps->dmamap_table,
664 1.1 bouyer dma_maps->dma_table,
665 1.1 bouyer dma_table_size, NULL, BUS_DMA_NOWAIT)) != 0) {
666 1.38 cube aprint_error(dmaerrfmt,
667 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
668 1.38 cube "load", drive, error);
669 1.1 bouyer return error;
670 1.1 bouyer }
671 1.16 thorpej ATADEBUG_PRINT(("pciide_dma_table_setup: phy addr of table 0x%lx\n",
672 1.1 bouyer (unsigned long)dma_maps->dmamap_table->dm_segs[0].ds_addr),
673 1.1 bouyer DEBUG_PROBE);
674 1.1 bouyer /* Create a xfer DMA map for this drive */
675 1.25 briggs if ((error = bus_dmamap_create(sc->sc_dmat, MAXPHYS,
676 1.24 briggs NIDEDMA_TABLES(sc), sc->sc_dma_maxsegsz, sc->sc_dma_boundary,
677 1.1 bouyer BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
678 1.1 bouyer &dma_maps->dmamap_xfer)) != 0) {
679 1.38 cube aprint_error(dmaerrfmt,
680 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
681 1.38 cube "create xfer", drive, error);
682 1.1 bouyer return error;
683 1.1 bouyer }
684 1.1 bouyer return 0;
685 1.1 bouyer }
686 1.1 bouyer
687 1.44 jakllsch void
688 1.44 jakllsch pciide_dma_table_teardown(struct pciide_softc *sc, int channel, int drive)
689 1.44 jakllsch {
690 1.44 jakllsch struct pciide_channel *cp;
691 1.44 jakllsch struct pciide_dma_maps *dma_maps;
692 1.44 jakllsch
693 1.44 jakllsch cp = &sc->pciide_channels[channel];
694 1.44 jakllsch dma_maps = &cp->dma_maps[drive];
695 1.44 jakllsch
696 1.44 jakllsch if (dma_maps->dma_table == NULL)
697 1.44 jakllsch return;
698 1.44 jakllsch
699 1.44 jakllsch bus_dmamap_destroy(sc->sc_dmat, dma_maps->dmamap_xfer);
700 1.44 jakllsch bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_table);
701 1.44 jakllsch bus_dmamap_destroy(sc->sc_dmat, dma_maps->dmamap_table);
702 1.44 jakllsch bus_dmamem_unmap(sc->sc_dmat, dma_maps->dma_table,
703 1.44 jakllsch sizeof(struct idedma_table) * NIDEDMA_TABLES(sc));
704 1.44 jakllsch bus_dmamem_free(sc->sc_dmat, &dma_maps->dmamap_table_seg,
705 1.44 jakllsch dma_maps->dmamap_table_nseg);
706 1.44 jakllsch
707 1.44 jakllsch dma_maps->dma_table = NULL;
708 1.44 jakllsch
709 1.44 jakllsch return;
710 1.44 jakllsch }
711 1.44 jakllsch
712 1.1 bouyer int
713 1.61 msaitoh pciide_dma_dmamap_setup(struct pciide_softc *sc, int channel, int drive,
714 1.61 msaitoh void *databuf, size_t datalen, int flags)
715 1.1 bouyer {
716 1.1 bouyer int error, seg;
717 1.3 fvdl struct pciide_channel *cp = &sc->pciide_channels[channel];
718 1.3 fvdl struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
719 1.1 bouyer
720 1.1 bouyer error = bus_dmamap_load(sc->sc_dmat,
721 1.1 bouyer dma_maps->dmamap_xfer,
722 1.1 bouyer databuf, datalen, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
723 1.1 bouyer ((flags & WDC_DMA_READ) ? BUS_DMA_READ : BUS_DMA_WRITE));
724 1.1 bouyer if (error) {
725 1.38 cube aprint_error(dmaerrfmt,
726 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
727 1.38 cube "load xfer", drive, error);
728 1.1 bouyer return error;
729 1.1 bouyer }
730 1.1 bouyer
731 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
732 1.1 bouyer dma_maps->dmamap_xfer->dm_mapsize,
733 1.1 bouyer (flags & WDC_DMA_READ) ?
734 1.1 bouyer BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
735 1.1 bouyer
736 1.1 bouyer for (seg = 0; seg < dma_maps->dmamap_xfer->dm_nsegs; seg++) {
737 1.70 thorpej bus_addr_t phys = dma_maps->dmamap_xfer->dm_segs[seg].ds_addr;
738 1.70 thorpej bus_size_t len = dma_maps->dmamap_xfer->dm_segs[seg].ds_len;
739 1.70 thorpej
740 1.1 bouyer #ifdef DIAGNOSTIC
741 1.1 bouyer /* A segment must not cross a 64k boundary */
742 1.1 bouyer {
743 1.1 bouyer if ((phys & ~IDEDMA_BYTE_COUNT_MASK) !=
744 1.1 bouyer ((phys + len - 1) & ~IDEDMA_BYTE_COUNT_MASK)) {
745 1.70 thorpej printf("pciide_dma: seg %d addr 0x%" PRIx64
746 1.70 thorpej " len 0x%" PRIx64 " not properly aligned\n",
747 1.70 thorpej seg, (uint64_t)phys, (uint64_t)len);
748 1.1 bouyer panic("pciide_dma: buf align");
749 1.1 bouyer }
750 1.1 bouyer }
751 1.1 bouyer #endif
752 1.70 thorpej /*
753 1.70 thorpej * Some controllers get really upset if the length
754 1.70 thorpej * of any DMA segment is odd. This isn't something
755 1.70 thorpej * that's going to happen in normal steady-state
756 1.70 thorpej * operation (reading VM pages, etc.), but physio users
757 1.70 thorpej * don't have as many guard rails.
758 1.70 thorpej *
759 1.70 thorpej * Consider an 8K read request that starts at an odd
760 1.70 thorpej * offset within a page. At first blush, all of the
761 1.70 thorpej * checks pass because it's a sector-rounded size, but
762 1.70 thorpej * unless the buffer spans 2 physically contiguous pages,
763 1.70 thorpej * it's going to result in 2 odd-length DMA segments.
764 1.70 thorpej *
765 1.70 thorpej * Odd start addresses are also frowned upon, so we
766 1.70 thorpej * catch those here, too.
767 1.70 thorpej *
768 1.70 thorpej * Returning EINVAL here will cause the upper layers to
769 1.70 thorpej * fall back onto PIO.
770 1.70 thorpej */
771 1.70 thorpej if ((phys & 1) != 0 || (len & 1) != 0) {
772 1.70 thorpej aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
773 1.70 thorpej "Invalid DMA segment: "
774 1.70 thorpej "seg %d addr 0x%" PRIx64 " len 0x%" PRIx64 "\n",
775 1.70 thorpej seg, (uint64_t)phys, (uint64_t)len);
776 1.70 thorpej bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
777 1.70 thorpej return EINVAL;
778 1.70 thorpej }
779 1.70 thorpej dma_maps->dma_table[seg].base_addr = htole32(phys);
780 1.1 bouyer dma_maps->dma_table[seg].byte_count =
781 1.70 thorpej htole32(len & IDEDMA_BYTE_COUNT_MASK);
782 1.16 thorpej ATADEBUG_PRINT(("\t seg %d len %d addr 0x%x\n",
783 1.1 bouyer seg, le32toh(dma_maps->dma_table[seg].byte_count),
784 1.1 bouyer le32toh(dma_maps->dma_table[seg].base_addr)), DEBUG_DMA);
785 1.1 bouyer
786 1.1 bouyer }
787 1.1 bouyer dma_maps->dma_table[dma_maps->dmamap_xfer->dm_nsegs -1].byte_count |=
788 1.1 bouyer htole32(IDEDMA_BYTE_COUNT_EOT);
789 1.1 bouyer
790 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_table, 0,
791 1.1 bouyer dma_maps->dmamap_table->dm_mapsize,
792 1.1 bouyer BUS_DMASYNC_PREWRITE);
793 1.1 bouyer
794 1.1 bouyer #ifdef DIAGNOSTIC
795 1.1 bouyer if (dma_maps->dmamap_table->dm_segs[0].ds_addr & ~IDEDMA_TBL_MASK) {
796 1.22 bouyer printf("pciide_dma_dmamap_setup: addr 0x%lx "
797 1.22 bouyer "not properly aligned\n",
798 1.1 bouyer (u_long)dma_maps->dmamap_table->dm_segs[0].ds_addr);
799 1.1 bouyer panic("pciide_dma_init: table align");
800 1.1 bouyer }
801 1.1 bouyer #endif
802 1.22 bouyer /* remember flags */
803 1.22 bouyer dma_maps->dma_flags = flags;
804 1.22 bouyer
805 1.22 bouyer return 0;
806 1.22 bouyer }
807 1.22 bouyer
808 1.22 bouyer int
809 1.61 msaitoh pciide_dma_init(void *v, int channel, int drive, void *databuf, size_t datalen,
810 1.61 msaitoh int flags)
811 1.22 bouyer {
812 1.22 bouyer struct pciide_softc *sc = v;
813 1.22 bouyer int error;
814 1.22 bouyer struct pciide_channel *cp = &sc->pciide_channels[channel];
815 1.22 bouyer struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
816 1.1 bouyer
817 1.22 bouyer if ((error = pciide_dma_dmamap_setup(sc, channel, drive,
818 1.22 bouyer databuf, datalen, flags)) != 0)
819 1.22 bouyer return error;
820 1.22 bouyer /* Maps are ready. Start DMA function */
821 1.1 bouyer /* Clear status bits */
822 1.3 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
823 1.3 fvdl bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
824 1.1 bouyer /* Write table addr */
825 1.3 fvdl bus_space_write_4(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_TBL], 0,
826 1.1 bouyer dma_maps->dmamap_table->dm_segs[0].ds_addr);
827 1.1 bouyer /* set read/write */
828 1.3 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
829 1.5 thorpej ((flags & WDC_DMA_READ) ? IDEDMA_CMD_WRITE : 0) | cp->idedma_cmd);
830 1.1 bouyer return 0;
831 1.1 bouyer }
832 1.1 bouyer
833 1.1 bouyer void
834 1.35 christos pciide_dma_start(void *v, int channel, int drive)
835 1.1 bouyer {
836 1.1 bouyer struct pciide_softc *sc = v;
837 1.3 fvdl struct pciide_channel *cp = &sc->pciide_channels[channel];
838 1.1 bouyer
839 1.16 thorpej ATADEBUG_PRINT(("pciide_dma_start\n"),DEBUG_XFERS);
840 1.3 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
841 1.3 fvdl bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
842 1.3 fvdl | IDEDMA_CMD_START);
843 1.1 bouyer }
844 1.1 bouyer
845 1.1 bouyer int
846 1.40 dsl pciide_dma_finish(void *v, int channel, int drive, int force)
847 1.1 bouyer {
848 1.1 bouyer struct pciide_softc *sc = v;
849 1.1 bouyer u_int8_t status;
850 1.1 bouyer int error = 0;
851 1.3 fvdl struct pciide_channel *cp = &sc->pciide_channels[channel];
852 1.3 fvdl struct pciide_dma_maps *dma_maps = &cp->dma_maps[drive];
853 1.1 bouyer
854 1.3 fvdl status = bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0);
855 1.16 thorpej ATADEBUG_PRINT(("pciide_dma_finish: status 0x%x\n", status),
856 1.1 bouyer DEBUG_XFERS);
857 1.1 bouyer
858 1.14 bouyer if (force == WDC_DMAEND_END && (status & IDEDMA_CTL_INTR) == 0)
859 1.1 bouyer return WDC_DMAST_NOIRQ;
860 1.1 bouyer
861 1.1 bouyer /* stop DMA channel */
862 1.3 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0,
863 1.3 fvdl bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CMD], 0)
864 1.3 fvdl & ~IDEDMA_CMD_START);
865 1.1 bouyer
866 1.1 bouyer /* Unload the map of the data buffer */
867 1.1 bouyer bus_dmamap_sync(sc->sc_dmat, dma_maps->dmamap_xfer, 0,
868 1.1 bouyer dma_maps->dmamap_xfer->dm_mapsize,
869 1.1 bouyer (dma_maps->dma_flags & WDC_DMA_READ) ?
870 1.1 bouyer BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
871 1.1 bouyer bus_dmamap_unload(sc->sc_dmat, dma_maps->dmamap_xfer);
872 1.1 bouyer
873 1.14 bouyer if ((status & IDEDMA_CTL_ERR) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
874 1.38 cube aprint_error("%s:%d:%d: bus-master DMA error: status=0x%x\n",
875 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev), channel,
876 1.38 cube drive, status);
877 1.1 bouyer error |= WDC_DMAST_ERR;
878 1.1 bouyer }
879 1.1 bouyer
880 1.14 bouyer if ((status & IDEDMA_CTL_INTR) == 0 && force != WDC_DMAEND_ABRT_QUIET) {
881 1.38 cube aprint_error("%s:%d:%d: bus-master DMA error: missing "
882 1.38 cube "interrupt, status=0x%x\n",
883 1.38 cube device_xname(sc->sc_wdcdev.sc_atac.atac_dev),
884 1.20 thorpej channel, drive, status);
885 1.1 bouyer error |= WDC_DMAST_NOIRQ;
886 1.1 bouyer }
887 1.1 bouyer
888 1.14 bouyer if ((status & IDEDMA_CTL_ACT) != 0 && force != WDC_DMAEND_ABRT_QUIET) {
889 1.1 bouyer /* data underrun, may be a valid condition for ATAPI */
890 1.1 bouyer error |= WDC_DMAST_UNDER;
891 1.1 bouyer }
892 1.1 bouyer return error;
893 1.1 bouyer }
894 1.1 bouyer
895 1.1 bouyer void
896 1.39 dsl pciide_irqack(struct ata_channel *chp)
897 1.1 bouyer {
898 1.19 thorpej struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
899 1.19 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
900 1.1 bouyer
901 1.1 bouyer /* clear status bits in IDE DMA registers */
902 1.3 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
903 1.3 fvdl bus_space_read_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0));
904 1.1 bouyer }
905 1.33 itohy #endif /* NATA_DMA */
906 1.1 bouyer
907 1.1 bouyer /* some common code used by several chip_map */
908 1.1 bouyer int
909 1.39 dsl pciide_chansetup(struct pciide_softc *sc, int channel, pcireg_t interface)
910 1.1 bouyer {
911 1.1 bouyer struct pciide_channel *cp = &sc->pciide_channels[channel];
912 1.17 thorpej sc->wdc_chanarray[channel] = &cp->ata_channel;
913 1.1 bouyer cp->name = PCIIDE_CHANNEL_NAME(channel);
914 1.17 thorpej cp->ata_channel.ch_channel = channel;
915 1.20 thorpej cp->ata_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
916 1.65 jdolecek
917 1.38 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
918 1.38 cube "%s channel %s to %s mode\n", cp->name,
919 1.1 bouyer (interface & PCIIDE_INTERFACE_SETTABLE(channel)) ?
920 1.1 bouyer "configured" : "wired",
921 1.1 bouyer (interface & PCIIDE_INTERFACE_PCI(channel)) ?
922 1.1 bouyer "native-PCI" : "compatibility");
923 1.1 bouyer return 1;
924 1.1 bouyer }
925 1.1 bouyer
926 1.1 bouyer /* some common code used by several chip channel_map */
927 1.1 bouyer void
928 1.61 msaitoh pciide_mapchan(const struct pci_attach_args *pa, struct pciide_channel *cp,
929 1.44 jakllsch pcireg_t interface, int (*pci_intr)(void *))
930 1.1 bouyer {
931 1.17 thorpej struct ata_channel *wdc_cp = &cp->ata_channel;
932 1.1 bouyer
933 1.8 thorpej if (interface & PCIIDE_INTERFACE_PCI(wdc_cp->ch_channel))
934 1.44 jakllsch pciide_mapregs_native(pa, cp, pci_intr);
935 1.13 bouyer else {
936 1.44 jakllsch pciide_mapregs_compat(pa, cp, wdc_cp->ch_channel);
937 1.17 thorpej if ((cp->ata_channel.ch_flags & ATACH_DISABLED) == 0)
938 1.13 bouyer pciide_map_compat_intr(pa, cp, wdc_cp->ch_channel);
939 1.13 bouyer }
940 1.1 bouyer wdcattach(wdc_cp);
941 1.1 bouyer }
942 1.1 bouyer
943 1.1 bouyer /*
944 1.1 bouyer * generic code to map the compat intr.
945 1.1 bouyer */
946 1.1 bouyer void
947 1.61 msaitoh pciide_map_compat_intr(const struct pci_attach_args *pa,
948 1.61 msaitoh struct pciide_channel *cp, int compatchan)
949 1.1 bouyer {
950 1.19 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
951 1.1 bouyer
952 1.1 bouyer #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
953 1.20 thorpej cp->ih =
954 1.38 cube pciide_machdep_compat_intr_establish(sc->sc_wdcdev.sc_atac.atac_dev,
955 1.20 thorpej pa, compatchan, pciide_compat_intr, cp);
956 1.1 bouyer if (cp->ih == NULL) {
957 1.1 bouyer #endif
958 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
959 1.38 cube "no compatibility interrupt for use by %s "
960 1.38 cube "channel\n", cp->name);
961 1.17 thorpej cp->ata_channel.ch_flags |= ATACH_DISABLED;
962 1.1 bouyer #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_ESTABLISH
963 1.1 bouyer }
964 1.1 bouyer #endif
965 1.1 bouyer }
966 1.1 bouyer
967 1.1 bouyer void
968 1.61 msaitoh pciide_unmap_compat_intr(pci_chipset_tag_t pc, struct pciide_channel *cp,
969 1.61 msaitoh int compatchan)
970 1.46 jakllsch {
971 1.46 jakllsch #ifdef __HAVE_PCIIDE_MACHDEP_COMPAT_INTR_DISESTABLISH
972 1.46 jakllsch struct pciide_softc *sc = CHAN_TO_PCIIDE(&cp->ata_channel);
973 1.46 jakllsch
974 1.46 jakllsch pciide_machdep_compat_intr_disestablish(sc->sc_wdcdev.sc_atac.atac_dev,
975 1.46 jakllsch sc->sc_pc, compatchan, cp->ih);
976 1.46 jakllsch #endif
977 1.46 jakllsch }
978 1.46 jakllsch
979 1.46 jakllsch void
980 1.49 dyoung default_chip_map(struct pciide_softc *sc, const struct pci_attach_args *pa)
981 1.1 bouyer {
982 1.1 bouyer struct pciide_channel *cp;
983 1.1 bouyer pcireg_t interface = PCI_INTERFACE(pa->pa_class);
984 1.1 bouyer pcireg_t csr;
985 1.33 itohy int channel;
986 1.33 itohy #if NATA_DMA
987 1.33 itohy int drive;
988 1.1 bouyer u_int8_t idedma_ctl;
989 1.33 itohy #endif
990 1.27 christos const char *failreason;
991 1.17 thorpej struct wdc_regs *wdr;
992 1.1 bouyer
993 1.1 bouyer if (pciide_chipen(sc, pa) == 0)
994 1.1 bouyer return;
995 1.1 bouyer
996 1.1 bouyer if (interface & PCIIDE_INTERFACE_BUS_MASTER_DMA) {
997 1.33 itohy #if NATA_DMA
998 1.38 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
999 1.38 cube "bus-master DMA support present");
1000 1.1 bouyer if (sc->sc_pp == &default_product_desc &&
1001 1.38 cube (device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev)->cf_flags &
1002 1.1 bouyer PCIIDE_OPTIONS_DMA) == 0) {
1003 1.36 ad aprint_verbose(", but unused (no driver support)");
1004 1.1 bouyer sc->sc_dma_ok = 0;
1005 1.1 bouyer } else {
1006 1.1 bouyer pciide_mapreg_dma(sc, pa);
1007 1.1 bouyer if (sc->sc_dma_ok != 0)
1008 1.36 ad aprint_verbose(", used without full driver "
1009 1.1 bouyer "support");
1010 1.1 bouyer }
1011 1.33 itohy #else
1012 1.38 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
1013 1.38 cube "bus-master DMA support present, but unused (no driver "
1014 1.38 cube "support)");
1015 1.33 itohy #endif /* NATA_DMA */
1016 1.1 bouyer } else {
1017 1.38 cube aprint_verbose_dev(sc->sc_wdcdev.sc_atac.atac_dev,
1018 1.38 cube "hardware does not support DMA");
1019 1.33 itohy #if NATA_DMA
1020 1.1 bouyer sc->sc_dma_ok = 0;
1021 1.33 itohy #endif
1022 1.1 bouyer }
1023 1.36 ad aprint_verbose("\n");
1024 1.33 itohy #if NATA_DMA
1025 1.1 bouyer if (sc->sc_dma_ok) {
1026 1.20 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DMA;
1027 1.1 bouyer sc->sc_wdcdev.irqack = pciide_irqack;
1028 1.1 bouyer }
1029 1.33 itohy #endif
1030 1.20 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
1031 1.33 itohy #if NATA_DMA
1032 1.20 thorpej sc->sc_wdcdev.sc_atac.atac_dma_cap = 0;
1033 1.33 itohy #endif
1034 1.1 bouyer
1035 1.20 thorpej sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
1036 1.20 thorpej sc->sc_wdcdev.sc_atac.atac_nchannels = PCIIDE_NUM_CHANNELS;
1037 1.20 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16;
1038 1.57 bouyer sc->sc_wdcdev.wdc_maxdrives = 2;
1039 1.1 bouyer
1040 1.17 thorpej wdc_allocate_regs(&sc->sc_wdcdev);
1041 1.17 thorpej
1042 1.20 thorpej for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
1043 1.20 thorpej channel++) {
1044 1.1 bouyer cp = &sc->pciide_channels[channel];
1045 1.1 bouyer if (pciide_chansetup(sc, channel, interface) == 0)
1046 1.1 bouyer continue;
1047 1.19 thorpej wdr = CHAN_TO_WDC_REGS(&cp->ata_channel);
1048 1.10 bouyer if (interface & PCIIDE_INTERFACE_PCI(channel))
1049 1.44 jakllsch pciide_mapregs_native(pa, cp, pciide_pci_intr);
1050 1.10 bouyer else
1051 1.10 bouyer pciide_mapregs_compat(pa, cp,
1052 1.44 jakllsch cp->ata_channel.ch_channel);
1053 1.17 thorpej if (cp->ata_channel.ch_flags & ATACH_DISABLED)
1054 1.1 bouyer continue;
1055 1.1 bouyer /*
1056 1.1 bouyer * Check to see if something appears to be there.
1057 1.1 bouyer */
1058 1.1 bouyer failreason = NULL;
1059 1.1 bouyer /*
1060 1.1 bouyer * In native mode, always enable the controller. It's
1061 1.1 bouyer * not possible to have an ISA board using the same address
1062 1.1 bouyer * anyway.
1063 1.1 bouyer */
1064 1.13 bouyer if (interface & PCIIDE_INTERFACE_PCI(channel)) {
1065 1.17 thorpej wdcattach(&cp->ata_channel);
1066 1.13 bouyer continue;
1067 1.13 bouyer }
1068 1.63 jdolecek if (!wdcprobe(CHAN_TO_WDC_REGS(&cp->ata_channel))) {
1069 1.1 bouyer failreason = "not responding; disabled or no drives?";
1070 1.1 bouyer goto next;
1071 1.1 bouyer }
1072 1.1 bouyer /*
1073 1.1 bouyer * Now, make sure it's actually attributable to this PCI IDE
1074 1.1 bouyer * channel by trying to access the channel again while the
1075 1.1 bouyer * PCI IDE controller's I/O space is disabled. (If the
1076 1.1 bouyer * channel no longer appears to be there, it belongs to
1077 1.1 bouyer * this controller.) YUCK!
1078 1.1 bouyer */
1079 1.1 bouyer csr = pci_conf_read(sc->sc_pc, sc->sc_tag,
1080 1.1 bouyer PCI_COMMAND_STATUS_REG);
1081 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_tag, PCI_COMMAND_STATUS_REG,
1082 1.1 bouyer csr & ~PCI_COMMAND_IO_ENABLE);
1083 1.63 jdolecek if (wdcprobe(CHAN_TO_WDC_REGS(&cp->ata_channel)))
1084 1.1 bouyer failreason = "other hardware responding at addresses";
1085 1.1 bouyer pci_conf_write(sc->sc_pc, sc->sc_tag,
1086 1.1 bouyer PCI_COMMAND_STATUS_REG, csr);
1087 1.1 bouyer next:
1088 1.1 bouyer if (failreason) {
1089 1.38 cube aprint_error_dev(sc->sc_wdcdev.sc_atac.atac_dev,
1090 1.38 cube "%s channel ignored (%s)\n", cp->name, failreason);
1091 1.17 thorpej cp->ata_channel.ch_flags |= ATACH_DISABLED;
1092 1.17 thorpej bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
1093 1.44 jakllsch wdr->cmd_ios);
1094 1.44 jakllsch bus_space_unmap(wdr->ctl_iot, wdr->ctl_ioh,
1095 1.44 jakllsch wdr->ctl_ios);
1096 1.10 bouyer } else {
1097 1.13 bouyer pciide_map_compat_intr(pa, cp,
1098 1.17 thorpej cp->ata_channel.ch_channel);
1099 1.17 thorpej wdcattach(&cp->ata_channel);
1100 1.1 bouyer }
1101 1.1 bouyer }
1102 1.1 bouyer
1103 1.33 itohy #if NATA_DMA
1104 1.1 bouyer if (sc->sc_dma_ok == 0)
1105 1.1 bouyer return;
1106 1.1 bouyer
1107 1.1 bouyer /* Allocate DMA maps */
1108 1.20 thorpej for (channel = 0; channel < sc->sc_wdcdev.sc_atac.atac_nchannels;
1109 1.20 thorpej channel++) {
1110 1.1 bouyer idedma_ctl = 0;
1111 1.1 bouyer cp = &sc->pciide_channels[channel];
1112 1.57 bouyer for (drive = 0; drive < sc->sc_wdcdev.wdc_maxdrives; drive++) {
1113 1.29 bouyer /*
1114 1.29 bouyer * we have not probed the drives yet, allocate
1115 1.67 msaitoh * resources for all of them.
1116 1.29 bouyer */
1117 1.1 bouyer if (pciide_dma_table_setup(sc, channel, drive) != 0) {
1118 1.1 bouyer /* Abort DMA setup */
1119 1.1 bouyer aprint_error(
1120 1.1 bouyer "%s:%d:%d: can't allocate DMA maps, "
1121 1.1 bouyer "using PIO transfers\n",
1122 1.38 cube device_xname(
1123 1.38 cube sc->sc_wdcdev.sc_atac.atac_dev),
1124 1.1 bouyer channel, drive);
1125 1.29 bouyer sc->sc_dma_ok = 0;
1126 1.29 bouyer sc->sc_wdcdev.sc_atac.atac_cap &= ~ATAC_CAP_DMA;
1127 1.29 bouyer sc->sc_wdcdev.irqack = NULL;
1128 1.29 bouyer break;
1129 1.1 bouyer }
1130 1.1 bouyer idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1131 1.1 bouyer }
1132 1.1 bouyer if (idedma_ctl != 0) {
1133 1.1 bouyer /* Add software bits in status register */
1134 1.3 fvdl bus_space_write_1(sc->sc_dma_iot,
1135 1.3 fvdl cp->dma_iohs[IDEDMA_CTL], 0, idedma_ctl);
1136 1.1 bouyer }
1137 1.1 bouyer }
1138 1.33 itohy #endif /* NATA_DMA */
1139 1.1 bouyer }
1140 1.1 bouyer
1141 1.1 bouyer void
1142 1.39 dsl sata_setup_channel(struct ata_channel *chp)
1143 1.1 bouyer {
1144 1.33 itohy #if NATA_DMA
1145 1.1 bouyer struct ata_drive_datas *drvp;
1146 1.34 itohy int drive;
1147 1.34 itohy #if NATA_UDMA
1148 1.34 itohy int s;
1149 1.34 itohy #endif
1150 1.1 bouyer u_int32_t idedma_ctl;
1151 1.19 thorpej struct pciide_channel *cp = CHAN_TO_PCHAN(chp);
1152 1.19 thorpej struct pciide_softc *sc = CHAN_TO_PCIIDE(chp);
1153 1.1 bouyer
1154 1.1 bouyer /* setup DMA if needed */
1155 1.1 bouyer pciide_channel_dma_setup(cp);
1156 1.1 bouyer
1157 1.1 bouyer idedma_ctl = 0;
1158 1.1 bouyer
1159 1.57 bouyer KASSERT(cp->ata_channel.ch_ndrives != 0);
1160 1.57 bouyer for (drive = 0; drive < cp->ata_channel.ch_ndrives; drive++) {
1161 1.1 bouyer drvp = &chp->ch_drive[drive];
1162 1.1 bouyer /* If no drive, skip */
1163 1.57 bouyer if (drvp->drive_type == ATA_DRIVET_NONE)
1164 1.1 bouyer continue;
1165 1.33 itohy #if NATA_UDMA
1166 1.57 bouyer if (drvp->drive_flags & ATA_DRIVE_UDMA) {
1167 1.1 bouyer /* use Ultra/DMA */
1168 1.21 thorpej s = splbio();
1169 1.57 bouyer drvp->drive_flags &= ~ATA_DRIVE_DMA;
1170 1.21 thorpej splx(s);
1171 1.1 bouyer idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1172 1.33 itohy } else
1173 1.33 itohy #endif /* NATA_UDMA */
1174 1.57 bouyer if (drvp->drive_flags & ATA_DRIVE_DMA) {
1175 1.1 bouyer idedma_ctl |= IDEDMA_CTL_DRV_DMA(drive);
1176 1.1 bouyer }
1177 1.1 bouyer }
1178 1.1 bouyer
1179 1.1 bouyer /*
1180 1.1 bouyer * Nothing to do to setup modes; it is meaningless in S-ATA
1181 1.1 bouyer * (but many S-ATA drives still want to get the SET_FEATURE
1182 1.1 bouyer * command).
1183 1.1 bouyer */
1184 1.1 bouyer if (idedma_ctl != 0) {
1185 1.1 bouyer /* Add software bits in status register */
1186 1.3 fvdl bus_space_write_1(sc->sc_dma_iot, cp->dma_iohs[IDEDMA_CTL], 0,
1187 1.1 bouyer idedma_ctl);
1188 1.1 bouyer }
1189 1.33 itohy #endif /* NATA_DMA */
1190 1.1 bouyer }
1191