OpenCores
URL https://opencores.org/ocsvn/plasma/plasma/trunk

Subversion Repositories plasma

[/] [plasma/] [trunk/] [kernel/] [ethernet.c] - Blame information for rev 430

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 416 rhoads
/*--------------------------------------------------------------------
2
 * TITLE: Plasma Ethernet MAC
3
 * AUTHOR: Steve Rhoads (rhoadss@yahoo.com)
4
 * DATE CREATED: 1/12/08
5
 * FILENAME: ethernet.c
6
 * PROJECT: Plasma CPU core
7
 * COPYRIGHT: Software placed into the public domain by the author.
8
 *    Software 'as is' without warranty.  Author liable for nothing.
9
 * DESCRIPTION:
10
 *    Ethernet MAC implementation.
11
 *    Data is received from the Ethernet PHY four bits at a time.
12
 *    After 32-bits are received they are written to 0x13ff0000 + N.
13
 *    The data is received LSB first for each byte which requires the
14
 *    nibbles to be swapped.
15
 *    Transmit data is read from 0x13fe0000.  Write length/4+1 to
16
 *    ETHERNET_REG to start transfer.
17
 *--------------------------------------------------------------------*/
18
#include "plasma.h"
19
#include "rtos.h"
20
#include "tcpip.h"
21
 
22
#define POLYNOMIAL  0x04C11DB7   //CRC bit 33 is truncated
23
#define TOPBIT      (1<<31)
24
#define BYTE_EMPTY  0xde         //Data copied into receive buffer
25
#define COUNT_EMPTY 16           //Count to decide there isn't data
26
#define INDEX_MASK  0xffff       //Size of receive buffer
27
 
28
static unsigned char gDestMac[]={0x5d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
29
static unsigned int CrcTable[256];
30
static unsigned char reflect[256];
31
static unsigned char reflectNibble[256];
32
static OS_Semaphore_t *SemEthernet, *SemEthTransmit;
33
static int gIndex;          //byte index into 0x13ff0000 receive buffer
34 418 rhoads
static int gCrcChecked;
35 416 rhoads
 
36
 
37
//Read received data from 0x13ff0000.  Data starts with 0x5d+MACaddress.
38
//Data is being received while processing the data.  Therefore,
39
//all errors require waiting and then re-processing the data
40
//to see if the error is fixed by receiving the rest of the packet.
41
int EthernetReceive(unsigned char *buffer, int length)
42
{
43
   int count;
44 418 rhoads
   int start, i, j, shift, offset, index, emptyCount;
45 416 rhoads
   int byte, byteNext;
46
   unsigned long crc;
47
   int byteCrc;
48
   volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
49
   int packetExpected;
50
 
51
   //Find the start of a frame
52
   packetExpected = MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_RECEIVE;
53
   MemoryRead(ETHERNET_REG);        //clear receive interrupt
54 418 rhoads
   emptyCount = 0;
55 416 rhoads
 
56
   //Find dest MAC address
57
   for(offset = 0; offset <= INDEX_MASK; ++offset)
58
   {
59
      index = (gIndex + offset) & INDEX_MASK;
60
      byte = buf[index];
61
      if(byte == 0x5d)  //bit pattern 01011101
62
      {
63
         for(i = 1; i < sizeof(gDestMac); ++i)
64
         {
65
            j = (index + i) & INDEX_MASK;
66
            byte = buf[j];
67
            if(byte != 0xff && byte != gDestMac[i])
68
               break;
69
         }
70
         if(i == sizeof(gDestMac))
71
            break;    //found dest MAC
72 418 rhoads
         emptyCount = 0;
73 416 rhoads
      }
74 418 rhoads
      else if(byte == BYTE_EMPTY)
75
      {
76
         if(packetExpected == 0 && ++emptyCount >= 4)
77
            return 0;
78
      }
79
      else
80
         emptyCount = 0;
81 416 rhoads
   }
82
   if(offset > INDEX_MASK)
83
      return 0;
84
   while(gIndex != index)
85
   {
86
      buf[gIndex] = BYTE_EMPTY;
87
      gIndex = (gIndex + 1) & INDEX_MASK;
88
   }
89
 
90
   //Found start of frame.  Now find end of frame and check CRC.
91
   start = gIndex;
92
   gIndex = (gIndex + 1) & INDEX_MASK;           //skip 0x5d byte
93
   crc = 0xffffffff;
94
   for(count = 0; count < length; )
95
   {
96
      byte = buf[gIndex];
97
      gIndex = (gIndex + 1) & INDEX_MASK;
98
 
99
      byte = ((byte << 4) & 0xf0) | (byte >> 4); //swap nibbles
100
      buffer[count++] = (unsigned char)byte;
101
      byte = reflect[byte] ^ (crc >> 24);        //calculate CRC32
102
      crc = CrcTable[byte] ^ (crc << 8);
103
      if(count >= 40)
104
      {
105
         //Check if CRC matches to detect end of frame
106
         byteCrc = reflectNibble[crc >> 24];
107
         byteNext = buf[gIndex];
108
         if(byteCrc == byteNext)
109
         {
110
            for(i = 1; i < 4; ++i)
111
            {
112
               shift = 24 - (i << 3);
113
               byteCrc = reflectNibble[(crc >> shift) & 0xff];
114
               byteNext = buf[(gIndex + i) & 0xffff];
115
               if(byteCrc != byteNext)
116
               {
117
                  //printf("nope %d %d 0x%x 0x%x\n", count, i, byteCrc, byteNext);
118
                  i = 99;
119
               }
120
            }
121
            if(i == 4)
122
            {
123
               //Found end of frame -- set used bytes to BYTE_EMPTY
124
               //printf("Found it! %d\n", count);
125
               gIndex = (gIndex + 4) & INDEX_MASK;
126
               for(i = 0; i < count+5; ++i)
127
                  buf[(start + i) & INDEX_MASK] = BYTE_EMPTY;
128
               while(gIndex & 3)
129
               {
130
                  buf[gIndex] = BYTE_EMPTY;
131
                  gIndex = (gIndex + 1) & INDEX_MASK;
132
               }
133 418 rhoads
               gCrcChecked = 0;
134 416 rhoads
               return count;
135
            }
136
         }
137
      }
138
   }
139
   gIndex = start;
140 418 rhoads
   if(++gCrcChecked > 0)     //if the CPU speed is > 25MHz, change to 1
141 416 rhoads
   {
142
      buf[gIndex] = BYTE_EMPTY;
143
      gIndex = (gIndex + 1) & INDEX_MASK;
144
   }
145 418 rhoads
   return -1;
146 416 rhoads
}
147
 
148
 
149
//Copy transmit data to 0x13fe0000 with preamble and CRC32
150
void EthernetTransmit(unsigned char *buffer, int length)
151
{
152
   int i, byte, shift;
153
   unsigned long crc;
154
   volatile unsigned char *buf = (unsigned char*)ETHERNET_TRANSMIT;
155
 
156
   OS_SemaphorePend(SemEthTransmit, OS_WAIT_FOREVER);
157
 
158
   //Wait for previous transfer to complete
159
   for(i = 0; i < 10000; ++i)
160
   {
161
      if(MemoryRead(IRQ_STATUS) & IRQ_ETHERNET_TRANSMIT)
162
         break;
163
   }
164
 
165
   Led(2, 2);
166
   while(length < 60 || (length & 3) != 0)
167
      buffer[length++] = 0;
168
 
169
   //Start of Ethernet frame
170
   for(i = 0; i < 7; ++i)
171
      buf[i] = 0x55;
172
   buf[7] = 0x5d;
173
 
174
   //Calculate CRC32
175
   crc = 0xffffffff;
176
   for(i = 0; i < length; ++i)
177
   {
178
      byte = buffer[i];
179
      buf[i + 8] = (unsigned char)((byte << 4) | (byte >> 4)); //swap nibbles
180
      byte = reflect[byte] ^ (crc >> 24);        //calculate CRC32
181
      crc = CrcTable[byte] ^ (crc << 8);
182
   }
183
 
184
   //Output CRC32
185
   for(i = 0; i < 4; ++i)
186
   {
187
      shift = 24 - (i << 3);
188
      byte = reflectNibble[(crc >> shift) & 0xff];
189
      buf[length + 8 + i] = (unsigned char)byte;
190
   }
191
 
192
   //Start transfer
193
   length = (length + 12 + 4) >> 2;
194
   MemoryWrite(ETHERNET_REG, length);
195
   Led(2, 0);
196
 
197
   OS_SemaphorePost(SemEthTransmit);
198
}
199
 
200
 
201
void EthernetThread(void *arg)
202
{
203
   int length;
204 375 rhoads
   int rc;
205 416 rhoads
   unsigned int ticks, ticksLast=0;
206 375 rhoads
   IPFrame *ethFrame=NULL;
207 416 rhoads
   (void)arg;
208
 
209
   for(;;)
210 375 rhoads
   {
211 418 rhoads
      OS_InterruptMaskSet(IRQ_ETHERNET_RECEIVE);    //enable interrupt
212
      OS_SemaphorePend(SemEthernet, 50);            //wait for interrupt
213 416 rhoads
 
214
      //Process all received packets
215
      for(;;)
216
      {
217 375 rhoads
         if(ethFrame == NULL)
218 416 rhoads
            ethFrame = IPFrameGet(FRAME_COUNT_RCV);
219
         if(ethFrame == NULL)
220 375 rhoads
            break;
221 416 rhoads
         length = EthernetReceive(ethFrame->packet, PACKET_SIZE);
222 373 rhoads
         if(length == 0)
223 418 rhoads
            break;         //no packet found
224
         if(length < 0)
225
            continue;      //CRC didn't match; process next packet
226 416 rhoads
         Led(1, 1);
227
         rc = IPProcessEthernetPacket(ethFrame, length);
228
         Led(1, 0);
229
         if(rc)
230
            ethFrame = NULL;
231
      }
232
 
233
      ticks = OS_ThreadTime();
234
      if(ticks - ticksLast >= 50)
235
      {
236
         IPTick();
237
         ticksLast = ticks;
238
      }
239
   }
240
}
241
 
242
 
243
void EthernetIsr(void *arg)
244
{
245 373 rhoads
   (void)arg;
246 416 rhoads
   OS_InterruptMaskClear(IRQ_ETHERNET_RECEIVE);
247 373 rhoads
   OS_SemaphorePost(SemEthernet);
248 416 rhoads
}
249
 
250
 
251
/******************* CRC32 calculations **********************
252 430 rhoads
 * The CRC32 code is modified from Michael Barr's article in
253 416 rhoads
 * Embedded Systems Programming January 2000.
254
 * A CRC is really modulo-2 binary division.  Substraction means XOR. */
255
static unsigned int Reflect(unsigned int value, int bits)
256
{
257
   unsigned int num=0;
258
   int i;
259
   for(i = 0; i < bits; ++i)
260
   {
261
      num = (num << 1) | (value & 1);
262
      value >>= 1;
263
   }
264
   return num;
265
}
266
 
267
 
268
static void CrcInit(void)
269
{
270
   unsigned int remainder;
271
   int dividend, bit, i;
272
 
273
   //Compute the remainder of each possible dividend
274
   for(dividend = 0; dividend < 256; ++dividend)
275
   {
276
      //Start with the dividend followed by zeros
277
      remainder = dividend << 24;
278
      //Perform modulo-2 division, a bit at a time
279
      for(bit = 8; bit > 0; --bit)
280
      {
281
         //Try to divide the current data bit
282
         if(remainder & TOPBIT)
283
            remainder = (remainder << 1) ^ POLYNOMIAL;
284
         else
285
            remainder = remainder << 1;
286
      }
287
      CrcTable[dividend] = remainder;
288
   }
289
   for(i = 0; i < 256; ++i)
290
   {
291
      reflect[i] = (unsigned char)Reflect(i, 8);
292
      reflectNibble[i] = (unsigned char)((Reflect((i >> 4) ^ 0xf, 4) << 4) |
293
         Reflect(i ^ 0xf, 4));
294
   }
295
}
296
 
297
 
298
static void SpinWait(int clocks)
299
{
300
   int value = *(volatile int*)COUNTER_REG + clocks;
301
   while(*(volatile int*)COUNTER_REG - value < 0)
302
      ;
303
}
304
 
305
 
306 422 rhoads
//Use the Ethernet MDIO bus to configure the Ethernet PHY
307
static int EthernetConfigure(int index, int value)
308
{
309
   unsigned int data;
310
   int i, bit, rc=0;
311
 
312
   //Format of SMI data: 0101 A4:A0 R4:R0 00 D15:D0
313
   if(value <= 0xffff)
314
      data = 0x5f800000;  //write
315
   else
316
      data = 0x6f800000;  //read
317
   data |= index << 18 | value;
318
 
319
   MemoryWrite(GPIO0_SET, ETHERNET_MDIO | ETHERNET_MDIO_WE | ETHERNET_MDC);
320
   for(i = 0; i < 34; ++i)
321
   {
322
      MemoryWrite(GPIO0_SET, ETHERNET_MDC);    //clock high
323
      SpinWait(10);
324
      MemoryWrite(GPIO0_CLEAR, ETHERNET_MDC);  //clock low
325
      SpinWait(10);
326
   }
327
   for(i = 31; i >= 0; --i)
328
   {
329
      bit = (data >> i) & 1;
330
      if(bit)
331
         MemoryWrite(GPIO0_SET, ETHERNET_MDIO);
332
      else
333
         MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO);
334
      SpinWait(10);
335
      MemoryWrite(GPIO0_SET, ETHERNET_MDC);    //clock high
336
      SpinWait(10);
337
      rc = rc << 1 | ((MemoryRead(GPIOA_IN) >> 13) & 1);
338
      MemoryWrite(GPIO0_CLEAR, ETHERNET_MDC);  //clock low
339
      SpinWait(10);
340
      if(value > 0xffff && i == 17)
341
         MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO_WE);
342
   }
343
   MemoryWrite(GPIO0_CLEAR, ETHERNET_MDIO | ETHERNET_MDIO_WE | ETHERNET_MDC);
344
   return (rc >> 1) & 0xffff;
345
}
346
 
347
 
348 416 rhoads
void EthernetInit(unsigned char MacAddress[6])
349
{
350
   int i, value;
351
   volatile unsigned char *buf = (unsigned char*)ETHERNET_RECEIVE;
352
 
353 375 rhoads
   CrcInit();
354
   if(MacAddress)
355 416 rhoads
   {
356
      for(i = 0; i < 6; ++i)
357
      {
358
         value = MacAddress[i];
359
         gDestMac[i+1] = (unsigned char)((value >> 4) | (value << 4));
360 375 rhoads
      }
361 416 rhoads
   }
362
 
363 422 rhoads
   EthernetConfigure(4, 0x0061);        //advertise 10Base-T full duplex
364
   EthernetConfigure(0, 0x1300);        //start auto negotiation
365
#if 0
366
   OS_ThreadSleep(100);
367
   printf("reg4=0x%x (0x61)\n", EthernetConfigure(4, 0x10000));
368
   printf("reg0=0x%x (0x1100)\n", EthernetConfigure(0, 0x10000));
369
   printf("reg1=status=0x%x (0x7809)\n", EthernetConfigure(1, 0x10000));
370
   printf("reg5=partner=0x%x (0x01e1)\n", EthernetConfigure(5, 0x10000));
371
#endif
372 416 rhoads
 
373 422 rhoads
   MemoryWrite(GPIO0_CLEAR, ETHERNET_ENABLE);
374
 
375 416 rhoads
   //Clear receive buffer
376
   for(i = 0; i <= INDEX_MASK; ++i)
377
      buf[i] = BYTE_EMPTY;
378
 
379 385 rhoads
   if(SemEthernet == NULL)
380
   {
381 416 rhoads
      SemEthernet = OS_SemaphoreCreate("eth", 0);
382
      SemEthTransmit = OS_SemaphoreCreate("ethT", 1);
383
      OS_ThreadCreate("eth", EthernetThread, NULL, 240, 0);
384
   }
385
 
386
   //Setup interrupts for receiving data
387 375 rhoads
   OS_InterruptRegister(IRQ_ETHERNET_RECEIVE, EthernetIsr);
388 416 rhoads
 
389
   //Start receive DMA
390 422 rhoads
   MemoryWrite(GPIO0_SET, ETHERNET_ENABLE);
391 416 rhoads
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.