summaryrefslogtreecommitdiff
path: root/src/drivers/net/rtl8139.c
blob: ad9370a1569fed8f14777c94857fb3694e6e55a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "rtl8139.h"

#include "string.h"
#include "stdio.h"

#define DEBUG

/*
Offset (from IO base) 	Size 	Name
0x08 	8 	MAR0-7
0x30 	4 	RBSTART
0x37 	1 	CMD
0x3C 	2 	IMR
0x3E 	2 	ISR 
*/

static rtl8139_vtable_t const rtl8139_vtable = {
	{
	rtl8139_activate,
	rtl8139_deactivate,
	rtl8139_deinit,
	rtl8139_print_info
	}
};

// registers
#define REG_MAC0 0x00
#define REG_CMD 0x37

// commands for REG_CMD register
#define CR_RESET 0x08

void rtl8139_init( rtl8139_t *rtl8139, pci_device_descriptor_t *descriptor, interrupt_t *interrupt, void *context )
{
	memset( rtl8139, 0, sizeof( rtl8139_t ) );

	network_init( (network_t *)rtl8139, interrupt, context );

	puts( "Initializing driver for Realtek 8139 network card.." );

	for( int i = 0; i < NOF_MAC_REGISTERS; i++ ) {
		port8_init( &rtl8139->MAC_port[i], descriptor->port_base + REG_MAC0 + i );
	}
	port8_init( &rtl8139->CMD_port, descriptor->port_base + REG_CMD );

	// software reset
	port8_write( &rtl8139->CMD_port, CR_RESET );

	// get MAC
	for( int i = 0; i < NOF_MAC_REGISTERS; i++ ) {
		rtl8139->base.mac_address.byte[i] = port8_read( &rtl8139->MAC_port[i] );
	}

#ifdef DEBUG
	char buf[20];
	network_mac_to_string( rtl8139->base.mac_address, buf, 20 );
	printf( "rtl8139 NIC at I/O base 0x%X, interrupt 0x%X, MAC: %s\n",
		descriptor->port_base, descriptor->interrupt, buf
		 );
#endif

	interrupt_handler_init( &rtl8139->interrupt_handler, IRQ_BASE + descriptor->interrupt, interrupt, rtl8139_handle_interrupt, rtl8139 );
	interrupts_register_interrupt_handler( rtl8139->interrupt_handler );

	((driver_t *)rtl8139)->vtable = (driver_vtable_t *)&rtl8139_vtable;
}

void rtl8139_activate( void *obj )
{
	puts( "Activating driver for Realtek 8139 network card.." );

	rtl8139_t *rtl8139 = obj;
}

void rtl8139_deactivate( void *obj )
{
	puts( "Dectivating driver for Realtek 8139 network card.." );

	rtl8139_t *rtl8139 = obj;
}

void rtl8139_deinit( void *obj )
{
	// nothing to do
}

uint32_t rtl8139_handle_interrupt( interrupt_handler_t *handler, uint32_t esp )
{
	rtl8139_t *rtl8139 = (rtl8139_t *)handler->driver;

#ifdef DEBUG
	printf( "RTL INTERRUPT\n" );
#endif

	return esp;
}

void rtl8139_print_info( void *obj )
{
	puts( "Realtek 8139 network card driver" );
}