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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [sw/] [tests/] [ethmac/] [sim/] [ethmac-rxtxoverflow.c] - Blame information for rev 755

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

Line No. Rev Author Line
1 530 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  Interrupt-driven Ethernet MAC transmit test code            ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  Attempt to overflow the RX path in MAC                      ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////      - jb, jb@orsoc.se, with parts taken from Linux kernel   ////
10
////        open_eth driver.                                      ////
11
////                                                              ////
12
////                                                              ////
13
//////////////////////////////////////////////////////////////////////
14
////                                                              ////
15
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
16
////                                                              ////
17
//// This source file may be used and distributed without         ////
18
//// restriction provided that this copyright statement is not    ////
19
//// removed from the file and that any derivative work contains  ////
20
//// the original copyright notice and the associated disclaimer. ////
21
////                                                              ////
22
//// This source file is free software; you can redistribute it   ////
23
//// and/or modify it under the terms of the GNU Lesser General   ////
24
//// Public License as published by the Free Software Foundation; ////
25
//// either version 2.1 of the License, or (at your option) any   ////
26
//// later version.                                               ////
27
////                                                              ////
28
//// This source is distributed in the hope that it will be       ////
29
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
30
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
31
//// PURPOSE.  See the GNU Lesser General Public License for more ////
32
//// details.                                                     ////
33
////                                                              ////
34
//// You should have received a copy of the GNU Lesser General    ////
35
//// Public License along with this source; if not, download it   ////
36
//// from http://www.opencores.org/lgpl.shtml                     ////
37
////                                                              ////
38
//////////////////////////////////////////////////////////////////////
39
 
40
// Test sends the test code (0x1) for overflow test in first packet, which is
41
// read by Verilog stimulus module.
42
 
43
// SW is setup to echo back all packets it receives. TB sends 16 packets, as
44
// fast as possible, which should saturate the MAC's buffers.
45
// TB then waits for a BD to be ready and sends one more packet, and ensures
46
// that arrives in memory OK.
47
// Test is over when this software manages to send back 4 reply packets.
48
 
49
#include "cpu-utils.h"
50
#include "board.h"
51
#include "int.h"
52
#include "ethmac.h"
53
#include "eth-phy-mii.h"
54
 
55
volatile unsigned tx_done;
56
volatile unsigned rx_done;
57
static int next_tx_buf_num;
58
 
59
/* Functions in this file */
60
void ethmac_setup(void);
61
/* Interrupt functions */
62
void oeth_interrupt(void);
63
static void oeth_rx(void);
64
static void oeth_tx(void);
65
 
66
/* Let the ethernet packets use a space beginning here for buffering */
67
#define ETH_BUFF_BASE 0x01000000
68
 
69
 
70
#define RXBUFF_PREALLOC 1
71
#define TXBUFF_PREALLOC 1
72
//#undef RXBUFF_PREALLOC
73
//#undef TXBUFF_PREALLOC
74
 
75
/* The transmitter timeout
76
*/
77
#define TX_TIMEOUT      (2*HZ)
78
 
79
/* Buffer number (must be 2^n)
80
*/
81
#define OETH_RXBD_NUM           16
82
#define OETH_TXBD_NUM           16
83
#define OETH_RXBD_NUM_MASK      (OETH_RXBD_NUM-1)
84
#define OETH_TXBD_NUM_MASK      (OETH_TXBD_NUM-1)
85
 
86
/* Buffer size
87
*/
88
#define OETH_RX_BUFF_SIZE       0x600-4
89
#define OETH_TX_BUFF_SIZE       0x600-4
90
 
91
/* Buffer size  (if not XXBUF_PREALLOC
92
*/
93
#define MAX_FRAME_SIZE          1518
94
 
95
/* The buffer descriptors track the ring buffers.
96
*/
97
struct oeth_private {
98
  //struct      sk_buff* rx_skbuff[OETH_RXBD_NUM];
99
  //struct      sk_buff* tx_skbuff[OETH_TXBD_NUM];
100
 
101
  unsigned short        tx_next; /* Next buffer to be sent */
102
  unsigned short        tx_last; /* Next buffer to be checked if packet sent */
103
  unsigned short        tx_full; /* Buffer ring fuul indicator */
104
  unsigned short        rx_cur;  /* Next buffer to be checked if packet
105
                                    received */
106
 
107
  oeth_regs     *regs;                  /* Address of controller registers. */
108
  oeth_bd               *rx_bd_base;            /* Address of Rx BDs. */
109
  oeth_bd               *tx_bd_base;            /* Address of Tx BDs. */
110
 
111
  //    struct net_device_stats stats;
112
};
113
 
114
#define PHYNUM 7
115
 
116
void
117
eth_mii_write(char phynum, short regnum, short data)
118
{
119
  static volatile oeth_regs *regs = (oeth_regs *)(OETH_REG_BASE);
120
  regs->miiaddress = (regnum << 8) | phynum;
121
  regs->miitx_data = data;
122
  regs->miicommand = OETH_MIICOMMAND_WCTRLDATA;
123
  regs->miicommand = 0;
124
  while(regs->miistatus & OETH_MIISTATUS_BUSY);
125
}
126
 
127
short
128
eth_mii_read(char phynum, short regnum)
129
{
130
  static volatile oeth_regs *regs = (oeth_regs *)(OETH_REG_BASE);
131
  regs->miiaddress = (regnum << 8) | phynum;
132
  regs->miicommand = OETH_MIICOMMAND_RSTAT;
133
  regs->miicommand = 0;
134
  while(regs->miistatus & OETH_MIISTATUS_BUSY);
135
 
136
  return regs->miirx_data;
137
}
138
 
139
 
140
 
141
// Wait here until all packets have been transmitted
142
void
143
wait_until_all_tx_clear(void)
144
{
145
  int i;
146
  volatile oeth_bd *tx_bd;
147
  tx_bd = (volatile oeth_bd *)OETH_BD_BASE; /* Search from beginning*/
148
 
149
  int some_tx_waiting = 1;
150
 
151
  while (some_tx_waiting)
152
    {
153
      some_tx_waiting = 0;
154
      /* Go through the TX buffs, search for unused one */
155
      for(i = 0; i < OETH_TXBD_NUM; i++) {
156
 
157
        // Looking for buffer ready for transmit
158
        if((tx_bd[i].len_status & OETH_TX_BD_READY))
159
          some_tx_waiting = 1;
160
 
161
      }
162
    }
163
}
164
 
165
 
166
void
167
ethphy_set_10mbit(int phynum)
168
{
169
  wait_until_all_tx_clear();
170
  // Hardset PHY to just use 10Mbit mode
171
  short cr = eth_mii_read(phynum, MII_BMCR);
172
  cr &= ~BMCR_ANENABLE; // Clear auto negotiate bit
173
  cr &= ~BMCR_SPEED100; // Clear fast eth. bit
174
  eth_mii_write(phynum, MII_BMCR, cr);
175
}
176
 
177
 
178
void
179
ethphy_set_100mbit(int phynum)
180
{
181
  wait_until_all_tx_clear();
182
  // Hardset PHY to just use 100Mbit mode
183
  short cr = eth_mii_read(phynum, MII_BMCR);
184
  cr |= BMCR_ANENABLE; // Clear auto negotiate bit
185
  cr |= BMCR_SPEED100; // Clear fast eth. bit
186
  eth_mii_write(phynum, MII_BMCR, cr);
187
}
188
 
189
 
190
void
191
ethmac_setup(void)
192
{
193
  // from arch/or32/drivers/open_eth.c
194
  volatile oeth_regs *regs;
195
 
196
  regs = (oeth_regs *)(OETH_REG_BASE);
197
 
198
  /* Reset MII mode module */
199
  regs->miimoder = OETH_MIIMODER_RST; /* MII Reset ON */
200
  regs->miimoder &= ~OETH_MIIMODER_RST; /* MII Reset OFF */
201
  regs->miimoder = 0x64; /* Clock divider for MII Management interface */
202
 
203
  /* Reset the controller.
204
  */
205
  regs->moder = OETH_MODER_RST; /* Reset ON */
206
  regs->moder &= ~OETH_MODER_RST;       /* Reset OFF */
207
 
208
  /* Setting TXBD base to OETH_TXBD_NUM.
209
  */
210
  regs->tx_bd_num = OETH_TXBD_NUM;
211
 
212
 
213
  /* Set min/max packet length
214
  */
215
  regs->packet_len = 0x00400600;
216
 
217
  /* Set IPGT register to recomended value
218
  */
219
  regs->ipgt = 0x12;
220
 
221
  /* Set IPGR1 register to recomended value
222
  */
223
  regs->ipgr1 = 0x0000000c;
224
 
225
  /* Set IPGR2 register to recomended value
226
  */
227
  regs->ipgr2 = 0x00000012;
228
 
229
  /* Set COLLCONF register to recomended value
230
  */
231
  regs->collconf = 0x000f003f;
232
 
233
  /* Set control module mode
234
  */
235
#if 0
236
  regs->ctrlmoder = OETH_CTRLMODER_TXFLOW | OETH_CTRLMODER_RXFLOW;
237
#else
238
  regs->ctrlmoder = 0;
239
#endif
240
 
241
  /* Clear MIIM registers */
242
  regs->miitx_data = 0;
243
  regs->miiaddress = 0;
244
  regs->miicommand = 0;
245
 
246
  regs->mac_addr1 = ETH_MACADDR0 << 8 | ETH_MACADDR1;
247
  regs->mac_addr0 = ETH_MACADDR2 << 24 | ETH_MACADDR3 << 16 | ETH_MACADDR4 << 8 | ETH_MACADDR5;
248
 
249
  /* Clear all pending interrupts
250
  */
251
  regs->int_src = 0xffffffff;
252
 
253
  /* Promisc, IFG, CRCEn
254
  */
255
  regs->moder |= OETH_MODER_PRO | OETH_MODER_PAD | OETH_MODER_IFG | OETH_MODER_CRCEN | OETH_MODER_FULLD;
256
 
257
  /* Enable interrupt sources.
258
  */
259
 
260
  regs->int_mask = OETH_INT_MASK_TXB    |
261
    OETH_INT_MASK_TXE   |
262
    OETH_INT_MASK_RXF   |
263
    OETH_INT_MASK_RXE   |
264
    OETH_INT_MASK_BUSY  |
265
    OETH_INT_MASK_TXC   |
266
    OETH_INT_MASK_RXC;
267
 
268
  // Buffer setup stuff
269
  volatile oeth_bd *tx_bd, *rx_bd;
270
  int i,j,k;
271
 
272
  /* Initialize TXBD pointer
273
  */
274
  tx_bd = (volatile oeth_bd *)OETH_BD_BASE;
275
 
276
  /* Initialize RXBD pointer
277
  */
278
  rx_bd = ((volatile oeth_bd *)OETH_BD_BASE) + OETH_TXBD_NUM;
279
 
280
  /* Preallocated ethernet buffer setup */
281
  unsigned long mem_addr = ETH_BUFF_BASE; /* Defined at top */
282
 
283
  // Setup TX Buffers
284
  for(i = 0; i < OETH_TXBD_NUM; i++) {
285
    //tx_bd[i].len_status = OETH_TX_BD_PAD | OETH_TX_BD_CRC | OETH_RX_BD_IRQ;
286
    tx_bd[i].len_status = OETH_TX_BD_PAD | OETH_TX_BD_CRC;
287
    tx_bd[i].addr = mem_addr;
288
    mem_addr += OETH_TX_BUFF_SIZE;
289
  }
290
  tx_bd[OETH_TXBD_NUM - 1].len_status |= OETH_TX_BD_WRAP;
291
 
292
  // Setup RX buffers
293
  for(i = 0; i < OETH_RXBD_NUM; i++) {
294
    rx_bd[i].len_status = OETH_RX_BD_EMPTY | OETH_RX_BD_IRQ; // Init. with IRQ
295
    rx_bd[i].addr = mem_addr;
296
    mem_addr += OETH_RX_BUFF_SIZE;
297
  }
298
  rx_bd[OETH_RXBD_NUM - 1].len_status |= OETH_RX_BD_WRAP; // Last buffer wraps
299
 
300
  /* Enable RX and TX in MAC
301
  */
302
  regs->moder &= ~(OETH_MODER_RXEN | OETH_MODER_TXEN);
303
  regs->moder |= OETH_MODER_RXEN | OETH_MODER_TXEN;
304
 
305
  next_tx_buf_num = 0; // init tx buffer pointer
306
 
307
  return;
308
}
309
 
310
// Enable RX in ethernet MAC
311
void
312
oeth_enable_rx(void)
313
{
314
  volatile oeth_regs *regs;
315
  regs = (oeth_regs *)(OETH_REG_BASE);
316
  regs->moder |= OETH_MODER_RXEN;
317
}
318
 
319
// Disable RX in ethernet MAC
320
void
321
oeth_disable_rx(void)
322
{
323
  volatile oeth_regs *regs;
324
  regs = (oeth_regs *)(OETH_REG_BASE);
325
  regs->moder &= ~(OETH_MODER_RXEN);
326
}
327
 
328
 
329
/* Setup buffer descriptors with data */
330
/* length is in BYTES */
331
void tx_packet(void* data, int length)
332
{
333
  volatile oeth_regs *regs;
334
  regs = (oeth_regs *)(OETH_REG_BASE);
335
 
336
  volatile oeth_bd *tx_bd;
337
  volatile int i;
338
 
339
  tx_bd = (volatile oeth_bd *)OETH_BD_BASE;
340
  tx_bd = (struct oeth_bd*) &tx_bd[next_tx_buf_num];
341
 
342
  // If it's in use - wait
343
  while ((tx_bd->len_status & OETH_TX_BD_IRQ));
344
 
345
  /* Clear all of the status flags.
346
  */
347
  tx_bd->len_status &= ~OETH_TX_BD_STATS;
348
 
349
  /* If the frame is short, tell CPM to pad it.
350
  */
351
#define ETH_ZLEN        60   /* Min. octets in frame sans FCS */
352
  if (length <= ETH_ZLEN)
353
    tx_bd->len_status |= OETH_TX_BD_PAD;
354
  else
355
    tx_bd->len_status &= ~OETH_TX_BD_PAD;
356
 
357
  if ((int) data != 0){
358
    //Copy the data into the transmit buffer, byte at a time 
359
    char* data_p = (char*) data;
360
    char* data_b = (char*) tx_bd->addr;
361
    for(i=0;i<length;i++)
362
      {
363
        data_b[i] = data_p[i];
364
      }
365
  }
366
 
367
  /* Set the length of the packet's data in the buffer descriptor */
368
  tx_bd->len_status = (tx_bd->len_status & 0x0000ffff) |
369
    ((length&0xffff) << 16);
370
 
371
  /* Send it on its way.  Tell controller its ready, interrupt when sent
372
  * and to put the CRC on the end.
373
  */
374
  tx_bd->len_status |= (OETH_TX_BD_READY  | OETH_TX_BD_CRC | OETH_TX_BD_IRQ);
375
 
376
  next_tx_buf_num = (next_tx_buf_num + 1) & OETH_TXBD_NUM_MASK;
377
 
378
  tx_done++;
379
 
380
  // This test over after 4 packets sent.
381
  if (tx_done == 4)
382
  {
383
          report(0x8000000d);
384
          exit(0);
385
  }
386
 
387
  return;
388
 
389
 
390
}
391
 
392
/* The interrupt handler.
393
*/
394
void
395
oeth_interrupt(void)
396
{
397
 
398
  volatile oeth_regs *regs;
399
  regs = (oeth_regs *)(OETH_REG_BASE);
400
 
401
  uint  int_events;
402
  int serviced;
403
 
404
  serviced = 0;
405
 
406
  /* Get the interrupt events that caused us to be here.
407
  */
408
  int_events = regs->int_src;
409
  regs->int_src = int_events;
410
 
411
  /* Handle receive event in its own function.
412
  */
413
  if (int_events & (OETH_INT_RXF | OETH_INT_RXE)) {
414
    serviced |= 0x1;
415
    oeth_rx();
416
  }
417
 
418
  /* Handle transmit event in its own function.
419
  */
420
  if (int_events & (OETH_INT_TXB | OETH_INT_TXE)) {
421
    serviced |= 0x2;
422
    oeth_tx();
423
    serviced |= 0x2;
424
 
425
  }
426
 
427
  /* Check for receive busy, i.e. packets coming but no place to
428
  * put them.
429
  */
430
  if (int_events & OETH_INT_BUSY) {
431
    serviced |= 0x4;
432
    if (!(int_events & (OETH_INT_RXF | OETH_INT_RXE)))
433
      oeth_rx();
434
  }
435
 
436
  return;
437
}
438
 
439
 
440
 
441
static void
442
oeth_rx(void)
443
{
444
  volatile oeth_regs *regs;
445
  regs = (oeth_regs *)(OETH_REG_BASE);
446
 
447
  volatile oeth_bd *rx_bdp;
448
  int   pkt_len, i;
449
  int   bad = 0;
450
 
451
  rx_bdp = ((oeth_bd *)OETH_BD_BASE) + OETH_TXBD_NUM;
452
 
453
 
454
  /* Find RX buffers marked as having received data */
455
  for(i = 0; i < OETH_RXBD_NUM; i++)
456
    {
457
      bad=0;
458
      if(!(rx_bdp[i].len_status & OETH_RX_BD_EMPTY)){ /* Looking for NOT empty buffers desc. */
459
        /* Check status for errors.
460
        */
461
        if (rx_bdp[i].len_status & (OETH_RX_BD_TOOLONG | OETH_RX_BD_SHORT)) {
462
          bad = 1;
463
          report(0xbaad0001);
464
        }
465
        if (rx_bdp[i].len_status & OETH_RX_BD_DRIBBLE) {
466
          bad = 1;
467
          report(0xbaad0002);
468
        }
469
        if (rx_bdp[i].len_status & OETH_RX_BD_CRCERR) {
470
          bad = 1;
471
          report(0xbaad0003);
472
        }
473
        if (rx_bdp[i].len_status & OETH_RX_BD_OVERRUN) {
474
          bad = 1;
475
          report(0xbaad0004);
476
        }
477
        if (rx_bdp[i].len_status & OETH_RX_BD_MISS) {
478
                // not necessarily an issue
479
          report(0xbaad0005);
480
        }
481
        if (rx_bdp[i].len_status & OETH_RX_BD_LATECOL) {
482
          bad = 1;
483
          report(0xbaad0006);
484
        }
485
        if (bad) {
486
          rx_bdp[i].len_status &= ~OETH_RX_BD_STATS;
487
          rx_bdp[i].len_status |= OETH_RX_BD_EMPTY;
488
          exit(0xbaaaaaad);
489
 
490
          continue;
491
        }
492
        else {
493
          /* Process the incoming frame.
494
          */
495
          pkt_len = rx_bdp[i].len_status >> 16;
496
 
497
          // Attempt to transmit it back
498
          tx_packet((void*)rx_bdp[i].addr,pkt_len);
499
 
500
          /* finish up */
501
          rx_bdp[i].len_status &= ~OETH_RX_BD_STATS; /* Clear stats */
502
          rx_bdp[i].len_status |= OETH_RX_BD_EMPTY; /* Mark RX BD as empty */
503
          rx_done++;
504
        }
505
      }
506
    }
507
}
508
 
509
 
510
 
511
static void
512
oeth_tx(void)
513
{
514
  volatile oeth_bd *tx_bd;
515
  int i;
516
 
517
  tx_bd = (volatile oeth_bd *)OETH_BD_BASE; /* Search from beginning*/
518
 
519
  /* Go through the TX buffs, search for one that was just sent */
520
  for(i = 0; i < OETH_TXBD_NUM; i++)
521
    {
522
      /* Looking for buffer NOT ready for transmit. and IRQ enabled */
523
      if( (!(tx_bd[i].len_status & (OETH_TX_BD_READY))) && (tx_bd[i].len_status & (OETH_TX_BD_IRQ)) )
524
        {
525
          /* Single threaded so no chance we have detected a buffer that has had its IRQ bit set but not its BD_READ flag. Maybe this won't work in linux */
526
          tx_bd[i].len_status &= ~OETH_TX_BD_IRQ;
527
 
528
          /* Probably good to check for TX errors here */
529
 
530
          /* set our test variable */
531
          tx_done++;
532
 
533
          // This test over after 4 packets sent.
534
          if (tx_done == 4)
535
          {
536
                  report(0x8000000d);
537
                  exit(0);
538
          }
539
 
540
        }
541
    }
542
  return;
543
}
544
 
545
// Send a packet, the very first byte of which will be read by the testbench
546
// and used to indicate which test we'll use.
547
static char cmd_tx_buffer[40];
548
void
549
send_ethmac_rxtx_test_init_packet(char test)
550
{
551
        int i;
552
 
553
        cmd_tx_buffer[0] = test;
554
        // Clear rest of buffer
555
        for(i=0;i<40;i++) cmd_tx_buffer[i] = test;
556
        tx_packet(cmd_tx_buffer,  40); // Smallest packet that can be sent
557
}
558
 
559
// Loop to check if a number is prime by doing mod divide of the number
560
// to test by every number less than it
561
int
562
is_prime_number(unsigned long n)
563
{
564
  unsigned long c;
565
  if (n < 2) return 0;
566
  for(c=2;c<n;c++)
567
    if ((n % c) == 0)
568
      return 0;
569
  return 1;
570
}
571
 
572
 
573
int
574
main ()
575
{
576
 
577
        /* Initialise handler vector */
578
        int_init();
579
 
580
        /* Install ethernet interrupt handler, it is enabled here too */
581
        int_add(ETH0_IRQ, oeth_interrupt, 0);
582
 
583
        /* Enable interrupts in supervisor register */
584
        cpu_enable_user_interrupts();
585
 
586
        /* Enable CPU timer */
587
        cpu_enable_timer();
588
 
589
        ethmac_setup(); /* Configure MAC, TX/RX BDs and enable RX and TX */
590
 
591
        /* clear tx_done, the tx interrupt handler will set it when it's been
592
           transmitted */
593
        tx_done = 0;
594
        rx_done = 0;
595
 
596
        ethphy_set_100mbit(0);
597
 
598
        send_ethmac_rxtx_test_init_packet(0x1); // 0x1 - overflow test
599
 
600
        while(1); // Sit and wait for test to finish in interrupt-triggered code
601
 
602
}

powered by: WebSVN 2.1.0

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