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

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