Home | History | Annotate | Line # | Download | only in podulebus
sec.c revision 1.16.14.1
      1  1.16.14.1    bouyer /* $NetBSD: sec.c,v 1.16.14.1 2017/04/21 16:53:52 bouyer Exp $ */
      2        1.1     bjh21 
      3        1.1     bjh21 /*-
      4        1.1     bjh21  * Copyright (c) 2000, 2001, 2006 Ben Harris
      5        1.1     bjh21  * All rights reserved.
      6        1.1     bjh21  *
      7        1.1     bjh21  * Redistribution and use in source and binary forms, with or without
      8        1.1     bjh21  * modification, are permitted provided that the following conditions
      9        1.1     bjh21  * are met:
     10        1.1     bjh21  * 1. Redistributions of source code must retain the above copyright
     11        1.1     bjh21  *    notice, this list of conditions and the following disclaimer.
     12        1.1     bjh21  * 2. Redistributions in binary form must reproduce the above copyright
     13        1.1     bjh21  *    notice, this list of conditions and the following disclaimer in the
     14        1.1     bjh21  *    documentation and/or other materials provided with the distribution.
     15        1.1     bjh21  * 3. The name of the author may not be used to endorse or promote products
     16        1.1     bjh21  *    derived from this software without specific prior written permission.
     17        1.1     bjh21  *
     18        1.1     bjh21  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19        1.1     bjh21  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20        1.1     bjh21  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21        1.1     bjh21  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22        1.1     bjh21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23        1.1     bjh21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24        1.1     bjh21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25        1.1     bjh21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26        1.1     bjh21  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27        1.1     bjh21  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28        1.1     bjh21  */
     29        1.1     bjh21 /*
     30        1.1     bjh21  * sec.c -- driver for Acorn SCSI expansion cards (AKA30, AKA31, AKA32)
     31        1.1     bjh21  *
     32        1.1     bjh21  * These cards are documented in:
     33        1.1     bjh21  * Acorn Archimedes 500 series / Acorn R200 series Technical Reference Manual
     34        1.1     bjh21  * Published by Acorn Computers Limited
     35        1.1     bjh21  * ISBN 1 85250 086 7
     36        1.1     bjh21  * Part number 0486,052
     37        1.1     bjh21  * Issue 1, November 1990
     38        1.1     bjh21  */
     39        1.1     bjh21 
     40        1.1     bjh21 #include <sys/cdefs.h>
     41  1.16.14.1    bouyer __KERNEL_RCSID(0, "$NetBSD: sec.c,v 1.16.14.1 2017/04/21 16:53:52 bouyer Exp $");
     42        1.1     bjh21 
     43        1.1     bjh21 #include <sys/param.h>
     44        1.1     bjh21 
     45        1.1     bjh21 #include <sys/buf.h>
     46        1.1     bjh21 #include <sys/device.h>
     47        1.1     bjh21 #include <sys/malloc.h>
     48        1.1     bjh21 #include <sys/reboot.h>	/* For bootverbose */
     49        1.1     bjh21 #include <sys/syslog.h>
     50        1.1     bjh21 #include <sys/systm.h>
     51        1.1     bjh21 
     52        1.1     bjh21 #include <dev/scsipi/scsi_all.h>
     53        1.1     bjh21 #include <dev/scsipi/scsipi_all.h>
     54        1.1     bjh21 #include <dev/scsipi/scsiconf.h>
     55        1.1     bjh21 
     56        1.9        ad #include <sys/bus.h>
     57        1.1     bjh21 
     58        1.1     bjh21 #include <dev/ic/wd33c93reg.h>
     59        1.1     bjh21 #include <dev/ic/wd33c93var.h>
     60        1.1     bjh21 #include <dev/ic/nec71071reg.h>
     61        1.1     bjh21 
     62        1.1     bjh21 #include <dev/podulebus/podulebus.h>
     63        1.1     bjh21 #include <dev/podulebus/podules.h>
     64        1.1     bjh21 #include <dev/podulebus/powerromreg.h>
     65        1.1     bjh21 #include <dev/podulebus/secreg.h>
     66        1.1     bjh21 
     67        1.1     bjh21 #include "opt_ddb.h"
     68        1.1     bjh21 
     69        1.1     bjh21 struct sec_softc {
     70        1.1     bjh21 	struct	wd33c93_softc sc_sbic;
     71        1.1     bjh21 	bus_space_tag_t		sc_pod_t;
     72        1.1     bjh21 	bus_space_handle_t	sc_pod_h;
     73        1.1     bjh21 	bus_space_tag_t		sc_mod_t;
     74        1.1     bjh21 	bus_space_handle_t	sc_mod_h;
     75        1.1     bjh21 	void			*sc_ih;
     76        1.1     bjh21 	struct		evcnt	sc_intrcnt;
     77        1.1     bjh21 	uint8_t			sc_mpr;
     78        1.1     bjh21 
     79        1.1     bjh21 	/* Details of the current DMA transfer */
     80        1.5   thorpej 	bool			sc_dmaactive;
     81        1.6  christos 	void *			sc_dmaaddr;
     82        1.1     bjh21 	int			sc_dmaoff;
     83        1.1     bjh21 	size_t			sc_dmalen;
     84        1.5   thorpej 	bool			sc_dmain;
     85        1.1     bjh21 	/* Details of the current block within the above transfer */
     86        1.1     bjh21 	size_t			sc_dmablk;
     87        1.1     bjh21 };
     88        1.1     bjh21 
     89        1.1     bjh21 #define SEC_DMABLK	16384
     90        1.1     bjh21 #define SEC_NBLKS	3
     91        1.2     bjh21 #define SEC_DMAMODE	MODE_TMODE_DMD
     92        1.1     bjh21 
     93        1.1     bjh21 /* autoconfiguration glue */
     94       1.12     bjh21 static int sec_match(device_t, cfdata_t, void *);
     95       1.12     bjh21 static void sec_attach(device_t, device_t, void *);
     96        1.1     bjh21 
     97        1.4     bjh21 /* shutdown hook */
     98       1.15     bjh21 static bool sec_shutdown(device_t, int);
     99        1.4     bjh21 
    100        1.1     bjh21 /* callbacks from MI WD33C93 driver */
    101        1.6  christos static int sec_dmasetup(struct wd33c93_softc *, void **, size_t *, int,
    102        1.1     bjh21     size_t *);
    103        1.1     bjh21 static int sec_dmago(struct wd33c93_softc *);
    104        1.1     bjh21 static void sec_dmastop(struct wd33c93_softc *);
    105        1.1     bjh21 static void sec_reset(struct wd33c93_softc *);
    106        1.1     bjh21 
    107        1.1     bjh21 static int sec_intr(void *);
    108        1.1     bjh21 static int sec_dmatc(struct sec_softc *sc);
    109        1.1     bjh21 
    110        1.1     bjh21 void sec_dumpdma(void *arg);
    111        1.1     bjh21 
    112       1.13     bjh21 CFATTACH_DECL_NEW(sec, sizeof(struct sec_softc),
    113        1.1     bjh21     sec_match, sec_attach, NULL, NULL);
    114        1.1     bjh21 
    115        1.1     bjh21 static inline void
    116        1.1     bjh21 sec_setpage(struct sec_softc *sc, int page)
    117        1.1     bjh21 {
    118        1.1     bjh21 
    119        1.1     bjh21 	sc->sc_mpr = (sc->sc_mpr & ~SEC_MPR_PAGE) | page;
    120        1.1     bjh21 	bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
    121        1.1     bjh21 }
    122        1.1     bjh21 
    123        1.1     bjh21 static inline void
    124        1.1     bjh21 sec_cli(struct sec_softc *sc)
    125        1.1     bjh21 {
    126        1.1     bjh21 
    127        1.1     bjh21 	bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_CLRINT, 0);
    128        1.1     bjh21 }
    129        1.1     bjh21 
    130        1.1     bjh21 static inline void
    131        1.1     bjh21 dmac_write(struct sec_softc *sc, int reg, uint8_t val)
    132        1.1     bjh21 {
    133        1.1     bjh21 
    134        1.1     bjh21 	bus_space_write_1(sc->sc_mod_t, sc->sc_mod_h,
    135        1.1     bjh21 	    SEC_DMAC + DMAC(reg), val);
    136        1.1     bjh21 }
    137        1.1     bjh21 
    138        1.1     bjh21 static inline uint8_t
    139        1.1     bjh21 dmac_read(struct sec_softc *sc, int reg)
    140        1.1     bjh21 {
    141        1.1     bjh21 
    142        1.1     bjh21 	return bus_space_read_1(sc->sc_mod_t, sc->sc_mod_h,
    143        1.1     bjh21 	    SEC_DMAC + DMAC(reg));
    144        1.1     bjh21 }
    145        1.1     bjh21 
    146        1.1     bjh21 static int
    147       1.12     bjh21 sec_match(device_t parent, cfdata_t cf, void *aux)
    148        1.1     bjh21 {
    149        1.1     bjh21 	struct podulebus_attach_args *pa = aux;
    150        1.1     bjh21 
    151        1.1     bjh21 	/* Standard ROM, skipping the MCS card that used the same ID. */
    152        1.1     bjh21 	if (pa->pa_product == PODULE_ACORN_SCSI &&
    153        1.1     bjh21 	    strncmp(pa->pa_descr, "MCS", 3) != 0)
    154        1.1     bjh21 		return 1;
    155        1.1     bjh21 
    156        1.1     bjh21 	/* PowerROM */
    157        1.1     bjh21         if (pa->pa_product == PODULE_ALSYSTEMS_SCSI &&
    158        1.1     bjh21             podulebus_initloader(pa) == 0 &&
    159        1.1     bjh21             podloader_callloader(pa, 0, 0) == PRID_ACORN_SCSI1)
    160        1.1     bjh21                 return 1;
    161        1.1     bjh21 
    162        1.1     bjh21 	return 0;
    163        1.1     bjh21 }
    164        1.1     bjh21 
    165        1.1     bjh21 static void
    166       1.12     bjh21 sec_attach(device_t parent, device_t self, void *aux)
    167        1.1     bjh21 {
    168        1.1     bjh21 	struct podulebus_attach_args *pa = aux;
    169        1.1     bjh21 	struct sec_softc *sc = device_private(self);
    170        1.1     bjh21 	int i;
    171        1.1     bjh21 
    172       1.13     bjh21 	sc->sc_sbic.sc_dev = self;
    173        1.1     bjh21 	/* Set up bus spaces */
    174        1.1     bjh21 	sc->sc_pod_t = pa->pa_fast_t;
    175        1.1     bjh21 	bus_space_map(pa->pa_fast_t, pa->pa_fast_base, 0x1000, 0,
    176        1.1     bjh21 	    &sc->sc_pod_h);
    177        1.1     bjh21 	sc->sc_mod_t = pa->pa_mod_t;
    178        1.1     bjh21 	bus_space_map(pa->pa_mod_t, pa->pa_mod_base, 0x1000, 0,
    179        1.1     bjh21 	    &sc->sc_mod_h);
    180        1.1     bjh21 
    181        1.1     bjh21 	sc->sc_sbic.sc_regt = sc->sc_mod_t;
    182       1.14    rumble 	bus_space_subregion(sc->sc_mod_t, sc->sc_mod_h, SEC_SBIC + 0, 1,
    183       1.14    rumble 	    &sc->sc_sbic.sc_asr_regh);
    184       1.14    rumble 	bus_space_subregion(sc->sc_mod_t, sc->sc_mod_h, SEC_SBIC + 1, 1,
    185       1.14    rumble 	    &sc->sc_sbic.sc_data_regh);
    186        1.1     bjh21 
    187        1.1     bjh21 	sc->sc_sbic.sc_id = 7;
    188        1.1     bjh21 	sc->sc_sbic.sc_clkfreq = SEC_CLKFREQ;
    189        1.2     bjh21 	sc->sc_sbic.sc_dmamode = SBIC_CTL_BURST_DMA;
    190        1.1     bjh21 
    191        1.1     bjh21 	sc->sc_sbic.sc_adapter.adapt_request = wd33c93_scsi_request;
    192        1.1     bjh21 	sc->sc_sbic.sc_adapter.adapt_minphys = minphys;
    193        1.1     bjh21 
    194        1.1     bjh21 	sc->sc_sbic.sc_dmasetup = sec_dmasetup;
    195        1.1     bjh21 	sc->sc_sbic.sc_dmago = sec_dmago;
    196        1.1     bjh21 	sc->sc_sbic.sc_dmastop = sec_dmastop;
    197        1.1     bjh21 	sc->sc_sbic.sc_reset = sec_reset;
    198        1.1     bjh21 
    199        1.1     bjh21 	sc->sc_mpr = 0;
    200        1.1     bjh21 	bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
    201        1.1     bjh21 
    202        1.1     bjh21 	for (i = 0; i < SEC_NPAGES; i++) {
    203        1.1     bjh21 		sec_setpage(sc, i);
    204        1.1     bjh21 		bus_space_set_region_2(sc->sc_mod_t, sc->sc_mod_h,
    205        1.1     bjh21 				       SEC_SRAM, 0, SEC_PAGESIZE / 2);
    206        1.1     bjh21 	}
    207        1.1     bjh21 
    208        1.1     bjh21 	wd33c93_attach(&sc->sc_sbic);
    209        1.1     bjh21 
    210        1.1     bjh21 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    211       1.10    cegger 	    device_xname(self), "intr");
    212        1.1     bjh21 	sc->sc_ih = podulebus_irq_establish(pa->pa_ih, IPL_BIO, sec_intr,
    213        1.1     bjh21 	    sc, &sc->sc_intrcnt);
    214        1.1     bjh21 	sec_cli(sc);
    215        1.1     bjh21 	sc->sc_mpr |= SEC_MPR_IE;
    216        1.1     bjh21 	bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
    217  1.16.14.1    bouyer 	if (!pmf_device_register1(sc->sc_sbic.sc_dev, NULL, NULL, sec_shutdown))
    218  1.16.14.1    bouyer 		aprint_error_dev(sc->sc_sbic.sc_dev,
    219  1.16.14.1    bouyer 		    "couldn't establish power handler\n");
    220        1.4     bjh21 }
    221        1.4     bjh21 
    222        1.4     bjh21 /*
    223        1.4     bjh21  * Before reboot, reset the page register to 0 so that RISC OS can see
    224        1.4     bjh21  * the podule ROM.
    225        1.4     bjh21  */
    226       1.15     bjh21 static bool
    227       1.15     bjh21 sec_shutdown(device_t dev, int howto)
    228        1.4     bjh21 {
    229       1.15     bjh21 	struct sec_softc *sc = device_private(dev);
    230        1.4     bjh21 
    231        1.4     bjh21 	sec_setpage(sc, 0);
    232       1.15     bjh21 	return true;
    233        1.1     bjh21 }
    234        1.1     bjh21 
    235        1.1     bjh21 static void
    236        1.1     bjh21 sec_copyin(struct sec_softc *sc, void *dest, int src, size_t size)
    237        1.1     bjh21 {
    238        1.1     bjh21 	uint16_t tmp, *wptr;
    239        1.1     bjh21 	int cnt, extra_byte;
    240        1.1     bjh21 
    241        1.1     bjh21 	KASSERT(src >= 0);
    242        1.1     bjh21 	KASSERT(src + size <= SEC_MEMSIZE);
    243        1.1     bjh21 	if (src % 2 != 0) {
    244        1.1     bjh21 		/*
    245        1.1     bjh21 		 * There's a stray byte at the start.  Read the word
    246        1.1     bjh21 		 * containing it.
    247        1.1     bjh21 		 */
    248        1.1     bjh21 		sec_setpage(sc, src / SEC_PAGESIZE);
    249        1.1     bjh21 		tmp = bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
    250        1.1     bjh21 		    SEC_SRAM + (src % SEC_PAGESIZE / 2));
    251        1.1     bjh21 		*(uint8_t *)dest = tmp >> 8;
    252        1.1     bjh21 		dest = ((uint8_t *)dest) + 1;
    253        1.1     bjh21 		src++; size--;
    254        1.1     bjh21 	}
    255        1.1     bjh21 	KASSERT(src % 2 == 0);
    256        1.1     bjh21 	KASSERT(ALIGNED_POINTER(dest, uint16_t));
    257        1.1     bjh21 	wptr = dest;
    258        1.1     bjh21 	extra_byte = size % 2;
    259        1.1     bjh21 	size -= extra_byte;
    260        1.1     bjh21 	while (size > 0) {
    261        1.1     bjh21 		cnt = SEC_PAGESIZE - src % SEC_PAGESIZE;
    262        1.1     bjh21 		if (cnt > size)
    263        1.1     bjh21 			cnt = size;
    264        1.1     bjh21 		sec_setpage(sc, src / SEC_PAGESIZE);
    265        1.1     bjh21 		/* bus ops are in words */
    266        1.1     bjh21 		bus_space_read_region_2(sc->sc_mod_t, sc->sc_mod_h,
    267        1.1     bjh21 		    SEC_SRAM + src % SEC_PAGESIZE / 2, wptr, cnt / 2);
    268        1.1     bjh21 		src += cnt;
    269        1.1     bjh21 		wptr += cnt / 2;
    270        1.1     bjh21 		size -= cnt;
    271        1.1     bjh21 	}
    272        1.1     bjh21 	if (extra_byte) {
    273        1.1     bjh21 		sec_setpage(sc, src / SEC_PAGESIZE);
    274        1.1     bjh21 		*(u_int8_t *)wptr =
    275        1.1     bjh21 		    bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
    276        1.1     bjh21 		    SEC_SRAM + src % SEC_PAGESIZE / 2) & 0xff;
    277        1.1     bjh21 	}
    278        1.1     bjh21 }
    279        1.1     bjh21 
    280        1.1     bjh21 static void
    281        1.1     bjh21 sec_copyout(struct sec_softc *sc, const void *src, int dest, size_t size)
    282        1.1     bjh21 {
    283        1.1     bjh21 	int cnt, extra_byte;
    284        1.1     bjh21 	const uint16_t *wptr;
    285        1.1     bjh21 	uint16_t tmp;
    286        1.1     bjh21 
    287        1.1     bjh21 	KASSERT(dest >= 0);
    288        1.1     bjh21 	KASSERT(dest + size <= SEC_MEMSIZE);
    289        1.1     bjh21 	if (dest % 2 != 0) {
    290        1.1     bjh21 		/*
    291        1.1     bjh21 		 * There's a stray byte at the start.  Read the word
    292        1.1     bjh21 		 * containing it.
    293        1.1     bjh21 		 */
    294        1.1     bjh21 		sec_setpage(sc, dest / SEC_PAGESIZE);
    295        1.1     bjh21 		tmp = bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
    296        1.1     bjh21 		    SEC_SRAM + (dest % SEC_PAGESIZE / 2));
    297        1.1     bjh21 		tmp &= 0xff;
    298        1.1     bjh21 		tmp |= *(uint8_t const *)src << 8;
    299        1.1     bjh21 		bus_space_write_2(sc->sc_mod_t, sc->sc_mod_h,
    300        1.1     bjh21 		    SEC_SRAM + (dest % SEC_PAGESIZE / 2), tmp);
    301        1.1     bjh21 		src = ((uint8_t const *)src) + 1;
    302        1.1     bjh21 		dest++; size--;
    303        1.1     bjh21 	}
    304        1.1     bjh21 	KASSERT(dest % 2 == 0);
    305        1.1     bjh21 	KASSERT(ALIGNED_POINTER(src, uint16_t));
    306        1.1     bjh21 	wptr = src;
    307        1.1     bjh21 	extra_byte = size % 2;
    308        1.1     bjh21 	size -= extra_byte;
    309        1.1     bjh21 	while (size > 0) {
    310        1.1     bjh21 		cnt = SEC_PAGESIZE - dest % SEC_PAGESIZE;
    311        1.1     bjh21 		if (cnt > size)
    312        1.1     bjh21 			cnt = size;
    313        1.1     bjh21 		sec_setpage(sc, dest / SEC_PAGESIZE);
    314        1.1     bjh21 		/* bus ops are in words */
    315        1.1     bjh21 		bus_space_write_region_2(sc->sc_mod_t, sc->sc_mod_h,
    316        1.1     bjh21 		    dest % SEC_PAGESIZE / 2, wptr, cnt / 2);
    317        1.1     bjh21 		wptr += cnt / 2;
    318        1.1     bjh21 		dest += cnt;
    319        1.1     bjh21 		size -= cnt;
    320        1.1     bjh21 	}
    321        1.1     bjh21 	if (extra_byte) {
    322        1.1     bjh21 		/*
    323        1.1     bjh21 		 * There's a stray byte at the end.  Read the word
    324        1.1     bjh21 		 * containing it.
    325        1.1     bjh21 		 */
    326        1.1     bjh21 		sec_setpage(sc, dest / SEC_PAGESIZE);
    327        1.1     bjh21 		tmp = bus_space_read_2(sc->sc_mod_t, sc->sc_mod_h,
    328        1.1     bjh21 		    SEC_SRAM + (dest % SEC_PAGESIZE / 2));
    329        1.1     bjh21 		tmp &= 0xff00;
    330        1.1     bjh21 		tmp |= *(uint8_t const *)wptr;
    331        1.1     bjh21 		bus_space_write_2(sc->sc_mod_t, sc->sc_mod_h,
    332        1.1     bjh21 		    SEC_SRAM + (dest % SEC_PAGESIZE / 2), tmp);
    333        1.1     bjh21 	}
    334        1.1     bjh21 }
    335        1.1     bjh21 
    336        1.1     bjh21 static void
    337        1.1     bjh21 sec_dmablk(struct sec_softc *sc, int blk)
    338        1.1     bjh21 {
    339        1.1     bjh21 	int off;
    340        1.1     bjh21 	size_t len;
    341        1.1     bjh21 
    342        1.1     bjh21 	KASSERT(blk >= 0);
    343        1.1     bjh21 	KASSERT(blk * SEC_DMABLK < sc->sc_dmalen);
    344        1.1     bjh21 	off = (blk % SEC_NBLKS) * SEC_DMABLK + sc->sc_dmaoff;
    345        1.1     bjh21 	len = MIN(SEC_DMABLK, sc->sc_dmalen - (blk * SEC_DMABLK));
    346        1.1     bjh21 	dmac_write(sc, NEC71071_ADDRLO, off & 0xff);
    347        1.1     bjh21 	dmac_write(sc, NEC71071_ADDRMID, off >> 8);
    348        1.1     bjh21 	dmac_write(sc, NEC71071_ADDRHI, 0);
    349        1.1     bjh21 	/*
    350        1.1     bjh21 	 * "Note: The number of DMA transfer cycles is actually the
    351        1.1     bjh21 	 * value of the current count register + 1.  Therefore, when
    352        1.1     bjh21 	 * programming the count register, specify the number of DMA
    353        1.1     bjh21 	 * transfers minus one." -- uPD71071 datasheet
    354        1.1     bjh21 	 */
    355        1.1     bjh21 	dmac_write(sc, NEC71071_COUNTLO, (len - 1) & 0xff);
    356        1.1     bjh21 	dmac_write(sc, NEC71071_COUNTHI, (len - 1) >> 8);
    357        1.1     bjh21 }
    358        1.1     bjh21 
    359        1.1     bjh21 static void
    360        1.3     bjh21 sec_copyoutblk(struct sec_softc *sc, int blk)
    361        1.3     bjh21 {
    362        1.3     bjh21 	int off;
    363        1.3     bjh21 	size_t len;
    364        1.3     bjh21 
    365        1.3     bjh21 	KASSERT(blk >= 0);
    366        1.3     bjh21 	KASSERT(blk * SEC_DMABLK < sc->sc_dmalen);
    367        1.3     bjh21 	KASSERT(!sc->sc_dmain);
    368        1.3     bjh21 	off = (blk % SEC_NBLKS) * SEC_DMABLK + sc->sc_dmaoff;
    369        1.3     bjh21 	len = MIN(SEC_DMABLK, sc->sc_dmalen - (blk * SEC_DMABLK));
    370        1.7        he 	sec_copyout(sc, (char*)sc->sc_dmaaddr + (blk * SEC_DMABLK), off, len);
    371        1.3     bjh21 }
    372        1.3     bjh21 
    373        1.3     bjh21 static void
    374        1.3     bjh21 sec_copyinblk(struct sec_softc *sc, int blk)
    375        1.1     bjh21 {
    376        1.1     bjh21 	int off;
    377        1.1     bjh21 	size_t len;
    378        1.1     bjh21 
    379        1.1     bjh21 	KASSERT(blk >= 0);
    380        1.1     bjh21 	KASSERT(blk * SEC_DMABLK < sc->sc_dmalen);
    381        1.3     bjh21 	KASSERT(sc->sc_dmain);
    382        1.1     bjh21 	off = (blk % SEC_NBLKS) * SEC_DMABLK + sc->sc_dmaoff;
    383        1.1     bjh21 	len = MIN(SEC_DMABLK, sc->sc_dmalen - (blk * SEC_DMABLK));
    384        1.7        he 	sec_copyin(sc, (char*)sc->sc_dmaaddr + (blk * SEC_DMABLK), off, len);
    385        1.1     bjh21 }
    386        1.1     bjh21 
    387        1.1     bjh21 static int
    388        1.6  christos sec_dmasetup(struct wd33c93_softc *sc_sbic, void **addr, size_t *len,
    389        1.1     bjh21     int datain, size_t *dmasize)
    390        1.1     bjh21 {
    391        1.1     bjh21 	struct sec_softc *sc = (struct sec_softc *)sc_sbic;
    392        1.1     bjh21 	uint8_t mode;
    393        1.1     bjh21 
    394        1.1     bjh21 	sc->sc_dmaaddr = *addr;
    395        1.1     bjh21 	sc->sc_dmaoff = ALIGNED_POINTER(*addr, uint16_t) ? 0 : 1;
    396        1.1     bjh21 	sc->sc_dmalen = *len;
    397        1.1     bjh21 	sc->sc_dmain = datain;
    398        1.1     bjh21 	sc->sc_dmablk = 0;
    399        1.1     bjh21 	mode = SEC_DMAMODE | (datain ? MODE_TDIR_IOTM : MODE_TDIR_MTIO);
    400        1.1     bjh21 	/* Program first block into DMAC and queue up second. */
    401        1.1     bjh21 	dmac_write(sc, NEC71071_CHANNEL, 0);
    402        1.3     bjh21 	if (!sc->sc_dmain)
    403        1.3     bjh21 		sec_copyoutblk(sc, 0);
    404        1.1     bjh21 	sec_dmablk(sc, 0);
    405        1.1     bjh21 	/* Mode control register */
    406        1.1     bjh21 	dmac_write(sc, NEC71071_MODE, mode);
    407        1.1     bjh21 	return sc->sc_dmalen;
    408        1.1     bjh21 }
    409        1.1     bjh21 
    410        1.1     bjh21 static int
    411        1.1     bjh21 sec_dmago(struct wd33c93_softc *sc_sbic)
    412        1.1     bjh21 {
    413        1.1     bjh21 	struct sec_softc *sc = (struct sec_softc *)sc_sbic;
    414        1.1     bjh21 
    415        1.1     bjh21 	dmac_write(sc, NEC71071_MASK, 0xe);
    416        1.8     bjh21 	sc->sc_dmaactive = true;
    417        1.3     bjh21 	if (!sc->sc_dmain && sc->sc_dmalen > SEC_DMABLK)
    418        1.3     bjh21 		sec_copyoutblk(sc, 1);
    419        1.1     bjh21 	return sc->sc_dmalen;
    420        1.1     bjh21 }
    421        1.1     bjh21 
    422        1.1     bjh21 static void
    423        1.1     bjh21 sec_dmastop(struct wd33c93_softc *sc_sbic)
    424        1.1     bjh21 {
    425        1.1     bjh21 	struct sec_softc *sc = (struct sec_softc *)sc_sbic;
    426        1.1     bjh21 
    427        1.1     bjh21 	dmac_write(sc, NEC71071_MASK, 0xf);
    428        1.3     bjh21 	if (sc->sc_dmaactive && sc->sc_dmain)
    429        1.3     bjh21 		sec_copyinblk(sc, sc->sc_dmablk);
    430        1.8     bjh21 	sc->sc_dmaactive = false;
    431        1.1     bjh21 }
    432        1.1     bjh21 
    433        1.1     bjh21 /*
    434        1.1     bjh21  * Reset the SCSI bus, and incidentally the SBIC and DMAC.
    435        1.1     bjh21  */
    436        1.1     bjh21 static void
    437        1.1     bjh21 sec_reset(struct wd33c93_softc *sc_sbic)
    438        1.1     bjh21 {
    439        1.1     bjh21 	struct sec_softc *sc = (struct sec_softc *)sc_sbic;
    440        1.1     bjh21 	uint8_t asr, csr;
    441        1.1     bjh21 
    442        1.1     bjh21 	bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR,
    443        1.1     bjh21 	    sc->sc_mpr | SEC_MPR_UR);
    444        1.1     bjh21 	DELAY(7);
    445        1.1     bjh21 	bus_space_write_1(sc->sc_pod_t, sc->sc_pod_h, SEC_MPR, sc->sc_mpr);
    446        1.1     bjh21 	/* Wait for and clear the reset-complete interrupt */
    447        1.1     bjh21 	do
    448        1.1     bjh21 		GET_SBIC_asr(sc_sbic, asr);
    449        1.1     bjh21 	while (!(asr & SBIC_ASR_INT));
    450        1.1     bjh21 	GET_SBIC_csr(sc_sbic, csr);
    451       1.16  christos 	__USE(csr);
    452        1.1     bjh21 	dmac_write(sc, NEC71071_DCTRL1, DCTRL1_CMP | DCTRL1_RQL);
    453        1.1     bjh21 	dmac_write(sc, NEC71071_DCTRL2, 0);
    454        1.1     bjh21 	sec_cli(sc);
    455        1.1     bjh21 }
    456        1.1     bjh21 
    457        1.1     bjh21 static int
    458        1.1     bjh21 sec_intr(void *arg)
    459        1.1     bjh21 {
    460        1.1     bjh21 	struct sec_softc *sc = arg;
    461        1.1     bjh21 	u_int8_t isr;
    462        1.1     bjh21 
    463        1.1     bjh21 	isr = bus_space_read_1(sc->sc_pod_t, sc->sc_pod_h, SEC_ISR);
    464        1.1     bjh21 	if (!(isr & SEC_ISR_IRQ))
    465        1.1     bjh21 		return 0;
    466        1.1     bjh21 	if (isr & SEC_ISR_DMAC)
    467        1.1     bjh21 		sec_dmatc(sc);
    468        1.1     bjh21 	if (isr & SEC_ISR_SBIC)
    469        1.1     bjh21 		wd33c93_intr(&sc->sc_sbic);
    470        1.1     bjh21 	return 1;
    471        1.1     bjh21 }
    472        1.1     bjh21 
    473        1.1     bjh21 static int
    474        1.1     bjh21 sec_dmatc(struct sec_softc *sc)
    475        1.1     bjh21 {
    476        1.1     bjh21 
    477        1.1     bjh21 	sec_cli(sc);
    478        1.1     bjh21 	/* DMAC finished block n-1 and is now working on block n */
    479        1.1     bjh21 	sc->sc_dmablk++;
    480        1.3     bjh21 	if (sc->sc_dmalen > sc->sc_dmablk * SEC_DMABLK) {
    481        1.3     bjh21 		dmac_write(sc, NEC71071_CHANNEL, 0);
    482        1.3     bjh21 		sec_dmablk(sc, sc->sc_dmablk);
    483        1.3     bjh21 		dmac_write(sc, NEC71071_MASK, 0xe);
    484        1.3     bjh21 		if (!sc->sc_dmain &&
    485        1.3     bjh21 		    sc->sc_dmalen > (sc->sc_dmablk + 1) * SEC_DMABLK)
    486        1.3     bjh21 			sec_copyoutblk(sc, sc->sc_dmablk + 1);
    487        1.1     bjh21 	} else {
    488        1.1     bjh21 		/* All blocks fully processed. */
    489        1.8     bjh21 		sc->sc_dmaactive = false;
    490        1.1     bjh21 	}
    491        1.3     bjh21 	if (sc->sc_dmain)
    492        1.3     bjh21 		sec_copyinblk(sc, sc->sc_dmablk - 1);
    493        1.1     bjh21 	return 1;
    494        1.1     bjh21 }
    495        1.1     bjh21 
    496        1.1     bjh21 #ifdef DDB
    497        1.1     bjh21 void
    498        1.1     bjh21 sec_dumpdma(void *arg)
    499        1.1     bjh21 {
    500        1.1     bjh21 	struct sec_softc *sc = arg;
    501        1.1     bjh21 
    502        1.1     bjh21 	dmac_write(sc, NEC71071_CHANNEL, 0);
    503        1.1     bjh21 	printf("%s: DMA state: cur count %02x%02x cur addr %02x%02x%02x ",
    504       1.13     bjh21 	    device_xname(sc->sc_sbic.sc_dev),
    505        1.1     bjh21 	    dmac_read(sc, NEC71071_COUNTHI), dmac_read(sc, NEC71071_COUNTLO),
    506        1.1     bjh21 	    dmac_read(sc, NEC71071_ADDRHI), dmac_read(sc, NEC71071_ADDRMID),
    507        1.1     bjh21 	    dmac_read(sc, NEC71071_ADDRLO));
    508        1.1     bjh21 	dmac_write(sc, NEC71071_CHANNEL, 0 | CHANNEL_WBASE);
    509        1.1     bjh21 	printf("base count %02x%02x base addr %02x%02x%02x\n",
    510        1.1     bjh21 	    dmac_read(sc, NEC71071_COUNTHI), dmac_read(sc, NEC71071_COUNTLO),
    511        1.1     bjh21 	    dmac_read(sc, NEC71071_ADDRHI), dmac_read(sc, NEC71071_ADDRMID),
    512        1.1     bjh21 	    dmac_read(sc, NEC71071_ADDRLO));
    513        1.1     bjh21 	printf("%s: DMA state: dctrl %1x%02x mode %02x status %02x req %02x "
    514        1.1     bjh21 	    "mask %02x\n",
    515       1.13     bjh21 	    device_xname(sc->sc_sbic.sc_dev), dmac_read(sc, NEC71071_DCTRL2),
    516        1.1     bjh21 	    dmac_read(sc, NEC71071_DCTRL1), dmac_read(sc, NEC71071_MODE),
    517        1.1     bjh21 	    dmac_read(sc, NEC71071_STATUS), dmac_read(sc, NEC71071_REQUEST),
    518        1.1     bjh21 	    dmac_read(sc, NEC71071_MASK));
    519       1.13     bjh21 	printf("%s: soft DMA state: %zd@%p%s%d\n",
    520       1.13     bjh21 	    device_xname(sc->sc_sbic.sc_dev),
    521        1.1     bjh21 	    sc->sc_dmalen, sc->sc_dmaaddr, sc->sc_dmain ? "<-" : "->",
    522        1.1     bjh21 	    sc->sc_dmaoff);
    523        1.1     bjh21 }
    524        1.1     bjh21 
    525        1.1     bjh21 void sec_dumpall(void); /* Call from DDB */
    526        1.1     bjh21 
    527        1.1     bjh21 extern struct cfdriver sec_cd;
    528        1.1     bjh21 
    529        1.1     bjh21 void sec_dumpall(void)
    530        1.1     bjh21 {
    531        1.1     bjh21 	int i;
    532       1.11    cegger 	struct sec_softc *sc;
    533        1.1     bjh21 
    534       1.11    cegger 	for (i = 0; i < sec_cd.cd_ndevs; ++i) {
    535       1.11    cegger 		sc = device_lookup_private(&sec_cd, i);
    536       1.11    cegger 		if (sc != NULL)
    537       1.11    cegger 			sec_dumpdma(sc);
    538       1.11    cegger 	}
    539        1.1     bjh21 }
    540        1.1     bjh21 #endif
    541