lan91c96.c 20 KB

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