eepro100.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <malloc.h>
  9. #include <net.h>
  10. #include <netdev.h>
  11. #include <asm/io.h>
  12. #include <pci.h>
  13. #include <miiphy.h>
  14. #undef DEBUG
  15. /* Ethernet chip registers.
  16. */
  17. #define SCBStatus 0 /* Rx/Command Unit Status *Word* */
  18. #define SCBIntAckByte 1 /* Rx/Command Unit STAT/ACK byte */
  19. #define SCBCmd 2 /* Rx/Command Unit Command *Word* */
  20. #define SCBIntrCtlByte 3 /* Rx/Command Unit Intr.Control Byte */
  21. #define SCBPointer 4 /* General purpose pointer. */
  22. #define SCBPort 8 /* Misc. commands and operands. */
  23. #define SCBflash 12 /* Flash memory control. */
  24. #define SCBeeprom 14 /* EEPROM memory control. */
  25. #define SCBCtrlMDI 16 /* MDI interface control. */
  26. #define SCBEarlyRx 20 /* Early receive byte count. */
  27. #define SCBGenControl 28 /* 82559 General Control Register */
  28. #define SCBGenStatus 29 /* 82559 General Status register */
  29. /* 82559 SCB status word defnitions
  30. */
  31. #define SCB_STATUS_CX 0x8000 /* CU finished command (transmit) */
  32. #define SCB_STATUS_FR 0x4000 /* frame received */
  33. #define SCB_STATUS_CNA 0x2000 /* CU left active state */
  34. #define SCB_STATUS_RNR 0x1000 /* receiver left ready state */
  35. #define SCB_STATUS_MDI 0x0800 /* MDI read/write cycle done */
  36. #define SCB_STATUS_SWI 0x0400 /* software generated interrupt */
  37. #define SCB_STATUS_FCP 0x0100 /* flow control pause interrupt */
  38. #define SCB_INTACK_MASK 0xFD00 /* all the above */
  39. #define SCB_INTACK_TX (SCB_STATUS_CX | SCB_STATUS_CNA)
  40. #define SCB_INTACK_RX (SCB_STATUS_FR | SCB_STATUS_RNR)
  41. /* System control block commands
  42. */
  43. /* CU Commands */
  44. #define CU_NOP 0x0000
  45. #define CU_START 0x0010
  46. #define CU_RESUME 0x0020
  47. #define CU_STATSADDR 0x0040 /* Load Dump Statistics ctrs addr */
  48. #define CU_SHOWSTATS 0x0050 /* Dump statistics counters. */
  49. #define CU_ADDR_LOAD 0x0060 /* Base address to add to CU commands */
  50. #define CU_DUMPSTATS 0x0070 /* Dump then reset stats counters. */
  51. /* RUC Commands */
  52. #define RUC_NOP 0x0000
  53. #define RUC_START 0x0001
  54. #define RUC_RESUME 0x0002
  55. #define RUC_ABORT 0x0004
  56. #define RUC_ADDR_LOAD 0x0006 /* (seems not to clear on acceptance) */
  57. #define RUC_RESUMENR 0x0007
  58. #define CU_CMD_MASK 0x00f0
  59. #define RU_CMD_MASK 0x0007
  60. #define SCB_M 0x0100 /* 0 = enable interrupt, 1 = disable */
  61. #define SCB_SWI 0x0200 /* 1 - cause device to interrupt */
  62. #define CU_STATUS_MASK 0x00C0
  63. #define RU_STATUS_MASK 0x003C
  64. #define RU_STATUS_IDLE (0<<2)
  65. #define RU_STATUS_SUS (1<<2)
  66. #define RU_STATUS_NORES (2<<2)
  67. #define RU_STATUS_READY (4<<2)
  68. #define RU_STATUS_NO_RBDS_SUS ((1<<2)|(8<<2))
  69. #define RU_STATUS_NO_RBDS_NORES ((2<<2)|(8<<2))
  70. #define RU_STATUS_NO_RBDS_READY ((4<<2)|(8<<2))
  71. /* 82559 Port interface commands.
  72. */
  73. #define I82559_RESET 0x00000000 /* Software reset */
  74. #define I82559_SELFTEST 0x00000001 /* 82559 Selftest command */
  75. #define I82559_SELECTIVE_RESET 0x00000002
  76. #define I82559_DUMP 0x00000003
  77. #define I82559_DUMP_WAKEUP 0x00000007
  78. /* 82559 Eeprom interface.
  79. */
  80. #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
  81. #define EE_CS 0x02 /* EEPROM chip select. */
  82. #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
  83. #define EE_WRITE_0 0x01
  84. #define EE_WRITE_1 0x05
  85. #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
  86. #define EE_ENB (0x4800 | EE_CS)
  87. #define EE_CMD_BITS 3
  88. #define EE_DATA_BITS 16
  89. /* The EEPROM commands include the alway-set leading bit.
  90. */
  91. #define EE_EWENB_CMD (4 << addr_len)
  92. #define EE_WRITE_CMD (5 << addr_len)
  93. #define EE_READ_CMD (6 << addr_len)
  94. #define EE_ERASE_CMD (7 << addr_len)
  95. /* Receive frame descriptors.
  96. */
  97. struct RxFD {
  98. volatile u16 status;
  99. volatile u16 control;
  100. volatile u32 link; /* struct RxFD * */
  101. volatile u32 rx_buf_addr; /* void * */
  102. volatile u32 count;
  103. volatile u8 data[PKTSIZE_ALIGN];
  104. };
  105. #define RFD_STATUS_C 0x8000 /* completion of received frame */
  106. #define RFD_STATUS_OK 0x2000 /* frame received with no errors */
  107. #define RFD_CONTROL_EL 0x8000 /* 1=last RFD in RFA */
  108. #define RFD_CONTROL_S 0x4000 /* 1=suspend RU after receiving frame */
  109. #define RFD_CONTROL_H 0x0010 /* 1=RFD is a header RFD */
  110. #define RFD_CONTROL_SF 0x0008 /* 0=simplified, 1=flexible mode */
  111. #define RFD_COUNT_MASK 0x3fff
  112. #define RFD_COUNT_F 0x4000
  113. #define RFD_COUNT_EOF 0x8000
  114. #define RFD_RX_CRC 0x0800 /* crc error */
  115. #define RFD_RX_ALIGNMENT 0x0400 /* alignment error */
  116. #define RFD_RX_RESOURCE 0x0200 /* out of space, no resources */
  117. #define RFD_RX_DMA_OVER 0x0100 /* DMA overrun */
  118. #define RFD_RX_SHORT 0x0080 /* short frame error */
  119. #define RFD_RX_LENGTH 0x0020
  120. #define RFD_RX_ERROR 0x0010 /* receive error */
  121. #define RFD_RX_NO_ADR_MATCH 0x0004 /* no address match */
  122. #define RFD_RX_IA_MATCH 0x0002 /* individual address does not match */
  123. #define RFD_RX_TCO 0x0001 /* TCO indication */
  124. /* Transmit frame descriptors
  125. */
  126. struct TxFD { /* Transmit frame descriptor set. */
  127. volatile u16 status;
  128. volatile u16 command;
  129. volatile u32 link; /* void * */
  130. volatile u32 tx_desc_addr; /* Always points to the tx_buf_addr element. */
  131. volatile s32 count;
  132. volatile u32 tx_buf_addr0; /* void *, frame to be transmitted. */
  133. volatile s32 tx_buf_size0; /* Length of Tx frame. */
  134. volatile u32 tx_buf_addr1; /* void *, frame to be transmitted. */
  135. volatile s32 tx_buf_size1; /* Length of Tx frame. */
  136. };
  137. #define TxCB_CMD_TRANSMIT 0x0004 /* transmit command */
  138. #define TxCB_CMD_SF 0x0008 /* 0=simplified, 1=flexible mode */
  139. #define TxCB_CMD_NC 0x0010 /* 0=CRC insert by controller */
  140. #define TxCB_CMD_I 0x2000 /* generate interrupt on completion */
  141. #define TxCB_CMD_S 0x4000 /* suspend on completion */
  142. #define TxCB_CMD_EL 0x8000 /* last command block in CBL */
  143. #define TxCB_COUNT_MASK 0x3fff
  144. #define TxCB_COUNT_EOF 0x8000
  145. /* The Speedo3 Rx and Tx frame/buffer descriptors.
  146. */
  147. struct descriptor { /* A generic descriptor. */
  148. volatile u16 status;
  149. volatile u16 command;
  150. volatile u32 link; /* struct descriptor * */
  151. unsigned char params[0];
  152. };
  153. #define CONFIG_SYS_CMD_EL 0x8000
  154. #define CONFIG_SYS_CMD_SUSPEND 0x4000
  155. #define CONFIG_SYS_CMD_INT 0x2000
  156. #define CONFIG_SYS_CMD_IAS 0x0001 /* individual address setup */
  157. #define CONFIG_SYS_CMD_CONFIGURE 0x0002 /* configure */
  158. #define CONFIG_SYS_STATUS_C 0x8000
  159. #define CONFIG_SYS_STATUS_OK 0x2000
  160. /* Misc.
  161. */
  162. #define NUM_RX_DESC PKTBUFSRX
  163. #define NUM_TX_DESC 1 /* Number of TX descriptors */
  164. #define TOUT_LOOP 1000000
  165. #define ETH_ALEN 6
  166. static struct RxFD rx_ring[NUM_RX_DESC]; /* RX descriptor ring */
  167. static struct TxFD tx_ring[NUM_TX_DESC]; /* TX descriptor ring */
  168. static int rx_next; /* RX descriptor ring pointer */
  169. static int tx_next; /* TX descriptor ring pointer */
  170. static int tx_threshold;
  171. /*
  172. * The parameters for a CmdConfigure operation.
  173. * There are so many options that it would be difficult to document
  174. * each bit. We mostly use the default or recommended settings.
  175. */
  176. static const char i82558_config_cmd[] = {
  177. 22, 0x08, 0, 1, 0, 0, 0x22, 0x03, 1, /* 1=Use MII 0=Use AUI */
  178. 0, 0x2E, 0, 0x60, 0x08, 0x88,
  179. 0x68, 0, 0x40, 0xf2, 0x84, /* Disable FC */
  180. 0x31, 0x05,
  181. };
  182. static void init_rx_ring (struct eth_device *dev);
  183. static void purge_tx_ring (struct eth_device *dev);
  184. static void read_hw_addr (struct eth_device *dev, bd_t * bis);
  185. static int eepro100_init (struct eth_device *dev, bd_t * bis);
  186. static int eepro100_send(struct eth_device *dev, void *packet, int length);
  187. static int eepro100_recv (struct eth_device *dev);
  188. static void eepro100_halt (struct eth_device *dev);
  189. #if defined(CONFIG_E500)
  190. #define bus_to_phys(a) (a)
  191. #define phys_to_bus(a) (a)
  192. #else
  193. #define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a)
  194. #define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
  195. #endif
  196. static inline int INW (struct eth_device *dev, u_long addr)
  197. {
  198. return le16_to_cpu(*(volatile u16 *)(addr + (u_long)dev->iobase));
  199. }
  200. static inline void OUTW (struct eth_device *dev, int command, u_long addr)
  201. {
  202. *(volatile u16 *)((addr + (u_long)dev->iobase)) = cpu_to_le16(command);
  203. }
  204. static inline void OUTL (struct eth_device *dev, int command, u_long addr)
  205. {
  206. *(volatile u32 *)((addr + (u_long)dev->iobase)) = cpu_to_le32(command);
  207. }
  208. #if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
  209. static inline int INL (struct eth_device *dev, u_long addr)
  210. {
  211. return le32_to_cpu(*(volatile u32 *)(addr + (u_long)dev->iobase));
  212. }
  213. static int get_phyreg (struct eth_device *dev, unsigned char addr,
  214. unsigned char reg, unsigned short *value)
  215. {
  216. int cmd;
  217. int timeout = 50;
  218. /* read requested data */
  219. cmd = (2 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
  220. OUTL (dev, cmd, SCBCtrlMDI);
  221. do {
  222. udelay(1000);
  223. cmd = INL (dev, SCBCtrlMDI);
  224. } while (!(cmd & (1 << 28)) && (--timeout));
  225. if (timeout == 0)
  226. return -1;
  227. *value = (unsigned short) (cmd & 0xffff);
  228. return 0;
  229. }
  230. static int set_phyreg (struct eth_device *dev, unsigned char addr,
  231. unsigned char reg, unsigned short value)
  232. {
  233. int cmd;
  234. int timeout = 50;
  235. /* write requested data */
  236. cmd = (1 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
  237. OUTL (dev, cmd | value, SCBCtrlMDI);
  238. while (!(INL (dev, SCBCtrlMDI) & (1 << 28)) && (--timeout))
  239. udelay(1000);
  240. if (timeout == 0)
  241. return -1;
  242. return 0;
  243. }
  244. /* Check if given phyaddr is valid, i.e. there is a PHY connected.
  245. * Do this by checking model value field from ID2 register.
  246. */
  247. static struct eth_device* verify_phyaddr (const char *devname,
  248. unsigned char addr)
  249. {
  250. struct eth_device *dev;
  251. unsigned short value;
  252. unsigned char model;
  253. dev = eth_get_dev_by_name(devname);
  254. if (dev == NULL) {
  255. printf("%s: no such device\n", devname);
  256. return NULL;
  257. }
  258. /* read id2 register */
  259. if (get_phyreg(dev, addr, MII_PHYSID2, &value) != 0) {
  260. printf("%s: mii read timeout!\n", devname);
  261. return NULL;
  262. }
  263. /* get model */
  264. model = (unsigned char)((value >> 4) & 0x003f);
  265. if (model == 0) {
  266. printf("%s: no PHY at address %d\n", devname, addr);
  267. return NULL;
  268. }
  269. return dev;
  270. }
  271. static int eepro100_miiphy_read(struct mii_dev *bus, int addr, int devad,
  272. int reg)
  273. {
  274. unsigned short value = 0;
  275. struct eth_device *dev;
  276. dev = verify_phyaddr(bus->name, addr);
  277. if (dev == NULL)
  278. return -1;
  279. if (get_phyreg(dev, addr, reg, &value) != 0) {
  280. printf("%s: mii read timeout!\n", bus->name);
  281. return -1;
  282. }
  283. return value;
  284. }
  285. static int eepro100_miiphy_write(struct mii_dev *bus, int addr, int devad,
  286. int reg, u16 value)
  287. {
  288. struct eth_device *dev;
  289. dev = verify_phyaddr(bus->name, addr);
  290. if (dev == NULL)
  291. return -1;
  292. if (set_phyreg(dev, addr, reg, value) != 0) {
  293. printf("%s: mii write timeout!\n", bus->name);
  294. return -1;
  295. }
  296. return 0;
  297. }
  298. #endif
  299. /* Wait for the chip get the command.
  300. */
  301. static int wait_for_eepro100 (struct eth_device *dev)
  302. {
  303. int i;
  304. for (i = 0; INW (dev, SCBCmd) & (CU_CMD_MASK | RU_CMD_MASK); i++) {
  305. if (i >= TOUT_LOOP) {
  306. return 0;
  307. }
  308. }
  309. return 1;
  310. }
  311. static struct pci_device_id supported[] = {
  312. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82557},
  313. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559},
  314. {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559ER},
  315. {}
  316. };
  317. int eepro100_initialize (bd_t * bis)
  318. {
  319. pci_dev_t devno;
  320. int card_number = 0;
  321. struct eth_device *dev;
  322. u32 iobase, status;
  323. int idx = 0;
  324. while (1) {
  325. /* Find PCI device
  326. */
  327. if ((devno = pci_find_devices (supported, idx++)) < 0) {
  328. break;
  329. }
  330. pci_read_config_dword (devno, PCI_BASE_ADDRESS_0, &iobase);
  331. iobase &= ~0xf;
  332. #ifdef DEBUG
  333. printf ("eepro100: Intel i82559 PCI EtherExpressPro @0x%x\n",
  334. iobase);
  335. #endif
  336. pci_write_config_dword (devno,
  337. PCI_COMMAND,
  338. PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
  339. /* Check if I/O accesses and Bus Mastering are enabled.
  340. */
  341. pci_read_config_dword (devno, PCI_COMMAND, &status);
  342. if (!(status & PCI_COMMAND_MEMORY)) {
  343. printf ("Error: Can not enable MEM access.\n");
  344. continue;
  345. }
  346. if (!(status & PCI_COMMAND_MASTER)) {
  347. printf ("Error: Can not enable Bus Mastering.\n");
  348. continue;
  349. }
  350. dev = (struct eth_device *) malloc (sizeof *dev);
  351. if (!dev) {
  352. printf("eepro100: Can not allocate memory\n");
  353. break;
  354. }
  355. memset(dev, 0, sizeof(*dev));
  356. sprintf (dev->name, "i82559#%d", card_number);
  357. dev->priv = (void *) devno; /* this have to come before bus_to_phys() */
  358. dev->iobase = bus_to_phys (iobase);
  359. dev->init = eepro100_init;
  360. dev->halt = eepro100_halt;
  361. dev->send = eepro100_send;
  362. dev->recv = eepro100_recv;
  363. eth_register (dev);
  364. #if defined (CONFIG_MII) || defined(CONFIG_CMD_MII)
  365. /* register mii command access routines */
  366. int retval;
  367. struct mii_dev *mdiodev = mdio_alloc();
  368. if (!mdiodev)
  369. return -ENOMEM;
  370. strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
  371. mdiodev->read = eepro100_miiphy_read;
  372. mdiodev->write = eepro100_miiphy_write;
  373. retval = mdio_register(mdiodev);
  374. if (retval < 0)
  375. return retval;
  376. #endif
  377. card_number++;
  378. /* Set the latency timer for value.
  379. */
  380. pci_write_config_byte (devno, PCI_LATENCY_TIMER, 0x20);
  381. udelay (10 * 1000);
  382. read_hw_addr (dev, bis);
  383. }
  384. return card_number;
  385. }
  386. static int eepro100_init (struct eth_device *dev, bd_t * bis)
  387. {
  388. int i, status = -1;
  389. int tx_cur;
  390. struct descriptor *ias_cmd, *cfg_cmd;
  391. /* Reset the ethernet controller
  392. */
  393. OUTL (dev, I82559_SELECTIVE_RESET, SCBPort);
  394. udelay (20);
  395. OUTL (dev, I82559_RESET, SCBPort);
  396. udelay (20);
  397. if (!wait_for_eepro100 (dev)) {
  398. printf ("Error: Can not reset ethernet controller.\n");
  399. goto Done;
  400. }
  401. OUTL (dev, 0, SCBPointer);
  402. OUTW (dev, SCB_M | RUC_ADDR_LOAD, SCBCmd);
  403. if (!wait_for_eepro100 (dev)) {
  404. printf ("Error: Can not reset ethernet controller.\n");
  405. goto Done;
  406. }
  407. OUTL (dev, 0, SCBPointer);
  408. OUTW (dev, SCB_M | CU_ADDR_LOAD, SCBCmd);
  409. /* Initialize Rx and Tx rings.
  410. */
  411. init_rx_ring (dev);
  412. purge_tx_ring (dev);
  413. /* Tell the adapter where the RX ring is located.
  414. */
  415. if (!wait_for_eepro100 (dev)) {
  416. printf ("Error: Can not reset ethernet controller.\n");
  417. goto Done;
  418. }
  419. OUTL (dev, phys_to_bus ((u32) & rx_ring[rx_next]), SCBPointer);
  420. OUTW (dev, SCB_M | RUC_START, SCBCmd);
  421. /* Send the Configure frame */
  422. tx_cur = tx_next;
  423. tx_next = ((tx_next + 1) % NUM_TX_DESC);
  424. cfg_cmd = (struct descriptor *) &tx_ring[tx_cur];
  425. cfg_cmd->command = cpu_to_le16 ((CONFIG_SYS_CMD_SUSPEND | CONFIG_SYS_CMD_CONFIGURE));
  426. cfg_cmd->status = 0;
  427. cfg_cmd->link = cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_next]));
  428. memcpy (cfg_cmd->params, i82558_config_cmd,
  429. sizeof (i82558_config_cmd));
  430. if (!wait_for_eepro100 (dev)) {
  431. printf ("Error---CONFIG_SYS_CMD_CONFIGURE: Can not reset ethernet controller.\n");
  432. goto Done;
  433. }
  434. OUTL (dev, phys_to_bus ((u32) & tx_ring[tx_cur]), SCBPointer);
  435. OUTW (dev, SCB_M | CU_START, SCBCmd);
  436. for (i = 0;
  437. !(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C);
  438. i++) {
  439. if (i >= TOUT_LOOP) {
  440. printf ("%s: Tx error buffer not ready\n", dev->name);
  441. goto Done;
  442. }
  443. }
  444. if (!(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) {
  445. printf ("TX error status = 0x%08X\n",
  446. le16_to_cpu (tx_ring[tx_cur].status));
  447. goto Done;
  448. }
  449. /* Send the Individual Address Setup frame
  450. */
  451. tx_cur = tx_next;
  452. tx_next = ((tx_next + 1) % NUM_TX_DESC);
  453. ias_cmd = (struct descriptor *) &tx_ring[tx_cur];
  454. ias_cmd->command = cpu_to_le16 ((CONFIG_SYS_CMD_SUSPEND | CONFIG_SYS_CMD_IAS));
  455. ias_cmd->status = 0;
  456. ias_cmd->link = cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_next]));
  457. memcpy (ias_cmd->params, dev->enetaddr, 6);
  458. /* Tell the adapter where the TX ring is located.
  459. */
  460. if (!wait_for_eepro100 (dev)) {
  461. printf ("Error: Can not reset ethernet controller.\n");
  462. goto Done;
  463. }
  464. OUTL (dev, phys_to_bus ((u32) & tx_ring[tx_cur]), SCBPointer);
  465. OUTW (dev, SCB_M | CU_START, SCBCmd);
  466. for (i = 0; !(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C);
  467. i++) {
  468. if (i >= TOUT_LOOP) {
  469. printf ("%s: Tx error buffer not ready\n",
  470. dev->name);
  471. goto Done;
  472. }
  473. }
  474. if (!(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) {
  475. printf ("TX error status = 0x%08X\n",
  476. le16_to_cpu (tx_ring[tx_cur].status));
  477. goto Done;
  478. }
  479. status = 0;
  480. Done:
  481. return status;
  482. }
  483. static int eepro100_send(struct eth_device *dev, void *packet, int length)
  484. {
  485. int i, status = -1;
  486. int tx_cur;
  487. if (length <= 0) {
  488. printf ("%s: bad packet size: %d\n", dev->name, length);
  489. goto Done;
  490. }
  491. tx_cur = tx_next;
  492. tx_next = (tx_next + 1) % NUM_TX_DESC;
  493. tx_ring[tx_cur].command = cpu_to_le16 ( TxCB_CMD_TRANSMIT |
  494. TxCB_CMD_SF |
  495. TxCB_CMD_S |
  496. TxCB_CMD_EL );
  497. tx_ring[tx_cur].status = 0;
  498. tx_ring[tx_cur].count = cpu_to_le32 (tx_threshold);
  499. tx_ring[tx_cur].link =
  500. cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_next]));
  501. tx_ring[tx_cur].tx_desc_addr =
  502. cpu_to_le32 (phys_to_bus ((u32) & tx_ring[tx_cur].tx_buf_addr0));
  503. tx_ring[tx_cur].tx_buf_addr0 =
  504. cpu_to_le32 (phys_to_bus ((u_long) packet));
  505. tx_ring[tx_cur].tx_buf_size0 = cpu_to_le32 (length);
  506. if (!wait_for_eepro100 (dev)) {
  507. printf ("%s: Tx error ethernet controller not ready.\n",
  508. dev->name);
  509. goto Done;
  510. }
  511. /* Send the packet.
  512. */
  513. OUTL (dev, phys_to_bus ((u32) & tx_ring[tx_cur]), SCBPointer);
  514. OUTW (dev, SCB_M | CU_START, SCBCmd);
  515. for (i = 0; !(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C);
  516. i++) {
  517. if (i >= TOUT_LOOP) {
  518. printf ("%s: Tx error buffer not ready\n", dev->name);
  519. goto Done;
  520. }
  521. }
  522. if (!(le16_to_cpu (tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) {
  523. printf ("TX error status = 0x%08X\n",
  524. le16_to_cpu (tx_ring[tx_cur].status));
  525. goto Done;
  526. }
  527. status = length;
  528. Done:
  529. return status;
  530. }
  531. static int eepro100_recv (struct eth_device *dev)
  532. {
  533. u16 status, stat;
  534. int rx_prev, length = 0;
  535. stat = INW (dev, SCBStatus);
  536. OUTW (dev, stat & SCB_STATUS_RNR, SCBStatus);
  537. for (;;) {
  538. status = le16_to_cpu (rx_ring[rx_next].status);
  539. if (!(status & RFD_STATUS_C)) {
  540. break;
  541. }
  542. /* Valid frame status.
  543. */
  544. if ((status & RFD_STATUS_OK)) {
  545. /* A valid frame received.
  546. */
  547. length = le32_to_cpu (rx_ring[rx_next].count) & 0x3fff;
  548. /* Pass the packet up to the protocol
  549. * layers.
  550. */
  551. net_process_received_packet((u8 *)rx_ring[rx_next].data,
  552. length);
  553. } else {
  554. /* There was an error.
  555. */
  556. printf ("RX error status = 0x%08X\n", status);
  557. }
  558. rx_ring[rx_next].control = cpu_to_le16 (RFD_CONTROL_S);
  559. rx_ring[rx_next].status = 0;
  560. rx_ring[rx_next].count = cpu_to_le32 (PKTSIZE_ALIGN << 16);
  561. rx_prev = (rx_next + NUM_RX_DESC - 1) % NUM_RX_DESC;
  562. rx_ring[rx_prev].control = 0;
  563. /* Update entry information.
  564. */
  565. rx_next = (rx_next + 1) % NUM_RX_DESC;
  566. }
  567. if (stat & SCB_STATUS_RNR) {
  568. printf ("%s: Receiver is not ready, restart it !\n", dev->name);
  569. /* Reinitialize Rx ring.
  570. */
  571. init_rx_ring (dev);
  572. if (!wait_for_eepro100 (dev)) {
  573. printf ("Error: Can not restart ethernet controller.\n");
  574. goto Done;
  575. }
  576. OUTL (dev, phys_to_bus ((u32) & rx_ring[rx_next]), SCBPointer);
  577. OUTW (dev, SCB_M | RUC_START, SCBCmd);
  578. }
  579. Done:
  580. return length;
  581. }
  582. static void eepro100_halt (struct eth_device *dev)
  583. {
  584. /* Reset the ethernet controller
  585. */
  586. OUTL (dev, I82559_SELECTIVE_RESET, SCBPort);
  587. udelay (20);
  588. OUTL (dev, I82559_RESET, SCBPort);
  589. udelay (20);
  590. if (!wait_for_eepro100 (dev)) {
  591. printf ("Error: Can not reset ethernet controller.\n");
  592. goto Done;
  593. }
  594. OUTL (dev, 0, SCBPointer);
  595. OUTW (dev, SCB_M | RUC_ADDR_LOAD, SCBCmd);
  596. if (!wait_for_eepro100 (dev)) {
  597. printf ("Error: Can not reset ethernet controller.\n");
  598. goto Done;
  599. }
  600. OUTL (dev, 0, SCBPointer);
  601. OUTW (dev, SCB_M | CU_ADDR_LOAD, SCBCmd);
  602. Done:
  603. return;
  604. }
  605. /* SROM Read.
  606. */
  607. static int read_eeprom (struct eth_device *dev, int location, int addr_len)
  608. {
  609. unsigned short retval = 0;
  610. int read_cmd = location | EE_READ_CMD;
  611. int i;
  612. OUTW (dev, EE_ENB & ~EE_CS, SCBeeprom);
  613. OUTW (dev, EE_ENB, SCBeeprom);
  614. /* Shift the read command bits out. */
  615. for (i = 12; i >= 0; i--) {
  616. short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  617. OUTW (dev, EE_ENB | dataval, SCBeeprom);
  618. udelay (1);
  619. OUTW (dev, EE_ENB | dataval | EE_SHIFT_CLK, SCBeeprom);
  620. udelay (1);
  621. }
  622. OUTW (dev, EE_ENB, SCBeeprom);
  623. for (i = 15; i >= 0; i--) {
  624. OUTW (dev, EE_ENB | EE_SHIFT_CLK, SCBeeprom);
  625. udelay (1);
  626. retval = (retval << 1) |
  627. ((INW (dev, SCBeeprom) & EE_DATA_READ) ? 1 : 0);
  628. OUTW (dev, EE_ENB, SCBeeprom);
  629. udelay (1);
  630. }
  631. /* Terminate the EEPROM access. */
  632. OUTW (dev, EE_ENB & ~EE_CS, SCBeeprom);
  633. return retval;
  634. }
  635. #ifdef CONFIG_EEPRO100_SROM_WRITE
  636. int eepro100_write_eeprom (struct eth_device* dev, int location, int addr_len, unsigned short data)
  637. {
  638. unsigned short dataval;
  639. int enable_cmd = 0x3f | EE_EWENB_CMD;
  640. int write_cmd = location | EE_WRITE_CMD;
  641. int i;
  642. unsigned long datalong, tmplong;
  643. OUTW(dev, EE_ENB & ~EE_CS, SCBeeprom);
  644. udelay(1);
  645. OUTW(dev, EE_ENB, SCBeeprom);
  646. /* Shift the enable command bits out. */
  647. for (i = (addr_len+EE_CMD_BITS-1); i >= 0; i--)
  648. {
  649. dataval = (enable_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  650. OUTW(dev, EE_ENB | dataval, SCBeeprom);
  651. udelay(1);
  652. OUTW(dev, EE_ENB | dataval | EE_SHIFT_CLK, SCBeeprom);
  653. udelay(1);
  654. }
  655. OUTW(dev, EE_ENB, SCBeeprom);
  656. udelay(1);
  657. OUTW(dev, EE_ENB & ~EE_CS, SCBeeprom);
  658. udelay(1);
  659. OUTW(dev, EE_ENB, SCBeeprom);
  660. /* Shift the write command bits out. */
  661. for (i = (addr_len+EE_CMD_BITS-1); i >= 0; i--)
  662. {
  663. dataval = (write_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  664. OUTW(dev, EE_ENB | dataval, SCBeeprom);
  665. udelay(1);
  666. OUTW(dev, EE_ENB | dataval | EE_SHIFT_CLK, SCBeeprom);
  667. udelay(1);
  668. }
  669. /* Write the data */
  670. datalong= (unsigned long) ((((data) & 0x00ff) << 8) | ( (data) >> 8));
  671. for (i = 0; i< EE_DATA_BITS; i++)
  672. {
  673. /* Extract and move data bit to bit DI */
  674. dataval = ((datalong & 0x8000)>>13) ? EE_DATA_WRITE : 0;
  675. OUTW(dev, EE_ENB | dataval, SCBeeprom);
  676. udelay(1);
  677. OUTW(dev, EE_ENB | dataval | EE_SHIFT_CLK, SCBeeprom);
  678. udelay(1);
  679. OUTW(dev, EE_ENB | dataval, SCBeeprom);
  680. udelay(1);
  681. datalong = datalong << 1; /* Adjust significant data bit*/
  682. }
  683. /* Finish up command (toggle CS) */
  684. OUTW(dev, EE_ENB & ~EE_CS, SCBeeprom);
  685. udelay(1); /* delay for more than 250 ns */
  686. OUTW(dev, EE_ENB, SCBeeprom);
  687. /* Wait for programming ready (D0 = 1) */
  688. tmplong = 10;
  689. do
  690. {
  691. dataval = INW(dev, SCBeeprom);
  692. if (dataval & EE_DATA_READ)
  693. break;
  694. udelay(10000);
  695. }
  696. while (-- tmplong);
  697. if (tmplong == 0)
  698. {
  699. printf ("Write i82559 eeprom timed out (100 ms waiting for data ready.\n");
  700. return -1;
  701. }
  702. /* Terminate the EEPROM access. */
  703. OUTW(dev, EE_ENB & ~EE_CS, SCBeeprom);
  704. return 0;
  705. }
  706. #endif
  707. static void init_rx_ring (struct eth_device *dev)
  708. {
  709. int i;
  710. for (i = 0; i < NUM_RX_DESC; i++) {
  711. rx_ring[i].status = 0;
  712. rx_ring[i].control =
  713. (i == NUM_RX_DESC - 1) ? cpu_to_le16 (RFD_CONTROL_S) : 0;
  714. rx_ring[i].link =
  715. cpu_to_le32 (phys_to_bus
  716. ((u32) & rx_ring[(i + 1) % NUM_RX_DESC]));
  717. rx_ring[i].rx_buf_addr = 0xffffffff;
  718. rx_ring[i].count = cpu_to_le32 (PKTSIZE_ALIGN << 16);
  719. }
  720. rx_next = 0;
  721. }
  722. static void purge_tx_ring (struct eth_device *dev)
  723. {
  724. int i;
  725. tx_next = 0;
  726. tx_threshold = 0x01208000;
  727. for (i = 0; i < NUM_TX_DESC; i++) {
  728. tx_ring[i].status = 0;
  729. tx_ring[i].command = 0;
  730. tx_ring[i].link = 0;
  731. tx_ring[i].tx_desc_addr = 0;
  732. tx_ring[i].count = 0;
  733. tx_ring[i].tx_buf_addr0 = 0;
  734. tx_ring[i].tx_buf_size0 = 0;
  735. tx_ring[i].tx_buf_addr1 = 0;
  736. tx_ring[i].tx_buf_size1 = 0;
  737. }
  738. }
  739. static void read_hw_addr (struct eth_device *dev, bd_t * bis)
  740. {
  741. u16 sum = 0;
  742. int i, j;
  743. int addr_len = read_eeprom (dev, 0, 6) == 0xffff ? 8 : 6;
  744. for (j = 0, i = 0; i < 0x40; i++) {
  745. u16 value = read_eeprom (dev, i, addr_len);
  746. sum += value;
  747. if (i < 3) {
  748. dev->enetaddr[j++] = value;
  749. dev->enetaddr[j++] = value >> 8;
  750. }
  751. }
  752. if (sum != 0xBABA) {
  753. memset (dev->enetaddr, 0, ETH_ALEN);
  754. #ifdef DEBUG
  755. printf ("%s: Invalid EEPROM checksum %#4.4x, "
  756. "check settings before activating this device!\n",
  757. dev->name, sum);
  758. #endif
  759. }
  760. }