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