eepro100.c 24 KB

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