blob: b41e4d2866b7f59f4249b9b232f969eb2fa0592c [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*------------------------------------------------------------------------
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)
wdenk42dfe7a2004-03-14 22:25:36 +000010 . Developed by Simple Network Magic Corporation (SNMC)
wdenkfe8c2802002-11-03 00:38:21 +000011 . 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
wdenk42dfe7a2004-03-14 22:25:36 +000020 . MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
wdenkfe8c2802002-11-03 00:38:21 +000021 . 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
wdenk42dfe7a2004-03-14 22:25:36 +000025 . Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
wdenkfe8c2802002-11-03 00:38:21 +000026 .
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:
wdenk42dfe7a2004-03-14 22:25:36 +000039 . io = for the base address
wdenkfe8c2802002-11-03 00:38:21 +000040 . irq = for the IRQ
41 .
42 . author:
wdenk42dfe7a2004-03-14 22:25:36 +000043 . Erik Stahlman ( erik@vt.edu )
44 . Daris A Nevil ( dnevil@snmc.com )
wdenkfe8c2802002-11-03 00:38:21 +000045 .
46 .
47 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
48 .
49 . Sources:
wdenk42dfe7a2004-03-14 22:25:36 +000050 . 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 )
wdenkfe8c2802002-11-03 00:38:21 +000053 .
54 . History:
wdenk42dfe7a2004-03-14 22:25:36 +000055 . 06/19/03 Richard Woodruff Made u-boot environment aware and added mac addr checks.
wdenkfe8c2802002-11-03 00:38:21 +000056 . 10/17/01 Marco Hasewinkel Modify for DNP/1110
wdenk42dfe7a2004-03-14 22:25:36 +000057 . 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
wdenkfe8c2802002-11-03 00:38:21 +000060 ----------------------------------------------------------------------------*/
61
62#include <common.h>
63#include <command.h>
wdenkf39748a2004-06-09 13:37:52 +000064#include <config.h>
wdenkfe8c2802002-11-03 00:38:21 +000065#include "smc91111.h"
66#include <net.h>
67
wdenkfe8c2802002-11-03 00:38:21 +000068/* Use power-down feature of the chip */
69#define POWER_DOWN 0
70
71#define NO_AUTOPROBE
72
Wolfgang Denk0be248f2006-03-07 00:22:36 +010073#define SMC_DEBUG 0
wdenk8bf3b002003-12-06 23:20:41 +000074
75#if SMC_DEBUG > 1
wdenkfe8c2802002-11-03 00:38:21 +000076static const char version[] =
77 "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
wdenk8bf3b002003-12-06 23:20:41 +000078#endif
wdenkfe8c2802002-11-03 00:38:21 +000079
wdenkf39748a2004-06-09 13:37:52 +000080/* Autonegotiation timeout in seconds */
81#ifndef CONFIG_SMC_AUTONEG_TIMEOUT
82#define CONFIG_SMC_AUTONEG_TIMEOUT 10
83#endif
84
wdenkfe8c2802002-11-03 00:38:21 +000085/*------------------------------------------------------------------------
86 .
87 . Configuration options, for the experienced user to change.
88 .
89 -------------------------------------------------------------------------*/
90
91/*
92 . Wait time for memory to be free. This probably shouldn't be
93 . tuned that much, as waiting for this means nothing else happens
94 . in the system
95*/
96#define MEMORY_WAIT_TIME 16
97
98
99#if (SMC_DEBUG > 2 )
100#define PRINTK3(args...) printf(args)
101#else
102#define PRINTK3(args...)
103#endif
104
105#if SMC_DEBUG > 1
106#define PRINTK2(args...) printf(args)
107#else
108#define PRINTK2(args...)
109#endif
110
111#ifdef SMC_DEBUG
112#define PRINTK(args...) printf(args)
113#else
114#define PRINTK(args...)
115#endif
116
117
118/*------------------------------------------------------------------------
119 .
wdenk42dfe7a2004-03-14 22:25:36 +0000120 . The internal workings of the driver. If you are changing anything
wdenkfe8c2802002-11-03 00:38:21 +0000121 . here with the SMC stuff, you should have the datasheet and know
122 . what you are doing.
123 .
124 -------------------------------------------------------------------------*/
125#define CARDNAME "LAN91C111"
126
127/* Memory sizing constant */
128#define LAN91C111_MEMORY_MULTIPLIER (1024*2)
129
130#ifndef CONFIG_SMC91111_BASE
131#define CONFIG_SMC91111_BASE 0x20000300
132#endif
133
134#define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
135
136#define SMC_DEV_NAME "SMC91111"
137#define SMC_PHY_ADDR 0x0000
138#define SMC_ALLOC_MAX_TRY 5
139#define SMC_TX_TIMEOUT 30
140
141#define SMC_PHY_CLOCK_DELAY 1000
142
143#define ETH_ZLEN 60
144
wdenk42dfe7a2004-03-14 22:25:36 +0000145#ifdef CONFIG_SMC_USE_32_BIT
wdenkfe8c2802002-11-03 00:38:21 +0000146#define USE_32_BIT 1
147#else
148#undef USE_32_BIT
149#endif
150/*-----------------------------------------------------------------
151 .
152 . The driver can be entered at any of the following entry points.
153 .
154 .------------------------------------------------------------------ */
155
156extern int eth_init(bd_t *bd);
157extern void eth_halt(void);
158extern int eth_rx(void);
159extern int eth_send(volatile void *packet, int length);
160
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100161#ifdef SHARED_RESOURCES
162 extern void swap_to(int device_id);
163#endif
wdenkfe8c2802002-11-03 00:38:21 +0000164
wdenkfe8c2802002-11-03 00:38:21 +0000165/*
166 . This is called by register_netdev(). It is responsible for
167 . checking the portlist for the SMC9000 series chipset. If it finds
168 . one, then it will initialize the device, find the hardware information,
169 . and sets up the appropriate device parameters.
170 . NOTE: Interrupts are *OFF* when this procedure is called.
171 .
172 . NB:This shouldn't be static since it is referred to externally.
173*/
174int smc_init(void);
175
176/*
177 . This is called by unregister_netdev(). It is responsible for
178 . cleaning up before the driver is finally unregistered and discarded.
179*/
180void smc_destructor(void);
181
182/*
183 . The kernel calls this function when someone wants to use the device,
184 . typically 'ifconfig ethX up'.
185*/
wdenk0b97ab12003-06-19 23:58:30 +0000186static int smc_open(bd_t *bd);
wdenkfe8c2802002-11-03 00:38:21 +0000187
188
189/*
190 . This is called by the kernel in response to 'ifconfig ethX down'. It
191 . is responsible for cleaning up everything that the open routine
192 . does, and maybe putting the card into a powerdown state.
193*/
194static int smc_close(void);
195
196/*
197 . Configures the PHY through the MII Management interface
198*/
199#ifndef CONFIG_SMC91111_EXT_PHY
200static void smc_phy_configure(void);
201#endif /* !CONFIG_SMC91111_EXT_PHY */
202
203/*
204 . This is a separate procedure to handle the receipt of a packet, to
205 . leave the interrupt code looking slightly cleaner
206*/
207static int smc_rcv(void);
208
wdenk0b97ab12003-06-19 23:58:30 +0000209/* See if a MAC address is defined in the current environment. If so use it. If not
wdenk8bde7f72003-06-27 21:31:46 +0000210 . print a warning and set the environment and other globals with the default.
wdenk0b97ab12003-06-19 23:58:30 +0000211 . If an EEPROM is present it really should be consulted.
212*/
213int smc_get_ethaddr(bd_t *bd);
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100214int get_rom_mac(uchar *v_rom_mac);
wdenkfe8c2802002-11-03 00:38:21 +0000215
216/*
217 ------------------------------------------------------------
218 .
219 . Internal routines
220 .
221 ------------------------------------------------------------
222*/
223
wdenkc3c7f862004-06-09 14:47:54 +0000224#ifdef CONFIG_SMC_USE_IOFUNCS
225/*
226 * input and output functions
227 *
228 * Implemented due to inx,outx macros accessing the device improperly
229 * and putting the device into an unkown state.
230 *
231 * For instance, on Sharp LPD7A400 SDK, affects were chip memory
232 * could not be free'd (hence the alloc failures), duplicate packets,
233 * packets being corrupt (shifted) on the wire, etc. Switching to the
234 * inx,outx functions fixed this problem.
235 */
236static inline word SMC_inw(dword offset);
237static inline void SMC_outw(word value, dword offset);
238static inline byte SMC_inb(dword offset);
239static inline void SMC_outb(byte value, dword offset);
240static inline void SMC_insw(dword offset, volatile uchar* buf, dword len);
241static inline void SMC_outsw(dword offset, uchar* buf, dword len);
242
243#define barrier() __asm__ __volatile__("": : :"memory")
244
245static inline word SMC_inw(dword offset)
246{
247 word v;
248 v = *((volatile word*)(SMC_BASE_ADDRESS+offset));
249 barrier(); *(volatile u32*)(0xc0000000);
250 return v;
251}
252
253static inline void SMC_outw(word value, dword offset)
254{
255 *((volatile word*)(SMC_BASE_ADDRESS+offset)) = value;
256 barrier(); *(volatile u32*)(0xc0000000);
257}
258
259static inline byte SMC_inb(dword offset)
260{
261 word _w;
262
263 _w = SMC_inw(offset & ~((dword)1));
264 return (offset & 1) ? (byte)(_w >> 8) : (byte)(_w);
265}
266
267static inline void SMC_outb(byte value, dword offset)
268{
269 word _w;
270
271 _w = SMC_inw(offset & ~((dword)1));
272 if (offset & 1)
273 *((volatile word*)(SMC_BASE_ADDRESS+(offset & ~((dword)1)))) = (value<<8) | (_w & 0x00ff);
274 else
275 *((volatile word*)(SMC_BASE_ADDRESS+offset)) = value | (_w & 0xff00);
276}
277
278static inline void SMC_insw(dword offset, volatile uchar* buf, dword len)
279{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100280 volatile word *p = (volatile word *)buf;
281
wdenkc3c7f862004-06-09 14:47:54 +0000282 while (len-- > 0) {
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100283 *p++ = SMC_inw(offset);
284 barrier();
285 *((volatile u32*)(0xc0000000));
wdenkc3c7f862004-06-09 14:47:54 +0000286 }
287}
288
289static inline void SMC_outsw(dword offset, uchar* buf, dword len)
290{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100291 volatile word *p = (volatile word *)buf;
292
wdenkc3c7f862004-06-09 14:47:54 +0000293 while (len-- > 0) {
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100294 SMC_outw(*p++, offset);
295 barrier();
296 *(volatile u32*)(0xc0000000);
wdenkc3c7f862004-06-09 14:47:54 +0000297 }
298}
299#endif /* CONFIG_SMC_USE_IOFUNCS */
300
wdenk8bf3b002003-12-06 23:20:41 +0000301static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
wdenkfe8c2802002-11-03 00:38:21 +0000302
303/*
304 * This function must be called before smc_open() if you want to override
305 * the default mac address.
306 */
307
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100308void smc_set_mac_addr(const unsigned char *addr) {
wdenkfe8c2802002-11-03 00:38:21 +0000309 int i;
310
311 for (i=0; i < sizeof(smc_mac_addr); i++){
312 smc_mac_addr[i] = addr[i];
313 }
314}
315
316/*
317 * smc_get_macaddr is no longer used. If you want to override the default
wdenk0b97ab12003-06-19 23:58:30 +0000318 * mac address, call smc_get_mac_addr as a part of the board initialization.
wdenkfe8c2802002-11-03 00:38:21 +0000319 */
320
321#if 0
322void smc_get_macaddr( byte *addr ) {
323 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
wdenk8bde7f72003-06-27 21:31:46 +0000324 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
wdenkfe8c2802002-11-03 00:38:21 +0000325 int i;
326
327
wdenk8bde7f72003-06-27 21:31:46 +0000328 for (i=0; i<6; i++) {
329 addr[0] = *(dnp1110_mac+0);
330 addr[1] = *(dnp1110_mac+1);
331 addr[2] = *(dnp1110_mac+2);
332 addr[3] = *(dnp1110_mac+3);
333 addr[4] = *(dnp1110_mac+4);
334 addr[5] = *(dnp1110_mac+5);
335 }
wdenkfe8c2802002-11-03 00:38:21 +0000336}
337#endif /* 0 */
338
339/***********************************************
wdenk42dfe7a2004-03-14 22:25:36 +0000340 * Show available memory *
wdenkfe8c2802002-11-03 00:38:21 +0000341 ***********************************************/
342void dump_memory_info(void)
343{
wdenk8bde7f72003-06-27 21:31:46 +0000344 word mem_info;
345 word old_bank;
wdenkfe8c2802002-11-03 00:38:21 +0000346
wdenk8bde7f72003-06-27 21:31:46 +0000347 old_bank = SMC_inw(BANK_SELECT)&0xF;
wdenkfe8c2802002-11-03 00:38:21 +0000348
wdenk8bde7f72003-06-27 21:31:46 +0000349 SMC_SELECT_BANK(0);
350 mem_info = SMC_inw( MIR_REG );
351 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
wdenkfe8c2802002-11-03 00:38:21 +0000352
wdenk8bde7f72003-06-27 21:31:46 +0000353 SMC_SELECT_BANK(old_bank);
wdenkfe8c2802002-11-03 00:38:21 +0000354}
355/*
356 . A rather simple routine to print out a packet for debugging purposes.
357*/
358#if SMC_DEBUG > 2
359static void print_packet( byte *, int );
360#endif
361
362#define tx_done(dev) 1
363
364
wdenkfe8c2802002-11-03 00:38:21 +0000365/* this does a soft reset on the device */
366static void smc_reset( void );
367
368/* Enable Interrupts, Receive, and Transmit */
369static void smc_enable( void );
370
371/* this puts the device in an inactive state */
372static void smc_shutdown( void );
373
374/* Routines to Read and Write the PHY Registers across the
375 MII Management Interface
376*/
377
378#ifndef CONFIG_SMC91111_EXT_PHY
379static word smc_read_phy_register(byte phyreg);
380static void smc_write_phy_register(byte phyreg, word phydata);
381#endif /* !CONFIG_SMC91111_EXT_PHY */
382
383
wdenkb56ddc62003-09-15 21:14:37 +0000384static int poll4int (byte mask, int timeout)
385{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200386 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
wdenkb56ddc62003-09-15 21:14:37 +0000387 int is_timeout = 0;
388 word old_bank = SMC_inw (BSR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000389
wdenkb56ddc62003-09-15 21:14:37 +0000390 PRINTK2 ("Polling...\n");
391 SMC_SELECT_BANK (2);
392 while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
393 if (get_timer (0) >= tmo) {
394 is_timeout = 1;
395 break;
396 }
wdenkfe8c2802002-11-03 00:38:21 +0000397 }
wdenkfe8c2802002-11-03 00:38:21 +0000398
wdenkb56ddc62003-09-15 21:14:37 +0000399 /* restore old bank selection */
400 SMC_SELECT_BANK (old_bank);
wdenkfe8c2802002-11-03 00:38:21 +0000401
wdenkb56ddc62003-09-15 21:14:37 +0000402 if (is_timeout)
403 return 1;
404 else
405 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000406}
407
wdenk487778b2003-06-06 11:20:01 +0000408/* Only one release command at a time, please */
wdenkb56ddc62003-09-15 21:14:37 +0000409static inline void smc_wait_mmu_release_complete (void)
wdenk487778b2003-06-06 11:20:01 +0000410{
411 int count = 0;
wdenkb56ddc62003-09-15 21:14:37 +0000412
wdenk487778b2003-06-06 11:20:01 +0000413 /* assume bank 2 selected */
wdenkb56ddc62003-09-15 21:14:37 +0000414 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
415 udelay (1); /* Wait until not busy */
416 if (++count > 200)
417 break;
wdenk487778b2003-06-06 11:20:01 +0000418 }
419}
420
wdenkfe8c2802002-11-03 00:38:21 +0000421/*
422 . Function: smc_reset( void )
423 . Purpose:
wdenk42dfe7a2004-03-14 22:25:36 +0000424 . This sets the SMC91111 chip to its normal state, hopefully from whatever
425 . mess that any other DOS driver has put it in.
wdenkfe8c2802002-11-03 00:38:21 +0000426 .
427 . Maybe I should reset more registers to defaults in here? SOFTRST should
428 . do that for me.
429 .
430 . Method:
431 . 1. send a SOFT RESET
432 . 2. wait for it to finish
433 . 3. enable autorelease mode
434 . 4. reset the memory management unit
435 . 5. clear all interrupts
436 .
437*/
wdenkb56ddc62003-09-15 21:14:37 +0000438static void smc_reset (void)
wdenkfe8c2802002-11-03 00:38:21 +0000439{
wdenkf39748a2004-06-09 13:37:52 +0000440 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000441
442 /* This resets the registers mostly to defaults, but doesn't
443 affect EEPROM. That seems unnecessary */
wdenkb56ddc62003-09-15 21:14:37 +0000444 SMC_SELECT_BANK (0);
445 SMC_outw (RCR_SOFTRST, RCR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000446
447 /* Setup the Configuration Register */
448 /* This is necessary because the CONFIG_REG is not affected */
449 /* by a soft reset */
450
wdenkb56ddc62003-09-15 21:14:37 +0000451 SMC_SELECT_BANK (1);
wdenkfe8c2802002-11-03 00:38:21 +0000452#if defined(CONFIG_SMC91111_EXT_PHY)
wdenkb56ddc62003-09-15 21:14:37 +0000453 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000454#else
wdenkb56ddc62003-09-15 21:14:37 +0000455 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000456#endif
457
458
459 /* Release from possible power-down state */
460 /* Configuration register is not affected by Soft Reset */
wdenkb56ddc62003-09-15 21:14:37 +0000461 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000462
wdenkb56ddc62003-09-15 21:14:37 +0000463 SMC_SELECT_BANK (0);
wdenkfe8c2802002-11-03 00:38:21 +0000464
465 /* this should pause enough for the chip to be happy */
wdenkb56ddc62003-09-15 21:14:37 +0000466 udelay (10);
wdenkfe8c2802002-11-03 00:38:21 +0000467
468 /* Disable transmit and receive functionality */
wdenkb56ddc62003-09-15 21:14:37 +0000469 SMC_outw (RCR_CLEAR, RCR_REG);
470 SMC_outw (TCR_CLEAR, TCR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000471
472 /* set the control register */
wdenkb56ddc62003-09-15 21:14:37 +0000473 SMC_SELECT_BANK (1);
474 SMC_outw (CTL_DEFAULT, CTL_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000475
476 /* Reset the MMU */
wdenkb56ddc62003-09-15 21:14:37 +0000477 SMC_SELECT_BANK (2);
478 smc_wait_mmu_release_complete ();
479 SMC_outw (MC_RESET, MMU_CMD_REG);
480 while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
481 udelay (1); /* Wait until not busy */
wdenkfe8c2802002-11-03 00:38:21 +0000482
483 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
484 but this is a place where future chipsets _COULD_ break. Be wary
wdenk8bde7f72003-06-27 21:31:46 +0000485 of issuing another MMU command right after this */
wdenkfe8c2802002-11-03 00:38:21 +0000486
487 /* Disable all interrupts */
wdenkb56ddc62003-09-15 21:14:37 +0000488 SMC_outb (0, IM_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000489}
490
491/*
492 . Function: smc_enable
493 . Purpose: let the chip talk to the outside work
494 . Method:
495 . 1. Enable the transmitter
496 . 2. Enable the receiver
497 . 3. Enable interrupts
498*/
499static void smc_enable()
500{
wdenkf39748a2004-06-09 13:37:52 +0000501 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000502 SMC_SELECT_BANK( 0 );
503 /* see the header file for options in TCR/RCR DEFAULT*/
504 SMC_outw( TCR_DEFAULT, TCR_REG );
505 SMC_outw( RCR_DEFAULT, RCR_REG );
506
507 /* clear MII_DIS */
508/* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
509}
510
511/*
512 . Function: smc_shutdown
513 . Purpose: closes down the SMC91xxx chip.
514 . Method:
515 . 1. zero the interrupt mask
516 . 2. clear the enable receive flag
517 . 3. clear the enable xmit flags
518 .
519 . TODO:
520 . (1) maybe utilize power down mode.
521 . Why not yet? Because while the chip will go into power down mode,
522 . the manual says that it will wake up in response to any I/O requests
wdenk42dfe7a2004-03-14 22:25:36 +0000523 . in the register space. Empirical results do not show this working.
wdenkfe8c2802002-11-03 00:38:21 +0000524*/
525static void smc_shutdown()
526{
wdenkf39748a2004-06-09 13:37:52 +0000527 PRINTK2(CARDNAME ": smc_shutdown\n");
wdenkfe8c2802002-11-03 00:38:21 +0000528
529 /* no more interrupts for me */
530 SMC_SELECT_BANK( 2 );
531 SMC_outb( 0, IM_REG );
532
533 /* and tell the card to stay away from that nasty outside world */
534 SMC_SELECT_BANK( 0 );
535 SMC_outb( RCR_CLEAR, RCR_REG );
536 SMC_outb( TCR_CLEAR, TCR_REG );
Wolfgang Denk0afe5192006-03-12 02:10:00 +0100537#ifdef SHARED_RESOURCES
538 swap_to(FLASH);
539#endif
wdenkfe8c2802002-11-03 00:38:21 +0000540}
541
542
543/*
544 . Function: smc_hardware_send_packet(struct net_device * )
545 . Purpose:
546 . This sends the actual packet to the SMC9xxx chip.
547 .
548 . Algorithm:
wdenk42dfe7a2004-03-14 22:25:36 +0000549 . First, see if a saved_skb is available.
wdenkfe8c2802002-11-03 00:38:21 +0000550 . ( this should NOT be called if there is no 'saved_skb'
551 . Now, find the packet number that the chip allocated
552 . Point the data pointers at it in memory
553 . Set the length word in the chip's memory
554 . Dump the packet to chip memory
555 . Check if a last byte is needed ( odd length packet )
556 . if so, set the control flag right
wdenk42dfe7a2004-03-14 22:25:36 +0000557 . Tell the card to send it
wdenkfe8c2802002-11-03 00:38:21 +0000558 . Enable the transmit interrupt, so I know if it failed
wdenk42dfe7a2004-03-14 22:25:36 +0000559 . Free the kernel data if I actually sent it.
wdenkfe8c2802002-11-03 00:38:21 +0000560*/
wdenkb56ddc62003-09-15 21:14:37 +0000561static int smc_send_packet (volatile void *packet, int packet_length)
wdenkfe8c2802002-11-03 00:38:21 +0000562{
wdenkb56ddc62003-09-15 21:14:37 +0000563 byte packet_no;
564 unsigned long ioaddr;
565 byte *buf;
566 int length;
567 int numPages;
568 int try = 0;
569 int time_out;
570 byte status;
wdenk518e2e12004-03-25 14:59:05 +0000571 byte saved_pnr;
572 word saved_ptr;
wdenkfe8c2802002-11-03 00:38:21 +0000573
wdenk518e2e12004-03-25 14:59:05 +0000574 /* save PTR and PNR registers before manipulation */
wdenkb79a11c2004-03-25 15:14:43 +0000575 SMC_SELECT_BANK (2);
wdenk518e2e12004-03-25 14:59:05 +0000576 saved_pnr = SMC_inb( PN_REG );
577 saved_ptr = SMC_inw( PTR_REG );
wdenkfe8c2802002-11-03 00:38:21 +0000578
wdenkf39748a2004-06-09 13:37:52 +0000579 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000580
581 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
582
583 /* allocate memory
wdenkb56ddc62003-09-15 21:14:37 +0000584 ** The MMU wants the number of pages to be the number of 256 bytes
585 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
586 **
587 ** The 91C111 ignores the size bits, but the code is left intact
588 ** for backwards and future compatibility.
589 **
590 ** Pkt size for allocating is data length +6 (for additional status
591 ** words, length and ctl!)
592 **
593 ** If odd size then last byte is included in this header.
594 */
595 numPages = ((length & 0xfffe) + 6);
596 numPages >>= 8; /* Divide by 256 */
wdenkfe8c2802002-11-03 00:38:21 +0000597
wdenkb56ddc62003-09-15 21:14:37 +0000598 if (numPages > 7) {
599 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000600 return 0;
601 }
602
603 /* now, try to allocate the memory */
wdenkb56ddc62003-09-15 21:14:37 +0000604 SMC_SELECT_BANK (2);
605 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000606
wdenkdc7c9a12003-03-26 06:55:25 +0000607 /* FIXME: the ALLOC_INT bit never gets set *
wdenk42dfe7a2004-03-14 22:25:36 +0000608 * so the following will always give a *
609 * memory allocation error. *
610 * same code works in armboot though *
wdenkdc7c9a12003-03-26 06:55:25 +0000611 * -ro
612 */
613
wdenkfe8c2802002-11-03 00:38:21 +0000614again:
615 try++;
616 time_out = MEMORY_WAIT_TIME;
617 do {
wdenkb56ddc62003-09-15 21:14:37 +0000618 status = SMC_inb (SMC91111_INT_REG);
619 if (status & IM_ALLOC_INT) {
wdenkfe8c2802002-11-03 00:38:21 +0000620 /* acknowledge the interrupt */
wdenkb56ddc62003-09-15 21:14:37 +0000621 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
wdenk8bde7f72003-06-27 21:31:46 +0000622 break;
wdenkfe8c2802002-11-03 00:38:21 +0000623 }
wdenkb56ddc62003-09-15 21:14:37 +0000624 } while (--time_out);
wdenkfe8c2802002-11-03 00:38:21 +0000625
wdenkb56ddc62003-09-15 21:14:37 +0000626 if (!time_out) {
627 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
628 SMC_DEV_NAME, try);
629 if (try < SMC_ALLOC_MAX_TRY)
630 goto again;
631 else
632 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000633 }
634
wdenkb56ddc62003-09-15 21:14:37 +0000635 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
636 SMC_DEV_NAME, try);
wdenkfe8c2802002-11-03 00:38:21 +0000637
638 /* I can send the packet now.. */
639
640 ioaddr = SMC_BASE_ADDRESS;
641
wdenkb56ddc62003-09-15 21:14:37 +0000642 buf = (byte *) packet;
wdenkfe8c2802002-11-03 00:38:21 +0000643
644 /* If I get here, I _know_ there is a packet slot waiting for me */
wdenkb56ddc62003-09-15 21:14:37 +0000645 packet_no = SMC_inb (AR_REG);
646 if (packet_no & AR_FAILED) {
wdenkfe8c2802002-11-03 00:38:21 +0000647 /* or isn't there? BAD CHIP! */
wdenkb56ddc62003-09-15 21:14:37 +0000648 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000649 return 0;
650 }
651
652 /* we have a packet address, so tell the card to use it */
wdenk1f6d4252004-11-02 13:00:33 +0000653#ifndef CONFIG_XAENIAX
wdenkb56ddc62003-09-15 21:14:37 +0000654 SMC_outb (packet_no, PN_REG);
wdenk1f6d4252004-11-02 13:00:33 +0000655#else
656 /* On Xaeniax board, we can't use SMC_outb here because that way
657 * the Allocate MMU command will end up written to the command register
658 * as well, which will lead to a problem.
659 */
660 SMC_outl (packet_no << 16, 0);
661#endif
wdenkb79a11c2004-03-25 15:14:43 +0000662 /* do not write new ptr value if Write data fifo not empty */
663 while ( saved_ptr & PTR_NOTEMPTY )
wdenk518e2e12004-03-25 14:59:05 +0000664 printf ("Write data fifo not empty!\n");
665
wdenkfe8c2802002-11-03 00:38:21 +0000666 /* point to the beginning of the packet */
wdenkb56ddc62003-09-15 21:14:37 +0000667 SMC_outw (PTR_AUTOINC, PTR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000668
wdenkb56ddc62003-09-15 21:14:37 +0000669 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
670 SMC_DEV_NAME, length);
wdenkfe8c2802002-11-03 00:38:21 +0000671
672#if SMC_DEBUG > 2
wdenkb56ddc62003-09-15 21:14:37 +0000673 printf ("Transmitting Packet\n");
674 print_packet (buf, length);
wdenkfe8c2802002-11-03 00:38:21 +0000675#endif
676
677 /* send the packet length ( +6 for status, length and ctl byte )
wdenk8bde7f72003-06-27 21:31:46 +0000678 and the status word ( set to zeros ) */
wdenkfe8c2802002-11-03 00:38:21 +0000679#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000680 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000681#else
wdenkb56ddc62003-09-15 21:14:37 +0000682 SMC_outw (0, SMC91111_DATA_REG);
683 /* send the packet length ( +6 for status words, length, and ctl */
684 SMC_outw ((length + 6), SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000685#endif
686
687 /* send the actual data
wdenkb56ddc62003-09-15 21:14:37 +0000688 . I _think_ it's faster to send the longs first, and then
689 . mop up by sending the last word. It depends heavily
wdenk42dfe7a2004-03-14 22:25:36 +0000690 . on alignment, at least on the 486. Maybe it would be
wdenkb56ddc62003-09-15 21:14:37 +0000691 . a good idea to check which is optimal? But that could take
692 . almost as much time as is saved?
693 */
wdenkfe8c2802002-11-03 00:38:21 +0000694#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000695 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
wdenkbb310d42004-11-22 22:20:07 +0000696#ifndef CONFIG_XAENIAX
wdenkb56ddc62003-09-15 21:14:37 +0000697 if (length & 0x2)
698 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
699 SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000700#else
wdenkbb310d42004-11-22 22:20:07 +0000701 /* On XANEIAX, we can only use 32-bit writes, so we need to handle
702 * unaligned tail part specially. The standard code doesn't work.
703 */
704 if ((length & 3) == 3) {
705 u16 * ptr = (u16*) &buf[length-3];
706 SMC_outl((*ptr) | ((0x2000 | buf[length-1]) << 16),
707 SMC91111_DATA_REG);
708 } else if ((length & 2) == 2) {
709 u16 * ptr = (u16*) &buf[length-2];
710 SMC_outl(*ptr, SMC91111_DATA_REG);
711 } else if (length & 1) {
712 SMC_outl((0x2000 | buf[length-1]), SMC91111_DATA_REG);
713 } else {
714 SMC_outl(0, SMC91111_DATA_REG);
715 }
716#endif
717#else
wdenkb56ddc62003-09-15 21:14:37 +0000718 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
wdenkfe8c2802002-11-03 00:38:21 +0000719#endif /* USE_32_BIT */
720
wdenkbb310d42004-11-22 22:20:07 +0000721#ifndef CONFIG_XAENIAX
wdenk42dfe7a2004-03-14 22:25:36 +0000722 /* Send the last byte, if there is one. */
wdenkb56ddc62003-09-15 21:14:37 +0000723 if ((length & 1) == 0) {
724 SMC_outw (0, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000725 } else {
wdenkb56ddc62003-09-15 21:14:37 +0000726 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000727 }
wdenkbb310d42004-11-22 22:20:07 +0000728#endif
wdenkfe8c2802002-11-03 00:38:21 +0000729
730 /* and let the chipset deal with it */
wdenkb56ddc62003-09-15 21:14:37 +0000731 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000732
733 /* poll for TX INT */
wdenk518e2e12004-03-25 14:59:05 +0000734 /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
735 /* poll for TX_EMPTY INT - autorelease enabled */
736 if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
wdenkfe8c2802002-11-03 00:38:21 +0000737 /* sending failed */
wdenkb56ddc62003-09-15 21:14:37 +0000738 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000739
740 /* release packet */
wdenk518e2e12004-03-25 14:59:05 +0000741 /* no need to release, MMU does that now */
wdenk1f6d4252004-11-02 13:00:33 +0000742#ifdef CONFIG_XAENIAX
743 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
744#endif
wdenkfe8c2802002-11-03 00:38:21 +0000745
wdenk8bde7f72003-06-27 21:31:46 +0000746 /* wait for MMU getting ready (low) */
wdenkb56ddc62003-09-15 21:14:37 +0000747 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
748 udelay (10);
wdenk8bde7f72003-06-27 21:31:46 +0000749 }
wdenkfe8c2802002-11-03 00:38:21 +0000750
wdenkb56ddc62003-09-15 21:14:37 +0000751 PRINTK2 ("MMU ready\n");
wdenkfe8c2802002-11-03 00:38:21 +0000752
753
754 return 0;
755 } else {
756 /* ack. int */
wdenk518e2e12004-03-25 14:59:05 +0000757 SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
758 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
wdenkb56ddc62003-09-15 21:14:37 +0000759 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
760 length);
wdenkfe8c2802002-11-03 00:38:21 +0000761
762 /* release packet */
wdenk518e2e12004-03-25 14:59:05 +0000763 /* no need to release, MMU does that now */
wdenk1f6d4252004-11-02 13:00:33 +0000764#ifdef CONFIG_XAENIAX
765 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
766#endif
wdenkfe8c2802002-11-03 00:38:21 +0000767
wdenk8bde7f72003-06-27 21:31:46 +0000768 /* wait for MMU getting ready (low) */
wdenkb56ddc62003-09-15 21:14:37 +0000769 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
770 udelay (10);
wdenk8bde7f72003-06-27 21:31:46 +0000771 }
wdenkfe8c2802002-11-03 00:38:21 +0000772
wdenkb56ddc62003-09-15 21:14:37 +0000773 PRINTK2 ("MMU ready\n");
wdenkfe8c2802002-11-03 00:38:21 +0000774
775
776 }
777
wdenk518e2e12004-03-25 14:59:05 +0000778 /* restore previously saved registers */
wdenk1f6d4252004-11-02 13:00:33 +0000779#ifndef CONFIG_XAENIAX
wdenk518e2e12004-03-25 14:59:05 +0000780 SMC_outb( saved_pnr, PN_REG );
wdenk1f6d4252004-11-02 13:00:33 +0000781#else
782 /* On Xaeniax board, we can't use SMC_outb here because that way
783 * the Allocate MMU command will end up written to the command register
784 * as well, which will lead to a problem.
785 */
786 SMC_outl(saved_pnr << 16, 0);
787#endif
wdenk518e2e12004-03-25 14:59:05 +0000788 SMC_outw( saved_ptr, PTR_REG );
789
wdenkfe8c2802002-11-03 00:38:21 +0000790 return length;
791}
792
793/*-------------------------------------------------------------------------
794 |
795 | smc_destructor( struct net_device * dev )
796 | Input parameters:
797 | dev, pointer to the device structure
798 |
799 | Output:
800 | None.
801 |
802 ---------------------------------------------------------------------------
803*/
804void smc_destructor()
805{
wdenkf39748a2004-06-09 13:37:52 +0000806 PRINTK2(CARDNAME ": smc_destructor\n");
wdenkfe8c2802002-11-03 00:38:21 +0000807}
808
809
810/*
811 * Open and Initialize the board
812 *
813 * Set up everything, reset the card, etc ..
814 *
815 */
wdenkb56ddc62003-09-15 21:14:37 +0000816static int smc_open (bd_t * bd)
wdenkfe8c2802002-11-03 00:38:21 +0000817{
wdenkb56ddc62003-09-15 21:14:37 +0000818 int i, err;
wdenkfe8c2802002-11-03 00:38:21 +0000819
wdenkf39748a2004-06-09 13:37:52 +0000820 PRINTK2 ("%s: smc_open\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000821
822 /* reset the hardware */
wdenkb56ddc62003-09-15 21:14:37 +0000823 smc_reset ();
824 smc_enable ();
wdenkfe8c2802002-11-03 00:38:21 +0000825
826 /* Configure the PHY */
827#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +0000828 smc_phy_configure ();
wdenkfe8c2802002-11-03 00:38:21 +0000829#endif
830
wdenkfe8c2802002-11-03 00:38:21 +0000831 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
832/* SMC_SELECT_BANK(0); */
833/* SMC_outw(0, RPC_REG); */
wdenkb56ddc62003-09-15 21:14:37 +0000834 SMC_SELECT_BANK (1);
wdenk8bde7f72003-06-27 21:31:46 +0000835
wdenkb56ddc62003-09-15 21:14:37 +0000836 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500837 if (err < 0)
838 return -1;
wdenkfe8c2802002-11-03 00:38:21 +0000839#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000840 for (i = 0; i < 6; i += 2) {
wdenkfe8c2802002-11-03 00:38:21 +0000841 word address;
842
wdenkb56ddc62003-09-15 21:14:37 +0000843 address = smc_mac_addr[i + 1] << 8;
844 address |= smc_mac_addr[i];
wdenk39539882004-07-01 16:30:44 +0000845 SMC_outw (address, (ADDR0_REG + i));
wdenkfe8c2802002-11-03 00:38:21 +0000846 }
847#else
wdenkb56ddc62003-09-15 21:14:37 +0000848 for (i = 0; i < 6; i++)
wdenk39539882004-07-01 16:30:44 +0000849 SMC_outb (smc_mac_addr[i], (ADDR0_REG + i));
wdenkfe8c2802002-11-03 00:38:21 +0000850#endif
851
852 return 0;
853}
854
wdenkfe8c2802002-11-03 00:38:21 +0000855/*-------------------------------------------------------------
856 .
857 . smc_rcv - receive a packet from the card
858 .
859 . There is ( at least ) a packet waiting to be read from
860 . chip-memory.
861 .
862 . o Read the status
863 . o If an error, record it
864 . o otherwise, read in the packet
865 --------------------------------------------------------------
866*/
867static int smc_rcv()
868{
wdenk42dfe7a2004-03-14 22:25:36 +0000869 int packet_number;
wdenkfe8c2802002-11-03 00:38:21 +0000870 word status;
871 word packet_length;
wdenk42dfe7a2004-03-14 22:25:36 +0000872 int is_error = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000873#ifdef USE_32_BIT
874 dword stat_len;
875#endif
wdenk518e2e12004-03-25 14:59:05 +0000876 byte saved_pnr;
877 word saved_ptr;
wdenkfe8c2802002-11-03 00:38:21 +0000878
wdenkfe8c2802002-11-03 00:38:21 +0000879 SMC_SELECT_BANK(2);
wdenk518e2e12004-03-25 14:59:05 +0000880 /* save PTR and PTR registers */
881 saved_pnr = SMC_inb( PN_REG );
882 saved_ptr = SMC_inw( PTR_REG );
883
wdenkfe8c2802002-11-03 00:38:21 +0000884 packet_number = SMC_inw( RXFIFO_REG );
885
886 if ( packet_number & RXFIFO_REMPTY ) {
887
888 return 0;
889 }
890
wdenkf39748a2004-06-09 13:37:52 +0000891 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000892 /* start reading from the start of the packet */
893 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
894
895 /* First two words are status and packet_length */
896#ifdef USE_32_BIT
897 stat_len = SMC_inl(SMC91111_DATA_REG);
898 status = stat_len & 0xffff;
899 packet_length = stat_len >> 16;
900#else
wdenk42dfe7a2004-03-14 22:25:36 +0000901 status = SMC_inw( SMC91111_DATA_REG );
902 packet_length = SMC_inw( SMC91111_DATA_REG );
wdenkfe8c2802002-11-03 00:38:21 +0000903#endif
904
905 packet_length &= 0x07ff; /* mask off top bits */
906
907 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
908
909 if ( !(status & RS_ERRORS ) ){
910 /* Adjust for having already read the first two words */
911 packet_length -= 4; /*4; */
912
913
wdenkfe8c2802002-11-03 00:38:21 +0000914 /* set odd length for bug in LAN91C111, */
915 /* which never sets RS_ODDFRAME */
916 /* TODO ? */
917
918
919#ifdef USE_32_BIT
920 PRINTK3(" Reading %d dwords (and %d bytes) \n",
921 packet_length >> 2, packet_length & 3 );
922 /* QUESTION: Like in the TX routine, do I want
923 to send the DWORDs or the bytes first, or some
924 mixture. A mixture might improve already slow PIO
wdenk42dfe7a2004-03-14 22:25:36 +0000925 performance */
wdenkfe8c2802002-11-03 00:38:21 +0000926 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
927 /* read the left over bytes */
928 if (packet_length & 3) {
929 int i;
930
wdenk699b13a2002-11-03 18:03:52 +0000931 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
wdenkfe8c2802002-11-03 00:38:21 +0000932 dword leftover = SMC_inl(SMC91111_DATA_REG);
933 for (i=0; i<(packet_length & 3); i++)
934 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
935 }
936#else
937 PRINTK3(" Reading %d words and %d byte(s) \n",
938 (packet_length >> 1 ), packet_length & 1 );
939 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
940
941#endif /* USE_32_BIT */
942
943#if SMC_DEBUG > 2
944 printf("Receiving Packet\n");
945 print_packet( NetRxPackets[0], packet_length );
946#endif
947 } else {
948 /* error ... */
949 /* TODO ? */
950 is_error = 1;
951 }
952
953 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
954 udelay(1); /* Wait until not busy */
955
956 /* error or good, tell the card to get rid of this packet */
957 SMC_outw( MC_RELEASE, MMU_CMD_REG );
958
959 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
960 udelay(1); /* Wait until not busy */
961
wdenk518e2e12004-03-25 14:59:05 +0000962 /* restore saved registers */
wdenk1f6d4252004-11-02 13:00:33 +0000963#ifndef CONFIG_XAENIAX
wdenk518e2e12004-03-25 14:59:05 +0000964 SMC_outb( saved_pnr, PN_REG );
wdenk1f6d4252004-11-02 13:00:33 +0000965#else
966 /* On Xaeniax board, we can't use SMC_outb here because that way
967 * the Allocate MMU command will end up written to the command register
968 * as well, which will lead to a problem.
969 */
970 SMC_outl( saved_pnr << 16, 0);
971#endif
wdenk518e2e12004-03-25 14:59:05 +0000972 SMC_outw( saved_ptr, PTR_REG );
973
wdenkfe8c2802002-11-03 00:38:21 +0000974 if (!is_error) {
975 /* Pass the packet up to the protocol layers. */
976 NetReceive(NetRxPackets[0], packet_length);
977 return packet_length;
978 } else {
979 return 0;
980 }
981
982}
983
984
wdenkfe8c2802002-11-03 00:38:21 +0000985/*----------------------------------------------------
986 . smc_close
987 .
988 . this makes the board clean up everything that it can
wdenk42dfe7a2004-03-14 22:25:36 +0000989 . and not talk to the outside world. Caused by
wdenkfe8c2802002-11-03 00:38:21 +0000990 . an 'ifconfig ethX down'
991 .
992 -----------------------------------------------------*/
993static int smc_close()
994{
wdenkf39748a2004-06-09 13:37:52 +0000995 PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000996
997 /* clear everything */
998 smc_shutdown();
999
1000 return 0;
1001}
1002
1003
1004#if 0
1005/*------------------------------------------------------------
1006 . Modify a bit in the LAN91C111 register set
1007 .-------------------------------------------------------------*/
1008static word smc_modify_regbit(int bank, int ioaddr, int reg,
1009 unsigned int bit, int val)
1010{
1011 word regval;
1012
1013 SMC_SELECT_BANK( bank );
1014
1015 regval = SMC_inw( reg );
1016 if (val)
1017 regval |= bit;
1018 else
1019 regval &= ~bit;
1020
1021 SMC_outw( regval, 0 );
1022 return(regval);
1023}
1024
1025
1026/*------------------------------------------------------------
1027 . Retrieve a bit in the LAN91C111 register set
1028 .-------------------------------------------------------------*/
1029static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
1030{
1031 SMC_SELECT_BANK( bank );
1032 if ( SMC_inw( reg ) & bit)
1033 return(1);
1034 else
1035 return(0);
1036}
1037
1038
1039/*------------------------------------------------------------
1040 . Modify a LAN91C111 register (word access only)
1041 .-------------------------------------------------------------*/
1042static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
1043{
1044 SMC_SELECT_BANK( bank );
1045 SMC_outw( val, reg );
1046}
1047
1048
1049/*------------------------------------------------------------
1050 . Retrieve a LAN91C111 register (word access only)
1051 .-------------------------------------------------------------*/
1052static int smc_get_reg(int bank, int ioaddr, int reg)
1053{
1054 SMC_SELECT_BANK( bank );
1055 return(SMC_inw( reg ));
1056}
1057
1058#endif /* 0 */
1059
1060/*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
1061
1062#if (SMC_DEBUG > 2 )
1063
1064/*------------------------------------------------------------
1065 . Debugging function for viewing MII Management serial bitstream
1066 .-------------------------------------------------------------*/
wdenkb56ddc62003-09-15 21:14:37 +00001067static void smc_dump_mii_stream (byte * bits, int size)
wdenkfe8c2802002-11-03 00:38:21 +00001068{
1069 int i;
1070
wdenkb56ddc62003-09-15 21:14:37 +00001071 printf ("BIT#:");
1072 for (i = 0; i < size; ++i) {
1073 printf ("%d", i % 10);
1074 }
wdenkfe8c2802002-11-03 00:38:21 +00001075
wdenkb56ddc62003-09-15 21:14:37 +00001076 printf ("\nMDOE:");
1077 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001078 if (bits[i] & MII_MDOE)
wdenkb56ddc62003-09-15 21:14:37 +00001079 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001080 else
wdenkb56ddc62003-09-15 21:14:37 +00001081 printf ("0");
1082 }
wdenkfe8c2802002-11-03 00:38:21 +00001083
wdenkb56ddc62003-09-15 21:14:37 +00001084 printf ("\nMDO :");
1085 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001086 if (bits[i] & MII_MDO)
wdenkb56ddc62003-09-15 21:14:37 +00001087 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001088 else
wdenkb56ddc62003-09-15 21:14:37 +00001089 printf ("0");
1090 }
wdenkfe8c2802002-11-03 00:38:21 +00001091
wdenkb56ddc62003-09-15 21:14:37 +00001092 printf ("\nMDI :");
1093 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001094 if (bits[i] & MII_MDI)
wdenkb56ddc62003-09-15 21:14:37 +00001095 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001096 else
wdenkb56ddc62003-09-15 21:14:37 +00001097 printf ("0");
1098 }
wdenkfe8c2802002-11-03 00:38:21 +00001099
wdenkb56ddc62003-09-15 21:14:37 +00001100 printf ("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001101}
1102#endif
1103
1104/*------------------------------------------------------------
1105 . Reads a register from the MII Management serial interface
1106 .-------------------------------------------------------------*/
1107#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +00001108static word smc_read_phy_register (byte phyreg)
wdenkfe8c2802002-11-03 00:38:21 +00001109{
1110 int oldBank;
1111 int i;
1112 byte mask;
1113 word mii_reg;
1114 byte bits[64];
1115 int clk_idx = 0;
1116 int input_idx;
1117 word phydata;
1118 byte phyaddr = SMC_PHY_ADDR;
1119
1120 /* 32 consecutive ones on MDO to establish sync */
1121 for (i = 0; i < 32; ++i)
1122 bits[clk_idx++] = MII_MDOE | MII_MDO;
1123
1124 /* Start code <01> */
1125 bits[clk_idx++] = MII_MDOE;
1126 bits[clk_idx++] = MII_MDOE | MII_MDO;
1127
1128 /* Read command <10> */
1129 bits[clk_idx++] = MII_MDOE | MII_MDO;
1130 bits[clk_idx++] = MII_MDOE;
1131
1132 /* Output the PHY address, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001133 mask = (byte) 0x10;
1134 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001135 if (phyaddr & mask)
1136 bits[clk_idx++] = MII_MDOE | MII_MDO;
1137 else
1138 bits[clk_idx++] = MII_MDOE;
1139
1140 /* Shift to next lowest bit */
1141 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001142 }
wdenkfe8c2802002-11-03 00:38:21 +00001143
1144 /* Output the phy register number, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001145 mask = (byte) 0x10;
1146 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001147 if (phyreg & mask)
1148 bits[clk_idx++] = MII_MDOE | MII_MDO;
1149 else
1150 bits[clk_idx++] = MII_MDOE;
1151
1152 /* Shift to next lowest bit */
1153 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001154 }
wdenkfe8c2802002-11-03 00:38:21 +00001155
1156 /* Tristate and turnaround (2 bit times) */
1157 bits[clk_idx++] = 0;
1158 /*bits[clk_idx++] = 0; */
1159
1160 /* Input starts at this bit time */
1161 input_idx = clk_idx;
1162
1163 /* Will input 16 bits */
1164 for (i = 0; i < 16; ++i)
1165 bits[clk_idx++] = 0;
1166
1167 /* Final clock bit */
1168 bits[clk_idx++] = 0;
1169
1170 /* Save the current bank */
wdenkb56ddc62003-09-15 21:14:37 +00001171 oldBank = SMC_inw (BANK_SELECT);
wdenkfe8c2802002-11-03 00:38:21 +00001172
1173 /* Select bank 3 */
wdenkb56ddc62003-09-15 21:14:37 +00001174 SMC_SELECT_BANK (3);
wdenkfe8c2802002-11-03 00:38:21 +00001175
1176 /* Get the current MII register value */
wdenkb56ddc62003-09-15 21:14:37 +00001177 mii_reg = SMC_inw (MII_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001178
1179 /* Turn off all MII Interface bits */
wdenkb56ddc62003-09-15 21:14:37 +00001180 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
wdenkfe8c2802002-11-03 00:38:21 +00001181
1182 /* Clock all 64 cycles */
wdenkb56ddc62003-09-15 21:14:37 +00001183 for (i = 0; i < sizeof bits; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001184 /* Clock Low - output data */
wdenkb56ddc62003-09-15 21:14:37 +00001185 SMC_outw (mii_reg | bits[i], MII_REG);
1186 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001187
1188
1189 /* Clock Hi - input data */
wdenkb56ddc62003-09-15 21:14:37 +00001190 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1191 udelay (SMC_PHY_CLOCK_DELAY);
1192 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1193 }
wdenkfe8c2802002-11-03 00:38:21 +00001194
1195 /* Return to idle state */
1196 /* Set clock to low, data to low, and output tristated */
wdenkb56ddc62003-09-15 21:14:37 +00001197 SMC_outw (mii_reg, MII_REG);
1198 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001199
1200 /* Restore original bank select */
wdenkb56ddc62003-09-15 21:14:37 +00001201 SMC_SELECT_BANK (oldBank);
wdenkfe8c2802002-11-03 00:38:21 +00001202
1203 /* Recover input data */
1204 phydata = 0;
wdenkb56ddc62003-09-15 21:14:37 +00001205 for (i = 0; i < 16; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001206 phydata <<= 1;
1207
1208 if (bits[input_idx++] & MII_MDI)
1209 phydata |= 0x0001;
wdenkb56ddc62003-09-15 21:14:37 +00001210 }
wdenkfe8c2802002-11-03 00:38:21 +00001211
1212#if (SMC_DEBUG > 2 )
wdenkb56ddc62003-09-15 21:14:37 +00001213 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
wdenkfe8c2802002-11-03 00:38:21 +00001214 phyaddr, phyreg, phydata);
wdenkb56ddc62003-09-15 21:14:37 +00001215 smc_dump_mii_stream (bits, sizeof bits);
wdenkfe8c2802002-11-03 00:38:21 +00001216#endif
1217
wdenkb56ddc62003-09-15 21:14:37 +00001218 return (phydata);
wdenkfe8c2802002-11-03 00:38:21 +00001219}
1220
1221
1222/*------------------------------------------------------------
1223 . Writes a register to the MII Management serial interface
1224 .-------------------------------------------------------------*/
wdenkb56ddc62003-09-15 21:14:37 +00001225static void smc_write_phy_register (byte phyreg, word phydata)
wdenkfe8c2802002-11-03 00:38:21 +00001226{
1227 int oldBank;
1228 int i;
1229 word mask;
1230 word mii_reg;
1231 byte bits[65];
1232 int clk_idx = 0;
1233 byte phyaddr = SMC_PHY_ADDR;
1234
1235 /* 32 consecutive ones on MDO to establish sync */
1236 for (i = 0; i < 32; ++i)
1237 bits[clk_idx++] = MII_MDOE | MII_MDO;
1238
1239 /* Start code <01> */
1240 bits[clk_idx++] = MII_MDOE;
1241 bits[clk_idx++] = MII_MDOE | MII_MDO;
1242
1243 /* Write command <01> */
1244 bits[clk_idx++] = MII_MDOE;
1245 bits[clk_idx++] = MII_MDOE | MII_MDO;
1246
1247 /* Output the PHY address, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001248 mask = (byte) 0x10;
1249 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001250 if (phyaddr & mask)
1251 bits[clk_idx++] = MII_MDOE | MII_MDO;
1252 else
1253 bits[clk_idx++] = MII_MDOE;
1254
1255 /* Shift to next lowest bit */
1256 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001257 }
wdenkfe8c2802002-11-03 00:38:21 +00001258
1259 /* Output the phy register number, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001260 mask = (byte) 0x10;
1261 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001262 if (phyreg & mask)
1263 bits[clk_idx++] = MII_MDOE | MII_MDO;
1264 else
1265 bits[clk_idx++] = MII_MDOE;
1266
1267 /* Shift to next lowest bit */
1268 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001269 }
wdenkfe8c2802002-11-03 00:38:21 +00001270
1271 /* Tristate and turnaround (2 bit times) */
1272 bits[clk_idx++] = 0;
1273 bits[clk_idx++] = 0;
1274
1275 /* Write out 16 bits of data, msb first */
1276 mask = 0x8000;
wdenkb56ddc62003-09-15 21:14:37 +00001277 for (i = 0; i < 16; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001278 if (phydata & mask)
1279 bits[clk_idx++] = MII_MDOE | MII_MDO;
1280 else
1281 bits[clk_idx++] = MII_MDOE;
1282
1283 /* Shift to next lowest bit */
1284 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001285 }
wdenkfe8c2802002-11-03 00:38:21 +00001286
1287 /* Final clock bit (tristate) */
1288 bits[clk_idx++] = 0;
1289
1290 /* Save the current bank */
wdenkb56ddc62003-09-15 21:14:37 +00001291 oldBank = SMC_inw (BANK_SELECT);
wdenkfe8c2802002-11-03 00:38:21 +00001292
1293 /* Select bank 3 */
wdenkb56ddc62003-09-15 21:14:37 +00001294 SMC_SELECT_BANK (3);
wdenkfe8c2802002-11-03 00:38:21 +00001295
1296 /* Get the current MII register value */
wdenkb56ddc62003-09-15 21:14:37 +00001297 mii_reg = SMC_inw (MII_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001298
1299 /* Turn off all MII Interface bits */
wdenkb56ddc62003-09-15 21:14:37 +00001300 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
wdenkfe8c2802002-11-03 00:38:21 +00001301
1302 /* Clock all cycles */
wdenkb56ddc62003-09-15 21:14:37 +00001303 for (i = 0; i < sizeof bits; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001304 /* Clock Low - output data */
wdenkb56ddc62003-09-15 21:14:37 +00001305 SMC_outw (mii_reg | bits[i], MII_REG);
1306 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001307
1308
1309 /* Clock Hi - input data */
wdenkb56ddc62003-09-15 21:14:37 +00001310 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1311 udelay (SMC_PHY_CLOCK_DELAY);
1312 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1313 }
wdenkfe8c2802002-11-03 00:38:21 +00001314
1315 /* Return to idle state */
1316 /* Set clock to low, data to low, and output tristated */
wdenkb56ddc62003-09-15 21:14:37 +00001317 SMC_outw (mii_reg, MII_REG);
1318 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001319
1320 /* Restore original bank select */
wdenkb56ddc62003-09-15 21:14:37 +00001321 SMC_SELECT_BANK (oldBank);
wdenkfe8c2802002-11-03 00:38:21 +00001322
1323#if (SMC_DEBUG > 2 )
wdenkb56ddc62003-09-15 21:14:37 +00001324 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
wdenkfe8c2802002-11-03 00:38:21 +00001325 phyaddr, phyreg, phydata);
wdenkb56ddc62003-09-15 21:14:37 +00001326 smc_dump_mii_stream (bits, sizeof bits);
wdenkfe8c2802002-11-03 00:38:21 +00001327#endif
1328}
1329#endif /* !CONFIG_SMC91111_EXT_PHY */
1330
1331
wdenkfe8c2802002-11-03 00:38:21 +00001332/*------------------------------------------------------------
1333 . Waits the specified number of milliseconds - kernel friendly
1334 .-------------------------------------------------------------*/
1335#ifndef CONFIG_SMC91111_EXT_PHY
1336static void smc_wait_ms(unsigned int ms)
1337{
1338 udelay(ms*1000);
1339}
1340#endif /* !CONFIG_SMC91111_EXT_PHY */
1341
1342
wdenkfe8c2802002-11-03 00:38:21 +00001343/*------------------------------------------------------------
1344 . Configures the specified PHY using Autonegotiation. Calls
1345 . smc_phy_fixed() if the user has requested a certain config.
1346 .-------------------------------------------------------------*/
1347#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +00001348static void smc_phy_configure ()
wdenkfe8c2802002-11-03 00:38:21 +00001349{
1350 int timeout;
1351 byte phyaddr;
wdenkb56ddc62003-09-15 21:14:37 +00001352 word my_phy_caps; /* My PHY capabilities */
1353 word my_ad_caps; /* My Advertised capabilities */
1354 word status = 0; /*;my status = 0 */
wdenkfe8c2802002-11-03 00:38:21 +00001355 int failed = 0;
1356
wdenkf39748a2004-06-09 13:37:52 +00001357 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001358
1359
wdenkfe8c2802002-11-03 00:38:21 +00001360 /* Get the detected phy address */
1361 phyaddr = SMC_PHY_ADDR;
1362
1363 /* Reset the PHY, setting all other bits to zero */
wdenkb56ddc62003-09-15 21:14:37 +00001364 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
wdenkfe8c2802002-11-03 00:38:21 +00001365
1366 /* Wait for the reset to complete, or time out */
wdenkb56ddc62003-09-15 21:14:37 +00001367 timeout = 6; /* Wait up to 3 seconds */
1368 while (timeout--) {
1369 if (!(smc_read_phy_register (PHY_CNTL_REG)
1370 & PHY_CNTL_RST)) {
wdenkfe8c2802002-11-03 00:38:21 +00001371 /* reset complete */
1372 break;
wdenkfe8c2802002-11-03 00:38:21 +00001373 }
1374
wdenkb56ddc62003-09-15 21:14:37 +00001375 smc_wait_ms (500); /* wait 500 millisecs */
1376 }
1377
1378 if (timeout < 1) {
1379 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001380 goto smc_phy_configure_exit;
wdenkb56ddc62003-09-15 21:14:37 +00001381 }
wdenkfe8c2802002-11-03 00:38:21 +00001382
1383 /* Read PHY Register 18, Status Output */
1384 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1385
1386 /* Enable PHY Interrupts (for register 18) */
1387 /* Interrupts listed here are disabled */
wdenk8bf3b002003-12-06 23:20:41 +00001388 smc_write_phy_register (PHY_MASK_REG, 0xffff);
wdenkfe8c2802002-11-03 00:38:21 +00001389
1390 /* Configure the Receive/Phy Control register */
wdenkb56ddc62003-09-15 21:14:37 +00001391 SMC_SELECT_BANK (0);
1392 SMC_outw (RPC_DEFAULT, RPC_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001393
1394 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
wdenkb56ddc62003-09-15 21:14:37 +00001395 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1396 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
wdenkfe8c2802002-11-03 00:38:21 +00001397
1398 if (my_phy_caps & PHY_STAT_CAP_T4)
1399 my_ad_caps |= PHY_AD_T4;
1400
1401 if (my_phy_caps & PHY_STAT_CAP_TXF)
1402 my_ad_caps |= PHY_AD_TX_FDX;
1403
1404 if (my_phy_caps & PHY_STAT_CAP_TXH)
1405 my_ad_caps |= PHY_AD_TX_HDX;
1406
1407 if (my_phy_caps & PHY_STAT_CAP_TF)
1408 my_ad_caps |= PHY_AD_10_FDX;
1409
1410 if (my_phy_caps & PHY_STAT_CAP_TH)
1411 my_ad_caps |= PHY_AD_10_HDX;
1412
1413 /* Update our Auto-Neg Advertisement Register */
wdenkb56ddc62003-09-15 21:14:37 +00001414 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
wdenkfe8c2802002-11-03 00:38:21 +00001415
wdenk518e2e12004-03-25 14:59:05 +00001416 /* Read the register back. Without this, it appears that when */
1417 /* auto-negotiation is restarted, sometimes it isn't ready and */
1418 /* the link does not come up. */
1419 smc_read_phy_register(PHY_AD_REG);
1420
wdenkf39748a2004-06-09 13:37:52 +00001421 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1422 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
wdenkfe8c2802002-11-03 00:38:21 +00001423
1424 /* Restart auto-negotiation process in order to advertise my caps */
wdenkb56ddc62003-09-15 21:14:37 +00001425 smc_write_phy_register (PHY_CNTL_REG,
1426 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
wdenkfe8c2802002-11-03 00:38:21 +00001427
1428 /* Wait for the auto-negotiation to complete. This may take from */
1429 /* 2 to 3 seconds. */
1430 /* Wait for the reset to complete, or time out */
wdenkf39748a2004-06-09 13:37:52 +00001431 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
wdenkb56ddc62003-09-15 21:14:37 +00001432 while (timeout--) {
wdenkf39748a2004-06-09 13:37:52 +00001433
wdenkb56ddc62003-09-15 21:14:37 +00001434 status = smc_read_phy_register (PHY_STAT_REG);
1435 if (status & PHY_STAT_ANEG_ACK) {
wdenkfe8c2802002-11-03 00:38:21 +00001436 /* auto-negotiate complete */
1437 break;
wdenkb56ddc62003-09-15 21:14:37 +00001438 }
wdenkfe8c2802002-11-03 00:38:21 +00001439
wdenkb56ddc62003-09-15 21:14:37 +00001440 smc_wait_ms (500); /* wait 500 millisecs */
wdenkfe8c2802002-11-03 00:38:21 +00001441
1442 /* Restart auto-negotiation if remote fault */
wdenkb56ddc62003-09-15 21:14:37 +00001443 if (status & PHY_STAT_REM_FLT) {
wdenkf39748a2004-06-09 13:37:52 +00001444 printf ("%s: PHY remote fault detected\n",
wdenkb56ddc62003-09-15 21:14:37 +00001445 SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001446
1447 /* Restart auto-negotiation */
wdenkf39748a2004-06-09 13:37:52 +00001448 printf ("%s: PHY restarting auto-negotiation\n",
wdenkfe8c2802002-11-03 00:38:21 +00001449 SMC_DEV_NAME);
wdenkb56ddc62003-09-15 21:14:37 +00001450 smc_write_phy_register (PHY_CNTL_REG,
1451 PHY_CNTL_ANEG_EN |
1452 PHY_CNTL_ANEG_RST |
1453 PHY_CNTL_SPEED |
1454 PHY_CNTL_DPLX);
wdenkfe8c2802002-11-03 00:38:21 +00001455 }
wdenkb56ddc62003-09-15 21:14:37 +00001456 }
wdenkfe8c2802002-11-03 00:38:21 +00001457
wdenkb56ddc62003-09-15 21:14:37 +00001458 if (timeout < 1) {
wdenkf39748a2004-06-09 13:37:52 +00001459 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001460 failed = 1;
wdenkb56ddc62003-09-15 21:14:37 +00001461 }
wdenkfe8c2802002-11-03 00:38:21 +00001462
1463 /* Fail if we detected an auto-negotiate remote fault */
wdenkb56ddc62003-09-15 21:14:37 +00001464 if (status & PHY_STAT_REM_FLT) {
wdenkf39748a2004-06-09 13:37:52 +00001465 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001466 failed = 1;
wdenkb56ddc62003-09-15 21:14:37 +00001467 }
wdenkfe8c2802002-11-03 00:38:21 +00001468
1469 /* Re-Configure the Receive/Phy Control register */
wdenkb56ddc62003-09-15 21:14:37 +00001470 SMC_outw (RPC_DEFAULT, RPC_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001471
wdenk26238132004-07-09 22:51:01 +00001472smc_phy_configure_exit: ;
wdenkfe8c2802002-11-03 00:38:21 +00001473
1474}
1475#endif /* !CONFIG_SMC91111_EXT_PHY */
1476
1477
1478#if SMC_DEBUG > 2
1479static void print_packet( byte * buf, int length )
1480{
wdenk8bde7f72003-06-27 21:31:46 +00001481 int i;
1482 int remainder;
1483 int lines;
wdenkfe8c2802002-11-03 00:38:21 +00001484
wdenk8bde7f72003-06-27 21:31:46 +00001485 printf("Packet of length %d \n", length );
wdenkfe8c2802002-11-03 00:38:21 +00001486
1487#if SMC_DEBUG > 3
wdenk8bde7f72003-06-27 21:31:46 +00001488 lines = length / 16;
1489 remainder = length % 16;
wdenkfe8c2802002-11-03 00:38:21 +00001490
wdenk8bde7f72003-06-27 21:31:46 +00001491 for ( i = 0; i < lines ; i ++ ) {
1492 int cur;
wdenkfe8c2802002-11-03 00:38:21 +00001493
wdenk8bde7f72003-06-27 21:31:46 +00001494 for ( cur = 0; cur < 8; cur ++ ) {
1495 byte a, b;
wdenkfe8c2802002-11-03 00:38:21 +00001496
wdenk8bde7f72003-06-27 21:31:46 +00001497 a = *(buf ++ );
1498 b = *(buf ++ );
1499 printf("%02x%02x ", a, b );
1500 }
1501 printf("\n");
1502 }
1503 for ( i = 0; i < remainder/2 ; i++ ) {
1504 byte a, b;
wdenkfe8c2802002-11-03 00:38:21 +00001505
wdenk8bde7f72003-06-27 21:31:46 +00001506 a = *(buf ++ );
1507 b = *(buf ++ );
1508 printf("%02x%02x ", a, b );
1509 }
1510 printf("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001511#endif
wdenkfe8c2802002-11-03 00:38:21 +00001512}
1513#endif
1514
1515int eth_init(bd_t *bd) {
Wolfgang Denk0afe5192006-03-12 02:10:00 +01001516#ifdef SHARED_RESOURCES
1517 swap_to(ETHERNET);
1518#endif
wdenk0b97ab12003-06-19 23:58:30 +00001519 return (smc_open(bd));
wdenkfe8c2802002-11-03 00:38:21 +00001520}
1521
1522void eth_halt() {
1523 smc_close();
1524}
1525
1526int eth_rx() {
1527 return smc_rcv();
1528}
1529
1530int eth_send(volatile void *packet, int length) {
1531 return smc_send_packet(packet, length);
1532}
1533
wdenkb56ddc62003-09-15 21:14:37 +00001534int smc_get_ethaddr (bd_t * bd)
wdenk0b97ab12003-06-19 23:58:30 +00001535{
Mike Frysinger03f3d8d2009-02-11 19:09:54 -05001536 uchar v_mac[6];
wdenk0b97ab12003-06-19 23:58:30 +00001537
Mike Frysinger03f3d8d2009-02-11 19:09:54 -05001538 if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
1539 /* get ROM mac value if any */
1540 if (!get_rom_mac(v_mac)) {
1541 printf("\n*** ERROR: ethaddr is NOT set !!\n");
1542 return -1;
wdenkb56ddc62003-09-15 21:14:37 +00001543 }
Mike Frysinger03f3d8d2009-02-11 19:09:54 -05001544 eth_setenv_enetaddr("ethaddr", v_mac);
wdenkb56ddc62003-09-15 21:14:37 +00001545 }
1546
Mike Frysinger03f3d8d2009-02-11 19:09:54 -05001547 smc_set_mac_addr(v_mac); /* use old function to update smc default */
1548 PRINTK("Using MAC Address %pM\n", v_mac);
1549 return 0;
wdenk0b97ab12003-06-19 23:58:30 +00001550}
1551
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001552int get_rom_mac (uchar *v_rom_mac)
wdenk0b97ab12003-06-19 23:58:30 +00001553{
wdenkb56ddc62003-09-15 21:14:37 +00001554#ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1555 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
1556
1557 memcpy (v_rom_mac, hw_mac_addr, 6);
1558 return (1);
wdenk0b97ab12003-06-19 23:58:30 +00001559#else
wdenk3d3befa2004-03-14 15:06:13 +00001560 int i;
wdenkf39748a2004-06-09 13:37:52 +00001561 int valid_mac = 0;
1562
wdenk3d3befa2004-03-14 15:06:13 +00001563 SMC_SELECT_BANK (1);
1564 for (i=0; i<6; i++)
1565 {
wdenk39539882004-07-01 16:30:44 +00001566 v_rom_mac[i] = SMC_inb ((ADDR0_REG + i));
wdenkf39748a2004-06-09 13:37:52 +00001567 valid_mac |= v_rom_mac[i];
wdenkb56ddc62003-09-15 21:14:37 +00001568 }
wdenkf39748a2004-06-09 13:37:52 +00001569
1570 return (valid_mac ? 1 : 0);
wdenk0b97ab12003-06-19 23:58:30 +00001571#endif
1572}