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