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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [eth/] [powerpc/] [quicc2/] [current/] [src/] [if_fec.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      dev/if_fec.c
4
//
5
//      Fast ethernet device driver for PowerPC MPC8260 boards
6
//
7
//==========================================================================
8
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
9
// -------------------------------------------                              
10
// This file is part of eCos, the Embedded Configurable Operating System.   
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
21
// for more details.                                                        
22
//
23
// You should have received a copy of the GNU General Public License        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    mtek
43
// Contributors: pfine
44
// Date:         2002-02-20
45
// Purpose:      
46
// Description:  hardware driver for MPC8260 FEC
47
//              
48
//
49
//####DESCRIPTIONEND####
50
//
51
//==========================================================================
52
 
53
#include <pkgconf/devs_eth_powerpc_quicc2.h>
54
#include <cyg/infra/cyg_type.h>
55
#include <cyg/infra/diag.h>
56
 
57
#include <cyg/hal/hal_arch.h>
58
#include <cyg/hal/hal_cache.h>
59
#include <cyg/hal/hal_intr.h>
60
#include <cyg/hal/var_intr.h>
61
#include <cyg/hal/drv_api.h>
62
#include <cyg/hal/hal_if.h>
63
#include <cyg/hal/mpc8260.h>
64
 
65
#include <cyg/io/eth/netdev.h>
66
#include <cyg/io/eth/eth_drv.h>
67
 
68
#ifdef CYGPKG_NET
69
#include <pkgconf/net.h>
70
#endif
71
 
72
#include "fec.h"
73
#include "EnetPHY.h"
74
 
75
#define ALIGN_TO_CACHE_LINES(x)  ( (long)((x) + 31) & 0xffffffe0 )
76
 
77
static unsigned char fec_eth_rxbufs[CYGNUM_DEVS_ETH_POWERPC_QUICC2_RxNUM *
78
                                    (CYGNUM_DEVS_ETH_POWERPC_QUICC2_BUFSIZE + 32)];
79
static unsigned char fec_eth_txbufs[CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM *
80
                                    (CYGNUM_DEVS_ETH_POWERPC_QUICC2_BUFSIZE + 32)];
81
 
82
// Buffer descriptors are in dual ported RAM, which is marked non-cached
83
#define FEC_BDs_NONCACHED
84
static struct fec_bd *const fec_eth_rxring = (struct fec_bd *)
85
  (QUICC2_VADS_IMM_BASE + FEC_PRAM_RxBD_Base);
86
static struct fec_bd *const fec_eth_txring = (struct fec_bd *)
87
  (QUICC2_VADS_IMM_BASE + FEC_PRAM_TxBD_Base);
88
 
89
static struct fec_eth_info fec_eth0_info;
90
 
91
static unsigned short _default_enaddr[] = {0x1234, 0x5678, 0x90a1};
92
static unsigned char enaddr[6];
93
 
94
#ifdef CYGPKG_REDBOOT
95
#include <pkgconf/redboot.h>
96
#ifdef CYGSEM_REDBOOT_FLASH_CONFIG
97
#include <redboot.h>
98
#include <flash_config.h>
99
RedBoot_config_option("Network hardware address [MAC]",
100
                      fec_esa,
101
                      ALWAYS_ENABLED, true,
102
                      CONFIG_ESA, 0
103
    );
104
RedBoot_config_option("Attempt to find 100 Mbps Ethernet",
105
                      fec_100,
106
                      ALWAYS_ENABLED, true,
107
                      CONFIG_BOOL, 0
108
    );
109
#endif
110
#endif
111
 
112
#define os_printf diag_printf
113
 
114
// CONFIG_ESA and CONFIG_BOOL are defined in redboot/include/flash_config.h
115
#ifndef CONFIG_ESA
116
#define CONFIG_ESA 6      // ethernet address length ...
117
#endif
118
 
119
#ifndef CONFIG_BOOL
120
#define CONFIG_BOOL 1
121
#endif
122
 
123
ETH_DRV_SC(fec_eth0_sc,
124
           &fec_eth0_info,     // Driver specific data
125
           "eth0",             // Name for this interface
126
           fec_eth_start,
127
           fec_eth_stop,
128
           fec_eth_control,
129
           fec_eth_can_send,
130
           fec_eth_send,
131
           fec_eth_recv,
132
           fec_eth_deliver,
133
           fec_eth_int,
134
           fec_eth_int_vector);
135
 
136
NETDEVTAB_ENTRY(fec_netdev,
137
                "fec_eth",
138
                fec_eth_init,
139
                &fec_eth0_sc);
140
 
141
#ifdef CYGPKG_NET
142
static cyg_interrupt fec_eth_interrupt;
143
static cyg_handle_t  fec_eth_interrupt_handle;
144
#endif
145
static void          fec_eth_int(struct eth_drv_sc *data);
146
 
147
#define FEC_ETH_INT CYGNUM_HAL_INTERRUPT_FCC2
148
 
149
// This ISR is called when the ethernet interrupt occurs
150
#ifdef CYGPKG_NET
151
static int
152
fec_eth_isr(cyg_vector_t vector, cyg_addrword_t data, HAL_SavedRegisters *regs)
153
{
154
  cyg_drv_interrupt_mask(FEC_ETH_INT);
155
  return (CYG_ISR_HANDLED|CYG_ISR_CALL_DSR);  // Run the DSR
156
}
157
#endif
158
 
159
// Deliver function (ex-DSR) handles the ethernet [logical] processing
160
static void
161
fec_eth_deliver(struct eth_drv_sc * sc)
162
{
163
  fec_eth_int(sc);
164
#ifdef CYGPKG_NET
165
  //  Clearing the event register acknowledges FCC2 interrupt ...
166
  //  cyg_drv_interrupt_acknowledge(FEC_ETH_INT);
167
  cyg_drv_interrupt_unmask(FEC_ETH_INT);
168
#endif
169
 
170
}
171
 
172
 
173
// Initialize the interface - performed at system startup
174
// This function must set up the interface, including arranging to
175
// handle interrupts, etc, so that it may be "started" cheaply later.
176
static bool
177
fec_eth_init(struct cyg_netdevtab_entry *tab)
178
{
179
    struct eth_drv_sc *sc = (struct eth_drv_sc *)tab->device_instance;
180
    struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
181
 
182
    volatile t_PQ2IMM    *IMM = (volatile t_PQ2IMM *) QUICC2_VADS_IMM_BASE;
183
    volatile t_Fcc_Pram  *fcc =  (volatile t_Fcc_Pram *) (QUICC2_VADS_IMM_BASE + FEC_PRAM_OFFSET);
184
    volatile t_EnetFcc_Pram *E_fcc = &(fcc->SpecificProtocol.e);
185
#if defined(CYGPKG_HAL_POWERPC_VADS) || defined(CYGPKG_HAL_POWERPC_TS6)
186
    volatile t_BCSR *CSR   = (t_BCSR *) 0x04500000;
187
#endif
188
 
189
    int i;
190
    bool esa_ok;
191
    bool fec_100;
192
    unsigned char *c_ptr;
193
    UINT16 link_speed;
194
 
195
    // Link the memory to the driver control memory
196
    qi->fcc_reg = & (IMM->fcc_regs[FCC2]);
197
 
198
    // just in case :  disable Transmit and Receive 
199
    qi->fcc_reg->fcc_gfmr &= ~(FEC_GFMR_EN_Rx | FEC_GFMR_EN_Tx);
200
 
201
    // Via BCSR, (re)start LXT970
202
#if defined(CYGPKG_HAL_POWERPC_VADS) || defined(CYGPKG_HAL_POWERPC_TS6)
203
    EnableResetPHY(CSR);
204
#endif
205
 
206
    // Try to read the ethernet address of the transciever ...
207
#ifdef CYGPKG_REDBOOT
208
    esa_ok = flash_get_config("fec_100", &fec_100, CONFIG_BOOL);
209
#else
210
    esa_ok = CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
211
                                         "fec_100", &fec_100, CONFIG_BOOL);
212
#endif
213
 
214
    link_speed = NOTLINKED;
215
    if(esa_ok && fec_100){
216
        // Via MII Management pins, tell LXT970 to initialize 
217
        os_printf("Attempting to acquire 100 Mbps half_duplex link ...");
218
        InitEthernetPHY((VUINT32 *) &(IMM->io_regs[PORT_C].pdir),
219
                        (VUINT32 *) &(IMM->io_regs[PORT_C].pdat),
220
                        HUNDRED_HD);
221
 
222
        link_speed = LinkTestPHY();
223
        os_printf("\n");
224
        if(link_speed == NOTLINKED){
225
            os_printf("Failed to get 100 Mbps half_duplex link.\n");
226
        }
227
    }
228
    if(link_speed == NOTLINKED){
229
        os_printf("Attempting to acquire 10 Mbps half_duplex link ...");
230
        InitEthernetPHY((VUINT32 *) &(IMM->io_regs[PORT_C].pdir),
231
                        (VUINT32 *) &(IMM->io_regs[PORT_C].pdat),
232
                        TEN_HD);
233
        link_speed = LinkTestPHY();
234
        os_printf("\n");
235
        if(link_speed == NOTLINKED){
236
            link_speed = LinkTestPHY();
237
            os_printf("Failed to get 10 Mbps half_duplex link.\n");
238
        }
239
 
240
    }
241
    switch ( link_speed ) {
242
 
243
    case HUNDRED_FD:
244
      os_printf("100 MB full-duplex ethernet link \n");
245
      break;
246
    case HUNDRED_HD:
247
      os_printf("100 MB half-duplex ethernet link \n");
248
      break;
249
    case TEN_FD:
250
      os_printf("10 MB full-duplex ethernet link \n");
251
      break;
252
    case TEN_HD:
253
      os_printf("10 MB half-duplex ethernet link \n");
254
      break;
255
    default:
256
      os_printf("NO ethernet link \n");
257
    }
258
 
259
    // Connect PORTC pins: (C19) to clk13, (C18) to clk 14
260
    IMM->io_regs[PORT_C].ppar |= 0x00003000;
261
    IMM->io_regs[PORT_C].podr &= ~(0x00003000);
262
    IMM->io_regs[PORT_C].psor &= ~(0x00003000);
263
    IMM->io_regs[PORT_C].pdir &= ~(0x00003000);
264
 
265
    // Connect clk13 to RxClk and clk14 to TxClk on FCC2
266
    IMM->cpm_mux_cmxfcr &= 0x7f007f00; // clear fcc2 clocks
267
    IMM->cpm_mux_cmxfcr |= 0x00250000; // set fcc2 clocks  (see 15-14)
268
    IMM->cpm_mux_cmxuar  = 0x0000;     // Utopia address reg, just clear
269
 
270
    // Initialize parallel port registers to connect FCC2 to MII
271
    IMM->io_regs[PORT_B].podr &= 0xffffc000; // clear bits 18-31 
272
    IMM->io_regs[PORT_B].psor &= 0xffffc000;
273
    IMM->io_regs[PORT_B].pdir &= 0xffffc000;
274
 
275
    IMM->io_regs[PORT_B].psor |= 0x00000004;
276
    IMM->io_regs[PORT_B].pdir |= 0x000003c5;
277
    IMM->io_regs[PORT_B].ppar |= 0x00003fff;
278
 
279
    // Initialize Receive Buffer Descriptors
280
    qi->rbase = fec_eth_rxring;
281
    qi->rxbd  = fec_eth_rxring;
282
    qi->rnext = fec_eth_rxring;
283
    c_ptr = fec_eth_rxbufs;
284
 
285
    for(i=0; i<CYGNUM_DEVS_ETH_POWERPC_QUICC2_RxNUM; i++) {
286
 
287
      fec_eth_rxring[i].ctrl   = (FEC_BD_Rx_Empty | FEC_BD_Rx_Int);
288
      fec_eth_rxring[i].length = 0;                   // reset 
289
      c_ptr = (unsigned char *) ALIGN_TO_CACHE_LINES(c_ptr);
290
      fec_eth_rxring[i].buffer = (volatile unsigned char *)c_ptr;
291
      c_ptr += CYGNUM_DEVS_ETH_POWERPC_QUICC2_BUFSIZE;
292
    }
293
 
294
    fec_eth_rxring[CYGNUM_DEVS_ETH_POWERPC_QUICC2_RxNUM-1].ctrl |= FEC_BD_Rx_Wrap;
295
 
296
    // Initialize Transmit Buffer Descriptors
297
    qi->tbase = fec_eth_txring;
298
    qi->txbd  = fec_eth_txring;
299
    qi->tnext = fec_eth_txring;
300
    c_ptr = fec_eth_txbufs;
301
 
302
    for(i=0; i<CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM; i++) {
303
 
304
      fec_eth_txring[i].ctrl   = (FEC_BD_Tx_Pad | FEC_BD_Tx_Int);
305
      fec_eth_txring[i].length = 0;   // reset : Write before send
306
      c_ptr = (unsigned char *) ALIGN_TO_CACHE_LINES(c_ptr);
307
      fec_eth_txring[i].buffer = (volatile unsigned char  *)c_ptr;
308
      c_ptr += CYGNUM_DEVS_ETH_POWERPC_QUICC2_BUFSIZE;
309
    }
310
 
311
    fec_eth_txring[CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM-1].ctrl |= FEC_BD_Tx_Wrap;
312
 
313
    // Common FCC Parameter RAM initialization
314
    fcc->riptr = FEC_PRAM_RIPTR;   // in dual port RAM (see 28-11)
315
    fcc->tiptr = FEC_PRAM_TIPTR;   // in dual port RAM (see 28-11)
316
    fcc->mrblr = FEC_PRAM_MRBLR;   // ?? FROM 8101 code ...
317
    fcc->rstate &= FEC_FCR_INIT;
318
    fcc->rstate |= FEC_FCR_MOT_BO;
319
    fcc->rbase = (long) fec_eth_rxring;
320
    fcc->tstate &= FEC_FCR_INIT;
321
    fcc->tstate |= FEC_FCR_MOT_BO;
322
    fcc->tbase = (long) fec_eth_txring;
323
 
324
    // Ethernet Specific FCC Parameter RAM Initialization     
325
    E_fcc->c_mask   = FEC_PRAM_C_MASK; // (see 30-9)
326
    E_fcc->c_pres   = FEC_PRAM_C_PRES;
327
    E_fcc->crcec    = 0;
328
    E_fcc->alec     = 0;
329
    E_fcc->disfc    = 0;
330
    E_fcc->ret_lim  = FEC_PRAM_RETLIM;
331
    E_fcc->p_per    = FEC_PRAM_PER_LO;
332
    E_fcc->gaddr_h  = 0;
333
    E_fcc->gaddr_l  = 0;
334
    E_fcc->tfcstat  = 0;
335
    E_fcc->mflr     = FEC_MAX_FLR;
336
 
337
    // Try to read the ethernet address of the transciever ...
338
#ifdef CYGPKG_REDBOOT
339
    esa_ok = flash_get_config("fec_esa", enaddr, CONFIG_ESA);
340
#else
341
    esa_ok = CYGACC_CALL_IF_FLASH_CFG_OP(CYGNUM_CALL_IF_FLASH_CFG_GET,
342
                                         "fec_esa", enaddr, CONFIG_ESA);
343
#endif
344
    if (!esa_ok) {
345
      // If can't use the default ...
346
      os_printf("FEC_ETH - Warning! ESA unknown\n");
347
      memcpy(enaddr, _default_enaddr, sizeof(enaddr));
348
    }
349
 
350
    E_fcc->paddr1_h = ((short)enaddr[5] << 8) | enaddr[4]; // enaddr[2]; 
351
    E_fcc->paddr1_m = ((short)enaddr[3] << 8) | enaddr[2]; // enaddr[1];
352
    E_fcc->paddr1_l = ((short)enaddr[1] << 8) | enaddr[0]; // enaddr[0];
353
 
354
    E_fcc->iaddr_h  = 0;
355
    E_fcc->iaddr_l  = 0;
356
    E_fcc->minflr   = FEC_MIN_FLR;
357
    E_fcc->taddr_h  = 0;
358
    E_fcc->taddr_m  = 0;
359
    E_fcc->taddr_l  = 0;
360
    E_fcc->pad_ptr  = FEC_PRAM_TIPTR; // No special padding char ...
361
    E_fcc->cf_type  = 0;
362
    E_fcc->maxd1    = FEC_PRAM_MAXD;
363
    E_fcc->maxd2    = FEC_PRAM_MAXD;
364
 
365
    // FCC register initialization 
366
    IMM->fcc_regs[FCC2].fcc_gfmr = FEC_GFMR_INIT;
367
    IMM->fcc_regs[FCC2].fcc_psmr = FEC_PSMR_INIT;
368
    IMM->fcc_regs[FCC2].fcc_dsr  = FEC_DSR_INIT;
369
 
370
#ifdef CYGPKG_NET
371
    // clear the events of FCC2
372
    IMM->fcc_regs[FCC2].fcc_fcce = 0xFFFF0000;
373
    IMM->fcc_regs[FCC2].fcc_fccm = FEC_EV_TXE | FEC_EV_TXB | FEC_EV_RXF;
374
 
375
    // Set up to handle interrupts
376
    cyg_drv_interrupt_create(FEC_ETH_INT,
377
                             0,  // Highest //CYGARC_SIU_PRIORITY_HIGH,
378
                             (cyg_addrword_t)sc, //  Data passed to ISR
379
                             (cyg_ISR_t *)fec_eth_isr,
380
                             (cyg_DSR_t *)eth_drv_dsr,
381
                             &fec_eth_interrupt_handle,
382
                             &fec_eth_interrupt);
383
    cyg_drv_interrupt_attach(fec_eth_interrupt_handle);
384
    cyg_drv_interrupt_acknowledge(FEC_ETH_INT);
385
    cyg_drv_interrupt_unmask(FEC_ETH_INT);
386
#else
387
 
388
    // Mask the interrupts 
389
    IMM->fcc_regs[FCC2].fcc_fccm = 0;
390
#endif
391
 
392
    // Issue Init RX & TX Parameters Command for FCC2
393
    while ((IMM->cpm_cpcr & CPCR_FLG) != CPCR_READY_TO_RX_CMD);
394
 
395
    IMM->cpm_cpcr = CPCR_INIT_TX_RX_PARAMS |
396
      CPCR_FCC2_CH |
397
      CPCR_MCN_FEC |
398
      CPCR_FLG;              /* ISSUE COMMAND */
399
 
400
    while ((IMM->cpm_cpcr & CPCR_FLG) != CPCR_READY_TO_RX_CMD);
401
 
402
    // Initialize upper level driver for ecos
403
    (sc->funs->eth_drv->init)(sc, (unsigned char *)&enaddr);
404
 
405
    return true;
406
}
407
 
408
//
409
// This function is called to "start up" the interface.  It may be called
410
// multiple times, even when the hardware is already running.  It will be
411
// called whenever something "hardware oriented" changes and should leave
412
// the hardware ready to send/receive packets.
413
//
414
static void
415
fec_eth_start(struct eth_drv_sc *sc, unsigned char *enaddr, int flags)
416
{
417
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
418
 
419
  // Enable the device : 
420
  // Set the ENT/ENR bits in the GFMR -- Enable Transmit/Receive
421
  qi->fcc_reg->fcc_gfmr |= (FEC_GFMR_EN_Rx | FEC_GFMR_EN_Tx);
422
 
423
}
424
 
425
//
426
// This function is called to shut down the interface.
427
//
428
static void
429
fec_eth_stop(struct eth_drv_sc *sc)
430
{
431
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
432
 
433
  // Disable the device : 
434
  // Clear the ENT/ENR bits in the GFMR -- Disable Transmit/Receive
435
  qi->fcc_reg->fcc_gfmr &= ~(FEC_GFMR_EN_Rx | FEC_GFMR_EN_Tx);
436
}
437
 
438
 
439
//
440
// This function is called for low level "control" operations
441
//
442
static int
443
fec_eth_control(struct eth_drv_sc *sc, unsigned long key,
444
                void *data, int length)
445
{
446
  switch (key) {
447
  case ETH_DRV_SET_MAC_ADDRESS:
448
    return 0;
449
    break;
450
  default:
451
    return 1;
452
    break;
453
  }
454
}
455
 
456
 
457
//
458
// This function is called to see if another packet can be sent.
459
// It should return the number of packets which can be handled.
460
// Zero should be returned if the interface is busy and can not send any more.
461
//
462
static int
463
fec_eth_can_send(struct eth_drv_sc *sc)
464
{
465
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
466
  volatile struct fec_bd *txbd = qi->txbd;
467
  int cache_state;
468
 
469
  HAL_DCACHE_IS_ENABLED(cache_state);
470
#ifndef FEC_BDs_NONCACHED
471
  if (cache_state) {
472
    HAL_DCACHE_INVALIDATE(fec_eth_txring,
473
                          8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM);
474
  }
475
#endif
476
 
477
  return ((txbd->ctrl & (FCC_BD_Tx_TC | FCC_BD_Tx_Ready)) == 0);
478
}
479
 
480
//
481
// This routine is called to send data to the hardware.
482
static void
483
fec_eth_send(struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len,
484
             int total_len, unsigned long key)
485
{
486
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
487
  struct fec_bd *txbd, *txfirst;
488
  volatile char *bp;
489
  int i, txindex, cache_state;
490
 
491
  HAL_DCACHE_IS_ENABLED(cache_state);
492
#ifndef FEC_BDs_NONCACHED
493
  if (cache_state) {
494
    HAL_DCACHE_INVALIDATE(fec_eth_txring,
495
                          8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM);
496
  }
497
#endif
498
 
499
  // Find a free buffer
500
  txbd = txfirst = qi->txbd;
501
  while (txbd->ctrl & FEC_BD_Tx_Ready) {
502
    // This buffer is busy, move to next one
503
    if (txbd->ctrl & FEC_BD_Tx_Wrap) {
504
      txbd = qi->tbase;
505
    } else {
506
      txbd++;
507
    }
508
    if (txbd == txfirst) {
509
#ifdef CYGPKG_NET
510
      panic ("No free xmit buffers");
511
#else
512
      os_printf("FEC Ethernet: No free xmit buffers\n");
513
#endif
514
    }
515
  }
516
 
517
  // Remember the next buffer to try
518
  if (txbd->ctrl & FEC_BD_Tx_Wrap) {
519
    qi->txbd = qi->tbase;
520
  } else {
521
    qi->txbd = txbd+1;
522
  }
523
 
524
  txindex = ((unsigned long)txbd - (unsigned long)qi->tbase) / sizeof(*txbd);
525
  qi->txkey[txindex] = key;
526
 
527
  // Set up buffer
528
  txbd->length = total_len;
529
  bp = txbd->buffer;
530
  for (i = 0;  i < sg_len;  i++) {
531
    memcpy((void *)bp, (void *)sg_list[i].buf, sg_list[i].len);
532
    bp += sg_list[i].len;
533
  }
534
 
535
  // Make sure no stale data buffer ...
536
  if (cache_state) {
537
    HAL_DCACHE_FLUSH(txbd->buffer, txbd->length);
538
  }
539
  // Send it on it's way
540
  txbd->ctrl |= FEC_BD_Tx_Ready | FEC_BD_Tx_Last | FEC_BD_Tx_TC;
541
#ifndef FEC_BDs_NONCACHED
542
  if (cache_state) {
543
    HAL_DCACHE_FLUSH(fec_eth_txring,
544
                     8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM);
545
  }
546
#endif  
547
 
548
}
549
 
550
//
551
// This function is called when a packet has been received.  It's job is
552
// to prepare to unload the packet from the hardware.  Once the length of
553
// the packet is known, the upper layer of the driver can be told.  When
554
// the upper layer is ready to unload the packet, the internal function
555
// 'fec_eth_recv' will be called to actually fetch it from the hardware.
556
//
557
static void
558
fec_eth_RxEvent(struct eth_drv_sc *sc)
559
{
560
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
561
  struct fec_bd *rxbd;
562
  int cache_state;
563
 
564
  HAL_DCACHE_IS_ENABLED(cache_state);
565
#ifndef FEC_BDs_NONCACHED
566
  if (cache_state) {
567
    HAL_DCACHE_INVALIDATE(fec_eth_rxring,
568
                          8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_RxNUM);
569
  }
570
#endif
571
 
572
  rxbd = qi->rnext;
573
  while ((rxbd->ctrl & FEC_BD_Rx_Empty) == 0) {
574
    qi->rxbd = rxbd;  // Save for callback
575
 
576
    // This is the right way of doing it, but dcbi has a bug ...
577
    //    if (cache_state) {
578
    //      HAL_DCACHE_INVALIDATE(rxbd->buffer, rxbd->length); 
579
    //    }
580
    (sc->funs->eth_drv->recv)(sc, rxbd->length);
581
    if (cache_state) {
582
      HAL_DCACHE_FLUSH(rxbd->buffer, rxbd->length);
583
    }
584
 
585
    rxbd->ctrl |= FEC_BD_Rx_Empty;
586
    if (rxbd->ctrl & FEC_BD_Rx_Wrap) {
587
      rxbd = qi->rbase;
588
    } else {
589
      rxbd++;
590
    }
591
  }
592
  // Remember where we left off
593
  qi->rnext = (struct fec_bd *)rxbd;
594
 
595
  // Make sure no stale data
596
#ifndef FEC_BDs_NONCACHED
597
  if (cache_state) {
598
    HAL_DCACHE_FLUSH(fec_eth_rxring,
599
                     8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_RxNUM);
600
  }
601
#endif
602
 
603
}
604
 
605
//
606
// This function is called as a result of the "eth_drv_recv()" call above.
607
// It's job is to actually fetch data for a packet from the hardware once
608
// memory buffers have been allocated for the packet.  Note that the buffers
609
// may come in pieces, using a scatter-gather list.  This allows for more
610
// efficient processing in the upper layers of the stack.
611
//
612
static void
613
fec_eth_recv(struct eth_drv_sc *sc, struct eth_drv_sg *sg_list, int sg_len)
614
{
615
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
616
  unsigned char *bp;
617
  int i;
618
 
619
  bp = (unsigned char *)qi->rxbd->buffer;
620
 
621
  for (i = 0;  i < sg_len;  i++) {
622
    if (sg_list[i].buf != 0) {
623
      memcpy((void *)sg_list[i].buf, bp, sg_list[i].len);
624
      bp += sg_list[i].len;
625
    }
626
  }
627
 
628
}
629
 
630
static void
631
fec_eth_TxEvent(struct eth_drv_sc *sc, int stat)
632
{
633
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
634
  struct fec_bd *txbd;
635
  int txindex, cache_state;
636
 
637
  // Make sure no stale data
638
  HAL_DCACHE_IS_ENABLED(cache_state);
639
#ifndef FEC_BDs_NONCACHED
640
  if (cache_state) {
641
    HAL_DCACHE_INVALIDATE(fec_eth_txring,
642
                          8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM);
643
  }
644
#endif
645
 
646
  txbd = qi->tnext;
647
  // Note: TC field is used to indicate the buffer has/had data in it
648
  while ( (txbd->ctrl & (FEC_BD_Tx_TC | FEC_BD_Tx_Ready)) == FEC_BD_Tx_TC ) {
649
    txindex = ((unsigned long)txbd - (unsigned long)qi->tbase) / sizeof(*txbd);
650
    (sc->funs->eth_drv->tx_done)(sc, qi->txkey[txindex], 0);
651
    txbd->ctrl &= ~FEC_BD_Tx_TC;
652
    if (txbd->ctrl & FEC_BD_Tx_Wrap) {
653
      txbd = qi->tbase;
654
    } else {
655
      txbd++;
656
    }
657
  }
658
  // Remember where we left off
659
  qi->tnext = (struct fec_bd *)txbd;
660
 
661
  // Make sure no stale data  
662
#ifndef FEC_BDs_NONCACHED
663
  if (cache_state) {
664
    HAL_DCACHE_FLUSH(fec_eth_txring,
665
                     8*CYGNUM_DEVS_ETH_POWERPC_QUICC2_TxNUM);
666
  }
667
#endif
668
 
669
}
670
 
671
//
672
// Interrupt processing
673
//
674
static void
675
fec_eth_int(struct eth_drv_sc *sc)
676
{
677
  struct fec_eth_info *qi = (struct fec_eth_info *)sc->driver_private;
678
  unsigned long iEvent;
679
 
680
  while ((iEvent = qi->fcc_reg->fcc_fcce) != 0){
681
 
682
    // Writing 1's clear fcce, Writing 0's have no effect
683
    qi->fcc_reg->fcc_fcce = iEvent;
684
 
685
    // Tx Done or Tx Error
686
    if ( iEvent & (FEC_EV_TXB | FEC_EV_TXE) ) {
687
      fec_eth_TxEvent(sc, iEvent);
688
    }
689
 
690
    // Complete or non-complete frame receive
691
    if (iEvent & (FEC_EV_RXF | FEC_EV_RXB) ) {
692
      fec_eth_RxEvent(sc);
693
    }
694
 
695
  }
696
 
697
 
698
}
699
 
700
//
701
// Interrupt vector
702
//
703
static int
704
fec_eth_int_vector(struct eth_drv_sc *sc)
705
{
706
    return (FEC_ETH_INT);
707
}
708
 

powered by: WebSVN 2.1.0

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