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