blob: bd6966ed76bedfc3d1de070e3e918a711eb23e94 [file] [log] [blame]
wdenk45219c42003-05-12 21:50:16 +00001/*------------------------------------------------------------------------
2 * lan91c96.c
3 * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
4 * on the SMC91111 driver from U-boot.
5 *
6 * (C) Copyright 2002
7 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
8 * Rolf Offermanns <rof@sysgo.de>
9 *
10 * Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
11 * Developed by Simple Network Magic Corporation (SNMC)
12 * Copyright (C) 1996 by Erik Stahlman (ES)
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * Information contained in this file was obtained from the LAN91C96
29 * manual from SMC. To get a copy, if you really want one, you can find
30 * information under www.smsc.com.
31 *
32 *
33 * "Features" of the SMC chip:
34 * 6144 byte packet memory. ( for the 91C96 )
35 * EEPROM for configuration
36 * AUI/TP selection ( mine has 10Base2/10BaseT select )
37 *
38 * Arguments:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +010039 * io = for the base address
wdenk45219c42003-05-12 21:50:16 +000040 * irq = for the IRQ
41 *
42 * author:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +010043 * Erik Stahlman ( erik@vt.edu )
44 * Daris A Nevil ( dnevil@snmc.com )
wdenk45219c42003-05-12 21:50:16 +000045 *
46 *
47 * Hardware multicast code from Peter Cammaert ( pc@denkart.be )
48 *
49 * Sources:
50 * o SMSC LAN91C96 databook (www.smsc.com)
51 * o smc91111.c (u-boot driver)
52 * o smc9194.c (linux kernel driver)
53 * o lan91c96.c (Intel Diagnostic Manager driver)
54 *
55 * History:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +010056 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
wdenk45219c42003-05-12 21:50:16 +000057 * for lan91c96
58 *---------------------------------------------------------------------------
59 */
60
61#include <common.h>
62#include <command.h>
Nishanth Menonb7ad4102009-10-16 00:06:35 -050063#include <malloc.h>
wdenk45219c42003-05-12 21:50:16 +000064#include "lan91c96.h"
65#include <net.h>
66
wdenk45219c42003-05-12 21:50:16 +000067/*------------------------------------------------------------------------
68 *
69 * Configuration options, for the experienced user to change.
70 *
71 -------------------------------------------------------------------------*/
72
73/* Use power-down feature of the chip */
74#define POWER_DOWN 0
75
76/*
77 * Wait time for memory to be free. This probably shouldn't be
78 * tuned that much, as waiting for this means nothing else happens
79 * in the system
80*/
81#define MEMORY_WAIT_TIME 16
82
83#define SMC_DEBUG 0
84
85#if (SMC_DEBUG > 2 )
86#define PRINTK3(args...) printf(args)
87#else
88#define PRINTK3(args...)
89#endif
90
91#if SMC_DEBUG > 1
92#define PRINTK2(args...) printf(args)
93#else
94#define PRINTK2(args...)
95#endif
96
97#ifdef SMC_DEBUG
98#define PRINTK(args...) printf(args)
99#else
100#define PRINTK(args...)
101#endif
102
103
104/*------------------------------------------------------------------------
105 *
106 * The internal workings of the driver. If you are changing anything
107 * here with the SMC stuff, you should have the datasheet and know
108 * what you are doing.
109 *
110 *------------------------------------------------------------------------
111 */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500112#define DRIVER_NAME "LAN91C96"
wdenk45219c42003-05-12 21:50:16 +0000113#define SMC_ALLOC_MAX_TRY 5
114#define SMC_TX_TIMEOUT 30
115
116#define ETH_ZLEN 60
117
118#ifdef CONFIG_LAN91C96_USE_32_BIT
119#define USE_32_BIT 1
120#else
121#undef USE_32_BIT
122#endif
123
wdenk5ca26792004-06-06 22:11:41 +0000124/* See if a MAC address is defined in the current environment. If so use it. If not
125 . print a warning and set the environment and other globals with the default.
126 . If an EEPROM is present it really should be consulted.
127*/
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500128static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
129static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
wdenk45219c42003-05-12 21:50:16 +0000130
wdenk45219c42003-05-12 21:50:16 +0000131/* ------------------------------------------------------------
132 * Internal routines
133 * ------------------------------------------------------------
134 */
135
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100136static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
wdenk45219c42003-05-12 21:50:16 +0000137
138/*
139 * This function must be called before smc_open() if you want to override
140 * the default mac address.
141 */
142
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500143static void smc_set_mac_addr(const unsigned char *addr)
wdenk45219c42003-05-12 21:50:16 +0000144{
145 int i;
146
147 for (i = 0; i < sizeof (smc_mac_addr); i++) {
148 smc_mac_addr[i] = addr[i];
149 }
150}
151
wdenk45219c42003-05-12 21:50:16 +0000152/***********************************************
153 * Show available memory *
154 ***********************************************/
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500155void dump_memory_info(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000156{
157 word mem_info;
158 word old_bank;
159
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500160 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
wdenk45219c42003-05-12 21:50:16 +0000161
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500162 SMC_SELECT_BANK(dev, 0);
163 mem_info = SMC_inw(dev, LAN91C96_MIR);
wdenk45219c42003-05-12 21:50:16 +0000164 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
165
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500166 SMC_SELECT_BANK(dev, old_bank);
wdenk45219c42003-05-12 21:50:16 +0000167}
168
169/*
170 * A rather simple routine to print out a packet for debugging purposes.
171 */
172#if SMC_DEBUG > 2
173static void print_packet (byte *, int);
174#endif
175
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500176static int poll4int (struct eth_device *dev, byte mask, int timeout)
wdenk45219c42003-05-12 21:50:16 +0000177{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200178 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
wdenk45219c42003-05-12 21:50:16 +0000179 int is_timeout = 0;
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500180 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
wdenk45219c42003-05-12 21:50:16 +0000181
182 PRINTK2 ("Polling...\n");
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500183 SMC_SELECT_BANK(dev, 2);
184 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
wdenk45219c42003-05-12 21:50:16 +0000185 if (get_timer (0) >= tmo) {
186 is_timeout = 1;
187 break;
188 }
189 }
190
191 /* restore old bank selection */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500192 SMC_SELECT_BANK(dev, old_bank);
wdenk45219c42003-05-12 21:50:16 +0000193
194 if (is_timeout)
195 return 1;
196 else
197 return 0;
198}
199
200/*
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500201 * Function: smc_reset
wdenk45219c42003-05-12 21:50:16 +0000202 * Purpose:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +0100203 * This sets the SMC91111 chip to its normal state, hopefully from whatever
204 * mess that any other DOS driver has put it in.
wdenk45219c42003-05-12 21:50:16 +0000205 *
206 * Maybe I should reset more registers to defaults in here? SOFTRST should
207 * do that for me.
208 *
209 * Method:
210 * 1. send a SOFT RESET
211 * 2. wait for it to finish
212 * 3. enable autorelease mode
213 * 4. reset the memory management unit
214 * 5. clear all interrupts
215 *
216*/
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500217static void smc_reset(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000218{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500219 PRINTK2("%s:smc_reset\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000220
221 /* This resets the registers mostly to defaults, but doesn't
222 affect EEPROM. That seems unnecessary */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500223 SMC_SELECT_BANK(dev, 0);
224 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
wdenk45219c42003-05-12 21:50:16 +0000225
226 udelay (10);
227
228 /* Disable transmit and receive functionality */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500229 SMC_outw(dev, 0, LAN91C96_RCR);
230 SMC_outw(dev, 0, LAN91C96_TCR);
wdenk45219c42003-05-12 21:50:16 +0000231
232 /* set the control register */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500233 SMC_SELECT_BANK(dev, 1);
234 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
wdenk45219c42003-05-12 21:50:16 +0000235 LAN91C96_CONTROL);
236
237 /* Disable all interrupts */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500238 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenk45219c42003-05-12 21:50:16 +0000239}
240
241/*
242 * Function: smc_enable
243 * Purpose: let the chip talk to the outside work
244 * Method:
245 * 1. Initialize the Memory Configuration Register
246 * 2. Enable the transmitter
247 * 3. Enable the receiver
248*/
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500249static void smc_enable(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000250{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500251 PRINTK2("%s:smc_enable\n", dev->name);
252 SMC_SELECT_BANK(dev, 0);
wdenk45219c42003-05-12 21:50:16 +0000253
254 /* Initialize the Memory Configuration Register. See page
255 49 of the LAN91C96 data sheet for details. */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500256 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
wdenk45219c42003-05-12 21:50:16 +0000257
258 /* Initialize the Transmit Control Register */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500259 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
wdenk45219c42003-05-12 21:50:16 +0000260 /* Initialize the Receive Control Register
261 * FIXME:
262 * The promiscuous bit set because I could not receive ARP reply
263 * packets from the server when I send a ARP request. It only works
264 * when I set the promiscuous bit
265 */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500266 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
wdenk45219c42003-05-12 21:50:16 +0000267}
268
269/*
270 * Function: smc_shutdown
271 * Purpose: closes down the SMC91xxx chip.
272 * Method:
273 * 1. zero the interrupt mask
274 * 2. clear the enable receive flag
275 * 3. clear the enable xmit flags
276 *
277 * TODO:
278 * (1) maybe utilize power down mode.
279 * Why not yet? Because while the chip will go into power down mode,
280 * the manual says that it will wake up in response to any I/O requests
281 * in the register space. Empirical results do not show this working.
282 */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500283static void smc_shutdown(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000284{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500285 PRINTK2("%s:smc_shutdown\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000286
287 /* no more interrupts for me */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500288 SMC_SELECT_BANK(dev, 2);
289 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenk45219c42003-05-12 21:50:16 +0000290
291 /* and tell the card to stay away from that nasty outside world */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500292 SMC_SELECT_BANK(dev, 0);
293 SMC_outb(dev, 0, LAN91C96_RCR);
294 SMC_outb(dev, 0, LAN91C96_TCR);
wdenk45219c42003-05-12 21:50:16 +0000295}
296
297
298/*
299 * Function: smc_hardware_send_packet(struct net_device * )
300 * Purpose:
301 * This sends the actual packet to the SMC9xxx chip.
302 *
303 * Algorithm:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +0100304 * First, see if a saved_skb is available.
wdenk45219c42003-05-12 21:50:16 +0000305 * ( this should NOT be called if there is no 'saved_skb'
306 * Now, find the packet number that the chip allocated
307 * Point the data pointers at it in memory
308 * Set the length word in the chip's memory
309 * Dump the packet to chip memory
310 * Check if a last byte is needed ( odd length packet )
311 * if so, set the control flag right
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +0100312 * Tell the card to send it
wdenk45219c42003-05-12 21:50:16 +0000313 * Enable the transmit interrupt, so I know if it failed
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +0100314 * Free the kernel data if I actually sent it.
wdenk45219c42003-05-12 21:50:16 +0000315 */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500316static int smc_send_packet(struct eth_device *dev, volatile void *packet,
317 int packet_length)
wdenk45219c42003-05-12 21:50:16 +0000318{
319 byte packet_no;
320 unsigned long ioaddr;
321 byte *buf;
322 int length;
323 int numPages;
324 int try = 0;
325 int time_out;
326 byte status;
327
328
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500329 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000330
331 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
332
333 /* allocate memory
334 ** The MMU wants the number of pages to be the number of 256 bytes
335 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
336 **
337 ** The 91C111 ignores the size bits, but the code is left intact
338 ** for backwards and future compatibility.
339 **
340 ** Pkt size for allocating is data length +6 (for additional status
341 ** words, length and ctl!)
342 **
343 ** If odd size then last byte is included in this header.
344 */
345 numPages = ((length & 0xfffe) + 6);
346 numPages >>= 8; /* Divide by 256 */
347
348 if (numPages > 7) {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500349 printf("%s: Far too big packet error. \n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000350 return 0;
351 }
352
353 /* now, try to allocate the memory */
354
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500355 SMC_SELECT_BANK(dev, 2);
356 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000357
358 again:
359 try++;
360 time_out = MEMORY_WAIT_TIME;
361 do {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500362 status = SMC_inb(dev, LAN91C96_INT_STATS);
wdenk45219c42003-05-12 21:50:16 +0000363 if (status & LAN91C96_IST_ALLOC_INT) {
364
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500365 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
366 LAN91C96_INT_STATS);
wdenk45219c42003-05-12 21:50:16 +0000367 break;
368 }
369 } while (--time_out);
370
371 if (!time_out) {
372 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500373 dev->name, try);
wdenk45219c42003-05-12 21:50:16 +0000374 if (try < SMC_ALLOC_MAX_TRY)
375 goto again;
376 else
377 return 0;
378 }
379
380 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500381 dev->name, try);
wdenk45219c42003-05-12 21:50:16 +0000382
383 /* I can send the packet now.. */
384
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500385 ioaddr = dev->iobase;
wdenk45219c42003-05-12 21:50:16 +0000386
387 buf = (byte *) packet;
388
389 /* If I get here, I _know_ there is a packet slot waiting for me */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500390 packet_no = SMC_inb(dev, LAN91C96_ARR);
wdenk45219c42003-05-12 21:50:16 +0000391 if (packet_no & LAN91C96_ARR_FAILED) {
392 /* or isn't there? BAD CHIP! */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500393 printf("%s: Memory allocation failed. \n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000394 return 0;
395 }
396
397 /* we have a packet address, so tell the card to use it */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500398 SMC_outb(dev, packet_no, LAN91C96_PNR);
wdenk45219c42003-05-12 21:50:16 +0000399
400 /* point to the beginning of the packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500401 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
wdenk45219c42003-05-12 21:50:16 +0000402
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500403 PRINTK3("%s: Trying to xmit packet of length %x\n",
404 dev->name, length);
wdenk45219c42003-05-12 21:50:16 +0000405
406#if SMC_DEBUG > 2
407 printf ("Transmitting Packet\n");
408 print_packet (buf, length);
409#endif
410
411 /* send the packet length ( +6 for status, length and ctl byte )
412 and the status word ( set to zeros ) */
413#ifdef USE_32_BIT
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500414 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000415#else
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500416 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000417 /* send the packet length ( +6 for status words, length, and ctl */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500418 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000419#endif /* USE_32_BIT */
420
421 /* send the actual data
422 * I _think_ it's faster to send the longs first, and then
423 * mop up by sending the last word. It depends heavily
424 * on alignment, at least on the 486. Maybe it would be
425 * a good idea to check which is optimal? But that could take
426 * almost as much time as is saved?
427 */
428#ifdef USE_32_BIT
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500429 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
wdenk45219c42003-05-12 21:50:16 +0000430 if (length & 0x2)
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500431 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
wdenk45219c42003-05-12 21:50:16 +0000432 LAN91C96_DATA_HIGH);
433#else
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500434 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
wdenk45219c42003-05-12 21:50:16 +0000435#endif /* USE_32_BIT */
436
437 /* Send the last byte, if there is one. */
438 if ((length & 1) == 0) {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500439 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000440 } else {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500441 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000442 }
443
444 /* and let the chipset deal with it */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500445 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000446
447 /* poll for TX INT */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500448 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
wdenk45219c42003-05-12 21:50:16 +0000449 /* sending failed */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500450 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000451
452 /* release packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500453 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000454
455 /* wait for MMU getting ready (low) */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500456 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenk45219c42003-05-12 21:50:16 +0000457 udelay (10);
wdenk45219c42003-05-12 21:50:16 +0000458
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500459 PRINTK2("MMU ready\n");
wdenk45219c42003-05-12 21:50:16 +0000460
461
462 return 0;
463 } else {
464 /* ack. int */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500465 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
wdenk45219c42003-05-12 21:50:16 +0000466
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500467 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
wdenk45219c42003-05-12 21:50:16 +0000468
469 /* release packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500470 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000471
472 /* wait for MMU getting ready (low) */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500473 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenk45219c42003-05-12 21:50:16 +0000474 udelay (10);
wdenk45219c42003-05-12 21:50:16 +0000475
476 PRINTK2 ("MMU ready\n");
477 }
478
479 return length;
480}
481
wdenk45219c42003-05-12 21:50:16 +0000482
483/*
484 * Open and Initialize the board
485 *
486 * Set up everything, reset the card, etc ..
487 *
488 */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500489static int smc_open(bd_t *bd, struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000490{
wdenk5ca26792004-06-06 22:11:41 +0000491 int i, err; /* used to set hw ethernet address */
wdenk45219c42003-05-12 21:50:16 +0000492
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500493 PRINTK2("%s:smc_open\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000494
495 /* reset the hardware */
496
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500497 smc_reset(dev);
498 smc_enable(dev);
wdenk45219c42003-05-12 21:50:16 +0000499
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500500 SMC_SELECT_BANK(dev, 1);
501 /* set smc_mac_addr, and sync it with u-boot globals */
502 err = smc_get_ethaddr(bd, dev);
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500503 if (err < 0)
504 return -1;
wdenk5ca26792004-06-06 22:11:41 +0000505#ifdef USE_32_BIT
wdenk45219c42003-05-12 21:50:16 +0000506 for (i = 0; i < 6; i += 2) {
507 word address;
508
509 address = smc_mac_addr[i + 1] << 8;
510 address |= smc_mac_addr[i];
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500511 SMC_outw(dev, address, LAN91C96_IA0 + i);
wdenk45219c42003-05-12 21:50:16 +0000512 }
wdenka56bd922004-06-06 23:13:55 +0000513#else
wdenk5ca26792004-06-06 22:11:41 +0000514 for (i = 0; i < 6; i++)
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500515 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
wdenk5ca26792004-06-06 22:11:41 +0000516#endif
wdenk45219c42003-05-12 21:50:16 +0000517 return 0;
518}
519
520/*-------------------------------------------------------------
521 *
522 * smc_rcv - receive a packet from the card
523 *
524 * There is ( at least ) a packet waiting to be read from
525 * chip-memory.
526 *
527 * o Read the status
528 * o If an error, record it
529 * o otherwise, read in the packet
530 *-------------------------------------------------------------
531 */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500532static int smc_rcv(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000533{
534 int packet_number;
535 word status;
536 word packet_length;
537 int is_error = 0;
538
539#ifdef USE_32_BIT
540 dword stat_len;
541#endif
542
543
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500544 SMC_SELECT_BANK(dev, 2);
545 packet_number = SMC_inw(dev, LAN91C96_FIFO);
wdenk45219c42003-05-12 21:50:16 +0000546
547 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
548 return 0;
549 }
550
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500551 PRINTK3("%s:smc_rcv\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000552 /* start reading from the start of the packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500553 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
wdenk45219c42003-05-12 21:50:16 +0000554 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
555
556 /* First two words are status and packet_length */
557#ifdef USE_32_BIT
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500558 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000559 status = stat_len & 0xffff;
560 packet_length = stat_len >> 16;
561#else
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500562 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
563 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000564#endif
565
566 packet_length &= 0x07ff; /* mask off top bits */
567
568 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
569
570 if (!(status & FRAME_FILTER)) {
571 /* Adjust for having already read the first two words */
572 packet_length -= 4; /*4; */
573
574
wdenk45219c42003-05-12 21:50:16 +0000575 /* set odd length for bug in LAN91C111, */
576 /* which never sets RS_ODDFRAME */
577 /* TODO ? */
578
579
580#ifdef USE_32_BIT
581 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
582 packet_length >> 2, packet_length & 3);
583 /* QUESTION: Like in the TX routine, do I want
584 to send the DWORDs or the bytes first, or some
585 mixture. A mixture might improve already slow PIO
586 performance */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500587 SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
588 packet_length >> 2);
wdenk45219c42003-05-12 21:50:16 +0000589 /* read the left over bytes */
590 if (packet_length & 3) {
591 int i;
592
593 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500594 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000595
596 for (i = 0; i < (packet_length & 3); i++)
597 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
598 }
599#else
600 PRINTK3 (" Reading %d words and %d byte(s) \n",
601 (packet_length >> 1), packet_length & 1);
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500602 SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
603 packet_length >> 1);
wdenk45219c42003-05-12 21:50:16 +0000604
605#endif /* USE_32_BIT */
606
607#if SMC_DEBUG > 2
608 printf ("Receiving Packet\n");
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500609 print_packet((byte *)NetRxPackets[0], packet_length);
wdenk45219c42003-05-12 21:50:16 +0000610#endif
611 } else {
612 /* error ... */
613 /* TODO ? */
614 is_error = 1;
615 }
616
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500617 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenk45219c42003-05-12 21:50:16 +0000618 udelay (1); /* Wait until not busy */
619
620 /* error or good, tell the card to get rid of this packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500621 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000622
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500623 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenk45219c42003-05-12 21:50:16 +0000624 udelay (1); /* Wait until not busy */
625
626 if (!is_error) {
627 /* Pass the packet up to the protocol layers. */
628 NetReceive (NetRxPackets[0], packet_length);
629 return packet_length;
630 } else {
631 return 0;
632 }
633
634}
635
636/*----------------------------------------------------
637 * smc_close
638 *
639 * this makes the board clean up everything that it can
640 * and not talk to the outside world. Caused by
641 * an 'ifconfig ethX down'
642 *
643 -----------------------------------------------------*/
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500644static int smc_close(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000645{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500646 PRINTK2("%s:smc_close\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000647
648 /* clear everything */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500649 smc_shutdown(dev);
wdenk45219c42003-05-12 21:50:16 +0000650
651 return 0;
652}
653
654#if SMC_DEBUG > 2
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500655static void print_packet(byte *buf, int length)
wdenk45219c42003-05-12 21:50:16 +0000656{
657#if 0
658 int i;
659 int remainder;
660 int lines;
661
662 printf ("Packet of length %d \n", length);
663
664 lines = length / 16;
665 remainder = length % 16;
666
667 for (i = 0; i < lines; i++) {
668 int cur;
669
670 for (cur = 0; cur < 8; cur++) {
671 byte a, b;
672
673 a = *(buf++);
674 b = *(buf++);
675 printf ("%02x%02x ", a, b);
676 }
677 printf ("\n");
678 }
679 for (i = 0; i < remainder / 2; i++) {
680 byte a, b;
681
682 a = *(buf++);
683 b = *(buf++);
684 printf ("%02x%02x ", a, b);
685 }
686 printf ("\n");
687#endif /* 0 */
688}
689#endif /* SMC_DEBUG > 2 */
690
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500691static int lan91c96_init(struct eth_device *dev, bd_t *bd)
wdenk45219c42003-05-12 21:50:16 +0000692{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500693 return smc_open(bd, dev);
wdenk45219c42003-05-12 21:50:16 +0000694}
695
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500696static void lan91c96_halt(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000697{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500698 smc_close(dev);
wdenk45219c42003-05-12 21:50:16 +0000699}
700
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500701static int lan91c96_recv(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000702{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500703 return smc_rcv(dev);
wdenk45219c42003-05-12 21:50:16 +0000704}
705
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500706static int lan91c96_send(struct eth_device *dev, volatile void *packet,
707 int length)
wdenk45219c42003-05-12 21:50:16 +0000708{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500709 return smc_send_packet(dev, packet, length);
wdenk45219c42003-05-12 21:50:16 +0000710}
711
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500712/* smc_get_ethaddr
wdenk5ca26792004-06-06 22:11:41 +0000713 *
714 * This checks both the environment and the ROM for an ethernet address. If
715 * found, the environment takes precedence.
716 */
717
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500718static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
wdenk5ca26792004-06-06 22:11:41 +0000719{
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500720 uchar v_mac[6];
wdenk5ca26792004-06-06 22:11:41 +0000721
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500722 if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
723 /* get ROM mac value if any */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500724 if (!get_rom_mac(dev, v_mac)) {
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500725 printf("\n*** ERROR: ethaddr is NOT set !!\n");
726 return -1;
wdenk5ca26792004-06-06 22:11:41 +0000727 }
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500728 eth_setenv_enetaddr("ethaddr", v_mac);
wdenk5ca26792004-06-06 22:11:41 +0000729 }
730
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500731 smc_set_mac_addr(v_mac); /* use old function to update smc default */
732 PRINTK("Using MAC Address %pM\n", v_mac);
733 return 0;
wdenk5ca26792004-06-06 22:11:41 +0000734}
735
wdenka56bd922004-06-06 23:13:55 +0000736/*
wdenk5ca26792004-06-06 22:11:41 +0000737 * get_rom_mac()
738 * Note, this has omly been tested for the OMAP730 P2.
739 */
740
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500741static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
wdenk5ca26792004-06-06 22:11:41 +0000742{
743#ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
744 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
745
746 memcpy (v_rom_mac, hw_mac_addr, 6);
747 return (1);
748#else
749 int i;
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500750 SMC_SELECT_BANK(dev, 1);
wdenk5ca26792004-06-06 22:11:41 +0000751 for (i=0; i<6; i++)
752 {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500753 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
wdenk5ca26792004-06-06 22:11:41 +0000754 }
755 return (1);
756#endif
757}
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500758
759/* Structure to detect the device IDs */
760struct id_type {
761 u8 id;
762 char *name;
763};
764static struct id_type supported_chips[] = {
765 {0, ""}, /* Dummy entry to prevent id check failure */
766 {9, "LAN91C110"},
767 {8, "LAN91C100FD"},
768 {7, "LAN91C100"},
769 {5, "LAN91C95"},
Yanjun Yang0e7790d2010-12-26 10:40:54 +0800770 {4, "LAN91C94/96"},
771 {3, "LAN91C90/92"},
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500772};
773/* lan91c96_detect_chip
774 * See:
775 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
776 * page 71 - that is the closest we get to detect this device
777 */
778static int lan91c96_detect_chip(struct eth_device *dev)
779{
780 u8 chip_id;
781 int r;
782 SMC_SELECT_BANK(dev, 3);
783 chip_id = SMC_inw(dev, 0xA) & LAN91C96_REV_REVID;
784 SMC_SELECT_BANK(dev, 0);
785 for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++)
786 if (chip_id == supported_chips[r].id)
787 return r;
788 return 0;
789}
790
791int lan91c96_initialize(u8 dev_num, int base_addr)
792{
793 struct eth_device *dev;
794 int r = 0;
795
796 dev = malloc(sizeof(*dev));
797 if (!dev) {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500798 return 0;
799 }
800 memset(dev, 0, sizeof(*dev));
801
802 dev->iobase = base_addr;
803
804 /* Try to detect chip. Will fail if not present. */
805 r = lan91c96_detect_chip(dev);
806 if (!r) {
807 free(dev);
808 return 0;
809 }
810 get_rom_mac(dev, dev->enetaddr);
811
812 dev->init = lan91c96_init;
813 dev->halt = lan91c96_halt;
814 dev->send = lan91c96_send;
815 dev->recv = lan91c96_recv;
816 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
817
818 eth_register(dev);
819 return 0;
820}