lan91c96.c 20 KB

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