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.

smc9000.c 26KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  1. #ifdef ALLMULTI
  2. #error multicast support is not yet implemented
  3. #endif
  4. /*------------------------------------------------------------------------
  5. * smc9000.c
  6. * This is a Etherboot driver for SMC's 9000 series of Ethernet cards.
  7. *
  8. * Copyright (C) 1998 Daniel Engström <daniel.engstrom@riksnett.no>
  9. * Based on the Linux SMC9000 driver, smc9194.c by Eric Stahlman
  10. * Copyright (C) 1996 by Erik Stahlman <eric@vt.edu>
  11. *
  12. * This software may be used and distributed according to the terms
  13. * of the GNU Public License, incorporated herein by reference.
  14. *
  15. * "Features" of the SMC chip:
  16. * 4608 byte packet memory. ( for the 91C92/4. Others have more )
  17. * EEPROM for configuration
  18. * AUI/TP selection
  19. *
  20. * Authors
  21. * Erik Stahlman <erik@vt.edu>
  22. * Daniel Engström <daniel.engstrom@riksnett.no>
  23. *
  24. * History
  25. * 98-09-25 Daniel Engström Etherboot driver crated from Eric's
  26. * Linux driver.
  27. *
  28. *---------------------------------------------------------------------------*/
  29. #define LINUX_OUT_MACROS 1
  30. #define SMC9000_DEBUG 0
  31. #if SMC9000_DEBUG > 1
  32. #define PRINTK2 printf
  33. #else
  34. #define PRINTK2(args...)
  35. #endif
  36. #include <gpxe/ethernet.h>
  37. #include <errno.h>
  38. #include "etherboot.h"
  39. #include "nic.h"
  40. #include <gpxe/isa.h>
  41. #include "timer.h"
  42. #include "smc9000.h"
  43. # define _outb outb
  44. # define _outw outw
  45. static const char smc9000_version[] = "Version 0.99 98-09-30";
  46. static const char *interfaces[ 2 ] = { "TP", "AUI" };
  47. static const char *chip_ids[ 15 ] = {
  48. NULL, NULL, NULL,
  49. /* 3 */ "SMC91C90/91C92",
  50. /* 4 */ "SMC91C94",
  51. /* 5 */ "SMC91C95",
  52. NULL,
  53. /* 7 */ "SMC91C100",
  54. /* 8 */ "SMC91C100FD",
  55. /* 9 */ "SMC91C11xFD",
  56. NULL, NULL,
  57. NULL, NULL, NULL
  58. };
  59. static const char smc91c96_id[] = "SMC91C96";
  60. /*------------------------------------------------------------
  61. . Reads a register from the MII Management serial interface
  62. .-------------------------------------------------------------*/
  63. static word smc_read_phy_register(int ioaddr, byte phyaddr, byte phyreg)
  64. {
  65. int oldBank;
  66. unsigned int i;
  67. byte mask;
  68. word mii_reg;
  69. byte bits[64];
  70. int clk_idx = 0;
  71. int input_idx;
  72. word phydata;
  73. // 32 consecutive ones on MDO to establish sync
  74. for (i = 0; i < 32; ++i)
  75. bits[clk_idx++] = MII_MDOE | MII_MDO;
  76. // Start code <01>
  77. bits[clk_idx++] = MII_MDOE;
  78. bits[clk_idx++] = MII_MDOE | MII_MDO;
  79. // Read command <10>
  80. bits[clk_idx++] = MII_MDOE | MII_MDO;
  81. bits[clk_idx++] = MII_MDOE;
  82. // Output the PHY address, msb first
  83. mask = (byte)0x10;
  84. for (i = 0; i < 5; ++i)
  85. {
  86. if (phyaddr & mask)
  87. bits[clk_idx++] = MII_MDOE | MII_MDO;
  88. else
  89. bits[clk_idx++] = MII_MDOE;
  90. // Shift to next lowest bit
  91. mask >>= 1;
  92. }
  93. // Output the phy register number, msb first
  94. mask = (byte)0x10;
  95. for (i = 0; i < 5; ++i)
  96. {
  97. if (phyreg & mask)
  98. bits[clk_idx++] = MII_MDOE | MII_MDO;
  99. else
  100. bits[clk_idx++] = MII_MDOE;
  101. // Shift to next lowest bit
  102. mask >>= 1;
  103. }
  104. // Tristate and turnaround (2 bit times)
  105. bits[clk_idx++] = 0;
  106. //bits[clk_idx++] = 0;
  107. // Input starts at this bit time
  108. input_idx = clk_idx;
  109. // Will input 16 bits
  110. for (i = 0; i < 16; ++i)
  111. bits[clk_idx++] = 0;
  112. // Final clock bit
  113. bits[clk_idx++] = 0;
  114. // Save the current bank
  115. oldBank = inw( ioaddr+BANK_SELECT );
  116. // Select bank 3
  117. SMC_SELECT_BANK(ioaddr, 3);
  118. // Get the current MII register value
  119. mii_reg = inw( ioaddr+MII_REG );
  120. // Turn off all MII Interface bits
  121. mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
  122. // Clock all 64 cycles
  123. for (i = 0; i < sizeof(bits); ++i)
  124. {
  125. // Clock Low - output data
  126. outw( mii_reg | bits[i], ioaddr+MII_REG );
  127. udelay(50);
  128. // Clock Hi - input data
  129. outw( mii_reg | bits[i] | MII_MCLK, ioaddr+MII_REG );
  130. udelay(50);
  131. bits[i] |= inw( ioaddr+MII_REG ) & MII_MDI;
  132. }
  133. // Return to idle state
  134. // Set clock to low, data to low, and output tristated
  135. outw( mii_reg, ioaddr+MII_REG );
  136. udelay(50);
  137. // Restore original bank select
  138. SMC_SELECT_BANK(ioaddr, oldBank);
  139. // Recover input data
  140. phydata = 0;
  141. for (i = 0; i < 16; ++i)
  142. {
  143. phydata <<= 1;
  144. if (bits[input_idx++] & MII_MDI)
  145. phydata |= 0x0001;
  146. }
  147. #if (SMC_DEBUG > 2 )
  148. printf("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  149. phyaddr, phyreg, phydata);
  150. #endif
  151. return(phydata);
  152. }
  153. /*------------------------------------------------------------
  154. . Writes a register to the MII Management serial interface
  155. .-------------------------------------------------------------*/
  156. static void smc_write_phy_register(int ioaddr,
  157. byte phyaddr, byte phyreg, word phydata)
  158. {
  159. int oldBank;
  160. unsigned int i;
  161. word mask;
  162. word mii_reg;
  163. byte bits[65];
  164. int clk_idx = 0;
  165. // 32 consecutive ones on MDO to establish sync
  166. for (i = 0; i < 32; ++i)
  167. bits[clk_idx++] = MII_MDOE | MII_MDO;
  168. // Start code <01>
  169. bits[clk_idx++] = MII_MDOE;
  170. bits[clk_idx++] = MII_MDOE | MII_MDO;
  171. // Write command <01>
  172. bits[clk_idx++] = MII_MDOE;
  173. bits[clk_idx++] = MII_MDOE | MII_MDO;
  174. // Output the PHY address, msb first
  175. mask = (byte)0x10;
  176. for (i = 0; i < 5; ++i)
  177. {
  178. if (phyaddr & mask)
  179. bits[clk_idx++] = MII_MDOE | MII_MDO;
  180. else
  181. bits[clk_idx++] = MII_MDOE;
  182. // Shift to next lowest bit
  183. mask >>= 1;
  184. }
  185. // Output the phy register number, msb first
  186. mask = (byte)0x10;
  187. for (i = 0; i < 5; ++i)
  188. {
  189. if (phyreg & mask)
  190. bits[clk_idx++] = MII_MDOE | MII_MDO;
  191. else
  192. bits[clk_idx++] = MII_MDOE;
  193. // Shift to next lowest bit
  194. mask >>= 1;
  195. }
  196. // Tristate and turnaround (2 bit times)
  197. bits[clk_idx++] = 0;
  198. bits[clk_idx++] = 0;
  199. // Write out 16 bits of data, msb first
  200. mask = 0x8000;
  201. for (i = 0; i < 16; ++i)
  202. {
  203. if (phydata & mask)
  204. bits[clk_idx++] = MII_MDOE | MII_MDO;
  205. else
  206. bits[clk_idx++] = MII_MDOE;
  207. // Shift to next lowest bit
  208. mask >>= 1;
  209. }
  210. // Final clock bit (tristate)
  211. bits[clk_idx++] = 0;
  212. // Save the current bank
  213. oldBank = inw( ioaddr+BANK_SELECT );
  214. // Select bank 3
  215. SMC_SELECT_BANK(ioaddr, 3);
  216. // Get the current MII register value
  217. mii_reg = inw( ioaddr+MII_REG );
  218. // Turn off all MII Interface bits
  219. mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
  220. // Clock all cycles
  221. for (i = 0; i < sizeof(bits); ++i)
  222. {
  223. // Clock Low - output data
  224. outw( mii_reg | bits[i], ioaddr+MII_REG );
  225. udelay(50);
  226. // Clock Hi - input data
  227. outw( mii_reg | bits[i] | MII_MCLK, ioaddr+MII_REG );
  228. udelay(50);
  229. bits[i] |= inw( ioaddr+MII_REG ) & MII_MDI;
  230. }
  231. // Return to idle state
  232. // Set clock to low, data to low, and output tristated
  233. outw( mii_reg, ioaddr+MII_REG );
  234. udelay(50);
  235. // Restore original bank select
  236. SMC_SELECT_BANK(ioaddr, oldBank);
  237. #if (SMC_DEBUG > 2 )
  238. printf("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  239. phyaddr, phyreg, phydata);
  240. #endif
  241. }
  242. /*------------------------------------------------------------
  243. . Finds and reports the PHY address
  244. .-------------------------------------------------------------*/
  245. static int smc_detect_phy(int ioaddr, byte *pphyaddr)
  246. {
  247. word phy_id1;
  248. word phy_id2;
  249. int phyaddr;
  250. int found = 0;
  251. // Scan all 32 PHY addresses if necessary
  252. for (phyaddr = 0; phyaddr < 32; ++phyaddr)
  253. {
  254. // Read the PHY identifiers
  255. phy_id1 = smc_read_phy_register(ioaddr, phyaddr, PHY_ID1_REG);
  256. phy_id2 = smc_read_phy_register(ioaddr, phyaddr, PHY_ID2_REG);
  257. // Make sure it is a valid identifier
  258. if ((phy_id2 > 0x0000) && (phy_id2 < 0xffff) &&
  259. (phy_id1 > 0x0000) && (phy_id1 < 0xffff))
  260. {
  261. if ((phy_id1 != 0x8000) && (phy_id2 != 0x8000))
  262. {
  263. // Save the PHY's address
  264. *pphyaddr = phyaddr;
  265. found = 1;
  266. break;
  267. }
  268. }
  269. }
  270. if (!found)
  271. {
  272. printf("No PHY found\n");
  273. return(0);
  274. }
  275. // Set the PHY type
  276. if ( (phy_id1 == 0x0016) && ((phy_id2 & 0xFFF0) == 0xF840 ) )
  277. {
  278. printf("PHY=LAN83C183 (LAN91C111 Internal)\n");
  279. }
  280. if ( (phy_id1 == 0x0282) && ((phy_id2 & 0xFFF0) == 0x1C50) )
  281. {
  282. printf("PHY=LAN83C180\n");
  283. }
  284. return(1);
  285. }
  286. /*------------------------------------------------------------
  287. . Configures the specified PHY using Autonegotiation. Calls
  288. . smc_phy_fixed() if the user has requested a certain config.
  289. .-------------------------------------------------------------*/
  290. static void smc_phy_configure(int ioaddr)
  291. {
  292. int timeout;
  293. byte phyaddr;
  294. word my_phy_caps; // My PHY capabilities
  295. word my_ad_caps; // My Advertised capabilities
  296. word status;
  297. int failed = 0;
  298. int rpc_cur_mode = RPC_DEFAULT;
  299. int lastPhy18;
  300. // Find the address and type of our phy
  301. if (!smc_detect_phy(ioaddr, &phyaddr))
  302. {
  303. return;
  304. }
  305. // Reset the PHY, setting all other bits to zero
  306. smc_write_phy_register(ioaddr, phyaddr, PHY_CNTL_REG, PHY_CNTL_RST);
  307. // Wait for the reset to complete, or time out
  308. timeout = 6; // Wait up to 3 seconds
  309. while (timeout--)
  310. {
  311. if (!(smc_read_phy_register(ioaddr, phyaddr, PHY_CNTL_REG)
  312. & PHY_CNTL_RST))
  313. {
  314. // reset complete
  315. break;
  316. }
  317. mdelay(500); // wait 500 millisecs
  318. }
  319. if (timeout < 1)
  320. {
  321. PRINTK2("PHY reset timed out\n");
  322. return;
  323. }
  324. // Read PHY Register 18, Status Output
  325. lastPhy18 = smc_read_phy_register(ioaddr, phyaddr, PHY_INT_REG);
  326. // Enable PHY Interrupts (for register 18)
  327. // Interrupts listed here are disabled
  328. smc_write_phy_register(ioaddr, phyaddr, PHY_MASK_REG,
  329. PHY_INT_LOSSSYNC | PHY_INT_CWRD | PHY_INT_SSD |
  330. PHY_INT_ESD | PHY_INT_RPOL | PHY_INT_JAB |
  331. PHY_INT_SPDDET | PHY_INT_DPLXDET);
  332. /* Configure the Receive/Phy Control register */
  333. SMC_SELECT_BANK(ioaddr, 0);
  334. outw( rpc_cur_mode, ioaddr + RPC_REG );
  335. // Copy our capabilities from PHY_STAT_REG to PHY_AD_REG
  336. my_phy_caps = smc_read_phy_register(ioaddr, phyaddr, PHY_STAT_REG);
  337. my_ad_caps = PHY_AD_CSMA; // I am CSMA capable
  338. if (my_phy_caps & PHY_STAT_CAP_T4)
  339. my_ad_caps |= PHY_AD_T4;
  340. if (my_phy_caps & PHY_STAT_CAP_TXF)
  341. my_ad_caps |= PHY_AD_TX_FDX;
  342. if (my_phy_caps & PHY_STAT_CAP_TXH)
  343. my_ad_caps |= PHY_AD_TX_HDX;
  344. if (my_phy_caps & PHY_STAT_CAP_TF)
  345. my_ad_caps |= PHY_AD_10_FDX;
  346. if (my_phy_caps & PHY_STAT_CAP_TH)
  347. my_ad_caps |= PHY_AD_10_HDX;
  348. // Update our Auto-Neg Advertisement Register
  349. smc_write_phy_register(ioaddr, phyaddr, PHY_AD_REG, my_ad_caps);
  350. PRINTK2("phy caps=%x\n", my_phy_caps);
  351. PRINTK2("phy advertised caps=%x\n", my_ad_caps);
  352. // Restart auto-negotiation process in order to advertise my caps
  353. smc_write_phy_register( ioaddr, phyaddr, PHY_CNTL_REG,
  354. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST );
  355. // Wait for the auto-negotiation to complete. This may take from
  356. // 2 to 3 seconds.
  357. // Wait for the reset to complete, or time out
  358. timeout = 20; // Wait up to 10 seconds
  359. while (timeout--)
  360. {
  361. status = smc_read_phy_register(ioaddr, phyaddr, PHY_STAT_REG);
  362. if (status & PHY_STAT_ANEG_ACK)
  363. {
  364. // auto-negotiate complete
  365. break;
  366. }
  367. mdelay(500); // wait 500 millisecs
  368. // Restart auto-negotiation if remote fault
  369. if (status & PHY_STAT_REM_FLT)
  370. {
  371. PRINTK2("PHY remote fault detected\n");
  372. // Restart auto-negotiation
  373. PRINTK2("PHY restarting auto-negotiation\n");
  374. smc_write_phy_register( ioaddr, phyaddr, PHY_CNTL_REG,
  375. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST |
  376. PHY_CNTL_SPEED | PHY_CNTL_DPLX);
  377. }
  378. }
  379. if (timeout < 1)
  380. {
  381. PRINTK2("PHY auto-negotiate timed out\n");
  382. failed = 1;
  383. }
  384. // Fail if we detected an auto-negotiate remote fault
  385. if (status & PHY_STAT_REM_FLT)
  386. {
  387. PRINTK2("PHY remote fault detected\n");
  388. failed = 1;
  389. }
  390. // Set our sysctl parameters to match auto-negotiation results
  391. if ( lastPhy18 & PHY_INT_SPDDET )
  392. {
  393. PRINTK2("PHY 100BaseT\n");
  394. rpc_cur_mode |= RPC_SPEED;
  395. }
  396. else
  397. {
  398. PRINTK2("PHY 10BaseT\n");
  399. rpc_cur_mode &= ~RPC_SPEED;
  400. }
  401. if ( lastPhy18 & PHY_INT_DPLXDET )
  402. {
  403. PRINTK2("PHY Full Duplex\n");
  404. rpc_cur_mode |= RPC_DPLX;
  405. }
  406. else
  407. {
  408. PRINTK2("PHY Half Duplex\n");
  409. rpc_cur_mode &= ~RPC_DPLX;
  410. }
  411. // Re-Configure the Receive/Phy Control register
  412. outw( rpc_cur_mode, ioaddr + RPC_REG );
  413. }
  414. /*
  415. * Function: smc_reset( int ioaddr )
  416. * Purpose:
  417. * This sets the SMC91xx chip to its normal state, hopefully from whatever
  418. * mess that any other DOS driver has put it in.
  419. *
  420. * Maybe I should reset more registers to defaults in here? SOFTRESET should
  421. * do that for me.
  422. *
  423. * Method:
  424. * 1. send a SOFT RESET
  425. * 2. wait for it to finish
  426. * 3. reset the memory management unit
  427. * 4. clear all interrupts
  428. *
  429. */
  430. static void smc_reset(int ioaddr)
  431. {
  432. /* This resets the registers mostly to defaults, but doesn't
  433. * affect EEPROM. That seems unnecessary */
  434. SMC_SELECT_BANK(ioaddr, 0);
  435. _outw( RCR_SOFTRESET, ioaddr + RCR );
  436. /* this should pause enough for the chip to be happy */
  437. SMC_DELAY(ioaddr);
  438. /* Set the transmit and receive configuration registers to
  439. * default values */
  440. _outw(RCR_CLEAR, ioaddr + RCR);
  441. _outw(TCR_CLEAR, ioaddr + TCR);
  442. /* Reset the MMU */
  443. SMC_SELECT_BANK(ioaddr, 2);
  444. _outw( MC_RESET, ioaddr + MMU_CMD );
  445. /* Note: It doesn't seem that waiting for the MMU busy is needed here,
  446. * but this is a place where future chipsets _COULD_ break. Be wary
  447. * of issuing another MMU command right after this */
  448. _outb(0, ioaddr + INT_MASK);
  449. }
  450. /*----------------------------------------------------------------------
  451. * Function: smc9000_probe_addr( int ioaddr )
  452. *
  453. * Purpose:
  454. * Tests to see if a given ioaddr points to an SMC9xxx chip.
  455. * Returns a 1 on success
  456. *
  457. * Algorithm:
  458. * (1) see if the high byte of BANK_SELECT is 0x33
  459. * (2) compare the ioaddr with the base register's address
  460. * (3) see if I recognize the chip ID in the appropriate register
  461. *
  462. * ---------------------------------------------------------------------
  463. */
  464. static int smc9000_probe_addr( isa_probe_addr_t ioaddr )
  465. {
  466. word bank;
  467. word revision_register;
  468. word base_address_register;
  469. /* First, see if the high byte is 0x33 */
  470. bank = inw(ioaddr + BANK_SELECT);
  471. if ((bank & 0xFF00) != 0x3300) {
  472. return 0;
  473. }
  474. /* The above MIGHT indicate a device, but I need to write to further
  475. * test this. */
  476. _outw(0x0, ioaddr + BANK_SELECT);
  477. bank = inw(ioaddr + BANK_SELECT);
  478. if ((bank & 0xFF00) != 0x3300) {
  479. return 0;
  480. }
  481. /* well, we've already written once, so hopefully another time won't
  482. * hurt. This time, I need to switch the bank register to bank 1,
  483. * so I can access the base address register */
  484. SMC_SELECT_BANK(ioaddr, 1);
  485. base_address_register = inw(ioaddr + BASE);
  486. if (ioaddr != (base_address_register >> 3 & 0x3E0)) {
  487. DBG("SMC9000: IOADDR %hX doesn't match configuration (%hX)."
  488. "Probably not a SMC chip\n",
  489. ioaddr, base_address_register >> 3 & 0x3E0);
  490. /* well, the base address register didn't match. Must not have
  491. * been a SMC chip after all. */
  492. return 0;
  493. }
  494. /* check if the revision register is something that I recognize.
  495. * These might need to be added to later, as future revisions
  496. * could be added. */
  497. SMC_SELECT_BANK(ioaddr, 3);
  498. revision_register = inw(ioaddr + REVISION);
  499. if (!chip_ids[(revision_register >> 4) & 0xF]) {
  500. /* I don't recognize this chip, so... */
  501. DBG( "SMC9000: IO %hX: Unrecognized revision register:"
  502. " %hX, Contact author.\n", ioaddr, revision_register );
  503. return 0;
  504. }
  505. /* at this point I'll assume that the chip is an SMC9xxx.
  506. * It might be prudent to check a listing of MAC addresses
  507. * against the hardware address, or do some other tests. */
  508. return 1;
  509. }
  510. /**************************************************************************
  511. * ETH_TRANSMIT - Transmit a frame
  512. ***************************************************************************/
  513. static void smc9000_transmit(
  514. struct nic *nic,
  515. const char *d, /* Destination */
  516. unsigned int t, /* Type */
  517. unsigned int s, /* size */
  518. const char *p) /* Packet */
  519. {
  520. word length; /* real, length incl. header */
  521. word numPages;
  522. unsigned long time_out;
  523. byte packet_no;
  524. word status;
  525. int i;
  526. /* We dont pad here since we can have the hardware doing it for us */
  527. length = (s + ETH_HLEN + 1)&~1;
  528. /* convert to MMU pages */
  529. numPages = length / 256;
  530. if (numPages > 7 ) {
  531. DBG("SMC9000: Far too big packet error. \n");
  532. return;
  533. }
  534. /* dont try more than, say 30 times */
  535. for (i=0;i<30;i++) {
  536. /* now, try to allocate the memory */
  537. SMC_SELECT_BANK(nic->ioaddr, 2);
  538. _outw(MC_ALLOC | numPages, nic->ioaddr + MMU_CMD);
  539. status = 0;
  540. /* wait for the memory allocation to finnish */
  541. for (time_out = currticks() + 5*TICKS_PER_SEC; currticks() < time_out; ) {
  542. status = inb(nic->ioaddr + INTERRUPT);
  543. if ( status & IM_ALLOC_INT ) {
  544. /* acknowledge the interrupt */
  545. _outb(IM_ALLOC_INT, nic->ioaddr + INTERRUPT);
  546. break;
  547. }
  548. }
  549. if ((status & IM_ALLOC_INT) != 0 ) {
  550. /* We've got the memory */
  551. break;
  552. } else {
  553. printf("SMC9000: Memory allocation timed out, resetting MMU.\n");
  554. _outw(MC_RESET, nic->ioaddr + MMU_CMD);
  555. }
  556. }
  557. /* If I get here, I _know_ there is a packet slot waiting for me */
  558. packet_no = inb(nic->ioaddr + PNR_ARR + 1);
  559. if (packet_no & 0x80) {
  560. /* or isn't there? BAD CHIP! */
  561. printf("SMC9000: Memory allocation failed. \n");
  562. return;
  563. }
  564. /* we have a packet address, so tell the card to use it */
  565. _outb(packet_no, nic->ioaddr + PNR_ARR);
  566. /* point to the beginning of the packet */
  567. _outw(PTR_AUTOINC, nic->ioaddr + POINTER);
  568. #if SMC9000_DEBUG > 2
  569. printf("Trying to xmit packet of length %hX\n", length );
  570. #endif
  571. /* send the packet length ( +6 for status, length and ctl byte )
  572. * and the status word ( set to zeros ) */
  573. _outw(0, nic->ioaddr + DATA_1 );
  574. /* send the packet length ( +6 for status words, length, and ctl) */
  575. _outb((length+6) & 0xFF, nic->ioaddr + DATA_1);
  576. _outb((length+6) >> 8 , nic->ioaddr + DATA_1);
  577. /* Write the contents of the packet */
  578. /* The ethernet header first... */
  579. outsw(nic->ioaddr + DATA_1, d, ETH_ALEN >> 1);
  580. outsw(nic->ioaddr + DATA_1, nic->node_addr, ETH_ALEN >> 1);
  581. _outw(htons(t), nic->ioaddr + DATA_1);
  582. /* ... the data ... */
  583. outsw(nic->ioaddr + DATA_1 , p, s >> 1);
  584. /* ... and the last byte, if there is one. */
  585. if ((s & 1) == 0) {
  586. _outw(0, nic->ioaddr + DATA_1);
  587. } else {
  588. _outb(p[s-1], nic->ioaddr + DATA_1);
  589. _outb(0x20, nic->ioaddr + DATA_1);
  590. }
  591. /* and let the chipset deal with it */
  592. _outw(MC_ENQUEUE , nic->ioaddr + MMU_CMD);
  593. status = 0; time_out = currticks() + 5*TICKS_PER_SEC;
  594. do {
  595. status = inb(nic->ioaddr + INTERRUPT);
  596. if ((status & IM_TX_INT ) != 0) {
  597. word tx_status;
  598. /* ack interrupt */
  599. _outb(IM_TX_INT, nic->ioaddr + INTERRUPT);
  600. packet_no = inw(nic->ioaddr + FIFO_PORTS);
  601. packet_no &= 0x7F;
  602. /* select this as the packet to read from */
  603. _outb( packet_no, nic->ioaddr + PNR_ARR );
  604. /* read the first word from this packet */
  605. _outw( PTR_AUTOINC | PTR_READ, nic->ioaddr + POINTER );
  606. tx_status = inw( nic->ioaddr + DATA_1 );
  607. if (0 == (tx_status & TS_SUCCESS)) {
  608. DBG("SMC9000: TX FAIL STATUS: %hX \n", tx_status);
  609. /* re-enable transmit */
  610. SMC_SELECT_BANK(nic->ioaddr, 0);
  611. _outw(inw(nic->ioaddr + TCR ) | TCR_ENABLE, nic->ioaddr + TCR );
  612. }
  613. /* kill the packet */
  614. SMC_SELECT_BANK(nic->ioaddr, 2);
  615. _outw(MC_FREEPKT, nic->ioaddr + MMU_CMD);
  616. return;
  617. }
  618. }while(currticks() < time_out);
  619. printf("SMC9000: TX timed out, resetting board\n");
  620. smc_reset(nic->ioaddr);
  621. return;
  622. }
  623. /**************************************************************************
  624. * ETH_POLL - Wait for a frame
  625. ***************************************************************************/
  626. static int smc9000_poll(struct nic *nic, int retrieve)
  627. {
  628. SMC_SELECT_BANK(nic->ioaddr, 2);
  629. if (inw(nic->ioaddr + FIFO_PORTS) & FP_RXEMPTY)
  630. return 0;
  631. if ( ! retrieve ) return 1;
  632. /* start reading from the start of the packet */
  633. _outw(PTR_READ | PTR_RCV | PTR_AUTOINC, nic->ioaddr + POINTER);
  634. /* First read the status and check that we're ok */
  635. if (!(inw(nic->ioaddr + DATA_1) & RS_ERRORS)) {
  636. /* Next: read the packet length and mask off the top bits */
  637. nic->packetlen = (inw(nic->ioaddr + DATA_1) & 0x07ff);
  638. /* the packet length includes the 3 extra words */
  639. nic->packetlen -= 6;
  640. #if SMC9000_DEBUG > 2
  641. printf(" Reading %d words (and %d byte(s))\n",
  642. (nic->packetlen >> 1), nic->packetlen & 1);
  643. #endif
  644. /* read the packet (and the last "extra" word) */
  645. insw(nic->ioaddr + DATA_1, nic->packet, (nic->packetlen+2) >> 1);
  646. /* is there an odd last byte ? */
  647. if (nic->packet[nic->packetlen+1] & 0x20)
  648. nic->packetlen++;
  649. /* error or good, tell the card to get rid of this packet */
  650. _outw(MC_RELEASE, nic->ioaddr + MMU_CMD);
  651. return 1;
  652. }
  653. printf("SMC9000: RX error\n");
  654. /* error or good, tell the card to get rid of this packet */
  655. _outw(MC_RELEASE, nic->ioaddr + MMU_CMD);
  656. return 0;
  657. }
  658. static void smc9000_disable ( struct nic *nic, struct isa_device *isa __unused ) {
  659. smc_reset(nic->ioaddr);
  660. /* no more interrupts for me */
  661. SMC_SELECT_BANK(nic->ioaddr, 2);
  662. _outb( 0, nic->ioaddr + INT_MASK);
  663. /* and tell the card to stay away from that nasty outside world */
  664. SMC_SELECT_BANK(nic->ioaddr, 0);
  665. _outb( RCR_CLEAR, nic->ioaddr + RCR );
  666. _outb( TCR_CLEAR, nic->ioaddr + TCR );
  667. }
  668. static void smc9000_irq(struct nic *nic __unused, irq_action_t action __unused)
  669. {
  670. switch ( action ) {
  671. case DISABLE :
  672. break;
  673. case ENABLE :
  674. break;
  675. case FORCE :
  676. break;
  677. }
  678. }
  679. static struct nic_operations smc9000_operations = {
  680. .connect = dummy_connect,
  681. .poll = smc9000_poll,
  682. .transmit = smc9000_transmit,
  683. .irq = smc9000_irq,
  684. };
  685. /**************************************************************************
  686. * ETH_PROBE - Look for an adapter
  687. ***************************************************************************/
  688. static int smc9000_probe ( struct nic *nic, struct isa_device *isa ) {
  689. unsigned short revision;
  690. int memory;
  691. int media;
  692. const char * version_string;
  693. const char * if_string;
  694. int i;
  695. nic->irqno = 0;
  696. nic->ioaddr = isa->ioaddr;
  697. /*
  698. * Get the MAC address ( bank 1, regs 4 - 9 )
  699. */
  700. SMC_SELECT_BANK(nic->ioaddr, 1);
  701. for ( i = 0; i < 6; i += 2 ) {
  702. word address;
  703. address = inw(nic->ioaddr + ADDR0 + i);
  704. nic->node_addr[i+1] = address >> 8;
  705. nic->node_addr[i] = address & 0xFF;
  706. }
  707. /* get the memory information */
  708. SMC_SELECT_BANK(nic->ioaddr, 0);
  709. memory = ( inw(nic->ioaddr + MCR) >> 9 ) & 0x7; /* multiplier */
  710. memory *= 256 * (inw(nic->ioaddr + MIR) & 0xFF);
  711. /*
  712. * Now, I want to find out more about the chip. This is sort of
  713. * redundant, but it's cleaner to have it in both, rather than having
  714. * one VERY long probe procedure.
  715. */
  716. SMC_SELECT_BANK(nic->ioaddr, 3);
  717. revision = inw(nic->ioaddr + REVISION);
  718. version_string = chip_ids[(revision >> 4) & 0xF];
  719. if (((revision & 0xF0) >> 4 == CHIP_9196) &&
  720. ((revision & 0x0F) >= REV_9196)) {
  721. /* This is a 91c96. 'c96 has the same chip id as 'c94 (4) but
  722. * a revision starting at 6 */
  723. version_string = smc91c96_id;
  724. }
  725. if ( !version_string ) {
  726. /* I shouldn't get here because this call was done before.... */
  727. return 0;
  728. }
  729. /* is it using AUI or 10BaseT ? */
  730. SMC_SELECT_BANK(nic->ioaddr, 1);
  731. if (inw(nic->ioaddr + CONFIG) & CFG_AUI_SELECT)
  732. media = 2;
  733. else
  734. media = 1;
  735. if_string = interfaces[media - 1];
  736. /* now, reset the chip, and put it into a known state */
  737. smc_reset(nic->ioaddr);
  738. printf("SMC9000 %s\n", smc9000_version);
  739. DBG("Copyright (C) 1998 Daniel Engstr\x94m\n");
  740. DBG("Copyright (C) 1996 Eric Stahlman\n");
  741. printf("%s rev:%d I/O port:%hX Interface:%s RAM:%d bytes \n",
  742. version_string, revision & 0xF,
  743. nic->ioaddr, if_string, memory );
  744. DBG ( "Ethernet MAC address: %s\n", eth_ntoa ( nic->node_addr ) );
  745. SMC_SELECT_BANK(nic->ioaddr, 0);
  746. /* see the header file for options in TCR/RCR NORMAL*/
  747. _outw(TCR_NORMAL, nic->ioaddr + TCR);
  748. _outw(RCR_NORMAL, nic->ioaddr + RCR);
  749. /* Select which interface to use */
  750. SMC_SELECT_BANK(nic->ioaddr, 1);
  751. if ( media == 1 ) {
  752. _outw( inw( nic->ioaddr + CONFIG ) & ~CFG_AUI_SELECT,
  753. nic->ioaddr + CONFIG );
  754. }
  755. else if ( media == 2 ) {
  756. _outw( inw( nic->ioaddr + CONFIG ) | CFG_AUI_SELECT,
  757. nic->ioaddr + CONFIG );
  758. }
  759. smc_phy_configure(nic->ioaddr);
  760. nic->nic_op = &smc9000_operations;
  761. return 1;
  762. }
  763. /*
  764. * The SMC9000 can be at any of the following port addresses. To
  765. * change for a slightly different card, you can add it to the array.
  766. *
  767. */
  768. static isa_probe_addr_t smc9000_probe_addrs[] = {
  769. 0x200, 0x220, 0x240, 0x260, 0x280, 0x2A0, 0x2C0, 0x2E0,
  770. 0x300, 0x320, 0x340, 0x360, 0x380, 0x3A0, 0x3C0, 0x3E0,
  771. };
  772. ISA_DRIVER ( smc9000_driver, smc9000_probe_addrs, smc9000_probe_addr,
  773. GENERIC_ISAPNP_VENDOR, 0x8228 );
  774. DRIVER ( "SMC9000", nic_driver, isa_driver, smc9000_driver,
  775. smc9000_probe, smc9000_disable );
  776. ISA_ROM ( "smc9000", "SMC9000" );
  777. /*
  778. * Local variables:
  779. * c-basic-offset: 8
  780. * c-indent-level: 8
  781. * tab-width: 8
  782. * End:
  783. */