smc91111.c 40 KB

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