wdc_xsurf.c revision 1.2.30.1 1 /* $NetBSD: wdc_xsurf.c,v 1.2.30.1 2017/04/24 08:48:45 jdolecek Exp $ */
2
3 /*-
4 * Copyright (c) 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Radoslaw Kujawa.
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 /* Driver for IDE controller present on X-Surf. */
33
34 #include <sys/cdefs.h>
35
36 #include <sys/systm.h>
37 #include <sys/types.h>
38 #include <sys/device.h>
39 #include <sys/bus.h>
40 #include <sys/conf.h>
41 #include <sys/kmem.h>
42
43 #include <machine/cpu.h>
44
45 #include <amiga/amiga/device.h>
46
47 #include <dev/ata/atavar.h>
48 #include <dev/ic/wdcvar.h>
49
50 #include <amiga/dev/zbusvar.h>
51 #include <amiga/dev/xsurfvar.h>
52
53 #define WDC_XSURF_CHANNELS 2
54 #define WDC_XSURF_CHANSIZE 0x30
55 #define WDC_XSURF_CHANOFF 0x2000
56
57 #define WDC_XSURF_ISR_OFF 0x7E
58 #define WDC_XSURF_ISR_HIGH 0x80
59
60 #define WDC_XSURF_OFF_DATA 0x0
61 #define WDC_XSURF_OFF_ERROR 0x6
62 #define WDC_XSURF_OFF_SECCNT 0xA
63 #define WDC_XSURF_OFF_SECTOR 0xE
64 #define WDC_XSURF_OFF_CYL_LO 0x12
65 #define WDC_XSURF_OFF_CYL_HI 0x16
66 #define WDC_XSURF_OFF_SDH 0x1A
67 #define WDC_XSURF_OFF_COMMAND 0x1E
68
69 struct wdc_xsurf_port {
70 struct ata_channel channel;
71 struct wdc_regs wdr;
72 };
73
74 struct wdc_xsurf_softc {
75 device_t sc_dev;
76
77 struct wdc_softc sc_wdcdev;
78 struct ata_channel *sc_chanarray[WDC_XSURF_CHANNELS];
79 struct wdc_xsurf_port sc_ports[WDC_XSURF_CHANNELS];
80
81 struct bus_space_tag sc_bst;
82 bus_space_tag_t sc_cmdt;
83 bus_space_handle_t sc_isrh;
84
85 struct isr sc_isr;
86 };
87
88 static int wdc_xsurf_match(device_t, cfdata_t, void *);
89 static void wdc_xsurf_attach(device_t, device_t, void *);
90 void wdc_xsurf_attach_channel(struct wdc_xsurf_softc *, int);
91 void wdc_xsurf_map_channel(struct wdc_xsurf_softc *, int);
92 int wdc_xsurf_intr(void *arg);
93
94 CFATTACH_DECL_NEW(wdc_xsurf, sizeof(struct wdc_softc),
95 wdc_xsurf_match, wdc_xsurf_attach, NULL, NULL);
96
97 static const unsigned int wdc_xsurf_wdr_offsets[] = {
98 WDC_XSURF_OFF_DATA, WDC_XSURF_OFF_ERROR, WDC_XSURF_OFF_SECCNT,
99 WDC_XSURF_OFF_SECTOR, WDC_XSURF_OFF_CYL_LO, WDC_XSURF_OFF_CYL_HI,
100 WDC_XSURF_OFF_SDH, WDC_XSURF_OFF_COMMAND
101 };
102
103 static int
104 wdc_xsurf_match(device_t parent, cfdata_t cf, void *aux)
105 {
106 struct xsurfbus_attach_args *xsb_aa = aux;
107
108 if (strcmp(xsb_aa->xaa_name, "wdc_xsurf") != 0)
109 return 0;
110
111 return 1;
112 }
113
114 static void
115 wdc_xsurf_attach(device_t parent, device_t self, void *aux)
116 {
117 struct wdc_xsurf_softc *sc;
118 struct xsurfbus_attach_args *xsb_aa;
119 int i;
120
121 aprint_normal("\n");
122 aprint_naive("\n");
123 xsb_aa = aux;
124 sc = device_private(self);
125 sc->sc_dev = self;
126
127 sc->sc_bst.base = xsb_aa->xaa_base;
128 sc->sc_bst.absm = &amiga_bus_stride_1swap;
129 sc->sc_cmdt = &sc->sc_bst;
130
131 sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
132 sc->sc_wdcdev.sc_atac.atac_nchannels = WDC_XSURF_CHANNELS;
133 sc->sc_wdcdev.sc_atac.atac_dev = self;
134 sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanarray;
135 sc->sc_wdcdev.sc_atac.atac_cap = ATAC_CAP_DATA16;
136 sc->sc_wdcdev.wdc_maxdrives = 2;
137 /* this controller has no aux control registers */
138 sc->sc_wdcdev.cap = WDC_CAPABILITY_NO_AUXCTL;
139
140 /* attach the channels */
141 wdc_allocate_regs(&sc->sc_wdcdev);
142 for (i = 0; i < WDC_XSURF_CHANNELS; i++) {
143 wdc_xsurf_attach_channel(sc, i);
144 }
145
146 /* map interrupt status register */
147 bus_space_map(sc->sc_cmdt, WDC_XSURF_ISR_OFF,
148 1, 0, &sc->sc_isrh);
149
150 /* attach interrupt */
151 sc->sc_isr.isr_intr = wdc_xsurf_intr;
152 sc->sc_isr.isr_arg = sc;
153 sc->sc_isr.isr_ipl = 2;
154 add_isr(&sc->sc_isr);
155 }
156
157 void
158 wdc_xsurf_attach_channel(struct wdc_xsurf_softc *sc, int chnum)
159 {
160 sc->sc_chanarray[chnum] = &sc->sc_ports[chnum].channel;
161 memset(&sc->sc_ports[chnum],0,sizeof(struct wdc_xsurf_port));
162 sc->sc_ports[chnum].channel.ch_channel = chnum;
163 sc->sc_ports[chnum].channel.ch_atac = &sc->sc_wdcdev.sc_atac;
164 sc->sc_ports[chnum].channel.ch_queue = ata_queue_alloc(1);
165
166 wdc_xsurf_map_channel(sc, chnum);
167
168 wdc_init_shadow_regs(&sc->sc_ports[chnum].channel);
169 wdcattach(&sc->sc_ports[chnum].channel);
170
171 #ifdef WDC_XSURF_DEBUG
172 aprint_normal_dev(sc->sc_dev, "done init for channel %d\n", chnum);
173 #endif /* WDC_XSURF_DEBUG */
174 }
175
176 void
177 wdc_xsurf_map_channel(struct wdc_xsurf_softc *sc, int chnum)
178 {
179 struct wdc_regs *wdr;
180 int i;
181
182 wdr = CHAN_TO_WDC_REGS(&sc->sc_ports[chnum].channel);
183
184 /* map the registers */
185 wdr->cmd_iot = sc->sc_cmdt;
186 bus_space_map(sc->sc_cmdt, chnum * WDC_XSURF_CHANOFF,
187 WDC_XSURF_CHANSIZE, 0, &wdr->cmd_baseioh);
188
189 for (i = 0; i < WDC_NREG; i++)
190 bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
191 wdc_xsurf_wdr_offsets[i], i == 0 ? 2 : 1,
192 &wdr->cmd_iohs[i]);
193 }
194
195 int
196 wdc_xsurf_intr(void *arg)
197 {
198 struct wdc_xsurf_softc *sc;
199 uint8_t intreq;
200 int r1, r2;
201
202 sc = (struct wdc_xsurf_softc *)arg;
203 r1 = r2 = 0;
204
205 intreq = bus_space_read_1(sc->sc_cmdt, sc->sc_isrh, 0);
206 bus_space_write_1(sc->sc_cmdt, sc->sc_isrh, 0, 0); /* pull A11 down */
207
208 /* only one register for both channels... :/ */
209 if (intreq & WDC_XSURF_ISR_HIGH) {
210 r1 = wdcintr(&sc->sc_ports[0].channel);
211 r2 = wdcintr(&sc->sc_ports[1].channel);
212 }
213
214 return r1 | r2;
215 }
216