1 /* $FreeBSD: src/sys/dev/ct/ct_isa.c,v 1.3.2.3 2003/06/01 04:26:44 nyan Exp $ */
2 /* $NecBSD: ct_isa.c,v 1.6 1999/07/26 06:32:01 honda Exp $ */
6 * [NetBSD for NEC PC-98 series]
7 * Copyright (c) 1995, 1996, 1997, 1998
8 * NetBSD/pc98 porting staff. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #define SCSIBUS_RESCAN
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/disklabel.h>
40 #if defined(__FreeBSD__) && __FreeBSD_version > 500001
42 #endif /* __ FreeBSD__ */
44 #include <sys/queue.h>
45 #include <sys/malloc.h>
46 #include <sys/device_port.h>
47 #include <sys/errno.h>
52 #include <machine/bus.h>
53 #include <machine/intr.h>
55 #include <dev/scsipi/scsi_all.h>
56 #include <dev/scsipi/scsipi_all.h>
57 #include <dev/scsipi/scsiconf.h>
58 #include <dev/scsipi/scsi_disk.h>
60 #include <dev/isa/isareg.h>
61 #include <dev/isa/isavar.h>
62 #include <dev/isa/isadmavar.h>
64 #include <machine/dvcfg.h>
65 #include <machine/physio_proc.h>
66 #include <machine/syspmgr.h>
68 #include <i386/Cbus/dev/scsi_low.h>
70 #include <dev/ic/wd33c93reg.h>
71 #include <i386/Cbus/dev/ct/ctvar.h>
72 #include <i386/Cbus/dev/ct/bshwvar.h>
73 #endif /* __NetBSD__ */
76 #include <machine/bus.h>
77 #include <machine/resource.h>
80 #include <machine/md_var.h>
82 #include <pc98/pc98/pc98.h>
83 #include <isa/isavar.h>
85 #include <machine/dvcfg.h>
86 #include <machine/physio_proc.h>
88 #include <cam/scsi/scsi_low.h>
90 #include <dev/ic/wd33c93reg.h>
91 #include <dev/ct/ctvar.h>
92 #include <dev/ct/bshwvar.h>
93 #endif /* __FreeBSD__ */
95 #define BSHW_IOSZ 0x08
96 #define BSHW_IOBASE 0xcc0
97 #define BSHW_MEMSZ (PAGE_SIZE * 2)
99 static int ct_isa_match(device_t);
100 static int ct_isa_attach(device_t);
101 static int ct_space_map(device_t, struct bshw *,
102 struct resource **, struct resource **);
103 static void ct_space_unmap(device_t, struct ct_softc *);
104 static struct bshw *ct_find_hw(device_t);
105 static void ct_dmamap(void *, bus_dma_segment_t *, int, int);
106 static void ct_isa_bus_access_weight __P((struct ct_bus_access_handle *));
107 static void ct_isa_dmasync_before __P((struct ct_softc *));
108 static void ct_isa_dmasync_after __P((struct ct_softc *));
110 struct ct_isa_softc {
111 struct ct_softc sc_ct;
112 struct bshw_softc sc_bshw;
115 static struct isa_pnp_id ct_pnp_ids[] = {
116 { 0x0100e7b1, "Logitec LHA-301" },
117 { 0x110154dc, "I-O DATA SC-98III" },
118 { 0x4120acb4, "MELCO IFC-NN" },
122 static device_method_t ct_isa_methods[] = {
123 /* Device interface */
124 DEVMETHOD(device_probe, ct_isa_match),
125 DEVMETHOD(device_attach, ct_isa_attach),
129 static driver_t ct_isa_driver = {
130 "ct", ct_isa_methods, sizeof(struct ct_isa_softc),
133 static devclass_t ct_devclass;
135 DRIVER_MODULE(ct, isa, ct_isa_driver, ct_devclass, 0, 0);
138 ct_isa_match(device_t dev)
141 struct resource *port_res, *mem_res;
142 struct ct_bus_access_handle ch;
145 if (ISA_PNP_PROBE(device_get_parent(dev), dev, ct_pnp_ids) == ENXIO)
148 switch (isa_get_logicalid(dev)) {
149 case 0x0100e7b1: /* LHA-301 */
150 case 0x110154dc: /* SC-98III */
151 case 0x4120acb4: /* IFC-NN */
152 /* XXX - force to SMIT mode */
153 device_set_flags(dev, device_get_flags(dev) | 0x40000);
157 if (isa_get_port(dev) == -1)
158 bus_set_resource(dev, SYS_RES_IOPORT, 0,
159 BSHW_IOBASE, BSHW_IOSZ);
161 if ((hw = ct_find_hw(dev)) == NULL)
163 if (ct_space_map(dev, hw, &port_res, &mem_res) != 0)
166 bzero(&ch, sizeof(ch));
167 ch.ch_iot = rman_get_bustag(port_res);
168 ch.ch_ioh = rman_get_bushandle(port_res),
169 ch.ch_bus_weight = ct_isa_bus_access_weight;
171 rv = ctprobesubr(&ch, 0, BSHW_DEFAULT_HOSTID,
172 BSHW_DEFAULT_CHIPCLK, NULL);
175 struct bshw_softc bshw_tab;
176 struct bshw_softc *bs = &bshw_tab;
178 memset(bs, 0, sizeof(*bs));
179 bshw_read_settings(&ch, bs);
180 bus_set_resource(dev, SYS_RES_IRQ, 0, bs->sc_irq, 1);
181 bus_set_resource(dev, SYS_RES_DRQ, 0, bs->sc_drq, 1);
184 bus_release_resource(dev, SYS_RES_IOPORT, 0, port_res);
186 bus_release_resource(dev, SYS_RES_MEMORY, 0, mem_res);
194 ct_isa_attach(device_t dev)
196 struct ct_isa_softc *pct = device_get_softc(dev);
197 struct ct_softc *ct = &pct->sc_ct;
198 struct ct_bus_access_handle *chp = &ct->sc_ch;
199 struct scsi_low_softc *slp = &ct->sc_sclow;
200 struct bshw_softc *bs = &pct->sc_bshw;
202 int irq_rid, drq_rid, chiprev;
207 hw = ct_find_hw(dev);
208 if (ct_space_map(dev, hw, &ct->port_res, &ct->mem_res) != 0) {
209 device_printf(dev, "bus io mem map failed\n");
213 bzero(chp, sizeof(*chp));
214 chp->ch_iot = rman_get_bustag(ct->port_res);
215 chp->ch_ioh = rman_get_bushandle(ct->port_res);
217 chp->ch_memt = rman_get_bustag(ct->mem_res);
218 chp->ch_memh = rman_get_bushandle(ct->mem_res);
220 chp->ch_bus_weight = ct_isa_bus_access_weight;
223 ct->irq_res = bus_alloc_resource(dev, SYS_RES_IRQ, &irq_rid, 0, ~0,
226 ct->drq_res = bus_alloc_resource(dev, SYS_RES_DRQ, &drq_rid, 0, ~0,
228 if (ct->irq_res == NULL || ct->drq_res == NULL) {
229 ct_space_unmap(dev, ct);
233 if (ctprobesubr(chp, 0, BSHW_DEFAULT_HOSTID,
234 BSHW_DEFAULT_CHIPCLK, &chiprev) == 0)
236 device_printf(dev, "hardware missing\n");
237 ct_space_unmap(dev, ct);
242 if (bus_dma_tag_create(NULL, 1, 0,
243 BUS_SPACE_MAXADDR_24BIT, BUS_SPACE_MAXADDR,
244 NULL, NULL, MAXBSIZE, 1,
245 BUS_SPACE_MAXSIZE_32BIT,
246 BUS_DMA_ALLOCNOW, &ct->sc_dmat) != 0) {
247 device_printf(dev, "can't set up ISA DMA map\n");
248 ct_space_unmap(dev, ct);
252 if (bus_dmamem_alloc(ct->sc_dmat, (void **)&vaddr, BUS_DMA_NOWAIT,
253 &ct->sc_dmamapt) != 0) {
254 device_printf(dev, "can't set up ISA DMA map\n");
255 ct_space_unmap(dev, ct);
259 bus_dmamap_load(ct->sc_dmat, ct->sc_dmamapt, vaddr, MAXBSIZE,
260 ct_dmamap, &addr, 0);
262 /* setup machdep softc */
264 bs->sc_io_control = 0;
265 bs->sc_bounce_phys = (u_int8_t *)addr;
266 bs->sc_bounce_addr = vaddr;
267 bs->sc_bounce_size = MAXBSIZE;
268 bs->sc_minphys = (1 << 24);
269 bs->sc_dmasync_before = ct_isa_dmasync_before;
270 bs->sc_dmasync_after = ct_isa_dmasync_after;
271 bshw_read_settings(chp, bs);
273 /* setup ct driver softc */
275 ct->ct_dma_xfer_start = bshw_dma_xfer_start;
276 ct->ct_pio_xfer_start = bshw_smit_xfer_start;
277 ct->ct_dma_xfer_stop = bshw_dma_xfer_stop;
278 ct->ct_pio_xfer_stop = bshw_smit_xfer_stop;
279 ct->ct_bus_reset = bshw_bus_reset;
280 ct->ct_synch_setup = bshw_synch_setup;
282 ct->sc_xmode = CT_XMODE_DMA;
283 if (chp->ch_memh != NULL)
284 ct->sc_xmode |= CT_XMODE_PIO;
286 ct->sc_chiprev = chiprev;
294 if (DVCFG_MAJOR(device_get_flags(dev)) > 0)
296 /* s = "AM33C93_A"; */
298 ct->sc_chiprev = CT_AM33C93_A;
302 /* s = "WD33C93_A"; */
308 /* s = "AM33C93_A"; */
314 /* s = "WD33C93_B"; */
319 printf("%s: chiprev %s chipclk %d Mhz\n",
320 slp->sl_dev.dv_xname, s, ct->sc_chipclk);
324 slp->sl_hostid = bs->sc_hostid;
325 slp->sl_irq = isa_get_irq(dev);
326 slp->sl_cfgflags = device_get_flags(dev);
332 if (bus_setup_intr(dev, ct->irq_res, INTR_TYPE_CAM,
333 (driver_intr_t *)ctintr, ct, &ct->sc_ih)) {
334 ct_space_unmap(dev, ct);
342 ct_find_hw(device_t dev)
344 return DVCFG_HW(&bshw_hwsel, DVCFG_MAJOR(device_get_flags(dev)));
348 ct_space_map(device_t dev, struct bshw *hw,
349 struct resource **iohp, struct resource **memhp)
351 int port_rid, mem_rid;
356 *iohp = bus_alloc_resource(dev, SYS_RES_IOPORT, &port_rid, 0, ~0,
357 BSHW_IOSZ, RF_ACTIVE);
361 if ((hw->hw_flags & BSHW_SMFIFO) == 0 || isa_get_maddr(dev) == -1)
365 *memhp = bus_alloc_resource(dev, SYS_RES_MEMORY, &mem_rid, 0, ~0,
366 BSHW_MEMSZ, RF_ACTIVE);
367 if (*memhp == NULL) {
368 bus_release_resource(dev, SYS_RES_IOPORT, port_rid, *iohp);
376 ct_space_unmap(device_t dev, struct ct_softc *ct)
378 if (ct->port_res != NULL)
379 bus_release_resource(dev, SYS_RES_IOPORT, 0, ct->port_res);
380 if (ct->mem_res != NULL)
381 bus_release_resource(dev, SYS_RES_MEMORY, 0, ct->mem_res);
382 if (ct->irq_res != NULL)
383 bus_release_resource(dev, SYS_RES_IRQ, 0, ct->irq_res);
384 if (ct->drq_res != NULL)
385 bus_release_resource(dev, SYS_RES_DRQ, 0, ct->drq_res);
389 ct_dmamap(void *arg, bus_dma_segment_t *seg, int nseg, int error)
391 bus_addr_t *addr = (bus_addr_t *)arg;
393 *addr = seg->ds_addr;
397 ct_isa_bus_access_weight(chp)
398 struct ct_bus_access_handle *chp;
405 ct_isa_dmasync_before(ct)
409 if (need_pre_dma_flush)
414 ct_isa_dmasync_after(ct)
418 if (need_post_dma_flush)