wdenk | 45219c4 | 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 | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 39 | * io = for the base address |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 40 | * irq = for the IRQ |
| 41 | * |
| 42 | * author: |
Jean-Christophe PLAGNIOL-VILLARD | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 43 | * Erik Stahlman ( erik@vt.edu ) |
| 44 | * Daris A Nevil ( dnevil@snmc.com ) |
wdenk | 45219c4 | 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 | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 56 | * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version) |
wdenk | 45219c4 | 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 | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 63 | #include <malloc.h> |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 64 | #include "lan91c96.h" |
| 65 | #include <net.h> |
| 66 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 67 | /*------------------------------------------------------------------------ |
| 68 | * |
| 69 | * Configuration options, for the experienced user to change. |
| 70 | * |
| 71 | -------------------------------------------------------------------------*/ |
| 72 | |
| 73 | /* Use power-down feature of the chip */ |
| 74 | #define POWER_DOWN 0 |
| 75 | |
| 76 | /* |
| 77 | * Wait time for memory to be free. This probably shouldn't be |
| 78 | * tuned that much, as waiting for this means nothing else happens |
| 79 | * in the system |
| 80 | */ |
| 81 | #define MEMORY_WAIT_TIME 16 |
| 82 | |
| 83 | #define SMC_DEBUG 0 |
| 84 | |
| 85 | #if (SMC_DEBUG > 2 ) |
| 86 | #define PRINTK3(args...) printf(args) |
| 87 | #else |
| 88 | #define PRINTK3(args...) |
| 89 | #endif |
| 90 | |
| 91 | #if SMC_DEBUG > 1 |
| 92 | #define PRINTK2(args...) printf(args) |
| 93 | #else |
| 94 | #define PRINTK2(args...) |
| 95 | #endif |
| 96 | |
| 97 | #ifdef SMC_DEBUG |
| 98 | #define PRINTK(args...) printf(args) |
| 99 | #else |
| 100 | #define PRINTK(args...) |
| 101 | #endif |
| 102 | |
| 103 | |
| 104 | /*------------------------------------------------------------------------ |
| 105 | * |
| 106 | * The internal workings of the driver. If you are changing anything |
| 107 | * here with the SMC stuff, you should have the datasheet and know |
| 108 | * what you are doing. |
| 109 | * |
| 110 | *------------------------------------------------------------------------ |
| 111 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 112 | #define DRIVER_NAME "LAN91C96" |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 113 | #define SMC_ALLOC_MAX_TRY 5 |
| 114 | #define SMC_TX_TIMEOUT 30 |
| 115 | |
| 116 | #define ETH_ZLEN 60 |
| 117 | |
| 118 | #ifdef CONFIG_LAN91C96_USE_32_BIT |
| 119 | #define USE_32_BIT 1 |
| 120 | #else |
| 121 | #undef USE_32_BIT |
| 122 | #endif |
| 123 | |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 124 | /* See if a MAC address is defined in the current environment. If so use it. If not |
| 125 | . print a warning and set the environment and other globals with the default. |
| 126 | . If an EEPROM is present it really should be consulted. |
| 127 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 128 | static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev); |
| 129 | static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 130 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 131 | /* ------------------------------------------------------------ |
| 132 | * Internal routines |
| 133 | * ------------------------------------------------------------ |
| 134 | */ |
| 135 | |
Wolfgang Denk | d52fb7e | 2006-03-11 22:53:33 +0100 | [diff] [blame] | 136 | static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c }; |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 137 | |
| 138 | /* |
| 139 | * This function must be called before smc_open() if you want to override |
| 140 | * the default mac address. |
| 141 | */ |
| 142 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 143 | static void smc_set_mac_addr(const unsigned char *addr) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 144 | { |
| 145 | int i; |
| 146 | |
| 147 | for (i = 0; i < sizeof (smc_mac_addr); i++) { |
| 148 | smc_mac_addr[i] = addr[i]; |
| 149 | } |
| 150 | } |
| 151 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 152 | /*********************************************** |
| 153 | * Show available memory * |
| 154 | ***********************************************/ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 155 | void dump_memory_info(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 156 | { |
| 157 | word mem_info; |
| 158 | word old_bank; |
| 159 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 160 | old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF; |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 161 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 162 | SMC_SELECT_BANK(dev, 0); |
| 163 | mem_info = SMC_inw(dev, LAN91C96_MIR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 164 | PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048); |
| 165 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 166 | SMC_SELECT_BANK(dev, old_bank); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | /* |
| 170 | * A rather simple routine to print out a packet for debugging purposes. |
| 171 | */ |
| 172 | #if SMC_DEBUG > 2 |
| 173 | static void print_packet (byte *, int); |
| 174 | #endif |
| 175 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 176 | static int poll4int (struct eth_device *dev, byte mask, int timeout) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 177 | { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 178 | int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ; |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 179 | int is_timeout = 0; |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 180 | word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 181 | |
| 182 | PRINTK2 ("Polling...\n"); |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 183 | SMC_SELECT_BANK(dev, 2); |
| 184 | while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) { |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 185 | if (get_timer (0) >= tmo) { |
| 186 | is_timeout = 1; |
| 187 | break; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /* restore old bank selection */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 192 | SMC_SELECT_BANK(dev, old_bank); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 193 | |
| 194 | if (is_timeout) |
| 195 | return 1; |
| 196 | else |
| 197 | return 0; |
| 198 | } |
| 199 | |
| 200 | /* |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 201 | * Function: smc_reset |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 202 | * Purpose: |
Jean-Christophe PLAGNIOL-VILLARD | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 203 | * This sets the SMC91111 chip to its normal state, hopefully from whatever |
| 204 | * mess that any other DOS driver has put it in. |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 205 | * |
| 206 | * Maybe I should reset more registers to defaults in here? SOFTRST should |
| 207 | * do that for me. |
| 208 | * |
| 209 | * Method: |
| 210 | * 1. send a SOFT RESET |
| 211 | * 2. wait for it to finish |
| 212 | * 3. enable autorelease mode |
| 213 | * 4. reset the memory management unit |
| 214 | * 5. clear all interrupts |
| 215 | * |
| 216 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 217 | static void smc_reset(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 218 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 219 | PRINTK2("%s:smc_reset\n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 220 | |
| 221 | /* This resets the registers mostly to defaults, but doesn't |
| 222 | affect EEPROM. That seems unnecessary */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 223 | SMC_SELECT_BANK(dev, 0); |
| 224 | SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 225 | |
| 226 | udelay (10); |
| 227 | |
| 228 | /* Disable transmit and receive functionality */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 229 | SMC_outw(dev, 0, LAN91C96_RCR); |
| 230 | SMC_outw(dev, 0, LAN91C96_TCR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 231 | |
| 232 | /* set the control register */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 233 | SMC_SELECT_BANK(dev, 1); |
| 234 | SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8, |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 235 | LAN91C96_CONTROL); |
| 236 | |
| 237 | /* Disable all interrupts */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 238 | SMC_outb(dev, 0, LAN91C96_INT_MASK); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | /* |
| 242 | * Function: smc_enable |
| 243 | * Purpose: let the chip talk to the outside work |
| 244 | * Method: |
| 245 | * 1. Initialize the Memory Configuration Register |
| 246 | * 2. Enable the transmitter |
| 247 | * 3. Enable the receiver |
| 248 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 249 | static void smc_enable(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 250 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 251 | PRINTK2("%s:smc_enable\n", dev->name); |
| 252 | SMC_SELECT_BANK(dev, 0); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 253 | |
| 254 | /* Initialize the Memory Configuration Register. See page |
| 255 | 49 of the LAN91C96 data sheet for details. */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 256 | SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 257 | |
| 258 | /* Initialize the Transmit Control Register */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 259 | SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 260 | /* Initialize the Receive Control Register |
| 261 | * FIXME: |
| 262 | * The promiscuous bit set because I could not receive ARP reply |
| 263 | * packets from the server when I send a ARP request. It only works |
| 264 | * when I set the promiscuous bit |
| 265 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 266 | SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | /* |
| 270 | * Function: smc_shutdown |
| 271 | * Purpose: closes down the SMC91xxx chip. |
| 272 | * Method: |
| 273 | * 1. zero the interrupt mask |
| 274 | * 2. clear the enable receive flag |
| 275 | * 3. clear the enable xmit flags |
| 276 | * |
| 277 | * TODO: |
| 278 | * (1) maybe utilize power down mode. |
| 279 | * Why not yet? Because while the chip will go into power down mode, |
| 280 | * the manual says that it will wake up in response to any I/O requests |
| 281 | * in the register space. Empirical results do not show this working. |
| 282 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 283 | static void smc_shutdown(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 284 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 285 | PRINTK2("%s:smc_shutdown\n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 286 | |
| 287 | /* no more interrupts for me */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 288 | SMC_SELECT_BANK(dev, 2); |
| 289 | SMC_outb(dev, 0, LAN91C96_INT_MASK); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 290 | |
| 291 | /* and tell the card to stay away from that nasty outside world */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 292 | SMC_SELECT_BANK(dev, 0); |
| 293 | SMC_outb(dev, 0, LAN91C96_RCR); |
| 294 | SMC_outb(dev, 0, LAN91C96_TCR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | |
| 298 | /* |
| 299 | * Function: smc_hardware_send_packet(struct net_device * ) |
| 300 | * Purpose: |
| 301 | * This sends the actual packet to the SMC9xxx chip. |
| 302 | * |
| 303 | * Algorithm: |
Jean-Christophe PLAGNIOL-VILLARD | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 304 | * First, see if a saved_skb is available. |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 305 | * ( this should NOT be called if there is no 'saved_skb' |
| 306 | * Now, find the packet number that the chip allocated |
| 307 | * Point the data pointers at it in memory |
| 308 | * Set the length word in the chip's memory |
| 309 | * Dump the packet to chip memory |
| 310 | * Check if a last byte is needed ( odd length packet ) |
| 311 | * if so, set the control flag right |
Jean-Christophe PLAGNIOL-VILLARD | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 312 | * Tell the card to send it |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 313 | * Enable the transmit interrupt, so I know if it failed |
Jean-Christophe PLAGNIOL-VILLARD | 1b76988 | 2008-01-25 07:54:47 +0100 | [diff] [blame] | 314 | * Free the kernel data if I actually sent it. |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 315 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 316 | static int smc_send_packet(struct eth_device *dev, volatile void *packet, |
| 317 | int packet_length) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 318 | { |
| 319 | byte packet_no; |
| 320 | unsigned long ioaddr; |
| 321 | byte *buf; |
| 322 | int length; |
| 323 | int numPages; |
| 324 | int try = 0; |
| 325 | int time_out; |
| 326 | byte status; |
| 327 | |
| 328 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 329 | PRINTK3("%s:smc_hardware_send_packet\n", dev->name); |
wdenk | 45219c4 | 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 | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 349 | printf("%s: Far too big packet error. \n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* now, try to allocate the memory */ |
| 354 | |
Nishanth Menon | b7ad410 | 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 | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 357 | |
| 358 | again: |
| 359 | try++; |
| 360 | time_out = MEMORY_WAIT_TIME; |
| 361 | do { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 362 | status = SMC_inb(dev, LAN91C96_INT_STATS); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 363 | if (status & LAN91C96_IST_ALLOC_INT) { |
| 364 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 365 | SMC_outb(dev, LAN91C96_IST_ALLOC_INT, |
| 366 | LAN91C96_INT_STATS); |
wdenk | 45219c4 | 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 | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 373 | dev->name, try); |
wdenk | 45219c4 | 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 | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 381 | dev->name, try); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 382 | |
| 383 | /* I can send the packet now.. */ |
| 384 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 385 | ioaddr = dev->iobase; |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 386 | |
| 387 | buf = (byte *) packet; |
| 388 | |
| 389 | /* If I get here, I _know_ there is a packet slot waiting for me */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 390 | packet_no = SMC_inb(dev, LAN91C96_ARR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 391 | if (packet_no & LAN91C96_ARR_FAILED) { |
| 392 | /* or isn't there? BAD CHIP! */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 393 | printf("%s: Memory allocation failed. \n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* we have a packet address, so tell the card to use it */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 398 | SMC_outb(dev, packet_no, LAN91C96_PNR); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 399 | |
| 400 | /* point to the beginning of the packet */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 401 | SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 402 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 403 | PRINTK3("%s: Trying to xmit packet of length %x\n", |
| 404 | dev->name, length); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 405 | |
| 406 | #if SMC_DEBUG > 2 |
| 407 | printf ("Transmitting Packet\n"); |
| 408 | print_packet (buf, length); |
| 409 | #endif |
| 410 | |
| 411 | /* send the packet length ( +6 for status, length and ctl byte ) |
| 412 | and the status word ( set to zeros ) */ |
| 413 | #ifdef USE_32_BIT |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 414 | SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 415 | #else |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 416 | SMC_outw(dev, 0, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 417 | /* send the packet length ( +6 for status words, length, and ctl */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 418 | SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 419 | #endif /* USE_32_BIT */ |
| 420 | |
| 421 | /* send the actual data |
| 422 | * I _think_ it's faster to send the longs first, and then |
| 423 | * mop up by sending the last word. It depends heavily |
| 424 | * on alignment, at least on the 486. Maybe it would be |
| 425 | * a good idea to check which is optimal? But that could take |
| 426 | * almost as much time as is saved? |
| 427 | */ |
| 428 | #ifdef USE_32_BIT |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 429 | SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 430 | if (length & 0x2) |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 431 | SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))), |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 432 | LAN91C96_DATA_HIGH); |
| 433 | #else |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 434 | SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 435 | #endif /* USE_32_BIT */ |
| 436 | |
| 437 | /* Send the last byte, if there is one. */ |
| 438 | if ((length & 1) == 0) { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 439 | SMC_outw(dev, 0, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 440 | } else { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 441 | SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | /* and let the chipset deal with it */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 445 | SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 446 | |
| 447 | /* poll for TX INT */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 448 | if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) { |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 449 | /* sending failed */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 450 | PRINTK2("%s: TX timeout, sending failed...\n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 451 | |
| 452 | /* release packet */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 453 | SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 454 | |
| 455 | /* wait for MMU getting ready (low) */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 456 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 457 | udelay (10); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 458 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 459 | PRINTK2("MMU ready\n"); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 460 | |
| 461 | |
| 462 | return 0; |
| 463 | } else { |
| 464 | /* ack. int */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 465 | SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 466 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 467 | PRINTK2("%s: Sent packet of length %d \n", dev->name, length); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 468 | |
| 469 | /* release packet */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 470 | SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 471 | |
| 472 | /* wait for MMU getting ready (low) */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 473 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 474 | udelay (10); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 475 | |
| 476 | PRINTK2 ("MMU ready\n"); |
| 477 | } |
| 478 | |
| 479 | return length; |
| 480 | } |
| 481 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 482 | |
| 483 | /* |
| 484 | * Open and Initialize the board |
| 485 | * |
| 486 | * Set up everything, reset the card, etc .. |
| 487 | * |
| 488 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 489 | static int smc_open(bd_t *bd, struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 490 | { |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 491 | int i, err; /* used to set hw ethernet address */ |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 492 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 493 | PRINTK2("%s:smc_open\n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 494 | |
| 495 | /* reset the hardware */ |
| 496 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 497 | smc_reset(dev); |
| 498 | smc_enable(dev); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 499 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 500 | SMC_SELECT_BANK(dev, 1); |
| 501 | /* set smc_mac_addr, and sync it with u-boot globals */ |
| 502 | err = smc_get_ethaddr(bd, dev); |
Mike Frysinger | 03f3d8d | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 503 | if (err < 0) |
| 504 | return -1; |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 505 | #ifdef USE_32_BIT |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 506 | for (i = 0; i < 6; i += 2) { |
| 507 | word address; |
| 508 | |
| 509 | address = smc_mac_addr[i + 1] << 8; |
| 510 | address |= smc_mac_addr[i]; |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 511 | SMC_outw(dev, address, LAN91C96_IA0 + i); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 512 | } |
wdenk | a56bd92 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 513 | #else |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 514 | for (i = 0; i < 6; i++) |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 515 | SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i); |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 516 | #endif |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 517 | return 0; |
| 518 | } |
| 519 | |
| 520 | /*------------------------------------------------------------- |
| 521 | * |
| 522 | * smc_rcv - receive a packet from the card |
| 523 | * |
| 524 | * There is ( at least ) a packet waiting to be read from |
| 525 | * chip-memory. |
| 526 | * |
| 527 | * o Read the status |
| 528 | * o If an error, record it |
| 529 | * o otherwise, read in the packet |
| 530 | *------------------------------------------------------------- |
| 531 | */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 532 | static int smc_rcv(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 533 | { |
| 534 | int packet_number; |
| 535 | word status; |
| 536 | word packet_length; |
| 537 | int is_error = 0; |
| 538 | |
| 539 | #ifdef USE_32_BIT |
| 540 | dword stat_len; |
| 541 | #endif |
| 542 | |
| 543 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 544 | SMC_SELECT_BANK(dev, 2); |
| 545 | packet_number = SMC_inw(dev, LAN91C96_FIFO); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 546 | |
| 547 | if (packet_number & LAN91C96_FIFO_RXEMPTY) { |
| 548 | return 0; |
| 549 | } |
| 550 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 551 | PRINTK3("%s:smc_rcv\n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 552 | /* start reading from the start of the packet */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 553 | SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 554 | LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER); |
| 555 | |
| 556 | /* First two words are status and packet_length */ |
| 557 | #ifdef USE_32_BIT |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 558 | stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 559 | status = stat_len & 0xffff; |
| 560 | packet_length = stat_len >> 16; |
| 561 | #else |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 562 | status = SMC_inw(dev, LAN91C96_DATA_HIGH); |
| 563 | packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 564 | #endif |
| 565 | |
| 566 | packet_length &= 0x07ff; /* mask off top bits */ |
| 567 | |
| 568 | PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length); |
| 569 | |
| 570 | if (!(status & FRAME_FILTER)) { |
| 571 | /* Adjust for having already read the first two words */ |
| 572 | packet_length -= 4; /*4; */ |
| 573 | |
| 574 | |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 575 | /* set odd length for bug in LAN91C111, */ |
| 576 | /* which never sets RS_ODDFRAME */ |
| 577 | /* TODO ? */ |
| 578 | |
| 579 | |
| 580 | #ifdef USE_32_BIT |
| 581 | PRINTK3 (" Reading %d dwords (and %d bytes) \n", |
| 582 | packet_length >> 2, packet_length & 3); |
| 583 | /* QUESTION: Like in the TX routine, do I want |
| 584 | to send the DWORDs or the bytes first, or some |
| 585 | mixture. A mixture might improve already slow PIO |
| 586 | performance */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 587 | SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0], |
| 588 | packet_length >> 2); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 589 | /* read the left over bytes */ |
| 590 | if (packet_length & 3) { |
| 591 | int i; |
| 592 | |
| 593 | byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3)); |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 594 | dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 595 | |
| 596 | for (i = 0; i < (packet_length & 3); i++) |
| 597 | *tail++ = (byte) (leftover >> (8 * i)) & 0xff; |
| 598 | } |
| 599 | #else |
| 600 | PRINTK3 (" Reading %d words and %d byte(s) \n", |
| 601 | (packet_length >> 1), packet_length & 1); |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 602 | SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0], |
| 603 | packet_length >> 1); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 604 | |
| 605 | #endif /* USE_32_BIT */ |
| 606 | |
| 607 | #if SMC_DEBUG > 2 |
| 608 | printf ("Receiving Packet\n"); |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 609 | print_packet((byte *)NetRxPackets[0], packet_length); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 610 | #endif |
| 611 | } else { |
| 612 | /* error ... */ |
| 613 | /* TODO ? */ |
| 614 | is_error = 1; |
| 615 | } |
| 616 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 617 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 618 | udelay (1); /* Wait until not busy */ |
| 619 | |
| 620 | /* error or good, tell the card to get rid of this packet */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 621 | SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 622 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 623 | while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 624 | udelay (1); /* Wait until not busy */ |
| 625 | |
| 626 | if (!is_error) { |
| 627 | /* Pass the packet up to the protocol layers. */ |
| 628 | NetReceive (NetRxPackets[0], packet_length); |
| 629 | return packet_length; |
| 630 | } else { |
| 631 | return 0; |
| 632 | } |
| 633 | |
| 634 | } |
| 635 | |
| 636 | /*---------------------------------------------------- |
| 637 | * smc_close |
| 638 | * |
| 639 | * this makes the board clean up everything that it can |
| 640 | * and not talk to the outside world. Caused by |
| 641 | * an 'ifconfig ethX down' |
| 642 | * |
| 643 | -----------------------------------------------------*/ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 644 | static int smc_close(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 645 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 646 | PRINTK2("%s:smc_close\n", dev->name); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 647 | |
| 648 | /* clear everything */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 649 | smc_shutdown(dev); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 650 | |
| 651 | return 0; |
| 652 | } |
| 653 | |
| 654 | #if SMC_DEBUG > 2 |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 655 | static void print_packet(byte *buf, int length) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 656 | { |
| 657 | #if 0 |
| 658 | int i; |
| 659 | int remainder; |
| 660 | int lines; |
| 661 | |
| 662 | printf ("Packet of length %d \n", length); |
| 663 | |
| 664 | lines = length / 16; |
| 665 | remainder = length % 16; |
| 666 | |
| 667 | for (i = 0; i < lines; i++) { |
| 668 | int cur; |
| 669 | |
| 670 | for (cur = 0; cur < 8; cur++) { |
| 671 | byte a, b; |
| 672 | |
| 673 | a = *(buf++); |
| 674 | b = *(buf++); |
| 675 | printf ("%02x%02x ", a, b); |
| 676 | } |
| 677 | printf ("\n"); |
| 678 | } |
| 679 | for (i = 0; i < remainder / 2; i++) { |
| 680 | byte a, b; |
| 681 | |
| 682 | a = *(buf++); |
| 683 | b = *(buf++); |
| 684 | printf ("%02x%02x ", a, b); |
| 685 | } |
| 686 | printf ("\n"); |
| 687 | #endif /* 0 */ |
| 688 | } |
| 689 | #endif /* SMC_DEBUG > 2 */ |
| 690 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 691 | static int lan91c96_init(struct eth_device *dev, bd_t *bd) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 692 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 693 | return smc_open(bd, dev); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 696 | static void lan91c96_halt(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 697 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 698 | smc_close(dev); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 701 | static int lan91c96_recv(struct eth_device *dev) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 702 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 703 | return smc_rcv(dev); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 706 | static int lan91c96_send(struct eth_device *dev, volatile void *packet, |
| 707 | int length) |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 708 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 709 | return smc_send_packet(dev, packet, length); |
wdenk | 45219c4 | 2003-05-12 21:50:16 +0000 | [diff] [blame] | 710 | } |
| 711 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 712 | /* smc_get_ethaddr |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 713 | * |
| 714 | * This checks both the environment and the ROM for an ethernet address. If |
| 715 | * found, the environment takes precedence. |
| 716 | */ |
| 717 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 718 | static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev) |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 719 | { |
Mike Frysinger | 03f3d8d | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 720 | uchar v_mac[6]; |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 721 | |
Mike Frysinger | 03f3d8d | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 722 | if (!eth_getenv_enetaddr("ethaddr", v_mac)) { |
| 723 | /* get ROM mac value if any */ |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 724 | if (!get_rom_mac(dev, v_mac)) { |
Mike Frysinger | 03f3d8d | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 725 | printf("\n*** ERROR: ethaddr is NOT set !!\n"); |
| 726 | return -1; |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 727 | } |
Mike Frysinger | 03f3d8d | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 728 | eth_setenv_enetaddr("ethaddr", v_mac); |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 729 | } |
| 730 | |
Mike Frysinger | 03f3d8d | 2009-02-11 19:09:54 -0500 | [diff] [blame] | 731 | smc_set_mac_addr(v_mac); /* use old function to update smc default */ |
| 732 | PRINTK("Using MAC Address %pM\n", v_mac); |
| 733 | return 0; |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 734 | } |
| 735 | |
wdenk | a56bd92 | 2004-06-06 23:13:55 +0000 | [diff] [blame] | 736 | /* |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 737 | * get_rom_mac() |
| 738 | * Note, this has omly been tested for the OMAP730 P2. |
| 739 | */ |
| 740 | |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 741 | static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac) |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 742 | { |
| 743 | #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */ |
| 744 | char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 }; |
| 745 | |
| 746 | memcpy (v_rom_mac, hw_mac_addr, 6); |
| 747 | return (1); |
| 748 | #else |
| 749 | int i; |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 750 | SMC_SELECT_BANK(dev, 1); |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 751 | for (i=0; i<6; i++) |
| 752 | { |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 753 | v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i); |
wdenk | 5ca2679 | 2004-06-06 22:11:41 +0000 | [diff] [blame] | 754 | } |
| 755 | return (1); |
| 756 | #endif |
| 757 | } |
Nishanth Menon | b7ad410 | 2009-10-16 00:06:35 -0500 | [diff] [blame] | 758 | |
| 759 | /* Structure to detect the device IDs */ |
| 760 | struct id_type { |
| 761 | u8 id; |
| 762 | char *name; |
| 763 | }; |
| 764 | static struct id_type supported_chips[] = { |
| 765 | {0, ""}, /* Dummy entry to prevent id check failure */ |
| 766 | {9, "LAN91C110"}, |
| 767 | {8, "LAN91C100FD"}, |
| 768 | {7, "LAN91C100"}, |
| 769 | {5, "LAN91C95"}, |
| 770 | {4, "LAN91C94/LAN91C96"}, |
| 771 | {3, "LAN91C90/LAN91C92"}, |
| 772 | }; |
| 773 | /* lan91c96_detect_chip |
| 774 | * See: |
| 775 | * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf |
| 776 | * page 71 - that is the closest we get to detect this device |
| 777 | */ |
| 778 | static int lan91c96_detect_chip(struct eth_device *dev) |
| 779 | { |
| 780 | u8 chip_id; |
| 781 | int r; |
| 782 | SMC_SELECT_BANK(dev, 3); |
| 783 | chip_id = SMC_inw(dev, 0xA) & LAN91C96_REV_REVID; |
| 784 | SMC_SELECT_BANK(dev, 0); |
| 785 | for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++) |
| 786 | if (chip_id == supported_chips[r].id) |
| 787 | return r; |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | int lan91c96_initialize(u8 dev_num, int base_addr) |
| 792 | { |
| 793 | struct eth_device *dev; |
| 794 | int r = 0; |
| 795 | |
| 796 | dev = malloc(sizeof(*dev)); |
| 797 | if (!dev) { |
| 798 | free(dev); |
| 799 | return 0; |
| 800 | } |
| 801 | memset(dev, 0, sizeof(*dev)); |
| 802 | |
| 803 | dev->iobase = base_addr; |
| 804 | |
| 805 | /* Try to detect chip. Will fail if not present. */ |
| 806 | r = lan91c96_detect_chip(dev); |
| 807 | if (!r) { |
| 808 | free(dev); |
| 809 | return 0; |
| 810 | } |
| 811 | get_rom_mac(dev, dev->enetaddr); |
| 812 | |
| 813 | dev->init = lan91c96_init; |
| 814 | dev->halt = lan91c96_halt; |
| 815 | dev->send = lan91c96_send; |
| 816 | dev->recv = lan91c96_recv; |
| 817 | sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num); |
| 818 | |
| 819 | eth_register(dev); |
| 820 | return 0; |
| 821 | } |