lan91c96.c 22 KB

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