xref: /netbsd-src/sys/dev/ic/wdcvar.h (revision ce2c90c7c172d95d2402a5b3d96d8f8e6d138a21)
1 /*	$NetBSD: wdcvar.h,v 1.86 2006/09/30 15:56:18 itohy Exp $	*/
2 
3 /*-
4  * Copyright (c) 1998, 2003, 2004 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 #include <dev/ata/ataconf.h>
45 #include <dev/ic/wdcreg.h>
46 
47 #define	WAITTIME    (10 * hz)    /* time to wait for a completion */
48 	/* this is a lot for hard drives, but not for cdroms */
49 
50 #define WDC_NREG	8 /* number of command registers */
51 #define	WDC_NSHADOWREG	2 /* number of command "shadow" registers */
52 
53 struct wdc_regs {
54 	/* Our registers */
55 	bus_space_tag_t       cmd_iot;
56 	bus_space_handle_t    cmd_baseioh;
57 	bus_space_handle_t    cmd_iohs[WDC_NREG+WDC_NSHADOWREG];
58 	bus_space_tag_t       ctl_iot;
59 	bus_space_handle_t    ctl_ioh;
60 
61 	/* data32{iot,ioh} are only used for 32-bit data xfers */
62 	bus_space_tag_t       data32iot;
63 	bus_space_handle_t    data32ioh;
64 };
65 
66 /*
67  * Per-controller data
68  */
69 struct wdc_softc {
70 	struct atac_softc sc_atac;	/* generic ATA controller info */
71 
72 	struct wdc_regs *regs;		/* register array (per-channel) */
73 
74 	int           cap;		/* controller capabilities */
75 #define WDC_CAPABILITY_NO_EXTRA_RESETS 0x0100 /* only reset once */
76 #define WDC_CAPABILITY_PREATA	0x0200	/* ctrl can be a pre-ata one */
77 #define WDC_CAPABILITY_WIDEREGS 0x0400  /* Ctrl has wide (16bit) registers  */
78 
79 #if NATA_DMA || NATA_PIOBM
80 	/* if WDC_CAPABILITY_DMA set in 'cap' */
81 	void            *dma_arg;
82 	int            (*dma_init)(void *, int, int, void *, size_t, int);
83 	void           (*dma_start)(void *, int, int);
84 	int            (*dma_finish)(void *, int, int, int);
85 #if NATA_PIOBM
86 	void           (*piobm_start)(void *, int, int, int, int, int);
87 	void           (*piobm_done)(void *, int, int);
88 #endif
89 /* flags passed to dma_init */
90 #define WDC_DMA_READ		0x01
91 #define WDC_DMA_IRQW		0x02
92 #define WDC_DMA_LBA48		0x04
93 #define WDC_DMA_PIOBM_ATA	0x08
94 #define WDC_DMA_PIOBM_ATAPI	0x10
95 #if NATA_PIOBM
96 /* flags passed to piobm_start */
97 #define WDC_PIOBM_XFER_IRQ	0x01
98 #endif
99 
100 /* values passed to dma_finish */
101 #define WDC_DMAEND_END	0	/* check for proper end of a DMA xfer */
102 #define WDC_DMAEND_ABRT 1	/* abort a DMA xfer, verbose */
103 #define WDC_DMAEND_ABRT_QUIET 2	/* abort a DMA xfer, quiet */
104 
105 	int		dma_status; /* status returned from dma_finish() */
106 #define WDC_DMAST_NOIRQ	0x01	/* missing IRQ */
107 #define WDC_DMAST_ERR	0x02	/* DMA error */
108 #define WDC_DMAST_UNDER	0x04	/* DMA underrun */
109 #endif	/* NATA_DMA || NATA_PIOBM */
110 
111 	/* Optional callback to select drive. */
112 	void		(*select)(struct ata_channel *,int);
113 
114 	/* Optional callback to ack IRQ. */
115 	void		(*irqack)(struct ata_channel *);
116 
117 	/* Optional callback to perform a bus reset */
118 	void		(*reset)(struct ata_channel *, int);
119 
120 	/* overridden if the backend has a different data transfer method */
121 	void	(*datain_pio)(struct ata_channel *, int, void *, size_t);
122 	void	(*dataout_pio)(struct ata_channel *, int, void *, size_t);
123 };
124 
125 /* Given an ata_channel, get the wdc_softc. */
126 #define	CHAN_TO_WDC(chp)	((struct wdc_softc *)(chp)->ch_atac)
127 
128 /* Given an ata_channel, get the wdc_regs. */
129 #define	CHAN_TO_WDC_REGS(chp)	(&CHAN_TO_WDC(chp)->regs[(chp)->ch_channel])
130 
131 /*
132  * Public functions which can be called by ATA or ATAPI specific parts,
133  * or bus-specific backends.
134  */
135 
136 void	wdc_allocate_regs(struct wdc_softc *);
137 void	wdc_init_shadow_regs(struct ata_channel *);
138 
139 int	wdcprobe(struct ata_channel *);
140 void	wdcattach(struct ata_channel *);
141 int	wdcdetach(struct device *, int);
142 int	wdcactivate(struct device *, enum devact);
143 int	wdcintr(void *);
144 
145 void	wdcrestart(void*);
146 
147 int	wdcwait(struct ata_channel *, int, int, int, int);
148 #define WDCWAIT_OK	0  /* we have what we asked */
149 #define WDCWAIT_TOUT	-1 /* timed out */
150 #define WDCWAIT_THR	1  /* return, the kernel thread has been awakened */
151 
152 void	wdcbit_bucket(struct ata_channel *, int);
153 
154 int	wdc_dmawait(struct ata_channel *, struct ata_xfer *, int);
155 void	wdccommand(struct ata_channel *, u_int8_t, u_int8_t, u_int16_t,
156 		   u_int8_t, u_int8_t, u_int8_t, u_int8_t);
157 void	wdccommandext(struct ata_channel *, u_int8_t, u_int8_t, u_int64_t,
158 		      u_int16_t);
159 void	wdccommandshort(struct ata_channel *, int, int);
160 void	wdctimeout(void *arg);
161 void	wdc_reset_drive(struct ata_drive_datas *, int);
162 void	wdc_reset_channel(struct ata_channel *, int);
163 void	wdc_do_reset(struct ata_channel *, int);
164 
165 int	wdc_exec_command(struct ata_drive_datas *, struct ata_command*);
166 
167 /*
168  * ST506 spec says that if READY or SEEKCMPLT go off, then the read or write
169  * command is aborted.
170  */
171 #define wdc_wait_for_drq(chp, timeout, flags) \
172 		wdcwait((chp), WDCS_DRQ, WDCS_DRQ, (timeout), (flags))
173 #define wdc_wait_for_unbusy(chp, timeout, flags) \
174 		wdcwait((chp), 0, 0, (timeout), (flags))
175 #define wdc_wait_for_ready(chp, timeout, flags) \
176 		wdcwait((chp), WDCS_DRDY, WDCS_DRDY, (timeout), (flags))
177 
178 /* ATA/ATAPI specs says a device can take 31s to reset */
179 #define WDC_RESET_WAIT 31000
180 
181 void	wdc_atapibus_attach(struct atabus_softc *);
182 
183 #endif /* _DEV_IC_WDCVAR_H_ */
184