blob: 69cbb1713da91853b0c68f2d58252e18e1ff8302 [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
wdenk8bf3b002003-12-06 23:20:41 +000075#define SMC_DEBUG 0
76
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);
213int get_rom_mac(char *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{
279 while (len-- > 0) {
280 *((word*)buf)++ = SMC_inw(offset);
281 barrier(); *((volatile u32*)(0xc0000000));
282 }
283}
284
285static inline void SMC_outsw(dword offset, uchar* buf, dword len)
286{
287 while (len-- > 0) {
288 SMC_outw(*((word*)buf)++, offset);
289 barrier(); *(volatile u32*)(0xc0000000);
290 }
291}
292#endif /* CONFIG_SMC_USE_IOFUNCS */
293
wdenk8bf3b002003-12-06 23:20:41 +0000294static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
wdenkfe8c2802002-11-03 00:38:21 +0000295
296/*
297 * This function must be called before smc_open() if you want to override
298 * the default mac address.
299 */
300
301void smc_set_mac_addr(const char *addr) {
302 int i;
303
304 for (i=0; i < sizeof(smc_mac_addr); i++){
305 smc_mac_addr[i] = addr[i];
306 }
307}
308
309/*
310 * smc_get_macaddr is no longer used. If you want to override the default
wdenk0b97ab12003-06-19 23:58:30 +0000311 * mac address, call smc_get_mac_addr as a part of the board initialization.
wdenkfe8c2802002-11-03 00:38:21 +0000312 */
313
314#if 0
315void smc_get_macaddr( byte *addr ) {
316 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
wdenk8bde7f72003-06-27 21:31:46 +0000317 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
wdenkfe8c2802002-11-03 00:38:21 +0000318 int i;
319
320
wdenk8bde7f72003-06-27 21:31:46 +0000321 for (i=0; i<6; i++) {
322 addr[0] = *(dnp1110_mac+0);
323 addr[1] = *(dnp1110_mac+1);
324 addr[2] = *(dnp1110_mac+2);
325 addr[3] = *(dnp1110_mac+3);
326 addr[4] = *(dnp1110_mac+4);
327 addr[5] = *(dnp1110_mac+5);
328 }
wdenkfe8c2802002-11-03 00:38:21 +0000329}
330#endif /* 0 */
331
332/***********************************************
wdenk42dfe7a2004-03-14 22:25:36 +0000333 * Show available memory *
wdenkfe8c2802002-11-03 00:38:21 +0000334 ***********************************************/
335void dump_memory_info(void)
336{
wdenk8bde7f72003-06-27 21:31:46 +0000337 word mem_info;
338 word old_bank;
wdenkfe8c2802002-11-03 00:38:21 +0000339
wdenk8bde7f72003-06-27 21:31:46 +0000340 old_bank = SMC_inw(BANK_SELECT)&0xF;
wdenkfe8c2802002-11-03 00:38:21 +0000341
wdenk8bde7f72003-06-27 21:31:46 +0000342 SMC_SELECT_BANK(0);
343 mem_info = SMC_inw( MIR_REG );
344 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
wdenkfe8c2802002-11-03 00:38:21 +0000345
wdenk8bde7f72003-06-27 21:31:46 +0000346 SMC_SELECT_BANK(old_bank);
wdenkfe8c2802002-11-03 00:38:21 +0000347}
348/*
349 . A rather simple routine to print out a packet for debugging purposes.
350*/
351#if SMC_DEBUG > 2
352static void print_packet( byte *, int );
353#endif
354
355#define tx_done(dev) 1
356
357
wdenkfe8c2802002-11-03 00:38:21 +0000358/* this does a soft reset on the device */
359static void smc_reset( void );
360
361/* Enable Interrupts, Receive, and Transmit */
362static void smc_enable( void );
363
364/* this puts the device in an inactive state */
365static void smc_shutdown( void );
366
367/* Routines to Read and Write the PHY Registers across the
368 MII Management Interface
369*/
370
371#ifndef CONFIG_SMC91111_EXT_PHY
372static word smc_read_phy_register(byte phyreg);
373static void smc_write_phy_register(byte phyreg, word phydata);
374#endif /* !CONFIG_SMC91111_EXT_PHY */
375
376
wdenkb56ddc62003-09-15 21:14:37 +0000377static int poll4int (byte mask, int timeout)
378{
379 int tmo = get_timer (0) + timeout * CFG_HZ;
380 int is_timeout = 0;
381 word old_bank = SMC_inw (BSR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000382
wdenkb56ddc62003-09-15 21:14:37 +0000383 PRINTK2 ("Polling...\n");
384 SMC_SELECT_BANK (2);
385 while ((SMC_inw (SMC91111_INT_REG) & mask) == 0) {
386 if (get_timer (0) >= tmo) {
387 is_timeout = 1;
388 break;
389 }
wdenkfe8c2802002-11-03 00:38:21 +0000390 }
wdenkfe8c2802002-11-03 00:38:21 +0000391
wdenkb56ddc62003-09-15 21:14:37 +0000392 /* restore old bank selection */
393 SMC_SELECT_BANK (old_bank);
wdenkfe8c2802002-11-03 00:38:21 +0000394
wdenkb56ddc62003-09-15 21:14:37 +0000395 if (is_timeout)
396 return 1;
397 else
398 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000399}
400
wdenk487778b2003-06-06 11:20:01 +0000401/* Only one release command at a time, please */
wdenkb56ddc62003-09-15 21:14:37 +0000402static inline void smc_wait_mmu_release_complete (void)
wdenk487778b2003-06-06 11:20:01 +0000403{
404 int count = 0;
wdenkb56ddc62003-09-15 21:14:37 +0000405
wdenk487778b2003-06-06 11:20:01 +0000406 /* assume bank 2 selected */
wdenkb56ddc62003-09-15 21:14:37 +0000407 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
408 udelay (1); /* Wait until not busy */
409 if (++count > 200)
410 break;
wdenk487778b2003-06-06 11:20:01 +0000411 }
412}
413
wdenkfe8c2802002-11-03 00:38:21 +0000414/*
415 . Function: smc_reset( void )
416 . Purpose:
wdenk42dfe7a2004-03-14 22:25:36 +0000417 . This sets the SMC91111 chip to its normal state, hopefully from whatever
418 . mess that any other DOS driver has put it in.
wdenkfe8c2802002-11-03 00:38:21 +0000419 .
420 . Maybe I should reset more registers to defaults in here? SOFTRST should
421 . do that for me.
422 .
423 . Method:
424 . 1. send a SOFT RESET
425 . 2. wait for it to finish
426 . 3. enable autorelease mode
427 . 4. reset the memory management unit
428 . 5. clear all interrupts
429 .
430*/
wdenkb56ddc62003-09-15 21:14:37 +0000431static void smc_reset (void)
wdenkfe8c2802002-11-03 00:38:21 +0000432{
wdenkf39748a2004-06-09 13:37:52 +0000433 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000434
435 /* This resets the registers mostly to defaults, but doesn't
436 affect EEPROM. That seems unnecessary */
wdenkb56ddc62003-09-15 21:14:37 +0000437 SMC_SELECT_BANK (0);
438 SMC_outw (RCR_SOFTRST, RCR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000439
440 /* Setup the Configuration Register */
441 /* This is necessary because the CONFIG_REG is not affected */
442 /* by a soft reset */
443
wdenkb56ddc62003-09-15 21:14:37 +0000444 SMC_SELECT_BANK (1);
wdenkfe8c2802002-11-03 00:38:21 +0000445#if defined(CONFIG_SMC91111_EXT_PHY)
wdenkb56ddc62003-09-15 21:14:37 +0000446 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000447#else
wdenkb56ddc62003-09-15 21:14:37 +0000448 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000449#endif
450
451
452 /* Release from possible power-down state */
453 /* Configuration register is not affected by Soft Reset */
wdenkb56ddc62003-09-15 21:14:37 +0000454 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000455
wdenkb56ddc62003-09-15 21:14:37 +0000456 SMC_SELECT_BANK (0);
wdenkfe8c2802002-11-03 00:38:21 +0000457
458 /* this should pause enough for the chip to be happy */
wdenkb56ddc62003-09-15 21:14:37 +0000459 udelay (10);
wdenkfe8c2802002-11-03 00:38:21 +0000460
461 /* Disable transmit and receive functionality */
wdenkb56ddc62003-09-15 21:14:37 +0000462 SMC_outw (RCR_CLEAR, RCR_REG);
463 SMC_outw (TCR_CLEAR, TCR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000464
465 /* set the control register */
wdenkb56ddc62003-09-15 21:14:37 +0000466 SMC_SELECT_BANK (1);
467 SMC_outw (CTL_DEFAULT, CTL_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000468
469 /* Reset the MMU */
wdenkb56ddc62003-09-15 21:14:37 +0000470 SMC_SELECT_BANK (2);
471 smc_wait_mmu_release_complete ();
472 SMC_outw (MC_RESET, MMU_CMD_REG);
473 while (SMC_inw (MMU_CMD_REG) & MC_BUSY)
474 udelay (1); /* Wait until not busy */
wdenkfe8c2802002-11-03 00:38:21 +0000475
476 /* Note: It doesn't seem that waiting for the MMU busy is needed here,
477 but this is a place where future chipsets _COULD_ break. Be wary
wdenk8bde7f72003-06-27 21:31:46 +0000478 of issuing another MMU command right after this */
wdenkfe8c2802002-11-03 00:38:21 +0000479
480 /* Disable all interrupts */
wdenkb56ddc62003-09-15 21:14:37 +0000481 SMC_outb (0, IM_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000482}
483
484/*
485 . Function: smc_enable
486 . Purpose: let the chip talk to the outside work
487 . Method:
488 . 1. Enable the transmitter
489 . 2. Enable the receiver
490 . 3. Enable interrupts
491*/
492static void smc_enable()
493{
wdenkf39748a2004-06-09 13:37:52 +0000494 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000495 SMC_SELECT_BANK( 0 );
496 /* see the header file for options in TCR/RCR DEFAULT*/
497 SMC_outw( TCR_DEFAULT, TCR_REG );
498 SMC_outw( RCR_DEFAULT, RCR_REG );
499
500 /* clear MII_DIS */
501/* smc_write_phy_register(PHY_CNTL_REG, 0x0000); */
502}
503
504/*
505 . Function: smc_shutdown
506 . Purpose: closes down the SMC91xxx chip.
507 . Method:
508 . 1. zero the interrupt mask
509 . 2. clear the enable receive flag
510 . 3. clear the enable xmit flags
511 .
512 . TODO:
513 . (1) maybe utilize power down mode.
514 . Why not yet? Because while the chip will go into power down mode,
515 . the manual says that it will wake up in response to any I/O requests
wdenk42dfe7a2004-03-14 22:25:36 +0000516 . in the register space. Empirical results do not show this working.
wdenkfe8c2802002-11-03 00:38:21 +0000517*/
518static void smc_shutdown()
519{
wdenkf39748a2004-06-09 13:37:52 +0000520 PRINTK2(CARDNAME ": smc_shutdown\n");
wdenkfe8c2802002-11-03 00:38:21 +0000521
522 /* no more interrupts for me */
523 SMC_SELECT_BANK( 2 );
524 SMC_outb( 0, IM_REG );
525
526 /* and tell the card to stay away from that nasty outside world */
527 SMC_SELECT_BANK( 0 );
528 SMC_outb( RCR_CLEAR, RCR_REG );
529 SMC_outb( TCR_CLEAR, TCR_REG );
530}
531
532
533/*
534 . Function: smc_hardware_send_packet(struct net_device * )
535 . Purpose:
536 . This sends the actual packet to the SMC9xxx chip.
537 .
538 . Algorithm:
wdenk42dfe7a2004-03-14 22:25:36 +0000539 . First, see if a saved_skb is available.
wdenkfe8c2802002-11-03 00:38:21 +0000540 . ( this should NOT be called if there is no 'saved_skb'
541 . Now, find the packet number that the chip allocated
542 . Point the data pointers at it in memory
543 . Set the length word in the chip's memory
544 . Dump the packet to chip memory
545 . Check if a last byte is needed ( odd length packet )
546 . if so, set the control flag right
wdenk42dfe7a2004-03-14 22:25:36 +0000547 . Tell the card to send it
wdenkfe8c2802002-11-03 00:38:21 +0000548 . Enable the transmit interrupt, so I know if it failed
wdenk42dfe7a2004-03-14 22:25:36 +0000549 . Free the kernel data if I actually sent it.
wdenkfe8c2802002-11-03 00:38:21 +0000550*/
wdenkb56ddc62003-09-15 21:14:37 +0000551static int smc_send_packet (volatile void *packet, int packet_length)
wdenkfe8c2802002-11-03 00:38:21 +0000552{
wdenkb56ddc62003-09-15 21:14:37 +0000553 byte packet_no;
554 unsigned long ioaddr;
555 byte *buf;
556 int length;
557 int numPages;
558 int try = 0;
559 int time_out;
560 byte status;
wdenk518e2e12004-03-25 14:59:05 +0000561 byte saved_pnr;
562 word saved_ptr;
wdenkfe8c2802002-11-03 00:38:21 +0000563
wdenk518e2e12004-03-25 14:59:05 +0000564 /* save PTR and PNR registers before manipulation */
wdenkb79a11c2004-03-25 15:14:43 +0000565 SMC_SELECT_BANK (2);
wdenk518e2e12004-03-25 14:59:05 +0000566 saved_pnr = SMC_inb( PN_REG );
567 saved_ptr = SMC_inw( PTR_REG );
wdenkfe8c2802002-11-03 00:38:21 +0000568
wdenkf39748a2004-06-09 13:37:52 +0000569 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000570
571 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
572
573 /* allocate memory
wdenkb56ddc62003-09-15 21:14:37 +0000574 ** The MMU wants the number of pages to be the number of 256 bytes
575 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
576 **
577 ** The 91C111 ignores the size bits, but the code is left intact
578 ** for backwards and future compatibility.
579 **
580 ** Pkt size for allocating is data length +6 (for additional status
581 ** words, length and ctl!)
582 **
583 ** If odd size then last byte is included in this header.
584 */
585 numPages = ((length & 0xfffe) + 6);
586 numPages >>= 8; /* Divide by 256 */
wdenkfe8c2802002-11-03 00:38:21 +0000587
wdenkb56ddc62003-09-15 21:14:37 +0000588 if (numPages > 7) {
589 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000590 return 0;
591 }
592
593 /* now, try to allocate the memory */
wdenkb56ddc62003-09-15 21:14:37 +0000594 SMC_SELECT_BANK (2);
595 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000596
wdenkdc7c9a12003-03-26 06:55:25 +0000597 /* FIXME: the ALLOC_INT bit never gets set *
wdenk42dfe7a2004-03-14 22:25:36 +0000598 * so the following will always give a *
599 * memory allocation error. *
600 * same code works in armboot though *
wdenkdc7c9a12003-03-26 06:55:25 +0000601 * -ro
602 */
603
wdenkfe8c2802002-11-03 00:38:21 +0000604again:
605 try++;
606 time_out = MEMORY_WAIT_TIME;
607 do {
wdenkb56ddc62003-09-15 21:14:37 +0000608 status = SMC_inb (SMC91111_INT_REG);
609 if (status & IM_ALLOC_INT) {
wdenkfe8c2802002-11-03 00:38:21 +0000610 /* acknowledge the interrupt */
wdenkb56ddc62003-09-15 21:14:37 +0000611 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
wdenk8bde7f72003-06-27 21:31:46 +0000612 break;
wdenkfe8c2802002-11-03 00:38:21 +0000613 }
wdenkb56ddc62003-09-15 21:14:37 +0000614 } while (--time_out);
wdenkfe8c2802002-11-03 00:38:21 +0000615
wdenkb56ddc62003-09-15 21:14:37 +0000616 if (!time_out) {
617 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
618 SMC_DEV_NAME, try);
619 if (try < SMC_ALLOC_MAX_TRY)
620 goto again;
621 else
622 return 0;
wdenkfe8c2802002-11-03 00:38:21 +0000623 }
624
wdenkb56ddc62003-09-15 21:14:37 +0000625 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
626 SMC_DEV_NAME, try);
wdenkfe8c2802002-11-03 00:38:21 +0000627
628 /* I can send the packet now.. */
629
630 ioaddr = SMC_BASE_ADDRESS;
631
wdenkb56ddc62003-09-15 21:14:37 +0000632 buf = (byte *) packet;
wdenkfe8c2802002-11-03 00:38:21 +0000633
634 /* If I get here, I _know_ there is a packet slot waiting for me */
wdenkb56ddc62003-09-15 21:14:37 +0000635 packet_no = SMC_inb (AR_REG);
636 if (packet_no & AR_FAILED) {
wdenkfe8c2802002-11-03 00:38:21 +0000637 /* or isn't there? BAD CHIP! */
wdenkb56ddc62003-09-15 21:14:37 +0000638 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000639 return 0;
640 }
641
642 /* we have a packet address, so tell the card to use it */
wdenk1f6d4252004-11-02 13:00:33 +0000643#ifndef CONFIG_XAENIAX
wdenkb56ddc62003-09-15 21:14:37 +0000644 SMC_outb (packet_no, PN_REG);
wdenk1f6d4252004-11-02 13:00:33 +0000645#else
646 /* On Xaeniax board, we can't use SMC_outb here because that way
647 * the Allocate MMU command will end up written to the command register
648 * as well, which will lead to a problem.
649 */
650 SMC_outl (packet_no << 16, 0);
651#endif
wdenkb79a11c2004-03-25 15:14:43 +0000652 /* do not write new ptr value if Write data fifo not empty */
653 while ( saved_ptr & PTR_NOTEMPTY )
wdenk518e2e12004-03-25 14:59:05 +0000654 printf ("Write data fifo not empty!\n");
655
wdenkfe8c2802002-11-03 00:38:21 +0000656 /* point to the beginning of the packet */
wdenkb56ddc62003-09-15 21:14:37 +0000657 SMC_outw (PTR_AUTOINC, PTR_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000658
wdenkb56ddc62003-09-15 21:14:37 +0000659 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
660 SMC_DEV_NAME, length);
wdenkfe8c2802002-11-03 00:38:21 +0000661
662#if SMC_DEBUG > 2
wdenkb56ddc62003-09-15 21:14:37 +0000663 printf ("Transmitting Packet\n");
664 print_packet (buf, length);
wdenkfe8c2802002-11-03 00:38:21 +0000665#endif
666
667 /* send the packet length ( +6 for status, length and ctl byte )
wdenk8bde7f72003-06-27 21:31:46 +0000668 and the status word ( set to zeros ) */
wdenkfe8c2802002-11-03 00:38:21 +0000669#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000670 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000671#else
wdenkb56ddc62003-09-15 21:14:37 +0000672 SMC_outw (0, SMC91111_DATA_REG);
673 /* send the packet length ( +6 for status words, length, and ctl */
674 SMC_outw ((length + 6), SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000675#endif
676
677 /* send the actual data
wdenkb56ddc62003-09-15 21:14:37 +0000678 . I _think_ it's faster to send the longs first, and then
679 . mop up by sending the last word. It depends heavily
wdenk42dfe7a2004-03-14 22:25:36 +0000680 . on alignment, at least on the 486. Maybe it would be
wdenkb56ddc62003-09-15 21:14:37 +0000681 . a good idea to check which is optimal? But that could take
682 . almost as much time as is saved?
683 */
wdenkfe8c2802002-11-03 00:38:21 +0000684#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000685 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
686 if (length & 0x2)
687 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
688 SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000689#else
wdenkb56ddc62003-09-15 21:14:37 +0000690 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
wdenkfe8c2802002-11-03 00:38:21 +0000691#endif /* USE_32_BIT */
692
wdenk42dfe7a2004-03-14 22:25:36 +0000693 /* Send the last byte, if there is one. */
wdenkb56ddc62003-09-15 21:14:37 +0000694 if ((length & 1) == 0) {
695 SMC_outw (0, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000696 } else {
wdenkb56ddc62003-09-15 21:14:37 +0000697 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000698 }
699
700 /* and let the chipset deal with it */
wdenkb56ddc62003-09-15 21:14:37 +0000701 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
wdenkfe8c2802002-11-03 00:38:21 +0000702
703 /* poll for TX INT */
wdenk518e2e12004-03-25 14:59:05 +0000704 /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
705 /* poll for TX_EMPTY INT - autorelease enabled */
706 if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
wdenkfe8c2802002-11-03 00:38:21 +0000707 /* sending failed */
wdenkb56ddc62003-09-15 21:14:37 +0000708 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000709
710 /* release packet */
wdenk518e2e12004-03-25 14:59:05 +0000711 /* no need to release, MMU does that now */
wdenk1f6d4252004-11-02 13:00:33 +0000712#ifdef CONFIG_XAENIAX
713 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
714#endif
wdenkfe8c2802002-11-03 00:38:21 +0000715
wdenk8bde7f72003-06-27 21:31:46 +0000716 /* wait for MMU getting ready (low) */
wdenkb56ddc62003-09-15 21:14:37 +0000717 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
718 udelay (10);
wdenk8bde7f72003-06-27 21:31:46 +0000719 }
wdenkfe8c2802002-11-03 00:38:21 +0000720
wdenkb56ddc62003-09-15 21:14:37 +0000721 PRINTK2 ("MMU ready\n");
wdenkfe8c2802002-11-03 00:38:21 +0000722
723
724 return 0;
725 } else {
726 /* ack. int */
wdenk518e2e12004-03-25 14:59:05 +0000727 SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
728 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
wdenkb56ddc62003-09-15 21:14:37 +0000729 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
730 length);
wdenkfe8c2802002-11-03 00:38:21 +0000731
732 /* release packet */
wdenk518e2e12004-03-25 14:59:05 +0000733 /* no need to release, MMU does that now */
wdenk1f6d4252004-11-02 13:00:33 +0000734#ifdef CONFIG_XAENIAX
735 SMC_outw (MC_FREEPKT, MMU_CMD_REG);
736#endif
wdenkfe8c2802002-11-03 00:38:21 +0000737
wdenk8bde7f72003-06-27 21:31:46 +0000738 /* wait for MMU getting ready (low) */
wdenkb56ddc62003-09-15 21:14:37 +0000739 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
740 udelay (10);
wdenk8bde7f72003-06-27 21:31:46 +0000741 }
wdenkfe8c2802002-11-03 00:38:21 +0000742
wdenkb56ddc62003-09-15 21:14:37 +0000743 PRINTK2 ("MMU ready\n");
wdenkfe8c2802002-11-03 00:38:21 +0000744
745
746 }
747
wdenk518e2e12004-03-25 14:59:05 +0000748 /* restore previously saved registers */
wdenk1f6d4252004-11-02 13:00:33 +0000749#ifndef CONFIG_XAENIAX
wdenk518e2e12004-03-25 14:59:05 +0000750 SMC_outb( saved_pnr, PN_REG );
wdenk1f6d4252004-11-02 13:00:33 +0000751#else
752 /* On Xaeniax board, we can't use SMC_outb here because that way
753 * the Allocate MMU command will end up written to the command register
754 * as well, which will lead to a problem.
755 */
756 SMC_outl(saved_pnr << 16, 0);
757#endif
wdenk518e2e12004-03-25 14:59:05 +0000758 SMC_outw( saved_ptr, PTR_REG );
759
wdenkfe8c2802002-11-03 00:38:21 +0000760 return length;
761}
762
763/*-------------------------------------------------------------------------
764 |
765 | smc_destructor( struct net_device * dev )
766 | Input parameters:
767 | dev, pointer to the device structure
768 |
769 | Output:
770 | None.
771 |
772 ---------------------------------------------------------------------------
773*/
774void smc_destructor()
775{
wdenkf39748a2004-06-09 13:37:52 +0000776 PRINTK2(CARDNAME ": smc_destructor\n");
wdenkfe8c2802002-11-03 00:38:21 +0000777}
778
779
780/*
781 * Open and Initialize the board
782 *
783 * Set up everything, reset the card, etc ..
784 *
785 */
wdenkb56ddc62003-09-15 21:14:37 +0000786static int smc_open (bd_t * bd)
wdenkfe8c2802002-11-03 00:38:21 +0000787{
wdenkb56ddc62003-09-15 21:14:37 +0000788 int i, err;
wdenkfe8c2802002-11-03 00:38:21 +0000789
wdenkf39748a2004-06-09 13:37:52 +0000790 PRINTK2 ("%s: smc_open\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000791
792 /* reset the hardware */
wdenkb56ddc62003-09-15 21:14:37 +0000793 smc_reset ();
794 smc_enable ();
wdenkfe8c2802002-11-03 00:38:21 +0000795
796 /* Configure the PHY */
797#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +0000798 smc_phy_configure ();
wdenkfe8c2802002-11-03 00:38:21 +0000799#endif
800
wdenkfe8c2802002-11-03 00:38:21 +0000801 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
802/* SMC_SELECT_BANK(0); */
803/* SMC_outw(0, RPC_REG); */
wdenkb56ddc62003-09-15 21:14:37 +0000804 SMC_SELECT_BANK (1);
wdenk8bde7f72003-06-27 21:31:46 +0000805
wdenkb56ddc62003-09-15 21:14:37 +0000806 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
807 if (err < 0) {
wdenk42dfe7a2004-03-14 22:25:36 +0000808 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
wdenkb56ddc62003-09-15 21:14:37 +0000809 return (-1); /* upper code ignores this, but NOT bi_enetaddr */
810 }
wdenkfe8c2802002-11-03 00:38:21 +0000811#ifdef USE_32_BIT
wdenkb56ddc62003-09-15 21:14:37 +0000812 for (i = 0; i < 6; i += 2) {
wdenkfe8c2802002-11-03 00:38:21 +0000813 word address;
814
wdenkb56ddc62003-09-15 21:14:37 +0000815 address = smc_mac_addr[i + 1] << 8;
816 address |= smc_mac_addr[i];
wdenk39539882004-07-01 16:30:44 +0000817 SMC_outw (address, (ADDR0_REG + i));
wdenkfe8c2802002-11-03 00:38:21 +0000818 }
819#else
wdenkb56ddc62003-09-15 21:14:37 +0000820 for (i = 0; i < 6; i++)
wdenk39539882004-07-01 16:30:44 +0000821 SMC_outb (smc_mac_addr[i], (ADDR0_REG + i));
wdenkfe8c2802002-11-03 00:38:21 +0000822#endif
823
824 return 0;
825}
826
wdenkfe8c2802002-11-03 00:38:21 +0000827/*-------------------------------------------------------------
828 .
829 . smc_rcv - receive a packet from the card
830 .
831 . There is ( at least ) a packet waiting to be read from
832 . chip-memory.
833 .
834 . o Read the status
835 . o If an error, record it
836 . o otherwise, read in the packet
837 --------------------------------------------------------------
838*/
839static int smc_rcv()
840{
wdenk42dfe7a2004-03-14 22:25:36 +0000841 int packet_number;
wdenkfe8c2802002-11-03 00:38:21 +0000842 word status;
843 word packet_length;
wdenk42dfe7a2004-03-14 22:25:36 +0000844 int is_error = 0;
wdenkfe8c2802002-11-03 00:38:21 +0000845#ifdef USE_32_BIT
846 dword stat_len;
847#endif
wdenk518e2e12004-03-25 14:59:05 +0000848 byte saved_pnr;
849 word saved_ptr;
wdenkfe8c2802002-11-03 00:38:21 +0000850
wdenkfe8c2802002-11-03 00:38:21 +0000851 SMC_SELECT_BANK(2);
wdenk518e2e12004-03-25 14:59:05 +0000852 /* save PTR and PTR registers */
853 saved_pnr = SMC_inb( PN_REG );
854 saved_ptr = SMC_inw( PTR_REG );
855
wdenkfe8c2802002-11-03 00:38:21 +0000856 packet_number = SMC_inw( RXFIFO_REG );
857
858 if ( packet_number & RXFIFO_REMPTY ) {
859
860 return 0;
861 }
862
wdenkf39748a2004-06-09 13:37:52 +0000863 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000864 /* start reading from the start of the packet */
865 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
866
867 /* First two words are status and packet_length */
868#ifdef USE_32_BIT
869 stat_len = SMC_inl(SMC91111_DATA_REG);
870 status = stat_len & 0xffff;
871 packet_length = stat_len >> 16;
872#else
wdenk42dfe7a2004-03-14 22:25:36 +0000873 status = SMC_inw( SMC91111_DATA_REG );
874 packet_length = SMC_inw( SMC91111_DATA_REG );
wdenkfe8c2802002-11-03 00:38:21 +0000875#endif
876
877 packet_length &= 0x07ff; /* mask off top bits */
878
879 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
880
881 if ( !(status & RS_ERRORS ) ){
882 /* Adjust for having already read the first two words */
883 packet_length -= 4; /*4; */
884
885
wdenkfe8c2802002-11-03 00:38:21 +0000886 /* set odd length for bug in LAN91C111, */
887 /* which never sets RS_ODDFRAME */
888 /* TODO ? */
889
890
891#ifdef USE_32_BIT
892 PRINTK3(" Reading %d dwords (and %d bytes) \n",
893 packet_length >> 2, packet_length & 3 );
894 /* QUESTION: Like in the TX routine, do I want
895 to send the DWORDs or the bytes first, or some
896 mixture. A mixture might improve already slow PIO
wdenk42dfe7a2004-03-14 22:25:36 +0000897 performance */
wdenkfe8c2802002-11-03 00:38:21 +0000898 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
899 /* read the left over bytes */
900 if (packet_length & 3) {
901 int i;
902
wdenk699b13a2002-11-03 18:03:52 +0000903 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
wdenkfe8c2802002-11-03 00:38:21 +0000904 dword leftover = SMC_inl(SMC91111_DATA_REG);
905 for (i=0; i<(packet_length & 3); i++)
906 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
907 }
908#else
909 PRINTK3(" Reading %d words and %d byte(s) \n",
910 (packet_length >> 1 ), packet_length & 1 );
911 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
912
913#endif /* USE_32_BIT */
914
915#if SMC_DEBUG > 2
916 printf("Receiving Packet\n");
917 print_packet( NetRxPackets[0], packet_length );
918#endif
919 } else {
920 /* error ... */
921 /* TODO ? */
922 is_error = 1;
923 }
924
925 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
926 udelay(1); /* Wait until not busy */
927
928 /* error or good, tell the card to get rid of this packet */
929 SMC_outw( MC_RELEASE, MMU_CMD_REG );
930
931 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
932 udelay(1); /* Wait until not busy */
933
wdenk518e2e12004-03-25 14:59:05 +0000934 /* restore saved registers */
wdenk1f6d4252004-11-02 13:00:33 +0000935#ifndef CONFIG_XAENIAX
wdenk518e2e12004-03-25 14:59:05 +0000936 SMC_outb( saved_pnr, PN_REG );
wdenk1f6d4252004-11-02 13:00:33 +0000937#else
938 /* On Xaeniax board, we can't use SMC_outb here because that way
939 * the Allocate MMU command will end up written to the command register
940 * as well, which will lead to a problem.
941 */
942 SMC_outl( saved_pnr << 16, 0);
943#endif
wdenk518e2e12004-03-25 14:59:05 +0000944 SMC_outw( saved_ptr, PTR_REG );
945
wdenkfe8c2802002-11-03 00:38:21 +0000946 if (!is_error) {
947 /* Pass the packet up to the protocol layers. */
948 NetReceive(NetRxPackets[0], packet_length);
949 return packet_length;
950 } else {
951 return 0;
952 }
953
954}
955
956
wdenkfe8c2802002-11-03 00:38:21 +0000957/*----------------------------------------------------
958 . smc_close
959 .
960 . this makes the board clean up everything that it can
wdenk42dfe7a2004-03-14 22:25:36 +0000961 . and not talk to the outside world. Caused by
wdenkfe8c2802002-11-03 00:38:21 +0000962 . an 'ifconfig ethX down'
963 .
964 -----------------------------------------------------*/
965static int smc_close()
966{
wdenkf39748a2004-06-09 13:37:52 +0000967 PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +0000968
969 /* clear everything */
970 smc_shutdown();
971
972 return 0;
973}
974
975
976#if 0
977/*------------------------------------------------------------
978 . Modify a bit in the LAN91C111 register set
979 .-------------------------------------------------------------*/
980static word smc_modify_regbit(int bank, int ioaddr, int reg,
981 unsigned int bit, int val)
982{
983 word regval;
984
985 SMC_SELECT_BANK( bank );
986
987 regval = SMC_inw( reg );
988 if (val)
989 regval |= bit;
990 else
991 regval &= ~bit;
992
993 SMC_outw( regval, 0 );
994 return(regval);
995}
996
997
998/*------------------------------------------------------------
999 . Retrieve a bit in the LAN91C111 register set
1000 .-------------------------------------------------------------*/
1001static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
1002{
1003 SMC_SELECT_BANK( bank );
1004 if ( SMC_inw( reg ) & bit)
1005 return(1);
1006 else
1007 return(0);
1008}
1009
1010
1011/*------------------------------------------------------------
1012 . Modify a LAN91C111 register (word access only)
1013 .-------------------------------------------------------------*/
1014static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
1015{
1016 SMC_SELECT_BANK( bank );
1017 SMC_outw( val, reg );
1018}
1019
1020
1021/*------------------------------------------------------------
1022 . Retrieve a LAN91C111 register (word access only)
1023 .-------------------------------------------------------------*/
1024static int smc_get_reg(int bank, int ioaddr, int reg)
1025{
1026 SMC_SELECT_BANK( bank );
1027 return(SMC_inw( reg ));
1028}
1029
1030#endif /* 0 */
1031
1032/*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
1033
1034#if (SMC_DEBUG > 2 )
1035
1036/*------------------------------------------------------------
1037 . Debugging function for viewing MII Management serial bitstream
1038 .-------------------------------------------------------------*/
wdenkb56ddc62003-09-15 21:14:37 +00001039static void smc_dump_mii_stream (byte * bits, int size)
wdenkfe8c2802002-11-03 00:38:21 +00001040{
1041 int i;
1042
wdenkb56ddc62003-09-15 21:14:37 +00001043 printf ("BIT#:");
1044 for (i = 0; i < size; ++i) {
1045 printf ("%d", i % 10);
1046 }
wdenkfe8c2802002-11-03 00:38:21 +00001047
wdenkb56ddc62003-09-15 21:14:37 +00001048 printf ("\nMDOE:");
1049 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001050 if (bits[i] & MII_MDOE)
wdenkb56ddc62003-09-15 21:14:37 +00001051 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001052 else
wdenkb56ddc62003-09-15 21:14:37 +00001053 printf ("0");
1054 }
wdenkfe8c2802002-11-03 00:38:21 +00001055
wdenkb56ddc62003-09-15 21:14:37 +00001056 printf ("\nMDO :");
1057 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001058 if (bits[i] & MII_MDO)
wdenkb56ddc62003-09-15 21:14:37 +00001059 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001060 else
wdenkb56ddc62003-09-15 21:14:37 +00001061 printf ("0");
1062 }
wdenkfe8c2802002-11-03 00:38:21 +00001063
wdenkb56ddc62003-09-15 21:14:37 +00001064 printf ("\nMDI :");
1065 for (i = 0; i < size; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001066 if (bits[i] & MII_MDI)
wdenkb56ddc62003-09-15 21:14:37 +00001067 printf ("1");
wdenkfe8c2802002-11-03 00:38:21 +00001068 else
wdenkb56ddc62003-09-15 21:14:37 +00001069 printf ("0");
1070 }
wdenkfe8c2802002-11-03 00:38:21 +00001071
wdenkb56ddc62003-09-15 21:14:37 +00001072 printf ("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001073}
1074#endif
1075
1076/*------------------------------------------------------------
1077 . Reads a register from the MII Management serial interface
1078 .-------------------------------------------------------------*/
1079#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +00001080static word smc_read_phy_register (byte phyreg)
wdenkfe8c2802002-11-03 00:38:21 +00001081{
1082 int oldBank;
1083 int i;
1084 byte mask;
1085 word mii_reg;
1086 byte bits[64];
1087 int clk_idx = 0;
1088 int input_idx;
1089 word phydata;
1090 byte phyaddr = SMC_PHY_ADDR;
1091
1092 /* 32 consecutive ones on MDO to establish sync */
1093 for (i = 0; i < 32; ++i)
1094 bits[clk_idx++] = MII_MDOE | MII_MDO;
1095
1096 /* Start code <01> */
1097 bits[clk_idx++] = MII_MDOE;
1098 bits[clk_idx++] = MII_MDOE | MII_MDO;
1099
1100 /* Read command <10> */
1101 bits[clk_idx++] = MII_MDOE | MII_MDO;
1102 bits[clk_idx++] = MII_MDOE;
1103
1104 /* Output the PHY address, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001105 mask = (byte) 0x10;
1106 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001107 if (phyaddr & mask)
1108 bits[clk_idx++] = MII_MDOE | MII_MDO;
1109 else
1110 bits[clk_idx++] = MII_MDOE;
1111
1112 /* Shift to next lowest bit */
1113 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001114 }
wdenkfe8c2802002-11-03 00:38:21 +00001115
1116 /* Output the phy register number, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001117 mask = (byte) 0x10;
1118 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001119 if (phyreg & mask)
1120 bits[clk_idx++] = MII_MDOE | MII_MDO;
1121 else
1122 bits[clk_idx++] = MII_MDOE;
1123
1124 /* Shift to next lowest bit */
1125 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001126 }
wdenkfe8c2802002-11-03 00:38:21 +00001127
1128 /* Tristate and turnaround (2 bit times) */
1129 bits[clk_idx++] = 0;
1130 /*bits[clk_idx++] = 0; */
1131
1132 /* Input starts at this bit time */
1133 input_idx = clk_idx;
1134
1135 /* Will input 16 bits */
1136 for (i = 0; i < 16; ++i)
1137 bits[clk_idx++] = 0;
1138
1139 /* Final clock bit */
1140 bits[clk_idx++] = 0;
1141
1142 /* Save the current bank */
wdenkb56ddc62003-09-15 21:14:37 +00001143 oldBank = SMC_inw (BANK_SELECT);
wdenkfe8c2802002-11-03 00:38:21 +00001144
1145 /* Select bank 3 */
wdenkb56ddc62003-09-15 21:14:37 +00001146 SMC_SELECT_BANK (3);
wdenkfe8c2802002-11-03 00:38:21 +00001147
1148 /* Get the current MII register value */
wdenkb56ddc62003-09-15 21:14:37 +00001149 mii_reg = SMC_inw (MII_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001150
1151 /* Turn off all MII Interface bits */
wdenkb56ddc62003-09-15 21:14:37 +00001152 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
wdenkfe8c2802002-11-03 00:38:21 +00001153
1154 /* Clock all 64 cycles */
wdenkb56ddc62003-09-15 21:14:37 +00001155 for (i = 0; i < sizeof bits; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001156 /* Clock Low - output data */
wdenkb56ddc62003-09-15 21:14:37 +00001157 SMC_outw (mii_reg | bits[i], MII_REG);
1158 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001159
1160
1161 /* Clock Hi - input data */
wdenkb56ddc62003-09-15 21:14:37 +00001162 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1163 udelay (SMC_PHY_CLOCK_DELAY);
1164 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1165 }
wdenkfe8c2802002-11-03 00:38:21 +00001166
1167 /* Return to idle state */
1168 /* Set clock to low, data to low, and output tristated */
wdenkb56ddc62003-09-15 21:14:37 +00001169 SMC_outw (mii_reg, MII_REG);
1170 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001171
1172 /* Restore original bank select */
wdenkb56ddc62003-09-15 21:14:37 +00001173 SMC_SELECT_BANK (oldBank);
wdenkfe8c2802002-11-03 00:38:21 +00001174
1175 /* Recover input data */
1176 phydata = 0;
wdenkb56ddc62003-09-15 21:14:37 +00001177 for (i = 0; i < 16; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001178 phydata <<= 1;
1179
1180 if (bits[input_idx++] & MII_MDI)
1181 phydata |= 0x0001;
wdenkb56ddc62003-09-15 21:14:37 +00001182 }
wdenkfe8c2802002-11-03 00:38:21 +00001183
1184#if (SMC_DEBUG > 2 )
wdenkb56ddc62003-09-15 21:14:37 +00001185 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
wdenkfe8c2802002-11-03 00:38:21 +00001186 phyaddr, phyreg, phydata);
wdenkb56ddc62003-09-15 21:14:37 +00001187 smc_dump_mii_stream (bits, sizeof bits);
wdenkfe8c2802002-11-03 00:38:21 +00001188#endif
1189
wdenkb56ddc62003-09-15 21:14:37 +00001190 return (phydata);
wdenkfe8c2802002-11-03 00:38:21 +00001191}
1192
1193
1194/*------------------------------------------------------------
1195 . Writes a register to the MII Management serial interface
1196 .-------------------------------------------------------------*/
wdenkb56ddc62003-09-15 21:14:37 +00001197static void smc_write_phy_register (byte phyreg, word phydata)
wdenkfe8c2802002-11-03 00:38:21 +00001198{
1199 int oldBank;
1200 int i;
1201 word mask;
1202 word mii_reg;
1203 byte bits[65];
1204 int clk_idx = 0;
1205 byte phyaddr = SMC_PHY_ADDR;
1206
1207 /* 32 consecutive ones on MDO to establish sync */
1208 for (i = 0; i < 32; ++i)
1209 bits[clk_idx++] = MII_MDOE | MII_MDO;
1210
1211 /* Start code <01> */
1212 bits[clk_idx++] = MII_MDOE;
1213 bits[clk_idx++] = MII_MDOE | MII_MDO;
1214
1215 /* Write command <01> */
1216 bits[clk_idx++] = MII_MDOE;
1217 bits[clk_idx++] = MII_MDOE | MII_MDO;
1218
1219 /* Output the PHY address, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001220 mask = (byte) 0x10;
1221 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001222 if (phyaddr & mask)
1223 bits[clk_idx++] = MII_MDOE | MII_MDO;
1224 else
1225 bits[clk_idx++] = MII_MDOE;
1226
1227 /* Shift to next lowest bit */
1228 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001229 }
wdenkfe8c2802002-11-03 00:38:21 +00001230
1231 /* Output the phy register number, msb first */
wdenkb56ddc62003-09-15 21:14:37 +00001232 mask = (byte) 0x10;
1233 for (i = 0; i < 5; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001234 if (phyreg & mask)
1235 bits[clk_idx++] = MII_MDOE | MII_MDO;
1236 else
1237 bits[clk_idx++] = MII_MDOE;
1238
1239 /* Shift to next lowest bit */
1240 mask >>= 1;
wdenkb56ddc62003-09-15 21:14:37 +00001241 }
wdenkfe8c2802002-11-03 00:38:21 +00001242
1243 /* Tristate and turnaround (2 bit times) */
1244 bits[clk_idx++] = 0;
1245 bits[clk_idx++] = 0;
1246
1247 /* Write out 16 bits of data, msb first */
1248 mask = 0x8000;
wdenkb56ddc62003-09-15 21:14:37 +00001249 for (i = 0; i < 16; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001250 if (phydata & 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 /* Final clock bit (tristate) */
1260 bits[clk_idx++] = 0;
1261
1262 /* Save the current bank */
wdenkb56ddc62003-09-15 21:14:37 +00001263 oldBank = SMC_inw (BANK_SELECT);
wdenkfe8c2802002-11-03 00:38:21 +00001264
1265 /* Select bank 3 */
wdenkb56ddc62003-09-15 21:14:37 +00001266 SMC_SELECT_BANK (3);
wdenkfe8c2802002-11-03 00:38:21 +00001267
1268 /* Get the current MII register value */
wdenkb56ddc62003-09-15 21:14:37 +00001269 mii_reg = SMC_inw (MII_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001270
1271 /* Turn off all MII Interface bits */
wdenkb56ddc62003-09-15 21:14:37 +00001272 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
wdenkfe8c2802002-11-03 00:38:21 +00001273
1274 /* Clock all cycles */
wdenkb56ddc62003-09-15 21:14:37 +00001275 for (i = 0; i < sizeof bits; ++i) {
wdenkfe8c2802002-11-03 00:38:21 +00001276 /* Clock Low - output data */
wdenkb56ddc62003-09-15 21:14:37 +00001277 SMC_outw (mii_reg | bits[i], MII_REG);
1278 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001279
1280
1281 /* Clock Hi - input data */
wdenkb56ddc62003-09-15 21:14:37 +00001282 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1283 udelay (SMC_PHY_CLOCK_DELAY);
1284 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1285 }
wdenkfe8c2802002-11-03 00:38:21 +00001286
1287 /* Return to idle state */
1288 /* Set clock to low, data to low, and output tristated */
wdenkb56ddc62003-09-15 21:14:37 +00001289 SMC_outw (mii_reg, MII_REG);
1290 udelay (SMC_PHY_CLOCK_DELAY);
wdenkfe8c2802002-11-03 00:38:21 +00001291
1292 /* Restore original bank select */
wdenkb56ddc62003-09-15 21:14:37 +00001293 SMC_SELECT_BANK (oldBank);
wdenkfe8c2802002-11-03 00:38:21 +00001294
1295#if (SMC_DEBUG > 2 )
wdenkb56ddc62003-09-15 21:14:37 +00001296 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
wdenkfe8c2802002-11-03 00:38:21 +00001297 phyaddr, phyreg, phydata);
wdenkb56ddc62003-09-15 21:14:37 +00001298 smc_dump_mii_stream (bits, sizeof bits);
wdenkfe8c2802002-11-03 00:38:21 +00001299#endif
1300}
1301#endif /* !CONFIG_SMC91111_EXT_PHY */
1302
1303
wdenkfe8c2802002-11-03 00:38:21 +00001304/*------------------------------------------------------------
1305 . Waits the specified number of milliseconds - kernel friendly
1306 .-------------------------------------------------------------*/
1307#ifndef CONFIG_SMC91111_EXT_PHY
1308static void smc_wait_ms(unsigned int ms)
1309{
1310 udelay(ms*1000);
1311}
1312#endif /* !CONFIG_SMC91111_EXT_PHY */
1313
1314
wdenkfe8c2802002-11-03 00:38:21 +00001315/*------------------------------------------------------------
1316 . Configures the specified PHY using Autonegotiation. Calls
1317 . smc_phy_fixed() if the user has requested a certain config.
1318 .-------------------------------------------------------------*/
1319#ifndef CONFIG_SMC91111_EXT_PHY
wdenkb56ddc62003-09-15 21:14:37 +00001320static void smc_phy_configure ()
wdenkfe8c2802002-11-03 00:38:21 +00001321{
1322 int timeout;
1323 byte phyaddr;
wdenkb56ddc62003-09-15 21:14:37 +00001324 word my_phy_caps; /* My PHY capabilities */
1325 word my_ad_caps; /* My Advertised capabilities */
1326 word status = 0; /*;my status = 0 */
wdenkfe8c2802002-11-03 00:38:21 +00001327 int failed = 0;
1328
wdenkf39748a2004-06-09 13:37:52 +00001329 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001330
1331
wdenkfe8c2802002-11-03 00:38:21 +00001332 /* Get the detected phy address */
1333 phyaddr = SMC_PHY_ADDR;
1334
1335 /* Reset the PHY, setting all other bits to zero */
wdenkb56ddc62003-09-15 21:14:37 +00001336 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
wdenkfe8c2802002-11-03 00:38:21 +00001337
1338 /* Wait for the reset to complete, or time out */
wdenkb56ddc62003-09-15 21:14:37 +00001339 timeout = 6; /* Wait up to 3 seconds */
1340 while (timeout--) {
1341 if (!(smc_read_phy_register (PHY_CNTL_REG)
1342 & PHY_CNTL_RST)) {
wdenkfe8c2802002-11-03 00:38:21 +00001343 /* reset complete */
1344 break;
wdenkfe8c2802002-11-03 00:38:21 +00001345 }
1346
wdenkb56ddc62003-09-15 21:14:37 +00001347 smc_wait_ms (500); /* wait 500 millisecs */
1348 }
1349
1350 if (timeout < 1) {
1351 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001352 goto smc_phy_configure_exit;
wdenkb56ddc62003-09-15 21:14:37 +00001353 }
wdenkfe8c2802002-11-03 00:38:21 +00001354
1355 /* Read PHY Register 18, Status Output */
1356 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1357
1358 /* Enable PHY Interrupts (for register 18) */
1359 /* Interrupts listed here are disabled */
wdenk8bf3b002003-12-06 23:20:41 +00001360 smc_write_phy_register (PHY_MASK_REG, 0xffff);
wdenkfe8c2802002-11-03 00:38:21 +00001361
1362 /* Configure the Receive/Phy Control register */
wdenkb56ddc62003-09-15 21:14:37 +00001363 SMC_SELECT_BANK (0);
1364 SMC_outw (RPC_DEFAULT, RPC_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001365
1366 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
wdenkb56ddc62003-09-15 21:14:37 +00001367 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1368 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
wdenkfe8c2802002-11-03 00:38:21 +00001369
1370 if (my_phy_caps & PHY_STAT_CAP_T4)
1371 my_ad_caps |= PHY_AD_T4;
1372
1373 if (my_phy_caps & PHY_STAT_CAP_TXF)
1374 my_ad_caps |= PHY_AD_TX_FDX;
1375
1376 if (my_phy_caps & PHY_STAT_CAP_TXH)
1377 my_ad_caps |= PHY_AD_TX_HDX;
1378
1379 if (my_phy_caps & PHY_STAT_CAP_TF)
1380 my_ad_caps |= PHY_AD_10_FDX;
1381
1382 if (my_phy_caps & PHY_STAT_CAP_TH)
1383 my_ad_caps |= PHY_AD_10_HDX;
1384
1385 /* Update our Auto-Neg Advertisement Register */
wdenkb56ddc62003-09-15 21:14:37 +00001386 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
wdenkfe8c2802002-11-03 00:38:21 +00001387
wdenk518e2e12004-03-25 14:59:05 +00001388 /* Read the register back. Without this, it appears that when */
1389 /* auto-negotiation is restarted, sometimes it isn't ready and */
1390 /* the link does not come up. */
1391 smc_read_phy_register(PHY_AD_REG);
1392
wdenkf39748a2004-06-09 13:37:52 +00001393 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1394 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
wdenkfe8c2802002-11-03 00:38:21 +00001395
1396 /* Restart auto-negotiation process in order to advertise my caps */
wdenkb56ddc62003-09-15 21:14:37 +00001397 smc_write_phy_register (PHY_CNTL_REG,
1398 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
wdenkfe8c2802002-11-03 00:38:21 +00001399
1400 /* Wait for the auto-negotiation to complete. This may take from */
1401 /* 2 to 3 seconds. */
1402 /* Wait for the reset to complete, or time out */
wdenkf39748a2004-06-09 13:37:52 +00001403 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
wdenkb56ddc62003-09-15 21:14:37 +00001404 while (timeout--) {
wdenkf39748a2004-06-09 13:37:52 +00001405
wdenkb56ddc62003-09-15 21:14:37 +00001406 status = smc_read_phy_register (PHY_STAT_REG);
1407 if (status & PHY_STAT_ANEG_ACK) {
wdenkfe8c2802002-11-03 00:38:21 +00001408 /* auto-negotiate complete */
1409 break;
wdenkb56ddc62003-09-15 21:14:37 +00001410 }
wdenkfe8c2802002-11-03 00:38:21 +00001411
wdenkb56ddc62003-09-15 21:14:37 +00001412 smc_wait_ms (500); /* wait 500 millisecs */
wdenkfe8c2802002-11-03 00:38:21 +00001413
1414 /* Restart auto-negotiation if remote fault */
wdenkb56ddc62003-09-15 21:14:37 +00001415 if (status & PHY_STAT_REM_FLT) {
wdenkf39748a2004-06-09 13:37:52 +00001416 printf ("%s: PHY remote fault detected\n",
wdenkb56ddc62003-09-15 21:14:37 +00001417 SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001418
1419 /* Restart auto-negotiation */
wdenkf39748a2004-06-09 13:37:52 +00001420 printf ("%s: PHY restarting auto-negotiation\n",
wdenkfe8c2802002-11-03 00:38:21 +00001421 SMC_DEV_NAME);
wdenkb56ddc62003-09-15 21:14:37 +00001422 smc_write_phy_register (PHY_CNTL_REG,
1423 PHY_CNTL_ANEG_EN |
1424 PHY_CNTL_ANEG_RST |
1425 PHY_CNTL_SPEED |
1426 PHY_CNTL_DPLX);
wdenkfe8c2802002-11-03 00:38:21 +00001427 }
wdenkb56ddc62003-09-15 21:14:37 +00001428 }
wdenkfe8c2802002-11-03 00:38:21 +00001429
wdenkb56ddc62003-09-15 21:14:37 +00001430 if (timeout < 1) {
wdenkf39748a2004-06-09 13:37:52 +00001431 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001432 failed = 1;
wdenkb56ddc62003-09-15 21:14:37 +00001433 }
wdenkfe8c2802002-11-03 00:38:21 +00001434
1435 /* Fail if we detected an auto-negotiate remote fault */
wdenkb56ddc62003-09-15 21:14:37 +00001436 if (status & PHY_STAT_REM_FLT) {
wdenkf39748a2004-06-09 13:37:52 +00001437 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
wdenkfe8c2802002-11-03 00:38:21 +00001438 failed = 1;
wdenkb56ddc62003-09-15 21:14:37 +00001439 }
wdenkfe8c2802002-11-03 00:38:21 +00001440
1441 /* Re-Configure the Receive/Phy Control register */
wdenkb56ddc62003-09-15 21:14:37 +00001442 SMC_outw (RPC_DEFAULT, RPC_REG);
wdenkfe8c2802002-11-03 00:38:21 +00001443
wdenk26238132004-07-09 22:51:01 +00001444smc_phy_configure_exit: ;
wdenkfe8c2802002-11-03 00:38:21 +00001445
1446}
1447#endif /* !CONFIG_SMC91111_EXT_PHY */
1448
1449
1450#if SMC_DEBUG > 2
1451static void print_packet( byte * buf, int length )
1452{
wdenk8bde7f72003-06-27 21:31:46 +00001453 int i;
1454 int remainder;
1455 int lines;
wdenkfe8c2802002-11-03 00:38:21 +00001456
wdenk8bde7f72003-06-27 21:31:46 +00001457 printf("Packet of length %d \n", length );
wdenkfe8c2802002-11-03 00:38:21 +00001458
1459#if SMC_DEBUG > 3
wdenk8bde7f72003-06-27 21:31:46 +00001460 lines = length / 16;
1461 remainder = length % 16;
wdenkfe8c2802002-11-03 00:38:21 +00001462
wdenk8bde7f72003-06-27 21:31:46 +00001463 for ( i = 0; i < lines ; i ++ ) {
1464 int cur;
wdenkfe8c2802002-11-03 00:38:21 +00001465
wdenk8bde7f72003-06-27 21:31:46 +00001466 for ( cur = 0; cur < 8; cur ++ ) {
1467 byte a, b;
wdenkfe8c2802002-11-03 00:38:21 +00001468
wdenk8bde7f72003-06-27 21:31:46 +00001469 a = *(buf ++ );
1470 b = *(buf ++ );
1471 printf("%02x%02x ", a, b );
1472 }
1473 printf("\n");
1474 }
1475 for ( i = 0; i < remainder/2 ; i++ ) {
1476 byte a, b;
wdenkfe8c2802002-11-03 00:38:21 +00001477
wdenk8bde7f72003-06-27 21:31:46 +00001478 a = *(buf ++ );
1479 b = *(buf ++ );
1480 printf("%02x%02x ", a, b );
1481 }
1482 printf("\n");
wdenkfe8c2802002-11-03 00:38:21 +00001483#endif
wdenkfe8c2802002-11-03 00:38:21 +00001484}
1485#endif
1486
1487int eth_init(bd_t *bd) {
wdenk0b97ab12003-06-19 23:58:30 +00001488 return (smc_open(bd));
wdenkfe8c2802002-11-03 00:38:21 +00001489}
1490
1491void eth_halt() {
1492 smc_close();
1493}
1494
1495int eth_rx() {
1496 return smc_rcv();
1497}
1498
1499int eth_send(volatile void *packet, int length) {
1500 return smc_send_packet(packet, length);
1501}
1502
wdenkb56ddc62003-09-15 21:14:37 +00001503int smc_get_ethaddr (bd_t * bd)
wdenk0b97ab12003-06-19 23:58:30 +00001504{
wdenkb56ddc62003-09-15 21:14:37 +00001505 int env_size, rom_valid, env_present = 0, reg;
1506 char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
1507 uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
wdenk0b97ab12003-06-19 23:58:30 +00001508
wdenkb56ddc62003-09-15 21:14:37 +00001509 env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
1510 if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
1511 printf ("\n*** ERROR: ethaddr is not set properly!!\n");
1512 return (-1);
wdenk8bde7f72003-06-27 21:31:46 +00001513 }
wdenk8bde7f72003-06-27 21:31:46 +00001514
wdenkb56ddc62003-09-15 21:14:37 +00001515 if (env_size > 0) {
1516 env_present = 1;
1517 s = s_env_mac;
wdenk8bde7f72003-06-27 21:31:46 +00001518 }
wdenkb56ddc62003-09-15 21:14:37 +00001519
wdenk42dfe7a2004-03-14 22:25:36 +00001520 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
wdenkb56ddc62003-09-15 21:14:37 +00001521 v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
1522 if (s)
1523 s = (*e) ? e + 1 : e;
1524 }
1525
1526 rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
1527
1528 if (!env_present) { /* if NO env */
1529 if (rom_valid) { /* but ROM is valid */
1530 v_mac = v_rom_mac;
1531 sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
1532 v_mac[0], v_mac[1], v_mac[2], v_mac[3],
1533 v_mac[4], v_mac[5]);
1534 setenv ("ethaddr", s_env_mac);
1535 } else { /* no env, bad ROM */
1536 printf ("\n*** ERROR: ethaddr is NOT set !!\n");
1537 return (-1);
1538 }
1539 } else { /* good env, don't care ROM */
1540 v_mac = v_env_mac; /* always use a good env over a ROM */
1541 }
1542
wdenk42dfe7a2004-03-14 22:25:36 +00001543 if (env_present && rom_valid) { /* if both env and ROM are good */
wdenkb56ddc62003-09-15 21:14:37 +00001544 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
wdenkb56ddc62003-09-15 21:14:37 +00001545 printf ("\nWarning: MAC addresses don't match:\n");
1546 printf ("\tHW MAC address: "
1547 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1548 v_rom_mac[0], v_rom_mac[1],
1549 v_rom_mac[2], v_rom_mac[3],
1550 v_rom_mac[4], v_rom_mac[5] );
1551 printf ("\t\"ethaddr\" value: "
1552 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1553 v_env_mac[0], v_env_mac[1],
1554 v_env_mac[2], v_env_mac[3],
1555 v_env_mac[4], v_env_mac[5]) ;
1556 debug ("### Set MAC addr from environment\n");
wdenkb56ddc62003-09-15 21:14:37 +00001557 }
1558 }
1559 memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
1560 smc_set_mac_addr (v_mac); /* use old function to update smc default */
wdenk3d3befa2004-03-14 15:06:13 +00001561 PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
wdenk42dfe7a2004-03-14 22:25:36 +00001562 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
wdenkb56ddc62003-09-15 21:14:37 +00001563 return (0);
wdenk0b97ab12003-06-19 23:58:30 +00001564}
1565
wdenkb56ddc62003-09-15 21:14:37 +00001566int get_rom_mac (char *v_rom_mac)
wdenk0b97ab12003-06-19 23:58:30 +00001567{
wdenkb56ddc62003-09-15 21:14:37 +00001568#ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1569 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
1570
1571 memcpy (v_rom_mac, hw_mac_addr, 6);
1572 return (1);
wdenk0b97ab12003-06-19 23:58:30 +00001573#else
wdenk3d3befa2004-03-14 15:06:13 +00001574 int i;
wdenkf39748a2004-06-09 13:37:52 +00001575 int valid_mac = 0;
1576
wdenk3d3befa2004-03-14 15:06:13 +00001577 SMC_SELECT_BANK (1);
1578 for (i=0; i<6; i++)
1579 {
wdenk39539882004-07-01 16:30:44 +00001580 v_rom_mac[i] = SMC_inb ((ADDR0_REG + i));
wdenkf39748a2004-06-09 13:37:52 +00001581 valid_mac |= v_rom_mac[i];
wdenkb56ddc62003-09-15 21:14:37 +00001582 }
wdenkf39748a2004-06-09 13:37:52 +00001583
1584 return (valid_mac ? 1 : 0);
wdenk0b97ab12003-06-19 23:58:30 +00001585#endif
1586}
wdenkfe8c2802002-11-03 00:38:21 +00001587#endif /* CONFIG_DRIVER_SMC91111 */