smc91111.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453
  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 ("\n*** Warning: Environment and ROM MAC addresses don't match\n");
  1143. printf ("*** Using Environment MAC\n");
  1144. -----
  1145. printf ("\nWarning: MAC addresses don't match:\n");
  1146. printf ("\tHW MAC address: "
  1147. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  1148. v_rom_mac[0], v_rom_mac[1],
  1149. v_rom_mac[2], v_rom_mac[3],
  1150. v_rom_mac[4], v_rom_mac[5] );
  1151. printf ("\t\"ethaddr\" value: "
  1152. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  1153. v_env_mac[0], v_env_mac[1],
  1154. v_env_mac[2], v_env_mac[3],
  1155. v_env_mac[4], v_env_mac[5]) ;
  1156. debug ("### Set MAC addr from environment\n");
  1157. memcpy (addr, env_enetaddr, 6);
  1158. }
  1159. }
  1160. memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
  1161. smc_set_mac_addr (v_mac); /* use old function to update smc default */
  1162. return (0);
  1163. }
  1164. int get_rom_mac (char *v_rom_mac)
  1165. {
  1166. int is_rom_present = 0;
  1167. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  1168. char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
  1169. memcpy (v_rom_mac, hw_mac_addr, 6);
  1170. return (1);
  1171. #else
  1172. if (is_rom_present) {
  1173. /* if eeprom contents are valid
  1174. * extract mac address into hw_mac_addr, 8 or 16 bit accesses
  1175. * memcpy (v_rom_mac, hc_mac_addr, 6);
  1176. * return(1);
  1177. */
  1178. }
  1179. memset (v_rom_mac, 0, 6);
  1180. return (0);
  1181. #endif
  1182. }
  1183. #endif /* CONFIG_DRIVER_SMC91111 */