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