做少许修改:
drivers/dm9000x.c
1 /*
2 dm9000.c: Version 1.2 12/15/2003
3
4 A Davicom DM9000 ISA NIC fast Ethernet driver for Linux.
5 Copyright (C) 1997 Sten Wang
6
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 (C)Copyright 1997-1998 DAVICOM Semiconductor,Inc. All Rights Reserved.
18
19 V0.11 06/20/2001 REG_0A bit3=1, default enable BP with DA match
20 06/22/2001 Support DM9801 progrmming
21 E3: R25 = ((R24 + NF) & 0x00ff) | 0xf000
22 E4: R25 = ((R24 + NF) & 0x00ff) | 0xc200
23 R17 = (R17 & 0xfff0) | NF + 3
24 E5: R25 = ((R24 + NF - 3) & 0x00ff) | 0xc200
25 R17 = (R17 & 0xfff0) | NF
26
27 v1.00 modify by simon 2001.9.5
28 change for kernel 2.4.x
29
30 v1.1 11/09/2001 fix force mode bug
31
32 v1.2 03/18/2003 Weilun Huang <weilun_huang@davicom.com.tw>:
33 Fixed phy reset.
34 Added tx/rx 32 bit mode.
35 Cleaned up for kernel merge.
36
37 --------------------------------------
38
39 12/15/2003 Initial port to u-boot by Sascha Hauer <saschahauer@web.de>
40
41 TODO: Homerun NIC and longrun NIC are not functional, only internal at the
42 moment.
43 */
44
45 //#include <common.h> // snallie
46 #include <armboot.h> /* snallie */
/*
* 45~46修改所包含的头文件
*/
47 #include <command.h>
48 #include <net.h>
49 #include <asm/io.h>
50
51 #ifdef CONFIG_DRIVER_DM9000
52
53 #include "dm9000x.h"
54
55 /* Board/System/Debug information/definition ---------------- */
56
57 #define DM9801_NOISE_FLOOR 0x08
58 #define DM9802_NOISE_FLOOR 0x05
59
60 /* #define CONFIG_DM9000_DEBUG */
61
62 #ifdef CONFIG_DM9000_DEBUG
63 #define DM9000_DBG(fmt,args...) printf(fmt ,##args)
64 #else /* */
65 #define DM9000_DBG(fmt,args...)
66 #endif /* */
67 enum DM9000_PHY_mode { DM9000_10MHD = 0, DM9000_100MHD =
68 1, DM9000_10MFD = 4, DM9000_100MFD = 5, DM9000_AUTO =
69 8, DM9000_1M_HPNA = 0x10
70 };
71 enum DM9000_NIC_TYPE { FASTETHER_NIC = 0, HOMERUN_NIC = 1, LONGRUN_NIC = 2
72 };
73
74 /* Structure/enum declaration ------------------------------- */
75 typedef struct board_info {
76 u32 runt_length_counter; /* counter: RX length < 64byte */
77 u32 long_length_counter; /* counter: RX length > 1514byte */
78 u32 reset_counter; /* counter: RESET */
79 u32 reset_tx_timeout; /* RESET caused by TX Timeout */
80 u32 reset_rx_status; /* RESET caused by RX Statsus wrong */
81 u16 tx_pkt_cnt;
82 u16 queue_start_addr;
83 u16 dbug_cnt;
84 u8 phy_addr;
85 u8 device_wait_reset; /* device state */
86 u8 nic_type; /* NIC type */
87 unsigned char srom[128];
88 } board_info_t;
89 board_info_t dmfe_info;
90
91 /* For module input parameter */
92 static int media_mode = DM9000_AUTO;
93 static u8 nfloor = 0;
94
95 /* function declaration ------------------------------------- */
96 int eth_init(bd_t * bd);
97 int eth_send(volatile void *, int);
98 int eth_rx(void);
99 void eth_halt(void);
100 static int dm9000_probe(void);
101 static u16 phy_read(int);
102 static void phy_write(int, u16);
103 static u16 read_srom_word(int);
104 static u8 DM9000_ior(int);
105 static void DM9000_iow(int reg, u8 value);
106
107 /* DM9000 network board routine ---------------------------- */
108
109 #define DM9000_outb(d,r) ( *(volatile u8 *)r = d )
110 #define DM9000_outw(d,r) ( *(volatile u16 *)r = d )
111 #define DM9000_outl(d,r) ( *(volatile u32 *)r = d )
112 #define DM9000_inb(r) (*(volatile u8 *)r)
113 #define DM9000_inw(r) (*(volatile u16 *)r)
114 #define DM9000_inl(r) (*(volatile u32 *)r)
115
116 #ifdef CONFIG_DM9000_DEBUG
117 static void
118 dump_regs(void)
119 {
120 DM9000_DBG("\n");
121 DM9000_DBG("NCR (0x00): %02x\n", DM9000_ior(0));
122 DM9000_DBG("NSR (0x01): %02x\n", DM9000_ior(1));
123 DM9000_DBG("TCR (0x02): %02x\n", DM9000_ior(2));
124 DM9000_DBG("TSRI (0x03): %02x\n", DM9000_ior(3));
125 DM9000_DBG("TSRII (0x04): %02x\n", DM9000_ior(4));
126 DM9000_DBG("RCR (0x05): %02x\n", DM9000_ior(5));
127 DM9000_DBG("RSR (0x06): %02x\n", DM9000_ior(6));
128 DM9000_DBG("ISR (0xFE): %02x\n", DM9000_ior(DM9000_ISR)); /* snallie */
/*
* 128修改DM9000的ISR寄存器的偏移量DM9000_ISR定义在dm9000x.h头文件中
*/
129 DM9000_DBG("\n");
130 }
131 #endif /* */
132
133 /*
134 Search DM9000 board, allocate space and register it
135 */
136 int
137 dm9000_probe(void)
138 {
139 u32 id_val;
140 id_val = DM9000_ior(DM9000_VIDL);
141 id_val |= DM9000_ior(DM9000_VIDH) << 8;
142 id_val |= DM9000_ior(DM9000_PIDL) << 16;
143 id_val |= DM9000_ior(DM9000_PIDH) << 24;
144 if (id_val == DM9000_ID) {
145 printf("dm9000 i/o: 0x%x, id: 0x%08lx \n", CONFIG_DM9000_BASE,
146 id_val);
147 return 0;
148 } else {
149 printf("dm9000 not found at 0x%08x id: 0x%08lx\n",
150 CONFIG_DM9000_BASE, id_val);
151 return -1;
152 }
153 }
154
155 /* Set PHY operationg mode
156 */
157 static void
158 set_PHY_mode(void)
159 {
160 u16 phy_reg4 = 0x01e1, phy_reg0 = 0x1000;
161 if (!(media_mode & DM9000_AUTO)) {
162 switch (media_mode) {
163 case DM9000_10MHD:
164 phy_reg4 = 0x21;
165 phy_reg0 = 0x0000;
166 break;
167 case DM9000_10MFD:
168 phy_reg4 = 0x41;
169 phy_reg0 = 0x1100;
170 break;
171 case DM9000_100MHD:
172 phy_reg4 = 0x81;
173 phy_reg0 = 0x2000;
174 break;
175 case DM9000_100MFD:
176 phy_reg4 = 0x101;
177 phy_reg0 = 0x3100;
178 break;
179 }
180 phy_write(4, phy_reg4); /* Set PHY media mode */
181 phy_write(0, phy_reg0); /* Tmp */
182 }
183 DM9000_iow(DM9000_GPCR, 0x01); /* Let GPIO0 output */
184 DM9000_iow(DM9000_GPR, 0x00); /* Enable PHY */
185 }
186
187 /*
188 Init HomeRun DM9801
189 */
190 static void
191 program_dm9801(u16 HPNA_rev)
192 {
193 __u16 reg16, reg17, reg24, reg25;
194 if (!nfloor)
195 nfloor = DM9801_NOISE_FLOOR;
196 reg16 = phy_read(16);
197 reg17 = phy_read(17);
198 reg24 = phy_read(24);
199 reg25 = phy_read(25);
200 switch (HPNA_rev) {
201 case 0xb900: /* DM9801 E3 */
202 reg16 |= 0x1000;
203 reg25 = ((reg24 + nfloor) & 0x00ff) | 0xf000;
204 break;
205 case 0xb901: /* DM9801 E4 */
206 reg25 = ((reg24 + nfloor) & 0x00ff) | 0xc200;
207 reg17 = (reg17 & 0xfff0) + nfloor + 3;
208 break;
209 case 0xb902: /* DM9801 E5 */
210 case 0xb903: /* DM9801 E6 */
211 default:
212 reg16 |= 0x1000;
213 reg25 = ((reg24 + nfloor - 3) & 0x00ff) | 0xc200;
214 reg17 = (reg17 & 0xfff0) + nfloor;
215 }
216 phy_write(16, reg16);
217 phy_write(17, reg17);
218 phy_write(25, reg25);
219 }
220
221 /*
222 Init LongRun DM9802
223 */
224 static void
225 program_dm9802(void)
226 {
227 __u16 reg25;
228 if (!nfloor)
229 nfloor = DM9802_NOISE_FLOOR;
230 reg25 = phy_read(25);
231 reg25 = (reg25 & 0xff00) + nfloor;
232 phy_write(25, reg25);
233 }
234
235 /* Identify NIC type
236 */
237 static void
238 identify_nic(void)
239 {
240 struct board_info *db = &dmfe_info; /* Point a board information structure */
241 u16 phy_reg3;
242 DM9000_iow(DM9000_NCR, NCR_EXT_PHY);
243 phy_reg3 = phy_read(3);
244 switch (phy_reg3 & 0xfff0) {
245 case 0xb900:
246 if (phy_read(31) == 0x4404) {
247 db->nic_type = HOMERUN_NIC;
248 program_dm9801(phy_reg3);
249 DM9000_DBG("found homerun NIC\n");
250 } else {
251 db->nic_type = LONGRUN_NIC;
252 DM9000_DBG("found longrun NIC\n");
253 program_dm9802();
254 }
255 break;
256 default:
257 db->nic_type = FASTETHER_NIC;
258 break;
259 }
260 DM9000_iow(DM9000_NCR, 0);
261 }
262
263 /* General Purpose dm9000 reset routine */
264 static void
265 dm9000_reset(void)
266 {
267 DM9000_DBG("resetting\n");
268 DM9000_iow(DM9000_NCR, NCR_RST);
269 udelay(1000); /* delay 1ms */
270 }
271
272 /* Initilize dm9000 board
273 */
274 int
275 eth_init(bd_t * bd)
276 {
277 int i, oft, lnk;
278 DM9000_DBG("eth_init()\n");
279
280 /* RESET device */
281 dm9000_reset();
282 dm9000_probe();
283
284 /* NIC Type: FASTETHER, HOMERUN, LONGRUN */
285 identify_nic();
286
287 /* GPIO0 on pre-activate PHY */
288 DM9000_iow(DM9000_GPR, 0x00); /*REG_1F bit0 activate phyxcer */
289
290 /* Set PHY */
291 set_PHY_mode();
292
293 /* Program operating register */
294 DM9000_iow(DM9000_NCR, 0x0); /* only intern phy supported by now */
295 DM9000_iow(DM9000_TCR, 0); /* TX Polling clear */
296 DM9000_iow(DM9000_BPTR, 0x3f); /* Less 3Kb, 200us */
297 DM9000_iow(DM9000_FCTR, FCTR_HWOT(3) | FCTR_LWOT(8)); /* Flow Control : High/Low Water */
298 DM9000_iow(DM9000_FCR, 0x0); /* SH FIXME: This looks strange! Flow Control */
299 DM9000_iow(DM9000_SMCR, 0); /* Special Mode */
300 DM9000_iow(DM9000_NSR, NSR_WAKEST | NSR_TX2END | NSR_TX1END); /* clear TX status */
301 DM9000_iow(DM9000_ISR, 0x0f); /* Clear interrupt status */
302
303 /* Set Node address */
304 // snalie
305 #if 1
306 for (i = 0; i < 6; i++)
307 printf("%08x ", read_srom_word(i));
308 printf("\n");
309 #endif
310 #ifndef CONFIG_ETHADDR
311 for (i = 0; i < 6; i++)
312 ((u16 *) bd->bi_enetaddr)[i] = read_srom_word(i);
313 #endif
/*
* 305~313加入调试输出
*/
314 // snallie
315 printf("MAC: %02x:%02x:%02x:%02x:%02x:%02x\n", bd->bi_enetaddr[0],
316 bd->bi_enetaddr[1], bd->bi_enetaddr[2], bd->bi_enetaddr[3],
317 bd->bi_enetaddr[4], bd->bi_enetaddr[5]);
318 for (i = 0, oft = 0x10; i < 6; i++, oft++)
319 DM9000_iow(oft, bd->bi_enetaddr[i]);
320 for (i = 0, oft = 0x16; i < 8; i++, oft++)
321 DM9000_iow(oft, 0xff);
322
323 /* read back mac, just to be sure */
324 for (i = 0, oft = 0x10; i < 6; i++, oft++)
325 DM9000_DBG("%02x:", DM9000_ior(oft));
326 DM9000_DBG("\n");
327
328 /* Activate DM9000 */
329 DM9000_iow(DM9000_RCR, RCR_DIS_LONG | RCR_DIS_CRC | RCR_RXEN); /* RX enable */
330 DM9000_iow(DM9000_IMR, IMR_PAR); /* Enable TX/RX interrupt mask */
331 i = 0;
332 while (!(phy_read(1) & 0x20)) { /* autonegation complete bit */
333 udelay(1000);
334 i++;
335 if (i == 10000) {
336 printf("could not establish link\n");
337 return 0;
338 }
339 }
340
341 /* see what we've got */
342 lnk = phy_read(17) >> 12;
343 printf("operating at ");
344 switch (lnk) {
345 case 1:
346 printf("10M half duplex ");
347 break;
348 case 2:
349 printf("10M full duplex ");
350 break;
351 case 4:
352 printf("100M half duplex ");
353 break;
354 case 8:
355 printf("100M full duplex ");
356 break;
357 default:
358 printf("unknown: %d ", lnk);
359 break;
360 }
361 printf("mode\n");
362 return 0;
363 }
364
365 /*
366 Hardware start transmission.
367 Send a packet to media from the upper layer.
368 */
369 int
370 eth_send(volatile void *packet, int length)
371 {
372 char *data_ptr;
373 u32 tmplen, i;
374 int tmo;
375 DM9000_DBG("eth_send: length: %d\n", length);
376 for (i = 0; i < length; i++) {
377 if (i % 8 == 0)
378 DM9000_DBG("\nSend: 02x: ", i);
379 DM9000_DBG("%02x ", ((unsigned char *) packet)[i]);
380 } DM9000_DBG("\n");
381
382 /* Move data to DM9000 TX RAM */
383 data_ptr = (char *) packet;
384 DM9000_outb(DM9000_MWCMD, DM9000_IO);
385
386 #ifdef CONFIG_DM9000_USE_8BIT
387 /* Byte mode */
388 for (i = 0; i < length; i++)
389 DM9000_outb((data_ptr[i] & 0xff), DM9000_DATA);
390
391 #endif /* */
392 #ifdef CONFIG_DM9000_USE_16BIT
393 tmplen = (length + 1) / 2;
394 for (i = 0; i < tmplen; i++)
395 DM9000_outw(((u16 *) data_ptr)[i], DM9000_DATA);
396
397 #endif /* */
398 #ifdef CONFIG_DM9000_USE_32BIT
399 tmplen = (length + 3) / 4;
400 for (i = 0; i < tmplen; i++)
401 DM9000_outl(((u32 *) data_ptr)[i], DM9000_DATA);
402
403 #endif /* */
404
405 /* Set TX length to DM9000 */
406 DM9000_iow(DM9000_TXPLL, length & 0xff);
407 DM9000_iow(DM9000_TXPLH, (length >> 8) & 0xff);
408
409 /* Issue TX polling command */
410 DM9000_iow(DM9000_TCR, TCR_TXREQ); /* Cleared after TX complete */
411
412 /* wait for end of transmission */
413 tmo = get_timer(0) + 5 * CFG_HZ;
414 while (DM9000_ior(DM9000_TCR) & TCR_TXREQ) {
415 if (get_timer(0) >= tmo) {
416 printf("transmission timeout\n");
417 break;
418 }
419 }
420 DM9000_DBG("transmit done\n\n");
421 return 0;
422 }
423
424 /*
425 Stop the interface.
426 The interface is stopped when it is brought.
427 */
428 void
429 eth_halt(void)
430 {
431 DM9000_DBG("eth_halt\n");
432
433 /* RESET devie */
434 phy_write(0, 0x8000); /* PHY RESET */
435 DM9000_iow(DM9000_GPR, 0x01); /* Power-Down PHY */
436 DM9000_iow(DM9000_IMR, 0x80); /* Disable all interrupt */
437 DM9000_iow(DM9000_RCR, 0x00); /* Disable RX */
438 }
439
440 /*
441 Received a packet and pass to upper layer
442 */
443 int
444 eth_rx(void)
445 {
446 u8 rxbyte, *rdptr = (u8 *) NetRxPackets[0];
447 u16 RxStatus, RxLen = 0;
448 u32 tmplen, i;
449
450 /* Check packet ready or not */
451 DM9000_ior(DM9000_MRCMDX); /* Dummy read */
452 rxbyte = DM9000_inb(DM9000_DATA); /* Got most updated data */
453 if (rxbyte == 0)
454 return 0;
455
456 /* Status check: this byte must be 0 or 1 */
457 if (rxbyte > 1) {
458 DM9000_iow(DM9000_RCR, 0x00); /* Stop Device */
459 DM9000_iow(DM9000_ISR, 0x80); /* Stop INT request */
460 DM9000_DBG("rx status check: %d\n", rxbyte);
461 }
462 DM9000_DBG("receiving packet\n");
463
464 /* A packet ready now & Get status/length */
465 DM9000_outb(DM9000_MRCMD, DM9000_IO);
466
467 #ifdef CONFIG_DM9000_USE_8BIT
468 RxStatus = DM9000_inb(DM9000_DATA) + (DM9000_inb(DM9000_DATA) << 8);
469 RxLen = DM9000_inb(DM9000_DATA) + (DM9000_inb(DM9000_DATA) << 8);
470
471 #endif /* */
472 #ifdef CONFIG_DM9000_USE_16BIT
473 RxStatus = DM9000_inw(DM9000_DATA);
474 RxLen = DM9000_inw(DM9000_DATA);
475
476 #endif /* */
477 #ifdef CONFIG_DM9000_USE_32BIT
478 tmpdata = DM9000_inl(DM9000_DATA);
479 RxStatus = tmpdata;
480 RxLen = tmpdata >> 16;
481
482 #endif /* */
483 DM9000_DBG("rx status: 0x%04x rx len: %d\n", RxStatus, RxLen);
484
485 /* Move data from DM9000 */
486 /* Read received packet from RX SRAM */
487 #ifdef CONFIG_DM9000_USE_8BIT
488 for (i = 0; i < RxLen; i++)
489 rdptr[i] = DM9000_inb(DM9000_DATA);
490
491 #endif /* */
492 #ifdef CONFIG_DM9000_USE_16BIT
493 tmplen = (RxLen + 1) / 2;
494 for (i = 0; i < tmplen; i++)
495 ((u16 *) rdptr)[i] = DM9000_inw(DM9000_DATA);
496
497 #endif /* */
498 #ifdef CONFIG_DM9000_USE_32BIT
499 tmplen = (RxLen + 3) / 4;
500 for (i = 0; i < tmplen; i++)
501 ((u32 *) rdptr)[i] = DM9000_inl(DM9000_DATA);
502
503 #endif /* */
504 if ((RxStatus & 0xbf00) || (RxLen < 0x40)
505 || (RxLen > DM9000_PKT_MAX)) {
506 if (RxStatus & 0x100) {
507 printf("rx fifo error\n");
508 }
509 if (RxStatus & 0x200) {
510 printf("rx crc error\n");
511 }
512 if (RxStatus & 0x8000) {
513 printf("rx length error\n");
514 }
515 if (RxLen > DM9000_PKT_MAX) {
516 printf("rx length too big\n");
517 dm9000_reset();
518 }
519 } else {
520
521 /* Pass to upper layer */
522 DM9000_DBG("passing packet to upper layer\n");
523 NetReceive(NetRxPackets[0], RxLen);
524 return RxLen;
525 }
526 return 0;
527 }
528
529 /*
530 Read a word data from SROM
531 */
532 static u16
533 read_srom_word(int offset)
534 {
535 DM9000_iow(DM9000_EPAR, offset);
536 DM9000_iow(DM9000_EPCR, 0x4);
537 udelay(200);
538 DM9000_iow(DM9000_EPCR, 0x0);
539 return (DM9000_ior(DM9000_EPDRL) + (DM9000_ior(DM9000_EPDRH) << 8));
540 }
541
542 /*
543 Read a byte from I/O port
544 */
545 static u8
546 DM9000_ior(int reg)
547 {
548 DM9000_outb(reg, DM9000_IO);
549 return DM9000_inb(DM9000_DATA);
550 }
551
552 /*
553 Write a byte to I/O port
554 */
555 static void
556 DM9000_iow(int reg, u8 value)
557 {
558 DM9000_outb(reg, DM9000_IO);
559 DM9000_outb(value, DM9000_DATA);
560 }
561
562 /*
563 Read a word from phyxcer
564 */
565 static u16
566 phy_read(int reg)
567 {
568 u16 val;
569
570 /* Fill the phyxcer register into REG_0C */
571 DM9000_iow(DM9000_EPAR, DM9000_PHY | reg);
572 DM9000_iow(DM9000_EPCR, 0xc); /* Issue phyxcer read command */
573 udelay(100); /* Wait read complete */
574 DM9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer read command */
575 val = (DM9000_ior(DM9000_EPDRH) << 8) | DM9000_ior(DM9000_EPDRL);
576
577 /* The read data keeps on REG_0D & REG_0E */
578 DM9000_DBG("phy_read(%d): %d\n", reg, val);
579 return val;
580 }
581
582 /*
583 Write a word to phyxcer
584 */
585 static void
586 phy_write(int reg, u16 value)
587 {
588
589 /* Fill the phyxcer register into REG_0C */
590 DM9000_iow(DM9000_EPAR, DM9000_PHY | reg);
591
592 /* Fill the written data into REG_0D & REG_0E */
593 DM9000_iow(DM9000_EPDRL, (value & 0xff));
594 DM9000_iow(DM9000_EPDRH, ((value >> 8) & 0xff));
595 DM9000_iow(DM9000_EPCR, 0xa); /* Issue phyxcer write command */
596 udelay(500); /* Wait write complete */
597 DM9000_iow(DM9000_EPCR, 0x0); /* Clear phyxcer write command */
598 DM9000_DBG("phy_write(reg:%d, value:%d)\n", reg, value);
599 }
600 #endif /* CONFIG_DRIVER_DM9000 */
////////////
