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