wdc_mb.c revision 1.10
1/*	$NetBSD: wdc_mb.c,v 1.10 2002/09/27 20:31:00 thorpej Exp $	*/
2
3/*-
4 * Copyright (c) 1998 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 * 3. All advertising materials mentioning features or use of this software
19 *    must display the following acknowledgement:
20 *        This product includes software developed by the NetBSD
21 *        Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 *    contributors may be used to endorse or promote products derived
24 *    from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39#include <sys/types.h>
40#include <sys/param.h>
41#include <sys/systm.h>
42#include <sys/malloc.h>
43#include <sys/device.h>
44
45#include <machine/bswap.h>
46#include <machine/cpu.h>
47#include <machine/bus.h>
48#include <machine/iomap.h>
49#include <machine/mfp.h>
50#include <machine/dma.h>
51
52#include <dev/ata/atavar.h>
53#include <dev/ic/wdcvar.h>
54
55#include <m68k/asm_single.h>
56
57#include <atari/dev/ym2149reg.h>
58#include <atari/atari/device.h>
59
60/*
61 * XXX This code currently doesn't even try to allow 32-bit data port use.
62 */
63static int	claim_hw __P((void *, int));
64static void	free_hw __P((void *));
65static void	read_multi_2_swap __P((bus_space_tag_t, bus_space_handle_t,
66				bus_size_t, u_int16_t *, bus_size_t));
67static void	write_multi_2_swap __P((bus_space_tag_t, bus_space_handle_t,
68				bus_size_t, const u_int16_t *, bus_size_t));
69
70struct wdc_mb_softc {
71	struct wdc_softc sc_wdcdev;
72	struct	channel_softc *wdc_chanptr;
73	struct  channel_softc wdc_channel;
74	void	*sc_ih;
75};
76
77int	wdc_mb_probe	__P((struct device *, struct cfdata *, void *));
78void	wdc_mb_attach	__P((struct device *, struct device *, void *));
79
80const struct cfattach wdc_mb_ca = {
81	sizeof(struct wdc_mb_softc), wdc_mb_probe, wdc_mb_attach
82};
83
84int
85wdc_mb_probe(parent, cfp, aux)
86	struct device *parent;
87	struct cfdata *cfp;
88	void *aux;
89{
90	static int	wdc_matched = 0;
91	struct channel_softc ch;
92	int	result = 0;
93	u_char	sv_ierb;
94
95	if ((machineid & ATARI_TT) || strcmp("wdc", aux) || wdc_matched)
96		return 0;
97	if (!atari_realconfig)
98		return 0;
99
100	memset(&ch, 0, sizeof(ch));
101
102	ch.cmd_iot = ch.ctl_iot = mb_alloc_bus_space_tag();
103	if (ch.cmd_iot == NULL)
104		return 0;
105	ch.cmd_iot->stride = 2;
106	ch.cmd_iot->wo_1   = 1;
107
108	if (bus_space_map(ch.cmd_iot, 0xfff00000, 0x40, 0, &ch.cmd_ioh))
109		return 0;
110	if (bus_space_subregion(ch.cmd_iot, ch.cmd_ioh, 0x38, 1, &ch.ctl_ioh))
111		return 0;
112
113	/*
114	 * Make sure IDE interrupts are disabled during probing.
115	 */
116	sv_ierb = MFP->mf_ierb;
117	MFP->mf_ierb &= ~IB_DINT;
118
119	/*
120	 * Make sure that IDE is turned on on the Falcon.
121	 */
122	if (machineid & ATARI_FALCON)
123		ym2149_ser2(0);
124
125	result = wdcprobe(&ch);
126
127	MFP->mf_ierb = sv_ierb;
128
129	bus_space_unmap(ch.cmd_iot,  ch.cmd_ioh, 0x40);
130	mb_free_bus_space_tag(ch.cmd_iot);
131
132	if (result)
133		wdc_matched = 1;
134	return (result);
135}
136
137void
138wdc_mb_attach(parent, self, aux)
139	struct device *parent, *self;
140	void *aux;
141{
142	struct wdc_mb_softc *sc = (void *)self;
143
144	printf("\n");
145
146	sc->wdc_channel.cmd_iot = sc->wdc_channel.ctl_iot =
147	    mb_alloc_bus_space_tag();
148	sc->wdc_channel.cmd_iot->stride = 2;
149	sc->wdc_channel.cmd_iot->wo_1   = 1;
150	sc->wdc_channel.cmd_iot->abs_rms_2 = read_multi_2_swap;
151	sc->wdc_channel.cmd_iot->abs_wms_2 = write_multi_2_swap;
152	if (bus_space_map(sc->wdc_channel.cmd_iot, 0xfff00000, 0x40, 0,
153			  &sc->wdc_channel.cmd_ioh)) {
154		printf("%s: couldn't map registers\n",
155		    sc->sc_wdcdev.sc_dev.dv_xname);
156		return;
157	}
158	if (bus_space_subregion(sc->wdc_channel.cmd_iot,
159	    sc->wdc_channel.cmd_ioh, 0x38, 1, &sc->wdc_channel.ctl_ioh))
160		return;
161
162	/*
163	 * Play a nasty trick here. Normally we only manipulate the
164	 * interrupt *mask*. However to defeat wd_get_parms(), we
165	 * disable the interrupts here using the *enable* register.
166	 */
167	MFP->mf_ierb &= ~IB_DINT;
168
169	sc->sc_wdcdev.cap |= WDC_CAPABILITY_DATA16 | WDC_CAPABILITY_HWLOCK |
170	    WDC_CAPABILITY_ATA_NOSTREAM;
171	sc->sc_wdcdev.PIO_cap = 0;
172	sc->sc_wdcdev.claim_hw = &claim_hw;
173	sc->sc_wdcdev.free_hw  = &free_hw;
174	sc->wdc_chanptr = &sc->wdc_channel;
175	sc->sc_wdcdev.channels = &sc->wdc_chanptr;
176	sc->sc_wdcdev.nchannels = 1;
177	sc->wdc_channel.channel = 0;
178	sc->wdc_channel.wdc = &sc->sc_wdcdev;
179	sc->wdc_channel.ch_queue = malloc(sizeof(struct channel_queue),
180	    M_DEVBUF, M_NOWAIT);
181	if (sc->wdc_channel.ch_queue == NULL) {
182	    printf("%s: can't allocate memory for command queue",
183		sc->sc_wdcdev.sc_dev.dv_xname);
184	    return;
185	}
186	wdcattach(&sc->wdc_channel);
187
188	/*
189	 * Setup & enable disk related interrupts.
190	 */
191	MFP->mf_ierb |= IB_DINT;
192	MFP->mf_iprb  = (u_int8_t)~IB_DINT;
193	MFP->mf_imrb |= IB_DINT;
194}
195
196/*
197 * Hardware locking
198 */
199static int	wd_lock;
200
201static int
202claim_hw(softc, maysleep)
203void *softc;
204int  maysleep;
205{
206	if (wd_lock != DMA_LOCK_GRANT) {
207		if (wd_lock == DMA_LOCK_REQ) {
208			/*
209			 * ST_DMA access is being claimed.
210			 */
211			return 0;
212		}
213		if (!st_dmagrab((dma_farg)wdcintr,
214		    (dma_farg)(maysleep ? NULL : wdcrestart), softc,
215		    &wd_lock, 1))
216			return 0;
217	}
218	return 1;
219}
220
221static void
222free_hw(softc)
223void *softc;
224{
225	/*
226	 * Flush pending interrupts before giving-up lock
227	 */
228	MFP->mf_iprb = (u_int8_t)~IB_DINT;
229
230	/*
231	 * Only free the lock on a Falcon. On the Hades, keep it.
232	 */
233/*	if (machineid & ATARI_FALCON) */
234		st_dmafree(softc, &wd_lock);
235}
236
237/*
238 * XXX
239 * This piece of uglyness is caused by the fact that the byte lanes of
240 * the data-register are swapped on the atari. This works OK for an IDE
241 * disk, but turns into a nightmare when used on atapi devices.
242 */
243#define calc_addr(base, off, stride, wm)	\
244	((u_long)(base) + ((off) << (stride)) + (wm))
245
246static void
247read_multi_2_swap(t, h, o, a, c)
248	bus_space_tag_t		t;
249	bus_space_handle_t	h;
250	bus_size_t		o, c;
251	u_int16_t		*a;
252{
253	u_int16_t	*ba;
254
255	ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
256	for (; c; a++, c--)
257		*a = bswap16(*ba);
258}
259
260static void
261write_multi_2_swap(t, h, o, a, c)
262	bus_space_tag_t		t;
263	bus_space_handle_t	h;
264	bus_size_t		o, c;
265	const u_int16_t		*a;
266{
267	u_int16_t	*ba;
268
269	ba = (u_int16_t *)calc_addr(h, o, t->stride, t->wo_2);
270	for (; c; a++, c--)
271		*ba = bswap16(*a);
272}
273