blob: 84e243ddd5282e145b8e7f957ad3b8a305929ba0 [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
68#ifdef CONFIG_DRIVER_SMC91111
69
70/* Use power-down feature of the chip */
71#define POWER_DOWN 0
72
73#define NO_AUTOPROBE
74
Wolfgang Denk0be248f2006-03-07 00:22:36 +010075#define SMC_DEBUG 0
wdenk8bf3b002003-12-06 23:20:41 +000076
77#if SMC_DEBUG > 1
wdenkfe8c2802002-11-03 00:38:21 +000078static const char version[] =
79 "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
wdenk8bf3b002003-12-06 23:20:41 +000080#endif
wdenkfe8c2802002-11-03 00:38:21 +000081
wdenkf39748a2004-06-09 13:37:52 +000082/* Autonegotiation timeout in seconds */
83#ifndef CONFIG_SMC_AUTONEG_TIMEOUT
84#define CONFIG_SMC_AUTONEG_TIMEOUT 10
85#endif
86
wdenkfe8c2802002-11-03 00:38:21 +000087/*------------------------------------------------------------------------
88 .
89 . Configuration options, for the experienced user to change.
90 .
91 -------------------------------------------------------------------------*/
92
93/*
94 . Wait time for memory to be free. This probably shouldn't be
95 . tuned that much, as waiting for this means nothing else happens
96 . in the system
97*/
98#define MEMORY_WAIT_TIME 16
99
100
101#if (SMC_DEBUG > 2 )
102#define PRINTK3(args...) printf(args)
103#else
104#define PRINTK3(args...)
105#endif
106
107#if SMC_DEBUG > 1
108#define PRINTK2(args...) printf(args)
109#else
110#define PRINTK2(args...)
111#endif
112
113#ifdef SMC_DEBUG
114#define PRINTK(args...) printf(args)
115#else
116#define PRINTK(args...)
117#endif
118
119
120/*------------------------------------------------------------------------
121 .
wdenk42dfe7a2004-03-14 22:25:36 +0000122 . The internal workings of the driver. If you are changing anything
wdenkfe8c2802002-11-03 00:38:21 +0000123 . here with the SMC stuff, you should have the datasheet and know
124 . what you are doing.
125 .
126 -------------------------------------------------------------------------*/
127#define CARDNAME "LAN91C111"
128
129/* Memory sizing constant */
130#define LAN91C111_MEMORY_MULTIPLIER (1024*2)
131
132#ifndef CONFIG_SMC91111_BASE
133#define CONFIG_SMC91111_BASE 0x20000300
134#endif
135
136#define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
137
138#define SMC_DEV_NAME "SMC91111"
139#define SMC_PHY_ADDR 0x0000
140#define SMC_ALLOC_MAX_TRY 5
141#define SMC_TX_TIMEOUT 30
142
143#define SMC_PHY_CLOCK_DELAY 1000
144
145#define ETH_ZLEN 60
146
wdenk42dfe7a2004-03-14 22:25:36 +0000147#ifdef CONFIG_SMC_USE_32_BIT
wdenkfe8c2802002-11-03 00:38:21 +0000148#define USE_32_BIT 1
149#else
150#undef USE_32_BIT
151#endif
152/*-----------------------------------------------------------------
153 .
154 . The driver can be entered at any of the following entry points.
155 .
156 .------------------------------------------------------------------ */
157
158extern int eth_init(bd_t *bd);
159extern void eth_halt(void);
160extern int eth_rx(void);
161extern int eth_send(volatile void *packet, int length);
162
163
wdenkfe8c2802002-11-03 00:38:21 +0000164/*
165 . This is called by register_netdev(). It is responsible for
166 . checking the portlist for the SMC9000 series chipset. If it finds
167 . one, then it will initialize the device, find the hardware information,
168 . and sets up the appropriate device parameters.
169 . NOTE: Interrupts are *OFF* when this procedure is called.
170 .
171 . NB:This shouldn't be static since it is referred to externally.
172*/
173int smc_init(void);
174
175/*
176 . This is called by unregister_netdev(). It is responsible for
177 . cleaning up before the driver is finally unregistered and discarded.
178*/
179void smc_destructor(void);
180
181/*
182 . The kernel calls this function when someone wants to use the device,
183 . typically 'ifconfig ethX up'.
184*/
wdenk0b97ab12003-06-19 23:58:30 +0000185static int smc_open(bd_t *bd);
wdenkfe8c2802002-11-03 00:38:21 +0000186
187
188/*
189 . This is called by the kernel in response to 'ifconfig ethX down'. It
190 . is responsible for cleaning up everything that the open routine
191 . does, and maybe putting the card into a powerdown state.
192*/
193static int smc_close(void);
194
195/*
196 . Configures the PHY through the MII Management interface
197*/
198#ifndef CONFIG_SMC91111_EXT_PHY
199static void smc_phy_configure(void);
200#endif /* !CONFIG_SMC91111_EXT_PHY */
201
202/*
203 . This is a separate procedure to handle the receipt of a packet, to
204 . leave the interrupt code looking slightly cleaner
205*/
206static int smc_rcv(void);
207
wdenk0b97ab12003-06-19 23:58:30 +0000208/* See if a MAC address is defined in the current environment. If so use it. If not
wdenk8bde7f72003-06-27 21:31:46 +0000209 . print a warning and set the environment and other globals with the default.
wdenk0b97ab12003-06-19 23:58:30 +0000210 . If an EEPROM is present it really should be consulted.
211*/
212int smc_get_ethaddr(bd_t *bd);
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100213int get_rom_mac(uchar *v_rom_mac);
wdenkfe8c2802002-11-03 00:38:21 +0000214
215/*
216 ------------------------------------------------------------
217 .
218 . Internal routines
219 .
220 ------------------------------------------------------------
221*/
222
wdenkc3c7f862004-06-09 14:47:54 +0000223#ifdef CONFIG_SMC_USE_IOFUNCS
224/*
225 * input and output functions
226 *
227 * Implemented due to inx,outx macros accessing the device improperly
228 * and putting the device into an unkown state.
229 *
230 * For instance, on Sharp LPD7A400 SDK, affects were chip memory
231 * could not be free'd (hence the alloc failures), duplicate packets,
232 * packets being corrupt (shifted) on the wire, etc. Switching to the
233 * inx,outx functions fixed this problem.
234 */
235static inline word SMC_inw(dword offset);
236static inline void SMC_outw(word value, dword offset);
237static inline byte SMC_inb(dword offset);
238static inline void SMC_outb(byte value, dword offset);
239static inline void SMC_insw(dword offset, volatile uchar* buf, dword len);
240static inline void SMC_outsw(dword offset, uchar* buf, dword len);
241
242#define barrier() __asm__ __volatile__("": : :"memory")
243
244static inline word SMC_inw(dword offset)
245{
246 word v;
247 v = *((volatile word*)(SMC_BASE_ADDRESS+offset));
248 barrier(); *(volatile u32*)(0xc0000000);
249 return v;
250}
251
252static inline void SMC_outw(word value, dword offset)
253{
254 *((volatile word*)(SMC_BASE_ADDRESS+offset)) = value;
255 barrier(); *(volatile u32*)(0xc0000000);
256}
257
258static inline byte SMC_inb(dword offset)
259{
260 word _w;
261
262 _w = SMC_inw(offset & ~((dword)1));
263 return (offset & 1) ? (byte)(_w >> 8) : (byte)(_w);
264}
265
266static inline void SMC_outb(byte value, dword offset)
267{
268 word _w;
269
270 _w = SMC_inw(offset & ~((dword)1));
271 if (offset & 1)
272 *((volatile word*)(SMC_BASE_ADDRESS+(offset & ~((dword)1)))) = (value<<8) | (_w & 0x00ff);
273 else
274 *((volatile word*)(SMC_BASE_ADDRESS+offset)) = value | (_w & 0xff00);
275}
276
277static inline void SMC_insw(dword offset, volatile uchar* buf, dword len)
278{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100279 volatile word *p = (volatile word *)buf;
280
wdenkc3c7f862004-06-09 14:47:54 +0000281 while (len-- > 0) {
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100282 *p++ = SMC_inw(offset);
283 barrier();
284 *((volatile u32*)(0xc0000000));
wdenkc3c7f862004-06-09 14:47:54 +0000285 }
286}
287
288static inline void SMC_outsw(dword offset, uchar* buf, dword len)
289{
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100290 volatile word *p = (volatile word *)buf;
291
wdenkc3c7f862004-06-09 14:47:54 +0000292 while (len-- > 0) {
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100293 SMC_outw(*p++, offset);
294 barrier();
295 *(volatile u32*)(0xc0000000);
wdenkc3c7f862004-06-09 14:47:54 +0000296 }
297}
298#endif /* CONFIG_SMC_USE_IOFUNCS */
299
wdenk8bf3b002003-12-06 23:20:41 +0000300static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
wdenkfe8c2802002-11-03 00:38:21 +0000301
302/*
303 * This function must be called before smc_open() if you want to override
304 * the default mac address.
305 */
306
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100307void smc_set_mac_addr(const unsigned char *addr) {
wdenkfe8c2802002-11-03 00:38:21 +0000308 int i;
309
310 for (i=0; i < sizeof(smc_mac_addr); i++){
311 smc_mac_addr[i] = addr[i];
312 }
313}
314
315/*
316 * smc_get_macaddr is no longer used. If you want to override the default
wdenk0b97ab12003-06-19 23:58:30 +0000317 * mac address, call smc_get_mac_addr as a part of the board initialization.
wdenkfe8c2802002-11-03 00:38:21 +0000318 */
319
320#if 0
321void smc_get_macaddr( byte *addr ) {
322 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
wdenk8bde7f72003-06-27 21:31:46 +0000323 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
wdenkfe8c2802002-11-03 00:38:21 +0000324 int i;
325
326
wdenk8bde7f72003-06-27 21:31:46 +0000327 for (i=0; i<6; i++) {
328 addr[0] = *(dnp1110_mac+0);
329 addr[1] = *(dnp1110_mac+1);
330 addr[2] = *(dnp1110_mac+2);
331 addr[3] = *(dnp1110_mac+3);
332 addr[4] = *(dnp1110_mac+4);
333 addr[5] = *(dnp1110_mac+5);
334 }
wdenkfe8c2802002-11-03 00:38:21 +0000335}
336#endif /* 0 */
337
338/***********************************************
wdenk42dfe7a2004-03-14 22:25:36 +0000339 * Show available memory *
wdenkfe8c2802002-11-03 00:38:21 +0000340 ***********************************************/
341void dump_memory_info(void)
342{
wdenk8bde7f72003-06-27 21:31:46 +0000343 word mem_info;
344 word old_bank;
wdenkfe8c2802002-11-03 00:38:21 +0000345
wdenk8bde7f72003-06-27 21:31:46 +0000346 old_bank = SMC_inw(BANK_SELECT)&0xF;
wdenkfe8c2802002-11-03 00:38:21 +0000347
wdenk8bde7f72003-06-27 21:31:46 +0000348 SMC_SELECT_BANK(0);
349 mem_info = SMC_inw( MIR_REG );
350 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
wdenkfe8c2802002-11-03 00:38:21 +0000351
wdenk8bde7f72003-06-27 21:31:46 +0000352 SMC_SELECT_BANK(old_bank);
wdenkfe8c2802002-11-03 00:38:21 +0000353}
354/*
355 . A rather simple routine to print out a packet for debugging purposes.
356*/
357#if SMC_DEBUG > 2
358static void print_packet( byte *, int );
359#endif
360
361#define tx_done(dev) 1
362
363
wdenkfe8c2802002-11-03 00:38:21 +0000364/* this does a soft reset on the device */
365static void smc_reset( void );
366
367/* Enable Interrupts, Receive, and Transmit */
368static void smc_enable( void );
369
370/* this puts the device in an inactive state */
371static void smc_shutdown( void );
372
373/* Routines to Read and Write the PHY Registers across the
374 MII Management Interface
375*/
376
377#ifndef CONFIG_SMC91111_EXT_PHY
378static word smc_read_phy_register(byte phyreg);
379static void smc_write_phy_register(byte phyreg, word phydata);
380#endif /* !CONFIG_SMC91111_EXT_PHY */
381
382
wdenkb56ddc62003-09-15 21:14:37 +0000383static int poll4int (byte mask, int timeout)
384{
385 int tmo = get_timer (0) + timeout * CFG_HZ;
386 int is_timeout = 0;
387 word old_bank = SMC_inw (BSR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000388
wdenkb56ddc62003-09-15 21:14:37 +0000389 PRINTK2 ("Polling...\n");
390 SMC_SELECT_BANK (2);
391 while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
392 if (get_timer (0) >= tmo) {
393 is_timeout = 1;
394 break;
395 }
wdenkfe8c2802002-11-03 00:38:21 +0000396 }
wdenkfe8c2802002-11-03 00:38:21 +0000397
wdenkb56ddc62003-09-15 21:14:37 +0000398 /* restore old bank selection */
399 SMC_SELECT_BANK (old_bank);
wdenkfe8c2802002-11-03 00:38:21 +0000400
wdenkb56ddc62003-09-15 21:14:37 +0000401 if (is_timeout)
402 return 1;
403 else
404 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000405}
406
wdenk487778b2003-06-06 11:20:01 +0000407/* Only one release command at a time, please */
wdenkb56ddc62003-09-15 21:14:37 +0000408static inline void smc_wait_mmu_release_complete (void)
wdenk487778b2003-06-06 11:20:01 +0000409{
410 int count = 0;
wdenkb56ddc62003-09-15 21:14:37 +0000411
wdenk487778b2003-06-06 11:20:01 +0000412 /* assume bank 2 selected */
wdenkb56ddc62003-09-15 21:14:37 +0000413 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
414 udelay (1); /* Wait until not busy */
415 if (++count > 200)
416 break;
wdenk487778b2003-06-06 11:20:01 +0000417 }
418}
419
wdenkfe8c2802002-11-03 00:38:21 +0000420/*
421 . Function: smc_reset( void )
422 . Purpose:
wdenk42dfe7a2004-03-14 22:25:36 +0000423 . This sets the SMC91111 chip to its normal state, hopefully from whatever
424 . mess that any other DOS driver has put it in.
wdenkfe8c2802002-11-03 00:38:21 +0000425 .
426 . Maybe I should reset more registers to defaults in here? SOFTRST should
427 . do that for me.
428 .
429 . Method:
430 . 1. send a SOFT RESET
431 . 2. wait for it to finish
432 . 3. enable autorelease mode
433 . 4. reset the memory management unit
434 . 5. clear all interrupts
435 .
436*/
wdenkb56ddc62003-09-15 21:14:37 +0000437static void smc_reset (void)
wdenkfe8c2802002-11-03 00:38:21 +0000438{
wdenkf39748a2004-06-09 13:37:52 +0000439 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000440
441 /* This resets the registers mostly to defaults, but doesn't
442 affect EEPROM. That seems unnecessary */
wdenkb56ddc62003-09-15 21:14:37 +0000443 SMC_SELECT_BANK (0);
444 SMC_outw (RCR_SOFTRST, RCR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000445
446 /* Setup the Configuration Register */
447 /* This is necessary because the CONFIG_REG is not affected */
448 /* by a soft reset */
449
wdenkb56ddc62003-09-15 21:14:37 +0000450 SMC_SELECT_BANK (1);
wdenkfe8c2802002-11-03 00:38:21 +0000451#if defined(CONFIG_SMC91111_EXT_PHY)
wdenkb56ddc62003-09-15 21:14:37 +0000452 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000453#else
wdenkb56ddc62003-09-15 21:14:37 +0000454 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000455#endif
456
457
458 /* Release from possible power-down state */
459 /* Configuration register is not affected by Soft Reset */
wdenkb56ddc62003-09-15 21:14:37 +0000460 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000461
wdenkb56ddc62003-09-15 21:14:37 +0000462 SMC_SELECT_BANK (0);
wdenkfe8c2802002-11-03 00:38:21 +0000463
464 /* this should pause enough for the chip to be happy */
wdenkb56ddc62003-09-15 21:14:37 +0000465 udelay (10);
wdenkfe8c2802002-11-03 00:38:21 +0000466
467 /* Disable transmit and receive functionality */
wdenkb56ddc62003-09-15 21:14:37 +0000468 SMC_outw (RCR_CLEAR, RCR_REG);
469 SMC_outw (TCR_CLEAR, TCR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000470
471 /* set the control register */
wdenkb56ddc62003-09-15 21:14:37 +0000472 SMC_SELECT_BANK (1);
473 SMC_outw (CTL_DEFAULT, CTL_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000474
475 /* Reset the MMU */
wdenkb56ddc62003-09-15 21:14:37 +0000476 SMC_SELECT_BANK (2);
477 smc_wait_mmu_release_complete ();
478 SMC_outw (MC_RESET, MMU_CMD_REG);
479 while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
480 udelay (1); /* Wait until not busy */
wdenkfe8c2802002-11-03 00:38:21 +0000481
482 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
483 but this is a place where future chipsets _COULD_ break. Be wary
wdenk8bde7f72003-06-27 21:31:46 +0000484 of issuing another MMU command right after this */
wdenkfe8c2802002-11-03 00:38:21 +0000485
486 /* Disable all interrupts */
wdenkb56ddc62003-09-15 21:14:37 +0000487 SMC_outb (0, IM_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000488}
489
490/*
491 . Function: smc_enable
492 . Purpose: let the chip talk to the outside work
493 . Method:
494 . 1. Enable the transmitter
495 . 2. Enable the receiver
496 . 3. Enable interrupts
497*/
498static void smc_enable()
499{
wdenkf39748a2004-06-09 13:37:52 +0000500 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000501 SMC_SELECT_BANK( 0 );
502 /* see the header file for options in TCR/RCR DEFAULT*/
503 SMC_outw( TCR_DEFAULT, TCR_REG );
504 SMC_outw( RCR_DEFAULT, RCR_REG );
505
506 /* clear MII_DIS */
507/* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
508}
509
510/*
511 . Function: smc_shutdown
512 . Purpose: closes down the SMC91xxx chip.
513 . Method:
514 . 1. zero the interrupt mask
515 . 2. clear the enable receive flag
516 . 3. clear the enable xmit flags
517 .
518 . TODO:
519 . (1) maybe utilize power down mode.
520 . Why not yet? Because while the chip will go into power down mode,
521 . the manual says that it will wake up in response to any I/O requests
wdenk42dfe7a2004-03-14 22:25:36 +0000522 . in the register space. Empirical results do not show this working.
wdenkfe8c2802002-11-03 00:38:21 +0000523*/
524static void smc_shutdown()
525{
wdenkf39748a2004-06-09 13:37:52 +0000526 PRINTK2(CARDNAME ": smc_shutdown\n");
wdenkfe8c2802002-11-03 00:38:21 +0000527
528 /* no more interrupts for me */
529 SMC_SELECT_BANK( 2 );
530 SMC_outb( 0, IM_REG );
531
532 /* and tell the card to stay away from that nasty outside world */
533 SMC_SELECT_BANK( 0 );
534 SMC_outb( RCR_CLEAR, RCR_REG );
535 SMC_outb( TCR_CLEAR, TCR_REG );
536}
537
538
539/*
540 . Function: smc_hardware_send_packet(struct net_device * )
541 . Purpose:
542 . This sends the actual packet to the SMC9xxx chip.
543 .
544 . Algorithm:
wdenk42dfe7a2004-03-14 22:25:36 +0000545 . First, see if a saved_skb is available.
wdenkfe8c2802002-11-03 00:38:21 +0000546 . ( this should NOT be called if there is no 'saved_skb'
547 . Now, find the packet number that the chip allocated
548 . Point the data pointers at it in memory
549 . Set the length word in the chip's memory
550 . Dump the packet to chip memory
551 . Check if a last byte is needed ( odd length packet )
552 . if so, set the control flag right
wdenk42dfe7a2004-03-14 22:25:36 +0000553 . Tell the card to send it
wdenkfe8c2802002-11-03 00:38:21 +0000554 . Enable the transmit interrupt, so I know if it failed
wdenk42dfe7a2004-03-14 22:25:36 +0000555 . Free the kernel data if I actually sent it.
wdenkfe8c2802002-11-03 00:38:21 +0000556*/
wdenkb56ddc62003-09-15 21:14:37 +0000557static int smc_send_packet (volatile void *packet, int packet_length)
wdenkfe8c2802002-11-03 00:38:21 +0000558{
wdenkb56ddc62003-09-15 21:14:37 +0000559 byte packet_no;
560 unsigned long ioaddr;
561 byte *buf;
562 int length;
563 int numPages;
564 int try = 0;
565 int time_out;
566 byte status;
wdenk518e2e12004-03-25 14:59:05 +0000567 byte saved_pnr;
568 word saved_ptr;
wdenkfe8c2802002-11-03 00:38:21 +0000569
wdenk518e2e12004-03-25 14:59:05 +0000570 /* save PTR and PNR registers before manipulation */
wdenkb79a11c2004-03-25 15:14:43 +0000571 SMC_SELECT_BANK (2);
wdenk518e2e12004-03-25 14:59:05 +0000572 saved_pnr = SMC_inb( PN_REG );
573 saved_ptr = SMC_inw( PTR_REG );
wdenkfe8c2802002-11-03 00:38:21 +0000574
wdenkf39748a2004-06-09 13:37:52 +0000575 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000576
577 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
578
579 /* allocate memory
wdenkb56ddc62003-09-15 21:14:37 +0000580 ** The MMU wants the number of pages to be the number of 256 bytes
581 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
582 **
583 ** The 91C111 ignores the size bits, but the code is left intact
584 ** for backwards and future compatibility.
585 **
586 ** Pkt size for allocating is data length +6 (for additional status
587 ** words, length and ctl!)
588 **
589 ** If odd size then last byte is included in this header.
590 */
591 numPages = ((length & 0xfffe) + 6);
592 numPages >>= 8; /* Divide by 256 */
wdenkfe8c2802002-11-03 00:38:21 +0000593
wdenkb56ddc62003-09-15 21:14:37 +0000594 if (numPages > 7) {
595 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000596 return 0;
597 }
598
599 /* now, try to allocate the memory */
wdenkb56ddc62003-09-15 21:14:37 +0000600 SMC_SELECT_BANK (2);
601 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000602
wdenkdc7c9a12003-03-26 06:55:25 +0000603 /* FIXME: the ALLOC_INT bit never gets set *
wdenk42dfe7a2004-03-14 22:25:36 +0000604 * so the following will always give a *
605 * memory allocation error. *
606 * same code works in armboot though *
wdenkdc7c9a12003-03-26 06:55:25 +0000607 * -ro
608 */
609
wdenkfe8c2802002-11-03 00:38:21 +0000610again:
611 try++;
612 time_out = MEMORY_WAIT_TIME;
613 do {
wdenkb56ddc62003-09-15 21:14:37 +0000614 status = SMC_inb (SMC91111_INT_REG);
615 if (status & IM_ALLOC_INT) {
wdenkfe8c2802002-11-03 00:38:21 +0000616 /* acknowledge the interrupt */
wdenkb56ddc62003-09-15 21:14:37 +0000617 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
wdenk8bde7f72003-06-27 21:31:46 +0000618 break;
wdenkfe8c2802002-11-03 00:38:21 +0000619 }
wdenkb56ddc62003-09-15 21:14:37 +0000620 } while (--time_out);
wdenkfe8c2802002-11-03 00:38:21 +0000621
wdenkb56ddc62003-09-15 21:14:37 +0000622 if (!time_out) {
623 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
624 SMC_DEV_NAME, try);
625 if (try < SMC_ALLOC_MAX_TRY)
626 goto again;
627 else
628 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000629 }
630
wdenkb56ddc62003-09-15 21:14:37 +0000631 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
632 SMC_DEV_NAME, try);
wdenkfe8c2802002-11-03 00:38:21 +0000633
634 /* I can send the packet now.. */
635
636 ioaddr = SMC_BASE_ADDRESS;
637
wdenkb56ddc62003-09-15 21:14:37 +0000638 buf = (byte *) packet;
wdenkfe8c2802002-11-03 00:38:21 +0000639
640 /* If I get here, I _know_ there is a packet slot waiting for me */
wdenkb56ddc62003-09-15 21:14:37 +0000641 packet_no = SMC_inb (AR_REG);
642 if (packet_no & AR_FAILED) {
wdenkfe8c2802002-11-03 00:38:21 +0000643 /* or isn't there? BAD CHIP! */
wdenkb56ddc62003-09-15 21:14:37 +0000644 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000645 return 0;
646 }
647
648 /* we have a packet address, so tell the card to use it */
wdenk1f6d4252004-11-02 13:00:33 +0000649#ifndef CONFIG_XAENIAX
wdenkb56ddc62003-09-15 21:14:37 +0000650 SMC_outb (packet_no, PN_REG);
wdenk1f6d4252004-11-02 13:00:33 +0000651#else
652 /* On Xaeniax board, we can't use SMC_outb here because that way
653 * the Allocate MMU command will end up written to the command register
654 * as well, which will lead to a problem.
655 */
656 SMC_outl (packet_no << 16, 0);
657#endif
wdenkb79a11c2004-03-25 15:14:43 +0000658 /* do not write new ptr value if Write data fifo not empty */
659 while ( saved_ptr & PTR_NOTEMPTY )
wdenk518e2e12004-03-25 14:59:05 +0000660 printf ("Write data fifo not empty!\n");
661
wdenkfe8c2802002-11-03 00:38:21 +0000662 /* point to the beginning of the packet */
wdenkb56ddc62003-09-15 21:14:37 +0000663 SMC_outw (PTR_AUTOINC, PTR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000664
wdenkb56ddc62003-09-15 21:14:37 +0000665 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
666 SMC_DEV_NAME, length);
wdenkfe8c2802002-11-03 00:38:21 +0000667
668#if SMC_DEBUG > 2
wdenkb56ddc62003-09-15 21:14:37 +0000669 printf ("Transmitting Packet\n");
670 print_packet (buf, length);
wdenkfe8c2802002-11-03 00:38:21 +0000671#endif
672
673 /* send the packet length ( +6 for status, length and ctl byte )
wdenk8bde7f72003-06-27 21:31:46 +0000674 and the status word ( set to zeros ) */
wdenkfe8c2802002-11-03 00:38:21 +0000675#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000676 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000677#else
wdenkb56ddc62003-09-15 21:14:37 +0000678 SMC_outw (0, SMC91111_DATA_REG);
679 /* send the packet length ( +6 for status words, length, and ctl */
680 SMC_outw ((length + 6), SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000681#endif
682
683 /* send the actual data
wdenkb56ddc62003-09-15 21:14:37 +0000684 . I _think_ it's faster to send the longs first, and then
685 . mop up by sending the last word. It depends heavily
wdenk42dfe7a2004-03-14 22:25:36 +0000686 . on alignment, at least on the 486. Maybe it would be
wdenkb56ddc62003-09-15 21:14:37 +0000687 . a good idea to check which is optimal? But that could take
688 . almost as much time as is saved?
689 */
wdenkfe8c2802002-11-03 00:38:21 +0000690#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000691 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
wdenkbb310d42004-11-22 22:20:07 +0000692#ifndef CONFIG_XAENIAX
wdenkb56ddc62003-09-15 21:14:37 +0000693 if (length & 0x2)
694 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
695 SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000696#else
wdenkbb310d42004-11-22 22:20:07 +0000697 /* On XANEIAX, we can only use 32-bit writes, so we need to handle
698 * unaligned tail part specially. The standard code doesn't work.
699 */
700 if ((length & 3) == 3) {
701 u16 * ptr = (u16*) &buf[length-3];
702 SMC_outl((*ptr) | ((0x2000 | buf[length-1]) << 16),
703 SMC91111_DATA_REG);
704 } else if ((length & 2) == 2) {
705 u16 * ptr = (u16*) &buf[length-2];
706 SMC_outl(*ptr, SMC91111_DATA_REG);
707 } else if (length & 1) {
708 SMC_outl((0x2000 | buf[length-1]), SMC91111_DATA_REG);
709 } else {
710 SMC_outl(0, SMC91111_DATA_REG);
711 }
712#endif
713#else
wdenkb56ddc62003-09-15 21:14:37 +0000714 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
wdenkfe8c2802002-11-03 00:38:21 +0000715#endif /* USE_32_BIT */
716
wdenkbb310d42004-11-22 22:20:07 +0000717#ifndef CONFIG_XAENIAX
wdenk42dfe7a2004-03-14 22:25:36 +0000718 /* Send the last byte, if there is one. */
wdenkb56ddc62003-09-15 21:14:37 +0000719 if ((length & 1) == 0) {
720 SMC_outw (0, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000721 } else {
wdenkb56ddc62003-09-15 21:14:37 +0000722 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000723 }
wdenkbb310d42004-11-22 22:20:07 +0000724#endif
wdenkfe8c2802002-11-03 00:38:21 +0000725
726 /* and let the chipset deal with it */
wdenkb56ddc62003-09-15 21:14:37 +0000727 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000728
729 /* poll for TX INT */
wdenk518e2e12004-03-25 14:59:05 +0000730 /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
731 /* poll for TX_EMPTY INT - autorelease enabled */
732 if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
wdenkfe8c2802002-11-03 00:38:21 +0000733 /* sending failed */
wdenkb56ddc62003-09-15 21:14:37 +0000734 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000735
736 /* release packet */
wdenk518e2e12004-03-25 14:59:05 +0000737 /* no need to release, MMU does that now */
wdenk1f6d4252004-11-02 13:00:33 +0000738#ifdef CONFIG_XAENIAX
739 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
740#endif
wdenkfe8c2802002-11-03 00:38:21 +0000741
wdenk8bde7f72003-06-27 21:31:46 +0000742 /* wait for MMU getting ready (low) */
wdenkb56ddc62003-09-15 21:14:37 +0000743 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
744 udelay (10);
wdenk8bde7f72003-06-27 21:31:46 +0000745 }
wdenkfe8c2802002-11-03 00:38:21 +0000746
wdenkb56ddc62003-09-15 21:14:37 +0000747 PRINTK2 ("MMU ready\n");
wdenkfe8c2802002-11-03 00:38:21 +0000748
749
750 return 0;
751 } else {
752 /* ack. int */
wdenk518e2e12004-03-25 14:59:05 +0000753 SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
754 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
wdenkb56ddc62003-09-15 21:14:37 +0000755 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
756 length);
wdenkfe8c2802002-11-03 00:38:21 +0000757
758 /* release packet */
wdenk518e2e12004-03-25 14:59:05 +0000759 /* no need to release, MMU does that now */
wdenk1f6d4252004-11-02 13:00:33 +0000760#ifdef CONFIG_XAENIAX
761 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
762#endif
wdenkfe8c2802002-11-03 00:38:21 +0000763
wdenk8bde7f72003-06-27 21:31:46 +0000764 /* wait for MMU getting ready (low) */
wdenkb56ddc62003-09-15 21:14:37 +0000765 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
766 udelay (10);
wdenk8bde7f72003-06-27 21:31:46 +0000767 }
wdenkfe8c2802002-11-03 00:38:21 +0000768
wdenkb56ddc62003-09-15 21:14:37 +0000769 PRINTK2 ("MMU ready\n");
wdenkfe8c2802002-11-03 00:38:21 +0000770
771
772 }
773
wdenk518e2e12004-03-25 14:59:05 +0000774 /* restore previously saved registers */
wdenk1f6d4252004-11-02 13:00:33 +0000775#ifndef CONFIG_XAENIAX
wdenk518e2e12004-03-25 14:59:05 +0000776 SMC_outb( saved_pnr, PN_REG );
wdenk1f6d4252004-11-02 13:00:33 +0000777#else
778 /* On Xaeniax board, we can't use SMC_outb here because that way
779 * the Allocate MMU command will end up written to the command register
780 * as well, which will lead to a problem.
781 */
782 SMC_outl(saved_pnr << 16, 0);
783#endif
wdenk518e2e12004-03-25 14:59:05 +0000784 SMC_outw( saved_ptr, PTR_REG );
785
wdenkfe8c2802002-11-03 00:38:21 +0000786 return length;
787}
788
789/*-------------------------------------------------------------------------
790 |
791 | smc_destructor( struct net_device * dev )
792 | Input parameters:
793 | dev, pointer to the device structure
794 |
795 | Output:
796 | None.
797 |
798 ---------------------------------------------------------------------------
799*/
800void smc_destructor()
801{
wdenkf39748a2004-06-09 13:37:52 +0000802 PRINTK2(CARDNAME ": smc_destructor\n");
wdenkfe8c2802002-11-03 00:38:21 +0000803}
804
805
806/*
807 * Open and Initialize the board
808 *
809 * Set up everything, reset the card, etc ..
810 *
811 */
wdenkb56ddc62003-09-15 21:14:37 +0000812static int smc_open (bd_t * bd)
wdenkfe8c2802002-11-03 00:38:21 +0000813{
wdenkb56ddc62003-09-15 21:14:37 +0000814 int i, err;
wdenkfe8c2802002-11-03 00:38:21 +0000815
wdenkf39748a2004-06-09 13:37:52 +0000816 PRINTK2 ("%s: smc_open\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000817
818 /* reset the hardware */
wdenkb56ddc62003-09-15 21:14:37 +0000819 smc_reset ();
820 smc_enable ();
wdenkfe8c2802002-11-03 00:38:21 +0000821
822 /* Configure the PHY */
823#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +0000824 smc_phy_configure ();
wdenkfe8c2802002-11-03 00:38:21 +0000825#endif
826
wdenkfe8c2802002-11-03 00:38:21 +0000827 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
828/* SMC_SELECT_BANK(0); */
829/* SMC_outw(0, RPC_REG); */
wdenkb56ddc62003-09-15 21:14:37 +0000830 SMC_SELECT_BANK (1);
wdenk8bde7f72003-06-27 21:31:46 +0000831
wdenkb56ddc62003-09-15 21:14:37 +0000832 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
833 if (err < 0) {
wdenk42dfe7a2004-03-14 22:25:36 +0000834 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
wdenkb56ddc62003-09-15 21:14:37 +0000835 return (-1); /* upper code ignores this, but NOT bi_enetaddr */
836 }
wdenkfe8c2802002-11-03 00:38:21 +0000837#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000838 for (i = 0; i < 6; i += 2) {
wdenkfe8c2802002-11-03 00:38:21 +0000839 word address;
840
wdenkb56ddc62003-09-15 21:14:37 +0000841 address = smc_mac_addr[i + 1] << 8;
842 address |= smc_mac_addr[i];
wdenk39539882004-07-01 16:30:44 +0000843 SMC_outw (address, (ADDR0_REG + i));
wdenkfe8c2802002-11-03 00:38:21 +0000844 }
845#else
wdenkb56ddc62003-09-15 21:14:37 +0000846 for (i = 0; i < 6; i++)
wdenk39539882004-07-01 16:30:44 +0000847 SMC_outb (smc_mac_addr[i], (ADDR0_REG + i));
wdenkfe8c2802002-11-03 00:38:21 +0000848#endif
849
850 return 0;
851}
852
wdenkfe8c2802002-11-03 00:38:21 +0000853/*-------------------------------------------------------------
854 .
855 . smc_rcv - receive a packet from the card
856 .
857 . There is ( at least ) a packet waiting to be read from
858 . chip-memory.
859 .
860 . o Read the status
861 . o If an error, record it
862 . o otherwise, read in the packet
863 --------------------------------------------------------------
864*/
865static int smc_rcv()
866{
wdenk42dfe7a2004-03-14 22:25:36 +0000867 int packet_number;
wdenkfe8c2802002-11-03 00:38:21 +0000868 word status;
869 word packet_length;
wdenk42dfe7a2004-03-14 22:25:36 +0000870 int is_error = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000871#ifdef USE_32_BIT
872 dword stat_len;
873#endif
wdenk518e2e12004-03-25 14:59:05 +0000874 byte saved_pnr;
875 word saved_ptr;
wdenkfe8c2802002-11-03 00:38:21 +0000876
wdenkfe8c2802002-11-03 00:38:21 +0000877 SMC_SELECT_BANK(2);
wdenk518e2e12004-03-25 14:59:05 +0000878 /* save PTR and PTR registers */
879 saved_pnr = SMC_inb( PN_REG );
880 saved_ptr = SMC_inw( PTR_REG );
881
wdenkfe8c2802002-11-03 00:38:21 +0000882 packet_number = SMC_inw( RXFIFO_REG );
883
884 if ( packet_number & RXFIFO_REMPTY ) {
885
886 return 0;
887 }
888
wdenkf39748a2004-06-09 13:37:52 +0000889 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000890 /* start reading from the start of the packet */
891 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
892
893 /* First two words are status and packet_length */
894#ifdef USE_32_BIT
895 stat_len = SMC_inl(SMC91111_DATA_REG);
896 status = stat_len & 0xffff;
897 packet_length = stat_len >> 16;
898#else
wdenk42dfe7a2004-03-14 22:25:36 +0000899 status = SMC_inw( SMC91111_DATA_REG );
900 packet_length = SMC_inw( SMC91111_DATA_REG );
wdenkfe8c2802002-11-03 00:38:21 +0000901#endif
902
903 packet_length &= 0x07ff; /* mask off top bits */
904
905 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
906
907 if ( !(status & RS_ERRORS ) ){
908 /* Adjust for having already read the first two words */
909 packet_length -= 4; /*4; */
910
911
wdenkfe8c2802002-11-03 00:38:21 +0000912 /* set odd length for bug in LAN91C111, */
913 /* which never sets RS_ODDFRAME */
914 /* TODO ? */
915
916
917#ifdef USE_32_BIT
918 PRINTK3(" Reading %d dwords (and %d bytes) \n",
919 packet_length >> 2, packet_length & 3 );
920 /* QUESTION: Like in the TX routine, do I want
921 to send the DWORDs or the bytes first, or some
922 mixture. A mixture might improve already slow PIO
wdenk42dfe7a2004-03-14 22:25:36 +0000923 performance */
wdenkfe8c2802002-11-03 00:38:21 +0000924 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
925 /* read the left over bytes */
926 if (packet_length & 3) {
927 int i;
928
wdenk699b13a2002-11-03 18:03:52 +0000929 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
wdenkfe8c2802002-11-03 00:38:21 +0000930 dword leftover = SMC_inl(SMC91111_DATA_REG);
931 for (i=0; i<(packet_length & 3); i++)
932 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
933 }
934#else
935 PRINTK3(" Reading %d words and %d byte(s) \n",
936 (packet_length >> 1 ), packet_length & 1 );
937 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
938
939#endif /* USE_32_BIT */
940
941#if SMC_DEBUG > 2
942 printf("Receiving Packet\n");
943 print_packet( NetRxPackets[0], packet_length );
944#endif
945 } else {
946 /* error ... */
947 /* TODO ? */
948 is_error = 1;
949 }
950
951 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
952 udelay(1); /* Wait until not busy */
953
954 /* error or good, tell the card to get rid of this packet */
955 SMC_outw( MC_RELEASE, MMU_CMD_REG );
956
957 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
958 udelay(1); /* Wait until not busy */
959
wdenk518e2e12004-03-25 14:59:05 +0000960 /* restore saved registers */
wdenk1f6d4252004-11-02 13:00:33 +0000961#ifndef CONFIG_XAENIAX
wdenk518e2e12004-03-25 14:59:05 +0000962 SMC_outb( saved_pnr, PN_REG );
wdenk1f6d4252004-11-02 13:00:33 +0000963#else
964 /* On Xaeniax board, we can't use SMC_outb here because that way
965 * the Allocate MMU command will end up written to the command register
966 * as well, which will lead to a problem.
967 */
968 SMC_outl( saved_pnr << 16, 0);
969#endif
wdenk518e2e12004-03-25 14:59:05 +0000970 SMC_outw( saved_ptr, PTR_REG );
971
wdenkfe8c2802002-11-03 00:38:21 +0000972 if (!is_error) {
973 /* Pass the packet up to the protocol layers. */
974 NetReceive(NetRxPackets[0], packet_length);
975 return packet_length;
976 } else {
977 return 0;
978 }
979
980}
981
982
wdenkfe8c2802002-11-03 00:38:21 +0000983/*----------------------------------------------------
984 . smc_close
985 .
986 . this makes the board clean up everything that it can
wdenk42dfe7a2004-03-14 22:25:36 +0000987 . and not talk to the outside world. Caused by
wdenkfe8c2802002-11-03 00:38:21 +0000988 . an 'ifconfig ethX down'
989 .
990 -----------------------------------------------------*/
991static int smc_close()
992{
wdenkf39748a2004-06-09 13:37:52 +0000993 PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000994
995 /* clear everything */
996 smc_shutdown();
997
998 return 0;
999}
1000
1001
1002#if 0
1003/*------------------------------------------------------------
1004 . Modify a bit in the LAN91C111 register set
1005 .-------------------------------------------------------------*/
1006static word smc_modify_regbit(int bank, int ioaddr, int reg,
1007 unsigned int bit, int val)
1008{
1009 word regval;
1010
1011 SMC_SELECT_BANK( bank );
1012
1013 regval = SMC_inw( reg );
1014 if (val)
1015 regval |= bit;
1016 else
1017 regval &= ~bit;
1018
1019 SMC_outw( regval, 0 );
1020 return(regval);
1021}
1022
1023
1024/*------------------------------------------------------------
1025 . Retrieve a bit in the LAN91C111 register set
1026 .-------------------------------------------------------------*/
1027static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
1028{
1029 SMC_SELECT_BANK( bank );
1030 if ( SMC_inw( reg ) & bit)
1031 return(1);
1032 else
1033 return(0);
1034}
1035
1036
1037/*------------------------------------------------------------
1038 . Modify a LAN91C111 register (word access only)
1039 .-------------------------------------------------------------*/
1040static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
1041{
1042 SMC_SELECT_BANK( bank );
1043 SMC_outw( val, reg );
1044}
1045
1046
1047/*------------------------------------------------------------
1048 . Retrieve a LAN91C111 register (word access only)
1049 .-------------------------------------------------------------*/
1050static int smc_get_reg(int bank, int ioaddr, int reg)
1051{
1052 SMC_SELECT_BANK( bank );
1053 return(SMC_inw( reg ));
1054}
1055
1056#endif /* 0 */
1057
1058/*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
1059
1060#if (SMC_DEBUG > 2 )
1061
1062/*------------------------------------------------------------
1063 . Debugging function for viewing MII Management serial bitstream
1064 .-------------------------------------------------------------*/
wdenkb56ddc62003-09-15 21:14:37 +00001065static void smc_dump_mii_stream (byte * bits, int size)
wdenkfe8c2802002-11-03 00:38:21 +00001066{
1067 int i;
1068
wdenkb56ddc62003-09-15 21:14:37 +00001069 printf ("BIT#:");
1070 for (i = 0; i < size; ++i) {
1071 printf ("%d", i % 10);
1072 }
wdenkfe8c2802002-11-03 00:38:21 +00001073
wdenkb56ddc62003-09-15 21:14:37 +00001074 printf ("\nMDOE:");
1075 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001076 if (bits[i] & MII_MDOE)
wdenkb56ddc62003-09-15 21:14:37 +00001077 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001078 else
wdenkb56ddc62003-09-15 21:14:37 +00001079 printf ("0");
1080 }
wdenkfe8c2802002-11-03 00:38:21 +00001081
wdenkb56ddc62003-09-15 21:14:37 +00001082 printf ("\nMDO :");
1083 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001084 if (bits[i] & MII_MDO)
wdenkb56ddc62003-09-15 21:14:37 +00001085 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001086 else
wdenkb56ddc62003-09-15 21:14:37 +00001087 printf ("0");
1088 }
wdenkfe8c2802002-11-03 00:38:21 +00001089
wdenkb56ddc62003-09-15 21:14:37 +00001090 printf ("\nMDI :");
1091 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001092 if (bits[i] & MII_MDI)
wdenkb56ddc62003-09-15 21:14:37 +00001093 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001094 else
wdenkb56ddc62003-09-15 21:14:37 +00001095 printf ("0");
1096 }
wdenkfe8c2802002-11-03 00:38:21 +00001097
wdenkb56ddc62003-09-15 21:14:37 +00001098 printf ("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001099}
1100#endif
1101
1102/*------------------------------------------------------------
1103 . Reads a register from the MII Management serial interface
1104 .-------------------------------------------------------------*/
1105#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +00001106static word smc_read_phy_register (byte phyreg)
wdenkfe8c2802002-11-03 00:38:21 +00001107{
1108 int oldBank;
1109 int i;
1110 byte mask;
1111 word mii_reg;
1112 byte bits[64];
1113 int clk_idx = 0;
1114 int input_idx;
1115 word phydata;
1116 byte phyaddr = SMC_PHY_ADDR;
1117
1118 /* 32 consecutive ones on MDO to establish sync */
1119 for (i = 0; i < 32; ++i)
1120 bits[clk_idx++] = MII_MDOE | MII_MDO;
1121
1122 /* Start code <01> */
1123 bits[clk_idx++] = MII_MDOE;
1124 bits[clk_idx++] = MII_MDOE | MII_MDO;
1125
1126 /* Read command <10> */
1127 bits[clk_idx++] = MII_MDOE | MII_MDO;
1128 bits[clk_idx++] = MII_MDOE;
1129
1130 /* Output the PHY address, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001131 mask = (byte) 0x10;
1132 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001133 if (phyaddr & mask)
1134 bits[clk_idx++] = MII_MDOE | MII_MDO;
1135 else
1136 bits[clk_idx++] = MII_MDOE;
1137
1138 /* Shift to next lowest bit */
1139 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001140 }
wdenkfe8c2802002-11-03 00:38:21 +00001141
1142 /* Output the phy register number, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001143 mask = (byte) 0x10;
1144 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001145 if (phyreg & mask)
1146 bits[clk_idx++] = MII_MDOE | MII_MDO;
1147 else
1148 bits[clk_idx++] = MII_MDOE;
1149
1150 /* Shift to next lowest bit */
1151 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001152 }
wdenkfe8c2802002-11-03 00:38:21 +00001153
1154 /* Tristate and turnaround (2 bit times) */
1155 bits[clk_idx++] = 0;
1156 /*bits[clk_idx++] = 0; */
1157
1158 /* Input starts at this bit time */
1159 input_idx = clk_idx;
1160
1161 /* Will input 16 bits */
1162 for (i = 0; i < 16; ++i)
1163 bits[clk_idx++] = 0;
1164
1165 /* Final clock bit */
1166 bits[clk_idx++] = 0;
1167
1168 /* Save the current bank */
wdenkb56ddc62003-09-15 21:14:37 +00001169 oldBank = SMC_inw (BANK_SELECT);
wdenkfe8c2802002-11-03 00:38:21 +00001170
1171 /* Select bank 3 */
wdenkb56ddc62003-09-15 21:14:37 +00001172 SMC_SELECT_BANK (3);
wdenkfe8c2802002-11-03 00:38:21 +00001173
1174 /* Get the current MII register value */
wdenkb56ddc62003-09-15 21:14:37 +00001175 mii_reg = SMC_inw (MII_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001176
1177 /* Turn off all MII Interface bits */
wdenkb56ddc62003-09-15 21:14:37 +00001178 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
wdenkfe8c2802002-11-03 00:38:21 +00001179
1180 /* Clock all 64 cycles */
wdenkb56ddc62003-09-15 21:14:37 +00001181 for (i = 0; i < sizeof bits; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001182 /* Clock Low - output data */
wdenkb56ddc62003-09-15 21:14:37 +00001183 SMC_outw (mii_reg | bits[i], MII_REG);
1184 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001185
1186
1187 /* Clock Hi - input data */
wdenkb56ddc62003-09-15 21:14:37 +00001188 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1189 udelay (SMC_PHY_CLOCK_DELAY);
1190 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1191 }
wdenkfe8c2802002-11-03 00:38:21 +00001192
1193 /* Return to idle state */
1194 /* Set clock to low, data to low, and output tristated */
wdenkb56ddc62003-09-15 21:14:37 +00001195 SMC_outw (mii_reg, MII_REG);
1196 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001197
1198 /* Restore original bank select */
wdenkb56ddc62003-09-15 21:14:37 +00001199 SMC_SELECT_BANK (oldBank);
wdenkfe8c2802002-11-03 00:38:21 +00001200
1201 /* Recover input data */
1202 phydata = 0;
wdenkb56ddc62003-09-15 21:14:37 +00001203 for (i = 0; i < 16; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001204 phydata <<= 1;
1205
1206 if (bits[input_idx++] & MII_MDI)
1207 phydata |= 0x0001;
wdenkb56ddc62003-09-15 21:14:37 +00001208 }
wdenkfe8c2802002-11-03 00:38:21 +00001209
1210#if (SMC_DEBUG > 2 )
wdenkb56ddc62003-09-15 21:14:37 +00001211 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
wdenkfe8c2802002-11-03 00:38:21 +00001212 phyaddr, phyreg, phydata);
wdenkb56ddc62003-09-15 21:14:37 +00001213 smc_dump_mii_stream (bits, sizeof bits);
wdenkfe8c2802002-11-03 00:38:21 +00001214#endif
1215
wdenkb56ddc62003-09-15 21:14:37 +00001216 return (phydata);
wdenkfe8c2802002-11-03 00:38:21 +00001217}
1218
1219
1220/*------------------------------------------------------------
1221 . Writes a register to the MII Management serial interface
1222 .-------------------------------------------------------------*/
wdenkb56ddc62003-09-15 21:14:37 +00001223static void smc_write_phy_register (byte phyreg, word phydata)
wdenkfe8c2802002-11-03 00:38:21 +00001224{
1225 int oldBank;
1226 int i;
1227 word mask;
1228 word mii_reg;
1229 byte bits[65];
1230 int clk_idx = 0;
1231 byte phyaddr = SMC_PHY_ADDR;
1232
1233 /* 32 consecutive ones on MDO to establish sync */
1234 for (i = 0; i < 32; ++i)
1235 bits[clk_idx++] = MII_MDOE | MII_MDO;
1236
1237 /* Start code <01> */
1238 bits[clk_idx++] = MII_MDOE;
1239 bits[clk_idx++] = MII_MDOE | MII_MDO;
1240
1241 /* Write command <01> */
1242 bits[clk_idx++] = MII_MDOE;
1243 bits[clk_idx++] = MII_MDOE | MII_MDO;
1244
1245 /* Output the PHY address, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001246 mask = (byte) 0x10;
1247 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001248 if (phyaddr & mask)
1249 bits[clk_idx++] = MII_MDOE | MII_MDO;
1250 else
1251 bits[clk_idx++] = MII_MDOE;
1252
1253 /* Shift to next lowest bit */
1254 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001255 }
wdenkfe8c2802002-11-03 00:38:21 +00001256
1257 /* Output the phy register number, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001258 mask = (byte) 0x10;
1259 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001260 if (phyreg & mask)
1261 bits[clk_idx++] = MII_MDOE | MII_MDO;
1262 else
1263 bits[clk_idx++] = MII_MDOE;
1264
1265 /* Shift to next lowest bit */
1266 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001267 }
wdenkfe8c2802002-11-03 00:38:21 +00001268
1269 /* Tristate and turnaround (2 bit times) */
1270 bits[clk_idx++] = 0;
1271 bits[clk_idx++] = 0;
1272
1273 /* Write out 16 bits of data, msb first */
1274 mask = 0x8000;
wdenkb56ddc62003-09-15 21:14:37 +00001275 for (i = 0; i < 16; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001276 if (phydata & mask)
1277 bits[clk_idx++] = MII_MDOE | MII_MDO;
1278 else
1279 bits[clk_idx++] = MII_MDOE;
1280
1281 /* Shift to next lowest bit */
1282 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001283 }
wdenkfe8c2802002-11-03 00:38:21 +00001284
1285 /* Final clock bit (tristate) */
1286 bits[clk_idx++] = 0;
1287
1288 /* Save the current bank */
wdenkb56ddc62003-09-15 21:14:37 +00001289 oldBank = SMC_inw (BANK_SELECT);
wdenkfe8c2802002-11-03 00:38:21 +00001290
1291 /* Select bank 3 */
wdenkb56ddc62003-09-15 21:14:37 +00001292 SMC_SELECT_BANK (3);
wdenkfe8c2802002-11-03 00:38:21 +00001293
1294 /* Get the current MII register value */
wdenkb56ddc62003-09-15 21:14:37 +00001295 mii_reg = SMC_inw (MII_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001296
1297 /* Turn off all MII Interface bits */
wdenkb56ddc62003-09-15 21:14:37 +00001298 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
wdenkfe8c2802002-11-03 00:38:21 +00001299
1300 /* Clock all cycles */
wdenkb56ddc62003-09-15 21:14:37 +00001301 for (i = 0; i < sizeof bits; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001302 /* Clock Low - output data */
wdenkb56ddc62003-09-15 21:14:37 +00001303 SMC_outw (mii_reg | bits[i], MII_REG);
1304 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001305
1306
1307 /* Clock Hi - input data */
wdenkb56ddc62003-09-15 21:14:37 +00001308 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1309 udelay (SMC_PHY_CLOCK_DELAY);
1310 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1311 }
wdenkfe8c2802002-11-03 00:38:21 +00001312
1313 /* Return to idle state */
1314 /* Set clock to low, data to low, and output tristated */
wdenkb56ddc62003-09-15 21:14:37 +00001315 SMC_outw (mii_reg, MII_REG);
1316 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001317
1318 /* Restore original bank select */
wdenkb56ddc62003-09-15 21:14:37 +00001319 SMC_SELECT_BANK (oldBank);
wdenkfe8c2802002-11-03 00:38:21 +00001320
1321#if (SMC_DEBUG > 2 )
wdenkb56ddc62003-09-15 21:14:37 +00001322 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
wdenkfe8c2802002-11-03 00:38:21 +00001323 phyaddr, phyreg, phydata);
wdenkb56ddc62003-09-15 21:14:37 +00001324 smc_dump_mii_stream (bits, sizeof bits);
wdenkfe8c2802002-11-03 00:38:21 +00001325#endif
1326}
1327#endif /* !CONFIG_SMC91111_EXT_PHY */
1328
1329
wdenkfe8c2802002-11-03 00:38:21 +00001330/*------------------------------------------------------------
1331 . Waits the specified number of milliseconds - kernel friendly
1332 .-------------------------------------------------------------*/
1333#ifndef CONFIG_SMC91111_EXT_PHY
1334static void smc_wait_ms(unsigned int ms)
1335{
1336 udelay(ms*1000);
1337}
1338#endif /* !CONFIG_SMC91111_EXT_PHY */
1339
1340
wdenkfe8c2802002-11-03 00:38:21 +00001341/*------------------------------------------------------------
1342 . Configures the specified PHY using Autonegotiation. Calls
1343 . smc_phy_fixed() if the user has requested a certain config.
1344 .-------------------------------------------------------------*/
1345#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +00001346static void smc_phy_configure ()
wdenkfe8c2802002-11-03 00:38:21 +00001347{
1348 int timeout;
1349 byte phyaddr;
wdenkb56ddc62003-09-15 21:14:37 +00001350 word my_phy_caps; /* My PHY capabilities */
1351 word my_ad_caps; /* My Advertised capabilities */
1352 word status = 0; /*;my status = 0 */
wdenkfe8c2802002-11-03 00:38:21 +00001353 int failed = 0;
1354
wdenkf39748a2004-06-09 13:37:52 +00001355 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001356
1357
wdenkfe8c2802002-11-03 00:38:21 +00001358 /* Get the detected phy address */
1359 phyaddr = SMC_PHY_ADDR;
1360
1361 /* Reset the PHY, setting all other bits to zero */
wdenkb56ddc62003-09-15 21:14:37 +00001362 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
wdenkfe8c2802002-11-03 00:38:21 +00001363
1364 /* Wait for the reset to complete, or time out */
wdenkb56ddc62003-09-15 21:14:37 +00001365 timeout = 6; /* Wait up to 3 seconds */
1366 while (timeout--) {
1367 if (!(smc_read_phy_register (PHY_CNTL_REG)
1368 & PHY_CNTL_RST)) {
wdenkfe8c2802002-11-03 00:38:21 +00001369 /* reset complete */
1370 break;
wdenkfe8c2802002-11-03 00:38:21 +00001371 }
1372
wdenkb56ddc62003-09-15 21:14:37 +00001373 smc_wait_ms (500); /* wait 500 millisecs */
1374 }
1375
1376 if (timeout < 1) {
1377 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001378 goto smc_phy_configure_exit;
wdenkb56ddc62003-09-15 21:14:37 +00001379 }
wdenkfe8c2802002-11-03 00:38:21 +00001380
1381 /* Read PHY Register 18, Status Output */
1382 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1383
1384 /* Enable PHY Interrupts (for register 18) */
1385 /* Interrupts listed here are disabled */
wdenk8bf3b002003-12-06 23:20:41 +00001386 smc_write_phy_register (PHY_MASK_REG, 0xffff);
wdenkfe8c2802002-11-03 00:38:21 +00001387
1388 /* Configure the Receive/Phy Control register */
wdenkb56ddc62003-09-15 21:14:37 +00001389 SMC_SELECT_BANK (0);
1390 SMC_outw (RPC_DEFAULT, RPC_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001391
1392 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
wdenkb56ddc62003-09-15 21:14:37 +00001393 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1394 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
wdenkfe8c2802002-11-03 00:38:21 +00001395
1396 if (my_phy_caps & PHY_STAT_CAP_T4)
1397 my_ad_caps |= PHY_AD_T4;
1398
1399 if (my_phy_caps & PHY_STAT_CAP_TXF)
1400 my_ad_caps |= PHY_AD_TX_FDX;
1401
1402 if (my_phy_caps & PHY_STAT_CAP_TXH)
1403 my_ad_caps |= PHY_AD_TX_HDX;
1404
1405 if (my_phy_caps & PHY_STAT_CAP_TF)
1406 my_ad_caps |= PHY_AD_10_FDX;
1407
1408 if (my_phy_caps & PHY_STAT_CAP_TH)
1409 my_ad_caps |= PHY_AD_10_HDX;
1410
1411 /* Update our Auto-Neg Advertisement Register */
wdenkb56ddc62003-09-15 21:14:37 +00001412 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
wdenkfe8c2802002-11-03 00:38:21 +00001413
wdenk518e2e12004-03-25 14:59:05 +00001414 /* Read the register back. Without this, it appears that when */
1415 /* auto-negotiation is restarted, sometimes it isn't ready and */
1416 /* the link does not come up. */
1417 smc_read_phy_register(PHY_AD_REG);
1418
wdenkf39748a2004-06-09 13:37:52 +00001419 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1420 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
wdenkfe8c2802002-11-03 00:38:21 +00001421
1422 /* Restart auto-negotiation process in order to advertise my caps */
wdenkb56ddc62003-09-15 21:14:37 +00001423 smc_write_phy_register (PHY_CNTL_REG,
1424 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
wdenkfe8c2802002-11-03 00:38:21 +00001425
1426 /* Wait for the auto-negotiation to complete. This may take from */
1427 /* 2 to 3 seconds. */
1428 /* Wait for the reset to complete, or time out */
wdenkf39748a2004-06-09 13:37:52 +00001429 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
wdenkb56ddc62003-09-15 21:14:37 +00001430 while (timeout--) {
wdenkf39748a2004-06-09 13:37:52 +00001431
wdenkb56ddc62003-09-15 21:14:37 +00001432 status = smc_read_phy_register (PHY_STAT_REG);
1433 if (status & PHY_STAT_ANEG_ACK) {
wdenkfe8c2802002-11-03 00:38:21 +00001434 /* auto-negotiate complete */
1435 break;
wdenkb56ddc62003-09-15 21:14:37 +00001436 }
wdenkfe8c2802002-11-03 00:38:21 +00001437
wdenkb56ddc62003-09-15 21:14:37 +00001438 smc_wait_ms (500); /* wait 500 millisecs */
wdenkfe8c2802002-11-03 00:38:21 +00001439
1440 /* Restart auto-negotiation if remote fault */
wdenkb56ddc62003-09-15 21:14:37 +00001441 if (status & PHY_STAT_REM_FLT) {
wdenkf39748a2004-06-09 13:37:52 +00001442 printf ("%s: PHY remote fault detected\n",
wdenkb56ddc62003-09-15 21:14:37 +00001443 SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001444
1445 /* Restart auto-negotiation */
wdenkf39748a2004-06-09 13:37:52 +00001446 printf ("%s: PHY restarting auto-negotiation\n",
wdenkfe8c2802002-11-03 00:38:21 +00001447 SMC_DEV_NAME);
wdenkb56ddc62003-09-15 21:14:37 +00001448 smc_write_phy_register (PHY_CNTL_REG,
1449 PHY_CNTL_ANEG_EN |
1450 PHY_CNTL_ANEG_RST |
1451 PHY_CNTL_SPEED |
1452 PHY_CNTL_DPLX);
wdenkfe8c2802002-11-03 00:38:21 +00001453 }
wdenkb56ddc62003-09-15 21:14:37 +00001454 }
wdenkfe8c2802002-11-03 00:38:21 +00001455
wdenkb56ddc62003-09-15 21:14:37 +00001456 if (timeout < 1) {
wdenkf39748a2004-06-09 13:37:52 +00001457 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001458 failed = 1;
wdenkb56ddc62003-09-15 21:14:37 +00001459 }
wdenkfe8c2802002-11-03 00:38:21 +00001460
1461 /* Fail if we detected an auto-negotiate remote fault */
wdenkb56ddc62003-09-15 21:14:37 +00001462 if (status & PHY_STAT_REM_FLT) {
wdenkf39748a2004-06-09 13:37:52 +00001463 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001464 failed = 1;
wdenkb56ddc62003-09-15 21:14:37 +00001465 }
wdenkfe8c2802002-11-03 00:38:21 +00001466
1467 /* Re-Configure the Receive/Phy Control register */
wdenkb56ddc62003-09-15 21:14:37 +00001468 SMC_outw (RPC_DEFAULT, RPC_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001469
wdenk26238132004-07-09 22:51:01 +00001470smc_phy_configure_exit: ;
wdenkfe8c2802002-11-03 00:38:21 +00001471
1472}
1473#endif /* !CONFIG_SMC91111_EXT_PHY */
1474
1475
1476#if SMC_DEBUG > 2
1477static void print_packet( byte * buf, int length )
1478{
wdenk8bde7f72003-06-27 21:31:46 +00001479 int i;
1480 int remainder;
1481 int lines;
wdenkfe8c2802002-11-03 00:38:21 +00001482
wdenk8bde7f72003-06-27 21:31:46 +00001483 printf("Packet of length %d \n", length );
wdenkfe8c2802002-11-03 00:38:21 +00001484
1485#if SMC_DEBUG > 3
wdenk8bde7f72003-06-27 21:31:46 +00001486 lines = length / 16;
1487 remainder = length % 16;
wdenkfe8c2802002-11-03 00:38:21 +00001488
wdenk8bde7f72003-06-27 21:31:46 +00001489 for ( i = 0; i < lines ; i ++ ) {
1490 int cur;
wdenkfe8c2802002-11-03 00:38:21 +00001491
wdenk8bde7f72003-06-27 21:31:46 +00001492 for ( cur = 0; cur < 8; cur ++ ) {
1493 byte a, b;
wdenkfe8c2802002-11-03 00:38:21 +00001494
wdenk8bde7f72003-06-27 21:31:46 +00001495 a = *(buf ++ );
1496 b = *(buf ++ );
1497 printf("%02x%02x ", a, b );
1498 }
1499 printf("\n");
1500 }
1501 for ( i = 0; i < remainder/2 ; i++ ) {
1502 byte a, b;
wdenkfe8c2802002-11-03 00:38:21 +00001503
wdenk8bde7f72003-06-27 21:31:46 +00001504 a = *(buf ++ );
1505 b = *(buf ++ );
1506 printf("%02x%02x ", a, b );
1507 }
1508 printf("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001509#endif
wdenkfe8c2802002-11-03 00:38:21 +00001510}
1511#endif
1512
1513int eth_init(bd_t *bd) {
wdenk0b97ab12003-06-19 23:58:30 +00001514 return (smc_open(bd));
wdenkfe8c2802002-11-03 00:38:21 +00001515}
1516
1517void eth_halt() {
1518 smc_close();
1519}
1520
1521int eth_rx() {
1522 return smc_rcv();
1523}
1524
1525int eth_send(volatile void *packet, int length) {
1526 return smc_send_packet(packet, length);
1527}
1528
wdenkb56ddc62003-09-15 21:14:37 +00001529int smc_get_ethaddr (bd_t * bd)
wdenk0b97ab12003-06-19 23:58:30 +00001530{
wdenkb56ddc62003-09-15 21:14:37 +00001531 int env_size, rom_valid, env_present = 0, reg;
1532 char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001533 char s_env_mac[64];
1534 uchar v_env_mac[6], v_rom_mac[6];
wdenk0b97ab12003-06-19 23:58:30 +00001535
wdenkb56ddc62003-09-15 21:14:37 +00001536 env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
1537 if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
1538 printf ("\n*** ERROR: ethaddr is not set properly!!\n");
1539 return (-1);
wdenk8bde7f72003-06-27 21:31:46 +00001540 }
wdenk8bde7f72003-06-27 21:31:46 +00001541
wdenkb56ddc62003-09-15 21:14:37 +00001542 if (env_size > 0) {
1543 env_present = 1;
1544 s = s_env_mac;
wdenk8bde7f72003-06-27 21:31:46 +00001545 }
wdenkb56ddc62003-09-15 21:14:37 +00001546
wdenk42dfe7a2004-03-14 22:25:36 +00001547 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
wdenkb56ddc62003-09-15 21:14:37 +00001548 v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
1549 if (s)
1550 s = (*e) ? e + 1 : e;
1551 }
1552
1553 rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
1554
1555 if (!env_present) { /* if NO env */
1556 if (rom_valid) { /* but ROM is valid */
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001557 v_mac = (char *)v_rom_mac;
wdenkb56ddc62003-09-15 21:14:37 +00001558 sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
1559 v_mac[0], v_mac[1], v_mac[2], v_mac[3],
1560 v_mac[4], v_mac[5]);
1561 setenv ("ethaddr", s_env_mac);
1562 } else { /* no env, bad ROM */
1563 printf ("\n*** ERROR: ethaddr is NOT set !!\n");
1564 return (-1);
1565 }
1566 } else { /* good env, don't care ROM */
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001567 v_mac = (char *)v_env_mac; /* always use a good env over a ROM */
wdenkb56ddc62003-09-15 21:14:37 +00001568 }
1569
wdenk42dfe7a2004-03-14 22:25:36 +00001570 if (env_present && rom_valid) { /* if both env and ROM are good */
wdenkb56ddc62003-09-15 21:14:37 +00001571 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
wdenkb56ddc62003-09-15 21:14:37 +00001572 printf ("\nWarning: MAC addresses don't match:\n");
1573 printf ("\tHW MAC address: "
1574 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1575 v_rom_mac[0], v_rom_mac[1],
1576 v_rom_mac[2], v_rom_mac[3],
1577 v_rom_mac[4], v_rom_mac[5] );
1578 printf ("\t\"ethaddr\" value: "
1579 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1580 v_env_mac[0], v_env_mac[1],
1581 v_env_mac[2], v_env_mac[3],
1582 v_env_mac[4], v_env_mac[5]) ;
1583 debug ("### Set MAC addr from environment\n");
wdenkb56ddc62003-09-15 21:14:37 +00001584 }
1585 }
1586 memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001587 smc_set_mac_addr ((uchar *)v_mac); /* use old function to update smc default */
wdenk3d3befa2004-03-14 15:06:13 +00001588 PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
wdenk42dfe7a2004-03-14 22:25:36 +00001589 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
wdenkb56ddc62003-09-15 21:14:37 +00001590 return (0);
wdenk0b97ab12003-06-19 23:58:30 +00001591}
1592
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +01001593int get_rom_mac (uchar *v_rom_mac)
wdenk0b97ab12003-06-19 23:58:30 +00001594{
wdenkb56ddc62003-09-15 21:14:37 +00001595#ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1596 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
1597
1598 memcpy (v_rom_mac, hw_mac_addr, 6);
1599 return (1);
wdenk0b97ab12003-06-19 23:58:30 +00001600#else
wdenk3d3befa2004-03-14 15:06:13 +00001601 int i;
wdenkf39748a2004-06-09 13:37:52 +00001602 int valid_mac = 0;
1603
wdenk3d3befa2004-03-14 15:06:13 +00001604 SMC_SELECT_BANK (1);
1605 for (i=0; i<6; i++)
1606 {
wdenk39539882004-07-01 16:30:44 +00001607 v_rom_mac[i] = SMC_inb ((ADDR0_REG + i));
wdenkf39748a2004-06-09 13:37:52 +00001608 valid_mac |= v_rom_mac[i];
wdenkb56ddc62003-09-15 21:14:37 +00001609 }
wdenkf39748a2004-06-09 13:37:52 +00001610
1611 return (valid_mac ? 1 : 0);
wdenk0b97ab12003-06-19 23:58:30 +00001612#endif
1613}
wdenkfe8c2802002-11-03 00:38:21 +00001614#endif /* CONFIG_DRIVER_SMC91111 */