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.

epic100.c 14KB

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