smc9000.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*------------------------------------------------------------------------
  2. * smc9000.c
  3. * This is a Etherboot driver for SMC's 9000 series of Ethernet cards.
  4. *
  5. * Copyright (C) 1998 Daniel Engström <daniel.engstrom@riksnett.no>
  6. * Based on the Linux SMC9000 driver, smc9194.c by Eric Stahlman
  7. * Copyright (C) 1996 by Erik Stahlman <eric@vt.edu>
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU Public License, incorporated herein by reference.
  11. *
  12. * "Features" of the SMC chip:
  13. * 4608 byte packet memory. ( for the 91C92/4. Others have more )
  14. * EEPROM for configuration
  15. * AUI/TP selection
  16. *
  17. * Authors
  18. * Erik Stahlman <erik@vt.edu>
  19. * Daniel Engström <daniel.engstrom@riksnett.no>
  20. *
  21. * History
  22. * 98-09-25 Daniel Engström Etherboot driver crated from Eric's
  23. * Linux driver.
  24. *
  25. *---------------------------------------------------------------------------*/
  26. #define LINUX_OUT_MACROS 1
  27. #define SMC9000_VERBOSE 1
  28. #define SMC9000_DEBUG 0
  29. #include "etherboot.h"
  30. #include "nic.h"
  31. #include "cards.h"
  32. #include "smc9000.h"
  33. # define _outb outb
  34. # define _outw outw
  35. static const char smc9000_version[] = "Version 0.99 98-09-30";
  36. static unsigned int smc9000_base=0;
  37. static const char *interfaces[ 2 ] = { "TP", "AUI" };
  38. static const char *chip_ids[ 15 ] = {
  39. NULL, NULL, NULL,
  40. /* 3 */ "SMC91C90/91C92",
  41. /* 4 */ "SMC91C94",
  42. /* 5 */ "SMC91C95",
  43. NULL,
  44. /* 7 */ "SMC91C100",
  45. /* 8 */ "SMC91C100FD",
  46. NULL, NULL, NULL,
  47. NULL, NULL, NULL
  48. };
  49. static const char smc91c96_id[] = "SMC91C96";
  50. /*
  51. * Function: smc_reset( int ioaddr )
  52. * Purpose:
  53. * This sets the SMC91xx chip to its normal state, hopefully from whatever
  54. * mess that any other DOS driver has put it in.
  55. *
  56. * Maybe I should reset more registers to defaults in here? SOFTRESET should
  57. * do that for me.
  58. *
  59. * Method:
  60. * 1. send a SOFT RESET
  61. * 2. wait for it to finish
  62. * 3. reset the memory management unit
  63. * 4. clear all interrupts
  64. *
  65. */
  66. static void smc_reset(int ioaddr)
  67. {
  68. /* This resets the registers mostly to defaults, but doesn't
  69. * affect EEPROM. That seems unnecessary */
  70. SMC_SELECT_BANK(ioaddr, 0);
  71. _outw( RCR_SOFTRESET, ioaddr + RCR );
  72. /* this should pause enough for the chip to be happy */
  73. SMC_DELAY(ioaddr);
  74. /* Set the transmit and receive configuration registers to
  75. * default values */
  76. _outw(RCR_CLEAR, ioaddr + RCR);
  77. _outw(TCR_CLEAR, ioaddr + TCR);
  78. /* Reset the MMU */
  79. SMC_SELECT_BANK(ioaddr, 2);
  80. _outw( MC_RESET, ioaddr + MMU_CMD );
  81. /* Note: It doesn't seem that waiting for the MMU busy is needed here,
  82. * but this is a place where future chipsets _COULD_ break. Be wary
  83. * of issuing another MMU command right after this */
  84. _outb(0, ioaddr + INT_MASK);
  85. }
  86. /*----------------------------------------------------------------------
  87. * Function: smc_probe( int ioaddr )
  88. *
  89. * Purpose:
  90. * Tests to see if a given ioaddr points to an SMC9xxx chip.
  91. * Returns a 0 on success
  92. *
  93. * Algorithm:
  94. * (1) see if the high byte of BANK_SELECT is 0x33
  95. * (2) compare the ioaddr with the base register's address
  96. * (3) see if I recognize the chip ID in the appropriate register
  97. *
  98. * ---------------------------------------------------------------------
  99. */
  100. static int smc_probe( int ioaddr )
  101. {
  102. word bank;
  103. word revision_register;
  104. word base_address_register;
  105. /* First, see if the high byte is 0x33 */
  106. bank = inw(ioaddr + BANK_SELECT);
  107. if ((bank & 0xFF00) != 0x3300) {
  108. return -1;
  109. }
  110. /* The above MIGHT indicate a device, but I need to write to further
  111. * test this. */
  112. _outw(0x0, ioaddr + BANK_SELECT);
  113. bank = inw(ioaddr + BANK_SELECT);
  114. if ((bank & 0xFF00) != 0x3300) {
  115. return -1;
  116. }
  117. /* well, we've already written once, so hopefully another time won't
  118. * hurt. This time, I need to switch the bank register to bank 1,
  119. * so I can access the base address register */
  120. SMC_SELECT_BANK(ioaddr, 1);
  121. base_address_register = inw(ioaddr + BASE);
  122. if (ioaddr != (base_address_register >> 3 & 0x3E0)) {
  123. #ifdef SMC9000_VERBOSE
  124. printf("SMC9000: IOADDR %hX doesn't match configuration (%hX)."
  125. "Probably not a SMC chip\n",
  126. ioaddr, base_address_register >> 3 & 0x3E0);
  127. #endif
  128. /* well, the base address register didn't match. Must not have
  129. * been a SMC chip after all. */
  130. return -1;
  131. }
  132. /* check if the revision register is something that I recognize.
  133. * These might need to be added to later, as future revisions
  134. * could be added. */
  135. SMC_SELECT_BANK(ioaddr, 3);
  136. revision_register = inw(ioaddr + REVISION);
  137. if (!chip_ids[(revision_register >> 4) & 0xF]) {
  138. /* I don't recognize this chip, so... */
  139. #ifdef SMC9000_VERBOSE
  140. printf("SMC9000: IO %hX: Unrecognized revision register:"
  141. " %hX, Contact author.\n", ioaddr, revision_register);
  142. #endif
  143. return -1;
  144. }
  145. /* at this point I'll assume that the chip is an SMC9xxx.
  146. * It might be prudent to check a listing of MAC addresses
  147. * against the hardware address, or do some other tests. */
  148. return 0;
  149. }
  150. /**************************************************************************
  151. * ETH_RESET - Reset adapter
  152. ***************************************************************************/
  153. static void smc9000_reset(struct nic *nic)
  154. {
  155. smc_reset(smc9000_base);
  156. }
  157. /**************************************************************************
  158. * ETH_TRANSMIT - Transmit a frame
  159. ***************************************************************************/
  160. static void smc9000_transmit(
  161. struct nic *nic,
  162. const char *d, /* Destination */
  163. unsigned int t, /* Type */
  164. unsigned int s, /* size */
  165. const char *p) /* Packet */
  166. {
  167. word length; /* real, length incl. header */
  168. word numPages;
  169. unsigned long time_out;
  170. byte packet_no;
  171. word status;
  172. int i;
  173. /* We dont pad here since we can have the hardware doing it for us */
  174. length = (s + ETH_HLEN + 1)&~1;
  175. /* convert to MMU pages */
  176. numPages = length / 256;
  177. if (numPages > 7 ) {
  178. #ifdef SMC9000_VERBOSE
  179. printf("SMC9000: Far too big packet error. \n");
  180. #endif
  181. return;
  182. }
  183. /* dont try more than, say 30 times */
  184. for (i=0;i<30;i++) {
  185. /* now, try to allocate the memory */
  186. SMC_SELECT_BANK(smc9000_base, 2);
  187. _outw(MC_ALLOC | numPages, smc9000_base + MMU_CMD);
  188. status = 0;
  189. /* wait for the memory allocation to finnish */
  190. for (time_out = currticks() + 5*TICKS_PER_SEC; currticks() < time_out; ) {
  191. status = inb(smc9000_base + INTERRUPT);
  192. if ( status & IM_ALLOC_INT ) {
  193. /* acknowledge the interrupt */
  194. _outb(IM_ALLOC_INT, smc9000_base + INTERRUPT);
  195. break;
  196. }
  197. }
  198. if ((status & IM_ALLOC_INT) != 0 ) {
  199. /* We've got the memory */
  200. break;
  201. } else {
  202. printf("SMC9000: Memory allocation timed out, resetting MMU.\n");
  203. _outw(MC_RESET, smc9000_base + MMU_CMD);
  204. }
  205. }
  206. /* If I get here, I _know_ there is a packet slot waiting for me */
  207. packet_no = inb(smc9000_base + PNR_ARR + 1);
  208. if (packet_no & 0x80) {
  209. /* or isn't there? BAD CHIP! */
  210. printf("SMC9000: Memory allocation failed. \n");
  211. return;
  212. }
  213. /* we have a packet address, so tell the card to use it */
  214. _outb(packet_no, smc9000_base + PNR_ARR);
  215. /* point to the beginning of the packet */
  216. _outw(PTR_AUTOINC, smc9000_base + POINTER);
  217. #if SMC9000_DEBUG > 2
  218. printf("Trying to xmit packet of length %hX\n", length );
  219. #endif
  220. /* send the packet length ( +6 for status, length and ctl byte )
  221. * and the status word ( set to zeros ) */
  222. _outw(0, smc9000_base + DATA_1 );
  223. /* send the packet length ( +6 for status words, length, and ctl) */
  224. _outb((length+6) & 0xFF, smc9000_base + DATA_1);
  225. _outb((length+6) >> 8 , smc9000_base + DATA_1);
  226. /* Write the contents of the packet */
  227. /* The ethernet header first... */
  228. outsw(smc9000_base + DATA_1, d, ETH_ALEN >> 1);
  229. outsw(smc9000_base + DATA_1, nic->node_addr, ETH_ALEN >> 1);
  230. _outw(htons(t), smc9000_base + DATA_1);
  231. /* ... the data ... */
  232. outsw(smc9000_base + DATA_1 , p, s >> 1);
  233. /* ... and the last byte, if there is one. */
  234. if ((s & 1) == 0) {
  235. _outw(0, smc9000_base + DATA_1);
  236. } else {
  237. _outb(p[s-1], smc9000_base + DATA_1);
  238. _outb(0x20, smc9000_base + DATA_1);
  239. }
  240. /* and let the chipset deal with it */
  241. _outw(MC_ENQUEUE , smc9000_base + MMU_CMD);
  242. status = 0; time_out = currticks() + 5*TICKS_PER_SEC;
  243. do {
  244. status = inb(smc9000_base + INTERRUPT);
  245. if ((status & IM_TX_INT ) != 0) {
  246. word tx_status;
  247. /* ack interrupt */
  248. _outb(IM_TX_INT, smc9000_base + INTERRUPT);
  249. packet_no = inw(smc9000_base + FIFO_PORTS);
  250. packet_no &= 0x7F;
  251. /* select this as the packet to read from */
  252. _outb( packet_no, smc9000_base + PNR_ARR );
  253. /* read the first word from this packet */
  254. _outw( PTR_AUTOINC | PTR_READ, smc9000_base + POINTER );
  255. tx_status = inw( smc9000_base + DATA_1 );
  256. if (0 == (tx_status & TS_SUCCESS)) {
  257. #ifdef SMC9000_VERBOSE
  258. printf("SMC9000: TX FAIL STATUS: %hX \n", tx_status);
  259. #endif
  260. /* re-enable transmit */
  261. SMC_SELECT_BANK(smc9000_base, 0);
  262. _outw(inw(smc9000_base + TCR ) | TCR_ENABLE, smc9000_base + TCR );
  263. }
  264. /* kill the packet */
  265. SMC_SELECT_BANK(smc9000_base, 2);
  266. _outw(MC_FREEPKT, smc9000_base + MMU_CMD);
  267. return;
  268. }
  269. }while(currticks() < time_out);
  270. printf("SMC9000: Waring TX timed out, resetting board\n");
  271. smc_reset(smc9000_base);
  272. return;
  273. }
  274. /**************************************************************************
  275. * ETH_POLL - Wait for a frame
  276. ***************************************************************************/
  277. static int smc9000_poll(struct nic *nic)
  278. {
  279. if(!smc9000_base)
  280. return 0;
  281. SMC_SELECT_BANK(smc9000_base, 2);
  282. if (inw(smc9000_base + FIFO_PORTS) & FP_RXEMPTY)
  283. return 0;
  284. /* start reading from the start of the packet */
  285. _outw(PTR_READ | PTR_RCV | PTR_AUTOINC, smc9000_base + POINTER);
  286. /* First read the status and check that we're ok */
  287. if (!(inw(smc9000_base + DATA_1) & RS_ERRORS)) {
  288. /* Next: read the packet length and mask off the top bits */
  289. nic->packetlen = (inw(smc9000_base + DATA_1) & 0x07ff);
  290. /* the packet length includes the 3 extra words */
  291. nic->packetlen -= 6;
  292. #if SMC9000_DEBUG > 2
  293. printf(" Reading %d words (and %d byte(s))\n",
  294. (nic->packetlen >> 1), nic->packetlen & 1);
  295. #endif
  296. /* read the packet (and the last "extra" word) */
  297. insw(smc9000_base + DATA_1, nic->packet, (nic->packetlen+2) >> 1);
  298. /* is there an odd last byte ? */
  299. if (nic->packet[nic->packetlen+1] & 0x20)
  300. nic->packetlen++;
  301. /* error or good, tell the card to get rid of this packet */
  302. _outw(MC_RELEASE, smc9000_base + MMU_CMD);
  303. return 1;
  304. }
  305. printf("SMC9000: RX error\n");
  306. /* error or good, tell the card to get rid of this packet */
  307. _outw(MC_RELEASE, smc9000_base + MMU_CMD);
  308. return 0;
  309. }
  310. static void smc9000_disable(struct nic *nic)
  311. {
  312. if(!smc9000_base)
  313. return;
  314. /* no more interrupts for me */
  315. SMC_SELECT_BANK(smc9000_base, 2);
  316. _outb( 0, smc9000_base + INT_MASK);
  317. /* and tell the card to stay away from that nasty outside world */
  318. SMC_SELECT_BANK(smc9000_base, 0);
  319. _outb( RCR_CLEAR, smc9000_base + RCR );
  320. _outb( TCR_CLEAR, smc9000_base + TCR );
  321. }
  322. /**************************************************************************
  323. * ETH_PROBE - Look for an adapter
  324. ***************************************************************************/
  325. struct nic *smc9000_probe(struct nic *nic, unsigned short *probe_addrs)
  326. {
  327. unsigned short revision;
  328. int memory;
  329. int media;
  330. const char * version_string;
  331. const char * if_string;
  332. int i;
  333. /*
  334. * the SMC9000 can be at any of the following port addresses. To change,
  335. * for a slightly different card, you can add it to the array. Keep in
  336. * mind that the array must end in zero.
  337. */
  338. static unsigned short portlist[] = {
  339. #ifdef SMC9000_SCAN
  340. SMC9000_SCAN,
  341. #else
  342. 0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
  343. 0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0,
  344. #endif
  345. 0 };
  346. printf("\nSMC9000 %s\n", smc9000_version);
  347. #ifdef SMC9000_VERBOSE
  348. printf("Copyright (C) 1998 Daniel Engstr\x94m\n");
  349. printf("Copyright (C) 1996 Eric Stahlman\n");
  350. #endif
  351. /* if no addresses supplied, fall back on defaults */
  352. if (probe_addrs == 0 || probe_addrs[0] == 0)
  353. probe_addrs = portlist;
  354. /* check every ethernet address */
  355. for (i = 0; probe_addrs[i]; i++) {
  356. /* check this specific address */
  357. if (smc_probe(probe_addrs[i]) == 0)
  358. smc9000_base = probe_addrs[i];
  359. }
  360. /* couldn't find anything */
  361. if(0 == smc9000_base)
  362. goto out;
  363. /*
  364. * Get the MAC address ( bank 1, regs 4 - 9 )
  365. */
  366. SMC_SELECT_BANK(smc9000_base, 1);
  367. for ( i = 0; i < 6; i += 2 ) {
  368. word address;
  369. address = inw(smc9000_base + ADDR0 + i);
  370. nic->node_addr[i+1] = address >> 8;
  371. nic->node_addr[i] = address & 0xFF;
  372. }
  373. /* get the memory information */
  374. SMC_SELECT_BANK(smc9000_base, 0);
  375. memory = ( inw(smc9000_base + MCR) >> 9 ) & 0x7; /* multiplier */
  376. memory *= 256 * (inw(smc9000_base + MIR) & 0xFF);
  377. /*
  378. * Now, I want to find out more about the chip. This is sort of
  379. * redundant, but it's cleaner to have it in both, rather than having
  380. * one VERY long probe procedure.
  381. */
  382. SMC_SELECT_BANK(smc9000_base, 3);
  383. revision = inw(smc9000_base + REVISION);
  384. version_string = chip_ids[(revision >> 4) & 0xF];
  385. if (((revision & 0xF0) >> 4 == CHIP_9196) &&
  386. ((revision & 0x0F) >= REV_9196)) {
  387. /* This is a 91c96. 'c96 has the same chip id as 'c94 (4) but
  388. * a revision starting at 6 */
  389. version_string = smc91c96_id;
  390. }
  391. if ( !version_string ) {
  392. /* I shouldn't get here because this call was done before.... */
  393. goto out;
  394. }
  395. /* is it using AUI or 10BaseT ? */
  396. SMC_SELECT_BANK(smc9000_base, 1);
  397. if (inw(smc9000_base + CONFIG) & CFG_AUI_SELECT)
  398. media = 2;
  399. else
  400. media = 1;
  401. if_string = interfaces[media - 1];
  402. /* now, reset the chip, and put it into a known state */
  403. smc_reset(smc9000_base);
  404. printf("%s rev:%d I/O port:%hX Interface:%s RAM:%d bytes \n",
  405. version_string, revision & 0xF,
  406. smc9000_base, if_string, memory );
  407. /*
  408. * Print the Ethernet address
  409. */
  410. printf("Ethernet MAC address: %!\n", nic->node_addr);
  411. SMC_SELECT_BANK(smc9000_base, 0);
  412. /* see the header file for options in TCR/RCR NORMAL*/
  413. _outw(TCR_NORMAL, smc9000_base + TCR);
  414. _outw(RCR_NORMAL, smc9000_base + RCR);
  415. /* Select which interface to use */
  416. SMC_SELECT_BANK(smc9000_base, 1);
  417. if ( media == 1 ) {
  418. _outw( inw( smc9000_base + CONFIG ) & ~CFG_AUI_SELECT,
  419. smc9000_base + CONFIG );
  420. }
  421. else if ( media == 2 ) {
  422. _outw( inw( smc9000_base + CONFIG ) | CFG_AUI_SELECT,
  423. smc9000_base + CONFIG );
  424. }
  425. nic->reset = smc9000_reset;
  426. nic->poll = smc9000_poll;
  427. nic->transmit = smc9000_transmit;
  428. nic->disable = smc9000_disable;
  429. return nic;
  430. out:
  431. #ifdef SMC9000_VERBOSE
  432. printf("No SMC9000 adapters found\n");
  433. #endif
  434. smc9000_base = 0;
  435. return (0);
  436. }