smc91111.c 36 KB

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