Sun Aug 28 09:32:21 2011 UTC ()
Make this compile again without WDC_NO_IDS.


(christos)
diff -r1.263 -r1.264 src/sys/dev/ic/wdc.c

cvs diff -r1.263 -r1.264 src/sys/dev/ic/wdc.c (switch to unified diff)

--- src/sys/dev/ic/wdc.c 2011/08/27 17:05:58 1.263
+++ src/sys/dev/ic/wdc.c 2011/08/28 09:32:21 1.264
@@ -1,1998 +1,1998 @@ @@ -1,1998 +1,1998 @@
1/* $NetBSD: wdc.c,v 1.263 2011/08/27 17:05:58 bouyer Exp $ */ 1/* $NetBSD: wdc.c,v 1.264 2011/08/28 09:32:21 christos Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1998, 2001, 2003 Manuel Bouyer. All rights reserved. 4 * Copyright (c) 1998, 2001, 2003 Manuel Bouyer. All rights reserved.
5 * 5 *
6 * Redistribution and use in source and binary forms, with or without 6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions 7 * modification, are permitted provided that the following conditions
8 * are met: 8 * are met:
9 * 1. Redistributions of source code must retain the above copyright 9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer. 10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright 11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the 12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution. 13 * documentation and/or other materials provided with the distribution.
14 * 14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27/*- 27/*-
28 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc. 28 * Copyright (c) 1998, 2003, 2004 The NetBSD Foundation, Inc.
29 * All rights reserved. 29 * All rights reserved.
30 * 30 *
31 * This code is derived from software contributed to The NetBSD Foundation 31 * This code is derived from software contributed to The NetBSD Foundation
32 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer. 32 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
33 * 33 *
34 * Redistribution and use in source and binary forms, with or without 34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions 35 * modification, are permitted provided that the following conditions
36 * are met: 36 * are met:
37 * 1. Redistributions of source code must retain the above copyright 37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer. 38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright 39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the 40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution. 41 * documentation and/or other materials provided with the distribution.
42 * 42 *
43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 43 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 44 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 45 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 46 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 47 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 48 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 49 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 50 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 51 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 52 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
53 * POSSIBILITY OF SUCH DAMAGE. 53 * POSSIBILITY OF SUCH DAMAGE.
54 */ 54 */
55 55
56/* 56/*
57 * CODE UNTESTED IN THE CURRENT REVISION: 57 * CODE UNTESTED IN THE CURRENT REVISION:
58 */ 58 */
59 59
60#include <sys/cdefs.h> 60#include <sys/cdefs.h>
61__KERNEL_RCSID(0, "$NetBSD: wdc.c,v 1.263 2011/08/27 17:05:58 bouyer Exp $"); 61__KERNEL_RCSID(0, "$NetBSD: wdc.c,v 1.264 2011/08/28 09:32:21 christos Exp $");
62 62
63#include "opt_ata.h" 63#include "opt_ata.h"
64#include "opt_wdc.h" 64#include "opt_wdc.h"
65 65
66#include <sys/param.h> 66#include <sys/param.h>
67#include <sys/systm.h> 67#include <sys/systm.h>
68#include <sys/kernel.h> 68#include <sys/kernel.h>
69#include <sys/conf.h> 69#include <sys/conf.h>
70#include <sys/buf.h> 70#include <sys/buf.h>
71#include <sys/device.h> 71#include <sys/device.h>
72#include <sys/malloc.h> 72#include <sys/malloc.h>
73#include <sys/syslog.h> 73#include <sys/syslog.h>
74#include <sys/proc.h> 74#include <sys/proc.h>
75 75
76#include <sys/intr.h> 76#include <sys/intr.h>
77#include <sys/bus.h> 77#include <sys/bus.h>
78 78
79#ifndef __BUS_SPACE_HAS_STREAM_METHODS 79#ifndef __BUS_SPACE_HAS_STREAM_METHODS
80#define bus_space_write_multi_stream_2 bus_space_write_multi_2 80#define bus_space_write_multi_stream_2 bus_space_write_multi_2
81#define bus_space_write_multi_stream_4 bus_space_write_multi_4 81#define bus_space_write_multi_stream_4 bus_space_write_multi_4
82#define bus_space_read_multi_stream_2 bus_space_read_multi_2 82#define bus_space_read_multi_stream_2 bus_space_read_multi_2
83#define bus_space_read_multi_stream_4 bus_space_read_multi_4 83#define bus_space_read_multi_stream_4 bus_space_read_multi_4
84#define bus_space_read_stream_2 bus_space_read_2 84#define bus_space_read_stream_2 bus_space_read_2
85#define bus_space_read_stream_4 bus_space_read_4 85#define bus_space_read_stream_4 bus_space_read_4
86#define bus_space_write_stream_2 bus_space_write_2 86#define bus_space_write_stream_2 bus_space_write_2
87#define bus_space_write_stream_4 bus_space_write_4 87#define bus_space_write_stream_4 bus_space_write_4
88#endif /* __BUS_SPACE_HAS_STREAM_METHODS */ 88#endif /* __BUS_SPACE_HAS_STREAM_METHODS */
89 89
90#include <dev/ata/atavar.h> 90#include <dev/ata/atavar.h>
91#include <dev/ata/atareg.h> 91#include <dev/ata/atareg.h>
92#include <dev/ata/satareg.h> 92#include <dev/ata/satareg.h>
93#include <dev/ata/satavar.h> 93#include <dev/ata/satavar.h>
94#include <dev/ic/wdcreg.h> 94#include <dev/ic/wdcreg.h>
95#include <dev/ic/wdcvar.h> 95#include <dev/ic/wdcvar.h>
96 96
97#include "locators.h" 97#include "locators.h"
98 98
99#include "atapibus.h" 99#include "atapibus.h"
100#include "wd.h" 100#include "wd.h"
101#include "sata.h" 101#include "sata.h"
102 102
103#define WDCDELAY 100 /* 100 microseconds */ 103#define WDCDELAY 100 /* 100 microseconds */
104#define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY) 104#define WDCNDELAY_RST (WDC_RESET_WAIT * 1000 / WDCDELAY)
105#if 0 105#if 0
106/* If you enable this, it will report any delays more than WDCDELAY * N long. */ 106/* If you enable this, it will report any delays more than WDCDELAY * N long. */
107#define WDCNDELAY_DEBUG 50 107#define WDCNDELAY_DEBUG 50
108#endif 108#endif
109 109
110/* When polling wait that much and then tsleep for 1/hz seconds */ 110/* When polling wait that much and then tsleep for 1/hz seconds */
111#define WDCDELAY_POLL 1 /* ms */ 111#define WDCDELAY_POLL 1 /* ms */
112 112
113/* timeout for the control commands */ 113/* timeout for the control commands */
114#define WDC_CTRL_DELAY 10000 /* 10s, for the recall command */ 114#define WDC_CTRL_DELAY 10000 /* 10s, for the recall command */
115 115
116/* 116/*
117 * timeout when waiting for BSY to deassert when probing. 117 * timeout when waiting for BSY to deassert when probing.
118 * set to 5s. From the standards this could be up to 31, but we can't 118 * set to 5s. From the standards this could be up to 31, but we can't
119 * wait that much at boot time, and 5s seems to be enough. 119 * wait that much at boot time, and 5s seems to be enough.
120 */ 120 */
121#define WDC_PROBE_WAIT 5 121#define WDC_PROBE_WAIT 5
122 122
123 123
124#if NWD > 0 124#if NWD > 0
125extern const struct ata_bustype wdc_ata_bustype; /* in ata_wdc.c */ 125extern const struct ata_bustype wdc_ata_bustype; /* in ata_wdc.c */
126#else 126#else
127/* A fake one, the autoconfig will print "wd at foo ... not configured */ 127/* A fake one, the autoconfig will print "wd at foo ... not configured */
128const struct ata_bustype wdc_ata_bustype = { 128const struct ata_bustype wdc_ata_bustype = {
129 SCSIPI_BUSTYPE_ATA, 129 SCSIPI_BUSTYPE_ATA,
130 NULL, /* wdc_ata_bio */ 130 NULL, /* wdc_ata_bio */
131 NULL, /* wdc_reset_drive */ 131 NULL, /* wdc_reset_drive */
132 wdc_reset_channel, 132 wdc_reset_channel,
133 wdc_exec_command, 133 wdc_exec_command,
134 NULL, /* ata_get_params */ 134 NULL, /* ata_get_params */
135 NULL, /* wdc_ata_addref */ 135 NULL, /* wdc_ata_addref */
136 NULL, /* wdc_ata_delref */ 136 NULL, /* wdc_ata_delref */
137 NULL /* ata_kill_pending */ 137 NULL /* ata_kill_pending */
138}; 138};
139#endif 139#endif
140 140
141/* Flags to wdcreset(). */ 141/* Flags to wdcreset(). */
142#define RESET_POLL 1 142#define RESET_POLL 1
143#define RESET_SLEEP 0 /* wdcreset() will use tsleep() */ 143#define RESET_SLEEP 0 /* wdcreset() will use tsleep() */
144 144
145static int wdcprobe1(struct ata_channel *, int); 145static int wdcprobe1(struct ata_channel *, int);
146static int wdcreset(struct ata_channel *, int); 146static int wdcreset(struct ata_channel *, int);
147static void __wdcerror(struct ata_channel *, const char *); 147static void __wdcerror(struct ata_channel *, const char *);
148static int __wdcwait_reset(struct ata_channel *, int, int); 148static int __wdcwait_reset(struct ata_channel *, int, int);
149static void __wdccommand_done(struct ata_channel *, struct ata_xfer *); 149static void __wdccommand_done(struct ata_channel *, struct ata_xfer *);
150static void __wdccommand_done_end(struct ata_channel *, struct ata_xfer *); 150static void __wdccommand_done_end(struct ata_channel *, struct ata_xfer *);
151static void __wdccommand_kill_xfer(struct ata_channel *, 151static void __wdccommand_kill_xfer(struct ata_channel *,
152 struct ata_xfer *, int); 152 struct ata_xfer *, int);
153static void __wdccommand_start(struct ata_channel *, struct ata_xfer *); 153static void __wdccommand_start(struct ata_channel *, struct ata_xfer *);
154static int __wdccommand_intr(struct ata_channel *, struct ata_xfer *, int); 154static int __wdccommand_intr(struct ata_channel *, struct ata_xfer *, int);
155static int __wdcwait(struct ata_channel *, int, int, int); 155static int __wdcwait(struct ata_channel *, int, int, int);
156 156
157static void wdc_datain_pio(struct ata_channel *, int, void *, size_t); 157static void wdc_datain_pio(struct ata_channel *, int, void *, size_t);
158static void wdc_dataout_pio(struct ata_channel *, int, void *, size_t); 158static void wdc_dataout_pio(struct ata_channel *, int, void *, size_t);
159 159
160#define DEBUG_INTR 0x01 160#define DEBUG_INTR 0x01
161#define DEBUG_XFERS 0x02 161#define DEBUG_XFERS 0x02
162#define DEBUG_STATUS 0x04 162#define DEBUG_STATUS 0x04
163#define DEBUG_FUNCS 0x08 163#define DEBUG_FUNCS 0x08
164#define DEBUG_PROBE 0x10 164#define DEBUG_PROBE 0x10
165#define DEBUG_DETACH 0x20 165#define DEBUG_DETACH 0x20
166#define DEBUG_DELAY 0x40 166#define DEBUG_DELAY 0x40
167#ifdef ATADEBUG 167#ifdef ATADEBUG
168extern int atadebug_mask; /* init'ed in ata.c */ 168extern int atadebug_mask; /* init'ed in ata.c */
169int wdc_nxfer = 0; 169int wdc_nxfer = 0;
170#define ATADEBUG_PRINT(args, level) if (atadebug_mask & (level)) printf args 170#define ATADEBUG_PRINT(args, level) if (atadebug_mask & (level)) printf args
171#else 171#else
172#define ATADEBUG_PRINT(args, level) 172#define ATADEBUG_PRINT(args, level)
173#endif 173#endif
174 174
175/* 175/*
176 * Initialize the "shadow register" handles for a standard wdc controller. 176 * Initialize the "shadow register" handles for a standard wdc controller.
177 */ 177 */
178void 178void
179wdc_init_shadow_regs(struct ata_channel *chp) 179wdc_init_shadow_regs(struct ata_channel *chp)
180{ 180{
181 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 181 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
182 182
183 wdr->cmd_iohs[wd_status] = wdr->cmd_iohs[wd_command]; 183 wdr->cmd_iohs[wd_status] = wdr->cmd_iohs[wd_command];
184 wdr->cmd_iohs[wd_features] = wdr->cmd_iohs[wd_error]; 184 wdr->cmd_iohs[wd_features] = wdr->cmd_iohs[wd_error];
185} 185}
186 186
187/* 187/*
188 * Allocate a wdc_regs array, based on the number of channels. 188 * Allocate a wdc_regs array, based on the number of channels.
189 */ 189 */
190void 190void
191wdc_allocate_regs(struct wdc_softc *wdc) 191wdc_allocate_regs(struct wdc_softc *wdc)
192{ 192{
193 193
194 wdc->regs = malloc(wdc->sc_atac.atac_nchannels * 194 wdc->regs = malloc(wdc->sc_atac.atac_nchannels *
195 sizeof(struct wdc_regs), M_DEVBUF, M_WAITOK); 195 sizeof(struct wdc_regs), M_DEVBUF, M_WAITOK);
196} 196}
197 197
198#if NSATA > 0 198#if NSATA > 0
199/* 199/*
200 * probe drives on SATA controllers with standard SATA registers: 200 * probe drives on SATA controllers with standard SATA registers:
201 * bring the PHYs online, read the drive signature and set drive flags 201 * bring the PHYs online, read the drive signature and set drive flags
202 * appropriately. 202 * appropriately.
203 */ 203 */
204void 204void
205wdc_sataprobe(struct ata_channel *chp) 205wdc_sataprobe(struct ata_channel *chp)
206{ 206{
207 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 207 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
208 uint8_t st = 0, sc, sn, cl, ch; 208 uint8_t st = 0, sc, sn, cl, ch;
209 int i, s; 209 int i, s;
210 210
211 /* XXX This should be done by other code. */ 211 /* XXX This should be done by other code. */
212 for (i = 0; i < chp->ch_ndrive; i++) { 212 for (i = 0; i < chp->ch_ndrive; i++) {
213 chp->ch_drive[i].chnl_softc = chp; 213 chp->ch_drive[i].chnl_softc = chp;
214 chp->ch_drive[i].drive = i; 214 chp->ch_drive[i].drive = i;
215 } 215 }
216 216
217 /* reset the PHY and bring online */ 217 /* reset the PHY and bring online */
218 switch (sata_reset_interface(chp, wdr->sata_iot, wdr->sata_control, 218 switch (sata_reset_interface(chp, wdr->sata_iot, wdr->sata_control,
219 wdr->sata_status)) { 219 wdr->sata_status)) {
220 case SStatus_DET_DEV: 220 case SStatus_DET_DEV:
221 /* wait 5s for BSY to clear */ 221 /* wait 5s for BSY to clear */
222 for (i = 0; i < WDC_PROBE_WAIT * hz; i++) { 222 for (i = 0; i < WDC_PROBE_WAIT * hz; i++) {
223 bus_space_write_1(wdr->cmd_iot, 223 bus_space_write_1(wdr->cmd_iot,
224 wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM); 224 wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM);
225 delay(10); /* 400ns delay */ 225 delay(10); /* 400ns delay */
226 st = bus_space_read_1(wdr->cmd_iot, 226 st = bus_space_read_1(wdr->cmd_iot,
227 wdr->cmd_iohs[wd_status], 0); 227 wdr->cmd_iohs[wd_status], 0);
228 if ((st & WDCS_BSY) == 0) 228 if ((st & WDCS_BSY) == 0)
229 break; 229 break;
230 tsleep(&chp, PRIBIO, "sataprb", 1); 230 tsleep(&chp, PRIBIO, "sataprb", 1);
231 } 231 }
232 if (i == WDC_PROBE_WAIT * hz) 232 if (i == WDC_PROBE_WAIT * hz)
233 aprint_error_dev(chp->ch_atac->atac_dev, 233 aprint_error_dev(chp->ch_atac->atac_dev,
234 "BSY never cleared, status 0x%02x\n", st); 234 "BSY never cleared, status 0x%02x\n", st);
235 sc = bus_space_read_1(wdr->cmd_iot, 235 sc = bus_space_read_1(wdr->cmd_iot,
236 wdr->cmd_iohs[wd_seccnt], 0); 236 wdr->cmd_iohs[wd_seccnt], 0);
237 sn = bus_space_read_1(wdr->cmd_iot, 237 sn = bus_space_read_1(wdr->cmd_iot,
238 wdr->cmd_iohs[wd_sector], 0); 238 wdr->cmd_iohs[wd_sector], 0);
239 cl = bus_space_read_1(wdr->cmd_iot, 239 cl = bus_space_read_1(wdr->cmd_iot,
240 wdr->cmd_iohs[wd_cyl_lo], 0); 240 wdr->cmd_iohs[wd_cyl_lo], 0);
241 ch = bus_space_read_1(wdr->cmd_iot, 241 ch = bus_space_read_1(wdr->cmd_iot,
242 wdr->cmd_iohs[wd_cyl_hi], 0); 242 wdr->cmd_iohs[wd_cyl_hi], 0);
243 ATADEBUG_PRINT(("%s: port %d: sc=0x%x sn=0x%x " 243 ATADEBUG_PRINT(("%s: port %d: sc=0x%x sn=0x%x "
244 "cl=0x%x ch=0x%x\n", 244 "cl=0x%x ch=0x%x\n",
245 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 245 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
246 sc, sn, cl, ch), DEBUG_PROBE); 246 sc, sn, cl, ch), DEBUG_PROBE);
247 /* 247 /*
248 * sc and sn are supposed to be 0x1 for ATAPI, but in some 248 * sc and sn are supposed to be 0x1 for ATAPI, but in some
249 * cases we get wrong values here, so ignore it. 249 * cases we get wrong values here, so ignore it.
250 */ 250 */
251 s = splbio(); 251 s = splbio();
252 if (cl == 0x14 && ch == 0xeb) 252 if (cl == 0x14 && ch == 0xeb)
253 chp->ch_drive[0].drive_flags |= DRIVE_ATAPI; 253 chp->ch_drive[0].drive_flags |= DRIVE_ATAPI;
254 else 254 else
255 chp->ch_drive[0].drive_flags |= DRIVE_ATA; 255 chp->ch_drive[0].drive_flags |= DRIVE_ATA;
256 splx(s); 256 splx(s);
257 257
258 /* 258 /*
259 * issue a reset in case only the interface part of the drive 259 * issue a reset in case only the interface part of the drive
260 * is up 260 * is up
261 */ 261 */
262 if (wdcreset(chp, RESET_SLEEP) != 0) 262 if (wdcreset(chp, RESET_SLEEP) != 0)
263 chp->ch_drive[0].drive_flags = 0; 263 chp->ch_drive[0].drive_flags = 0;
264 break; 264 break;
265 265
266 default: 266 default:
267 break; 267 break;
268 } 268 }
269} 269}
270#endif /* NSATA > 0 */ 270#endif /* NSATA > 0 */
271 271
272 272
273/* Test to see controller with at last one attached drive is there. 273/* Test to see controller with at last one attached drive is there.
274 * Returns a bit for each possible drive found (0x01 for drive 0, 274 * Returns a bit for each possible drive found (0x01 for drive 0,
275 * 0x02 for drive 1). 275 * 0x02 for drive 1).
276 * Logic: 276 * Logic:
277 * - If a status register is at 0xff, assume there is no drive here 277 * - If a status register is at 0xff, assume there is no drive here
278 * (ISA has pull-up resistors). Similarly if the status register has 278 * (ISA has pull-up resistors). Similarly if the status register has
279 * the value we last wrote to the bus (for IDE interfaces without pullups). 279 * the value we last wrote to the bus (for IDE interfaces without pullups).
280 * If no drive at all -> return. 280 * If no drive at all -> return.
281 * - reset the controller, wait for it to complete (may take up to 31s !). 281 * - reset the controller, wait for it to complete (may take up to 31s !).
282 * If timeout -> return. 282 * If timeout -> return.
283 * - test ATA/ATAPI signatures. If at last one drive found -> return. 283 * - test ATA/ATAPI signatures. If at last one drive found -> return.
284 * - try an ATA command on the master. 284 * - try an ATA command on the master.
285 */ 285 */
286 286
287void 287void
288wdc_drvprobe(struct ata_channel *chp) 288wdc_drvprobe(struct ata_channel *chp)
289{ 289{
290 struct ataparams params; /* XXX: large struct */ 290 struct ataparams params; /* XXX: large struct */
291 struct atac_softc *atac = chp->ch_atac; 291 struct atac_softc *atac = chp->ch_atac;
292 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 292 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
293 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 293 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
294 u_int8_t st0 = 0, st1 = 0; 294 u_int8_t st0 = 0, st1 = 0;
295 int i, j, error, s; 295 int i, j, error, s;
296 296
297 if (wdcprobe1(chp, 0) == 0) { 297 if (wdcprobe1(chp, 0) == 0) {
298 /* No drives, abort the attach here. */ 298 /* No drives, abort the attach here. */
299 return; 299 return;
300 } 300 }
301 301
 302#ifndef WDC_NO_IDS
302 s = splbio(); 303 s = splbio();
 304#endif
303 /* for ATA/OLD drives, wait for DRDY, 3s timeout */ 305 /* for ATA/OLD drives, wait for DRDY, 3s timeout */
304 for (i = 0; i < mstohz(3000); i++) { 306 for (i = 0; i < mstohz(3000); i++) {
305 /* 307 /*
306 * select drive 1 first, so that master is selected on 308 * select drive 1 first, so that master is selected on
307 * exit from the loop 309 * exit from the loop
308 */ 310 */
309 if (chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD)) { 311 if (chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD)) {
310 if (wdc->select) 312 if (wdc->select)
311 wdc->select(chp,1); 313 wdc->select(chp,1);
312 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 314 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
313 0, WDSD_IBM | 0x10); 315 0, WDSD_IBM | 0x10);
314 delay(10); /* 400ns delay */ 316 delay(10); /* 400ns delay */
315 st1 = bus_space_read_1(wdr->cmd_iot, 317 st1 = bus_space_read_1(wdr->cmd_iot,
316 wdr->cmd_iohs[wd_status], 0); 318 wdr->cmd_iohs[wd_status], 0);
317 } 319 }
318 if (chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD)) { 320 if (chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD)) {
319 if (wdc->select) 321 if (wdc->select)
320 wdc->select(chp,0); 322 wdc->select(chp,0);
321 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 323 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
322 0, WDSD_IBM); 324 0, WDSD_IBM);
323 delay(10); /* 400ns delay */ 325 delay(10); /* 400ns delay */
324 st0 = bus_space_read_1(wdr->cmd_iot, 326 st0 = bus_space_read_1(wdr->cmd_iot,
325 wdr->cmd_iohs[wd_status], 0); 327 wdr->cmd_iohs[wd_status], 0);
326 } 328 }
327 329
328 330
329 if (((chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD)) 331 if (((chp->ch_drive[0].drive_flags & (DRIVE_ATA|DRIVE_OLD))
330 == 0 || 332 == 0 ||
331 (st0 & WDCS_DRDY)) && 333 (st0 & WDCS_DRDY)) &&
332 ((chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD)) 334 ((chp->ch_drive[1].drive_flags & (DRIVE_ATA|DRIVE_OLD))
333 == 0 || 335 == 0 ||
334 (st1 & WDCS_DRDY))) 336 (st1 & WDCS_DRDY)))
335 break; 337 break;
336#ifdef WDC_NO_IDS 338#ifdef WDC_NO_IDS
337 /* cannot tsleep here (can't enable IPL_BIO interrups), 339 /* cannot tsleep here (can't enable IPL_BIO interrups),
338 * delay instead 340 * delay instead
339 */ 341 */
340 delay(1000000 / hz); 342 delay(1000000 / hz);
 343 }
341#else 344#else
342#error "NEED WDC_NO_IDS" 
343 tsleep(&params, PRIBIO, "atadrdy", 1); 345 tsleep(&params, PRIBIO, "atadrdy", 1);
344#endif 
345 } 346 }
 347 s = splbio();
 348#endif
346 if ((st0 & WDCS_DRDY) == 0) 349 if ((st0 & WDCS_DRDY) == 0)
347 chp->ch_drive[0].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD); 350 chp->ch_drive[0].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD);
348 if ((st1 & WDCS_DRDY) == 0) 351 if ((st1 & WDCS_DRDY) == 0)
349 chp->ch_drive[1].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD); 352 chp->ch_drive[1].drive_flags &= ~(DRIVE_ATA|DRIVE_OLD);
350 splx(s); 353 splx(s);
351 354
352 ATADEBUG_PRINT(("%s:%d: wait DRDY st0 0x%x st1 0x%x\n", 355 ATADEBUG_PRINT(("%s:%d: wait DRDY st0 0x%x st1 0x%x\n",
353 device_xname(atac->atac_dev), 356 device_xname(atac->atac_dev),
354 chp->ch_channel, st0, st1), DEBUG_PROBE); 357 chp->ch_channel, st0, st1), DEBUG_PROBE);
355 358
356 /* Wait a bit, some devices are weird just after a reset. */ 359 /* Wait a bit, some devices are weird just after a reset. */
357 delay(5000); 360 delay(5000);
358 361
359 for (i = 0; i < chp->ch_ndrive; i++) { 362 for (i = 0; i < chp->ch_ndrive; i++) {
360 /* XXX This should be done by other code. */ 363 /* XXX This should be done by other code. */
361 chp->ch_drive[i].chnl_softc = chp; 364 chp->ch_drive[i].chnl_softc = chp;
362 chp->ch_drive[i].drive = i; 365 chp->ch_drive[i].drive = i;
363 366
364#if NATA_DMA 367#if NATA_DMA
365 /* 368 /*
366 * Init error counter so that an error withing the first xfers 369 * Init error counter so that an error withing the first xfers
367 * will trigger a downgrade 370 * will trigger a downgrade
368 */ 371 */
369 chp->ch_drive[i].n_dmaerrs = NERRS_MAX-1; 372 chp->ch_drive[i].n_dmaerrs = NERRS_MAX-1;
370#endif 373#endif
371 374
372 /* If controller can't do 16bit flag the drives as 32bit */ 375 /* If controller can't do 16bit flag the drives as 32bit */
373 if ((atac->atac_cap & 376 if ((atac->atac_cap &
374 (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) == ATAC_CAP_DATA32) { 377 (ATAC_CAP_DATA16 | ATAC_CAP_DATA32)) == ATAC_CAP_DATA32) {
375 s = splbio(); 378 s = splbio();
376 chp->ch_drive[i].drive_flags |= DRIVE_CAP32; 379 chp->ch_drive[i].drive_flags |= DRIVE_CAP32;
377 splx(s); 380 splx(s);
378 } 381 }
379 if ((chp->ch_drive[i].drive_flags & DRIVE) == 0) 382 if ((chp->ch_drive[i].drive_flags & DRIVE) == 0)
380 continue; 383 continue;
381 384
382 /* Shortcut in case we've been shutdown */ 385 /* Shortcut in case we've been shutdown */
383 if (chp->ch_flags & ATACH_SHUTDOWN) 386 if (chp->ch_flags & ATACH_SHUTDOWN)
384 return; 387 return;
385 388
386 /* 389 /*
387 * Issue an identify, to try to detect ghosts. 390 * Issue an identify, to try to detect ghosts.
388 * Note that we can't use interrupts here, because if there 391 * Note that we can't use interrupts here, because if there
389 * is no devices, we will get a command aborted without 392 * is no devices, we will get a command aborted without
390 * interrupts. 393 * interrupts.
391 */ 394 */
392 error = ata_get_params(&chp->ch_drive[i], 395 error = ata_get_params(&chp->ch_drive[i],
393 AT_WAIT | AT_POLL, &params); 396 AT_WAIT | AT_POLL, &params);
394 if (error != CMD_OK) { 397 if (error != CMD_OK) {
395 tsleep(&params, PRIBIO, "atacnf", mstohz(1000)); 398 tsleep(&params, PRIBIO, "atacnf", mstohz(1000));
396 399
397 /* Shortcut in case we've been shutdown */ 400 /* Shortcut in case we've been shutdown */
398 if (chp->ch_flags & ATACH_SHUTDOWN) 401 if (chp->ch_flags & ATACH_SHUTDOWN)
399 return; 402 return;
400 403
401 error = ata_get_params(&chp->ch_drive[i], 404 error = ata_get_params(&chp->ch_drive[i],
402 AT_WAIT | AT_POLL, &params); 405 AT_WAIT | AT_POLL, &params);
403 } 406 }
404 if (error == CMD_OK) { 407 if (error == CMD_OK) {
405 /* If IDENTIFY succeeded, this is not an OLD ctrl */ 408 /* If IDENTIFY succeeded, this is not an OLD ctrl */
406 s = splbio(); 409 s = splbio();
407 for (j = 0; j < chp->ch_ndrive; j++) 410 for (j = 0; j < chp->ch_ndrive; j++)
408 chp->ch_drive[j].drive_flags &= ~DRIVE_OLD; 411 chp->ch_drive[j].drive_flags &= ~DRIVE_OLD;
409 splx(s); 412 splx(s);
410 } else { 413 } else {
411 s = splbio(); 414 s = splbio();
412 chp->ch_drive[i].drive_flags &= 415 chp->ch_drive[i].drive_flags &=
413 ~(DRIVE_ATA | DRIVE_ATAPI); 416 ~(DRIVE_ATA | DRIVE_ATAPI);
414 splx(s); 417 splx(s);
415 ATADEBUG_PRINT(("%s:%d:%d: IDENTIFY failed (%d)\n", 418 ATADEBUG_PRINT(("%s:%d:%d: IDENTIFY failed (%d)\n",
416 device_xname(atac->atac_dev), 419 device_xname(atac->atac_dev),
417 chp->ch_channel, i, error), DEBUG_PROBE); 420 chp->ch_channel, i, error), DEBUG_PROBE);
418 if ((chp->ch_drive[i].drive_flags & DRIVE_OLD) == 0) 421 if ((chp->ch_drive[i].drive_flags & DRIVE_OLD) == 0)
419 continue; 422 continue;
420 /* 423 /*
421 * Pre-ATA drive ? 424 * Pre-ATA drive ?
422 * Test registers writability (Error register not 425 * Test registers writability (Error register not
423 * writable, but cyllo is), then try an ATA command. 426 * writable, but cyllo is), then try an ATA command.
424 */ 427 */
425 if (wdc->select) 428 if (wdc->select)
426 wdc->select(chp,i); 429 wdc->select(chp,i);
427 bus_space_write_1(wdr->cmd_iot, 430 bus_space_write_1(wdr->cmd_iot,
428 wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM | (i << 4)); 431 wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM | (i << 4));
429 delay(10); /* 400ns delay */ 432 delay(10); /* 400ns delay */
430 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 433 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error],
431 0, 0x58); 434 0, 0x58);
432 bus_space_write_1(wdr->cmd_iot, 435 bus_space_write_1(wdr->cmd_iot,
433 wdr->cmd_iohs[wd_cyl_lo], 0, 0xa5); 436 wdr->cmd_iohs[wd_cyl_lo], 0, 0xa5);
434 if (bus_space_read_1(wdr->cmd_iot, 437 if (bus_space_read_1(wdr->cmd_iot,
435 wdr->cmd_iohs[wd_error], 0) == 0x58 || 438 wdr->cmd_iohs[wd_error], 0) == 0x58 ||
436 bus_space_read_1(wdr->cmd_iot, 439 bus_space_read_1(wdr->cmd_iot,
437 wdr->cmd_iohs[wd_cyl_lo], 0) != 0xa5) { 440 wdr->cmd_iohs[wd_cyl_lo], 0) != 0xa5) {
438 ATADEBUG_PRINT(("%s:%d:%d: register " 441 ATADEBUG_PRINT(("%s:%d:%d: register "
439 "writability failed\n", 442 "writability failed\n",
440 device_xname(atac->atac_dev), 443 device_xname(atac->atac_dev),
441 chp->ch_channel, i), DEBUG_PROBE); 444 chp->ch_channel, i), DEBUG_PROBE);
442 s = splbio(); 445 s = splbio();
443 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD; 446 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
444 splx(s); 447 splx(s);
445 continue; 448 continue;
446 } 449 }
447 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) { 450 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) {
448 ATADEBUG_PRINT(("%s:%d:%d: not ready\n", 451 ATADEBUG_PRINT(("%s:%d:%d: not ready\n",
449 device_xname(atac->atac_dev), 452 device_xname(atac->atac_dev),
450 chp->ch_channel, i), DEBUG_PROBE); 453 chp->ch_channel, i), DEBUG_PROBE);
451 s = splbio(); 454 s = splbio();
452 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD; 455 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
453 splx(s); 456 splx(s);
454 continue; 457 continue;
455 } 458 }
456 bus_space_write_1(wdr->cmd_iot, 459 bus_space_write_1(wdr->cmd_iot,
457 wdr->cmd_iohs[wd_command], 0, WDCC_RECAL); 460 wdr->cmd_iohs[wd_command], 0, WDCC_RECAL);
458 delay(10); /* 400ns delay */ 461 delay(10); /* 400ns delay */
459 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) { 462 if (wdc_wait_for_ready(chp, 10000, 0) == WDCWAIT_TOUT) {
460 ATADEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n", 463 ATADEBUG_PRINT(("%s:%d:%d: WDCC_RECAL failed\n",
461 device_xname(atac->atac_dev), 464 device_xname(atac->atac_dev),
462 chp->ch_channel, i), DEBUG_PROBE); 465 chp->ch_channel, i), DEBUG_PROBE);
463 s = splbio(); 466 s = splbio();
464 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD; 467 chp->ch_drive[i].drive_flags &= ~DRIVE_OLD;
465 splx(s); 468 splx(s);
466 } else { 469 } else {
467 s = splbio(); 470 s = splbio();
468 for (j = 0; j < chp->ch_ndrive; j++) 471 for (j = 0; j < chp->ch_ndrive; j++)
469 chp->ch_drive[j].drive_flags &= 472 chp->ch_drive[j].drive_flags &=
470 ~(DRIVE_ATA | DRIVE_ATAPI); 473 ~(DRIVE_ATA | DRIVE_ATAPI);
471 splx(s); 474 splx(s);
472 } 475 }
473 } 476 }
474 } 477 }
475} 478}
476 479
477int 480int
478wdcprobe(struct ata_channel *chp) 481wdcprobe(struct ata_channel *chp)
479{ 482{
480 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 483 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
481 /* default reset method */ 484 /* default reset method */
482 if (wdc->reset == NULL) 485 if (wdc->reset == NULL)
483 wdc->reset = wdc_do_reset; 486 wdc->reset = wdc_do_reset;
484 487
485 return (wdcprobe1(chp, 1)); 488 return (wdcprobe1(chp, 1));
486} 489}
487 490
488static int 491static int
489wdcprobe1(struct ata_channel *chp, int poll) 492wdcprobe1(struct ata_channel *chp, int poll)
490{ 493{
491 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 494 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
492 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 495 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
493 u_int8_t st0 = 0, st1 = 0, sc, sn, cl, ch; 496 u_int8_t st0 = 0, st1 = 0, sc, sn, cl, ch;
494 u_int8_t ret_value = 0x03; 497 u_int8_t ret_value = 0x03;
495 u_int8_t drive; 498 u_int8_t drive;
496 int s; 499 int s;
497 /* XXX if poll, wdc_probe_count is 0. */ 500 /* XXX if poll, wdc_probe_count is 0. */
498 int wdc_probe_count = 501 int wdc_probe_count =
499 poll ? (WDC_PROBE_WAIT / WDCDELAY) 502 poll ? (WDC_PROBE_WAIT / WDCDELAY)
500 : (WDC_PROBE_WAIT * hz); 503 : (WDC_PROBE_WAIT * hz);
501 504
502 /* 505 /*
503 * Sanity check to see if the wdc channel responds at all. 506 * Sanity check to see if the wdc channel responds at all.
504 */ 507 */
505 508
506 s = splbio(); 509 s = splbio();
507 if ((wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) { 510 if ((wdc->cap & WDC_CAPABILITY_NO_EXTRA_RESETS) == 0) {
508 while (wdc_probe_count-- > 0) { 511 while (wdc_probe_count-- > 0) {
509 if (wdc->select) 512 if (wdc->select)
510 wdc->select(chp,0); 513 wdc->select(chp,0);
511 514
512 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 515 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
513 0, WDSD_IBM); 516 0, WDSD_IBM);
514 delay(10); /* 400ns delay */ 517 delay(10); /* 400ns delay */
515 st0 = bus_space_read_1(wdr->cmd_iot, 518 st0 = bus_space_read_1(wdr->cmd_iot,
516 wdr->cmd_iohs[wd_status], 0); 519 wdr->cmd_iohs[wd_status], 0);
517 520
518 if (wdc->select) 521 if (wdc->select)
519 wdc->select(chp,1); 522 wdc->select(chp,1);
520 523
521 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 524 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
522 0, WDSD_IBM | 0x10); 525 0, WDSD_IBM | 0x10);
523 delay(10); /* 400ns delay */ 526 delay(10); /* 400ns delay */
524 st1 = bus_space_read_1(wdr->cmd_iot, 527 st1 = bus_space_read_1(wdr->cmd_iot,
525 wdr->cmd_iohs[wd_status], 0); 528 wdr->cmd_iohs[wd_status], 0);
526 if ((st0 & WDCS_BSY) == 0) 529 if ((st0 & WDCS_BSY) == 0)
527 break; 530 break;
528 } 531 }
529 532
530 ATADEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n", 533 ATADEBUG_PRINT(("%s:%d: before reset, st0=0x%x, st1=0x%x\n",
531 device_xname(chp->ch_atac->atac_dev), 534 device_xname(chp->ch_atac->atac_dev),
532 chp->ch_channel, st0, st1), DEBUG_PROBE); 535 chp->ch_channel, st0, st1), DEBUG_PROBE);
533 536
534 if (st0 == 0xff || st0 == WDSD_IBM) 537 if (st0 == 0xff || st0 == WDSD_IBM)
535 ret_value &= ~0x01; 538 ret_value &= ~0x01;
536 if (st1 == 0xff || st1 == (WDSD_IBM | 0x10)) 539 if (st1 == 0xff || st1 == (WDSD_IBM | 0x10))
537 ret_value &= ~0x02; 540 ret_value &= ~0x02;
538 /* Register writability test, drive 0. */ 541 /* Register writability test, drive 0. */
539 if (ret_value & 0x01) { 542 if (ret_value & 0x01) {
540 if (wdc->select) 543 if (wdc->select)
541 wdc->select(chp,0); 544 wdc->select(chp,0);
542 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 545 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
543 0, WDSD_IBM); 546 0, WDSD_IBM);
544 bus_space_write_1(wdr->cmd_iot, 547 bus_space_write_1(wdr->cmd_iot,
545 wdr->cmd_iohs[wd_cyl_lo], 0, 0x02); 548 wdr->cmd_iohs[wd_cyl_lo], 0, 0x02);
546 cl = bus_space_read_1(wdr->cmd_iot, 549 cl = bus_space_read_1(wdr->cmd_iot,
547 wdr->cmd_iohs[wd_cyl_lo], 0); 550 wdr->cmd_iohs[wd_cyl_lo], 0);
548 if (cl != 0x02) { 551 if (cl != 0x02) {
549 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: " 552 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: "
550 "got 0x%x != 0x02\n", 553 "got 0x%x != 0x02\n",
551 device_xname(chp->ch_atac->atac_dev), 554 device_xname(chp->ch_atac->atac_dev),
552 chp->ch_channel, cl), 555 chp->ch_channel, cl),
553 DEBUG_PROBE); 556 DEBUG_PROBE);
554 ret_value &= ~0x01; 557 ret_value &= ~0x01;
555 } 558 }
556 bus_space_write_1(wdr->cmd_iot, 559 bus_space_write_1(wdr->cmd_iot,
557 wdr->cmd_iohs[wd_cyl_lo], 0, 0x01); 560 wdr->cmd_iohs[wd_cyl_lo], 0, 0x01);
558 cl = bus_space_read_1(wdr->cmd_iot, 561 cl = bus_space_read_1(wdr->cmd_iot,
559 wdr->cmd_iohs[wd_cyl_lo], 0); 562 wdr->cmd_iohs[wd_cyl_lo], 0);
560 if (cl != 0x01) { 563 if (cl != 0x01) {
561 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: " 564 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo: "
562 "got 0x%x != 0x01\n", 565 "got 0x%x != 0x01\n",
563 device_xname(chp->ch_atac->atac_dev), 566 device_xname(chp->ch_atac->atac_dev),
564 chp->ch_channel, cl), 567 chp->ch_channel, cl),
565 DEBUG_PROBE); 568 DEBUG_PROBE);
566 ret_value &= ~0x01; 569 ret_value &= ~0x01;
567 } 570 }
568 bus_space_write_1(wdr->cmd_iot, 571 bus_space_write_1(wdr->cmd_iot,
569 wdr->cmd_iohs[wd_sector], 0, 0x01); 572 wdr->cmd_iohs[wd_sector], 0, 0x01);
570 cl = bus_space_read_1(wdr->cmd_iot, 573 cl = bus_space_read_1(wdr->cmd_iot,
571 wdr->cmd_iohs[wd_sector], 0); 574 wdr->cmd_iohs[wd_sector], 0);
572 if (cl != 0x01) { 575 if (cl != 0x01) {
573 ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: " 576 ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: "
574 "got 0x%x != 0x01\n", 577 "got 0x%x != 0x01\n",
575 device_xname(chp->ch_atac->atac_dev), 578 device_xname(chp->ch_atac->atac_dev),
576 chp->ch_channel, cl), 579 chp->ch_channel, cl),
577 DEBUG_PROBE); 580 DEBUG_PROBE);
578 ret_value &= ~0x01; 581 ret_value &= ~0x01;
579 } 582 }
580 bus_space_write_1(wdr->cmd_iot, 583 bus_space_write_1(wdr->cmd_iot,
581 wdr->cmd_iohs[wd_sector], 0, 0x02); 584 wdr->cmd_iohs[wd_sector], 0, 0x02);
582 cl = bus_space_read_1(wdr->cmd_iot, 585 cl = bus_space_read_1(wdr->cmd_iot,
583 wdr->cmd_iohs[wd_sector], 0); 586 wdr->cmd_iohs[wd_sector], 0);
584 if (cl != 0x02) { 587 if (cl != 0x02) {
585 ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: " 588 ATADEBUG_PRINT(("%s:%d drive 0 wd_sector: "
586 "got 0x%x != 0x02\n", 589 "got 0x%x != 0x02\n",
587 device_xname(chp->ch_atac->atac_dev), 590 device_xname(chp->ch_atac->atac_dev),
588 chp->ch_channel, cl), 591 chp->ch_channel, cl),
589 DEBUG_PROBE); 592 DEBUG_PROBE);
590 ret_value &= ~0x01; 593 ret_value &= ~0x01;
591 } 594 }
592 cl = bus_space_read_1(wdr->cmd_iot, 595 cl = bus_space_read_1(wdr->cmd_iot,
593 wdr->cmd_iohs[wd_cyl_lo], 0); 596 wdr->cmd_iohs[wd_cyl_lo], 0);
594 if (cl != 0x01) { 597 if (cl != 0x01) {
595 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo(2): " 598 ATADEBUG_PRINT(("%s:%d drive 0 wd_cyl_lo(2): "
596 "got 0x%x != 0x01\n", 599 "got 0x%x != 0x01\n",
597 device_xname(chp->ch_atac->atac_dev), 600 device_xname(chp->ch_atac->atac_dev),
598 chp->ch_channel, cl), 601 chp->ch_channel, cl),
599 DEBUG_PROBE); 602 DEBUG_PROBE);
600 ret_value &= ~0x01; 603 ret_value &= ~0x01;
601 } 604 }
602 } 605 }
603 /* Register writability test, drive 1. */ 606 /* Register writability test, drive 1. */
604 if (ret_value & 0x02) { 607 if (ret_value & 0x02) {
605 if (wdc->select) 608 if (wdc->select)
606 wdc->select(chp,1); 609 wdc->select(chp,1);
607 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 610 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
608 0, WDSD_IBM | 0x10); 611 0, WDSD_IBM | 0x10);
609 bus_space_write_1(wdr->cmd_iot, 612 bus_space_write_1(wdr->cmd_iot,
610 wdr->cmd_iohs[wd_cyl_lo], 0, 0x02); 613 wdr->cmd_iohs[wd_cyl_lo], 0, 0x02);
611 cl = bus_space_read_1(wdr->cmd_iot, 614 cl = bus_space_read_1(wdr->cmd_iot,
612 wdr->cmd_iohs[wd_cyl_lo], 0); 615 wdr->cmd_iohs[wd_cyl_lo], 0);
613 if (cl != 0x02) { 616 if (cl != 0x02) {
614 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: " 617 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: "
615 "got 0x%x != 0x02\n", 618 "got 0x%x != 0x02\n",
616 device_xname(chp->ch_atac->atac_dev), 619 device_xname(chp->ch_atac->atac_dev),
617 chp->ch_channel, cl), 620 chp->ch_channel, cl),
618 DEBUG_PROBE); 621 DEBUG_PROBE);
619 ret_value &= ~0x02; 622 ret_value &= ~0x02;
620 } 623 }
621 bus_space_write_1(wdr->cmd_iot, 624 bus_space_write_1(wdr->cmd_iot,
622 wdr->cmd_iohs[wd_cyl_lo], 0, 0x01); 625 wdr->cmd_iohs[wd_cyl_lo], 0, 0x01);
623 cl = bus_space_read_1(wdr->cmd_iot, 626 cl = bus_space_read_1(wdr->cmd_iot,
624 wdr->cmd_iohs[wd_cyl_lo], 0); 627 wdr->cmd_iohs[wd_cyl_lo], 0);
625 if (cl != 0x01) { 628 if (cl != 0x01) {
626 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: " 629 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo: "
627 "got 0x%x != 0x01\n", 630 "got 0x%x != 0x01\n",
628 device_xname(chp->ch_atac->atac_dev), 631 device_xname(chp->ch_atac->atac_dev),
629 chp->ch_channel, cl), 632 chp->ch_channel, cl),
630 DEBUG_PROBE); 633 DEBUG_PROBE);
631 ret_value &= ~0x02; 634 ret_value &= ~0x02;
632 } 635 }
633 bus_space_write_1(wdr->cmd_iot, 636 bus_space_write_1(wdr->cmd_iot,
634 wdr->cmd_iohs[wd_sector], 0, 0x01); 637 wdr->cmd_iohs[wd_sector], 0, 0x01);
635 cl = bus_space_read_1(wdr->cmd_iot, 638 cl = bus_space_read_1(wdr->cmd_iot,
636 wdr->cmd_iohs[wd_sector], 0); 639 wdr->cmd_iohs[wd_sector], 0);
637 if (cl != 0x01) { 640 if (cl != 0x01) {
638 ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: " 641 ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: "
639 "got 0x%x != 0x01\n", 642 "got 0x%x != 0x01\n",
640 device_xname(chp->ch_atac->atac_dev), 643 device_xname(chp->ch_atac->atac_dev),
641 chp->ch_channel, cl), 644 chp->ch_channel, cl),
642 DEBUG_PROBE); 645 DEBUG_PROBE);
643 ret_value &= ~0x02; 646 ret_value &= ~0x02;
644 } 647 }
645 bus_space_write_1(wdr->cmd_iot, 648 bus_space_write_1(wdr->cmd_iot,
646 wdr->cmd_iohs[wd_sector], 0, 0x02); 649 wdr->cmd_iohs[wd_sector], 0, 0x02);
647 cl = bus_space_read_1(wdr->cmd_iot, 650 cl = bus_space_read_1(wdr->cmd_iot,
648 wdr->cmd_iohs[wd_sector], 0); 651 wdr->cmd_iohs[wd_sector], 0);
649 if (cl != 0x02) { 652 if (cl != 0x02) {
650 ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: " 653 ATADEBUG_PRINT(("%s:%d drive 1 wd_sector: "
651 "got 0x%x != 0x02\n", 654 "got 0x%x != 0x02\n",
652 device_xname(chp->ch_atac->atac_dev), 655 device_xname(chp->ch_atac->atac_dev),
653 chp->ch_channel, cl), 656 chp->ch_channel, cl),
654 DEBUG_PROBE); 657 DEBUG_PROBE);
655 ret_value &= ~0x02; 658 ret_value &= ~0x02;
656 } 659 }
657 cl = bus_space_read_1(wdr->cmd_iot, 660 cl = bus_space_read_1(wdr->cmd_iot,
658 wdr->cmd_iohs[wd_cyl_lo], 0); 661 wdr->cmd_iohs[wd_cyl_lo], 0);
659 if (cl != 0x01) { 662 if (cl != 0x01) {
660 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo(2): " 663 ATADEBUG_PRINT(("%s:%d drive 1 wd_cyl_lo(2): "
661 "got 0x%x != 0x01\n", 664 "got 0x%x != 0x01\n",
662 device_xname(chp->ch_atac->atac_dev), 665 device_xname(chp->ch_atac->atac_dev),
663 chp->ch_channel, cl), 666 chp->ch_channel, cl),
664 DEBUG_PROBE); 667 DEBUG_PROBE);
665 ret_value &= ~0x02; 668 ret_value &= ~0x02;
666 } 669 }
667 } 670 }
668 671
669 if (ret_value == 0) { 672 if (ret_value == 0) {
670 splx(s); 673 splx(s);
671 return 0; 674 return 0;
672 } 675 }
673 } 676 }
674 677
675 678
676#if 0 /* XXX this break some ATA or ATAPI devices */ 679#if 0 /* XXX this break some ATA or ATAPI devices */
677 /* 680 /*
678 * reset bus. Also send an ATAPI_RESET to devices, in case there are 681 * reset bus. Also send an ATAPI_RESET to devices, in case there are
679 * ATAPI device out there which don't react to the bus reset 682 * ATAPI device out there which don't react to the bus reset
680 */ 683 */
681 if (ret_value & 0x01) { 684 if (ret_value & 0x01) {
682 if (wdc->select) 685 if (wdc->select)
683 wdc->select(chp,0); 686 wdc->select(chp,0);
684 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 687 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
685 0, WDSD_IBM); 688 0, WDSD_IBM);
686 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, 689 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0,
687 ATAPI_SOFT_RESET); 690 ATAPI_SOFT_RESET);
688 } 691 }
689 if (ret_value & 0x02) { 692 if (ret_value & 0x02) {
690 if (wdc->select) 693 if (wdc->select)
691 wdc->select(chp,0); 694 wdc->select(chp,0);
692 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 695 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
693 0, WDSD_IBM | 0x10); 696 0, WDSD_IBM | 0x10);
694 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, 697 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0,
695 ATAPI_SOFT_RESET); 698 ATAPI_SOFT_RESET);
696 } 699 }
697 700
698 delay(5000); 701 delay(5000);
699#endif 702#endif
700 703
701 wdc->reset(chp, RESET_POLL); 704 wdc->reset(chp, RESET_POLL);
702 DELAY(2000); 705 DELAY(2000);
703 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0); 706 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
704 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT); 707 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
705#ifdef WDC_NO_IDS 708#ifdef WDC_NO_IDS
706 ret_value = __wdcwait_reset(chp, ret_value, RESET_POLL); 709 ret_value = __wdcwait_reset(chp, ret_value, RESET_POLL);
707#else 710#else
708 splx(s); 711 splx(s);
709 ret_value = __wdcwait_reset(chp, ret_value, poll); 712 ret_value = __wdcwait_reset(chp, ret_value, poll);
710 s = splbio(); 713 s = splbio();
711#endif 714#endif
712 ATADEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n", 715 ATADEBUG_PRINT(("%s:%d: after reset, ret_value=0x%d\n",
713 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 716 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
714 ret_value), DEBUG_PROBE); 717 ret_value), DEBUG_PROBE);
715 718
716 /* if reset failed, there's nothing here */ 719 /* if reset failed, there's nothing here */
717 if (ret_value == 0) { 720 if (ret_value == 0) {
718 splx(s); 721 splx(s);
719 return 0; 722 return 0;
720 } 723 }
721 724
722 /* 725 /*
723 * Test presence of drives. First test register signatures looking 726 * Test presence of drives. First test register signatures looking
724 * for ATAPI devices. If it's not an ATAPI and reset said there may 727 * for ATAPI devices. If it's not an ATAPI and reset said there may
725 * be something here assume it's ATA or OLD. Ghost will be killed 728 * be something here assume it's ATA or OLD. Ghost will be killed
726 * later in attach routine. 729 * later in attach routine.
727 */ 730 */
728 for (drive = 0; drive < chp->ch_ndrive; drive++) { 731 for (drive = 0; drive < chp->ch_ndrive; drive++) {
729 if ((ret_value & (0x01 << drive)) == 0) 732 if ((ret_value & (0x01 << drive)) == 0)
730 continue; 733 continue;
731 if (wdc->select) 734 if (wdc->select)
732 wdc->select(chp,drive); 735 wdc->select(chp,drive);
733 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 736 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
734 WDSD_IBM | (drive << 4)); 737 WDSD_IBM | (drive << 4));
735 delay(10); /* 400ns delay */ 738 delay(10); /* 400ns delay */
736 /* Save registers contents */ 739 /* Save registers contents */
737 sc = bus_space_read_1(wdr->cmd_iot, 740 sc = bus_space_read_1(wdr->cmd_iot,
738 wdr->cmd_iohs[wd_seccnt], 0); 741 wdr->cmd_iohs[wd_seccnt], 0);
739 sn = bus_space_read_1(wdr->cmd_iot, 742 sn = bus_space_read_1(wdr->cmd_iot,
740 wdr->cmd_iohs[wd_sector], 0); 743 wdr->cmd_iohs[wd_sector], 0);
741 cl = bus_space_read_1(wdr->cmd_iot, 744 cl = bus_space_read_1(wdr->cmd_iot,
742 wdr->cmd_iohs[wd_cyl_lo], 0); 745 wdr->cmd_iohs[wd_cyl_lo], 0);
743 ch = bus_space_read_1(wdr->cmd_iot, 746 ch = bus_space_read_1(wdr->cmd_iot,
744 wdr->cmd_iohs[wd_cyl_hi], 0); 747 wdr->cmd_iohs[wd_cyl_hi], 0);
745 748
746 ATADEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x " 749 ATADEBUG_PRINT(("%s:%d:%d: after reset, sc=0x%x sn=0x%x "
747 "cl=0x%x ch=0x%x\n", 750 "cl=0x%x ch=0x%x\n",
748 device_xname(chp->ch_atac->atac_dev), 751 device_xname(chp->ch_atac->atac_dev),
749 chp->ch_channel, drive, sc, sn, cl, ch), DEBUG_PROBE); 752 chp->ch_channel, drive, sc, sn, cl, ch), DEBUG_PROBE);
750 /* 753 /*
751 * sc & sn are supposted to be 0x1 for ATAPI but in some cases 754 * sc & sn are supposted to be 0x1 for ATAPI but in some cases
752 * we get wrong values here, so ignore it. 755 * we get wrong values here, so ignore it.
753 */ 756 */
754 if (cl == 0x14 && ch == 0xeb) { 757 if (cl == 0x14 && ch == 0xeb) {
755 chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI; 758 chp->ch_drive[drive].drive_flags |= DRIVE_ATAPI;
756 } else { 759 } else {
757 chp->ch_drive[drive].drive_flags |= DRIVE_ATA; 760 chp->ch_drive[drive].drive_flags |= DRIVE_ATA;
758 if ((wdc->cap & WDC_CAPABILITY_PREATA) != 0) 761 if ((wdc->cap & WDC_CAPABILITY_PREATA) != 0)
759 chp->ch_drive[drive].drive_flags |= DRIVE_OLD; 762 chp->ch_drive[drive].drive_flags |= DRIVE_OLD;
760 } 763 }
761 } 764 }
762 /* 765 /*
763 * Select an existing drive before lowering spl, some WDC_NO_IDS 766 * Select an existing drive before lowering spl, some WDC_NO_IDS
764 * devices incorrectly assert IRQ on nonexistent slave 767 * devices incorrectly assert IRQ on nonexistent slave
765 */ 768 */
766 if (ret_value & 0x01) { 769 if (ret_value & 0x01) {
767 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 770 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
768 WDSD_IBM); 771 WDSD_IBM);
769 (void)bus_space_read_1(wdr->cmd_iot, 772 (void)bus_space_read_1(wdr->cmd_iot,
770 wdr->cmd_iohs[wd_status], 0); 773 wdr->cmd_iohs[wd_status], 0);
771 } 774 }
772 splx(s); 775 splx(s);
773 return (ret_value); 776 return (ret_value);
774} 777}
775 778
776void 779void
777wdcattach(struct ata_channel *chp) 780wdcattach(struct ata_channel *chp)
778{ 781{
779 struct atac_softc *atac = chp->ch_atac; 782 struct atac_softc *atac = chp->ch_atac;
780 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 783 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
781 784
782 KASSERT(chp->ch_ndrive > 0 && chp->ch_ndrive < 3); 785 KASSERT(chp->ch_ndrive > 0 && chp->ch_ndrive < 3);
783 786
784 /* default data transfer methods */ 787 /* default data transfer methods */
785 if (wdc->datain_pio == NULL) 788 if (wdc->datain_pio == NULL)
786 wdc->datain_pio = wdc_datain_pio; 789 wdc->datain_pio = wdc_datain_pio;
787 if (wdc->dataout_pio == NULL) 790 if (wdc->dataout_pio == NULL)
788 wdc->dataout_pio = wdc_dataout_pio; 791 wdc->dataout_pio = wdc_dataout_pio;
789 /* default reset method */ 792 /* default reset method */
790 if (wdc->reset == NULL) 793 if (wdc->reset == NULL)
791 wdc->reset = wdc_do_reset; 794 wdc->reset = wdc_do_reset;
792 795
793 /* initialise global data */ 796 /* initialise global data */
794 if (atac->atac_bustype_ata == NULL) 797 if (atac->atac_bustype_ata == NULL)
795 atac->atac_bustype_ata = &wdc_ata_bustype; 798 atac->atac_bustype_ata = &wdc_ata_bustype;
796 if (atac->atac_probe == NULL) 799 if (atac->atac_probe == NULL)
797 atac->atac_probe = wdc_drvprobe; 800 atac->atac_probe = wdc_drvprobe;
798#if NATAPIBUS > 0 801#if NATAPIBUS > 0
799 if (atac->atac_atapibus_attach == NULL) 802 if (atac->atac_atapibus_attach == NULL)
800 atac->atac_atapibus_attach = wdc_atapibus_attach; 803 atac->atac_atapibus_attach = wdc_atapibus_attach;
801#endif 804#endif
802 805
803 ata_channel_attach(chp); 806 ata_channel_attach(chp);
804} 807}
805 808
806void 809void
807wdc_childdetached(device_t self, device_t child) 810wdc_childdetached(device_t self, device_t child)
808{ 811{
809 struct atac_softc *atac = device_private(self); 812 struct atac_softc *atac = device_private(self);
810 struct ata_channel *chp; 813 struct ata_channel *chp;
811 int i; 814 int i;
812 815
813 for (i = 0; i < atac->atac_nchannels; i++) { 816 for (i = 0; i < atac->atac_nchannels; i++) {
814 chp = atac->atac_channels[i]; 817 chp = atac->atac_channels[i];
815 if (child == chp->atabus) { 818 if (child == chp->atabus) {
816 chp->atabus = NULL; 819 chp->atabus = NULL;
817 return; 820 return;
818 } 821 }
819 } 822 }
820} 823}
821 824
822int 825int
823wdcdetach(device_t self, int flags) 826wdcdetach(device_t self, int flags)
824{ 827{
825 struct atac_softc *atac = device_private(self); 828 struct atac_softc *atac = device_private(self);
826 struct ata_channel *chp; 829 struct ata_channel *chp;
827 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic; 830 struct scsipi_adapter *adapt = &atac->atac_atapi_adapter._generic;
828 int i, error = 0; 831 int i, error = 0;
829 832
830 for (i = 0; i < atac->atac_nchannels; i++) { 833 for (i = 0; i < atac->atac_nchannels; i++) {
831 chp = atac->atac_channels[i]; 834 chp = atac->atac_channels[i];
832 if (chp->atabus == NULL) 835 if (chp->atabus == NULL)
833 continue; 836 continue;
834 ATADEBUG_PRINT(("wdcdetach: %s: detaching %s\n", 837 ATADEBUG_PRINT(("wdcdetach: %s: detaching %s\n",
835 device_xname(atac->atac_dev), device_xname(chp->atabus)), 838 device_xname(atac->atac_dev), device_xname(chp->atabus)),
836 DEBUG_DETACH); 839 DEBUG_DETACH);
837 if ((error = config_detach(chp->atabus, flags)) != 0) 840 if ((error = config_detach(chp->atabus, flags)) != 0)
838 return error; 841 return error;
839 } 842 }
840 if (adapt->adapt_refcnt != 0) 843 if (adapt->adapt_refcnt != 0)
841 return EBUSY; 844 return EBUSY;
842 return 0; 845 return 0;
843} 846}
844 847
845/* restart an interrupted I/O */ 848/* restart an interrupted I/O */
846void 849void
847wdcrestart(void *v) 850wdcrestart(void *v)
848{ 851{
849 struct ata_channel *chp = v; 852 struct ata_channel *chp = v;
850 int s; 853 int s;
851 854
852 s = splbio(); 855 s = splbio();
853 atastart(chp); 856 atastart(chp);
854 splx(s); 857 splx(s);
855} 858}
856 859
857 860
858/* 861/*
859 * Interrupt routine for the controller. Acknowledge the interrupt, check for 862 * Interrupt routine for the controller. Acknowledge the interrupt, check for
860 * errors on the current operation, mark it done if necessary, and start the 863 * errors on the current operation, mark it done if necessary, and start the
861 * next request. Also check for a partially done transfer, and continue with 864 * next request. Also check for a partially done transfer, and continue with
862 * the next chunk if so. 865 * the next chunk if so.
863 */ 866 */
864int 867int
865wdcintr(void *arg) 868wdcintr(void *arg)
866{ 869{
867 struct ata_channel *chp = arg; 870 struct ata_channel *chp = arg;
868 struct atac_softc *atac = chp->ch_atac; 871 struct atac_softc *atac = chp->ch_atac;
869 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 872 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
870 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 873 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
871 struct ata_xfer *xfer; 874 struct ata_xfer *xfer;
872 int ret; 875 int ret;
873 876
874 if (!device_is_active(atac->atac_dev)) { 877 if (!device_is_active(atac->atac_dev)) {
875 ATADEBUG_PRINT(("wdcintr: deactivated controller\n"), 878 ATADEBUG_PRINT(("wdcintr: deactivated controller\n"),
876 DEBUG_INTR); 879 DEBUG_INTR);
877 return (0); 880 return (0);
878 } 881 }
879 if ((chp->ch_flags & ATACH_IRQ_WAIT) == 0) { 882 if ((chp->ch_flags & ATACH_IRQ_WAIT) == 0) {
880 ATADEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR); 883 ATADEBUG_PRINT(("wdcintr: inactive controller\n"), DEBUG_INTR);
881 /* try to clear the pending interrupt anyway */ 884 /* try to clear the pending interrupt anyway */
882 (void)bus_space_read_1(wdr->cmd_iot, 885 (void)bus_space_read_1(wdr->cmd_iot,
883 wdr->cmd_iohs[wd_status], 0); 886 wdr->cmd_iohs[wd_status], 0);
884 return (0); 887 return (0);
885 } 888 }
886 889
887 ATADEBUG_PRINT(("wdcintr\n"), DEBUG_INTR); 890 ATADEBUG_PRINT(("wdcintr\n"), DEBUG_INTR);
888 xfer = chp->ch_queue->active_xfer; 891 xfer = chp->ch_queue->active_xfer;
889#ifdef DIAGNOSTIC 892#ifdef DIAGNOSTIC
890 if (xfer == NULL) 893 if (xfer == NULL)
891 panic("wdcintr: no xfer"); 894 panic("wdcintr: no xfer");
892 if (xfer->c_chp != chp) { 895 if (xfer->c_chp != chp) {
893 printf("channel %d expected %d\n", xfer->c_chp->ch_channel, 896 printf("channel %d expected %d\n", xfer->c_chp->ch_channel,
894 chp->ch_channel); 897 chp->ch_channel);
895 panic("wdcintr: wrong channel"); 898 panic("wdcintr: wrong channel");
896 } 899 }
897#endif 900#endif
898#if NATA_DMA || NATA_PIOBM 901#if NATA_DMA || NATA_PIOBM
899 if (chp->ch_flags & ATACH_DMA_WAIT) { 902 if (chp->ch_flags & ATACH_DMA_WAIT) {
900 wdc->dma_status = 903 wdc->dma_status =
901 (*wdc->dma_finish)(wdc->dma_arg, chp->ch_channel, 904 (*wdc->dma_finish)(wdc->dma_arg, chp->ch_channel,
902 xfer->c_drive, WDC_DMAEND_END); 905 xfer->c_drive, WDC_DMAEND_END);
903 if (wdc->dma_status & WDC_DMAST_NOIRQ) { 906 if (wdc->dma_status & WDC_DMAST_NOIRQ) {
904 /* IRQ not for us, not detected by DMA engine */ 907 /* IRQ not for us, not detected by DMA engine */
905 return 0; 908 return 0;
906 } 909 }
907 chp->ch_flags &= ~ATACH_DMA_WAIT; 910 chp->ch_flags &= ~ATACH_DMA_WAIT;
908 } 911 }
909#endif 912#endif
910 chp->ch_flags &= ~ATACH_IRQ_WAIT; 913 chp->ch_flags &= ~ATACH_IRQ_WAIT;
911 KASSERT(xfer->c_intr != NULL); 914 KASSERT(xfer->c_intr != NULL);
912 ret = xfer->c_intr(chp, xfer, 1); 915 ret = xfer->c_intr(chp, xfer, 1);
913 if (ret == 0) /* irq was not for us, still waiting for irq */ 916 if (ret == 0) /* irq was not for us, still waiting for irq */
914 chp->ch_flags |= ATACH_IRQ_WAIT; 917 chp->ch_flags |= ATACH_IRQ_WAIT;
915 return (ret); 918 return (ret);
916} 919}
917 920
918/* Put all disk in RESET state */ 921/* Put all disk in RESET state */
919void 922void
920wdc_reset_drive(struct ata_drive_datas *drvp, int flags) 923wdc_reset_drive(struct ata_drive_datas *drvp, int flags)
921{ 924{
922 struct ata_channel *chp = drvp->chnl_softc; 925 struct ata_channel *chp = drvp->chnl_softc;
923 926
924 ATADEBUG_PRINT(("wdc_reset_drive %s:%d for drive %d\n", 927 ATADEBUG_PRINT(("wdc_reset_drive %s:%d for drive %d\n",
925 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 928 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
926 drvp->drive), DEBUG_FUNCS); 929 drvp->drive), DEBUG_FUNCS);
927 930
928 ata_reset_channel(chp, flags); 931 ata_reset_channel(chp, flags);
929} 932}
930 933
931void 934void
932wdc_reset_channel(struct ata_channel *chp, int flags) 935wdc_reset_channel(struct ata_channel *chp, int flags)
933{ 936{
934 TAILQ_HEAD(, ata_xfer) reset_xfer; 937 TAILQ_HEAD(, ata_xfer) reset_xfer;
935 struct ata_xfer *xfer, *next_xfer; 938 struct ata_xfer *xfer, *next_xfer;
936#if NATA_DMA || NATA_PIOBM 939#if NATA_DMA || NATA_PIOBM
937 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 940 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
938#endif 941#endif
939 TAILQ_INIT(&reset_xfer); 942 TAILQ_INIT(&reset_xfer);
940 943
941 chp->ch_flags &= ~ATACH_IRQ_WAIT; 944 chp->ch_flags &= ~ATACH_IRQ_WAIT;
942 945
943 /* 946 /*
944 * if the current command if on an ATAPI device, issue a 947 * if the current command if on an ATAPI device, issue a
945 * ATAPI_SOFT_RESET 948 * ATAPI_SOFT_RESET
946 */ 949 */
947 xfer = chp->ch_queue->active_xfer; 950 xfer = chp->ch_queue->active_xfer;
948 if (xfer && xfer->c_chp == chp && (xfer->c_flags & C_ATAPI)) { 951 if (xfer && xfer->c_chp == chp && (xfer->c_flags & C_ATAPI)) {
949 wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET); 952 wdccommandshort(chp, xfer->c_drive, ATAPI_SOFT_RESET);
950 if (flags & AT_WAIT) 953 if (flags & AT_WAIT)
951 tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1); 954 tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1);
952 else 955 else
953 delay(1000); 956 delay(1000);
954 } 957 }
955 958
956 /* reset the channel */ 959 /* reset the channel */
957 if (flags & AT_WAIT) 960 if (flags & AT_WAIT)
958 (void) wdcreset(chp, RESET_SLEEP); 961 (void) wdcreset(chp, RESET_SLEEP);
959 else 962 else
960 (void) wdcreset(chp, RESET_POLL); 963 (void) wdcreset(chp, RESET_POLL);
961 964
962 /* 965 /*
963 * wait a bit after reset; in case the DMA engines needs some time 966 * wait a bit after reset; in case the DMA engines needs some time
964 * to recover. 967 * to recover.
965 */ 968 */
966 if (flags & AT_WAIT) 969 if (flags & AT_WAIT)
967 tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1); 970 tsleep(&flags, PRIBIO, "atardl", mstohz(1) + 1);
968 else 971 else
969 delay(1000); 972 delay(1000);
970 /* 973 /*
971 * look for pending xfers. If we have a shared queue, we'll also reset 974 * look for pending xfers. If we have a shared queue, we'll also reset
972 * the other channel if the current xfer is running on it. 975 * the other channel if the current xfer is running on it.
973 * Then we'll dequeue only the xfers for this channel. 976 * Then we'll dequeue only the xfers for this channel.
974 */ 977 */
975 if ((flags & AT_RST_NOCMD) == 0) { 978 if ((flags & AT_RST_NOCMD) == 0) {
976 /* 979 /*
977 * move all xfers queued for this channel to the reset queue, 980 * move all xfers queued for this channel to the reset queue,
978 * and then process the current xfer and then the reset queue. 981 * and then process the current xfer and then the reset queue.
979 * We have to use a temporary queue because c_kill_xfer() 982 * We have to use a temporary queue because c_kill_xfer()
980 * may requeue commands. 983 * may requeue commands.
981 */ 984 */
982 for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer); 985 for (xfer = TAILQ_FIRST(&chp->ch_queue->queue_xfer);
983 xfer != NULL; xfer = next_xfer) { 986 xfer != NULL; xfer = next_xfer) {
984 next_xfer = TAILQ_NEXT(xfer, c_xferchain); 987 next_xfer = TAILQ_NEXT(xfer, c_xferchain);
985 if (xfer->c_chp != chp) 988 if (xfer->c_chp != chp)
986 continue; 989 continue;
987 TAILQ_REMOVE(&chp->ch_queue->queue_xfer, 990 TAILQ_REMOVE(&chp->ch_queue->queue_xfer,
988 xfer, c_xferchain); 991 xfer, c_xferchain);
989 TAILQ_INSERT_TAIL(&reset_xfer, xfer, c_xferchain); 992 TAILQ_INSERT_TAIL(&reset_xfer, xfer, c_xferchain);
990 } 993 }
991 xfer = chp->ch_queue->active_xfer; 994 xfer = chp->ch_queue->active_xfer;
992 if (xfer) { 995 if (xfer) {
993 if (xfer->c_chp != chp) 996 if (xfer->c_chp != chp)
994 ata_reset_channel(xfer->c_chp, flags); 997 ata_reset_channel(xfer->c_chp, flags);
995 else { 998 else {
996 callout_stop(&chp->ch_callout); 999 callout_stop(&chp->ch_callout);
997#if NATA_DMA || NATA_PIOBM 1000#if NATA_DMA || NATA_PIOBM
998 /* 1001 /*
999 * If we're waiting for DMA, stop the 1002 * If we're waiting for DMA, stop the
1000 * DMA engine 1003 * DMA engine
1001 */ 1004 */
1002 if (chp->ch_flags & ATACH_DMA_WAIT) { 1005 if (chp->ch_flags & ATACH_DMA_WAIT) {
1003 (*wdc->dma_finish)( 1006 (*wdc->dma_finish)(
1004 wdc->dma_arg, 1007 wdc->dma_arg,
1005 chp->ch_channel, 1008 chp->ch_channel,
1006 xfer->c_drive, 1009 xfer->c_drive,
1007 WDC_DMAEND_ABRT_QUIET); 1010 WDC_DMAEND_ABRT_QUIET);
1008 chp->ch_flags &= ~ATACH_DMA_WAIT; 1011 chp->ch_flags &= ~ATACH_DMA_WAIT;
1009 } 1012 }
1010#endif 1013#endif
1011 chp->ch_queue->active_xfer = NULL; 1014 chp->ch_queue->active_xfer = NULL;
1012 if ((flags & AT_RST_EMERG) == 0) 1015 if ((flags & AT_RST_EMERG) == 0)
1013 xfer->c_kill_xfer( 1016 xfer->c_kill_xfer(
1014 chp, xfer, KILL_RESET); 1017 chp, xfer, KILL_RESET);
1015 } 1018 }
1016 } 1019 }
1017 1020
1018 for (xfer = TAILQ_FIRST(&reset_xfer); 1021 for (xfer = TAILQ_FIRST(&reset_xfer);
1019 xfer != NULL; xfer = next_xfer) { 1022 xfer != NULL; xfer = next_xfer) {
1020 next_xfer = TAILQ_NEXT(xfer, c_xferchain); 1023 next_xfer = TAILQ_NEXT(xfer, c_xferchain);
1021 TAILQ_REMOVE(&reset_xfer, xfer, c_xferchain); 1024 TAILQ_REMOVE(&reset_xfer, xfer, c_xferchain);
1022 if ((flags & AT_RST_EMERG) == 0) 1025 if ((flags & AT_RST_EMERG) == 0)
1023 xfer->c_kill_xfer(chp, xfer, KILL_RESET); 1026 xfer->c_kill_xfer(chp, xfer, KILL_RESET);
1024 } 1027 }
1025 } 1028 }
1026} 1029}
1027 1030
1028static int 1031static int
1029wdcreset(struct ata_channel *chp, int poll) 1032wdcreset(struct ata_channel *chp, int poll)
1030{ 1033{
1031 struct atac_softc *atac = chp->ch_atac; 1034 struct atac_softc *atac = chp->ch_atac;
1032 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1035 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1033 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1036 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1034 int drv_mask1, drv_mask2; 1037 int drv_mask1, drv_mask2;
1035 1038
1036#ifdef WDC_NO_IDS 1039#ifdef WDC_NO_IDS
1037 poll = RESET_POLL; 1040 poll = RESET_POLL;
1038#endif 1041#endif
1039 wdc->reset(chp, poll); 1042 wdc->reset(chp, poll);
1040 1043
1041 drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00; 1044 drv_mask1 = (chp->ch_drive[0].drive_flags & DRIVE) ? 0x01:0x00;
1042 drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00; 1045 drv_mask1 |= (chp->ch_drive[1].drive_flags & DRIVE) ? 0x02:0x00;
1043 drv_mask2 = __wdcwait_reset(chp, drv_mask1, 1046 drv_mask2 = __wdcwait_reset(chp, drv_mask1,
1044 (poll == RESET_SLEEP) ? 0 : 1); 1047 (poll == RESET_SLEEP) ? 0 : 1);
1045 if (drv_mask2 != drv_mask1) { 1048 if (drv_mask2 != drv_mask1) {
1046 aprint_error("%s channel %d: reset failed for", 1049 aprint_error("%s channel %d: reset failed for",
1047 device_xname(atac->atac_dev), chp->ch_channel); 1050 device_xname(atac->atac_dev), chp->ch_channel);
1048 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0) 1051 if ((drv_mask1 & 0x01) != 0 && (drv_mask2 & 0x01) == 0)
1049 aprint_normal(" drive 0"); 1052 aprint_normal(" drive 0");
1050 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0) 1053 if ((drv_mask1 & 0x02) != 0 && (drv_mask2 & 0x02) == 0)
1051 aprint_normal(" drive 1"); 1054 aprint_normal(" drive 1");
1052 aprint_normal("\n"); 1055 aprint_normal("\n");
1053 } 1056 }
1054 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT); 1057 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_4BIT);
1055 return (drv_mask1 != drv_mask2) ? 1 : 0; 1058 return (drv_mask1 != drv_mask2) ? 1 : 0;
1056} 1059}
1057 1060
1058void 1061void
1059wdc_do_reset(struct ata_channel *chp, int poll) 1062wdc_do_reset(struct ata_channel *chp, int poll)
1060{ 1063{
1061 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1064 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1062 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1065 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1063 int s = 0; 1066 int s = 0;
1064 1067
1065 if (poll != RESET_SLEEP) 1068 if (poll != RESET_SLEEP)
1066 s = splbio(); 1069 s = splbio();
1067 if (wdc->select) 1070 if (wdc->select)
1068 wdc->select(chp,0); 1071 wdc->select(chp,0);
1069 /* master */ 1072 /* master */
1070 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM); 1073 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, WDSD_IBM);
1071 delay(10); /* 400ns delay */ 1074 delay(10); /* 400ns delay */
1072 /* assert SRST, wait for reset to complete */ 1075 /* assert SRST, wait for reset to complete */
1073 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1076 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
1074 WDCTL_RST | WDCTL_IDS | WDCTL_4BIT); 1077 WDCTL_RST | WDCTL_IDS | WDCTL_4BIT);
1075 delay(2000); 1078 delay(2000);
1076 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0); 1079 (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
1077 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1080 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
1078 WDCTL_4BIT | WDCTL_IDS); 1081 WDCTL_4BIT | WDCTL_IDS);
1079 delay(10); /* 400ns delay */ 1082 delay(10); /* 400ns delay */
1080 if (poll != RESET_SLEEP) { 1083 if (poll != RESET_SLEEP) {
1081 /* ACK interrupt in case there is one pending left */ 1084 /* ACK interrupt in case there is one pending left */
1082 if (wdc->irqack) 1085 if (wdc->irqack)
1083 wdc->irqack(chp); 1086 wdc->irqack(chp);
1084 splx(s); 1087 splx(s);
1085 } 1088 }
1086} 1089}
1087 1090
1088static int 1091static int
1089__wdcwait_reset(struct ata_channel *chp, int drv_mask, int poll) 1092__wdcwait_reset(struct ata_channel *chp, int drv_mask, int poll)
1090{ 1093{
1091 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1094 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1092 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1095 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1093 int timeout, nloop; 1096 int timeout, nloop;
1094 u_int8_t st0 = 0, st1 = 0; 1097 u_int8_t st0 = 0, st1 = 0;
1095#ifdef ATADEBUG 1098#ifdef ATADEBUG
1096 u_int8_t sc0 = 0, sn0 = 0, cl0 = 0, ch0 = 0; 1099 u_int8_t sc0 = 0, sn0 = 0, cl0 = 0, ch0 = 0;
1097 u_int8_t sc1 = 0, sn1 = 0, cl1 = 0, ch1 = 0; 1100 u_int8_t sc1 = 0, sn1 = 0, cl1 = 0, ch1 = 0;
1098#endif 1101#endif
1099 KASSERT(poll == 1); 1102 KASSERT(poll == RESET_POLL);
1100 if (poll) 1103 if (poll)
1101 nloop = WDCNDELAY_RST; 1104 nloop = WDCNDELAY_RST;
1102 else 1105 else
1103 nloop = WDC_RESET_WAIT * hz / 1000; 1106 nloop = WDC_RESET_WAIT * hz / 1000;
1104 /* wait for BSY to deassert */ 1107 /* wait for BSY to deassert */
1105 for (timeout = 0; timeout < nloop; timeout++) { 1108 for (timeout = 0; timeout < nloop; timeout++) {
1106 if ((drv_mask & 0x01) != 0) { 1109 if ((drv_mask & 0x01) != 0) {
1107 if (wdc->select) 1110 if (wdc->select)
1108 wdc->select(chp,0); 1111 wdc->select(chp,0);
1109 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 1112 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
1110 0, WDSD_IBM); /* master */ 1113 0, WDSD_IBM); /* master */
1111 delay(10); 1114 delay(10);
1112 st0 = bus_space_read_1(wdr->cmd_iot, 1115 st0 = bus_space_read_1(wdr->cmd_iot,
1113 wdr->cmd_iohs[wd_status], 0); 1116 wdr->cmd_iohs[wd_status], 0);
1114#ifdef ATADEBUG 1117#ifdef ATADEBUG
1115 sc0 = bus_space_read_1(wdr->cmd_iot, 1118 sc0 = bus_space_read_1(wdr->cmd_iot,
1116 wdr->cmd_iohs[wd_seccnt], 0); 1119 wdr->cmd_iohs[wd_seccnt], 0);
1117 sn0 = bus_space_read_1(wdr->cmd_iot, 1120 sn0 = bus_space_read_1(wdr->cmd_iot,
1118 wdr->cmd_iohs[wd_sector], 0); 1121 wdr->cmd_iohs[wd_sector], 0);
1119 cl0 = bus_space_read_1(wdr->cmd_iot, 1122 cl0 = bus_space_read_1(wdr->cmd_iot,
1120 wdr->cmd_iohs[wd_cyl_lo], 0); 1123 wdr->cmd_iohs[wd_cyl_lo], 0);
1121 ch0 = bus_space_read_1(wdr->cmd_iot, 1124 ch0 = bus_space_read_1(wdr->cmd_iot,
1122 wdr->cmd_iohs[wd_cyl_hi], 0); 1125 wdr->cmd_iohs[wd_cyl_hi], 0);
1123#endif 1126#endif
1124 } 1127 }
1125 if ((drv_mask & 0x02) != 0) { 1128 if ((drv_mask & 0x02) != 0) {
1126 if (wdc->select) 1129 if (wdc->select)
1127 wdc->select(chp,1); 1130 wdc->select(chp,1);
1128 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 1131 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh],
1129 0, WDSD_IBM | 0x10); /* slave */ 1132 0, WDSD_IBM | 0x10); /* slave */
1130 delay(10); 1133 delay(10);
1131 st1 = bus_space_read_1(wdr->cmd_iot, 1134 st1 = bus_space_read_1(wdr->cmd_iot,
1132 wdr->cmd_iohs[wd_status], 0); 1135 wdr->cmd_iohs[wd_status], 0);
1133#ifdef ATADEBUG 1136#ifdef ATADEBUG
1134 sc1 = bus_space_read_1(wdr->cmd_iot, 1137 sc1 = bus_space_read_1(wdr->cmd_iot,
1135 wdr->cmd_iohs[wd_seccnt], 0); 1138 wdr->cmd_iohs[wd_seccnt], 0);
1136 sn1 = bus_space_read_1(wdr->cmd_iot, 1139 sn1 = bus_space_read_1(wdr->cmd_iot,
1137 wdr->cmd_iohs[wd_sector], 0); 1140 wdr->cmd_iohs[wd_sector], 0);
1138 cl1 = bus_space_read_1(wdr->cmd_iot, 1141 cl1 = bus_space_read_1(wdr->cmd_iot,
1139 wdr->cmd_iohs[wd_cyl_lo], 0); 1142 wdr->cmd_iohs[wd_cyl_lo], 0);
1140 ch1 = bus_space_read_1(wdr->cmd_iot, 1143 ch1 = bus_space_read_1(wdr->cmd_iot,
1141 wdr->cmd_iohs[wd_cyl_hi], 0); 1144 wdr->cmd_iohs[wd_cyl_hi], 0);
1142#endif 1145#endif
1143 } 1146 }
1144 1147
1145 if ((drv_mask & 0x01) == 0) { 1148 if ((drv_mask & 0x01) == 0) {
1146 /* no master */ 1149 /* no master */
1147 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) { 1150 if ((drv_mask & 0x02) != 0 && (st1 & WDCS_BSY) == 0) {
1148 /* No master, slave is ready, it's done */ 1151 /* No master, slave is ready, it's done */
1149 goto end; 1152 goto end;
1150 } 1153 }
1151 if ((drv_mask & 0x02) == 0) { 1154 if ((drv_mask & 0x02) == 0) {
1152 /* No master, no slave: it's done */ 1155 /* No master, no slave: it's done */
1153 goto end; 1156 goto end;
1154 } 1157 }
1155 } else if ((drv_mask & 0x02) == 0) { 1158 } else if ((drv_mask & 0x02) == 0) {
1156 /* no slave */ 1159 /* no slave */
1157 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) { 1160 if ((drv_mask & 0x01) != 0 && (st0 & WDCS_BSY) == 0) {
1158 /* No slave, master is ready, it's done */ 1161 /* No slave, master is ready, it's done */
1159 goto end; 1162 goto end;
1160 } 1163 }
1161 } else { 1164 } else {
1162 /* Wait for both master and slave to be ready */ 1165 /* Wait for both master and slave to be ready */
1163 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) { 1166 if ((st0 & WDCS_BSY) == 0 && (st1 & WDCS_BSY) == 0) {
1164 goto end; 1167 goto end;
1165 } 1168 }
1166 } 1169 }
1167 if (poll) 1170 if (poll)
1168 delay(WDCDELAY); 1171 delay(WDCDELAY);
1169 else 1172 else
1170 tsleep(&nloop, PRIBIO, "atarst", 1); 1173 tsleep(&nloop, PRIBIO, "atarst", 1);
1171 } 1174 }
1172 /* Reset timed out. Maybe it's because drv_mask was not right */ 1175 /* Reset timed out. Maybe it's because drv_mask was not right */
1173 if (st0 & WDCS_BSY) 1176 if (st0 & WDCS_BSY)
1174 drv_mask &= ~0x01; 1177 drv_mask &= ~0x01;
1175 if (st1 & WDCS_BSY) 1178 if (st1 & WDCS_BSY)
1176 drv_mask &= ~0x02; 1179 drv_mask &= ~0x02;
1177end: 1180end:
1178 ATADEBUG_PRINT(("%s:%d:0: after reset, sc=0x%x sn=0x%x " 1181 ATADEBUG_PRINT(("%s:%d:0: after reset, sc=0x%x sn=0x%x "
1179 "cl=0x%x ch=0x%x\n", 1182 "cl=0x%x ch=0x%x\n",
1180 device_xname(chp->ch_atac->atac_dev), 1183 device_xname(chp->ch_atac->atac_dev),
1181 chp->ch_channel, sc0, sn0, cl0, ch0), DEBUG_PROBE); 1184 chp->ch_channel, sc0, sn0, cl0, ch0), DEBUG_PROBE);
1182 ATADEBUG_PRINT(("%s:%d:1: after reset, sc=0x%x sn=0x%x " 1185 ATADEBUG_PRINT(("%s:%d:1: after reset, sc=0x%x sn=0x%x "
1183 "cl=0x%x ch=0x%x\n", 1186 "cl=0x%x ch=0x%x\n",
1184 device_xname(chp->ch_atac->atac_dev), 1187 device_xname(chp->ch_atac->atac_dev),
1185 chp->ch_channel, sc1, sn1, cl1, ch1), DEBUG_PROBE); 1188 chp->ch_channel, sc1, sn1, cl1, ch1), DEBUG_PROBE);
1186 1189
1187 ATADEBUG_PRINT(("%s:%d: wdcwait_reset() end, st0=0x%x st1=0x%x\n", 1190 ATADEBUG_PRINT(("%s:%d: wdcwait_reset() end, st0=0x%x st1=0x%x\n",
1188 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1191 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
1189 st0, st1), DEBUG_PROBE); 1192 st0, st1), DEBUG_PROBE);
1190 1193
1191 return drv_mask; 1194 return drv_mask;
1192} 1195}
1193 1196
1194/* 1197/*
1195 * Wait for a drive to be !BSY, and have mask in its status register. 1198 * Wait for a drive to be !BSY, and have mask in its status register.
1196 * return -1 for a timeout after "timeout" ms. 1199 * return -1 for a timeout after "timeout" ms.
1197 */ 1200 */
1198static int 1201static int
1199__wdcwait(struct ata_channel *chp, int mask, int bits, int timeout) 1202__wdcwait(struct ata_channel *chp, int mask, int bits, int timeout)
1200{ 1203{
1201 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1204 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1202 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1205 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1203 u_char status; 1206 u_char status;
1204 int xtime = 0; 1207 int xtime = 0;
1205 1208
1206 ATADEBUG_PRINT(("__wdcwait %s:%d\n", 1209 ATADEBUG_PRINT(("__wdcwait %s:%d\n",
1207 device_xname(chp->ch_atac->atac_dev), 1210 device_xname(chp->ch_atac->atac_dev),
1208 chp->ch_channel), DEBUG_STATUS); 1211 chp->ch_channel), DEBUG_STATUS);
1209 chp->ch_error = 0; 1212 chp->ch_error = 0;
1210 1213
1211 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */ 1214 timeout = timeout * 1000 / WDCDELAY; /* delay uses microseconds */
1212 1215
1213 for (;;) { 1216 for (;;) {
1214 chp->ch_status = status = 1217 chp->ch_status = status =
1215 bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0); 1218 bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_status], 0);
1216 if ((status & (WDCS_BSY | mask)) == bits) 1219 if ((status & (WDCS_BSY | mask)) == bits)
1217 break; 1220 break;
1218 if (++xtime > timeout) { 1221 if (++xtime > timeout) {
1219 ATADEBUG_PRINT(("__wdcwait: timeout (time=%d), " 1222 ATADEBUG_PRINT(("__wdcwait: timeout (time=%d), "
1220 "status %x error %x (mask 0x%x bits 0x%x)\n", 1223 "status %x error %x (mask 0x%x bits 0x%x)\n",
1221 xtime, status, 1224 xtime, status,
1222 bus_space_read_1(wdr->cmd_iot, 1225 bus_space_read_1(wdr->cmd_iot,
1223 wdr->cmd_iohs[wd_error], 0), mask, bits), 1226 wdr->cmd_iohs[wd_error], 0), mask, bits),
1224 DEBUG_STATUS | DEBUG_PROBE | DEBUG_DELAY); 1227 DEBUG_STATUS | DEBUG_PROBE | DEBUG_DELAY);
1225 return(WDCWAIT_TOUT); 1228 return(WDCWAIT_TOUT);
1226 } 1229 }
1227 delay(WDCDELAY); 1230 delay(WDCDELAY);
1228 } 1231 }
1229#ifdef ATADEBUG 1232#ifdef ATADEBUG
1230 if (xtime > 0 && (atadebug_mask & DEBUG_DELAY)) 1233 if (xtime > 0 && (atadebug_mask & DEBUG_DELAY))
1231 printf("__wdcwait: did busy-wait, time=%d\n", xtime); 1234 printf("__wdcwait: did busy-wait, time=%d\n", xtime);
1232#endif 1235#endif
1233 if (status & WDCS_ERR) 1236 if (status & WDCS_ERR)
1234 chp->ch_error = bus_space_read_1(wdr->cmd_iot, 1237 chp->ch_error = bus_space_read_1(wdr->cmd_iot,
1235 wdr->cmd_iohs[wd_error], 0); 1238 wdr->cmd_iohs[wd_error], 0);
1236#ifdef WDCNDELAY_DEBUG 1239#ifdef WDCNDELAY_DEBUG
1237 /* After autoconfig, there should be no long delays. */ 1240 /* After autoconfig, there should be no long delays. */
1238 if (!cold && xtime > WDCNDELAY_DEBUG) { 1241 if (!cold && xtime > WDCNDELAY_DEBUG) {
1239 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1242 struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1240 if (xfer == NULL) 1243 if (xfer == NULL)
1241 printf("%s channel %d: warning: busy-wait took %dus\n", 1244 printf("%s channel %d: warning: busy-wait took %dus\n",
1242 device_xname(chp->ch_atac->atac_dev), 1245 device_xname(chp->ch_atac->atac_dev),
1243 chp->ch_channel, WDCDELAY * xtime); 1246 chp->ch_channel, WDCDELAY * xtime);
1244 else 1247 else
1245 printf("%s:%d:%d: warning: busy-wait took %dus\n", 1248 printf("%s:%d:%d: warning: busy-wait took %dus\n",
1246 device_xname(chp->ch_atac->atac_dev), 1249 device_xname(chp->ch_atac->atac_dev),
1247 chp->ch_channel, xfer->c_drive, 1250 chp->ch_channel, xfer->c_drive,
1248 WDCDELAY * xtime); 1251 WDCDELAY * xtime);
1249 } 1252 }
1250#endif 1253#endif
1251 return(WDCWAIT_OK); 1254 return(WDCWAIT_OK);
1252} 1255}
1253 1256
1254/* 1257/*
1255 * Call __wdcwait(), polling using tsleep() or waking up the kernel 1258 * Call __wdcwait(), polling using tsleep() or waking up the kernel
1256 * thread if possible 1259 * thread if possible
1257 */ 1260 */
1258int 1261int
1259wdcwait(struct ata_channel *chp, int mask, int bits, int timeout, int flags) 1262wdcwait(struct ata_channel *chp, int mask, int bits, int timeout, int flags)
1260{ 1263{
1261 int error, i, timeout_hz = mstohz(timeout); 1264 int error, i, timeout_hz = mstohz(timeout);
1262 1265
1263 if (timeout_hz == 0 || 1266 if (timeout_hz == 0 ||
1264 (flags & (AT_WAIT | AT_POLL)) == AT_POLL) 1267 (flags & (AT_WAIT | AT_POLL)) == AT_POLL)
1265 error = __wdcwait(chp, mask, bits, timeout); 1268 error = __wdcwait(chp, mask, bits, timeout);
1266 else { 1269 else {
1267 error = __wdcwait(chp, mask, bits, WDCDELAY_POLL); 1270 error = __wdcwait(chp, mask, bits, WDCDELAY_POLL);
1268 if (error != 0) { 1271 if (error != 0) {
1269 if ((chp->ch_flags & ATACH_TH_RUN) || 1272 if ((chp->ch_flags & ATACH_TH_RUN) ||
1270 (flags & AT_WAIT)) { 1273 (flags & AT_WAIT)) {
1271 /* 1274 /*
1272 * we're running in the channel thread 1275 * we're running in the channel thread
1273 * or some userland thread context 1276 * or some userland thread context
1274 */ 1277 */
1275 for (i = 0; i < timeout_hz; i++) { 1278 for (i = 0; i < timeout_hz; i++) {
1276 if (__wdcwait(chp, mask, bits, 1279 if (__wdcwait(chp, mask, bits,
1277 WDCDELAY_POLL) == 0) { 1280 WDCDELAY_POLL) == 0) {
1278 error = 0; 1281 error = 0;
1279 break; 1282 break;
1280 } 1283 }
1281 tsleep(&chp, PRIBIO, "atapoll", 1); 1284 tsleep(&chp, PRIBIO, "atapoll", 1);
1282 } 1285 }
1283 } else { 1286 } else {
1284 /* 1287 /*
1285 * we're probably in interrupt context, 1288 * we're probably in interrupt context,
1286 * ask the thread to come back here 1289 * ask the thread to come back here
1287 */ 1290 */
1288#ifdef DIAGNOSTIC 1291#ifdef DIAGNOSTIC
1289 if (chp->ch_queue->queue_freeze > 0) 1292 if (chp->ch_queue->queue_freeze > 0)
1290 panic("wdcwait: queue_freeze"); 1293 panic("wdcwait: queue_freeze");
1291#endif 1294#endif
1292 chp->ch_queue->queue_freeze++; 1295 chp->ch_queue->queue_freeze++;
1293 wakeup(&chp->ch_thread); 1296 wakeup(&chp->ch_thread);
1294 return(WDCWAIT_THR); 1297 return(WDCWAIT_THR);
1295 } 1298 }
1296 } 1299 }
1297 } 1300 }
1298 return (error); 1301 return (error);
1299} 1302}
1300 1303
1301 1304
1302#if NATA_DMA 1305#if NATA_DMA
1303/* 1306/*
1304 * Busy-wait for DMA to complete 1307 * Busy-wait for DMA to complete
1305 */ 1308 */
1306int 1309int
1307wdc_dmawait(struct ata_channel *chp, struct ata_xfer *xfer, int timeout) 1310wdc_dmawait(struct ata_channel *chp, struct ata_xfer *xfer, int timeout)
1308{ 1311{
1309 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1312 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1310 int xtime; 1313 int xtime;
1311 1314
1312 for (xtime = 0; xtime < timeout * 1000 / WDCDELAY; xtime++) { 1315 for (xtime = 0; xtime < timeout * 1000 / WDCDELAY; xtime++) {
1313 wdc->dma_status = 1316 wdc->dma_status =
1314 (*wdc->dma_finish)(wdc->dma_arg, 1317 (*wdc->dma_finish)(wdc->dma_arg,
1315 chp->ch_channel, xfer->c_drive, WDC_DMAEND_END); 1318 chp->ch_channel, xfer->c_drive, WDC_DMAEND_END);
1316 if ((wdc->dma_status & WDC_DMAST_NOIRQ) == 0) 1319 if ((wdc->dma_status & WDC_DMAST_NOIRQ) == 0)
1317 return 0; 1320 return 0;
1318 delay(WDCDELAY); 1321 delay(WDCDELAY);
1319 } 1322 }
1320 /* timeout, force a DMA halt */ 1323 /* timeout, force a DMA halt */
1321 wdc->dma_status = (*wdc->dma_finish)(wdc->dma_arg, 1324 wdc->dma_status = (*wdc->dma_finish)(wdc->dma_arg,
1322 chp->ch_channel, xfer->c_drive, WDC_DMAEND_ABRT); 1325 chp->ch_channel, xfer->c_drive, WDC_DMAEND_ABRT);
1323 return 1; 1326 return 1;
1324} 1327}
1325#endif 1328#endif
1326 1329
1327void 1330void
1328wdctimeout(void *arg) 1331wdctimeout(void *arg)
1329{ 1332{
1330 struct ata_channel *chp = (struct ata_channel *)arg; 1333 struct ata_channel *chp = (struct ata_channel *)arg;
1331#if NATA_DMA || NATA_PIOBM 1334#if NATA_DMA || NATA_PIOBM
1332 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1335 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1333#endif 1336#endif
1334 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1337 struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1335 int s; 1338 int s;
1336 1339
1337 ATADEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS); 1340 ATADEBUG_PRINT(("wdctimeout\n"), DEBUG_FUNCS);
1338 1341
1339 s = splbio(); 1342 s = splbio();
1340 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) { 1343 if ((chp->ch_flags & ATACH_IRQ_WAIT) != 0) {
1341 __wdcerror(chp, "lost interrupt"); 1344 __wdcerror(chp, "lost interrupt");
1342 printf("\ttype: %s tc_bcount: %d tc_skip: %d\n", 1345 printf("\ttype: %s tc_bcount: %d tc_skip: %d\n",
1343 (xfer->c_flags & C_ATAPI) ? "atapi" : "ata", 1346 (xfer->c_flags & C_ATAPI) ? "atapi" : "ata",
1344 xfer->c_bcount, 1347 xfer->c_bcount,
1345 xfer->c_skip); 1348 xfer->c_skip);
1346#if NATA_DMA || NATA_PIOBM 1349#if NATA_DMA || NATA_PIOBM
1347 if (chp->ch_flags & ATACH_DMA_WAIT) { 1350 if (chp->ch_flags & ATACH_DMA_WAIT) {
1348 wdc->dma_status = 1351 wdc->dma_status =
1349 (*wdc->dma_finish)(wdc->dma_arg, 1352 (*wdc->dma_finish)(wdc->dma_arg,
1350 chp->ch_channel, xfer->c_drive, 1353 chp->ch_channel, xfer->c_drive,
1351 WDC_DMAEND_ABRT); 1354 WDC_DMAEND_ABRT);
1352 chp->ch_flags &= ~ATACH_DMA_WAIT; 1355 chp->ch_flags &= ~ATACH_DMA_WAIT;
1353 } 1356 }
1354#endif 1357#endif
1355 /* 1358 /*
1356 * Call the interrupt routine. If we just missed an interrupt, 1359 * Call the interrupt routine. If we just missed an interrupt,
1357 * it will do what's needed. Else, it will take the needed 1360 * it will do what's needed. Else, it will take the needed
1358 * action (reset the device). 1361 * action (reset the device).
1359 * Before that we need to reinstall the timeout callback, 1362 * Before that we need to reinstall the timeout callback,
1360 * in case it will miss another irq while in this transfer 1363 * in case it will miss another irq while in this transfer
1361 * We arbitray chose it to be 1s 1364 * We arbitray chose it to be 1s
1362 */ 1365 */
1363 callout_reset(&chp->ch_callout, hz, wdctimeout, chp); 1366 callout_reset(&chp->ch_callout, hz, wdctimeout, chp);
1364 xfer->c_flags |= C_TIMEOU; 1367 xfer->c_flags |= C_TIMEOU;
1365 chp->ch_flags &= ~ATACH_IRQ_WAIT; 1368 chp->ch_flags &= ~ATACH_IRQ_WAIT;
1366 KASSERT(xfer->c_intr != NULL); 1369 KASSERT(xfer->c_intr != NULL);
1367 xfer->c_intr(chp, xfer, 1); 1370 xfer->c_intr(chp, xfer, 1);
1368 } else 1371 } else
1369 __wdcerror(chp, "missing untimeout"); 1372 __wdcerror(chp, "missing untimeout");
1370 splx(s); 1373 splx(s);
1371} 1374}
1372 1375
1373int 1376int
1374wdc_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c) 1377wdc_exec_command(struct ata_drive_datas *drvp, struct ata_command *ata_c)
1375{ 1378{
1376 struct ata_channel *chp = drvp->chnl_softc; 1379 struct ata_channel *chp = drvp->chnl_softc;
1377 struct ata_xfer *xfer; 1380 struct ata_xfer *xfer;
1378 int s, ret; 1381 int s, ret;
1379 1382
1380 ATADEBUG_PRINT(("wdc_exec_command %s:%d:%d\n", 1383 ATADEBUG_PRINT(("wdc_exec_command %s:%d:%d\n",
1381 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1384 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
1382 drvp->drive), DEBUG_FUNCS); 1385 drvp->drive), DEBUG_FUNCS);
1383 1386
1384 /* set up an xfer and queue. Wait for completion */ 1387 /* set up an xfer and queue. Wait for completion */
1385 xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP : 1388 xfer = ata_get_xfer(ata_c->flags & AT_WAIT ? ATAXF_CANSLEEP :
1386 ATAXF_NOSLEEP); 1389 ATAXF_NOSLEEP);
1387 if (xfer == NULL) { 1390 if (xfer == NULL) {
1388 return ATACMD_TRY_AGAIN; 1391 return ATACMD_TRY_AGAIN;
1389 } 1392 }
1390 1393
1391 if (chp->ch_atac->atac_cap & ATAC_CAP_NOIRQ) 1394 if (chp->ch_atac->atac_cap & ATAC_CAP_NOIRQ)
1392 ata_c->flags |= AT_POLL; 1395 ata_c->flags |= AT_POLL;
1393 if (ata_c->flags & AT_POLL) 1396 if (ata_c->flags & AT_POLL)
1394 xfer->c_flags |= C_POLL; 1397 xfer->c_flags |= C_POLL;
1395 if (ata_c->flags & AT_WAIT) 1398 if (ata_c->flags & AT_WAIT)
1396 xfer->c_flags |= C_WAIT; 1399 xfer->c_flags |= C_WAIT;
1397 xfer->c_drive = drvp->drive; 1400 xfer->c_drive = drvp->drive;
1398 xfer->c_databuf = ata_c->data; 1401 xfer->c_databuf = ata_c->data;
1399 xfer->c_bcount = ata_c->bcount; 1402 xfer->c_bcount = ata_c->bcount;
1400 xfer->c_cmd = ata_c; 1403 xfer->c_cmd = ata_c;
1401 xfer->c_start = __wdccommand_start; 1404 xfer->c_start = __wdccommand_start;
1402 xfer->c_intr = __wdccommand_intr; 1405 xfer->c_intr = __wdccommand_intr;
1403 xfer->c_kill_xfer = __wdccommand_kill_xfer; 1406 xfer->c_kill_xfer = __wdccommand_kill_xfer;
1404 1407
1405 s = splbio(); 1408 s = splbio();
1406 ata_exec_xfer(chp, xfer); 1409 ata_exec_xfer(chp, xfer);
1407#ifdef DIAGNOSTIC 1410#ifdef DIAGNOSTIC
1408 if ((ata_c->flags & AT_POLL) != 0 && 1411 if ((ata_c->flags & AT_POLL) != 0 &&
1409 (ata_c->flags & AT_DONE) == 0) 1412 (ata_c->flags & AT_DONE) == 0)
1410 panic("wdc_exec_command: polled command not done"); 1413 panic("wdc_exec_command: polled command not done");
1411#endif 1414#endif
1412 if (ata_c->flags & AT_DONE) { 1415 if (ata_c->flags & AT_DONE) {
1413 ret = ATACMD_COMPLETE; 1416 ret = ATACMD_COMPLETE;
1414 } else { 1417 } else {
1415 if (ata_c->flags & AT_WAIT) { 1418 if (ata_c->flags & AT_WAIT) {
1416 while ((ata_c->flags & AT_DONE) == 0) { 1419 while ((ata_c->flags & AT_DONE) == 0) {
1417 tsleep(ata_c, PRIBIO, "wdccmd", 0); 1420 tsleep(ata_c, PRIBIO, "wdccmd", 0);
1418 } 1421 }
1419 ret = ATACMD_COMPLETE; 1422 ret = ATACMD_COMPLETE;
1420 } else { 1423 } else {
1421 ret = ATACMD_QUEUED; 1424 ret = ATACMD_QUEUED;
1422 } 1425 }
1423 } 1426 }
1424 splx(s); 1427 splx(s);
1425 return ret; 1428 return ret;
1426} 1429}
1427 1430
1428static void 1431static void
1429__wdccommand_start(struct ata_channel *chp, struct ata_xfer *xfer) 1432__wdccommand_start(struct ata_channel *chp, struct ata_xfer *xfer)
1430{ 1433{
1431 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1434 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1432 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1435 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1433 int drive = xfer->c_drive; 1436 int drive = xfer->c_drive;
1434 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0; 1437 int wait_flags = (xfer->c_flags & C_POLL) ? AT_POLL : 0;
1435 struct ata_command *ata_c = xfer->c_cmd; 1438 struct ata_command *ata_c = xfer->c_cmd;
1436 1439
1437 ATADEBUG_PRINT(("__wdccommand_start %s:%d:%d\n", 1440 ATADEBUG_PRINT(("__wdccommand_start %s:%d:%d\n",
1438 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1441 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
1439 xfer->c_drive), 1442 xfer->c_drive),
1440 DEBUG_FUNCS); 1443 DEBUG_FUNCS);
1441 1444
1442 if (wdc->select) 1445 if (wdc->select)
1443 wdc->select(chp,drive); 1446 wdc->select(chp,drive);
1444 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1447 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
1445 WDSD_IBM | (drive << 4)); 1448 WDSD_IBM | (drive << 4));
1446 switch(wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ, 1449 switch(wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ,
1447 ata_c->r_st_bmask, ata_c->timeout, wait_flags)) { 1450 ata_c->r_st_bmask, ata_c->timeout, wait_flags)) {
1448 case WDCWAIT_OK: 1451 case WDCWAIT_OK:
1449 break; 1452 break;
1450 case WDCWAIT_TOUT: 1453 case WDCWAIT_TOUT:
1451 ata_c->flags |= AT_TIMEOU; 1454 ata_c->flags |= AT_TIMEOU;
1452 __wdccommand_done(chp, xfer); 1455 __wdccommand_done(chp, xfer);
1453 return; 1456 return;
1454 case WDCWAIT_THR: 1457 case WDCWAIT_THR:
1455 return; 1458 return;
1456 } 1459 }
1457 if (ata_c->flags & AT_POLL) { 1460 if (ata_c->flags & AT_POLL) {
1458 /* polled command, disable interrupts */ 1461 /* polled command, disable interrupts */
1459 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1462 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
1460 WDCTL_4BIT | WDCTL_IDS); 1463 WDCTL_4BIT | WDCTL_IDS);
1461 } 1464 }
1462 wdccommand(chp, drive, ata_c->r_command, ata_c->r_cyl, ata_c->r_head, 1465 wdccommand(chp, drive, ata_c->r_command, ata_c->r_cyl, ata_c->r_head,
1463 ata_c->r_sector, ata_c->r_count, ata_c->r_features); 1466 ata_c->r_sector, ata_c->r_count, ata_c->r_features);
1464 1467
1465 if ((ata_c->flags & AT_POLL) == 0) { 1468 if ((ata_c->flags & AT_POLL) == 0) {
1466 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 1469 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1467 callout_reset(&chp->ch_callout, ata_c->timeout / 1000 * hz, 1470 callout_reset(&chp->ch_callout, ata_c->timeout / 1000 * hz,
1468 wdctimeout, chp); 1471 wdctimeout, chp);
1469 return; 1472 return;
1470 } 1473 }
1471 /* 1474 /*
1472 * Polled command. Wait for drive ready or drq. Done in intr(). 1475 * Polled command. Wait for drive ready or drq. Done in intr().
1473 * Wait for at last 400ns for status bit to be valid. 1476 * Wait for at last 400ns for status bit to be valid.
1474 */ 1477 */
1475 delay(10); /* 400ns delay */ 1478 delay(10); /* 400ns delay */
1476 __wdccommand_intr(chp, xfer, 0); 1479 __wdccommand_intr(chp, xfer, 0);
1477} 1480}
1478 1481
1479static int 1482static int
1480__wdccommand_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq) 1483__wdccommand_intr(struct ata_channel *chp, struct ata_xfer *xfer, int irq)
1481{ 1484{
1482 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1485 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1483 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1486 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1484 struct ata_command *ata_c = xfer->c_cmd; 1487 struct ata_command *ata_c = xfer->c_cmd;
1485 int bcount = ata_c->bcount; 1488 int bcount = ata_c->bcount;
1486 char *data = ata_c->data; 1489 char *data = ata_c->data;
1487 int wflags; 1490 int wflags;
1488 int drive_flags; 1491 int drive_flags;
1489 1492
1490 if (ata_c->r_command == WDCC_IDENTIFY || 1493 if (ata_c->r_command == WDCC_IDENTIFY ||
1491 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) { 1494 ata_c->r_command == ATAPI_IDENTIFY_DEVICE) {
1492 /* 1495 /*
1493 * The IDENTIFY data has been designed as an array of 1496 * The IDENTIFY data has been designed as an array of
1494 * u_int16_t, so we can byteswap it on the fly. 1497 * u_int16_t, so we can byteswap it on the fly.
1495 * Historically it's what we have always done so keeping it 1498 * Historically it's what we have always done so keeping it
1496 * here ensure binary backward compatibility. 1499 * here ensure binary backward compatibility.
1497 */ 1500 */
1498 drive_flags = DRIVE_NOSTREAM |  1501 drive_flags = DRIVE_NOSTREAM |
1499 chp->ch_drive[xfer->c_drive].drive_flags; 1502 chp->ch_drive[xfer->c_drive].drive_flags;
1500 } else { 1503 } else {
1501 /* 1504 /*
1502 * Other data structure are opaque and should be transfered 1505 * Other data structure are opaque and should be transfered
1503 * as is. 1506 * as is.
1504 */ 1507 */
1505 drive_flags = chp->ch_drive[xfer->c_drive].drive_flags; 1508 drive_flags = chp->ch_drive[xfer->c_drive].drive_flags;
1506 } 1509 }
1507 1510
1508#ifdef WDC_NO_IDS 1511#ifndef WDC_NO_IDS
1509 wflags = AT_POLL; 
1510#else 
1511 if ((ata_c->flags & (AT_WAIT | AT_POLL)) == (AT_WAIT | AT_POLL)) { 1512 if ((ata_c->flags & (AT_WAIT | AT_POLL)) == (AT_WAIT | AT_POLL)) {
1512 /* both wait and poll, we can tsleep here */ 1513 /* both wait and poll, we can tsleep here */
1513 wflags = AT_WAIT | AT_POLL; 1514 wflags = AT_WAIT | AT_POLL;
1514 } else { 1515 } else
1515 wflags = AT_POLL; 
1516 } 
1517#endif 1516#endif
 1517 wflags = AT_POLL;
1518 1518
1519 again: 1519 again:
1520 ATADEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n", 1520 ATADEBUG_PRINT(("__wdccommand_intr %s:%d:%d\n",
1521 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, 1521 device_xname(chp->ch_atac->atac_dev), chp->ch_channel,
1522 xfer->c_drive), DEBUG_INTR); 1522 xfer->c_drive), DEBUG_INTR);
1523 /* 1523 /*
1524 * after a ATAPI_SOFT_RESET, the device will have released the bus. 1524 * after a ATAPI_SOFT_RESET, the device will have released the bus.
1525 * Reselect again, it doesn't hurt for others commands, and the time 1525 * Reselect again, it doesn't hurt for others commands, and the time
1526 * penalty for the extra regiter write is acceptable, 1526 * penalty for the extra regiter write is acceptable,
1527 * wdc_exec_command() isn't called often (mosly for autoconfig) 1527 * wdc_exec_command() isn't called often (mosly for autoconfig)
1528 */ 1528 */
1529 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1529 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
1530 WDSD_IBM | (xfer->c_drive << 4)); 1530 WDSD_IBM | (xfer->c_drive << 4));
1531 if ((ata_c->flags & AT_XFDONE) != 0) { 1531 if ((ata_c->flags & AT_XFDONE) != 0) {
1532 /* 1532 /*
1533 * We have completed a data xfer. The drive should now be 1533 * We have completed a data xfer. The drive should now be
1534 * in its initial state 1534 * in its initial state
1535 */ 1535 */
1536 if (wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ, 1536 if (wdcwait(chp, ata_c->r_st_bmask | WDCS_DRQ,
1537 ata_c->r_st_bmask, (irq == 0) ? ata_c->timeout : 0, 1537 ata_c->r_st_bmask, (irq == 0) ? ata_c->timeout : 0,
1538 wflags) == WDCWAIT_TOUT) { 1538 wflags) == WDCWAIT_TOUT) {
1539 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 1539 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
1540 return 0; /* IRQ was not for us */ 1540 return 0; /* IRQ was not for us */
1541 ata_c->flags |= AT_TIMEOU; 1541 ata_c->flags |= AT_TIMEOU;
1542 } 1542 }
1543 goto out; 1543 goto out;
1544 } 1544 }
1545 if (wdcwait(chp, ata_c->r_st_pmask, ata_c->r_st_pmask, 1545 if (wdcwait(chp, ata_c->r_st_pmask, ata_c->r_st_pmask,
1546 (irq == 0) ? ata_c->timeout : 0, wflags) == WDCWAIT_TOUT) { 1546 (irq == 0) ? ata_c->timeout : 0, wflags) == WDCWAIT_TOUT) {
1547 if (irq && (xfer->c_flags & C_TIMEOU) == 0) 1547 if (irq && (xfer->c_flags & C_TIMEOU) == 0)
1548 return 0; /* IRQ was not for us */ 1548 return 0; /* IRQ was not for us */
1549 ata_c->flags |= AT_TIMEOU; 1549 ata_c->flags |= AT_TIMEOU;
1550 goto out; 1550 goto out;
1551 } 1551 }
1552 if (wdc->irqack) 1552 if (wdc->irqack)
1553 wdc->irqack(chp); 1553 wdc->irqack(chp);
1554 if (ata_c->flags & AT_READ) { 1554 if (ata_c->flags & AT_READ) {
1555 if ((chp->ch_status & WDCS_DRQ) == 0) { 1555 if ((chp->ch_status & WDCS_DRQ) == 0) {
1556 ata_c->flags |= AT_TIMEOU; 1556 ata_c->flags |= AT_TIMEOU;
1557 goto out; 1557 goto out;
1558 } 1558 }
1559 wdc->datain_pio(chp, drive_flags, data, bcount); 1559 wdc->datain_pio(chp, drive_flags, data, bcount);
1560 /* at this point the drive should be in its initial state */ 1560 /* at this point the drive should be in its initial state */
1561 ata_c->flags |= AT_XFDONE; 1561 ata_c->flags |= AT_XFDONE;
1562 /* 1562 /*
1563 * XXX checking the status register again here cause some 1563 * XXX checking the status register again here cause some
1564 * hardware to timeout. 1564 * hardware to timeout.
1565 */ 1565 */
1566 } else if (ata_c->flags & AT_WRITE) { 1566 } else if (ata_c->flags & AT_WRITE) {
1567 if ((chp->ch_status & WDCS_DRQ) == 0) { 1567 if ((chp->ch_status & WDCS_DRQ) == 0) {
1568 ata_c->flags |= AT_TIMEOU; 1568 ata_c->flags |= AT_TIMEOU;
1569 goto out; 1569 goto out;
1570 } 1570 }
1571 wdc->dataout_pio(chp, drive_flags, data, bcount); 1571 wdc->dataout_pio(chp, drive_flags, data, bcount);
1572 ata_c->flags |= AT_XFDONE; 1572 ata_c->flags |= AT_XFDONE;
1573 if ((ata_c->flags & AT_POLL) == 0) { 1573 if ((ata_c->flags & AT_POLL) == 0) {
1574 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */ 1574 chp->ch_flags |= ATACH_IRQ_WAIT; /* wait for interrupt */
1575 callout_reset(&chp->ch_callout, 1575 callout_reset(&chp->ch_callout,
1576 mstohz(ata_c->timeout), wdctimeout, chp); 1576 mstohz(ata_c->timeout), wdctimeout, chp);
1577 return 1; 1577 return 1;
1578 } else { 1578 } else {
1579 goto again; 1579 goto again;
1580 } 1580 }
1581 } 1581 }
1582 out: 1582 out:
1583 __wdccommand_done(chp, xfer); 1583 __wdccommand_done(chp, xfer);
1584 return 1; 1584 return 1;
1585} 1585}
1586 1586
1587static void 1587static void
1588__wdccommand_done(struct ata_channel *chp, struct ata_xfer *xfer) 1588__wdccommand_done(struct ata_channel *chp, struct ata_xfer *xfer)
1589{ 1589{
1590 struct atac_softc *atac = chp->ch_atac; 1590 struct atac_softc *atac = chp->ch_atac;
1591 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1591 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1592 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1592 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1593 struct ata_command *ata_c = xfer->c_cmd; 1593 struct ata_command *ata_c = xfer->c_cmd;
1594 1594
1595 ATADEBUG_PRINT(("__wdccommand_done %s:%d:%d flags 0x%x\n", 1595 ATADEBUG_PRINT(("__wdccommand_done %s:%d:%d flags 0x%x\n",
1596 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive, 1596 device_xname(atac->atac_dev), chp->ch_channel, xfer->c_drive,
1597 ata_c->flags), DEBUG_FUNCS); 1597 ata_c->flags), DEBUG_FUNCS);
1598 1598
1599 1599
1600 if (chp->ch_status & WDCS_DWF) 1600 if (chp->ch_status & WDCS_DWF)
1601 ata_c->flags |= AT_DF; 1601 ata_c->flags |= AT_DF;
1602 if (chp->ch_status & WDCS_ERR) { 1602 if (chp->ch_status & WDCS_ERR) {
1603 ata_c->flags |= AT_ERROR; 1603 ata_c->flags |= AT_ERROR;
1604 ata_c->r_error = chp->ch_error; 1604 ata_c->r_error = chp->ch_error;
1605 } 1605 }
1606 if ((ata_c->flags & AT_READREG) != 0 && 1606 if ((ata_c->flags & AT_READREG) != 0 &&
1607 device_is_active(atac->atac_dev) && 1607 device_is_active(atac->atac_dev) &&
1608 (ata_c->flags & (AT_ERROR | AT_DF)) == 0) { 1608 (ata_c->flags & (AT_ERROR | AT_DF)) == 0) {
1609 ata_c->r_head = bus_space_read_1(wdr->cmd_iot, 1609 ata_c->r_head = bus_space_read_1(wdr->cmd_iot,
1610 wdr->cmd_iohs[wd_sdh], 0); 1610 wdr->cmd_iohs[wd_sdh], 0);
1611 ata_c->r_count = bus_space_read_1(wdr->cmd_iot, 1611 ata_c->r_count = bus_space_read_1(wdr->cmd_iot,
1612 wdr->cmd_iohs[wd_seccnt], 0); 1612 wdr->cmd_iohs[wd_seccnt], 0);
1613 ata_c->r_sector = bus_space_read_1(wdr->cmd_iot, 1613 ata_c->r_sector = bus_space_read_1(wdr->cmd_iot,
1614 wdr->cmd_iohs[wd_sector], 0); 1614 wdr->cmd_iohs[wd_sector], 0);
1615 ata_c->r_cyl = bus_space_read_1(wdr->cmd_iot, 1615 ata_c->r_cyl = bus_space_read_1(wdr->cmd_iot,
1616 wdr->cmd_iohs[wd_cyl_hi], 0) << 8; 1616 wdr->cmd_iohs[wd_cyl_hi], 0) << 8;
1617 ata_c->r_cyl |= bus_space_read_1(wdr->cmd_iot, 1617 ata_c->r_cyl |= bus_space_read_1(wdr->cmd_iot,
1618 wdr->cmd_iohs[wd_cyl_lo], 0); 1618 wdr->cmd_iohs[wd_cyl_lo], 0);
1619 ata_c->r_error = bus_space_read_1(wdr->cmd_iot, 1619 ata_c->r_error = bus_space_read_1(wdr->cmd_iot,
1620 wdr->cmd_iohs[wd_error], 0); 1620 wdr->cmd_iohs[wd_error], 0);
1621 ata_c->r_features = bus_space_read_1(wdr->cmd_iot, 1621 ata_c->r_features = bus_space_read_1(wdr->cmd_iot,
1622 wdr->cmd_iohs[wd_features], 0); 1622 wdr->cmd_iohs[wd_features], 0);
1623 } 1623 }
1624 callout_stop(&chp->ch_callout); 1624 callout_stop(&chp->ch_callout);
1625 chp->ch_queue->active_xfer = NULL; 1625 chp->ch_queue->active_xfer = NULL;
1626 if (ata_c->flags & AT_POLL) { 1626 if (ata_c->flags & AT_POLL) {
1627 /* enable interrupts */ 1627 /* enable interrupts */
1628 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 1628 bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr,
1629 WDCTL_4BIT); 1629 WDCTL_4BIT);
1630 delay(10); /* some drives need a little delay here */ 1630 delay(10); /* some drives need a little delay here */
1631 } 1631 }
1632 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) { 1632 if (chp->ch_drive[xfer->c_drive].drive_flags & DRIVE_WAITDRAIN) {
1633 __wdccommand_kill_xfer(chp, xfer, KILL_GONE); 1633 __wdccommand_kill_xfer(chp, xfer, KILL_GONE);
1634 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN; 1634 chp->ch_drive[xfer->c_drive].drive_flags &= ~DRIVE_WAITDRAIN;
1635 wakeup(&chp->ch_queue->active_xfer); 1635 wakeup(&chp->ch_queue->active_xfer);
1636 } else 1636 } else
1637 __wdccommand_done_end(chp, xfer); 1637 __wdccommand_done_end(chp, xfer);
1638} 1638}
1639 1639
1640static void 1640static void
1641__wdccommand_done_end(struct ata_channel *chp, struct ata_xfer *xfer) 1641__wdccommand_done_end(struct ata_channel *chp, struct ata_xfer *xfer)
1642{ 1642{
1643 struct ata_command *ata_c = xfer->c_cmd; 1643 struct ata_command *ata_c = xfer->c_cmd;
1644 1644
1645 ata_c->flags |= AT_DONE; 1645 ata_c->flags |= AT_DONE;
1646 ata_free_xfer(chp, xfer); 1646 ata_free_xfer(chp, xfer);
1647 if (ata_c->flags & AT_WAIT) 1647 if (ata_c->flags & AT_WAIT)
1648 wakeup(ata_c); 1648 wakeup(ata_c);
1649 else if (ata_c->callback) 1649 else if (ata_c->callback)
1650 ata_c->callback(ata_c->callback_arg); 1650 ata_c->callback(ata_c->callback_arg);
1651 atastart(chp); 1651 atastart(chp);
1652 return; 1652 return;
1653} 1653}
1654 1654
1655static void 1655static void
1656__wdccommand_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer, 1656__wdccommand_kill_xfer(struct ata_channel *chp, struct ata_xfer *xfer,
1657 int reason) 1657 int reason)
1658{ 1658{
1659 struct ata_command *ata_c = xfer->c_cmd; 1659 struct ata_command *ata_c = xfer->c_cmd;
1660 1660
1661 switch (reason) { 1661 switch (reason) {
1662 case KILL_GONE: 1662 case KILL_GONE:
1663 ata_c->flags |= AT_GONE; 1663 ata_c->flags |= AT_GONE;
1664 break; 1664 break;
1665 case KILL_RESET: 1665 case KILL_RESET:
1666 ata_c->flags |= AT_RESET; 1666 ata_c->flags |= AT_RESET;
1667 break; 1667 break;
1668 default: 1668 default:
1669 printf("__wdccommand_kill_xfer: unknown reason %d\n", 1669 printf("__wdccommand_kill_xfer: unknown reason %d\n",
1670 reason); 1670 reason);
1671 panic("__wdccommand_kill_xfer"); 1671 panic("__wdccommand_kill_xfer");
1672 } 1672 }
1673 __wdccommand_done_end(chp, xfer); 1673 __wdccommand_done_end(chp, xfer);
1674} 1674}
1675 1675
1676/* 1676/*
1677 * Send a command. The drive should be ready. 1677 * Send a command. The drive should be ready.
1678 * Assumes interrupts are blocked. 1678 * Assumes interrupts are blocked.
1679 */ 1679 */
1680void 1680void
1681wdccommand(struct ata_channel *chp, u_int8_t drive, u_int8_t command, 1681wdccommand(struct ata_channel *chp, u_int8_t drive, u_int8_t command,
1682 u_int16_t cylin, u_int8_t head, u_int8_t sector, u_int8_t count, 1682 u_int16_t cylin, u_int8_t head, u_int8_t sector, u_int8_t count,
1683 u_int8_t features) 1683 u_int8_t features)
1684{ 1684{
1685 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1685 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1686 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1686 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1687 1687
1688 ATADEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d " 1688 ATADEBUG_PRINT(("wdccommand %s:%d:%d: command=0x%x cylin=%d head=%d "
1689 "sector=%d count=%d features=%d\n", 1689 "sector=%d count=%d features=%d\n",
1690 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, drive, 1690 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, drive,
1691 command, cylin, head, sector, count, features), DEBUG_FUNCS); 1691 command, cylin, head, sector, count, features), DEBUG_FUNCS);
1692 1692
1693 if (wdc->select) 1693 if (wdc->select)
1694 wdc->select(chp,drive); 1694 wdc->select(chp,drive);
1695 1695
1696 /* Select drive, head, and addressing mode. */ 1696 /* Select drive, head, and addressing mode. */
1697 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1697 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
1698 WDSD_IBM | (drive << 4) | head); 1698 WDSD_IBM | (drive << 4) | head);
1699 /* Load parameters into the wd_features register. */ 1699 /* Load parameters into the wd_features register. */
1700 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0, 1700 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0,
1701 features); 1701 features);
1702 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 0, count); 1702 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 0, count);
1703 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sector], 0, sector); 1703 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sector], 0, sector);
1704 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0, cylin); 1704 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_lo], 0, cylin);
1705 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi], 1705 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_cyl_hi],
1706 0, cylin >> 8); 1706 0, cylin >> 8);
1707 1707
1708 /* Send command. */ 1708 /* Send command. */
1709 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command); 1709 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command);
1710 return; 1710 return;
1711} 1711}
1712 1712
1713/* 1713/*
1714 * Send a 48-bit addressing command. The drive should be ready. 1714 * Send a 48-bit addressing command. The drive should be ready.
1715 * Assumes interrupts are blocked. 1715 * Assumes interrupts are blocked.
1716 */ 1716 */
1717void 1717void
1718wdccommandext(struct ata_channel *chp, u_int8_t drive, u_int8_t command, 1718wdccommandext(struct ata_channel *chp, u_int8_t drive, u_int8_t command,
1719 u_int64_t blkno, u_int16_t count) 1719 u_int64_t blkno, u_int16_t count)
1720{ 1720{
1721 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1721 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1722 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1722 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1723 1723
1724 ATADEBUG_PRINT(("wdccommandext %s:%d:%d: command=0x%x blkno=%d " 1724 ATADEBUG_PRINT(("wdccommandext %s:%d:%d: command=0x%x blkno=%d "
1725 "count=%d\n", device_xname(chp->ch_atac->atac_dev), 1725 "count=%d\n", device_xname(chp->ch_atac->atac_dev),
1726 chp->ch_channel, drive, command, (u_int32_t) blkno, count), 1726 chp->ch_channel, drive, command, (u_int32_t) blkno, count),
1727 DEBUG_FUNCS); 1727 DEBUG_FUNCS);
1728 1728
1729 if (wdc->select) 1729 if (wdc->select)
1730 wdc->select(chp,drive); 1730 wdc->select(chp,drive);
1731 1731
1732 /* Select drive, head, and addressing mode. */ 1732 /* Select drive, head, and addressing mode. */
1733 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1733 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
1734 (drive << 4) | WDSD_LBA); 1734 (drive << 4) | WDSD_LBA);
1735 1735
1736 if (wdc->cap & WDC_CAPABILITY_WIDEREGS) { 1736 if (wdc->cap & WDC_CAPABILITY_WIDEREGS) {
1737 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0, 1737 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0,
1738 0); 1738 0);
1739 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 1739 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt],
1740 0, count); 1740 0, count);
1741 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 1741 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo],
1742 0, (((blkno >> 16) & 0xff00) | (blkno & 0x00ff))); 1742 0, (((blkno >> 16) & 0xff00) | (blkno & 0x00ff)));
1743 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi], 1743 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi],
1744 0, (((blkno >> 24) & 0xff00) | ((blkno >> 8) & 0x00ff))); 1744 0, (((blkno >> 24) & 0xff00) | ((blkno >> 8) & 0x00ff)));
1745 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi], 1745 bus_space_write_2(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi],
1746 0, (((blkno >> 32) & 0xff00) | ((blkno >> 16) & 0x00ff))); 1746 0, (((blkno >> 32) & 0xff00) | ((blkno >> 16) & 0x00ff)));
1747 } else { 1747 } else {
1748 /* previous */ 1748 /* previous */
1749 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0, 1749 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0,
1750 0); 1750 0);
1751 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 1751 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt],
1752 0, count >> 8); 1752 0, count >> 8);
1753 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 1753 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo],
1754 0, blkno >> 24); 1754 0, blkno >> 24);
1755 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi], 1755 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi],
1756 0, blkno >> 32); 1756 0, blkno >> 32);
1757 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi], 1757 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi],
1758 0, blkno >> 40); 1758 0, blkno >> 40);
1759 1759
1760 /* current */ 1760 /* current */
1761 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0, 1761 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_features], 0,
1762 0); 1762 0);
1763 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 0, 1763 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_seccnt], 0,
1764 count); 1764 count);
1765 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 0, 1765 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_lo], 0,
1766 blkno); 1766 blkno);
1767 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi], 1767 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_mi],
1768 0, blkno >> 8); 1768 0, blkno >> 8);
1769 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi], 1769 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_lba_hi],
1770 0, blkno >> 16); 1770 0, blkno >> 16);
1771 } 1771 }
1772 1772
1773 /* Send command. */ 1773 /* Send command. */
1774 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command); 1774 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command);
1775 return; 1775 return;
1776} 1776}
1777 1777
1778/* 1778/*
1779 * Simplified version of wdccommand(). Unbusy/ready/drq must be 1779 * Simplified version of wdccommand(). Unbusy/ready/drq must be
1780 * tested by the caller. 1780 * tested by the caller.
1781 */ 1781 */
1782void 1782void
1783wdccommandshort(struct ata_channel *chp, int drive, int command) 1783wdccommandshort(struct ata_channel *chp, int drive, int command)
1784{ 1784{
1785 struct wdc_softc *wdc = CHAN_TO_WDC(chp); 1785 struct wdc_softc *wdc = CHAN_TO_WDC(chp);
1786 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel]; 1786 struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
1787 1787
1788 ATADEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n", 1788 ATADEBUG_PRINT(("wdccommandshort %s:%d:%d command 0x%x\n",
1789 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, drive, 1789 device_xname(chp->ch_atac->atac_dev), chp->ch_channel, drive,
1790 command), DEBUG_FUNCS); 1790 command), DEBUG_FUNCS);
1791 1791
1792 if (wdc->select) 1792 if (wdc->select)
1793 wdc->select(chp,drive); 1793 wdc->select(chp,drive);
1794 1794
1795 /* Select drive. */ 1795 /* Select drive. */
1796 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0, 1796 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_sdh], 0,
1797 WDSD_IBM | (drive << 4)); 1797 WDSD_IBM | (drive << 4));
1798 1798
1799 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command); 1799 bus_space_write_1(wdr->cmd_iot, wdr->cmd_iohs[wd_command], 0, command);
1800} 1800}
1801 1801
1802static void 1802static void
1803__wdcerror(struct ata_channel *chp, const char *msg) 1803__wdcerror(struct ata_channel *chp, const char *msg)
1804{ 1804{
1805 struct atac_softc *atac = chp->ch_atac; 1805 struct atac_softc *atac = chp->ch_atac;
1806 struct ata_xfer *xfer = chp->ch_queue->active_xfer; 1806 struct ata_xfer *xfer = chp->ch_queue->active_xfer;
1807 1807
1808 if (xfer == NULL) 1808 if (xfer == NULL)
1809 aprint_error("%s:%d: %s\n", device_xname(atac->atac_dev), 1809 aprint_error("%s:%d: %s\n", device_xname(atac->atac_dev),
1810 chp->ch_channel, msg); 1810 chp->ch_channel, msg);
1811 else 1811 else
1812 aprint_error("%s:%d:%d: %s\n", device_xname(atac->atac_dev), 1812 aprint_error("%s:%d:%d: %s\n", device_xname(atac->atac_dev),
1813 chp->ch_channel, xfer->c_drive, msg); 1813 chp->ch_channel, xfer->c_drive, msg);
1814} 1814}
1815 1815
1816/* 1816/*
1817 * the bit bucket 1817 * the bit bucket
1818 */ 1818 */
1819void 1819void
1820wdcbit_bucket(struct ata_channel *chp, int size) 1820wdcbit_bucket(struct ata_channel *chp, int size)
1821{ 1821{
1822 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 1822 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
1823 1823
1824 for (; size >= 2; size -= 2) 1824 for (; size >= 2; size -= 2)
1825 (void)bus_space_read_2(wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0); 1825 (void)bus_space_read_2(wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0);
1826 if (size) 1826 if (size)
1827 (void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0); 1827 (void)bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0);
1828} 1828}
1829 1829
1830static void 1830static void
1831wdc_datain_pio(struct ata_channel *chp, int flags, void *bf, size_t len) 1831wdc_datain_pio(struct ata_channel *chp, int flags, void *bf, size_t len)
1832{ 1832{
1833 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 1833 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
1834 1834
1835#ifndef __NO_STRICT_ALIGNMENT 1835#ifndef __NO_STRICT_ALIGNMENT
1836 if ((uintptr_t)bf & 1) 1836 if ((uintptr_t)bf & 1)
1837 goto unaligned; 1837 goto unaligned;
1838 if ((flags & DRIVE_CAP32) && ((uintptr_t)bf & 3)) 1838 if ((flags & DRIVE_CAP32) && ((uintptr_t)bf & 3))
1839 goto unaligned; 1839 goto unaligned;
1840#endif 1840#endif
1841 1841
1842 if (flags & DRIVE_NOSTREAM) { 1842 if (flags & DRIVE_NOSTREAM) {
1843 if (flags & DRIVE_CAP32) { 1843 if (flags & DRIVE_CAP32) {
1844 bus_space_read_multi_4(wdr->data32iot, 1844 bus_space_read_multi_4(wdr->data32iot,
1845 wdr->data32ioh, 0, bf, len >> 2); 1845 wdr->data32ioh, 0, bf, len >> 2);
1846 bf = (char *)bf + (len & ~3); 1846 bf = (char *)bf + (len & ~3);
1847 len &= 3; 1847 len &= 3;
1848 } 1848 }
1849 if (len) { 1849 if (len) {
1850 bus_space_read_multi_2(wdr->cmd_iot, 1850 bus_space_read_multi_2(wdr->cmd_iot,
1851 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1851 wdr->cmd_iohs[wd_data], 0, bf, len >> 1);
1852 } 1852 }
1853 } else { 1853 } else {
1854 if (flags & DRIVE_CAP32) { 1854 if (flags & DRIVE_CAP32) {
1855 bus_space_read_multi_stream_4(wdr->data32iot, 1855 bus_space_read_multi_stream_4(wdr->data32iot,
1856 wdr->data32ioh, 0, bf, len >> 2); 1856 wdr->data32ioh, 0, bf, len >> 2);
1857 bf = (char *)bf + (len & ~3); 1857 bf = (char *)bf + (len & ~3);
1858 len &= 3; 1858 len &= 3;
1859 } 1859 }
1860 if (len) { 1860 if (len) {
1861 bus_space_read_multi_stream_2(wdr->cmd_iot, 1861 bus_space_read_multi_stream_2(wdr->cmd_iot,
1862 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1862 wdr->cmd_iohs[wd_data], 0, bf, len >> 1);
1863 } 1863 }
1864 } 1864 }
1865 return; 1865 return;
1866 1866
1867#ifndef __NO_STRICT_ALIGNMENT 1867#ifndef __NO_STRICT_ALIGNMENT
1868unaligned: 1868unaligned:
1869 if (flags & DRIVE_NOSTREAM) { 1869 if (flags & DRIVE_NOSTREAM) {
1870 if (flags & DRIVE_CAP32) { 1870 if (flags & DRIVE_CAP32) {
1871 while (len > 3) { 1871 while (len > 3) {
1872 uint32_t val; 1872 uint32_t val;
1873 1873
1874 val = bus_space_read_4(wdr->data32iot, 1874 val = bus_space_read_4(wdr->data32iot,
1875 wdr->data32ioh, 0); 1875 wdr->data32ioh, 0);
1876 memcpy(bf, &val, 4); 1876 memcpy(bf, &val, 4);
1877 bf = (char *)bf + 4; 1877 bf = (char *)bf + 4;
1878 len -= 4; 1878 len -= 4;
1879 } 1879 }
1880 } 1880 }
1881 while (len > 1) { 1881 while (len > 1) {
1882 uint16_t val; 1882 uint16_t val;
1883 1883
1884 val = bus_space_read_2(wdr->cmd_iot, 1884 val = bus_space_read_2(wdr->cmd_iot,
1885 wdr->cmd_iohs[wd_data], 0); 1885 wdr->cmd_iohs[wd_data], 0);
1886 memcpy(bf, &val, 2); 1886 memcpy(bf, &val, 2);
1887 bf = (char *)bf + 2; 1887 bf = (char *)bf + 2;
1888 len -= 2; 1888 len -= 2;
1889 } 1889 }
1890 } else { 1890 } else {
1891 if (flags & DRIVE_CAP32) { 1891 if (flags & DRIVE_CAP32) {
1892 while (len > 3) { 1892 while (len > 3) {
1893 uint32_t val; 1893 uint32_t val;
1894 1894
1895 val = bus_space_read_stream_4(wdr->data32iot, 1895 val = bus_space_read_stream_4(wdr->data32iot,
1896 wdr->data32ioh, 0); 1896 wdr->data32ioh, 0);
1897 memcpy(bf, &val, 4); 1897 memcpy(bf, &val, 4);
1898 bf = (char *)bf + 4; 1898 bf = (char *)bf + 4;
1899 len -= 4; 1899 len -= 4;
1900 } 1900 }
1901 } 1901 }
1902 while (len > 1) { 1902 while (len > 1) {
1903 uint16_t val; 1903 uint16_t val;
1904 1904
1905 val = bus_space_read_stream_2(wdr->cmd_iot, 1905 val = bus_space_read_stream_2(wdr->cmd_iot,
1906 wdr->cmd_iohs[wd_data], 0); 1906 wdr->cmd_iohs[wd_data], 0);
1907 memcpy(bf, &val, 2); 1907 memcpy(bf, &val, 2);
1908 bf = (char *)bf + 2; 1908 bf = (char *)bf + 2;
1909 len -= 2; 1909 len -= 2;
1910 } 1910 }
1911 } 1911 }
1912#endif 1912#endif
1913} 1913}
1914 1914
1915static void 1915static void
1916wdc_dataout_pio(struct ata_channel *chp, int flags, void *bf, size_t len) 1916wdc_dataout_pio(struct ata_channel *chp, int flags, void *bf, size_t len)
1917{ 1917{
1918 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp); 1918 struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
1919 1919
1920#ifndef __NO_STRICT_ALIGNMENT 1920#ifndef __NO_STRICT_ALIGNMENT
1921 if ((uintptr_t)bf & 1) 1921 if ((uintptr_t)bf & 1)
1922 goto unaligned; 1922 goto unaligned;
1923 if ((flags & DRIVE_CAP32) && ((uintptr_t)bf & 3)) 1923 if ((flags & DRIVE_CAP32) && ((uintptr_t)bf & 3))
1924 goto unaligned; 1924 goto unaligned;
1925#endif 1925#endif
1926 1926
1927 if (flags & DRIVE_NOSTREAM) { 1927 if (flags & DRIVE_NOSTREAM) {
1928 if (flags & DRIVE_CAP32) { 1928 if (flags & DRIVE_CAP32) {
1929 bus_space_write_multi_4(wdr->data32iot, 1929 bus_space_write_multi_4(wdr->data32iot,
1930 wdr->data32ioh, 0, bf, len >> 2); 1930 wdr->data32ioh, 0, bf, len >> 2);
1931 bf = (char *)bf + (len & ~3); 1931 bf = (char *)bf + (len & ~3);
1932 len &= 3; 1932 len &= 3;
1933 } 1933 }
1934 if (len) { 1934 if (len) {
1935 bus_space_write_multi_2(wdr->cmd_iot, 1935 bus_space_write_multi_2(wdr->cmd_iot,
1936 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1936 wdr->cmd_iohs[wd_data], 0, bf, len >> 1);
1937 } 1937 }
1938 } else { 1938 } else {
1939 if (flags & DRIVE_CAP32) { 1939 if (flags & DRIVE_CAP32) {
1940 bus_space_write_multi_stream_4(wdr->data32iot, 1940 bus_space_write_multi_stream_4(wdr->data32iot,
1941 wdr->data32ioh, 0, bf, len >> 2); 1941 wdr->data32ioh, 0, bf, len >> 2);
1942 bf = (char *)bf + (len & ~3); 1942 bf = (char *)bf + (len & ~3);
1943 len &= 3; 1943 len &= 3;
1944 } 1944 }
1945 if (len) { 1945 if (len) {
1946 bus_space_write_multi_stream_2(wdr->cmd_iot, 1946 bus_space_write_multi_stream_2(wdr->cmd_iot,
1947 wdr->cmd_iohs[wd_data], 0, bf, len >> 1); 1947 wdr->cmd_iohs[wd_data], 0, bf, len >> 1);
1948 } 1948 }
1949 } 1949 }
1950 return; 1950 return;
1951 1951
1952#ifndef __NO_STRICT_ALIGNMENT 1952#ifndef __NO_STRICT_ALIGNMENT
1953unaligned: 1953unaligned:
1954 if (flags & DRIVE_NOSTREAM) { 1954 if (flags & DRIVE_NOSTREAM) {
1955 if (flags & DRIVE_CAP32) { 1955 if (flags & DRIVE_CAP32) {
1956 while (len > 3) { 1956 while (len > 3) {
1957 uint32_t val; 1957 uint32_t val;
1958 1958
1959 memcpy(&val, bf, 4); 1959 memcpy(&val, bf, 4);
1960 bus_space_write_4(wdr->data32iot, 1960 bus_space_write_4(wdr->data32iot,
1961 wdr->data32ioh, 0, val); 1961 wdr->data32ioh, 0, val);
1962 bf = (char *)bf + 4; 1962 bf = (char *)bf + 4;
1963 len -= 4; 1963 len -= 4;
1964 } 1964 }
1965 } 1965 }
1966 while (len > 1) { 1966 while (len > 1) {
1967 uint16_t val; 1967 uint16_t val;
1968 1968
1969 memcpy(&val, bf, 2); 1969 memcpy(&val, bf, 2);
1970 bus_space_write_2(wdr->cmd_iot, 1970 bus_space_write_2(wdr->cmd_iot,
1971 wdr->cmd_iohs[wd_data], 0, val); 1971 wdr->cmd_iohs[wd_data], 0, val);
1972 bf = (char *)bf + 2; 1972 bf = (char *)bf + 2;
1973 len -= 2; 1973 len -= 2;
1974 } 1974 }
1975 } else { 1975 } else {
1976 if (flags & DRIVE_CAP32) { 1976 if (flags & DRIVE_CAP32) {
1977 while (len > 3) { 1977 while (len > 3) {
1978 uint32_t val; 1978 uint32_t val;
1979 1979
1980 memcpy(&val, bf, 4); 1980 memcpy(&val, bf, 4);
1981 bus_space_write_stream_4(wdr->data32iot, 1981 bus_space_write_stream_4(wdr->data32iot,
1982 wdr->data32ioh, 0, val); 1982 wdr->data32ioh, 0, val);
1983 bf = (char *)bf + 4; 1983 bf = (char *)bf + 4;
1984 len -= 4; 1984 len -= 4;
1985 } 1985 }
1986 } 1986 }
1987 while (len > 1) { 1987 while (len > 1) {
1988 uint16_t val; 1988 uint16_t val;
1989 1989
1990 memcpy(&val, bf, 2); 1990 memcpy(&val, bf, 2);
1991 bus_space_write_stream_2(wdr->cmd_iot, 1991 bus_space_write_stream_2(wdr->cmd_iot,
1992 wdr->cmd_iohs[wd_data], 0, val); 1992 wdr->cmd_iohs[wd_data], 0, val);
1993 bf = (char *)bf + 2; 1993 bf = (char *)bf + 2;
1994 len -= 2; 1994 len -= 2;
1995 } 1995 }
1996 } 1996 }
1997#endif 1997#endif
1998} 1998}