1 /* $NecBSD: bshw.c,v 1.1 1997/07/18 09:19:03 kmatsuda Exp $ */
3 /* $DragonFly: src/sys/dev/disk/i386/bs/Attic/bshw.c,v 1.5 2004/02/13 01:04:14 joerg Exp $ */
5 * [NetBSD for NEC PC98 series]
6 * Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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 copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
24 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
29 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
33 * Copyright (c) 1994, 1995, 1996 Naofumi HONDA. All rights reserved.
35 * $FreeBSD: src/sys/i386/isa/bs/bshw.c,v 1.7.2.1 2001/07/26 02:32:18 nyan Exp $
36 * $DragonFly: src/sys/dev/disk/i386/bs/Attic/bshw.c,v 1.5 2004/02/13 01:04:14 joerg Exp $
40 #include <dev/isa/isadmareg.h>
41 #include <i386/Cbus/dev/bs/bsif.h>
42 #include <i386/Cbus/dev/bs/bshw.lst>
44 #if defined(__DragonFly__) || defined(__FreeBSD__)
46 #include <i386/isa/ic/i8237.h>
49 #include <machine/clock.h>
53 static struct bs_softc *gbsc;
55 /**************************************************
57 **************************************************/
58 static void bshw_force_bsmode (struct bs_softc *);
60 /**************************************************
62 **************************************************/
63 static int irq_tbl[] = { 3, 5, 6, 9, 12, 13 };
65 /**************************************************
67 **************************************************/
68 #define RS (BSSAT | BSSMIT | BSLINK | BSREAD)
69 #define WS (BSSAT | BSSMIT | BSLINK)
70 #define EOK (BSERROROK)
72 u_int8_t bshw_cmd[256] = {
73 /* 0 1 2 3 4 5 6 7 8 9 A B C E D F */
74 /*0*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,RS ,0 ,WS ,0 ,0 ,0 ,0 ,0 ,
75 /*1*/0 ,0 ,EOK,0 ,0 ,0 ,0 ,0 ,0 ,0 ,EOK,0 ,0 ,0 ,0 ,0 ,
76 /*2*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,RS ,0 ,WS ,0 ,0 ,0 ,0 ,0 ,
77 /*3*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
78 /*4*/0 ,0 ,EOK,EOK,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
79 /*5*/0 ,0 ,0 ,0 ,EOK,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
80 /*6*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
81 /*7*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
82 /*8*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
83 /*9*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
84 /*A*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
85 /*B*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
86 /*C*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
87 /*D*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
88 /*E*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
89 /*F*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
96 /**********************************************
98 **********************************************/
100 bshw_force_bsmode(bsc)
101 struct bs_softc *bsc;
104 if (bsc->sc_flags & BSBSMODE)
106 bsc->sc_flags |= BSBSMODE;
109 * If you have memory over 16M, some stupid boards always force to
110 * use the io polling mode. Check such a case and change mode into
111 * bus master DMA. However this depends heavily on the board's
115 if (bsc->sc_hw->dma_init && ((*bsc->sc_hw->dma_init)(bsc)))
116 printf("%s change mode using external DMA (%x)\n",
117 bsc->sc_dvname, (u_int)read_wd33c93(bsc, 0x37));
120 #define RESET_DEFAULT 2000
124 struct bs_softc *bsc;
134 bshw_get_auxstat(bsc);
135 bshw_get_busstat(bsc);
137 write_wd33c93(bsc, wd3s_oid, IDR_EHP | bsc->sc_cspeed | bsc->sc_hostid);
138 write_wd33c93(bsc, wd3s_cmd, WD3S_RESET);
140 for (ct = RESET_DEFAULT; ct > 0; ct--)
142 aux = bshw_get_auxstat(bsc);
143 if (aux != 0xff && (aux & STR_INT))
145 if (bshw_get_busstat(bsc) == 0)
148 write_wd33c93(bsc, wd3s_cmd, WD3S_RESET);
159 bshw_force_bsmode(bsc);
161 write_wd33c93(bsc, wd3s_tout, BSHW_SEL_TIMEOUT);
162 write_wd33c93(bsc, wd3s_sid, SIDR_RESEL);
163 bsc->sc_flags |= BSDMATRANSFER;
164 write_wd33c93(bsc, wd3s_ctrl, CR_DEFAULT);
165 write_wd33c93(bsc, wd3s_synch, 0);
167 bshw_get_auxstat(bsc);
168 bsc->sc_busstat = bshw_get_busstat(bsc);
174 /* scsi bus hard reset */
175 #define TWIDDLEWAIT 10000
177 static char tw_chars[] = "|/-\\";
179 /* this is some jokes */
185 cnputc(tw_chars[tw_pos++]);
186 tw_pos %= (sizeof(tw_chars) - 1);
190 static void bshw_set_vsp (struct bs_softc *, u_int, u_int8_t);
193 bshw_set_vsp(bsc, chan, spva)
194 struct bs_softc *bsc;
198 struct bshw *hw = bsc->sc_hw;
200 if (hw->sregaddr == 0)
203 write_wd33c93(bsc, hw->sregaddr + chan, spva);
204 if (hw->hw_flags & BSHW_DOUBLE_DMACHAN)
205 write_wd33c93(bsc, hw->sregaddr + chan + 8, spva);
210 struct bs_softc *bsc;
212 struct targ_info *ti;
215 if (bsc->sc_RSTdelay == 0)
216 bsc->sc_RSTdelay = 6 * 1000 * 1000;
220 * second time reset will be requested by hardware failuer.
222 bsc->sc_RSTdelay = 12 * 1000 * 1000;
226 write_wd33c93(bsc, wd3s_mbank, (bsc->sc_membank | MBR_RST) & ~MBR_IEN);
228 write_wd33c93(bsc, wd3s_mbank, (bsc->sc_membank) & ~MBR_IEN);
231 for (lpc = 0; lpc < 2; lpc ++)
234 for (i = 0; i <= bsc->sc_RSTdelay / TWIDDLEWAIT; i++)
238 (void) read_wd33c93(bsc, wd3s_auxc);
242 if ((read_wd33c93(bsc, wd3s_auxc) & AUXCR_RRST) == 0)
245 printf("\nreset state still continue, wait ...");
248 for (i = 0; i < NTARGETS; i++)
250 if ((ti = bsc->sc_ti[i]) != NULL)
253 bshw_set_vsp(bsc, i, 0);
260 bshw_board_probe(bsc, drq, irq)
261 struct bs_softc *bsc;
268 bshw_print_port(bsc);
269 #endif /* SHOW_PORT */
271 bsc->sc_hostid = (read_wd33c93(bsc, wd3s_auxc) & AUXCR_HIDM);
273 if ((*irq) == IRQUNK)
274 *irq = irq_tbl[(read_wd33c93(bsc, wd3s_auxc) >> 3) & 7];
276 if ((*drq) == DRQUNK)
277 *drq = BUS_IOR(cmd_port) & 3;
279 bsc->sc_dmachan = *drq;
280 bsc->sc_irq = (*irq);
282 bsc->sc_membank = read_wd33c93(bsc, wd3s_mbank);
283 bsc->sc_membank &= ~MBR_RST;
284 bsc->sc_membank |= MBR_IEN;
286 bsc->sc_cspeed = (read_wd33c93(bsc, wd3s_oid) & (~IDR_IDM));
287 switch (BSC_CHIP_CLOCK(bsc->sc_cfgflags))
293 bsc->sc_cspeed &= ~(IDR_FS_12_15 | IDR_FS_16_20);
297 bsc->sc_cspeed &= ~(IDR_FS_12_15 | IDR_FS_16_20);
298 bsc->sc_cspeed |= IDR_FS_12_15;
302 bsc->sc_cspeed &= ~(IDR_FS_12_15 | IDR_FS_16_20);
303 bsc->sc_cspeed |= IDR_FS_16_20;
307 /* XXX: host id fixed(7) */
310 if (bshw_chip_reset(bsc))
318 * Assume the board clock rate must be 20Mhz (always satisfied, maybe)!
319 * Only 10M/s 6.6M/s 5.0M/s 3.3M/s for synchronus transfer speed set.
321 #define ILLEGAL_SYNCH
323 /* A 10 6.6 5.0 4.0 3.3 2.8 2.5 2.0 M/s */
324 /* X 100 150 200 250 300 350 400 500 ns */
325 static u_int bshw_scsi_period[] =
326 {0, 25, 37, 50, 62, 75, 87, 100, 125};
327 static u_int8_t bshw_chip_pval[] =
328 {0, 0xa0, 0xb0, 0x20, 0xd0, 0x30, 0xf0, 0x40, 0x50};
329 #else /* !ILLEGAL_SYNCH */
330 /* A 10 6.6 5.0 3.3 2.5 M/s */
331 /* X 100 150 200 300 400 ns */
332 static u_int bshw_scsi_period[] =
333 {0, 25, 37, 50, 75, 100};
334 static u_int8_t bshw_chip_pval[] =
335 {0, 0xa0, 0xb0, 0x20, 0x30, 0x40};
336 #endif /* !ILLEGAL_SYNCH */
339 bshw_adj_syncdata(sdp)
340 struct syncdata *sdp;
344 if (sdp->offset == 0 || sdp->period < 25 || sdp->period > 100)
345 sdp->offset = sdp->period = 0;
348 for (i = 0; sdp->period > bshw_scsi_period[i] + 2; i ++)
350 sdp->period = bshw_scsi_period[i];
355 bshw_set_synchronous(bsc, ti)
356 struct bs_softc *bsc;
357 struct targ_info *ti;
363 bshw_adj_syncdata(&sd);
364 for (i = 0; sd.period != bshw_scsi_period[i]; i++)
367 ti->ti_sync = ((sd.offset & 0x0f) | bshw_chip_pval[i]);
368 bshw_set_vsp(bsc, ti->ti_id, ti->ti_sync);
370 if (bsc->sc_nexus == ti)
371 bshw_set_sync_reg(bsc, ti->ti_sync);
376 bshw_setup_ctrl_reg(bsc, flags)
377 struct bs_softc *bsc;
382 regval = (flags & BS_SCSI_NOPARITY) ? CR_DEFAULT : CR_DEFAULT_HP;
383 if (bsc->sc_flags & BSDMATRANSFER)
385 write_wd33c93(bsc, wd3s_ctrl, regval);
390 bshw_issue_satcmd(bsc, cb, link)
391 struct bs_softc *bsc;
397 BUS_IOW(addr_port, wd3s_cdb);
398 for (i = 0; i < cb->cmdlen - 1; i++)
399 BUS_IOW(ctrl_port, cb->cmd[i]);
400 BUS_IOW(ctrl_port, cb->cmd[i] | (link ? 1 : 0));
406 struct bs_softc *bsc;
410 write_wd33c93(bsc, wd3s_mbank, bsc->sc_membank & (~MBR_IEN));
415 struct bs_softc *bsc;
418 if ((--bsc->sc_hwlock) <= 0)
419 write_wd33c93(bsc, wd3s_mbank, bsc->sc_membank);
422 /**********************************************
424 **********************************************/
426 #include <i386/Cbus/dev/bs/bshw_dma.c>
427 #include <i386/Cbus/dev/bs/bshw_pdma.c>
429 #if defined(__DragonFly__) || defined(__FreeBSD__)
430 #include "bshw_dma.c"
431 #include "bshw_pdma.c"
434 /**********************************************
436 **********************************************/
440 struct bs_softc * bsc;
449 for (j = 0; j <= 0x70; j += 0x10)
451 printf("port %x: ", port);
452 for (i = 0; i < 0x10; i++)
453 printf("%x ", (u_int) read_wd33c93(bsc, port++));