summaryrefslogtreecommitdiffstats
path: root/sys/dev/ct/ct_isa.c
blob: 959a2a85e476c20c84100bfbe2b1fe4c0916ea24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* $FreeBSD$ */
/*	$NecBSD: ct_isa.c,v 1.6 1999/07/26 06:32:01 honda Exp $	*/
/*	$NetBSD$	*/

/*
 * [NetBSD for NEC PC-98 series]
 *  Copyright (c) 1995, 1996, 1997, 1998
 *	NetBSD/pc98 porting staff. All rights reserved.
 * 
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#define	SCSIBUS_RESCAN

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/disklabel.h>
#include <sys/bio.h>
#include <sys/buf.h>
#include <sys/queue.h>
#include <sys/malloc.h>
#include <sys/device_port.h>
#include <sys/errno.h>

#include <vm/vm.h>

#ifdef __NetBSD__
#include <machine/bus.h>
#include <machine/intr.h>

#include <dev/scsipi/scsi_all.h>
#include <dev/scsipi/scsipi_all.h>
#include <dev/scsipi/scsiconf.h>
#include <dev/scsipi/scsi_disk.h>

#include <dev/isa/isareg.h>
#include <dev/isa/isavar.h>
#include <dev/isa/isadmavar.h>

#include <machine/dvcfg.h>
#include <machine/physio_proc.h>
#include <machine/syspmgr.h>

#include <i386/Cbus/dev/scsi_low.h>

#include <dev/ic/wd33c93reg.h>
#include <i386/Cbus/dev/ct/ctvar.h>
#include <i386/Cbus/dev/ct/bshwvar.h>
#endif /* __NetBSD__ */

#ifdef __FreeBSD__
#include <machine/bus.h>
#include <machine/resource.h>
#include <sys/bus.h>
#include <sys/rman.h>

#include <pc98/pc98/pc98.h>
#include <isa/isavar.h>

#include <machine/dvcfg.h>
#include <machine/physio_proc.h>

#include <cam/scsi/scsi_low.h>

#include <i386/isa/ic/wd33c93.h>
#include <dev/ct/ctvar.h>
#include <dev/ct/bshwvar.h>
#endif /* __FreeBSD__ */

#define	BSHW_IOSZ	0x08
#define	BSHW_IOBASE 	0xcc0
#define	BSHW_MEMSZ	(PAGE_SIZE * 2)

static int ct_isa_match(device_t);
static int ct_isa_attach(device_t);
static int ct_space_map(device_t, struct bshw *,
			struct resource **, struct resource **);
static void ct_space_unmap(device_t, struct ct_softc *);
static struct bshw *ct_find_hw(device_t);
static void ct_dmamap(void *, bus_dma_segment_t *, int, int);

struct ct_isa_softc {
	struct ct_softc sc_ct;
	struct bshw_softc sc_bshw;
};

static struct isa_pnp_id ct_pnp_ids[] = {
	{ 0x110154dc,	"I-O DATA SC-98III" },
	{ 0x4120acb4,	"MELCO IFC-NN" },
	{ 0,		NULL }
};

static device_method_t ct_isa_methods[] = {
	/* Device interface */
	DEVMETHOD(device_probe,		ct_isa_match),
	DEVMETHOD(device_attach,	ct_isa_attach),
	{ 0, 0 }
};

static driver_t ct_isa_driver = {
	"ct", ct_isa_methods, sizeof(struct ct_isa_softc),
};

static devclass_t ct_devclass;

DRIVER_MODULE(ct, isa, ct_isa_driver, ct_devclass, 0, 0);

static int
ct_isa_match(device_t dev)
{
	struct bshw *hw;
	struct resource *port_res, *mem_res;
	int rv;

	if (ISA_PNP_PROBE(device_get_parent(dev), dev, ct_pnp_ids) == ENXIO)
		return ENXIO;

	if (isa_get_port(dev) == -1)
		bus_set_resource(dev, SYS_RES_IOPORT, 0,
				 BSHW_IOBASE, BSHW_IOSZ);

	if ((hw = ct_find_hw(dev)) == NULL)
		return ENXIO;
	if (ct_space_map(dev, hw, &port_res, &mem_res) != 0)
		return ENXIO;

	rv = ctprobesubr(rman_get_bustag(port_res),
			 rman_get_bushandle(port_res),
			 0, BSHW_DEFAULT_HOSTID, BSHW_DEFAULT_CHIPCLK);
	if (rv != 0)
	{
		struct bshw_softc bshw_tab;
		struct bshw_softc *bs = &bshw_tab;

		memset(bs, 0, sizeof(*bs));
		bshw_read_settings(rman_get_bustag(port_res),
				   rman_get_bushandle(port_res), bs);
		bus_set_resource(dev, SYS_RES_IRQ, 0, bs->sc_irq, 1);
		bus_set_resource(dev, SYS_RES_DRQ, 0, bs->sc_drq, 1);
	}

	bus_release_resource(dev, SYS_RES_IOPORT, 0, port_res);
	if (mem_res != NULL)
		bus_release_resource(dev, SYS_RES_MEMORY, 0, mem_res);

	if (rv != 0)
		return 0;
	return ENXIO;
}

static int
ct_isa_attach(device_t dev)
{
	struct ct_isa_softc *pct = device_get_softc(dev);
	struct ct_softc *ct = &pct->sc_ct;
	struct scsi_low_softc *slp = &ct->sc_sclow;
	struct bshw_softc *bs = &pct->sc_bshw;
	struct bshw *hw;
	int irq_rid, drq_rid;
	u_int8_t *vaddr;
	bus_addr_t addr;

	hw = ct_find_hw(dev);
	if (ct_space_map(dev, hw, &ct->port_res, &ct->mem_res) != 0) {
		device_printf(dev, "bus io mem map failed\n");
		return ENXIO;
	}

	ct->sc_iot = rman_get_bustag(ct->port_res);
	ct->sc_ioh = rman_get_bushandle(ct->port_res);
	if (ct->mem_res) {
		ct->sc_memt = rman_get_bustag(ct->mem_res);
		ct->sc_memh = rman_get_bushandle(ct->mem_res);
	}

	irq_rid = 0;
	ct->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &irq_rid, 0, ~0,
					 1, RF_ACTIVE);
	drq_rid = 0;
	ct->drq_res = bus_alloc_resource(dev, SYS_RES_DRQ, &drq_rid, 0, ~0,
					 1, RF_ACTIVE);
	if (ct->irq_res == NULL || ct->drq_res == NULL) {
		ct_space_unmap(dev, ct);
		return ENXIO;
	}

	/* setup DMA map */
	if (bus_dma_tag_create(NULL, 1, 0,
			       BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR,
			       NULL, NULL, MAXBSIZE, 1,
			       BUS_SPACE_MAXSIZE_32BIT,
			       BUS_DMA_ALLOCNOW, &ct->sc_dmat) != 0) {
		device_printf(dev, "can't set up ISA DMA map\n");
		ct_space_unmap(dev, ct);
		return ENXIO;
	}

	if (bus_dmamem_alloc(ct->sc_dmat, (void **)&vaddr, BUS_DMA_NOWAIT,
			     &ct->sc_dmamapt) != 0) {
		device_printf(dev, "can't set up ISA DMA map\n");
		ct_space_unmap(dev, ct);
		return ENXIO;
	}

	bus_dmamap_load(ct->sc_dmat, ct->sc_dmamapt, vaddr, MAXBSIZE,
			ct_dmamap, &addr, 0);

	/* setup machdep softc */
	bs->sc_hw = hw;
	bs->sc_bounce_phys = (u_int8_t *)addr;
	bs->sc_bounce_addr = vaddr;
	bs->sc_bounce_size = MAXBSIZE;
	bs->sc_minphys = (1 << 24);
	bshw_read_settings(ct->sc_iot, ct->sc_ioh, bs);

	/* setup ct driver softc */
	ct->ct_hw = bs;
	ct->ct_dma_xfer_start = bshw_dma_xfer_start;
	ct->ct_pio_xfer_start = bshw_smit_xfer_start;
	ct->ct_dma_xfer_stop = bshw_dma_xfer_stop;
	ct->ct_pio_xfer_stop = bshw_smit_xfer_stop;
	ct->ct_bus_reset = bshw_bus_reset;
	ct->ct_synch_setup = bshw_synch_setup;

	ct->sc_xmode = CT_XMODE_DMA;
	if (ct->sc_memh != NULL)
		ct->sc_xmode |= CT_XMODE_PIO;
	ct->sc_chiprev = CT_WD33C93_B;
	ct->sc_chipclk = BSHW_DEFAULT_CHIPCLK;

	slp->sl_dev = dev;
	slp->sl_hostid = bs->sc_hostid;
	slp->sl_irq = isa_get_irq(dev);
	slp->sl_cfgflags = device_get_flags(dev);

	ctattachsubr(ct);

	if (bus_setup_intr(dev, ct->irq_res, INTR_TYPE_CAM,
			   (driver_intr_t *)ctintr, ct, &ct->sc_ih)) {
		ct_space_unmap(dev, ct);
		return ENXIO;
	}

	return 0;
}

static struct bshw *
ct_find_hw(device_t dev)
{
	return DVCFG_HW(&bshw_hwsel, DVCFG_MAJOR(device_get_flags(dev)));
}

static int
ct_space_map(device_t dev, struct bshw *hw,
	     struct resource **iohp, struct resource **memhp)
{
	int port_rid, mem_rid;

	*memhp = NULL;

	port_rid = 0;
	*iohp = bus_alloc_resource(dev, SYS_RES_IOPORT, &port_rid, 0, ~0,
				   BSHW_IOSZ, RF_ACTIVE);
	if (*iohp == NULL)
		return ENXIO;

	if ((hw->hw_flags & BSHW_SMFIFO) == 0 || isa_get_maddr(dev) == -1)
		return 0;

	mem_rid = 0;
	*memhp = bus_alloc_resource(dev, SYS_RES_MEMORY, &mem_rid, 0, ~0,
				    BSHW_MEMSZ, RF_ACTIVE);
	if (*memhp == NULL) {
		bus_release_resource(dev, SYS_RES_IOPORT, port_rid, *iohp);
		return ENXIO;
	}

	return 0;
}

static void
ct_space_unmap(device_t dev, struct ct_softc *ct)
{
	if (ct->port_res != NULL)
		bus_release_resource(dev, SYS_RES_IOPORT, 0, ct->port_res);
	if (ct->mem_res != NULL)
		bus_release_resource(dev, SYS_RES_MEMORY, 0, ct->mem_res);
	if (ct->irq_res != NULL)
		bus_release_resource(dev, SYS_RES_IRQ, 0, ct->irq_res);
	if (ct->drq_res != NULL)
		bus_release_resource(dev, SYS_RES_DRQ, 0, ct->drq_res);
}

static void
ct_dmamap(void *arg, bus_dma_segment_t *seg, int nseg, int error)
{
	bus_addr_t *addr = (bus_addr_t *)arg;

	*addr = seg->ds_addr;
}
OpenPOWER on IntegriCloud