1 /* $NetBSD: if_ae.c,v 1.79 2007/01/06 13:25:19 martin 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.79 2007/01/06 13:25:19 martin 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 #define ETHER_PAD_LEN (ETHER_MIN_LEN - ETHER_CRC_LEN) 38 39 int 40 ae_size_card_memory(bus_space_tag_t bst, bus_space_handle_t bsh, int ofs) 41 { 42 int i1, i2, i3, i4, i8; 43 44 /* 45 * banks; also assume it will generally mirror in upper banks 46 * if not installed. 47 */ 48 i1 = (8192 * 0); 49 i2 = (8192 * 1); 50 i3 = (8192 * 2); 51 i4 = (8192 * 3); 52 53 i8 = (8192 * 4); 54 55 bus_space_write_2(bst, bsh, ofs + i8, 0x8888); 56 bus_space_write_2(bst, bsh, ofs + i4, 0x4444); 57 bus_space_write_2(bst, bsh, ofs + i3, 0x3333); 58 bus_space_write_2(bst, bsh, ofs + i2, 0x2222); 59 bus_space_write_2(bst, bsh, ofs + i1, 0x1111); 60 61 /* 62 * 1) If the memory range is decoded completely, it does not 63 * matter what we write first: High tags written into 64 * the void are lost. 65 * 2) If the memory range is not decoded completely (banks are 66 * mirrored), high tags are overwritten by lower ones. 67 * 3) Lazy implementation of pathological cases - none found yet. 68 */ 69 70 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 && 71 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 && 72 bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 && 73 bus_space_read_2(bst, bsh, ofs + i4) == 0x4444 && 74 bus_space_read_2(bst, bsh, ofs + i8) == 0x8888) 75 return 8192 * 8; 76 77 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 && 78 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222 && 79 bus_space_read_2(bst, bsh, ofs + i3) == 0x3333 && 80 bus_space_read_2(bst, bsh, ofs + i4) == 0x4444) 81 return 8192 * 4; 82 83 if ((bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 && 84 bus_space_read_2(bst, bsh, ofs + i2) == 0x2222) || 85 (bus_space_read_2(bst, bsh, ofs + i1) == 0x3333 && 86 bus_space_read_2(bst, bsh, ofs + i2) == 0x4444)) 87 return 8192 * 2; 88 89 if (bus_space_read_2(bst, bsh, ofs + i1) == 0x1111 || 90 bus_space_read_2(bst, bsh, ofs + i1) == 0x4444) 91 return 8192; 92 93 return 0; 94 } 95 96 /* 97 * Zero memory and verify that it is clear. The only difference between 98 * this and the default test_mem function is that the DP8390-based NuBus 99 * cards * apparently require word-wide writes and byte-wide reads, an 100 * `interesting' combination. 101 */ 102 int 103 ae_test_mem(struct dp8390_softc *sc) 104 { 105 bus_space_tag_t buft = sc->sc_buft; 106 bus_space_handle_t bufh = sc->sc_bufh; 107 int i; 108 109 bus_space_set_region_2(buft, bufh, sc->mem_start, 0, 110 sc->mem_size / 2); 111 112 for (i = 0; i < sc->mem_size; ++i) { 113 if (bus_space_read_1(sc->sc_buft, sc->sc_bufh, i)) { 114 printf(": failed to clear NIC buffer at offset %x - " 115 "check configuration\n", (sc->mem_start + i)); 116 return 1; 117 } 118 } 119 120 return 0; 121 } 122 123 /* 124 * Copy packet from mbuf to the board memory Currently uses an extra 125 * buffer/extra memory copy, unless the whole packet fits in one mbuf. 126 * 127 * As in the test_mem function, we use word-wide writes. 128 */ 129 int 130 ae_write_mbuf(struct dp8390_softc *sc, struct mbuf *m, int buf) 131 { 132 u_char *data, savebyte[2]; 133 int len, wantbyte; 134 u_short totlen = 0; 135 136 wantbyte = 0; 137 138 for (; m ; m = m->m_next) { 139 data = mtod(m, u_char *); 140 len = m->m_len; 141 totlen += len; 142 if (len > 0) { 143 /* Finish the last word. */ 144 if (wantbyte) { 145 savebyte[1] = *data; 146 bus_space_write_region_2(sc->sc_buft, 147 sc->sc_bufh, buf, (u_int16_t *)savebyte, 1); 148 buf += 2; 149 data++; 150 len--; 151 wantbyte = 0; 152 } 153 /* Output contiguous words. */ 154 if (len > 1) { 155 bus_space_write_region_2( 156 sc->sc_buft, sc->sc_bufh, 157 buf, (u_int16_t *)data, len >> 1); 158 buf += len & ~1; 159 data += len & ~1; 160 len &= 1; 161 } 162 /* Save last byte, if necessary. */ 163 if (len == 1) { 164 savebyte[0] = *data; 165 wantbyte = 1; 166 } 167 } 168 } 169 170 len = ETHER_PAD_LEN - totlen; 171 if (wantbyte) { 172 savebyte[1] = 0; 173 bus_space_write_region_2(sc->sc_buft, sc->sc_bufh, 174 buf, (u_int16_t *)savebyte, 1); 175 buf += 2; 176 totlen++; 177 len--; 178 } 179 /* if sent data is shorter than EHTER_PAD_LEN, put 0 to padding */ 180 if (len > 0) { 181 bus_space_set_region_2(sc->sc_buft, sc->sc_bufh, buf, 0, 182 len >> 1); 183 totlen = ETHER_PAD_LEN; 184 } 185 return (totlen); 186 } 187