lan91c96.c 19 KB

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