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 *---------------------------------------------------------------------------
66 #include <linux/compiler.h>
68 /*------------------------------------------------------------------------
70 * Configuration options, for the experienced user to change.
72 -------------------------------------------------------------------------*/
74 /* Use power-down feature of the chip */
78 * Wait time for memory to be free. This probably shouldn't be
79 * tuned that much, as waiting for this means nothing else happens
82 #define MEMORY_WAIT_TIME 16
87 #define PRINTK3(args...) printf(args)
89 #define PRINTK3(args...)
93 #define PRINTK2(args...) printf(args)
95 #define PRINTK2(args...)
99 #define PRINTK(args...) printf(args)
101 #define PRINTK(args...)
105 /*------------------------------------------------------------------------
107 * The internal workings of the driver. If you are changing anything
108 * here with the SMC stuff, you should have the datasheet and know
109 * what you are doing.
111 *------------------------------------------------------------------------
113 #define DRIVER_NAME "LAN91C96"
114 #define SMC_ALLOC_MAX_TRY 5
115 #define SMC_TX_TIMEOUT 30
119 #ifdef CONFIG_LAN91C96_USE_32_BIT
125 /* See if a MAC address is defined in the current environment. If so use it. If not
126 . print a warning and set the environment and other globals with the default.
127 . If an EEPROM is present it really should be consulted.
129 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
130 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
132 /* ------------------------------------------------------------
134 * ------------------------------------------------------------
137 static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
140 * This function must be called before smc_open() if you want to override
141 * the default mac address.
144 static void smc_set_mac_addr(const unsigned char *addr)
148 for (i = 0; i < sizeof (smc_mac_addr); i++) {
149 smc_mac_addr[i] = addr[i];
153 /***********************************************
154 * Show available memory *
155 ***********************************************/
156 void dump_memory_info(struct eth_device *dev)
158 __maybe_unused word mem_info;
161 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
163 SMC_SELECT_BANK(dev, 0);
164 mem_info = SMC_inw(dev, LAN91C96_MIR);
165 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
167 SMC_SELECT_BANK(dev, old_bank);
171 * A rather simple routine to print out a packet for debugging purposes.
174 static void print_packet (byte *, int);
177 static int poll4int (struct eth_device *dev, byte mask, int timeout)
179 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
181 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
183 PRINTK2 ("Polling...\n");
184 SMC_SELECT_BANK(dev, 2);
185 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
186 if (get_timer (0) >= tmo) {
192 /* restore old bank selection */
193 SMC_SELECT_BANK(dev, old_bank);
202 * Function: smc_reset
204 * This sets the SMC91111 chip to its normal state, hopefully from whatever
205 * mess that any other DOS driver has put it in.
207 * Maybe I should reset more registers to defaults in here? SOFTRST should
211 * 1. send a SOFT RESET
212 * 2. wait for it to finish
213 * 3. enable autorelease mode
214 * 4. reset the memory management unit
215 * 5. clear all interrupts
218 static void smc_reset(struct eth_device *dev)
220 PRINTK2("%s:smc_reset\n", dev->name);
222 /* This resets the registers mostly to defaults, but doesn't
223 affect EEPROM. That seems unnecessary */
224 SMC_SELECT_BANK(dev, 0);
225 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
229 /* Disable transmit and receive functionality */
230 SMC_outw(dev, 0, LAN91C96_RCR);
231 SMC_outw(dev, 0, LAN91C96_TCR);
233 /* set the control register */
234 SMC_SELECT_BANK(dev, 1);
235 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
238 /* Disable all interrupts */
239 SMC_outb(dev, 0, LAN91C96_INT_MASK);
243 * Function: smc_enable
244 * Purpose: let the chip talk to the outside work
246 * 1. Initialize the Memory Configuration Register
247 * 2. Enable the transmitter
248 * 3. Enable the receiver
250 static void smc_enable(struct eth_device *dev)
252 PRINTK2("%s:smc_enable\n", dev->name);
253 SMC_SELECT_BANK(dev, 0);
255 /* Initialize the Memory Configuration Register. See page
256 49 of the LAN91C96 data sheet for details. */
257 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
259 /* Initialize the Transmit Control Register */
260 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
261 /* Initialize the Receive Control Register
263 * The promiscuous bit set because I could not receive ARP reply
264 * packets from the server when I send a ARP request. It only works
265 * when I set the promiscuous bit
267 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
271 * Function: smc_shutdown
272 * Purpose: closes down the SMC91xxx chip.
274 * 1. zero the interrupt mask
275 * 2. clear the enable receive flag
276 * 3. clear the enable xmit flags
279 * (1) maybe utilize power down mode.
280 * Why not yet? Because while the chip will go into power down mode,
281 * the manual says that it will wake up in response to any I/O requests
282 * in the register space. Empirical results do not show this working.
284 static void smc_shutdown(struct eth_device *dev)
286 PRINTK2("%s:smc_shutdown\n", dev->name);
288 /* no more interrupts for me */
289 SMC_SELECT_BANK(dev, 2);
290 SMC_outb(dev, 0, LAN91C96_INT_MASK);
292 /* and tell the card to stay away from that nasty outside world */
293 SMC_SELECT_BANK(dev, 0);
294 SMC_outb(dev, 0, LAN91C96_RCR);
295 SMC_outb(dev, 0, LAN91C96_TCR);
300 * Function: smc_hardware_send_packet(struct net_device * )
302 * This sends the actual packet to the SMC9xxx chip.
305 * First, see if a saved_skb is available.
306 * ( this should NOT be called if there is no 'saved_skb'
307 * Now, find the packet number that the chip allocated
308 * Point the data pointers at it in memory
309 * Set the length word in the chip's memory
310 * Dump the packet to chip memory
311 * Check if a last byte is needed ( odd length packet )
312 * if so, set the control flag right
313 * Tell the card to send it
314 * Enable the transmit interrupt, so I know if it failed
315 * Free the kernel data if I actually sent it.
317 static int smc_send_packet(struct eth_device *dev, void *packet,
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.. */
384 buf = (byte *) packet;
386 /* If I get here, I _know_ there is a packet slot waiting for me */
387 packet_no = SMC_inb(dev, LAN91C96_ARR);
388 if (packet_no & LAN91C96_ARR_FAILED) {
389 /* or isn't there? BAD CHIP! */
390 printf("%s: Memory allocation failed. \n", dev->name);
394 /* we have a packet address, so tell the card to use it */
395 SMC_outb(dev, packet_no, LAN91C96_PNR);
397 /* point to the beginning of the packet */
398 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
400 PRINTK3("%s: Trying to xmit packet of length %x\n",
404 printf ("Transmitting Packet\n");
405 print_packet (buf, length);
408 /* send the packet length ( +6 for status, length and ctl byte )
409 and the status word ( set to zeros ) */
411 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
413 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
414 /* send the packet length ( +6 for status words, length, and ctl */
415 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
416 #endif /* USE_32_BIT */
418 /* send the actual data
419 * I _think_ it's faster to send the longs first, and then
420 * mop up by sending the last word. It depends heavily
421 * on alignment, at least on the 486. Maybe it would be
422 * a good idea to check which is optimal? But that could take
423 * almost as much time as is saved?
426 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
428 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
431 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
432 #endif /* USE_32_BIT */
434 /* Send the last byte, if there is one. */
435 if ((length & 1) == 0) {
436 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
438 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
441 /* and let the chipset deal with it */
442 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
444 /* poll for TX INT */
445 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
447 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
450 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
452 /* wait for MMU getting ready (low) */
453 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
456 PRINTK2("MMU ready\n");
462 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
464 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
467 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
469 /* wait for MMU getting ready (low) */
470 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
473 PRINTK2 ("MMU ready\n");
481 * Open and Initialize the board
483 * Set up everything, reset the card, etc ..
486 static int smc_open(bd_t *bd, struct eth_device *dev)
488 int i, err; /* used to set hw ethernet address */
490 PRINTK2("%s:smc_open\n", dev->name);
492 /* reset the hardware */
497 SMC_SELECT_BANK(dev, 1);
498 /* set smc_mac_addr, and sync it with u-boot globals */
499 err = smc_get_ethaddr(bd, dev);
503 for (i = 0; i < 6; i += 2) {
506 address = smc_mac_addr[i + 1] << 8;
507 address |= smc_mac_addr[i];
508 SMC_outw(dev, address, LAN91C96_IA0 + i);
511 for (i = 0; i < 6; i++)
512 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
517 /*-------------------------------------------------------------
519 * smc_rcv - receive a packet from the card
521 * There is ( at least ) a packet waiting to be read from
525 * o If an error, record it
526 * o otherwise, read in the packet
527 *-------------------------------------------------------------
529 static int smc_rcv(struct eth_device *dev)
541 SMC_SELECT_BANK(dev, 2);
542 packet_number = SMC_inw(dev, LAN91C96_FIFO);
544 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
548 PRINTK3("%s:smc_rcv\n", dev->name);
549 /* start reading from the start of the packet */
550 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
551 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
553 /* First two words are status and packet_length */
555 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
556 status = stat_len & 0xffff;
557 packet_length = stat_len >> 16;
559 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
560 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
563 packet_length &= 0x07ff; /* mask off top bits */
565 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
567 if (!(status & FRAME_FILTER)) {
568 /* Adjust for having already read the first two words */
569 packet_length -= 4; /*4; */
572 /* set odd length for bug in LAN91C111, */
573 /* which never sets RS_ODDFRAME */
578 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
579 packet_length >> 2, packet_length & 3);
580 /* QUESTION: Like in the TX routine, do I want
581 to send the DWORDs or the bytes first, or some
582 mixture. A mixture might improve already slow PIO
584 SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
586 /* read the left over bytes */
587 if (packet_length & 3) {
590 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
591 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
593 for (i = 0; i < (packet_length & 3); i++)
594 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
597 PRINTK3 (" Reading %d words and %d byte(s) \n",
598 (packet_length >> 1), packet_length & 1);
599 SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
602 #endif /* USE_32_BIT */
605 printf ("Receiving Packet\n");
606 print_packet((byte *)NetRxPackets[0], packet_length);
614 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
615 udelay (1); /* Wait until not busy */
617 /* error or good, tell the card to get rid of this packet */
618 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
620 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
621 udelay (1); /* Wait until not busy */
624 /* Pass the packet up to the protocol layers. */
625 NetReceive (NetRxPackets[0], packet_length);
626 return packet_length;
633 /*----------------------------------------------------
636 * this makes the board clean up everything that it can
637 * and not talk to the outside world. Caused by
638 * an 'ifconfig ethX down'
640 -----------------------------------------------------*/
641 static int smc_close(struct eth_device *dev)
643 PRINTK2("%s:smc_close\n", dev->name);
645 /* clear everything */
652 static void print_packet(byte *buf, int length)
659 printf ("Packet of length %d \n", length);
662 remainder = length % 16;
664 for (i = 0; i < lines; i++) {
667 for (cur = 0; cur < 8; cur++) {
672 printf ("%02x%02x ", a, b);
676 for (i = 0; i < remainder / 2; i++) {
681 printf ("%02x%02x ", a, b);
686 #endif /* SMC_DEBUG > 2 */
688 static int lan91c96_init(struct eth_device *dev, bd_t *bd)
690 return smc_open(bd, dev);
693 static void lan91c96_halt(struct eth_device *dev)
698 static int lan91c96_recv(struct eth_device *dev)
703 static int lan91c96_send(struct eth_device *dev, void *packet,
706 return smc_send_packet(dev, packet, length);
711 * This checks both the environment and the ROM for an ethernet address. If
712 * found, the environment takes precedence.
715 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
719 if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
720 /* get ROM mac value if any */
721 if (!get_rom_mac(dev, v_mac)) {
722 printf("\n*** ERROR: ethaddr is NOT set !!\n");
725 eth_setenv_enetaddr("ethaddr", v_mac);
728 smc_set_mac_addr(v_mac); /* use old function to update smc default */
729 PRINTK("Using MAC Address %pM\n", v_mac);
735 * Note, this has omly been tested for the OMAP730 P2.
738 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
740 #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
741 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
743 memcpy (v_rom_mac, hw_mac_addr, 6);
747 SMC_SELECT_BANK(dev, 1);
750 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
756 /* Structure to detect the device IDs */
761 static struct id_type supported_chips[] = {
762 {0, ""}, /* Dummy entry to prevent id check failure */
770 /* lan91c96_detect_chip
772 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
773 * page 71 - that is the closest we get to detect this device
775 static int lan91c96_detect_chip(struct eth_device *dev)
779 SMC_SELECT_BANK(dev, 3);
780 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
781 SMC_SELECT_BANK(dev, 0);
782 for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++)
783 if (chip_id == supported_chips[r].id)
788 int lan91c96_initialize(u8 dev_num, int base_addr)
790 struct eth_device *dev;
793 dev = malloc(sizeof(*dev));
797 memset(dev, 0, sizeof(*dev));
799 dev->iobase = base_addr;
801 /* Try to detect chip. Will fail if not present. */
802 r = lan91c96_detect_chip(dev);
807 get_rom_mac(dev, dev->enetaddr);
809 dev->init = lan91c96_init;
810 dev->halt = lan91c96_halt;
811 dev->send = lan91c96_send;
812 dev->recv = lan91c96_recv;
813 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);