lan91c96.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967
  1. /*------------------------------------------------------------------------
  2. * lan91c96.c
  3. * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
  4. * on the SMC91111 driver from U-boot.
  5. *
  6. * (C) Copyright 2002
  7. * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
  8. * Rolf Offermanns <rof@sysgo.de>
  9. *
  10. * Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
  11. * Developed by Simple Network Magic Corporation (SNMC)
  12. * Copyright (C) 1996 by Erik Stahlman (ES)
  13. *
  14. * This program is free software; you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation; either version 2 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program; if not, write to the Free Software
  26. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27. *
  28. * Information contained in this file was obtained from the LAN91C96
  29. * manual from SMC. To get a copy, if you really want one, you can find
  30. * information under www.smsc.com.
  31. *
  32. *
  33. * "Features" of the SMC chip:
  34. * 6144 byte packet memory. ( for the 91C96 )
  35. * EEPROM for configuration
  36. * AUI/TP selection ( mine has 10Base2/10BaseT select )
  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 LAN91C96 databook (www.smsc.com)
  51. * o smc91111.c (u-boot driver)
  52. * o smc9194.c (linux kernel driver)
  53. * o lan91c96.c (Intel Diagnostic Manager driver)
  54. *
  55. * History:
  56. * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
  57. * for lan91c96
  58. *---------------------------------------------------------------------------
  59. */
  60. #include <common.h>
  61. #include <command.h>
  62. #include "lan91c96.h"
  63. #include <net.h>
  64. #ifdef CONFIG_DRIVER_LAN91C96
  65. #if (CONFIG_COMMANDS & CFG_CMD_NET)
  66. /*------------------------------------------------------------------------
  67. *
  68. * Configuration options, for the experienced user to change.
  69. *
  70. -------------------------------------------------------------------------*/
  71. /* Use power-down feature of the chip */
  72. #define POWER_DOWN 0
  73. /*
  74. * Wait time for memory to be free. This probably shouldn't be
  75. * tuned that much, as waiting for this means nothing else happens
  76. * in the system
  77. */
  78. #define MEMORY_WAIT_TIME 16
  79. #define SMC_DEBUG 0
  80. #if (SMC_DEBUG > 2 )
  81. #define PRINTK3(args...) printf(args)
  82. #else
  83. #define PRINTK3(args...)
  84. #endif
  85. #if SMC_DEBUG > 1
  86. #define PRINTK2(args...) printf(args)
  87. #else
  88. #define PRINTK2(args...)
  89. #endif
  90. #ifdef SMC_DEBUG
  91. #define PRINTK(args...) printf(args)
  92. #else
  93. #define PRINTK(args...)
  94. #endif
  95. /*------------------------------------------------------------------------
  96. *
  97. * The internal workings of the driver. If you are changing anything
  98. * here with the SMC stuff, you should have the datasheet and know
  99. * what you are doing.
  100. *
  101. *------------------------------------------------------------------------
  102. */
  103. #define CARDNAME "LAN91C96"
  104. #define SMC_BASE_ADDRESS CONFIG_LAN91C96_BASE
  105. #define SMC_DEV_NAME "LAN91C96"
  106. #define SMC_ALLOC_MAX_TRY 5
  107. #define SMC_TX_TIMEOUT 30
  108. #define ETH_ZLEN 60
  109. #ifdef CONFIG_LAN91C96_USE_32_BIT
  110. #define USE_32_BIT 1
  111. #else
  112. #undef USE_32_BIT
  113. #endif
  114. /*-----------------------------------------------------------------
  115. *
  116. * The driver can be entered at any of the following entry points.
  117. *
  118. *-----------------------------------------------------------------
  119. */
  120. extern int eth_init (bd_t * bd);
  121. extern void eth_halt (void);
  122. extern int eth_rx (void);
  123. extern int eth_send (volatile void *packet, int length);
  124. #if 0
  125. static int smc_hw_init (void);
  126. #endif
  127. /*
  128. * This is called by register_netdev(). It is responsible for
  129. * checking the portlist for the SMC9000 series chipset. If it finds
  130. * one, then it will initialize the device, find the hardware information,
  131. * and sets up the appropriate device parameters.
  132. * NOTE: Interrupts are *OFF* when this procedure is called.
  133. *
  134. * NB:This shouldn't be static since it is referred to externally.
  135. */
  136. int smc_init (void);
  137. /*
  138. * This is called by unregister_netdev(). It is responsible for
  139. * cleaning up before the driver is finally unregistered and discarded.
  140. */
  141. void smc_destructor (void);
  142. /*
  143. * The kernel calls this function when someone wants to use the device,
  144. * typically 'ifconfig ethX up'.
  145. */
  146. static int smc_open (bd_t *bd);
  147. /*
  148. * This is called by the kernel in response to 'ifconfig ethX down'. It
  149. * is responsible for cleaning up everything that the open routine
  150. * does, and maybe putting the card into a powerdown state.
  151. */
  152. static int smc_close (void);
  153. /*
  154. * This is a separate procedure to handle the receipt of a packet, to
  155. * leave the interrupt code looking slightly cleaner
  156. */
  157. static int smc_rcv (void);
  158. /* See if a MAC address is defined in the current environment. If so use it. If not
  159. . print a warning and set the environment and other globals with the default.
  160. . If an EEPROM is present it really should be consulted.
  161. */
  162. int smc_get_ethaddr(bd_t *bd);
  163. int get_rom_mac(char *v_rom_mac);
  164. /* ------------------------------------------------------------
  165. * Internal routines
  166. * ------------------------------------------------------------
  167. */
  168. static char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
  169. /*
  170. * This function must be called before smc_open() if you want to override
  171. * the default mac address.
  172. */
  173. void smc_set_mac_addr (const char *addr)
  174. {
  175. int i;
  176. for (i = 0; i < sizeof (smc_mac_addr); i++) {
  177. smc_mac_addr[i] = addr[i];
  178. }
  179. }
  180. /*
  181. * smc_get_macaddr is no longer used. If you want to override the default
  182. * mac address, call smc_get_mac_addr as a part of the board initialisation.
  183. */
  184. #if 0
  185. void smc_get_macaddr (byte * addr)
  186. {
  187. /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
  188. unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
  189. int i;
  190. for (i = 0; i < 6; i++) {
  191. addr[0] = *(dnp1110_mac + 0);
  192. addr[1] = *(dnp1110_mac + 1);
  193. addr[2] = *(dnp1110_mac + 2);
  194. addr[3] = *(dnp1110_mac + 3);
  195. addr[4] = *(dnp1110_mac + 4);
  196. addr[5] = *(dnp1110_mac + 5);
  197. }
  198. }
  199. #endif /* 0 */
  200. /***********************************************
  201. * Show available memory *
  202. ***********************************************/
  203. void dump_memory_info (void)
  204. {
  205. word mem_info;
  206. word old_bank;
  207. old_bank = SMC_inw (LAN91C96_BANK_SELECT) & 0xF;
  208. SMC_SELECT_BANK (0);
  209. mem_info = SMC_inw (LAN91C96_MIR);
  210. PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
  211. SMC_SELECT_BANK (old_bank);
  212. }
  213. /*
  214. * A rather simple routine to print out a packet for debugging purposes.
  215. */
  216. #if SMC_DEBUG > 2
  217. static void print_packet (byte *, int);
  218. #endif
  219. /* #define tx_done(dev) 1 */
  220. /* this does a soft reset on the device */
  221. static void smc_reset (void);
  222. /* Enable Interrupts, Receive, and Transmit */
  223. static void smc_enable (void);
  224. /* this puts the device in an inactive state */
  225. static void smc_shutdown (void);
  226. static int poll4int (byte mask, int timeout)
  227. {
  228. int tmo = get_timer (0) + timeout * CFG_HZ;
  229. int is_timeout = 0;
  230. word old_bank = SMC_inw (LAN91C96_BANK_SELECT);
  231. PRINTK2 ("Polling...\n");
  232. SMC_SELECT_BANK (2);
  233. while ((SMC_inw (LAN91C96_INT_STATS) & mask) == 0) {
  234. if (get_timer (0) >= tmo) {
  235. is_timeout = 1;
  236. break;
  237. }
  238. }
  239. /* restore old bank selection */
  240. SMC_SELECT_BANK (old_bank);
  241. if (is_timeout)
  242. return 1;
  243. else
  244. return 0;
  245. }
  246. /*
  247. * Function: smc_reset( void )
  248. * Purpose:
  249. * This sets the SMC91111 chip to its normal state, hopefully from whatever
  250. * mess that any other DOS driver has put it in.
  251. *
  252. * Maybe I should reset more registers to defaults in here? SOFTRST should
  253. * do that for me.
  254. *
  255. * Method:
  256. * 1. send a SOFT RESET
  257. * 2. wait for it to finish
  258. * 3. enable autorelease mode
  259. * 4. reset the memory management unit
  260. * 5. clear all interrupts
  261. *
  262. */
  263. static void smc_reset (void)
  264. {
  265. PRINTK2 ("%s:smc_reset\n", SMC_DEV_NAME);
  266. /* This resets the registers mostly to defaults, but doesn't
  267. affect EEPROM. That seems unnecessary */
  268. SMC_SELECT_BANK (0);
  269. SMC_outw (LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
  270. udelay (10);
  271. /* Disable transmit and receive functionality */
  272. SMC_outw (0, LAN91C96_RCR);
  273. SMC_outw (0, LAN91C96_TCR);
  274. /* set the control register */
  275. SMC_SELECT_BANK (1);
  276. SMC_outw (SMC_inw (LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
  277. LAN91C96_CONTROL);
  278. /* Disable all interrupts */
  279. SMC_outb (0, LAN91C96_INT_MASK);
  280. }
  281. /*
  282. * Function: smc_enable
  283. * Purpose: let the chip talk to the outside work
  284. * Method:
  285. * 1. Initialize the Memory Configuration Register
  286. * 2. Enable the transmitter
  287. * 3. Enable the receiver
  288. */
  289. static void smc_enable ()
  290. {
  291. PRINTK2 ("%s:smc_enable\n", SMC_DEV_NAME);
  292. SMC_SELECT_BANK (0);
  293. /* Initialize the Memory Configuration Register. See page
  294. 49 of the LAN91C96 data sheet for details. */
  295. SMC_outw (LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
  296. /* Initialize the Transmit Control Register */
  297. SMC_outw (LAN91C96_TCR_TXENA, LAN91C96_TCR);
  298. /* Initialize the Receive Control Register
  299. * FIXME:
  300. * The promiscuous bit set because I could not receive ARP reply
  301. * packets from the server when I send a ARP request. It only works
  302. * when I set the promiscuous bit
  303. */
  304. SMC_outw (LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
  305. }
  306. /*
  307. * Function: smc_shutdown
  308. * Purpose: closes down the SMC91xxx chip.
  309. * Method:
  310. * 1. zero the interrupt mask
  311. * 2. clear the enable receive flag
  312. * 3. clear the enable xmit flags
  313. *
  314. * TODO:
  315. * (1) maybe utilize power down mode.
  316. * Why not yet? Because while the chip will go into power down mode,
  317. * the manual says that it will wake up in response to any I/O requests
  318. * in the register space. Empirical results do not show this working.
  319. */
  320. static void smc_shutdown ()
  321. {
  322. PRINTK2 (CARDNAME ":smc_shutdown\n");
  323. /* no more interrupts for me */
  324. SMC_SELECT_BANK (2);
  325. SMC_outb (0, LAN91C96_INT_MASK);
  326. /* and tell the card to stay away from that nasty outside world */
  327. SMC_SELECT_BANK (0);
  328. SMC_outb (0, LAN91C96_RCR);
  329. SMC_outb (0, LAN91C96_TCR);
  330. }
  331. /*
  332. * Function: smc_hardware_send_packet(struct net_device * )
  333. * Purpose:
  334. * This sends the actual packet to the SMC9xxx chip.
  335. *
  336. * Algorithm:
  337. * First, see if a saved_skb is available.
  338. * ( this should NOT be called if there is no 'saved_skb'
  339. * Now, find the packet number that the chip allocated
  340. * Point the data pointers at it in memory
  341. * Set the length word in the chip's memory
  342. * Dump the packet to chip memory
  343. * Check if a last byte is needed ( odd length packet )
  344. * if so, set the control flag right
  345. * Tell the card to send it
  346. * Enable the transmit interrupt, so I know if it failed
  347. * Free the kernel data if I actually sent it.
  348. */
  349. static int smc_send_packet (volatile void *packet, int packet_length)
  350. {
  351. byte packet_no;
  352. unsigned long ioaddr;
  353. byte *buf;
  354. int length;
  355. int numPages;
  356. int try = 0;
  357. int time_out;
  358. byte status;
  359. PRINTK3 ("%s:smc_hardware_send_packet\n", SMC_DEV_NAME);
  360. length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
  361. /* allocate memory
  362. ** The MMU wants the number of pages to be the number of 256 bytes
  363. ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
  364. **
  365. ** The 91C111 ignores the size bits, but the code is left intact
  366. ** for backwards and future compatibility.
  367. **
  368. ** Pkt size for allocating is data length +6 (for additional status
  369. ** words, length and ctl!)
  370. **
  371. ** If odd size then last byte is included in this header.
  372. */
  373. numPages = ((length & 0xfffe) + 6);
  374. numPages >>= 8; /* Divide by 256 */
  375. if (numPages > 7) {
  376. printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
  377. return 0;
  378. }
  379. /* now, try to allocate the memory */
  380. SMC_SELECT_BANK (2);
  381. SMC_outw (LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
  382. again:
  383. try++;
  384. time_out = MEMORY_WAIT_TIME;
  385. do {
  386. status = SMC_inb (LAN91C96_INT_STATS);
  387. if (status & LAN91C96_IST_ALLOC_INT) {
  388. SMC_outb (LAN91C96_IST_ALLOC_INT, LAN91C96_INT_STATS);
  389. break;
  390. }
  391. } while (--time_out);
  392. if (!time_out) {
  393. PRINTK2 ("%s: memory allocation, try %d failed ...\n",
  394. SMC_DEV_NAME, try);
  395. if (try < SMC_ALLOC_MAX_TRY)
  396. goto again;
  397. else
  398. return 0;
  399. }
  400. PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
  401. SMC_DEV_NAME, try);
  402. /* I can send the packet now.. */
  403. ioaddr = SMC_BASE_ADDRESS;
  404. buf = (byte *) packet;
  405. /* If I get here, I _know_ there is a packet slot waiting for me */
  406. packet_no = SMC_inb (LAN91C96_ARR);
  407. if (packet_no & LAN91C96_ARR_FAILED) {
  408. /* or isn't there? BAD CHIP! */
  409. printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
  410. return 0;
  411. }
  412. /* we have a packet address, so tell the card to use it */
  413. SMC_outb (packet_no, LAN91C96_PNR);
  414. /* point to the beginning of the packet */
  415. SMC_outw (LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
  416. PRINTK3 ("%s: Trying to xmit packet of length %x\n",
  417. SMC_DEV_NAME, length);
  418. #if SMC_DEBUG > 2
  419. printf ("Transmitting Packet\n");
  420. print_packet (buf, length);
  421. #endif
  422. /* send the packet length ( +6 for status, length and ctl byte )
  423. and the status word ( set to zeros ) */
  424. #ifdef USE_32_BIT
  425. SMC_outl ((length + 6) << 16, LAN91C96_DATA_HIGH);
  426. #else
  427. SMC_outw (0, LAN91C96_DATA_HIGH);
  428. /* send the packet length ( +6 for status words, length, and ctl */
  429. SMC_outw ((length + 6), LAN91C96_DATA_HIGH);
  430. #endif /* USE_32_BIT */
  431. /* send the actual data
  432. * I _think_ it's faster to send the longs first, and then
  433. * mop up by sending the last word. It depends heavily
  434. * on alignment, at least on the 486. Maybe it would be
  435. * a good idea to check which is optimal? But that could take
  436. * almost as much time as is saved?
  437. */
  438. #ifdef USE_32_BIT
  439. SMC_outsl (LAN91C96_DATA_HIGH, buf, length >> 2);
  440. if (length & 0x2)
  441. SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
  442. LAN91C96_DATA_HIGH);
  443. #else
  444. SMC_outsw (LAN91C96_DATA_HIGH, buf, (length) >> 1);
  445. #endif /* USE_32_BIT */
  446. /* Send the last byte, if there is one. */
  447. if ((length & 1) == 0) {
  448. SMC_outw (0, LAN91C96_DATA_HIGH);
  449. } else {
  450. SMC_outw (buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
  451. }
  452. /* and let the chipset deal with it */
  453. SMC_outw (LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
  454. /* poll for TX INT */
  455. if (poll4int (LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
  456. /* sending failed */
  457. PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
  458. /* release packet */
  459. SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
  460. /* wait for MMU getting ready (low) */
  461. while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
  462. udelay (10);
  463. }
  464. PRINTK2 ("MMU ready\n");
  465. return 0;
  466. } else {
  467. /* ack. int */
  468. SMC_outw (LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
  469. PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME, length);
  470. /* release packet */
  471. SMC_outw (LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
  472. /* wait for MMU getting ready (low) */
  473. while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) {
  474. udelay (10);
  475. }
  476. PRINTK2 ("MMU ready\n");
  477. }
  478. return length;
  479. }
  480. /*-------------------------------------------------------------------------
  481. * smc_destructor( struct net_device * dev )
  482. * Input parameters:
  483. * dev, pointer to the device structure
  484. *
  485. * Output:
  486. * None.
  487. *--------------------------------------------------------------------------
  488. */
  489. void smc_destructor ()
  490. {
  491. PRINTK2 (CARDNAME ":smc_destructor\n");
  492. }
  493. /*
  494. * Open and Initialize the board
  495. *
  496. * Set up everything, reset the card, etc ..
  497. *
  498. */
  499. static int smc_open (bd_t *bd)
  500. {
  501. int i, err; /* used to set hw ethernet address */
  502. PRINTK2 ("%s:smc_open\n", SMC_DEV_NAME);
  503. /* reset the hardware */
  504. smc_reset ();
  505. smc_enable ();
  506. SMC_SELECT_BANK (1);
  507. err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
  508. if (err < 0) {
  509. memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
  510. return (-1); /* upper code ignores this, but NOT bi_enetaddr */
  511. }
  512. #ifdef USE_32_BIT
  513. for (i = 0; i < 6; i += 2) {
  514. word address;
  515. address = smc_mac_addr[i + 1] << 8;
  516. address |= smc_mac_addr[i];
  517. SMC_outw (address, LAN91C96_IA0 + i);
  518. }
  519. #else
  520. for (i = 0; i < 6; i++)
  521. SMC_outb (smc_mac_addr[i], LAN91C96_IA0 + i);
  522. #endif
  523. return 0;
  524. }
  525. /*-------------------------------------------------------------
  526. *
  527. * smc_rcv - receive a packet from the card
  528. *
  529. * There is ( at least ) a packet waiting to be read from
  530. * chip-memory.
  531. *
  532. * o Read the status
  533. * o If an error, record it
  534. * o otherwise, read in the packet
  535. *-------------------------------------------------------------
  536. */
  537. static int smc_rcv ()
  538. {
  539. int packet_number;
  540. word status;
  541. word packet_length;
  542. int is_error = 0;
  543. #ifdef USE_32_BIT
  544. dword stat_len;
  545. #endif
  546. SMC_SELECT_BANK (2);
  547. packet_number = SMC_inw (LAN91C96_FIFO);
  548. if (packet_number & LAN91C96_FIFO_RXEMPTY) {
  549. return 0;
  550. }
  551. PRINTK3 ("%s:smc_rcv\n", SMC_DEV_NAME);
  552. /* start reading from the start of the packet */
  553. SMC_outw (LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
  554. LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
  555. /* First two words are status and packet_length */
  556. #ifdef USE_32_BIT
  557. stat_len = SMC_inl (LAN91C96_DATA_HIGH);
  558. status = stat_len & 0xffff;
  559. packet_length = stat_len >> 16;
  560. #else
  561. status = SMC_inw (LAN91C96_DATA_HIGH);
  562. packet_length = SMC_inw (LAN91C96_DATA_HIGH);
  563. #endif
  564. packet_length &= 0x07ff; /* mask off top bits */
  565. PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
  566. if (!(status & FRAME_FILTER)) {
  567. /* Adjust for having already read the first two words */
  568. packet_length -= 4; /*4; */
  569. /* set odd length for bug in LAN91C111, */
  570. /* which never sets RS_ODDFRAME */
  571. /* TODO ? */
  572. #ifdef USE_32_BIT
  573. PRINTK3 (" Reading %d dwords (and %d bytes) \n",
  574. packet_length >> 2, packet_length & 3);
  575. /* QUESTION: Like in the TX routine, do I want
  576. to send the DWORDs or the bytes first, or some
  577. mixture. A mixture might improve already slow PIO
  578. performance */
  579. SMC_insl (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 2);
  580. /* read the left over bytes */
  581. if (packet_length & 3) {
  582. int i;
  583. byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
  584. dword leftover = SMC_inl (LAN91C96_DATA_HIGH);
  585. for (i = 0; i < (packet_length & 3); i++)
  586. *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
  587. }
  588. #else
  589. PRINTK3 (" Reading %d words and %d byte(s) \n",
  590. (packet_length >> 1), packet_length & 1);
  591. SMC_insw (LAN91C96_DATA_HIGH, NetRxPackets[0], packet_length >> 1);
  592. #endif /* USE_32_BIT */
  593. #if SMC_DEBUG > 2
  594. printf ("Receiving Packet\n");
  595. print_packet (NetRxPackets[0], packet_length);
  596. #endif
  597. } else {
  598. /* error ... */
  599. /* TODO ? */
  600. is_error = 1;
  601. }
  602. while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
  603. udelay (1); /* Wait until not busy */
  604. /* error or good, tell the card to get rid of this packet */
  605. SMC_outw (LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
  606. while (SMC_inw (LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
  607. udelay (1); /* Wait until not busy */
  608. if (!is_error) {
  609. /* Pass the packet up to the protocol layers. */
  610. NetReceive (NetRxPackets[0], packet_length);
  611. return packet_length;
  612. } else {
  613. return 0;
  614. }
  615. }
  616. /*----------------------------------------------------
  617. * smc_close
  618. *
  619. * this makes the board clean up everything that it can
  620. * and not talk to the outside world. Caused by
  621. * an 'ifconfig ethX down'
  622. *
  623. -----------------------------------------------------*/
  624. static int smc_close ()
  625. {
  626. PRINTK2 ("%s:smc_close\n", SMC_DEV_NAME);
  627. /* clear everything */
  628. smc_shutdown ();
  629. return 0;
  630. }
  631. #if SMC_DEBUG > 2
  632. static void print_packet (byte * buf, int length)
  633. {
  634. #if 0
  635. int i;
  636. int remainder;
  637. int lines;
  638. printf ("Packet of length %d \n", length);
  639. lines = length / 16;
  640. remainder = length % 16;
  641. for (i = 0; i < lines; i++) {
  642. int cur;
  643. for (cur = 0; cur < 8; cur++) {
  644. byte a, b;
  645. a = *(buf++);
  646. b = *(buf++);
  647. printf ("%02x%02x ", a, b);
  648. }
  649. printf ("\n");
  650. }
  651. for (i = 0; i < remainder / 2; i++) {
  652. byte a, b;
  653. a = *(buf++);
  654. b = *(buf++);
  655. printf ("%02x%02x ", a, b);
  656. }
  657. printf ("\n");
  658. #endif /* 0 */
  659. }
  660. #endif /* SMC_DEBUG > 2 */
  661. int eth_init (bd_t * bd)
  662. {
  663. return (smc_open(bd));
  664. }
  665. void eth_halt ()
  666. {
  667. smc_close ();
  668. }
  669. int eth_rx ()
  670. {
  671. return smc_rcv ();
  672. }
  673. int eth_send (volatile void *packet, int length)
  674. {
  675. return smc_send_packet (packet, length);
  676. }
  677. #if 0
  678. /*-------------------------------------------------------------------------
  679. * smc_hw_init()
  680. *
  681. * Function:
  682. * Reset and enable the device, check if the I/O space location
  683. * is correct
  684. *
  685. * Input parameters:
  686. * None
  687. *
  688. * Output:
  689. * 0 --> success
  690. * 1 --> error
  691. *--------------------------------------------------------------------------
  692. */
  693. static int smc_hw_init ()
  694. {
  695. unsigned short status_test;
  696. /* The attribute register of the LAN91C96 is located at address
  697. 0x0e000000 on the lubbock platform */
  698. volatile unsigned *attaddr = (unsigned *) (0x0e000000);
  699. /* first reset, then enable the device. Sequence is critical */
  700. attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_SRESET;
  701. udelay (100);
  702. attaddr[LAN91C96_ECOR] &= ~LAN91C96_ECOR_SRESET;
  703. attaddr[LAN91C96_ECOR] |= LAN91C96_ECOR_ENABLE;
  704. /* force 16-bit mode */
  705. attaddr[LAN91C96_ECSR] &= ~LAN91C96_ECSR_IOIS8;
  706. udelay (100);
  707. /* check if the I/O address is correct, the upper byte of the
  708. bank select register should read 0x33 */
  709. status_test = SMC_inw (LAN91C96_BANK_SELECT);
  710. if ((status_test & 0xFF00) != 0x3300) {
  711. printf ("Failed to initialize ethernetchip\n");
  712. return 1;
  713. }
  714. return 0;
  715. }
  716. #endif /* 0 */
  717. #endif /* COMMANDS & CFG_NET */
  718. /* smc_get_ethaddr (bd_t * bd)
  719. *
  720. * This checks both the environment and the ROM for an ethernet address. If
  721. * found, the environment takes precedence.
  722. */
  723. int smc_get_ethaddr (bd_t * bd)
  724. {
  725. int env_size = 0;
  726. int rom_valid = 0;
  727. int env_present = 0;
  728. int reg = 0;
  729. char *s = NULL;
  730. char *e = NULL;
  731. char *v_mac, es[] = "11:22:33:44:55:66";
  732. uchar s_env_mac[64];
  733. uchar v_env_mac[6];
  734. uchar v_rom_mac[6];
  735. env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
  736. if (env_size != sizeof(es)) { /* Ignore if env is bad or not set */
  737. printf ("\n*** Warning: ethaddr is not set properly, ignoring!!\n");
  738. } else {
  739. env_present = 1;
  740. s = s_env_mac;
  741. for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
  742. v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
  743. if (s)
  744. s = (*e) ? e + 1 : e;
  745. }
  746. }
  747. rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
  748. if (!env_present) { /* if NO env */
  749. if (rom_valid) { /* but ROM is valid */
  750. v_mac = v_rom_mac;
  751. sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
  752. v_mac[0], v_mac[1], v_mac[2], v_mac[3],
  753. v_mac[4], v_mac[5]);
  754. setenv ("ethaddr", s_env_mac);
  755. } else { /* no env, bad ROM */
  756. printf ("\n*** ERROR: ethaddr is NOT set !!\n");
  757. return (-1);
  758. }
  759. } else { /* good env, don't care ROM */
  760. v_mac = v_env_mac; /* always use a good env over a ROM */
  761. }
  762. if (env_present && rom_valid) { /* if both env and ROM are good */
  763. if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
  764. printf ("\nWarning: MAC addresses don't match:\n");
  765. printf ("\tHW MAC address: "
  766. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  767. v_rom_mac[0], v_rom_mac[1],
  768. v_rom_mac[2], v_rom_mac[3],
  769. v_rom_mac[4], v_rom_mac[5] );
  770. printf ("\t\"ethaddr\" value: "
  771. "%02X:%02X:%02X:%02X:%02X:%02X\n",
  772. v_env_mac[0], v_env_mac[1],
  773. v_env_mac[2], v_env_mac[3],
  774. v_env_mac[4], v_env_mac[5]) ;
  775. debug ("### Set MAC addr from environment\n");
  776. }
  777. }
  778. memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
  779. smc_set_mac_addr (v_mac); /* use old function to update smc default */
  780. PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
  781. v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
  782. return (0);
  783. }
  784. /*
  785. * get_rom_mac()
  786. * Note, this has omly been tested for the OMAP730 P2.
  787. */
  788. int get_rom_mac (char *v_rom_mac)
  789. {
  790. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  791. char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
  792. memcpy (v_rom_mac, hw_mac_addr, 6);
  793. return (1);
  794. #else
  795. int i;
  796. SMC_SELECT_BANK (1);
  797. for (i=0; i<6; i++)
  798. {
  799. v_rom_mac[i] = SMC_inb (LAN91C96_IA0 + i);
  800. }
  801. return (1);
  802. #endif
  803. }
  804. #endif /* CONFIG_DRIVER_LAN91C96 */