wdc_extio.c revision 1.10
1/*	$NetBSD: wdc_extio.c,v 1.10 2017/10/20 07:06:07 jdolecek 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.10 2017/10/20 07:06:07 jdolecek Exp $");
65
66#include <sys/param.h>
67#include <sys/bus.h>
68#include <sys/callout.h>
69#include <sys/cpu.h>
70#include <sys/device.h>
71#include <sys/intr.h>
72#include <sys/kernel.h>
73#include <sys/malloc.h>
74#include <sys/systm.h>
75
76#include <mips/adm5120/include/adm5120_extiovar.h>
77
78#include <dev/ata/atareg.h>
79#include <dev/ata/atavar.h>
80#include <dev/ic/wdcvar.h>
81
82#ifdef WDC_EXTIO_DEBUG
83int wdc_extio_debug = 1;
84#define	WDC_EXTIO_DPRINTF(__fmt, ...)		\
85do {						\
86	if (wdc_extio_debug)			\
87		printf((__fmt), __VA_ARGS__);	\
88} while (/*CONSTCOND*/0)
89#else /* !WDC_EXTIO_DEBUG */
90#define	WDC_EXTIO_DPRINTF(__fmt, ...)	do { } while (/*CONSTCOND*/0)
91#endif /* WDC_EXTIO_DEBUG */
92
93#define	WDC_OBIO_CF_WINSIZE	0x1000
94#define	WDC_OBIO_REG_OFFSET	0x0800
95#define	WDC_OBIO_DATA_OFFSET	0x0808
96#define	WDC_OBIO_AUXREG_OFFSET	0x080e
97
98struct wdc_extio_softc {
99	struct wdc_softc	sc_wdcdev;
100	struct ata_channel	*sc_chanlist[1];
101	struct ata_channel	sc_channel;
102	struct wdc_regs		sc_wdc_regs;
103	void			*sc_gpio;
104	int			sc_map;
105	struct gpio_pinmap	sc_pinmap;
106};
107
108int	wdc_extio_match(device_t, cfdata_t, void *);
109void	wdc_extio_attach(device_t, device_t, void *);
110
111CFATTACH_DECL_NEW(wdc_extio, sizeof(struct wdc_extio_softc),
112    wdc_extio_match, wdc_extio_attach, NULL, NULL);
113
114#if 0
115void	wdc_extio_reset(struct ata_channel *, int);
116void
117wdc_extio_reset(struct ata_channel *chp, int poll)
118{
119	struct wdc_softc *wdc = CHAN_TO_WDC(chp);
120	struct wdc_regs *wdr = &wdc->regs[chp->ch_channel];
121	uint8_t st0;
122	int i, s = 0;
123
124	if (poll)
125		s = splbio();
126	if (wdc->select)
127		wdc->select(chp,0);
128	/* assert SRST, wait for reset to complete */
129	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, WDCTL_RST);
130	delay(2000);
131	(void) bus_space_read_1(wdr->cmd_iot, wdr->cmd_iohs[wd_error], 0);
132	bus_space_write_1(wdr->ctl_iot, wdr->ctl_ioh, wd_aux_ctlr, 0);
133	delay(10);	/* 400ns delay */
134	for (i = 3100000; --i > 0; ) {
135		st0 = bus_space_read_1(wdr->cmd_iot,
136		    wdr->cmd_iohs[wd_status], 0);
137		if ((st0 & WDCS_BSY) == 0)
138			break;
139		delay(10);
140	}
141	if (i == 0)
142		printf("%s: reset failed\n", __func__);
143	else
144		WDC_EXTIO_DPRINTF("%s: reset in %d.%02dms\n", __func__,
145		    (3100000 - i) / 100,
146		    (3100000 - i) % 100);
147
148	if (poll) {
149		/* ACK interrupt in case there is one pending left */
150		if (wdc->irqack)
151			wdc->irqack(chp);
152		splx(s);
153	}
154}
155#endif
156
157static int
158wdc_extio_map(struct extio_attach_args *ea, struct wdc_regs *wdr,
159    struct ata_channel *chp, void **gpio, struct gpio_pinmap *pinmap)
160{
161	int i;
162
163	*gpio = ea->ea_gpio;
164	wdr->cmd_iot = wdr->ctl_iot = ea->ea_st;
165
166#if 0
167	if (gpio_pin_map(ea->ea_gpio, 0, ea->ea_gpio_mask, pinmap) != 0) {
168		printf("%s: couldn't map GPIO\n", __func__);
169		return -1;
170	}
171#endif
172
173	if (bus_space_map(wdr->cmd_iot, ea->ea_addr, WDC_OBIO_CF_WINSIZE, 0,
174	                  &wdr->cmd_baseioh)) {
175		aprint_error("%s: couldn't map registers\n", __func__);
176		goto post_gpio_err;
177	}
178
179	for (i = 1; i < WDC_NREG; i++) {
180		if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
181		                        WDC_OBIO_REG_OFFSET + i, 1,
182					&wdr->cmd_iohs[i]) != 0) {
183			aprint_error(
184			    "%s: unable to subregion control register\n",
185			    __func__);
186			goto post_bus_err;
187		}
188	}
189	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
190				WDC_OBIO_DATA_OFFSET, 2,
191				&wdr->cmd_iohs[wd_data]) != 0) {
192		aprint_error("%s: unable to subregion data register\n",
193		    __func__);
194		goto post_bus_err;
195	}
196	if (bus_space_subregion(wdr->cmd_iot, wdr->cmd_baseioh,
197				WDC_OBIO_AUXREG_OFFSET, 1, &wdr->ctl_ioh)) {
198		aprint_error("%s: unable to subregion aux register\n",
199		    __func__);
200		goto post_bus_err;
201	}
202
203	wdc_init_shadow_regs(chp);
204
205	return 0;
206post_bus_err:
207	bus_space_unmap(wdr->cmd_iot, wdr->cmd_baseioh, WDC_OBIO_CF_WINSIZE);
208post_gpio_err:
209#if 0
210	gpio_pin_unmap(ea->ea_gpio, pinmap);
211#endif
212	return -1;
213}
214
215static void
216wdc_extio_dataout(struct ata_channel *chp, int flags, void *bf, size_t len)
217{
218	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
219
220	bus_space_write_multi_1(
221	    wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
222}
223
224static void
225wdc_extio_datain(struct ata_channel *chp, int flags, void *bf, size_t len)
226{
227	struct wdc_regs *wdr = CHAN_TO_WDC_REGS(chp);
228
229	bus_space_read_multi_1(
230	    wdr->cmd_iot, wdr->cmd_iohs[wd_data], 0, bf, len);
231}
232
233int
234wdc_extio_match(device_t parent, cfdata_t cf, void *aux)
235{
236	struct extio_attach_args *ea = (struct extio_attach_args *)aux;
237	struct ata_channel ch;
238	struct wdc_softc wdc;
239	struct wdc_regs wdr;
240	int result = 0;
241	void *gpio;
242	int map;
243	struct gpio_pinmap pm = {.pm_map = &map};
244
245	if (strcmp(ea->ea_name, cf->cf_name) != 0)
246		return 0;
247
248	WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
249
250	memset(&wdc, 0, sizeof(wdc));
251	memset(&ch, 0, sizeof(ch));
252	ch.ch_atac = &wdc.sc_atac;
253	wdc.regs = &wdr;
254	wdc.datain_pio = wdc_extio_datain;
255	wdc.dataout_pio = wdc_extio_dataout;
256	if (cf->cf_flags != -1) {
257		wdc.sc_atac.atac_cap |= cf->cf_flags &
258		    (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
259		wdc.cap |= cf->cf_flags &
260		    (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
261	}
262
263	if (wdc_extio_map(ea, &wdr, &ch, &gpio, &pm) == -1)
264		return 0;
265
266	result = wdcprobe(&ch);
267
268	bus_space_unmap(wdr.cmd_iot, wdr.cmd_baseioh, WDC_OBIO_CF_WINSIZE);
269#if 0
270	gpio_pin_unmap(ea->ea_gpio, &pm);
271#endif
272
273	return result;
274}
275
276void
277wdc_extio_attach(device_t parent, device_t self, void *aux)
278{
279	struct wdc_extio_softc *sc = device_private(self);
280	struct wdc_regs *wdr;
281	struct extio_attach_args *ea = aux;
282	struct ata_channel *chp = &sc->sc_channel;
283	struct cfdata *cf;
284
285	WDC_EXTIO_DPRINTF("%s: enter\n", __func__);
286
287	sc->sc_wdcdev.sc_atac.atac_dev = self;
288	sc->sc_pinmap.pm_map = &sc->sc_map;
289	sc->sc_wdcdev.regs = wdr = &sc->sc_wdc_regs;
290	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
291
292	if (wdc_extio_map(ea, wdr, chp, &sc->sc_gpio, &sc->sc_pinmap) == -1)
293		return;
294
295	cf = device_cfdata(sc->sc_wdcdev.sc_atac.atac_dev);
296	if (cf->cf_flags != -1) {
297		aprint_normal(" flags %04x", cf->cf_flags);
298		sc->sc_wdcdev.sc_atac.atac_cap |= cf->cf_flags &
299		    (ATAC_CAP_NOIRQ|ATAC_CAP_DATA32|ATAC_CAP_DATA16);
300		sc->sc_wdcdev.cap |= cf->cf_flags &
301		    (WDC_CAPABILITY_PREATA|WDC_CAPABILITY_NO_EXTRA_RESETS);
302	}
303	sc->sc_wdcdev.datain_pio = wdc_extio_datain;
304	sc->sc_wdcdev.dataout_pio = wdc_extio_dataout;
305	sc->sc_wdcdev.sc_atac.atac_pio_cap = 0;
306	sc->sc_chanlist[0] = chp;
307	sc->sc_wdcdev.sc_atac.atac_channels = sc->sc_chanlist;
308	sc->sc_wdcdev.sc_atac.atac_nchannels = 1;
309	sc->sc_wdcdev.wdc_maxdrives = 2;
310
311	chp->ch_channel = 0;
312	chp->ch_atac = &sc->sc_wdcdev.sc_atac;
313
314	aprint_normal("\n");
315
316	wdcattach(chp);
317}
318