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