]> git.ipfire.org Git - people/ms/u-boot.git/blame - drivers/smc91111.c
Patch by Stefan Roese, 02 Jul 2004
[people/ms/u-boot.git] / drivers / smc91111.c
CommitLineData
fe8c2806
WD
1/*------------------------------------------------------------------------
2 . smc91111.c
3 . This is a driver for SMSC's 91C111 single-chip Ethernet device.
4 .
5 . (C) Copyright 2002
6 . Sysgo Real-Time Solutions, GmbH <www.elinos.com>
7 . Rolf Offermanns <rof@sysgo.de>
8 .
9 . Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
42dfe7a1 10 . Developed by Simple Network Magic Corporation (SNMC)
fe8c2806
WD
11 . Copyright (C) 1996 by Erik Stahlman (ES)
12 .
13 . This program is free software; you can redistribute it and/or modify
14 . it under the terms of the GNU General Public License as published by
15 . the Free Software Foundation; either version 2 of the License, or
16 . (at your option) any later version.
17 .
18 . This program is distributed in the hope that it will be useful,
19 . but WITHOUT ANY WARRANTY; without even the implied warranty of
42dfe7a1 20 . MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
fe8c2806
WD
21 . GNU General Public License for more details.
22 .
23 . You should have received a copy of the GNU General Public License
24 . along with this program; if not, write to the Free Software
42dfe7a1 25 . Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
fe8c2806
WD
26 .
27 . Information contained in this file was obtained from the LAN91C111
28 . manual from SMC. To get a copy, if you really want one, you can find
29 . information under www.smsc.com.
30 .
31 .
32 . "Features" of the SMC chip:
33 . Integrated PHY/MAC for 10/100BaseT Operation
34 . Supports internal and external MII
35 . Integrated 8K packet memory
36 . EEPROM interface for configuration
37 .
38 . Arguments:
42dfe7a1 39 . io = for the base address
fe8c2806
WD
40 . irq = for the IRQ
41 .
42 . author:
42dfe7a1
WD
43 . Erik Stahlman ( erik@vt.edu )
44 . Daris A Nevil ( dnevil@snmc.com )
fe8c2806
WD
45 .
46 .
47 . Hardware multicast code from Peter Cammaert ( pc@denkart.be )
48 .
49 . Sources:
42dfe7a1
WD
50 . o SMSC LAN91C111 databook (www.smsc.com)
51 . o smc9194.c by Erik Stahlman
52 . o skeleton.c by Donald Becker ( becker@cesdis.gsfc.nasa.gov )
fe8c2806
WD
53 .
54 . History:
42dfe7a1 55 . 06/19/03 Richard Woodruff Made u-boot environment aware and added mac addr checks.
fe8c2806 56 . 10/17/01 Marco Hasewinkel Modify for DNP/1110
42dfe7a1
WD
57 . 07/25/01 Woojung Huh Modify for ADS Bitsy
58 . 04/25/01 Daris A Nevil Initial public release through SMSC
59 . 03/16/01 Daris A Nevil Modified smc9194.c for use with LAN91C111
fe8c2806
WD
60 ----------------------------------------------------------------------------*/
61
62#include <common.h>
63#include <command.h>
f39748ae 64#include <config.h>
fe8c2806
WD
65#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
8bf3b005
WD
75#define SMC_DEBUG 0
76
77#if SMC_DEBUG > 1
fe8c2806
WD
78static const char version[] =
79 "smc91111.c:v1.0 04/25/01 by Daris A Nevil (dnevil@snmc.com)\n";
8bf3b005 80#endif
fe8c2806 81
f39748ae
WD
82/* Autonegotiation timeout in seconds */
83#ifndef CONFIG_SMC_AUTONEG_TIMEOUT
84#define CONFIG_SMC_AUTONEG_TIMEOUT 10
85#endif
86
fe8c2806
WD
87/*------------------------------------------------------------------------
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 .
42dfe7a1 122 . The internal workings of the driver. If you are changing anything
fe8c2806
WD
123 . 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
42dfe7a1 147#ifdef CONFIG_SMC_USE_32_BIT
fe8c2806
WD
148#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
fe8c2806
WD
164/*
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*/
0b97ab14 185static int smc_open(bd_t *bd);
fe8c2806
WD
186
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
0b97ab14 208/* See if a MAC address is defined in the current environment. If so use it. If not
8bde7f77 209 . print a warning and set the environment and other globals with the default.
0b97ab14
WD
210 . 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);
fe8c2806
WD
214
215/*
216 ------------------------------------------------------------
217 .
218 . Internal routines
219 .
220 ------------------------------------------------------------
221*/
222
c3c7f861
WD
223#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
8bf3b005 294static char unsigned smc_mac_addr[6] = {0x02, 0x80, 0xad, 0x20, 0x31, 0xb8};
fe8c2806
WD
295
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
0b97ab14 311 * mac address, call smc_get_mac_addr as a part of the board initialization.
fe8c2806
WD
312 */
313
314#if 0
315void smc_get_macaddr( byte *addr ) {
316 /* MAC ADDRESS AT FLASHBLOCK 1 / OFFSET 0x10 */
8bde7f77 317 unsigned char *dnp1110_mac = (unsigned char *) (0xE8000000 + 0x20010);
fe8c2806
WD
318 int i;
319
320
8bde7f77
WD
321 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 }
fe8c2806
WD
329}
330#endif /* 0 */
331
332/***********************************************
42dfe7a1 333 * Show available memory *
fe8c2806
WD
334 ***********************************************/
335void dump_memory_info(void)
336{
8bde7f77
WD
337 word mem_info;
338 word old_bank;
fe8c2806 339
8bde7f77 340 old_bank = SMC_inw(BANK_SELECT)&0xF;
fe8c2806 341
8bde7f77
WD
342 SMC_SELECT_BANK(0);
343 mem_info = SMC_inw( MIR_REG );
344 PRINTK2("Memory: %4d available\n", (mem_info >> 8)*2048);
fe8c2806 345
8bde7f77 346 SMC_SELECT_BANK(old_bank);
fe8c2806
WD
347}
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
fe8c2806
WD
358/* 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
b56ddc63
WD
377static 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);
382
383 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 }
fe8c2806 390 }
fe8c2806 391
b56ddc63
WD
392 /* restore old bank selection */
393 SMC_SELECT_BANK (old_bank);
fe8c2806 394
b56ddc63
WD
395 if (is_timeout)
396 return 1;
397 else
398 return 0;
fe8c2806
WD
399}
400
487778b7 401/* Only one release command at a time, please */
b56ddc63 402static inline void smc_wait_mmu_release_complete (void)
487778b7
WD
403{
404 int count = 0;
b56ddc63 405
487778b7 406 /* assume bank 2 selected */
b56ddc63
WD
407 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
408 udelay (1); /* Wait until not busy */
409 if (++count > 200)
410 break;
487778b7
WD
411 }
412}
413
fe8c2806
WD
414/*
415 . Function: smc_reset( void )
416 . Purpose:
42dfe7a1
WD
417 . This sets the SMC91111 chip to its normal state, hopefully from whatever
418 . mess that any other DOS driver has put it in.
fe8c2806
WD
419 .
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*/
b56ddc63 431static void smc_reset (void)
fe8c2806 432{
f39748ae 433 PRINTK2 ("%s: smc_reset\n", SMC_DEV_NAME);
fe8c2806
WD
434
435 /* This resets the registers mostly to defaults, but doesn't
436 affect EEPROM. That seems unnecessary */
b56ddc63
WD
437 SMC_SELECT_BANK (0);
438 SMC_outw (RCR_SOFTRST, RCR_REG);
fe8c2806
WD
439
440 /* Setup the Configuration Register */
441 /* This is necessary because the CONFIG_REG is not affected */
442 /* by a soft reset */
443
b56ddc63 444 SMC_SELECT_BANK (1);
fe8c2806 445#if defined(CONFIG_SMC91111_EXT_PHY)
b56ddc63 446 SMC_outw (CONFIG_DEFAULT | CONFIG_EXT_PHY, CONFIG_REG);
fe8c2806 447#else
b56ddc63 448 SMC_outw (CONFIG_DEFAULT, CONFIG_REG);
fe8c2806
WD
449#endif
450
451
452 /* Release from possible power-down state */
453 /* Configuration register is not affected by Soft Reset */
b56ddc63 454 SMC_outw (SMC_inw (CONFIG_REG) | CONFIG_EPH_POWER_EN, CONFIG_REG);
fe8c2806 455
b56ddc63 456 SMC_SELECT_BANK (0);
fe8c2806
WD
457
458 /* this should pause enough for the chip to be happy */
b56ddc63 459 udelay (10);
fe8c2806
WD
460
461 /* Disable transmit and receive functionality */
b56ddc63
WD
462 SMC_outw (RCR_CLEAR, RCR_REG);
463 SMC_outw (TCR_CLEAR, TCR_REG);
fe8c2806
WD
464
465 /* set the control register */
b56ddc63
WD
466 SMC_SELECT_BANK (1);
467 SMC_outw (CTL_DEFAULT, CTL_REG);
fe8c2806
WD
468
469 /* Reset the MMU */
b56ddc63
WD
470 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 */
fe8c2806
WD
475
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
8bde7f77 478 of issuing another MMU command right after this */
fe8c2806
WD
479
480 /* Disable all interrupts */
b56ddc63 481 SMC_outb (0, IM_REG);
fe8c2806
WD
482}
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{
f39748ae 494 PRINTK2("%s: smc_enable\n", SMC_DEV_NAME);
fe8c2806
WD
495 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
42dfe7a1 516 . in the register space. Empirical results do not show this working.
fe8c2806
WD
517*/
518static void smc_shutdown()
519{
f39748ae 520 PRINTK2(CARDNAME ": smc_shutdown\n");
fe8c2806
WD
521
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:
42dfe7a1 539 . First, see if a saved_skb is available.
fe8c2806
WD
540 . ( 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
42dfe7a1 547 . Tell the card to send it
fe8c2806 548 . Enable the transmit interrupt, so I know if it failed
42dfe7a1 549 . Free the kernel data if I actually sent it.
fe8c2806 550*/
b56ddc63 551static int smc_send_packet (volatile void *packet, int packet_length)
fe8c2806 552{
b56ddc63
WD
553 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;
518e2e1a
WD
561 byte saved_pnr;
562 word saved_ptr;
fe8c2806 563
518e2e1a 564 /* save PTR and PNR registers before manipulation */
b79a11cc 565 SMC_SELECT_BANK (2);
518e2e1a
WD
566 saved_pnr = SMC_inb( PN_REG );
567 saved_ptr = SMC_inw( PTR_REG );
fe8c2806 568
f39748ae 569 PRINTK3 ("%s: smc_hardware_send_packet\n", SMC_DEV_NAME);
fe8c2806
WD
570
571 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
572
573 /* allocate memory
b56ddc63
WD
574 ** 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 */
587
588 if (numPages > 7) {
589 printf ("%s: Far too big packet error. \n", SMC_DEV_NAME);
fe8c2806
WD
590 return 0;
591 }
592
593 /* now, try to allocate the memory */
b56ddc63
WD
594 SMC_SELECT_BANK (2);
595 SMC_outw (MC_ALLOC | numPages, MMU_CMD_REG);
fe8c2806 596
dc7c9a1a 597 /* FIXME: the ALLOC_INT bit never gets set *
42dfe7a1
WD
598 * so the following will always give a *
599 * memory allocation error. *
600 * same code works in armboot though *
dc7c9a1a
WD
601 * -ro
602 */
603
fe8c2806
WD
604again:
605 try++;
606 time_out = MEMORY_WAIT_TIME;
607 do {
b56ddc63
WD
608 status = SMC_inb (SMC91111_INT_REG);
609 if (status & IM_ALLOC_INT) {
fe8c2806 610 /* acknowledge the interrupt */
b56ddc63 611 SMC_outb (IM_ALLOC_INT, SMC91111_INT_REG);
8bde7f77 612 break;
fe8c2806 613 }
b56ddc63
WD
614 } while (--time_out);
615
616 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;
fe8c2806
WD
623 }
624
b56ddc63
WD
625 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
626 SMC_DEV_NAME, try);
fe8c2806
WD
627
628 /* I can send the packet now.. */
629
630 ioaddr = SMC_BASE_ADDRESS;
631
b56ddc63 632 buf = (byte *) packet;
fe8c2806
WD
633
634 /* If I get here, I _know_ there is a packet slot waiting for me */
b56ddc63
WD
635 packet_no = SMC_inb (AR_REG);
636 if (packet_no & AR_FAILED) {
fe8c2806 637 /* or isn't there? BAD CHIP! */
b56ddc63 638 printf ("%s: Memory allocation failed. \n", SMC_DEV_NAME);
fe8c2806
WD
639 return 0;
640 }
641
642 /* we have a packet address, so tell the card to use it */
b56ddc63 643 SMC_outb (packet_no, PN_REG);
fe8c2806 644
b79a11cc
WD
645 /* do not write new ptr value if Write data fifo not empty */
646 while ( saved_ptr & PTR_NOTEMPTY )
518e2e1a
WD
647 printf ("Write data fifo not empty!\n");
648
fe8c2806 649 /* point to the beginning of the packet */
b56ddc63 650 SMC_outw (PTR_AUTOINC, PTR_REG);
fe8c2806 651
b56ddc63
WD
652 PRINTK3 ("%s: Trying to xmit packet of length %x\n",
653 SMC_DEV_NAME, length);
fe8c2806
WD
654
655#if SMC_DEBUG > 2
b56ddc63
WD
656 printf ("Transmitting Packet\n");
657 print_packet (buf, length);
fe8c2806
WD
658#endif
659
660 /* send the packet length ( +6 for status, length and ctl byte )
8bde7f77 661 and the status word ( set to zeros ) */
fe8c2806 662#ifdef USE_32_BIT
b56ddc63 663 SMC_outl ((length + 6) << 16, SMC91111_DATA_REG);
fe8c2806 664#else
b56ddc63
WD
665 SMC_outw (0, SMC91111_DATA_REG);
666 /* send the packet length ( +6 for status words, length, and ctl */
667 SMC_outw ((length + 6), SMC91111_DATA_REG);
fe8c2806
WD
668#endif
669
670 /* send the actual data
b56ddc63
WD
671 . I _think_ it's faster to send the longs first, and then
672 . mop up by sending the last word. It depends heavily
42dfe7a1 673 . on alignment, at least on the 486. Maybe it would be
b56ddc63
WD
674 . a good idea to check which is optimal? But that could take
675 . almost as much time as is saved?
676 */
fe8c2806 677#ifdef USE_32_BIT
b56ddc63
WD
678 SMC_outsl (SMC91111_DATA_REG, buf, length >> 2);
679 if (length & 0x2)
680 SMC_outw (*((word *) (buf + (length & 0xFFFFFFFC))),
681 SMC91111_DATA_REG);
fe8c2806 682#else
b56ddc63 683 SMC_outsw (SMC91111_DATA_REG, buf, (length) >> 1);
fe8c2806
WD
684#endif /* USE_32_BIT */
685
42dfe7a1 686 /* Send the last byte, if there is one. */
b56ddc63
WD
687 if ((length & 1) == 0) {
688 SMC_outw (0, SMC91111_DATA_REG);
fe8c2806 689 } else {
b56ddc63 690 SMC_outw (buf[length - 1] | 0x2000, SMC91111_DATA_REG);
fe8c2806
WD
691 }
692
693 /* and let the chipset deal with it */
b56ddc63 694 SMC_outw (MC_ENQUEUE, MMU_CMD_REG);
fe8c2806
WD
695
696 /* poll for TX INT */
518e2e1a
WD
697 /* if (poll4int (IM_TX_INT, SMC_TX_TIMEOUT)) { */
698 /* poll for TX_EMPTY INT - autorelease enabled */
699 if (poll4int(IM_TX_EMPTY_INT, SMC_TX_TIMEOUT)) {
fe8c2806 700 /* sending failed */
b56ddc63 701 PRINTK2 ("%s: TX timeout, sending failed...\n", SMC_DEV_NAME);
fe8c2806
WD
702
703 /* release packet */
518e2e1a
WD
704 /* no need to release, MMU does that now */
705 /* SMC_outw (MC_FREEPKT, MMU_CMD_REG); */
fe8c2806 706
8bde7f77 707 /* wait for MMU getting ready (low) */
b56ddc63
WD
708 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
709 udelay (10);
8bde7f77 710 }
fe8c2806 711
b56ddc63 712 PRINTK2 ("MMU ready\n");
fe8c2806
WD
713
714
715 return 0;
716 } else {
717 /* ack. int */
518e2e1a
WD
718 SMC_outb (IM_TX_EMPTY_INT, SMC91111_INT_REG);
719 /* SMC_outb (IM_TX_INT, SMC91111_INT_REG); */
b56ddc63
WD
720 PRINTK2 ("%s: Sent packet of length %d \n", SMC_DEV_NAME,
721 length);
fe8c2806
WD
722
723 /* release packet */
518e2e1a
WD
724 /* no need to release, MMU does that now */
725 /* SMC_outw (MC_FREEPKT, MMU_CMD_REG); */
fe8c2806 726
8bde7f77 727 /* wait for MMU getting ready (low) */
b56ddc63
WD
728 while (SMC_inw (MMU_CMD_REG) & MC_BUSY) {
729 udelay (10);
8bde7f77 730 }
fe8c2806 731
b56ddc63 732 PRINTK2 ("MMU ready\n");
fe8c2806
WD
733
734
735 }
736
518e2e1a
WD
737 /* restore previously saved registers */
738 SMC_outb( saved_pnr, PN_REG );
739 SMC_outw( saved_ptr, PTR_REG );
740
fe8c2806
WD
741 return length;
742}
743
744/*-------------------------------------------------------------------------
745 |
746 | smc_destructor( struct net_device * dev )
747 | Input parameters:
748 | dev, pointer to the device structure
749 |
750 | Output:
751 | None.
752 |
753 ---------------------------------------------------------------------------
754*/
755void smc_destructor()
756{
f39748ae 757 PRINTK2(CARDNAME ": smc_destructor\n");
fe8c2806
WD
758}
759
760
761/*
762 * Open and Initialize the board
763 *
764 * Set up everything, reset the card, etc ..
765 *
766 */
b56ddc63 767static int smc_open (bd_t * bd)
fe8c2806 768{
b56ddc63 769 int i, err;
fe8c2806 770
f39748ae 771 PRINTK2 ("%s: smc_open\n", SMC_DEV_NAME);
fe8c2806
WD
772
773 /* reset the hardware */
b56ddc63
WD
774 smc_reset ();
775 smc_enable ();
fe8c2806
WD
776
777 /* Configure the PHY */
778#ifndef CONFIG_SMC91111_EXT_PHY
b56ddc63 779 smc_phy_configure ();
fe8c2806
WD
780#endif
781
fe8c2806
WD
782 /* conservative setting (10Mbps, HalfDuplex, no AutoNeg.) */
783/* SMC_SELECT_BANK(0); */
784/* SMC_outw(0, RPC_REG); */
b56ddc63 785 SMC_SELECT_BANK (1);
487778b7 786
b56ddc63
WD
787 err = smc_get_ethaddr (bd); /* set smc_mac_addr, and sync it with u-boot globals */
788 if (err < 0) {
42dfe7a1 789 memset (bd->bi_enetaddr, 0, 6); /* hack to make error stick! upper code will abort if not set */
b56ddc63
WD
790 return (-1); /* upper code ignores this, but NOT bi_enetaddr */
791 }
fe8c2806 792#ifdef USE_32_BIT
b56ddc63 793 for (i = 0; i < 6; i += 2) {
fe8c2806
WD
794 word address;
795
b56ddc63
WD
796 address = smc_mac_addr[i + 1] << 8;
797 address |= smc_mac_addr[i];
39539887 798 SMC_outw (address, (ADDR0_REG + i));
fe8c2806
WD
799 }
800#else
b56ddc63 801 for (i = 0; i < 6; i++)
39539887 802 SMC_outb (smc_mac_addr[i], (ADDR0_REG + i));
fe8c2806
WD
803#endif
804
805 return 0;
806}
807
fe8c2806
WD
808/*-------------------------------------------------------------
809 .
810 . smc_rcv - receive a packet from the card
811 .
812 . There is ( at least ) a packet waiting to be read from
813 . chip-memory.
814 .
815 . o Read the status
816 . o If an error, record it
817 . o otherwise, read in the packet
818 --------------------------------------------------------------
819*/
820static int smc_rcv()
821{
42dfe7a1 822 int packet_number;
fe8c2806
WD
823 word status;
824 word packet_length;
42dfe7a1 825 int is_error = 0;
fe8c2806
WD
826#ifdef USE_32_BIT
827 dword stat_len;
828#endif
518e2e1a
WD
829 byte saved_pnr;
830 word saved_ptr;
fe8c2806 831
fe8c2806 832 SMC_SELECT_BANK(2);
518e2e1a
WD
833 /* save PTR and PTR registers */
834 saved_pnr = SMC_inb( PN_REG );
835 saved_ptr = SMC_inw( PTR_REG );
836
fe8c2806
WD
837 packet_number = SMC_inw( RXFIFO_REG );
838
839 if ( packet_number & RXFIFO_REMPTY ) {
840
841 return 0;
842 }
843
f39748ae 844 PRINTK3("%s: smc_rcv\n", SMC_DEV_NAME);
fe8c2806
WD
845 /* start reading from the start of the packet */
846 SMC_outw( PTR_READ | PTR_RCV | PTR_AUTOINC, PTR_REG );
847
848 /* First two words are status and packet_length */
849#ifdef USE_32_BIT
850 stat_len = SMC_inl(SMC91111_DATA_REG);
851 status = stat_len & 0xffff;
852 packet_length = stat_len >> 16;
853#else
42dfe7a1
WD
854 status = SMC_inw( SMC91111_DATA_REG );
855 packet_length = SMC_inw( SMC91111_DATA_REG );
fe8c2806
WD
856#endif
857
858 packet_length &= 0x07ff; /* mask off top bits */
859
860 PRINTK2("RCV: STATUS %4x LENGTH %4x\n", status, packet_length );
861
862 if ( !(status & RS_ERRORS ) ){
863 /* Adjust for having already read the first two words */
864 packet_length -= 4; /*4; */
865
866
fe8c2806
WD
867 /* set odd length for bug in LAN91C111, */
868 /* which never sets RS_ODDFRAME */
869 /* TODO ? */
870
871
872#ifdef USE_32_BIT
873 PRINTK3(" Reading %d dwords (and %d bytes) \n",
874 packet_length >> 2, packet_length & 3 );
875 /* QUESTION: Like in the TX routine, do I want
876 to send the DWORDs or the bytes first, or some
877 mixture. A mixture might improve already slow PIO
42dfe7a1 878 performance */
fe8c2806
WD
879 SMC_insl( SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 2 );
880 /* read the left over bytes */
881 if (packet_length & 3) {
882 int i;
883
699b13a6 884 byte *tail = (byte *)(NetRxPackets[0] + (packet_length & ~3));
fe8c2806
WD
885 dword leftover = SMC_inl(SMC91111_DATA_REG);
886 for (i=0; i<(packet_length & 3); i++)
887 *tail++ = (byte) (leftover >> (8*i)) & 0xff;
888 }
889#else
890 PRINTK3(" Reading %d words and %d byte(s) \n",
891 (packet_length >> 1 ), packet_length & 1 );
892 SMC_insw(SMC91111_DATA_REG , NetRxPackets[0], packet_length >> 1);
893
894#endif /* USE_32_BIT */
895
896#if SMC_DEBUG > 2
897 printf("Receiving Packet\n");
898 print_packet( NetRxPackets[0], packet_length );
899#endif
900 } else {
901 /* error ... */
902 /* TODO ? */
903 is_error = 1;
904 }
905
906 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
907 udelay(1); /* Wait until not busy */
908
909 /* error or good, tell the card to get rid of this packet */
910 SMC_outw( MC_RELEASE, MMU_CMD_REG );
911
912 while ( SMC_inw( MMU_CMD_REG ) & MC_BUSY )
913 udelay(1); /* Wait until not busy */
914
518e2e1a
WD
915 /* restore saved registers */
916 SMC_outb( saved_pnr, PN_REG );
917 SMC_outw( saved_ptr, PTR_REG );
918
fe8c2806
WD
919 if (!is_error) {
920 /* Pass the packet up to the protocol layers. */
921 NetReceive(NetRxPackets[0], packet_length);
922 return packet_length;
923 } else {
924 return 0;
925 }
926
927}
928
929
fe8c2806
WD
930/*----------------------------------------------------
931 . smc_close
932 .
933 . this makes the board clean up everything that it can
42dfe7a1 934 . and not talk to the outside world. Caused by
fe8c2806
WD
935 . an 'ifconfig ethX down'
936 .
937 -----------------------------------------------------*/
938static int smc_close()
939{
f39748ae 940 PRINTK2("%s: smc_close\n", SMC_DEV_NAME);
fe8c2806
WD
941
942 /* clear everything */
943 smc_shutdown();
944
945 return 0;
946}
947
948
949#if 0
950/*------------------------------------------------------------
951 . Modify a bit in the LAN91C111 register set
952 .-------------------------------------------------------------*/
953static word smc_modify_regbit(int bank, int ioaddr, int reg,
954 unsigned int bit, int val)
955{
956 word regval;
957
958 SMC_SELECT_BANK( bank );
959
960 regval = SMC_inw( reg );
961 if (val)
962 regval |= bit;
963 else
964 regval &= ~bit;
965
966 SMC_outw( regval, 0 );
967 return(regval);
968}
969
970
971/*------------------------------------------------------------
972 . Retrieve a bit in the LAN91C111 register set
973 .-------------------------------------------------------------*/
974static int smc_get_regbit(int bank, int ioaddr, int reg, unsigned int bit)
975{
976 SMC_SELECT_BANK( bank );
977 if ( SMC_inw( reg ) & bit)
978 return(1);
979 else
980 return(0);
981}
982
983
984/*------------------------------------------------------------
985 . Modify a LAN91C111 register (word access only)
986 .-------------------------------------------------------------*/
987static void smc_modify_reg(int bank, int ioaddr, int reg, word val)
988{
989 SMC_SELECT_BANK( bank );
990 SMC_outw( val, reg );
991}
992
993
994/*------------------------------------------------------------
995 . Retrieve a LAN91C111 register (word access only)
996 .-------------------------------------------------------------*/
997static int smc_get_reg(int bank, int ioaddr, int reg)
998{
999 SMC_SELECT_BANK( bank );
1000 return(SMC_inw( reg ));
1001}
1002
1003#endif /* 0 */
1004
1005/*---PHY CONTROL AND CONFIGURATION----------------------------------------- */
1006
1007#if (SMC_DEBUG > 2 )
1008
1009/*------------------------------------------------------------
1010 . Debugging function for viewing MII Management serial bitstream
1011 .-------------------------------------------------------------*/
b56ddc63 1012static void smc_dump_mii_stream (byte * bits, int size)
fe8c2806
WD
1013{
1014 int i;
1015
b56ddc63
WD
1016 printf ("BIT#:");
1017 for (i = 0; i < size; ++i) {
1018 printf ("%d", i % 10);
1019 }
fe8c2806 1020
b56ddc63
WD
1021 printf ("\nMDOE:");
1022 for (i = 0; i < size; ++i) {
fe8c2806 1023 if (bits[i] & MII_MDOE)
b56ddc63 1024 printf ("1");
fe8c2806 1025 else
b56ddc63
WD
1026 printf ("0");
1027 }
fe8c2806 1028
b56ddc63
WD
1029 printf ("\nMDO :");
1030 for (i = 0; i < size; ++i) {
fe8c2806 1031 if (bits[i] & MII_MDO)
b56ddc63 1032 printf ("1");
fe8c2806 1033 else
b56ddc63
WD
1034 printf ("0");
1035 }
fe8c2806 1036
b56ddc63
WD
1037 printf ("\nMDI :");
1038 for (i = 0; i < size; ++i) {
fe8c2806 1039 if (bits[i] & MII_MDI)
b56ddc63 1040 printf ("1");
fe8c2806 1041 else
b56ddc63
WD
1042 printf ("0");
1043 }
fe8c2806 1044
b56ddc63 1045 printf ("\n");
fe8c2806
WD
1046}
1047#endif
1048
1049/*------------------------------------------------------------
1050 . Reads a register from the MII Management serial interface
1051 .-------------------------------------------------------------*/
1052#ifndef CONFIG_SMC91111_EXT_PHY
b56ddc63 1053static word smc_read_phy_register (byte phyreg)
fe8c2806
WD
1054{
1055 int oldBank;
1056 int i;
1057 byte mask;
1058 word mii_reg;
1059 byte bits[64];
1060 int clk_idx = 0;
1061 int input_idx;
1062 word phydata;
1063 byte phyaddr = SMC_PHY_ADDR;
1064
1065 /* 32 consecutive ones on MDO to establish sync */
1066 for (i = 0; i < 32; ++i)
1067 bits[clk_idx++] = MII_MDOE | MII_MDO;
1068
1069 /* Start code <01> */
1070 bits[clk_idx++] = MII_MDOE;
1071 bits[clk_idx++] = MII_MDOE | MII_MDO;
1072
1073 /* Read command <10> */
1074 bits[clk_idx++] = MII_MDOE | MII_MDO;
1075 bits[clk_idx++] = MII_MDOE;
1076
1077 /* Output the PHY address, msb first */
b56ddc63
WD
1078 mask = (byte) 0x10;
1079 for (i = 0; i < 5; ++i) {
fe8c2806
WD
1080 if (phyaddr & mask)
1081 bits[clk_idx++] = MII_MDOE | MII_MDO;
1082 else
1083 bits[clk_idx++] = MII_MDOE;
1084
1085 /* Shift to next lowest bit */
1086 mask >>= 1;
b56ddc63 1087 }
fe8c2806
WD
1088
1089 /* Output the phy register number, msb first */
b56ddc63
WD
1090 mask = (byte) 0x10;
1091 for (i = 0; i < 5; ++i) {
fe8c2806
WD
1092 if (phyreg & mask)
1093 bits[clk_idx++] = MII_MDOE | MII_MDO;
1094 else
1095 bits[clk_idx++] = MII_MDOE;
1096
1097 /* Shift to next lowest bit */
1098 mask >>= 1;
b56ddc63 1099 }
fe8c2806
WD
1100
1101 /* Tristate and turnaround (2 bit times) */
1102 bits[clk_idx++] = 0;
1103 /*bits[clk_idx++] = 0; */
1104
1105 /* Input starts at this bit time */
1106 input_idx = clk_idx;
1107
1108 /* Will input 16 bits */
1109 for (i = 0; i < 16; ++i)
1110 bits[clk_idx++] = 0;
1111
1112 /* Final clock bit */
1113 bits[clk_idx++] = 0;
1114
1115 /* Save the current bank */
b56ddc63 1116 oldBank = SMC_inw (BANK_SELECT);
fe8c2806
WD
1117
1118 /* Select bank 3 */
b56ddc63 1119 SMC_SELECT_BANK (3);
fe8c2806
WD
1120
1121 /* Get the current MII register value */
b56ddc63 1122 mii_reg = SMC_inw (MII_REG);
fe8c2806
WD
1123
1124 /* Turn off all MII Interface bits */
b56ddc63 1125 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
fe8c2806
WD
1126
1127 /* Clock all 64 cycles */
b56ddc63 1128 for (i = 0; i < sizeof bits; ++i) {
fe8c2806 1129 /* Clock Low - output data */
b56ddc63
WD
1130 SMC_outw (mii_reg | bits[i], MII_REG);
1131 udelay (SMC_PHY_CLOCK_DELAY);
fe8c2806
WD
1132
1133
1134 /* Clock Hi - input data */
b56ddc63
WD
1135 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1136 udelay (SMC_PHY_CLOCK_DELAY);
1137 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1138 }
fe8c2806
WD
1139
1140 /* Return to idle state */
1141 /* Set clock to low, data to low, and output tristated */
b56ddc63
WD
1142 SMC_outw (mii_reg, MII_REG);
1143 udelay (SMC_PHY_CLOCK_DELAY);
fe8c2806
WD
1144
1145 /* Restore original bank select */
b56ddc63 1146 SMC_SELECT_BANK (oldBank);
fe8c2806
WD
1147
1148 /* Recover input data */
1149 phydata = 0;
b56ddc63 1150 for (i = 0; i < 16; ++i) {
fe8c2806
WD
1151 phydata <<= 1;
1152
1153 if (bits[input_idx++] & MII_MDI)
1154 phydata |= 0x0001;
b56ddc63 1155 }
fe8c2806
WD
1156
1157#if (SMC_DEBUG > 2 )
b56ddc63 1158 printf ("smc_read_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
fe8c2806 1159 phyaddr, phyreg, phydata);
b56ddc63 1160 smc_dump_mii_stream (bits, sizeof bits);
fe8c2806
WD
1161#endif
1162
b56ddc63 1163 return (phydata);
fe8c2806
WD
1164}
1165
1166
1167/*------------------------------------------------------------
1168 . Writes a register to the MII Management serial interface
1169 .-------------------------------------------------------------*/
b56ddc63 1170static void smc_write_phy_register (byte phyreg, word phydata)
fe8c2806
WD
1171{
1172 int oldBank;
1173 int i;
1174 word mask;
1175 word mii_reg;
1176 byte bits[65];
1177 int clk_idx = 0;
1178 byte phyaddr = SMC_PHY_ADDR;
1179
1180 /* 32 consecutive ones on MDO to establish sync */
1181 for (i = 0; i < 32; ++i)
1182 bits[clk_idx++] = MII_MDOE | MII_MDO;
1183
1184 /* Start code <01> */
1185 bits[clk_idx++] = MII_MDOE;
1186 bits[clk_idx++] = MII_MDOE | MII_MDO;
1187
1188 /* Write command <01> */
1189 bits[clk_idx++] = MII_MDOE;
1190 bits[clk_idx++] = MII_MDOE | MII_MDO;
1191
1192 /* Output the PHY address, msb first */
b56ddc63
WD
1193 mask = (byte) 0x10;
1194 for (i = 0; i < 5; ++i) {
fe8c2806
WD
1195 if (phyaddr & mask)
1196 bits[clk_idx++] = MII_MDOE | MII_MDO;
1197 else
1198 bits[clk_idx++] = MII_MDOE;
1199
1200 /* Shift to next lowest bit */
1201 mask >>= 1;
b56ddc63 1202 }
fe8c2806
WD
1203
1204 /* Output the phy register number, msb first */
b56ddc63
WD
1205 mask = (byte) 0x10;
1206 for (i = 0; i < 5; ++i) {
fe8c2806
WD
1207 if (phyreg & mask)
1208 bits[clk_idx++] = MII_MDOE | MII_MDO;
1209 else
1210 bits[clk_idx++] = MII_MDOE;
1211
1212 /* Shift to next lowest bit */
1213 mask >>= 1;
b56ddc63 1214 }
fe8c2806
WD
1215
1216 /* Tristate and turnaround (2 bit times) */
1217 bits[clk_idx++] = 0;
1218 bits[clk_idx++] = 0;
1219
1220 /* Write out 16 bits of data, msb first */
1221 mask = 0x8000;
b56ddc63 1222 for (i = 0; i < 16; ++i) {
fe8c2806
WD
1223 if (phydata & mask)
1224 bits[clk_idx++] = MII_MDOE | MII_MDO;
1225 else
1226 bits[clk_idx++] = MII_MDOE;
1227
1228 /* Shift to next lowest bit */
1229 mask >>= 1;
b56ddc63 1230 }
fe8c2806
WD
1231
1232 /* Final clock bit (tristate) */
1233 bits[clk_idx++] = 0;
1234
1235 /* Save the current bank */
b56ddc63 1236 oldBank = SMC_inw (BANK_SELECT);
fe8c2806
WD
1237
1238 /* Select bank 3 */
b56ddc63 1239 SMC_SELECT_BANK (3);
fe8c2806
WD
1240
1241 /* Get the current MII register value */
b56ddc63 1242 mii_reg = SMC_inw (MII_REG);
fe8c2806
WD
1243
1244 /* Turn off all MII Interface bits */
b56ddc63 1245 mii_reg &= ~(MII_MDOE | MII_MCLK | MII_MDI | MII_MDO);
fe8c2806
WD
1246
1247 /* Clock all cycles */
b56ddc63 1248 for (i = 0; i < sizeof bits; ++i) {
fe8c2806 1249 /* Clock Low - output data */
b56ddc63
WD
1250 SMC_outw (mii_reg | bits[i], MII_REG);
1251 udelay (SMC_PHY_CLOCK_DELAY);
fe8c2806
WD
1252
1253
1254 /* Clock Hi - input data */
b56ddc63
WD
1255 SMC_outw (mii_reg | bits[i] | MII_MCLK, MII_REG);
1256 udelay (SMC_PHY_CLOCK_DELAY);
1257 bits[i] |= SMC_inw (MII_REG) & MII_MDI;
1258 }
fe8c2806
WD
1259
1260 /* Return to idle state */
1261 /* Set clock to low, data to low, and output tristated */
b56ddc63
WD
1262 SMC_outw (mii_reg, MII_REG);
1263 udelay (SMC_PHY_CLOCK_DELAY);
fe8c2806
WD
1264
1265 /* Restore original bank select */
b56ddc63 1266 SMC_SELECT_BANK (oldBank);
fe8c2806
WD
1267
1268#if (SMC_DEBUG > 2 )
b56ddc63 1269 printf ("smc_write_phy_register(): phyaddr=%x,phyreg=%x,phydata=%x\n",
fe8c2806 1270 phyaddr, phyreg, phydata);
b56ddc63 1271 smc_dump_mii_stream (bits, sizeof bits);
fe8c2806
WD
1272#endif
1273}
1274#endif /* !CONFIG_SMC91111_EXT_PHY */
1275
1276
fe8c2806
WD
1277/*------------------------------------------------------------
1278 . Waits the specified number of milliseconds - kernel friendly
1279 .-------------------------------------------------------------*/
1280#ifndef CONFIG_SMC91111_EXT_PHY
1281static void smc_wait_ms(unsigned int ms)
1282{
1283 udelay(ms*1000);
1284}
1285#endif /* !CONFIG_SMC91111_EXT_PHY */
1286
1287
fe8c2806
WD
1288/*------------------------------------------------------------
1289 . Configures the specified PHY using Autonegotiation. Calls
1290 . smc_phy_fixed() if the user has requested a certain config.
1291 .-------------------------------------------------------------*/
1292#ifndef CONFIG_SMC91111_EXT_PHY
b56ddc63 1293static void smc_phy_configure ()
fe8c2806
WD
1294{
1295 int timeout;
1296 byte phyaddr;
b56ddc63
WD
1297 word my_phy_caps; /* My PHY capabilities */
1298 word my_ad_caps; /* My Advertised capabilities */
1299 word status = 0; /*;my status = 0 */
fe8c2806
WD
1300 int failed = 0;
1301
f39748ae 1302 PRINTK3 ("%s: smc_program_phy()\n", SMC_DEV_NAME);
fe8c2806
WD
1303
1304
fe8c2806
WD
1305 /* Get the detected phy address */
1306 phyaddr = SMC_PHY_ADDR;
1307
1308 /* Reset the PHY, setting all other bits to zero */
b56ddc63 1309 smc_write_phy_register (PHY_CNTL_REG, PHY_CNTL_RST);
fe8c2806
WD
1310
1311 /* Wait for the reset to complete, or time out */
b56ddc63
WD
1312 timeout = 6; /* Wait up to 3 seconds */
1313 while (timeout--) {
1314 if (!(smc_read_phy_register (PHY_CNTL_REG)
1315 & PHY_CNTL_RST)) {
fe8c2806
WD
1316 /* reset complete */
1317 break;
fe8c2806
WD
1318 }
1319
b56ddc63
WD
1320 smc_wait_ms (500); /* wait 500 millisecs */
1321 }
1322
1323 if (timeout < 1) {
1324 printf ("%s:PHY reset timed out\n", SMC_DEV_NAME);
fe8c2806 1325 goto smc_phy_configure_exit;
b56ddc63 1326 }
fe8c2806
WD
1327
1328 /* Read PHY Register 18, Status Output */
1329 /* lp->lastPhy18 = smc_read_phy_register(PHY_INT_REG); */
1330
1331 /* Enable PHY Interrupts (for register 18) */
1332 /* Interrupts listed here are disabled */
8bf3b005 1333 smc_write_phy_register (PHY_MASK_REG, 0xffff);
fe8c2806
WD
1334
1335 /* Configure the Receive/Phy Control register */
b56ddc63
WD
1336 SMC_SELECT_BANK (0);
1337 SMC_outw (RPC_DEFAULT, RPC_REG);
fe8c2806
WD
1338
1339 /* Copy our capabilities from PHY_STAT_REG to PHY_AD_REG */
b56ddc63
WD
1340 my_phy_caps = smc_read_phy_register (PHY_STAT_REG);
1341 my_ad_caps = PHY_AD_CSMA; /* I am CSMA capable */
fe8c2806
WD
1342
1343 if (my_phy_caps & PHY_STAT_CAP_T4)
1344 my_ad_caps |= PHY_AD_T4;
1345
1346 if (my_phy_caps & PHY_STAT_CAP_TXF)
1347 my_ad_caps |= PHY_AD_TX_FDX;
1348
1349 if (my_phy_caps & PHY_STAT_CAP_TXH)
1350 my_ad_caps |= PHY_AD_TX_HDX;
1351
1352 if (my_phy_caps & PHY_STAT_CAP_TF)
1353 my_ad_caps |= PHY_AD_10_FDX;
1354
1355 if (my_phy_caps & PHY_STAT_CAP_TH)
1356 my_ad_caps |= PHY_AD_10_HDX;
1357
1358 /* Update our Auto-Neg Advertisement Register */
b56ddc63 1359 smc_write_phy_register (PHY_AD_REG, my_ad_caps);
fe8c2806 1360
518e2e1a
WD
1361 /* Read the register back. Without this, it appears that when */
1362 /* auto-negotiation is restarted, sometimes it isn't ready and */
1363 /* the link does not come up. */
1364 smc_read_phy_register(PHY_AD_REG);
1365
f39748ae
WD
1366 PRINTK2 ("%s: phy caps=%x\n", SMC_DEV_NAME, my_phy_caps);
1367 PRINTK2 ("%s: phy advertised caps=%x\n", SMC_DEV_NAME, my_ad_caps);
fe8c2806
WD
1368
1369 /* Restart auto-negotiation process in order to advertise my caps */
b56ddc63
WD
1370 smc_write_phy_register (PHY_CNTL_REG,
1371 PHY_CNTL_ANEG_EN | PHY_CNTL_ANEG_RST);
fe8c2806
WD
1372
1373 /* Wait for the auto-negotiation to complete. This may take from */
1374 /* 2 to 3 seconds. */
1375 /* Wait for the reset to complete, or time out */
f39748ae 1376 timeout = CONFIG_SMC_AUTONEG_TIMEOUT * 2;
b56ddc63 1377 while (timeout--) {
f39748ae 1378
b56ddc63
WD
1379 status = smc_read_phy_register (PHY_STAT_REG);
1380 if (status & PHY_STAT_ANEG_ACK) {
fe8c2806
WD
1381 /* auto-negotiate complete */
1382 break;
b56ddc63 1383 }
fe8c2806 1384
b56ddc63 1385 smc_wait_ms (500); /* wait 500 millisecs */
fe8c2806
WD
1386
1387 /* Restart auto-negotiation if remote fault */
b56ddc63 1388 if (status & PHY_STAT_REM_FLT) {
f39748ae 1389 printf ("%s: PHY remote fault detected\n",
b56ddc63 1390 SMC_DEV_NAME);
fe8c2806
WD
1391
1392 /* Restart auto-negotiation */
f39748ae 1393 printf ("%s: PHY restarting auto-negotiation\n",
fe8c2806 1394 SMC_DEV_NAME);
b56ddc63
WD
1395 smc_write_phy_register (PHY_CNTL_REG,
1396 PHY_CNTL_ANEG_EN |
1397 PHY_CNTL_ANEG_RST |
1398 PHY_CNTL_SPEED |
1399 PHY_CNTL_DPLX);
fe8c2806 1400 }
b56ddc63 1401 }
fe8c2806 1402
b56ddc63 1403 if (timeout < 1) {
f39748ae 1404 printf ("%s: PHY auto-negotiate timed out\n", SMC_DEV_NAME);
fe8c2806 1405 failed = 1;
b56ddc63 1406 }
fe8c2806
WD
1407
1408 /* Fail if we detected an auto-negotiate remote fault */
b56ddc63 1409 if (status & PHY_STAT_REM_FLT) {
f39748ae 1410 printf ("%s: PHY remote fault detected\n", SMC_DEV_NAME);
fe8c2806 1411 failed = 1;
b56ddc63 1412 }
fe8c2806
WD
1413
1414 /* Re-Configure the Receive/Phy Control register */
b56ddc63 1415 SMC_outw (RPC_DEFAULT, RPC_REG);
fe8c2806 1416
b56ddc63 1417 smc_phy_configure_exit:
fe8c2806
WD
1418
1419}
1420#endif /* !CONFIG_SMC91111_EXT_PHY */
1421
1422
1423#if SMC_DEBUG > 2
1424static void print_packet( byte * buf, int length )
1425{
8bde7f77
WD
1426 int i;
1427 int remainder;
1428 int lines;
fe8c2806 1429
8bde7f77 1430 printf("Packet of length %d \n", length );
fe8c2806
WD
1431
1432#if SMC_DEBUG > 3
8bde7f77
WD
1433 lines = length / 16;
1434 remainder = length % 16;
1435
1436 for ( i = 0; i < lines ; i ++ ) {
1437 int cur;
1438
1439 for ( cur = 0; cur < 8; cur ++ ) {
1440 byte a, b;
1441
1442 a = *(buf ++ );
1443 b = *(buf ++ );
1444 printf("%02x%02x ", a, b );
1445 }
1446 printf("\n");
1447 }
1448 for ( i = 0; i < remainder/2 ; i++ ) {
1449 byte a, b;
1450
1451 a = *(buf ++ );
1452 b = *(buf ++ );
1453 printf("%02x%02x ", a, b );
1454 }
1455 printf("\n");
fe8c2806 1456#endif
fe8c2806
WD
1457}
1458#endif
1459
1460int eth_init(bd_t *bd) {
0b97ab14 1461 return (smc_open(bd));
fe8c2806
WD
1462}
1463
1464void eth_halt() {
1465 smc_close();
1466}
1467
1468int eth_rx() {
1469 return smc_rcv();
1470}
1471
1472int eth_send(volatile void *packet, int length) {
1473 return smc_send_packet(packet, length);
1474}
1475
b56ddc63 1476int smc_get_ethaddr (bd_t * bd)
0b97ab14 1477{
b56ddc63
WD
1478 int env_size, rom_valid, env_present = 0, reg;
1479 char *s = NULL, *e, *v_mac, es[] = "11:22:33:44:55:66";
1480 uchar s_env_mac[64], v_env_mac[6], v_rom_mac[6];
1481
1482 env_size = getenv_r ("ethaddr", s_env_mac, sizeof (s_env_mac));
1483 if ((env_size > 0) && (env_size < sizeof (es))) { /* exit if env is bad */
1484 printf ("\n*** ERROR: ethaddr is not set properly!!\n");
1485 return (-1);
1486 }
1487
1488 if (env_size > 0) {
1489 env_present = 1;
1490 s = s_env_mac;
8bde7f77 1491 }
8bde7f77 1492
42dfe7a1 1493 for (reg = 0; reg < 6; ++reg) { /* turn string into mac value */
b56ddc63
WD
1494 v_env_mac[reg] = s ? simple_strtoul (s, &e, 16) : 0;
1495 if (s)
1496 s = (*e) ? e + 1 : e;
8bde7f77 1497 }
b56ddc63
WD
1498
1499 rom_valid = get_rom_mac (v_rom_mac); /* get ROM mac value if any */
1500
1501 if (!env_present) { /* if NO env */
1502 if (rom_valid) { /* but ROM is valid */
1503 v_mac = v_rom_mac;
1504 sprintf (s_env_mac, "%02X:%02X:%02X:%02X:%02X:%02X",
1505 v_mac[0], v_mac[1], v_mac[2], v_mac[3],
1506 v_mac[4], v_mac[5]);
1507 setenv ("ethaddr", s_env_mac);
1508 } else { /* no env, bad ROM */
1509 printf ("\n*** ERROR: ethaddr is NOT set !!\n");
1510 return (-1);
1511 }
1512 } else { /* good env, don't care ROM */
1513 v_mac = v_env_mac; /* always use a good env over a ROM */
1514 }
1515
42dfe7a1 1516 if (env_present && rom_valid) { /* if both env and ROM are good */
b56ddc63 1517 if (memcmp (v_env_mac, v_rom_mac, 6) != 0) {
b56ddc63
WD
1518 printf ("\nWarning: MAC addresses don't match:\n");
1519 printf ("\tHW MAC address: "
1520 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1521 v_rom_mac[0], v_rom_mac[1],
1522 v_rom_mac[2], v_rom_mac[3],
1523 v_rom_mac[4], v_rom_mac[5] );
1524 printf ("\t\"ethaddr\" value: "
1525 "%02X:%02X:%02X:%02X:%02X:%02X\n",
1526 v_env_mac[0], v_env_mac[1],
1527 v_env_mac[2], v_env_mac[3],
1528 v_env_mac[4], v_env_mac[5]) ;
1529 debug ("### Set MAC addr from environment\n");
b56ddc63
WD
1530 }
1531 }
1532 memcpy (bd->bi_enetaddr, v_mac, 6); /* update global address to match env (allows env changing) */
1533 smc_set_mac_addr (v_mac); /* use old function to update smc default */
3d3befa7 1534 PRINTK("Using MAC Address %02X:%02X:%02X:%02X:%02X:%02X\n", v_mac[0], v_mac[1],
42dfe7a1 1535 v_mac[2], v_mac[3], v_mac[4], v_mac[5]);
b56ddc63 1536 return (0);
0b97ab14
WD
1537}
1538
b56ddc63 1539int get_rom_mac (char *v_rom_mac)
0b97ab14 1540{
b56ddc63
WD
1541#ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
1542 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
0b97ab14 1543
b56ddc63
WD
1544 memcpy (v_rom_mac, hw_mac_addr, 6);
1545 return (1);
0b97ab14 1546#else
3d3befa7 1547 int i;
f39748ae
WD
1548 int valid_mac = 0;
1549
3d3befa7
WD
1550 SMC_SELECT_BANK (1);
1551 for (i=0; i<6; i++)
1552 {
39539887 1553 v_rom_mac[i] = SMC_inb ((ADDR0_REG + i));
f39748ae 1554 valid_mac |= v_rom_mac[i];
b56ddc63 1555 }
f39748ae
WD
1556
1557 return (valid_mac ? 1 : 0);
0b97ab14
WD
1558#endif
1559}
fe8c2806 1560#endif /* CONFIG_DRIVER_SMC91111 */