smc91111.c 40 KB

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