You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3c595.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. /*
  2. * 3c595.c -- 3COM 3C595 Fast Etherlink III PCI driver for etherboot
  3. *
  4. * Copyright (C) 2000 Shusuke Nisiyama <shu@athena.qe.eng.hokudai.ac.jp>
  5. * All rights reserved.
  6. * Mar. 14, 2000
  7. *
  8. * This software may be used, modified, copied, distributed, and sold, in
  9. * both source and binary form provided that the above copyright and these
  10. * terms are retained. Under no circumstances are the authors responsible for
  11. * the proper functioning of this software, nor do the authors assume any
  12. * responsibility for damages incurred with its use.
  13. *
  14. * This code is based on Martin Renters' etherboot-4.4.3 3c509.c and
  15. * Herb Peyerl's FreeBSD 3.4-RELEASE if_vx.c driver.
  16. *
  17. * Copyright (C) 1993-1994, David Greenman, Martin Renters.
  18. * Copyright (C) 1993-1995, Andres Vega Garcia.
  19. * Copyright (C) 1995, Serge Babkin.
  20. *
  21. * Copyright (c) 1994 Herb Peyerl <hpeyerl@novatel.ca>
  22. *
  23. * timlegge 08-24-2003 Add Multicast Support
  24. */
  25. /* #define EDEBUG */
  26. #include "etherboot.h"
  27. #include "nic.h"
  28. #include <gpxe/pci.h>
  29. #include <gpxe/ethernet.h>
  30. #include "3c595.h"
  31. #include "timer.h"
  32. static struct nic_operations t595_operations;
  33. static unsigned short eth_nic_base;
  34. static unsigned short vx_connector, vx_connectors;
  35. static struct connector_entry {
  36. int bit;
  37. char *name;
  38. } conn_tab[VX_CONNECTORS] = {
  39. #define CONNECTOR_UTP 0
  40. { 0x08, "utp"},
  41. #define CONNECTOR_AUI 1
  42. { 0x20, "aui"},
  43. /* dummy */
  44. { 0, "???"},
  45. #define CONNECTOR_BNC 3
  46. { 0x10, "bnc"},
  47. #define CONNECTOR_TX 4
  48. { 0x02, "tx"},
  49. #define CONNECTOR_FX 5
  50. { 0x04, "fx"},
  51. #define CONNECTOR_MII 6
  52. { 0x40, "mii"},
  53. { 0, "???"}
  54. };
  55. static void vxgetlink(void);
  56. static void vxsetlink(void);
  57. /**************************************************************************
  58. ETH_RESET - Reset adapter
  59. ***************************************************************************/
  60. static void t595_reset(struct nic *nic)
  61. {
  62. int i;
  63. /***********************************************************
  64. Reset 3Com 595 card
  65. *************************************************************/
  66. /* stop card */
  67. outw(RX_DISABLE, BASE + VX_COMMAND);
  68. outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
  69. VX_BUSY_WAIT;
  70. outw(TX_DISABLE, BASE + VX_COMMAND);
  71. outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
  72. udelay(8000);
  73. outw(RX_RESET, BASE + VX_COMMAND);
  74. VX_BUSY_WAIT;
  75. outw(TX_RESET, BASE + VX_COMMAND);
  76. VX_BUSY_WAIT;
  77. outw(C_INTR_LATCH, BASE + VX_COMMAND);
  78. outw(SET_RD_0_MASK, BASE + VX_COMMAND);
  79. outw(SET_INTR_MASK, BASE + VX_COMMAND);
  80. outw(SET_RX_FILTER, BASE + VX_COMMAND);
  81. /*
  82. * initialize card
  83. */
  84. VX_BUSY_WAIT;
  85. GO_WINDOW(0);
  86. /* Disable the card */
  87. /* outw(0, BASE + VX_W0_CONFIG_CTRL); */
  88. /* Configure IRQ to none */
  89. /* outw(SET_IRQ(0), BASE + VX_W0_RESOURCE_CFG); */
  90. /* Enable the card */
  91. /* outw(ENABLE_DRQ_IRQ, BASE + VX_W0_CONFIG_CTRL); */
  92. GO_WINDOW(2);
  93. /* Reload the ether_addr. */
  94. for (i = 0; i < ETH_ALEN; i++)
  95. outb(nic->node_addr[i], BASE + VX_W2_ADDR_0 + i);
  96. outw(RX_RESET, BASE + VX_COMMAND);
  97. VX_BUSY_WAIT;
  98. outw(TX_RESET, BASE + VX_COMMAND);
  99. VX_BUSY_WAIT;
  100. /* Window 1 is operating window */
  101. GO_WINDOW(1);
  102. for (i = 0; i < 31; i++)
  103. inb(BASE + VX_W1_TX_STATUS);
  104. outw(SET_RD_0_MASK | S_CARD_FAILURE | S_RX_COMPLETE |
  105. S_TX_COMPLETE | S_TX_AVAIL, BASE + VX_COMMAND);
  106. outw(SET_INTR_MASK | S_CARD_FAILURE | S_RX_COMPLETE |
  107. S_TX_COMPLETE | S_TX_AVAIL, BASE + VX_COMMAND);
  108. /*
  109. * Attempt to get rid of any stray interrupts that occured during
  110. * configuration. On the i386 this isn't possible because one may
  111. * already be queued. However, a single stray interrupt is
  112. * unimportant.
  113. */
  114. outw(ACK_INTR | 0xff, BASE + VX_COMMAND);
  115. outw(SET_RX_FILTER | FIL_INDIVIDUAL |
  116. FIL_BRDCST|FIL_MULTICAST, BASE + VX_COMMAND);
  117. vxsetlink();
  118. /*{
  119. int i,j;
  120. i = CONNECTOR_TX;
  121. GO_WINDOW(3);
  122. j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
  123. outl(BASE + VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
  124. GO_WINDOW(4);
  125. outw(LINKBEAT_ENABLE, BASE + VX_W4_MEDIA_TYPE);
  126. GO_WINDOW(1);
  127. }*/
  128. /* start tranciever and receiver */
  129. outw(RX_ENABLE, BASE + VX_COMMAND);
  130. outw(TX_ENABLE, BASE + VX_COMMAND);
  131. }
  132. /**************************************************************************
  133. ETH_TRANSMIT - Transmit a frame
  134. ***************************************************************************/
  135. static char padmap[] = {
  136. 0, 3, 2, 1};
  137. static void t595_transmit(
  138. struct nic *nic,
  139. const char *d, /* Destination */
  140. unsigned int t, /* Type */
  141. unsigned int s, /* size */
  142. const char *p) /* Packet */
  143. {
  144. register int len;
  145. int pad;
  146. int status;
  147. #ifdef EDEBUG
  148. printf("{l=%d,t=%hX}",s+ETH_HLEN,t);
  149. #endif
  150. /* swap bytes of type */
  151. t= htons(t);
  152. len=s+ETH_HLEN; /* actual length of packet */
  153. pad = padmap[len & 3];
  154. /*
  155. * The 3c595 automatically pads short packets to minimum ethernet length,
  156. * but we drop packets that are too large. Perhaps we should truncate
  157. * them instead?
  158. */
  159. if (len + pad > ETH_FRAME_LEN) {
  160. return;
  161. }
  162. /* drop acknowledgements */
  163. while(( status=inb(BASE + VX_W1_TX_STATUS) )& TXS_COMPLETE ) {
  164. if(status & (TXS_UNDERRUN|TXS_MAX_COLLISION|TXS_STATUS_OVERFLOW)) {
  165. outw(TX_RESET, BASE + VX_COMMAND);
  166. outw(TX_ENABLE, BASE + VX_COMMAND);
  167. }
  168. outb(0x0, BASE + VX_W1_TX_STATUS);
  169. }
  170. while (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) {
  171. /* no room in FIFO */
  172. }
  173. outw(len, BASE + VX_W1_TX_PIO_WR_1);
  174. outw(0x0, BASE + VX_W1_TX_PIO_WR_1); /* Second dword meaningless */
  175. /* write packet */
  176. outsw(BASE + VX_W1_TX_PIO_WR_1, d, ETH_ALEN/2);
  177. outsw(BASE + VX_W1_TX_PIO_WR_1, nic->node_addr, ETH_ALEN/2);
  178. outw(t, BASE + VX_W1_TX_PIO_WR_1);
  179. outsw(BASE + VX_W1_TX_PIO_WR_1, p, s / 2);
  180. if (s & 1)
  181. outb(*(p+s - 1), BASE + VX_W1_TX_PIO_WR_1);
  182. while (pad--)
  183. outb(0, BASE + VX_W1_TX_PIO_WR_1); /* Padding */
  184. /* wait for Tx complete */
  185. while((inw(BASE + VX_STATUS) & S_COMMAND_IN_PROGRESS) != 0)
  186. ;
  187. }
  188. /**************************************************************************
  189. ETH_POLL - Wait for a frame
  190. ***************************************************************************/
  191. static int t595_poll(struct nic *nic, int retrieve)
  192. {
  193. /* common variables */
  194. /* variables for 3C595 */
  195. short status, cst;
  196. register short rx_fifo;
  197. cst=inw(BASE + VX_STATUS);
  198. #ifdef EDEBUG
  199. if(cst & 0x1FFF)
  200. printf("-%hX-",cst);
  201. #endif
  202. if( (cst & S_RX_COMPLETE)==0 ) {
  203. /* acknowledge everything */
  204. outw(ACK_INTR | cst, BASE + VX_COMMAND);
  205. outw(C_INTR_LATCH, BASE + VX_COMMAND);
  206. return 0;
  207. }
  208. status = inw(BASE + VX_W1_RX_STATUS);
  209. #ifdef EDEBUG
  210. printf("*%hX*",status);
  211. #endif
  212. if (status & ERR_RX) {
  213. outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
  214. return 0;
  215. }
  216. rx_fifo = status & RX_BYTES_MASK;
  217. if (rx_fifo==0)
  218. return 0;
  219. if ( ! retrieve ) return 1;
  220. /* read packet */
  221. #ifdef EDEBUG
  222. printf("[l=%d",rx_fifo);
  223. #endif
  224. insw(BASE + VX_W1_RX_PIO_RD_1, nic->packet, rx_fifo / 2);
  225. if(rx_fifo & 1)
  226. nic->packet[rx_fifo-1]=inb(BASE + VX_W1_RX_PIO_RD_1);
  227. nic->packetlen=rx_fifo;
  228. while(1) {
  229. status = inw(BASE + VX_W1_RX_STATUS);
  230. #ifdef EDEBUG
  231. printf("*%hX*",status);
  232. #endif
  233. rx_fifo = status & RX_BYTES_MASK;
  234. if(rx_fifo>0) {
  235. insw(BASE + VX_W1_RX_PIO_RD_1, nic->packet+nic->packetlen, rx_fifo / 2);
  236. if(rx_fifo & 1)
  237. nic->packet[nic->packetlen+rx_fifo-1]=inb(BASE + VX_W1_RX_PIO_RD_1);
  238. nic->packetlen+=rx_fifo;
  239. #ifdef EDEBUG
  240. printf("+%d",rx_fifo);
  241. #endif
  242. }
  243. if(( status & RX_INCOMPLETE )==0) {
  244. #ifdef EDEBUG
  245. printf("=%d",nic->packetlen);
  246. #endif
  247. break;
  248. }
  249. udelay(1000);
  250. }
  251. /* acknowledge reception of packet */
  252. outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
  253. while (inw(BASE + VX_STATUS) & S_COMMAND_IN_PROGRESS);
  254. #ifdef EDEBUG
  255. {
  256. unsigned short type = 0; /* used by EDEBUG */
  257. type = (nic->packet[12]<<8) | nic->packet[13];
  258. if(nic->packet[0]+nic->packet[1]+nic->packet[2]+nic->packet[3]+nic->packet[4]+
  259. nic->packet[5] == 0xFF*ETH_ALEN)
  260. printf(",t=%hX,b]",type);
  261. else
  262. printf(",t=%hX]",type);
  263. }
  264. #endif
  265. return 1;
  266. }
  267. /*************************************************************************
  268. 3Com 595 - specific routines
  269. **************************************************************************/
  270. static int
  271. eeprom_rdy()
  272. {
  273. int i;
  274. for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
  275. udelay(1000);
  276. if (i >= MAX_EEPROMBUSY) {
  277. /* printf("3c595: eeprom failed to come ready.\n"); */
  278. printf("3c595: eeprom is busy.\n"); /* memory in EPROM is tight */
  279. return (0);
  280. }
  281. return (1);
  282. }
  283. /*
  284. * get_e: gets a 16 bits word from the EEPROM. we must have set the window
  285. * before
  286. */
  287. static int
  288. get_e(offset)
  289. int offset;
  290. {
  291. if (!eeprom_rdy())
  292. return (0xffff);
  293. outw(EEPROM_CMD_RD | offset, BASE + VX_W0_EEPROM_COMMAND);
  294. if (!eeprom_rdy())
  295. return (0xffff);
  296. return (inw(BASE + VX_W0_EEPROM_DATA));
  297. }
  298. static void
  299. vxgetlink(void)
  300. {
  301. int n, k;
  302. GO_WINDOW(3);
  303. vx_connectors = inw(BASE + VX_W3_RESET_OPT) & 0x7f;
  304. for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
  305. if (vx_connectors & conn_tab[k].bit) {
  306. if (n > 0) {
  307. printf("/");
  308. }
  309. printf(conn_tab[k].name);
  310. n++;
  311. }
  312. }
  313. if (vx_connectors == 0) {
  314. printf("no connectors!");
  315. return;
  316. }
  317. GO_WINDOW(3);
  318. vx_connector = (inl(BASE + VX_W3_INTERNAL_CFG)
  319. & INTERNAL_CONNECTOR_MASK)
  320. >> INTERNAL_CONNECTOR_BITS;
  321. if (vx_connector & 0x10) {
  322. vx_connector &= 0x0f;
  323. printf("[*%s*]", conn_tab[vx_connector].name);
  324. printf(": disable 'auto select' with DOS util!");
  325. } else {
  326. printf("[*%s*]", conn_tab[vx_connector].name);
  327. }
  328. }
  329. static void
  330. vxsetlink(void)
  331. {
  332. int i, j;
  333. char *reason, *warning;
  334. static char prev_conn = -1;
  335. if (prev_conn == -1) {
  336. prev_conn = vx_connector;
  337. }
  338. i = vx_connector; /* default in EEPROM */
  339. reason = "default";
  340. warning = 0;
  341. if ((vx_connectors & conn_tab[vx_connector].bit) == 0) {
  342. warning = "strange connector type in EEPROM.";
  343. reason = "forced";
  344. i = CONNECTOR_UTP;
  345. }
  346. if (warning != 0) {
  347. printf("warning: %s\n", warning);
  348. }
  349. printf("selected %s. (%s)\n", conn_tab[i].name, reason);
  350. /* Set the selected connector. */
  351. GO_WINDOW(3);
  352. j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
  353. outl(j | (i <<INTERNAL_CONNECTOR_BITS), BASE + VX_W3_INTERNAL_CFG);
  354. /* First, disable all. */
  355. outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
  356. udelay(8000);
  357. GO_WINDOW(4);
  358. outw(0, BASE + VX_W4_MEDIA_TYPE);
  359. /* Second, enable the selected one. */
  360. switch(i) {
  361. case CONNECTOR_UTP:
  362. GO_WINDOW(4);
  363. outw(ENABLE_UTP, BASE + VX_W4_MEDIA_TYPE);
  364. break;
  365. case CONNECTOR_BNC:
  366. outw(START_TRANSCEIVER,BASE + VX_COMMAND);
  367. udelay(8000);
  368. break;
  369. case CONNECTOR_TX:
  370. case CONNECTOR_FX:
  371. GO_WINDOW(4);
  372. outw(LINKBEAT_ENABLE, BASE + VX_W4_MEDIA_TYPE);
  373. break;
  374. default: /* AUI and MII fall here */
  375. break;
  376. }
  377. GO_WINDOW(1);
  378. }
  379. static void t595_disable ( struct nic *nic ) {
  380. t595_reset(nic);
  381. outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
  382. udelay(8000);
  383. GO_WINDOW(4);
  384. outw(0, BASE + VX_W4_MEDIA_TYPE);
  385. GO_WINDOW(1);
  386. }
  387. static void t595_irq(struct nic *nic __unused, irq_action_t action __unused)
  388. {
  389. switch ( action ) {
  390. case DISABLE :
  391. break;
  392. case ENABLE :
  393. break;
  394. case FORCE :
  395. break;
  396. }
  397. }
  398. /**************************************************************************
  399. ETH_PROBE - Look for an adapter
  400. ***************************************************************************/
  401. static int t595_probe ( struct nic *nic, struct pci_device *pci ) {
  402. int i;
  403. unsigned short *p;
  404. if (pci->ioaddr == 0)
  405. return 0;
  406. eth_nic_base = pci->ioaddr;
  407. nic->irqno = 0;
  408. pci_fill_nic ( nic, pci );
  409. nic->ioaddr = pci->ioaddr;
  410. GO_WINDOW(0);
  411. outw(GLOBAL_RESET, BASE + VX_COMMAND);
  412. VX_BUSY_WAIT;
  413. vxgetlink();
  414. /*
  415. printf("\nEEPROM:");
  416. for (i = 0; i < (EEPROMSIZE/2); i++) {
  417. printf("%hX:", get_e(i));
  418. }
  419. printf("\n");
  420. */
  421. /*
  422. * Read the station address from the eeprom
  423. */
  424. p = (unsigned short *) nic->node_addr;
  425. for (i = 0; i < 3; i++) {
  426. GO_WINDOW(0);
  427. p[i] = htons(get_e(EEPROM_OEM_ADDR_0 + i));
  428. GO_WINDOW(2);
  429. outw(ntohs(p[i]), BASE + VX_W2_ADDR_0 + (i * 2));
  430. }
  431. DBG ( "Ethernet address: %s\n", eth_ntoa (nic->node_addr) );
  432. t595_reset(nic);
  433. nic->nic_op = &t595_operations;
  434. return 1;
  435. }
  436. static struct nic_operations t595_operations = {
  437. .connect = dummy_connect,
  438. .poll = t595_poll,
  439. .transmit = t595_transmit,
  440. .irq = t595_irq,
  441. };
  442. static struct pci_device_id t595_nics[] = {
  443. PCI_ROM(0x10b7, 0x5900, "3c590", "3Com590"), /* Vortex 10Mbps */
  444. PCI_ROM(0x10b7, 0x5950, "3c595", "3Com595"), /* Vortex 100baseTx */
  445. PCI_ROM(0x10b7, 0x5951, "3c595-1", "3Com595"), /* Vortex 100baseT4 */
  446. PCI_ROM(0x10b7, 0x5952, "3c595-2", "3Com595"), /* Vortex 100base-MII */
  447. PCI_ROM(0x10b7, 0x9000, "3c900-tpo", "3Com900-TPO"), /* 10 Base TPO */
  448. PCI_ROM(0x10b7, 0x9001, "3c900-t4", "3Com900-Combo"), /* 10/100 T4 */
  449. PCI_ROM(0x10b7, 0x9004, "3c900b-tpo", "3Com900B-TPO"), /* 10 Base TPO */
  450. PCI_ROM(0x10b7, 0x9005, "3c900b-combo", "3Com900B-Combo"), /* 10 Base Combo */
  451. PCI_ROM(0x10b7, 0x9006, "3c900b-tpb2", "3Com900B-2/T"), /* 10 Base TP and Base2 */
  452. PCI_ROM(0x10b7, 0x900a, "3c900b-fl", "3Com900B-FL"), /* 10 Base F */
  453. PCI_ROM(0x10b7, 0x9800, "3c980-cyclone-1", "3Com980-Cyclone"), /* Cyclone */
  454. PCI_ROM(0x10b7, 0x9805, "3c9805-1", "3Com9805"), /* Dual Port Server Cyclone */
  455. PCI_ROM(0x10b7, 0x7646, "3csoho100-tx-1", "3CSOHO100-TX"), /* Hurricane */
  456. PCI_ROM(0x10b7, 0x4500, "3c450-1", "3Com450 HomePNA Tornado"),
  457. };
  458. PCI_DRIVER ( t595_driver, t595_nics, PCI_NO_CLASS );
  459. DRIVER ( "3C595", nic_driver, pci_driver, t595_driver,
  460. t595_probe, t595_disable );
  461. /*
  462. * Local variables:
  463. * c-basic-offset: 8
  464. * End:
  465. */