wdc_mb.c revision 1.38.28.1 1 1.38.28.1 jdolecek /* $NetBSD: wdc_mb.c,v 1.38.28.1 2017/04/24 08:48:45 jdolecek 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.38.28.1 jdolecek __KERNEL_RCSID(0, "$NetBSD: wdc_mb.c,v 1.38.28.1 2017/04/24 08:48:45 jdolecek 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 wdc_regs sc_wdc_regs;
76 1.34 tsutsui void *sc_ih;
77 1.1 leo };
78 1.1 leo
79 1.34 tsutsui int wdc_mb_probe(device_t, struct cfdata *, void *);
80 1.34 tsutsui void wdc_mb_attach(device_t, device_t, void *);
81 1.1 leo
82 1.31 cube CFATTACH_DECL_NEW(wdc_mb, sizeof(struct wdc_mb_softc),
83 1.11 thorpej wdc_mb_probe, wdc_mb_attach, NULL, NULL);
84 1.1 leo
85 1.1 leo int
86 1.31 cube wdc_mb_probe(device_t parent, cfdata_t cfp, void *aux)
87 1.1 leo {
88 1.34 tsutsui static int wdc_matched = 0;
89 1.23 thorpej struct ata_channel ch;
90 1.23 thorpej struct wdc_softc wdc;
91 1.23 thorpej struct wdc_regs wdr;
92 1.34 tsutsui int result = 0, i;
93 1.34 tsutsui uint8_t sv_ierb;
94 1.1 leo
95 1.9 leo if ((machineid & ATARI_TT) || strcmp("wdc", aux) || wdc_matched)
96 1.1 leo return 0;
97 1.1 leo if (!atari_realconfig)
98 1.1 leo return 0;
99 1.8 thorpej
100 1.23 thorpej memset(&wdc, 0, sizeof(wdc));
101 1.8 thorpej memset(&ch, 0, sizeof(ch));
102 1.24 thorpej ch.ch_atac = &wdc.sc_atac;
103 1.23 thorpej wdc.regs = &wdr;
104 1.1 leo
105 1.23 thorpej wdr.cmd_iot = wdr.ctl_iot = mb_alloc_bus_space_tag();
106 1.23 thorpej if (wdr.cmd_iot == NULL)
107 1.1 leo return 0;
108 1.29 tsutsui wdr.cmd_iot->stride = 0;
109 1.23 thorpej wdr.cmd_iot->wo_1 = 1;
110 1.1 leo
111 1.25 jdc if (bus_space_map(wdr.cmd_iot, FALCON_WD_BASE, FALCON_WD_LEN, 0,
112 1.25 jdc &wdr.cmd_baseioh))
113 1.29 tsutsui goto out;
114 1.30 tsutsui for (i = 0; i < WDC_NREG; i++) {
115 1.25 jdc if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh,
116 1.29 tsutsui i * 4, 4, &wdr.cmd_iohs[i]) != 0)
117 1.29 tsutsui goto outunmap;
118 1.29 tsutsui }
119 1.29 tsutsui wdc_init_shadow_regs(&ch);
120 1.29 tsutsui
121 1.29 tsutsui if (bus_space_subregion(wdr.cmd_iot, wdr.cmd_baseioh, FALCON_WD_AUX, 4,
122 1.29 tsutsui &wdr.ctl_ioh))
123 1.29 tsutsui goto outunmap;
124 1.1 leo
125 1.1 leo /*
126 1.1 leo * Make sure IDE interrupts are disabled during probing.
127 1.1 leo */
128 1.1 leo sv_ierb = MFP->mf_ierb;
129 1.1 leo MFP->mf_ierb &= ~IB_DINT;
130 1.1 leo
131 1.1 leo /*
132 1.1 leo * Make sure that IDE is turned on on the Falcon.
133 1.1 leo */
134 1.1 leo if (machineid & ATARI_FALCON)
135 1.1 leo ym2149_ser2(0);
136 1.1 leo
137 1.3 bouyer result = wdcprobe(&ch);
138 1.1 leo
139 1.1 leo MFP->mf_ierb = sv_ierb;
140 1.1 leo
141 1.29 tsutsui outunmap:
142 1.25 jdc bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, FALCON_WD_LEN);
143 1.29 tsutsui out:
144 1.23 thorpej mb_free_bus_space_tag(wdr.cmd_iot);
145 1.1 leo
146 1.9 leo if (result)
147 1.9 leo wdc_matched = 1;
148 1.34 tsutsui return result;
149 1.1 leo }
150 1.1 leo
151 1.1 leo void
152 1.31 cube wdc_mb_attach(device_t parent, device_t self, void *aux)
153 1.1 leo {
154 1.31 cube struct wdc_mb_softc *sc = device_private(self);
155 1.23 thorpej struct wdc_regs *wdr;
156 1.25 jdc int i;
157 1.1 leo
158 1.31 cube aprint_normal("\n");
159 1.1 leo
160 1.31 cube sc->sc_wdcdev.sc_atac.atac_dev = self;
161 1.23 thorpej sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
162 1.23 thorpej wdr->cmd_iot = wdr->ctl_iot =
163 1.3 bouyer mb_alloc_bus_space_tag();
164 1.29 tsutsui wdr->cmd_iot->stride = 0;
165 1.23 thorpej wdr->cmd_iot->wo_1 = 1;
166 1.23 thorpej wdr->cmd_iot->abs_rms_2 = read_multi_2_swap;
167 1.23 thorpej wdr->cmd_iot->abs_wms_2 = write_multi_2_swap;
168 1.25 jdc if (bus_space_map(wdr->cmd_iot, FALCON_WD_BASE, FALCON_WD_LEN, 0,
169 1.23 thorpej &wdr->cmd_baseioh)) {
170 1.31 cube aprint_error_dev(self, "couldn't map registers\n");
171 1.1 leo return;
172 1.1 leo }
173 1.29 tsutsui for (i = 0; i < WDC_NREG; i++) {
174 1.25 jdc if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
175 1.29 tsutsui i * 4, 4, &wdr->cmd_iohs[i]) != 0) {
176 1.31 cube aprint_error_dev(self,
177 1.31 cube "couldn't subregion cmd reg %i\n", i);
178 1.25 jdc bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh,
179 1.25 jdc FALCON_WD_LEN);
180 1.25 jdc return;
181 1.25 jdc }
182 1.29 tsutsui }
183 1.29 tsutsui
184 1.29 tsutsui if (bus_space_subregion(wdr->cmd_iot,
185 1.29 tsutsui wdr->cmd_baseioh, FALCON_WD_AUX, 4, &wdr->ctl_ioh)) {
186 1.29 tsutsui bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, FALCON_WD_LEN);
187 1.31 cube aprint_error_dev(self, "couldn't subregion aux reg\n");
188 1.29 tsutsui return;
189 1.29 tsutsui }
190 1.1 leo
191 1.1 leo /*
192 1.1 leo * Play a nasty trick here. Normally we only manipulate the
193 1.1 leo * interrupt *mask*. However to defeat wd_get_parms(), we
194 1.1 leo * disable the interrupts here using the *enable* register.
195 1.1 leo */
196 1.1 leo MFP->mf_ierb &= ~IB_DINT;
197 1.1 leo
198 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_cap |= ATAC_CAP_DATA16 |
199 1.24 thorpej ATAC_CAP_ATA_NOSTREAM;
200 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
201 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_claim_hw = &claim_hw;
202 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_free_hw = &free_hw;
203 1.23 thorpej sc->sc_chanlist[0] = &sc->sc_channel;
204 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
205 1.24 thorpej sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
206 1.38 bouyer sc->sc_wdcdev.wdc_maxdrives = 2;
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.38.28.1 jdolecek sc->sc_channel.ch_queue = ata_queue_alloc(1);
210 1.29 tsutsui wdc_init_shadow_regs(&sc->sc_channel);
211 1.1 leo
212 1.1 leo /*
213 1.1 leo * Setup & enable disk related interrupts.
214 1.1 leo */
215 1.7 leo MFP->mf_ierb |= IB_DINT;
216 1.34 tsutsui MFP->mf_iprb = (uint8_t)~IB_DINT;
217 1.7 leo MFP->mf_imrb |= IB_DINT;
218 1.13 mycroft
219 1.23 thorpej wdcattach(&sc->sc_channel);
220 1.1 leo }
221 1.1 leo
222 1.1 leo /*
223 1.1 leo * Hardware locking
224 1.1 leo */
225 1.1 leo static int wd_lock;
226 1.1 leo
227 1.1 leo static int
228 1.34 tsutsui claim_hw(struct ata_channel *chp, int maysleep)
229 1.1 leo {
230 1.34 tsutsui
231 1.1 leo if (wd_lock != DMA_LOCK_GRANT) {
232 1.1 leo if (wd_lock == DMA_LOCK_REQ) {
233 1.1 leo /*
234 1.1 leo * ST_DMA access is being claimed.
235 1.1 leo */
236 1.1 leo return 0;
237 1.1 leo }
238 1.1 leo if (!st_dmagrab((dma_farg)wdcintr,
239 1.24 thorpej (dma_farg)(maysleep ? NULL : wdcrestart), chp,
240 1.1 leo &wd_lock, 1))
241 1.1 leo return 0;
242 1.1 leo }
243 1.1 leo return 1;
244 1.1 leo }
245 1.1 leo
246 1.1 leo static void
247 1.34 tsutsui free_hw(struct ata_channel *chp)
248 1.1 leo {
249 1.34 tsutsui
250 1.1 leo /*
251 1.1 leo * Flush pending interrupts before giving-up lock
252 1.1 leo */
253 1.34 tsutsui MFP->mf_iprb = (uint8_t)~IB_DINT;
254 1.1 leo
255 1.1 leo /*
256 1.1 leo * Only free the lock on a Falcon. On the Hades, keep it.
257 1.1 leo */
258 1.1 leo /* if (machineid & ATARI_FALCON) */
259 1.24 thorpej st_dmafree(chp, &wd_lock);
260 1.3 bouyer }
261 1.3 bouyer
262 1.3 bouyer /*
263 1.3 bouyer * XXX
264 1.3 bouyer * This piece of uglyness is caused by the fact that the byte lanes of
265 1.3 bouyer * the data-register are swapped on the atari. This works OK for an IDE
266 1.3 bouyer * disk, but turns into a nightmare when used on atapi devices.
267 1.3 bouyer */
268 1.3 bouyer #define calc_addr(base, off, stride, wm) \
269 1.3 bouyer ((u_long)(base) + ((off) << (stride)) + (wm))
270 1.3 bouyer
271 1.3 bouyer static void
272 1.34 tsutsui read_multi_2_swap(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
273 1.34 tsutsui uint16_t *a, bus_size_t c)
274 1.3 bouyer {
275 1.34 tsutsui volatile uint16_t *ba;
276 1.3 bouyer
277 1.34 tsutsui ba = (volatile uint16_t *)calc_addr(h, o, t->stride, t->wo_2);
278 1.3 bouyer for (; c; a++, c--)
279 1.3 bouyer *a = bswap16(*ba);
280 1.3 bouyer }
281 1.3 bouyer
282 1.3 bouyer static void
283 1.34 tsutsui write_multi_2_swap(bus_space_tag_t t, bus_space_handle_t h, bus_size_t o,
284 1.34 tsutsui const uint16_t *a, bus_size_t c)
285 1.3 bouyer {
286 1.34 tsutsui volatile uint16_t *ba;
287 1.3 bouyer
288 1.34 tsutsui ba = (volatile uint16_t *)calc_addr(h, o, t->stride, t->wo_2);
289 1.3 bouyer for (; c; a++, c--)
290 1.3 bouyer *ba = bswap16(*a);
291 1.1 leo }
292