1 /*------------------------------------------------------------------------
3 * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
4 * on the SMC91111 driver from U-boot.
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Rolf Offermanns <rof@sysgo.de>
10 * Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
11 * Developed by Simple Network Magic Corporation (SNMC)
12 * Copyright (C) 1996 by Erik Stahlman (ES)
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.
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.
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
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.
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 )
39 * io = for the base address
43 * Erik Stahlman ( erik@vt.edu )
44 * Daris A Nevil ( dnevil@snmc.com )
47 * Hardware multicast code from Peter Cammaert ( pc@denkart.be )
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)
56 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
58 *---------------------------------------------------------------------------
67 /*------------------------------------------------------------------------
69 * Configuration options, for the experienced user to change.
71 -------------------------------------------------------------------------*/
73 /* Use power-down feature of the chip */
77 * Wait time for memory to be free. This probably shouldn't be
78 * tuned that much, as waiting for this means nothing else happens
81 #define MEMORY_WAIT_TIME 16
86 #define PRINTK3(args...) printf(args)
88 #define PRINTK3(args...)
92 #define PRINTK2(args...) printf(args)
94 #define PRINTK2(args...)
98 #define PRINTK(args...) printf(args)
100 #define PRINTK(args...)
104 /*------------------------------------------------------------------------
106 * The internal workings of the driver. If you are changing anything
107 * here with the SMC stuff, you should have the datasheet and know
108 * what you are doing.
110 *------------------------------------------------------------------------
112 #define DRIVER_NAME "LAN91C96"
113 #define SMC_ALLOC_MAX_TRY 5
114 #define SMC_TX_TIMEOUT 30
118 #ifdef CONFIG_LAN91C96_USE_32_BIT
124 /* See if a MAC address is defined in the current environment. If so use it. If not
125 . print a warning and set the environment and other globals with the default.
126 . If an EEPROM is present it really should be consulted.
128 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
129 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
131 /* ------------------------------------------------------------
133 * ------------------------------------------------------------
136 static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
139 * This function must be called before smc_open() if you want to override
140 * the default mac address.
143 static void smc_set_mac_addr(const unsigned char *addr)
147 for (i = 0; i < sizeof (smc_mac_addr); i++) {
148 smc_mac_addr[i] = addr[i];
152 /***********************************************
153 * Show available memory *
154 ***********************************************/
155 void dump_memory_info(struct eth_device *dev)
160 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
162 SMC_SELECT_BANK(dev, 0);
163 mem_info = SMC_inw(dev, LAN91C96_MIR);
164 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
166 SMC_SELECT_BANK(dev, old_bank);
170 * A rather simple routine to print out a packet for debugging purposes.
173 static void print_packet (byte *, int);
176 static int poll4int (struct eth_device *dev, byte mask, int timeout)
178 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
180 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
182 PRINTK2 ("Polling...\n");
183 SMC_SELECT_BANK(dev, 2);
184 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
185 if (get_timer (0) >= tmo) {
191 /* restore old bank selection */
192 SMC_SELECT_BANK(dev, old_bank);
201 * Function: smc_reset
203 * This sets the SMC91111 chip to its normal state, hopefully from whatever
204 * mess that any other DOS driver has put it in.
206 * Maybe I should reset more registers to defaults in here? SOFTRST should
210 * 1. send a SOFT RESET
211 * 2. wait for it to finish
212 * 3. enable autorelease mode
213 * 4. reset the memory management unit
214 * 5. clear all interrupts
217 static void smc_reset(struct eth_device *dev)
219 PRINTK2("%s:smc_reset\n", dev->name);
221 /* This resets the registers mostly to defaults, but doesn't
222 affect EEPROM. That seems unnecessary */
223 SMC_SELECT_BANK(dev, 0);
224 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
228 /* Disable transmit and receive functionality */
229 SMC_outw(dev, 0, LAN91C96_RCR);
230 SMC_outw(dev, 0, LAN91C96_TCR);
232 /* set the control register */
233 SMC_SELECT_BANK(dev, 1);
234 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
237 /* Disable all interrupts */
238 SMC_outb(dev, 0, LAN91C96_INT_MASK);
242 * Function: smc_enable
243 * Purpose: let the chip talk to the outside work
245 * 1. Initialize the Memory Configuration Register
246 * 2. Enable the transmitter
247 * 3. Enable the receiver
249 static void smc_enable(struct eth_device *dev)
251 PRINTK2("%s:smc_enable\n", dev->name);
252 SMC_SELECT_BANK(dev, 0);
254 /* Initialize the Memory Configuration Register. See page
255 49 of the LAN91C96 data sheet for details. */
256 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
258 /* Initialize the Transmit Control Register */
259 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
260 /* Initialize the Receive Control Register
262 * The promiscuous bit set because I could not receive ARP reply
263 * packets from the server when I send a ARP request. It only works
264 * when I set the promiscuous bit
266 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
270 * Function: smc_shutdown
271 * Purpose: closes down the SMC91xxx chip.
273 * 1. zero the interrupt mask
274 * 2. clear the enable receive flag
275 * 3. clear the enable xmit flags
278 * (1) maybe utilize power down mode.
279 * Why not yet? Because while the chip will go into power down mode,
280 * the manual says that it will wake up in response to any I/O requests
281 * in the register space. Empirical results do not show this working.
283 static void smc_shutdown(struct eth_device *dev)
285 PRINTK2("%s:smc_shutdown\n", dev->name);
287 /* no more interrupts for me */
288 SMC_SELECT_BANK(dev, 2);
289 SMC_outb(dev, 0, LAN91C96_INT_MASK);
291 /* and tell the card to stay away from that nasty outside world */
292 SMC_SELECT_BANK(dev, 0);
293 SMC_outb(dev, 0, LAN91C96_RCR);
294 SMC_outb(dev, 0, LAN91C96_TCR);
299 * Function: smc_hardware_send_packet(struct net_device * )
301 * This sends the actual packet to the SMC9xxx chip.
304 * First, see if a saved_skb is available.
305 * ( this should NOT be called if there is no 'saved_skb'
306 * Now, find the packet number that the chip allocated
307 * Point the data pointers at it in memory
308 * Set the length word in the chip's memory
309 * Dump the packet to chip memory
310 * Check if a last byte is needed ( odd length packet )
311 * if so, set the control flag right
312 * Tell the card to send it
313 * Enable the transmit interrupt, so I know if it failed
314 * Free the kernel data if I actually sent it.
316 static int smc_send_packet(struct eth_device *dev, volatile void *packet,
320 unsigned long ioaddr;
329 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
331 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
334 ** The MMU wants the number of pages to be the number of 256 bytes
335 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
337 ** The 91C111 ignores the size bits, but the code is left intact
338 ** for backwards and future compatibility.
340 ** Pkt size for allocating is data length +6 (for additional status
341 ** words, length and ctl!)
343 ** If odd size then last byte is included in this header.
345 numPages = ((length & 0xfffe) + 6);
346 numPages >>= 8; /* Divide by 256 */
349 printf("%s: Far too big packet error. \n", dev->name);
353 /* now, try to allocate the memory */
355 SMC_SELECT_BANK(dev, 2);
356 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
360 time_out = MEMORY_WAIT_TIME;
362 status = SMC_inb(dev, LAN91C96_INT_STATS);
363 if (status & LAN91C96_IST_ALLOC_INT) {
365 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
369 } while (--time_out);
372 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
374 if (try < SMC_ALLOC_MAX_TRY)
380 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
383 /* I can send the packet now.. */
385 ioaddr = dev->iobase;
387 buf = (byte *) packet;
389 /* If I get here, I _know_ there is a packet slot waiting for me */
390 packet_no = SMC_inb(dev, LAN91C96_ARR);
391 if (packet_no & LAN91C96_ARR_FAILED) {
392 /* or isn't there? BAD CHIP! */
393 printf("%s: Memory allocation failed. \n", dev->name);
397 /* we have a packet address, so tell the card to use it */
398 SMC_outb(dev, packet_no, LAN91C96_PNR);
400 /* point to the beginning of the packet */
401 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
403 PRINTK3("%s: Trying to xmit packet of length %x\n",
407 printf ("Transmitting Packet\n");
408 print_packet (buf, length);
411 /* send the packet length ( +6 for status, length and ctl byte )
412 and the status word ( set to zeros ) */
414 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
416 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
417 /* send the packet length ( +6 for status words, length, and ctl */
418 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
419 #endif /* USE_32_BIT */
421 /* send the actual data
422 * I _think_ it's faster to send the longs first, and then
423 * mop up by sending the last word. It depends heavily
424 * on alignment, at least on the 486. Maybe it would be
425 * a good idea to check which is optimal? But that could take
426 * almost as much time as is saved?
429 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
431 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
434 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
435 #endif /* USE_32_BIT */
437 /* Send the last byte, if there is one. */
438 if ((length & 1) == 0) {
439 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
441 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
444 /* and let the chipset deal with it */
445 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
447 /* poll for TX INT */
448 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
450 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
453 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
455 /* wait for MMU getting ready (low) */
456 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
459 PRINTK2("MMU ready\n");
465 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
467 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
470 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
472 /* wait for MMU getting ready (low) */
473 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
476 PRINTK2 ("MMU ready\n");
484 * Open and Initialize the board
486 * Set up everything, reset the card, etc ..
489 static int smc_open(bd_t *bd, struct eth_device *dev)
491 int i, err; /* used to set hw ethernet address */
493 PRINTK2("%s:smc_open\n", dev->name);
495 /* reset the hardware */
500 SMC_SELECT_BANK(dev, 1);
501 /* set smc_mac_addr, and sync it with u-boot globals */
502 err = smc_get_ethaddr(bd, dev);
506 for (i = 0; i < 6; i += 2) {
509 address = smc_mac_addr[i + 1] << 8;
510 address |= smc_mac_addr[i];
511 SMC_outw(dev, address, LAN91C96_IA0 + i);
514 for (i = 0; i < 6; i++)
515 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
520 /*-------------------------------------------------------------
522 * smc_rcv - receive a packet from the card
524 * There is ( at least ) a packet waiting to be read from
528 * o If an error, record it
529 * o otherwise, read in the packet
530 *-------------------------------------------------------------
532 static int smc_rcv(struct eth_device *dev)
544 SMC_SELECT_BANK(dev, 2);
545 packet_number = SMC_inw(dev, LAN91C96_FIFO);
547 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
551 PRINTK3("%s:smc_rcv\n", dev->name);
552 /* start reading from the start of the packet */
553 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
554 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
556 /* First two words are status and packet_length */
558 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
559 status = stat_len & 0xffff;
560 packet_length = stat_len >> 16;
562 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
563 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
566 packet_length &= 0x07ff; /* mask off top bits */
568 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
570 if (!(status & FRAME_FILTER)) {
571 /* Adjust for having already read the first two words */
572 packet_length -= 4; /*4; */
575 /* set odd length for bug in LAN91C111, */
576 /* which never sets RS_ODDFRAME */
581 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
582 packet_length >> 2, packet_length & 3);
583 /* QUESTION: Like in the TX routine, do I want
584 to send the DWORDs or the bytes first, or some
585 mixture. A mixture might improve already slow PIO
587 SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
589 /* read the left over bytes */
590 if (packet_length & 3) {
593 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
594 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
596 for (i = 0; i < (packet_length & 3); i++)
597 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
600 PRINTK3 (" Reading %d words and %d byte(s) \n",
601 (packet_length >> 1), packet_length & 1);
602 SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
605 #endif /* USE_32_BIT */
608 printf ("Receiving Packet\n");
609 print_packet((byte *)NetRxPackets[0], packet_length);
617 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
618 udelay (1); /* Wait until not busy */
620 /* error or good, tell the card to get rid of this packet */
621 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
623 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
624 udelay (1); /* Wait until not busy */
627 /* Pass the packet up to the protocol layers. */
628 NetReceive (NetRxPackets[0], packet_length);
629 return packet_length;
636 /*----------------------------------------------------
639 * this makes the board clean up everything that it can
640 * and not talk to the outside world. Caused by
641 * an 'ifconfig ethX down'
643 -----------------------------------------------------*/
644 static int smc_close(struct eth_device *dev)
646 PRINTK2("%s:smc_close\n", dev->name);
648 /* clear everything */
655 static void print_packet(byte *buf, int length)
662 printf ("Packet of length %d \n", length);
665 remainder = length % 16;
667 for (i = 0; i < lines; i++) {
670 for (cur = 0; cur < 8; cur++) {
675 printf ("%02x%02x ", a, b);
679 for (i = 0; i < remainder / 2; i++) {
684 printf ("%02x%02x ", a, b);
689 #endif /* SMC_DEBUG > 2 */
691 static int lan91c96_init(struct eth_device *dev, bd_t *bd)
693 return smc_open(bd, dev);
696 static void lan91c96_halt(struct eth_device *dev)
701 static int lan91c96_recv(struct eth_device *dev)
706 static int lan91c96_send(struct eth_device *dev, volatile void *packet,
709 return smc_send_packet(dev, packet, length);
714 * This checks both the environment and the ROM for an ethernet address. If
715 * found, the environment takes precedence.
718 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
722 if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
723 /* get ROM mac value if any */
724 if (!get_rom_mac(dev, v_mac)) {
725 printf("\n*** ERROR: ethaddr is NOT set !!\n");
728 eth_setenv_enetaddr("ethaddr", v_mac);
731 smc_set_mac_addr(v_mac); /* use old function to update smc default */
732 PRINTK("Using MAC Address %pM\n", v_mac);
738 * Note, this has omly been tested for the OMAP730 P2.
741 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
743 #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
744 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
746 memcpy (v_rom_mac, hw_mac_addr, 6);
750 SMC_SELECT_BANK(dev, 1);
753 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
759 /* Structure to detect the device IDs */
764 static struct id_type supported_chips[] = {
765 {0, ""}, /* Dummy entry to prevent id check failure */
770 {4, "LAN91C94/LAN91C96"},
771 {3, "LAN91C90/LAN91C92"},
773 /* lan91c96_detect_chip
775 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
776 * page 71 - that is the closest we get to detect this device
778 static int lan91c96_detect_chip(struct eth_device *dev)
782 SMC_SELECT_BANK(dev, 3);
783 chip_id = SMC_inw(dev, 0xA) & LAN91C96_REV_REVID;
784 SMC_SELECT_BANK(dev, 0);
785 for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++)
786 if (chip_id == supported_chips[r].id)
791 int lan91c96_initialize(u8 dev_num, int base_addr)
793 struct eth_device *dev;
796 dev = malloc(sizeof(*dev));
800 memset(dev, 0, sizeof(*dev));
802 dev->iobase = base_addr;
804 /* Try to detect chip. Will fail if not present. */
805 r = lan91c96_detect_chip(dev);
810 get_rom_mac(dev, dev->enetaddr);
812 dev->init = lan91c96_init;
813 dev->halt = lan91c96_halt;
814 dev->send = lan91c96_send;
815 dev->recv = lan91c96_recv;
816 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);