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