wdcvar.h revision 1.65 1 /* $NetBSD: wdcvar.h,v 1.65 2004/08/12 04:57:19 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Charles M. Hannum, by Onno van der Linden and by Manuel Bouyer.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #ifndef _DEV_IC_WDCVAR_H_
40 #define _DEV_IC_WDCVAR_H_
41
42 #include <sys/callout.h>
43
44 /* XXX For scsipi_adapter and scsipi_channel. */
45 #include <dev/scsipi/scsipi_all.h>
46 #include <dev/scsipi/atapiconf.h>
47
48 #include <dev/ic/wdcreg.h>
49
50 #define WAITTIME (10 * hz) /* time to wait for a completion */
51 /* this is a lot for hard drives, but not for cdroms */
52
53 #define WDC_NREG 8 /* number of command registers */
54 #define WDC_NSHADOWREG 2 /* number of command "shadow" registers */
55
56 /*
57 * Per-channel data
58 */
59 struct wdc_channel {
60 struct callout ch_callout; /* callout handle */
61 int ch_channel; /* location */
62 struct wdc_softc *ch_wdc; /* controller's softc */
63
64 /* Our registers */
65 bus_space_tag_t cmd_iot;
66 bus_space_handle_t cmd_baseioh;
67 bus_space_handle_t cmd_iohs[WDC_NREG+WDC_NSHADOWREG];
68 bus_space_tag_t ctl_iot;
69 bus_space_handle_t ctl_ioh;
70 bus_space_tag_t data32iot;
71 bus_space_handle_t data32ioh;
72
73 /* Our state */
74 volatile int ch_flags;
75 #define WDCF_SHUTDOWN 0x02 /* channel is shutting down */
76 #define WDCF_IRQ_WAIT 0x10 /* controller is waiting for irq */
77 #define WDCF_DMA_WAIT 0x20 /* controller is waiting for DMA */
78 #define WDCF_DISABLED 0x80 /* channel is disabled */
79 #define WDCF_TH_RUN 0x100 /* the kenrel thread is working */
80 #define WDCF_TH_RESET 0x200 /* someone ask the thread to reset */
81 u_int8_t ch_status; /* copy of status register */
82 u_int8_t ch_error; /* copy of error register */
83
84 /* for the reset callback */
85 int ch_reset_flags;
86
87 /* per-drive info */
88 struct ata_drive_datas ch_drive[2];
89
90 struct device *atabus; /* self */
91
92 /* ATAPI children */
93 struct device *atapibus;
94 struct scsipi_channel ch_atapi_channel;
95
96 /* ATA children */
97 struct device *ata_drives[2];
98
99 /*
100 * Channel queues. May be the same for all channels, if hw
101 * channels are not independent.
102 */
103 struct ata_queue *ch_queue;
104
105 /* The channel kernel thread */
106 struct proc *ch_thread;
107 };
108
109 /*
110 * Per-controller data
111 */
112 struct wdc_softc {
113 struct device sc_dev; /* generic device info */
114
115 int cap; /* controller capabilities */
116 #define WDC_CAPABILITY_DATA16 0x0001 /* can do 16-bit data access */
117 #define WDC_CAPABILITY_DATA32 0x0002 /* can do 32-bit data access */
118 #define WDC_CAPABILITY_MODE 0x0004 /* controller knows its PIO/DMA modes */
119 #define WDC_CAPABILITY_DMA 0x0008 /* DMA */
120 #define WDC_CAPABILITY_UDMA 0x0010 /* Ultra-DMA/33 */
121 #define WDC_CAPABILITY_HWLOCK 0x0020 /* Needs to lock HW */
122 #define WDC_CAPABILITY_ATA_NOSTREAM 0x0040 /* Don't use stream funcs on ATA */
123 #define WDC_CAPABILITY_ATAPI_NOSTREAM 0x0080 /* Don't use stream f on ATAPI */
124 #define WDC_CAPABILITY_NO_EXTRA_RESETS 0x0100 /* only reset once */
125 #define WDC_CAPABILITY_PREATA 0x0200 /* ctrl can be a pre-ata one */
126 #define WDC_CAPABILITY_IRQACK 0x0400 /* callback to ack interrupt */
127 #define WDC_CAPABILITY_NOIRQ 0x1000 /* Controller never interrupts */
128 #define WDC_CAPABILITY_SELECT 0x2000 /* Controller selects target */
129 #define WDC_CAPABILITY_RAID 0x4000 /* Controller "supports" RAID */
130 u_int8_t PIO_cap; /* highest PIO mode supported */
131 u_int8_t DMA_cap; /* highest DMA mode supported */
132 u_int8_t UDMA_cap; /* highest UDMA mode supported */
133 int nchannels; /* # channels on this controller */
134 struct wdc_channel **channels; /* channel-specific data (array) */
135
136 /*
137 * The reference count here is used for both IDE and ATAPI devices.
138 */
139 struct atapi_adapter sc_atapi_adapter;
140
141 /* Function used to probe for drives. */
142 void (*drv_probe)(struct wdc_channel *);
143
144 /* if WDC_CAPABILITY_DMA set in 'cap' */
145 void *dma_arg;
146 int (*dma_init)(void *, int, int, void *, size_t, int);
147 void (*dma_start)(void *, int, int);
148 int (*dma_finish)(void *, int, int, int);
149 /* flags passed to dma_init */
150 #define WDC_DMA_READ 0x01
151 #define WDC_DMA_IRQW 0x02
152 #define WDC_DMA_LBA48 0x04
153
154 /* values passed to dma_finish */
155 #define WDC_DMAEND_END 0 /* check for proper end of a DMA xfer */
156 #define WDC_DMAEND_ABRT 1 /* abort a DMA xfer, verbose */
157 #define WDC_DMAEND_ABRT_QUIET 2 /* abort a DMA xfer, quiet */
158
159 int dma_status; /* status returned from dma_finish() */
160 #define WDC_DMAST_NOIRQ 0x01 /* missing IRQ */
161 #define WDC_DMAST_ERR 0x02 /* DMA error */
162 #define WDC_DMAST_UNDER 0x04 /* DMA underrun */
163
164 /* if WDC_CAPABILITY_HWLOCK set in 'cap' */
165 int (*claim_hw)(void *, int);
166 void (*free_hw)(void *);
167
168 /* if WDC_CAPABILITY_MODE set in 'cap' */
169 void (*set_modes)(struct wdc_channel *);
170
171 /* if WDC_CAPABILITY_SELECT set in 'cap' */
172 void (*select)(struct wdc_channel *,int);
173
174 /* if WDC_CAPABILITY_IRQACK set in 'cap' */
175 void (*irqack)(struct wdc_channel *);
176
177 /* overridden if the backend has a different data transfer method */
178 void (*datain_pio)(struct wdc_channel *, int, void *, size_t);
179 void (*dataout_pio)(struct wdc_channel *, int, void *, size_t);
180 };
181
182 /*
183 * Public functions which can be called by ATA or ATAPI specific parts,
184 * or bus-specific backends.
185 */
186
187 void wdc_init_shadow_regs(struct wdc_channel *);
188
189 int wdcprobe(struct wdc_channel *);
190 void wdcattach(struct wdc_channel *);
191 int wdcdetach(struct device *, int);
192 int wdcactivate(struct device *, enum devact);
193 int wdcintr(void *);
194 void wdc_exec_xfer(struct wdc_channel *, struct ata_xfer *);
195
196 struct ata_xfer *wdc_get_xfer(int); /* int = WDC_NOSLEEP/CANSLEEP */
197 #define WDC_CANSLEEP 0x00
198 #define WDC_NOSLEEP 0x01
199
200 void wdc_free_xfer (struct wdc_channel *, struct ata_xfer *);
201 void wdcstart(struct wdc_channel *);
202 void wdcrestart(void*);
203
204 int wdcreset(struct wdc_channel *, int);
205 #define RESET_POLL 1
206 #define RESET_SLEEP 0 /* wdcreset will use tsleep() */
207
208 int wdcwait(struct wdc_channel *, int, int, int, int);
209 #define WDCWAIT_OK 0 /* we have what we asked */
210 #define WDCWAIT_TOUT -1 /* timed out */
211 #define WDCWAIT_THR 1 /* return, the kernel thread has been awakened */
212
213 void wdc_datain_pio(struct wdc_channel *, int, void *, size_t);
214 void wdc_dataout_pio(struct wdc_channel *, int, void *, size_t);
215 void wdcbit_bucket(struct wdc_channel *, int);
216
217 int wdc_dmawait(struct wdc_channel *, struct ata_xfer *, int);
218 void wdccommand(struct wdc_channel *, u_int8_t, u_int8_t, u_int16_t,
219 u_int8_t, u_int8_t, u_int8_t, u_int8_t);
220 void wdccommandext(struct wdc_channel *, u_int8_t, u_int8_t, u_int64_t,
221 u_int16_t);
222 void wdccommandshort(struct wdc_channel *, int, int);
223 void wdctimeout(void *arg);
224 void wdc_reset_drive(struct ata_drive_datas *, int);
225 void wdc_reset_channel(struct wdc_channel *, int);
226
227 int wdc_exec_command(struct ata_drive_datas *, struct ata_command*);
228 #define WDC_COMPLETE 0x01
229 #define WDC_QUEUED 0x02
230 #define WDC_TRY_AGAIN 0x03
231
232 int wdc_addref(struct wdc_channel *);
233 void wdc_delref(struct wdc_channel *);
234 void wdc_kill_pending(struct ata_drive_datas *);
235
236 void wdc_print_modes (struct wdc_channel *);
237 void wdc_probe_caps(struct ata_drive_datas*);
238
239 /*
240 * ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
241 * command is aborted.
242 */
243 #define wdc_wait_for_drq(chp, timeout, flags) \
244 wdcwait((chp), WDCS_DRQ, WDCS_DRQ, (timeout), (flags))
245 #define wdc_wait_for_unbusy(chp, timeout, flags) \
246 wdcwait((chp), 0, 0, (timeout), (flags))
247 #define wdc_wait_for_ready(chp, timeout, flags) \
248 wdcwait((chp), WDCS_DRDY, WDCS_DRDY, (timeout), (flags))
249
250 /* ATA/ATAPI specs says a device can take 31s to reset */
251 #define WDC_RESET_WAIT 31000
252
253 void wdc_atapibus_attach(struct atabus_softc *);
254
255 /* XXX */
256 struct atabus_softc;
257 void atabusconfig(struct atabus_softc *);
258
259 #endif /* _DEV_IC_WDCVAR_H_ */
260