lan91c96.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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 <malloc.h>
  63. #include "lan91c96.h"
  64. #include <net.h>
  65. #include <linux/compiler.h>
  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 DRIVER_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. /* See if a MAC address is defined in the current environment. If so use it. If not
  113. . print a warning and set the environment and other globals with the default.
  114. . If an EEPROM is present it really should be consulted.
  115. */
  116. static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
  117. static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
  118. /* ------------------------------------------------------------
  119. * Internal routines
  120. * ------------------------------------------------------------
  121. */
  122. static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
  123. /*
  124. * This function must be called before smc_open() if you want to override
  125. * the default mac address.
  126. */
  127. static void smc_set_mac_addr(const unsigned char *addr)
  128. {
  129. int i;
  130. for (i = 0; i < sizeof (smc_mac_addr); i++) {
  131. smc_mac_addr[i] = addr[i];
  132. }
  133. }
  134. /***********************************************
  135. * Show available memory *
  136. ***********************************************/
  137. void dump_memory_info(struct eth_device *dev)
  138. {
  139. __maybe_unused word mem_info;
  140. word old_bank;
  141. old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
  142. SMC_SELECT_BANK(dev, 0);
  143. mem_info = SMC_inw(dev, LAN91C96_MIR);
  144. PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
  145. SMC_SELECT_BANK(dev, old_bank);
  146. }
  147. /*
  148. * A rather simple routine to print out a packet for debugging purposes.
  149. */
  150. #if SMC_DEBUG > 2
  151. static void print_packet (byte *, int);
  152. #endif
  153. static int poll4int (struct eth_device *dev, byte mask, int timeout)
  154. {
  155. int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
  156. int is_timeout = 0;
  157. word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
  158. PRINTK2 ("Polling...\n");
  159. SMC_SELECT_BANK(dev, 2);
  160. while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
  161. if (get_timer (0) >= tmo) {
  162. is_timeout = 1;
  163. break;
  164. }
  165. }
  166. /* restore old bank selection */
  167. SMC_SELECT_BANK(dev, old_bank);
  168. if (is_timeout)
  169. return 1;
  170. else
  171. return 0;
  172. }
  173. /*
  174. * Function: smc_reset
  175. * Purpose:
  176. * This sets the SMC91111 chip to its normal state, hopefully from whatever
  177. * mess that any other DOS driver has put it in.
  178. *
  179. * Maybe I should reset more registers to defaults in here? SOFTRST should
  180. * do that for me.
  181. *
  182. * Method:
  183. * 1. send a SOFT RESET
  184. * 2. wait for it to finish
  185. * 3. enable autorelease mode
  186. * 4. reset the memory management unit
  187. * 5. clear all interrupts
  188. *
  189. */
  190. static void smc_reset(struct eth_device *dev)
  191. {
  192. PRINTK2("%s:smc_reset\n", dev->name);
  193. /* This resets the registers mostly to defaults, but doesn't
  194. affect EEPROM. That seems unnecessary */
  195. SMC_SELECT_BANK(dev, 0);
  196. SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
  197. udelay (10);
  198. /* Disable transmit and receive functionality */
  199. SMC_outw(dev, 0, LAN91C96_RCR);
  200. SMC_outw(dev, 0, LAN91C96_TCR);
  201. /* set the control register */
  202. SMC_SELECT_BANK(dev, 1);
  203. SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
  204. LAN91C96_CONTROL);
  205. /* Disable all interrupts */
  206. SMC_outb(dev, 0, LAN91C96_INT_MASK);
  207. }
  208. /*
  209. * Function: smc_enable
  210. * Purpose: let the chip talk to the outside work
  211. * Method:
  212. * 1. Initialize the Memory Configuration Register
  213. * 2. Enable the transmitter
  214. * 3. Enable the receiver
  215. */
  216. static void smc_enable(struct eth_device *dev)
  217. {
  218. PRINTK2("%s:smc_enable\n", dev->name);
  219. SMC_SELECT_BANK(dev, 0);
  220. /* Initialize the Memory Configuration Register. See page
  221. 49 of the LAN91C96 data sheet for details. */
  222. SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
  223. /* Initialize the Transmit Control Register */
  224. SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
  225. /* Initialize the Receive Control Register
  226. * FIXME:
  227. * The promiscuous bit set because I could not receive ARP reply
  228. * packets from the server when I send a ARP request. It only works
  229. * when I set the promiscuous bit
  230. */
  231. SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
  232. }
  233. /*
  234. * Function: smc_shutdown
  235. * Purpose: closes down the SMC91xxx chip.
  236. * Method:
  237. * 1. zero the interrupt mask
  238. * 2. clear the enable receive flag
  239. * 3. clear the enable xmit flags
  240. *
  241. * TODO:
  242. * (1) maybe utilize power down mode.
  243. * Why not yet? Because while the chip will go into power down mode,
  244. * the manual says that it will wake up in response to any I/O requests
  245. * in the register space. Empirical results do not show this working.
  246. */
  247. static void smc_shutdown(struct eth_device *dev)
  248. {
  249. PRINTK2("%s:smc_shutdown\n", dev->name);
  250. /* no more interrupts for me */
  251. SMC_SELECT_BANK(dev, 2);
  252. SMC_outb(dev, 0, LAN91C96_INT_MASK);
  253. /* and tell the card to stay away from that nasty outside world */
  254. SMC_SELECT_BANK(dev, 0);
  255. SMC_outb(dev, 0, LAN91C96_RCR);
  256. SMC_outb(dev, 0, LAN91C96_TCR);
  257. }
  258. /*
  259. * Function: smc_hardware_send_packet(struct net_device * )
  260. * Purpose:
  261. * This sends the actual packet to the SMC9xxx chip.
  262. *
  263. * Algorithm:
  264. * First, see if a saved_skb is available.
  265. * ( this should NOT be called if there is no 'saved_skb'
  266. * Now, find the packet number that the chip allocated
  267. * Point the data pointers at it in memory
  268. * Set the length word in the chip's memory
  269. * Dump the packet to chip memory
  270. * Check if a last byte is needed ( odd length packet )
  271. * if so, set the control flag right
  272. * Tell the card to send it
  273. * Enable the transmit interrupt, so I know if it failed
  274. * Free the kernel data if I actually sent it.
  275. */
  276. static int smc_send_packet(struct eth_device *dev, void *packet,
  277. int packet_length)
  278. {
  279. byte packet_no;
  280. byte *buf;
  281. int length;
  282. int numPages;
  283. int try = 0;
  284. int time_out;
  285. byte status;
  286. PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
  287. length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
  288. /* allocate memory
  289. ** The MMU wants the number of pages to be the number of 256 bytes
  290. ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
  291. **
  292. ** The 91C111 ignores the size bits, but the code is left intact
  293. ** for backwards and future compatibility.
  294. **
  295. ** Pkt size for allocating is data length +6 (for additional status
  296. ** words, length and ctl!)
  297. **
  298. ** If odd size then last byte is included in this header.
  299. */
  300. numPages = ((length & 0xfffe) + 6);
  301. numPages >>= 8; /* Divide by 256 */
  302. if (numPages > 7) {
  303. printf("%s: Far too big packet error. \n", dev->name);
  304. return 0;
  305. }
  306. /* now, try to allocate the memory */
  307. SMC_SELECT_BANK(dev, 2);
  308. SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
  309. again:
  310. try++;
  311. time_out = MEMORY_WAIT_TIME;
  312. do {
  313. status = SMC_inb(dev, LAN91C96_INT_STATS);
  314. if (status & LAN91C96_IST_ALLOC_INT) {
  315. SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
  316. LAN91C96_INT_STATS);
  317. break;
  318. }
  319. } while (--time_out);
  320. if (!time_out) {
  321. PRINTK2 ("%s: memory allocation, try %d failed ...\n",
  322. dev->name, try);
  323. if (try < SMC_ALLOC_MAX_TRY)
  324. goto again;
  325. else
  326. return 0;
  327. }
  328. PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
  329. dev->name, try);
  330. /* I can send the packet now.. */
  331. buf = (byte *) packet;
  332. /* If I get here, I _know_ there is a packet slot waiting for me */
  333. packet_no = SMC_inb(dev, LAN91C96_ARR);
  334. if (packet_no & LAN91C96_ARR_FAILED) {
  335. /* or isn't there? BAD CHIP! */
  336. printf("%s: Memory allocation failed. \n", dev->name);
  337. return 0;
  338. }
  339. /* we have a packet address, so tell the card to use it */
  340. SMC_outb(dev, packet_no, LAN91C96_PNR);
  341. /* point to the beginning of the packet */
  342. SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
  343. PRINTK3("%s: Trying to xmit packet of length %x\n",
  344. dev->name, length);
  345. #if SMC_DEBUG > 2
  346. printf ("Transmitting Packet\n");
  347. print_packet (buf, length);
  348. #endif
  349. /* send the packet length ( +6 for status, length and ctl byte )
  350. and the status word ( set to zeros ) */
  351. #ifdef USE_32_BIT
  352. SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
  353. #else
  354. SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
  355. /* send the packet length ( +6 for status words, length, and ctl */
  356. SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
  357. #endif /* USE_32_BIT */
  358. /* send the actual data
  359. * I _think_ it's faster to send the longs first, and then
  360. * mop up by sending the last word. It depends heavily
  361. * on alignment, at least on the 486. Maybe it would be
  362. * a good idea to check which is optimal? But that could take
  363. * almost as much time as is saved?
  364. */
  365. #ifdef USE_32_BIT
  366. SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
  367. if (length & 0x2)
  368. SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
  369. LAN91C96_DATA_HIGH);
  370. #else
  371. SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
  372. #endif /* USE_32_BIT */
  373. /* Send the last byte, if there is one. */
  374. if ((length & 1) == 0) {
  375. SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
  376. } else {
  377. SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
  378. }
  379. /* and let the chipset deal with it */
  380. SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
  381. /* poll for TX INT */
  382. if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
  383. /* sending failed */
  384. PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
  385. /* release packet */
  386. SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
  387. /* wait for MMU getting ready (low) */
  388. while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
  389. udelay (10);
  390. PRINTK2("MMU ready\n");
  391. return 0;
  392. } else {
  393. /* ack. int */
  394. SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
  395. PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
  396. /* release packet */
  397. SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
  398. /* wait for MMU getting ready (low) */
  399. while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
  400. udelay (10);
  401. PRINTK2 ("MMU ready\n");
  402. }
  403. return length;
  404. }
  405. /*
  406. * Open and Initialize the board
  407. *
  408. * Set up everything, reset the card, etc ..
  409. *
  410. */
  411. static int smc_open(bd_t *bd, struct eth_device *dev)
  412. {
  413. int i, err; /* used to set hw ethernet address */
  414. PRINTK2("%s:smc_open\n", dev->name);
  415. /* reset the hardware */
  416. smc_reset(dev);
  417. smc_enable(dev);
  418. SMC_SELECT_BANK(dev, 1);
  419. /* set smc_mac_addr, and sync it with u-boot globals */
  420. err = smc_get_ethaddr(bd, dev);
  421. if (err < 0)
  422. return -1;
  423. #ifdef USE_32_BIT
  424. for (i = 0; i < 6; i += 2) {
  425. word address;
  426. address = smc_mac_addr[i + 1] << 8;
  427. address |= smc_mac_addr[i];
  428. SMC_outw(dev, address, LAN91C96_IA0 + i);
  429. }
  430. #else
  431. for (i = 0; i < 6; i++)
  432. SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
  433. #endif
  434. return 0;
  435. }
  436. /*-------------------------------------------------------------
  437. *
  438. * smc_rcv - receive a packet from the card
  439. *
  440. * There is ( at least ) a packet waiting to be read from
  441. * chip-memory.
  442. *
  443. * o Read the status
  444. * o If an error, record it
  445. * o otherwise, read in the packet
  446. *-------------------------------------------------------------
  447. */
  448. static int smc_rcv(struct eth_device *dev)
  449. {
  450. int packet_number;
  451. word status;
  452. word packet_length;
  453. int is_error = 0;
  454. #ifdef USE_32_BIT
  455. dword stat_len;
  456. #endif
  457. SMC_SELECT_BANK(dev, 2);
  458. packet_number = SMC_inw(dev, LAN91C96_FIFO);
  459. if (packet_number & LAN91C96_FIFO_RXEMPTY) {
  460. return 0;
  461. }
  462. PRINTK3("%s:smc_rcv\n", dev->name);
  463. /* start reading from the start of the packet */
  464. SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
  465. LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
  466. /* First two words are status and packet_length */
  467. #ifdef USE_32_BIT
  468. stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
  469. status = stat_len & 0xffff;
  470. packet_length = stat_len >> 16;
  471. #else
  472. status = SMC_inw(dev, LAN91C96_DATA_HIGH);
  473. packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
  474. #endif
  475. packet_length &= 0x07ff; /* mask off top bits */
  476. PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
  477. if (!(status & FRAME_FILTER)) {
  478. /* Adjust for having already read the first two words */
  479. packet_length -= 4; /*4; */
  480. /* set odd length for bug in LAN91C111, */
  481. /* which never sets RS_ODDFRAME */
  482. /* TODO ? */
  483. #ifdef USE_32_BIT
  484. PRINTK3 (" Reading %d dwords (and %d bytes) \n",
  485. packet_length >> 2, packet_length & 3);
  486. /* QUESTION: Like in the TX routine, do I want
  487. to send the DWORDs or the bytes first, or some
  488. mixture. A mixture might improve already slow PIO
  489. performance */
  490. SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
  491. packet_length >> 2);
  492. /* read the left over bytes */
  493. if (packet_length & 3) {
  494. int i;
  495. byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
  496. dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
  497. for (i = 0; i < (packet_length & 3); i++)
  498. *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
  499. }
  500. #else
  501. PRINTK3 (" Reading %d words and %d byte(s) \n",
  502. (packet_length >> 1), packet_length & 1);
  503. SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
  504. packet_length >> 1);
  505. #endif /* USE_32_BIT */
  506. #if SMC_DEBUG > 2
  507. printf ("Receiving Packet\n");
  508. print_packet((byte *)NetRxPackets[0], packet_length);
  509. #endif
  510. } else {
  511. /* error ... */
  512. /* TODO ? */
  513. is_error = 1;
  514. }
  515. while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
  516. udelay (1); /* Wait until not busy */
  517. /* error or good, tell the card to get rid of this packet */
  518. SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
  519. while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
  520. udelay (1); /* Wait until not busy */
  521. if (!is_error) {
  522. /* Pass the packet up to the protocol layers. */
  523. NetReceive (NetRxPackets[0], packet_length);
  524. return packet_length;
  525. } else {
  526. return 0;
  527. }
  528. }
  529. /*----------------------------------------------------
  530. * smc_close
  531. *
  532. * this makes the board clean up everything that it can
  533. * and not talk to the outside world. Caused by
  534. * an 'ifconfig ethX down'
  535. *
  536. -----------------------------------------------------*/
  537. static int smc_close(struct eth_device *dev)
  538. {
  539. PRINTK2("%s:smc_close\n", dev->name);
  540. /* clear everything */
  541. smc_shutdown(dev);
  542. return 0;
  543. }
  544. #if SMC_DEBUG > 2
  545. static void print_packet(byte *buf, int length)
  546. {
  547. #if 0
  548. int i;
  549. int remainder;
  550. int lines;
  551. printf ("Packet of length %d \n", length);
  552. lines = length / 16;
  553. remainder = length % 16;
  554. for (i = 0; i < lines; i++) {
  555. int cur;
  556. for (cur = 0; cur < 8; cur++) {
  557. byte a, b;
  558. a = *(buf++);
  559. b = *(buf++);
  560. printf ("%02x%02x ", a, b);
  561. }
  562. printf ("\n");
  563. }
  564. for (i = 0; i < remainder / 2; i++) {
  565. byte a, b;
  566. a = *(buf++);
  567. b = *(buf++);
  568. printf ("%02x%02x ", a, b);
  569. }
  570. printf ("\n");
  571. #endif /* 0 */
  572. }
  573. #endif /* SMC_DEBUG > 2 */
  574. static int lan91c96_init(struct eth_device *dev, bd_t *bd)
  575. {
  576. return smc_open(bd, dev);
  577. }
  578. static void lan91c96_halt(struct eth_device *dev)
  579. {
  580. smc_close(dev);
  581. }
  582. static int lan91c96_recv(struct eth_device *dev)
  583. {
  584. return smc_rcv(dev);
  585. }
  586. static int lan91c96_send(struct eth_device *dev, void *packet,
  587. int length)
  588. {
  589. return smc_send_packet(dev, packet, length);
  590. }
  591. /* smc_get_ethaddr
  592. *
  593. * This checks both the environment and the ROM for an ethernet address. If
  594. * found, the environment takes precedence.
  595. */
  596. static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
  597. {
  598. uchar v_mac[6];
  599. if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
  600. /* get ROM mac value if any */
  601. if (!get_rom_mac(dev, v_mac)) {
  602. printf("\n*** ERROR: ethaddr is NOT set !!\n");
  603. return -1;
  604. }
  605. eth_setenv_enetaddr("ethaddr", v_mac);
  606. }
  607. smc_set_mac_addr(v_mac); /* use old function to update smc default */
  608. PRINTK("Using MAC Address %pM\n", v_mac);
  609. return 0;
  610. }
  611. /*
  612. * get_rom_mac()
  613. * Note, this has omly been tested for the OMAP730 P2.
  614. */
  615. static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
  616. {
  617. #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
  618. char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
  619. memcpy (v_rom_mac, hw_mac_addr, 6);
  620. return (1);
  621. #else
  622. int i;
  623. SMC_SELECT_BANK(dev, 1);
  624. for (i=0; i<6; i++)
  625. {
  626. v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
  627. }
  628. return (1);
  629. #endif
  630. }
  631. /* Structure to detect the device IDs */
  632. struct id_type {
  633. u8 id;
  634. char *name;
  635. };
  636. static struct id_type supported_chips[] = {
  637. {0, ""}, /* Dummy entry to prevent id check failure */
  638. {9, "LAN91C110"},
  639. {8, "LAN91C100FD"},
  640. {7, "LAN91C100"},
  641. {5, "LAN91C95"},
  642. {4, "LAN91C94/96"},
  643. {3, "LAN91C90/92"},
  644. };
  645. /* lan91c96_detect_chip
  646. * See:
  647. * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
  648. * page 71 - that is the closest we get to detect this device
  649. */
  650. static int lan91c96_detect_chip(struct eth_device *dev)
  651. {
  652. u8 chip_id;
  653. int r;
  654. SMC_SELECT_BANK(dev, 3);
  655. chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
  656. SMC_SELECT_BANK(dev, 0);
  657. for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++)
  658. if (chip_id == supported_chips[r].id)
  659. return r;
  660. return 0;
  661. }
  662. int lan91c96_initialize(u8 dev_num, int base_addr)
  663. {
  664. struct eth_device *dev;
  665. int r = 0;
  666. dev = malloc(sizeof(*dev));
  667. if (!dev) {
  668. return 0;
  669. }
  670. memset(dev, 0, sizeof(*dev));
  671. dev->iobase = base_addr;
  672. /* Try to detect chip. Will fail if not present. */
  673. r = lan91c96_detect_chip(dev);
  674. if (!r) {
  675. free(dev);
  676. return 0;
  677. }
  678. get_rom_mac(dev, dev->enetaddr);
  679. dev->init = lan91c96_init;
  680. dev->halt = lan91c96_halt;
  681. dev->send = lan91c96_send;
  682. dev->recv = lan91c96_recv;
  683. sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
  684. eth_register(dev);
  685. return 0;
  686. }