smc91111.c 37 KB

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