3c595.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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. */
  24. /* #define EDEBUG */
  25. #include "etherboot.h"
  26. #include "nic.h"
  27. #include "pci.h"
  28. #include "3c595.h"
  29. #include "timer.h"
  30. static unsigned short eth_nic_base, eth_asic_base;
  31. static unsigned short vx_connector, vx_connectors;
  32. static struct connector_entry {
  33. int bit;
  34. char *name;
  35. } conn_tab[VX_CONNECTORS] = {
  36. #define CONNECTOR_UTP 0
  37. { 0x08, "utp"},
  38. #define CONNECTOR_AUI 1
  39. { 0x20, "aui"},
  40. /* dummy */
  41. { 0, "???"},
  42. #define CONNECTOR_BNC 3
  43. { 0x10, "bnc"},
  44. #define CONNECTOR_TX 4
  45. { 0x02, "tx"},
  46. #define CONNECTOR_FX 5
  47. { 0x04, "fx"},
  48. #define CONNECTOR_MII 6
  49. { 0x40, "mii"},
  50. { 0, "???"}
  51. };
  52. static void vxgetlink(void);
  53. static void vxsetlink(void);
  54. #define udelay(n) waiton_timer2(((n)*TICKS_PER_MS)/1000)
  55. /**************************************************************************
  56. ETH_RESET - Reset adapter
  57. ***************************************************************************/
  58. static void t595_reset(struct nic *nic)
  59. {
  60. int i, j;
  61. /***********************************************************
  62. Reset 3Com 595 card
  63. *************************************************************/
  64. /* stop card */
  65. outw(RX_DISABLE, BASE + VX_COMMAND);
  66. outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
  67. VX_BUSY_WAIT;
  68. outw(TX_DISABLE, BASE + VX_COMMAND);
  69. outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
  70. udelay(8000);
  71. outw(RX_RESET, BASE + VX_COMMAND);
  72. VX_BUSY_WAIT;
  73. outw(TX_RESET, BASE + VX_COMMAND);
  74. VX_BUSY_WAIT;
  75. outw(C_INTR_LATCH, BASE + VX_COMMAND);
  76. outw(SET_RD_0_MASK, BASE + VX_COMMAND);
  77. outw(SET_INTR_MASK, BASE + VX_COMMAND);
  78. outw(SET_RX_FILTER, BASE + VX_COMMAND);
  79. /*
  80. * initialize card
  81. */
  82. VX_BUSY_WAIT;
  83. GO_WINDOW(0);
  84. /* Disable the card */
  85. /* outw(0, BASE + VX_W0_CONFIG_CTRL); */
  86. /* Configure IRQ to none */
  87. /* outw(SET_IRQ(0), BASE + VX_W0_RESOURCE_CFG); */
  88. /* Enable the card */
  89. /* outw(ENABLE_DRQ_IRQ, BASE + VX_W0_CONFIG_CTRL); */
  90. GO_WINDOW(2);
  91. /* Reload the ether_addr. */
  92. for (i = 0; i < ETH_ALEN; i++)
  93. outb(nic->node_addr[i], BASE + VX_W2_ADDR_0 + i);
  94. outw(RX_RESET, BASE + VX_COMMAND);
  95. VX_BUSY_WAIT;
  96. outw(TX_RESET, BASE + VX_COMMAND);
  97. VX_BUSY_WAIT;
  98. /* Window 1 is operating window */
  99. GO_WINDOW(1);
  100. for (i = 0; i < 31; i++)
  101. inb(BASE + VX_W1_TX_STATUS);
  102. outw(SET_RD_0_MASK | S_CARD_FAILURE | S_RX_COMPLETE |
  103. S_TX_COMPLETE | S_TX_AVAIL, BASE + VX_COMMAND);
  104. outw(SET_INTR_MASK | S_CARD_FAILURE | S_RX_COMPLETE |
  105. S_TX_COMPLETE | S_TX_AVAIL, BASE + VX_COMMAND);
  106. /*
  107. * Attempt to get rid of any stray interrupts that occured during
  108. * configuration. On the i386 this isn't possible because one may
  109. * already be queued. However, a single stray interrupt is
  110. * unimportant.
  111. */
  112. outw(ACK_INTR | 0xff, BASE + VX_COMMAND);
  113. outw(SET_RX_FILTER | FIL_INDIVIDUAL |
  114. FIL_BRDCST, BASE + VX_COMMAND);
  115. vxsetlink();
  116. /*{
  117. int i,j;
  118. i = CONNECTOR_TX;
  119. GO_WINDOW(3);
  120. j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
  121. outl(BASE + VX_W3_INTERNAL_CFG, j | (i <<INTERNAL_CONNECTOR_BITS));
  122. GO_WINDOW(4);
  123. outw(LINKBEAT_ENABLE, BASE + VX_W4_MEDIA_TYPE);
  124. GO_WINDOW(1);
  125. }*/
  126. /* start tranciever and receiver */
  127. outw(RX_ENABLE, BASE + VX_COMMAND);
  128. outw(TX_ENABLE, BASE + VX_COMMAND);
  129. }
  130. /**************************************************************************
  131. ETH_TRANSMIT - Transmit a frame
  132. ***************************************************************************/
  133. static char padmap[] = {
  134. 0, 3, 2, 1};
  135. static void t595_transmit(
  136. struct nic *nic,
  137. const char *d, /* Destination */
  138. unsigned int t, /* Type */
  139. unsigned int s, /* size */
  140. const char *p) /* Packet */
  141. {
  142. register int len;
  143. int pad;
  144. int status;
  145. #ifdef EDEBUG
  146. printf("{l=%d,t=%hX}",s+ETH_HLEN,t);
  147. #endif
  148. /* swap bytes of type */
  149. t= htons(t);
  150. len=s+ETH_HLEN; /* actual length of packet */
  151. pad = padmap[len & 3];
  152. /*
  153. * The 3c595 automatically pads short packets to minimum ethernet length,
  154. * but we drop packets that are too large. Perhaps we should truncate
  155. * them instead?
  156. */
  157. if (len + pad > ETH_FRAME_LEN) {
  158. return;
  159. }
  160. /* drop acknowledgements */
  161. while(( status=inb(BASE + VX_W1_TX_STATUS) )& TXS_COMPLETE ) {
  162. if(status & (TXS_UNDERRUN|TXS_MAX_COLLISION|TXS_STATUS_OVERFLOW)) {
  163. outw(TX_RESET, BASE + VX_COMMAND);
  164. outw(TX_ENABLE, BASE + VX_COMMAND);
  165. }
  166. outb(0x0, BASE + VX_W1_TX_STATUS);
  167. }
  168. while (inw(BASE + VX_W1_FREE_TX) < len + pad + 4) {
  169. /* no room in FIFO */
  170. }
  171. outw(len, BASE + VX_W1_TX_PIO_WR_1);
  172. outw(0x0, BASE + VX_W1_TX_PIO_WR_1); /* Second dword meaningless */
  173. /* write packet */
  174. outsw(BASE + VX_W1_TX_PIO_WR_1, d, ETH_ALEN/2);
  175. outsw(BASE + VX_W1_TX_PIO_WR_1, nic->node_addr, ETH_ALEN/2);
  176. outw(t, BASE + VX_W1_TX_PIO_WR_1);
  177. outsw(BASE + VX_W1_TX_PIO_WR_1, p, s / 2);
  178. if (s & 1)
  179. outb(*(p+s - 1), BASE + VX_W1_TX_PIO_WR_1);
  180. while (pad--)
  181. outb(0, BASE + VX_W1_TX_PIO_WR_1); /* Padding */
  182. /* wait for Tx complete */
  183. while((inw(BASE + VX_STATUS) & S_COMMAND_IN_PROGRESS) != 0)
  184. ;
  185. }
  186. /**************************************************************************
  187. ETH_POLL - Wait for a frame
  188. ***************************************************************************/
  189. static int t595_poll(struct nic *nic)
  190. {
  191. /* common variables */
  192. unsigned short type = 0; /* used by EDEBUG */
  193. /* variables for 3C595 */
  194. short status, cst;
  195. register short rx_fifo;
  196. cst=inw(BASE + VX_STATUS);
  197. #ifdef EDEBUG
  198. if(cst & 0x1FFF)
  199. printf("-%hX-",cst);
  200. #endif
  201. if( (cst & S_RX_COMPLETE)==0 ) {
  202. /* acknowledge everything */
  203. outw(ACK_INTR | cst, BASE + VX_COMMAND);
  204. outw(C_INTR_LATCH, BASE + VX_COMMAND);
  205. return 0;
  206. }
  207. status = inw(BASE + VX_W1_RX_STATUS);
  208. #ifdef EDEBUG
  209. printf("*%hX*",status);
  210. #endif
  211. if (status & ERR_RX) {
  212. outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
  213. return 0;
  214. }
  215. rx_fifo = status & RX_BYTES_MASK;
  216. if (rx_fifo==0)
  217. return 0;
  218. /* read packet */
  219. #ifdef EDEBUG
  220. printf("[l=%d",rx_fifo);
  221. #endif
  222. insw(BASE + VX_W1_RX_PIO_RD_1, nic->packet, rx_fifo / 2);
  223. if(rx_fifo & 1)
  224. nic->packet[rx_fifo-1]=inb(BASE + VX_W1_RX_PIO_RD_1);
  225. nic->packetlen=rx_fifo;
  226. while(1) {
  227. status = inw(BASE + VX_W1_RX_STATUS);
  228. #ifdef EDEBUG
  229. printf("*%hX*",status);
  230. #endif
  231. rx_fifo = status & RX_BYTES_MASK;
  232. if(rx_fifo>0) {
  233. insw(BASE + VX_W1_RX_PIO_RD_1, nic->packet+nic->packetlen, rx_fifo / 2);
  234. if(rx_fifo & 1)
  235. nic->packet[nic->packetlen+rx_fifo-1]=inb(BASE + VX_W1_RX_PIO_RD_1);
  236. nic->packetlen+=rx_fifo;
  237. #ifdef EDEBUG
  238. printf("+%d",rx_fifo);
  239. #endif
  240. }
  241. if(( status & RX_INCOMPLETE )==0) {
  242. #ifdef EDEBUG
  243. printf("=%d",nic->packetlen);
  244. #endif
  245. break;
  246. }
  247. udelay(1000);
  248. }
  249. /* acknowledge reception of packet */
  250. outw(RX_DISCARD_TOP_PACK, BASE + VX_COMMAND);
  251. while (inw(BASE + VX_STATUS) & S_COMMAND_IN_PROGRESS);
  252. #ifdef EDEBUG
  253. type = (nic->packet[12]<<8) | nic->packet[13];
  254. if(nic->packet[0]+nic->packet[1]+nic->packet[2]+nic->packet[3]+nic->packet[4]+
  255. nic->packet[5] == 0xFF*ETH_ALEN)
  256. printf(",t=%hX,b]",type);
  257. else
  258. printf(",t=%hX]",type);
  259. #endif
  260. return 1;
  261. }
  262. /*************************************************************************
  263. 3Com 595 - specific routines
  264. **************************************************************************/
  265. static int
  266. eeprom_rdy()
  267. {
  268. int i;
  269. for (i = 0; is_eeprom_busy(BASE) && i < MAX_EEPROMBUSY; i++)
  270. udelay(1000);
  271. if (i >= MAX_EEPROMBUSY) {
  272. /* printf("3c595: eeprom failed to come ready.\n"); */
  273. printf("3c595: eeprom is busy.\n"); /* memory in EPROM is tight */
  274. return (0);
  275. }
  276. return (1);
  277. }
  278. /*
  279. * get_e: gets a 16 bits word from the EEPROM. we must have set the window
  280. * before
  281. */
  282. static int
  283. get_e(offset)
  284. int offset;
  285. {
  286. if (!eeprom_rdy())
  287. return (0xffff);
  288. outw(EEPROM_CMD_RD | offset, BASE + VX_W0_EEPROM_COMMAND);
  289. if (!eeprom_rdy())
  290. return (0xffff);
  291. return (inw(BASE + VX_W0_EEPROM_DATA));
  292. }
  293. static void
  294. vxgetlink(void)
  295. {
  296. int n, k;
  297. GO_WINDOW(3);
  298. vx_connectors = inw(BASE + VX_W3_RESET_OPT) & 0x7f;
  299. for (n = 0, k = 0; k < VX_CONNECTORS; k++) {
  300. if (vx_connectors & conn_tab[k].bit) {
  301. if (n > 0) {
  302. printf("/");
  303. }
  304. printf(conn_tab[k].name);
  305. n++;
  306. }
  307. }
  308. if (vx_connectors == 0) {
  309. printf("no connectors!");
  310. return;
  311. }
  312. GO_WINDOW(3);
  313. vx_connector = (inl(BASE + VX_W3_INTERNAL_CFG)
  314. & INTERNAL_CONNECTOR_MASK)
  315. >> INTERNAL_CONNECTOR_BITS;
  316. if (vx_connector & 0x10) {
  317. vx_connector &= 0x0f;
  318. printf("[*%s*]", conn_tab[vx_connector].name);
  319. printf(": disable 'auto select' with DOS util!");
  320. } else {
  321. printf("[*%s*]", conn_tab[vx_connector].name);
  322. }
  323. }
  324. static void
  325. vxsetlink(void)
  326. {
  327. int i, j, k;
  328. char *reason, *warning;
  329. static short prev_flags;
  330. static char prev_conn = -1;
  331. if (prev_conn == -1) {
  332. prev_conn = vx_connector;
  333. }
  334. i = vx_connector; /* default in EEPROM */
  335. reason = "default";
  336. warning = 0;
  337. if ((vx_connectors & conn_tab[vx_connector].bit) == 0) {
  338. warning = "strange connector type in EEPROM.";
  339. reason = "forced";
  340. i = CONNECTOR_UTP;
  341. }
  342. if (warning != 0) {
  343. printf("warning: %s\n", warning);
  344. }
  345. printf("selected %s. (%s)\n", conn_tab[i].name, reason);
  346. /* Set the selected connector. */
  347. GO_WINDOW(3);
  348. j = inl(BASE + VX_W3_INTERNAL_CFG) & ~INTERNAL_CONNECTOR_MASK;
  349. outl(j | (i <<INTERNAL_CONNECTOR_BITS), BASE + VX_W3_INTERNAL_CFG);
  350. /* First, disable all. */
  351. outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
  352. udelay(8000);
  353. GO_WINDOW(4);
  354. outw(0, BASE + VX_W4_MEDIA_TYPE);
  355. /* Second, enable the selected one. */
  356. switch(i) {
  357. case CONNECTOR_UTP:
  358. GO_WINDOW(4);
  359. outw(ENABLE_UTP, BASE + VX_W4_MEDIA_TYPE);
  360. break;
  361. case CONNECTOR_BNC:
  362. outw(START_TRANSCEIVER,BASE + VX_COMMAND);
  363. udelay(8000);
  364. break;
  365. case CONNECTOR_TX:
  366. case CONNECTOR_FX:
  367. GO_WINDOW(4);
  368. outw(LINKBEAT_ENABLE, BASE + VX_W4_MEDIA_TYPE);
  369. break;
  370. default: /* AUI and MII fall here */
  371. break;
  372. }
  373. GO_WINDOW(1);
  374. }
  375. static void t595_disable(struct nic *nic)
  376. {
  377. outw(STOP_TRANSCEIVER, BASE + VX_COMMAND);
  378. udelay(8000);
  379. GO_WINDOW(4);
  380. outw(0, BASE + VX_W4_MEDIA_TYPE);
  381. GO_WINDOW(1);
  382. }
  383. /**************************************************************************
  384. ETH_PROBE - Look for an adapter
  385. ***************************************************************************/
  386. struct nic *t595_probe(struct nic *nic, unsigned short *probeaddrs, struct pci_device *pci)
  387. {
  388. int i;
  389. unsigned short *p;
  390. if (probeaddrs == 0 || probeaddrs[0] == 0)
  391. return 0;
  392. /* eth_nic_base = probeaddrs[0] & ~3; */
  393. eth_nic_base = pci->ioaddr;
  394. GO_WINDOW(0);
  395. outw(GLOBAL_RESET, BASE + VX_COMMAND);
  396. VX_BUSY_WAIT;
  397. vxgetlink();
  398. /*
  399. printf("\nEEPROM:");
  400. for (i = 0; i < (EEPROMSIZE/2); i++) {
  401. printf("%hX:", get_e(i));
  402. }
  403. printf("\n");
  404. */
  405. /*
  406. * Read the station address from the eeprom
  407. */
  408. p = (unsigned short *) nic->node_addr;
  409. for (i = 0; i < 3; i++) {
  410. GO_WINDOW(0);
  411. p[i] = htons(get_e(EEPROM_OEM_ADDR_0 + i));
  412. GO_WINDOW(2);
  413. outw(ntohs(p[i]), BASE + VX_W2_ADDR_0 + (i * 2));
  414. }
  415. printf("Ethernet address: %!\n", nic->node_addr);
  416. t595_reset(nic);
  417. nic->reset = t595_reset;
  418. nic->poll = t595_poll;
  419. nic->transmit = t595_transmit;
  420. nic->disable = t595_disable;
  421. return nic;
  422. }
  423. /*
  424. * Local variables:
  425. * c-basic-offset: 8
  426. * End:
  427. */