00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <string.h>
00062 #include <io.h>
00063
00064 #include "rtlregs.h"
00065 #include "util.h"
00066 #include "eboot.h"
00067 #include "arp.h"
00068 #include "ether.h"
00069
00070
00071
00072
00073
00074
00075 #define NIC_PAGE_SIZE 0x100
00076 #define NIC_START_PAGE 0x40
00077 #define NIC_STOP_PAGE 0x60
00078 #define NIC_TX_PAGES 6
00079 #define NIC_TX_BUFFERS 2
00080 #define NIC_FIRST_TX_PAGE NIC_START_PAGE
00081 #define NIC_FIRST_RX_PAGE (NIC_FIRST_TX_PAGE + NIC_TX_PAGES * NIC_TX_BUFFERS)
00082 #define TX_PAGES 12
00083
00084 u_char mac[6] = { 0x00,0x06,0x98,0x00,0x00,0x00 };
00085
00086 volatile u_char *base = (u_char *)0x8300;
00087
00088
00089
00090
00091 struct nic_pkt_header {
00092 u_char ph_status;
00093 u_char ph_nextpg;
00094 u_short ph_size;
00095 };
00096
00097
00098
00099
00100
00101
00102 static int NicReset(void)
00103 {
00104 u_char i;
00105 u_char j;
00106
00107 for(j = 0; j < 20; j++) {
00108 i = nic_read(NIC_RESET);
00109 Delay(500);
00110 nic_write(NIC_RESET, i);
00111 for(i = 0; i < 20; i++) {
00112 Delay(5000);
00113 if(nic_read(NIC_PG0_ISR) & NIC_ISR_RST)
00114 return 0;
00115 }
00116 }
00117 return -1;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126 void NicInit(void)
00127 {
00128 u_short i;
00129
00130
00131
00132
00133
00134 outp(BV(SRE) | BV(SRW), MCUCR);
00135
00136
00137 if(NicReset())
00138 return;
00139
00140 nic_write(NIC_PG0_IMR, 0);
00141 nic_write(NIC_PG0_ISR, 0xff);
00142 nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0 | NIC_CR_PS1);
00143 nic_write(NIC_PG3_EECR, NIC_EECR_EEM0 | NIC_EECR_EEM1);
00144 nic_write(NIC_PG3_CONFIG3, 0);
00145 nic_write(NIC_PG3_CONFIG2, NIC_CONFIG2_BSELB);
00146 nic_write(NIC_PG3_EECR, 0);
00147 Delay(50000);
00148 nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
00149 nic_write(NIC_PG0_DCR, NIC_DCR_LS | NIC_DCR_FT1);
00150 nic_write(NIC_PG0_RBCR0, 0);
00151 nic_write(NIC_PG0_RBCR1, 0);
00152 nic_write(NIC_PG0_RCR, NIC_RCR_MON);
00153 nic_write(NIC_PG0_TCR, NIC_TCR_LB0);
00154 nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
00155 nic_write(NIC_PG0_BNRY, NIC_STOP_PAGE - 1);
00156 nic_write(NIC_PG0_PSTART, NIC_FIRST_RX_PAGE);
00157 nic_write(NIC_PG0_PSTOP, NIC_STOP_PAGE);
00158 nic_write(NIC_PG0_ISR, 0xff);
00159 nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2 | NIC_CR_PS0);
00160 for(i = 0; i < 6; i++)
00161 nic_write(NIC_PG1_PAR0 + i, mac[i]);
00162 for(i = 0; i < 8; i++)
00163 nic_write(NIC_PG1_MAR0 + i, 0);
00164 nic_write(NIC_PG1_CURR, NIC_START_PAGE + TX_PAGES);
00165 nic_write(NIC_CR, NIC_CR_STP | NIC_CR_RD2);
00166 nic_write(NIC_PG0_RCR, NIC_RCR_AB);
00167 nic_write(NIC_PG0_ISR, 0xff);
00168 nic_write(NIC_PG0_IMR, 0);
00169 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00170 nic_write(NIC_PG0_TCR, 0);
00171 Delay(2000000);
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183 int EtherOutput(u_char *dmac, u_short type, u_short len)
00184 {
00185 u_short i;
00186 u_short sz;
00187 u_char *cp;
00188 ETHERHDR *eh;
00189 #if 0
00190 u_char isr;
00191 #endif
00192
00193 if(type == ETHERTYPE_ARP) {
00194 eh = &arpframe.eth_hdr;
00195 cp = (u_char *)&arpframe;
00196 }
00197 else {
00198 eh = &sframe.eth_hdr;
00199 cp = (u_char *)&sframe;
00200 }
00201
00202 for(i = 0; i < 6; i++)
00203 eh->ether_shost[i] = mac[i];
00204 if(dmac) {
00205 for(i = 0; i < 6; i++)
00206 eh->ether_dhost[i] = dmac[i];
00207 }
00208 else {
00209 for(i = 0; i < 6; i++)
00210 eh->ether_dhost[i] = 0xFF;
00211 }
00212 eh->ether_type = type;
00213 if((len += sizeof(ETHERHDR)) < 60)
00214 sz = 60;
00215 else
00216 sz = len;
00217
00218 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00219 nic_write(NIC_PG0_RBCR0, (u_char)sz);
00220 nic_write(NIC_PG0_RBCR1, (u_char)(sz >> 8));
00221 nic_write(NIC_PG0_RSAR0, 0);
00222 nic_write(NIC_PG0_RSAR1, NIC_FIRST_TX_PAGE);
00223
00224 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD1);
00225
00226
00227
00228
00229 for(i = 0; i < len; i++, cp++)
00230 nic_write(NIC_IOPORT, *cp);
00231 for(i = len; i < sz; i++)
00232 nic_write(NIC_IOPORT, 0);
00233
00234
00235
00236
00237 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00238 for(i = 0; i <= 20; i++)
00239 if(nic_read(NIC_PG0_ISR) & NIC_ISR_RDC)
00240 break;
00241 nic_write(NIC_PG0_ISR, NIC_ISR_RDC);
00242
00243
00244
00245
00246 nic_write(NIC_PG0_TBCR0, (sz & 0xff));
00247 nic_write(NIC_PG0_TBCR1, ((sz >> 8) & 0xff));
00248
00249
00250
00251
00252 nic_write(NIC_PG0_TPSR, NIC_FIRST_TX_PAGE);
00253
00254
00255
00256
00257 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_TXP | NIC_CR_RD2);
00258
00259
00260
00261
00262
00263 while(nic_read(NIC_CR) & NIC_CR_TXP);
00264
00265 return 0;
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279 int EtherInput(u_short type, u_short tms)
00280 {
00281 int rc = 0;
00282 u_char isr = 0;
00283 struct nic_pkt_header hdr;
00284 u_char *buf;
00285 u_char nextpg;
00286 u_char bnry;
00287 u_char curr;
00288 u_short i;
00289 u_char wtc;
00290
00291 while(tms && rc == 0) {
00292 for(wtc = 1; tms; wtc++) {
00293 isr = nic_read(NIC_PG0_ISR);
00294 if(isr & NIC_ISR_RXE) {
00295 return -1;
00296 }
00297 if(isr & NIC_ISR_PRX) {
00298 break;
00299 }
00300 if(wtc == 0)
00301 tms--;
00302 }
00303
00304
00305
00306
00307 if((isr & NIC_ISR_PRX) == 0) {
00308 nic_write(NIC_PG0_ISR, isr);
00309 break;
00310 }
00311
00312
00313
00314
00315
00316 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2 | NIC_CR_PS0);
00317 curr = nic_read(NIC_PG1_CURR);
00318 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00319
00320
00321
00322
00323
00324
00325
00326 if((bnry = nic_read(NIC_PG0_BNRY) + 1) >= NIC_STOP_PAGE)
00327 bnry = NIC_FIRST_RX_PAGE;
00328 if(bnry == curr) {
00329 nic_write(NIC_PG0_ISR, isr);
00330 continue;
00331 }
00332
00333
00334
00335
00336 nic_write(NIC_PG0_RBCR0, sizeof(struct nic_pkt_header));
00337 nic_write(NIC_PG0_RBCR1, 0);
00338 nic_write(NIC_PG0_RSAR0, 0);
00339 nic_write(NIC_PG0_RSAR1, bnry);
00340 buf = (u_char *)&hdr;
00341 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD0);
00342 for(i = 0; i < sizeof(struct nic_pkt_header); i++)
00343 *buf++ = nic_read(NIC_IOPORT);
00344
00345
00346
00347
00348 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00349 for(i = 0; i <= 20; i++) {
00350 if(nic_read(NIC_PG0_ISR) & NIC_ISR_RDC)
00351 break;
00352 }
00353 nic_write(NIC_PG0_ISR, NIC_ISR_RDC);
00354
00355
00356
00357
00358 if(hdr.ph_size < 60 + sizeof(struct nic_pkt_header) ||
00359 hdr.ph_size > 1518 + sizeof(struct nic_pkt_header)) {
00360 return 0;
00361 }
00362
00363
00364
00365
00366
00367
00368 nextpg = bnry + (hdr.ph_size >> 8) + ((hdr.ph_size & 0xFF) != 0);
00369 if(nextpg >= NIC_STOP_PAGE) {
00370 nextpg -= NIC_STOP_PAGE;
00371 nextpg += NIC_FIRST_RX_PAGE;
00372 }
00373 if(nextpg != hdr.ph_nextpg) {
00374 u_char nextpg1 = nextpg + 1;
00375
00376 if(nextpg1 >= NIC_STOP_PAGE) {
00377 nextpg1 -= NIC_STOP_PAGE;
00378 nextpg1 += NIC_FIRST_RX_PAGE;
00379 }
00380 if(nextpg1 != hdr.ph_nextpg) {
00381 nic_write(NIC_PG0_ISR, isr);
00382 break;
00383 }
00384 nextpg = nextpg1;
00385 }
00386
00387
00388
00389
00390 if((hdr.ph_status & 0x0F) == 1) {
00391 rc = hdr.ph_size - sizeof(struct nic_pkt_header);
00392
00393
00394
00395
00396
00397
00398 nic_write(NIC_PG0_RBCR0, (u_char)rc);
00399 nic_write(NIC_PG0_RBCR1, (u_char)((u_short)rc >> 8));
00400 nic_write(NIC_PG0_RSAR0, sizeof(struct nic_pkt_header));
00401 nic_write(NIC_PG0_RSAR1, bnry);
00402
00403
00404
00405
00406 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD0);
00407 buf = (u_char *)&rframe;
00408 for(i = 0; i < rc; i++)
00409 *buf++ = nic_read(NIC_IOPORT);
00410
00411
00412
00413
00414 nic_write(NIC_CR, NIC_CR_STA | NIC_CR_RD2);
00415 for(i = 0; i <= 20; i++) {
00416 if(nic_read(NIC_PG0_ISR) & NIC_ISR_RDC)
00417 break;
00418 }
00419 nic_write(NIC_PG0_ISR, NIC_ISR_RDC);
00420 }
00421
00422
00423
00424
00425 if(--nextpg < NIC_FIRST_RX_PAGE)
00426 nextpg = NIC_STOP_PAGE - 1;
00427 nic_write(NIC_PG0_BNRY, nextpg);
00428
00429
00430
00431
00432 if(rframe.eth_hdr.ether_type != type) {
00433 if(rframe.eth_hdr.ether_type == ETHERTYPE_ARP)
00434 ArpRespond();
00435 rc = 0;
00436 }
00437 }
00438 return rc;
00439 }
00440
00441