epic100.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /* epic100.c: A SMC 83c170 EPIC/100 fast ethernet driver for Etherboot */
  2. #define LINUX_OUT_MACROS
  3. #include "etherboot.h"
  4. #include "nic.h"
  5. #include "cards.h"
  6. #include "timer.h"
  7. #include "epic100.h"
  8. #undef virt_to_bus
  9. #define virt_to_bus(x) ((unsigned long)x)
  10. #define TX_RING_SIZE 2 /* use at least 2 buffers for TX */
  11. #define RX_RING_SIZE 2
  12. #define PKT_BUF_SZ 1536 /* Size of each temporary Tx/Rx buffer.*/
  13. /*
  14. #define DEBUG_RX
  15. #define DEBUG_TX
  16. #define DEBUG_EEPROM
  17. */
  18. #define EPIC_DEBUG 0 /* debug level */
  19. /* The EPIC100 Rx and Tx buffer descriptors. */
  20. struct epic_rx_desc {
  21. unsigned short status;
  22. unsigned short rxlength;
  23. unsigned long bufaddr;
  24. unsigned short buflength;
  25. unsigned short control;
  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 short status;
  32. unsigned short txlength;
  33. unsigned long bufaddr;
  34. unsigned short buflength;
  35. unsigned short control;
  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);
  44. static void epic100_transmit(struct nic *nic, const char *destaddr,
  45. unsigned int type, unsigned int len, const char *data);
  46. static int read_eeprom(int location);
  47. static int mii_read(int phy_id, int location);
  48. static int ioaddr;
  49. static int command;
  50. static int intstat;
  51. static int intmask;
  52. static int genctl ;
  53. static int eectl ;
  54. static int test ;
  55. static int mmctl ;
  56. static int mmdata ;
  57. static int lan0 ;
  58. static int rxcon ;
  59. static int txcon ;
  60. static int prcdar ;
  61. static int ptcdar ;
  62. static int eththr ;
  63. static unsigned int cur_rx, cur_tx; /* The next free ring entry */
  64. #ifdef DEBUG_EEPROM
  65. static unsigned short eeprom[64];
  66. #endif
  67. static signed char phys[4]; /* MII device addresses. */
  68. static struct epic_rx_desc rx_ring[RX_RING_SIZE];
  69. static struct epic_tx_desc tx_ring[TX_RING_SIZE];
  70. #ifdef USE_LOWMEM_BUFFER
  71. #define rx_packet ((char *)0x10000 - PKT_BUF_SZ * RX_RING_SIZE)
  72. #define tx_packet ((char *)0x10000 - PKT_BUF_SZ * RX_RING_SIZE - PKT_BUF_SZ * TX_RING_SIZE)
  73. #else
  74. static char rx_packet[PKT_BUF_SZ * RX_RING_SIZE];
  75. static char tx_packet[PKT_BUF_SZ * TX_RING_SIZE];
  76. #endif
  77. /***********************************************************************/
  78. /* Externally visible functions */
  79. /***********************************************************************/
  80. static void
  81. epic100_reset(struct nic *nic)
  82. {
  83. /* Soft reset the chip. */
  84. outl(GC_SOFT_RESET, genctl);
  85. }
  86. struct nic*
  87. epic100_probe(struct nic *nic, unsigned short *probeaddrs)
  88. {
  89. unsigned short sum = 0;
  90. unsigned short value;
  91. int i;
  92. unsigned short* ap;
  93. unsigned int phy, phy_idx;
  94. if (probeaddrs == 0 || probeaddrs[0] == 0)
  95. return 0;
  96. /* Ideally we would detect all network cards in slot order. That would
  97. be best done a central PCI probe dispatch, which wouldn't work
  98. well with the current structure. So instead we detect just the
  99. Epic cards in slot order. */
  100. ioaddr = probeaddrs[0] & ~3; /* Mask the bit that says "this is an io addr" */
  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. 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. for (i = 0; i < 64; i++) {
  137. value = read_eeprom(i);
  138. eeprom[i] = value;
  139. sum += value;
  140. }
  141. #if (EPIC_DEBUG > 1)
  142. printf("EEPROM contents\n");
  143. for (i = 0; i < 64; i++) {
  144. printf(" %hhX%s", eeprom[i], i % 16 == 15 ? "\n" : "");
  145. }
  146. #endif
  147. #endif
  148. /* This could also be read from the EEPROM. */
  149. ap = (unsigned short*)nic->node_addr;
  150. for (i = 0; i < 3; i++)
  151. *ap++ = inw(lan0 + i*4);
  152. printf(" I/O %#hX %! ", ioaddr, nic->node_addr);
  153. /* Find the connected MII xcvrs. */
  154. for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(phys); phy++) {
  155. int mii_status = mii_read(phy, 0);
  156. if (mii_status != 0xffff && mii_status != 0x0000) {
  157. phys[phy_idx++] = phy;
  158. #if (EPIC_DEBUG > 1)
  159. printf("MII transceiver found at address %d.\n", phy);
  160. #endif
  161. }
  162. }
  163. if (phy_idx == 0) {
  164. #if (EPIC_DEBUG > 1)
  165. printf("***WARNING***: No MII transceiver found!\n");
  166. #endif
  167. /* Use the known PHY address of the EPII. */
  168. phys[0] = 3;
  169. }
  170. epic100_open();
  171. nic->reset = epic100_reset;
  172. nic->poll = epic100_poll;
  173. nic->transmit = epic100_transmit;
  174. nic->disable = epic100_disable;
  175. return nic;
  176. }
  177. static void
  178. epic100_open(void)
  179. {
  180. int mii_reg5;
  181. int full_duplex = 0;
  182. unsigned long tmp;
  183. epic100_init_ring();
  184. /* Pull the chip out of low-power mode, and set for PCI read multiple. */
  185. outl(GC_RX_FIFO_THR_64 | GC_MRC_READ_MULT | GC_ONE_COPY, genctl);
  186. outl(TX_FIFO_THRESH, eththr);
  187. tmp = TC_EARLY_TX_ENABLE | TX_SLOT_TIME;
  188. mii_reg5 = mii_read(phys[0], 5);
  189. if (mii_reg5 != 0xffff && (mii_reg5 & 0x0100)) {
  190. full_duplex = 1;
  191. printf(" full-duplex mode");
  192. tmp |= TC_LM_FULL_DPX;
  193. } else
  194. tmp |= TC_LM_NORMAL;
  195. outl(tmp, txcon);
  196. /* Give adress of RX and TX ring to the chip */
  197. outl(virt_to_bus(&rx_ring), prcdar);
  198. outl(virt_to_bus(&tx_ring), ptcdar);
  199. /* Start the chip's Rx process: receive unicast and broadcast */
  200. outl(0x04, rxcon);
  201. outl(CR_START_RX | CR_QUEUE_RX, command);
  202. putchar('\n');
  203. }
  204. /* Initialize the Rx and Tx rings. */
  205. static void
  206. epic100_init_ring(void)
  207. {
  208. int i;
  209. char* p;
  210. cur_rx = cur_tx = 0;
  211. p = &rx_packet[0];
  212. for (i = 0; i < RX_RING_SIZE; i++) {
  213. rx_ring[i].status = RRING_OWN; /* Owned by Epic chip */
  214. rx_ring[i].buflength = PKT_BUF_SZ;
  215. rx_ring[i].bufaddr = virt_to_bus(p + (PKT_BUF_SZ * i));
  216. rx_ring[i].control = 0;
  217. rx_ring[i].next = virt_to_bus(&(rx_ring[i + 1]) );
  218. }
  219. /* Mark the last entry as wrapping the ring. */
  220. rx_ring[i-1].next = virt_to_bus(&rx_ring[0]);
  221. /*
  222. *The Tx buffer descriptor is filled in as needed,
  223. * but we do need to clear the ownership bit.
  224. */
  225. p = &tx_packet[0];
  226. for (i = 0; i < TX_RING_SIZE; i++) {
  227. tx_ring[i].status = 0; /* Owned by CPU */
  228. tx_ring[i].bufaddr = virt_to_bus(p + (PKT_BUF_SZ * i));
  229. tx_ring[i].control = TD_STDFLAGS;
  230. tx_ring[i].next = virt_to_bus(&(tx_ring[i + 1]) );
  231. }
  232. tx_ring[i-1].next = virt_to_bus(&tx_ring[0]);
  233. }
  234. /* function: epic100_transmit
  235. * This transmits a packet.
  236. *
  237. * Arguments: char d[6]: destination ethernet address.
  238. * unsigned short t: ethernet protocol type.
  239. * unsigned short s: size of the data-part of the packet.
  240. * char *p: the data for the packet.
  241. * returns: void.
  242. */
  243. static void
  244. epic100_transmit(struct nic *nic, const char *destaddr, unsigned int type,
  245. unsigned int len, const char *data)
  246. {
  247. unsigned short nstype;
  248. char* txp;
  249. int entry;
  250. /* Calculate the next Tx descriptor entry. */
  251. entry = cur_tx % TX_RING_SIZE;
  252. if ((tx_ring[entry].status & TRING_OWN) == TRING_OWN) {
  253. printf("eth_transmit: Unable to transmit. status=%hX. Resetting...\n",
  254. tx_ring[entry].status);
  255. epic100_open();
  256. return;
  257. }
  258. txp = (char*)tx_ring[entry].bufaddr;
  259. memcpy(txp, destaddr, ETH_ALEN);
  260. memcpy(txp + ETH_ALEN, nic->node_addr, ETH_ALEN);
  261. nstype = htons(type);
  262. memcpy(txp + 12, (char*)&nstype, 2);
  263. memcpy(txp + ETH_HLEN, data, len);
  264. len += ETH_HLEN;
  265. /*
  266. * Caution: the write order is important here,
  267. * set the base address with the "ownership"
  268. * bits last.
  269. */
  270. tx_ring[entry].txlength = (len >= 60 ? len : 60);
  271. tx_ring[entry].buflength = len;
  272. tx_ring[entry].status = TRING_OWN; /* Pass ownership to the chip. */
  273. cur_tx++;
  274. /* Trigger an immediate transmit demand. */
  275. outl(CR_QUEUE_TX, command);
  276. load_timer2(10*TICKS_PER_MS); /* timeout 10 ms for transmit */
  277. while ((tx_ring[entry].status & TRING_OWN) && timer2_running())
  278. /* Wait */;
  279. if ((tx_ring[entry].status & TRING_OWN) != 0)
  280. printf("Oops, transmitter timeout, status=%hX\n",
  281. tx_ring[entry].status);
  282. }
  283. /* function: epic100_poll / eth_poll
  284. * This receives a packet from the network.
  285. *
  286. * Arguments: none
  287. *
  288. * returns: 1 if a packet was received.
  289. * 0 if no pacet was received.
  290. * side effects:
  291. * returns the packet in the array nic->packet.
  292. * returns the length of the packet in nic->packetlen.
  293. */
  294. static int
  295. epic100_poll(struct nic *nic)
  296. {
  297. int entry;
  298. int status;
  299. int retcode;
  300. entry = cur_rx % RX_RING_SIZE;
  301. if ((status = rx_ring[entry].status & RRING_OWN) == RRING_OWN)
  302. return (0);
  303. /* We own the next entry, it's a new packet. Send it up. */
  304. #if (EPIC_DEBUG > 4)
  305. printf("epic_poll: entry %d status %hX\n", entry, status);
  306. #endif
  307. cur_rx++;
  308. if (status & 0x2000) {
  309. printf("epic_poll: Giant packet\n");
  310. retcode = 0;
  311. } else if (status & 0x0006) {
  312. /* Rx Frame errors are counted in hardware. */
  313. printf("epic_poll: Frame received with errors\n");
  314. retcode = 0;
  315. } else {
  316. /* Omit the four octet CRC from the length. */
  317. nic->packetlen = rx_ring[entry].rxlength - 4;
  318. memcpy(nic->packet, (char*)rx_ring[entry].bufaddr, nic->packetlen);
  319. retcode = 1;
  320. }
  321. /* Clear all error sources. */
  322. outl(status & INTR_CLEARERRS, intstat);
  323. /* Give the descriptor back to the chip */
  324. rx_ring[entry].status = RRING_OWN;
  325. /* Restart Receiver */
  326. outl(CR_START_RX | CR_QUEUE_RX, command);
  327. return retcode;
  328. }
  329. static void
  330. epic100_disable(struct nic *nic)
  331. {
  332. }
  333. #ifdef DEBUG_EEPROM
  334. /* Serial EEPROM section. */
  335. /* EEPROM_Ctrl bits. */
  336. #define EE_SHIFT_CLK 0x04 /* EEPROM shift clock. */
  337. #define EE_CS 0x02 /* EEPROM chip select. */
  338. #define EE_DATA_WRITE 0x08 /* EEPROM chip data in. */
  339. #define EE_WRITE_0 0x01
  340. #define EE_WRITE_1 0x09
  341. #define EE_DATA_READ 0x10 /* EEPROM chip data out. */
  342. #define EE_ENB (0x0001 | EE_CS)
  343. /* The EEPROM commands include the alway-set leading bit. */
  344. #define EE_WRITE_CMD (5 << 6)
  345. #define EE_READ_CMD (6 << 6)
  346. #define EE_ERASE_CMD (7 << 6)
  347. #define eeprom_delay(n) delay(n)
  348. static int
  349. read_eeprom(int location)
  350. {
  351. int i;
  352. int retval = 0;
  353. int read_cmd = location | EE_READ_CMD;
  354. outl(EE_ENB & ~EE_CS, eectl);
  355. outl(EE_ENB, eectl);
  356. /* Shift the read command bits out. */
  357. for (i = 10; i >= 0; i--) {
  358. short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
  359. outl(EE_ENB | dataval, eectl);
  360. eeprom_delay(100);
  361. outl(EE_ENB | dataval | EE_SHIFT_CLK, eectl);
  362. eeprom_delay(150);
  363. outl(EE_ENB | dataval, eectl); /* Finish EEPROM a clock tick. */
  364. eeprom_delay(250);
  365. }
  366. outl(EE_ENB, eectl);
  367. for (i = 16; i > 0; i--) {
  368. outl(EE_ENB | EE_SHIFT_CLK, eectl);
  369. eeprom_delay(100);
  370. retval = (retval << 1) | ((inl(eectl) & EE_DATA_READ) ? 1 : 0);
  371. outl(EE_ENB, eectl);
  372. eeprom_delay(100);
  373. }
  374. /* Terminate the EEPROM access. */
  375. outl(EE_ENB & ~EE_CS, eectl);
  376. return retval;
  377. }
  378. #endif
  379. #define MII_READOP 1
  380. #define MII_WRITEOP 2
  381. static int
  382. mii_read(int phy_id, int location)
  383. {
  384. int i;
  385. outl((phy_id << 9) | (location << 4) | MII_READOP, mmctl);
  386. /* Typical operation takes < 50 ticks. */
  387. for (i = 4000; i > 0; i--)
  388. if ((inl(mmctl) & MII_READOP) == 0)
  389. break;
  390. return inw(mmdata);
  391. }