lan91c96.c 19 KB

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