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

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