smc91111.c 40 KB

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