Files
opencoreC/hal/include/e1000.h
T

108 lines
2.6 KiB
C

#pragma once
#include "stdint.h"
#include "stdbool.h"
#include "stddef.h"
#include "pci.h"
#ifdef __cplusplus
extern "C" {
#endif
#define E1000_NUM_RX_DESC 16
#define E1000_NUM_TX_DESC 16
#define E1000_RX_BUF_SIZE 2048
#define E1000_TX_BUF_SIZE 2048
// E1000 MMIO Register Offsets
#define E1000_REG_CTRL 0x0000
#define E1000_REG_STATUS 0x0008
#define E1000_REG_EEPROM 0x0014
#define E1000_REG_ICR 0x00C0
#define E1000_REG_IMS 0x00D0
#define E1000_REG_RCTL 0x0100
#define E1000_REG_RDBAL 0x2800
#define E1000_REG_RDBAH 0x2804
#define E1000_REG_RDLEN 0x2808
#define E1000_REG_RDH 0x2810
#define E1000_REG_RDT 0x2818
#define E1000_REG_TCTL 0x0400
#define E1000_REG_TDBAL 0x3800
#define E1000_REG_TDBAH 0x3804
#define E1000_REG_TDLEN 0x3808
#define E1000_REG_TDH 0x3810
#define E1000_REG_TDT 0x3818
#define E1000_REG_RAL 0x5400
#define E1000_REG_RAH 0x5404
// RCTL bits
#define E1000_RCTL_EN (1 << 1)
#define E1000_RCTL_SBP (1 << 2)
#define E1000_RCTL_UPE (1 << 3)
#define E1000_RCTL_MPE (1 << 4)
#define E1000_RCTL_BAM (1 << 15)
#define E1000_RCTL_BSIZE_2K (0 << 16)
#define E1000_RCTL_SECRC (1 << 26)
// TCTL bits
#define E1000_TCTL_EN (1 << 1)
#define E1000_TCTL_PSP (1 << 3)
#define E1000_TCTL_CT_SHIFT 4
#define E1000_TCTL_COLD_SHIFT 12
// TX Command bits
#define E1000_TXD_CMD_EOP (1 << 0)
#define E1000_TXD_CMD_IFCS (1 << 1)
#define E1000_TXD_CMD_RS (1 << 3)
// RX Status bits
#define E1000_RXD_STAT_DD (1 << 0)
#define E1000_RXD_STAT_EOP (1 << 1)
// E1000 RX Descriptor
typedef struct __attribute__((packed)) e1000_rx_desc {
uint64_t addr;
uint16_t length;
uint16_t checksum;
uint8_t status;
uint8_t errors;
uint16_t special;
} e1000_rx_desc_t;
// E1000 TX Descriptor
typedef struct __attribute__((packed)) e1000_tx_desc {
uint64_t addr;
uint16_t length;
uint8_t cso;
uint8_t cmd;
uint8_t status;
uint8_t css;
uint16_t special;
} e1000_tx_desc_t;
typedef struct e1000_device {
uint8_t bus;
uint8_t slot;
uint8_t func;
uint16_t device_id;
uint64_t mmio_base;
uint8_t mac[6];
uint32_t rx_cur;
uint32_t tx_cur;
uint64_t rx_packets;
uint64_t rx_bytes;
uint64_t tx_packets;
uint64_t tx_bytes;
bool initialized;
} e1000_device_t;
extern e1000_device_t g_e1000;
int e1000_detect_and_init(void);
int e1000_send_packet(const void *packet, size_t length);
int e1000_receive_packet(void *buffer, size_t max_length);
#ifdef __cplusplus
}
#endif