summaryrefslogtreecommitdiff
path: root/src/drivers/net/ne2k.c
blob: 70ef0efc3954c34aba724d61c70abab9cacea6f5 (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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
ETHERBOOT -  BOOTP/TFTP Bootstrap Program

Author: Martin Renters
  Date: May/94

 This code is based heavily on David Greenman's if_ed.c driver

 Copyright (C) 1993-1994, David Greenman, Martin Renters.
  This software may be used, modified, copied, distributed, and sold, in
  both source and binary form provided that the above copyright and these
  terms are retained. Under no circumstances are the authors responsible for
  the proper functioning of this software, nor do the authors assume any
  responsibility for damages incurred with its use.

Multicast support added by Timothy Legge (timlegge@users.sourceforge.net) 09/28/2003
Relocation support added by Ken Yap (ken_yap@users.sourceforge.net) 28/12/02
3c503 support added by Bill Paul (wpaul@ctr.columbia.edu) on 11/15/94
SMC8416 support added by Bill Paul (wpaul@ctr.columbia.edu) on 12/25/94
3c503 PIO support added by Jim Hague (jim.hague@acm.org) on 2/17/98
RX overrun by Klaus Espenlaub (espenlaub@informatik.uni-ulm.de) on 3/10/99
  parts taken from the Linux 8390 driver (by Donald Becker and Paul Gortmaker)
SMC8416 PIO support added by Andrew Bettison (andrewb@zip.com.au) on 4/3/02
  based on the Linux 8390 driver (by Donald Becker and Paul Gortmaker)

(C) Rudolf Marek <r.marek@assembler.cz> Simplify for RTL8029, Add coreboot glue logic

*/

#include <arch/io.h>
#include <commonlib/bsd/ipchksum.h>
#include <console/ne2k.h>
#include <device/device.h>
#include <device/pci.h>
#include <device/pci_ops.h>

#include "ns8390.h"

#define ETH_ALEN		6	/* Size of Ethernet address */
#define ETH_HLEN		14	/* Size of ethernet header */
#define	ETH_ZLEN		60	/* Minimum packet */
#define	ETH_FRAME_LEN		1514	/* Maximum packet */
#define ETH_DATA_ALIGN		2	/* Amount needed to align the data after an ethernet header */
#define	ETH_MAX_MTU		(ETH_FRAME_LEN-ETH_HLEN)

#define MEM_SIZE MEM_32768
#define TX_START 64
#define RX_START (64 + D8390_TXBUF_SIZE)

static unsigned int get_count(unsigned int eth_nic_base)
{
	unsigned int ret;
	outb(D8390_COMMAND_RD2 + D8390_COMMAND_PS1,
	     eth_nic_base + D8390_P0_COMMAND);

	ret = inb(eth_nic_base + 8 + 0) | (inb(eth_nic_base + 8 + 1) << 8);

	outb(D8390_COMMAND_RD2 + D8390_COMMAND_PS0,
	     eth_nic_base + D8390_P0_COMMAND);
	return ret;
}

static void set_count(unsigned int eth_nic_base, unsigned int what)
{
	outb(D8390_COMMAND_RD2 + D8390_COMMAND_PS1,
	     eth_nic_base + D8390_P0_COMMAND);

	outb(what & 0xff,eth_nic_base + 8);
	outb((what >> 8) & 0xff,eth_nic_base + 8 + 1);

	outb(D8390_COMMAND_RD2 + D8390_COMMAND_PS0,
	     eth_nic_base + D8390_P0_COMMAND);
}

static void eth_pio_write(unsigned char *src, unsigned int dst, unsigned int cnt,
				unsigned int eth_nic_base)
{
	outb(D8390_COMMAND_RD2 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);
	outb(D8390_ISR_RDC, eth_nic_base + D8390_P0_ISR);
	outb(cnt, eth_nic_base + D8390_P0_RBCR0);
	outb(cnt >> 8, eth_nic_base + D8390_P0_RBCR1);
	outb(dst, eth_nic_base + D8390_P0_RSAR0);
	outb(dst >> 8, eth_nic_base + D8390_P0_RSAR1);
	outb(D8390_COMMAND_RD1 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);

	while (cnt--) {
		outb(*(src++), eth_nic_base + NE_ASIC_OFFSET + NE_DATA);
	}
	/*
	#warning "Add timeout"
	*/
	/* wait for operation finish */
	while ((inb(eth_nic_base + D8390_P0_ISR) & D8390_ISR_RDC) != D8390_ISR_RDC)
		;
}

void ne2k_append_data(unsigned char *d, int len, unsigned int base)
{
	eth_pio_write(d, (TX_START << 8) + 42 + get_count(base), len, base);
	set_count(base, get_count(base)+len);
}

static void str2ip(const char *str, unsigned char *ip)
{
	unsigned char c, i = 0;
	int acc = 0;

	do {
		c = str[i];
		if ((c >= '0') && (c <= '9')) {
			acc *= 10;
			acc += (c - '0');
		} else {
			*ip++ = acc;
			acc = 0;
		}
		i++;
	} while (c != '\0');
}

static void str2mac(const char *str, unsigned char *mac)
{
	unsigned char c, i = 0;
	int acc = 0;

	do {
		c = str[i];
		if ((c >= '0') && (c <= '9')) {
			acc *= 16;
			acc += (c - '0');
		} else if ((c >= 'a') && (c <= 'f')) {
			acc *= 16;
			acc += ((c - 'a') + 10);
		} else if ((c >= 'A') && (c <= 'F')) {
			acc *= 16;
			acc += ((c - 'A') + 10);
		} else {
			*mac++ = acc;
			acc = 0;
		}

		i++;
	} while (c != '\0');
}

static void ns8390_tx_header(unsigned int eth_nic_base, int pktlen)
{
	unsigned short chksum;
	unsigned char hdr[] = {
		/* ETHERNET HDR */
		/* destination macaddr */
		0x02, 0x00, 0x00, 0x00, 0x00, 0x01,
		/* source mac */
		0x02, 0x00, 0x00, 0xC0, 0xFF, 0xEE,
		/* ethtype (IP) */
		0x08, 0x00,

		/* IP HDR */
		0x45, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00,
		/* TTL, proto (UDP), chksum_hi, chksum_lo, IP0, IP1, IP2, IP3, */
		0x40, 0x11, 0x0, 0x0, 0x7f, 0x0, 0x0, 0x1,
		/* IP0, IP1, IP2, IP3  */
		0xff, 0xff, 0xff, 0xff,

		/* UDP HDR */
		/* SRC PORT DST PORT (2 bytes each),
		 * ulen, uchksum (must be zero or correct */
		0x1a, 0x0b, 0x1a, 0x0a, 0x00, 0x9, 0x00, 0x00,
	};

	str2mac(CONFIG_CONSOLE_NE2K_DST_MAC,  &hdr[0]);
	str2ip(CONFIG_CONSOLE_NE2K_DST_IP, &hdr[30]);
	str2ip(CONFIG_CONSOLE_NE2K_SRC_IP, &hdr[26]);

	/* zero checksum */
	hdr[24] = 0;
	hdr[25] = 0;

	/* update IP packet len */
	hdr[16] = ((28 + pktlen) >> 8) & 0xff;
	hdr[17] = (28 + pktlen) & 0xff;

	/* update UDP len */
	hdr[38] = (8 + pktlen) >> 8;
	hdr[39] = 8 + pktlen;

	chksum = ipchksum(&hdr[14], 20);

	hdr[25] = chksum >> 8;
	hdr[24] = chksum;
	eth_pio_write(hdr, (TX_START << 8), sizeof(hdr), eth_nic_base);
}

void ne2k_transmit(unsigned int eth_nic_base)
{
	unsigned int pktsize;
	unsigned int len = get_count(eth_nic_base);

	// so place whole header inside chip buffer
	ns8390_tx_header(eth_nic_base, len);

	// commit sending now
	outb(D8390_COMMAND_PS0 | D8390_COMMAND_RD2 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);

	outb(TX_START, eth_nic_base + D8390_P0_TPSR);

	pktsize = 42 + len;
	if (pktsize < 64)
		pktsize = 64;

	outb(pktsize, eth_nic_base + D8390_P0_TBCR0);
	outb(pktsize >> 8, eth_nic_base + D8390_P0_TBCR1);

	outb(D8390_ISR_PTX, eth_nic_base + D8390_P0_ISR);

	outb(D8390_COMMAND_PS0 | D8390_COMMAND_TXP | D8390_COMMAND_RD2 | D8390_COMMAND_STA, eth_nic_base + D8390_P0_COMMAND);

	/* wait for operation finish */
	while ((inb(eth_nic_base + D8390_P0_ISR) & D8390_ISR_PTX) != D8390_ISR_PTX)
		;

	set_count(eth_nic_base, 0);
}

static void ns8390_reset(unsigned int eth_nic_base)
{
	int i;

	outb(D8390_COMMAND_PS0 | D8390_COMMAND_RD2 |
	     D8390_COMMAND_STP, eth_nic_base + D8390_P0_COMMAND);

	outb(0x48, eth_nic_base + D8390_P0_DCR);
	outb(0, eth_nic_base + D8390_P0_RBCR0);
	outb(0, eth_nic_base + D8390_P0_RBCR1);
	outb(0x20, eth_nic_base + D8390_P0_RCR);
	outb(2, eth_nic_base + D8390_P0_TCR);
	outb(TX_START, eth_nic_base + D8390_P0_TPSR);
	outb(RX_START, eth_nic_base + D8390_P0_PSTART);
	outb(MEM_SIZE, eth_nic_base + D8390_P0_PSTOP);
	outb(MEM_SIZE - 1, eth_nic_base + D8390_P0_BOUND);
	outb(0xFF, eth_nic_base + D8390_P0_ISR);
	outb(0, eth_nic_base + D8390_P0_IMR);

	outb(D8390_COMMAND_PS1 |
	     D8390_COMMAND_RD2 | D8390_COMMAND_STP,
	     eth_nic_base + D8390_P0_COMMAND);

	for (i = 0; i < ETH_ALEN; i++)
		outb(0x0C, eth_nic_base + D8390_P1_PAR0 + i);

	for (i = 0; i < ETH_ALEN; i++)
		outb(0xFF, eth_nic_base + D8390_P1_MAR0 + i);

	outb(RX_START, eth_nic_base + D8390_P1_CURR);
	outb(D8390_COMMAND_PS0 |
	     D8390_COMMAND_RD2 | D8390_COMMAND_STA,
	     eth_nic_base + D8390_P0_COMMAND);
	outb(0xFF, eth_nic_base + D8390_P0_ISR);
	outb(0, eth_nic_base + D8390_P0_TCR);
	outb(4, eth_nic_base + D8390_P0_RCR);
	set_count(eth_nic_base, 0);
}

int ne2k_init(unsigned int eth_nic_base)
{
	pci_devfn_t dev;
	unsigned char c;

	if (!ENV_ROMSTAGE_OR_BEFORE)
		return 0;

	/* For this to work, mainboard code must have configured
	   PCI bridges prior to calling console_init(). */
	dev = pci_locate_device(PCI_ID(0x10ec, 0x8029), 0);
	if (dev == PCI_DEV_INVALID)
		return 0;

	pci_s_write_config32(dev, 0x10, eth_nic_base | 1);
	pci_s_write_config8(dev, 0x4, 0x1);

	c = inb(eth_nic_base + NE_ASIC_OFFSET + NE_RESET);
	outb(c, eth_nic_base + NE_ASIC_OFFSET + NE_RESET);

	(void)inb(0x84);

	outb(D8390_COMMAND_STP | D8390_COMMAND_RD2, eth_nic_base + D8390_P0_COMMAND);
	outb(D8390_RCR_MON, eth_nic_base + D8390_P0_RCR);

	outb(D8390_DCR_FT1 | D8390_DCR_LS, eth_nic_base + D8390_P0_DCR);
	outb(MEM_8192, eth_nic_base + D8390_P0_PSTART);
	outb(MEM_16384, eth_nic_base + D8390_P0_PSTOP);

	ns8390_reset(eth_nic_base);
	return 1;
}

static void read_resources(struct device *dev)
{
	struct resource *res;

	res = new_resource(dev, PCI_BASE_ADDRESS_0);
	res->base = CONFIG_CONSOLE_NE2K_IO_PORT;
	res->size = 32;
	res->align = 5;
	res->gran = 5;
	res->limit = res->base + res->size - 1;
	res->flags = IORESOURCE_IO | IORESOURCE_FIXED | IORESOURCE_STORED |
				IORESOURCE_ASSIGNED;
}

static struct device_operations ne2k_ops  = {
	.read_resources   = read_resources,
	.set_resources    = pci_dev_set_resources,
	.enable_resources = pci_dev_enable_resources,
};

static const struct pci_driver ne2k_driver __pci_driver = {
	.ops    = &ne2k_ops,
	.vendor = 0x10ec,
	.device = 0x8029,
};