eepro100.c 24 KB

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