smc91111.c 34 KB

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