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 *---------------------------------------------------------------------------
51 #include <linux/delay.h>
54 #include <linux/compiler.h>
56 /*------------------------------------------------------------------------
58 * Configuration options, for the experienced user to change.
60 -------------------------------------------------------------------------*/
62 /* Use power-down feature of the chip */
66 * Wait time for memory to be free. This probably shouldn't be
67 * tuned that much, as waiting for this means nothing else happens
70 #define MEMORY_WAIT_TIME 16
75 #define PRINTK3(args...) printf(args)
77 #define PRINTK3(args...)
81 #define PRINTK2(args...) printf(args)
83 #define PRINTK2(args...)
87 #define PRINTK(args...) printf(args)
89 #define PRINTK(args...)
93 /*------------------------------------------------------------------------
95 * The internal workings of the driver. If you are changing anything
96 * here with the SMC stuff, you should have the datasheet and know
99 *------------------------------------------------------------------------
101 #define DRIVER_NAME "LAN91C96"
102 #define SMC_ALLOC_MAX_TRY 5
103 #define SMC_TX_TIMEOUT 30
107 #ifdef CONFIG_LAN91C96_USE_32_BIT
113 /* See if a MAC address is defined in the current environment. If so use it. If not
114 . print a warning and set the environment and other globals with the default.
115 . If an EEPROM is present it really should be consulted.
117 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
118 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
120 /* ------------------------------------------------------------
122 * ------------------------------------------------------------
125 static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
128 * This function must be called before smc_open() if you want to override
129 * the default mac address.
132 static void smc_set_mac_addr(const unsigned char *addr)
136 for (i = 0; i < sizeof (smc_mac_addr); i++) {
137 smc_mac_addr[i] = addr[i];
141 /***********************************************
142 * Show available memory *
143 ***********************************************/
144 void dump_memory_info(struct eth_device *dev)
146 __maybe_unused word mem_info;
149 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
151 SMC_SELECT_BANK(dev, 0);
152 mem_info = SMC_inw(dev, LAN91C96_MIR);
153 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
155 SMC_SELECT_BANK(dev, old_bank);
159 * A rather simple routine to print out a packet for debugging purposes.
162 static void print_packet (byte *, int);
165 static int poll4int (struct eth_device *dev, byte mask, int timeout)
167 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
169 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
171 PRINTK2 ("Polling...\n");
172 SMC_SELECT_BANK(dev, 2);
173 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
174 if (get_timer (0) >= tmo) {
180 /* restore old bank selection */
181 SMC_SELECT_BANK(dev, old_bank);
190 * Function: smc_reset
192 * This sets the SMC91111 chip to its normal state, hopefully from whatever
193 * mess that any other DOS driver has put it in.
195 * Maybe I should reset more registers to defaults in here? SOFTRST should
199 * 1. send a SOFT RESET
200 * 2. wait for it to finish
201 * 3. enable autorelease mode
202 * 4. reset the memory management unit
203 * 5. clear all interrupts
206 static void smc_reset(struct eth_device *dev)
208 PRINTK2("%s:smc_reset\n", dev->name);
210 /* This resets the registers mostly to defaults, but doesn't
211 affect EEPROM. That seems unnecessary */
212 SMC_SELECT_BANK(dev, 0);
213 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
217 /* Disable transmit and receive functionality */
218 SMC_outw(dev, 0, LAN91C96_RCR);
219 SMC_outw(dev, 0, LAN91C96_TCR);
221 /* set the control register */
222 SMC_SELECT_BANK(dev, 1);
223 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
226 /* Disable all interrupts */
227 SMC_outb(dev, 0, LAN91C96_INT_MASK);
231 * Function: smc_enable
232 * Purpose: let the chip talk to the outside work
234 * 1. Initialize the Memory Configuration Register
235 * 2. Enable the transmitter
236 * 3. Enable the receiver
238 static void smc_enable(struct eth_device *dev)
240 PRINTK2("%s:smc_enable\n", dev->name);
241 SMC_SELECT_BANK(dev, 0);
243 /* Initialize the Memory Configuration Register. See page
244 49 of the LAN91C96 data sheet for details. */
245 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
247 /* Initialize the Transmit Control Register */
248 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
249 /* Initialize the Receive Control Register
251 * The promiscuous bit set because I could not receive ARP reply
252 * packets from the server when I send a ARP request. It only works
253 * when I set the promiscuous bit
255 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
259 * Function: smc_shutdown
260 * Purpose: closes down the SMC91xxx chip.
262 * 1. zero the interrupt mask
263 * 2. clear the enable receive flag
264 * 3. clear the enable xmit flags
267 * (1) maybe utilize power down mode.
268 * Why not yet? Because while the chip will go into power down mode,
269 * the manual says that it will wake up in response to any I/O requests
270 * in the register space. Empirical results do not show this working.
272 static void smc_shutdown(struct eth_device *dev)
274 PRINTK2("%s:smc_shutdown\n", dev->name);
276 /* no more interrupts for me */
277 SMC_SELECT_BANK(dev, 2);
278 SMC_outb(dev, 0, LAN91C96_INT_MASK);
280 /* and tell the card to stay away from that nasty outside world */
281 SMC_SELECT_BANK(dev, 0);
282 SMC_outb(dev, 0, LAN91C96_RCR);
283 SMC_outb(dev, 0, LAN91C96_TCR);
288 * Function: smc_hardware_send_packet(struct net_device * )
290 * This sends the actual packet to the SMC9xxx chip.
293 * First, see if a saved_skb is available.
294 * ( this should NOT be called if there is no 'saved_skb'
295 * Now, find the packet number that the chip allocated
296 * Point the data pointers at it in memory
297 * Set the length word in the chip's memory
298 * Dump the packet to chip memory
299 * Check if a last byte is needed ( odd length packet )
300 * if so, set the control flag right
301 * Tell the card to send it
302 * Enable the transmit interrupt, so I know if it failed
303 * Free the kernel data if I actually sent it.
305 static int smc_send_packet(struct eth_device *dev, void *packet,
317 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
319 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
322 ** The MMU wants the number of pages to be the number of 256 bytes
323 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
325 ** The 91C111 ignores the size bits, but the code is left intact
326 ** for backwards and future compatibility.
328 ** Pkt size for allocating is data length +6 (for additional status
329 ** words, length and ctl!)
331 ** If odd size then last byte is included in this header.
333 numPages = ((length & 0xfffe) + 6);
334 numPages >>= 8; /* Divide by 256 */
337 printf("%s: Far too big packet error. \n", dev->name);
341 /* now, try to allocate the memory */
343 SMC_SELECT_BANK(dev, 2);
344 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
348 time_out = MEMORY_WAIT_TIME;
350 status = SMC_inb(dev, LAN91C96_INT_STATS);
351 if (status & LAN91C96_IST_ALLOC_INT) {
353 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
357 } while (--time_out);
360 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
362 if (try < SMC_ALLOC_MAX_TRY)
368 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
371 /* I can send the packet now.. */
372 buf = (byte *) packet;
374 /* If I get here, I _know_ there is a packet slot waiting for me */
375 packet_no = SMC_inb(dev, LAN91C96_ARR);
376 if (packet_no & LAN91C96_ARR_FAILED) {
377 /* or isn't there? BAD CHIP! */
378 printf("%s: Memory allocation failed. \n", dev->name);
382 /* we have a packet address, so tell the card to use it */
383 SMC_outb(dev, packet_no, LAN91C96_PNR);
385 /* point to the beginning of the packet */
386 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
388 PRINTK3("%s: Trying to xmit packet of length %x\n",
392 printf ("Transmitting Packet\n");
393 print_packet (buf, length);
396 /* send the packet length ( +6 for status, length and ctl byte )
397 and the status word ( set to zeros ) */
399 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
401 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
402 /* send the packet length ( +6 for status words, length, and ctl */
403 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
404 #endif /* USE_32_BIT */
406 /* send the actual data
407 * I _think_ it's faster to send the longs first, and then
408 * mop up by sending the last word. It depends heavily
409 * on alignment, at least on the 486. Maybe it would be
410 * a good idea to check which is optimal? But that could take
411 * almost as much time as is saved?
414 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
416 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
419 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
420 #endif /* USE_32_BIT */
422 /* Send the last byte, if there is one. */
423 if ((length & 1) == 0) {
424 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
426 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
429 /* and let the chipset deal with it */
430 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
432 /* poll for TX INT */
433 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
435 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
438 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
440 /* wait for MMU getting ready (low) */
441 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
444 PRINTK2("MMU ready\n");
450 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
452 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
455 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
457 /* wait for MMU getting ready (low) */
458 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
461 PRINTK2 ("MMU ready\n");
469 * Open and Initialize the board
471 * Set up everything, reset the card, etc ..
474 static int smc_open(bd_t *bd, struct eth_device *dev)
476 int i, err; /* used to set hw ethernet address */
478 PRINTK2("%s:smc_open\n", dev->name);
480 /* reset the hardware */
485 SMC_SELECT_BANK(dev, 1);
486 /* set smc_mac_addr, and sync it with u-boot globals */
487 err = smc_get_ethaddr(bd, dev);
491 for (i = 0; i < 6; i += 2) {
494 address = smc_mac_addr[i + 1] << 8;
495 address |= smc_mac_addr[i];
496 SMC_outw(dev, address, LAN91C96_IA0 + i);
499 for (i = 0; i < 6; i++)
500 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
505 /*-------------------------------------------------------------
507 * smc_rcv - receive a packet from the card
509 * There is ( at least ) a packet waiting to be read from
513 * o If an error, record it
514 * o otherwise, read in the packet
515 *-------------------------------------------------------------
517 static int smc_rcv(struct eth_device *dev)
529 SMC_SELECT_BANK(dev, 2);
530 packet_number = SMC_inw(dev, LAN91C96_FIFO);
532 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
536 PRINTK3("%s:smc_rcv\n", dev->name);
537 /* start reading from the start of the packet */
538 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
539 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
541 /* First two words are status and packet_length */
543 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
544 status = stat_len & 0xffff;
545 packet_length = stat_len >> 16;
547 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
548 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
551 packet_length &= 0x07ff; /* mask off top bits */
553 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
555 if (!(status & FRAME_FILTER)) {
556 /* Adjust for having already read the first two words */
557 packet_length -= 4; /*4; */
560 /* set odd length for bug in LAN91C111, */
561 /* which never sets RS_ODDFRAME */
566 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
567 packet_length >> 2, packet_length & 3);
568 /* QUESTION: Like in the TX routine, do I want
569 to send the DWORDs or the bytes first, or some
570 mixture. A mixture might improve already slow PIO
572 SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
574 /* read the left over bytes */
575 if (packet_length & 3) {
578 byte *tail = (byte *)(net_rx_packets[0] +
579 (packet_length & ~3));
580 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
582 for (i = 0; i < (packet_length & 3); i++)
583 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
586 PRINTK3(" Reading %d words and %d byte(s)\n",
587 (packet_length >> 1), packet_length & 1);
588 SMC_insw(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
591 #endif /* USE_32_BIT */
594 printf ("Receiving Packet\n");
595 print_packet((byte *)net_rx_packets[0], packet_length);
603 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
604 udelay(1); /* Wait until not busy */
606 /* error or good, tell the card to get rid of this packet */
607 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
609 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
610 udelay(1); /* Wait until not busy */
613 /* Pass the packet up to the protocol layers. */
614 net_process_received_packet(net_rx_packets[0], packet_length);
615 return packet_length;
622 /*----------------------------------------------------
625 * this makes the board clean up everything that it can
626 * and not talk to the outside world. Caused by
627 * an 'ifconfig ethX down'
629 -----------------------------------------------------*/
630 static int smc_close(struct eth_device *dev)
632 PRINTK2("%s:smc_close\n", dev->name);
634 /* clear everything */
641 static void print_packet(byte *buf, int length)
648 printf ("Packet of length %d \n", length);
651 remainder = length % 16;
653 for (i = 0; i < lines; i++) {
656 for (cur = 0; cur < 8; cur++) {
661 printf ("%02x%02x ", a, b);
665 for (i = 0; i < remainder / 2; i++) {
670 printf ("%02x%02x ", a, b);
675 #endif /* SMC_DEBUG > 2 */
677 static int lan91c96_init(struct eth_device *dev, bd_t *bd)
679 return smc_open(bd, dev);
682 static void lan91c96_halt(struct eth_device *dev)
687 static int lan91c96_recv(struct eth_device *dev)
692 static int lan91c96_send(struct eth_device *dev, void *packet,
695 return smc_send_packet(dev, packet, length);
700 * This checks both the environment and the ROM for an ethernet address. If
701 * found, the environment takes precedence.
704 static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
708 if (!eth_env_get_enetaddr("ethaddr", v_mac)) {
709 /* get ROM mac value if any */
710 if (!get_rom_mac(dev, v_mac)) {
711 printf("\n*** ERROR: ethaddr is NOT set !!\n");
714 eth_env_set_enetaddr("ethaddr", v_mac);
717 smc_set_mac_addr(v_mac); /* use old function to update smc default */
718 PRINTK("Using MAC Address %pM\n", v_mac);
724 * Note, this has omly been tested for the OMAP730 P2.
727 static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
730 SMC_SELECT_BANK(dev, 1);
733 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
738 /* Structure to detect the device IDs */
743 static struct id_type supported_chips[] = {
744 {0, ""}, /* Dummy entry to prevent id check failure */
752 /* lan91c96_detect_chip
754 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
755 * page 71 - that is the closest we get to detect this device
757 static int lan91c96_detect_chip(struct eth_device *dev)
761 SMC_SELECT_BANK(dev, 3);
762 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
763 SMC_SELECT_BANK(dev, 0);
764 for (r = 0; r < ARRAY_SIZE(supported_chips); r++)
765 if (chip_id == supported_chips[r].id)
770 int lan91c96_initialize(u8 dev_num, int base_addr)
772 struct eth_device *dev;
775 dev = malloc(sizeof(*dev));
779 memset(dev, 0, sizeof(*dev));
781 dev->iobase = base_addr;
783 /* Try to detect chip. Will fail if not present. */
784 r = lan91c96_detect_chip(dev);
789 get_rom_mac(dev, dev->enetaddr);
791 dev->init = lan91c96_init;
792 dev->halt = lan91c96_halt;
793 dev->send = lan91c96_send;
794 dev->recv = lan91c96_recv;
795 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);