1 //==========================================================================
5 // National Semiconductor DP83902a ethernet chip
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 // Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
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 version.
17 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
18 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
19 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 // You should have received a copy of the GNU General Public License along
23 // with eCos; if not, write to the Free Software Foundation, Inc.,
24 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26 // As a special exception, if other files instantiate templates or use macros
27 // or inline functions from this file, or you compile this file and link it
28 // with other works to produce a work based on this file, this file does not
29 // by itself cause the resulting work to be covered by the GNU General Public
30 // License. However the source code for this file must still be made available
31 // in accordance with section (3) of the GNU General Public License.
33 // This exception does not invalidate any other reasons why a work based on
34 // this file might be covered by the GNU General Public License.
36 // Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
37 // at http://sources.redhat.com/ecos/ecos-license/
38 // -------------------------------------------
39 //####ECOSGPLCOPYRIGHTEND####
40 //####BSDCOPYRIGHTBEGIN####
42 // -------------------------------------------
44 // Portions of this software may have been derived from OpenBSD or other sources,
45 // and are covered by the appropriate copyright disclaimers included herein.
47 // -------------------------------------------
49 //####BSDCOPYRIGHTEND####
50 //==========================================================================
51 //#####DESCRIPTIONBEGIN####
54 // Contributors: gthomas, jskov
59 //####DESCRIPTIONEND####
61 //==========================================================================
63 #include <cyg/hal/hal_io.h>
64 #include <pkgconf/devs_eth_ns_dp83902a.h>
67 #include CYGDAT_DEVS_ETH_NS_DP83902A_INL
70 // ------------------------------------------------------------------------
74 // 0 disables all debug output
75 // 1 for process debug output
76 // 2 for added data IO output: get_reg, put_reg
77 // 4 for packet allocation/free output
78 // 8 for only startup status, so we can tell we're installed OK
82 #define DEBUG_FUNCTION() do { diag_printf("%s\n", __FUNCTION__); } while (0)
83 #define DEBUG_LINE() do { diag_printf("%d\n", __LINE__); } while (0)
85 #define DEBUG_FUNCTION() do {} while(0)
86 #define DEBUG_LINE() do {} while(0)
89 // ------------------------------------------------------------------------
90 // MAcros for keeping track of statistics
91 #if defined(ETH_DRV_GET_IF_STATS) || defined (ETH_DRV_GET_IF_STATS_UD)
92 #define KEEP_STATISTICS
95 #ifdef KEEP_STATISTICS
96 #define INCR_STAT( _x_ ) (dp->stats. _x_ ++)
98 #define INCR_STAT( _x_ ) CYG_EMPTY_STATEMENT
101 // ------------------------------------------------------------------------
102 // Private driver structure
103 typedef struct dp83902a_priv_data {
107 int tx_next; // First free Tx page
108 int tx_int; // Expecting interrupt from this buffer
109 int rx_next; // First free Rx page
110 int tx1, tx2; // Page numbers for Tx buffers
111 unsigned long tx1_key, tx2_key; // Used to ack when packet sent
112 int tx1_len, tx2_len;
113 bool tx_started, running, hardwired_esa;
114 struct cyg_netdevtab_entry *tab;
116 cyg_vector_t interrupt; // Interrupt vector used by controller
117 cyg_handle_t interrupt_handle;
118 cyg_interrupt interrupt_object;
122 volatile int cr_lock;
123 volatile int cr_owner;
126 int tx_buf1, tx_buf2;
127 int rx_buf_start, rx_buf_end;
128 } dp83902a_priv_data_t;
130 // ------------------------------------------------------------------------
131 // Macros for accessing structure elements
133 #define _SU8( _base_, _offset_) \
134 *((volatile cyg_uint8 *)((CYG_ADDRWORD)_base_+(_offset_)))
135 #define _SU16( _base_, _offset_) \
136 *((volatile cyg_uint16 *)((CYG_ADDRWORD)_base_+(_offset_)))
137 #define _SU32( _base_, _offset_) \
138 *((volatile cyg_uint32 *)((CYG_ADDRWORD)_base_+(_offset_)))
140 #define _SI8( _base_, _offset_) \
141 *((volatile cyg_int8 *)((CYG_ADDRWORD)_base_+(_offset_)))
142 #define _SI16( _base_, _offset_) \
143 *((volatile cyg_int16 *)((CYG_ADDRWORD)_base_+(_offset_)))
144 #define _SI32( _base_, _offset_) \
145 *((volatile cyg_int32 *)((CYG_ADDRWORD)_base_+(_offset_)))
147 // ------------------------------------------------------------------------
148 // Macros for accessing DP registers
149 // These can be overridden by the platform header
152 # define DP_IN(_b_, _o_, _d_) HAL_READ_UINT8 ((cyg_addrword_t)(_b_)+(_o_), (_d_))
153 # define DP_OUT(_b_, _o_, _d_) HAL_WRITE_UINT8((cyg_addrword_t)(_b_)+(_o_), (_d_))
157 # ifdef CYGHWR_NS_DP83902A_PLF_16BIT_DATA
159 # define DP_IN_DATA(_b_, _d_) \
162 HAL_READ_UINT16 ((cyg_addrword_t)(_b_), _t); \
164 (_d_) = ((_t >> 8) & 0xff) | ((_t & 0xff) << 8); \
167 # define DP_OUT_DATA(_b_, _d_) \
171 (_t) = (((_t) >> 8) & 0xff) | ((_t & 0xff) << 8); \
172 HAL_WRITE_UINT16((cyg_addrword_t)(_b_), _t); \
176 # define DP_IN_DATA(_b_, _d_) HAL_READ_UINT16 ((cyg_addrword_t)(_b_), (_d_))
177 # define DP_OUT_DATA(_b_, _d_) HAL_WRITE_UINT16 ((cyg_addrword_t)(_b_), (_d_))
180 # define DP_IN_DATA(_b_, _d_) HAL_READ_UINT8 ((cyg_addrword_t)(_b_), (_d_))
181 # define DP_OUT_DATA(_b_, _d_) HAL_WRITE_UINT8 ((cyg_addrword_t)(_b_), (_d_))
185 // ------------------------------------------------------------------------
186 // Macros allowing platform to customize some of the driver details
188 #ifndef CYGHWR_NS_DP83902A_PLF_RESET
189 # define CYGHWR_NS_DP83902A_PLF_RESET(_b_) do { } while (0)
192 #ifndef CYGHWR_NS_DP83902A_PLF_INT_CLEAR
193 # define CYGHWR_NS_DP83902A_PLF_INT_CLEAR(_dp_)
196 #ifndef CYGHWR_NS_DP83902A_PLF_INIT
197 #define CYGHWR_NS_DP83902A_PLF_INIT(dp) do { } while (0)
200 // ------------------------------------------------------------------------
201 // Hack that should go away, probably
204 if (++dp->cr_lock > 1) { \
205 diag_printf("*** Race on CR %s:%d owner %d\n", \
206 __FUNCTION__, __LINE__, dp->cr_owner); \
209 dp->cr_owner = __LINE__; \
215 // ------------------------------------------------------------------------
216 // Some forward declarations
217 static void dp83902a_poll(struct eth_drv_sc *sc);
219 // ------------------------------------------------------------------------
223 #define DP_CLDA0 0x01
224 #define DP_PSTART 0x01 // write
225 #define DP_CLDA1 0x02
226 #define DP_PSTOP 0x02 // write
227 #define DP_BNDRY 0x03
229 #define DP_TPSR 0x04 // write
231 #define DP_TBCL 0x05 // write
233 #define DP_TBCH 0x06 // write
235 #define DP_CRDA0 0x08
236 #define DP_RSAL 0x08 // write
237 #define DP_CRDA1 0x09
238 #define DP_RSAH 0x09 // write
239 #define DP_RBCL 0x0a // write
240 #define DP_RBCH 0x0b // write
242 #define DP_RCR 0x0c // write
244 #define DP_TCR 0x0d // write
246 #define DP_DCR 0x0e // write
247 #define DP_MISSED 0x0f
248 #define DP_IMR 0x0f // write
249 #define DP_DATAPORT 0x10 // "eprom" data port
251 #define DP_P1_CR 0x00
252 #define DP_P1_PAR0 0x01
253 #define DP_P1_PAR1 0x02
254 #define DP_P1_PAR2 0x03
255 #define DP_P1_PAR3 0x04
256 #define DP_P1_PAR4 0x05
257 #define DP_P1_PAR5 0x06
258 #define DP_P1_CURP 0x07
259 #define DP_P1_MAR0 0x08
260 #define DP_P1_MAR1 0x09
261 #define DP_P1_MAR2 0x0a
262 #define DP_P1_MAR3 0x0b
263 #define DP_P1_MAR4 0x0c
264 #define DP_P1_MAR5 0x0d
265 #define DP_P1_MAR6 0x0e
266 #define DP_P1_MAR7 0x0f
268 #define DP_P2_CR 0x00
269 #define DP_P2_PSTART 0x01
270 #define DP_P2_CLDA0 0x01 // write
271 #define DP_P2_PSTOP 0x02
272 #define DP_P2_CLDA1 0x02 // write
273 #define DP_P2_RNPP 0x03
274 #define DP_P2_TPSR 0x04
275 #define DP_P2_LNPP 0x05
276 #define DP_P2_ACH 0x06
277 #define DP_P2_ACL 0x07
278 #define DP_P2_RCR 0x0c
279 #define DP_P2_TCR 0x0d
280 #define DP_P2_DCR 0x0e
281 #define DP_P2_IMR 0x0f
283 // Command register - common to all pages
285 #define DP_CR_STOP 0x01 // Stop: software reset
286 #define DP_CR_START 0x02 // Start: initialize device
287 #define DP_CR_TXPKT 0x04 // Transmit packet
288 #define DP_CR_RDMA 0x08 // Read DMA (recv data from device)
289 #define DP_CR_WDMA 0x10 // Write DMA (send data to device)
290 #define DP_CR_SEND 0x18 // Send packet
291 #define DP_CR_NODMA 0x20 // Remote (or no) DMA
292 #define DP_CR_PAGE0 0x00 // Page select
293 #define DP_CR_PAGE1 0x40
294 #define DP_CR_PAGE2 0x80
295 #define DP_CR_PAGEMSK 0x3F // Used to mask out page bits
297 // Data configuration register
299 #define DP_DCR_WTS 0x01 // 1=16 bit word transfers
300 #define DP_DCR_BOS 0x02 // 1=Little Endian
301 #define DP_DCR_LAS 0x04 // 1=Single 32 bit DMA mode
302 #define DP_DCR_LS 0x08 // 1=normal mode, 0=loopback
303 #define DP_DCR_ARM 0x10 // 0=no send command (program I/O)
304 #define DP_DCR_FIFO_1 0x00 // FIFO threshold
305 #define DP_DCR_FIFO_2 0x20
306 #define DP_DCR_FIFO_4 0x40
307 #define DP_DCR_FIFO_6 0x60
309 #ifdef CYGHWR_NS_DP83902A_PLF_16BIT_DATA
311 # define DP_DCR_INIT (DP_DCR_BOS|DP_DCR_WTS|DP_DCR_LS|DP_DCR_FIFO_4)
313 # define DP_DCR_INIT (DP_DCR_WTS|DP_DCR_LS|DP_DCR_FIFO_4)
316 # define DP_DCR_INIT (DP_DCR_LS|DP_DCR_FIFO_4)
319 // Interrupt status register
321 #define DP_ISR_RxP 0x01 // Packet received
322 #define DP_ISR_TxP 0x02 // Packet transmitted
323 #define DP_ISR_RxE 0x04 // Receive error
324 #define DP_ISR_TxE 0x08 // Transmit error
325 #define DP_ISR_OFLW 0x10 // Receive overflow
326 #define DP_ISR_CNT 0x20 // Tally counters need emptying
327 #define DP_ISR_RDC 0x40 // Remote DMA complete
328 #define DP_ISR_RESET 0x80 // Device has reset (shutdown, error)
330 // Interrupt mask register
332 #define DP_IMR_RxP 0x01 // Packet received
333 #define DP_IMR_TxP 0x02 // Packet transmitted
334 #define DP_IMR_RxE 0x04 // Receive error
335 #define DP_IMR_TxE 0x08 // Transmit error
336 #define DP_IMR_OFLW 0x10 // Receive overflow
337 #define DP_IMR_CNT 0x20 // Tall counters need emptying
338 #define DP_IMR_RDC 0x40 // Remote DMA complete
340 #define DP_IMR_All 0x3F // Everything but remote DMA
342 // Receiver control register
344 #define DP_RCR_SEP 0x01 // Save bad(error) packets
345 #define DP_RCR_AR 0x02 // Accept runt packets
346 #define DP_RCR_AB 0x04 // Accept broadcast packets
347 #define DP_RCR_AM 0x08 // Accept multicast packets
348 #define DP_RCR_PROM 0x10 // Promiscuous mode
349 #define DP_RCR_MON 0x20 // Monitor mode - 1=accept no packets
351 // Receiver status register
353 #define DP_RSR_RxP 0x01 // Packet received
354 #define DP_RSR_CRC 0x02 // CRC error
355 #define DP_RSR_FRAME 0x04 // Framing error
356 #define DP_RSR_FO 0x08 // FIFO overrun
357 #define DP_RSR_MISS 0x10 // Missed packet
358 #define DP_RSR_PHY 0x20 // 0=pad match, 1=mad match
359 #define DP_RSR_DIS 0x40 // Receiver disabled
360 #define DP_RSR_DFR 0x80 // Receiver processing deferred
362 // Transmitter control register
364 #define DP_TCR_NOCRC 0x01 // 1=inhibit CRC
365 #define DP_TCR_NORMAL 0x00 // Normal transmitter operation
366 #define DP_TCR_LOCAL 0x02 // Internal NIC loopback
367 #define DP_TCR_INLOOP 0x04 // Full internal loopback
368 #define DP_TCR_OUTLOOP 0x08 // External loopback
369 #define DP_TCR_ATD 0x10 // Auto transmit disable
370 #define DP_TCR_OFFSET 0x20 // Collision offset adjust
372 // Transmit status register
374 #define DP_TSR_TxP 0x01 // Packet transmitted
375 #define DP_TSR_COL 0x04 // Collision (at least one)
376 #define DP_TSR_ABT 0x08 // Aborted because of too many collisions
377 #define DP_TSR_CRS 0x10 // Lost carrier
378 #define DP_TSR_FU 0x20 // FIFO underrun
379 #define DP_TSR_CDH 0x40 // Collision Detect Heartbeat
380 #define DP_TSR_OWC 0x80 // Collision outside normal window
382 #define IEEE_8023_MAX_FRAME 1518 // Largest possible ethernet frame
383 #define IEEE_8023_MIN_FRAME 64 // Smallest possible ethernet frame