smc91111.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572
  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 * CONFIG_SYS_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. return -1;
  706. #ifdef USE_32_BIT
  707. for (i = 0; i < 6; i += 2) {
  708. word address;
  709. address = smc_mac_addr[i + 1] << 8;
  710. address |= smc_mac_addr[i];
  711. SMC_outw (address, (ADDR0_REG + i));
  712. }
  713. #else
  714. for (i = 0; i < 6; i++)
  715. SMC_outb (smc_mac_addr[i], (ADDR0_REG + i));
  716. #endif
  717. return 0;
  718. }
  719. /*-------------------------------------------------------------
  720. .
  721. . smc_rcv - receive a packet from the card
  722. .
  723. . There is ( at least ) a packet waiting to be read from
  724. . chip-memory.
  725. .
  726. . o Read the status
  727. . o If an error, record it
  728. . o otherwise, read in the packet
  729. --------------------------------------------------------------
  730. */
  731. static int smc_rcv()
  732. {
  733. int packet_number;
  734. word status;
  735. word packet_length;
  736. int is_error = 0;
  737. #ifdef USE_32_BIT
  738. dword stat_len;
  739. #endif
  740. byte saved_pnr;
  741. word saved_ptr;
  742. SMC_SELECT_BANK(2);
  743. /* save PTR and PTR registers */
  744. saved_pnr = SMC_inb( PN_REG );
  745. saved_ptr = SMC_inw( PTR_REG );
  746. packet_number = SMC_inw( RXFIFO_REG );
  747. if ( packet_number & RXFIFO_REMPTY ) {
  748. return 0;
  749. }
  750. PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
  751. /* start reading from the start of the packet */
  752. SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
  753. /* First two words are status and packet_length */
  754. #ifdef USE_32_BIT
  755. stat_len = SMC_inl(SMC91111_DATA_REG);
  756. status = stat_len & 0xffff;
  757. packet_length = stat_len >> 16;
  758. #else
  759. status = SMC_inw( SMC91111_DATA_REG );
  760. packet_length = SMC_inw( SMC91111_DATA_REG );
  761. #endif
  762. packet_length &= 0x07ff; /* mask off top bits */
  763. PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
  764. if ( !(status & RS_ERRORS ) ){
  765. /* Adjust for having already read the first two words */
  766. packet_length -= 4; /*4; */
  767. /* set odd length for bug in LAN91C111, */
  768. /* which never sets RS_ODDFRAME */
  769. /* TODO ? */
  770. #ifdef USE_32_BIT
  771. PRINTK3(" Reading %d dwords (and %d bytes) \n",
  772. packet_length >> 2, packet_length & 3 );
  773. /* QUESTION: Like in the TX routine, do I want
  774. to send the DWORDs or the bytes first, or some
  775. mixture. A mixture might improve already slow PIO
  776. performance */
  777. SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
  778. /* read the left over bytes */
  779. if (packet_length & 3) {
  780. int i;
  781. byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
  782. dword leftover = SMC_inl(SMC91111_DATA_REG);
  783. for (i=0; i<(packet_length & 3); i++)
  784. *tail++ = (byte) (leftover >> (8*i)) & 0xff;
  785. }
  786. #else
  787. PRINTK3(" Reading %d words and %d byte(s) \n",
  788. (packet_length >> 1 ), packet_length & 1 );
  789. SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
  790. #endif /* USE_32_BIT */
  791. #if SMC_DEBUG > 2
  792. printf("Receiving Packet\n");
  793. print_packet( NetRxPackets[0], packet_length );
  794. #endif
  795. } else {
  796. /* error ... */
  797. /* TODO ? */
  798. is_error = 1;
  799. }
  800. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  801. udelay(1); /* Wait until not busy */
  802. /* error or good, tell the card to get rid of this packet */
  803. SMC_outw( MC_RELEASE, MMU_CMD_REG );
  804. while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
  805. udelay(1); /* Wait until not busy */
  806. /* restore saved registers */
  807. #ifndef CONFIG_XAENIAX
  808. SMC_outb( saved_pnr, PN_REG );
  809. #else
  810. /* On Xaeniax board, we can't use SMC_outb here because that way
  811. * the Allocate MMU command will end up written to the command register
  812. * as well, which will lead to a problem.
  813. */
  814. SMC_outl( saved_pnr << 16, 0);
  815. #endif
  816. SMC_outw( saved_ptr, PTR_REG );
  817. if (!is_error) {
  818. /* Pass the packet up to the protocol layers. */
  819. NetReceive(NetRxPackets[0], packet_length);
  820. return packet_length;
  821. } else {
  822. return 0;
  823. }
  824. }
  825. /*----------------------------------------------------
  826. . smc_close
  827. .
  828. . this makes the board clean up everything that it can
  829. . and not talk to the outside world. Caused by
  830. . an 'ifconfig ethX down'
  831. .
  832. -----------------------------------------------------*/
  833. static int smc_close()
  834. {
  835. PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
  836. /* clear everything */
  837. smc_shutdown();
  838. return 0;
  839. }
  840. #if 0
  841. /*------------------------------------------------------------
  842. . Modify a bit in the LAN91C111 register set
  843. .-------------------------------------------------------------*/
  844. static word smc_modify_regbit(int bank, int ioaddr, int reg,
  845. unsigned int bit, int val)
  846. {
  847. word regval;
  848. SMC_SELECT_BANK( bank );
  849. regval = SMC_inw( reg );
  850. if (val)
  851. regval |= bit;
  852. else
  853. regval &= ~bit;
  854. SMC_outw( regval, 0 );
  855. return(regval);
  856. }
  857. /*------------------------------------------------------------
  858. . Retrieve a bit in the LAN91C111 register set
  859. .-------------------------------------------------------------*/
  860. static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
  861. {
  862. SMC_SELECT_BANK( bank );
  863. if ( SMC_inw( reg ) & bit)
  864. return(1);
  865. else
  866. return(0);
  867. }
  868. /*------------------------------------------------------------
  869. . Modify a LAN91C111 register (word access only)
  870. .-------------------------------------------------------------*/
  871. static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
  872. {
  873. SMC_SELECT_BANK( bank );
  874. SMC_outw( val, reg );
  875. }
  876. /*------------------------------------------------------------
  877. . Retrieve a LAN91C111 register (word access only)
  878. .-------------------------------------------------------------*/
  879. static int smc_get_reg(int bank, int ioaddr, int reg)
  880. {
  881. SMC_SELECT_BANK( bank );
  882. return(SMC_inw( reg ));
  883. }
  884. #endif /* 0 */
  885. /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
  886. #if (SMC_DEBUG > 2 )
  887. /*------------------------------------------------------------
  888. . Debugging function for viewing MII Management serial bitstream
  889. .-------------------------------------------------------------*/
  890. static void smc_dump_mii_stream (byte * bits, int size)
  891. {
  892. int i;
  893. printf ("BIT#:");
  894. for (i = 0; i < size; ++i) {
  895. printf ("%d", i % 10);
  896. }
  897. printf ("\nMDOE:");
  898. for (i = 0; i < size; ++i) {
  899. if (bits[i] & MII_MDOE)
  900. printf ("1");
  901. else
  902. printf ("0");
  903. }
  904. printf ("\nMDO :");
  905. for (i = 0; i < size; ++i) {
  906. if (bits[i] & MII_MDO)
  907. printf ("1");
  908. else
  909. printf ("0");
  910. }
  911. printf ("\nMDI :");
  912. for (i = 0; i < size; ++i) {
  913. if (bits[i] & MII_MDI)
  914. printf ("1");
  915. else
  916. printf ("0");
  917. }
  918. printf ("\n");
  919. }
  920. #endif
  921. /*------------------------------------------------------------
  922. . Reads a register from the MII Management serial interface
  923. .-------------------------------------------------------------*/
  924. #ifndef CONFIG_SMC91111_EXT_PHY
  925. static word smc_read_phy_register (byte phyreg)
  926. {
  927. int oldBank;
  928. int i;
  929. byte mask;
  930. word mii_reg;
  931. byte bits[64];
  932. int clk_idx = 0;
  933. int input_idx;
  934. word phydata;
  935. byte phyaddr = SMC_PHY_ADDR;
  936. /* 32 consecutive ones on MDO to establish sync */
  937. for (i = 0; i < 32; ++i)
  938. bits[clk_idx++] = MII_MDOE | MII_MDO;
  939. /* Start code <01> */
  940. bits[clk_idx++] = MII_MDOE;
  941. bits[clk_idx++] = MII_MDOE | MII_MDO;
  942. /* Read command <10> */
  943. bits[clk_idx++] = MII_MDOE | MII_MDO;
  944. bits[clk_idx++] = MII_MDOE;
  945. /* Output the PHY address, msb first */
  946. mask = (byte) 0x10;
  947. for (i = 0; i < 5; ++i) {
  948. if (phyaddr & mask)
  949. bits[clk_idx++] = MII_MDOE | MII_MDO;
  950. else
  951. bits[clk_idx++] = MII_MDOE;
  952. /* Shift to next lowest bit */
  953. mask >>= 1;
  954. }
  955. /* Output the phy register number, msb first */
  956. mask = (byte) 0x10;
  957. for (i = 0; i < 5; ++i) {
  958. if (phyreg & mask)
  959. bits[clk_idx++] = MII_MDOE | MII_MDO;
  960. else
  961. bits[clk_idx++] = MII_MDOE;
  962. /* Shift to next lowest bit */
  963. mask >>= 1;
  964. }
  965. /* Tristate and turnaround (2 bit times) */
  966. bits[clk_idx++] = 0;
  967. /*bits[clk_idx++] = 0; */
  968. /* Input starts at this bit time */
  969. input_idx = clk_idx;
  970. /* Will input 16 bits */
  971. for (i = 0; i < 16; ++i)
  972. bits[clk_idx++] = 0;
  973. /* Final clock bit */
  974. bits[clk_idx++] = 0;
  975. /* Save the current bank */
  976. oldBank = SMC_inw (BANK_SELECT);
  977. /* Select bank 3 */
  978. SMC_SELECT_BANK (3);
  979. /* Get the current MII register value */
  980. mii_reg = SMC_inw (MII_REG);
  981. /* Turn off all MII Interface bits */
  982. mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
  983. /* Clock all 64 cycles */
  984. for (i = 0; i < sizeof bits; ++i) {
  985. /* Clock Low - output data */
  986. SMC_outw (mii_reg | bits[i], MII_REG);
  987. udelay (SMC_PHY_CLOCK_DELAY);
  988. /* Clock Hi - input data */
  989. SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
  990. udelay (SMC_PHY_CLOCK_DELAY);
  991. bits[i] |= SMC_inw (MII_REG) & MII_MDI;
  992. }
  993. /* Return to idle state */
  994. /* Set clock to low, data to low, and output tristated */
  995. SMC_outw (mii_reg, MII_REG);
  996. udelay (SMC_PHY_CLOCK_DELAY);
  997. /* Restore original bank select */
  998. SMC_SELECT_BANK (oldBank);
  999. /* Recover input data */
  1000. phydata = 0;
  1001. for (i = 0; i < 16; ++i) {
  1002. phydata <<= 1;
  1003. if (bits[input_idx++] & MII_MDI)
  1004. phydata |= 0x0001;
  1005. }
  1006. #if (SMC_DEBUG > 2 )
  1007. printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  1008. phyaddr, phyreg, phydata);
  1009. smc_dump_mii_stream (bits, sizeof bits);
  1010. #endif
  1011. return (phydata);
  1012. }
  1013. /*------------------------------------------------------------
  1014. . Writes a register to the MII Management serial interface
  1015. .-------------------------------------------------------------*/
  1016. static void smc_write_phy_register (byte phyreg, word phydata)
  1017. {
  1018. int oldBank;
  1019. int i;
  1020. word mask;
  1021. word mii_reg;
  1022. byte bits[65];
  1023. int clk_idx = 0;
  1024. byte phyaddr = SMC_PHY_ADDR;
  1025. /* 32 consecutive ones on MDO to establish sync */
  1026. for (i = 0; i < 32; ++i)
  1027. bits[clk_idx++] = MII_MDOE | MII_MDO;
  1028. /* Start code <01> */
  1029. bits[clk_idx++] = MII_MDOE;
  1030. bits[clk_idx++] = MII_MDOE | MII_MDO;
  1031. /* Write command <01> */
  1032. bits[clk_idx++] = MII_MDOE;
  1033. bits[clk_idx++] = MII_MDOE | MII_MDO;
  1034. /* Output the PHY address, msb first */
  1035. mask = (byte) 0x10;
  1036. for (i = 0; i < 5; ++i) {
  1037. if (phyaddr & mask)
  1038. bits[clk_idx++] = MII_MDOE | MII_MDO;
  1039. else
  1040. bits[clk_idx++] = MII_MDOE;
  1041. /* Shift to next lowest bit */
  1042. mask >>= 1;
  1043. }
  1044. /* Output the phy register number, msb first */
  1045. mask = (byte) 0x10;
  1046. for (i = 0; i < 5; ++i) {
  1047. if (phyreg & mask)
  1048. bits[clk_idx++] = MII_MDOE | MII_MDO;
  1049. else
  1050. bits[clk_idx++] = MII_MDOE;
  1051. /* Shift to next lowest bit */
  1052. mask >>= 1;
  1053. }
  1054. /* Tristate and turnaround (2 bit times) */
  1055. bits[clk_idx++] = 0;
  1056. bits[clk_idx++] = 0;
  1057. /* Write out 16 bits of data, msb first */
  1058. mask = 0x8000;
  1059. for (i = 0; i < 16; ++i) {
  1060. if (phydata & mask)
  1061. bits[clk_idx++] = MII_MDOE | MII_MDO;
  1062. else
  1063. bits[clk_idx++] = MII_MDOE;
  1064. /* Shift to next lowest bit */
  1065. mask >>= 1;
  1066. }
  1067. /* Final clock bit (tristate) */
  1068. bits[clk_idx++] = 0;
  1069. /* Save the current bank */
  1070. oldBank = SMC_inw (BANK_SELECT);
  1071. /* Select bank 3 */
  1072. SMC_SELECT_BANK (3);
  1073. /* Get the current MII register value */
  1074. mii_reg = SMC_inw (MII_REG);
  1075. /* Turn off all MII Interface bits */
  1076. mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
  1077. /* Clock all cycles */
  1078. for (i = 0; i < sizeof bits; ++i) {
  1079. /* Clock Low - output data */
  1080. SMC_outw (mii_reg | bits[i], MII_REG);
  1081. udelay (SMC_PHY_CLOCK_DELAY);
  1082. /* Clock Hi - input data */
  1083. SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
  1084. udelay (SMC_PHY_CLOCK_DELAY);
  1085. bits[i] |= SMC_inw (MII_REG) & MII_MDI;
  1086. }
  1087. /* Return to idle state */
  1088. /* Set clock to low, data to low, and output tristated */
  1089. SMC_outw (mii_reg, MII_REG);
  1090. udelay (SMC_PHY_CLOCK_DELAY);
  1091. /* Restore original bank select */
  1092. SMC_SELECT_BANK (oldBank);
  1093. #if (SMC_DEBUG > 2 )
  1094. printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
  1095. phyaddr, phyreg, phydata);
  1096. smc_dump_mii_stream (bits, sizeof bits);
  1097. #endif
  1098. }
  1099. #endif /* !CONFIG_SMC91111_EXT_PHY */
  1100. /*------------------------------------------------------------
  1101. . Waits the specified number of milliseconds - kernel friendly
  1102. .-------------------------------------------------------------*/
  1103. #ifndef CONFIG_SMC91111_EXT_PHY
  1104. static void smc_wait_ms(unsigned int ms)
  1105. {
  1106. udelay(ms*1000);
  1107. }
  1108. #endif /* !CONFIG_SMC91111_EXT_PHY */
  1109. /*------------------------------------------------------------
  1110. . Configures the specified PHY using Autonegotiation. Calls
  1111. . smc_phy_fixed() if the user has requested a certain config.
  1112. .-------------------------------------------------------------*/
  1113. #ifndef CONFIG_SMC91111_EXT_PHY
  1114. static void smc_phy_configure ()
  1115. {
  1116. int timeout;
  1117. byte phyaddr;
  1118. word my_phy_caps; /* My PHY capabilities */
  1119. word my_ad_caps; /* My Advertised capabilities */
  1120. word status = 0; /*;my status = 0 */
  1121. int failed = 0;
  1122. PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
  1123. /* Get the detected phy address */
  1124. phyaddr = SMC_PHY_ADDR;
  1125. /* Reset the PHY, setting all other bits to zero */
  1126. smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
  1127. /* Wait for the reset to complete, or time out */
  1128. timeout = 6; /* Wait up to 3 seconds */
  1129. while (timeout--) {
  1130. if (!(smc_read_phy_register (PHY_CNTL_REG)
  1131. & PHY_CNTL_RST)) {
  1132. /* reset complete */
  1133. break;
  1134. }
  1135. smc_wait_ms (500); /* wait 500 millisecs */
  1136. }
  1137. if (timeout < 1) {
  1138. printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
  1139. goto smc_phy_configure_exit;
  1140. }
  1141. /* Read PHY Register 18, Status Output */
  1142. /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
  1143. /* Enable PHY Interrupts (for register 18) */
  1144. /* Interrupts listed here are disabled */
  1145. smc_write_phy_register (PHY_MASK_REG, 0xffff);
  1146. /* Configure the Receive/Phy Control register */
  1147. SMC_SELECT_BANK (0);
  1148. SMC_outw (RPC_DEFAULT, RPC_REG);
  1149. /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
  1150. my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
  1151. my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
  1152. if (my_phy_caps & PHY_STAT_CAP_T4)
  1153. my_ad_caps |= PHY_AD_T4;
  1154. if (my_phy_caps & PHY_STAT_CAP_TXF)
  1155. my_ad_caps |= PHY_AD_TX_FDX;
  1156. if (my_phy_caps & PHY_STAT_CAP_TXH)
  1157. my_ad_caps |= PHY_AD_TX_HDX;
  1158. if (my_phy_caps & PHY_STAT_CAP_TF)
  1159. my_ad_caps |= PHY_AD_10_FDX;
  1160. if (my_phy_caps & PHY_STAT_CAP_TH)
  1161. my_ad_caps |= PHY_AD_10_HDX;
  1162. /* Update our Auto-Neg Advertisement Register */
  1163. smc_write_phy_register (PHY_AD_REG, my_ad_caps);
  1164. /* Read the register back. Without this, it appears that when */
  1165. /* auto-negotiation is restarted, sometimes it isn't ready and */
  1166. /* the link does not come up. */
  1167. smc_read_phy_register(PHY_AD_REG);
  1168. PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
  1169. PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
  1170. /* Restart auto-negotiation process in order to advertise my caps */
  1171. smc_write_phy_register (PHY_CNTL_REG,
  1172. PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
  1173. /* Wait for the auto-negotiation to complete. This may take from */
  1174. /* 2 to 3 seconds. */
  1175. /* Wait for the reset to complete, or time out */
  1176. timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
  1177. while (timeout--) {
  1178. status = smc_read_phy_register (PHY_STAT_REG);
  1179. if (status & PHY_STAT_ANEG_ACK) {
  1180. /* auto-negotiate complete */
  1181. break;
  1182. }
  1183. smc_wait_ms (500); /* wait 500 millisecs */
  1184. /* Restart auto-negotiation if remote fault */
  1185. if (status & PHY_STAT_REM_FLT) {
  1186. printf ("%s: PHY remote fault detected\n",
  1187. SMC_DEV_NAME);
  1188. /* Restart auto-negotiation */
  1189. printf ("%s: PHY restarting auto-negotiation\n",
  1190. SMC_DEV_NAME);
  1191. smc_write_phy_register (PHY_CNTL_REG,
  1192. PHY_CNTL_ANEG_EN |
  1193. PHY_CNTL_ANEG_RST |
  1194. PHY_CNTL_SPEED |
  1195. PHY_CNTL_DPLX);
  1196. }
  1197. }
  1198. if (timeout < 1) {
  1199. printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
  1200. failed = 1;
  1201. }
  1202. /* Fail if we detected an auto-negotiate remote fault */
  1203. if (status & PHY_STAT_REM_FLT) {
  1204. printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
  1205. failed = 1;
  1206. }
  1207. /* Re-Configure the Receive/Phy Control register */
  1208. SMC_outw (RPC_DEFAULT, RPC_REG);
  1209. smc_phy_configure_exit: ;
  1210. }
  1211. #endif /* !CONFIG_SMC91111_EXT_PHY */
  1212. #if SMC_DEBUG > 2
  1213. static void print_packet( byte * buf, int length )
  1214. {
  1215. int i;
  1216. int remainder;
  1217. int lines;
  1218. printf("Packet of length %d \n", length );
  1219. #if SMC_DEBUG > 3
  1220. lines = length / 16;
  1221. remainder = length % 16;
  1222. for ( i = 0; i < lines ; i ++ ) {
  1223. int cur;
  1224. for ( cur = 0; cur < 8; cur ++ ) {
  1225. byte a, b;
  1226. a = *(buf ++ );
  1227. b = *(buf ++ );
  1228. printf("%02x%02x ", a, b );
  1229. }
  1230. printf("\n");
  1231. }
  1232. for ( i = 0; i < remainder/2 ; i++ ) {
  1233. byte a, b;
  1234. a = *(buf ++ );
  1235. b = *(buf ++ );
  1236. printf("%02x%02x ", a, b );
  1237. }
  1238. printf("\n");
  1239. #endif
  1240. }
  1241. #endif
  1242. int eth_init(bd_t *bd) {
  1243. #ifdef SHARED_RESOURCES
  1244. swap_to(ETHERNET);
  1245. #endif
  1246. return (smc_open(bd));
  1247. }
  1248. void eth_halt() {
  1249. smc_close();
  1250. }
  1251. int eth_rx() {
  1252. return smc_rcv();
  1253. }
  1254. int eth_send(volatile void *packet, int length) {
  1255. return smc_send_packet(packet, length);
  1256. }
  1257. int smc_get_ethaddr (bd_t * bd)
  1258. {
  1259. uchar v_mac[6];
  1260. if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
  1261. /* get ROM mac value if any */
  1262. if (!get_rom_mac(v_mac)) {
  1263. printf("\n*** ERROR: ethaddr is NOT set !!\n");
  1264. return -1;
  1265. }
  1266. eth_setenv_enetaddr("ethaddr", v_mac);
  1267. }
  1268. smc_set_mac_addr(v_mac); /* use old function to update smc default */
  1269. PRINTK("Using MAC Address %pM\n", v_mac);
  1270. return 0;
  1271. }
  1272. int get_rom_mac (uchar *v_rom_mac)
  1273. {
  1274. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  1275. char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
  1276. memcpy (v_rom_mac, hw_mac_addr, 6);
  1277. return (1);
  1278. #else
  1279. int i;
  1280. int valid_mac = 0;
  1281. SMC_SELECT_BANK (1);
  1282. for (i=0; i<6; i++)
  1283. {
  1284. v_rom_mac[i] = SMC_inb ((ADDR0_REG + i));
  1285. valid_mac |= v_rom_mac[i];
  1286. }
  1287. return (valid_mac ? 1 : 0);
  1288. #endif
  1289. }