wdc_buddha.c revision 1.3 1 /* $NetBSD: wdc_buddha.c,v 1.3 2008/03/18 20:46:35 cube Exp $ */
2
3 /*-
4 * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Michael L. Hitch and Ignatios Souvatzis.
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/cpu.h>
46 #include <machine/bus.h>
47 #include <machine/intr.h>
48 #include <machine/bswap.h>
49
50 #include <amiga/amiga/cia.h>
51 #include <amiga/amiga/custom.h>
52 #include <amiga/amiga/device.h>
53 #include <amiga/dev/zbusvar.h>
54
55 #include <dev/ata/atavar.h>
56 #include <dev/ic/wdcvar.h>
57
58 #define BUDDHA_MAX_CHANNELS 3
59
60 struct wdc_buddha_softc {
61 struct wdc_softc sc_wdcdev;
62 struct ata_channel *wdc_chanarray[BUDDHA_MAX_CHANNELS];
63 struct ata_channel channels[BUDDHA_MAX_CHANNELS];
64 struct bus_space_tag sc_iot;
65 struct isr sc_isr;
66 volatile char *ba;
67 };
68
69 int wdc_buddha_probe(device_t, cfdata_t, void *);
70 void wdc_buddha_attach(device_t, device_t, void *);
71 int wdc_buddha_intr(void *);
72
73 CFATTACH_DECL_NEW(wdc_buddha, sizeof(struct wdc_buddha_softc),
74 wdc_buddha_probe, wdc_buddha_attach, NULL, NULL);
75
76 int
77 wdc_buddha_probe(device_t parent, cfdata_t cfp, void *aux)
78 {
79 struct zbus_args *zap;
80
81 zap = aux;
82
83 if (zap->manid != 4626)
84 return 0;
85
86 if ((zap->prodid != 0) && /* Buddha */
87 (zap->prodid != 42)) /* Catweasel */
88 return 0;
89
90 return 1;
91 }
92
93 void
94 wdc_buddha_attach(device_t parent, device_t self, void *aux)
95 {
96 struct wdc_buddha_softc *sc;
97 struct zbus_args *zap;
98 int nchannels;
99 int ch;
100
101 sc = device_private(self);
102 sc->sc_wdcdev.sc_atac.atac_dev = self;
103 zap = aux;
104
105 sc->ba = zap->va;
106
107 sc->sc_iot.base = (bus_addr_t)sc->ba;
108 sc->sc_iot.absm = &amiga_bus_stride_4swap;
109
110 nchannels = 2;
111 if (zap->prodid == 42) {
112 aprint_normal(": Catweasel Z2\n");
113 nchannels = 3;
114 } else if (zap->serno == 0)
115 aprint_normal(": Buddha\n");
116 else
117 aprint_normal(": Buddha Flash\n");
118
119 /* XXX pio mode setting not implemented yet. */
120 sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
121 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
122 sc->sc_wdcdev.sc_atac.atac_channels = sc->wdc_chanarray;
123 sc->sc_wdcdev.sc_atac.atac_nchannels = nchannels;
124
125 wdc_allocate_regs(&sc->sc_wdcdev);
126
127 for (ch = 0; ch < nchannels; ch++) {
128 struct ata_channel *cp;
129 struct wdc_regs *wdr;
130 int i;
131
132 cp = &sc->channels[ch];
133 sc->wdc_chanarray[ch] = cp;
134
135 cp->ch_channel = ch;
136 cp->ch_atac = &sc->sc_wdcdev.sc_atac;
137 cp->ch_queue =
138 malloc(sizeof(struct ata_queue), M_DEVBUF, M_NOWAIT);
139 if (cp->ch_queue == NULL) {
140 aprint_error_dev(self,
141 "can't allocate memory for command queue\n");
142 return;
143 }
144 cp->ch_ndrive = 2;
145
146 /*
147 * XXX According to the Buddha docs, we should use a method
148 * array that adds 0x40 to the address for byte accesses, to
149 * get the slow timing for command accesses, and the 0x00
150 * offset for the word (fast) accesses. This will be
151 * reconsidered when implementing setting the timing.
152 *
153 * XXX We also could consider to abuse the 32bit capability, or
154 * 32bit accesses to the words (which will read in two words)
155 * for better performance.
156 * -is
157 */
158 wdr = CHAN_TO_WDC_REGS(cp);
159
160 wdr->cmd_iot = &sc->sc_iot;
161 if (bus_space_map(wdr->cmd_iot, 0x210+ch*0x80, 8, 0,
162 &wdr->cmd_baseioh)) {
163 aprint_error_dev(self, "couldn't map cmd registers\n");
164 return;
165 }
166
167 wdr->ctl_iot = &sc->sc_iot;
168 if (bus_space_map(wdr->ctl_iot, 0x250+ch*0x80, 2, 0,
169 &wdr->ctl_ioh)) {
170 bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, 8);
171 aprint_error_dev(self, "couldn't map ctl registers\n");
172 return;
173 }
174
175 for (i = 0; i < WDC_NREG; i++) {
176 if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
177 i, i == 0 ? 4 : 1, &wdr->cmd_iohs[i]) != 0) {
178 aprint_error_dev(self,
179 "couldn't subregion cmd regs\n");
180 return;
181 }
182 }
183
184 wdc_init_shadow_regs(cp);
185 wdcattach(cp);
186 }
187
188 sc->sc_isr.isr_intr = wdc_buddha_intr;
189 sc->sc_isr.isr_arg = sc;
190 sc->sc_isr.isr_ipl = 2;
191 add_isr (&sc->sc_isr);
192 sc->ba[0xfc0] = 0; /* enable interrupts */
193 }
194
195 int
196 wdc_buddha_intr(void *arg)
197 {
198 struct wdc_buddha_softc *sc = (struct wdc_buddha_softc *)arg;
199 int nchannels, i, ret;
200 volatile char *p;
201
202 ret = 0;
203 nchannels = sc->sc_wdcdev.sc_atac.atac_nchannels;
204 p = sc->ba;
205
206 for (i=0; i<nchannels; i++) {
207 if (p[0xf00 + 0x40*i] & 0x80) {
208 wdcintr(&sc->channels[i]);
209 ret = 1;
210 }
211 }
212
213 return ret;
214 }
215