enet.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  1. /*
  2. * (C) Copyright 2002
  3. * Adam Kowalczyk, ACK Software Controls Inc. akowalczyk@cogeco.ca
  4. *
  5. * Some portions taken from 3c59x.c Written 1996-1999 by Donald Becker.
  6. *
  7. * Outline of the program based on eepro100.c which is
  8. *
  9. * (C) Copyright 2002
  10. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <malloc.h>
  29. #include <net.h>
  30. #include <asm/io.h>
  31. #include <pci.h>
  32. #include "articiaS.h"
  33. #include "memio.h"
  34. /* 3Com Ethernet PCI definitions*/
  35. /* #define PCI_VENDOR_ID_3COM 0x10B7 */
  36. #define PCI_DEVICE_ID_3COM_3C905C 0x9200
  37. /* 3Com Commands, top 5 bits are command and bottom 11 bits are parameters */
  38. #define TotalReset (0<<11)
  39. #define SelectWindow (1<<11)
  40. #define StartCoax (2<<11)
  41. #define RxDisable (3<<11)
  42. #define RxEnable (4<<11)
  43. #define RxReset (5<<11)
  44. #define UpStall (6<<11)
  45. #define UpUnstall (6<<11)+1
  46. #define DownStall (6<<11)+2
  47. #define DownUnstall (6<<11)+3
  48. #define RxDiscard (8<<11)
  49. #define TxEnable (9<<11)
  50. #define TxDisable (10<<11)
  51. #define TxReset (11<<11)
  52. #define FakeIntr (12<<11)
  53. #define AckIntr (13<<11)
  54. #define SetIntrEnb (14<<11)
  55. #define SetStatusEnb (15<<11)
  56. #define SetRxFilter (16<<11)
  57. #define SetRxThreshold (17<<11)
  58. #define SetTxThreshold (18<<11)
  59. #define SetTxStart (19<<11)
  60. #define StartDMAUp (20<<11)
  61. #define StartDMADown (20<<11)+1
  62. #define StatsEnable (21<<11)
  63. #define StatsDisable (22<<11)
  64. #define StopCoax (23<<11)
  65. #define SetFilterBit (25<<11)
  66. /* The SetRxFilter command accepts the following classes */
  67. #define RxStation 1
  68. #define RxMulticast 2
  69. #define RxBroadcast 4
  70. #define RxProm 8
  71. /* 3Com status word defnitions */
  72. #define IntLatch 0x0001
  73. #define HostError 0x0002
  74. #define TxComplete 0x0004
  75. #define TxAvailable 0x0008
  76. #define RxComplete 0x0010
  77. #define RxEarly 0x0020
  78. #define IntReq 0x0040
  79. #define StatsFull 0x0080
  80. #define DMADone (1<<8)
  81. #define DownComplete (1<<9)
  82. #define UpComplete (1<<10)
  83. #define DMAInProgress (1<<11) /* DMA controller is still busy.*/
  84. #define CmdInProgress (1<<12) /* EL3_CMD is still busy.*/
  85. /* Polling Registers */
  86. #define DnPoll 0x2d
  87. #define UpPoll 0x3d
  88. /* Register window 0 offets */
  89. #define Wn0EepromCmd 10 /* Window 0: EEPROM command register. */
  90. #define Wn0EepromData 12 /* Window 0: EEPROM results register. */
  91. #define IntrStatus 0x0E /* Valid in all windows. */
  92. /* Register window 0 EEPROM bits */
  93. #define EEPROM_Read 0x80
  94. #define EEPROM_WRITE 0x40
  95. #define EEPROM_ERASE 0xC0
  96. #define EEPROM_EWENB 0x30 /* Enable erasing/writing for 10 msec. */
  97. #define EEPROM_EWDIS 0x00 /* Disable EWENB before 10 msec timeout. */
  98. /* EEPROM locations. */
  99. #define PhysAddr01 0
  100. #define PhysAddr23 1
  101. #define PhysAddr45 2
  102. #define ModelID 3
  103. #define EtherLink3ID 7
  104. #define IFXcvrIO 8
  105. #define IRQLine 9
  106. #define NodeAddr01 10
  107. #define NodeAddr23 11
  108. #define NodeAddr45 12
  109. #define DriverTune 13
  110. #define Checksum 15
  111. /* Register window 1 offsets, the window used in normal operation */
  112. #define TX_FIFO 0x10
  113. #define RX_FIFOa 0x10
  114. #define RxErrors 0x14
  115. #define RxStatus 0x18
  116. #define Timer 0x1A
  117. #define TxStatus 0x1B
  118. #define TxFree 0x1C /* Remaining free bytes in Tx buffer. */
  119. /* Register Window 2 */
  120. #define Wn2_ResetOptions 12
  121. /* Register Window 3: MAC/config bits */
  122. #define Wn3_Config 0 /* Internal Configuration */
  123. #define Wn3_MAC_Ctrl 6
  124. #define Wn3_Options 8
  125. #define BFEXT(value, offset, bitcount) \
  126. ((((unsigned long)(value)) >> (offset)) & ((1 << (bitcount)) - 1))
  127. #define BFINS(lhs, rhs, offset, bitcount) \
  128. (((lhs) & ~((((1 << (bitcount)) - 1)) << (offset))) | \
  129. (((rhs) & ((1 << (bitcount)) - 1)) << (offset)))
  130. #define RAM_SIZE(v) BFEXT(v, 0, 3)
  131. #define RAM_WIDTH(v) BFEXT(v, 3, 1)
  132. #define RAM_SPEED(v) BFEXT(v, 4, 2)
  133. #define ROM_SIZE(v) BFEXT(v, 6, 2)
  134. #define RAM_SPLIT(v) BFEXT(v, 16, 2)
  135. #define XCVR(v) BFEXT(v, 20, 4)
  136. #define AUTOSELECT(v) BFEXT(v, 24, 1)
  137. /* Register Window 4: Xcvr/media bits */
  138. #define Wn4_FIFODiag 4
  139. #define Wn4_NetDiag 6
  140. #define Wn4_PhysicalMgmt 8
  141. #define Wn4_Media 10
  142. #define Media_SQE 0x0008 /* Enable SQE error counting for AUI. */
  143. #define Media_10TP 0x00C0 /* Enable link beat and jabber for 10baseT. */
  144. #define Media_Lnk 0x0080 /* Enable just link beat for 100TX/100FX. */
  145. #define Media_LnkBeat 0x0800
  146. /* Register Window 7: Bus Master control */
  147. #define Wn7_MasterAddr 0
  148. #define Wn7_MasterLen 6
  149. #define Wn7_MasterStatus 12
  150. /* Boomerang bus master control registers. */
  151. #define PktStatus 0x20
  152. #define DownListPtr 0x24
  153. #define FragAddr 0x28
  154. #define FragLen 0x2c
  155. #define TxFreeThreshold 0x2f
  156. #define UpPktStatus 0x30
  157. #define UpListPtr 0x38
  158. /* The Rx and Tx descriptor lists. */
  159. #define LAST_FRAG 0x80000000 /* Last Addr/Len pair in descriptor. */
  160. #define DN_COMPLETE 0x00010000 /* This packet has been downloaded */
  161. struct rx_desc_3com {
  162. u32 next; /* Last entry points to 0 */
  163. u32 status; /* FSH -> Frame Start Header */
  164. u32 addr; /* Up to 63 addr/len pairs possible */
  165. u32 length; /* Set LAST_FRAG to indicate last pair */
  166. };
  167. /* Values for the Rx status entry. */
  168. #define RxDComplete 0x00008000
  169. #define RxDError 0x4000
  170. #define IPChksumErr (1<<25)
  171. #define TCPChksumErr (1<<26)
  172. #define UDPChksumErr (1<<27)
  173. #define IPChksumValid (1<<29)
  174. #define TCPChksumValid (1<<30)
  175. #define UDPChksumValid (1<<31)
  176. struct tx_desc_3com {
  177. u32 next; /* Last entry points to 0 */
  178. u32 status; /* bits 0:12 length, others see below */
  179. u32 addr;
  180. u32 length;
  181. };
  182. /* Values for the Tx status entry. */
  183. #define CRCDisable 0x2000
  184. #define TxDComplete 0x8000
  185. #define AddIPChksum 0x02000000
  186. #define AddTCPChksum 0x04000000
  187. #define AddUDPChksum 0x08000000
  188. #define TxIntrUploaded 0x80000000 /* IRQ when in FIFO, but maybe not sent. */
  189. /* XCVR Types */
  190. #define XCVR_10baseT 0
  191. #define XCVR_AUI 1
  192. #define XCVR_10baseTOnly 2
  193. #define XCVR_10base2 3
  194. #define XCVR_100baseTx 4
  195. #define XCVR_100baseFx 5
  196. #define XCVR_MII 6
  197. #define XCVR_NWAY 8
  198. #define XCVR_ExtMII 9
  199. #define XCVR_Default 10 /* I don't think this is correct -> should have been 0x10 if Auto Negotiate */
  200. struct descriptor { /* A generic descriptor. */
  201. u32 next; /* Last entry points to 0 */
  202. u32 status; /* FSH -> Frame Start Header */
  203. u32 addr; /* Up to 63 addr/len pairs possible */
  204. u32 length; /* Set LAST_FRAG to indicate last pair */
  205. };
  206. /* Misc. definitions */
  207. #define NUM_RX_DESC PKTBUFSRX * 10
  208. #define NUM_TX_DESC 1 /* Number of TX descriptors */
  209. #define TOUT_LOOP 1000000
  210. #define ETH_ALEN 6
  211. #define EL3WINDOW(dev, win_num) ETH_OUTW(dev, SelectWindow + (win_num), EL3_CMD)
  212. #define EL3_CMD 0x0e
  213. #define EL3_STATUS 0x0e
  214. #undef ETH_DEBUG
  215. #ifdef ETH_DEBUG
  216. #define PRINTF(fmt,args...) printf (fmt ,##args)
  217. #else
  218. #define PRINTF(fmt,args...)
  219. #endif
  220. static struct rx_desc_3com *rx_ring; /* RX descriptor ring */
  221. static struct tx_desc_3com *tx_ring; /* TX descriptor ring */
  222. static u8 rx_buffer[NUM_RX_DESC][PKTSIZE_ALIGN];/* storage for the incoming messages */
  223. static int rx_next = 0; /* RX descriptor ring pointer */
  224. static int tx_next = 0; /* TX descriptor ring pointer */
  225. static int tx_threshold;
  226. static void init_rx_ring(struct eth_device* dev);
  227. static void purge_tx_ring(struct eth_device* dev);
  228. static void read_hw_addr(struct eth_device* dev, bd_t * bis);
  229. static int eth_3com_init(struct eth_device* dev, bd_t *bis);
  230. static int eth_3com_send(struct eth_device* dev, volatile void *packet, int length);
  231. static int eth_3com_recv(struct eth_device* dev);
  232. static void eth_3com_halt(struct eth_device* dev);
  233. #define io_to_phys(a) pci_io_to_phys((pci_dev_t)dev->priv, a)
  234. #define phys_to_io(a) pci_phys_to_io((pci_dev_t)dev->priv, a)
  235. #define mem_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a)
  236. #define phys_to_mem(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
  237. static inline int ETH_INL(struct eth_device* dev, u_long addr)
  238. {
  239. __asm volatile ("eieio");
  240. return le32_to_cpu(*(volatile u32 *)io_to_phys(addr + dev->iobase));
  241. }
  242. static inline int ETH_INW(struct eth_device* dev, u_long addr)
  243. {
  244. __asm volatile ("eieio");
  245. return le16_to_cpu(*(volatile u16 *)io_to_phys(addr + dev->iobase));
  246. }
  247. static inline int ETH_INB(struct eth_device* dev, u_long addr)
  248. {
  249. __asm volatile ("eieio");
  250. return *(volatile u8 *)io_to_phys(addr + dev->iobase);
  251. }
  252. static inline void ETH_OUTB(struct eth_device* dev, int command, u_long addr)
  253. {
  254. *(volatile u8 *)io_to_phys(addr + dev->iobase) = command;
  255. __asm volatile ("eieio");
  256. }
  257. static inline void ETH_OUTW(struct eth_device* dev, int command, u_long addr)
  258. {
  259. *(volatile u16 *)io_to_phys(addr + dev->iobase) = cpu_to_le16(command);
  260. __asm volatile ("eieio");
  261. }
  262. static inline void ETH_OUTL(struct eth_device* dev, int command, u_long addr)
  263. {
  264. *(volatile u32 *)io_to_phys(addr + dev->iobase) = cpu_to_le32(command);
  265. __asm volatile ("eieio");
  266. }
  267. static inline int ETH_STATUS(struct eth_device* dev)
  268. {
  269. __asm volatile ("eieio");
  270. return le16_to_cpu(*(volatile u16 *)io_to_phys(EL3_STATUS + dev->iobase));
  271. }
  272. static inline void ETH_CMD(struct eth_device* dev, int command)
  273. {
  274. *(volatile u16 *)io_to_phys(EL3_CMD + dev->iobase) = cpu_to_le16(command);
  275. __asm volatile ("eieio");
  276. }
  277. /* Command register is always in the same spot in all the register windows */
  278. /* This function issues a command and waits for it so complete by checking the CmdInProgress bit */
  279. static int issue_and_wait(struct eth_device* dev, int command)
  280. {
  281. int i, status;
  282. ETH_CMD(dev, command);
  283. for (i = 0; i < 2000; i++) {
  284. status = ETH_STATUS(dev);
  285. /*printf ("Issue: status 0x%4x.\n", status); */
  286. if (!(status & CmdInProgress))
  287. return 1;
  288. }
  289. /* OK, that didn't work. Do it the slow way. One second */
  290. for (i = 0; i < 100000; i++) {
  291. status = ETH_STATUS(dev);
  292. /*printf ("Issue: status 0x%4x.\n", status); */
  293. return 1;
  294. udelay(10);
  295. }
  296. PRINTF("Ethernet command: 0x%4x did not complete! Status: 0x%4x\n", command, ETH_STATUS(dev) );
  297. return 0;
  298. }
  299. /* Determine network media type and set up 3com accordingly */
  300. /* I think I'm going to start with something known first like 10baseT */
  301. static int auto_negotiate (struct eth_device *dev)
  302. {
  303. int i;
  304. EL3WINDOW (dev, 1);
  305. /* Wait for Auto negotiation to complete */
  306. for (i = 0; i <= 1000; i++) {
  307. if (ETH_INW (dev, 2) & 0x04)
  308. break;
  309. udelay (100);
  310. if (i == 1000) {
  311. PRINTF ("Error: Auto negotiation failed\n");
  312. return 0;
  313. }
  314. }
  315. return 1;
  316. }
  317. void eth_interrupt (struct eth_device *dev)
  318. {
  319. u16 status = ETH_STATUS (dev);
  320. printf ("eth0: status = 0x%04x\n", status);
  321. if (!(status & IntLatch))
  322. return;
  323. if (status & (1 << 6)) {
  324. ETH_CMD (dev, AckIntr | (1 << 6));
  325. printf ("Acknowledged Interrupt command\n");
  326. }
  327. if (status & DownComplete) {
  328. ETH_CMD (dev, AckIntr | DownComplete);
  329. printf ("Acknowledged DownComplete\n");
  330. }
  331. if (status & UpComplete) {
  332. ETH_CMD (dev, AckIntr | UpComplete);
  333. printf ("Acknowledged UpComplete\n");
  334. }
  335. ETH_CMD (dev, AckIntr | IntLatch);
  336. printf ("Acknowledged IntLatch\n");
  337. }
  338. int eth_3com_initialize (bd_t * bis)
  339. {
  340. u32 eth_iobase = 0, status;
  341. int card_number = 0, ret;
  342. struct eth_device *dev;
  343. pci_dev_t devno;
  344. char *s;
  345. s = getenv ("3com_base");
  346. /* Find ethernet controller on the PCI bus */
  347. if ((devno =
  348. pci_find_device (PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C905C,
  349. 0)) < 0) {
  350. PRINTF ("Error: Cannot find the ethernet device on the PCI bus\n");
  351. goto Done;
  352. }
  353. if (s) {
  354. unsigned long base = atoi (s);
  355. pci_write_config_dword (devno, PCI_BASE_ADDRESS_0,
  356. base | 0x01);
  357. }
  358. ret = pci_read_config_dword (devno, PCI_BASE_ADDRESS_0, &eth_iobase);
  359. eth_iobase &= ~0xf;
  360. PRINTF ("eth: 3Com Found at Address: 0x%x\n", eth_iobase);
  361. pci_write_config_dword (devno, PCI_COMMAND,
  362. PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  363. PCI_COMMAND_MASTER);
  364. /* Check if I/O accesses and Bus Mastering are enabled */
  365. ret = pci_read_config_dword (devno, PCI_COMMAND, &status);
  366. if (!(status & PCI_COMMAND_IO)) {
  367. printf ("Error: Cannot enable IO access.\n");
  368. goto Done;
  369. }
  370. if (!(status & PCI_COMMAND_MEMORY)) {
  371. printf ("Error: Cannot enable MEMORY access.\n");
  372. goto Done;
  373. }
  374. if (!(status & PCI_COMMAND_MASTER)) {
  375. printf ("Error: Cannot enable Bus Mastering.\n");
  376. goto Done;
  377. }
  378. dev = (struct eth_device *) malloc (sizeof (*dev)); /*struct eth_device)); */
  379. sprintf (dev->name, "3Com 3c920c#%d", card_number);
  380. dev->iobase = eth_iobase;
  381. dev->priv = (void *) devno;
  382. dev->init = eth_3com_init;
  383. dev->halt = eth_3com_halt;
  384. dev->send = eth_3com_send;
  385. dev->recv = eth_3com_recv;
  386. eth_register (dev);
  387. /* { */
  388. /* char interrupt; */
  389. /* devno = pci_find_device(PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C905C, 0); */
  390. /* pci_read_config_byte(devno, PCI_INTERRUPT_LINE, &interrupt); */
  391. /* printf("Installing eth0 interrupt handler to %d\n", interrupt); */
  392. /* irq_install_handler(interrupt, eth_interrupt, dev); */
  393. /* } */
  394. card_number++;
  395. /* Set the latency timer for value */
  396. s = getenv ("3com_latency");
  397. if (s) {
  398. ret = pci_write_config_byte (devno, PCI_LATENCY_TIMER,
  399. (unsigned char) atoi (s));
  400. } else
  401. ret = pci_write_config_byte (devno, PCI_LATENCY_TIMER, 0x0a);
  402. read_hw_addr (dev, bis); /* get the MAC address from Window 2 */
  403. /* Reset the ethernet controller */
  404. PRINTF ("Issuing reset command....\n");
  405. if (!issue_and_wait (dev, TotalReset)) {
  406. printf ("Error: Cannot reset ethernet controller.\n");
  407. goto Done;
  408. } else
  409. PRINTF ("Ethernet controller reset.\n");
  410. /* allocate memory for rx and tx rings */
  411. if (!(rx_ring = memalign (sizeof (struct rx_desc_3com) * NUM_RX_DESC, 16))) {
  412. PRINTF ("Cannot allocate memory for RX_RING.....\n");
  413. goto Done;
  414. }
  415. if (!(tx_ring = memalign (sizeof (struct tx_desc_3com) * NUM_TX_DESC, 16))) {
  416. PRINTF ("Cannot allocate memory for TX_RING.....\n");
  417. goto Done;
  418. }
  419. Done:
  420. return status;
  421. }
  422. static int eth_3com_init (struct eth_device *dev, bd_t * bis)
  423. {
  424. int i, status = 0;
  425. int tx_cur, loop;
  426. u16 status_enable, intr_enable;
  427. struct descriptor *ias_cmd;
  428. /* Determine what type of network the machine is connected to */
  429. /* presently drops the connect to 10Mbps */
  430. if (!auto_negotiate (dev)) {
  431. printf ("Error: Cannot determine network media.\n");
  432. goto Done;
  433. }
  434. issue_and_wait (dev, TxReset);
  435. issue_and_wait (dev, RxReset | 0x04);
  436. /* Switch to register set 7 for normal use. */
  437. EL3WINDOW (dev, 7);
  438. /* Initialize Rx and Tx rings */
  439. init_rx_ring (dev);
  440. purge_tx_ring (dev);
  441. ETH_CMD (dev, SetRxFilter | RxStation | RxBroadcast | RxProm);
  442. issue_and_wait (dev, SetTxStart | 0x07ff);
  443. /* Below sets which indication bits to be seen. */
  444. status_enable =
  445. SetStatusEnb | HostError | DownComplete | UpComplete | (1 <<
  446. 6);
  447. ETH_CMD (dev, status_enable);
  448. /* Below sets no bits are to cause an interrupt since this is just polling */
  449. intr_enable = SetIntrEnb;
  450. /* intr_enable = SetIntrEnb | (1<<9) | (1<<10) | (1<<6); */
  451. ETH_CMD (dev, intr_enable);
  452. ETH_OUTB (dev, 127, UpPoll);
  453. /* Ack all pending events, and set active indicator mask */
  454. ETH_CMD (dev, AckIntr | IntLatch | TxAvailable | RxEarly | IntReq);
  455. ETH_CMD (dev, intr_enable);
  456. /* Tell the adapter where the RX ring is located */
  457. issue_and_wait (dev, UpStall); /* Stall and set the UplistPtr */
  458. ETH_OUTL (dev, (u32) & rx_ring[rx_next], UpListPtr);
  459. ETH_CMD (dev, RxEnable); /* Enable the receiver. */
  460. issue_and_wait (dev, UpUnstall);
  461. /* Send the Individual Address Setup frame */
  462. tx_cur = tx_next;
  463. tx_next = ((tx_next + 1) % NUM_TX_DESC);
  464. ias_cmd = (struct descriptor *) &tx_ring[tx_cur];
  465. ias_cmd->status = cpu_to_le32 (1 << 31); /* set DnIndicate bit. */
  466. ias_cmd->next = 0;
  467. ias_cmd->addr = cpu_to_le32 ((u32) & bis->bi_enetaddr[0]);
  468. ias_cmd->length = cpu_to_le32 (6 | LAST_FRAG);
  469. /* Tell the adapter where the TX ring is located */
  470. ETH_CMD (dev, TxEnable); /* Enable transmitter. */
  471. issue_and_wait (dev, DownStall); /* Stall and set the DownListPtr. */
  472. ETH_OUTL (dev, (u32) & tx_ring[tx_cur], DownListPtr);
  473. issue_and_wait (dev, DownUnstall);
  474. for (i = 0; !(ETH_STATUS (dev) & DownComplete); i++) {
  475. if (i >= TOUT_LOOP) {
  476. PRINTF ("TX Ring status (Init): 0x%4x\n",
  477. le32_to_cpu (tx_ring[tx_cur].status));
  478. PRINTF ("ETH_STATUS: 0x%x\n", ETH_STATUS (dev));
  479. goto Done;
  480. }
  481. }
  482. if (ETH_STATUS (dev) & DownComplete) { /* If DownLoad Complete ACK the bit */
  483. ETH_CMD (dev, AckIntr | DownComplete); /* acknowledge the indication bit */
  484. issue_and_wait (dev, DownStall); /* stall and clear DownListPtr */
  485. ETH_OUTL (dev, 0, DownListPtr);
  486. issue_and_wait (dev, DownUnstall);
  487. }
  488. status = 1;
  489. Done:
  490. return status;
  491. }
  492. int eth_3com_send (struct eth_device *dev, volatile void *packet, int length)
  493. {
  494. int i, status = 0;
  495. int tx_cur;
  496. if (length <= 0) {
  497. PRINTF ("eth: bad packet size: %d\n", length);
  498. goto Done;
  499. }
  500. tx_cur = tx_next;
  501. tx_next = (tx_next + 1) % NUM_TX_DESC;
  502. tx_ring[tx_cur].status = cpu_to_le32 (1 << 31); /* set DnIndicate bit */
  503. tx_ring[tx_cur].next = 0;
  504. tx_ring[tx_cur].addr = cpu_to_le32 (((u32) packet));
  505. tx_ring[tx_cur].length = cpu_to_le32 (length | LAST_FRAG);
  506. /* Send the packet */
  507. issue_and_wait (dev, DownStall); /* stall and set the DownListPtr */
  508. ETH_OUTL (dev, (u32) & tx_ring[tx_cur], DownListPtr);
  509. issue_and_wait (dev, DownUnstall);
  510. for (i = 0; !(ETH_STATUS (dev) & DownComplete); i++) {
  511. if (i >= TOUT_LOOP) {
  512. PRINTF ("TX Ring status (send): 0x%4x\n",
  513. le32_to_cpu (tx_ring[tx_cur].status));
  514. goto Done;
  515. }
  516. }
  517. if (ETH_STATUS (dev) & DownComplete) { /* If DownLoad Complete ACK the bit */
  518. ETH_CMD (dev, AckIntr | DownComplete); /* acknowledge the indication bit */
  519. issue_and_wait (dev, DownStall); /* stall and clear DownListPtr */
  520. ETH_OUTL (dev, 0, DownListPtr);
  521. issue_and_wait (dev, DownUnstall);
  522. }
  523. status = 1;
  524. Done:
  525. return status;
  526. }
  527. void PrintPacket (uchar * packet, int length)
  528. {
  529. int loop;
  530. uchar *ptr;
  531. printf ("Printing packet of length %x.\n\n", length);
  532. ptr = packet;
  533. for (loop = 1; loop <= length; loop++) {
  534. printf ("%2x ", *ptr++);
  535. if ((loop % 40) == 0)
  536. printf ("\n");
  537. }
  538. }
  539. int eth_3com_recv (struct eth_device *dev)
  540. {
  541. u16 stat = 0;
  542. u32 status;
  543. int rx_prev, length = 0;
  544. while (!(ETH_STATUS (dev) & UpComplete)) /* wait on receipt of packet */
  545. ;
  546. status = le32_to_cpu (rx_ring[rx_next].status); /* packet status */
  547. while (status & (1 << 15)) {
  548. /* A packet has been received */
  549. if (status & (1 << 15)) {
  550. /* A valid frame received */
  551. length = le32_to_cpu (rx_ring[rx_next].status) & 0x1fff; /* length is in bits 0 - 12 */
  552. /* Pass the packet up to the protocol layers */
  553. NetReceive ((uchar *)
  554. le32_to_cpu (rx_ring[rx_next].addr),
  555. length);
  556. rx_ring[rx_next].status = 0; /* clear the status word */
  557. ETH_CMD (dev, AckIntr | UpComplete);
  558. issue_and_wait (dev, UpUnstall);
  559. } else if (stat & HostError) {
  560. /* There was an error */
  561. printf ("Rx error status: 0x%4x\n", stat);
  562. init_rx_ring (dev);
  563. goto Done;
  564. }
  565. rx_prev = rx_next;
  566. rx_next = (rx_next + 1) % NUM_RX_DESC;
  567. stat = ETH_STATUS (dev); /* register status */
  568. status = le32_to_cpu (rx_ring[rx_next].status); /* packet status */
  569. }
  570. Done:
  571. return length;
  572. }
  573. void eth_3com_halt (struct eth_device *dev)
  574. {
  575. if (!(dev->iobase)) {
  576. goto Done;
  577. }
  578. issue_and_wait (dev, DownStall); /* shut down transmit and receive */
  579. issue_and_wait (dev, UpStall);
  580. issue_and_wait (dev, RxDisable);
  581. issue_and_wait (dev, TxDisable);
  582. /* free(tx_ring); /###* release memory allocated to the DPD and UPD rings */
  583. /* free(rx_ring); */
  584. Done:
  585. return;
  586. }
  587. static void init_rx_ring (struct eth_device *dev)
  588. {
  589. int i;
  590. PRINTF ("Initializing rx_ring. rx_buffer = %p\n", rx_buffer);
  591. issue_and_wait (dev, UpStall);
  592. for (i = 0; i < NUM_RX_DESC; i++) {
  593. rx_ring[i].next =
  594. cpu_to_le32 (((u32) &
  595. rx_ring[(i + 1) % NUM_RX_DESC]));
  596. rx_ring[i].status = 0;
  597. rx_ring[i].addr = cpu_to_le32 (((u32) & rx_buffer[i][0]));
  598. rx_ring[i].length = cpu_to_le32 (PKTSIZE_ALIGN | LAST_FRAG);
  599. }
  600. rx_next = 0;
  601. }
  602. static void purge_tx_ring (struct eth_device *dev)
  603. {
  604. int i;
  605. PRINTF ("Purging tx_ring.\n");
  606. tx_next = 0;
  607. for (i = 0; i < NUM_TX_DESC; i++) {
  608. tx_ring[i].next = 0;
  609. tx_ring[i].status = 0;
  610. tx_ring[i].addr = 0;
  611. tx_ring[i].length = 0;
  612. }
  613. }
  614. static void read_hw_addr (struct eth_device *dev, bd_t * bis)
  615. {
  616. u8 hw_addr[ETH_ALEN];
  617. unsigned int eeprom[0x40];
  618. unsigned int checksum = 0;
  619. int i, j, timer;
  620. /* Read the station address from the EEPROM. */
  621. EL3WINDOW (dev, 0);
  622. for (i = 0; i < 0x40; i++) {
  623. ETH_OUTW (dev, EEPROM_Read + i, Wn0EepromCmd);
  624. /* Pause for at least 162 us. for the read to take place. */
  625. for (timer = 10; timer >= 0; timer--) {
  626. udelay (162);
  627. if ((ETH_INW (dev, Wn0EepromCmd) & 0x8000) == 0)
  628. break;
  629. }
  630. eeprom[i] = ETH_INW (dev, Wn0EepromData);
  631. }
  632. /* Checksum calculation. I'm not sure about this part and there seems to be a bug on the 3com side of things */
  633. for (i = 0; i < 0x21; i++)
  634. checksum ^= eeprom[i];
  635. checksum = (checksum ^ (checksum >> 8)) & 0xff;
  636. if (checksum != 0xbb)
  637. printf (" *** INVALID EEPROM CHECKSUM %4.4x *** \n",
  638. checksum);
  639. for (i = 0, j = 0; i < 3; i++) {
  640. hw_addr[j++] = (u8) ((eeprom[i + 10] >> 8) & 0xff);
  641. hw_addr[j++] = (u8) (eeprom[i + 10] & 0xff);
  642. }
  643. /* MAC Address is in window 2, write value from EEPROM to window 2 */
  644. EL3WINDOW (dev, 2);
  645. for (i = 0; i < 6; i++)
  646. ETH_OUTB (dev, hw_addr[i], i);
  647. for (j = 0; j < ETH_ALEN; j += 2) {
  648. hw_addr[j] = (u8) (ETH_INW (dev, j) & 0xff);
  649. hw_addr[j + 1] = (u8) ((ETH_INW (dev, j) >> 8) & 0xff);
  650. }
  651. for (i = 0; i < ETH_ALEN; i++) {
  652. if (hw_addr[i] != bis->bi_enetaddr[i]) {
  653. /* printf("Warning: HW address don't match:\n"); */
  654. /* printf("Address in 3Com Window 2 is " */
  655. /* "%02X:%02X:%02X:%02X:%02X:%02X\n", */
  656. /* hw_addr[0], hw_addr[1], hw_addr[2], */
  657. /* hw_addr[3], hw_addr[4], hw_addr[5]); */
  658. /* printf("Address used by U-Boot is " */
  659. /* "%02X:%02X:%02X:%02X:%02X:%02X\n", */
  660. /* bis->bi_enetaddr[0], bis->bi_enetaddr[1], */
  661. /* bis->bi_enetaddr[2], bis->bi_enetaddr[3], */
  662. /* bis->bi_enetaddr[4], bis->bi_enetaddr[5]); */
  663. /* goto Done; */
  664. char buffer[256];
  665. if (bis->bi_enetaddr[0] == 0
  666. && bis->bi_enetaddr[1] == 0
  667. && bis->bi_enetaddr[2] == 0
  668. && bis->bi_enetaddr[3] == 0
  669. && bis->bi_enetaddr[4] == 0
  670. && bis->bi_enetaddr[5] == 0) {
  671. sprintf (buffer,
  672. "%02X:%02X:%02X:%02X:%02X:%02X",
  673. hw_addr[0], hw_addr[1], hw_addr[2],
  674. hw_addr[3], hw_addr[4], hw_addr[5]);
  675. setenv ("ethaddr", buffer);
  676. }
  677. }
  678. }
  679. for (i = 0; i < ETH_ALEN; i++)
  680. dev->enetaddr[i] = hw_addr[i];
  681. Done:
  682. return;
  683. }