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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [or1ksim/] [testsuite/] [test-code-or1k/] [eth/] [eth.c] - Blame information for rev 522

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

Line No. Rev Author Line
1 90 jeremybenn
/* eth.c. Test of Or1ksim Ethernet
2
 
3 457 julius
   Copyright (C) 1999-2006, 2010 OpenCores
4 90 jeremybenn
   Copyright (C) 2010 Embecosm Limited
5
 
6
   Contributors various OpenCores participants
7
   Contributor Jeremy Bennett <jeremy.bennett@embecosm.com>
8 457 julius
   Contributor Julius Baxter <julius@opencores.org>
9 90 jeremybenn
 
10
   This file is part of OpenRISC 1000 Architectural Simulator.
11
 
12
   This program is free software; you can redistribute it and/or modify it
13
   under the terms of the GNU General Public License as published by the Free
14
   Software Foundation; either version 3 of the License, or (at your option)
15
   any later version.
16
 
17
   This program is distributed in the hope that it will be useful, but WITHOUT
18
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
19
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
20
   more details.
21
 
22
   You should have received a copy of the GNU General Public License along
23
   with this program.  If not, see <http:  www.gnu.org/licenses/>.  */
24
 
25
/* ----------------------------------------------------------------------------
26
   This code is commented throughout for use with Doxygen.
27
   --------------------------------------------------------------------------*/
28
 
29 457 julius
/* TODO: Add loopback test.
30
*/
31
 
32 90 jeremybenn
#include "spr-defs.h"
33
#include "support.h"
34
#include "board.h"
35
 
36
typedef long off_t;
37
 
38
#include "fields.h"
39
#include "eth.h"
40
 
41
typedef volatile unsigned long *REGISTER;
42
 
43
REGISTER
44
    eth_moder = (unsigned long *)(ETH_BASE + ETH_MODER),
45
        eth_int_source = (unsigned long *)(ETH_BASE + ETH_INT_SOURCE),
46
        eth_int_mask = (unsigned long *)(ETH_BASE + ETH_INT_MASK),
47
        eth_ipgt = (unsigned long *)(ETH_BASE + ETH_IPGT),
48
        eth_ipgr1 = (unsigned long *)(ETH_BASE + ETH_IPGR1),
49
        eth_ipgr2 = (unsigned long *)(ETH_BASE + ETH_IPGR2),
50
        eth_packetlen = (unsigned long *)(ETH_BASE + ETH_PACKETLEN),
51
        eth_collconf = (unsigned long *)(ETH_BASE + ETH_COLLCONF),
52
        eth_tx_bd_num = (unsigned long *)(ETH_BASE + ETH_TX_BD_NUM),
53
        eth_controlmoder = (unsigned long *)(ETH_BASE + ETH_CTRLMODER),
54
        eth_miimoder = (unsigned long *)(ETH_BASE + ETH_MIIMODER),
55
        eth_miicommand = (unsigned long *)(ETH_BASE + ETH_MIICOMMAND),
56
        eth_miiaddress = (unsigned long *)(ETH_BASE + ETH_MIIADDRESS),
57
        eth_miitx_data = (unsigned long *)(ETH_BASE + ETH_MIITX_DATA),
58
        eth_miirx_data = (unsigned long *)(ETH_BASE + ETH_MIIRX_DATA),
59
        eth_miistatus = (unsigned long *)(ETH_BASE + ETH_MIISTATUS),
60
        eth_mac_addr0 = (unsigned long *)(ETH_BASE + ETH_MAC_ADDR0),
61
        eth_mac_addr1 = (unsigned long *)(ETH_BASE + ETH_MAC_ADDR1),
62
        eth_bd_base = (unsigned long *)(ETH_BASE + ETH_BD_BASE);
63
 
64
volatile unsigned int_happend;
65 457 julius
#define R_PACKET_SIZE 2000
66
unsigned char r_packet[R_PACKET_SIZE];
67
#define S_PACKET_SIZE 1003
68
unsigned char s_packet[S_PACKET_SIZE];
69 90 jeremybenn
unsigned tx_bindex;
70
unsigned rx_bindex;
71
 
72
void interrupt_handler()
73
{
74
    unsigned i,len;
75
 
76
    printf ("Int\n");
77
    switch (*eth_int_source & 0x7f) {
78
    case 0x2 :
79
        printf ("Transmit Error.\n");
80
        *eth_int_source = 0x2;
81
         break;
82
    case 0x8 :
83
        printf ("Receive Error\n");
84
        *eth_int_source = 0x8;
85
        break;
86
    case 0x4 :
87
        printf ("Receive Frame\n");
88
        *eth_int_source = 0x4;
89
 
90
        CLEAR_FLAG(*eth_moder, ETH_MODER, RXEN);
91
 
92
        len = GET_FIELD(eth_bd_base[(*eth_tx_bd_num << 1) + 2], ETH_RX_BD, LENGTH);
93
        for (i=0; i<len; i++)
94
          if (r_packet[i] != (unsigned char)i)
95
          {
96
              printf("Failed at byte %d. expect %d, received %d\n", i, i, r_packet[i]);
97
              exit(1);
98
          }
99
        break;
100
    case 0x10:
101
        printf ("Busy\n");
102
        *eth_int_source = 0x10;
103
        break;
104
    case 0x1 :
105
        printf ("Transmit Frame.\n");
106
        *eth_int_source = 0x1;
107
        CLEAR_FLAG(*eth_moder, ETH_MODER, TXEN);
108
 
109
        break;
110
    default:
111
        printf ("Invalid int @ %0x\n", (unsigned int)*eth_int_source & 0x7f);
112
        *eth_int_source = 0x7f;
113
        exit (1);
114
    }
115
 
116
    mtspr(SPR_PICSR, 0);
117
    int_happend = 1;
118
}
119
 
120
static void set_mac( void )
121
{
122
        *eth_mac_addr0 = 0x04030201LU;
123
        *eth_mac_addr1 = 0x00000605LU;
124
}
125
 
126
static void transmit_one_packet( void )
127
{
128
        unsigned i;
129 457 julius
 
130 90 jeremybenn
        /* Initialize packet */
131 457 julius
        for ( i = 0; i < S_PACKET_SIZE; ++ i )
132 90 jeremybenn
                s_packet[i] = (unsigned char)i;
133
 
134
        /* Set Ethernet BD */
135 457 julius
        SET_FIELD(eth_bd_base[tx_bindex], ETH_TX_BD, LENGTH, S_PACKET_SIZE);
136 90 jeremybenn
        eth_bd_base[tx_bindex + 1] = (unsigned long)s_packet;
137
 
138
        /* Start Ethernet */
139
        SET_FLAG(eth_bd_base[tx_bindex], ETH_TX_BD, READY);
140
        SET_FLAG(*eth_moder, ETH_MODER, TXEN);
141
 
142
        /* Now wait till sent */
143
        while ( TEST_FLAG( eth_bd_base[tx_bindex], ETH_TX_BD, READY ) );
144
        CLEAR_FLAG(*eth_moder, ETH_MODER, TXEN);
145
        *eth_int_source = 0x7f;
146
}
147
 
148
static void transmit_one_packet_int( void )
149
{
150
        unsigned i;
151
 
152
        int_happend = 0;
153
        /* Initialize packet */
154
        printf("Init\n");
155 457 julius
        for ( i = 0; i < S_PACKET_SIZE; ++ i )
156 90 jeremybenn
                s_packet[i] = (unsigned char)i;
157
 
158
        /* Set Ethernet BD */
159
        printf("Set BD\n");
160 457 julius
        SET_FIELD(eth_bd_base[tx_bindex], ETH_TX_BD, LENGTH, S_PACKET_SIZE);
161 90 jeremybenn
        eth_bd_base[tx_bindex + 1] = (unsigned long)s_packet;
162
 
163
        /* Start Ethernet */
164
        printf("Set Flags\n");
165
        SET_FLAG(eth_bd_base[tx_bindex], ETH_TX_BD, IRQ);
166
        SET_FLAG(eth_bd_base[tx_bindex], ETH_TX_BD, READY);
167
        SET_FLAG(*eth_moder, ETH_MODER, TXEN);
168
}
169
 
170
 
171
static void receive_one_packet(void)
172
{
173
  unsigned int i;
174
  unsigned int len;
175
 
176
  eth_bd_base[rx_bindex + 1] = (unsigned long)r_packet;
177
 
178
  SET_FLAG(eth_bd_base[rx_bindex], ETH_RX_BD, READY);
179
  SET_FLAG(*eth_moder, ETH_MODER, RXEN);
180
 
181
  while ( TEST_FLAG( eth_bd_base[rx_bindex], ETH_RX_BD, READY ) );
182
  CLEAR_FLAG(*eth_moder, ETH_MODER, RXEN);
183
  *eth_int_source = 0x7f;
184 457 julius
 
185 90 jeremybenn
  len = GET_FIELD(eth_bd_base[rx_bindex], ETH_RX_BD, LENGTH);
186
  for (i=0; i<len; i++)
187
      if (r_packet[i] != (unsigned char)i)
188
      {
189
          printf("Failed at byte %d. expect %d, received %d\n", i, i, r_packet[i]);
190
          exit(1);
191
      }
192
}
193
 
194
static void receive_one_packet_int(void)
195
{
196
  int_happend = 0;
197
  printf("Set BD\n");
198
  eth_bd_base[rx_bindex + 1] = (unsigned long)r_packet;
199
 
200
  printf("SetFlags\n");
201
  SET_FLAG(eth_bd_base[rx_bindex], ETH_RX_BD, IRQ);
202
  SET_FLAG(eth_bd_base[rx_bindex], ETH_RX_BD, READY);
203
  SET_FLAG(*eth_moder, ETH_MODER, RXEN);
204
}
205
 
206
int main()
207
{
208
        printf( "Starting Ethernet test\n" );
209
 
210 457 julius
        /* Buffer descriptor indexes. These are not changed in between the
211
        polling tests, as the RXEN and TXEN bits in the MODER are disabled
212
        between tests, resetting the respective buffer descriptor indexes,
213
        and so these should stay at their initial values. */
214 90 jeremybenn
        tx_bindex = 0;
215
        rx_bindex = *eth_tx_bd_num << 1;
216
 
217
        set_mac();
218 457 julius
 
219
        /* Set promiscuous mode */
220
        *eth_moder |= (1 << ETH_MODER_PRO_OFFSET);
221
 
222 90 jeremybenn
        /*-------------------*/
223
        /* non iterrupt test */
224
        transmit_one_packet();
225 457 julius
 
226 90 jeremybenn
        receive_one_packet();
227 457 julius
 
228 90 jeremybenn
        transmit_one_packet();
229 457 julius
 
230 90 jeremybenn
        receive_one_packet();
231
 
232
 
233
        /*-------------------*/
234
        /* interrupt test    */
235
        excpt_int = (unsigned long)interrupt_handler;
236
        /* Enable interrup      ts */
237
        printf("enable ints\n");
238
        mtspr (SPR_SR, mfspr(SPR_SR) | SPR_SR_IEE);
239
        mtspr (SPR_PICMR, mfspr(SPR_PICMR) | (0x00000001L << ETH_IRQ));
240
 
241
        printf("set mask flags TX\n");
242
        SET_FLAG(*eth_int_mask, ETH_INT_MASK, TXB_M);
243
        transmit_one_packet_int();
244
        tx_bindex += 2;
245
        /* printf("waiting for int\n"); */
246
        while (!int_happend);
247
        CLEAR_FLAG(*eth_int_mask, ETH_INT_MASK, TXB_M);
248
 
249
        printf("seting mask flag RX\n");
250
        SET_FLAG(*eth_int_mask, ETH_INT_MASK, RXB_M);
251
        receive_one_packet_int();
252
        rx_bindex += 2;
253
        /* printf("waiting for int\n"); */
254
        while (!int_happend);
255
        CLEAR_FLAG(*eth_int_mask, ETH_INT_MASK, RXB_M);
256
 
257
 
258
        printf( "Ending Ethernet test\n" );
259
 
260
        report (0xdeaddead);
261
        exit(0);
262
}
263
 
264
 

powered by: WebSVN 2.1.0

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