wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1 | /*------------------------------------------------------------------------ |
| 2 | . smc91111.c |
| 3 | . This is a driver for SMSC's 91C111 single-chip Ethernet device. |
| 4 | . |
| 5 | . (C) Copyright 2002 |
| 6 | . Sysgo Real-Time Solutions, GmbH <www.elinos.com> |
| 7 | . Rolf Offermanns <rof@sysgo.de> |
| 8 | . |
| 9 | . Copyright (C) 2001 Standard Microsystems Corporation (SMSC) |
| 10 | . Developed by Simple Network Magic Corporation (SNMC) |
| 11 | . Copyright (C) 1996 by Erik Stahlman (ES) |
| 12 | . |
| 13 | . This program is free software; you can redistribute it and/or modify |
| 14 | . it under the terms of the GNU General Public License as published by |
| 15 | . the Free Software Foundation; either version 2 of the License, or |
| 16 | . (at your option) any later version. |
| 17 | . |
| 18 | . This program is distributed in the hope that it will be useful, |
| 19 | . but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 20 | . MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 21 | . GNU General Public License for more details. |
| 22 | . |
| 23 | . You should have received a copy of the GNU General Public License |
| 24 | . along with this program; if not, write to the Free Software |
| 25 | . Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 26 | . |
| 27 | . Information contained in this file was obtained from the LAN91C111 |
| 28 | . manual from SMC. To get a copy, if you really want one, you can find |
| 29 | . information under www.smsc.com. |
| 30 | . |
| 31 | . |
| 32 | . "Features" of the SMC chip: |
| 33 | . Integrated PHY/MAC for 10/100BaseT Operation |
| 34 | . Supports internal and external MII |
| 35 | . Integrated 8K packet memory |
| 36 | . EEPROM interface for configuration |
| 37 | . |
| 38 | . Arguments: |
| 39 | . io = for the base address |
| 40 | . irq = for the IRQ |
| 41 | . |
| 42 | . author: |
| 43 | . Erik Stahlman ( erik@vt.edu ) |
| 44 | . Daris A Nevil ( dnevil@snmc.com ) |
| 45 | . |
| 46 | . |
| 47 | . Hardware multicast code from Peter Cammaert ( pc@denkart.be ) |
| 48 | . |
| 49 | . Sources: |
| 50 | . o SMSC LAN91C111 databook (www.smsc.com) |
| 51 | . o smc9194.c by Erik Stahlman |
| 52 | . o skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov ) |
| 53 | . |
| 54 | . History: |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 55 | . 06/19/03 Richard Woodruff Made u-boot environment aware and added mac addr checks. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 56 | . 10/17/01 Marco Hasewinkel Modify for DNP/1110 |
| 57 | . 07/25/01 Woojung Huh Modify for ADS Bitsy |
| 58 | . 04/25/01 Daris A Nevil Initial public release through SMSC |
| 59 | . 03/16/01 Daris A Nevil Modified smc9194.c for use with LAN91C111 |
| 60 | ----------------------------------------------------------------------------*/ |
| 61 | |
| 62 | #include <common.h> |
| 63 | #include <command.h> |
| 64 | #include "smc91111.h" |
| 65 | #include <net.h> |
| 66 | |
| 67 | #ifdef CONFIG_DRIVER_SMC91111 |
| 68 | |
| 69 | /* Use power-down feature of the chip */ |
| 70 | #define POWER_DOWN 0 |
| 71 | |
| 72 | #define NO_AUTOPROBE |
| 73 | |
| 74 | static const char version[] = |
| 75 | "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n"; |
| 76 | |
| 77 | #define SMC_DEBUG 0 |
| 78 | |
| 79 | /*------------------------------------------------------------------------ |
| 80 | . |
| 81 | . Configuration options, for the experienced user to change. |
| 82 | . |
| 83 | -------------------------------------------------------------------------*/ |
| 84 | |
| 85 | /* |
| 86 | . Wait time for memory to be free. This probably shouldn't be |
| 87 | . tuned that much, as waiting for this means nothing else happens |
| 88 | . in the system |
| 89 | */ |
| 90 | #define MEMORY_WAIT_TIME 16 |
| 91 | |
| 92 | |
| 93 | #if (SMC_DEBUG > 2 ) |
| 94 | #define PRINTK3(args...) printf(args) |
| 95 | #else |
| 96 | #define PRINTK3(args...) |
| 97 | #endif |
| 98 | |
| 99 | #if SMC_DEBUG > 1 |
| 100 | #define PRINTK2(args...) printf(args) |
| 101 | #else |
| 102 | #define PRINTK2(args...) |
| 103 | #endif |
| 104 | |
| 105 | #ifdef SMC_DEBUG |
| 106 | #define PRINTK(args...) printf(args) |
| 107 | #else |
| 108 | #define PRINTK(args...) |
| 109 | #endif |
| 110 | |
| 111 | |
| 112 | /*------------------------------------------------------------------------ |
| 113 | . |
| 114 | . The internal workings of the driver. If you are changing anything |
| 115 | . here with the SMC stuff, you should have the datasheet and know |
| 116 | . what you are doing. |
| 117 | . |
| 118 | -------------------------------------------------------------------------*/ |
| 119 | #define CARDNAME "LAN91C111" |
| 120 | |
| 121 | /* Memory sizing constant */ |
| 122 | #define LAN91C111_MEMORY_MULTIPLIER (1024*2) |
| 123 | |
| 124 | #ifndef CONFIG_SMC91111_BASE |
| 125 | #define CONFIG_SMC91111_BASE 0x20000300 |
| 126 | #endif |
| 127 | |
| 128 | #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE |
| 129 | |
| 130 | #define SMC_DEV_NAME "SMC91111" |
| 131 | #define SMC_PHY_ADDR 0x0000 |
| 132 | #define SMC_ALLOC_MAX_TRY 5 |
| 133 | #define SMC_TX_TIMEOUT 30 |
| 134 | |
| 135 | #define SMC_PHY_CLOCK_DELAY 1000 |
| 136 | |
| 137 | #define ETH_ZLEN 60 |
| 138 | |
| 139 | #ifdef CONFIG_SMC_USE_32_BIT |
| 140 | #define USE_32_BIT 1 |
| 141 | #else |
| 142 | #undef USE_32_BIT |
| 143 | #endif |
| 144 | /*----------------------------------------------------------------- |
| 145 | . |
| 146 | . The driver can be entered at any of the following entry points. |
| 147 | . |
| 148 | .------------------------------------------------------------------ */ |
| 149 | |
| 150 | extern int eth_init(bd_t *bd); |
| 151 | extern void eth_halt(void); |
| 152 | extern int eth_rx(void); |
| 153 | extern int eth_send(volatile void *packet, int length); |
| 154 | |
| 155 | |
| 156 | |
| 157 | |
| 158 | |
| 159 | /* |
| 160 | . This is called by register_netdev(). It is responsible for |
| 161 | . checking the portlist for the SMC9000 series chipset. If it finds |
| 162 | . one, then it will initialize the device, find the hardware information, |
| 163 | . and sets up the appropriate device parameters. |
| 164 | . NOTE: Interrupts are *OFF* when this procedure is called. |
| 165 | . |
| 166 | . NB:This shouldn't be static since it is referred to externally. |
| 167 | */ |
| 168 | int smc_init(void); |
| 169 | |
| 170 | /* |
| 171 | . This is called by unregister_netdev(). It is responsible for |
| 172 | . cleaning up before the driver is finally unregistered and discarded. |
| 173 | */ |
| 174 | void smc_destructor(void); |
| 175 | |
| 176 | /* |
| 177 | . The kernel calls this function when someone wants to use the device, |
| 178 | . typically 'ifconfig ethX up'. |
| 179 | */ |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 180 | static int smc_open(bd_t *bd); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 181 | |
| 182 | |
| 183 | /* |
| 184 | . This is called by the kernel in response to 'ifconfig ethX down'. It |
| 185 | . is responsible for cleaning up everything that the open routine |
| 186 | . does, and maybe putting the card into a powerdown state. |
| 187 | */ |
| 188 | static int smc_close(void); |
| 189 | |
| 190 | /* |
| 191 | . Configures the PHY through the MII Management interface |
| 192 | */ |
| 193 | #ifndef CONFIG_SMC91111_EXT_PHY |
| 194 | static void smc_phy_configure(void); |
| 195 | #endif /* !CONFIG_SMC91111_EXT_PHY */ |
| 196 | |
| 197 | /* |
| 198 | . This is a separate procedure to handle the receipt of a packet, to |
| 199 | . leave the interrupt code looking slightly cleaner |
| 200 | */ |
| 201 | static int smc_rcv(void); |
| 202 | |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 203 | /* See if a MAC address is defined in the current environment. If so use it. If not |
| 204 | . print a warning and set the environment and other globals with the default. |
| 205 | . If an EEPROM is present it really should be consulted. |
| 206 | */ |
| 207 | int smc_get_ethaddr(bd_t *bd); |
| 208 | int get_rom_mac(char *v_rom_mac); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 209 | |
| 210 | /* |
| 211 | ------------------------------------------------------------ |
| 212 | . |
| 213 | . Internal routines |
| 214 | . |
| 215 | ------------------------------------------------------------ |
| 216 | */ |
| 217 | |
| 218 | static char smc_mac_addr[] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8}; |
| 219 | |
| 220 | /* |
| 221 | * This function must be called before smc_open() if you want to override |
| 222 | * the default mac address. |
| 223 | */ |
| 224 | |
| 225 | void smc_set_mac_addr(const char *addr) { |
| 226 | int i; |
| 227 | |
| 228 | for (i=0; i < sizeof(smc_mac_addr); i++){ |
| 229 | smc_mac_addr[i] = addr[i]; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /* |
| 234 | * smc_get_macaddr is no longer used. If you want to override the default |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 235 | * mac address, call smc_get_mac_addr as a part of the board initialization. |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 236 | */ |
| 237 | |
| 238 | #if 0 |
| 239 | void smc_get_macaddr( byte *addr ) { |
| 240 | /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */ |
| 241 | unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010); |
| 242 | int i; |
| 243 | |
| 244 | |
| 245 | for (i=0; i<6; i++) { |
| 246 | addr[0] = *(dnp1110_mac+0); |
| 247 | addr[1] = *(dnp1110_mac+1); |
| 248 | addr[2] = *(dnp1110_mac+2); |
| 249 | addr[3] = *(dnp1110_mac+3); |
| 250 | addr[4] = *(dnp1110_mac+4); |
| 251 | addr[5] = *(dnp1110_mac+5); |
| 252 | } |
| 253 | } |
| 254 | #endif /* 0 */ |
| 255 | |
| 256 | /*********************************************** |
| 257 | * Show available memory * |
| 258 | ***********************************************/ |
| 259 | void dump_memory_info(void) |
| 260 | { |
| 261 | word mem_info; |
| 262 | word old_bank; |
| 263 | |
| 264 | old_bank = SMC_inw(BANK_SELECT)&0xF; |
| 265 | |
| 266 | SMC_SELECT_BANK(0); |
| 267 | mem_info = SMC_inw( MIR_REG ); |
| 268 | PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048); |
| 269 | |
| 270 | SMC_SELECT_BANK(old_bank); |
| 271 | } |
| 272 | /* |
| 273 | . A rather simple routine to print out a packet for debugging purposes. |
| 274 | */ |
| 275 | #if SMC_DEBUG > 2 |
| 276 | static void print_packet( byte *, int ); |
| 277 | #endif |
| 278 | |
| 279 | #define tx_done(dev) 1 |
| 280 | |
| 281 | |
| 282 | |
| 283 | /* this does a soft reset on the device */ |
| 284 | static void smc_reset( void ); |
| 285 | |
| 286 | /* Enable Interrupts, Receive, and Transmit */ |
| 287 | static void smc_enable( void ); |
| 288 | |
| 289 | /* this puts the device in an inactive state */ |
| 290 | static void smc_shutdown( void ); |
| 291 | |
| 292 | /* Routines to Read and Write the PHY Registers across the |
| 293 | MII Management Interface |
| 294 | */ |
| 295 | |
| 296 | #ifndef CONFIG_SMC91111_EXT_PHY |
| 297 | static word smc_read_phy_register(byte phyreg); |
| 298 | static void smc_write_phy_register(byte phyreg, word phydata); |
| 299 | #endif /* !CONFIG_SMC91111_EXT_PHY */ |
| 300 | |
| 301 | |
| 302 | static int poll4int( byte mask, int timeout ) { |
| 303 | int tmo = get_timer(0) + timeout * CFG_HZ; |
| 304 | int is_timeout = 0; |
| 305 | word old_bank = SMC_inw(BSR_REG); |
| 306 | |
| 307 | PRINTK2("Polling...\n"); |
| 308 | SMC_SELECT_BANK(2); |
| 309 | while((SMC_inw(SMC91111_INT_REG) & mask) == 0) |
| 310 | { |
| 311 | if (get_timer(0) >= tmo) { |
| 312 | is_timeout = 1; |
| 313 | break; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | /* restore old bank selection */ |
| 318 | SMC_SELECT_BANK(old_bank); |
| 319 | |
| 320 | if (is_timeout) |
| 321 | return 1; |
| 322 | else |
| 323 | return 0; |
| 324 | } |
| 325 | |
wdenk | b2abefb | 2003-06-06 11:20:01 +0000 | [diff] [blame] | 326 | /* Only one release command at a time, please */ |
| 327 | static inline void smc_wait_mmu_release_complete(void) |
| 328 | { |
| 329 | int count = 0; |
| 330 | /* assume bank 2 selected */ |
| 331 | while ( SMC_inw(MMU_CMD_REG) & MC_BUSY ) { |
| 332 | udelay(1); // Wait until not busy |
| 333 | if( ++count > 200) break; |
| 334 | } |
| 335 | } |
| 336 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 337 | /* |
| 338 | . Function: smc_reset( void ) |
| 339 | . Purpose: |
| 340 | . This sets the SMC91111 chip to its normal state, hopefully from whatever |
| 341 | . mess that any other DOS driver has put it in. |
| 342 | . |
| 343 | . Maybe I should reset more registers to defaults in here? SOFTRST should |
| 344 | . do that for me. |
| 345 | . |
| 346 | . Method: |
| 347 | . 1. send a SOFT RESET |
| 348 | . 2. wait for it to finish |
| 349 | . 3. enable autorelease mode |
| 350 | . 4. reset the memory management unit |
| 351 | . 5. clear all interrupts |
| 352 | . |
| 353 | */ |
| 354 | static void smc_reset( void ) |
| 355 | { |
| 356 | PRINTK2("%s:smc_reset\n", SMC_DEV_NAME); |
| 357 | |
| 358 | /* This resets the registers mostly to defaults, but doesn't |
| 359 | affect EEPROM. That seems unnecessary */ |
| 360 | SMC_SELECT_BANK( 0 ); |
| 361 | SMC_outw( RCR_SOFTRST, RCR_REG ); |
| 362 | |
| 363 | /* Setup the Configuration Register */ |
| 364 | /* This is necessary because the CONFIG_REG is not affected */ |
| 365 | /* by a soft reset */ |
| 366 | |
| 367 | SMC_SELECT_BANK( 1 ); |
| 368 | #if defined(CONFIG_SMC91111_EXT_PHY) |
| 369 | SMC_outw( CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG); |
| 370 | #else |
| 371 | SMC_outw( CONFIG_DEFAULT, CONFIG_REG); |
| 372 | #endif |
| 373 | |
| 374 | |
| 375 | /* Release from possible power-down state */ |
| 376 | /* Configuration register is not affected by Soft Reset */ |
| 377 | SMC_outw( SMC_inw( CONFIG_REG ) | CONFIG_EPH_POWER_EN, CONFIG_REG ); |
| 378 | |
| 379 | SMC_SELECT_BANK( 0 ); |
| 380 | |
| 381 | /* this should pause enough for the chip to be happy */ |
| 382 | udelay(10); |
| 383 | |
| 384 | /* Disable transmit and receive functionality */ |
| 385 | SMC_outw( RCR_CLEAR, RCR_REG ); |
| 386 | SMC_outw( TCR_CLEAR, TCR_REG ); |
| 387 | |
| 388 | /* set the control register */ |
| 389 | SMC_SELECT_BANK( 1 ); |
| 390 | SMC_outw( CTL_DEFAULT, CTL_REG ); |
| 391 | |
| 392 | /* Reset the MMU */ |
| 393 | SMC_SELECT_BANK( 2 ); |
wdenk | b2abefb | 2003-06-06 11:20:01 +0000 | [diff] [blame] | 394 | smc_wait_mmu_release_complete(); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 395 | SMC_outw( MC_RESET, MMU_CMD_REG ); |
| 396 | while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY ) |
| 397 | udelay(1); /* Wait until not busy */ |
| 398 | |
| 399 | /* Note: It doesn't seem that waiting for the MMU busy is needed here, |
| 400 | but this is a place where future chipsets _COULD_ break. Be wary |
| 401 | of issuing another MMU command right after this */ |
| 402 | |
| 403 | /* Disable all interrupts */ |
| 404 | SMC_outb( 0, IM_REG ); |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | . Function: smc_enable |
| 409 | . Purpose: let the chip talk to the outside work |
| 410 | . Method: |
| 411 | . 1. Enable the transmitter |
| 412 | . 2. Enable the receiver |
| 413 | . 3. Enable interrupts |
| 414 | */ |
| 415 | static void smc_enable() |
| 416 | { |
| 417 | PRINTK2("%s:smc_enable\n", SMC_DEV_NAME); |
| 418 | SMC_SELECT_BANK( 0 ); |
| 419 | /* see the header file for options in TCR/RCR DEFAULT*/ |
| 420 | SMC_outw( TCR_DEFAULT, TCR_REG ); |
| 421 | SMC_outw( RCR_DEFAULT, RCR_REG ); |
| 422 | |
| 423 | /* clear MII_DIS */ |
| 424 | /* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */ |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | . Function: smc_shutdown |
| 429 | . Purpose: closes down the SMC91xxx chip. |
| 430 | . Method: |
| 431 | . 1. zero the interrupt mask |
| 432 | . 2. clear the enable receive flag |
| 433 | . 3. clear the enable xmit flags |
| 434 | . |
| 435 | . TODO: |
| 436 | . (1) maybe utilize power down mode. |
| 437 | . Why not yet? Because while the chip will go into power down mode, |
| 438 | . the manual says that it will wake up in response to any I/O requests |
| 439 | . in the register space. Empirical results do not show this working. |
| 440 | */ |
| 441 | static void smc_shutdown() |
| 442 | { |
| 443 | PRINTK2(CARDNAME ":smc_shutdown\n"); |
| 444 | |
| 445 | /* no more interrupts for me */ |
| 446 | SMC_SELECT_BANK( 2 ); |
| 447 | SMC_outb( 0, IM_REG ); |
| 448 | |
| 449 | /* and tell the card to stay away from that nasty outside world */ |
| 450 | SMC_SELECT_BANK( 0 ); |
| 451 | SMC_outb( RCR_CLEAR, RCR_REG ); |
| 452 | SMC_outb( TCR_CLEAR, TCR_REG ); |
| 453 | } |
| 454 | |
| 455 | |
| 456 | /* |
| 457 | . Function: smc_hardware_send_packet(struct net_device * ) |
| 458 | . Purpose: |
| 459 | . This sends the actual packet to the SMC9xxx chip. |
| 460 | . |
| 461 | . Algorithm: |
| 462 | . First, see if a saved_skb is available. |
| 463 | . ( this should NOT be called if there is no 'saved_skb' |
| 464 | . Now, find the packet number that the chip allocated |
| 465 | . Point the data pointers at it in memory |
| 466 | . Set the length word in the chip's memory |
| 467 | . Dump the packet to chip memory |
| 468 | . Check if a last byte is needed ( odd length packet ) |
| 469 | . if so, set the control flag right |
| 470 | . Tell the card to send it |
| 471 | . Enable the transmit interrupt, so I know if it failed |
| 472 | . Free the kernel data if I actually sent it. |
| 473 | */ |
| 474 | static int smc_send_packet(volatile void *packet, int packet_length) |
| 475 | { |
| 476 | byte packet_no; |
| 477 | unsigned long ioaddr; |
| 478 | byte * buf; |
| 479 | int length; |
| 480 | int numPages; |
| 481 | int try = 0; |
| 482 | int time_out; |
| 483 | byte status; |
| 484 | |
| 485 | |
| 486 | PRINTK3("%s:smc_hardware_send_packet\n", SMC_DEV_NAME); |
| 487 | |
| 488 | length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN; |
| 489 | |
| 490 | /* allocate memory |
| 491 | ** The MMU wants the number of pages to be the number of 256 bytes |
| 492 | ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) ) |
| 493 | ** |
| 494 | ** The 91C111 ignores the size bits, but the code is left intact |
| 495 | ** for backwards and future compatibility. |
| 496 | ** |
| 497 | ** Pkt size for allocating is data length +6 (for additional status |
| 498 | ** words, length and ctl!) |
| 499 | ** |
| 500 | ** If odd size then last byte is included in this header. |
| 501 | */ |
| 502 | numPages = ((length & 0xfffe) + 6); |
| 503 | numPages >>= 8; /* Divide by 256 */ |
| 504 | |
| 505 | if (numPages > 7 ) { |
| 506 | printf("%s: Far too big packet error. \n", SMC_DEV_NAME); |
| 507 | return 0; |
| 508 | } |
| 509 | |
| 510 | /* now, try to allocate the memory */ |
| 511 | SMC_SELECT_BANK( 2 ); |
| 512 | SMC_outw( MC_ALLOC | numPages, MMU_CMD_REG ); |
| 513 | |
wdenk | c8434db | 2003-03-26 06:55:25 +0000 | [diff] [blame] | 514 | /* FIXME: the ALLOC_INT bit never gets set * |
| 515 | * so the following will always give a * |
| 516 | * memory allocation error. * |
| 517 | * same code works in armboot though * |
| 518 | * -ro |
| 519 | */ |
| 520 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 521 | again: |
| 522 | try++; |
| 523 | time_out = MEMORY_WAIT_TIME; |
| 524 | do { |
| 525 | status = SMC_inb( SMC91111_INT_REG ); |
| 526 | if ( status & IM_ALLOC_INT ) { |
| 527 | /* acknowledge the interrupt */ |
| 528 | SMC_outb( IM_ALLOC_INT, SMC91111_INT_REG ); |
| 529 | break; |
| 530 | } |
| 531 | } while ( -- time_out ); |
| 532 | |
| 533 | if ( !time_out ) { |
| 534 | PRINTK2("%s: memory allocation, try %d failed ...\n", |
| 535 | SMC_DEV_NAME, try); |
| 536 | if (try < SMC_ALLOC_MAX_TRY) |
| 537 | goto again; |
| 538 | else |
| 539 | return 0; |
| 540 | } |
| 541 | |
| 542 | PRINTK2("%s: memory allocation, try %d succeeded ...\n", |
| 543 | SMC_DEV_NAME, |
| 544 | try); |
| 545 | |
| 546 | /* I can send the packet now.. */ |
| 547 | |
| 548 | ioaddr = SMC_BASE_ADDRESS; |
| 549 | |
| 550 | buf = (byte *)packet; |
| 551 | |
| 552 | /* If I get here, I _know_ there is a packet slot waiting for me */ |
| 553 | packet_no = SMC_inb( AR_REG ); |
| 554 | if ( packet_no & AR_FAILED ) { |
| 555 | /* or isn't there? BAD CHIP! */ |
| 556 | printf("%s: Memory allocation failed. \n", |
| 557 | SMC_DEV_NAME); |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | /* we have a packet address, so tell the card to use it */ |
| 562 | SMC_outb( packet_no, PN_REG ); |
| 563 | |
| 564 | /* point to the beginning of the packet */ |
| 565 | SMC_outw( PTR_AUTOINC , PTR_REG ); |
| 566 | |
| 567 | PRINTK3("%s: Trying to xmit packet of length %x\n", |
| 568 | SMC_DEV_NAME, length); |
| 569 | |
| 570 | #if SMC_DEBUG > 2 |
| 571 | printf("Transmitting Packet\n"); |
| 572 | print_packet( buf, length ); |
| 573 | #endif |
| 574 | |
| 575 | /* send the packet length ( +6 for status, length and ctl byte ) |
| 576 | and the status word ( set to zeros ) */ |
| 577 | #ifdef USE_32_BIT |
| 578 | SMC_outl( (length +6 ) << 16 , SMC91111_DATA_REG ); |
| 579 | #else |
| 580 | SMC_outw( 0, SMC91111_DATA_REG ); |
| 581 | /* send the packet length ( +6 for status words, length, and ctl*/ |
| 582 | SMC_outw( (length+6), SMC91111_DATA_REG ); |
| 583 | #endif |
| 584 | |
| 585 | /* send the actual data |
| 586 | . I _think_ it's faster to send the longs first, and then |
| 587 | . mop up by sending the last word. It depends heavily |
| 588 | . on alignment, at least on the 486. Maybe it would be |
| 589 | . a good idea to check which is optimal? But that could take |
| 590 | . almost as much time as is saved? |
| 591 | */ |
| 592 | #ifdef USE_32_BIT |
| 593 | SMC_outsl(SMC91111_DATA_REG, buf, length >> 2 ); |
| 594 | if ( length & 0x2 ) |
| 595 | SMC_outw(*((word *)(buf + (length & 0xFFFFFFFC))), SMC91111_DATA_REG); |
| 596 | #else |
| 597 | SMC_outsw(SMC91111_DATA_REG , buf, (length ) >> 1); |
| 598 | #endif /* USE_32_BIT */ |
| 599 | |
| 600 | /* Send the last byte, if there is one. */ |
| 601 | if ( (length & 1) == 0 ) { |
| 602 | SMC_outw( 0, SMC91111_DATA_REG ); |
| 603 | } else { |
| 604 | SMC_outw( buf[length -1 ] | 0x2000, SMC91111_DATA_REG ); |
| 605 | } |
| 606 | |
| 607 | /* and let the chipset deal with it */ |
| 608 | SMC_outw( MC_ENQUEUE , MMU_CMD_REG ); |
| 609 | |
| 610 | /* poll for TX INT */ |
| 611 | if (poll4int(IM_TX_INT, SMC_TX_TIMEOUT)) { |
| 612 | /* sending failed */ |
| 613 | PRINTK2("%s: TX timeout, sending failed...\n", |
| 614 | SMC_DEV_NAME); |
| 615 | |
| 616 | /* release packet */ |
| 617 | SMC_outw(MC_FREEPKT, MMU_CMD_REG); |
| 618 | |
| 619 | /* wait for MMU getting ready (low) */ |
| 620 | while (SMC_inw(MMU_CMD_REG) & MC_BUSY) |
| 621 | { |
| 622 | udelay(10); |
| 623 | } |
| 624 | |
| 625 | PRINTK2("MMU ready\n"); |
| 626 | |
| 627 | |
| 628 | return 0; |
| 629 | } else { |
| 630 | /* ack. int */ |
| 631 | SMC_outw(IM_TX_INT, SMC91111_INT_REG); |
| 632 | PRINTK2("%s: Sent packet of length %d \n", SMC_DEV_NAME, length); |
| 633 | |
| 634 | /* release packet */ |
| 635 | SMC_outw(MC_FREEPKT, MMU_CMD_REG); |
| 636 | |
| 637 | /* wait for MMU getting ready (low) */ |
| 638 | while (SMC_inw(MMU_CMD_REG) & MC_BUSY) |
| 639 | { |
| 640 | udelay(10); |
| 641 | } |
| 642 | |
| 643 | PRINTK2("MMU ready\n"); |
| 644 | |
| 645 | |
| 646 | } |
| 647 | |
| 648 | return length; |
| 649 | } |
| 650 | |
| 651 | /*------------------------------------------------------------------------- |
| 652 | | |
| 653 | | smc_destructor( struct net_device * dev ) |
| 654 | | Input parameters: |
| 655 | | dev, pointer to the device structure |
| 656 | | |
| 657 | | Output: |
| 658 | | None. |
| 659 | | |
| 660 | --------------------------------------------------------------------------- |
| 661 | */ |
| 662 | void smc_destructor() |
| 663 | { |
| 664 | PRINTK2(CARDNAME ":smc_destructor\n"); |
| 665 | } |
| 666 | |
| 667 | |
| 668 | /* |
| 669 | * Open and Initialize the board |
| 670 | * |
| 671 | * Set up everything, reset the card, etc .. |
| 672 | * |
| 673 | */ |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 674 | static int smc_open(bd_t *bd) |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 675 | { |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 676 | int i, err; |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 677 | |
| 678 | PRINTK2("%s:smc_open\n", SMC_DEV_NAME); |
| 679 | |
| 680 | /* reset the hardware */ |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 681 | smc_reset(); |
| 682 | smc_enable(); |
| 683 | |
| 684 | /* Configure the PHY */ |
| 685 | #ifndef CONFIG_SMC91111_EXT_PHY |
| 686 | smc_phy_configure(); |
| 687 | #endif |
| 688 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 689 | /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */ |
| 690 | /* SMC_SELECT_BANK(0); */ |
| 691 | /* SMC_outw(0, RPC_REG); */ |
wdenk | b2abefb | 2003-06-06 11:20:01 +0000 | [diff] [blame] | 692 | SMC_SELECT_BANK(1); |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 693 | |
| 694 | err = smc_get_ethaddr(bd); /* set smc_mac_addr, and sync it with u-boot globals */ |
| 695 | if(err < 0){ |
| 696 | memset(bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set*/ |
| 697 | return(-1); /* upper code ignores this, but NOT bi_enetaddr */ |
| 698 | } |
wdenk | b2abefb | 2003-06-06 11:20:01 +0000 | [diff] [blame] | 699 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 700 | #ifdef USE_32_BIT |
| 701 | for ( i = 0; i < 6; i += 2 ) { |
| 702 | word address; |
| 703 | |
| 704 | address = smc_mac_addr[ i + 1 ] << 8 ; |
| 705 | address |= smc_mac_addr[ i ]; |
| 706 | SMC_outw( address, ADDR0_REG + i ); |
| 707 | } |
| 708 | #else |
| 709 | for ( i = 0; i < 6; i ++ ) |
| 710 | SMC_outb( smc_mac_addr[i], ADDR0_REG + i ); |
| 711 | #endif |
| 712 | |
| 713 | return 0; |
| 714 | } |
| 715 | |
| 716 | #if 0 /* dead code? -- wd */ |
| 717 | #ifdef USE_32_BIT |
| 718 | void |
| 719 | insl32(r,b,l) |
| 720 | { |
| 721 | int __i ; |
| 722 | dword *__b2; |
| 723 | |
| 724 | __b2 = (dword *) b; |
| 725 | for (__i = 0; __i < l; __i++) { |
| 726 | *(__b2 + __i) = *(dword *)(r+0x10000300); |
| 727 | } |
| 728 | } |
| 729 | #endif |
| 730 | #endif |
| 731 | |
| 732 | /*------------------------------------------------------------- |
| 733 | . |
| 734 | . smc_rcv - receive a packet from the card |
| 735 | . |
| 736 | . There is ( at least ) a packet waiting to be read from |
| 737 | . chip-memory. |
| 738 | . |
| 739 | . o Read the status |
| 740 | . o If an error, record it |
| 741 | . o otherwise, read in the packet |
| 742 | -------------------------------------------------------------- |
| 743 | */ |
| 744 | static int smc_rcv() |
| 745 | { |
| 746 | int packet_number; |
| 747 | word status; |
| 748 | word packet_length; |
| 749 | int is_error = 0; |
| 750 | #ifdef USE_32_BIT |
| 751 | dword stat_len; |
| 752 | #endif |
| 753 | |
| 754 | |
| 755 | SMC_SELECT_BANK(2); |
| 756 | packet_number = SMC_inw( RXFIFO_REG ); |
| 757 | |
| 758 | if ( packet_number & RXFIFO_REMPTY ) { |
| 759 | |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | PRINTK3("%s:smc_rcv\n", SMC_DEV_NAME); |
| 764 | /* start reading from the start of the packet */ |
| 765 | SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG ); |
| 766 | |
| 767 | /* First two words are status and packet_length */ |
| 768 | #ifdef USE_32_BIT |
| 769 | stat_len = SMC_inl(SMC91111_DATA_REG); |
| 770 | status = stat_len & 0xffff; |
| 771 | packet_length = stat_len >> 16; |
| 772 | #else |
| 773 | status = SMC_inw( SMC91111_DATA_REG ); |
| 774 | packet_length = SMC_inw( SMC91111_DATA_REG ); |
| 775 | #endif |
| 776 | |
| 777 | packet_length &= 0x07ff; /* mask off top bits */ |
| 778 | |
| 779 | PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length ); |
| 780 | |
| 781 | if ( !(status & RS_ERRORS ) ){ |
| 782 | /* Adjust for having already read the first two words */ |
| 783 | packet_length -= 4; /*4; */ |
| 784 | |
| 785 | |
| 786 | |
| 787 | /* set odd length for bug in LAN91C111, */ |
| 788 | /* which never sets RS_ODDFRAME */ |
| 789 | /* TODO ? */ |
| 790 | |
| 791 | |
| 792 | #ifdef USE_32_BIT |
| 793 | PRINTK3(" Reading %d dwords (and %d bytes) \n", |
| 794 | packet_length >> 2, packet_length & 3 ); |
| 795 | /* QUESTION: Like in the TX routine, do I want |
| 796 | to send the DWORDs or the bytes first, or some |
| 797 | mixture. A mixture might improve already slow PIO |
| 798 | performance */ |
| 799 | SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 ); |
| 800 | /* read the left over bytes */ |
| 801 | if (packet_length & 3) { |
| 802 | int i; |
| 803 | |
wdenk | 699b13a | 2002-11-03 18:03:52 +0000 | [diff] [blame] | 804 | byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 805 | dword leftover = SMC_inl(SMC91111_DATA_REG); |
| 806 | for (i=0; i<(packet_length & 3); i++) |
| 807 | *tail++ = (byte) (leftover >> (8*i)) & 0xff; |
| 808 | } |
| 809 | #else |
| 810 | PRINTK3(" Reading %d words and %d byte(s) \n", |
| 811 | (packet_length >> 1 ), packet_length & 1 ); |
| 812 | SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1); |
| 813 | |
| 814 | #endif /* USE_32_BIT */ |
| 815 | |
| 816 | #if SMC_DEBUG > 2 |
| 817 | printf("Receiving Packet\n"); |
| 818 | print_packet( NetRxPackets[0], packet_length ); |
| 819 | #endif |
| 820 | } else { |
| 821 | /* error ... */ |
| 822 | /* TODO ? */ |
| 823 | is_error = 1; |
| 824 | } |
| 825 | |
| 826 | while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY ) |
| 827 | udelay(1); /* Wait until not busy */ |
| 828 | |
| 829 | /* error or good, tell the card to get rid of this packet */ |
| 830 | SMC_outw( MC_RELEASE, MMU_CMD_REG ); |
| 831 | |
| 832 | while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY ) |
| 833 | udelay(1); /* Wait until not busy */ |
| 834 | |
| 835 | if (!is_error) { |
| 836 | /* Pass the packet up to the protocol layers. */ |
| 837 | NetReceive(NetRxPackets[0], packet_length); |
| 838 | return packet_length; |
| 839 | } else { |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | } |
| 844 | |
| 845 | |
| 846 | |
| 847 | /*---------------------------------------------------- |
| 848 | . smc_close |
| 849 | . |
| 850 | . this makes the board clean up everything that it can |
| 851 | . and not talk to the outside world. Caused by |
| 852 | . an 'ifconfig ethX down' |
| 853 | . |
| 854 | -----------------------------------------------------*/ |
| 855 | static int smc_close() |
| 856 | { |
| 857 | PRINTK2("%s:smc_close\n", SMC_DEV_NAME); |
| 858 | |
| 859 | /* clear everything */ |
| 860 | smc_shutdown(); |
| 861 | |
| 862 | return 0; |
| 863 | } |
| 864 | |
| 865 | |
| 866 | #if 0 |
| 867 | /*------------------------------------------------------------ |
| 868 | . Modify a bit in the LAN91C111 register set |
| 869 | .-------------------------------------------------------------*/ |
| 870 | static word smc_modify_regbit(int bank, int ioaddr, int reg, |
| 871 | unsigned int bit, int val) |
| 872 | { |
| 873 | word regval; |
| 874 | |
| 875 | SMC_SELECT_BANK( bank ); |
| 876 | |
| 877 | regval = SMC_inw( reg ); |
| 878 | if (val) |
| 879 | regval |= bit; |
| 880 | else |
| 881 | regval &= ~bit; |
| 882 | |
| 883 | SMC_outw( regval, 0 ); |
| 884 | return(regval); |
| 885 | } |
| 886 | |
| 887 | |
| 888 | /*------------------------------------------------------------ |
| 889 | . Retrieve a bit in the LAN91C111 register set |
| 890 | .-------------------------------------------------------------*/ |
| 891 | static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit) |
| 892 | { |
| 893 | SMC_SELECT_BANK( bank ); |
| 894 | if ( SMC_inw( reg ) & bit) |
| 895 | return(1); |
| 896 | else |
| 897 | return(0); |
| 898 | } |
| 899 | |
| 900 | |
| 901 | /*------------------------------------------------------------ |
| 902 | . Modify a LAN91C111 register (word access only) |
| 903 | .-------------------------------------------------------------*/ |
| 904 | static void smc_modify_reg(int bank, int ioaddr, int reg, word val) |
| 905 | { |
| 906 | SMC_SELECT_BANK( bank ); |
| 907 | SMC_outw( val, reg ); |
| 908 | } |
| 909 | |
| 910 | |
| 911 | /*------------------------------------------------------------ |
| 912 | . Retrieve a LAN91C111 register (word access only) |
| 913 | .-------------------------------------------------------------*/ |
| 914 | static int smc_get_reg(int bank, int ioaddr, int reg) |
| 915 | { |
| 916 | SMC_SELECT_BANK( bank ); |
| 917 | return(SMC_inw( reg )); |
| 918 | } |
| 919 | |
| 920 | #endif /* 0 */ |
| 921 | |
| 922 | /*---PHY CONTROL AND CONFIGURATION----------------------------------------- */ |
| 923 | |
| 924 | #if (SMC_DEBUG > 2 ) |
| 925 | |
| 926 | /*------------------------------------------------------------ |
| 927 | . Debugging function for viewing MII Management serial bitstream |
| 928 | .-------------------------------------------------------------*/ |
| 929 | static void smc_dump_mii_stream(byte* bits, int size) |
| 930 | { |
| 931 | int i; |
| 932 | |
| 933 | printf("BIT#:"); |
| 934 | for (i = 0; i < size; ++i) |
| 935 | { |
| 936 | printf("%d", i%10); |
| 937 | } |
| 938 | |
| 939 | printf("\nMDOE:"); |
| 940 | for (i = 0; i < size; ++i) |
| 941 | { |
| 942 | if (bits[i] & MII_MDOE) |
| 943 | printf("1"); |
| 944 | else |
| 945 | printf("0"); |
| 946 | } |
| 947 | |
| 948 | printf("\nMDO :"); |
| 949 | for (i = 0; i < size; ++i) |
| 950 | { |
| 951 | if (bits[i] & MII_MDO) |
| 952 | printf("1"); |
| 953 | else |
| 954 | printf("0"); |
| 955 | } |
| 956 | |
| 957 | printf("\nMDI :"); |
| 958 | for (i = 0; i < size; ++i) |
| 959 | { |
| 960 | if (bits[i] & MII_MDI) |
| 961 | printf("1"); |
| 962 | else |
| 963 | printf("0"); |
| 964 | } |
| 965 | |
| 966 | printf("\n"); |
| 967 | } |
| 968 | #endif |
| 969 | |
| 970 | /*------------------------------------------------------------ |
| 971 | . Reads a register from the MII Management serial interface |
| 972 | .-------------------------------------------------------------*/ |
| 973 | #ifndef CONFIG_SMC91111_EXT_PHY |
| 974 | static word smc_read_phy_register(byte phyreg) |
| 975 | { |
| 976 | int oldBank; |
| 977 | int i; |
| 978 | byte mask; |
| 979 | word mii_reg; |
| 980 | byte bits[64]; |
| 981 | int clk_idx = 0; |
| 982 | int input_idx; |
| 983 | word phydata; |
| 984 | byte phyaddr = SMC_PHY_ADDR; |
| 985 | |
| 986 | /* 32 consecutive ones on MDO to establish sync */ |
| 987 | for (i = 0; i < 32; ++i) |
| 988 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 989 | |
| 990 | /* Start code <01> */ |
| 991 | bits[clk_idx++] = MII_MDOE; |
| 992 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 993 | |
| 994 | /* Read command <10> */ |
| 995 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 996 | bits[clk_idx++] = MII_MDOE; |
| 997 | |
| 998 | /* Output the PHY address, msb first */ |
| 999 | mask = (byte)0x10; |
| 1000 | for (i = 0; i < 5; ++i) |
| 1001 | { |
| 1002 | if (phyaddr & mask) |
| 1003 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1004 | else |
| 1005 | bits[clk_idx++] = MII_MDOE; |
| 1006 | |
| 1007 | /* Shift to next lowest bit */ |
| 1008 | mask >>= 1; |
| 1009 | } |
| 1010 | |
| 1011 | /* Output the phy register number, msb first */ |
| 1012 | mask = (byte)0x10; |
| 1013 | for (i = 0; i < 5; ++i) |
| 1014 | { |
| 1015 | if (phyreg & mask) |
| 1016 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1017 | else |
| 1018 | bits[clk_idx++] = MII_MDOE; |
| 1019 | |
| 1020 | /* Shift to next lowest bit */ |
| 1021 | mask >>= 1; |
| 1022 | } |
| 1023 | |
| 1024 | /* Tristate and turnaround (2 bit times) */ |
| 1025 | bits[clk_idx++] = 0; |
| 1026 | /*bits[clk_idx++] = 0; */ |
| 1027 | |
| 1028 | /* Input starts at this bit time */ |
| 1029 | input_idx = clk_idx; |
| 1030 | |
| 1031 | /* Will input 16 bits */ |
| 1032 | for (i = 0; i < 16; ++i) |
| 1033 | bits[clk_idx++] = 0; |
| 1034 | |
| 1035 | /* Final clock bit */ |
| 1036 | bits[clk_idx++] = 0; |
| 1037 | |
| 1038 | /* Save the current bank */ |
| 1039 | oldBank = SMC_inw( BANK_SELECT ); |
| 1040 | |
| 1041 | /* Select bank 3 */ |
| 1042 | SMC_SELECT_BANK( 3 ); |
| 1043 | |
| 1044 | /* Get the current MII register value */ |
| 1045 | mii_reg = SMC_inw( MII_REG ); |
| 1046 | |
| 1047 | /* Turn off all MII Interface bits */ |
| 1048 | mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO); |
| 1049 | |
| 1050 | /* Clock all 64 cycles */ |
| 1051 | for (i = 0; i < sizeof bits; ++i) |
| 1052 | { |
| 1053 | /* Clock Low - output data */ |
| 1054 | SMC_outw( mii_reg | bits[i], MII_REG ); |
| 1055 | udelay(SMC_PHY_CLOCK_DELAY); |
| 1056 | |
| 1057 | |
| 1058 | /* Clock Hi - input data */ |
| 1059 | SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG ); |
| 1060 | udelay(SMC_PHY_CLOCK_DELAY); |
| 1061 | bits[i] |= SMC_inw( MII_REG ) & MII_MDI; |
| 1062 | } |
| 1063 | |
| 1064 | /* Return to idle state */ |
| 1065 | /* Set clock to low, data to low, and output tristated */ |
| 1066 | SMC_outw( mii_reg, MII_REG ); |
| 1067 | udelay(SMC_PHY_CLOCK_DELAY); |
| 1068 | |
| 1069 | /* Restore original bank select */ |
| 1070 | SMC_SELECT_BANK( oldBank ); |
| 1071 | |
| 1072 | /* Recover input data */ |
| 1073 | phydata = 0; |
| 1074 | for (i = 0; i < 16; ++i) |
| 1075 | { |
| 1076 | phydata <<= 1; |
| 1077 | |
| 1078 | if (bits[input_idx++] & MII_MDI) |
| 1079 | phydata |= 0x0001; |
| 1080 | } |
| 1081 | |
| 1082 | #if (SMC_DEBUG > 2 ) |
| 1083 | printf("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n", |
| 1084 | phyaddr, phyreg, phydata); |
| 1085 | smc_dump_mii_stream(bits, sizeof bits); |
| 1086 | #endif |
| 1087 | |
| 1088 | return(phydata); |
| 1089 | } |
| 1090 | |
| 1091 | |
| 1092 | /*------------------------------------------------------------ |
| 1093 | . Writes a register to the MII Management serial interface |
| 1094 | .-------------------------------------------------------------*/ |
| 1095 | static void smc_write_phy_register(byte phyreg, word phydata) |
| 1096 | { |
| 1097 | int oldBank; |
| 1098 | int i; |
| 1099 | word mask; |
| 1100 | word mii_reg; |
| 1101 | byte bits[65]; |
| 1102 | int clk_idx = 0; |
| 1103 | byte phyaddr = SMC_PHY_ADDR; |
| 1104 | |
| 1105 | /* 32 consecutive ones on MDO to establish sync */ |
| 1106 | for (i = 0; i < 32; ++i) |
| 1107 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1108 | |
| 1109 | /* Start code <01> */ |
| 1110 | bits[clk_idx++] = MII_MDOE; |
| 1111 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1112 | |
| 1113 | /* Write command <01> */ |
| 1114 | bits[clk_idx++] = MII_MDOE; |
| 1115 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1116 | |
| 1117 | /* Output the PHY address, msb first */ |
| 1118 | mask = (byte)0x10; |
| 1119 | for (i = 0; i < 5; ++i) |
| 1120 | { |
| 1121 | if (phyaddr & mask) |
| 1122 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1123 | else |
| 1124 | bits[clk_idx++] = MII_MDOE; |
| 1125 | |
| 1126 | /* Shift to next lowest bit */ |
| 1127 | mask >>= 1; |
| 1128 | } |
| 1129 | |
| 1130 | /* Output the phy register number, msb first */ |
| 1131 | mask = (byte)0x10; |
| 1132 | for (i = 0; i < 5; ++i) |
| 1133 | { |
| 1134 | if (phyreg & mask) |
| 1135 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1136 | else |
| 1137 | bits[clk_idx++] = MII_MDOE; |
| 1138 | |
| 1139 | /* Shift to next lowest bit */ |
| 1140 | mask >>= 1; |
| 1141 | } |
| 1142 | |
| 1143 | /* Tristate and turnaround (2 bit times) */ |
| 1144 | bits[clk_idx++] = 0; |
| 1145 | bits[clk_idx++] = 0; |
| 1146 | |
| 1147 | /* Write out 16 bits of data, msb first */ |
| 1148 | mask = 0x8000; |
| 1149 | for (i = 0; i < 16; ++i) |
| 1150 | { |
| 1151 | if (phydata & mask) |
| 1152 | bits[clk_idx++] = MII_MDOE | MII_MDO; |
| 1153 | else |
| 1154 | bits[clk_idx++] = MII_MDOE; |
| 1155 | |
| 1156 | /* Shift to next lowest bit */ |
| 1157 | mask >>= 1; |
| 1158 | } |
| 1159 | |
| 1160 | /* Final clock bit (tristate) */ |
| 1161 | bits[clk_idx++] = 0; |
| 1162 | |
| 1163 | /* Save the current bank */ |
| 1164 | oldBank = SMC_inw( BANK_SELECT ); |
| 1165 | |
| 1166 | /* Select bank 3 */ |
| 1167 | SMC_SELECT_BANK( 3 ); |
| 1168 | |
| 1169 | /* Get the current MII register value */ |
| 1170 | mii_reg = SMC_inw( MII_REG ); |
| 1171 | |
| 1172 | /* Turn off all MII Interface bits */ |
| 1173 | mii_reg &= ~(MII_MDOE|MII_MCLK|MII_MDI|MII_MDO); |
| 1174 | |
| 1175 | /* Clock all cycles */ |
| 1176 | for (i = 0; i < sizeof bits; ++i) |
| 1177 | { |
| 1178 | /* Clock Low - output data */ |
| 1179 | SMC_outw( mii_reg | bits[i], MII_REG ); |
| 1180 | udelay(SMC_PHY_CLOCK_DELAY); |
| 1181 | |
| 1182 | |
| 1183 | /* Clock Hi - input data */ |
| 1184 | SMC_outw( mii_reg | bits[i] | MII_MCLK, MII_REG ); |
| 1185 | udelay(SMC_PHY_CLOCK_DELAY); |
| 1186 | bits[i] |= SMC_inw( MII_REG ) & MII_MDI; |
| 1187 | } |
| 1188 | |
| 1189 | /* Return to idle state */ |
| 1190 | /* Set clock to low, data to low, and output tristated */ |
| 1191 | SMC_outw( mii_reg, MII_REG ); |
| 1192 | udelay(SMC_PHY_CLOCK_DELAY); |
| 1193 | |
| 1194 | /* Restore original bank select */ |
| 1195 | SMC_SELECT_BANK( oldBank ); |
| 1196 | |
| 1197 | #if (SMC_DEBUG > 2 ) |
| 1198 | printf("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n", |
| 1199 | phyaddr, phyreg, phydata); |
| 1200 | smc_dump_mii_stream(bits, sizeof bits); |
| 1201 | #endif |
| 1202 | } |
| 1203 | #endif /* !CONFIG_SMC91111_EXT_PHY */ |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | /*------------------------------------------------------------ |
| 1208 | . Waits the specified number of milliseconds - kernel friendly |
| 1209 | .-------------------------------------------------------------*/ |
| 1210 | #ifndef CONFIG_SMC91111_EXT_PHY |
| 1211 | static void smc_wait_ms(unsigned int ms) |
| 1212 | { |
| 1213 | udelay(ms*1000); |
| 1214 | } |
| 1215 | #endif /* !CONFIG_SMC91111_EXT_PHY */ |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | /*------------------------------------------------------------ |
| 1220 | . Configures the specified PHY using Autonegotiation. Calls |
| 1221 | . smc_phy_fixed() if the user has requested a certain config. |
| 1222 | .-------------------------------------------------------------*/ |
| 1223 | #ifndef CONFIG_SMC91111_EXT_PHY |
| 1224 | static void smc_phy_configure() |
| 1225 | { |
| 1226 | int timeout; |
| 1227 | byte phyaddr; |
| 1228 | word my_phy_caps; /* My PHY capabilities */ |
| 1229 | word my_ad_caps; /* My Advertised capabilities */ |
| 1230 | word status = 0; /*;my status = 0 */ |
| 1231 | int failed = 0; |
| 1232 | |
| 1233 | PRINTK3("%s:smc_program_phy()\n", SMC_DEV_NAME); |
| 1234 | |
| 1235 | |
| 1236 | |
| 1237 | /* Get the detected phy address */ |
| 1238 | phyaddr = SMC_PHY_ADDR; |
| 1239 | |
| 1240 | /* Reset the PHY, setting all other bits to zero */ |
| 1241 | smc_write_phy_register(PHY_CNTL_REG, PHY_CNTL_RST); |
| 1242 | |
| 1243 | /* Wait for the reset to complete, or time out */ |
| 1244 | timeout = 6; /* Wait up to 3 seconds */ |
| 1245 | while (timeout--) |
| 1246 | { |
| 1247 | if (!(smc_read_phy_register(PHY_CNTL_REG) |
| 1248 | & PHY_CNTL_RST)) |
| 1249 | { |
| 1250 | /* reset complete */ |
| 1251 | break; |
| 1252 | } |
| 1253 | |
| 1254 | smc_wait_ms(500); /* wait 500 millisecs */ |
| 1255 | } |
| 1256 | |
| 1257 | if (timeout < 1) |
| 1258 | { |
| 1259 | printf("%s:PHY reset timed out\n", SMC_DEV_NAME); |
| 1260 | goto smc_phy_configure_exit; |
| 1261 | } |
| 1262 | |
| 1263 | /* Read PHY Register 18, Status Output */ |
| 1264 | /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */ |
| 1265 | |
| 1266 | /* Enable PHY Interrupts (for register 18) */ |
| 1267 | /* Interrupts listed here are disabled */ |
| 1268 | smc_write_phy_register(PHY_INT_REG, 0xffff); |
| 1269 | |
| 1270 | /* Configure the Receive/Phy Control register */ |
| 1271 | SMC_SELECT_BANK( 0 ); |
| 1272 | SMC_outw( RPC_DEFAULT, RPC_REG ); |
| 1273 | |
| 1274 | /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */ |
| 1275 | my_phy_caps = smc_read_phy_register(PHY_STAT_REG); |
| 1276 | my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */ |
| 1277 | |
| 1278 | if (my_phy_caps & PHY_STAT_CAP_T4) |
| 1279 | my_ad_caps |= PHY_AD_T4; |
| 1280 | |
| 1281 | if (my_phy_caps & PHY_STAT_CAP_TXF) |
| 1282 | my_ad_caps |= PHY_AD_TX_FDX; |
| 1283 | |
| 1284 | if (my_phy_caps & PHY_STAT_CAP_TXH) |
| 1285 | my_ad_caps |= PHY_AD_TX_HDX; |
| 1286 | |
| 1287 | if (my_phy_caps & PHY_STAT_CAP_TF) |
| 1288 | my_ad_caps |= PHY_AD_10_FDX; |
| 1289 | |
| 1290 | if (my_phy_caps & PHY_STAT_CAP_TH) |
| 1291 | my_ad_caps |= PHY_AD_10_HDX; |
| 1292 | |
| 1293 | /* Update our Auto-Neg Advertisement Register */ |
| 1294 | smc_write_phy_register( PHY_AD_REG, my_ad_caps); |
| 1295 | |
| 1296 | PRINTK2("%s:phy caps=%x\n", SMC_DEV_NAME, my_phy_caps); |
| 1297 | PRINTK2("%s:phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps); |
| 1298 | |
| 1299 | /* Restart auto-negotiation process in order to advertise my caps */ |
| 1300 | smc_write_phy_register( PHY_CNTL_REG, |
| 1301 | PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST ); |
| 1302 | |
| 1303 | /* Wait for the auto-negotiation to complete. This may take from */ |
| 1304 | /* 2 to 3 seconds. */ |
| 1305 | /* Wait for the reset to complete, or time out */ |
| 1306 | timeout = 20; /* Wait up to 10 seconds */ |
| 1307 | while (timeout--) |
| 1308 | { |
| 1309 | status = smc_read_phy_register( PHY_STAT_REG); |
| 1310 | if (status & PHY_STAT_ANEG_ACK) |
| 1311 | { |
| 1312 | /* auto-negotiate complete */ |
| 1313 | break; |
| 1314 | } |
| 1315 | |
| 1316 | smc_wait_ms(500); /* wait 500 millisecs */ |
| 1317 | |
| 1318 | /* Restart auto-negotiation if remote fault */ |
| 1319 | if (status & PHY_STAT_REM_FLT) |
| 1320 | { |
| 1321 | printf("%s:PHY remote fault detected\n", SMC_DEV_NAME); |
| 1322 | |
| 1323 | /* Restart auto-negotiation */ |
| 1324 | printf("%s:PHY restarting auto-negotiation\n", |
| 1325 | SMC_DEV_NAME); |
| 1326 | smc_write_phy_register( PHY_CNTL_REG, |
| 1327 | PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST | |
| 1328 | PHY_CNTL_SPEED | PHY_CNTL_DPLX); |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | if (timeout < 1) |
| 1333 | { |
| 1334 | printf("%s:PHY auto-negotiate timed out\n", |
| 1335 | SMC_DEV_NAME); |
| 1336 | printf("%s:PHY auto-negotiate timed out\n", SMC_DEV_NAME); |
| 1337 | failed = 1; |
| 1338 | } |
| 1339 | |
| 1340 | /* Fail if we detected an auto-negotiate remote fault */ |
| 1341 | if (status & PHY_STAT_REM_FLT) |
| 1342 | { |
| 1343 | printf( "%s:PHY remote fault detected\n", SMC_DEV_NAME); |
| 1344 | printf("%s:PHY remote fault detected\n", SMC_DEV_NAME); |
| 1345 | failed = 1; |
| 1346 | } |
| 1347 | |
| 1348 | /* Re-Configure the Receive/Phy Control register */ |
| 1349 | SMC_outw( RPC_DEFAULT, RPC_REG ); |
| 1350 | |
| 1351 | smc_phy_configure_exit: |
| 1352 | |
| 1353 | } |
| 1354 | #endif /* !CONFIG_SMC91111_EXT_PHY */ |
| 1355 | |
| 1356 | |
| 1357 | #if SMC_DEBUG > 2 |
| 1358 | static void print_packet( byte * buf, int length ) |
| 1359 | { |
| 1360 | #if 0 |
| 1361 | int i; |
| 1362 | int remainder; |
| 1363 | int lines; |
| 1364 | |
| 1365 | printf("Packet of length %d \n", length ); |
| 1366 | |
| 1367 | #if SMC_DEBUG > 3 |
| 1368 | lines = length / 16; |
| 1369 | remainder = length % 16; |
| 1370 | |
| 1371 | for ( i = 0; i < lines ; i ++ ) { |
| 1372 | int cur; |
| 1373 | |
| 1374 | for ( cur = 0; cur < 8; cur ++ ) { |
| 1375 | byte a, b; |
| 1376 | |
| 1377 | a = *(buf ++ ); |
| 1378 | b = *(buf ++ ); |
| 1379 | printf("%02x%02x ", a, b ); |
| 1380 | } |
| 1381 | printf("\n"); |
| 1382 | } |
| 1383 | for ( i = 0; i < remainder/2 ; i++ ) { |
| 1384 | byte a, b; |
| 1385 | |
| 1386 | a = *(buf ++ ); |
| 1387 | b = *(buf ++ ); |
| 1388 | printf("%02x%02x ", a, b ); |
| 1389 | } |
| 1390 | printf("\n"); |
| 1391 | #endif |
| 1392 | #endif |
| 1393 | } |
| 1394 | #endif |
| 1395 | |
| 1396 | int eth_init(bd_t *bd) { |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 1397 | return (smc_open(bd)); |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | void eth_halt() { |
| 1401 | smc_close(); |
| 1402 | } |
| 1403 | |
| 1404 | int eth_rx() { |
| 1405 | return smc_rcv(); |
| 1406 | } |
| 1407 | |
| 1408 | int eth_send(volatile void *packet, int length) { |
| 1409 | return smc_send_packet(packet, length); |
| 1410 | } |
| 1411 | |
wdenk | 3e8b7dc | 2003-06-19 23:58:30 +0000 | [diff] [blame^] | 1412 | int smc_get_ethaddr(bd_t *bd) |
| 1413 | { |
| 1414 | int env_size, rom_valid, env_present = 0, reg; |
| 1415 | char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66"; |
| 1416 | uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6]; |
| 1417 | |
| 1418 | env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac)); |
| 1419 | if ((env_size > 0) && (env_size < sizeof(es))) { /* exit if env is bad */ |
| 1420 | printf("\n*** ERROR: ethaddr is not set properly!!\n"); |
| 1421 | return(-1); |
| 1422 | } |
| 1423 | |
| 1424 | if(env_size > 0){ |
| 1425 | env_present = 1; |
| 1426 | s = s_env_mac; |
| 1427 | } |
| 1428 | |
| 1429 | for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */ |
| 1430 | v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0; |
| 1431 | if (s) |
| 1432 | s = (*e) ? e + 1 : e; |
| 1433 | } |
| 1434 | |
| 1435 | rom_valid = get_rom_mac(v_rom_mac); /* get ROM mac value if any */ |
| 1436 | |
| 1437 | if(!env_present){ /* if NO env */ |
| 1438 | if(rom_valid){ /* but ROM is valid */ |
| 1439 | v_mac = v_rom_mac; |
| 1440 | sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X", v_mac[0], |
| 1441 | v_mac[1] ,v_mac[2], v_mac[3],v_mac[4], v_mac[5]) ; |
| 1442 | setenv ("ethaddr", s_env_mac); |
| 1443 | }else{ /* no env, bad ROM */ |
| 1444 | printf("\n*** ERROR: ethaddr is NOT set !!\n"); |
| 1445 | return(-1); |
| 1446 | } |
| 1447 | }else /* good env, don't care ROM */ |
| 1448 | v_mac = v_env_mac; /* always use a good env over a ROM */ |
| 1449 | |
| 1450 | if(env_present && rom_valid) /* if both env and ROM are good */ |
| 1451 | if(memcmp(v_env_mac, v_rom_mac, 6) != 0){ |
| 1452 | printf("\n*** Warning: Environment and ROM MAC addresses don't match\n"); |
| 1453 | printf("*** Using Environment MAC\n"); |
| 1454 | } |
| 1455 | memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */ |
| 1456 | smc_set_mac_addr(v_mac); /* use old function to update smc default */ |
| 1457 | return(0); |
| 1458 | } |
| 1459 | |
| 1460 | int get_rom_mac(char *v_rom_mac) |
| 1461 | { |
| 1462 | int is_rom_present = 0; |
| 1463 | #ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */ |
| 1464 | char hw_mac_addr[] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8}; |
| 1465 | |
| 1466 | memcpy (v_rom_mac, hw_mac_addr, 6); |
| 1467 | return(1); |
| 1468 | #else |
| 1469 | if(is_rom_present) |
| 1470 | { |
| 1471 | /* if eeprom contents are valid |
| 1472 | * extract mac address into hw_mac_addr, 8 or 16 bit accesses |
| 1473 | * memcpy (v_rom_mac, hc_mac_addr, 6); |
| 1474 | * return(1); |
| 1475 | */ |
| 1476 | } |
| 1477 | memset(v_rom_mac, 0, 6); |
| 1478 | return(0); |
| 1479 | #endif |
| 1480 | } |
| 1481 | |
wdenk | fe8c280 | 2002-11-03 00:38:21 +0000 | [diff] [blame] | 1482 | #endif /* CONFIG_DRIVER_SMC91111 */ |