1 /* $NecBSD: bshw.c,v 1.1 1997/07/18 09:19:03 kmatsuda Exp $ */
4 * [NetBSD for NEC PC98 series]
5 * Copyright (c) 1994, 1995, 1996 NetBSD/pc98 porting staff.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
28 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 * Copyright (c) 1994, 1995, 1996 Naofumi HONDA. All rights reserved.
36 #include <dev/isa/isadmareg.h>
37 #include <i386/Cbus/dev/bs/bsif.h>
38 #include <i386/Cbus/dev/bs/bshw.lst>
42 #include <i386/isa/ic/i8237.h>
43 #include <i386/isa/bs/bsif.h>
44 #include <i386/isa/bs/bshw.lst>
45 #include <machine/clock.h>
49 static struct bs_softc *gbsc;
51 /**************************************************
53 **************************************************/
54 static void bshw_force_bsmode __P((struct bs_softc *));
56 /**************************************************
58 **************************************************/
59 static int irq_tbl[] = { 3, 5, 6, 9, 12, 13 };
61 /**************************************************
63 **************************************************/
64 #define RS (BSSAT | BSSMIT | BSLINK | BSREAD)
65 #define WS (BSSAT | BSSMIT | BSLINK)
66 #define EOK (BSERROROK)
68 u_int8_t bshw_cmd[256] = {
69 /* 0 1 2 3 4 5 6 7 8 9 A B C E D F */
70 /*0*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,RS ,0 ,WS ,0 ,0 ,0 ,0 ,0 ,
71 /*1*/0 ,0 ,EOK,0 ,0 ,0 ,0 ,0 ,0 ,0 ,EOK,0 ,0 ,0 ,0 ,0 ,
72 /*2*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,RS ,0 ,WS ,0 ,0 ,0 ,0 ,0 ,
73 /*3*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
74 /*4*/0 ,0 ,EOK,EOK,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
75 /*5*/0 ,0 ,0 ,0 ,EOK,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
76 /*6*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
77 /*7*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
78 /*8*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
79 /*9*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
80 /*A*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
81 /*B*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
82 /*C*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
83 /*D*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
84 /*E*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
85 /*F*/0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,0 ,
92 /**********************************************
94 **********************************************/
96 bshw_force_bsmode(bsc)
100 if (bsc->sc_flags & BSBSMODE)
102 bsc->sc_flags |= BSBSMODE;
105 * If you have memory over 16M, some stupid boards always force to
106 * use the io polling mode. Check such a case and change mode into
107 * bus master DMA. However this depends heavily on the board's
111 if (bsc->sc_hw->dma_init && ((*bsc->sc_hw->dma_init)(bsc)))
112 printf("%s change mode using external DMA (%x)\n",
113 bsc->sc_dvname, (u_int)read_wd33c93(bsc, 0x37));
116 #define RESET_DEFAULT 2000
120 struct bs_softc *bsc;
130 bshw_get_auxstat(bsc);
131 bshw_get_busstat(bsc);
133 write_wd33c93(bsc, wd3s_oid, IDR_EHP | bsc->sc_cspeed | bsc->sc_hostid);
134 write_wd33c93(bsc, wd3s_cmd, WD3S_RESET);
136 for (ct = RESET_DEFAULT; ct > 0; ct--)
138 aux = bshw_get_auxstat(bsc);
139 if (aux != 0xff && (aux & STR_INT))
141 if (bshw_get_busstat(bsc) == 0)
144 write_wd33c93(bsc, wd3s_cmd, WD3S_RESET);
155 bshw_force_bsmode(bsc);
157 write_wd33c93(bsc, wd3s_tout, BSHW_SEL_TIMEOUT);
158 write_wd33c93(bsc, wd3s_sid, SIDR_RESEL);
159 bsc->sc_flags |= BSDMATRANSFER;
160 write_wd33c93(bsc, wd3s_ctrl, CR_DEFAULT);
161 write_wd33c93(bsc, wd3s_synch, 0);
163 bshw_get_auxstat(bsc);
164 bsc->sc_busstat = bshw_get_busstat(bsc);
170 /* scsi bus hard reset */
171 #define TWIDDLEWAIT 10000
173 static char tw_chars[] = "|/-\\";
175 /* this is some jokes */
181 cnputc(tw_chars[tw_pos++]);
182 tw_pos %= (sizeof(tw_chars) - 1);
186 static void bshw_set_vsp __P((struct bs_softc *, u_int, u_int8_t));
189 bshw_set_vsp(bsc, chan, spva)
190 struct bs_softc *bsc;
194 struct bshw *hw = bsc->sc_hw;
196 if (hw->sregaddr == 0)
199 write_wd33c93(bsc, hw->sregaddr + chan, spva);
200 if (hw->hw_flags & BSHW_DOUBLE_DMACHAN)
201 write_wd33c93(bsc, hw->sregaddr + chan + 8, spva);
206 struct bs_softc *bsc;
208 struct targ_info *ti;
211 if (bsc->sc_RSTdelay == 0)
212 bsc->sc_RSTdelay = 6 * 1000 * 1000;
216 * second time reset will be requested by hardware failuer.
218 bsc->sc_RSTdelay = 12 * 1000 * 1000;
222 write_wd33c93(bsc, wd3s_mbank, (bsc->sc_membank | MBR_RST) & ~MBR_IEN);
224 write_wd33c93(bsc, wd3s_mbank, (bsc->sc_membank) & ~MBR_IEN);
227 for (lpc = 0; lpc < 2; lpc ++)
230 for (i = 0; i <= bsc->sc_RSTdelay / TWIDDLEWAIT; i++)
234 (void) read_wd33c93(bsc, wd3s_auxc);
238 if ((read_wd33c93(bsc, wd3s_auxc) & AUXCR_RRST) == 0)
241 printf("\nreset state still continue, wait ...");
244 for (i = 0; i < NTARGETS; i++)
246 if ((ti = bsc->sc_ti[i]) != NULL)
249 bshw_set_vsp(bsc, i, 0);
256 bshw_board_probe(bsc, drq, irq)
257 struct bs_softc *bsc;
264 bshw_print_port(bsc);
265 #endif /* SHOW_PORT */
267 bsc->sc_hostid = (read_wd33c93(bsc, wd3s_auxc) & AUXCR_HIDM);
269 if ((*irq) == IRQUNK)
270 *irq = irq_tbl[(read_wd33c93(bsc, wd3s_auxc) >> 3) & 7];
272 if ((*drq) == DRQUNK)
273 *drq = BUS_IOR(cmd_port) & 3;
275 bsc->sc_dmachan = *drq;
276 bsc->sc_irq = (*irq);
278 bsc->sc_membank = read_wd33c93(bsc, wd3s_mbank);
279 bsc->sc_membank &= ~MBR_RST;
280 bsc->sc_membank |= MBR_IEN;
282 bsc->sc_cspeed = (read_wd33c93(bsc, wd3s_oid) & (~IDR_IDM));
283 switch (BSC_CHIP_CLOCK(bsc->sc_cfgflags))
289 bsc->sc_cspeed &= ~(IDR_FS_12_15 | IDR_FS_15_20);
293 bsc->sc_cspeed &= ~(IDR_FS_12_15 | IDR_FS_15_20);
294 bsc->sc_cspeed |= IDR_FS_12_15;
298 bsc->sc_cspeed &= ~(IDR_FS_12_15 | IDR_FS_15_20);
299 bsc->sc_cspeed |= IDR_FS_15_20;
303 /* XXX: host id fixed(7) */
306 if (bshw_chip_reset(bsc))
314 * Assume the board clock rate must be 20Mhz (always satisfied, maybe)!
315 * Only 10M/s 6.6M/s 5.0M/s 3.3M/s for synchronus transfer speed set.
317 #define ILLEGAL_SYNCH
319 /* A 10 6.6 5.0 4.0 3.3 2.8 2.5 2.0 M/s */
320 /* X 100 150 200 250 300 350 400 500 ns */
321 static u_int bshw_scsi_period[] =
322 {0, 25, 37, 50, 62, 75, 87, 100, 125};
323 static u_int8_t bshw_chip_pval[] =
324 {0, 0xa0, 0xb0, 0x20, 0xd0, 0x30, 0xf0, 0x40, 0x50};
325 #else /* !ILLEGAL_SYNCH */
326 /* A 10 6.6 5.0 3.3 2.5 M/s */
327 /* X 100 150 200 300 400 ns */
328 static u_int bshw_scsi_period[] =
329 {0, 25, 37, 50, 75, 100};
330 static u_int8_t bshw_chip_pval[] =
331 {0, 0xa0, 0xb0, 0x20, 0x30, 0x40};
332 #endif /* !ILLEGAL_SYNCH */
335 bshw_adj_syncdata(sdp)
336 struct syncdata *sdp;
340 if (sdp->offset == 0 || sdp->period < 25 || sdp->period > 100)
341 sdp->offset = sdp->period = 0;
344 for (i = 0; sdp->period > bshw_scsi_period[i] + 2; i ++)
346 sdp->period = bshw_scsi_period[i];
351 bshw_set_synchronous(bsc, ti)
352 struct bs_softc *bsc;
353 struct targ_info *ti;
359 bshw_adj_syncdata(&sd);
360 for (i = 0; sd.period != bshw_scsi_period[i]; i++)
363 ti->ti_sync = ((sd.offset & 0x0f) | bshw_chip_pval[i]);
364 bshw_set_vsp(bsc, ti->ti_id, ti->ti_sync);
366 if (bsc->sc_nexus == ti)
367 bshw_set_sync_reg(bsc, ti->ti_sync);
372 bshw_setup_ctrl_reg(bsc, flags)
373 struct bs_softc *bsc;
378 regval = (flags & BS_SCSI_NOPARITY) ? CR_DEFAULT : CR_DEFAULT_HP;
379 if (bsc->sc_flags & BSDMATRANSFER)
381 write_wd33c93(bsc, wd3s_ctrl, regval);
386 bshw_issue_satcmd(bsc, cb, link)
387 struct bs_softc *bsc;
393 BUS_IOW(addr_port, wd3s_cdb);
394 for (i = 0; i < cb->cmdlen - 1; i++)
395 BUS_IOW(ctrl_port, cb->cmd[i]);
396 BUS_IOW(ctrl_port, cb->cmd[i] | (link ? 1 : 0));
402 struct bs_softc *bsc;
406 write_wd33c93(bsc, wd3s_mbank, bsc->sc_membank & (~MBR_IEN));
411 struct bs_softc *bsc;
414 if ((--bsc->sc_hwlock) <= 0)
415 write_wd33c93(bsc, wd3s_mbank, bsc->sc_membank);
418 /**********************************************
420 **********************************************/
422 #include <i386/Cbus/dev/bs/bshw_dma.c>
423 #include <i386/Cbus/dev/bs/bshw_pdma.c>
426 #include <i386/isa/bs/bshw_dma.c>
427 #include <i386/isa/bs/bshw_pdma.c>
430 /**********************************************
432 **********************************************/
436 struct bs_softc * bsc;
445 for (j = 0; j <= 0x70; j += 0x10)
447 printf("port %x: ", port);
448 for (i = 0; i < 0x10; i++)
449 printf("%x ", (u_int) read_wd33c93(bsc, port++));