wdc_extio.c revision 1.1 1 1.1 dyoung /* $NetBSD: wdc_extio.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $ */
2 1.1 dyoung
3 1.1 dyoung /*-
4 1.1 dyoung * Copyright (c) 2007 David Young. All rights reserved.
5 1.1 dyoung *
6 1.1 dyoung * CompactFlash attachment for RouterBoard 153. Derived from
7 1.1 dyoung * the NetBSD/mac68k OBIO-IDE attachment.
8 1.1 dyoung *
9 1.1 dyoung * Redistribution and use in source and binary forms, with or
10 1.1 dyoung * without modification, are permitted provided that the following
11 1.1 dyoung * conditions are met:
12 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
13 1.1 dyoung * notice, this list of conditions and the following disclaimer.
14 1.1 dyoung * 2. Redistributions in binary form must reproduce the above
15 1.1 dyoung * copyright notice, this list of conditions and the following
16 1.1 dyoung * disclaimer in the documentation and/or other materials provided
17 1.1 dyoung * with the distribution.
18 1.1 dyoung * 3. The name of the author may not be used to endorse or promote
19 1.1 dyoung * products derived from this software without specific prior
20 1.1 dyoung * written permission.
21 1.1 dyoung *
22 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
23 1.1 dyoung * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 1.1 dyoung * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
25 1.1 dyoung * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR
26 1.1 dyoung * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27 1.1 dyoung * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 1.1 dyoung * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
29 1.1 dyoung * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 1.1 dyoung * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 1.1 dyoung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
33 1.1 dyoung * OF SUCH DAMAGE.
34 1.1 dyoung */
35 1.1 dyoung /*
36 1.1 dyoung * Copyright (c) 2002 Takeshi Shibagaki All rights reserved.
37 1.1 dyoung *
38 1.1 dyoung * mac68k OBIO-IDE attachment created by Takeshi Shibagaki
39 1.1 dyoung *
40 1.1 dyoung * Redistribution and use in source and binary forms, with or without
41 1.1 dyoung * modification, are permitted provided that the following conditions
42 1.1 dyoung * are met:
43 1.1 dyoung * 1. Redistributions of source code must retain the above copyright
44 1.1 dyoung * notice, this list of conditions and the following disclaimer.
45 1.1 dyoung * 2. Redistributions in binary form must reproduce the above copyright
46 1.1 dyoung * notice, this list of conditions and the following disclaimer in the
47 1.1 dyoung * documentation and/or other materials provided with the distribution.
48 1.1 dyoung * 3. All advertising materials mentioning features or use of this software
49 1.1 dyoung * must display the following acknowledgement:
50 1.1 dyoung * This product includes software developed by Charles M. Hannum.
51 1.1 dyoung * 4. The name of the author may not be used to endorse or promote products
52 1.1 dyoung * derived from this software without specific prior written permission.
53 1.1 dyoung *
54 1.1 dyoung * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
55 1.1 dyoung * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
56 1.1 dyoung * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
57 1.1 dyoung * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
58 1.1 dyoung * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
59 1.1 dyoung * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
60 1.1 dyoung * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
61 1.1 dyoung * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
62 1.1 dyoung * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
63 1.1 dyoung * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
64 1.1 dyoung */
65 1.1 dyoung
66 1.1 dyoung #include <sys/cdefs.h>
67 1.1 dyoung __KERNEL_RCSID(0, "$NetBSD: wdc_extio.c,v 1.1 2007/03/20 08:52:02 dyoung Exp $");
68 1.1 dyoung
69 1.1 dyoung #include <sys/types.h>
70 1.1 dyoung #include <sys/param.h>
71 1.1 dyoung #include <sys/systm.h>
72 1.1 dyoung #include <sys/device.h>
73 1.1 dyoung #include <sys/malloc.h>
74 1.1 dyoung #include <sys/kernel.h>
75 1.1 dyoung #include <sys/callout.h>
76 1.1 dyoung
77 1.1 dyoung #include <machine/bus.h>
78 1.1 dyoung #include <machine/intr.h>
79 1.1 dyoung #include <machine/cpu.h>
80 1.1 dyoung
81 1.1 dyoung #include <mips/adm5120/include/adm5120_extiovar.h>
82 1.1 dyoung
83 1.1 dyoung #include <dev/ata/atareg.h>
84 1.1 dyoung #include <dev/ata/atavar.h>
85 1.1 dyoung #include <dev/ic/wdcvar.h>
86 1.1 dyoung
87 1.1 dyoung #ifdef WDC_EXTIO_DEBUG
88 1.1 dyoung int wdc_extio_debug = 1;
89 1.1 dyoung #define WDC_EXTIO_DPRINTF(__fmt, ...) \
90 1.1 dyoung do { \
91 1.1 dyoung if (wdc_extio_debug) \
92 1.1 dyoung printf((__fmt), __VA_ARGS__); \
93 1.1 dyoung } while (/*CONSTCOND*/0)
94 1.1 dyoung #else /* !WDC_EXTIO_DEBUG */
95 1.1 dyoung #define WDC_EXTIO_DPRINTF(__fmt, ...) do { } while (/*CONSTCOND*/0)
96 1.1 dyoung #endif /* WDC_EXTIO_DEBUG */
97 1.1 dyoung
98 1.1 dyoung #define WDC_OBIO_CF_WINSIZE 0x1000
99 1.1 dyoung #define WDC_OBIO_REG_OFFSET 0x0800
100 1.1 dyoung #define WDC_OBIO_DATA_OFFSET 0x0808
101 1.1 dyoung #define WDC_OBIO_AUXREG_OFFSET 0x080e
102 1.1 dyoung
103 1.1 dyoung struct wdc_extio_softc {
104 1.1 dyoung struct wdc_softc sc_wdcdev;
105 1.1 dyoung struct ata_channel *sc_chanlist[1];
106 1.1 dyoung struct ata_channel sc_channel;
107 1.1 dyoung struct ata_queue sc_chqueue;
108 1.1 dyoung struct wdc_regs sc_wdc_regs;
109 1.1 dyoung void *sc_gpio;
110 1.1 dyoung int sc_map;
111 1.1 dyoung struct gpio_pinmap sc_pinmap;
112 1.1 dyoung };
113 1.1 dyoung
114 1.1 dyoung int wdc_extio_match(struct device *, struct cfdata *, void *);
115 1.1 dyoung void wdc_extio_attach(struct device *, struct device *, void *);
116 1.1 dyoung
117 1.1 dyoung CFATTACH_DECL(wdc_extio, sizeof(struct wdc_extio_softc),
118 1.1 dyoung wdc_extio_match, wdc_extio_attach, NULL, NULL);
119 1.1 dyoung
120 1.1 dyoung #if 0
121 1.1 dyoung void wdc_extio_reset(struct ata_channel *, int);
122 1.1 dyoung void
123 1.1 dyoung wdc_extio_reset(struct ata_channel *chp, int poll)
124 1.1 dyoung {
125 1.1 dyoung struct wdc_softc *wdc = CHAN_TO_WDC(chp);
126 1.1 dyoung struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
127 1.1 dyoung uint8_t st0;
128 1.1 dyoung int i, s = 0;
129 1.1 dyoung
130 1.1 dyoung if (poll)
131 1.1 dyoung s = splbio();
132 1.1 dyoung if (wdc->select)
133 1.1 dyoung wdc->select(chp,0);
134 1.1 dyoung /* assert SRST, wait for reset to complete */
135 1.1 dyoung bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_RST);
136 1.1 dyoung delay(2000);
137 1.1 dyoung (void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
138 1.1 dyoung bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 0);
139 1.1 dyoung delay(10); /* 400ns delay */
140 1.1 dyoung for (i = 3100000; --i > 0; ) {
141 1.1 dyoung st0 = bus_space_read_1(wdr->cmd_iot,
142 1.1 dyoung wdr->cmd_iohs[wd_status], 0);
143 1.1 dyoung if ((st0 & WDCS_BSY) == 0)
144 1.1 dyoung break;
145 1.1 dyoung delay(10);
146 1.1 dyoung }
147 1.1 dyoung if (i == 0)
148 1.1 dyoung printf("%s: reset failed\n", __func__);
149 1.1 dyoung else
150 1.1 dyoung WDC_EXTIO_DPRINTF("%s: reset in %d.%02dms\n", __func__,
151 1.1 dyoung (3100000 - i) / 100,
152 1.1 dyoung (3100000 - i) % 100);
153 1.1 dyoung
154 1.1 dyoung if (poll) {
155 1.1 dyoung /* ACK interrupt in case there is one pending left */
156 1.1 dyoung if (wdc->irqack)
157 1.1 dyoung wdc->irqack(chp);
158 1.1 dyoung splx(s);
159 1.1 dyoung }
160 1.1 dyoung }
161 1.1 dyoung #endif
162 1.1 dyoung
163 1.1 dyoung static int
164 1.1 dyoung wdc_extio_map(struct extio_attach_args *ea, struct wdc_regs *wdr,
165 1.1 dyoung struct ata_channel *chp, void **gpio, struct gpio_pinmap *pinmap)
166 1.1 dyoung {
167 1.1 dyoung int i;
168 1.1 dyoung
169 1.1 dyoung *gpio = ea->ea_gpio;
170 1.1 dyoung wdr->cmd_iot = wdr->ctl_iot = ea->ea_st;
171 1.1 dyoung
172 1.1 dyoung #if 0
173 1.1 dyoung if (gpio_pin_map(ea->ea_gpio, 0, ea->ea_gpio_mask, pinmap) != 0) {
174 1.1 dyoung printf("%s: couldn't map GPIO\n", __func__);
175 1.1 dyoung return -1;
176 1.1 dyoung }
177 1.1 dyoung #endif
178 1.1 dyoung
179 1.1 dyoung if (bus_space_map(wdr->cmd_iot, ea->ea_addr, WDC_OBIO_CF_WINSIZE, 0,
180 1.1 dyoung &wdr->cmd_baseioh)) {
181 1.1 dyoung printf("%s: couldn't map registers\n", __func__);
182 1.1 dyoung goto post_gpio_err;
183 1.1 dyoung }
184 1.1 dyoung
185 1.1 dyoung for (i = 1; i < WDC_NREG; i++) {
186 1.1 dyoung if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
187 1.1 dyoung WDC_OBIO_REG_OFFSET + i, 1,
188 1.1 dyoung &wdr->cmd_iohs[i]) != 0) {
189 1.1 dyoung printf("%s: unable to subregion control register\n",
190 1.1 dyoung __func__);
191 1.1 dyoung goto post_bus_err;
192 1.1 dyoung }
193 1.1 dyoung }
194 1.1 dyoung if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
195 1.1 dyoung WDC_OBIO_DATA_OFFSET, 2,
196 1.1 dyoung &wdr->cmd_iohs[wd_data]) != 0) {
197 1.1 dyoung printf("%s: unable to subregion data register\n", __func__);
198 1.1 dyoung goto post_bus_err;
199 1.1 dyoung }
200 1.1 dyoung if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
201 1.1 dyoung WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
202 1.1 dyoung printf("%s: unable to subregion aux register\n", __func__);
203 1.1 dyoung goto post_bus_err;
204 1.1 dyoung }
205 1.1 dyoung
206 1.1 dyoung wdc_init_shadow_regs(chp);
207 1.1 dyoung
208 1.1 dyoung return 0;
209 1.1 dyoung post_bus_err:
210 1.1 dyoung bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_CF_WINSIZE);
211 1.1 dyoung post_gpio_err:
212 1.1 dyoung #if 0
213 1.1 dyoung gpio_pin_unmap(ea->ea_gpio, pinmap);
214 1.1 dyoung #endif
215 1.1 dyoung return -1;
216 1.1 dyoung }
217 1.1 dyoung
218 1.1 dyoung static void
219 1.1 dyoung wdc_extio_dataout(struct ata_channel *chp, int flags, void *bf, size_t len)
220 1.1 dyoung {
221 1.1 dyoung struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
222 1.1 dyoung
223 1.1 dyoung bus_space_write_multi_1(
224 1.1 dyoung wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
225 1.1 dyoung }
226 1.1 dyoung
227 1.1 dyoung static void
228 1.1 dyoung wdc_extio_datain(struct ata_channel *chp, int flags, void *bf, size_t len)
229 1.1 dyoung {
230 1.1 dyoung struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
231 1.1 dyoung
232 1.1 dyoung bus_space_read_multi_1(
233 1.1 dyoung wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
234 1.1 dyoung }
235 1.1 dyoung
236 1.1 dyoung int
237 1.1 dyoung wdc_extio_match(struct device *parent, struct cfdata *cf, void *aux)
238 1.1 dyoung {
239 1.1 dyoung struct extio_attach_args *ea = (struct extio_attach_args *)aux;
240 1.1 dyoung struct ata_channel ch;
241 1.1 dyoung struct wdc_softc wdc;
242 1.1 dyoung struct wdc_regs wdr;
243 1.1 dyoung int result = 0;
244 1.1 dyoung void *gpio;
245 1.1 dyoung int map;
246 1.1 dyoung struct gpio_pinmap pm = {.pm_map = &map};
247 1.1 dyoung
248 1.1 dyoung if (strcmp(ea->ea_name, cf->cf_name) != 0)
249 1.1 dyoung return 0;
250 1.1 dyoung
251 1.1 dyoung WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
252 1.1 dyoung
253 1.1 dyoung memset(&wdc, 0, sizeof(wdc));
254 1.1 dyoung memset(&ch, 0, sizeof(ch));
255 1.1 dyoung ch.ch_atac = &wdc.sc_atac;
256 1.1 dyoung wdc.regs = &wdr;
257 1.1 dyoung wdc.datain_pio = wdc_extio_datain;
258 1.1 dyoung wdc.dataout_pio = wdc_extio_dataout;
259 1.1 dyoung if (cf->cf_flags != -1) {
260 1.1 dyoung wdc.sc_atac.atac_cap |= cf->cf_flags &
261 1.1 dyoung (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
262 1.1 dyoung wdc.cap |= cf->cf_flags &
263 1.1 dyoung (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
264 1.1 dyoung }
265 1.1 dyoung
266 1.1 dyoung if (wdc_extio_map(ea, &wdr, &ch, &gpio, &pm) == -1)
267 1.1 dyoung return 0;
268 1.1 dyoung
269 1.1 dyoung result = wdcprobe(&ch);
270 1.1 dyoung
271 1.1 dyoung bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_CF_WINSIZE);
272 1.1 dyoung #if 0
273 1.1 dyoung gpio_pin_unmap(ea->ea_gpio, &pm);
274 1.1 dyoung #endif
275 1.1 dyoung
276 1.1 dyoung return result;
277 1.1 dyoung }
278 1.1 dyoung
279 1.1 dyoung void
280 1.1 dyoung wdc_extio_attach(struct device *parent, struct device *self, void *aux)
281 1.1 dyoung {
282 1.1 dyoung struct wdc_extio_softc *sc = (void *)self;
283 1.1 dyoung struct wdc_regs *wdr;
284 1.1 dyoung struct extio_attach_args *ea = aux;
285 1.1 dyoung struct ata_channel *chp = &sc->sc_channel;
286 1.1 dyoung struct cfdata *cf;
287 1.1 dyoung
288 1.1 dyoung WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
289 1.1 dyoung
290 1.1 dyoung sc->sc_pinmap.pm_map = &sc->sc_map;
291 1.1 dyoung sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
292 1.1 dyoung chp->ch_atac = &sc->sc_wdcdev.sc_atac;
293 1.1 dyoung
294 1.1 dyoung if (wdc_extio_map(ea, wdr, chp, &sc->sc_gpio, &sc->sc_pinmap) == -1)
295 1.1 dyoung return;
296 1.1 dyoung
297 1.1 dyoung cf = device_cfdata(&sc->sc_wdcdev.sc_atac.atac_dev);
298 1.1 dyoung if (cf->cf_flags != -1) {
299 1.1 dyoung printf(" flags %04x", cf->cf_flags);
300 1.1 dyoung sc->sc_wdcdev.sc_atac.atac_cap |= cf->cf_flags &
301 1.1 dyoung (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
302 1.1 dyoung sc->sc_wdcdev.cap |= cf->cf_flags &
303 1.1 dyoung (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
304 1.1 dyoung }
305 1.1 dyoung sc->sc_wdcdev.datain_pio = wdc_extio_datain;
306 1.1 dyoung sc->sc_wdcdev.dataout_pio = wdc_extio_dataout;
307 1.1 dyoung sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
308 1.1 dyoung sc->sc_chanlist[0] = chp;
309 1.1 dyoung sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
310 1.1 dyoung sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
311 1.1 dyoung chp->ch_channel = 0;
312 1.1 dyoung chp->ch_atac = &sc->sc_wdcdev.sc_atac;
313 1.1 dyoung chp->ch_queue = &sc->sc_chqueue;
314 1.1 dyoung chp->ch_ndrive = 2;
315 1.1 dyoung
316 1.1 dyoung printf("\n");
317 1.1 dyoung
318 1.1 dyoung wdcattach(chp);
319 1.1 dyoung }
320