enet.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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_FIFO 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. {
  308. if (ETH_INW(dev, 2) & 0x04)
  309. break;
  310. udelay(100);
  311. if (i == 1000)
  312. {
  313. PRINTF("Error: Auto negotiation failed\n");
  314. return 0;
  315. }
  316. }
  317. return 1;
  318. }
  319. void eth_interrupt(struct eth_device *dev)
  320. {
  321. u16 status = ETH_STATUS(dev);
  322. printf("eth0: status = 0x%04x\n", status);
  323. if (!(status & IntLatch))
  324. return;
  325. if (status & (1<<6))
  326. {
  327. ETH_CMD(dev, AckIntr | (1<<6));
  328. printf("Acknowledged Interrupt command\n");
  329. }
  330. if (status & DownComplete)
  331. {
  332. ETH_CMD(dev, AckIntr | DownComplete);
  333. printf("Acknowledged DownComplete\n");
  334. }
  335. if (status & UpComplete)
  336. {
  337. ETH_CMD(dev, AckIntr | UpComplete);
  338. printf("Acknowledged UpComplete\n");
  339. }
  340. ETH_CMD(dev, AckIntr | IntLatch);
  341. printf("Acknowledged IntLatch\n");
  342. }
  343. int eth_3com_initialize(bd_t *bis)
  344. {
  345. u32 eth_iobase = 0, status;
  346. int card_number = 0, ret;
  347. struct eth_device* dev;
  348. pci_dev_t devno;
  349. char *s;
  350. s = getenv("3com_base");
  351. /* Find ethernet controller on the PCI bus */
  352. if ((devno = pci_find_device(PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C905C, 0)) < 0)
  353. {
  354. PRINTF("Error: Cannot find the ethernet device on the PCI bus\n");
  355. goto Done;
  356. }
  357. if (s)
  358. {
  359. unsigned long base = atoi(s);
  360. pci_write_config_dword(devno, PCI_BASE_ADDRESS_0, base | 0x01);
  361. }
  362. ret = pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &eth_iobase);
  363. eth_iobase &= ~0xf;
  364. PRINTF("eth: 3Com Found at Address: 0x%x\n", eth_iobase);
  365. pci_write_config_dword(devno, PCI_COMMAND, PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  366. /* Check if I/O accesses and Bus Mastering are enabled */
  367. ret = pci_read_config_dword(devno, PCI_COMMAND, &status);
  368. if (!(status & PCI_COMMAND_IO))
  369. {
  370. printf("Error: Cannot enable IO access.\n");
  371. goto Done;
  372. }
  373. if (!(status & PCI_COMMAND_MEMORY))
  374. {
  375. printf("Error: Cannot enable MEMORY access.\n");
  376. goto Done;
  377. }
  378. if (!(status & PCI_COMMAND_MASTER))
  379. {
  380. printf("Error: Cannot enable Bus Mastering.\n");
  381. goto Done;
  382. }
  383. dev = (struct eth_device*) malloc(sizeof(*dev)); /*struct eth_device)); */
  384. sprintf(dev->name, "3Com 3c920c#%d", card_number);
  385. dev->iobase = eth_iobase;
  386. dev->priv = (void*) devno;
  387. dev->init = eth_3com_init;
  388. dev->halt = eth_3com_halt;
  389. dev->send = eth_3com_send;
  390. dev->recv = eth_3com_recv;
  391. eth_register(dev);
  392. /* { */
  393. /* char interrupt; */
  394. /* devno = pci_find_device(PCI_VENDOR_ID_3COM, PCI_DEVICE_ID_3COM_3C905C, 0); */
  395. /* pci_read_config_byte(devno, PCI_INTERRUPT_LINE, &interrupt); */
  396. /* printf("Installing eth0 interrupt handler to %d\n", interrupt); */
  397. /* irq_install_handler(interrupt, eth_interrupt, dev); */
  398. /* } */
  399. card_number++;
  400. /* Set the latency timer for value */
  401. s = getenv("3com_latency");
  402. if (s)
  403. {
  404. ret = pci_write_config_byte(devno, PCI_LATENCY_TIMER, (unsigned char)atoi(s));
  405. }
  406. else ret = pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x0a);
  407. read_hw_addr(dev, bis); /* get the MAC address from Window 2*/
  408. /* Reset the ethernet controller */
  409. PRINTF ("Issuing reset command....\n");
  410. if (!issue_and_wait(dev, TotalReset))
  411. {
  412. printf("Error: Cannot reset ethernet controller.\n");
  413. goto Done;
  414. }
  415. else
  416. PRINTF ("Ethernet controller reset.\n");
  417. /* allocate memory for rx and tx rings */
  418. if(!(rx_ring = memalign(sizeof(struct rx_desc_3com) * NUM_RX_DESC, 16)))
  419. {
  420. PRINTF ("Cannot allocate memory for RX_RING.....\n");
  421. goto Done;
  422. }
  423. if (!(tx_ring = memalign(sizeof(struct tx_desc_3com) * NUM_TX_DESC, 16)))
  424. {
  425. PRINTF ("Cannot allocate memory for TX_RING.....\n");
  426. goto Done;
  427. }
  428. Done:
  429. return status;
  430. }
  431. static int eth_3com_init(struct eth_device* dev, bd_t *bis)
  432. {
  433. int i, status = 0;
  434. int tx_cur, loop;
  435. u16 status_enable, intr_enable;
  436. struct descriptor *ias_cmd;
  437. /* Determine what type of network the machine is connected to */
  438. /* presently drops the connect to 10Mbps */
  439. if (!auto_negotiate(dev))
  440. {
  441. printf("Error: Cannot determine network media.\n");
  442. goto Done;
  443. }
  444. issue_and_wait(dev, TxReset);
  445. issue_and_wait(dev, RxReset|0x04);
  446. /* Switch to register set 7 for normal use. */
  447. EL3WINDOW(dev, 7);
  448. /* Initialize Rx and Tx rings */
  449. init_rx_ring(dev);
  450. purge_tx_ring(dev);
  451. ETH_CMD(dev, SetRxFilter | RxStation | RxBroadcast | RxProm);
  452. issue_and_wait(dev,SetTxStart|0x07ff);
  453. /* Below sets which indication bits to be seen. */
  454. status_enable = SetStatusEnb | HostError | DownComplete | UpComplete | (1<<6);
  455. ETH_CMD(dev, status_enable);
  456. /* Below sets no bits are to cause an interrupt since this is just polling */
  457. intr_enable = SetIntrEnb;
  458. /* intr_enable = SetIntrEnb | (1<<9) | (1<<10) | (1<<6); */
  459. ETH_CMD(dev, intr_enable);
  460. ETH_OUTB(dev, 127, UpPoll);
  461. /* Ack all pending events, and set active indicator mask */
  462. ETH_CMD(dev, AckIntr | IntLatch | TxAvailable | RxEarly | IntReq);
  463. ETH_CMD(dev, intr_enable);
  464. /* Tell the adapter where the RX ring is located */
  465. issue_and_wait(dev,UpStall); /* Stall and set the UplistPtr */
  466. ETH_OUTL(dev, (u32)&rx_ring[rx_next], UpListPtr);
  467. ETH_CMD(dev, RxEnable); /* Enable the receiver. */
  468. issue_and_wait(dev,UpUnstall);
  469. /* Send the Individual Address Setup frame */
  470. tx_cur = tx_next;
  471. tx_next = ((tx_next+1) % NUM_TX_DESC);
  472. ias_cmd = (struct descriptor *)&tx_ring[tx_cur];
  473. ias_cmd->status = cpu_to_le32(1<<31); /* set DnIndicate bit. */
  474. ias_cmd->next = 0;
  475. ias_cmd->addr = cpu_to_le32((u32)&bis->bi_enetaddr[0]);
  476. ias_cmd->length = cpu_to_le32(6 | LAST_FRAG);
  477. /* Tell the adapter where the TX ring is located */
  478. ETH_CMD(dev, TxEnable); /* Enable transmitter. */
  479. issue_and_wait(dev, DownStall); /* Stall and set the DownListPtr. */
  480. ETH_OUTL(dev, (u32)&tx_ring[tx_cur], DownListPtr);
  481. issue_and_wait(dev, DownUnstall);
  482. for (i=0; !(ETH_STATUS(dev) & DownComplete); i++)
  483. {
  484. if (i >= TOUT_LOOP)
  485. {
  486. PRINTF("TX Ring status (Init): 0x%4x\n", le32_to_cpu(tx_ring[tx_cur].status));
  487. PRINTF("ETH_STATUS: 0x%x\n", ETH_STATUS(dev));
  488. goto Done;
  489. }
  490. }
  491. if (ETH_STATUS(dev) & DownComplete) /* If DownLoad Complete ACK the bit */
  492. {
  493. ETH_CMD(dev, AckIntr | DownComplete); /* acknowledge the indication bit */
  494. issue_and_wait(dev, DownStall); /* stall and clear DownListPtr */
  495. ETH_OUTL(dev, 0, DownListPtr);
  496. issue_and_wait(dev, DownUnstall);
  497. }
  498. status = 1;
  499. Done:
  500. return status;
  501. }
  502. int eth_3com_send(struct eth_device* dev, volatile void *packet, int length)
  503. {
  504. int i, status = 0;
  505. int tx_cur;
  506. if (length <= 0)
  507. {
  508. PRINTF("eth: bad packet size: %d\n", length);
  509. goto Done;
  510. }
  511. tx_cur = tx_next;
  512. tx_next = (tx_next+1) % NUM_TX_DESC;
  513. tx_ring[tx_cur].status = cpu_to_le32(1<<31); /* set DnIndicate bit */
  514. tx_ring[tx_cur].next = 0;
  515. tx_ring[tx_cur].addr = cpu_to_le32(((u32) packet));
  516. tx_ring[tx_cur].length = cpu_to_le32(length | LAST_FRAG);
  517. /* Send the packet */
  518. issue_and_wait(dev, DownStall); /* stall and set the DownListPtr */
  519. ETH_OUTL(dev, (u32) &tx_ring[tx_cur], DownListPtr);
  520. issue_and_wait(dev, DownUnstall);
  521. for (i=0; !(ETH_STATUS(dev) & DownComplete); i++)
  522. {
  523. if (i >= TOUT_LOOP)
  524. {
  525. PRINTF("TX Ring status (send): 0x%4x\n", le32_to_cpu(tx_ring[tx_cur].status));
  526. goto Done;
  527. }
  528. }
  529. if (ETH_STATUS(dev) & DownComplete) /* If DownLoad Complete ACK the bit */
  530. {
  531. ETH_CMD(dev, AckIntr | DownComplete); /* acknowledge the indication bit */
  532. issue_and_wait(dev, DownStall); /* stall and clear DownListPtr */
  533. ETH_OUTL(dev, 0, DownListPtr);
  534. issue_and_wait(dev, DownUnstall);
  535. }
  536. status=1;
  537. Done:
  538. return status;
  539. }
  540. void PrintPacket (uchar *packet, int length)
  541. {
  542. int loop;
  543. uchar *ptr;
  544. printf ("Printing packet of length %x.\n\n", length);
  545. ptr = packet;
  546. for (loop = 1; loop <= length; loop++)
  547. {
  548. printf ("%2x ", *ptr++);
  549. if ((loop % 40)== 0)
  550. printf ("\n");
  551. }
  552. }
  553. int eth_3com_recv(struct eth_device* dev)
  554. {
  555. u16 stat = 0;
  556. u32 status;
  557. int rx_prev, length = 0;
  558. while (!(ETH_STATUS(dev) & UpComplete)) /* wait on receipt of packet */
  559. ;
  560. status = le32_to_cpu(rx_ring[rx_next].status); /* packet status */
  561. while (status & (1<<15))
  562. {
  563. /* A packet has been received */
  564. if (status & (1<<15))
  565. {
  566. /* A valid frame received */
  567. length = le32_to_cpu(rx_ring[rx_next].status) & 0x1fff; /* length is in bits 0 - 12 */
  568. /* Pass the packet up to the protocol layers */
  569. NetReceive((uchar *)le32_to_cpu(rx_ring[rx_next].addr), length);
  570. rx_ring[rx_next].status = 0; /* clear the status word */
  571. ETH_CMD(dev, AckIntr | UpComplete);
  572. issue_and_wait(dev, UpUnstall);
  573. }
  574. else
  575. if (stat & HostError)
  576. {
  577. /* There was an error */
  578. printf("Rx error status: 0x%4x\n", stat);
  579. init_rx_ring(dev);
  580. goto Done;
  581. }
  582. rx_prev = rx_next;
  583. rx_next = (rx_next + 1) % NUM_RX_DESC;
  584. stat = ETH_STATUS(dev); /* register status */
  585. status = le32_to_cpu(rx_ring[rx_next].status); /* packet status */
  586. }
  587. Done:
  588. return length;
  589. }
  590. void eth_3com_halt(struct eth_device* dev)
  591. {
  592. if (!(dev->iobase))
  593. {
  594. goto Done;
  595. }
  596. issue_and_wait(dev, DownStall); /* shut down transmit and receive */
  597. issue_and_wait(dev, UpStall);
  598. issue_and_wait(dev, RxDisable);
  599. issue_and_wait(dev, TxDisable);
  600. /* free(tx_ring); /###* release memory allocated to the DPD and UPD rings */
  601. /* free(rx_ring); */
  602. Done:
  603. return;
  604. }
  605. static void init_rx_ring(struct eth_device* dev)
  606. {
  607. int i;
  608. PRINTF("Initializing rx_ring. rx_buffer = %p\n", rx_buffer);
  609. issue_and_wait(dev, UpStall);
  610. for (i = 0; i < NUM_RX_DESC; i++)
  611. {
  612. rx_ring[i].next = cpu_to_le32(((u32) &rx_ring[(i+1) % NUM_RX_DESC]));
  613. rx_ring[i].status = 0;
  614. rx_ring[i].addr = cpu_to_le32(((u32) &rx_buffer[i][0]));
  615. rx_ring[i].length = cpu_to_le32(PKTSIZE_ALIGN | LAST_FRAG);
  616. }
  617. rx_next = 0;
  618. }
  619. static void purge_tx_ring(struct eth_device* dev)
  620. {
  621. int i;
  622. PRINTF("Purging tx_ring.\n");
  623. tx_next = 0;
  624. for (i = 0; i < NUM_TX_DESC; i++)
  625. {
  626. tx_ring[i].next = 0;
  627. tx_ring[i].status = 0;
  628. tx_ring[i].addr = 0;
  629. tx_ring[i].length = 0;
  630. }
  631. }
  632. static void read_hw_addr(struct eth_device* dev, bd_t *bis)
  633. {
  634. u8 hw_addr[ETH_ALEN];
  635. unsigned int eeprom[0x40];
  636. unsigned int checksum = 0;
  637. int i, j, timer;
  638. /* Read the station address from the EEPROM. */
  639. EL3WINDOW(dev, 0);
  640. for (i = 0; i < 0x40; i++)
  641. {
  642. ETH_OUTW(dev, EEPROM_Read + i, Wn0EepromCmd);
  643. /* Pause for at least 162 us. for the read to take place. */
  644. for (timer = 10; timer >= 0; timer--)
  645. {
  646. udelay(162);
  647. if ((ETH_INW(dev, Wn0EepromCmd) & 0x8000) == 0)
  648. break;
  649. }
  650. eeprom[i] = ETH_INW(dev, Wn0EepromData);
  651. }
  652. /* Checksum calculation. I'm not sure about this part and there seems to be a bug on the 3com side of things */
  653. for (i = 0; i < 0x21; i++)
  654. checksum ^= eeprom[i];
  655. checksum = (checksum ^ (checksum >> 8)) & 0xff;
  656. if (checksum != 0xbb)
  657. printf(" *** INVALID EEPROM CHECKSUM %4.4x *** \n", checksum);
  658. for (i = 0, j = 0; i < 3; i++)
  659. {
  660. hw_addr[j++] = (u8)((eeprom[i+10] >> 8) & 0xff);
  661. hw_addr[j++] = (u8)(eeprom[i+10] & 0xff);
  662. }
  663. /* MAC Address is in window 2, write value from EEPROM to window 2 */
  664. EL3WINDOW(dev, 2);
  665. for (i = 0; i < 6; i++)
  666. ETH_OUTB(dev, hw_addr[i], i);
  667. for (j = 0; j < ETH_ALEN; j+=2)
  668. {
  669. hw_addr[j] = (u8)(ETH_INW(dev, j) & 0xff);
  670. hw_addr[j+1] = (u8)((ETH_INW(dev, j) >> 8) & 0xff);
  671. }
  672. for (i=0;i<ETH_ALEN;i++)
  673. {
  674. if (hw_addr[i] != bis->bi_enetaddr[i])
  675. {
  676. /* printf("Warning: HW address don't match:\n"); */
  677. /* printf("Address in 3Com Window 2 is " */
  678. /* "%02X:%02X:%02X:%02X:%02X:%02X\n", */
  679. /* hw_addr[0], hw_addr[1], hw_addr[2], */
  680. /* hw_addr[3], hw_addr[4], hw_addr[5]); */
  681. /* printf("Address used by U-Boot is " */
  682. /* "%02X:%02X:%02X:%02X:%02X:%02X\n", */
  683. /* bis->bi_enetaddr[0], bis->bi_enetaddr[1], */
  684. /* bis->bi_enetaddr[2], bis->bi_enetaddr[3], */
  685. /* bis->bi_enetaddr[4], bis->bi_enetaddr[5]); */
  686. /* goto Done; */
  687. char buffer[256];
  688. if (bis->bi_enetaddr[0] == 0 && bis->bi_enetaddr[1] == 0 &&
  689. bis->bi_enetaddr[2] == 0 && bis->bi_enetaddr[3] == 0 &&
  690. bis->bi_enetaddr[4] == 0 && bis->bi_enetaddr[5] == 0)
  691. {
  692. sprintf(buffer, "%02X:%02X:%02X:%02X:%02X:%02X",
  693. hw_addr[0], hw_addr[1], hw_addr[2],
  694. hw_addr[3], hw_addr[4], hw_addr[5]);
  695. setenv("ethaddr", buffer);
  696. }
  697. }
  698. }
  699. for(i=0; i<ETH_ALEN; i++) dev->enetaddr[i] = hw_addr[i];
  700. Done:
  701. return;
  702. }