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