smc91111.c 39 KB

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