eepro100.c 24 KB

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