smc91111.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473
  1. /*------------------------------------------------------------------------
  2. . smc91111.c
  3. . This is a driver for SMSC's 91C111 single-chip Ethernet device.
  4. .
  5. . (C) Copyright 2002
  6. . Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  7. . Rolf Offermanns <rof@sysgo.de>
  8. .
  9. . Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
  10. . Developed by Simple Network Magic Corporation (SNMC)
  11. . Copyright (C) 1996 by Erik Stahlman (ES)
  12. .
  13. . This program is free software; you can redistribute it and/or modify
  14. . it under the terms of the GNU General Public License as published by
  15. . the Free Software Foundation; either version 2 of the License, or
  16. . (at your option) any later version.
  17. .
  18. . This program is distributed in the hope that it will be useful,
  19. . but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. . MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. . GNU General Public License for more details.
  22. .
  23. . You should have received a copy of the GNU General Public License
  24. . along with this program; if not, write to the Free Software
  25. . Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. .
  27. . Information contained in this file was obtained from the LAN91C111
  28. . manual from SMC. To get a copy, if you really want one, you can find
  29. . information under www.smsc.com.
  30. .
  31. .
  32. . "Features" of the SMC chip:
  33. . Integrated PHY/MAC for 10/100BaseT Operation
  34. . Supports internal and external MII
  35. . Integrated 8K packet memory
  36. . EEPROM interface for configuration
  37. .
  38. . Arguments:
  39. . io = for the base address
  40. . irq = for the IRQ
  41. .
  42. . author:
  43. . Erik Stahlman ( erik@vt.edu )
  44. . Daris A Nevil ( dnevil@snmc.com )
  45. .
  46. .
  47. . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
  48. .
  49. . Sources:
  50. . o SMSC LAN91C111 databook (www.smsc.com)
  51. . o smc9194.c by Erik Stahlman
  52. . o skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov )
  53. .
  54. . History:
  55. . 06/19/03 Richard Woodruff Made u-boot environment aware and added mac addr checks.
  56. . 10/17/01 Marco Hasewinkel Modify for DNP/1110
  57. . 07/25/01 Woojung Huh Modify for ADS Bitsy
  58. . 04/25/01 Daris A Nevil Initial public release through SMSC
  59. . 03/16/01 Daris A Nevil Modified smc9194.c for use with LAN91C111
  60. ----------------------------------------------------------------------------*/
  61. #include <common.h>
  62. #include <command.h>
  63. #include "smc91111.h"
  64. #include <net.h>
  65. #ifdef CONFIG_DRIVER_SMC91111
  66. /* Use power-down feature of the chip */
  67. #define POWER_DOWN 0
  68. #define NO_AUTOPROBE
  69. static const char version[] =
  70. "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
  71. #define SMC_DEBUG 0
  72. /*------------------------------------------------------------------------
  73. .
  74. . Configuration options, for the experienced user to change.
  75. .
  76. -------------------------------------------------------------------------*/
  77. /*
  78. . Wait time for memory to be free. This probably shouldn't be
  79. . tuned that much, as waiting for this means nothing else happens
  80. . in the system
  81. */
  82. #define MEMORY_WAIT_TIME 16
  83. #if (SMC_DEBUG > 2 )
  84. #define PRINTK3(args...) printf(args)
  85. #else
  86. #define PRINTK3(args...)
  87. #endif
  88. #if SMC_DEBUG > 1
  89. #define PRINTK2(args...) printf(args)
  90. #else
  91. #define PRINTK2(args...)
  92. #endif
  93. #ifdef SMC_DEBUG
  94. #define PRINTK(args...) printf(args)
  95. #else
  96. #define PRINTK(args...)
  97. #endif
  98. /*------------------------------------------------------------------------
  99. .
  100. . The internal workings of the driver. If you are changing anything
  101. . here with the SMC stuff, you should have the datasheet and know
  102. . what you are doing.
  103. .
  104. -------------------------------------------------------------------------*/
  105. #define CARDNAME "LAN91C111"
  106. /* Memory sizing constant */
  107. #define LAN91C111_MEMORY_MULTIPLIER (1024*2)
  108. #ifndef CONFIG_SMC91111_BASE
  109. #define CONFIG_SMC91111_BASE 0x20000300
  110. #endif
  111. #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
  112. #define SMC_DEV_NAME "SMC91111"
  113. #define SMC_PHY_ADDR 0x0000
  114. #define SMC_ALLOC_MAX_TRY 5
  115. #define SMC_TX_TIMEOUT 30
  116. #define SMC_PHY_CLOCK_DELAY 1000
  117. #define ETH_ZLEN 60
  118. #ifdef CONFIG_SMC_USE_32_BIT
  119. #define USE_32_BIT 1
  120. #else
  121. #undef USE_32_BIT
  122. #endif
  123. /*-----------------------------------------------------------------
  124. .
  125. . The driver can be entered at any of the following entry points.
  126. .
  127. .------------------------------------------------------------------ */
  128. extern int eth_init(bd_t *bd);
  129. extern void eth_halt(void);
  130. extern int eth_rx(void);
  131. extern int eth_send(volatile void *packet, int length);
  132. /*
  133. . This is called by register_netdev(). It is responsible for
  134. . checking the portlist for the SMC9000 series chipset. If it finds
  135. . one, then it will initialize the device, find the hardware information,
  136. . and sets up the appropriate device parameters.
  137. . NOTE: Interrupts are *OFF* when this procedure is called.
  138. .
  139. . NB:This shouldn't be static since it is referred to externally.
  140. */
  141. int smc_init(void);
  142. /*
  143. . This is called by unregister_netdev(). It is responsible for
  144. . cleaning up before the driver is finally unregistered and discarded.
  145. */
  146. void smc_destructor(void);
  147. /*
  148. . The kernel calls this function when someone wants to use the device,
  149. . typically 'ifconfig ethX up'.
  150. */
  151. static int smc_open(bd_t *bd);
  152. /*
  153. . This is called by the kernel in response to 'ifconfig ethX down'. It
  154. . is responsible for cleaning up everything that the open routine
  155. . does, and maybe putting the card into a powerdown state.
  156. */
  157. static int smc_close(void);
  158. /*
  159. . Configures the PHY through the MII Management interface
  160. */
  161. #ifndef CONFIG_SMC91111_EXT_PHY
  162. static void smc_phy_configure(void);
  163. #endif /* !CONFIG_SMC91111_EXT_PHY */
  164. /*
  165. . This is a separate procedure to handle the receipt of a packet, to
  166. . leave the interrupt code looking slightly cleaner
  167. */
  168. static int smc_rcv(void);
  169. /* See if a MAC address is defined in the current environment. If so use it. If not
  170. . print a warning and set the environment and other globals with the default.
  171. . If an EEPROM is present it really should be consulted.
  172. */
  173. int smc_get_ethaddr(bd_t *bd);
  174. int get_rom_mac(char *v_rom_mac);
  175. /*
  176. ------------------------------------------------------------
  177. .
  178. . Internal routines
  179. .
  180. ------------------------------------------------------------
  181. */
  182. static char smc_mac_addr[] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
  183. /*
  184. * This function must be called before smc_open() if you want to override
  185. * the default mac address.
  186. */
  187. void smc_set_mac_addr(const char *addr) {
  188. int i;
  189. for (i=0; i < sizeof(smc_mac_addr); i++){
  190. smc_mac_addr[i] = addr[i];
  191. }
  192. }
  193. /*
  194. * smc_get_macaddr is no longer used. If you want to override the default
  195. * mac address, call smc_get_mac_addr as a part of the board initialization.
  196. */
  197. #if 0
  198. void smc_get_macaddr( byte *addr ) {
  199. /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
  200. unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
  201. int i;
  202. for (i=0; i<6; i++) {
  203. addr[0] = *(dnp1110_mac+0);
  204. addr[1] = *(dnp1110_mac+1);
  205. addr[2] = *(dnp1110_mac+2);
  206. addr[3] = *(dnp1110_mac+3);
  207. addr[4] = *(dnp1110_mac+4);
  208. addr[5] = *(dnp1110_mac+5);
  209. }
  210. }
  211. #endif /* 0 */
  212. /***********************************************
  213. * Show available memory *
  214. ***********************************************/
  215. void dump_memory_info(void)
  216. {
  217. word mem_info;
  218. word old_bank;
  219. old_bank = SMC_inw(BANK_SELECT)&0xF;
  220. SMC_SELECT_BANK(0);
  221. mem_info = SMC_inw( MIR_REG );
  222. PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
  223. SMC_SELECT_BANK(old_bank);
  224. }
  225. /*
  226. . A rather simple routine to print out a packet for debugging purposes.
  227. */
  228. #if SMC_DEBUG > 2
  229. static void print_packet( byte *, int );
  230. #endif
  231. #define tx_done(dev) 1
  232. /* this does a soft reset on the device */
  233. static void smc_reset( void );
  234. /* Enable Interrupts, Receive, and Transmit */
  235. static void smc_enable( void );
  236. /* this puts the device in an inactive state */
  237. static void smc_shutdown( void );
  238. /* Routines to Read and Write the PHY Registers across the
  239. MII Management Interface
  240. */
  241. #ifndef CONFIG_SMC91111_EXT_PHY
  242. static word smc_read_phy_register(byte phyreg);
  243. static void smc_write_phy_register(byte phyreg, word phydata);
  244. #endif /* !CONFIG_SMC91111_EXT_PHY */
  245. static int poll4int( byte mask, int timeout ) {
  246. int tmo = get_timer(0) + timeout * CFG_HZ;
  247. int is_timeout = 0;
  248. word old_bank = SMC_inw(BSR_REG);
  249. PRINTK2("Polling...\n");
  250. SMC_SELECT_BANK(2);
  251. while((SMC_inw(SMC91111_INT_REG) & mask) == 0)
  252. {
  253. if (get_timer(0) >= tmo) {
  254. is_timeout = 1;
  255. break;
  256. }
  257. }
  258. /* restore old bank selection */
  259. SMC_SELECT_BANK(old_bank);
  260. if (is_timeout)
  261. return 1;
  262. else
  263. return 0;
  264. }
  265. /* Only one release command at a time, please */
  266. static inline void smc_wait_mmu_release_complete(void)
  267. {
  268. int count = 0;
  269. /* assume bank 2 selected */
  270. while ( SMC_inw(MMU_CMD_REG) & MC_BUSY ) {
  271. udelay(1); /* Wait until not busy */
  272. if( ++count > 200) break;
  273. }
  274. }
  275. /*
  276. . Function: smc_reset( void )
  277. . Purpose:
  278. . This sets the SMC91111 chip to its normal state, hopefully from whatever
  279. . mess that any other DOS driver has put it in.
  280. .
  281. . Maybe I should reset more registers to defaults in here? SOFTRST should
  282. . do that for me.
  283. .
  284. . Method:
  285. . 1. send a SOFT RESET
  286. . 2. wait for it to finish
  287. . 3. enable autorelease mode
  288. . 4. reset the memory management unit
  289. . 5. clear all interrupts
  290. .
  291. */
  292. static void smc_reset( void )
  293. {
  294. PRINTK2("%s:smc_reset\n", SMC_DEV_NAME);
  295. /* This resets the registers mostly to defaults, but doesn't
  296. affect EEPROM. That seems unnecessary */
  297. SMC_SELECT_BANK( 0 );
  298. SMC_outw( RCR_SOFTRST, RCR_REG );
  299. /* Setup the Configuration Register */
  300. /* This is necessary because the CONFIG_REG is not affected */
  301. /* by a soft reset */
  302. SMC_SELECT_BANK( 1 );
  303. #if defined(CONFIG_SMC91111_EXT_PHY)
  304. SMC_outw( CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
  305. #else
  306. SMC_outw( CONFIG_DEFAULT, CONFIG_REG);
  307. #endif
  308. /* Release from possible power-down state */
  309. /* Configuration register is not affected by Soft Reset */
  310. SMC_outw( SMC_inw( CONFIG_REG ) | CONFIG_EPH_POWER_EN, CONFIG_REG );
  311. SMC_SELECT_BANK( 0 );
  312. /* this should pause enough for the chip to be happy */
  313. udelay(10);
  314. /* Disable transmit and receive functionality */
  315. SMC_outw( RCR_CLEAR, RCR_REG );
  316. SMC_outw( TCR_CLEAR, TCR_REG );
  317. /* set the control register */
  318. SMC_SELECT_BANK( 1 );
  319. SMC_outw( CTL_DEFAULT, CTL_REG );
  320. /* Reset the MMU */
  321. SMC_SELECT_BANK( 2 );
  322. smc_wait_mmu_release_complete();
  323. SMC_outw( MC_RESET, MMU_CMD_REG );
  324. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  325. udelay(1); /* Wait until not busy */
  326. /* Note: It doesn't seem that waiting for the MMU busy is needed here,
  327. but this is a place where future chipsets _COULD_ break. Be wary
  328. of issuing another MMU command right after this */
  329. /* Disable all interrupts */
  330. SMC_outb( 0, IM_REG );
  331. }
  332. /*
  333. . Function: smc_enable
  334. . Purpose: let the chip talk to the outside work
  335. . Method:
  336. . 1. Enable the transmitter
  337. . 2. Enable the receiver
  338. . 3. Enable interrupts
  339. */
  340. static void smc_enable()
  341. {
  342. PRINTK2("%s:smc_enable\n", SMC_DEV_NAME);
  343. SMC_SELECT_BANK( 0 );
  344. /* see the header file for options in TCR/RCR DEFAULT*/
  345. SMC_outw( TCR_DEFAULT, TCR_REG );
  346. SMC_outw( RCR_DEFAULT, RCR_REG );
  347. /* clear MII_DIS */
  348. /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
  349. }
  350. /*
  351. . Function: smc_shutdown
  352. . Purpose: closes down the SMC91xxx chip.
  353. . Method:
  354. . 1. zero the interrupt mask
  355. . 2. clear the enable receive flag
  356. . 3. clear the enable xmit flags
  357. .
  358. . TODO:
  359. . (1) maybe utilize power down mode.
  360. . Why not yet? Because while the chip will go into power down mode,
  361. . the manual says that it will wake up in response to any I/O requests
  362. . in the register space. Empirical results do not show this working.
  363. */
  364. static void smc_shutdown()
  365. {
  366. PRINTK2(CARDNAME ":smc_shutdown\n");
  367. /* no more interrupts for me */
  368. SMC_SELECT_BANK( 2 );
  369. SMC_outb( 0, IM_REG );
  370. /* and tell the card to stay away from that nasty outside world */
  371. SMC_SELECT_BANK( 0 );
  372. SMC_outb( RCR_CLEAR, RCR_REG );
  373. SMC_outb( TCR_CLEAR, TCR_REG );
  374. }
  375. /*
  376. . Function: smc_hardware_send_packet(struct net_device * )
  377. . Purpose:
  378. . This sends the actual packet to the SMC9xxx chip.
  379. .
  380. . Algorithm:
  381. . First, see if a saved_skb is available.
  382. . ( this should NOT be called if there is no 'saved_skb'
  383. . Now, find the packet number that the chip allocated
  384. . Point the data pointers at it in memory
  385. . Set the length word in the chip's memory
  386. . Dump the packet to chip memory
  387. . Check if a last byte is needed ( odd length packet )
  388. . if so, set the control flag right
  389. . Tell the card to send it
  390. . Enable the transmit interrupt, so I know if it failed
  391. . Free the kernel data if I actually sent it.
  392. */
  393. static int smc_send_packet(volatile void *packet, int packet_length)
  394. {
  395. byte packet_no;
  396. unsigned long ioaddr;
  397. byte * buf;
  398. int length;
  399. int numPages;
  400. int try = 0;
  401. int time_out;
  402. byte status;
  403. PRINTK3("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
  404. length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
  405. /* allocate memory
  406. ** The MMU wants the number of pages to be the number of 256 bytes
  407. ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
  408. **
  409. ** The 91C111 ignores the size bits, but the code is left intact
  410. ** for backwards and future compatibility.
  411. **
  412. ** Pkt size for allocating is data length +6 (for additional status
  413. ** words, length and ctl!)
  414. **
  415. ** If odd size then last byte is included in this header.
  416. */
  417. numPages = ((length & 0xfffe) + 6);
  418. numPages >>= 8; /* Divide by 256 */
  419. if (numPages > 7 ) {
  420. printf("%s: Far too big packet error. \n", SMC_DEV_NAME);
  421. return 0;
  422. }
  423. /* now, try to allocate the memory */
  424. SMC_SELECT_BANK( 2 );
  425. SMC_outw( MC_ALLOC | numPages, MMU_CMD_REG );
  426. /* FIXME: the ALLOC_INT bit never gets set *
  427. * so the following will always give a *
  428. * memory allocation error. *
  429. * same code works in armboot though *
  430. * -ro
  431. */
  432. again:
  433. try++;
  434. time_out = MEMORY_WAIT_TIME;
  435. do {
  436. status = SMC_inb( SMC91111_INT_REG );
  437. if ( status & IM_ALLOC_INT ) {
  438. /* acknowledge the interrupt */
  439. SMC_outb( IM_ALLOC_INT, SMC91111_INT_REG );
  440. break;
  441. }
  442. } while ( -- time_out );
  443. if ( !time_out ) {
  444. PRINTK2("%s: memory allocation, try %d failed ...\n",
  445. SMC_DEV_NAME, try);
  446. if (try < SMC_ALLOC_MAX_TRY)
  447. goto again;
  448. else
  449. return 0;
  450. }
  451. PRINTK2("%s: memory allocation, try %d succeeded ...\n",
  452. SMC_DEV_NAME,
  453. try);
  454. /* I can send the packet now.. */
  455. ioaddr = SMC_BASE_ADDRESS;
  456. buf = (byte *)packet;
  457. /* If I get here, I _know_ there is a packet slot waiting for me */
  458. packet_no = SMC_inb( AR_REG );
  459. if ( packet_no & AR_FAILED ) {
  460. /* or isn't there? BAD CHIP! */
  461. printf("%s: Memory allocation failed. \n",
  462. SMC_DEV_NAME);
  463. return 0;
  464. }
  465. /* we have a packet address, so tell the card to use it */
  466. SMC_outb( packet_no, PN_REG );
  467. /* point to the beginning of the packet */
  468. SMC_outw( PTR_AUTOINC , PTR_REG );
  469. PRINTK3("%s: Trying to xmit packet of length %x\n",
  470. SMC_DEV_NAME, length);
  471. #if SMC_DEBUG > 2
  472. printf("Transmitting Packet\n");
  473. print_packet( buf, length );
  474. #endif
  475. /* send the packet length ( +6 for status, length and ctl byte )
  476. and the status word ( set to zeros ) */
  477. #ifdef USE_32_BIT
  478. SMC_outl( (length +6 ) << 16 , SMC91111_DATA_REG );
  479. #else
  480. SMC_outw( 0, SMC91111_DATA_REG );
  481. /* send the packet length ( +6 for status words, length, and ctl*/
  482. SMC_outw( (length+6), SMC91111_DATA_REG );
  483. #endif
  484. /* send the actual data
  485. . I _think_ it's faster to send the longs first, and then
  486. . mop up by sending the last word. It depends heavily
  487. . on alignment, at least on the 486. Maybe it would be
  488. . a good idea to check which is optimal? But that could take
  489. . almost as much time as is saved?
  490. */
  491. #ifdef USE_32_BIT
  492. SMC_outsl(SMC91111_DATA_REG, buf, length >> 2 );
  493. if ( length & 0x2 )
  494. SMC_outw(*((word *)(buf + (length & 0xFFFFFFFC))), SMC91111_DATA_REG);
  495. #else
  496. SMC_outsw(SMC91111_DATA_REG , buf, (length ) >> 1);
  497. #endif /* USE_32_BIT */
  498. /* Send the last byte, if there is one. */
  499. if ( (length & 1) == 0 ) {
  500. SMC_outw( 0, SMC91111_DATA_REG );
  501. } else {
  502. SMC_outw( buf[length -1 ] | 0x2000, SMC91111_DATA_REG );
  503. }
  504. /* and let the chipset deal with it */
  505. SMC_outw( MC_ENQUEUE , MMU_CMD_REG );
  506. /* poll for TX INT */
  507. if (poll4int(IM_TX_INT, SMC_TX_TIMEOUT)) {
  508. /* sending failed */
  509. PRINTK2("%s: TX timeout, sending failed...\n",
  510. SMC_DEV_NAME);
  511. /* release packet */
  512. SMC_outw(MC_FREEPKT, MMU_CMD_REG);
  513. /* wait for MMU getting ready (low) */
  514. while (SMC_inw(MMU_CMD_REG) & MC_BUSY)
  515. {
  516. udelay(10);
  517. }
  518. PRINTK2("MMU ready\n");
  519. return 0;
  520. } else {
  521. /* ack. int */
  522. SMC_outw(IM_TX_INT, SMC91111_INT_REG);
  523. PRINTK2("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
  524. /* release packet */
  525. SMC_outw(MC_FREEPKT, MMU_CMD_REG);
  526. /* wait for MMU getting ready (low) */
  527. while (SMC_inw(MMU_CMD_REG) & MC_BUSY)
  528. {
  529. udelay(10);
  530. }
  531. PRINTK2("MMU ready\n");
  532. }
  533. return length;
  534. }
  535. /*-------------------------------------------------------------------------
  536. |
  537. | smc_destructor( struct net_device * dev )
  538. | Input parameters:
  539. | dev, pointer to the device structure
  540. |
  541. | Output:
  542. | None.
  543. |
  544. ---------------------------------------------------------------------------
  545. */
  546. void smc_destructor()
  547. {
  548. PRINTK2(CARDNAME ":smc_destructor\n");
  549. }
  550. /*
  551. * Open and Initialize the board
  552. *
  553. * Set up everything, reset the card, etc ..
  554. *
  555. */
  556. static int smc_open(bd_t *bd)
  557. {
  558. int i, err;
  559. PRINTK2("%s:smc_open\n", SMC_DEV_NAME);
  560. /* reset the hardware */
  561. smc_reset();
  562. smc_enable();
  563. /* Configure the PHY */
  564. #ifndef CONFIG_SMC91111_EXT_PHY
  565. smc_phy_configure();
  566. #endif
  567. /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
  568. /* SMC_SELECT_BANK(0); */
  569. /* SMC_outw(0, RPC_REG); */
  570. SMC_SELECT_BANK(1);
  571. err = smc_get_ethaddr(bd); /* set smc_mac_addr, and sync it with u-boot globals */
  572. if(err < 0){
  573. memset(bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set*/
  574. return(-1); /* upper code ignores this, but NOT bi_enetaddr */
  575. }
  576. #ifdef USE_32_BIT
  577. for ( i = 0; i < 6; i += 2 ) {
  578. word address;
  579. address = smc_mac_addr[ i + 1 ] << 8 ;
  580. address |= smc_mac_addr[ i ];
  581. SMC_outw( address, ADDR0_REG + i );
  582. }
  583. #else
  584. for ( i = 0; i < 6; i ++ )
  585. SMC_outb( smc_mac_addr[i], ADDR0_REG + i );
  586. #endif
  587. return 0;
  588. }
  589. #if 0 /* dead code? -- wd */
  590. #ifdef USE_32_BIT
  591. void
  592. insl32(r,b,l)
  593. {
  594. int __i ;
  595. dword *__b2;
  596. __b2 = (dword *) b;
  597. for (__i = 0; __i < l; __i++) {
  598. *(__b2 + __i) = *(dword *)(r+0x10000300);
  599. }
  600. }
  601. #endif
  602. #endif
  603. /*-------------------------------------------------------------
  604. .
  605. . smc_rcv - receive a packet from the card
  606. .
  607. . There is ( at least ) a packet waiting to be read from
  608. . chip-memory.
  609. .
  610. . o Read the status
  611. . o If an error, record it
  612. . o otherwise, read in the packet
  613. --------------------------------------------------------------
  614. */
  615. static int smc_rcv()
  616. {
  617. int packet_number;
  618. word status;
  619. word packet_length;
  620. int is_error = 0;
  621. #ifdef USE_32_BIT
  622. dword stat_len;
  623. #endif
  624. SMC_SELECT_BANK(2);
  625. packet_number = SMC_inw( RXFIFO_REG );
  626. if ( packet_number & RXFIFO_REMPTY ) {
  627. return 0;
  628. }
  629. PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME);
  630. /* start reading from the start of the packet */
  631. SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
  632. /* First two words are status and packet_length */
  633. #ifdef USE_32_BIT
  634. stat_len = SMC_inl(SMC91111_DATA_REG);
  635. status = stat_len & 0xffff;
  636. packet_length = stat_len >> 16;
  637. #else
  638. status = SMC_inw( SMC91111_DATA_REG );
  639. packet_length = SMC_inw( SMC91111_DATA_REG );
  640. #endif
  641. packet_length &= 0x07ff; /* mask off top bits */
  642. PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
  643. if ( !(status & RS_ERRORS ) ){
  644. /* Adjust for having already read the first two words */
  645. packet_length -= 4; /*4; */
  646. /* set odd length for bug in LAN91C111, */
  647. /* which never sets RS_ODDFRAME */
  648. /* TODO ? */
  649. #ifdef USE_32_BIT
  650. PRINTK3(" Reading %d dwords (and %d bytes) \n",
  651. packet_length >> 2, packet_length & 3 );
  652. /* QUESTION: Like in the TX routine, do I want
  653. to send the DWORDs or the bytes first, or some
  654. mixture. A mixture might improve already slow PIO
  655. performance */
  656. SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
  657. /* read the left over bytes */
  658. if (packet_length & 3) {
  659. int i;
  660. byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
  661. dword leftover = SMC_inl(SMC91111_DATA_REG);
  662. for (i=0; i<(packet_length & 3); i++)
  663. *tail++ = (byte) (leftover >> (8*i)) & 0xff;
  664. }
  665. #else
  666. PRINTK3(" Reading %d words and %d byte(s) \n",
  667. (packet_length >> 1 ), packet_length & 1 );
  668. SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
  669. #endif /* USE_32_BIT */
  670. #if SMC_DEBUG > 2
  671. printf("Receiving Packet\n");
  672. print_packet( NetRxPackets[0], packet_length );
  673. #endif
  674. } else {
  675. /* error ... */
  676. /* TODO ? */
  677. is_error = 1;
  678. }
  679. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  680. udelay(1); /* Wait until not busy */
  681. /* error or good, tell the card to get rid of this packet */
  682. SMC_outw( MC_RELEASE, MMU_CMD_REG );
  683. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  684. udelay(1); /* Wait until not busy */
  685. if (!is_error) {
  686. /* Pass the packet up to the protocol layers. */
  687. NetReceive(NetRxPackets[0], packet_length);
  688. return packet_length;
  689. } else {
  690. return 0;
  691. }
  692. }
  693. /*----------------------------------------------------
  694. . smc_close
  695. .
  696. . this makes the board clean up everything that it can
  697. . and not talk to the outside world. Caused by
  698. . an 'ifconfig ethX down'
  699. .
  700. -----------------------------------------------------*/
  701. static int smc_close()
  702. {
  703. PRINTK2("%s:smc_close\n", SMC_DEV_NAME);
  704. /* clear everything */
  705. smc_shutdown();
  706. return 0;
  707. }
  708. #if 0
  709. /*------------------------------------------------------------
  710. . Modify a bit in the LAN91C111 register set
  711. .-------------------------------------------------------------*/
  712. static word smc_modify_regbit(int bank, int ioaddr, int reg,
  713. unsigned int bit, int val)
  714. {
  715. word regval;
  716. SMC_SELECT_BANK( bank );
  717. regval = SMC_inw( reg );
  718. if (val)
  719. regval |= bit;
  720. else
  721. regval &= ~bit;
  722. SMC_outw( regval, 0 );
  723. return(regval);
  724. }
  725. /*------------------------------------------------------------
  726. . Retrieve a bit in the LAN91C111 register set
  727. .-------------------------------------------------------------*/
  728. static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
  729. {
  730. SMC_SELECT_BANK( bank );
  731. if ( SMC_inw( reg ) & bit)
  732. return(1);
  733. else
  734. return(0);
  735. }
  736. /*------------------------------------------------------------
  737. . Modify a LAN91C111 register (word access only)
  738. .-------------------------------------------------------------*/
  739. static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
  740. {
  741. SMC_SELECT_BANK( bank );
  742. SMC_outw( val, reg );
  743. }
  744. /*------------------------------------------------------------
  745. . Retrieve a LAN91C111 register (word access only)
  746. .-------------------------------------------------------------*/
  747. static int smc_get_reg(int bank, int ioaddr, int reg)
  748. {
  749. SMC_SELECT_BANK( bank );
  750. return(SMC_inw( reg ));
  751. }
  752. #endif /* 0 */
  753. /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
  754. #if (SMC_DEBUG > 2 )
  755. /*------------------------------------------------------------
  756. . Debugging function for viewing MII Management serial bitstream
  757. .-------------------------------------------------------------*/
  758. static void smc_dump_mii_stream(byte* bits, int size)
  759. {
  760. int i;
  761. printf("BIT#:");
  762. for (i = 0; i < size; ++i)
  763. {
  764. printf("%d", i%10);
  765. }
  766. printf("\nMDOE:");
  767. for (i = 0; i < size; ++i)
  768. {
  769. if (bits[i] & MII_MDOE)
  770. printf("1");
  771. else
  772. printf("0");
  773. }
  774. printf("\nMDO :");
  775. for (i = 0; i < size; ++i)
  776. {
  777. if (bits[i] & MII_MDO)
  778. printf("1");
  779. else
  780. printf("0");
  781. }
  782. printf("\nMDI :");
  783. for (i = 0; i < size; ++i)
  784. {
  785. if (bits[i] & MII_MDI)
  786. printf("1");
  787. else
  788. printf("0");
  789. }
  790. printf("\n");
  791. }
  792. #endif
  793. /*------------------------------------------------------------
  794. . Reads a register from the MII Management serial interface
  795. .-------------------------------------------------------------*/
  796. #ifndef CONFIG_SMC91111_EXT_PHY
  797. static word smc_read_phy_register(byte phyreg)
  798. {
  799. int oldBank;
  800. int i;
  801. byte mask;
  802. word mii_reg;
  803. byte bits[64];
  804. int clk_idx = 0;
  805. int input_idx;
  806. word phydata;
  807. byte phyaddr = SMC_PHY_ADDR;
  808. /* 32 consecutive ones on MDO to establish sync */
  809. for (i = 0; i < 32; ++i)
  810. bits[clk_idx++] = MII_MDOE | MII_MDO;
  811. /* Start code <01> */
  812. bits[clk_idx++] = MII_MDOE;
  813. bits[clk_idx++] = MII_MDOE | MII_MDO;
  814. /* Read command <10> */
  815. bits[clk_idx++] = MII_MDOE | MII_MDO;
  816. bits[clk_idx++] = MII_MDOE;
  817. /* Output the PHY address, msb first */
  818. mask = (byte)0x10;
  819. for (i = 0; i < 5; ++i)
  820. {
  821. if (phyaddr & mask)
  822. bits[clk_idx++] = MII_MDOE | MII_MDO;
  823. else
  824. bits[clk_idx++] = MII_MDOE;
  825. /* Shift to next lowest bit */
  826. mask >>= 1;
  827. }
  828. /* Output the phy register number, msb first */
  829. mask = (byte)0x10;
  830. for (i = 0; i < 5; ++i)
  831. {
  832. if (phyreg & mask)
  833. bits[clk_idx++] = MII_MDOE | MII_MDO;
  834. else
  835. bits[clk_idx++] = MII_MDOE;
  836. /* Shift to next lowest bit */
  837. mask >>= 1;
  838. }
  839. /* Tristate and turnaround (2 bit times) */
  840. bits[clk_idx++] = 0;
  841. /*bits[clk_idx++] = 0; */
  842. /* Input starts at this bit time */
  843. input_idx = clk_idx;
  844. /* Will input 16 bits */
  845. for (i = 0; i < 16; ++i)
  846. bits[clk_idx++] = 0;
  847. /* Final clock bit */
  848. bits[clk_idx++] = 0;
  849. /* Save the current bank */
  850. oldBank = SMC_inw( BANK_SELECT );
  851. /* Select bank 3 */
  852. SMC_SELECT_BANK( 3 );
  853. /* Get the current MII register value */
  854. mii_reg = SMC_inw( MII_REG );
  855. /* Turn off all MII Interface bits */
  856. mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
  857. /* Clock all 64 cycles */
  858. for (i = 0; i < sizeof bits; ++i)
  859. {
  860. /* Clock Low - output data */
  861. SMC_outw( mii_reg | bits[i], MII_REG );
  862. udelay(SMC_PHY_CLOCK_DELAY);
  863. /* Clock Hi - input data */
  864. SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG );
  865. udelay(SMC_PHY_CLOCK_DELAY);
  866. bits[i] |= SMC_inw( MII_REG ) & MII_MDI;
  867. }
  868. /* Return to idle state */
  869. /* Set clock to low, data to low, and output tristated */
  870. SMC_outw( mii_reg, MII_REG );
  871. udelay(SMC_PHY_CLOCK_DELAY);
  872. /* Restore original bank select */
  873. SMC_SELECT_BANK( oldBank );
  874. /* Recover input data */
  875. phydata = 0;
  876. for (i = 0; i < 16; ++i)
  877. {
  878. phydata <<= 1;
  879. if (bits[input_idx++] & MII_MDI)
  880. phydata |= 0x0001;
  881. }
  882. #if (SMC_DEBUG > 2 )
  883. printf("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  884. phyaddr, phyreg, phydata);
  885. smc_dump_mii_stream(bits, sizeof bits);
  886. #endif
  887. return(phydata);
  888. }
  889. /*------------------------------------------------------------
  890. . Writes a register to the MII Management serial interface
  891. .-------------------------------------------------------------*/
  892. static void smc_write_phy_register(byte phyreg, word phydata)
  893. {
  894. int oldBank;
  895. int i;
  896. word mask;
  897. word mii_reg;
  898. byte bits[65];
  899. int clk_idx = 0;
  900. byte phyaddr = SMC_PHY_ADDR;
  901. /* 32 consecutive ones on MDO to establish sync */
  902. for (i = 0; i < 32; ++i)
  903. bits[clk_idx++] = MII_MDOE | MII_MDO;
  904. /* Start code <01> */
  905. bits[clk_idx++] = MII_MDOE;
  906. bits[clk_idx++] = MII_MDOE | MII_MDO;
  907. /* Write command <01> */
  908. bits[clk_idx++] = MII_MDOE;
  909. bits[clk_idx++] = MII_MDOE | MII_MDO;
  910. /* Output the PHY address, msb first */
  911. mask = (byte)0x10;
  912. for (i = 0; i < 5; ++i)
  913. {
  914. if (phyaddr & mask)
  915. bits[clk_idx++] = MII_MDOE | MII_MDO;
  916. else
  917. bits[clk_idx++] = MII_MDOE;
  918. /* Shift to next lowest bit */
  919. mask >>= 1;
  920. }
  921. /* Output the phy register number, msb first */
  922. mask = (byte)0x10;
  923. for (i = 0; i < 5; ++i)
  924. {
  925. if (phyreg & mask)
  926. bits[clk_idx++] = MII_MDOE | MII_MDO;
  927. else
  928. bits[clk_idx++] = MII_MDOE;
  929. /* Shift to next lowest bit */
  930. mask >>= 1;
  931. }
  932. /* Tristate and turnaround (2 bit times) */
  933. bits[clk_idx++] = 0;
  934. bits[clk_idx++] = 0;
  935. /* Write out 16 bits of data, msb first */
  936. mask = 0x8000;
  937. for (i = 0; i < 16; ++i)
  938. {
  939. if (phydata & mask)
  940. bits[clk_idx++] = MII_MDOE | MII_MDO;
  941. else
  942. bits[clk_idx++] = MII_MDOE;
  943. /* Shift to next lowest bit */
  944. mask >>= 1;
  945. }
  946. /* Final clock bit (tristate) */
  947. bits[clk_idx++] = 0;
  948. /* Save the current bank */
  949. oldBank = SMC_inw( BANK_SELECT );
  950. /* Select bank 3 */
  951. SMC_SELECT_BANK( 3 );
  952. /* Get the current MII register value */
  953. mii_reg = SMC_inw( MII_REG );
  954. /* Turn off all MII Interface bits */
  955. mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO);
  956. /* Clock all cycles */
  957. for (i = 0; i < sizeof bits; ++i)
  958. {
  959. /* Clock Low - output data */
  960. SMC_outw( mii_reg | bits[i], MII_REG );
  961. udelay(SMC_PHY_CLOCK_DELAY);
  962. /* Clock Hi - input data */
  963. SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG );
  964. udelay(SMC_PHY_CLOCK_DELAY);
  965. bits[i] |= SMC_inw( MII_REG ) & MII_MDI;
  966. }
  967. /* Return to idle state */
  968. /* Set clock to low, data to low, and output tristated */
  969. SMC_outw( mii_reg, MII_REG );
  970. udelay(SMC_PHY_CLOCK_DELAY);
  971. /* Restore original bank select */
  972. SMC_SELECT_BANK( oldBank );
  973. #if (SMC_DEBUG > 2 )
  974. printf("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  975. phyaddr, phyreg, phydata);
  976. smc_dump_mii_stream(bits, sizeof bits);
  977. #endif
  978. }
  979. #endif /* !CONFIG_SMC91111_EXT_PHY */
  980. /*------------------------------------------------------------
  981. . Waits the specified number of milliseconds - kernel friendly
  982. .-------------------------------------------------------------*/
  983. #ifndef CONFIG_SMC91111_EXT_PHY
  984. static void smc_wait_ms(unsigned int ms)
  985. {
  986. udelay(ms*1000);
  987. }
  988. #endif /* !CONFIG_SMC91111_EXT_PHY */
  989. /*------------------------------------------------------------
  990. . Configures the specified PHY using Autonegotiation. Calls
  991. . smc_phy_fixed() if the user has requested a certain config.
  992. .-------------------------------------------------------------*/
  993. #ifndef CONFIG_SMC91111_EXT_PHY
  994. static void smc_phy_configure()
  995. {
  996. int timeout;
  997. byte phyaddr;
  998. word my_phy_caps; /* My PHY capabilities */
  999. word my_ad_caps; /* My Advertised capabilities */
  1000. word status = 0; /*;my status = 0 */
  1001. int failed = 0;
  1002. PRINTK3("%s:smc_program_phy()\n", SMC_DEV_NAME);
  1003. /* Get the detected phy address */
  1004. phyaddr = SMC_PHY_ADDR;
  1005. /* Reset the PHY, setting all other bits to zero */
  1006. smc_write_phy_register(PHY_CNTL_REG, PHY_CNTL_RST);
  1007. /* Wait for the reset to complete, or time out */
  1008. timeout = 6; /* Wait up to 3 seconds */
  1009. while (timeout--)
  1010. {
  1011. if (!(smc_read_phy_register(PHY_CNTL_REG)
  1012. & PHY_CNTL_RST))
  1013. {
  1014. /* reset complete */
  1015. break;
  1016. }
  1017. smc_wait_ms(500); /* wait 500 millisecs */
  1018. }
  1019. if (timeout < 1)
  1020. {
  1021. printf("%s:PHY reset timed out\n", SMC_DEV_NAME);
  1022. goto smc_phy_configure_exit;
  1023. }
  1024. /* Read PHY Register 18, Status Output */
  1025. /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
  1026. /* Enable PHY Interrupts (for register 18) */
  1027. /* Interrupts listed here are disabled */
  1028. smc_write_phy_register(PHY_INT_REG, 0xffff);
  1029. /* Configure the Receive/Phy Control register */
  1030. SMC_SELECT_BANK( 0 );
  1031. SMC_outw( RPC_DEFAULT, RPC_REG );
  1032. /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
  1033. my_phy_caps = smc_read_phy_register(PHY_STAT_REG);
  1034. my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
  1035. if (my_phy_caps & PHY_STAT_CAP_T4)
  1036. my_ad_caps |= PHY_AD_T4;
  1037. if (my_phy_caps & PHY_STAT_CAP_TXF)
  1038. my_ad_caps |= PHY_AD_TX_FDX;
  1039. if (my_phy_caps & PHY_STAT_CAP_TXH)
  1040. my_ad_caps |= PHY_AD_TX_HDX;
  1041. if (my_phy_caps & PHY_STAT_CAP_TF)
  1042. my_ad_caps |= PHY_AD_10_FDX;
  1043. if (my_phy_caps & PHY_STAT_CAP_TH)
  1044. my_ad_caps |= PHY_AD_10_HDX;
  1045. /* Update our Auto-Neg Advertisement Register */
  1046. smc_write_phy_register( PHY_AD_REG, my_ad_caps);
  1047. PRINTK2("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
  1048. PRINTK2("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
  1049. /* Restart auto-negotiation process in order to advertise my caps */
  1050. smc_write_phy_register( PHY_CNTL_REG,
  1051. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST );
  1052. /* Wait for the auto-negotiation to complete. This may take from */
  1053. /* 2 to 3 seconds. */
  1054. /* Wait for the reset to complete, or time out */
  1055. timeout = 20; /* Wait up to 10 seconds */
  1056. while (timeout--)
  1057. {
  1058. status = smc_read_phy_register( PHY_STAT_REG);
  1059. if (status & PHY_STAT_ANEG_ACK)
  1060. {
  1061. /* auto-negotiate complete */
  1062. break;
  1063. }
  1064. smc_wait_ms(500); /* wait 500 millisecs */
  1065. /* Restart auto-negotiation if remote fault */
  1066. if (status & PHY_STAT_REM_FLT)
  1067. {
  1068. printf("%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1069. /* Restart auto-negotiation */
  1070. printf("%s:PHY restarting auto-negotiation\n",
  1071. SMC_DEV_NAME);
  1072. smc_write_phy_register( PHY_CNTL_REG,
  1073. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST |
  1074. PHY_CNTL_SPEED | PHY_CNTL_DPLX);
  1075. }
  1076. }
  1077. if (timeout < 1)
  1078. {
  1079. printf("%s:PHY auto-negotiate timed out\n",
  1080. SMC_DEV_NAME);
  1081. printf("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
  1082. failed = 1;
  1083. }
  1084. /* Fail if we detected an auto-negotiate remote fault */
  1085. if (status & PHY_STAT_REM_FLT)
  1086. {
  1087. printf( "%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1088. printf("%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1089. failed = 1;
  1090. }
  1091. /* Re-Configure the Receive/Phy Control register */
  1092. SMC_outw( RPC_DEFAULT, RPC_REG );
  1093. smc_phy_configure_exit:
  1094. }
  1095. #endif /* !CONFIG_SMC91111_EXT_PHY */
  1096. #if SMC_DEBUG > 2
  1097. static void print_packet( byte * buf, int length )
  1098. {
  1099. #if 0
  1100. int i;
  1101. int remainder;
  1102. int lines;
  1103. printf("Packet of length %d \n", length );
  1104. #if SMC_DEBUG > 3
  1105. lines = length / 16;
  1106. remainder = length % 16;
  1107. for ( i = 0; i < lines ; i ++ ) {
  1108. int cur;
  1109. for ( cur = 0; cur < 8; cur ++ ) {
  1110. byte a, b;
  1111. a = *(buf ++ );
  1112. b = *(buf ++ );
  1113. printf("%02x%02x ", a, b );
  1114. }
  1115. printf("\n");
  1116. }
  1117. for ( i = 0; i < remainder/2 ; i++ ) {
  1118. byte a, b;
  1119. a = *(buf ++ );
  1120. b = *(buf ++ );
  1121. printf("%02x%02x ", a, b );
  1122. }
  1123. printf("\n");
  1124. #endif
  1125. #endif
  1126. }
  1127. #endif
  1128. int eth_init(bd_t *bd) {
  1129. return (smc_open(bd));
  1130. }
  1131. void eth_halt() {
  1132. smc_close();
  1133. }
  1134. int eth_rx() {
  1135. return smc_rcv();
  1136. }
  1137. int eth_send(volatile void *packet, int length) {
  1138. return smc_send_packet(packet, length);
  1139. }
  1140. int smc_get_ethaddr(bd_t *bd)
  1141. {
  1142. int env_size, rom_valid, env_present = 0, reg;
  1143. char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
  1144. uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
  1145. env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
  1146. if ((env_size > 0) && (env_size < sizeof(es))) { /* exit if env is bad */
  1147. printf("\n*** ERROR: ethaddr is not set properly!!\n");
  1148. return(-1);
  1149. }
  1150. if(env_size > 0){
  1151. env_present = 1;
  1152. s = s_env_mac;
  1153. }
  1154. for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
  1155. v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  1156. if (s)
  1157. s = (*e) ? e + 1 : e;
  1158. }
  1159. rom_valid = get_rom_mac(v_rom_mac); /* get ROM mac value if any */
  1160. if(!env_present){ /* if NO env */
  1161. if(rom_valid){ /* but ROM is valid */
  1162. v_mac = v_rom_mac;
  1163. sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X", v_mac[0],
  1164. v_mac[1] ,v_mac[2], v_mac[3],v_mac[4], v_mac[5]) ;
  1165. setenv ("ethaddr", s_env_mac);
  1166. }else{ /* no env, bad ROM */
  1167. printf("\n*** ERROR: ethaddr is NOT set !!\n");
  1168. return(-1);
  1169. }
  1170. }else /* good env, don't care ROM */
  1171. v_mac = v_env_mac; /* always use a good env over a ROM */
  1172. if(env_present && rom_valid) /* if both env and ROM are good */
  1173. if(memcmp(v_env_mac, v_rom_mac, 6) != 0){
  1174. printf("\n*** Warning: Environment and ROM MAC addresses don't match\n");
  1175. printf("*** Using Environment MAC\n");
  1176. }
  1177. memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
  1178. smc_set_mac_addr(v_mac); /* use old function to update smc default */
  1179. return(0);
  1180. }
  1181. int get_rom_mac(char *v_rom_mac)
  1182. {
  1183. int is_rom_present = 0;
  1184. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  1185. char hw_mac_addr[] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
  1186. memcpy (v_rom_mac, hw_mac_addr, 6);
  1187. return(1);
  1188. #else
  1189. if(is_rom_present)
  1190. {
  1191. /* if eeprom contents are valid
  1192. * extract mac address into hw_mac_addr, 8 or 16 bit accesses
  1193. * memcpy (v_rom_mac, hc_mac_addr, 6);
  1194. * return(1);
  1195. */
  1196. }
  1197. memset(v_rom_mac, 0, 6);
  1198. return(0);
  1199. #endif
  1200. }
  1201. #endif /* CONFIG_DRIVER_SMC91111 */