Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

epic100.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. /* epic100.c: A SMC 83c170 EPIC/100 fast ethernet driver for Etherboot */
  2. /* 05/06/2003 timlegge Fixed relocation and implemented Multicast */
  3. #define LINUX_OUT_MACROS
  4. #include "etherboot.h"
  5. #include <gpxe/pci.h>
  6. #include <gpxe/ethernet.h>
  7. #include "nic.h"
  8. #include "timer.h"
  9. #include "console.h"
  10. #include "epic100.h"
  11. /* Condensed operations for readability */
  12. #define virt_to_le32desc(addr) cpu_to_le32(virt_to_bus(addr))
  13. #define le32desc_to_virt(addr) bus_to_virt(le32_to_cpu(addr))
  14. #define TX_RING_SIZE 2 /* use at least 2 buffers for TX */
  15. #define RX_RING_SIZE 2
  16. #define PKT_BUF_SZ 1536 /* Size of each temporary Tx/Rx buffer.*/
  17. /*
  18. #define DEBUG_RX
  19. #define DEBUG_TX
  20. #define DEBUG_EEPROM
  21. */
  22. #define EPIC_DEBUG 0 /* debug level */
  23. /* The EPIC100 Rx and Tx buffer descriptors. */
  24. struct epic_rx_desc {
  25. unsigned long status;
  26. unsigned long bufaddr;
  27. unsigned long buflength;
  28. unsigned long next;
  29. };
  30. /* description of the tx descriptors control bits commonly used */
  31. #define TD_STDFLAGS TD_LASTDESC
  32. struct epic_tx_desc {
  33. unsigned long status;
  34. unsigned long bufaddr;
  35. unsigned long buflength;
  36. unsigned long next;
  37. };
  38. #define delay(nanosec) do { int _i = 3; while (--_i > 0) \
  39. { __SLOW_DOWN_IO; }} while (0)
  40. static void epic100_open(void);
  41. static void epic100_init_ring(void);
  42. static void epic100_disable(struct nic *nic);
  43. static int epic100_poll(struct nic *nic, int retrieve);
  44. static void epic100_transmit(struct nic *nic, const char *destaddr,
  45. unsigned int type, unsigned int len, const char *data);
  46. #ifdef DEBUG_EEPROM
  47. static int read_eeprom(int location);
  48. #endif
  49. static int mii_read(int phy_id, int location);
  50. static void epic100_irq(struct nic *nic, irq_action_t action);
  51. static struct nic_operations epic100_operations;
  52. static int ioaddr;
  53. static int command;
  54. static int intstat;
  55. static int intmask;
  56. static int genctl ;
  57. static int eectl ;
  58. static int test ;
  59. static int mmctl ;
  60. static int mmdata ;
  61. static int lan0 ;
  62. static int mc0 ;
  63. static int rxcon ;
  64. static int txcon ;
  65. static int prcdar ;
  66. static int ptcdar ;
  67. static int eththr ;
  68. static unsigned int cur_rx, cur_tx; /* The next free ring entry */
  69. #ifdef DEBUG_EEPROM
  70. static unsigned short eeprom[64];
  71. #endif
  72. static signed char phys[4]; /* MII device addresses. */
  73. struct {
  74. struct epic_rx_desc rx_ring[RX_RING_SIZE]
  75. __attribute__ ((aligned(4)));
  76. struct epic_tx_desc tx_ring[TX_RING_SIZE]
  77. __attribute__ ((aligned(4)));
  78. unsigned char rx_packet[PKT_BUF_SZ * RX_RING_SIZE];
  79. unsigned char tx_packet[PKT_BUF_SZ * TX_RING_SIZE];
  80. } epic100_bufs __shared;
  81. #define rx_ring epic100_bufs.rx_ring
  82. #define tx_ring epic100_bufs.tx_ring
  83. #define rx_packet epic100_bufs.rx_packet
  84. #define tx_packet epic100_bufs.tx_packet
  85. /***********************************************************************/
  86. /* Externally visible functions */
  87. /***********************************************************************/
  88. static int
  89. epic100_probe ( struct nic *nic, struct pci_device *pci ) {
  90. int i;
  91. unsigned short* ap;
  92. unsigned int phy, phy_idx;
  93. if (pci->ioaddr == 0)
  94. return 0;
  95. /* Ideally we would detect all network cards in slot order. That would
  96. be best done a central PCI probe dispatch, which wouldn't work
  97. well with the current structure. So instead we detect just the
  98. Epic cards in slot order. */
  99. ioaddr = pci->ioaddr;
  100. nic->irqno = 0;
  101. pci_fill_nic ( nic, pci );
  102. nic->ioaddr = pci->ioaddr & ~3;
  103. /* compute all used static epic100 registers address */
  104. command = ioaddr + COMMAND; /* Control Register */
  105. intstat = ioaddr + INTSTAT; /* Interrupt Status */
  106. intmask = ioaddr + INTMASK; /* Interrupt Mask */
  107. genctl = ioaddr + GENCTL; /* General Control */
  108. eectl = ioaddr + EECTL; /* EEPROM Control */
  109. test = ioaddr + TEST; /* Test register (clocks) */
  110. mmctl = ioaddr + MMCTL; /* MII Management Interface Control */
  111. mmdata = ioaddr + MMDATA; /* MII Management Interface Data */
  112. lan0 = ioaddr + LAN0; /* MAC address. (0x40-0x48) */
  113. mc0 = ioaddr + MC0; /* Multicast Control */
  114. rxcon = ioaddr + RXCON; /* Receive Control */
  115. txcon = ioaddr + TXCON; /* Transmit Control */
  116. prcdar = ioaddr + PRCDAR; /* PCI Receive Current Descr Address */
  117. ptcdar = ioaddr + PTCDAR; /* PCI Transmit Current Descr Address */
  118. eththr = ioaddr + ETHTHR; /* Early Transmit Threshold */
  119. /* Reset the chip & bring it out of low-power mode. */
  120. outl(GC_SOFT_RESET, genctl);
  121. /* Disable ALL interrupts by setting the interrupt mask. */
  122. outl(INTR_DISABLE, intmask);
  123. /*
  124. * set the internal clocks:
  125. * Application Note 7.15 says:
  126. * In order to set the CLOCK TEST bit in the TEST register,
  127. * perform the following:
  128. *
  129. * Write 0x0008 to the test register at least sixteen
  130. * consecutive times.
  131. *
  132. * The CLOCK TEST bit is Write-Only. Writing it several times
  133. * consecutively insures a successful write to the bit...
  134. */
  135. for (i = 0; i < 16; i++) {
  136. outl(0x00000008, test);
  137. }
  138. #ifdef DEBUG_EEPROM
  139. {
  140. unsigned short sum = 0;
  141. unsigned short value;
  142. for (i = 0; i < 64; i++) {
  143. value = read_eeprom(i);
  144. eeprom[i] = value;
  145. sum += value;
  146. }
  147. }
  148. #if (EPIC_DEBUG > 1)
  149. printf("EEPROM contents\n");
  150. for (i = 0; i < 64; i++) {
  151. printf(" %hhX%s", eeprom[i], i % 16 == 15 ? "\n" : "");
  152. }
  153. #endif
  154. #endif
  155. /* This could also be read from the EEPROM. */
  156. ap = (unsigned short*)nic->node_addr;
  157. for (i = 0; i < 3; i++)
  158. *ap++ = inw(lan0 + i*4);
  159. DBG ( " I/O %4.4x %s ", ioaddr, eth_ntoa ( nic->node_addr ) );
  160. /* Find the connected MII xcvrs. */
  161. for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(phys); phy++) {
  162. int mii_status = mii_read(phy, 0);
  163. if (mii_status != 0xffff && mii_status != 0x0000) {
  164. phys[phy_idx++] = phy;
  165. #if (EPIC_DEBUG > 1)
  166. printf("MII transceiver found at address %d.\n", phy);
  167. #endif
  168. }
  169. }
  170. if (phy_idx == 0) {
  171. #if (EPIC_DEBUG > 1)
  172. printf("***WARNING***: No MII transceiver found!\n");
  173. #endif
  174. /* Use the known PHY address of the EPII. */
  175. phys[0] = 3;
  176. }
  177. epic100_open();
  178. nic->nic_op = &epic100_operations;
  179. return 1;
  180. }
  181. static void set_rx_mode(void)
  182. {
  183. unsigned char mc_filter[8];
  184. int i;
  185. memset(mc_filter, 0xff, sizeof(mc_filter));
  186. outl(0x0C, rxcon);
  187. for(i = 0; i < 4; i++)
  188. outw(((unsigned short *)mc_filter)[i], mc0 + i*4);
  189. return;
  190. }
  191. static void
  192. epic100_open(void)
  193. {
  194. int mii_reg5;
  195. int full_duplex = 0;
  196. unsigned long tmp;
  197. epic100_init_ring();
  198. /* Pull the chip out of low-power mode, and set for PCI read multiple. */
  199. outl(GC_RX_FIFO_THR_64 | GC_MRC_READ_MULT | GC_ONE_COPY, genctl);
  200. outl(TX_FIFO_THRESH, eththr);
  201. tmp = TC_EARLY_TX_ENABLE | TX_SLOT_TIME;
  202. mii_reg5 = mii_read(phys[0], 5);
  203. if (mii_reg5 != 0xffff && (mii_reg5 & 0x0100)) {
  204. full_duplex = 1;
  205. printf(" full-duplex mode");
  206. tmp |= TC_LM_FULL_DPX;
  207. } else
  208. tmp |= TC_LM_NORMAL;
  209. outl(tmp, txcon);
  210. /* Give adress of RX and TX ring to the chip */
  211. outl(virt_to_le32desc(&rx_ring), prcdar);
  212. outl(virt_to_le32desc(&tx_ring), ptcdar);
  213. /* Start the chip's Rx process: receive unicast and broadcast */
  214. set_rx_mode();
  215. outl(CR_START_RX | CR_QUEUE_RX, command);
  216. putchar('\n');
  217. }
  218. /* Initialize the Rx and Tx rings. */
  219. static void
  220. epic100_init_ring(void)
  221. {
  222. int i;
  223. cur_rx = cur_tx = 0;
  224. for (i = 0; i < RX_RING_SIZE; i++) {
  225. rx_ring[i].status = cpu_to_le32(RRING_OWN); /* Owned by Epic chip */
  226. rx_ring[i].buflength = cpu_to_le32(PKT_BUF_SZ);
  227. rx_ring[i].bufaddr = virt_to_bus(&rx_packet[i * PKT_BUF_SZ]);
  228. rx_ring[i].next = virt_to_le32desc(&rx_ring[i + 1]) ;
  229. }
  230. /* Mark the last entry as wrapping the ring. */
  231. rx_ring[i-1].next = virt_to_le32desc(&rx_ring[0]);
  232. /*
  233. *The Tx buffer descriptor is filled in as needed,
  234. * but we do need to clear the ownership bit.
  235. */
  236. for (i = 0; i < TX_RING_SIZE; i++) {
  237. tx_ring[i].status = 0x0000; /* Owned by CPU */
  238. tx_ring[i].buflength = 0x0000 | cpu_to_le32(TD_STDFLAGS << 16);
  239. tx_ring[i].bufaddr = virt_to_bus(&tx_packet[i * PKT_BUF_SZ]);
  240. tx_ring[i].next = virt_to_le32desc(&tx_ring[i + 1]);
  241. }
  242. tx_ring[i-1].next = virt_to_le32desc(&tx_ring[0]);
  243. }
  244. /* function: epic100_transmit
  245. * This transmits a packet.
  246. *
  247. * Arguments: char d[6]: destination ethernet address.
  248. * unsigned short t: ethernet protocol type.
  249. * unsigned short s: size of the data-part of the packet.
  250. * char *p: the data for the packet.
  251. * returns: void.
  252. */
  253. static void
  254. epic100_transmit(struct nic *nic, const char *destaddr, unsigned int type,
  255. unsigned int len, const char *data)
  256. {
  257. unsigned short nstype;
  258. unsigned char *txp;
  259. int entry;
  260. /* Calculate the next Tx descriptor entry. */
  261. entry = cur_tx % TX_RING_SIZE;
  262. if ((tx_ring[entry].status & TRING_OWN) == TRING_OWN) {
  263. printf("eth_transmit: Unable to transmit. status=%4.4lx. Resetting...\n",
  264. tx_ring[entry].status);
  265. epic100_open();
  266. return;
  267. }
  268. txp = tx_packet + (entry * PKT_BUF_SZ);
  269. memcpy(txp, destaddr, ETH_ALEN);
  270. memcpy(txp + ETH_ALEN, nic->node_addr, ETH_ALEN);
  271. nstype = htons(type);
  272. memcpy(txp + 12, (char*)&nstype, 2);
  273. memcpy(txp + ETH_HLEN, data, len);
  274. len += ETH_HLEN;
  275. len &= 0x0FFF;
  276. while(len < ETH_ZLEN)
  277. txp[len++] = '\0';
  278. /*
  279. * Caution: the write order is important here,
  280. * set the base address with the "ownership"
  281. * bits last.
  282. */
  283. tx_ring[entry].buflength |= cpu_to_le32(len);
  284. tx_ring[entry].status = cpu_to_le32(len << 16) |
  285. cpu_to_le32(TRING_OWN); /* Pass ownership to the chip. */
  286. cur_tx++;
  287. /* Trigger an immediate transmit demand. */
  288. outl(CR_QUEUE_TX, command);
  289. load_timer2(10*TICKS_PER_MS); /* timeout 10 ms for transmit */
  290. while ((le32_to_cpu(tx_ring[entry].status) & (TRING_OWN)) && timer2_running())
  291. /* Wait */;
  292. if ((le32_to_cpu(tx_ring[entry].status) & TRING_OWN) != 0)
  293. printf("Oops, transmitter timeout, status=%4.4lX\n",
  294. tx_ring[entry].status);
  295. }
  296. /* function: epic100_poll / eth_poll
  297. * This receives a packet from the network.
  298. *
  299. * Arguments: none
  300. *
  301. * returns: 1 if a packet was received.
  302. * 0 if no pacet was received.
  303. * side effects:
  304. * returns the packet in the array nic->packet.
  305. * returns the length of the packet in nic->packetlen.
  306. */
  307. static int
  308. epic100_poll(struct nic *nic, int retrieve)
  309. {
  310. int entry;
  311. int retcode;
  312. int status;
  313. entry = cur_rx % RX_RING_SIZE;
  314. if ((rx_ring[entry].status & cpu_to_le32(RRING_OWN)) == RRING_OWN)
  315. return (0);
  316. if ( ! retrieve ) return 1;
  317. status = le32_to_cpu(rx_ring[entry].status);
  318. /* We own the next entry, it's a new packet. Send it up. */
  319. #if (EPIC_DEBUG > 4)
  320. printf("epic_poll: entry %d status %hX\n", entry, status);
  321. #endif
  322. cur_rx++;
  323. if (status & 0x2000) {
  324. printf("epic_poll: Giant packet\n");
  325. retcode = 0;
  326. } else if (status & 0x0006) {
  327. /* Rx Frame errors are counted in hardware. */
  328. printf("epic_poll: Frame received with errors\n");
  329. retcode = 0;
  330. } else {
  331. /* Omit the four octet CRC from the length. */
  332. nic->packetlen = le32_to_cpu((rx_ring[entry].buflength))- 4;
  333. memcpy(nic->packet, &rx_packet[entry * PKT_BUF_SZ], nic->packetlen);
  334. retcode = 1;
  335. }
  336. /* Clear all error sources. */
  337. outl(status & INTR_CLEARERRS, intstat);
  338. /* Give the descriptor back to the chip */
  339. rx_ring[entry].status = RRING_OWN;
  340. /* Restart Receiver */
  341. outl(CR_START_RX | CR_QUEUE_RX, command);
  342. return retcode;
  343. }
  344. static void epic100_disable ( struct nic *nic __unused ) {
  345. /* Soft reset the chip. */
  346. outl(GC_SOFT_RESET, genctl);
  347. }
  348. static void epic100_irq(struct nic *nic __unused, irq_action_t action __unused)
  349. {
  350. switch ( action ) {
  351. case DISABLE :
  352. break;
  353. case ENABLE :
  354. break;
  355. case FORCE :
  356. break;
  357. }
  358. }
  359. #ifdef DEBUG_EEPROM
  360. /* Serial EEPROM section. */
  361. /* EEPROM_Ctrl bits. */
  362. #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
  363. #define EE_CS 0x02 /* EEPROM chip select. */
  364. #define EE_DATA_WRITE 0x08 /* EEPROM chip data in. */
  365. #define EE_WRITE_0 0x01
  366. #define EE_WRITE_1 0x09
  367. #define EE_DATA_READ 0x10 /* EEPROM chip data out. */
  368. #define EE_ENB (0x0001 | EE_CS)
  369. /* The EEPROM commands include the alway-set leading bit. */
  370. #define EE_WRITE_CMD (5 << 6)
  371. #define EE_READ_CMD (6 << 6)
  372. #define EE_ERASE_CMD (7 << 6)
  373. #define eeprom_delay(n) delay(n)
  374. static int
  375. read_eeprom(int location)
  376. {
  377. int i;
  378. int retval = 0;
  379. int read_cmd = location | EE_READ_CMD;
  380. outl(EE_ENB & ~EE_CS, eectl);
  381. outl(EE_ENB, eectl);
  382. /* Shift the read command bits out. */
  383. for (i = 10; i >= 0; i--) {
  384. short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  385. outl(EE_ENB | dataval, eectl);
  386. eeprom_delay(100);
  387. outl(EE_ENB | dataval | EE_SHIFT_CLK, eectl);
  388. eeprom_delay(150);
  389. outl(EE_ENB | dataval, eectl); /* Finish EEPROM a clock tick. */
  390. eeprom_delay(250);
  391. }
  392. outl(EE_ENB, eectl);
  393. for (i = 16; i > 0; i--) {
  394. outl(EE_ENB | EE_SHIFT_CLK, eectl);
  395. eeprom_delay(100);
  396. retval = (retval << 1) | ((inl(eectl) & EE_DATA_READ) ? 1 : 0);
  397. outl(EE_ENB, eectl);
  398. eeprom_delay(100);
  399. }
  400. /* Terminate the EEPROM access. */
  401. outl(EE_ENB & ~EE_CS, eectl);
  402. return retval;
  403. }
  404. #endif
  405. #define MII_READOP 1
  406. #define MII_WRITEOP 2
  407. static int
  408. mii_read(int phy_id, int location)
  409. {
  410. int i;
  411. outl((phy_id << 9) | (location << 4) | MII_READOP, mmctl);
  412. /* Typical operation takes < 50 ticks. */
  413. for (i = 4000; i > 0; i--)
  414. if ((inl(mmctl) & MII_READOP) == 0)
  415. break;
  416. return inw(mmdata);
  417. }
  418. static struct nic_operations epic100_operations = {
  419. .connect = dummy_connect,
  420. .poll = epic100_poll,
  421. .transmit = epic100_transmit,
  422. .irq = epic100_irq,
  423. };
  424. static struct pci_device_id epic100_nics[] = {
  425. PCI_ROM(0x10b8, 0x0005, "epic100", "SMC EtherPowerII"), /* SMC 83c170 EPIC/100 */
  426. PCI_ROM(0x10b8, 0x0006, "smc-83c175", "SMC EPIC/C 83c175"),
  427. };
  428. PCI_DRIVER ( epic100_driver, epic100_nics, PCI_NO_CLASS );
  429. DRIVER ( "EPIC100", nic_driver, pci_driver, epic100_driver,
  430. epic100_probe, epic100_disable );