smc91111.c 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480
  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. byte saved_pnr;
  407. word saved_ptr;
  408. /* save PTR and PNR registers before manipulation */
  409. SMC_SELECT_BANK (2);
  410. saved_pnr = SMC_inb( PN_REG );
  411. saved_ptr = SMC_inw( PTR_REG );
  412. PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
  413. length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
  414. /* allocate memory
  415. ** The MMU wants the number of pages to be the number of 256 bytes
  416. ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
  417. **
  418. ** The 91C111 ignores the size bits, but the code is left intact
  419. ** for backwards and future compatibility.
  420. **
  421. ** Pkt size for allocating is data length +6 (for additional status
  422. ** words, length and ctl!)
  423. **
  424. ** If odd size then last byte is included in this header.
  425. */
  426. numPages = ((length & 0xfffe) + 6);
  427. numPages >>= 8; /* Divide by 256 */
  428. if (numPages > 7) {
  429. printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
  430. return 0;
  431. }
  432. /* now, try to allocate the memory */
  433. SMC_SELECT_BANK (2);
  434. SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
  435. /* FIXME: the ALLOC_INT bit never gets set *
  436. * so the following will always give a *
  437. * memory allocation error. *
  438. * same code works in armboot though *
  439. * -ro
  440. */
  441. again:
  442. try++;
  443. time_out = MEMORY_WAIT_TIME;
  444. do {
  445. status = SMC_inb (SMC91111_INT_REG);
  446. if (status & IM_ALLOC_INT) {
  447. /* acknowledge the interrupt */
  448. SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
  449. break;
  450. }
  451. } while (--time_out);
  452. if (!time_out) {
  453. PRINTK2 ("%s: memory allocation, try %d failed ...\n",
  454. SMC_DEV_NAME, try);
  455. if (try < SMC_ALLOC_MAX_TRY)
  456. goto again;
  457. else
  458. return 0;
  459. }
  460. PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
  461. SMC_DEV_NAME, try);
  462. /* I can send the packet now.. */
  463. ioaddr = SMC_BASE_ADDRESS;
  464. buf = (byte *) packet;
  465. /* If I get here, I _know_ there is a packet slot waiting for me */
  466. packet_no = SMC_inb (AR_REG);
  467. if (packet_no & AR_FAILED) {
  468. /* or isn't there? BAD CHIP! */
  469. printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
  470. return 0;
  471. }
  472. /* we have a packet address, so tell the card to use it */
  473. SMC_outb (packet_no, PN_REG);
  474. /* do not write new ptr value if Write data fifo not empty */
  475. while ( saved_ptr & PTR_NOTEMPTY )
  476. printf ("Write data fifo not empty!\n");
  477. /* point to the beginning of the packet */
  478. SMC_outw (PTR_AUTOINC, PTR_REG);
  479. PRINTK3 ("%s: Trying to xmit packet of length %x\n",
  480. SMC_DEV_NAME, length);
  481. #if SMC_DEBUG > 2
  482. printf ("Transmitting Packet\n");
  483. print_packet (buf, length);
  484. #endif
  485. /* send the packet length ( +6 for status, length and ctl byte )
  486. and the status word ( set to zeros ) */
  487. #ifdef USE_32_BIT
  488. SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
  489. #else
  490. SMC_outw (0, SMC91111_DATA_REG);
  491. /* send the packet length ( +6 for status words, length, and ctl */
  492. SMC_outw ((length + 6), SMC91111_DATA_REG);
  493. #endif
  494. /* send the actual data
  495. . I _think_ it's faster to send the longs first, and then
  496. . mop up by sending the last word. It depends heavily
  497. . on alignment, at least on the 486. Maybe it would be
  498. . a good idea to check which is optimal? But that could take
  499. . almost as much time as is saved?
  500. */
  501. #ifdef USE_32_BIT
  502. SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
  503. if (length & 0x2)
  504. SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
  505. SMC91111_DATA_REG);
  506. #else
  507. SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
  508. #endif /* USE_32_BIT */
  509. /* Send the last byte, if there is one. */
  510. if ((length & 1) == 0) {
  511. SMC_outw (0, SMC91111_DATA_REG);
  512. } else {
  513. SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
  514. }
  515. /* and let the chipset deal with it */
  516. SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
  517. /* poll for TX INT */
  518. /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
  519. /* poll for TX_EMPTY INT - autorelease enabled */
  520. if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
  521. /* sending failed */
  522. PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
  523. /* release packet */
  524. /* no need to release, MMU does that now */
  525. /* SMC_outw (MC_FREEPKT, MMU_CMD_REG); */
  526. /* wait for MMU getting ready (low) */
  527. while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
  528. udelay (10);
  529. }
  530. PRINTK2 ("MMU ready\n");
  531. return 0;
  532. } else {
  533. /* ack. int */
  534. SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
  535. /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
  536. PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
  537. length);
  538. /* release packet */
  539. /* no need to release, MMU does that now */
  540. /* SMC_outw (MC_FREEPKT, MMU_CMD_REG); */
  541. /* wait for MMU getting ready (low) */
  542. while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
  543. udelay (10);
  544. }
  545. PRINTK2 ("MMU ready\n");
  546. }
  547. /* restore previously saved registers */
  548. SMC_outb( saved_pnr, PN_REG );
  549. SMC_outw( saved_ptr, PTR_REG );
  550. return length;
  551. }
  552. /*-------------------------------------------------------------------------
  553. |
  554. | smc_destructor( struct net_device * dev )
  555. | Input parameters:
  556. | dev, pointer to the device structure
  557. |
  558. | Output:
  559. | None.
  560. |
  561. ---------------------------------------------------------------------------
  562. */
  563. void smc_destructor()
  564. {
  565. PRINTK2(CARDNAME ":smc_destructor\n");
  566. }
  567. /*
  568. * Open and Initialize the board
  569. *
  570. * Set up everything, reset the card, etc ..
  571. *
  572. */
  573. static int smc_open (bd_t * bd)
  574. {
  575. int i, err;
  576. PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
  577. /* reset the hardware */
  578. smc_reset ();
  579. smc_enable ();
  580. /* Configure the PHY */
  581. #ifndef CONFIG_SMC91111_EXT_PHY
  582. smc_phy_configure ();
  583. #endif
  584. /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
  585. /* SMC_SELECT_BANK(0); */
  586. /* SMC_outw(0, RPC_REG); */
  587. SMC_SELECT_BANK (1);
  588. err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
  589. if (err < 0) {
  590. memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
  591. return (-1); /* upper code ignores this, but NOT bi_enetaddr */
  592. }
  593. #ifdef USE_32_BIT
  594. for (i = 0; i < 6; i += 2) {
  595. word address;
  596. address = smc_mac_addr[i + 1] << 8;
  597. address |= smc_mac_addr[i];
  598. SMC_outw (address, ADDR0_REG + i);
  599. }
  600. #else
  601. for (i = 0; i < 6; i++)
  602. SMC_outb (smc_mac_addr[i], ADDR0_REG + i);
  603. #endif
  604. return 0;
  605. }
  606. /*-------------------------------------------------------------
  607. .
  608. . smc_rcv - receive a packet from the card
  609. .
  610. . There is ( at least ) a packet waiting to be read from
  611. . chip-memory.
  612. .
  613. . o Read the status
  614. . o If an error, record it
  615. . o otherwise, read in the packet
  616. --------------------------------------------------------------
  617. */
  618. static int smc_rcv()
  619. {
  620. int packet_number;
  621. word status;
  622. word packet_length;
  623. int is_error = 0;
  624. #ifdef USE_32_BIT
  625. dword stat_len;
  626. #endif
  627. byte saved_pnr;
  628. word saved_ptr;
  629. SMC_SELECT_BANK(2);
  630. /* save PTR and PTR registers */
  631. saved_pnr = SMC_inb( PN_REG );
  632. saved_ptr = SMC_inw( PTR_REG );
  633. packet_number = SMC_inw( RXFIFO_REG );
  634. if ( packet_number & RXFIFO_REMPTY ) {
  635. return 0;
  636. }
  637. PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME);
  638. /* start reading from the start of the packet */
  639. SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
  640. /* First two words are status and packet_length */
  641. #ifdef USE_32_BIT
  642. stat_len = SMC_inl(SMC91111_DATA_REG);
  643. status = stat_len & 0xffff;
  644. packet_length = stat_len >> 16;
  645. #else
  646. status = SMC_inw( SMC91111_DATA_REG );
  647. packet_length = SMC_inw( SMC91111_DATA_REG );
  648. #endif
  649. packet_length &= 0x07ff; /* mask off top bits */
  650. PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
  651. if ( !(status & RS_ERRORS ) ){
  652. /* Adjust for having already read the first two words */
  653. packet_length -= 4; /*4; */
  654. /* set odd length for bug in LAN91C111, */
  655. /* which never sets RS_ODDFRAME */
  656. /* TODO ? */
  657. #ifdef USE_32_BIT
  658. PRINTK3(" Reading %d dwords (and %d bytes) \n",
  659. packet_length >> 2, packet_length & 3 );
  660. /* QUESTION: Like in the TX routine, do I want
  661. to send the DWORDs or the bytes first, or some
  662. mixture. A mixture might improve already slow PIO
  663. performance */
  664. SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
  665. /* read the left over bytes */
  666. if (packet_length & 3) {
  667. int i;
  668. byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
  669. dword leftover = SMC_inl(SMC91111_DATA_REG);
  670. for (i=0; i<(packet_length & 3); i++)
  671. *tail++ = (byte) (leftover >> (8*i)) & 0xff;
  672. }
  673. #else
  674. PRINTK3(" Reading %d words and %d byte(s) \n",
  675. (packet_length >> 1 ), packet_length & 1 );
  676. SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
  677. #endif /* USE_32_BIT */
  678. #if SMC_DEBUG > 2
  679. printf("Receiving Packet\n");
  680. print_packet( NetRxPackets[0], packet_length );
  681. #endif
  682. } else {
  683. /* error ... */
  684. /* TODO ? */
  685. is_error = 1;
  686. }
  687. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  688. udelay(1); /* Wait until not busy */
  689. /* error or good, tell the card to get rid of this packet */
  690. SMC_outw( MC_RELEASE, MMU_CMD_REG );
  691. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  692. udelay(1); /* Wait until not busy */
  693. /* restore saved registers */
  694. SMC_outb( saved_pnr, PN_REG );
  695. SMC_outw( saved_ptr, PTR_REG );
  696. if (!is_error) {
  697. /* Pass the packet up to the protocol layers. */
  698. NetReceive(NetRxPackets[0], packet_length);
  699. return packet_length;
  700. } else {
  701. return 0;
  702. }
  703. }
  704. /*----------------------------------------------------
  705. . smc_close
  706. .
  707. . this makes the board clean up everything that it can
  708. . and not talk to the outside world. Caused by
  709. . an 'ifconfig ethX down'
  710. .
  711. -----------------------------------------------------*/
  712. static int smc_close()
  713. {
  714. PRINTK2("%s:smc_close\n", SMC_DEV_NAME);
  715. /* clear everything */
  716. smc_shutdown();
  717. return 0;
  718. }
  719. #if 0
  720. /*------------------------------------------------------------
  721. . Modify a bit in the LAN91C111 register set
  722. .-------------------------------------------------------------*/
  723. static word smc_modify_regbit(int bank, int ioaddr, int reg,
  724. unsigned int bit, int val)
  725. {
  726. word regval;
  727. SMC_SELECT_BANK( bank );
  728. regval = SMC_inw( reg );
  729. if (val)
  730. regval |= bit;
  731. else
  732. regval &= ~bit;
  733. SMC_outw( regval, 0 );
  734. return(regval);
  735. }
  736. /*------------------------------------------------------------
  737. . Retrieve a bit in the LAN91C111 register set
  738. .-------------------------------------------------------------*/
  739. static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
  740. {
  741. SMC_SELECT_BANK( bank );
  742. if ( SMC_inw( reg ) & bit)
  743. return(1);
  744. else
  745. return(0);
  746. }
  747. /*------------------------------------------------------------
  748. . Modify a LAN91C111 register (word access only)
  749. .-------------------------------------------------------------*/
  750. static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
  751. {
  752. SMC_SELECT_BANK( bank );
  753. SMC_outw( val, reg );
  754. }
  755. /*------------------------------------------------------------
  756. . Retrieve a LAN91C111 register (word access only)
  757. .-------------------------------------------------------------*/
  758. static int smc_get_reg(int bank, int ioaddr, int reg)
  759. {
  760. SMC_SELECT_BANK( bank );
  761. return(SMC_inw( reg ));
  762. }
  763. #endif /* 0 */
  764. /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
  765. #if (SMC_DEBUG > 2 )
  766. /*------------------------------------------------------------
  767. . Debugging function for viewing MII Management serial bitstream
  768. .-------------------------------------------------------------*/
  769. static void smc_dump_mii_stream (byte * bits, int size)
  770. {
  771. int i;
  772. printf ("BIT#:");
  773. for (i = 0; i < size; ++i) {
  774. printf ("%d", i % 10);
  775. }
  776. printf ("\nMDOE:");
  777. for (i = 0; i < size; ++i) {
  778. if (bits[i] & MII_MDOE)
  779. printf ("1");
  780. else
  781. printf ("0");
  782. }
  783. printf ("\nMDO :");
  784. for (i = 0; i < size; ++i) {
  785. if (bits[i] & MII_MDO)
  786. printf ("1");
  787. else
  788. printf ("0");
  789. }
  790. printf ("\nMDI :");
  791. for (i = 0; i < size; ++i) {
  792. if (bits[i] & MII_MDI)
  793. printf ("1");
  794. else
  795. printf ("0");
  796. }
  797. printf ("\n");
  798. }
  799. #endif
  800. /*------------------------------------------------------------
  801. . Reads a register from the MII Management serial interface
  802. .-------------------------------------------------------------*/
  803. #ifndef CONFIG_SMC91111_EXT_PHY
  804. static word smc_read_phy_register (byte phyreg)
  805. {
  806. int oldBank;
  807. int i;
  808. byte mask;
  809. word mii_reg;
  810. byte bits[64];
  811. int clk_idx = 0;
  812. int input_idx;
  813. word phydata;
  814. byte phyaddr = SMC_PHY_ADDR;
  815. /* 32 consecutive ones on MDO to establish sync */
  816. for (i = 0; i < 32; ++i)
  817. bits[clk_idx++] = MII_MDOE | MII_MDO;
  818. /* Start code <01> */
  819. bits[clk_idx++] = MII_MDOE;
  820. bits[clk_idx++] = MII_MDOE | MII_MDO;
  821. /* Read command <10> */
  822. bits[clk_idx++] = MII_MDOE | MII_MDO;
  823. bits[clk_idx++] = MII_MDOE;
  824. /* Output the PHY address, msb first */
  825. mask = (byte) 0x10;
  826. for (i = 0; i < 5; ++i) {
  827. if (phyaddr & mask)
  828. bits[clk_idx++] = MII_MDOE | MII_MDO;
  829. else
  830. bits[clk_idx++] = MII_MDOE;
  831. /* Shift to next lowest bit */
  832. mask >>= 1;
  833. }
  834. /* Output the phy register number, msb first */
  835. mask = (byte) 0x10;
  836. for (i = 0; i < 5; ++i) {
  837. if (phyreg & mask)
  838. bits[clk_idx++] = MII_MDOE | MII_MDO;
  839. else
  840. bits[clk_idx++] = MII_MDOE;
  841. /* Shift to next lowest bit */
  842. mask >>= 1;
  843. }
  844. /* Tristate and turnaround (2 bit times) */
  845. bits[clk_idx++] = 0;
  846. /*bits[clk_idx++] = 0; */
  847. /* Input starts at this bit time */
  848. input_idx = clk_idx;
  849. /* Will input 16 bits */
  850. for (i = 0; i < 16; ++i)
  851. bits[clk_idx++] = 0;
  852. /* Final clock bit */
  853. bits[clk_idx++] = 0;
  854. /* Save the current bank */
  855. oldBank = SMC_inw (BANK_SELECT);
  856. /* Select bank 3 */
  857. SMC_SELECT_BANK (3);
  858. /* Get the current MII register value */
  859. mii_reg = SMC_inw (MII_REG);
  860. /* Turn off all MII Interface bits */
  861. mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
  862. /* Clock all 64 cycles */
  863. for (i = 0; i < sizeof bits; ++i) {
  864. /* Clock Low - output data */
  865. SMC_outw (mii_reg | bits[i], MII_REG);
  866. udelay (SMC_PHY_CLOCK_DELAY);
  867. /* Clock Hi - input data */
  868. SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
  869. udelay (SMC_PHY_CLOCK_DELAY);
  870. bits[i] |= SMC_inw (MII_REG) & MII_MDI;
  871. }
  872. /* Return to idle state */
  873. /* Set clock to low, data to low, and output tristated */
  874. SMC_outw (mii_reg, MII_REG);
  875. udelay (SMC_PHY_CLOCK_DELAY);
  876. /* Restore original bank select */
  877. SMC_SELECT_BANK (oldBank);
  878. /* Recover input data */
  879. phydata = 0;
  880. for (i = 0; i < 16; ++i) {
  881. phydata <<= 1;
  882. if (bits[input_idx++] & MII_MDI)
  883. phydata |= 0x0001;
  884. }
  885. #if (SMC_DEBUG > 2 )
  886. printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  887. phyaddr, phyreg, phydata);
  888. smc_dump_mii_stream (bits, sizeof bits);
  889. #endif
  890. return (phydata);
  891. }
  892. /*------------------------------------------------------------
  893. . Writes a register to the MII Management serial interface
  894. .-------------------------------------------------------------*/
  895. static void smc_write_phy_register (byte phyreg, word phydata)
  896. {
  897. int oldBank;
  898. int i;
  899. word mask;
  900. word mii_reg;
  901. byte bits[65];
  902. int clk_idx = 0;
  903. byte phyaddr = SMC_PHY_ADDR;
  904. /* 32 consecutive ones on MDO to establish sync */
  905. for (i = 0; i < 32; ++i)
  906. bits[clk_idx++] = MII_MDOE | MII_MDO;
  907. /* Start code <01> */
  908. bits[clk_idx++] = MII_MDOE;
  909. bits[clk_idx++] = MII_MDOE | MII_MDO;
  910. /* Write command <01> */
  911. bits[clk_idx++] = MII_MDOE;
  912. bits[clk_idx++] = MII_MDOE | MII_MDO;
  913. /* Output the PHY address, msb first */
  914. mask = (byte) 0x10;
  915. for (i = 0; i < 5; ++i) {
  916. if (phyaddr & mask)
  917. bits[clk_idx++] = MII_MDOE | MII_MDO;
  918. else
  919. bits[clk_idx++] = MII_MDOE;
  920. /* Shift to next lowest bit */
  921. mask >>= 1;
  922. }
  923. /* Output the phy register number, msb first */
  924. mask = (byte) 0x10;
  925. for (i = 0; i < 5; ++i) {
  926. if (phyreg & mask)
  927. bits[clk_idx++] = MII_MDOE | MII_MDO;
  928. else
  929. bits[clk_idx++] = MII_MDOE;
  930. /* Shift to next lowest bit */
  931. mask >>= 1;
  932. }
  933. /* Tristate and turnaround (2 bit times) */
  934. bits[clk_idx++] = 0;
  935. bits[clk_idx++] = 0;
  936. /* Write out 16 bits of data, msb first */
  937. mask = 0x8000;
  938. for (i = 0; i < 16; ++i) {
  939. if (phydata & mask)
  940. bits[clk_idx++] = MII_MDOE | MII_MDO;
  941. else
  942. bits[clk_idx++] = MII_MDOE;
  943. /* Shift to next lowest bit */
  944. mask >>= 1;
  945. }
  946. /* Final clock bit (tristate) */
  947. bits[clk_idx++] = 0;
  948. /* Save the current bank */
  949. oldBank = SMC_inw (BANK_SELECT);
  950. /* Select bank 3 */
  951. SMC_SELECT_BANK (3);
  952. /* Get the current MII register value */
  953. mii_reg = SMC_inw (MII_REG);
  954. /* Turn off all MII Interface bits */
  955. mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
  956. /* Clock all cycles */
  957. for (i = 0; i < sizeof bits; ++i) {
  958. /* Clock Low - output data */
  959. SMC_outw (mii_reg | bits[i], MII_REG);
  960. udelay (SMC_PHY_CLOCK_DELAY);
  961. /* Clock Hi - input data */
  962. SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
  963. udelay (SMC_PHY_CLOCK_DELAY);
  964. bits[i] |= SMC_inw (MII_REG) & MII_MDI;
  965. }
  966. /* Return to idle state */
  967. /* Set clock to low, data to low, and output tristated */
  968. SMC_outw (mii_reg, MII_REG);
  969. udelay (SMC_PHY_CLOCK_DELAY);
  970. /* Restore original bank select */
  971. SMC_SELECT_BANK (oldBank);
  972. #if (SMC_DEBUG > 2 )
  973. printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  974. phyaddr, phyreg, phydata);
  975. smc_dump_mii_stream (bits, sizeof bits);
  976. #endif
  977. }
  978. #endif /* !CONFIG_SMC91111_EXT_PHY */
  979. /*------------------------------------------------------------
  980. . Waits the specified number of milliseconds - kernel friendly
  981. .-------------------------------------------------------------*/
  982. #ifndef CONFIG_SMC91111_EXT_PHY
  983. static void smc_wait_ms(unsigned int ms)
  984. {
  985. udelay(ms*1000);
  986. }
  987. #endif /* !CONFIG_SMC91111_EXT_PHY */
  988. /*------------------------------------------------------------
  989. . Configures the specified PHY using Autonegotiation. Calls
  990. . smc_phy_fixed() if the user has requested a certain config.
  991. .-------------------------------------------------------------*/
  992. #ifndef CONFIG_SMC91111_EXT_PHY
  993. static void smc_phy_configure ()
  994. {
  995. int timeout;
  996. byte phyaddr;
  997. word my_phy_caps; /* My PHY capabilities */
  998. word my_ad_caps; /* My Advertised capabilities */
  999. word status = 0; /*;my status = 0 */
  1000. int failed = 0;
  1001. PRINTK3 ("%s:smc_program_phy()\n", SMC_DEV_NAME);
  1002. /* Get the detected phy address */
  1003. phyaddr = SMC_PHY_ADDR;
  1004. /* Reset the PHY, setting all other bits to zero */
  1005. smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
  1006. /* Wait for the reset to complete, or time out */
  1007. timeout = 6; /* Wait up to 3 seconds */
  1008. while (timeout--) {
  1009. if (!(smc_read_phy_register (PHY_CNTL_REG)
  1010. & PHY_CNTL_RST)) {
  1011. /* reset complete */
  1012. break;
  1013. }
  1014. smc_wait_ms (500); /* wait 500 millisecs */
  1015. }
  1016. if (timeout < 1) {
  1017. printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
  1018. goto smc_phy_configure_exit;
  1019. }
  1020. /* Read PHY Register 18, Status Output */
  1021. /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
  1022. /* Enable PHY Interrupts (for register 18) */
  1023. /* Interrupts listed here are disabled */
  1024. smc_write_phy_register (PHY_MASK_REG, 0xffff);
  1025. /* Configure the Receive/Phy Control register */
  1026. SMC_SELECT_BANK (0);
  1027. SMC_outw (RPC_DEFAULT, RPC_REG);
  1028. /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
  1029. my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
  1030. my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
  1031. if (my_phy_caps & PHY_STAT_CAP_T4)
  1032. my_ad_caps |= PHY_AD_T4;
  1033. if (my_phy_caps & PHY_STAT_CAP_TXF)
  1034. my_ad_caps |= PHY_AD_TX_FDX;
  1035. if (my_phy_caps & PHY_STAT_CAP_TXH)
  1036. my_ad_caps |= PHY_AD_TX_HDX;
  1037. if (my_phy_caps & PHY_STAT_CAP_TF)
  1038. my_ad_caps |= PHY_AD_10_FDX;
  1039. if (my_phy_caps & PHY_STAT_CAP_TH)
  1040. my_ad_caps |= PHY_AD_10_HDX;
  1041. /* Update our Auto-Neg Advertisement Register */
  1042. smc_write_phy_register (PHY_AD_REG, my_ad_caps);
  1043. /* Read the register back. Without this, it appears that when */
  1044. /* auto-negotiation is restarted, sometimes it isn't ready and */
  1045. /* the link does not come up. */
  1046. smc_read_phy_register(PHY_AD_REG);
  1047. PRINTK2 ("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
  1048. PRINTK2 ("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
  1049. /* Restart auto-negotiation process in order to advertise my caps */
  1050. smc_write_phy_register (PHY_CNTL_REG,
  1051. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
  1052. /* Wait for the auto-negotiation to complete. This may take from */
  1053. /* 2 to 3 seconds. */
  1054. /* Wait for the reset to complete, or time out */
  1055. timeout = 20; /* Wait up to 10 seconds */
  1056. while (timeout--) {
  1057. status = smc_read_phy_register (PHY_STAT_REG);
  1058. if (status & PHY_STAT_ANEG_ACK) {
  1059. /* auto-negotiate complete */
  1060. break;
  1061. }
  1062. smc_wait_ms (500); /* wait 500 millisecs */
  1063. /* Restart auto-negotiation if remote fault */
  1064. if (status & PHY_STAT_REM_FLT) {
  1065. printf ("%s:PHY remote fault detected\n",
  1066. SMC_DEV_NAME);
  1067. /* Restart auto-negotiation */
  1068. printf ("%s:PHY restarting auto-negotiation\n",
  1069. SMC_DEV_NAME);
  1070. smc_write_phy_register (PHY_CNTL_REG,
  1071. PHY_CNTL_ANEG_EN |
  1072. PHY_CNTL_ANEG_RST |
  1073. PHY_CNTL_SPEED |
  1074. PHY_CNTL_DPLX);
  1075. }
  1076. }
  1077. if (timeout < 1) {
  1078. printf ("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
  1079. printf ("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME);
  1080. failed = 1;
  1081. }
  1082. /* Fail if we detected an auto-negotiate remote fault */
  1083. if (status & PHY_STAT_REM_FLT) {
  1084. printf ("%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1085. printf ("%s:PHY remote fault detected\n", SMC_DEV_NAME);
  1086. failed = 1;
  1087. }
  1088. /* Re-Configure the Receive/Phy Control register */
  1089. SMC_outw (RPC_DEFAULT, RPC_REG);
  1090. smc_phy_configure_exit:
  1091. }
  1092. #endif /* !CONFIG_SMC91111_EXT_PHY */
  1093. #if SMC_DEBUG > 2
  1094. static void print_packet( byte * buf, int length )
  1095. {
  1096. int i;
  1097. int remainder;
  1098. int lines;
  1099. printf("Packet of length %d \n", length );
  1100. #if SMC_DEBUG > 3
  1101. lines = length / 16;
  1102. remainder = length % 16;
  1103. for ( i = 0; i < lines ; i ++ ) {
  1104. int cur;
  1105. for ( cur = 0; cur < 8; cur ++ ) {
  1106. byte a, b;
  1107. a = *(buf ++ );
  1108. b = *(buf ++ );
  1109. printf("%02x%02x ", a, b );
  1110. }
  1111. printf("\n");
  1112. }
  1113. for ( i = 0; i < remainder/2 ; i++ ) {
  1114. byte a, b;
  1115. a = *(buf ++ );
  1116. b = *(buf ++ );
  1117. printf("%02x%02x ", a, b );
  1118. }
  1119. printf("\n");
  1120. #endif
  1121. }
  1122. #endif
  1123. int eth_init(bd_t *bd) {
  1124. return (smc_open(bd));
  1125. }
  1126. void eth_halt() {
  1127. smc_close();
  1128. }
  1129. int eth_rx() {
  1130. return smc_rcv();
  1131. }
  1132. int eth_send(volatile void *packet, int length) {
  1133. return smc_send_packet(packet, length);
  1134. }
  1135. int smc_get_ethaddr (bd_t * bd)
  1136. {
  1137. int env_size, rom_valid, env_present = 0, reg;
  1138. char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
  1139. uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
  1140. env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
  1141. if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
  1142. printf ("\n*** ERROR: ethaddr is not set properly!!\n");
  1143. return (-1);
  1144. }
  1145. if (env_size > 0) {
  1146. env_present = 1;
  1147. s = s_env_mac;
  1148. }
  1149. for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
  1150. v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  1151. if (s)
  1152. s = (*e) ? e + 1 : e;
  1153. }
  1154. rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
  1155. if (!env_present) { /* if NO env */
  1156. if (rom_valid) { /* but ROM is valid */
  1157. v_mac = v_rom_mac;
  1158. sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
  1159. v_mac[0], v_mac[1], v_mac[2], v_mac[3],
  1160. v_mac[4], v_mac[5]);
  1161. setenv ("ethaddr", s_env_mac);
  1162. } else { /* no env, bad ROM */
  1163. printf ("\n*** ERROR: ethaddr is NOT set !!\n");
  1164. return (-1);
  1165. }
  1166. } else { /* good env, don't care ROM */
  1167. v_mac = v_env_mac; /* always use a good env over a ROM */
  1168. }
  1169. if (env_present && rom_valid) { /* if both env and ROM are good */
  1170. if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
  1171. printf ("\nWarning: MAC addresses don't match:\n");
  1172. printf ("\tHW MAC address: "
  1173. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  1174. v_rom_mac[0], v_rom_mac[1],
  1175. v_rom_mac[2], v_rom_mac[3],
  1176. v_rom_mac[4], v_rom_mac[5] );
  1177. printf ("\t\"ethaddr\" value: "
  1178. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  1179. v_env_mac[0], v_env_mac[1],
  1180. v_env_mac[2], v_env_mac[3],
  1181. v_env_mac[4], v_env_mac[5]) ;
  1182. debug ("### Set MAC addr from environment\n");
  1183. }
  1184. }
  1185. memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
  1186. smc_set_mac_addr (v_mac); /* use old function to update smc default */
  1187. PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
  1188. v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
  1189. return (0);
  1190. }
  1191. int get_rom_mac (char *v_rom_mac)
  1192. {
  1193. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  1194. char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
  1195. memcpy (v_rom_mac, hw_mac_addr, 6);
  1196. return (1);
  1197. #else
  1198. int i;
  1199. SMC_SELECT_BANK (1);
  1200. for (i=0; i<6; i++)
  1201. {
  1202. v_rom_mac[i] = SMC_inb (ADDR0_REG + i);
  1203. }
  1204. return (1);
  1205. #endif
  1206. }
  1207. #endif /* CONFIG_DRIVER_SMC91111 */