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