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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [devs/] [eth/] [arm/] [lpc2xxx/] [current/] [src/] [if_lpc2xxx.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
2
//
3
//      if_lpc2xxx.c
4
//
5
//==========================================================================
6
// ####ECOSGPLCOPYRIGHTBEGIN####                                            
7
// -------------------------------------------                              
8
// This file is part of eCos, the Embedded Configurable Operating System.   
9
// Copyright (C) 2008 Free Software Foundation, Inc.                        
10
//
11
// eCos is free software; you can redistribute it and/or modify it under    
12
// the terms of the GNU General Public License as published by the Free     
13
// Software Foundation; either version 2 or (at your option) any later      
14
// version.                                                                 
15
//
16
// eCos is distributed in the hope that it will be useful, but WITHOUT      
17
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or    
18
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License    
19
// for more details.                                                        
20
//
21
// You should have received a copy of the GNU General Public License        
22
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
23
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
24
//
25
// As a special exception, if other files instantiate templates or use      
26
// macros or inline functions from this file, or you compile this file      
27
// and link it with other works to produce a work based on this file,       
28
// this file does not by itself cause the resulting work to be covered by   
29
// the GNU General Public License. However the source code for this file    
30
// must still be made available in accordance with section (3) of the GNU   
31
// General Public License v2.                                               
32
//
33
// This exception does not invalidate any other reasons why a work based    
34
// on this file might be covered by the GNU General Public License.         
35
// -------------------------------------------                              
36
// ####ECOSGPLCOPYRIGHTEND####                                              
37
//==========================================================================
38
//#####DESCRIPTIONBEGIN####
39
//
40
// Author(s):    Uwe Kindler
41
// Contributors: ilijak
42
// Date:         2008-08-10
43
// Description:  Hardware driver for LPC2xxx on-chip EMAC peripheral
44
//
45
//               This driver was originally written for LPC2468 and may
46
//               require some modifications to work on other LPC2xxx
47
//               variants as well.
48
//
49
//####DESCRIPTIONEND####
50
//
51
//========================================================================*/
52
 
53
 
54
#include <pkgconf/system.h>
55
#include <pkgconf/hal.h>
56
#include <pkgconf/devs_eth_arm_lpc2xxx.h>
57
#include <pkgconf/io_eth_drivers.h>
58
#if defined(CYGPKG_REDBOOT)
59
   #include <pkgconf/redboot.h>
60
#endif
61
 
62
#include <cyg/hal/hal_io.h>
63
#include <cyg/hal/hal_intr.h>
64
#include <cyg/hal/hal_arch.h>
65
#include <cyg/hal/drv_api.h>
66
#include <cyg/hal/hal_diag.h>
67
#include <cyg/infra/cyg_type.h>
68
#include <cyg/infra/cyg_ass.h>
69
#include <cyg/infra/diag.h>
70
#include <cyg/io/eth/netdev.h>
71
#include <cyg/io/eth/eth_drv.h>
72
#include <cyg/io/eth/eth_drv_stats.h>
73
#include <cyg/io/eth_phy.h>
74
#include <errno.h>
75
#include <string.h>
76
#ifdef CYGPKG_NET
77
#include <pkgconf/net.h>
78
#include <net/if.h>  /* Needed for struct ifnet */
79
#endif
80
#include <cyg/io/eth/eth_drv_stats.h>
81
 
82
 
83
//===========================================================================
84
//                                DEFINES
85
//===========================================================================
86
#ifndef CYGARC_HAL_LPC2XXX_REG_EMAC_BASE
87
#error "CYGARC_HAL_LPC2XXX_REG_EMAC_BASE not defined by varaint/platform."
88
#else
89
#define EMAC_BASE CYGARC_HAL_LPC2XXX_REG_EMAC_BASE
90
#endif
91
 
92
//
93
// MAC registers
94
//
95
#define EMAC_MAC1   (EMAC_BASE + 0x0000)
96
#define EMAC_MAC2   (EMAC_BASE + 0x0004)
97
#define EMAC_IPGT   (EMAC_BASE + 0x0008)
98
#define EMAC_IPGR   (EMAC_BASE + 0x000C)
99
#define EMAC_CLRT   (EMAC_BASE + 0x0010)
100
#define EMAC_MAXF   (EMAC_BASE + 0x0014)
101
#define EMAC_SUPP   (EMAC_BASE + 0x0018)
102
#define EMAC_TEST   (EMAC_BASE + 0x001C)
103
#define EMAC_MCFG   (EMAC_BASE + 0x0020)
104
#define EMAC_MCMD   (EMAC_BASE + 0x0024)
105
#define EMAC_MADR   (EMAC_BASE + 0x0028)
106
#define EMAC_MWTD   (EMAC_BASE + 0x002C)
107
#define EMAC_MRDD   (EMAC_BASE + 0x0030)
108
#define EMAC_MIND   (EMAC_BASE + 0x0034)
109
#define EMAC_SA0    (EMAC_BASE + 0x0040)
110
#define EMAC_SA1    (EMAC_BASE + 0x0044)
111
#define EMAC_SA2    (EMAC_BASE + 0x0048)
112
 
113
//
114
// Control registers
115
//
116
#define EMAC_CMD            (EMAC_BASE + 0x0100)
117
#define EMAC_STAT           (EMAC_BASE + 0x0104)
118
#define EMAC_RXDESC         (EMAC_BASE + 0x0108)
119
#define EMAC_RXSTAT         (EMAC_BASE + 0x010C)
120
#define EMAC_RXDESC_NUM     (EMAC_BASE + 0x0110)
121
#define EMAC_RX_PROD_IDX    (EMAC_BASE + 0x0114)
122
#define EMAC_RX_CONSUME_IDX (EMAC_BASE + 0x0118)
123
#define EMAC_TXDESC         (EMAC_BASE + 0x011C)
124
#define EMAC_TXSTAT         (EMAC_BASE + 0x0120)
125
#define EMAC_TXDESC_NUM     (EMAC_BASE + 0x0124)
126
#define EMAC_TX_PROD_IDX    (EMAC_BASE + 0x0128)
127
#define EMAC_TX_CONSUME_IDX (EMAC_BASE + 0x012C)
128
#define EMAC_TSV0           (EMAC_BASE + 0x0158)
129
#define EMAC_TSV1           (EMAC_BASE + 0x015C)
130
#define EMAC_RSV            (EMAC_BASE + 0x0160)
131
#define EMAC_FLOWCTRL_CNT   (EMAC_BASE + 0x0170)
132
#define EMAC_FLOWCTRL_STAT  (EMAC_BASE + 0x0174)
133
 
134
//
135
// Rx filter registers
136
//
137
#define EMAC_RXFILT_CTRL      (EMAC_BASE + 0x0200)
138
#define EMAC_RXFILT_WOL_STAT  (EMAC_BASE + 0x0204)
139
#define EMAC_RXFILT_WOL_CLR   (EMAC_BASE + 0x0204)
140
#define EMAC_HASH_FILT_L      (EMAC_BASE + 0x0210)
141
#define EMAC_HASH_FILT_H      (EMAC_BASE + 0x0214)
142
 
143
//
144
// Module control registers
145
//
146
#define EMAC_INT_STATUS       (EMAC_BASE + 0x0FE0)
147
#define EMAC_INT_EN           (EMAC_BASE + 0x0FE4)
148
#define EMAC_INT_CLR          (EMAC_BASE + 0x0FE8)
149
#define EMAC_INT_SET          (EMAC_BASE + 0x0FEC)
150
#define EMAC_POWER_DOWN       (EMAC_BASE + 0x0FF4)
151
#define EMAC_MODULE_ID        (EMAC_BASE + 0x0FFC)
152
 
153
//
154
// Register bits MCMD
155
//
156
#define  MCMD_WRITE           0x00
157
#define  MCMD_READ            0x01
158
 
159
//
160
// Register bits COMMAND register
161
//
162
#define CMD_RX_ENABLE        (1 << 0)
163
#define CMD_TX_ENABLE        (1 << 1)
164
#define CMD_REG_RESET        (1 << 3)
165
#define CMD_TX_RESET         (1 << 4)
166
#define CMD_RX_RESET         (1 << 5)
167
#define CMD_PASS_RUNT_FRAME  (1 << 6)
168
#define CMD_PASS_RX_FILTER   (1 << 7)
169
#define CMD_TX_FLOW_CONTROL  (1 << 8)
170
#define CMD_RMII             (1 << 9)
171
#define CMD_FULL_DUPLEX      (1 << 10)
172
 
173
//
174
// Register bits SUPP register
175
//
176
#define SUPP_SPEED_100MB     (1 << 8)
177
 
178
#define  EMAC_OLD_MODULE_ID  ((0x3902 << 16) | 0x2000)
179
 
180
//
181
// Register bits MAC1
182
//
183
#define MAC1_RX_EN                (1 << 0)
184
#define MAC1_PASS_ALL_RX_FRAMES   (1 << 1)
185
#define MAC1_RX_FLOW_CTRL         (1 << 2)
186
#define MAC1_TX_FLOW_CTRL         (1 << 3)
187
#define MAC1_LOOPBACK             (1 << 4)
188
#define MAC1_RESET_TX             (1 << 8)
189
#define MAC1_RESET_MCS_TX         (1 << 9)
190
#define MAC1_RESET_RX             (1 << 10)
191
#define MAC1_RESET_MCS_RX         (1 << 11)
192
#define MAC1_RESET_SIM            (1 << 14)
193
#define MAC1_SOFT_RESET           (1 << 15)
194
 
195
//
196
// Register bits MAC2
197
//
198
#define MAC2_FDX                  (1 << 0)
199
#define MAC2_FRAME_LEN_CHECK      (1 << 1)
200
#define MAC2_HUGE_FRAME_EN        (1 << 2)
201
#define MAC2_DELAYED_CRC          (1 << 3)
202
#define MAC2_CRC_EN               (1 << 4)
203
#define MAC2_PAD_CRC_EN           (1 << 5)
204
#define MAC2_VLAN_PAD_EN          (1 << 6)
205
#define MAC2_AUTO_DETECT_PAD_EN   (1 << 7)
206
#define MAC2_PURE_PREAMBLE        (1 << 8)
207
#define MAC2_LONG_PREAMBLE        (1 << 9)
208
#define MAC2_NO_BACKOFF           (1 << 12)
209
#define MAC2_BACK_PRESS           (1 << 13)
210
#define MAC2_EXCESS_DEFER         (1 << 14)
211
 
212
//
213
// Register bits RXFILT_CTRL
214
//
215
#define RXFILT_CTRL_ACC_UNICAST            (1 << 0)
216
#define RXFILT_CTRL_ACC_BROADCAST          (1 << 1)
217
#define RXFILT_CTRL_ACC_MULTICAST          (1 << 2)
218
#define RXFILT_CTRL_ACC_UNICAST_HASH       (1 << 3)
219
#define RXFILT_CTRL_ACC_MULTICAST_HASH     (1 << 4)
220
#define RXFILT_CTRL_ACC_PERFECT            (1 << 5)
221
#define RXFILT_CTRL_MAGIG_PKT_WOL          (1 << 12)
222
#define RXFILT_CTRL_RXFILT_WOL             (1 << 13)
223
 
224
//
225
// Register bits MCFG
226
//
227
#define MCFG_SCAN_INCR      (1 << 0)
228
#define MCFG_SUPP_PREAMBLE  (1 << 1)
229
#define MCFG_RESET_MII_MGMT (1 << 15)
230
 
231
//
232
// Register bits MIND
233
//
234
#define MIND_BUSY      (1 << 0)
235
#define MIND_SCANNING  (1 << 1)
236
#define MIND_NOT_VALID (1 << 2)
237
#define MIND_LINK_FAIL (1 << 3)
238
 
239
//
240
// EMAC interrupts
241
//
242
#define EMAC_INT_RXOVERRUN  (1 << 0)
243
#define EMAC_INT_RXERROR    (1 << 1)
244
#define EMAC_INT_RXFINISHED (1 << 2)
245
#define EMAC_INT_RXDONE     (1 << 3)
246
#define EMAC_INT_TXUNDERRUN (1 << 4)
247
#define EMAC_INT_TXERROR    (1 << 5)
248
#define EMAC_INT_TXFINISHED (1 << 6)
249
#define EMAC_INT_TXDONE     (1 << 7)
250
#define EMAC_INT_SOFTINT    (1 << 12)
251
#define EMAC_INT_WOL        (1 << 13)
252
 
253
 
254
//===========================================================================
255
//                ETHERNET RAM AND DMA CONFIGURATION
256
//===========================================================================
257
#ifdef CYGHWR_HAL_LPC_EMAC_RAM_AHB
258
# include <cyg/infra/cyg_type.h>
259
# define EMAC_RAM_MEM_SECTION   CYGBLD_ATTRIB_SECTION(CYGHWR_HAL_LPC_EMAC_MEM_SECTION)
260
# define EMAC_RAM_BASE          (&emac_ahb_ram)
261
# define EMAC_RAM_SIZE          sizeof(emac_ahb_ram)
262
# define EMAC_BLOCK_SIZE        CYGHWR_HAL_LPC_EMAC_BLOCK_SIZE
263
#else // Backward compatibility
264
# define EMAC_RAM_BASE          0x7FE00000
265
# define EMAC_RAM_SIZE          0x00004000
266
# define EMAC_BLOCK_SIZE        0x600
267
#endif // CYGHWR_HAL_LPC_EMAC_RAM_AHB
268
 
269
//
270
// EMAC Descriptor TX and RX Control fields
271
//
272
#define EMAC_TX_DESC_INT        0x80000000
273
#define EMAC_TX_DESC_LAST       0x40000000
274
#define EMAC_TX_DESC_CRC        0x20000000
275
#define EMAC_TX_DESC_PAD        0x10000000
276
#define EMAC_TX_DESC_HUGE       0x08000000
277
#define EMAC_TX_DESC_OVERRIDE   0x04000000
278
#define EMAC_RX_DESC_INT        0x80000000
279
 
280
//
281
// EMAC Descriptor status related definition
282
//
283
#define TX_DESC_STATUS_ERR      0x80000000
284
#define TX_DESC_STATUS_NODESC   0x40000000
285
#define TX_DESC_STATUS_UNDERRUN 0x20000000
286
#define TX_DESC_STATUS_LCOL     0x10000000
287
#define TX_DESC_STATUS_ECOL     0x08000000
288
#define TX_DESC_STATUS_EDEFER   0x04000000
289
#define TX_DESC_STATUS_DEFER    0x02000000
290
#define TX_DESC_STATUS_COLCNT   0x01E00000
291
 
292
#define RX_DESC_STATUS_ERR      0x80000000
293
#define RX_DESC_STATUS_LAST     0x40000000
294
#define RX_DESC_STATUS_NODESC   0x20000000
295
#define RX_DESC_STATUS_OVERRUN  0x10000000
296
#define RX_DESC_STATUS_ALGNERR  0x08000000
297
#define RX_DESC_STATUS_RNGERR   0x04000000
298
#define RX_DESC_STATUS_LENERR   0x02000000
299
#define RX_DESC_STATUS_SYMERR   0x01000000
300
#define RX_DESC_STATUS_CRCERR   0x00800000
301
#define RX_DESC_STATUS_BCAST    0x00400000
302
#define RX_DESC_STATUS_MCAST    0x00200000
303
#define RX_DESC_STATUS_FAILFLT  0x00100000
304
#define RX_DESC_STATUS_VLAN     0x00080000
305
#define RX_DESC_STATUS_CTLFRAM  0x00040000
306
 
307
#define DESC_SIZE_MASK          0x000007FF
308
 
309
//
310
// This type defines a descriptor
311
//
312
typedef struct descriptor_st
313
{
314
    cyg_uint32 packet;
315
    cyg_uint32 control;
316
} descriptor_t;
317
 
318
//
319
// This type defines a rx status
320
// (double word aligned)
321
//
322
typedef struct rx_status_st
323
{
324
    cyg_uint32 info;
325
    cyg_uint32 hash_crc;
326
} rx_status_t;
327
 
328
//
329
// This type defines a tx status
330
//
331
typedef cyg_uint32 tx_status_t;
332
typedef union eth_buf_st
333
{
334
    cyg_uint32 dwords[EMAC_BLOCK_SIZE / sizeof(cyg_uint32)];
335
    cyg_uint8  bytes[EMAC_BLOCK_SIZE];
336
} eth_buf_t;
337
 
338
 
339
//
340
// Ethernet RAM and buffer configuration type
341
// The descriptors and statuses require a certain alignment. By defining each
342
// data type we assure this alignment.
343
// The TX descriptor array, the TX status array and the RX descriptor array
344
// need to be aligned on a 4 Byte (32 bit) address boundary. The RX status
345
// array need to be aligned on a 8 Byte (64 bit) address boundary.
346
//
347
typedef struct eth_ram_cfg_st
348
{
349
    volatile rx_status_t  rx_status[CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS];
350
    volatile descriptor_t rx_descr[CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS];
351
    volatile tx_status_t  tx_status[CYGNUM_DEVS_ETH_ARM_LPC2XXX_TX_BUFS];
352
    volatile descriptor_t tx_descr[CYGNUM_DEVS_ETH_ARM_LPC2XXX_TX_BUFS];
353
    volatile eth_buf_t    rx_buf[CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS];
354
} eth_ram_cfg_t;
355
 
356
#ifdef CYGHWR_HAL_LPC_EMAC_RAM_AHB
357
volatile static eth_ram_cfg_t emac_ahb_ram EMAC_RAM_MEM_SECTION;
358
#endif
359
 
360
#if CYGPKG_DEVS_ETH_ARM_LPC2XXX_DEBUG_LEVEL < 3
361
#if CYGPKG_DEVS_ETH_ARM_LPC2XXX_DEBUG_LEVEL > 0
362
   #define debug1_printf(args...) diag_printf(args)
363
#else
364
   #define debug1_printf(args...)
365
#endif
366
#if CYGPKG_DEVS_ETH_ARM_LPC2XXX_DEBUG_LEVEL > 1
367
   #define debug2_printf(args...) diag_printf(args)
368
#else
369
   #define debug2_printf(args...)
370
#endif
371
#else // CYGPKG_DEVS_ETH_ARM_LPC2XXX_DEBUG_LEVEL > 3
372
//
373
// Debug level 3 is a special kind of debug level. It prints the same debug
374
// messages like level 2 but it forces the debug output to the second serial
375
// port. This may be necessary for debugging the ethernet driver if the
376
// stand-alone network stack from redboot is used. It is possible to
377
// print debug messages while an application is loaded and run via GDB
378
// TCP/IP connection
379
//
380
static int cur;
381
#define debug1_printf(args...)                                               \
382
{                                                                            \
383
   cur =                                                                     \
384
   CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);\
385
   CYGACC_CALL_IF_SET_CONSOLE_COMM(1);                                       \
386
   diag_printf(args);                                                        \
387
   CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);                                     \
388
}
389
#define debug2_printf(args...)                                               \
390
{                                                                            \
391
   cur =                                                                     \
392
   CYGACC_CALL_IF_SET_CONSOLE_COMM(CYGNUM_CALL_IF_SET_COMM_ID_QUERY_CURRENT);\
393
   CYGACC_CALL_IF_SET_CONSOLE_COMM(1);                                       \
394
   diag_printf(args);                                                        \
395
   CYGACC_CALL_IF_SET_CONSOLE_COMM(cur);                                     \
396
}
397
#endif // CYGPKG_DEVS_ETH_ARM_LPC2XXX_DEBUG_LEVEL > 3
398
 
399
 
400
//Driver interface callbacks
401
#define _eth_drv_init(sc,mac)           \
402
  (sc->funs->eth_drv->init)(sc,(unsigned char *)mac)
403
#define _eth_drv_tx_done(sc,key,status)     \
404
  (sc->funs->eth_drv->tx_done)(sc,key,status)
405
#define _eth_drv_recv(sc,len)           \
406
  (sc->funs->eth_drv->recv)(sc,len)
407
 
408
#ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
409
static cyg_uint32
410
lpc2xxx_eth_isr (cyg_vector_t vector, cyg_addrword_t data);
411
#endif
412
 
413
 
414
// --------------------------------------------------------------
415
// RedBoot configuration options for managing ESAs for us
416
 
417
// Decide whether to have redboot config vars for it...
418
#if defined(CYGSEM_REDBOOT_FLASH_CONFIG) && defined(CYGPKG_REDBOOT_NETWORKING)
419
#include <redboot.h>
420
#include <flash_config.h>
421
 
422
#ifdef CYGSEM_DEVS_ETH_ARM_LPC2XXX_REDBOOT_HOLDS_ESA_ETH0
423
RedBoot_config_option("Network hardware address [MAC] for eth0",
424
                      eth0_esa_data,
425
                      ALWAYS_ENABLED, true,
426
                      CONFIG_ESA, 0);
427
#endif // CYGSEM_DEVS_ETH_ARM_LPC2XXX_REDBOOT_HOLDS_ESA_ETH0
428
#endif  // CYGPKG_REDBOOT_NETWORKING && CYGSEM_REDBOOT_FLASH_CONFIG
429
 
430
// and initialization code to read them
431
// - independent of whether we are building RedBoot right now:
432
#ifdef CYGPKG_DEVS_ETH_ARM_LPC2XXX_REDBOOT_HOLDS_ESA
433
#include <cyg/hal/hal_if.h>
434
#ifndef CONFIG_ESA
435
    #define CONFIG_ESA (6)
436
#endif
437
 
438
#define CYGHWR_DEVS_ETH_ARM_LPC2XXX_GET_ESA( mac_address, ok )        \
439
  CYG_MACRO_START                                                     \
440
  ok = CYGACC_CALL_IF_FLASH_CFG_OP( CYGNUM_CALL_IF_FLASH_CFG_GET,     \
441
                                    "eth0_esa_data",                  \
442
                                    mac_address,                      \
443
                                    CONFIG_ESA);                      \
444
  CYG_MACRO_END
445
#endif // CYGPKG_DEVS_ETH_ARM_LPC2XXX_REDBOOT_HOLDS_ESA
446
 
447
//
448
// LPC2xxx Ethernet private data
449
// The LPC2468 has a single on-chip ethernet device, so all device-specific
450
// data could be held in statics. However, in case this driver ever gets
451
// re-used for a chip with multiple ethernet devices that data is held
452
// in a driver-specific structure.
453
//
454
typedef struct lpc2xxx_eth_priv_s
455
{
456
   cyg_uint32              intr_vector;
457
   cyg_uint8              *enaddr;
458
   cyg_uint32              base;      // Base address of device
459
   eth_phy_access_t       *phy;
460
   cyg_uint32              cur_tx_key;
461
   cyg_uint32              total_len;
462
   cyg_uint8               iterator;
463
   cyg_bool                tx_busy;
464
   volatile eth_ram_cfg_t *ram;
465
#ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
466
   cyg_interrupt intr;
467
   cyg_handle_t  intr_handle;
468
#endif
469
} lpc2xxx_eth_priv_t;
470
 
471
 
472
//===========================================================================
473
//                           PHY SPECIFIC STUFF
474
//===========================================================================
475
 
476
 
477
//===========================================================================
478
// Write one of the PHY registers via the MII bus
479
//===========================================================================
480
static void
481
lpc2xxx_write_phy(int reg_addr, int phy_addr, unsigned short data)
482
{
483
    cyg_uint32 regval;
484
 
485
    HAL_WRITE_UINT32(EMAC_MCMD, MCMD_WRITE);
486
    regval = (phy_addr << 8) | reg_addr;
487
    HAL_WRITE_UINT32(EMAC_MADR, regval);
488
    HAL_WRITE_UINT32(EMAC_MWTD, data);
489
 
490
    do
491
    {
492
        HAL_READ_UINT32(EMAC_MIND, regval);
493
    }
494
    while (regval != 0);
495
}
496
 
497
 
498
//===========================================================================
499
// Read one of the PHY registers via the MII bus
500
//===========================================================================
501
static bool
502
lpc2xxx_read_phy(int reg_addr, int phy_addr, unsigned short *data)
503
{
504
    cyg_uint32 regval;
505
 
506
    HAL_WRITE_UINT32(EMAC_MCMD, 0);
507
    regval = (phy_addr << 8) | reg_addr;
508
    HAL_WRITE_UINT32(EMAC_MADR, regval);
509
    HAL_WRITE_UINT32(EMAC_MCMD, MCMD_READ);
510
 
511
    do
512
    {
513
        HAL_READ_UINT32(EMAC_MIND, regval);
514
    }
515
    while ((regval & ~MIND_LINK_FAIL) != 0);
516
 
517
    if (regval & MIND_LINK_FAIL)
518
    {
519
        debug1_printf("LPC2XXX_ETH: PHY link fail\n");
520
    }
521
 
522
    HAL_WRITE_UINT32(EMAC_MCMD, 0x0000);
523
    HAL_READ_UINT32(EMAC_MRDD, regval);
524
    debug2_printf("LPC2XXX_ETH: read result %08x\n", regval);
525
    *data = regval & 0xFFFF;
526
    return true;
527
}
528
 
529
 
530
//===========================================================================
531
// Init device for phy access
532
//===========================================================================
533
static void lpc2xxx_init_phy(void)
534
{
535
    int        i;
536
    cyg_uint32 regval;
537
 
538
    //
539
    // host clock divided by 20, no suppress preamble, no scan increment
540
    // Reset management hardware
541
    //
542
    HAL_WRITE_UINT32(EMAC_MCFG, 0x0018 | MCFG_RESET_MII_MGMT);
543
    for (i = 0; i < 0x40; ++i)
544
    {
545
        asm volatile ("  nop");
546
    }
547
    HAL_WRITE_UINT32(EMAC_MCFG, 0x0018);
548
    HAL_WRITE_UINT32(EMAC_MCMD, MCMD_WRITE);
549
 
550
    HAL_READ_UINT32(EMAC_CMD, regval);
551
    HAL_WRITE_UINT32(EMAC_CMD, regval | CMD_RMII); // RMII configuration
552
    HAL_WRITE_UINT32(EMAC_SUPP, 0x800 | SUPP_SPEED_100MB);
553
    HAL_DELAY_US(50);
554
    HAL_WRITE_UINT32(EMAC_SUPP, SUPP_SPEED_100MB);
555
    HAL_DELAY_US(1000);
556
}
557
 
558
 
559
//===========================================================================
560
// PHY specific data structures
561
//===========================================================================
562
ETH_PHY_REG_LEVEL_ACCESS_FUNS(lpc2xxx_phy,
563
                              lpc2xxx_init_phy,
564
                              NULL,
565
                              lpc2xxx_write_phy,
566
                              lpc2xxx_read_phy);
567
 
568
 
569
//===========================================================================
570
// Hardware driver specific data
571
//===========================================================================                              
572
lpc2xxx_eth_priv_t lpc2xxx_priv_data =
573
{
574
   .intr_vector = CYGNUM_HAL_INTERRUPT_ETH,
575
   .base        = EMAC_BASE,
576
   .phy         = &lpc2xxx_phy,
577
   .ram         =  (volatile eth_ram_cfg_t*)EMAC_RAM_BASE,
578
   .total_len   = 0,
579
   .iterator    = 0,
580
   .tx_busy     = false
581
};
582
 
583
 
584
//===========================================================================
585
// Initialize TX descriptors
586
// To avoid copy operations the higher-level sg_list entries will be 
587
// directly put into the device's TX descriptors (ring buffer).
588
//===========================================================================
589
static void lpc2xxx_eth_txdesc_init(lpc2xxx_eth_priv_t * priv)
590
{
591
    int i;
592
 
593
    debug1_printf("\nLPC2XXX_ETH: Initialising TX descriptors\n");
594
    debug2_printf("&priv->ram->tx_descr[0]:  0x%08X\n",
595
                  (cyg_uint32)priv->ram->tx_descr);
596
    debug2_printf("&priv->ram->tx_status[0]: 0x%08X\n",
597
                  (cyg_uint32)priv->ram->tx_descr);
598
    debug2_printf("No of TX descr: %d\n",
599
                  CYGNUM_DEVS_ETH_ARM_LPC2XXX_TX_BUFS);
600
 
601
    //
602
    // Init status for all TX descriptors
603
    //
604
    for (i = 0; i < CYGNUM_DEVS_ETH_ARM_LPC2XXX_TX_BUFS; i++)
605
    {
606
        priv->ram->tx_status[i] = 0;
607
    }
608
 
609
    //
610
    // Initialise TX pointers
611
    //
612
    HAL_WRITE_UINT32(EMAC_TXDESC, (cyg_uint32)priv->ram->tx_descr);
613
    HAL_WRITE_UINT32(EMAC_TXSTAT, (cyg_uint32)priv->ram->tx_status);
614
    HAL_WRITE_UINT32(EMAC_TXDESC_NUM, CYGNUM_DEVS_ETH_ARM_LPC2XXX_TX_BUFS - 1);
615
    HAL_WRITE_UINT32(EMAC_TX_PROD_IDX, 0);
616
}
617
 
618
 
619
//===========================================================================
620
// Initialize RX descriptors
621
// The driver currently supports up to 4 rx buffers. In the current 
622
// implementation the size of the RX buffers is fixed to 1536 (0x600) bytes.
623
// Incoming frames are then copied to higher-level's code sg lists.
624
//===========================================================================
625
static void lpc2xxx_eth_rxdesc_init(lpc2xxx_eth_priv_t * priv)
626
{
627
    int i;
628
 
629
    debug1_printf("\nLPC2XXX_ETH: Initialising RX descriptors\n");
630
    debug2_printf("&priv->ram->rx_descr[0]:  0x%08X\n",
631
                  (cyg_uint32)priv->ram->rx_descr);
632
    debug2_printf("&priv->ram->rx_status[0]: 0x%08X\n",
633
                  (cyg_uint32)priv->ram->rx_status);
634
    debug2_printf("No of TX descr: %d\n", CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS);
635
 
636
    //
637
    // Initialise RX descriptors - store pointers to receive buffers and
638
    // RX status, two words, status info. and status hash CRC.
639
    //
640
    for (i = 0; i < CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS; ++i)
641
    {
642
        priv->ram->rx_descr[i].packet  = (cyg_uint32)priv->ram->rx_buf[i].bytes;
643
        debug2_printf("priv->ram->rx_descr[%d].packet (0x%08X) "
644
                      "= &priv->ram->rx_buf[%d].bytes[0] (0x%08X)\n",
645
                      i, (cyg_uint32)&priv->ram->rx_descr[i].packet,
646
                      i, (cyg_uint32)priv->ram->rx_buf[i].bytes);
647
        priv->ram->rx_descr[i].control = (EMAC_RX_DESC_INT
648
                                       | ((EMAC_BLOCK_SIZE - 1)
649
                                          & DESC_SIZE_MASK));
650
        debug2_printf("priv->ram->rx_descr[%d].control (0x%08X) = 0x%08X\n",
651
                      i, (cyg_uint32)&priv->ram->rx_descr[i].control,
652
                      priv->ram->rx_descr[i].control);
653
        priv->ram->rx_status[i].info     = 0;
654
        priv->ram->rx_status[i].hash_crc = 0;
655
    }
656
 
657
    //
658
    // Initialise RX pointers
659
    //
660
    HAL_WRITE_UINT32(EMAC_RXDESC, (cyg_uint32)priv->ram->rx_descr);
661
    HAL_WRITE_UINT32(EMAC_RXSTAT, (cyg_uint32)priv->ram->rx_status);
662
    HAL_WRITE_UINT32(EMAC_RXDESC_NUM, CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS - 1);
663
    HAL_WRITE_UINT32(EMAC_RX_CONSUME_IDX, 0);
664
}
665
 
666
 
667
//===========================================================================
668
// Set a specific address match to a given address. Packets received which
669
// match this address will be passed on.
670
//===========================================================================
671
static void lpc2xxx_set_mac(lpc2xxx_eth_priv_t * priv, cyg_uint8 * enaddr)
672
{
673
    HAL_WRITE_UINT32(EMAC_SA0, (enaddr[5] << 8 | enaddr[4]));
674
    HAL_WRITE_UINT32(EMAC_SA1, (enaddr[3] << 8 | enaddr[2]));
675
    HAL_WRITE_UINT32(EMAC_SA2, (enaddr[1] << 8 | enaddr[0]));
676
}
677
 
678
 
679
//===========================================================================
680
// This function is called to stop the interface.
681
//===========================================================================
682
static void lpc2xxx_eth_stop(struct eth_drv_sc *sc)
683
{
684
    cyg_uint32          regval;
685
 
686
    // Disable the receiver and transmitter
687
    HAL_READ_UINT32(EMAC_MAC1, regval);
688
    HAL_WRITE_UINT32(EMAC_MAC1, regval & ~MAC1_RX_EN);
689
    HAL_READ_UINT32(EMAC_CMD, regval);
690
    HAL_WRITE_UINT32(EMAC_CMD, regval & ~(CMD_RX_ENABLE | CMD_TX_ENABLE));
691
}
692
 
693
 
694
//===========================================================================
695
// This function is called to "start up" the interface. It may be called
696
// multiple times, even when the hardware is already running.
697
//===========================================================================
698
static void
699
lpc2xxx_eth_start(struct eth_drv_sc *sc, unsigned char *enaddr, int flags)
700
{
701
    cyg_uint32  regval;
702
 
703
    //
704
    // To prevent overflow in the receive DMA engine the receive DMA engine 
705
    // should be enabled by setting the RxEnable bit in the Command register 
706
    // before enabling the receive datapath in the MAC by setting the 
707
    // RECEIVE ENABLE bit in the MAC1 register
708
    //
709
    HAL_READ_UINT32(EMAC_CMD, regval);
710
    HAL_WRITE_UINT32(EMAC_CMD, regval | CMD_RX_ENABLE | CMD_TX_ENABLE);
711
    HAL_READ_UINT32(EMAC_MAC1, regval);
712
    HAL_WRITE_UINT32(EMAC_MAC1, regval | MAC1_RX_EN);
713
}
714
 
715
 
716
//===========================================================================
717
// Enable and Disable of the receiver and transmitter.
718
// Initialize the interface. This configures the interface ready for use.
719
// Interrupts are grabbed etc. This means the start function has
720
// little to do except enable the receiver
721
//===========================================================================
722
static bool lpc2xxx_eth_init(struct cyg_netdevtab_entry *tab)
723
{
724
    struct eth_drv_sc *sc    = (struct eth_drv_sc *)tab->device_instance;
725
    lpc2xxx_eth_priv_t *priv = (lpc2xxx_eth_priv_t *)sc->driver_private;
726
    bool esa_ok = false;
727
    unsigned char enaddr[6] = { CYGPKG_DEVS_ETH_ARM_LPC2XXX_MACADDR};
728
    unsigned short phy_state = 0;
729
    cyg_uint32 regval;
730
 
731
    debug1_printf("\nLPC2XXX_ETH: Initialising 0x%08X\n",priv->base);
732
 
733
    //
734
    // turn on the ethernet MAC clock in PCONP, bit 30 and then do
735
    // a short delay
736
    //
737
    HAL_READ_UINT32(CYGARC_HAL_LPC24XX_REG_SCB_BASE +
738
                    CYGARC_HAL_LPC24XX_REG_PCONP, regval);
739
    HAL_WRITE_UINT32(CYGARC_HAL_LPC24XX_REG_SCB_BASE +
740
                     CYGARC_HAL_LPC24XX_REG_PCONP,
741
                     regval | CYGARC_HAL_LPC24XX_REG_PCONP_ENET);
742
    HAL_DELAY_US(50);
743
 
744
    //
745
    // Because of a device errate in Rev '-' devices we need to check the
746
    // module ID to setup the RMII mode properly
747
    //
748
    HAL_READ_UINT32(EMAC_MODULE_ID, regval);
749
    if (EMAC_OLD_MODULE_ID == regval)
750
    {
751
        //
752
        // On Rev. '-', EMAC_MODULE_ID should be equal to
753
        // EMAC_OLD_MODULE_ID, P1.6 should be set -
754
        // selects P1[0,1,4,6,8,9,10,14,15]
755
        //
756
        HAL_WRITE_UINT32(CYGARC_HAL_LPC24XX_REG_PIN_BASE +
757
                        CYGARC_HAL_LPC24XX_REG_PINSEL2, 0x50151105);
758
    }
759
   else
760
   {
761
        //
762
        // on Rev. 'A', EMAC_MODULE_ID should not equal to
763
        // OLD_EMAC_MODULE_ID, P1.6 should not be set.
764
        // selects P1[0,1,4,8,9,10,14,15]
765
            //
766
        HAL_WRITE_UINT32(CYGARC_HAL_LPC24XX_REG_PIN_BASE +
767
                         CYGARC_HAL_LPC24XX_REG_PINSEL2, 0x50150105);
768
 
769
    } // if (EMAC_OLD_MODULE_ID == regval)
770
 
771
    // selects P1[17:16]
772
    HAL_WRITE_UINT32(CYGARC_HAL_LPC24XX_REG_PIN_BASE +
773
                     CYGARC_HAL_LPC24XX_REG_PINSEL3, 0x00000005);
774
    //
775
    // reset MAC modules, tx, mcs_tx, rx, mcs_rx, simulation and soft reset,
776
    // reset datapaths and host registers, disable Tx and RX and do a
777
    // short delay
778
    //
779
    HAL_WRITE_UINT32(EMAC_MAC1, MAC1_RESET_TX
780
                                  | MAC1_RESET_MCS_TX
781
                                  | MAC1_RESET_RX
782
                                  | MAC1_RESET_MCS_RX
783
                                  | MAC1_RESET_SIM
784
                                  | MAC1_SOFT_RESET);
785
    HAL_WRITE_UINT32(EMAC_CMD, CMD_REG_RESET
786
                                 | CMD_TX_RESET
787
                                 | CMD_RX_RESET);
788
    HAL_DELAY_US(10);
789
    HAL_WRITE_UINT32(EMAC_MAC1, 0x0);
790
    lpc2xxx_eth_stop(sc); // disable transmitter and receiver
791
 
792
    HAL_WRITE_UINT32(EMAC_MAC2, 0); // initialize MAC2 register to default val.
793
    HAL_WRITE_UINT32(EMAC_IPGR, 0x12);   // manual recommends value 0x12
794
    HAL_WRITE_UINT32(EMAC_CLRT, 0x370F); // Using recommended value from manual.
795
    HAL_WRITE_UINT32(EMAC_MAXF, 0x0600); // manual recommends value 0x600
796
 
797
#ifdef CYGHWR_DEVS_ETH_ARM_LPC2XXX_GET_ESA
798
    // Get MAC address from RedBoot configuration variables
799
    CYGHWR_DEVS_ETH_ARM_LPC2XXX_GET_ESA(&enaddr[0], esa_ok);
800
    // If this call fails myMacAddr is unchanged and MAC address from
801
    // CDL is used
802
#endif
803
 
804
    if (!esa_ok)
805
    {
806
       // Can't figure out ESA
807
       debug1_printf("LPC2XXX_ETH - Warning! ESA unknown\n");
808
    }
809
    debug1_printf("LPC2XXX_ETH: %02x:%02x:%02x:%02x:%02x:%02x\n",
810
                  enaddr[0],enaddr[1],enaddr[2],
811
                  enaddr[3],enaddr[4],enaddr[5]);
812
 
813
    // Give the EMAC its address
814
    lpc2xxx_set_mac(priv, enaddr);
815
 
816
    // Setup the PHY
817
    CYG_ASSERTC(priv->phy);
818
    if (!_eth_phy_init(priv->phy))
819
    {
820
       return (false);
821
    }
822
 
823
    // Get the current mode and print it
824
    phy_state = _eth_phy_state(priv->phy);
825
    if (phy_state & ETH_PHY_STAT_LINK)
826
    {
827
        cyg_uint32 cmd;
828
        HAL_READ_UINT32(EMAC_CMD, cmd);
829
        cmd |= CMD_PASS_RUNT_FRAME;
830
        HAL_READ_UINT32(EMAC_SUPP, regval);
831
        if (phy_state & ETH_PHY_STAT_100MB)
832
        {
833
            HAL_WRITE_UINT32(EMAC_SUPP, regval | SUPP_SPEED_100MB);
834
            debug1_printf("LPC2XXX_ETH: 100Mbyte/s");
835
        }
836
        else
837
        {
838
            HAL_WRITE_UINT32(EMAC_SUPP, regval &~ SUPP_SPEED_100MB);
839
            debug1_printf("LPC2XXX_ETH: 10Mbyte/s");
840
        }
841
        if(phy_state & ETH_PHY_STAT_FDX)
842
        {
843
            HAL_WRITE_UINT32(EMAC_MAC2, MAC2_FDX | MAC2_CRC_EN
844
                             | MAC2_PAD_CRC_EN);
845
            // manual recommends value of 0x15 for FDX
846
            HAL_WRITE_UINT32(EMAC_IPGT, 0x15);
847
            cmd |= CMD_FULL_DUPLEX;
848
            debug1_printf(" Full Duplex\n");
849
        }
850
        else
851
        {
852
            HAL_WRITE_UINT32(EMAC_MAC2, MAC2_CRC_EN | MAC2_PAD_CRC_EN);
853
            // manual recommends value of 0x12 for HDX
854
            HAL_WRITE_UINT32(EMAC_IPGT, 0x12);
855
            debug1_printf(" Half Duplex\n");
856
        }
857
        HAL_WRITE_UINT32(EMAC_CMD, cmd);
858
    }
859
    else
860
    {
861
       debug1_printf("LPC2XXX_ETH: No Link\n");
862
    }
863
 
864
    lpc2xxx_eth_txdesc_init(priv);
865
    lpc2xxx_eth_rxdesc_init(priv);
866
 
867
    // set up the Rx filter
868
    // [0]-AllUnicast, [1]-AllBroadCast, [2]-AllMulticast, [3]-UnicastHash
869
    // [4]-MulticastHash, [5]-Perfect, [12]-MagicPacketEnWoL, 
870
    // [13]-RxFilterEnWoL
871
    HAL_WRITE_UINT32(EMAC_RXFILT_CTRL, RXFILT_CTRL_ACC_BROADCAST
872
                                         | RXFILT_CTRL_ACC_PERFECT);
873
    //
874
    // If we are building an interrupt enabled version, install the
875
    // interrupt handler
876
#ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
877
    debug1_printf("LPC2XXX_ETH: Installing Interrupts on IRQ %d\n",
878
                  priv->intr_vector);
879
    cyg_drv_interrupt_create(priv->intr_vector,
880
                             CYGPKG_DEVS_ETH_ARM_LPC2XXX_INTPRIO,
881
                             (cyg_addrword_t)sc,
882
                             lpc2xxx_eth_isr,
883
                             eth_drv_dsr,
884
                             &priv->intr_handle,
885
                             &priv->intr);
886
 
887
    cyg_drv_interrupt_attach(priv->intr_handle);
888
    cyg_drv_interrupt_unmask(priv->intr_vector);
889
#endif // CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
890
 
891
    // Initialize the upper layer driver
892
    _eth_drv_init(sc, enaddr);
893
 
894
    // clear all interrupts
895
    HAL_WRITE_UINT32(EMAC_INT_CLR, 0xFFFF);
896
 
897
    //
898
    // Enable the interrupts we are interested in (not SoftInt and WoL)
899
    // We only want interrupts if a whole frame was transmitted so we 
900
    // enable only the TXFINISHED interrupt and not the TXDONE interrupt
901
    //
902
    HAL_WRITE_UINT32(EMAC_INT_EN, EMAC_INT_RXOVERRUN
903
                                    | EMAC_INT_RXFINISHED
904
                                    | EMAC_INT_RXDONE
905
                                    | EMAC_INT_TXUNDERRUN
906
                                    | EMAC_INT_TXERROR
907
                                    | EMAC_INT_TXFINISHED);
908
 
909
    return (true);
910
}
911
 
912
 
913
//===========================================================================
914
// This function is called for low level "control" operations
915
//===========================================================================
916
static int
917
lpc2xxx_eth_control(struct eth_drv_sc *sc, unsigned long key,
918
                    void *data, int length)
919
{
920
    lpc2xxx_eth_priv_t *priv = (lpc2xxx_eth_priv_t *)sc->driver_private;
921
    switch (key)
922
    {
923
        case ETH_DRV_SET_MAC_ADDRESS:
924
        {
925
            if(length >= ETHER_ADDR_LEN)
926
            {
927
               lpc2xxx_eth_stop(sc);
928
 
929
               cyg_uint8 * enaddr = (cyg_uint8 *)data;
930
               debug1_printf("LPC2XXX_ETH: %02x:%02x:%02x:%02x:%02x:%02x\n",
931
                             enaddr[0],enaddr[1],enaddr[2],
932
                             enaddr[3],enaddr[4],enaddr[5]);
933
 
934
               lpc2xxx_set_mac(priv, enaddr);
935
               lpc2xxx_eth_start(sc,enaddr,0);
936
               return 0;
937
            }
938
            return 1;
939
        }
940
 
941
#ifdef CYGPKG_NET
942
        case ETH_DRV_GET_IF_STATS:
943
        {
944
            // todo
945
        }
946
#endif
947
 
948
        default:
949
        {
950
            diag_printf("%s.%d: key %lx\n", __FUNCTION__, __LINE__, key);
951
            return (1);
952
        }
953
   } // switch (key)
954
}
955
 
956
 
957
//===========================================================================
958
// This function is called to see if another packet can be sent.
959
// It should return the number of packets which can be handled.
960
// Zero should be returned if the interface is busy and can not send
961
// any more.
962
//===========================================================================
963
static int lpc2xxx_eth_can_send(struct eth_drv_sc *sc)
964
{
965
    int                 can_send;
966
    lpc2xxx_eth_priv_t *priv = (lpc2xxx_eth_priv_t *)sc->driver_private;
967
    if (priv->tx_busy)
968
    {
969
        can_send = 0;
970
    }
971
    else
972
    {
973
        can_send = 1;
974
    }
975
 
976
    debug2_printf("can_send: %d\n", can_send);
977
    return (can_send);
978
}
979
 
980
//===========================================================================
981
// This routine is called to send data to the hardware
982
// For tx the current code supports a single outgoing transmission at a
983
// time. This does limit outgoing bandwidth a bit, but only a bit, and
984
// saves memory and complexity.
985
//===========================================================================
986
static void
987
lpc2xxx_eth_send(struct        eth_drv_sc *sc,
988
                 struct        eth_drv_sg *sg_list,
989
                 int           sg_len,
990
                 int           total_len,
991
                 unsigned long key)
992
{
993
    cyg_uint32 tx_producer_idx;
994
    cyg_uint32 tx_consumer_idx;
995
    cyg_uint32 tx_desc_ctrl;
996
    lpc2xxx_eth_priv_t *priv = (lpc2xxx_eth_priv_t *)sc->driver_private;
997
    int i;
998
 
999
    priv->tx_busy = true;
1000
    //
1001
    // Store addresses of entries in scatter/gather list into tx descriptors
1002
    //
1003
    HAL_READ_UINT32(EMAC_TX_PROD_IDX, tx_producer_idx);
1004
    HAL_READ_UINT32(EMAC_TX_CONSUME_IDX, tx_consumer_idx);
1005
    debug2_printf("TPI: %d TCI: %d\n", tx_producer_idx, tx_consumer_idx);
1006
    debug2_printf("TX: sg_len %d, total_len %d, key 0x%08lX\n",
1007
                  sg_len, total_len, key);
1008
    for(i = 0; i < sg_len; i++)
1009
    {
1010
        priv->ram->tx_descr[tx_producer_idx].packet = sg_list[i].buf;
1011
        tx_desc_ctrl = (sg_list[i].len - 1) | EMAC_TX_DESC_INT;
1012
        if (i == (sg_len - 1))
1013
        {
1014
            tx_desc_ctrl |= EMAC_TX_DESC_LAST;
1015
        }
1016
        priv->ram->tx_descr[tx_producer_idx].control = tx_desc_ctrl;
1017
        priv->ram->tx_status[tx_producer_idx] = 0;
1018
 
1019
        tx_producer_idx = (tx_producer_idx + 1) %
1020
                           CYGNUM_DEVS_ETH_ARM_LPC2XXX_TX_BUFS;
1021
    }
1022
 
1023
    debug2_printf("TPI: %d TCI: %d\n", tx_producer_idx, tx_consumer_idx);
1024
    // Store away the key for the time when the transmit has completed
1025
    // and we need to tell the stack which transmit has completed
1026
    // and then start the transmitter by incrementing the tx producer
1027
    // index
1028
    priv->cur_tx_key = key;
1029
    HAL_WRITE_UINT32(EMAC_TX_PROD_IDX, tx_producer_idx);
1030
}
1031
 
1032
 
1033
//===========================================================================
1034
// EMAC ISR
1035
//===========================================================================
1036
#ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
1037
static cyg_uint32
1038
lpc2xxx_eth_isr (cyg_vector_t vector, cyg_addrword_t data)
1039
{
1040
    debug2_printf("ISR\n");
1041
    cyg_drv_interrupt_mask(vector);
1042
    cyg_interrupt_acknowledge(vector);
1043
    return CYG_ISR_CALL_DSR;
1044
}
1045
#endif // CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
1046
 
1047
 
1048
//===========================================================================
1049
// The high level DSR thaqt does all the work
1050
//===========================================================================
1051
static void lpc2xxx_eth_deliver(struct eth_drv_sc *sc)
1052
{
1053
    lpc2xxx_eth_priv_t *priv = (lpc2xxx_eth_priv_t *)sc->driver_private;
1054
    cyg_uint32          intstatus;
1055
 
1056
    HAL_READ_UINT32(EMAC_INT_STATUS, intstatus);
1057
#ifndef CYGPKG_REDBOOT
1058
    //
1059
    // In redbot the ethernet driver is polled and this debug message would
1060
    // pollute the redboot console
1061
    //
1062
    debug2_printf("INT_STATUS: %x\n", intstatus);
1063
#endif // CYGPKG_REDBOOT
1064
 
1065
    if (intstatus & EMAC_INT_RXOVERRUN)
1066
    {
1067
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_RXOVERRUN);
1068
        debug1_printf("LPC2XXX_ETH: RX overrun\n");
1069
    }
1070
 
1071
    if (intstatus & EMAC_INT_TXUNDERRUN)
1072
    {
1073
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_TXUNDERRUN);
1074
        debug1_printf("LPC2XXX_ETH: TX underrun\n");
1075
    }
1076
 
1077
    if (intstatus & EMAC_INT_TXERROR)
1078
    {
1079
        cyg_uint32 tx_consumer_idx;
1080
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_TXERROR);
1081
        HAL_READ_UINT32(EMAC_TX_CONSUME_IDX, tx_consumer_idx);
1082
        debug1_printf("LPC2XXX_ETH: TX error, status %08x\n",
1083
                      priv->ram->tx_status[tx_consumer_idx]);
1084
    }
1085
 
1086
    if (intstatus & EMAC_INT_SOFTINT)
1087
    {
1088
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_SOFTINT);
1089
    }
1090
 
1091
    if (intstatus & EMAC_INT_WOL)
1092
    {
1093
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_WOL);
1094
        debug1_printf("LPC2XXX_ETH: WOL\n");
1095
    }
1096
 
1097
    //
1098
    // TxFinishedInt
1099
    // Interrupt triggered when all transmit descriptors have been
1100
    // processed i.e. on the transition to the situation where
1101
    // ProduceIndex == ConsumeIndex.
1102
    //
1103
    if (intstatus & EMAC_INT_TXFINISHED)
1104
    {
1105
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_TXFINISHED);
1106
        _eth_drv_tx_done(sc, priv->cur_tx_key, 0);
1107
        debug2_printf("TX finished - key: 0x%08X\n", priv->cur_tx_key);
1108
        priv->tx_busy = false;
1109
    }
1110
 
1111
    //
1112
    // RxDoneInt
1113
    // Interrupt triggered when a receive descriptor has been processed
1114
    // while the Interrupt bit in the Control field of the descriptor was set.
1115
    //
1116
    if (intstatus & (EMAC_INT_RXDONE | EMAC_INT_RXFINISHED))
1117
    {
1118
        cyg_uint32 rx_producer_idx;
1119
        cyg_uint32 rx_consumer_idx;
1120
 
1121
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_RXDONE);
1122
        HAL_WRITE_UINT32(EMAC_INT_CLR, EMAC_INT_RXFINISHED);
1123
        HAL_READ_UINT32(EMAC_RX_PROD_IDX, rx_producer_idx);
1124
        HAL_READ_UINT32(EMAC_RX_CONSUME_IDX, rx_consumer_idx);
1125
        debug2_printf("\n\nRPIdx %d RCIdx %d\n", rx_producer_idx,
1126
                      rx_consumer_idx);
1127
 
1128
        //
1129
        // Iterate through array of rx descriptors to check if the end of
1130
        // frame was received.
1131
        //
1132
        while (priv->iterator != rx_producer_idx)
1133
        {
1134
            priv->total_len += ((priv->ram->rx_status[priv->iterator].info
1135
                                     & DESC_SIZE_MASK) + 1);
1136
            debug2_printf("total_len %d rx_status[%d] = %x rx_len %d\n",
1137
                              priv->total_len, priv->iterator,
1138
                              priv->ram->rx_status[priv->iterator].info,
1139
                              ((priv->ram->rx_status[priv->iterator].info
1140
                              & DESC_SIZE_MASK) + 1));
1141
            if (priv->ram->rx_status[priv->iterator].info & RX_DESC_STATUS_LAST)
1142
            {
1143
                //
1144
                // We found the end of the frame and have something to receive
1145
                // so we can call _eth_drv_recv() now
1146
                //
1147
                if (priv->total_len)
1148
                {
1149
                    _eth_drv_recv(sc, priv->total_len);
1150
                    HAL_READ_UINT32(EMAC_RX_CONSUME_IDX, rx_consumer_idx);
1151
                    debug2_printf("RPIdx %d RCIdx %d Iterator %d\n",
1152
                                      rx_producer_idx, rx_consumer_idx,
1153
                                      priv->iterator);
1154
                    priv->total_len = 0;
1155
                } // if (total_len)
1156
                else
1157
                {
1158
                    //
1159
                    // If we do not call _eth_drv_recv then we need to set the
1160
                    // consumer index here to indicate that the buffers are
1161
                    // free for reception
1162
                    //
1163
                    HAL_WRITE_UINT32(EMAC_RX_CONSUME_IDX, priv->iterator);
1164
                } // if (priv->total_len)
1165
            } // if (priv->ram->rx_status[iterator].info & RX_DESC_STATUS_LAST)
1166
            priv->iterator = (priv->iterator + 1) %
1167
                              CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS;
1168
        }//if (priv->ram->rx_status[priv->iterator].info & RX_DESC_STATUS_LAST)
1169
 
1170
    } // if (intstatus & EMAC_INT_RXDONE)
1171
#ifdef CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
1172
    cyg_drv_interrupt_unmask(priv->intr_vector);
1173
#endif // CYGINT_IO_ETH_INT_SUPPORT_REQUIRED
1174
}
1175
 
1176
 
1177
//===========================================================================
1178
// This routine is called to receive data from the hardware
1179
//===========================================================================
1180
static void lpc2xxx_eth_recv(struct eth_drv_sc *sc,
1181
                             struct eth_drv_sg *sg_list,
1182
                             int    sg_len)
1183
{
1184
    lpc2xxx_eth_priv_t *priv = (lpc2xxx_eth_priv_t *)sc->driver_private;
1185
    int i;
1186
    cyg_uint32 rx_producer_idx;
1187
    cyg_uint32 rx_consumer_idx;
1188
    cyg_uint32 bytes_in_buffer;
1189
    cyg_uint32 bytes_in_list = 0;
1190
    cyg_uint32 bytes_needed_list = 0;
1191
    cyg_uint32 buffer_pos = 0;
1192
    cyg_uint32 total_bytes = 0;
1193
    cyg_uint8 *sg_buf;
1194
    cyg_uint8 *hw_rxbuf;
1195
 
1196
    HAL_READ_UINT32(EMAC_RX_PROD_IDX, rx_producer_idx);
1197
    HAL_READ_UINT32(EMAC_RX_CONSUME_IDX, rx_consumer_idx);
1198
    debug2_printf("lpc2xxx_eth_recv: sg_len %d\n", sg_len);
1199
 
1200
    for(i = 0; i < sg_len; ++i)
1201
    {
1202
        bytes_in_list = 0;
1203
        while (bytes_in_list < sg_list[i].len)
1204
        {
1205
            bytes_needed_list = sg_list[i].len - bytes_in_list;
1206
            debug2_printf("bil: %d sg_list[%d].len: %d bnl: %d\n",
1207
                          bytes_in_list,
1208
                          i,
1209
                          sg_list[i].len,
1210
                          bytes_needed_list);
1211
            bytes_in_buffer = (priv->ram->rx_status[rx_consumer_idx].info &
1212
                               DESC_SIZE_MASK) + 1;
1213
            debug2_printf("bib: %d buffer_pos: %d\n", bytes_in_buffer,
1214
                          buffer_pos);
1215
            bytes_in_buffer -= buffer_pos;
1216
            sg_buf   = (cyg_uint8 *)(sg_list[i].buf);
1217
            hw_rxbuf = (cyg_uint8 *)priv->ram->rx_buf[rx_consumer_idx].bytes;
1218
            if(bytes_needed_list < bytes_in_buffer)
1219
            {
1220
                if(sg_buf)
1221
                {
1222
                    debug2_printf("(1) memcpy(0x%08X, 0x%08X, %d) "
1223
                                  "[0x%08X - 0x%08X]\n",
1224
                                  (cyg_uint32)&sg_buf[bytes_in_list],
1225
                                  (cyg_uint32)&hw_rxbuf[buffer_pos],
1226
                                   bytes_needed_list,
1227
                                  (cyg_uint32)&sg_buf[bytes_in_list],
1228
                                  (cyg_uint32)&sg_buf[bytes_in_list] +
1229
                                  bytes_needed_list);
1230
 
1231
                    memcpy(&sg_buf[bytes_in_list],
1232
                           (cyg_uint8 *)&hw_rxbuf[buffer_pos],
1233
                           bytes_needed_list);
1234
                }
1235
                bytes_in_list += bytes_needed_list;
1236
                buffer_pos += bytes_needed_list;
1237
                total_bytes += bytes_needed_list;
1238
            }
1239
            else
1240
            {
1241
                if(sg_buf)
1242
                {
1243
                    debug2_printf("(2) memcpy(0x%08X, 0x%08X, %d)"
1244
                                  "[0x%08X - 0x%08X]\n",
1245
                                  (cyg_uint32)&sg_buf[bytes_in_list],
1246
                                  (cyg_uint32)&hw_rxbuf[buffer_pos],
1247
                                  bytes_in_buffer,
1248
                                  (cyg_uint32)&sg_buf[bytes_in_list],
1249
                                  (cyg_uint32)&sg_buf[bytes_in_list] +
1250
                                  bytes_in_buffer);
1251
 
1252
                    memcpy(&sg_buf[bytes_in_list],
1253
                           (cyg_uint8 *)&hw_rxbuf[buffer_pos],
1254
                           bytes_in_buffer);
1255
                }
1256
                priv->ram->rx_status[rx_consumer_idx].info = 0;
1257
                priv->ram->rx_status[rx_consumer_idx].hash_crc = 0;
1258
                rx_consumer_idx = (rx_consumer_idx + 1) %
1259
                                  CYGNUM_DEVS_ETH_ARM_LPC2XXX_RX_BUFS;
1260
                HAL_WRITE_UINT32(EMAC_RX_CONSUME_IDX, rx_consumer_idx);
1261
                debug2_printf("RCIdx++ %d\n", rx_consumer_idx);
1262
                bytes_in_list += bytes_in_buffer;
1263
                total_bytes += bytes_in_buffer;
1264
                buffer_pos = 0;
1265
                if (bytes_in_list < sg_list[i].len)
1266
                {
1267
                    debug2_printf("bytes_in_list < sg_list[i].len");
1268
                }
1269
            }
1270
        } // while (bytes_in_list < sg_list[i].len)
1271
    } // for(i = 0; i < sg_len; ++i)
1272
}
1273
 
1274
 
1275
//===========================================================================
1276
// routine called to handle ethernet controller in polled mode
1277
//===========================================================================
1278
static void lpc2xxx_eth_poll(struct eth_drv_sc *sc)
1279
{
1280
    lpc2xxx_eth_deliver(sc);
1281
}
1282
 
1283
 
1284
//===========================================================================
1285
// routine called to handle ethernet controller in polled mode
1286
//===========================================================================
1287
static int lpc2xxx_eth_int_vector(struct eth_drv_sc *sc)
1288
{
1289
   return CYGNUM_HAL_INTERRUPT_ETH;
1290
}
1291
 
1292
 
1293
//===========================================================================
1294
// Ethernet driver instance
1295
//===========================================================================
1296
ETH_DRV_SC(lpc2xxx_sc,
1297
           &lpc2xxx_priv_data,       // Driver specific data
1298
           "eth0",                   // Name for this interface
1299
           lpc2xxx_eth_start,
1300
           lpc2xxx_eth_stop,
1301
           lpc2xxx_eth_control,
1302
           lpc2xxx_eth_can_send,
1303
           lpc2xxx_eth_send,
1304
           lpc2xxx_eth_recv,
1305
           lpc2xxx_eth_deliver,
1306
           lpc2xxx_eth_poll,
1307
           lpc2xxx_eth_int_vector);
1308
 
1309
NETDEVTAB_ENTRY(lpc2xxx_netdev,
1310
                "lpc2xxx",
1311
                lpc2xxx_eth_init,
1312
                &lpc2xxx_sc);
1313
//---------------------------------------------------------------------------
1314
// EOF if_lpc2xxx.c

powered by: WebSVN 2.1.0

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