smc91111.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  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. {
  247. int tmo = get_timer (0) + timeout * CFG_HZ;
  248. int is_timeout = 0;
  249. word old_bank = SMC_inw (BSR_REG);
  250. PRINTK2 ("Polling...\n");
  251. SMC_SELECT_BANK (2);
  252. while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
  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)
  273. break;
  274. }
  275. }
  276. /*
  277. . Function: smc_reset( void )
  278. . Purpose:
  279. . This sets the SMC91111 chip to its normal state, hopefully from whatever
  280. . mess that any other DOS driver has put it in.
  281. .
  282. . Maybe I should reset more registers to defaults in here? SOFTRST should
  283. . do that for me.
  284. .
  285. . Method:
  286. . 1. send a SOFT RESET
  287. . 2. wait for it to finish
  288. . 3. enable autorelease mode
  289. . 4. reset the memory management unit
  290. . 5. clear all interrupts
  291. .
  292. */
  293. static void smc_reset (void)
  294. {
  295. PRINTK2 ("%s:smc_reset\n", SMC_DEV_NAME);
  296. /* This resets the registers mostly to defaults, but doesn't
  297. affect EEPROM. That seems unnecessary */
  298. SMC_SELECT_BANK (0);
  299. SMC_outw (RCR_SOFTRST, RCR_REG);
  300. /* Setup the Configuration Register */
  301. /* This is necessary because the CONFIG_REG is not affected */
  302. /* by a soft reset */
  303. SMC_SELECT_BANK (1);
  304. #if defined(CONFIG_SMC91111_EXT_PHY)
  305. SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
  306. #else
  307. SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
  308. #endif
  309. /* Release from possible power-down state */
  310. /* Configuration register is not affected by Soft Reset */
  311. SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
  312. SMC_SELECT_BANK (0);
  313. /* this should pause enough for the chip to be happy */
  314. udelay (10);
  315. /* Disable transmit and receive functionality */
  316. SMC_outw (RCR_CLEAR, RCR_REG);
  317. SMC_outw (TCR_CLEAR, TCR_REG);
  318. /* set the control register */
  319. SMC_SELECT_BANK (1);
  320. SMC_outw (CTL_DEFAULT, CTL_REG);
  321. /* Reset the MMU */
  322. SMC_SELECT_BANK (2);
  323. smc_wait_mmu_release_complete ();
  324. SMC_outw (MC_RESET, MMU_CMD_REG);
  325. while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
  326. udelay (1); /* Wait until not busy */
  327. /* Note: It doesn't seem that waiting for the MMU busy is needed here,
  328. but this is a place where future chipsets _COULD_ break. Be wary
  329. of issuing another MMU command right after this */
  330. /* Disable all interrupts */
  331. SMC_outb (0, IM_REG);
  332. }
  333. /*
  334. . Function: smc_enable
  335. . Purpose: let the chip talk to the outside work
  336. . Method:
  337. . 1. Enable the transmitter
  338. . 2. Enable the receiver
  339. . 3. Enable interrupts
  340. */
  341. static void smc_enable()
  342. {
  343. PRINTK2("%s:smc_enable\n", SMC_DEV_NAME);
  344. SMC_SELECT_BANK( 0 );
  345. /* see the header file for options in TCR/RCR DEFAULT*/
  346. SMC_outw( TCR_DEFAULT, TCR_REG );
  347. SMC_outw( RCR_DEFAULT, RCR_REG );
  348. /* clear MII_DIS */
  349. /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
  350. }
  351. /*
  352. . Function: smc_shutdown
  353. . Purpose: closes down the SMC91xxx chip.
  354. . Method:
  355. . 1. zero the interrupt mask
  356. . 2. clear the enable receive flag
  357. . 3. clear the enable xmit flags
  358. .
  359. . TODO:
  360. . (1) maybe utilize power down mode.
  361. . Why not yet? Because while the chip will go into power down mode,
  362. . the manual says that it will wake up in response to any I/O requests
  363. . in the register space. Empirical results do not show this working.
  364. */
  365. static void smc_shutdown()
  366. {
  367. PRINTK2(CARDNAME ":smc_shutdown\n");
  368. /* no more interrupts for me */
  369. SMC_SELECT_BANK( 2 );
  370. SMC_outb( 0, IM_REG );
  371. /* and tell the card to stay away from that nasty outside world */
  372. SMC_SELECT_BANK( 0 );
  373. SMC_outb( RCR_CLEAR, RCR_REG );
  374. SMC_outb( TCR_CLEAR, TCR_REG );
  375. }
  376. /*
  377. . Function: smc_hardware_send_packet(struct net_device * )
  378. . Purpose:
  379. . This sends the actual packet to the SMC9xxx chip.
  380. .
  381. . Algorithm:
  382. . First, see if a saved_skb is available.
  383. . ( this should NOT be called if there is no 'saved_skb'
  384. . Now, find the packet number that the chip allocated
  385. . Point the data pointers at it in memory
  386. . Set the length word in the chip's memory
  387. . Dump the packet to chip memory
  388. . Check if a last byte is needed ( odd length packet )
  389. . if so, set the control flag right
  390. . Tell the card to send it
  391. . Enable the transmit interrupt, so I know if it failed
  392. . Free the kernel data if I actually sent it.
  393. */
  394. static int smc_send_packet (volatile void *packet, int packet_length)
  395. {
  396. byte packet_no;
  397. unsigned long ioaddr;
  398. byte *buf;
  399. int length;
  400. int numPages;
  401. int try = 0;
  402. int time_out;
  403. byte status;
  404. PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
  405. length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
  406. /* allocate memory
  407. ** The MMU wants the number of pages to be the number of 256 bytes
  408. ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
  409. **
  410. ** The 91C111 ignores the size bits, but the code is left intact
  411. ** for backwards and future compatibility.
  412. **
  413. ** Pkt size for allocating is data length +6 (for additional status
  414. ** words, length and ctl!)
  415. **
  416. ** If odd size then last byte is included in this header.
  417. */
  418. numPages = ((length & 0xfffe) + 6);
  419. numPages >>= 8; /* Divide by 256 */
  420. if (numPages > 7) {
  421. printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
  422. return 0;
  423. }
  424. /* now, try to allocate the memory */
  425. SMC_SELECT_BANK (2);
  426. SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
  427. /* FIXME: the ALLOC_INT bit never gets set *
  428. * so the following will always give a *
  429. * memory allocation error. *
  430. * same code works in armboot though *
  431. * -ro
  432. */
  433. again:
  434. try++;
  435. time_out = MEMORY_WAIT_TIME;
  436. do {
  437. status = SMC_inb (SMC91111_INT_REG);
  438. if (status & IM_ALLOC_INT) {
  439. /* acknowledge the interrupt */
  440. SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
  441. break;
  442. }
  443. } while (--time_out);
  444. if (!time_out) {
  445. PRINTK2 ("%s: memory allocation, try %d failed ...\n",
  446. SMC_DEV_NAME, try);
  447. if (try < SMC_ALLOC_MAX_TRY)
  448. goto again;
  449. else
  450. return 0;
  451. }
  452. PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
  453. SMC_DEV_NAME, 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", SMC_DEV_NAME);
  462. return 0;
  463. }
  464. /* we have a packet address, so tell the card to use it */
  465. SMC_outb (packet_no, PN_REG);
  466. /* point to the beginning of the packet */
  467. SMC_outw (PTR_AUTOINC, PTR_REG);
  468. PRINTK3 ("%s: Trying to xmit packet of length %x\n",
  469. SMC_DEV_NAME, length);
  470. #if SMC_DEBUG > 2
  471. printf ("Transmitting Packet\n");
  472. print_packet (buf, length);
  473. #endif
  474. /* send the packet length ( +6 for status, length and ctl byte )
  475. and the status word ( set to zeros ) */
  476. #ifdef USE_32_BIT
  477. SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
  478. #else
  479. SMC_outw (0, SMC91111_DATA_REG);
  480. /* send the packet length ( +6 for status words, length, and ctl */
  481. SMC_outw ((length + 6), SMC91111_DATA_REG);
  482. #endif
  483. /* send the actual data
  484. . I _think_ it's faster to send the longs first, and then
  485. . mop up by sending the last word. It depends heavily
  486. . on alignment, at least on the 486. Maybe it would be
  487. . a good idea to check which is optimal? But that could take
  488. . almost as much time as is saved?
  489. */
  490. #ifdef USE_32_BIT
  491. SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
  492. if (length & 0x2)
  493. SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
  494. 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", SMC_DEV_NAME);
  510. /* release packet */
  511. SMC_outw (MC_FREEPKT, MMU_CMD_REG);
  512. /* wait for MMU getting ready (low) */
  513. while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
  514. udelay (10);
  515. }
  516. PRINTK2 ("MMU ready\n");
  517. return 0;
  518. } else {
  519. /* ack. int */
  520. SMC_outw (IM_TX_INT, SMC91111_INT_REG);
  521. PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
  522. length);
  523. /* release packet */
  524. SMC_outw (MC_FREEPKT, MMU_CMD_REG);
  525. /* wait for MMU getting ready (low) */
  526. while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
  527. udelay (10);
  528. }
  529. PRINTK2 ("MMU ready\n");
  530. }
  531. return length;
  532. }
  533. /*-------------------------------------------------------------------------
  534. |
  535. | smc_destructor( struct net_device * dev )
  536. | Input parameters:
  537. | dev, pointer to the device structure
  538. |
  539. | Output:
  540. | None.
  541. |
  542. ---------------------------------------------------------------------------
  543. */
  544. void smc_destructor()
  545. {
  546. PRINTK2(CARDNAME ":smc_destructor\n");
  547. }
  548. /*
  549. * Open and Initialize the board
  550. *
  551. * Set up everything, reset the card, etc ..
  552. *
  553. */
  554. static int smc_open (bd_t * bd)
  555. {
  556. int i, err;
  557. PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
  558. /* reset the hardware */
  559. smc_reset ();
  560. smc_enable ();
  561. /* Configure the PHY */
  562. #ifndef CONFIG_SMC91111_EXT_PHY
  563. smc_phy_configure ();
  564. #endif
  565. /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
  566. /* SMC_SELECT_BANK(0); */
  567. /* SMC_outw(0, RPC_REG); */
  568. SMC_SELECT_BANK (1);
  569. err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
  570. if (err < 0) {
  571. memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
  572. return (-1); /* upper code ignores this, but NOT bi_enetaddr */
  573. }
  574. #ifdef USE_32_BIT
  575. for (i = 0; i < 6; i += 2) {
  576. word address;
  577. address = smc_mac_addr[i + 1] << 8;
  578. address |= smc_mac_addr[i];
  579. SMC_outw (address, ADDR0_REG + i);
  580. }
  581. #else
  582. for (i = 0; i < 6; i++)
  583. SMC_outb (smc_mac_addr[i], ADDR0_REG + i);
  584. #endif
  585. return 0;
  586. }
  587. /*-------------------------------------------------------------
  588. .
  589. . smc_rcv - receive a packet from the card
  590. .
  591. . There is ( at least ) a packet waiting to be read from
  592. . chip-memory.
  593. .
  594. . o Read the status
  595. . o If an error, record it
  596. . o otherwise, read in the packet
  597. --------------------------------------------------------------
  598. */
  599. static int smc_rcv()
  600. {
  601. int packet_number;
  602. word status;
  603. word packet_length;
  604. int is_error = 0;
  605. #ifdef USE_32_BIT
  606. dword stat_len;
  607. #endif
  608. SMC_SELECT_BANK(2);
  609. packet_number = SMC_inw( RXFIFO_REG );
  610. if ( packet_number & RXFIFO_REMPTY ) {
  611. return 0;
  612. }
  613. PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME);
  614. /* start reading from the start of the packet */
  615. SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
  616. /* First two words are status and packet_length */
  617. #ifdef USE_32_BIT
  618. stat_len = SMC_inl(SMC91111_DATA_REG);
  619. status = stat_len & 0xffff;
  620. packet_length = stat_len >> 16;
  621. #else
  622. status = SMC_inw( SMC91111_DATA_REG );
  623. packet_length = SMC_inw( SMC91111_DATA_REG );
  624. #endif
  625. packet_length &= 0x07ff; /* mask off top bits */
  626. PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
  627. if ( !(status & RS_ERRORS ) ){
  628. /* Adjust for having already read the first two words */
  629. packet_length -= 4; /*4; */
  630. /* set odd length for bug in LAN91C111, */
  631. /* which never sets RS_ODDFRAME */
  632. /* TODO ? */
  633. #ifdef USE_32_BIT
  634. PRINTK3(" Reading %d dwords (and %d bytes) \n",
  635. packet_length >> 2, packet_length & 3 );
  636. /* QUESTION: Like in the TX routine, do I want
  637. to send the DWORDs or the bytes first, or some
  638. mixture. A mixture might improve already slow PIO
  639. performance */
  640. SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
  641. /* read the left over bytes */
  642. if (packet_length & 3) {
  643. int i;
  644. byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
  645. dword leftover = SMC_inl(SMC91111_DATA_REG);
  646. for (i=0; i<(packet_length & 3); i++)
  647. *tail++ = (byte) (leftover >> (8*i)) & 0xff;
  648. }
  649. #else
  650. PRINTK3(" Reading %d words and %d byte(s) \n",
  651. (packet_length >> 1 ), packet_length & 1 );
  652. SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
  653. #endif /* USE_32_BIT */
  654. #if SMC_DEBUG > 2
  655. printf("Receiving Packet\n");
  656. print_packet( NetRxPackets[0], packet_length );
  657. #endif
  658. } else {
  659. /* error ... */
  660. /* TODO ? */
  661. is_error = 1;
  662. }
  663. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  664. udelay(1); /* Wait until not busy */
  665. /* error or good, tell the card to get rid of this packet */
  666. SMC_outw( MC_RELEASE, MMU_CMD_REG );
  667. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  668. udelay(1); /* Wait until not busy */
  669. if (!is_error) {
  670. /* Pass the packet up to the protocol layers. */
  671. NetReceive(NetRxPackets[0], packet_length);
  672. return packet_length;
  673. } else {
  674. return 0;
  675. }
  676. }
  677. /*----------------------------------------------------
  678. . smc_close
  679. .
  680. . this makes the board clean up everything that it can
  681. . and not talk to the outside world. Caused by
  682. . an 'ifconfig ethX down'
  683. .
  684. -----------------------------------------------------*/
  685. static int smc_close()
  686. {
  687. PRINTK2("%s:smc_close\n", SMC_DEV_NAME);
  688. /* clear everything */
  689. smc_shutdown();
  690. return 0;
  691. }
  692. #if 0
  693. /*------------------------------------------------------------
  694. . Modify a bit in the LAN91C111 register set
  695. .-------------------------------------------------------------*/
  696. static word smc_modify_regbit(int bank, int ioaddr, int reg,
  697. unsigned int bit, int val)
  698. {
  699. word regval;
  700. SMC_SELECT_BANK( bank );
  701. regval = SMC_inw( reg );
  702. if (val)
  703. regval |= bit;
  704. else
  705. regval &= ~bit;
  706. SMC_outw( regval, 0 );
  707. return(regval);
  708. }
  709. /*------------------------------------------------------------
  710. . Retrieve a bit in the LAN91C111 register set
  711. .-------------------------------------------------------------*/
  712. static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
  713. {
  714. SMC_SELECT_BANK( bank );
  715. if ( SMC_inw( reg ) & bit)
  716. return(1);
  717. else
  718. return(0);
  719. }
  720. /*------------------------------------------------------------
  721. . Modify a LAN91C111 register (word access only)
  722. .-------------------------------------------------------------*/
  723. static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
  724. {
  725. SMC_SELECT_BANK( bank );
  726. SMC_outw( val, reg );
  727. }
  728. /*------------------------------------------------------------
  729. . Retrieve a LAN91C111 register (word access only)
  730. .-------------------------------------------------------------*/
  731. static int smc_get_reg(int bank, int ioaddr, int reg)
  732. {
  733. SMC_SELECT_BANK( bank );
  734. return(SMC_inw( reg ));
  735. }
  736. #endif /* 0 */
  737. /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
  738. #if (SMC_DEBUG > 2 )
  739. /*------------------------------------------------------------
  740. . Debugging function for viewing MII Management serial bitstream
  741. .-------------------------------------------------------------*/
  742. static void smc_dump_mii_stream (byte * bits, int size)
  743. {
  744. int i;
  745. printf ("BIT#:");
  746. for (i = 0; i < size; ++i) {
  747. printf ("%d", i % 10);
  748. }
  749. printf ("\nMDOE:");
  750. for (i = 0; i < size; ++i) {
  751. if (bits[i] & MII_MDOE)
  752. printf ("1");
  753. else
  754. printf ("0");
  755. }
  756. printf ("\nMDO :");
  757. for (i = 0; i < size; ++i) {
  758. if (bits[i] & MII_MDO)
  759. printf ("1");
  760. else
  761. printf ("0");
  762. }
  763. printf ("\nMDI :");
  764. for (i = 0; i < size; ++i) {
  765. if (bits[i] & MII_MDI)
  766. printf ("1");
  767. else
  768. printf ("0");
  769. }
  770. printf ("\n");
  771. }
  772. #endif
  773. /*------------------------------------------------------------
  774. . Reads a register from the MII Management serial interface
  775. .-------------------------------------------------------------*/
  776. #ifndef CONFIG_SMC91111_EXT_PHY
  777. static word smc_read_phy_register (byte phyreg)
  778. {
  779. int oldBank;
  780. int i;
  781. byte mask;
  782. word mii_reg;
  783. byte bits[64];
  784. int clk_idx = 0;
  785. int input_idx;
  786. word phydata;
  787. byte phyaddr = SMC_PHY_ADDR;
  788. /* 32 consecutive ones on MDO to establish sync */
  789. for (i = 0; i < 32; ++i)
  790. bits[clk_idx++] = MII_MDOE | MII_MDO;
  791. /* Start code <01> */
  792. bits[clk_idx++] = MII_MDOE;
  793. bits[clk_idx++] = MII_MDOE | MII_MDO;
  794. /* Read command <10> */
  795. bits[clk_idx++] = MII_MDOE | MII_MDO;
  796. bits[clk_idx++] = MII_MDOE;
  797. /* Output the PHY address, msb first */
  798. mask = (byte) 0x10;
  799. for (i = 0; i < 5; ++i) {
  800. if (phyaddr & mask)
  801. bits[clk_idx++] = MII_MDOE | MII_MDO;
  802. else
  803. bits[clk_idx++] = MII_MDOE;
  804. /* Shift to next lowest bit */
  805. mask >>= 1;
  806. }
  807. /* Output the phy register number, msb first */
  808. mask = (byte) 0x10;
  809. for (i = 0; i < 5; ++i) {
  810. if (phyreg & mask)
  811. bits[clk_idx++] = MII_MDOE | MII_MDO;
  812. else
  813. bits[clk_idx++] = MII_MDOE;
  814. /* Shift to next lowest bit */
  815. mask >>= 1;
  816. }
  817. /* Tristate and turnaround (2 bit times) */
  818. bits[clk_idx++] = 0;
  819. /*bits[clk_idx++] = 0; */
  820. /* Input starts at this bit time */
  821. input_idx = clk_idx;
  822. /* Will input 16 bits */
  823. for (i = 0; i < 16; ++i)
  824. bits[clk_idx++] = 0;
  825. /* Final clock bit */
  826. bits[clk_idx++] = 0;
  827. /* Save the current bank */
  828. oldBank = SMC_inw (BANK_SELECT);
  829. /* Select bank 3 */
  830. SMC_SELECT_BANK (3);
  831. /* Get the current MII register value */
  832. mii_reg = SMC_inw (MII_REG);
  833. /* Turn off all MII Interface bits */
  834. mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
  835. /* Clock all 64 cycles */
  836. for (i = 0; i < sizeof bits; ++i) {
  837. /* Clock Low - output data */
  838. SMC_outw (mii_reg | bits[i], MII_REG);
  839. udelay (SMC_PHY_CLOCK_DELAY);
  840. /* Clock Hi - input data */
  841. SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
  842. udelay (SMC_PHY_CLOCK_DELAY);
  843. bits[i] |= SMC_inw (MII_REG) & MII_MDI;
  844. }
  845. /* Return to idle state */
  846. /* Set clock to low, data to low, and output tristated */
  847. SMC_outw (mii_reg, MII_REG);
  848. udelay (SMC_PHY_CLOCK_DELAY);
  849. /* Restore original bank select */
  850. SMC_SELECT_BANK (oldBank);
  851. /* Recover input data */
  852. phydata = 0;
  853. for (i = 0; i < 16; ++i) {
  854. phydata <<= 1;
  855. if (bits[input_idx++] & MII_MDI)
  856. phydata |= 0x0001;
  857. }
  858. #if (SMC_DEBUG > 2 )
  859. printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  860. phyaddr, phyreg, phydata);
  861. smc_dump_mii_stream (bits, sizeof bits);
  862. #endif
  863. return (phydata);
  864. }
  865. /*------------------------------------------------------------
  866. . Writes a register to the MII Management serial interface
  867. .-------------------------------------------------------------*/
  868. static void smc_write_phy_register (byte phyreg, word phydata)
  869. {
  870. int oldBank;
  871. int i;
  872. word mask;
  873. word mii_reg;
  874. byte bits[65];
  875. int clk_idx = 0;
  876. byte phyaddr = SMC_PHY_ADDR;
  877. /* 32 consecutive ones on MDO to establish sync */
  878. for (i = 0; i < 32; ++i)
  879. bits[clk_idx++] = MII_MDOE | MII_MDO;
  880. /* Start code <01> */
  881. bits[clk_idx++] = MII_MDOE;
  882. bits[clk_idx++] = MII_MDOE | MII_MDO;
  883. /* Write command <01> */
  884. bits[clk_idx++] = MII_MDOE;
  885. bits[clk_idx++] = MII_MDOE | MII_MDO;
  886. /* Output the PHY address, msb first */
  887. mask = (byte) 0x10;
  888. for (i = 0; i < 5; ++i) {
  889. if (phyaddr & mask)
  890. bits[clk_idx++] = MII_MDOE | MII_MDO;
  891. else
  892. bits[clk_idx++] = MII_MDOE;
  893. /* Shift to next lowest bit */
  894. mask >>= 1;
  895. }
  896. /* Output the phy register number, msb first */
  897. mask = (byte) 0x10;
  898. for (i = 0; i < 5; ++i) {
  899. if (phyreg & mask)
  900. bits[clk_idx++] = MII_MDOE | MII_MDO;
  901. else
  902. bits[clk_idx++] = MII_MDOE;
  903. /* Shift to next lowest bit */
  904. mask >>= 1;
  905. }
  906. /* Tristate and turnaround (2 bit times) */
  907. bits[clk_idx++] = 0;
  908. bits[clk_idx++] = 0;
  909. /* Write out 16 bits of data, msb first */
  910. mask = 0x8000;
  911. for (i = 0; i < 16; ++i) {
  912. if (phydata & mask)
  913. bits[clk_idx++] = MII_MDOE | MII_MDO;
  914. else
  915. bits[clk_idx++] = MII_MDOE;
  916. /* Shift to next lowest bit */
  917. mask >>= 1;
  918. }
  919. /* Final clock bit (tristate) */
  920. bits[clk_idx++] = 0;
  921. /* Save the current bank */
  922. oldBank = SMC_inw (BANK_SELECT);
  923. /* Select bank 3 */
  924. SMC_SELECT_BANK (3);
  925. /* Get the current MII register value */
  926. mii_reg = SMC_inw (MII_REG);
  927. /* Turn off all MII Interface bits */
  928. mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
  929. /* Clock all cycles */
  930. for (i = 0; i < sizeof bits; ++i) {
  931. /* Clock Low - output data */
  932. SMC_outw (mii_reg | bits[i], MII_REG);
  933. udelay (SMC_PHY_CLOCK_DELAY);
  934. /* Clock Hi - input data */
  935. SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
  936. udelay (SMC_PHY_CLOCK_DELAY);
  937. bits[i] |= SMC_inw (MII_REG) & MII_MDI;
  938. }
  939. /* Return to idle state */
  940. /* Set clock to low, data to low, and output tristated */
  941. SMC_outw (mii_reg, MII_REG);
  942. udelay (SMC_PHY_CLOCK_DELAY);
  943. /* Restore original bank select */
  944. SMC_SELECT_BANK (oldBank);
  945. #if (SMC_DEBUG > 2 )
  946. printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  947. phyaddr, phyreg, phydata);
  948. smc_dump_mii_stream (bits, sizeof bits);
  949. #endif
  950. }
  951. #endif /* !CONFIG_SMC91111_EXT_PHY */
  952. /*------------------------------------------------------------
  953. . Waits the specified number of milliseconds - kernel friendly
  954. .-------------------------------------------------------------*/
  955. #ifndef CONFIG_SMC91111_EXT_PHY
  956. static void smc_wait_ms(unsigned int ms)
  957. {
  958. udelay(ms*1000);
  959. }
  960. #endif /* !CONFIG_SMC91111_EXT_PHY */
  961. /*------------------------------------------------------------
  962. . Configures the specified PHY using Autonegotiation. Calls
  963. . smc_phy_fixed() if the user has requested a certain config.
  964. .-------------------------------------------------------------*/
  965. #ifndef CONFIG_SMC91111_EXT_PHY
  966. static void smc_phy_configure ()
  967. {
  968. int timeout;
  969. byte phyaddr;
  970. word my_phy_caps; /* My PHY capabilities */
  971. word my_ad_caps; /* My Advertised capabilities */
  972. word status = 0; /*;my status = 0 */
  973. int failed = 0;
  974. PRINTK3 ("%s:smc_program_phy()\n", SMC_DEV_NAME);
  975. /* Get the detected phy address */
  976. phyaddr = SMC_PHY_ADDR;
  977. /* Reset the PHY, setting all other bits to zero */
  978. smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
  979. /* Wait for the reset to complete, or time out */
  980. timeout = 6; /* Wait up to 3 seconds */
  981. while (timeout--) {
  982. if (!(smc_read_phy_register (PHY_CNTL_REG)
  983. & PHY_CNTL_RST)) {
  984. /* reset complete */
  985. break;
  986. }
  987. smc_wait_ms (500); /* wait 500 millisecs */
  988. }
  989. if (timeout < 1) {
  990. printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
  991. goto smc_phy_configure_exit;
  992. }
  993. /* Read PHY Register 18, Status Output */
  994. /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
  995. /* Enable PHY Interrupts (for register 18) */
  996. /* Interrupts listed here are disabled */
  997. smc_write_phy_register (PHY_INT_REG, 0xffff);
  998. /* Configure the Receive/Phy Control register */
  999. SMC_SELECT_BANK (0);
  1000. SMC_outw (RPC_DEFAULT, RPC_REG);
  1001. /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
  1002. my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
  1003. my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
  1004. if (my_phy_caps & PHY_STAT_CAP_T4)
  1005. my_ad_caps |= PHY_AD_T4;
  1006. if (my_phy_caps & PHY_STAT_CAP_TXF)
  1007. my_ad_caps |= PHY_AD_TX_FDX;
  1008. if (my_phy_caps & PHY_STAT_CAP_TXH)
  1009. my_ad_caps |= PHY_AD_TX_HDX;
  1010. if (my_phy_caps & PHY_STAT_CAP_TF)
  1011. my_ad_caps |= PHY_AD_10_FDX;
  1012. if (my_phy_caps & PHY_STAT_CAP_TH)
  1013. my_ad_caps |= PHY_AD_10_HDX;
  1014. /* Update our Auto-Neg Advertisement Register */
  1015. smc_write_phy_register (PHY_AD_REG, my_ad_caps);
  1016. PRINTK2 ("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
  1017. PRINTK2 ("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
  1018. /* Restart auto-negotiation process in order to advertise my caps */
  1019. smc_write_phy_register (PHY_CNTL_REG,
  1020. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
  1021. /* Wait for the auto-negotiation to complete. This may take from */
  1022. /* 2 to 3 seconds. */
  1023. /* Wait for the reset to complete, or time out */
  1024. timeout = 20; /* Wait up to 10 seconds */
  1025. while (timeout--) {
  1026. status = smc_read_phy_register (PHY_STAT_REG);
  1027. if (status & PHY_STAT_ANEG_ACK) {
  1028. /* auto-negotiate complete */
  1029. break;
  1030. }
  1031. smc_wait_ms (500); /* wait 500 millisecs */
  1032. /* Restart auto-negotiation if remote fault */
  1033. if (status & PHY_STAT_REM_FLT) {
  1034. printf ("%s:PHY remote fault detected\n",
  1035. SMC_DEV_NAME);
  1036. /* Restart auto-negotiation */
  1037. printf ("%s:PHY restarting auto-negotiation\n",
  1038. SMC_DEV_NAME);
  1039. smc_write_phy_register (PHY_CNTL_REG,
  1040. PHY_CNTL_ANEG_EN |
  1041. PHY_CNTL_ANEG_RST |
  1042. PHY_CNTL_SPEED |
  1043. PHY_CNTL_DPLX);
  1044. }
  1045. }
  1046. if (timeout < 1) {
  1047. printf ("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
  1048. printf ("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
  1049. failed = 1;
  1050. }
  1051. /* Fail if we detected an auto-negotiate remote fault */
  1052. if (status & PHY_STAT_REM_FLT) {
  1053. printf ("%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1054. printf ("%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1055. failed = 1;
  1056. }
  1057. /* Re-Configure the Receive/Phy Control register */
  1058. SMC_outw (RPC_DEFAULT, RPC_REG);
  1059. smc_phy_configure_exit:
  1060. }
  1061. #endif /* !CONFIG_SMC91111_EXT_PHY */
  1062. #if SMC_DEBUG > 2
  1063. static void print_packet( byte * buf, int length )
  1064. {
  1065. #if 0
  1066. int i;
  1067. int remainder;
  1068. int lines;
  1069. printf("Packet of length %d \n", length );
  1070. #if SMC_DEBUG > 3
  1071. lines = length / 16;
  1072. remainder = length % 16;
  1073. for ( i = 0; i < lines ; i ++ ) {
  1074. int cur;
  1075. for ( cur = 0; cur < 8; cur ++ ) {
  1076. byte a, b;
  1077. a = *(buf ++ );
  1078. b = *(buf ++ );
  1079. printf("%02x%02x ", a, b );
  1080. }
  1081. printf("\n");
  1082. }
  1083. for ( i = 0; i < remainder/2 ; i++ ) {
  1084. byte a, b;
  1085. a = *(buf ++ );
  1086. b = *(buf ++ );
  1087. printf("%02x%02x ", a, b );
  1088. }
  1089. printf("\n");
  1090. #endif
  1091. #endif
  1092. }
  1093. #endif
  1094. int eth_init(bd_t *bd) {
  1095. return (smc_open(bd));
  1096. }
  1097. void eth_halt() {
  1098. smc_close();
  1099. }
  1100. int eth_rx() {
  1101. return smc_rcv();
  1102. }
  1103. int eth_send(volatile void *packet, int length) {
  1104. return smc_send_packet(packet, length);
  1105. }
  1106. int smc_get_ethaddr (bd_t * bd)
  1107. {
  1108. int env_size, rom_valid, env_present = 0, reg;
  1109. char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
  1110. uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
  1111. env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
  1112. if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
  1113. printf ("\n*** ERROR: ethaddr is not set properly!!\n");
  1114. return (-1);
  1115. }
  1116. if (env_size > 0) {
  1117. env_present = 1;
  1118. s = s_env_mac;
  1119. }
  1120. for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
  1121. v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  1122. if (s)
  1123. s = (*e) ? e + 1 : e;
  1124. }
  1125. rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
  1126. if (!env_present) { /* if NO env */
  1127. if (rom_valid) { /* but ROM is valid */
  1128. v_mac = v_rom_mac;
  1129. sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
  1130. v_mac[0], v_mac[1], v_mac[2], v_mac[3],
  1131. v_mac[4], v_mac[5]);
  1132. setenv ("ethaddr", s_env_mac);
  1133. } else { /* no env, bad ROM */
  1134. printf ("\n*** ERROR: ethaddr is NOT set !!\n");
  1135. return (-1);
  1136. }
  1137. } else { /* good env, don't care ROM */
  1138. v_mac = v_env_mac; /* always use a good env over a ROM */
  1139. }
  1140. if (env_present && rom_valid) { /* if both env and ROM are good */
  1141. if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
  1142. printf ("\nWarning: MAC addresses don't match:\n");
  1143. printf ("\tHW MAC address: "
  1144. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  1145. v_rom_mac[0], v_rom_mac[1],
  1146. v_rom_mac[2], v_rom_mac[3],
  1147. v_rom_mac[4], v_rom_mac[5] );
  1148. printf ("\t\"ethaddr\" value: "
  1149. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  1150. v_env_mac[0], v_env_mac[1],
  1151. v_env_mac[2], v_env_mac[3],
  1152. v_env_mac[4], v_env_mac[5]) ;
  1153. debug ("### Set MAC addr from environment\n");
  1154. }
  1155. }
  1156. memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
  1157. smc_set_mac_addr (v_mac); /* use old function to update smc default */
  1158. return (0);
  1159. }
  1160. int get_rom_mac (char *v_rom_mac)
  1161. {
  1162. int is_rom_present = 0;
  1163. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  1164. char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
  1165. memcpy (v_rom_mac, hw_mac_addr, 6);
  1166. return (1);
  1167. #else
  1168. if (is_rom_present) {
  1169. /* if eeprom contents are valid
  1170. * extract mac address into hw_mac_addr, 8 or 16 bit accesses
  1171. * memcpy (v_rom_mac, hc_mac_addr, 6);
  1172. * return(1);
  1173. */
  1174. }
  1175. memset (v_rom_mac, 0, 6);
  1176. return (0);
  1177. #endif
  1178. }
  1179. #endif /* CONFIG_DRIVER_SMC91111 */