Home | History | Annotate | Line # | Download | only in dev
if_ed_zbus.c revision 1.2.6.2
      1  1.2.6.2  riz /*	$NetBSD: if_ed_zbus.c,v 1.2.6.2 2012/11/22 00:27:54 riz Exp $ */
      2  1.2.6.2  riz 
      3  1.2.6.2  riz /*-
      4  1.2.6.2  riz  * Copyright (c) 2012 The NetBSD Foundation, Inc.
      5  1.2.6.2  riz  * All rights reserved.
      6  1.2.6.2  riz  *
      7  1.2.6.2  riz  * This code is derived from software contributed to The NetBSD Foundation
      8  1.2.6.2  riz  * by Frank Wille.
      9  1.2.6.2  riz  *
     10  1.2.6.2  riz  * Redistribution and use in source and binary forms, with or without
     11  1.2.6.2  riz  * modification, are permitted provided that the following conditions
     12  1.2.6.2  riz  * are met:
     13  1.2.6.2  riz  * 1. Redistributions of source code must retain the above copyright
     14  1.2.6.2  riz  *    notice, this list of conditions and the following disclaimer.
     15  1.2.6.2  riz  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.2.6.2  riz  *    notice, this list of conditions and the following disclaimer in the
     17  1.2.6.2  riz  *    documentation and/or other materials provided with the distribution.
     18  1.2.6.2  riz  *
     19  1.2.6.2  riz  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.2.6.2  riz  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.2.6.2  riz  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.2.6.2  riz  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.2.6.2  riz  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.2.6.2  riz  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.2.6.2  riz  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.2.6.2  riz  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.2.6.2  riz  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.2.6.2  riz  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.2.6.2  riz  * POSSIBILITY OF SUCH DAMAGE.
     30  1.2.6.2  riz  */
     31  1.2.6.2  riz 
     32  1.2.6.2  riz /*
     33  1.2.6.2  riz  * Device driver for the Hydra Systems and ASDG ethernet cards.
     34  1.2.6.2  riz  * Based on the National Semiconductor DS8390/WD83C690.
     35  1.2.6.2  riz  */
     36  1.2.6.2  riz 
     37  1.2.6.2  riz #include <sys/cdefs.h>
     38  1.2.6.2  riz __KERNEL_RCSID(0, "$NetBSD: if_ed_zbus.c,v 1.2.6.2 2012/11/22 00:27:54 riz Exp $");
     39  1.2.6.2  riz 
     40  1.2.6.2  riz #include <sys/param.h>
     41  1.2.6.2  riz #include <sys/device.h>
     42  1.2.6.2  riz #include <sys/bus.h>
     43  1.2.6.2  riz 
     44  1.2.6.2  riz #include <net/if.h>
     45  1.2.6.2  riz #include <net/if_media.h>
     46  1.2.6.2  riz #include <net/if_ether.h>
     47  1.2.6.2  riz 
     48  1.2.6.2  riz #include <dev/ic/dp8390reg.h>
     49  1.2.6.2  riz #include <dev/ic/dp8390var.h>
     50  1.2.6.2  riz 
     51  1.2.6.2  riz #include <amiga/amiga/device.h>
     52  1.2.6.2  riz #include <amiga/amiga/isr.h>
     53  1.2.6.2  riz #include <amiga/dev/zbusvar.h>
     54  1.2.6.2  riz 
     55  1.2.6.2  riz #define ETHER_PAD_LEN	(ETHER_MIN_LEN - ETHER_CRC_LEN)
     56  1.2.6.2  riz 
     57  1.2.6.2  riz /* Hydra Systems AmigaNet */
     58  1.2.6.2  riz #define HYDRA_MANID	2121
     59  1.2.6.2  riz #define HYDRA_PRODID	1
     60  1.2.6.2  riz #define HYDRA_REGADDR	0xffe1
     61  1.2.6.2  riz #define	HYDRA_MEMADDR	0
     62  1.2.6.2  riz #define HYDRA_PROMADDR	0xffc0
     63  1.2.6.2  riz 
     64  1.2.6.2  riz /* ASDG LANRover */
     65  1.2.6.2  riz #define ASDG_MANID	1023
     66  1.2.6.2  riz #define ASDG_PRODID	254
     67  1.2.6.2  riz #define ASDG_REGADDR	0x1
     68  1.2.6.2  riz #define ASDG_MEMADDR	0x8000
     69  1.2.6.2  riz #define ASDG_PROMADDR	0x100
     70  1.2.6.2  riz 
     71  1.2.6.2  riz /* Buffer size is always 16k */
     72  1.2.6.2  riz #define ED_ZBUS_MEMSIZE	0x4000
     73  1.2.6.2  riz 
     74  1.2.6.2  riz int	ed_zbus_match(device_t, cfdata_t , void *);
     75  1.2.6.2  riz void	ed_zbus_attach(device_t, device_t, void *);
     76  1.2.6.2  riz int	ed_zbus_test_mem(struct dp8390_softc *);
     77  1.2.6.2  riz void	ed_zbus_read_hdr(struct dp8390_softc *, int, struct dp8390_ring *);
     78  1.2.6.2  riz int	ed_zbus_ring_copy(struct dp8390_softc *, int, void *, u_short);
     79  1.2.6.2  riz int	ed_zbus_write_mbuf(struct dp8390_softc *, struct mbuf *, int);
     80  1.2.6.2  riz 
     81  1.2.6.2  riz struct ed_zbus_softc {
     82  1.2.6.2  riz 	struct dp8390_softc	sc_dp8390;
     83  1.2.6.2  riz 	struct bus_space_tag	sc_bst;
     84  1.2.6.2  riz 	struct isr		sc_isr;
     85  1.2.6.2  riz };
     86  1.2.6.2  riz 
     87  1.2.6.2  riz CFATTACH_DECL_NEW(ed_zbus, sizeof(struct ed_zbus_softc),
     88  1.2.6.2  riz     ed_zbus_match, ed_zbus_attach, NULL, NULL);
     89  1.2.6.2  riz 
     90  1.2.6.2  riz 
     91  1.2.6.2  riz int
     92  1.2.6.2  riz ed_zbus_match(device_t parent, cfdata_t cf, void *aux)
     93  1.2.6.2  riz {
     94  1.2.6.2  riz 	struct zbus_args *zap = aux;
     95  1.2.6.2  riz 
     96  1.2.6.2  riz 	if (zap->manid == HYDRA_MANID && zap->prodid == HYDRA_PRODID)
     97  1.2.6.2  riz 		return 1;
     98  1.2.6.2  riz 	else if (zap->manid == ASDG_MANID && zap->prodid == ASDG_PRODID)
     99  1.2.6.2  riz 		return 1;
    100  1.2.6.2  riz 	return 0;
    101  1.2.6.2  riz }
    102  1.2.6.2  riz 
    103  1.2.6.2  riz void
    104  1.2.6.2  riz ed_zbus_attach(device_t parent, device_t self, void *aux)
    105  1.2.6.2  riz {
    106  1.2.6.2  riz 	struct ed_zbus_softc *zsc = device_private(self);
    107  1.2.6.2  riz 	struct dp8390_softc *sc = &zsc->sc_dp8390;
    108  1.2.6.2  riz 	struct zbus_args *zap = aux;
    109  1.2.6.2  riz 	bus_space_handle_t promh;
    110  1.2.6.2  riz 	bus_addr_t memaddr, promaddr, regaddr;
    111  1.2.6.2  riz 	int i;
    112  1.2.6.2  riz 
    113  1.2.6.2  riz 	zsc->sc_bst.base = (bus_addr_t)zap->va;
    114  1.2.6.2  riz 	zsc->sc_bst.absm = &amiga_bus_stride_1;
    115  1.2.6.2  riz 
    116  1.2.6.2  riz 	if (zap->manid == HYDRA_MANID) {
    117  1.2.6.2  riz 		regaddr = HYDRA_REGADDR;
    118  1.2.6.2  riz 		memaddr = HYDRA_MEMADDR;
    119  1.2.6.2  riz 		promaddr = HYDRA_PROMADDR;
    120  1.2.6.2  riz 	} else {
    121  1.2.6.2  riz 		regaddr = ASDG_REGADDR;
    122  1.2.6.2  riz 		memaddr = ASDG_MEMADDR;
    123  1.2.6.2  riz 		promaddr = ASDG_PROMADDR;
    124  1.2.6.2  riz 	}
    125  1.2.6.2  riz 
    126  1.2.6.2  riz 	sc->sc_dev = self;
    127  1.2.6.2  riz 	sc->sc_regt = &zsc->sc_bst;
    128  1.2.6.2  riz 	sc->sc_buft = &zsc->sc_bst;
    129  1.2.6.2  riz 
    130  1.2.6.2  riz 	if (bus_space_map(sc->sc_regt, regaddr, 0x20, 0, &sc->sc_regh)) {
    131  1.2.6.2  riz 		aprint_error_dev(self, "can't map i/o space\n");
    132  1.2.6.2  riz 		return;
    133  1.2.6.2  riz 	}
    134  1.2.6.2  riz 
    135  1.2.6.2  riz 	if (bus_space_map(sc->sc_buft, memaddr, ED_ZBUS_MEMSIZE, 0,
    136  1.2.6.2  riz 	    &sc->sc_bufh)) {
    137  1.2.6.2  riz 		aprint_error_dev(self, "can't map buffer space\n");
    138  1.2.6.2  riz 		return;
    139  1.2.6.2  riz 	}
    140  1.2.6.2  riz 
    141  1.2.6.2  riz 	/* SRAM buffer size is always 16K */
    142  1.2.6.2  riz 	sc->mem_start = 0;
    143  1.2.6.2  riz 	sc->mem_size = ED_ZBUS_MEMSIZE;
    144  1.2.6.2  riz 
    145  1.2.6.2  riz 	/*
    146  1.2.6.2  riz 	 * Read the ethernet address from the PROM.
    147  1.2.6.2  riz 	 * Interrupts must be inactive when reading the PROM, as the
    148  1.2.6.2  riz 	 * interrupt line is shared with one of its address lines.
    149  1.2.6.2  riz 	 */
    150  1.2.6.2  riz 
    151  1.2.6.2  riz 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_IMR, 0x00);
    152  1.2.6.2  riz 	NIC_PUT(sc->sc_regt, sc->sc_regh, ED_P0_ISR, 0xff);
    153  1.2.6.2  riz 
    154  1.2.6.2  riz 	if (bus_space_map(&zsc->sc_bst, promaddr, ETHER_ADDR_LEN * 2, 0,
    155  1.2.6.2  riz 	    &promh) == 0) {
    156  1.2.6.2  riz 		for (i = 0; i < ETHER_ADDR_LEN; i++)
    157  1.2.6.2  riz 			sc->sc_enaddr[i] =
    158  1.2.6.2  riz 			    bus_space_read_1(&zsc->sc_bst, promh, i * 2);
    159  1.2.6.2  riz 
    160  1.2.6.2  riz 		bus_space_unmap(&zsc->sc_bst, promh, ETHER_ADDR_LEN * 2);
    161  1.2.6.2  riz 	}
    162  1.2.6.2  riz 
    163  1.2.6.2  riz 	/* Initialize sc_reg_map[]. Registers have stride 2 on the bus. */
    164  1.2.6.2  riz 	for (i = 0; i < 16; i++)
    165  1.2.6.2  riz 		sc->sc_reg_map[i] = i << 1;
    166  1.2.6.2  riz 
    167  1.2.6.2  riz 	/*
    168  1.2.6.2  riz 	 * Set 2 word FIFO threshold, no auto-init Remote DMA,
    169  1.2.6.2  riz 	 * byte order 68k, word-wide DMA xfers.
    170  1.2.6.2  riz 	 */
    171  1.2.6.2  riz 	sc->dcr_reg = ED_DCR_FT0 | ED_DCR_WTS | ED_DCR_LS | ED_DCR_BOS;
    172  1.2.6.2  riz 
    173  1.2.6.2  riz 	/* Remote DMA abort .*/
    174  1.2.6.2  riz 	sc->cr_proto = ED_CR_RD2;
    175  1.2.6.2  riz 
    176  1.2.6.2  riz 	/*
    177  1.2.6.2  riz 	 * Override all functions which deal with the buffer, because
    178  1.2.6.2  riz 	 * this implementation only allows 16-bit buffer accesses.
    179  1.2.6.2  riz 	 */
    180  1.2.6.2  riz 	sc->test_mem = ed_zbus_test_mem;
    181  1.2.6.2  riz 	sc->read_hdr = ed_zbus_read_hdr;
    182  1.2.6.2  riz 	sc->ring_copy = ed_zbus_ring_copy;
    183  1.2.6.2  riz 	sc->write_mbuf = ed_zbus_write_mbuf;
    184  1.2.6.2  riz 
    185  1.2.6.2  riz 	sc->sc_flags = device_cfdata(self)->cf_flags;
    186  1.2.6.2  riz 	sc->is790 = 0;
    187  1.2.6.2  riz 	sc->sc_media_init = dp8390_media_init;
    188  1.2.6.2  riz 	sc->sc_enabled = 1;
    189  1.2.6.2  riz 
    190  1.2.6.2  riz 	/* Do generic DS8390/WD83C690 config. */
    191  1.2.6.2  riz 	if (dp8390_config(sc)) {
    192  1.2.6.2  riz 		bus_space_unmap(sc->sc_buft, sc->sc_bufh, ED_ZBUS_MEMSIZE);
    193  1.2.6.2  riz 		bus_space_unmap(sc->sc_regt, sc->sc_regh, 0x10);
    194  1.2.6.2  riz 		return;
    195  1.2.6.2  riz 	}
    196  1.2.6.2  riz 
    197  1.2.6.2  riz 	/* establish level 2 interrupt handler */
    198  1.2.6.2  riz 	zsc->sc_isr.isr_intr = dp8390_intr;
    199  1.2.6.2  riz 	zsc->sc_isr.isr_arg = sc;
    200  1.2.6.2  riz 	zsc->sc_isr.isr_ipl = 2;
    201  1.2.6.2  riz 	add_isr(&zsc->sc_isr);
    202  1.2.6.2  riz }
    203  1.2.6.2  riz 
    204  1.2.6.2  riz int
    205  1.2.6.2  riz ed_zbus_test_mem(struct dp8390_softc *sc)
    206  1.2.6.2  riz {
    207  1.2.6.2  riz 	bus_space_tag_t buft = sc->sc_buft;
    208  1.2.6.2  riz 	bus_space_handle_t bufh = sc->sc_bufh;
    209  1.2.6.2  riz 	int i;
    210  1.2.6.2  riz 
    211  1.2.6.2  riz 	bus_space_set_region_2(buft, bufh, sc->mem_start, 0,
    212  1.2.6.2  riz 	    sc->mem_size >> 1);
    213  1.2.6.2  riz 
    214  1.2.6.2  riz 	for (i = 0; i < sc->mem_size; i += 2) {
    215  1.2.6.2  riz 		if (bus_space_read_2(sc->sc_buft, sc->sc_bufh, i)) {
    216  1.2.6.2  riz 			printf(": failed to clear NIC buffer at offset %x - "
    217  1.2.6.2  riz 			    "check configuration\n", (sc->mem_start + i));
    218  1.2.6.2  riz 			return 1;
    219  1.2.6.2  riz 		}
    220  1.2.6.2  riz 	}
    221  1.2.6.2  riz 	return 0;
    222  1.2.6.2  riz }
    223  1.2.6.2  riz 
    224  1.2.6.2  riz void
    225  1.2.6.2  riz ed_zbus_read_hdr(struct dp8390_softc *sc, int src, struct dp8390_ring *hdrp)
    226  1.2.6.2  riz {
    227  1.2.6.2  riz 	bus_space_tag_t buft = sc->sc_buft;
    228  1.2.6.2  riz 	bus_space_handle_t bufh = sc->sc_bufh;
    229  1.2.6.2  riz 	uint16_t wrd[2];
    230  1.2.6.2  riz 
    231  1.2.6.2  riz 	/*
    232  1.2.6.2  riz 	 * Read the 4-byte header as two 16-bit words in little-endian
    233  1.2.6.2  riz 	 * format. Convert into big-endian and put them into hdrp.
    234  1.2.6.2  riz 	 */
    235  1.2.6.2  riz 	bus_space_read_region_stream_2(buft, bufh, src, wrd, 2);
    236  1.2.6.2  riz 	hdrp->rsr = wrd[0] & 0xff;
    237  1.2.6.2  riz 	hdrp->next_packet = wrd[0] >> 8;
    238  1.2.6.2  riz 	hdrp->count = bswap16(wrd[1]);
    239  1.2.6.2  riz }
    240  1.2.6.2  riz 
    241  1.2.6.2  riz /*
    242  1.2.6.2  riz  * Copy `amount' bytes from a packet in the ring buffer to a linear
    243  1.2.6.2  riz  * destination buffer, given a source offset and destination address.
    244  1.2.6.2  riz  * Takes into account ring-wrap.
    245  1.2.6.2  riz  */
    246  1.2.6.2  riz int
    247  1.2.6.2  riz ed_zbus_ring_copy(struct dp8390_softc *sc, int src, void *dst, u_short amount)
    248  1.2.6.2  riz {
    249  1.2.6.2  riz 	bus_space_tag_t buft = sc->sc_buft;
    250  1.2.6.2  riz 	bus_space_handle_t bufh = sc->sc_bufh;
    251  1.2.6.2  riz 	u_short tmp_amount;
    252  1.2.6.2  riz 	u_char readbyte[2];
    253  1.2.6.2  riz 
    254  1.2.6.2  riz 	/* Does copy wrap to lower addr in ring buffer? */
    255  1.2.6.2  riz 	if (src + amount > sc->mem_end) {
    256  1.2.6.2  riz 		tmp_amount = sc->mem_end - src;
    257  1.2.6.2  riz 
    258  1.2.6.2  riz 		/* copy amount up to end of NIC memory */
    259  1.2.6.2  riz 		bus_space_read_region_stream_2(buft, bufh, src, dst,
    260  1.2.6.2  riz 		    tmp_amount >> 1);
    261  1.2.6.2  riz 
    262  1.2.6.2  riz 		amount -= tmp_amount;
    263  1.2.6.2  riz 		src = sc->mem_ring;
    264  1.2.6.2  riz 		dst = (u_char *)dst + tmp_amount;
    265  1.2.6.2  riz 	}
    266  1.2.6.2  riz 
    267  1.2.6.2  riz 	bus_space_read_region_stream_2(buft, bufh, src, dst, amount >> 1);
    268  1.2.6.2  riz 
    269  1.2.6.2  riz 	/* handle odd length packet */
    270  1.2.6.2  riz 	if (amount & 1) {
    271  1.2.6.2  riz 		bus_space_read_region_stream_2(buft, bufh, src + amount - 1,
    272  1.2.6.2  riz 		    (u_int16_t *)readbyte, 1);
    273  1.2.6.2  riz 		*((u_char *)dst + amount - 1) = readbyte[0];
    274  1.2.6.2  riz 		amount++;
    275  1.2.6.2  riz 	}
    276  1.2.6.2  riz 
    277  1.2.6.2  riz 	return src + amount;
    278  1.2.6.2  riz }
    279  1.2.6.2  riz 
    280  1.2.6.2  riz /*
    281  1.2.6.2  riz  * Copy packet from mbuf to the board memory. Currently uses an extra
    282  1.2.6.2  riz  * buffer/extra memory copy, unless the whole packet fits in one mbuf.
    283  1.2.6.2  riz  * As in the test_mem function, we use word-wide writes.
    284  1.2.6.2  riz  */
    285  1.2.6.2  riz int
    286  1.2.6.2  riz ed_zbus_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf)
    287  1.2.6.2  riz {
    288  1.2.6.2  riz 	u_char *data, savebyte[2];
    289  1.2.6.2  riz 	int len, wantbyte;
    290  1.2.6.2  riz 	u_short totlen = 0;
    291  1.2.6.2  riz 
    292  1.2.6.2  riz 	wantbyte = 0;
    293  1.2.6.2  riz 
    294  1.2.6.2  riz 	for (; m ; m = m->m_next) {
    295  1.2.6.2  riz 		data = mtod(m, u_char *);
    296  1.2.6.2  riz 		len = m->m_len;
    297  1.2.6.2  riz 		totlen += len;
    298  1.2.6.2  riz 		if (len > 0) {
    299  1.2.6.2  riz 			/* Finish the last word. */
    300  1.2.6.2  riz 			if (wantbyte) {
    301  1.2.6.2  riz 				savebyte[1] = *data;
    302  1.2.6.2  riz 				bus_space_write_region_stream_2(sc->sc_buft,
    303  1.2.6.2  riz 				    sc->sc_bufh, buf,
    304  1.2.6.2  riz 				    (u_int16_t *)savebyte, 1);
    305  1.2.6.2  riz 				buf += 2;
    306  1.2.6.2  riz 				data++;
    307  1.2.6.2  riz 				len--;
    308  1.2.6.2  riz 				wantbyte = 0;
    309  1.2.6.2  riz 			}
    310  1.2.6.2  riz 			/* Output contiguous words. */
    311  1.2.6.2  riz 			if (len > 1) {
    312  1.2.6.2  riz 				bus_space_write_region_stream_2(sc->sc_buft,
    313  1.2.6.2  riz 				    sc->sc_bufh, buf,
    314  1.2.6.2  riz 				    (u_int16_t *)data, len >> 1);
    315  1.2.6.2  riz 				buf += len & ~1;
    316  1.2.6.2  riz 				data += len & ~1;
    317  1.2.6.2  riz 				len &= 1;
    318  1.2.6.2  riz 			}
    319  1.2.6.2  riz 			/* Save last byte, if necessary. */
    320  1.2.6.2  riz 			if (len == 1) {
    321  1.2.6.2  riz 				savebyte[0] = *data;
    322  1.2.6.2  riz 				wantbyte = 1;
    323  1.2.6.2  riz 			}
    324  1.2.6.2  riz 		}
    325  1.2.6.2  riz 	}
    326  1.2.6.2  riz 
    327  1.2.6.2  riz 	len = ETHER_PAD_LEN - totlen;
    328  1.2.6.2  riz 	if (wantbyte) {
    329  1.2.6.2  riz 		savebyte[1] = 0;
    330  1.2.6.2  riz 		bus_space_write_region_stream_2(sc->sc_buft, sc->sc_bufh,
    331  1.2.6.2  riz 		    buf, (u_int16_t *)savebyte, 1);
    332  1.2.6.2  riz 		buf += 2;
    333  1.2.6.2  riz 		totlen++;
    334  1.2.6.2  riz 		len--;
    335  1.2.6.2  riz 	}
    336  1.2.6.2  riz 	/* if sent data is shorter than EHTER_PAD_LEN, put 0 to padding */
    337  1.2.6.2  riz 	if (len > 0) {
    338  1.2.6.2  riz 		bus_space_set_region_2(sc->sc_buft, sc->sc_bufh, buf, 0,
    339  1.2.6.2  riz 		    len >> 1);
    340  1.2.6.2  riz 		totlen = ETHER_PAD_LEN;
    341  1.2.6.2  riz 	}
    342  1.2.6.2  riz 	return totlen;
    343  1.2.6.2  riz }
    344