1 // SPDX-License-Identifier: GPL-2.0+
2 /*------------------------------------------------------------------------
4 * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
5 * on the SMC91111 driver from U-Boot.
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Rolf Offermanns <rof@sysgo.de>
11 * Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
12 * Developed by Simple Network Magic Corporation (SNMC)
13 * Copyright (C) 1996 by Erik Stahlman (ES)
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.
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 )
25 * io = for the base address
29 * Erik Stahlman ( erik@vt.edu )
30 * Daris A Nevil ( dnevil@snmc.com )
33 * Hardware multicast code from Peter Cammaert ( pc@denkart.be )
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)
42 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
44 *---------------------------------------------------------------------------
52 #include <linux/compiler.h>
54 /*------------------------------------------------------------------------
56 * Configuration options, for the experienced user to change.
58 -------------------------------------------------------------------------*/
60 /* Use power-down feature of the chip */
64 * Wait time for memory to be free. This probably shouldn't be
65 * tuned that much, as waiting for this means nothing else happens
68 #define MEMORY_WAIT_TIME 16
73 #define PRINTK3(args...) printf(args)
75 #define PRINTK3(args...)
79 #define PRINTK2(args...) printf(args)
81 #define PRINTK2(args...)
85 #define PRINTK(args...) printf(args)
87 #define PRINTK(args...)
91 /*------------------------------------------------------------------------
93 * The internal workings of the driver. If you are changing anything
94 * here with the SMC stuff, you should have the datasheet and know
97 *------------------------------------------------------------------------
99 #define DRIVER_NAME "LAN91C96"
100 #define SMC_ALLOC_MAX_TRY 5
101 #define SMC_TX_TIMEOUT 30
105 #ifdef CONFIG_LAN91C96_USE_32_BIT
111 /* See if a MAC address is defined in the current environment. If so use it. If not
112 . print a warning and set the environment and other globals with the default.
113 . If an EEPROM is present it really should be consulted.
115 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
116 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
118 /* ------------------------------------------------------------
120 * ------------------------------------------------------------
123 static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
126 * This function must be called before smc_open() if you want to override
127 * the default mac address.
130 static void smc_set_mac_addr(const unsigned char *addr)
134 for (i = 0; i < sizeof (smc_mac_addr); i++) {
135 smc_mac_addr[i] = addr[i];
139 /***********************************************
140 * Show available memory *
141 ***********************************************/
142 void dump_memory_info(struct eth_device *dev)
144 __maybe_unused word mem_info;
147 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
149 SMC_SELECT_BANK(dev, 0);
150 mem_info = SMC_inw(dev, LAN91C96_MIR);
151 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
153 SMC_SELECT_BANK(dev, old_bank);
157 * A rather simple routine to print out a packet for debugging purposes.
160 static void print_packet (byte *, int);
163 static int poll4int (struct eth_device *dev, byte mask, int timeout)
165 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
167 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
169 PRINTK2 ("Polling...\n");
170 SMC_SELECT_BANK(dev, 2);
171 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
172 if (get_timer (0) >= tmo) {
178 /* restore old bank selection */
179 SMC_SELECT_BANK(dev, old_bank);
188 * Function: smc_reset
190 * This sets the SMC91111 chip to its normal state, hopefully from whatever
191 * mess that any other DOS driver has put it in.
193 * Maybe I should reset more registers to defaults in here? SOFTRST should
197 * 1. send a SOFT RESET
198 * 2. wait for it to finish
199 * 3. enable autorelease mode
200 * 4. reset the memory management unit
201 * 5. clear all interrupts
204 static void smc_reset(struct eth_device *dev)
206 PRINTK2("%s:smc_reset\n", dev->name);
208 /* This resets the registers mostly to defaults, but doesn't
209 affect EEPROM. That seems unnecessary */
210 SMC_SELECT_BANK(dev, 0);
211 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
215 /* Disable transmit and receive functionality */
216 SMC_outw(dev, 0, LAN91C96_RCR);
217 SMC_outw(dev, 0, LAN91C96_TCR);
219 /* set the control register */
220 SMC_SELECT_BANK(dev, 1);
221 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
224 /* Disable all interrupts */
225 SMC_outb(dev, 0, LAN91C96_INT_MASK);
229 * Function: smc_enable
230 * Purpose: let the chip talk to the outside work
232 * 1. Initialize the Memory Configuration Register
233 * 2. Enable the transmitter
234 * 3. Enable the receiver
236 static void smc_enable(struct eth_device *dev)
238 PRINTK2("%s:smc_enable\n", dev->name);
239 SMC_SELECT_BANK(dev, 0);
241 /* Initialize the Memory Configuration Register. See page
242 49 of the LAN91C96 data sheet for details. */
243 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
245 /* Initialize the Transmit Control Register */
246 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
247 /* Initialize the Receive Control Register
249 * The promiscuous bit set because I could not receive ARP reply
250 * packets from the server when I send a ARP request. It only works
251 * when I set the promiscuous bit
253 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
257 * Function: smc_shutdown
258 * Purpose: closes down the SMC91xxx chip.
260 * 1. zero the interrupt mask
261 * 2. clear the enable receive flag
262 * 3. clear the enable xmit flags
265 * (1) maybe utilize power down mode.
266 * Why not yet? Because while the chip will go into power down mode,
267 * the manual says that it will wake up in response to any I/O requests
268 * in the register space. Empirical results do not show this working.
270 static void smc_shutdown(struct eth_device *dev)
272 PRINTK2("%s:smc_shutdown\n", dev->name);
274 /* no more interrupts for me */
275 SMC_SELECT_BANK(dev, 2);
276 SMC_outb(dev, 0, LAN91C96_INT_MASK);
278 /* and tell the card to stay away from that nasty outside world */
279 SMC_SELECT_BANK(dev, 0);
280 SMC_outb(dev, 0, LAN91C96_RCR);
281 SMC_outb(dev, 0, LAN91C96_TCR);
286 * Function: smc_hardware_send_packet(struct net_device * )
288 * This sends the actual packet to the SMC9xxx chip.
291 * First, see if a saved_skb is available.
292 * ( this should NOT be called if there is no 'saved_skb'
293 * Now, find the packet number that the chip allocated
294 * Point the data pointers at it in memory
295 * Set the length word in the chip's memory
296 * Dump the packet to chip memory
297 * Check if a last byte is needed ( odd length packet )
298 * if so, set the control flag right
299 * Tell the card to send it
300 * Enable the transmit interrupt, so I know if it failed
301 * Free the kernel data if I actually sent it.
303 static int smc_send_packet(struct eth_device *dev, void *packet,
315 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
317 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
320 ** The MMU wants the number of pages to be the number of 256 bytes
321 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
323 ** The 91C111 ignores the size bits, but the code is left intact
324 ** for backwards and future compatibility.
326 ** Pkt size for allocating is data length +6 (for additional status
327 ** words, length and ctl!)
329 ** If odd size then last byte is included in this header.
331 numPages = ((length & 0xfffe) + 6);
332 numPages >>= 8; /* Divide by 256 */
335 printf("%s: Far too big packet error. \n", dev->name);
339 /* now, try to allocate the memory */
341 SMC_SELECT_BANK(dev, 2);
342 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
346 time_out = MEMORY_WAIT_TIME;
348 status = SMC_inb(dev, LAN91C96_INT_STATS);
349 if (status & LAN91C96_IST_ALLOC_INT) {
351 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
355 } while (--time_out);
358 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
360 if (try < SMC_ALLOC_MAX_TRY)
366 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
369 /* I can send the packet now.. */
370 buf = (byte *) packet;
372 /* If I get here, I _know_ there is a packet slot waiting for me */
373 packet_no = SMC_inb(dev, LAN91C96_ARR);
374 if (packet_no & LAN91C96_ARR_FAILED) {
375 /* or isn't there? BAD CHIP! */
376 printf("%s: Memory allocation failed. \n", dev->name);
380 /* we have a packet address, so tell the card to use it */
381 SMC_outb(dev, packet_no, LAN91C96_PNR);
383 /* point to the beginning of the packet */
384 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
386 PRINTK3("%s: Trying to xmit packet of length %x\n",
390 printf ("Transmitting Packet\n");
391 print_packet (buf, length);
394 /* send the packet length ( +6 for status, length and ctl byte )
395 and the status word ( set to zeros ) */
397 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
399 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
400 /* send the packet length ( +6 for status words, length, and ctl */
401 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
402 #endif /* USE_32_BIT */
404 /* send the actual data
405 * I _think_ it's faster to send the longs first, and then
406 * mop up by sending the last word. It depends heavily
407 * on alignment, at least on the 486. Maybe it would be
408 * a good idea to check which is optimal? But that could take
409 * almost as much time as is saved?
412 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
414 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
417 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
418 #endif /* USE_32_BIT */
420 /* Send the last byte, if there is one. */
421 if ((length & 1) == 0) {
422 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
424 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
427 /* and let the chipset deal with it */
428 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
430 /* poll for TX INT */
431 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
433 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
436 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
438 /* wait for MMU getting ready (low) */
439 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
442 PRINTK2("MMU ready\n");
448 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
450 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
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");
467 * Open and Initialize the board
469 * Set up everything, reset the card, etc ..
472 static int smc_open(bd_t *bd, struct eth_device *dev)
474 int i, err; /* used to set hw ethernet address */
476 PRINTK2("%s:smc_open\n", dev->name);
478 /* reset the hardware */
483 SMC_SELECT_BANK(dev, 1);
484 /* set smc_mac_addr, and sync it with u-boot globals */
485 err = smc_get_ethaddr(bd, dev);
489 for (i = 0; i < 6; i += 2) {
492 address = smc_mac_addr[i + 1] << 8;
493 address |= smc_mac_addr[i];
494 SMC_outw(dev, address, LAN91C96_IA0 + i);
497 for (i = 0; i < 6; i++)
498 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
503 /*-------------------------------------------------------------
505 * smc_rcv - receive a packet from the card
507 * There is ( at least ) a packet waiting to be read from
511 * o If an error, record it
512 * o otherwise, read in the packet
513 *-------------------------------------------------------------
515 static int smc_rcv(struct eth_device *dev)
527 SMC_SELECT_BANK(dev, 2);
528 packet_number = SMC_inw(dev, LAN91C96_FIFO);
530 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
534 PRINTK3("%s:smc_rcv\n", dev->name);
535 /* start reading from the start of the packet */
536 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
537 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
539 /* First two words are status and packet_length */
541 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
542 status = stat_len & 0xffff;
543 packet_length = stat_len >> 16;
545 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
546 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
549 packet_length &= 0x07ff; /* mask off top bits */
551 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
553 if (!(status & FRAME_FILTER)) {
554 /* Adjust for having already read the first two words */
555 packet_length -= 4; /*4; */
558 /* set odd length for bug in LAN91C111, */
559 /* which never sets RS_ODDFRAME */
564 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
565 packet_length >> 2, packet_length & 3);
566 /* QUESTION: Like in the TX routine, do I want
567 to send the DWORDs or the bytes first, or some
568 mixture. A mixture might improve already slow PIO
570 SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
572 /* read the left over bytes */
573 if (packet_length & 3) {
576 byte *tail = (byte *)(net_rx_packets[0] +
577 (packet_length & ~3));
578 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
580 for (i = 0; i < (packet_length & 3); i++)
581 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
584 PRINTK3(" Reading %d words and %d byte(s)\n",
585 (packet_length >> 1), packet_length & 1);
586 SMC_insw(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
589 #endif /* USE_32_BIT */
592 printf ("Receiving Packet\n");
593 print_packet((byte *)net_rx_packets[0], packet_length);
601 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
602 udelay (1); /* Wait until not busy */
604 /* error or good, tell the card to get rid of this packet */
605 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
607 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
608 udelay (1); /* Wait until not busy */
611 /* Pass the packet up to the protocol layers. */
612 net_process_received_packet(net_rx_packets[0], packet_length);
613 return packet_length;
620 /*----------------------------------------------------
623 * this makes the board clean up everything that it can
624 * and not talk to the outside world. Caused by
625 * an 'ifconfig ethX down'
627 -----------------------------------------------------*/
628 static int smc_close(struct eth_device *dev)
630 PRINTK2("%s:smc_close\n", dev->name);
632 /* clear everything */
639 static void print_packet(byte *buf, int length)
646 printf ("Packet of length %d \n", length);
649 remainder = length % 16;
651 for (i = 0; i < lines; i++) {
654 for (cur = 0; cur < 8; cur++) {
659 printf ("%02x%02x ", a, b);
663 for (i = 0; i < remainder / 2; i++) {
668 printf ("%02x%02x ", a, b);
673 #endif /* SMC_DEBUG > 2 */
675 static int lan91c96_init(struct eth_device *dev, bd_t *bd)
677 return smc_open(bd, dev);
680 static void lan91c96_halt(struct eth_device *dev)
685 static int lan91c96_recv(struct eth_device *dev)
690 static int lan91c96_send(struct eth_device *dev, void *packet,
693 return smc_send_packet(dev, packet, length);
698 * This checks both the environment and the ROM for an ethernet address. If
699 * found, the environment takes precedence.
702 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
706 if (!eth_env_get_enetaddr("ethaddr", v_mac)) {
707 /* get ROM mac value if any */
708 if (!get_rom_mac(dev, v_mac)) {
709 printf("\n*** ERROR: ethaddr is NOT set !!\n");
712 eth_env_set_enetaddr("ethaddr", v_mac);
715 smc_set_mac_addr(v_mac); /* use old function to update smc default */
716 PRINTK("Using MAC Address %pM\n", v_mac);
722 * Note, this has omly been tested for the OMAP730 P2.
725 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
728 SMC_SELECT_BANK(dev, 1);
731 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
736 /* Structure to detect the device IDs */
741 static struct id_type supported_chips[] = {
742 {0, ""}, /* Dummy entry to prevent id check failure */
750 /* lan91c96_detect_chip
752 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
753 * page 71 - that is the closest we get to detect this device
755 static int lan91c96_detect_chip(struct eth_device *dev)
759 SMC_SELECT_BANK(dev, 3);
760 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
761 SMC_SELECT_BANK(dev, 0);
762 for (r = 0; r < ARRAY_SIZE(supported_chips); r++)
763 if (chip_id == supported_chips[r].id)
768 int lan91c96_initialize(u8 dev_num, int base_addr)
770 struct eth_device *dev;
773 dev = malloc(sizeof(*dev));
777 memset(dev, 0, sizeof(*dev));
779 dev->iobase = base_addr;
781 /* Try to detect chip. Will fail if not present. */
782 r = lan91c96_detect_chip(dev);
787 get_rom_mac(dev, dev->enetaddr);
789 dev->init = lan91c96_init;
790 dev->halt = lan91c96_halt;
791 dev->send = lan91c96_send;
792 dev->recv = lan91c96_recv;
793 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);