enet.c 23 KB

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