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