1 /* $NetBSD: if_ae.c,v 1.77 2005/12/11 12:18:02 christos Exp $ */ 2 3 /* 4 * Device driver for National Semiconductor DS8390/WD83C690 based ethernet 5 * adapters. 6 * 7 * Copyright (c) 1994, 1995 Charles M. Hannum. All rights reserved. 8 * 9 * Copyright (C) 1993, David Greenman. This software may be used, modified, 10 * copied, distributed, and sold, in both source and binary form provided that 11 * the above copyright and these terms are retained. Under no circumstances is 12 * the author responsible for the proper functioning of this software, nor does 13 * the author assume any responsibility for damages incurred with its use. 14 */ 15 16 #include <sys/cdefs.h> 17 __KERNEL_RCSID(0, "$NetBSD: if_ae.c,v 1.77 2005/12/11 12:18:02 christos Exp $"); 18 19 #include "bpfilter.h" 20 21 #include <sys/param.h> 22 #include <sys/systm.h> 23 #include <sys/device.h> 24 #include <sys/mbuf.h> 25 #include <sys/socket.h> 26 27 #include <net/if.h> 28 #include <net/if_media.h> 29 #include <net/if_ether.h> 30 31 #include <machine/bus.h> 32 33 #include <dev/ic/dp8390reg.h> 34 #include <dev/ic/dp8390var.h> 35 #include <mac68k/dev/if_aevar.h> 36 37 int 38 ae_size_card_memory(bus_space_tag_t bst, bus_space_handle_t bsh, int ofs) 39 { 40 int i1, i2, i3, i4, i8; 41 42 /* 43 * banks; also assume it will generally mirror in upper banks 44 * if not installed. 45 */ 46 i1 = (8192 * 0); 47 i2 = (8192 * 1); 48 i3 = (8192 * 2); 49 i4 = (8192 * 3); 50 51 i8 = (8192 * 4); 52 53 bus_space_write_2(bst, bsh, ofs + i8, 0x8888); 54 bus_space_write_2(bst, bsh, ofs + i4, 0x4444); 55 bus_space_write_2(bst, bsh, ofs + i3, 0x3333); 56 bus_space_write_2(bst, bsh, ofs + i2, 0x2222); 57 bus_space_write_2(bst, bsh, ofs + i1, 0x1111); 58 59 /* 60 * 1) If the memory range is decoded completely, it does not 61 * matter what we write first: High tags written into 62 * the void are lost. 63 * 2) If the memory range is not decoded completely (banks are 64 * mirrored), high tags are overwritten by lower ones. 65 * 3) Lazy implementation of pathological cases - none found yet. 66 */ 67 68 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 && 69 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 && 70 bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 && 71 bus_space_read_2(bst, bsh, ofs + i4) == 0x4444 && 72 bus_space_read_2(bst, bsh, ofs + i8) == 0x8888) 73 return 8192 * 8; 74 75 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 && 76 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 && 77 bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 && 78 bus_space_read_2(bst, bsh, ofs + i4) == 0x4444) 79 return 8192 * 4; 80 81 if ((bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 && 82 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222) || 83 (bus_space_read_2(bst, bsh, ofs + i1) == 0x3333 && 84 bus_space_read_2(bst, bsh, ofs + i2) == 0x4444)) 85 return 8192 * 2; 86 87 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 || 88 bus_space_read_2(bst, bsh, ofs + i1) == 0x4444) 89 return 8192; 90 91 return 0; 92 } 93 94 /* 95 * Zero memory and verify that it is clear. The only difference between 96 * this and the default test_mem function is that the DP8390-based NuBus 97 * cards * apparently require word-wide writes and byte-wide reads, an 98 * `interesting' combination. 99 */ 100 int 101 ae_test_mem(struct dp8390_softc *sc) 102 { 103 bus_space_tag_t buft = sc->sc_buft; 104 bus_space_handle_t bufh = sc->sc_bufh; 105 int i; 106 107 bus_space_set_region_2(buft, bufh, sc->mem_start, 0, 108 sc->mem_size / 2); 109 110 for (i = 0; i < sc->mem_size; ++i) { 111 if (bus_space_read_1(sc->sc_buft, sc->sc_bufh, i)) { 112 printf(": failed to clear NIC buffer at offset %x - " 113 "check configuration\n", (sc->mem_start + i)); 114 return 1; 115 } 116 } 117 118 return 0; 119 } 120 121 /* 122 * Copy packet from mbuf to the board memory Currently uses an extra 123 * buffer/extra memory copy, unless the whole packet fits in one mbuf. 124 * 125 * As in the test_mem function, we use word-wide writes. 126 */ 127 int 128 ae_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf) 129 { 130 u_char *data, savebyte[2]; 131 int len, wantbyte; 132 u_short totlen = 0; 133 134 wantbyte = 0; 135 136 for (; m ; m = m->m_next) { 137 data = mtod(m, u_char *); 138 len = m->m_len; 139 totlen += len; 140 if (len > 0) { 141 /* Finish the last word. */ 142 if (wantbyte) { 143 savebyte[1] = *data; 144 bus_space_write_region_2(sc->sc_buft, 145 sc->sc_bufh, buf, (u_int16_t *)savebyte, 1); 146 buf += 2; 147 data++; 148 len--; 149 wantbyte = 0; 150 } 151 /* Output contiguous words. */ 152 if (len > 1) { 153 bus_space_write_region_2( 154 sc->sc_buft, sc->sc_bufh, 155 buf, (u_int16_t *)data, len >> 1); 156 buf += len & ~1; 157 data += len & ~1; 158 len &= 1; 159 } 160 /* Save last byte, if necessary. */ 161 if (len == 1) { 162 savebyte[0] = *data; 163 wantbyte = 1; 164 } 165 } 166 } 167 168 if (wantbyte) { 169 savebyte[1] = 0; 170 bus_space_write_region_2(sc->sc_buft, sc->sc_bufh, 171 buf, (u_int16_t *)savebyte, 1); 172 buf += 2; 173 } 174 if (totlen < ETHER_MIN_LEN - ETHER_CRC_LEN) { 175 bus_space_set_region_2(sc->sc_buft, sc->sc_bufh, buf, 0, 176 (ETHER_MIN_LEN - ETHER_CRC_LEN - totlen) >> 1); 177 totlen = ETHER_MIN_LEN - ETHER_CRC_LEN; 178 } 179 return (totlen); 180 } 181