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.

epic100.c 14KB

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