Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 2 | /*------------------------------------------------------------------------ |
| 3 | * lan91c96.c |
| 4 | * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 5 | * on the SMC91111 driver from U-Boot. |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 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 | * |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 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 | * |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 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: |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 25 | * io = for the base address |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 26 | * irq = for the IRQ |
| 27 | * |
| 28 | * author: |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 29 | * Erik Stahlman ( erik@vt.edu ) |
| 30 | * Daris A Nevil ( dnevil@snmc.com ) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 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: |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 42 | * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 43 | * for lan91c96 |
| 44 | *--------------------------------------------------------------------------- |
| 45 | */ |
| 46 | |
| 47 | #include <common.h> |
| 48 | #include <command.h> |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 49 | #include <malloc.h> |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 50 | #include "lan91c96.h" |
| 51 | #include <net.h> |
Anatolij Gustschin | d1d92d7 | 2011-11-19 13:12:12 +0000 | [diff] [blame] | 52 | #include <linux/compiler.h> |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 53 | |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 54 | /*------------------------------------------------------------------------ |
| 55 | * |
| 56 | * Configuration options, for the experienced user to change. |
| 57 | * |
| 58 | -------------------------------------------------------------------------*/ |
| 59 | |
| 60 | /* Use power-down feature of the chip */ |
| 61 | #define POWER_DOWN 0 |
| 62 | |
| 63 | /* |
| 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 |
| 66 | * in the system |
| 67 | */ |
| 68 | #define MEMORY_WAIT_TIME 16 |
| 69 | |
| 70 | #define SMC_DEBUG 0 |
| 71 | |
| 72 | #if (SMC_DEBUG > 2 ) |
| 73 | #define PRINTK3(args...) printf(args) |
| 74 | #else |
| 75 | #define PRINTK3(args...) |
| 76 | #endif |
| 77 | |
| 78 | #if SMC_DEBUG > 1 |
| 79 | #define PRINTK2(args...) printf(args) |
| 80 | #else |
| 81 | #define PRINTK2(args...) |
| 82 | #endif |
| 83 | |
| 84 | #ifdef SMC_DEBUG |
| 85 | #define PRINTK(args...) printf(args) |
| 86 | #else |
| 87 | #define PRINTK(args...) |
| 88 | #endif |
| 89 | |
| 90 | |
| 91 | /*------------------------------------------------------------------------ |
| 92 | * |
| 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 |
| 95 | * what you are doing. |
| 96 | * |
| 97 | *------------------------------------------------------------------------ |
| 98 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 99 | #define DRIVER_NAME "LAN91C96" |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 100 | #define SMC_ALLOC_MAX_TRY 5 |
| 101 | #define SMC_TX_TIMEOUT 30 |
| 102 | |
| 103 | #define ETH_ZLEN 60 |
| 104 | |
| 105 | #ifdef CONFIG_LAN91C96_USE_32_BIT |
| 106 | #define USE_32_BIT 1 |
| 107 | #else |
| 108 | #undef USE_32_BIT |
| 109 | #endif |
| 110 | |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 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. |
| 114 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 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); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 117 | |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 118 | /* ------------------------------------------------------------ |
| 119 | * Internal routines |
| 120 | * ------------------------------------------------------------ |
| 121 | */ |
| 122 | |
Wolfgang Denk | 7fa6e90 | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 123 | static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c }; |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
| 126 | * This function must be called before smc_open() if you want to override |
| 127 | * the default mac address. |
| 128 | */ |
| 129 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 130 | static void smc_set_mac_addr(const unsigned char *addr) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 131 | { |
| 132 | int i; |
| 133 | |
| 134 | for (i = 0; i < sizeof (smc_mac_addr); i++) { |
| 135 | smc_mac_addr[i] = addr[i]; |
| 136 | } |
| 137 | } |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 138 | |
| 139 | /*********************************************** |
| 140 | * Show available memory * |
| 141 | ***********************************************/ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 142 | void dump_memory_info(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 143 | { |
Anatolij Gustschin | d1d92d7 | 2011-11-19 13:12:12 +0000 | [diff] [blame] | 144 | __maybe_unused word mem_info; |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 145 | word old_bank; |
| 146 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 147 | old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF; |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 148 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 149 | SMC_SELECT_BANK(dev, 0); |
| 150 | mem_info = SMC_inw(dev, LAN91C96_MIR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 151 | PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048); |
| 152 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 153 | SMC_SELECT_BANK(dev, old_bank); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* |
| 157 | * A rather simple routine to print out a packet for debugging purposes. |
| 158 | */ |
| 159 | #if SMC_DEBUG > 2 |
| 160 | static void print_packet (byte *, int); |
| 161 | #endif |
| 162 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 163 | static int poll4int (struct eth_device *dev, byte mask, int timeout) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 164 | { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 165 | int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ; |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 166 | int is_timeout = 0; |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 167 | word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 168 | |
| 169 | PRINTK2 ("Polling...\n"); |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 170 | SMC_SELECT_BANK(dev, 2); |
| 171 | while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) { |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 172 | if (get_timer (0) >= tmo) { |
| 173 | is_timeout = 1; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /* restore old bank selection */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 179 | SMC_SELECT_BANK(dev, old_bank); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 180 | |
| 181 | if (is_timeout) |
| 182 | return 1; |
| 183 | else |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /* |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 188 | * Function: smc_reset |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 189 | * Purpose: |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 190 | * This sets the SMC91111 chip to its normal state, hopefully from whatever |
| 191 | * mess that any other DOS driver has put it in. |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 192 | * |
| 193 | * Maybe I should reset more registers to defaults in here? SOFTRST should |
| 194 | * do that for me. |
| 195 | * |
| 196 | * Method: |
| 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 |
| 202 | * |
| 203 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 204 | static void smc_reset(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 205 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 206 | PRINTK2("%s:smc_reset\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 207 | |
| 208 | /* This resets the registers mostly to defaults, but doesn't |
| 209 | affect EEPROM. That seems unnecessary */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 210 | SMC_SELECT_BANK(dev, 0); |
| 211 | SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 212 | |
| 213 | udelay (10); |
| 214 | |
| 215 | /* Disable transmit and receive functionality */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 216 | SMC_outw(dev, 0, LAN91C96_RCR); |
| 217 | SMC_outw(dev, 0, LAN91C96_TCR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 218 | |
| 219 | /* set the control register */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 220 | SMC_SELECT_BANK(dev, 1); |
| 221 | SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8, |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 222 | LAN91C96_CONTROL); |
| 223 | |
| 224 | /* Disable all interrupts */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 225 | SMC_outb(dev, 0, LAN91C96_INT_MASK); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Function: smc_enable |
| 230 | * Purpose: let the chip talk to the outside work |
| 231 | * Method: |
| 232 | * 1. Initialize the Memory Configuration Register |
| 233 | * 2. Enable the transmitter |
| 234 | * 3. Enable the receiver |
| 235 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 236 | static void smc_enable(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 237 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 238 | PRINTK2("%s:smc_enable\n", dev->name); |
| 239 | SMC_SELECT_BANK(dev, 0); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 240 | |
| 241 | /* Initialize the Memory Configuration Register. See page |
| 242 | 49 of the LAN91C96 data sheet for details. */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 243 | SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 244 | |
| 245 | /* Initialize the Transmit Control Register */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 246 | SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 247 | /* Initialize the Receive Control Register |
| 248 | * FIXME: |
| 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 |
| 252 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 253 | SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Function: smc_shutdown |
| 258 | * Purpose: closes down the SMC91xxx chip. |
| 259 | * Method: |
| 260 | * 1. zero the interrupt mask |
| 261 | * 2. clear the enable receive flag |
| 262 | * 3. clear the enable xmit flags |
| 263 | * |
| 264 | * TODO: |
| 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. |
| 269 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 270 | static void smc_shutdown(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 271 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 272 | PRINTK2("%s:smc_shutdown\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 273 | |
| 274 | /* no more interrupts for me */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 275 | SMC_SELECT_BANK(dev, 2); |
| 276 | SMC_outb(dev, 0, LAN91C96_INT_MASK); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 277 | |
| 278 | /* and tell the card to stay away from that nasty outside world */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 279 | SMC_SELECT_BANK(dev, 0); |
| 280 | SMC_outb(dev, 0, LAN91C96_RCR); |
| 281 | SMC_outb(dev, 0, LAN91C96_TCR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | |
| 285 | /* |
| 286 | * Function: smc_hardware_send_packet(struct net_device * ) |
| 287 | * Purpose: |
| 288 | * This sends the actual packet to the SMC9xxx chip. |
| 289 | * |
| 290 | * Algorithm: |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 291 | * First, see if a saved_skb is available. |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 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 |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 299 | * Tell the card to send it |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 300 | * Enable the transmit interrupt, so I know if it failed |
Jean-Christophe PLAGNIOL-VILLARD | 4134872 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 301 | * Free the kernel data if I actually sent it. |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 302 | */ |
Joe Hershberger | c78248f | 2012-05-21 14:45:30 +0000 | [diff] [blame] | 303 | static int smc_send_packet(struct eth_device *dev, void *packet, |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 304 | int packet_length) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 305 | { |
| 306 | byte packet_no; |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 307 | byte *buf; |
| 308 | int length; |
| 309 | int numPages; |
| 310 | int try = 0; |
| 311 | int time_out; |
| 312 | byte status; |
| 313 | |
| 314 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 315 | PRINTK3("%s:smc_hardware_send_packet\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 316 | |
| 317 | length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN; |
| 318 | |
| 319 | /* allocate memory |
| 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 :) ) |
| 322 | ** |
| 323 | ** The 91C111 ignores the size bits, but the code is left intact |
| 324 | ** for backwards and future compatibility. |
| 325 | ** |
| 326 | ** Pkt size for allocating is data length +6 (for additional status |
| 327 | ** words, length and ctl!) |
| 328 | ** |
| 329 | ** If odd size then last byte is included in this header. |
| 330 | */ |
| 331 | numPages = ((length & 0xfffe) + 6); |
| 332 | numPages >>= 8; /* Divide by 256 */ |
| 333 | |
| 334 | if (numPages > 7) { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 335 | printf("%s: Far too big packet error. \n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /* now, try to allocate the memory */ |
| 340 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 341 | SMC_SELECT_BANK(dev, 2); |
| 342 | SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 343 | |
| 344 | again: |
| 345 | try++; |
| 346 | time_out = MEMORY_WAIT_TIME; |
| 347 | do { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 348 | status = SMC_inb(dev, LAN91C96_INT_STATS); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 349 | if (status & LAN91C96_IST_ALLOC_INT) { |
| 350 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 351 | SMC_outb(dev, LAN91C96_IST_ALLOC_INT, |
| 352 | LAN91C96_INT_STATS); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 353 | break; |
| 354 | } |
| 355 | } while (--time_out); |
| 356 | |
| 357 | if (!time_out) { |
| 358 | PRINTK2 ("%s: memory allocation, try %d failed ...\n", |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 359 | dev->name, try); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 360 | if (try < SMC_ALLOC_MAX_TRY) |
| 361 | goto again; |
| 362 | else |
| 363 | return 0; |
| 364 | } |
| 365 | |
| 366 | PRINTK2 ("%s: memory allocation, try %d succeeded ...\n", |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 367 | dev->name, try); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 368 | |
| 369 | /* I can send the packet now.. */ |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 370 | buf = (byte *) packet; |
| 371 | |
| 372 | /* If I get here, I _know_ there is a packet slot waiting for me */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 373 | packet_no = SMC_inb(dev, LAN91C96_ARR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 374 | if (packet_no & LAN91C96_ARR_FAILED) { |
| 375 | /* or isn't there? BAD CHIP! */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 376 | printf("%s: Memory allocation failed. \n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 377 | return 0; |
| 378 | } |
| 379 | |
| 380 | /* we have a packet address, so tell the card to use it */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 381 | SMC_outb(dev, packet_no, LAN91C96_PNR); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 382 | |
| 383 | /* point to the beginning of the packet */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 384 | SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 385 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 386 | PRINTK3("%s: Trying to xmit packet of length %x\n", |
| 387 | dev->name, length); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 388 | |
| 389 | #if SMC_DEBUG > 2 |
| 390 | printf ("Transmitting Packet\n"); |
| 391 | print_packet (buf, length); |
| 392 | #endif |
| 393 | |
| 394 | /* send the packet length ( +6 for status, length and ctl byte ) |
| 395 | and the status word ( set to zeros ) */ |
| 396 | #ifdef USE_32_BIT |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 397 | SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 398 | #else |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 399 | SMC_outw(dev, 0, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 400 | /* send the packet length ( +6 for status words, length, and ctl */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 401 | SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 402 | #endif /* USE_32_BIT */ |
| 403 | |
| 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? |
| 410 | */ |
| 411 | #ifdef USE_32_BIT |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 412 | SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 413 | if (length & 0x2) |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 414 | SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))), |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 415 | LAN91C96_DATA_HIGH); |
| 416 | #else |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 417 | SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 418 | #endif /* USE_32_BIT */ |
| 419 | |
| 420 | /* Send the last byte, if there is one. */ |
| 421 | if ((length & 1) == 0) { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 422 | SMC_outw(dev, 0, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 423 | } else { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 424 | SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | /* and let the chipset deal with it */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 428 | SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 429 | |
| 430 | /* poll for TX INT */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 431 | if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) { |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 432 | /* sending failed */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 433 | PRINTK2("%s: TX timeout, sending failed...\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 434 | |
| 435 | /* release packet */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 436 | SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 437 | |
| 438 | /* wait for MMU getting ready (low) */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 439 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 440 | udelay (10); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 441 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 442 | PRINTK2("MMU ready\n"); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 443 | |
| 444 | |
| 445 | return 0; |
| 446 | } else { |
| 447 | /* ack. int */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 448 | SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 449 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 450 | PRINTK2("%s: Sent packet of length %d \n", dev->name, length); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 451 | |
| 452 | /* release packet */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 453 | SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 454 | |
| 455 | /* wait for MMU getting ready (low) */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 456 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 457 | udelay (10); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 458 | |
| 459 | PRINTK2 ("MMU ready\n"); |
| 460 | } |
| 461 | |
| 462 | return length; |
| 463 | } |
| 464 | |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 465 | |
| 466 | /* |
| 467 | * Open and Initialize the board |
| 468 | * |
| 469 | * Set up everything, reset the card, etc .. |
| 470 | * |
| 471 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 472 | static int smc_open(bd_t *bd, struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 473 | { |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 474 | int i, err; /* used to set hw ethernet address */ |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 475 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 476 | PRINTK2("%s:smc_open\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 477 | |
| 478 | /* reset the hardware */ |
| 479 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 480 | smc_reset(dev); |
| 481 | smc_enable(dev); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 482 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 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); |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 486 | if (err < 0) |
| 487 | return -1; |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 488 | #ifdef USE_32_BIT |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 489 | for (i = 0; i < 6; i += 2) { |
| 490 | word address; |
| 491 | |
| 492 | address = smc_mac_addr[i + 1] << 8; |
| 493 | address |= smc_mac_addr[i]; |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 494 | SMC_outw(dev, address, LAN91C96_IA0 + i); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 495 | } |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 496 | #else |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 497 | for (i = 0; i < 6; i++) |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 498 | SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i); |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 499 | #endif |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 500 | return 0; |
| 501 | } |
| 502 | |
| 503 | /*------------------------------------------------------------- |
| 504 | * |
| 505 | * smc_rcv - receive a packet from the card |
| 506 | * |
| 507 | * There is ( at least ) a packet waiting to be read from |
| 508 | * chip-memory. |
| 509 | * |
| 510 | * o Read the status |
| 511 | * o If an error, record it |
| 512 | * o otherwise, read in the packet |
| 513 | *------------------------------------------------------------- |
| 514 | */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 515 | static int smc_rcv(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 516 | { |
| 517 | int packet_number; |
| 518 | word status; |
| 519 | word packet_length; |
| 520 | int is_error = 0; |
| 521 | |
| 522 | #ifdef USE_32_BIT |
| 523 | dword stat_len; |
| 524 | #endif |
| 525 | |
| 526 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 527 | SMC_SELECT_BANK(dev, 2); |
| 528 | packet_number = SMC_inw(dev, LAN91C96_FIFO); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 529 | |
| 530 | if (packet_number & LAN91C96_FIFO_RXEMPTY) { |
| 531 | return 0; |
| 532 | } |
| 533 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 534 | PRINTK3("%s:smc_rcv\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 535 | /* start reading from the start of the packet */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 536 | SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV | |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 537 | LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER); |
| 538 | |
| 539 | /* First two words are status and packet_length */ |
| 540 | #ifdef USE_32_BIT |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 541 | stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 542 | status = stat_len & 0xffff; |
| 543 | packet_length = stat_len >> 16; |
| 544 | #else |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 545 | status = SMC_inw(dev, LAN91C96_DATA_HIGH); |
| 546 | packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 547 | #endif |
| 548 | |
| 549 | packet_length &= 0x07ff; /* mask off top bits */ |
| 550 | |
| 551 | PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length); |
| 552 | |
| 553 | if (!(status & FRAME_FILTER)) { |
| 554 | /* Adjust for having already read the first two words */ |
| 555 | packet_length -= 4; /*4; */ |
| 556 | |
| 557 | |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 558 | /* set odd length for bug in LAN91C111, */ |
| 559 | /* which never sets RS_ODDFRAME */ |
| 560 | /* TODO ? */ |
| 561 | |
| 562 | |
| 563 | #ifdef USE_32_BIT |
| 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 |
| 569 | performance */ |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 570 | SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0], |
| 571 | packet_length >> 2); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 572 | /* read the left over bytes */ |
| 573 | if (packet_length & 3) { |
| 574 | int i; |
| 575 | |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 576 | byte *tail = (byte *)(net_rx_packets[0] + |
| 577 | (packet_length & ~3)); |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 578 | dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 579 | |
| 580 | for (i = 0; i < (packet_length & 3); i++) |
| 581 | *tail++ = (byte) (leftover >> (8 * i)) & 0xff; |
| 582 | } |
| 583 | #else |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 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], |
| 587 | packet_length >> 1); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 588 | |
| 589 | #endif /* USE_32_BIT */ |
| 590 | |
| 591 | #if SMC_DEBUG > 2 |
| 592 | printf ("Receiving Packet\n"); |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 593 | print_packet((byte *)net_rx_packets[0], packet_length); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 594 | #endif |
| 595 | } else { |
| 596 | /* error ... */ |
| 597 | /* TODO ? */ |
| 598 | is_error = 1; |
| 599 | } |
| 600 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 601 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 602 | udelay (1); /* Wait until not busy */ |
| 603 | |
| 604 | /* error or good, tell the card to get rid of this packet */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 605 | SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 606 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 607 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 608 | udelay (1); /* Wait until not busy */ |
| 609 | |
| 610 | if (!is_error) { |
| 611 | /* Pass the packet up to the protocol layers. */ |
Joe Hershberger | 9f09a36 | 2015-04-08 01:41:06 -0500 | [diff] [blame] | 612 | net_process_received_packet(net_rx_packets[0], packet_length); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 613 | return packet_length; |
| 614 | } else { |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | } |
| 619 | |
| 620 | /*---------------------------------------------------- |
| 621 | * smc_close |
| 622 | * |
| 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' |
| 626 | * |
| 627 | -----------------------------------------------------*/ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 628 | static int smc_close(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 629 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 630 | PRINTK2("%s:smc_close\n", dev->name); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 631 | |
| 632 | /* clear everything */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 633 | smc_shutdown(dev); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 634 | |
| 635 | return 0; |
| 636 | } |
| 637 | |
| 638 | #if SMC_DEBUG > 2 |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 639 | static void print_packet(byte *buf, int length) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 640 | { |
| 641 | #if 0 |
| 642 | int i; |
| 643 | int remainder; |
| 644 | int lines; |
| 645 | |
| 646 | printf ("Packet of length %d \n", length); |
| 647 | |
| 648 | lines = length / 16; |
| 649 | remainder = length % 16; |
| 650 | |
| 651 | for (i = 0; i < lines; i++) { |
| 652 | int cur; |
| 653 | |
| 654 | for (cur = 0; cur < 8; cur++) { |
| 655 | byte a, b; |
| 656 | |
| 657 | a = *(buf++); |
| 658 | b = *(buf++); |
| 659 | printf ("%02x%02x ", a, b); |
| 660 | } |
| 661 | printf ("\n"); |
| 662 | } |
| 663 | for (i = 0; i < remainder / 2; i++) { |
| 664 | byte a, b; |
| 665 | |
| 666 | a = *(buf++); |
| 667 | b = *(buf++); |
| 668 | printf ("%02x%02x ", a, b); |
| 669 | } |
| 670 | printf ("\n"); |
| 671 | #endif /* 0 */ |
| 672 | } |
| 673 | #endif /* SMC_DEBUG > 2 */ |
| 674 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 675 | static int lan91c96_init(struct eth_device *dev, bd_t *bd) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 676 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 677 | return smc_open(bd, dev); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 680 | static void lan91c96_halt(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 681 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 682 | smc_close(dev); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 685 | static int lan91c96_recv(struct eth_device *dev) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 686 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 687 | return smc_rcv(dev); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 688 | } |
| 689 | |
Joe Hershberger | c78248f | 2012-05-21 14:45:30 +0000 | [diff] [blame] | 690 | static int lan91c96_send(struct eth_device *dev, void *packet, |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 691 | int length) |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 692 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 693 | return smc_send_packet(dev, packet, length); |
wdenk | aa60336 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 696 | /* smc_get_ethaddr |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 697 | * |
| 698 | * This checks both the environment and the ROM for an ethernet address. If |
| 699 | * found, the environment takes precedence. |
| 700 | */ |
| 701 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 702 | static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev) |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 703 | { |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 704 | uchar v_mac[6]; |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 705 | |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 706 | if (!eth_env_get_enetaddr("ethaddr", v_mac)) { |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 707 | /* get ROM mac value if any */ |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 708 | if (!get_rom_mac(dev, v_mac)) { |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 709 | printf("\n*** ERROR: ethaddr is NOT set !!\n"); |
| 710 | return -1; |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 711 | } |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 712 | eth_env_set_enetaddr("ethaddr", v_mac); |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 713 | } |
| 714 | |
Mike Frysinger | 719b675 | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 715 | smc_set_mac_addr(v_mac); /* use old function to update smc default */ |
| 716 | PRINTK("Using MAC Address %pM\n", v_mac); |
| 717 | return 0; |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 718 | } |
| 719 | |
wdenk | e3a0680 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 720 | /* |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 721 | * get_rom_mac() |
| 722 | * Note, this has omly been tested for the OMAP730 P2. |
| 723 | */ |
| 724 | |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 725 | static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac) |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 726 | { |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 727 | int i; |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 728 | SMC_SELECT_BANK(dev, 1); |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 729 | for (i=0; i<6; i++) |
| 730 | { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 731 | v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i); |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 732 | } |
| 733 | return (1); |
wdenk | 6b1aa8d | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 734 | } |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 735 | |
| 736 | /* Structure to detect the device IDs */ |
| 737 | struct id_type { |
| 738 | u8 id; |
| 739 | char *name; |
| 740 | }; |
| 741 | static struct id_type supported_chips[] = { |
| 742 | {0, ""}, /* Dummy entry to prevent id check failure */ |
| 743 | {9, "LAN91C110"}, |
| 744 | {8, "LAN91C100FD"}, |
| 745 | {7, "LAN91C100"}, |
| 746 | {5, "LAN91C95"}, |
Yanjun Yang | f52a06c | 2010-12-26 10:40:54 +0800 | [diff] [blame] | 747 | {4, "LAN91C94/96"}, |
| 748 | {3, "LAN91C90/92"}, |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 749 | }; |
| 750 | /* lan91c96_detect_chip |
| 751 | * See: |
| 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 |
| 754 | */ |
| 755 | static int lan91c96_detect_chip(struct eth_device *dev) |
| 756 | { |
| 757 | u8 chip_id; |
| 758 | int r; |
| 759 | SMC_SELECT_BANK(dev, 3); |
Yanjun Yang | d99ecfec | 2010-12-28 16:08:25 +0800 | [diff] [blame] | 760 | chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4; |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 761 | SMC_SELECT_BANK(dev, 0); |
Axel Lin | 10a72b6 | 2013-07-03 11:24:18 +0800 | [diff] [blame] | 762 | for (r = 0; r < ARRAY_SIZE(supported_chips); r++) |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 763 | if (chip_id == supported_chips[r].id) |
| 764 | return r; |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | int lan91c96_initialize(u8 dev_num, int base_addr) |
| 769 | { |
| 770 | struct eth_device *dev; |
| 771 | int r = 0; |
| 772 | |
| 773 | dev = malloc(sizeof(*dev)); |
| 774 | if (!dev) { |
Nishanth Menon | f95b93b | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | memset(dev, 0, sizeof(*dev)); |
| 778 | |
| 779 | dev->iobase = base_addr; |
| 780 | |
| 781 | /* Try to detect chip. Will fail if not present. */ |
| 782 | r = lan91c96_detect_chip(dev); |
| 783 | if (!r) { |
| 784 | free(dev); |
| 785 | return 0; |
| 786 | } |
| 787 | get_rom_mac(dev, dev->enetaddr); |
| 788 | |
| 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); |
| 794 | |
| 795 | eth_register(dev); |
| 796 | return 0; |
| 797 | } |