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

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