wdc_mb.c revision 1.28.48.2 1 1.28.48.2 matt /* wdc_mb.c,v 1.28.48.1 2008/01/09 01:45:31 matt Exp */
2 1.1 leo
3 1.2 mycroft /*-
4 1.13 mycroft * Copyright (c) 1998, 2003 The NetBSD Foundation, Inc.
5 1.2 mycroft * All rights reserved.
6 1.1 leo *
7 1.2 mycroft * This code is derived from software contributed to The NetBSD Foundation
8 1.2 mycroft * by Charles M. Hannum and by Onno van der Linden.
9 1.1 leo *
10 1.1 leo * Redistribution and use in source and binary forms, with or without
11 1.1 leo * modification, are permitted provided that the following conditions
12 1.1 leo * are met:
13 1.1 leo * 1. Redistributions of source code must retain the above copyright
14 1.1 leo * notice, this list of conditions and the following disclaimer.
15 1.1 leo * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 leo * notice, this list of conditions and the following disclaimer in the
17 1.1 leo * documentation and/or other materials provided with the distribution.
18 1.1 leo * 3. All advertising materials mentioning features or use of this software
19 1.1 leo * must display the following acknowledgement:
20 1.2 mycroft * This product includes software developed by the NetBSD
21 1.2 mycroft * Foundation, Inc. and its contributors.
22 1.2 mycroft * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 mycroft * contributors may be used to endorse or promote products derived
24 1.2 mycroft * from this software without specific prior written permission.
25 1.1 leo *
26 1.2 mycroft * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 mycroft * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 mycroft * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 mycroft * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 mycroft * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 mycroft * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 mycroft * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 mycroft * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 mycroft * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 mycroft * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 mycroft * POSSIBILITY OF SUCH DAMAGE.
37 1.1 leo */
38 1.12 lukem
39 1.12 lukem #include <sys/cdefs.h>
40 1.28.48.2 matt __KERNEL_RCSID(0, "wdc_mb.c,v 1.28.48.1 2008/01/09 01:45:31 matt Exp");
41 1.1 leo
42 1.1 leo #include <sys/types.h>
43 1.1 leo #include <sys/param.h>
44 1.1 leo #include <sys/systm.h>
45 1.3 bouyer #include <sys/malloc.h>
46 1.1 leo #include <sys/device.h>
47 1.1 leo
48 1.28 dsl #include <sys/bswap.h>
49 1.1 leo #include <machine/cpu.h>
50 1.1 leo #include <machine/bus.h>
51 1.1 leo #include <machine/iomap.h>
52 1.1 leo #include <machine/mfp.h>
53 1.1 leo #include <machine/dma.h>
54 1.1 leo
55 1.3 bouyer #include <dev/ata/atavar.h>
56 1.1 leo #include <dev/ic/wdcvar.h>
57 1.1 leo
58 1.1 leo #include <m68k/asm_single.h>
59 1.1 leo
60 1.1 leo #include <atari/dev/ym2149reg.h>
61 1.1 leo #include <atari/atari/device.h>
62 1.1 leo
63 1.25 jdc /* Falcon IDE register locations (base and offsets). */
64 1.25 jdc #define FALCON_WD_BASE 0xfff00000
65 1.25 jdc #define FALCON_WD_LEN 0x40
66 1.28.48.1 matt #define FALCON_WD_AUX 0x38
67 1.25 jdc
68 1.1 leo /*
69 1.1 leo * XXX This code currently doesn't even try to allow 32-bit data port use.
70 1.1 leo */
71 1.28.48.2 matt static int claim_hw (struct ata_channel *, int);
72 1.28.48.2 matt static void free_hw (struct ata_channel *);
73 1.28.48.2 matt static void read_multi_2_swap (bus_space_tag_t, bus_space_handle_t,
74 1.28.48.2 matt bus_size_t, u_int16_t *, bus_size_t);
75 1.28.48.2 matt static void write_multi_2_swap (bus_space_tag_t, bus_space_handle_t,
76 1.28.48.2 matt bus_size_t, const u_int16_t *, bus_size_t);
77 1.1 leo
78 1.1 leo struct wdc_mb_softc {
79 1.1 leo struct wdc_softc sc_wdcdev;
80 1.23 thorpej struct ata_channel *sc_chanlist[1];
81 1.23 thorpej struct ata_channel sc_channel;
82 1.23 thorpej struct ata_queue sc_chqueue;
83 1.23 thorpej struct wdc_regs sc_wdc_regs;
84 1.1 leo void *sc_ih;
85 1.1 leo };
86 1.1 leo
87 1.28.48.2 matt int wdc_mb_probe (device_t, struct cfdata *, void *);
88 1.28.48.2 matt void wdc_mb_attach (device_t, device_t, void *);
89 1.1 leo
90 1.28.48.2 matt CFATTACH_DECL_NEW(wdc_mb, sizeof(struct wdc_mb_softc),
91 1.11 thorpej wdc_mb_probe, wdc_mb_attach, NULL, NULL);
92 1.1 leo
93 1.1 leo int
94 1.28.48.2 matt wdc_mb_probe(device_t parent, cfdata_t cfp, void *aux)
95 1.1 leo {
96 1.9 leo static int wdc_matched = 0;
97 1.23 thorpej struct ata_channel ch;
98 1.23 thorpej struct wdc_softc wdc;
99 1.23 thorpej struct wdc_regs wdr;
100 1.25 jdc int result = 0, i;
101 1.1 leo u_char sv_ierb;
102 1.1 leo
103 1.9 leo if ((machineid & ATARI_TT) || strcmp("wdc", aux) || wdc_matched)
104 1.1 leo return 0;
105 1.1 leo if (!atari_realconfig)
106 1.1 leo return 0;
107 1.8 thorpej
108 1.23 thorpej memset(&wdc, 0, sizeof(wdc));
109 1.8 thorpej memset(&ch, 0, sizeof(ch));
110 1.24 thorpej ch.ch_atac = &wdc.sc_atac;
111 1.23 thorpej wdc.regs = &wdr;
112 1.1 leo
113 1.23 thorpej wdr.cmd_iot = wdr.ctl_iot = mb_alloc_bus_space_tag();
114 1.23 thorpej if (wdr.cmd_iot == NULL)
115 1.1 leo return 0;
116 1.28.48.1 matt wdr.cmd_iot->stride = 0;
117 1.23 thorpej wdr.cmd_iot->wo_1 = 1;
118 1.1 leo
119 1.25 jdc if (bus_space_map(wdr.cmd_iot, FALCON_WD_BASE, FALCON_WD_LEN, 0,
120 1.25 jdc &wdr.cmd_baseioh))
121 1.28.48.1 matt goto out;
122 1.28.48.1 matt for (i = 0; i < WDC_NREG; i++) {
123 1.25 jdc if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh,
124 1.28.48.1 matt i * 4, 4, &wdr.cmd_iohs[i]) != 0)
125 1.28.48.1 matt goto outunmap;
126 1.28.48.1 matt }
127 1.28.48.1 matt wdc_init_shadow_regs(&ch);
128 1.28.48.1 matt
129 1.28.48.1 matt if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, FALCON_WD_AUX, 4,
130 1.28.48.1 matt &wdr.ctl_ioh))
131 1.28.48.1 matt goto outunmap;
132 1.1 leo
133 1.1 leo /*
134 1.1 leo * Make sure IDE interrupts are disabled during probing.
135 1.1 leo */
136 1.1 leo sv_ierb = MFP->mf_ierb;
137 1.1 leo MFP->mf_ierb &= ~IB_DINT;
138 1.1 leo
139 1.1 leo /*
140 1.1 leo * Make sure that IDE is turned on on the Falcon.
141 1.1 leo */
142 1.1 leo if (machineid & ATARI_FALCON)
143 1.1 leo ym2149_ser2(0);
144 1.1 leo
145 1.3 bouyer result = wdcprobe(&ch);
146 1.1 leo
147 1.1 leo MFP->mf_ierb = sv_ierb;
148 1.1 leo
149 1.28.48.1 matt outunmap:
150 1.25 jdc bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, FALCON_WD_LEN);
151 1.28.48.1 matt out:
152 1.23 thorpej mb_free_bus_space_tag(wdr.cmd_iot);
153 1.1 leo
154 1.9 leo if (result)
155 1.9 leo wdc_matched = 1;
156 1.1 leo return (result);
157 1.1 leo }
158 1.1 leo
159 1.1 leo void
160 1.28.48.2 matt wdc_mb_attach(device_t parent, device_t self, void *aux)
161 1.1 leo {
162 1.28.48.2 matt struct wdc_mb_softc *sc = device_private(self);
163 1.23 thorpej struct wdc_regs *wdr;
164 1.25 jdc int i;
165 1.1 leo
166 1.28.48.2 matt aprint_normal("\n");
167 1.1 leo
168 1.28.48.2 matt sc->sc_wdcdev.sc_atac.atac_dev = self;
169 1.23 thorpej sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
170 1.23 thorpej wdr->cmd_iot = wdr->ctl_iot =
171 1.3 bouyer mb_alloc_bus_space_tag();
172 1.28.48.1 matt wdr->cmd_iot->stride = 0;
173 1.23 thorpej wdr->cmd_iot->wo_1 = 1;
174 1.23 thorpej wdr->cmd_iot->abs_rms_2 = read_multi_2_swap;
175 1.23 thorpej wdr->cmd_iot->abs_wms_2 = write_multi_2_swap;
176 1.25 jdc if (bus_space_map(wdr->cmd_iot, FALCON_WD_BASE, FALCON_WD_LEN, 0,
177 1.23 thorpej &wdr->cmd_baseioh)) {
178 1.28.48.2 matt aprint_error_dev(self, "couldn't map registers\n");
179 1.1 leo return;
180 1.1 leo }
181 1.28.48.1 matt for (i = 0; i < WDC_NREG; i++) {
182 1.25 jdc if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
183 1.28.48.1 matt i * 4, 4, &wdr->cmd_iohs[i]) != 0) {
184 1.28.48.2 matt aprint_error_dev(self,
185 1.28.48.2 matt "couldn't subregion cmd reg %i\n", i);
186 1.25 jdc bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
187 1.25 jdc FALCON_WD_LEN);
188 1.25 jdc return;
189 1.25 jdc }
190 1.28.48.1 matt }
191 1.28.48.1 matt
192 1.28.48.1 matt if (bus_space_subregion(wdr->cmd_iot,
193 1.28.48.1 matt wdr->cmd_baseioh, FALCON_WD_AUX, 4, &wdr->ctl_ioh)) {
194 1.28.48.1 matt bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, FALCON_WD_LEN);
195 1.28.48.2 matt aprint_error_dev(self, "couldn't subregion aux reg\n");
196 1.28.48.1 matt return;
197 1.28.48.1 matt }
198 1.1 leo
199 1.1 leo /*
200 1.1 leo * Play a nasty trick here. Normally we only manipulate the
201 1.1 leo * interrupt *mask*. However to defeat wd_get_parms(), we
202 1.1 leo * disable the interrupts here using the *enable* register.
203 1.1 leo */
204 1.1 leo MFP->mf_ierb &= ~IB_DINT;
205 1.1 leo
206 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 |
207 1.24 thorpej ATAC_CAP_ATA_NOSTREAM;
208 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
209 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_claim_hw = &claim_hw;
210 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_free_hw = &free_hw;
211 1.23 thorpej sc->sc_chanlist[0] = &sc->sc_channel;
212 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
213 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
214 1.23 thorpej sc->sc_channel.ch_channel = 0;
215 1.24 thorpej sc->sc_channel.ch_atac = &sc->sc_wdcdev.sc_atac;
216 1.23 thorpej sc->sc_channel.ch_queue = &sc->sc_chqueue;
217 1.27 bouyer sc->sc_channel.ch_ndrive = 2;
218 1.28.48.1 matt wdc_init_shadow_regs(&sc->sc_channel);
219 1.1 leo
220 1.1 leo /*
221 1.1 leo * Setup & enable disk related interrupts.
222 1.1 leo */
223 1.7 leo MFP->mf_ierb |= IB_DINT;
224 1.7 leo MFP->mf_iprb = (u_int8_t)~IB_DINT;
225 1.7 leo MFP->mf_imrb |= IB_DINT;
226 1.13 mycroft
227 1.23 thorpej wdcattach(&sc->sc_channel);
228 1.1 leo }
229 1.1 leo
230 1.1 leo /*
231 1.1 leo * Hardware locking
232 1.1 leo */
233 1.1 leo static int wd_lock;
234 1.1 leo
235 1.1 leo static int
236 1.24 thorpej claim_hw(chp, maysleep)
237 1.24 thorpej struct ata_channel *chp;
238 1.1 leo int maysleep;
239 1.1 leo {
240 1.1 leo if (wd_lock != DMA_LOCK_GRANT) {
241 1.1 leo if (wd_lock == DMA_LOCK_REQ) {
242 1.1 leo /*
243 1.1 leo * ST_DMA access is being claimed.
244 1.1 leo */
245 1.1 leo return 0;
246 1.1 leo }
247 1.1 leo if (!st_dmagrab((dma_farg)wdcintr,
248 1.24 thorpej (dma_farg)(maysleep ? NULL : wdcrestart), chp,
249 1.1 leo &wd_lock, 1))
250 1.1 leo return 0;
251 1.1 leo }
252 1.1 leo return 1;
253 1.1 leo }
254 1.1 leo
255 1.1 leo static void
256 1.24 thorpej free_hw(chp)
257 1.24 thorpej struct ata_channel *chp;
258 1.1 leo {
259 1.1 leo /*
260 1.1 leo * Flush pending interrupts before giving-up lock
261 1.1 leo */
262 1.7 leo MFP->mf_iprb = (u_int8_t)~IB_DINT;
263 1.1 leo
264 1.1 leo /*
265 1.1 leo * Only free the lock on a Falcon. On the Hades, keep it.
266 1.1 leo */
267 1.1 leo /* if (machineid & ATARI_FALCON) */
268 1.24 thorpej st_dmafree(chp, &wd_lock);
269 1.3 bouyer }
270 1.3 bouyer
271 1.3 bouyer /*
272 1.3 bouyer * XXX
273 1.3 bouyer * This piece of uglyness is caused by the fact that the byte lanes of
274 1.3 bouyer * the data-register are swapped on the atari. This works OK for an IDE
275 1.3 bouyer * disk, but turns into a nightmare when used on atapi devices.
276 1.3 bouyer */
277 1.3 bouyer #define calc_addr(base, off, stride, wm) \
278 1.3 bouyer ((u_long)(base) + ((off) << (stride)) + (wm))
279 1.3 bouyer
280 1.3 bouyer static void
281 1.3 bouyer read_multi_2_swap(t, h, o, a, c)
282 1.3 bouyer bus_space_tag_t t;
283 1.3 bouyer bus_space_handle_t h;
284 1.3 bouyer bus_size_t o, c;
285 1.3 bouyer u_int16_t *a;
286 1.3 bouyer {
287 1.3 bouyer u_int16_t *ba;
288 1.3 bouyer
289 1.3 bouyer ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
290 1.3 bouyer for (; c; a++, c--)
291 1.3 bouyer *a = bswap16(*ba);
292 1.3 bouyer }
293 1.3 bouyer
294 1.3 bouyer static void
295 1.3 bouyer write_multi_2_swap(t, h, o, a, c)
296 1.3 bouyer bus_space_tag_t t;
297 1.3 bouyer bus_space_handle_t h;
298 1.3 bouyer bus_size_t o, c;
299 1.3 bouyer const u_int16_t *a;
300 1.3 bouyer {
301 1.3 bouyer u_int16_t *ba;
302 1.3 bouyer
303 1.3 bouyer ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
304 1.3 bouyer for (; c; a++, c--)
305 1.3 bouyer *ba = bswap16(*a);
306 1.1 leo }
307