iPXE
realtek.h
Go to the documentation of this file.
1#ifndef _REALTEK_H
2#define _REALTEK_H
3
4/** @file
5 *
6 * Realtek 10/100/1000 network card driver
7 *
8 */
9
10FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
11FILE_SECBOOT ( PERMITTED );
12
13#include <ipxe/spi.h>
14#include <ipxe/spi_bit.h>
15#include <ipxe/nvo.h>
16#include <ipxe/if_ether.h>
17
18/** PCI memory BAR size */
19#define RTL_BAR_SIZE 0x100
20
21/** A packet descriptor */
23 /** Buffer size */
25 /** Flags */
27 /** Reserved */
29 /** Buffer address */
31} __attribute__ (( packed ));
32
33/** Descriptor buffer size mask */
34#define RTL_DESC_SIZE_MASK 0x3fff
35
36/** Packet descriptor flags */
38 /** Descriptor is owned by NIC */
39 RTL_DESC_OWN = 0x8000,
40 /** End of descriptor ring */
41 RTL_DESC_EOR = 0x4000,
42 /** First segment descriptor */
43 RTL_DESC_FS = 0x2000,
44 /** Last segment descriptor */
45 RTL_DESC_LS = 0x1000,
46 /** Receive error summary */
47 RTL_DESC_RES = 0x0020,
48};
49
50/** Descriptor ring alignment */
51#define RTL_RING_ALIGN 256
52
53/** A legacy mode receive packet header */
55 /** Status */
57 /** Length */
59 /** Packet data */
61} __attribute__ (( packed ));
62
63/** Legacy mode status bits */
65 /** Received OK */
66 RTL_STAT_ROK = 0x0001,
67};
68
69/** ID Register 0 (6 bytes) */
70#define RTL_IDR0 0x00
71
72/** Multicast Register 0 (dword) */
73#define RTL_MAR0 0x08
74
75/** Multicast Register 4 (dword) */
76#define RTL_MAR4 0x0c
77
78/** Transmit Status of Descriptor N (dword, 8139 only) */
79#define RTL_TSD(n) ( 0x10 + 4 * (n) )
80#define RTL_TSD_ERTXTH(x) ( (x) << 16 ) /**< Early TX threshold */
81#define RTL_TSD_ERTXTH_DEFAULT RTL_TSD_ERTXTH ( 256 / 32 )
82#define RTL_TSD_OWN 0x00002000UL /**< Ownership */
83
84/** Transmit Start Address of Descriptor N (dword, 8139 only) */
85#define RTL_TSAD(n) ( 0x20 + 4 * (n) )
86
87/** Transmit Normal Priority Descriptors (qword) */
88#define RTL_TNPDS 0x20
89
90/** Number of transmit descriptors
91 *
92 * This is a hardware limit when using legacy mode.
93 */
94#define RTL_NUM_TX_DESC 4
95
96/** Receive Buffer Start Address (dword, 8139 only) */
97#define RTL_RBSTART 0x30
98
99/** Receive buffer length */
100#define RTL_RXBUF_LEN 8192
101
102/** Receive buffer padding */
103#define RTL_RXBUF_PAD 2038 /* Allow space for WRAP */
104
105/** Receive buffer alignment */
106#define RTL_RXBUF_ALIGN 16
107
108/** Command Register (byte) */
109#define RTL_CR 0x37
110#define RTL_CR_RST 0x10 /**< Reset */
111#define RTL_CR_RE 0x08 /**< Receiver Enable */
112#define RTL_CR_TE 0x04 /**< Transmit Enable */
113#define RTL_CR_BUFE 0x01 /**< Receive buffer empty */
114
115/** Maximum time to wait for a reset, in milliseconds */
116#define RTL_RESET_MAX_WAIT_MS 100
117
118/** Current Address of Packet Read (word, 8139 only) */
119#define RTL_CAPR 0x38
120
121/** Transmit Priority Polling Register (byte, 8169 only) */
122#define RTL_TPPOLL_8169 0x38
123#define RTL_TPPOLL_NPQ 0x40 /**< Normal Priority Queue Polling */
124
125/** Interrupt Mask Register (word) */
126#define RTL_IMR 0x3c
127#define RTL_IRQ_PUN_LINKCHG 0x0020 /**< Packet underrun / link change */
128#define RTL_IRQ_TER 0x0008 /**< Transmit error */
129#define RTL_IRQ_TOK 0x0004 /**< Transmit OK */
130#define RTL_IRQ_RER 0x0002 /**< Receive error */
131#define RTL_IRQ_ROK 0x0001 /**< Receive OK */
132
133/** Interrupt Status Register (word) */
134#define RTL_ISR 0x3e
135
136/** Transmit (Tx) Configuration Register (dword) */
137#define RTL_TCR 0x40
138#define RTL_TCR_MXDMA(x) ( (x) << 8 ) /**< Max DMA burst size */
139#define RTL_TCR_MXDMA_MASK RTL_TCR_MXDMA ( 0x7 )
140#define RTL_TCR_MXDMA_DEFAULT RTL_TCR_MXDMA ( 0x7 /* Unlimited */ )
141
142/** Receive (Rx) Configuration Register (dword) */
143#define RTL_RCR 0x44
144#define RTL_RCR_STOP_WORKING 0x01000000UL /**< Here be dragons */
145#define RTL_RCR_RXFTH(x) ( (x) << 13 ) /**< Receive FIFO threshold */
146#define RTL_RCR_RXFTH_MASK RTL_RCR_RXFTH ( 0x7 )
147#define RTL_RCR_RXFTH_DEFAULT RTL_RCR_RXFTH ( 0x7 /* Whole packet */ )
148#define RTL_RCR_RBLEN(x) ( (x) << 11 ) /**< Receive buffer length */
149#define RTL_RCR_RBLEN_MASK RTL_RCR_RBLEN ( 0x3 )
150#define RTL_RCR_RBLEN_DEFAULT RTL_RCR_RBLEN ( 0 /* 8kB */ )
151#define RTL_RCR_MXDMA(x) ( (x) << 8 ) /**< Max DMA burst size */
152#define RTL_RCR_MXDMA_MASK RTL_RCR_MXDMA ( 0x7 )
153#define RTL_RCR_MXDMA_DEFAULT RTL_RCR_MXDMA ( 0x7 /* Unlimited */ )
154#define RTL_RCR_WRAP 0x00000080UL /**< Overrun receive buffer */
155#define RTL_RCR_9356SEL 0x00000040UL /**< EEPROM is a 93C56 */
156#define RTL_RCR_AB 0x00000008UL /**< Accept broadcast packets */
157#define RTL_RCR_AM 0x00000004UL /**< Accept multicast packets */
158#define RTL_RCR_APM 0x00000002UL /**< Accept physical match */
159#define RTL_RCR_AAP 0x00000001UL /**< Accept all packets */
160
161/** 93C46 (93C56) Command Register (byte) */
162#define RTL_9346CR 0x50
163#define RTL_9346CR_EEM(x) ( (x) << 6 ) /**< Mode select */
164#define RTL_9346CR_EEM_EEPROM RTL_9346CR_EEM ( 0x2 ) /**< EEPROM mode */
165#define RTL_9346CR_EEM_NORMAL RTL_9346CR_EEM ( 0x0 ) /**< Normal mode */
166#define RTL_9346CR_EECS 0x08 /**< Chip select */
167#define RTL_9346CR_EESK 0x04 /**< Clock */
168#define RTL_9346CR_EEDI 0x02 /**< Data in */
169#define RTL_9346CR_EEDO 0x01 /**< Data out */
170
171/** Word offset of ID code word within EEPROM */
172#define RTL_EEPROM_ID ( 0x00 / 2 )
173
174/** EEPROM code word magic value */
175#define RTL_EEPROM_ID_MAGIC 0x8129
176
177/** Word offset of MAC address within EEPROM */
178#define RTL_EEPROM_MAC ( 0x0e / 2 )
179
180/** Word offset of VPD / non-volatile options within EEPROM */
181#define RTL_EEPROM_VPD ( 0x40 / 2 )
182
183/** Length of VPD / non-volatile options within EEPROM */
184#define RTL_EEPROM_VPD_LEN 0x40
185
186/** Configuration Register 1 (byte) */
187#define RTL_CONFIG1 0x52
188#define RTL_CONFIG1_VPD 0x02 /**< Vital Product Data enabled */
189
190/** Media Status Register (byte, 8139 only) */
191#define RTL_MSR 0x58
192#define RTL_MSR_TXFCE 0x80 /**< TX flow control enabled */
193#define RTL_MSR_RXFCE 0x40 /**< RX flow control enabled */
194#define RTL_MSR_AUX_STATUS 0x10 /**< Aux power present */
195#define RTL_MSR_SPEED_10 0x08 /**< 10Mbps */
196#define RTL_MSR_LINKB 0x04 /**< Inverse of link status */
197#define RTL_MSR_TXPF 0x02 /**< TX pause flag */
198#define RTL_MSR_RXPF 0x01 /**< RX pause flag */
199
200/** PHY Access Register (dword, 8169 only) */
201#define RTL_PHYAR 0x60
202#define RTL_PHYAR_FLAG 0x80000000UL /**< Read/write flag */
203
204/** Construct PHY Access Register value */
205#define RTL_PHYAR_VALUE( flag, reg, data ) ( (flag) | ( (reg) << 16 ) | (data) )
206
207/** Extract PHY Access Register data */
208#define RTL_PHYAR_DATA( value ) ( (value) & 0xffff )
209
210/** Maximum time to wait for PHY access, in microseconds */
211#define RTL_MII_MAX_WAIT_US 500
212
213/** PHY (GMII, MII, or TBI) Status Register (byte, 8169 only) */
214#define RTL_PHYSTATUS 0x6c
215#define RTL_PHYSTATUS_ENTBI 0x80 /**< TBI / GMII mode */
216#define RTL_PHYSTATUS_TXFLOW 0x40 /**< TX flow control enabled */
217#define RTL_PHYSTATUS_RXFLOW 0x20 /**< RX flow control enabled */
218#define RTL_PHYSTATUS_1000MF 0x10 /**< 1000Mbps full-duplex */
219#define RTL_PHYSTATUS_100M 0x08 /**< 100Mbps */
220#define RTL_PHYSTATUS_10M 0x04 /**< 10Mbps */
221#define RTL_PHYSTATUS_LINKSTS 0x02 /**< Link ok */
222#define RTL_PHYSTATUS_FULLDUP 0x01 /**< Full duplex */
223
224/** Transmit Priority Polling Register (byte, 8139C+ only) */
225#define RTL_TPPOLL_8139CP 0xd9
226
227/** RX Packet Maximum Size Register (word) */
228#define RTL_RMS 0xda
229
230/** C+ Command Register (word) */
231#define RTL_CPCR 0xe0
232#define RTL_CPCR_VLAN 0x0040 /**< VLAN tag stripping enable */
233#define RTL_CPCR_DAC 0x0010 /**< PCI Dual Address Cycle enable */
234#define RTL_CPCR_MULRW 0x0008 /**< PCI Multiple Read/Write enable */
235#define RTL_CPCR_CPRX 0x0002 /**< C+ receive enable */
236#define RTL_CPCR_CPTX 0x0001 /**< C+ transmit enable */
237
238/** Receive Descriptor Start Address Register (qword) */
239#define RTL_RDSAR 0xe4
240
241/** Number of receive descriptors */
242#define RTL_NUM_RX_DESC 4
243
244/** Receive buffer length */
245#define RTL_RX_MAX_LEN \
246 ( ETH_FRAME_LEN + 4 /* VLAN */ + 4 /* CRC */ + 4 /* extra space */ )
247
248/** A Realtek descriptor ring */
250 /** Descriptors */
252 /** Descriptor ring DMA mapping */
254 /** Producer index */
255 unsigned int prod;
256 /** Consumer index */
257 unsigned int cons;
258
259 /** Descriptor start address register */
260 unsigned int reg;
261 /** Length (in bytes) */
262 size_t len;
263};
264
265/**
266 * Initialise descriptor ring
267 *
268 * @v ring Descriptor ring
269 * @v count Number of descriptors
270 * @v reg Descriptor start address register
271 */
272static inline __attribute__ (( always_inline)) void
273realtek_init_ring ( struct realtek_ring *ring, unsigned int count,
274 unsigned int reg ) {
275 ring->len = ( count * sizeof ( ring->desc[0] ) );
276 ring->reg = reg;
277}
278
279/** Receive buffer (legacy mode *) */
281 /** Buffer */
282 void *data;
283 /** Buffer DMA mapping */
285 /** Offset within buffer */
286 unsigned int offset;
287};
288
289/** A Realtek network card */
291 /** Registers */
292 void *regs;
293 /** DMA device */
295 /** SPI bit-bashing interface */
297 /** EEPROM */
299 /** Non-volatile options */
301 /** MII interface */
303 /** MII device */
305
306 /** Legacy datapath mode */
308 /** PHYAR and PHYSTATUS registers are present */
310 /** TPPoll register offset */
311 unsigned int tppoll;
312
313 /** Transmit descriptor ring */
315 /** Receive descriptor ring */
317 /** Receive I/O buffers */
319 /** Receive buffer (legacy mode) */
321};
322
323#endif /* _REALTEK_H */
unsigned short uint16_t
Definition stdint.h:11
unsigned int uint32_t
Definition stdint.h:12
unsigned long long uint64_t
Definition stdint.h:13
unsigned char uint8_t
Definition stdint.h:10
static unsigned int count
Number of entries.
Definition dwmac.h:220
#define FILE_LICENCE(_licence)
Declare a particular licence as applying to a file.
Definition compiler.h:896
#define FILE_SECBOOT(_status)
Declare a file's UEFI Secure Boot permission status.
Definition compiler.h:926
#define __attribute__(x)
Definition compiler.h:10
static unsigned int unsigned int reg
Definition myson.h:162
Non-volatile stored options.
static void realtek_init_ring(struct realtek_ring *ring, unsigned int count, unsigned int reg)
Initialise descriptor ring.
Definition realtek.h:273
realtek_descriptor_flags
Packet descriptor flags.
Definition realtek.h:37
@ RTL_DESC_OWN
Descriptor is owned by NIC.
Definition realtek.h:39
@ RTL_DESC_RES
Receive error summary.
Definition realtek.h:47
@ RTL_DESC_LS
Last segment descriptor.
Definition realtek.h:45
@ RTL_DESC_EOR
End of descriptor ring.
Definition realtek.h:41
@ RTL_DESC_FS
First segment descriptor.
Definition realtek.h:43
#define RTL_NUM_RX_DESC
Number of receive descriptors.
Definition realtek.h:242
realtek_legacy_status
Legacy mode status bits.
Definition realtek.h:64
@ RTL_STAT_ROK
Received OK.
Definition realtek.h:66
SPI interface.
SPI bit-bashing interface.
A DMA-capable device.
Definition dma.h:48
A DMA mapping.
Definition dma.h:33
A persistent I/O buffer.
Definition iobuf.h:38
An MII device.
Definition mii.h:50
An MII interface.
Definition mii.h:44
A block of non-volatile stored options.
Definition nvo.h:23
A packet descriptor.
Definition realtek.h:22
uint16_t flags
Flags.
Definition realtek.h:26
uint32_t reserved
Reserved.
Definition realtek.h:28
uint16_t length
Buffer size.
Definition realtek.h:24
uint64_t address
Buffer address.
Definition realtek.h:30
A legacy mode receive packet header.
Definition realtek.h:54
uint8_t data[0]
Packet data.
Definition realtek.h:60
uint16_t length
Length.
Definition realtek.h:58
uint16_t status
Status.
Definition realtek.h:56
A Realtek network card.
Definition realtek.h:290
struct realtek_rx_buffer rxbuf
Receive buffer (legacy mode)
Definition realtek.h:320
int have_phy_regs
PHYAR and PHYSTATUS registers are present.
Definition realtek.h:309
int legacy
Legacy datapath mode.
Definition realtek.h:307
struct io_buffer * rx_iobuf[RTL_NUM_RX_DESC]
Receive I/O buffers.
Definition realtek.h:318
struct mii_device mii
MII device.
Definition realtek.h:304
struct nvo_block nvo
Non-volatile options.
Definition realtek.h:300
struct spi_device eeprom
EEPROM.
Definition realtek.h:298
struct dma_device * dma
DMA device.
Definition realtek.h:294
unsigned int tppoll
TPPoll register offset.
Definition realtek.h:311
struct spi_bit_basher spibit
SPI bit-bashing interface.
Definition realtek.h:296
struct realtek_ring tx
Transmit descriptor ring.
Definition realtek.h:314
struct realtek_ring rx
Receive descriptor ring.
Definition realtek.h:316
void * regs
Registers.
Definition realtek.h:292
struct mii_interface mdio
MII interface.
Definition realtek.h:302
A Realtek descriptor ring.
Definition realtek.h:249
unsigned int prod
Producer index.
Definition realtek.h:255
unsigned int cons
Consumer index.
Definition realtek.h:257
size_t len
Length (in bytes)
Definition realtek.h:262
struct dma_mapping map
Descriptor ring DMA mapping.
Definition realtek.h:253
unsigned int reg
Descriptor start address register.
Definition realtek.h:260
struct realtek_descriptor * desc
Descriptors.
Definition realtek.h:251
Receive buffer (legacy mode *)
Definition realtek.h:280
struct dma_mapping map
Buffer DMA mapping.
Definition realtek.h:284
void * data
Buffer.
Definition realtek.h:282
unsigned int offset
Offset within buffer.
Definition realtek.h:286
A bit-bashing SPI bus.
Definition spi_bit.h:17
An SPI device.
Definition spi.h:87