Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

3c5x9.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /**************************************************************************
  2. ETHERBOOT - BOOTP/TFTP Bootstrap Program
  3. Author: Martin Renters.
  4. Date: Mar 22 1995
  5. This code is based heavily on David Greenman's if_ed.c driver and
  6. Andres Vega Garcia's if_ep.c driver.
  7. Copyright (C) 1993-1994, David Greenman, Martin Renters.
  8. Copyright (C) 1993-1995, Andres Vega Garcia.
  9. Copyright (C) 1995, Serge Babkin.
  10. This software may be used, modified, copied, distributed, and sold, in
  11. both source and binary form provided that the above copyright and these
  12. terms are retained. Under no circumstances are the authors responsible for
  13. the proper functioning of this software, nor do the authors assume any
  14. responsibility for damages incurred with its use.
  15. 3c509 support added by Serge Babkin (babkin@hq.icb.chel.su)
  16. $Id$
  17. ***************************************************************************/
  18. /* #define EDEBUG */
  19. #include <gpxe/ethernet.h>
  20. #include "etherboot.h"
  21. #include "nic.h"
  22. #include "isa.h"
  23. #include "timer.h"
  24. #include "3c509.h"
  25. static enum { none, bnc, utp } connector = none; /* for 3C509 */
  26. /**************************************************************************
  27. ETH_RESET - Reset adapter
  28. ***************************************************************************/
  29. void t5x9_disable ( struct nic *nic ) {
  30. /* stop card */
  31. outw(RX_DISABLE, nic->ioaddr + EP_COMMAND);
  32. outw(RX_DISCARD_TOP_PACK, nic->ioaddr + EP_COMMAND);
  33. while (inw(nic->ioaddr + EP_STATUS) & S_COMMAND_IN_PROGRESS)
  34. ;
  35. outw(TX_DISABLE, nic->ioaddr + EP_COMMAND);
  36. outw(STOP_TRANSCEIVER, nic->ioaddr + EP_COMMAND);
  37. udelay(1000);
  38. outw(RX_RESET, nic->ioaddr + EP_COMMAND);
  39. outw(TX_RESET, nic->ioaddr + EP_COMMAND);
  40. outw(C_INTR_LATCH, nic->ioaddr + EP_COMMAND);
  41. outw(SET_RD_0_MASK, nic->ioaddr + EP_COMMAND);
  42. outw(SET_INTR_MASK, nic->ioaddr + EP_COMMAND);
  43. outw(SET_RX_FILTER, nic->ioaddr + EP_COMMAND);
  44. /*
  45. * wait for reset to complete
  46. */
  47. while (inw(nic->ioaddr + EP_STATUS) & S_COMMAND_IN_PROGRESS)
  48. ;
  49. GO_WINDOW(nic->ioaddr,0);
  50. /* Disable the card */
  51. outw(0, nic->ioaddr + EP_W0_CONFIG_CTRL);
  52. /* Configure IRQ to none */
  53. outw(SET_IRQ(0), nic->ioaddr + EP_W0_RESOURCE_CFG);
  54. }
  55. static void t509_enable ( struct nic *nic ) {
  56. int i;
  57. /* Enable the card */
  58. GO_WINDOW(nic->ioaddr,0);
  59. outw(ENABLE_DRQ_IRQ, nic->ioaddr + EP_W0_CONFIG_CTRL);
  60. GO_WINDOW(nic->ioaddr,2);
  61. /* Reload the ether_addr. */
  62. for (i = 0; i < ETH_ALEN; i++)
  63. outb(nic->node_addr[i], nic->ioaddr + EP_W2_ADDR_0 + i);
  64. outw(RX_RESET, nic->ioaddr + EP_COMMAND);
  65. outw(TX_RESET, nic->ioaddr + EP_COMMAND);
  66. /* Window 1 is operating window */
  67. GO_WINDOW(nic->ioaddr,1);
  68. for (i = 0; i < 31; i++)
  69. inb(nic->ioaddr + EP_W1_TX_STATUS);
  70. /* get rid of stray intr's */
  71. outw(ACK_INTR | 0xff, nic->ioaddr + EP_COMMAND);
  72. outw(SET_RD_0_MASK | S_5_INTS, nic->ioaddr + EP_COMMAND);
  73. outw(SET_INTR_MASK, nic->ioaddr + EP_COMMAND);
  74. outw(SET_RX_FILTER | FIL_GROUP | FIL_INDIVIDUAL | FIL_BRDCST,
  75. nic->ioaddr + EP_COMMAND);
  76. /* configure BNC */
  77. if (connector == bnc) {
  78. outw(START_TRANSCEIVER, nic->ioaddr + EP_COMMAND);
  79. udelay(1000);
  80. }
  81. /* configure UTP */
  82. else if (connector == utp) {
  83. GO_WINDOW(nic->ioaddr,4);
  84. outw(ENABLE_UTP, nic->ioaddr + EP_W4_MEDIA_TYPE);
  85. sleep(2); /* Give time for media to negotiate */
  86. GO_WINDOW(nic->ioaddr,1);
  87. }
  88. /* start transceiver and receiver */
  89. outw(RX_ENABLE, nic->ioaddr + EP_COMMAND);
  90. outw(TX_ENABLE, nic->ioaddr + EP_COMMAND);
  91. /* set early threshold for minimal packet length */
  92. outw(SET_RX_EARLY_THRESH | ETH_ZLEN, nic->ioaddr + EP_COMMAND);
  93. outw(SET_TX_START_THRESH | 16, nic->ioaddr + EP_COMMAND);
  94. }
  95. static void t509_reset ( struct nic *nic ) {
  96. t5x9_disable ( nic );
  97. t509_enable ( nic );
  98. }
  99. /**************************************************************************
  100. ETH_TRANSMIT - Transmit a frame
  101. ***************************************************************************/
  102. static char padmap[] = {
  103. 0, 3, 2, 1};
  104. static void t509_transmit(
  105. struct nic *nic,
  106. const char *d, /* Destination */
  107. unsigned int t, /* Type */
  108. unsigned int s, /* size */
  109. const char *p) /* Packet */
  110. {
  111. register unsigned int len;
  112. int pad;
  113. int status;
  114. #ifdef EDEBUG
  115. printf("{l=%d,t=%hX}",s+ETH_HLEN,t);
  116. #endif
  117. /* swap bytes of type */
  118. t= htons(t);
  119. len=s+ETH_HLEN; /* actual length of packet */
  120. pad = padmap[len & 3];
  121. /*
  122. * The 3c509 automatically pads short packets to minimum ethernet length,
  123. * but we drop packets that are too large. Perhaps we should truncate
  124. * them instead?
  125. */
  126. if (len + pad > ETH_FRAME_LEN) {
  127. return;
  128. }
  129. /* drop acknowledgements */
  130. while ((status=inb(nic->ioaddr + EP_W1_TX_STATUS)) & TXS_COMPLETE ) {
  131. if (status & (TXS_UNDERRUN|TXS_MAX_COLLISION|TXS_STATUS_OVERFLOW)) {
  132. outw(TX_RESET, nic->ioaddr + EP_COMMAND);
  133. outw(TX_ENABLE, nic->ioaddr + EP_COMMAND);
  134. }
  135. outb(0x0, nic->ioaddr + EP_W1_TX_STATUS);
  136. }
  137. while (inw(nic->ioaddr + EP_W1_FREE_TX) < (unsigned short)len + pad + 4)
  138. ; /* no room in FIFO */
  139. outw(len, nic->ioaddr + EP_W1_TX_PIO_WR_1);
  140. outw(0x0, nic->ioaddr + EP_W1_TX_PIO_WR_1); /* Second dword meaningless */
  141. /* write packet */
  142. outsw(nic->ioaddr + EP_W1_TX_PIO_WR_1, d, ETH_ALEN/2);
  143. outsw(nic->ioaddr + EP_W1_TX_PIO_WR_1, nic->node_addr, ETH_ALEN/2);
  144. outw(t, nic->ioaddr + EP_W1_TX_PIO_WR_1);
  145. outsw(nic->ioaddr + EP_W1_TX_PIO_WR_1, p, s / 2);
  146. if (s & 1)
  147. outb(*(p+s - 1), nic->ioaddr + EP_W1_TX_PIO_WR_1);
  148. while (pad--)
  149. outb(0, nic->ioaddr + EP_W1_TX_PIO_WR_1); /* Padding */
  150. /* wait for Tx complete */
  151. while((inw(nic->ioaddr + EP_STATUS) & S_COMMAND_IN_PROGRESS) != 0)
  152. ;
  153. }
  154. /**************************************************************************
  155. ETH_POLL - Wait for a frame
  156. ***************************************************************************/
  157. static int t509_poll(struct nic *nic, int retrieve)
  158. {
  159. /* common variables */
  160. /* variables for 3C509 */
  161. short status, cst;
  162. register short rx_fifo;
  163. cst=inw(nic->ioaddr + EP_STATUS);
  164. #ifdef EDEBUG
  165. if(cst & 0x1FFF)
  166. printf("-%hX-",cst);
  167. #endif
  168. if( (cst & S_RX_COMPLETE)==0 ) {
  169. /* acknowledge everything */
  170. outw(ACK_INTR| (cst & S_5_INTS), nic->ioaddr + EP_COMMAND);
  171. outw(C_INTR_LATCH, nic->ioaddr + EP_COMMAND);
  172. return 0;
  173. }
  174. status = inw(nic->ioaddr + EP_W1_RX_STATUS);
  175. #ifdef EDEBUG
  176. printf("*%hX*",status);
  177. #endif
  178. if (status & ERR_RX) {
  179. outw(RX_DISCARD_TOP_PACK, nic->ioaddr + EP_COMMAND);
  180. return 0;
  181. }
  182. rx_fifo = status & RX_BYTES_MASK;
  183. if (rx_fifo==0)
  184. return 0;
  185. if ( ! retrieve ) return 1;
  186. /* read packet */
  187. #ifdef EDEBUG
  188. printf("[l=%d",rx_fifo);
  189. #endif
  190. insw(nic->ioaddr + EP_W1_RX_PIO_RD_1, nic->packet, rx_fifo / 2);
  191. if(rx_fifo & 1)
  192. nic->packet[rx_fifo-1]=inb(nic->ioaddr + EP_W1_RX_PIO_RD_1);
  193. nic->packetlen=rx_fifo;
  194. while(1) {
  195. status = inw(nic->ioaddr + EP_W1_RX_STATUS);
  196. #ifdef EDEBUG
  197. printf("*%hX*",status);
  198. #endif
  199. rx_fifo = status & RX_BYTES_MASK;
  200. if(rx_fifo>0) {
  201. insw(nic->ioaddr + EP_W1_RX_PIO_RD_1, nic->packet+nic->packetlen, rx_fifo / 2);
  202. if(rx_fifo & 1)
  203. nic->packet[nic->packetlen+rx_fifo-1]=inb(nic->ioaddr + EP_W1_RX_PIO_RD_1);
  204. nic->packetlen+=rx_fifo;
  205. #ifdef EDEBUG
  206. printf("+%d",rx_fifo);
  207. #endif
  208. }
  209. if(( status & RX_INCOMPLETE )==0) {
  210. #ifdef EDEBUG
  211. printf("=%d",nic->packetlen);
  212. #endif
  213. break;
  214. }
  215. udelay(1000); /* if incomplete wait 1 ms */
  216. }
  217. /* acknowledge reception of packet */
  218. outw(RX_DISCARD_TOP_PACK, nic->ioaddr + EP_COMMAND);
  219. while (inw(nic->ioaddr + EP_STATUS) & S_COMMAND_IN_PROGRESS)
  220. ;
  221. #ifdef EDEBUG
  222. {
  223. unsigned short type = 0; /* used by EDEBUG */
  224. type = (nic->packet[12]<<8) | nic->packet[13];
  225. if(nic->packet[0]+nic->packet[1]+nic->packet[2]+nic->packet[3]+nic->packet[4]+
  226. nic->packet[5] == 0xFF*ETH_ALEN)
  227. printf(",t=%hX,b]",type);
  228. else
  229. printf(",t=%hX]",type);
  230. }
  231. #endif
  232. return (1);
  233. }
  234. /**************************************************************************
  235. ETH_IRQ - interrupt handling
  236. ***************************************************************************/
  237. static void t509_irq(struct nic *nic __unused, irq_action_t action __unused)
  238. {
  239. switch ( action ) {
  240. case DISABLE :
  241. break;
  242. case ENABLE :
  243. break;
  244. case FORCE :
  245. break;
  246. }
  247. }
  248. /*************************************************************************
  249. 3Com 509 - specific routines
  250. **************************************************************************/
  251. static int eeprom_rdy ( uint16_t ioaddr ) {
  252. int i;
  253. for (i = 0; is_eeprom_busy(ioaddr) && i < MAX_EEPROMBUSY; i++);
  254. if (i >= MAX_EEPROMBUSY) {
  255. /* printf("3c509: eeprom failed to come ready.\n"); */
  256. /* memory in EPROM is tight */
  257. /* printf("3c509: eeprom busy.\n"); */
  258. return (0);
  259. }
  260. return (1);
  261. }
  262. /*
  263. * get_e: gets a 16 bits word from the EEPROM.
  264. */
  265. static int get_e ( uint16_t ioaddr, int offset ) {
  266. GO_WINDOW(ioaddr,0);
  267. if (!eeprom_rdy(ioaddr))
  268. return (0xffff);
  269. outw(EEPROM_CMD_RD | offset, ioaddr + EP_W0_EEPROM_COMMAND);
  270. if (!eeprom_rdy(ioaddr))
  271. return (0xffff);
  272. return (inw(ioaddr + EP_W0_EEPROM_DATA));
  273. }
  274. static struct nic_operations t509_operations = {
  275. .connect = dummy_connect,
  276. .poll = t509_poll,
  277. .transmit = t509_transmit,
  278. .irq = t509_irq,
  279. };
  280. /**************************************************************************
  281. ETH_PROBE - Look for an adapter
  282. ***************************************************************************/
  283. int t5x9_probe ( struct nic *nic,
  284. uint16_t prod_id_check, uint16_t prod_id_mask ) {
  285. uint16_t prod_id;
  286. int i,j;
  287. unsigned short *p;
  288. /* Check product ID */
  289. prod_id = get_e ( nic->ioaddr, EEPROM_PROD_ID );
  290. if ( ( prod_id & prod_id_mask ) != prod_id_check ) {
  291. printf ( "EEPROM Product ID is incorrect (%hx & %hx != %hx)\n",
  292. prod_id, prod_id_mask, prod_id_check );
  293. return 0;
  294. }
  295. /* test for presence of connectors */
  296. GO_WINDOW(nic->ioaddr,0);
  297. i = inw(nic->ioaddr + EP_W0_CONFIG_CTRL);
  298. j = (inw(nic->ioaddr + EP_W0_ADDRESS_CFG) >> 14) & 0x3;
  299. switch(j) {
  300. case 0:
  301. if (i & IS_UTP) {
  302. printf("10baseT\n");
  303. connector = utp;
  304. } else {
  305. printf("10baseT not present\n");
  306. return 0;
  307. }
  308. break;
  309. case 1:
  310. if (i & IS_AUI) {
  311. printf("10base5\n");
  312. } else {
  313. printf("10base5 not present\n");
  314. return 0;
  315. }
  316. break;
  317. case 3:
  318. if (i & IS_BNC) {
  319. printf("10base2\n");
  320. connector = bnc;
  321. } else {
  322. printf("10base2 not present\n");
  323. return 0;
  324. }
  325. break;
  326. default:
  327. printf("unknown connector\n");
  328. return 0;
  329. }
  330. /*
  331. * Read the station address from the eeprom
  332. */
  333. p = (unsigned short *) nic->node_addr;
  334. for (i = 0; i < ETH_ALEN / 2; i++) {
  335. p[i] = htons(get_e(nic->ioaddr,i));
  336. GO_WINDOW(nic->ioaddr,2);
  337. outw(ntohs(p[i]), nic->ioaddr + EP_W2_ADDR_0 + (i * 2));
  338. }
  339. DBG ( "Ethernet Address: %s\n", eth_ntoa ( nic->node_addr ) );
  340. t509_reset(nic);
  341. nic->nic_op = &t509_operations;
  342. return 1;
  343. }
  344. /*
  345. * Local variables:
  346. * c-basic-offset: 8
  347. * End:
  348. */