Переглянути джерело

Imported from Etherboot 5.4 tree

tags/v0.9.3
Michael Brown 19 роки тому
джерело
коміт
e14c739d2d
3 змінених файлів з 1555 додано та 0 видалено
  1. 681
    0
      src/drivers/net/amd8111e.c
  2. 629
    0
      src/drivers/net/amd8111e.h
  3. 245
    0
      src/proto/fsp.c

+ 681
- 0
src/drivers/net/amd8111e.c Переглянути файл

@@ -0,0 +1,681 @@
1
+/* Advanced  Micro Devices Inc. AMD8111E Linux Network Driver 
2
+ * Copyright (C) 2004 Advanced Micro Devices 
3
+ * Copyright (C) 2005 Liu Tao <liutao1980@gmail.com> [etherboot port]
4
+ * 
5
+ * Copyright 2001,2002 Jeff Garzik <jgarzik@mandrakesoft.com> [ 8139cp.c,tg3.c ]
6
+ * Copyright (C) 2001, 2002 David S. Miller (davem@redhat.com)[ tg3.c]
7
+ * Copyright 1996-1999 Thomas Bogendoerfer [ pcnet32.c ]
8
+ * Derived from the lance driver written 1993,1994,1995 by Donald Becker.
9
+ * Copyright 1993 United States Government as represented by the
10
+ *	Director, National Security Agency.[ pcnet32.c ]
11
+ * Carsten Langgaard, carstenl@mips.com [ pcnet32.c ]
12
+ * Copyright (C) 2000 MIPS Technologies, Inc.  All rights reserved.
13
+ *
14
+ * 
15
+ * This program is free software; you can redistribute it and/or modify
16
+ * it under the terms of the GNU General Public License as published by
17
+ * the Free Software Foundation; either version 2 of the License, or
18
+ * (at your option) any later version.
19
+ *
20
+ * This program is distributed in the hope that it will be useful,
21
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23
+ * GNU General Public License for more details.
24
+ *
25
+ * You should have received a copy of the GNU General Public License
26
+ * along with this program; if not, write to the Free Software
27
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 
28
+ * USA
29
+ */
30
+
31
+#include "etherboot.h"
32
+#include "nic.h"
33
+#include "mii.h"
34
+#include "pci.h"
35
+#include "timer.h"
36
+#include "string.h"
37
+#include "stdint.h"
38
+#include "amd8111e.h"
39
+
40
+
41
+/* driver definitions */
42
+#define NUM_TX_SLOTS	2
43
+#define NUM_RX_SLOTS	4
44
+#define TX_SLOTS_MASK	1
45
+#define RX_SLOTS_MASK	3
46
+
47
+#define TX_BUF_LEN	1536
48
+#define RX_BUF_LEN	1536
49
+
50
+#define TX_PKT_LEN_MAX	(ETH_FRAME_LEN - ETH_HLEN)
51
+#define RX_PKT_LEN_MIN	60
52
+#define RX_PKT_LEN_MAX	ETH_FRAME_LEN
53
+
54
+#define TX_TIMEOUT	3000
55
+#define TX_PROCESS_TIME	10
56
+#define TX_RETRY	(TX_TIMEOUT / TX_PROCESS_TIME)
57
+
58
+#define PHY_RW_RETRY	10
59
+
60
+
61
+struct amd8111e_tx_desc {
62
+	u16 buf_len;
63
+	u16 tx_flags;
64
+	u16 tag_ctrl_info;
65
+	u16 tag_ctrl_cmd;
66
+	u32 buf_phy_addr;
67
+	u32 reserved;
68
+}; 
69
+
70
+struct amd8111e_rx_desc {
71
+	u32 reserved;
72
+	u16 msg_len;
73
+	u16 tag_ctrl_info; 
74
+	u16 buf_len;
75
+	u16 rx_flags;
76
+	u32 buf_phy_addr;
77
+};
78
+
79
+struct eth_frame {
80
+	u8 dst_addr[ETH_ALEN];
81
+	u8 src_addr[ETH_ALEN];
82
+	u16 type;
83
+	u8 data[ETH_FRAME_LEN - ETH_HLEN];
84
+} __attribute__((packed));
85
+
86
+struct amd8111e_priv {
87
+	struct amd8111e_tx_desc tx_ring[NUM_TX_SLOTS];
88
+	struct amd8111e_rx_desc rx_ring[NUM_RX_SLOTS];
89
+	unsigned char tx_buf[NUM_TX_SLOTS][TX_BUF_LEN];
90
+	unsigned char rx_buf[NUM_RX_SLOTS][RX_BUF_LEN];
91
+	unsigned long tx_idx, rx_idx;
92
+	int tx_consistent;
93
+
94
+	char opened;
95
+	char link;
96
+	char speed;
97
+	char duplex;
98
+	int ext_phy_addr;
99
+	u32 ext_phy_id;
100
+
101
+	struct pci_device *pdev;
102
+	struct nic *nic;
103
+	void *mmio;
104
+};
105
+
106
+static struct amd8111e_priv amd8111e;
107
+
108
+
109
+/********************************************************
110
+ * 		locale functions			*
111
+ ********************************************************/
112
+static void amd8111e_init_hw_default(struct amd8111e_priv *lp);
113
+static int amd8111e_start(struct amd8111e_priv *lp);
114
+static int amd8111e_read_phy(struct amd8111e_priv *lp, int phy_addr, int reg, u32 *val);
115
+#if 0
116
+static int amd8111e_write_phy(struct amd8111e_priv *lp, int phy_addr, int reg, u32 val);
117
+#endif
118
+static void amd8111e_probe_ext_phy(struct amd8111e_priv *lp);
119
+static void amd8111e_disable_interrupt(struct amd8111e_priv *lp);
120
+static void amd8111e_enable_interrupt(struct amd8111e_priv *lp);
121
+static void amd8111e_force_interrupt(struct amd8111e_priv *lp);
122
+static int amd8111e_get_mac_address(struct amd8111e_priv *lp);
123
+static int amd8111e_init_rx_ring(struct amd8111e_priv *lp);
124
+static int amd8111e_init_tx_ring(struct amd8111e_priv *lp);
125
+static int amd8111e_wait_tx_ring(struct amd8111e_priv *lp, unsigned int index);
126
+static void amd8111e_wait_link(struct amd8111e_priv *lp);
127
+static void amd8111e_poll_link(struct amd8111e_priv *lp);
128
+static void amd8111e_restart(struct amd8111e_priv *lp);
129
+
130
+
131
+/* 
132
+ * This function clears necessary the device registers. 
133
+ */	
134
+static void amd8111e_init_hw_default(struct amd8111e_priv *lp)
135
+{
136
+	unsigned int reg_val;
137
+	unsigned int logic_filter[2] = {0,};
138
+	void *mmio = lp->mmio;
139
+
140
+        /* stop the chip */
141
+	writel(RUN, mmio + CMD0);
142
+
143
+	/* Clear RCV_RING_BASE_ADDR */
144
+	writel(0, mmio + RCV_RING_BASE_ADDR0);
145
+
146
+	/* Clear XMT_RING_BASE_ADDR */
147
+	writel(0, mmio + XMT_RING_BASE_ADDR0);
148
+	writel(0, mmio + XMT_RING_BASE_ADDR1);
149
+	writel(0, mmio + XMT_RING_BASE_ADDR2);
150
+	writel(0, mmio + XMT_RING_BASE_ADDR3);
151
+
152
+	/* Clear CMD0  */
153
+	writel(CMD0_CLEAR, mmio + CMD0);
154
+	
155
+	/* Clear CMD2 */
156
+	writel(CMD2_CLEAR, mmio + CMD2);
157
+
158
+	/* Clear CMD7 */
159
+	writel(CMD7_CLEAR, mmio + CMD7);
160
+
161
+	/* Clear DLY_INT_A and DLY_INT_B */
162
+	writel(0x0, mmio + DLY_INT_A);
163
+	writel(0x0, mmio + DLY_INT_B);
164
+
165
+	/* Clear FLOW_CONTROL */
166
+	writel(0x0, mmio + FLOW_CONTROL);
167
+
168
+	/* Clear INT0  write 1 to clear register */
169
+	reg_val = readl(mmio + INT0);
170
+	writel(reg_val, mmio + INT0);
171
+
172
+	/* Clear STVAL */
173
+	writel(0x0, mmio + STVAL);
174
+
175
+	/* Clear INTEN0 */
176
+	writel(INTEN0_CLEAR, mmio + INTEN0);
177
+
178
+	/* Clear LADRF */
179
+	writel(0x0, mmio + LADRF);
180
+
181
+	/* Set SRAM_SIZE & SRAM_BOUNDARY registers  */
182
+	writel(0x80010, mmio + SRAM_SIZE);
183
+
184
+	/* Clear RCV_RING0_LEN */
185
+	writel(0x0, mmio +  RCV_RING_LEN0);
186
+
187
+	/* Clear XMT_RING0/1/2/3_LEN */
188
+	writel(0x0, mmio +  XMT_RING_LEN0);
189
+	writel(0x0, mmio +  XMT_RING_LEN1);
190
+	writel(0x0, mmio +  XMT_RING_LEN2);
191
+	writel(0x0, mmio +  XMT_RING_LEN3);
192
+
193
+	/* Clear XMT_RING_LIMIT */
194
+	writel(0x0, mmio + XMT_RING_LIMIT);
195
+
196
+	/* Clear MIB */
197
+	writew(MIB_CLEAR, mmio + MIB_ADDR);
198
+
199
+	/* Clear LARF */
200
+	amd8111e_writeq(*(u64*)logic_filter, mmio + LADRF);
201
+
202
+	/* SRAM_SIZE register */
203
+	reg_val = readl(mmio + SRAM_SIZE);
204
+	
205
+	/* Set default value to CTRL1 Register */
206
+	writel(CTRL1_DEFAULT, mmio + CTRL1);
207
+
208
+	/* To avoid PCI posting bug */
209
+	readl(mmio + CMD2);
210
+}
211
+
212
+/* 
213
+ * This function initializes the device registers  and starts the device.  
214
+ */
215
+static int amd8111e_start(struct amd8111e_priv *lp)
216
+{
217
+	struct nic *nic = lp->nic;
218
+	void *mmio = lp->mmio;
219
+	int i, reg_val;
220
+
221
+	/* stop the chip */
222
+	writel(RUN, mmio + CMD0);
223
+
224
+	/* AUTOPOLL0 Register *//*TBD default value is 8100 in FPS */
225
+	writew(0x8100 | lp->ext_phy_addr, mmio + AUTOPOLL0);
226
+
227
+	/* enable the port manager and set auto negotiation always */
228
+	writel(VAL1 | EN_PMGR, mmio + CMD3 );
229
+	writel(XPHYANE | XPHYRST, mmio + CTRL2); 
230
+
231
+	/* set control registers */
232
+	reg_val = readl(mmio + CTRL1);
233
+	reg_val &= ~XMTSP_MASK;
234
+	writel(reg_val | XMTSP_128 | CACHE_ALIGN, mmio + CTRL1);
235
+
236
+	/* initialize tx and rx ring base addresses */
237
+	amd8111e_init_tx_ring(lp);
238
+	amd8111e_init_rx_ring(lp);
239
+	writel(virt_to_bus(lp->tx_ring), mmio + XMT_RING_BASE_ADDR0);
240
+	writel(virt_to_bus(lp->rx_ring), mmio + RCV_RING_BASE_ADDR0);
241
+	writew(NUM_TX_SLOTS, mmio + XMT_RING_LEN0);
242
+	writew(NUM_RX_SLOTS, mmio + RCV_RING_LEN0);
243
+	
244
+	/* set default IPG to 96 */
245
+	writew(DEFAULT_IPG, mmio + IPG);
246
+	writew(DEFAULT_IPG - IFS1_DELTA, mmio + IFS1); 
247
+
248
+	/* AutoPAD transmit, Retransmit on Underflow */
249
+	writel(VAL0 | APAD_XMT | REX_RTRY | REX_UFLO, mmio + CMD2);
250
+	
251
+	/* JUMBO disabled */
252
+	writel(JUMBO, mmio + CMD3);
253
+
254
+	/* Setting the MAC address to the device */
255
+	for(i = 0; i < ETH_ALEN; i++)
256
+		writeb(nic->node_addr[i], mmio + PADR + i); 
257
+
258
+	/* set RUN bit to start the chip, interrupt not enabled */
259
+	writel(VAL2 | RDMD0 | VAL0 | RUN, mmio + CMD0);
260
+	
261
+	/* To avoid PCI posting bug */
262
+	readl(mmio + CMD0);
263
+	return 0;
264
+}
265
+
266
+/* 
267
+This function will read the PHY registers.
268
+*/
269
+static int amd8111e_read_phy(struct amd8111e_priv *lp, int phy_addr, int reg, u32 *val)
270
+{
271
+	void *mmio = lp->mmio;
272
+	unsigned int reg_val;
273
+	unsigned int retry = PHY_RW_RETRY;
274
+
275
+	reg_val = readl(mmio + PHY_ACCESS);
276
+	while (reg_val & PHY_CMD_ACTIVE)
277
+		reg_val = readl(mmio + PHY_ACCESS);
278
+
279
+	writel(PHY_RD_CMD | ((phy_addr & 0x1f) << 21) | ((reg & 0x1f) << 16),
280
+		mmio + PHY_ACCESS);
281
+	do {
282
+		reg_val = readl(mmio + PHY_ACCESS);
283
+		udelay(30);  /* It takes 30 us to read/write data */
284
+	} while (--retry && (reg_val & PHY_CMD_ACTIVE));
285
+
286
+	if (reg_val & PHY_RD_ERR) {
287
+		*val = 0;
288
+		return -1;
289
+	}
290
+	
291
+	*val = reg_val & 0xffff;
292
+	return 0;
293
+}
294
+
295
+/* 
296
+This function will write into PHY registers. 
297
+*/
298
+#if 0
299
+static int amd8111e_write_phy(struct amd8111e_priv *lp, int phy_addr, int reg, u32 val)
300
+{
301
+	void *mmio = lp->mmio;
302
+	unsigned int reg_val;
303
+	unsigned int retry = PHY_RW_RETRY;
304
+
305
+	reg_val = readl(mmio + PHY_ACCESS);
306
+	while (reg_val & PHY_CMD_ACTIVE)
307
+		reg_val = readl(mmio + PHY_ACCESS);
308
+
309
+	writel(PHY_WR_CMD | ((phy_addr & 0x1f) << 21) | ((reg & 0x1f) << 16) | val,
310
+		mmio + PHY_ACCESS);
311
+	do {
312
+		reg_val = readl(mmio + PHY_ACCESS);
313
+		udelay(30);  /* It takes 30 us to read/write the data */
314
+	} while (--retry && (reg_val & PHY_CMD_ACTIVE));
315
+	
316
+	if(reg_val & PHY_RD_ERR)
317
+		return -1;
318
+
319
+	return 0;
320
+}
321
+#endif
322
+
323
+static void amd8111e_probe_ext_phy(struct amd8111e_priv *lp)
324
+{
325
+	int i;
326
+
327
+	lp->ext_phy_id = 0;
328
+	lp->ext_phy_addr = 1;
329
+	
330
+	for (i = 0x1e; i >= 0; i--) {
331
+		u32 id1, id2;
332
+
333
+		if (amd8111e_read_phy(lp, i, MII_PHYSID1, &id1))
334
+			continue;
335
+		if (amd8111e_read_phy(lp, i, MII_PHYSID2, &id2))
336
+			continue;
337
+		lp->ext_phy_id = (id1 << 16) | id2;
338
+		lp->ext_phy_addr = i;
339
+		break;
340
+	}
341
+
342
+	if (lp->ext_phy_id)
343
+		printf("Found MII PHY ID 0x%08x at address 0x%02x\n",
344
+			lp->ext_phy_id, lp->ext_phy_addr);
345
+	else
346
+		printf("Couldn't detect MII PHY, assuming address 0x01\n");
347
+}
348
+
349
+static void amd8111e_disable_interrupt(struct amd8111e_priv *lp)
350
+{
351
+	void *mmio = lp->mmio;
352
+	unsigned int int0;
353
+
354
+	writel(INTREN, mmio + CMD0);
355
+	writel(INTEN0_CLEAR, mmio + INTEN0);
356
+	int0 = readl(mmio + INT0);
357
+	writel(int0, mmio + INT0);
358
+	readl(mmio + INT0);
359
+}
360
+
361
+static void amd8111e_enable_interrupt(struct amd8111e_priv *lp)
362
+{
363
+	void *mmio = lp->mmio;
364
+
365
+	writel(VAL3 | LCINTEN | VAL1 | TINTEN0 | VAL0 | RINTEN0, mmio + INTEN0);
366
+	writel(VAL0 | INTREN, mmio + CMD0);
367
+	readl(mmio + CMD0);
368
+}
369
+
370
+static void amd8111e_force_interrupt(struct amd8111e_priv *lp)
371
+{
372
+	void *mmio = lp->mmio;
373
+
374
+	writel(VAL0 | UINTCMD, mmio + CMD0);
375
+	readl(mmio + CMD0);
376
+}
377
+
378
+static int amd8111e_get_mac_address(struct amd8111e_priv *lp)
379
+{
380
+	struct nic *nic = lp->nic;
381
+	void *mmio = lp->mmio;
382
+	int i;
383
+
384
+	/* BIOS should have set mac address to PADR register,
385
+	 * so we read PADR to get it.
386
+	 */
387
+	for (i = 0; i < ETH_ALEN; i++)
388
+		nic->node_addr[i] = readb(mmio + PADR + i);
389
+	printf("Ethernet addr: %!\n", nic->node_addr);
390
+
391
+	return 0;
392
+}
393
+
394
+static int amd8111e_init_rx_ring(struct amd8111e_priv *lp)
395
+{
396
+	int i;
397
+
398
+	lp->rx_idx = 0;
399
+	
400
+        /* Initilaizing receive descriptors */
401
+	for (i = 0; i < NUM_RX_SLOTS; i++) {
402
+		lp->rx_ring[i].buf_phy_addr = cpu_to_le32(virt_to_bus(lp->rx_buf[i]));
403
+		lp->rx_ring[i].buf_len = cpu_to_le16(RX_BUF_LEN);
404
+		wmb();
405
+		lp->rx_ring[i].rx_flags = cpu_to_le16(OWN_BIT);
406
+	}
407
+
408
+	return 0;
409
+}
410
+
411
+static int amd8111e_init_tx_ring(struct amd8111e_priv *lp)
412
+{
413
+	int i;
414
+
415
+	lp->tx_idx = 0;
416
+	lp->tx_consistent = 1;
417
+	
418
+	/* Initializing transmit descriptors */
419
+	for (i = 0; i < NUM_TX_SLOTS; i++) {
420
+		lp->tx_ring[i].tx_flags = 0;
421
+		lp->tx_ring[i].buf_phy_addr = 0;
422
+		lp->tx_ring[i].buf_len = 0;
423
+	}
424
+
425
+	return 0;
426
+}
427
+
428
+static int amd8111e_wait_tx_ring(struct amd8111e_priv *lp, unsigned int index)
429
+{
430
+	volatile u16 status;
431
+	int retry = TX_RETRY;
432
+
433
+	status = le16_to_cpu(lp->tx_ring[index].tx_flags);
434
+	while (--retry && (status & OWN_BIT)) {
435
+		mdelay(TX_PROCESS_TIME);
436
+		status = le16_to_cpu(lp->tx_ring[index].tx_flags);
437
+	}
438
+	if (status & OWN_BIT) {
439
+		printf("Error: tx slot %d timeout, stat = 0x%x\n", index, status);
440
+		amd8111e_restart(lp);
441
+		return -1;
442
+	}
443
+
444
+	return 0;
445
+}
446
+
447
+static void amd8111e_wait_link(struct amd8111e_priv *lp)
448
+{
449
+	unsigned int status;
450
+	u32 reg_val;
451
+
452
+	do {
453
+		/* read phy to update STAT0 register */
454
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_BMCR, &reg_val);
455
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_BMSR, &reg_val);
456
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_ADVERTISE, &reg_val);
457
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_LPA, &reg_val);
458
+		status = readl(lp->mmio + STAT0);
459
+	} while (!(status & AUTONEG_COMPLETE) || !(status & LINK_STATS));
460
+}
461
+
462
+static void amd8111e_poll_link(struct amd8111e_priv *lp)
463
+{
464
+	unsigned int status, speed;
465
+	u32 reg_val;
466
+
467
+	if (!lp->link) {
468
+		/* read phy to update STAT0 register */
469
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_BMCR, &reg_val);
470
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_BMSR, &reg_val);
471
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_ADVERTISE, &reg_val);
472
+		amd8111e_read_phy(lp, lp->ext_phy_addr, MII_LPA, &reg_val);
473
+		status = readl(lp->mmio + STAT0);
474
+
475
+		if (status & LINK_STATS) {
476
+			lp->link = 1;
477
+			speed = (status & SPEED_MASK) >> 7;
478
+			if (speed == PHY_SPEED_100)
479
+				lp->speed = 1;
480
+			else
481
+				lp->speed = 0;
482
+			if (status & FULL_DPLX)
483
+				lp->duplex = 1;
484
+			else
485
+				lp->duplex = 0;
486
+
487
+			printf("Link is up: %s Mbps %s duplex\n",
488
+				lp->speed ? "100" : "10", lp->duplex ? "full" : "half");
489
+		}
490
+	} else {
491
+		status = readl(lp->mmio + STAT0);
492
+		if (!(status & LINK_STATS)) {
493
+			lp->link = 0;
494
+			printf("Link is down\n");
495
+		}
496
+	}
497
+}
498
+
499
+static void amd8111e_restart(struct amd8111e_priv *lp)
500
+{
501
+	printf("\nStarting nic...\n");
502
+	amd8111e_disable_interrupt(lp);
503
+	amd8111e_init_hw_default(lp);
504
+	amd8111e_probe_ext_phy(lp);
505
+	amd8111e_get_mac_address(lp);
506
+	amd8111e_start(lp);
507
+
508
+	printf("Waiting link up...\n");
509
+	lp->link = 0;
510
+	amd8111e_wait_link(lp);
511
+	amd8111e_poll_link(lp);
512
+}
513
+
514
+
515
+/********************************************************
516
+ * 		Interface Functions			*
517
+ ********************************************************/
518
+
519
+static void amd8111e_transmit(struct nic *nic, const char *dst_addr,
520
+		unsigned int type, unsigned int size, const char *packet)
521
+{
522
+	struct amd8111e_priv *lp = nic->priv_data;
523
+	struct eth_frame *frame;
524
+	unsigned int index;
525
+
526
+	/* check packet size */
527
+	if (size > TX_PKT_LEN_MAX) {
528
+		printf("amd8111e_transmit(): too large packet, drop\n");
529
+		return;
530
+	}
531
+
532
+	/* get tx slot */
533
+	index = lp->tx_idx;
534
+	if (amd8111e_wait_tx_ring(lp, index))
535
+		return;
536
+
537
+	/* fill frame */
538
+	frame = (struct eth_frame *)lp->tx_buf[index];
539
+	memset(frame->data, 0, TX_PKT_LEN_MAX);
540
+	memcpy(frame->dst_addr, dst_addr, ETH_ALEN);
541
+	memcpy(frame->src_addr, nic->node_addr, ETH_ALEN);
542
+	frame->type = htons(type);
543
+	memcpy(frame->data, packet, size);
544
+
545
+	/* start xmit */
546
+	lp->tx_ring[index].buf_len = cpu_to_le16(ETH_HLEN + size);
547
+	lp->tx_ring[index].buf_phy_addr = cpu_to_le32(virt_to_bus(frame));
548
+	wmb();
549
+	lp->tx_ring[index].tx_flags = 
550
+		cpu_to_le16(OWN_BIT | STP_BIT | ENP_BIT | ADD_FCS_BIT | LTINT_BIT);
551
+	writel(VAL1 | TDMD0, lp->mmio + CMD0);
552
+	readl(lp->mmio + CMD0);
553
+
554
+	/* update slot pointer */
555
+	lp->tx_idx = (lp->tx_idx + 1) & TX_SLOTS_MASK;
556
+}
557
+
558
+static int amd8111e_poll(struct nic *nic, int retrieve)
559
+{
560
+	/* return true if there's an ethernet packet ready to read */
561
+	/* nic->packet should contain data on return */
562
+	/* nic->packetlen should contain length of data */
563
+
564
+	struct amd8111e_priv *lp = nic->priv_data;
565
+	u16 status, pkt_len;
566
+	unsigned int index, pkt_ok;
567
+
568
+	amd8111e_poll_link(lp);
569
+
570
+	index = lp->rx_idx;
571
+	status = le16_to_cpu(lp->rx_ring[index].rx_flags);
572
+	pkt_len = le16_to_cpu(lp->rx_ring[index].msg_len) - 4;	/* remove 4bytes FCS */
573
+	
574
+	if (status & OWN_BIT)
575
+		return 0;
576
+
577
+	if (status & ERR_BIT)
578
+		pkt_ok = 0;
579
+	else if (!(status & STP_BIT))
580
+		pkt_ok = 0;
581
+	else if (!(status & ENP_BIT))
582
+		pkt_ok = 0;
583
+	else if (pkt_len < RX_PKT_LEN_MIN)
584
+		pkt_ok = 0;
585
+	else if (pkt_len > RX_PKT_LEN_MAX)
586
+		pkt_ok = 0;
587
+	else
588
+		pkt_ok = 1;
589
+
590
+	if (pkt_ok) {
591
+		if (!retrieve)
592
+			return 1;
593
+		nic->packetlen = pkt_len;
594
+		memcpy(nic->packet, lp->rx_buf[index], nic->packetlen);
595
+	}
596
+
597
+	lp->rx_ring[index].buf_phy_addr = cpu_to_le32(virt_to_bus(lp->rx_buf[index]));
598
+	lp->rx_ring[index].buf_len = cpu_to_le16(RX_BUF_LEN);
599
+	wmb();
600
+	lp->rx_ring[index].rx_flags = cpu_to_le16(OWN_BIT);
601
+	writel(VAL2 | RDMD0, lp->mmio + CMD0);
602
+	readl(lp->mmio + CMD0);
603
+
604
+	lp->rx_idx = (lp->rx_idx + 1) & RX_SLOTS_MASK;
605
+	return pkt_ok;
606
+}
607
+
608
+static void amd8111e_disable(struct nic *nic)
609
+{
610
+	struct amd8111e_priv *lp = nic->priv_data;
611
+
612
+	/* disable interrupt */
613
+	amd8111e_disable_interrupt(lp);
614
+
615
+	/* stop chip */
616
+	amd8111e_init_hw_default(lp);
617
+
618
+	/* unmap mmio */
619
+	iounmap(lp->mmio);
620
+
621
+	/* update status */
622
+	lp->opened = 0;
623
+}
624
+
625
+static void amd8111e_irq(struct nic *nic, irq_action_t action)
626
+{
627
+	struct amd8111e_priv *lp = nic->priv_data;
628
+		
629
+	switch (action) {
630
+	case DISABLE:
631
+		amd8111e_disable_interrupt(lp);
632
+		break;
633
+	case ENABLE:
634
+		amd8111e_enable_interrupt(lp);
635
+		break;
636
+	case FORCE:
637
+		amd8111e_force_interrupt(lp);
638
+		break;
639
+	}
640
+}
641
+
642
+static struct nic_operations amd8111e_operations = {
643
+	.connect	= dummy_connect,
644
+	.poll		= amd8111e_poll,
645
+	.transmit	= amd8111e_transmit,
646
+	.irq		= amd8111e_irq,
647
+};
648
+
649
+static int amd8111e_probe(struct nic *nic, struct pci_device *pdev)
650
+{
651
+	struct amd8111e_priv *lp = &amd8111e;
652
+	unsigned long mmio_start, mmio_len;
653
+
654
+	pci_fill_nic ( nic, pdev );
655
+	
656
+	mmio_start = pci_bar_start(pdev, PCI_BASE_ADDRESS_0);
657
+	mmio_len = pci_bar_size(pdev, PCI_BASE_ADDRESS_0);
658
+
659
+	memset(lp, 0, sizeof(*lp));
660
+	lp->pdev = pdev;
661
+	lp->nic = nic;
662
+	lp->mmio = ioremap(mmio_start, mmio_len);
663
+	lp->opened = 1;
664
+	adjust_pci_device(pdev);
665
+
666
+	nic->priv_data = lp;
667
+
668
+	amd8111e_restart(lp);
669
+
670
+	nic->nic_op	= &amd8111e_operations;
671
+	return 1;
672
+}
673
+
674
+static struct pci_id amd8111e_nics[] = {
675
+	PCI_ROM(0x1022, 0x7462, "amd8111e",	"AMD8111E"),
676
+};
677
+
678
+PCI_DRIVER ( amd8111e_driver, amd8111e_nics, PCI_NO_CLASS );
679
+
680
+DRIVER ( "AMD8111E", nic_driver, pci_driver, amd8111e_driver,
681
+	 amd8111e_probe, amd8111e_disable );

+ 629
- 0
src/drivers/net/amd8111e.h Переглянути файл

@@ -0,0 +1,629 @@
1
+/*
2
+ * Advanced  Micro Devices Inc. AMD8111E Linux Network Driver 
3
+ * Copyright (C) 2003 Advanced Micro Devices 
4
+ *
5
+ * This program is free software; you can redistribute it and/or modify
6
+ * it under the terms of the GNU General Public License as published by
7
+ * the Free Software Foundation; either version 2 of the License, or
8
+ * (at your option) any later version.
9
+ *
10
+ * This program is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
+ * GNU General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU General Public License
16
+ * along with this program; if not, write to the Free Software
17
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 
18
+ * USA
19
+
20
+Module Name:
21
+
22
+    amd8111e.h
23
+
24
+Abstract:
25
+	
26
+ 	 AMD8111 based 10/100 Ethernet Controller driver definitions. 
27
+
28
+Environment:
29
+    
30
+	Kernel Mode
31
+
32
+Revision History:
33
+ 	3.0.0
34
+	   Initial Revision.
35
+	3.0.1
36
+*/
37
+
38
+#ifndef _AMD811E_H
39
+#define _AMD811E_H
40
+
41
+/* Command style register access
42
+
43
+Registers CMD0, CMD2, CMD3,CMD7 and INTEN0 uses a write access technique called command style access. It allows the write to selected bits of this register without altering the bits that are not selected. Command style registers are divided into 4 bytes that can be written independently. Higher order bit of each byte is the  value bit that specifies the value that will be written into the selected bits of register. 
44
+
45
+eg., if the value 10011010b is written into the least significant byte of a command style register, bits 1,3 and 4 of the register will be set to 1, and the other bits will not be altered. If the value 00011010b is written into the same byte, bits 1,3 and 4 will be cleared to 0 and the other bits will not be altered.
46
+
47
+*/
48
+
49
+/*  Offset for Memory Mapped Registers. */
50
+/* 32 bit registers */
51
+
52
+#define  ASF_STAT		0x00	/* ASF status register */
53
+#define CHIPID			0x04	/* Chip ID regsiter */
54
+#define	MIB_DATA		0x10	/* MIB data register */
55
+#define MIB_ADDR		0x14	/* MIB address register */
56
+#define STAT0			0x30	/* Status0 register */
57
+#define INT0			0x38	/* Interrupt0 register */
58
+#define INTEN0			0x40	/* Interrupt0  enable register*/
59
+#define CMD0			0x48	/* Command0 register */
60
+#define CMD2			0x50	/* Command2 register */
61
+#define CMD3			0x54	/* Command3 resiter */
62
+#define CMD7			0x64	/* Command7 register */
63
+
64
+#define CTRL1 			0x6C	/* Control1 register */
65
+#define CTRL2 			0x70	/* Control2 register */
66
+
67
+#define XMT_RING_LIMIT		0x7C	/* Transmit ring limit register */
68
+
69
+#define AUTOPOLL0		0x88	/* Auto-poll0 register */
70
+#define AUTOPOLL1		0x8A	/* Auto-poll1 register */
71
+#define AUTOPOLL2		0x8C	/* Auto-poll2 register */
72
+#define AUTOPOLL3		0x8E	/* Auto-poll3 register */
73
+#define AUTOPOLL4		0x90	/* Auto-poll4 register */
74
+#define	AUTOPOLL5		0x92	/* Auto-poll5 register */
75
+
76
+#define AP_VALUE		0x98	/* Auto-poll value register */
77
+#define DLY_INT_A		0xA8	/* Group A delayed interrupt register */
78
+#define DLY_INT_B		0xAC	/* Group B delayed interrupt register */
79
+
80
+#define FLOW_CONTROL		0xC8	/* Flow control register */
81
+#define PHY_ACCESS		0xD0	/* PHY access register */
82
+
83
+#define STVAL			0xD8	/* Software timer value register */
84
+
85
+#define XMT_RING_BASE_ADDR0	0x100	/* Transmit ring0 base addr register */
86
+#define XMT_RING_BASE_ADDR1	0x108	/* Transmit ring1 base addr register */
87
+#define XMT_RING_BASE_ADDR2	0x110	/* Transmit ring2 base addr register */
88
+#define XMT_RING_BASE_ADDR3	0x118	/* Transmit ring2 base addr register */
89
+
90
+#define RCV_RING_BASE_ADDR0	0x120	/* Transmit ring0 base addr register */
91
+
92
+#define PMAT0			0x190	/* OnNow pattern register0 */
93
+#define PMAT1			0x194	/* OnNow pattern register1 */
94
+
95
+/* 16bit registers */
96
+
97
+#define XMT_RING_LEN0		0x140	/* Transmit Ring0 length register */
98
+#define XMT_RING_LEN1		0x144	/* Transmit Ring1 length register */
99
+#define XMT_RING_LEN2		0x148 	/* Transmit Ring2 length register */
100
+#define XMT_RING_LEN3		0x14C	/* Transmit Ring3 length register */
101
+
102
+#define RCV_RING_LEN0		0x150	/* Receive Ring0 length register */
103
+
104
+#define SRAM_SIZE		0x178	/* SRAM size register */
105
+#define SRAM_BOUNDARY		0x17A	/* SRAM boundary register */
106
+
107
+/* 48bit register */
108
+
109
+#define PADR			0x160	/* Physical address register */
110
+
111
+#define IFS1			0x18C	/* Inter-frame spacing Part1 register */
112
+#define IFS			0x18D	/* Inter-frame spacing register */
113
+#define IPG			0x18E	/* Inter-frame gap register */
114
+/* 64bit register */
115
+
116
+#define LADRF			0x168	/* Logical address filter register */
117
+
118
+
119
+/* Register Bit Definitions */
120
+typedef enum {
121
+
122
+	ASF_INIT_DONE		= (1 << 1),
123
+	ASF_INIT_PRESENT	= (1 << 0),
124
+
125
+}STAT_ASF_BITS; 
126
+   
127
+typedef enum {
128
+
129
+	MIB_CMD_ACTIVE		= (1 << 15 ),
130
+	MIB_RD_CMD		= (1 << 13 ),
131
+	MIB_CLEAR		= (1 << 12 ),
132
+	MIB_ADDRESS		= (1 << 0) | (1 << 1) | (1 << 2) | (1 << 3)|
133
+					(1 << 4) | (1 << 5),
134
+}MIB_ADDR_BITS;
135
+
136
+
137
+typedef enum {
138
+	
139
+	PMAT_DET		= (1 << 12),
140
+	MP_DET		        = (1 << 11),
141
+	LC_DET			= (1 << 10),
142
+	SPEED_MASK		= (1 << 9)|(1 << 8)|(1 << 7),
143
+	FULL_DPLX		= (1 << 6),
144
+	LINK_STATS		= (1 << 5),
145
+	AUTONEG_COMPLETE	= (1 << 4),
146
+	MIIPD			= (1 << 3),
147
+	RX_SUSPENDED		= (1 << 2),
148
+	TX_SUSPENDED		= (1 << 1),
149
+	RUNNING			= (1 << 0),
150
+
151
+}STAT0_BITS;
152
+
153
+#define PHY_SPEED_10		0x2
154
+#define PHY_SPEED_100		0x3
155
+
156
+/* INT0				0x38, 32bit register */
157
+typedef enum {
158
+
159
+	INTR			= (1 << 31),
160
+	PCSINT			= (1 << 28), 
161
+	LCINT			= (1 << 27),
162
+	APINT5			= (1 << 26),
163
+	APINT4			= (1 << 25),
164
+	APINT3			= (1 << 24),
165
+	TINT_SUM		= (1 << 23),
166
+	APINT2			= (1 << 22),
167
+	APINT1			= (1 << 21),
168
+	APINT0			= (1 << 20),
169
+	MIIPDTINT		= (1 << 19),
170
+	MCCINT			= (1 << 17),
171
+	MREINT			= (1 << 16),
172
+	RINT_SUM		= (1 << 15),
173
+	SPNDINT			= (1 << 14),
174
+	MPINT			= (1 << 13),
175
+	SINT			= (1 << 12),
176
+	TINT3			= (1 << 11),
177
+	TINT2			= (1 << 10),
178
+	TINT1			= (1 << 9),
179
+	TINT0			= (1 << 8),
180
+	UINT			= (1 << 7),
181
+	STINT			= (1 << 4),
182
+	RINT0			= (1 << 0),
183
+
184
+}INT0_BITS;
185
+
186
+typedef enum {
187
+
188
+	VAL3			= (1 << 31),   /* VAL bit for byte 3 */
189
+	VAL2			= (1 << 23),   /* VAL bit for byte 2 */
190
+	VAL1			= (1 << 15),   /* VAL bit for byte 1 */
191
+	VAL0			= (1 << 7),    /* VAL bit for byte 0 */
192
+
193
+}VAL_BITS;
194
+
195
+typedef enum {
196
+
197
+	/* VAL3 */
198
+	LCINTEN			= (1 << 27),
199
+	APINT5EN		= (1 << 26),
200
+	APINT4EN		= (1 << 25),
201
+	APINT3EN		= (1 << 24),
202
+	/* VAL2 */
203
+	APINT2EN		= (1 << 22),
204
+	APINT1EN		= (1 << 21),
205
+	APINT0EN		= (1 << 20),
206
+	MIIPDTINTEN		= (1 << 19),
207
+	MCCIINTEN		= (1 << 18),
208
+	MCCINTEN		= (1 << 17),
209
+	MREINTEN		= (1 << 16),
210
+	/* VAL1 */
211
+	SPNDINTEN		= (1 << 14),
212
+	MPINTEN			= (1 << 13),
213
+	TINTEN3			= (1 << 11),
214
+	SINTEN			= (1 << 12),
215
+	TINTEN2			= (1 << 10),
216
+	TINTEN1			= (1 << 9),
217
+	TINTEN0			= (1 << 8),
218
+	/* VAL0 */
219
+	STINTEN			= (1 << 4),
220
+	RINTEN0			= (1 << 0),
221
+
222
+	INTEN0_CLEAR 		= 0x1F7F7F1F, /* Command style register */
223
+
224
+}INTEN0_BITS;		
225
+
226
+typedef enum {
227
+	/* VAL2 */
228
+	RDMD0			= (1 << 16),
229
+	/* VAL1 */
230
+	TDMD3			= (1 << 11),
231
+	TDMD2			= (1 << 10),
232
+	TDMD1			= (1 << 9),
233
+	TDMD0			= (1 << 8),
234
+	/* VAL0 */
235
+	UINTCMD			= (1 << 6),
236
+	RX_FAST_SPND		= (1 << 5),
237
+	TX_FAST_SPND		= (1 << 4),
238
+	RX_SPND			= (1 << 3),
239
+	TX_SPND			= (1 << 2),
240
+	INTREN			= (1 << 1),
241
+	RUN			= (1 << 0),
242
+
243
+	CMD0_CLEAR 		= 0x000F0F7F,   /* Command style register */	
244
+
245
+}CMD0_BITS;
246
+
247
+typedef enum {
248
+
249
+	/* VAL3 */
250
+	CONDUIT_MODE		= (1 << 29),
251
+	/* VAL2 */
252
+	RPA			= (1 << 19),
253
+	DRCVPA			= (1 << 18),
254
+	DRCVBC			= (1 << 17),
255
+	PROM			= (1 << 16),
256
+	/* VAL1 */
257
+	ASTRP_RCV		= (1 << 13),
258
+	RCV_DROP0	  	= (1 << 12),
259
+	EMBA			= (1 << 11),
260
+	DXMT2PD			= (1 << 10),
261
+	LTINTEN			= (1 << 9),
262
+	DXMTFCS			= (1 << 8),
263
+	/* VAL0 */
264
+	APAD_XMT		= (1 << 6),
265
+	DRTY			= (1 << 5),
266
+	INLOOP			= (1 << 4),
267
+	EXLOOP			= (1 << 3),
268
+	REX_RTRY		= (1 << 2),
269
+	REX_UFLO		= (1 << 1),
270
+	REX_LCOL		= (1 << 0),
271
+
272
+	CMD2_CLEAR 		= 0x3F7F3F7F,   /* Command style register */
273
+
274
+}CMD2_BITS;
275
+
276
+typedef enum {
277
+
278
+	/* VAL3 */
279
+	ASF_INIT_DONE_ALIAS	= (1 << 29),
280
+	/* VAL2 */
281
+	JUMBO			= (1 << 21),
282
+	VSIZE			= (1 << 20),	
283
+	VLONLY			= (1 << 19),
284
+	VL_TAG_DEL		= (1 << 18),	
285
+	/* VAL1 */
286
+	EN_PMGR			= (1 << 14),			
287
+	INTLEVEL		= (1 << 13),
288
+	FORCE_FULL_DUPLEX	= (1 << 12),	
289
+	FORCE_LINK_STATUS	= (1 << 11),	
290
+	APEP			= (1 << 10),	
291
+	MPPLBA			= (1 << 9),	
292
+	/* VAL0 */
293
+	RESET_PHY_PULSE		= (1 << 2),	
294
+	RESET_PHY		= (1 << 1),	
295
+	PHY_RST_POL		= (1 << 0),	
296
+
297
+}CMD3_BITS;
298
+
299
+
300
+typedef enum {
301
+
302
+	/* VAL0 */
303
+	PMAT_SAVE_MATCH		= (1 << 4),
304
+	PMAT_MODE		= (1 << 3),
305
+	MPEN_SW			= (1 << 1),
306
+	LCMODE_SW		= (1 << 0),
307
+
308
+	CMD7_CLEAR  		= 0x0000001B	/* Command style register */
309
+
310
+}CMD7_BITS;
311
+
312
+
313
+typedef enum {
314
+
315
+	RESET_PHY_WIDTH		= (0xF << 16) | (0xF<< 20), /* 0x00FF0000 */
316
+	XMTSP_MASK		= (1 << 9) | (1 << 8),	/* 9:8 */
317
+	XMTSP_128		= (1 << 9),	/* 9 */	
318
+	XMTSP_64		= (1 << 8),
319
+	CACHE_ALIGN		= (1 << 4),
320
+	BURST_LIMIT_MASK	= (0xF << 0 ),
321
+	CTRL1_DEFAULT		= 0x00010111,
322
+
323
+}CTRL1_BITS;
324
+
325
+typedef enum {
326
+
327
+	FMDC_MASK		= (1 << 9)|(1 << 8),	/* 9:8 */
328
+	XPHYRST			= (1 << 7),
329
+	XPHYANE			= (1 << 6),
330
+	XPHYFD			= (1 << 5),
331
+	XPHYSP			= (1 << 4) | (1 << 3),	/* 4:3 */
332
+	APDW_MASK		= (1 <<	2) | (1 << 1) | (1 << 0), /* 2:0 */
333
+
334
+}CTRL2_BITS;
335
+
336
+/* XMT_RING_LIMIT		0x7C, 32bit register */
337
+typedef enum {
338
+
339
+	XMT_RING2_LIMIT		= (0xFF << 16),	/* 23:16 */
340
+	XMT_RING1_LIMIT		= (0xFF << 8),	/* 15:8 */
341
+	XMT_RING0_LIMIT		= (0xFF << 0), 	/* 7:0 */
342
+
343
+}XMT_RING_LIMIT_BITS;
344
+
345
+typedef enum {
346
+
347
+	AP_REG0_EN		= (1 << 15),
348
+	AP_REG0_ADDR_MASK	= (0xF << 8) |(1 << 12),/* 12:8 */
349
+	AP_PHY0_ADDR_MASK	= (0xF << 0) |(1 << 4),/* 4:0 */
350
+
351
+}AUTOPOLL0_BITS;
352
+
353
+/* AUTOPOLL1			0x8A, 16bit register */
354
+typedef enum {
355
+
356
+	AP_REG1_EN		= (1 << 15),
357
+	AP_REG1_ADDR_MASK	= (0xF << 8) |(1 << 12),/* 12:8 */
358
+	AP_PRE_SUP1		= (1 << 6),
359
+	AP_PHY1_DFLT		= (1 << 5),
360
+	AP_PHY1_ADDR_MASK	= (0xF << 0) |(1 << 4),/* 4:0 */
361
+
362
+}AUTOPOLL1_BITS;
363
+
364
+
365
+typedef enum {
366
+
367
+	AP_REG2_EN		= (1 << 15),
368
+	AP_REG2_ADDR_MASK	= (0xF << 8) |(1 << 12),/* 12:8 */
369
+	AP_PRE_SUP2		= (1 << 6),
370
+	AP_PHY2_DFLT		= (1 << 5),
371
+	AP_PHY2_ADDR_MASK	= (0xF << 0) |(1 << 4),/* 4:0 */
372
+
373
+}AUTOPOLL2_BITS;
374
+
375
+typedef enum {
376
+
377
+	AP_REG3_EN		= (1 << 15),
378
+	AP_REG3_ADDR_MASK	= (0xF << 8) |(1 << 12),/* 12:8 */
379
+	AP_PRE_SUP3		= (1 << 6),
380
+	AP_PHY3_DFLT		= (1 << 5),
381
+	AP_PHY3_ADDR_MASK	= (0xF << 0) |(1 << 4),/* 4:0 */
382
+
383
+}AUTOPOLL3_BITS;
384
+
385
+
386
+typedef enum {
387
+
388
+	AP_REG4_EN		= (1 << 15),
389
+	AP_REG4_ADDR_MASK	= (0xF << 8) |(1 << 12),/* 12:8 */
390
+	AP_PRE_SUP4		= (1 << 6),
391
+	AP_PHY4_DFLT		= (1 << 5),
392
+	AP_PHY4_ADDR_MASK	= (0xF << 0) |(1 << 4),/* 4:0 */
393
+
394
+}AUTOPOLL4_BITS;
395
+
396
+
397
+typedef enum {
398
+
399
+	AP_REG5_EN		= (1 << 15),
400
+	AP_REG5_ADDR_MASK	= (0xF << 8) |(1 << 12),/* 12:8 */
401
+	AP_PRE_SUP5		= (1 << 6),
402
+	AP_PHY5_DFLT		= (1 << 5),
403
+	AP_PHY5_ADDR_MASK	= (0xF << 0) |(1 << 4),/* 4:0 */
404
+
405
+}AUTOPOLL5_BITS;
406
+
407
+
408
+
409
+
410
+/* AP_VALUE 			0x98, 32bit ragister */
411
+typedef enum {
412
+
413
+	AP_VAL_ACTIVE		= (1 << 31),
414
+	AP_VAL_RD_CMD		= ( 1 << 29),
415
+	AP_ADDR			= (1 << 18)|(1 << 17)|(1 << 16), /* 18:16 */
416
+	AP_VAL			= (0xF << 0) | (0xF << 4) |( 0xF << 8) |
417
+				  (0xF << 12),	/* 15:0 */
418
+
419
+}AP_VALUE_BITS;
420
+
421
+typedef enum {
422
+
423
+	DLY_INT_A_R3		= (1 << 31),
424
+	DLY_INT_A_R2		= (1 << 30),
425
+	DLY_INT_A_R1		= (1 << 29),
426
+	DLY_INT_A_R0		= (1 << 28),
427
+	DLY_INT_A_T3		= (1 << 27),
428
+	DLY_INT_A_T2		= (1 << 26),
429
+	DLY_INT_A_T1		= (1 << 25),
430
+	DLY_INT_A_T0		= ( 1 << 24),
431
+	EVENT_COUNT_A		= (0xF << 16) | (0x1 << 20),/* 20:16 */
432
+	MAX_DELAY_TIME_A	= (0xF << 0) | (0xF << 4) | (1 << 8)|
433
+				  (1 << 9) | (1 << 10),	/* 10:0 */
434
+
435
+}DLY_INT_A_BITS;
436
+
437
+typedef enum {
438
+
439
+	DLY_INT_B_R3		= (1 << 31),
440
+	DLY_INT_B_R2		= (1 << 30),
441
+	DLY_INT_B_R1		= (1 << 29),
442
+	DLY_INT_B_R0		= (1 << 28),
443
+	DLY_INT_B_T3		= (1 << 27),
444
+	DLY_INT_B_T2		= (1 << 26),
445
+	DLY_INT_B_T1		= (1 << 25),
446
+	DLY_INT_B_T0		= ( 1 << 24),
447
+	EVENT_COUNT_B		= (0xF << 16) | (0x1 << 20),/* 20:16 */
448
+	MAX_DELAY_TIME_B	= (0xF << 0) | (0xF << 4) | (1 << 8)| 
449
+				  (1 << 9) | (1 << 10),	/* 10:0 */
450
+}DLY_INT_B_BITS;
451
+
452
+
453
+/* FLOW_CONTROL 		0xC8, 32bit register */
454
+typedef enum {
455
+
456
+	PAUSE_LEN_CHG		= (1 << 30),
457
+	FTPE			= (1 << 22),
458
+	FRPE			= (1 << 21),
459
+	NAPA			= (1 << 20),
460
+	NPA			= (1 << 19),
461
+	FIXP			= ( 1 << 18),
462
+	FCCMD			= ( 1 << 16),
463
+	PAUSE_LEN		= (0xF << 0) | (0xF << 4) |( 0xF << 8) |	 				  (0xF << 12),	/* 15:0 */
464
+
465
+}FLOW_CONTROL_BITS;
466
+
467
+/* PHY_ ACCESS			0xD0, 32bit register */
468
+typedef enum {
469
+
470
+	PHY_CMD_ACTIVE		= (1 << 31),
471
+	PHY_WR_CMD		= (1 << 30),
472
+	PHY_RD_CMD		= (1 << 29),
473
+	PHY_RD_ERR		= (1 << 28),
474
+	PHY_PRE_SUP		= (1 << 27),
475
+	PHY_ADDR		= (1 << 21) | (1 << 22) | (1 << 23)|
476
+				  	(1 << 24) |(1 << 25),/* 25:21 */
477
+	PHY_REG_ADDR		= (1 << 16) | (1 << 17) | (1 << 18)|	 			  	   	  	(1 << 19) | (1 << 20),/* 20:16 */
478
+	PHY_DATA		= (0xF << 0)|(0xF << 4) |(0xF << 8)|
479
+					(0xF << 12),/* 15:0 */
480
+
481
+}PHY_ACCESS_BITS;
482
+
483
+
484
+/* PMAT0			0x190,	 32bit register */
485
+typedef enum {
486
+	PMR_ACTIVE		= (1 << 31),
487
+	PMR_WR_CMD		= (1 << 30),
488
+	PMR_RD_CMD		= (1 << 29),
489
+	PMR_BANK		= (1 <<28),
490
+	PMR_ADDR		= (0xF << 16)|(1 << 20)|(1 << 21)|
491
+				  	(1 << 22),/* 22:16 */
492
+	PMR_B4			= (0xF << 0) | (0xF << 4),/* 15:0 */
493
+}PMAT0_BITS;
494
+
495
+
496
+/* PMAT1			0x194,	 32bit register */
497
+typedef enum {
498
+	PMR_B3			= (0xF << 24) | (0xF <<28),/* 31:24 */
499
+	PMR_B2			= (0xF << 16) |(0xF << 20),/* 23:16 */
500
+	PMR_B1			= (0xF << 8) | (0xF <<12), /* 15:8 */
501
+	PMR_B0			= (0xF << 0)|(0xF << 4),/* 7:0 */
502
+}PMAT1_BITS;
503
+
504
+/************************************************************************/
505
+/*                                                                      */
506
+/*                      MIB counter definitions                         */
507
+/*                                                                      */
508
+/************************************************************************/
509
+
510
+#define rcv_miss_pkts				0x00
511
+#define rcv_octets				0x01
512
+#define rcv_broadcast_pkts			0x02
513
+#define rcv_multicast_pkts			0x03
514
+#define rcv_undersize_pkts			0x04
515
+#define rcv_oversize_pkts			0x05
516
+#define rcv_fragments				0x06
517
+#define rcv_jabbers				0x07
518
+#define rcv_unicast_pkts			0x08
519
+#define rcv_alignment_errors			0x09
520
+#define rcv_fcs_errors				0x0A
521
+#define rcv_good_octets				0x0B
522
+#define rcv_mac_ctrl				0x0C
523
+#define rcv_flow_ctrl				0x0D
524
+#define rcv_pkts_64_octets			0x0E
525
+#define rcv_pkts_65to127_octets			0x0F
526
+#define rcv_pkts_128to255_octets		0x10
527
+#define rcv_pkts_256to511_octets		0x11
528
+#define rcv_pkts_512to1023_octets		0x12
529
+#define rcv_pkts_1024to1518_octets		0x13
530
+#define rcv_unsupported_opcode			0x14
531
+#define rcv_symbol_errors			0x15
532
+#define rcv_drop_pkts_ring1			0x16
533
+#define rcv_drop_pkts_ring2			0x17
534
+#define rcv_drop_pkts_ring3			0x18
535
+#define rcv_drop_pkts_ring4			0x19
536
+#define rcv_jumbo_pkts				0x1A
537
+
538
+#define xmt_underrun_pkts			0x20
539
+#define xmt_octets				0x21
540
+#define xmt_packets				0x22
541
+#define xmt_broadcast_pkts			0x23
542
+#define xmt_multicast_pkts			0x24
543
+#define xmt_collisions				0x25
544
+#define xmt_unicast_pkts			0x26
545
+#define xmt_one_collision			0x27
546
+#define xmt_multiple_collision			0x28
547
+#define xmt_deferred_transmit			0x29
548
+#define xmt_late_collision			0x2A
549
+#define xmt_excessive_defer			0x2B
550
+#define xmt_loss_carrier			0x2C
551
+#define xmt_excessive_collision			0x2D
552
+#define xmt_back_pressure			0x2E
553
+#define xmt_flow_ctrl				0x2F
554
+#define xmt_pkts_64_octets			0x30
555
+#define xmt_pkts_65to127_octets			0x31
556
+#define xmt_pkts_128to255_octets		0x32
557
+#define xmt_pkts_256to511_octets		0x33
558
+#define xmt_pkts_512to1023_octets		0x34
559
+#define xmt_pkts_1024to1518_octet		0x35
560
+#define xmt_oversize_pkts			0x36
561
+#define xmt_jumbo_pkts				0x37
562
+
563
+/* ipg parameters */
564
+#define DEFAULT_IPG			0x60
565
+#define IFS1_DELTA			36
566
+#define	IPG_CONVERGE_JIFFIES (HZ/2)
567
+#define	IPG_STABLE_TIME	5
568
+#define	MIN_IPG	96
569
+#define	MAX_IPG	255
570
+#define IPG_STEP	16
571
+#define CSTATE  1 
572
+#define SSTATE  2 
573
+
574
+/* amd8111e decriptor flag definitions */
575
+typedef enum {
576
+
577
+	OWN_BIT		=	(1 << 15),
578
+	ADD_FCS_BIT	=	(1 << 13),
579
+	LTINT_BIT	=	(1 << 12),
580
+	STP_BIT		=	(1 << 9),
581
+	ENP_BIT		=	(1 << 8),
582
+	KILL_BIT	= 	(1 << 6),
583
+	TCC_VLAN_INSERT	=	(1 << 1),
584
+	TCC_VLAN_REPLACE =	(1 << 1) |( 1<< 0),
585
+
586
+}TX_FLAG_BITS;
587
+
588
+typedef enum {
589
+	ERR_BIT 	=	(1 << 14),
590
+	FRAM_BIT	=  	(1 << 13),
591
+	OFLO_BIT	=       (1 << 12),
592
+	CRC_BIT		=	(1 << 11),
593
+	PAM_BIT		=	(1 << 6),
594
+	LAFM_BIT	= 	(1 << 5),
595
+	BAM_BIT		=	(1 << 4),
596
+	TT_VLAN_TAGGED	= 	(1 << 3) |(1 << 2),/* 0x000 */
597
+	TT_PRTY_TAGGED	=	(1 << 3),/* 0x0008 */
598
+
599
+}RX_FLAG_BITS;
600
+
601
+#define RESET_RX_FLAGS		0x0000
602
+#define TT_MASK			0x000c
603
+#define TCC_MASK		0x0003
604
+
605
+/* driver ioctl parameters */
606
+#define AMD8111E_REG_DUMP_LEN	 13*sizeof(u32) 
607
+
608
+/* crc generator constants */
609
+#define CRC32 0xedb88320
610
+#define INITCRC 0xFFFFFFFF
611
+
612
+/* kernel provided writeq does not write 64 bits into the amd8111e device register instead writes only higher 32bits data into lower 32bits of the register.
613
+BUG? */
614
+#define  amd8111e_writeq(_UlData,_memMap)   \
615
+		writel(*(u32*)(&_UlData), _memMap);	\
616
+		writel(*(u32*)((u8*)(&_UlData)+4), _memMap+4)	
617
+
618
+/* maps the external speed options to internal value */
619
+typedef enum {
620
+	SPEED_AUTONEG,
621
+	SPEED10_HALF,
622
+	SPEED10_FULL,
623
+	SPEED100_HALF,
624
+	SPEED100_FULL,
625
+}EXT_PHY_OPTION;
626
+
627
+
628
+#endif /* _AMD8111E_H */
629
+

+ 245
- 0
src/proto/fsp.c Переглянути файл

@@ -0,0 +1,245 @@
1
+    /*********************************************************************\
2
+    * Copyright (c) 2005 by Radim Kolar (hsn-sendmail.cz)                 *
3
+    *                                                                     *
4
+    * You may copy or modify this file in any manner you wish, provided   *
5
+    * that this notice is always included, and that you hold the author   *
6
+    * harmless for any loss or damage resulting from the installation or  *
7
+    * use of this software.                                               *
8
+    *                                                                     *
9
+    * This file provides support for FSP v2 protocol written from scratch *
10
+    * by Radim Kolar,   FSP project leader.                               *
11
+    *                                                                     *
12
+    * ABOUT FSP                                                           *
13
+    * FSP is a lightweight file transfer protocol and is being used for   *
14
+    * booting, Internet firmware updates, embedded devices and in         *
15
+    * wireless applications. FSP is very easy to implement; contact Radim *
16
+    * Kolar if you need hand optimized assembler FSP stacks for various   *
17
+    * microcontrollers, CPUs or consultations.                            *
18
+    * http://fsp.sourceforge.net/                                         *
19
+    *                                                                     *
20
+    * REVISION HISTORY                                                    *
21
+    * 1.0 2005-03-17 rkolar   Initial coding                              *
22
+    * 1.1 2005-03-24 rkolar   We really need to send CC_BYE to the server *
23
+    *                         at end of transfer, because next stage boot *
24
+    *                         loader is unable to contact FSP server      *
25
+    *                         until session timeouts.                     *
26
+    * 1.2 2005-03-26 rkolar   We need to query filesize in advance,       *
27
+    *                         because NBI loader do not reads file until  *
28
+    *                         eof is reached.
29
+    * REMARKS                                                             *
30
+    * there is no support for selecting port number of fsp server, maybe  *
31
+    *   we should parse fsp:// URLs in boot image filename.               *
32
+    * this implementation has filename limit 255 chars.                   *
33
+    \*********************************************************************/
34
+
35
+#ifdef DOWNLOAD_PROTO_FSP
36
+#include "etherboot.h"
37
+#include "nic.h"
38
+
39
+#define FSP_PORT 21
40
+
41
+/* FSP commands */
42
+#define CC_GET_FILE	0x42
43
+#define CC_BYE		0x4A
44
+#define CC_ERR		0x40
45
+#define CC_STAT		0x4D
46
+
47
+/* etherboot limits */
48
+#define FSP_MAXFILENAME 255
49
+
50
+struct fsp_info {
51
+	in_addr server_ip;
52
+	uint16_t server_port;
53
+	uint16_t local_port;
54
+	const char *filename;
55
+	int (*fnc)(unsigned char *, unsigned int, unsigned int, int);
56
+};
57
+
58
+struct fsp_header {
59
+    	uint8_t cmd;
60
+	uint8_t sum;
61
+	uint16_t key;
62
+	uint16_t seq;
63
+	uint16_t len;
64
+	uint32_t pos;
65
+} PACKED;
66
+
67
+#define FSP_MAXPAYLOAD (ETH_MAX_MTU - \
68
+  (sizeof(struct iphdr) + sizeof(struct udphdr) + sizeof(struct fsp_header)))
69
+
70
+static struct fsp_request {
71
+	struct iphdr ip;
72
+	struct udphdr udp;
73
+	struct fsp_header fsp;
74
+	unsigned char data[FSP_MAXFILENAME + 1 + 2];
75
+} request;
76
+
77
+struct fsp_reply {
78
+	struct iphdr ip;
79
+	struct udphdr udp;
80
+	struct fsp_header fsp;
81
+	unsigned char data[FSP_MAXPAYLOAD];
82
+} PACKED;
83
+
84
+
85
+static int await_fsp(int ival, void *ptr, unsigned short ptype __unused,
86
+                      struct iphdr *ip, struct udphdr *udp)
87
+{
88
+	if(!udp)
89
+	    return 0;
90
+	if (ip->dest.s_addr != arptable[ARP_CLIENT].ipaddr.s_addr) 
91
+	    return 0;
92
+        if (ntohs(udp->dest) != ival)
93
+            return 0;
94
+	if (ntohs(udp->len) < 12+sizeof(struct udphdr))
95
+	    return 0;
96
+	return 1;
97
+}
98
+
99
+static int proto_fsp(struct fsp_info *info)
100
+{
101
+    uint32_t filepos;
102
+    uint32_t filelength=0;
103
+    int i,retry;
104
+    uint16_t reqlen;
105
+    struct fsp_reply *reply;
106
+    int block=1;
107
+    
108
+    /* prepare FSP request packet */
109
+    filepos=0;
110
+    i=strlen(info->filename);
111
+    if(i>FSP_MAXFILENAME)
112
+    {
113
+	printf("Boot filename is too long.\n");
114
+	return 0;
115
+    }
116
+    strcpy(request.data,info->filename);
117
+    *(uint16_t *)(request.data+i+1)=htons(FSP_MAXPAYLOAD);
118
+    request.fsp.len=htons(i+1);
119
+    reqlen=i+3+12;
120
+
121
+    rx_qdrain();
122
+    retry=0;
123
+
124
+    /* main loop */
125
+    for(;;) {
126
+	int  sum;
127
+	long timeout;
128
+
129
+        /* query filelength if not known */
130
+	if(filelength == 0)
131
+	    request.fsp.cmd=CC_STAT;
132
+		
133
+	/* prepare request packet */
134
+	request.fsp.pos=htonl(filepos);
135
+	request.fsp.seq=random();
136
+	request.fsp.sum=0;
137
+	for(i=0,sum=reqlen;i<reqlen;i++)
138
+	{
139
+	    sum += ((uint8_t *)&request.fsp)[i];
140
+        }
141
+	request.fsp.sum= sum + (sum >> 8);
142
+	/* send request */
143
+        if (!udp_transmit(info->server_ip.s_addr, info->local_port,
144
+	                 info->server_port, sizeof(request.ip) +
145
+			 sizeof(request.udp) + reqlen, &request))
146
+	                    return (0);
147
+	/* wait for retry */		    
148
+#ifdef  CONGESTED
149
+        timeout =
150
+            rfc2131_sleep_interval(filepos ? TFTP_REXMT : TIMEOUT, retry);
151
+#else
152
+	timeout = rfc2131_sleep_interval(TIMEOUT, retry);
153
+#endif
154
+	retry++;
155
+        if (!await_reply(await_fsp, info->local_port, NULL, timeout))
156
+	    continue;
157
+	reply=(struct fsp_reply *) &nic.packet[ETH_HLEN];    
158
+	/* check received packet */
159
+	if (reply->fsp.seq != request.fsp.seq)
160
+	    continue;
161
+	reply->udp.len=ntohs(reply->udp.len)-sizeof(struct udphdr);
162
+	if(reply->udp.len < ntohs(reply->fsp.len) + 12 )
163
+	    continue;
164
+        sum=-reply->fsp.sum;
165
+	for(i=0;i<reply->udp.len;i++)
166
+	{
167
+	    sum += ((uint8_t *)&(reply->fsp))[i];
168
+        }
169
+        sum = (sum + (sum >> 8)) & 0xff;
170
+	if(sum != reply->fsp.sum)
171
+	{
172
+	    printf("FSP checksum failed. computed %d, but packet has %d.\n",sum,reply->fsp.sum);
173
+	    continue;
174
+	}
175
+	if(reply->fsp.cmd == CC_ERR)
176
+	{
177
+	    printf("\nFSP error: %s",info->filename);
178
+	    if(reply->fsp.len)
179
+	        printf(" [%s]",reply->data);
180
+	    printf("\n");
181
+	    return 0;
182
+	}
183
+	if(reply->fsp.cmd == CC_BYE && filelength == 1)
184
+	{
185
+	    info->fnc(request.data,block,1,1);
186
+	    return 1;
187
+	}
188
+	if(reply->fsp.cmd == CC_STAT)
189
+	{
190
+	    if(reply->data[8] == 0)
191
+	    {
192
+		/* file not found, etc. */
193
+		filelength=0xffffffff;
194
+	    } else
195
+	    {
196
+		filelength= ntohl(*((uint32_t *)&reply->data[4]));
197
+	    }
198
+	    request.fsp.cmd = CC_GET_FILE;
199
+	    request.fsp.key = reply->fsp.key;
200
+	    retry=0;
201
+	    continue;
202
+	}
203
+
204
+	if(reply->fsp.cmd == CC_GET_FILE)
205
+	{
206
+	    if(ntohl(reply->fsp.pos) != filepos)
207
+		continue;
208
+	    request.fsp.key = reply->fsp.key;
209
+	    retry=0;
210
+	    i=ntohs(reply->fsp.len);
211
+	    if(i == 1)
212
+	    {
213
+		request.fsp.cmd=CC_BYE;
214
+		request.data[0]=reply->data[0];
215
+		continue;
216
+	    }
217
+	    /* let last byte alone */
218
+            if(i >= filelength)
219
+		i = filelength - 1;
220
+	    if(!info->fnc(reply->data,block++,i,0))
221
+		return 0;
222
+	    filepos += i;
223
+	    filelength -= i;
224
+	}
225
+    }
226
+
227
+    return 0;
228
+}
229
+
230
+int url_fsp(const char *name, int (*fnc)(unsigned char *, unsigned int, unsigned int, int))
231
+{
232
+	struct fsp_info info;
233
+	/* Set the defaults */
234
+	info.server_ip.s_addr    = arptable[ARP_SERVER].ipaddr.s_addr;
235
+	info.server_port         = FSP_PORT;
236
+	info.local_port		 = 1024 + random() & 0xfbff;
237
+	info.fnc                 = fnc;
238
+	
239
+	/* Now parse the url */
240
+	/* printf("fsp-URI: [%s]\n", name); */
241
+        /* quick hack for now */
242
+	info.filename=name;
243
+	return proto_fsp(&info);
244
+}
245
+#endif

Завантаження…
Відмінити
Зберегти