blob: 11d350eb8daeeefdf87724b62c4f0e4039a9e713 [file] [log] [blame]
wdenkaa603362003-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-VILLARD41348722008-01-25 07:54:47 +010039 * io = for the base address
wdenkaa603362003-05-12 21:50:16 +000040 * irq = for the IRQ
41 *
42 * author:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +010043 * Erik Stahlman ( erik@vt.edu )
44 * Daris A Nevil ( dnevil@snmc.com )
wdenkaa603362003-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-VILLARD41348722008-01-25 07:54:47 +010056 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
wdenkaa603362003-05-12 21:50:16 +000057 * for lan91c96
58 *---------------------------------------------------------------------------
59 */
60
61#include <common.h>
62#include <command.h>
Nishanth Menonf95b93b2009-10-16 00:06:35 -050063#include <malloc.h>
wdenkaa603362003-05-12 21:50:16 +000064#include "lan91c96.h"
65#include <net.h>
Anatolij Gustschind1d92d72011-11-19 13:12:12 +000066#include <linux/compiler.h>
wdenkaa603362003-05-12 21:50:16 +000067
wdenkaa603362003-05-12 21:50:16 +000068/*------------------------------------------------------------------------
69 *
70 * Configuration options, for the experienced user to change.
71 *
72 -------------------------------------------------------------------------*/
73
74/* Use power-down feature of the chip */
75#define POWER_DOWN 0
76
77/*
78 * Wait time for memory to be free. This probably shouldn't be
79 * tuned that much, as waiting for this means nothing else happens
80 * in the system
81*/
82#define MEMORY_WAIT_TIME 16
83
84#define SMC_DEBUG 0
85
86#if (SMC_DEBUG > 2 )
87#define PRINTK3(args...) printf(args)
88#else
89#define PRINTK3(args...)
90#endif
91
92#if SMC_DEBUG > 1
93#define PRINTK2(args...) printf(args)
94#else
95#define PRINTK2(args...)
96#endif
97
98#ifdef SMC_DEBUG
99#define PRINTK(args...) printf(args)
100#else
101#define PRINTK(args...)
102#endif
103
104
105/*------------------------------------------------------------------------
106 *
107 * The internal workings of the driver. If you are changing anything
108 * here with the SMC stuff, you should have the datasheet and know
109 * what you are doing.
110 *
111 *------------------------------------------------------------------------
112 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500113#define DRIVER_NAME "LAN91C96"
wdenkaa603362003-05-12 21:50:16 +0000114#define SMC_ALLOC_MAX_TRY 5
115#define SMC_TX_TIMEOUT 30
116
117#define ETH_ZLEN 60
118
119#ifdef CONFIG_LAN91C96_USE_32_BIT
120#define USE_32_BIT 1
121#else
122#undef USE_32_BIT
123#endif
124
wdenk6b1aa8d2004-06-06 22:11:41 +0000125/* See if a MAC address is defined in the current environment. If so use it. If not
126 . print a warning and set the environment and other globals with the default.
127 . If an EEPROM is present it really should be consulted.
128*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500129static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
130static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
wdenkaa603362003-05-12 21:50:16 +0000131
wdenkaa603362003-05-12 21:50:16 +0000132/* ------------------------------------------------------------
133 * Internal routines
134 * ------------------------------------------------------------
135 */
136
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100137static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
wdenkaa603362003-05-12 21:50:16 +0000138
139/*
140 * This function must be called before smc_open() if you want to override
141 * the default mac address.
142 */
143
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500144static void smc_set_mac_addr(const unsigned char *addr)
wdenkaa603362003-05-12 21:50:16 +0000145{
146 int i;
147
148 for (i = 0; i < sizeof (smc_mac_addr); i++) {
149 smc_mac_addr[i] = addr[i];
150 }
151}
wdenkaa603362003-05-12 21:50:16 +0000152
153/***********************************************
154 * Show available memory *
155 ***********************************************/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500156void dump_memory_info(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000157{
Anatolij Gustschind1d92d72011-11-19 13:12:12 +0000158 __maybe_unused word mem_info;
wdenkaa603362003-05-12 21:50:16 +0000159 word old_bank;
160
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500161 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
wdenkaa603362003-05-12 21:50:16 +0000162
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500163 SMC_SELECT_BANK(dev, 0);
164 mem_info = SMC_inw(dev, LAN91C96_MIR);
wdenkaa603362003-05-12 21:50:16 +0000165 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
166
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500167 SMC_SELECT_BANK(dev, old_bank);
wdenkaa603362003-05-12 21:50:16 +0000168}
169
170/*
171 * A rather simple routine to print out a packet for debugging purposes.
172 */
173#if SMC_DEBUG > 2
174static void print_packet (byte *, int);
175#endif
176
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500177static int poll4int (struct eth_device *dev, byte mask, int timeout)
wdenkaa603362003-05-12 21:50:16 +0000178{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200179 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
wdenkaa603362003-05-12 21:50:16 +0000180 int is_timeout = 0;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500181 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
wdenkaa603362003-05-12 21:50:16 +0000182
183 PRINTK2 ("Polling...\n");
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500184 SMC_SELECT_BANK(dev, 2);
185 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
wdenkaa603362003-05-12 21:50:16 +0000186 if (get_timer (0) >= tmo) {
187 is_timeout = 1;
188 break;
189 }
190 }
191
192 /* restore old bank selection */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500193 SMC_SELECT_BANK(dev, old_bank);
wdenkaa603362003-05-12 21:50:16 +0000194
195 if (is_timeout)
196 return 1;
197 else
198 return 0;
199}
200
201/*
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500202 * Function: smc_reset
wdenkaa603362003-05-12 21:50:16 +0000203 * Purpose:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100204 * This sets the SMC91111 chip to its normal state, hopefully from whatever
205 * mess that any other DOS driver has put it in.
wdenkaa603362003-05-12 21:50:16 +0000206 *
207 * Maybe I should reset more registers to defaults in here? SOFTRST should
208 * do that for me.
209 *
210 * Method:
211 * 1. send a SOFT RESET
212 * 2. wait for it to finish
213 * 3. enable autorelease mode
214 * 4. reset the memory management unit
215 * 5. clear all interrupts
216 *
217*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500218static void smc_reset(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000219{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500220 PRINTK2("%s:smc_reset\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000221
222 /* This resets the registers mostly to defaults, but doesn't
223 affect EEPROM. That seems unnecessary */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500224 SMC_SELECT_BANK(dev, 0);
225 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
wdenkaa603362003-05-12 21:50:16 +0000226
227 udelay (10);
228
229 /* Disable transmit and receive functionality */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500230 SMC_outw(dev, 0, LAN91C96_RCR);
231 SMC_outw(dev, 0, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000232
233 /* set the control register */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500234 SMC_SELECT_BANK(dev, 1);
235 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
wdenkaa603362003-05-12 21:50:16 +0000236 LAN91C96_CONTROL);
237
238 /* Disable all interrupts */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500239 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenkaa603362003-05-12 21:50:16 +0000240}
241
242/*
243 * Function: smc_enable
244 * Purpose: let the chip talk to the outside work
245 * Method:
246 * 1. Initialize the Memory Configuration Register
247 * 2. Enable the transmitter
248 * 3. Enable the receiver
249*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500250static void smc_enable(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000251{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500252 PRINTK2("%s:smc_enable\n", dev->name);
253 SMC_SELECT_BANK(dev, 0);
wdenkaa603362003-05-12 21:50:16 +0000254
255 /* Initialize the Memory Configuration Register. See page
256 49 of the LAN91C96 data sheet for details. */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500257 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
wdenkaa603362003-05-12 21:50:16 +0000258
259 /* Initialize the Transmit Control Register */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500260 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000261 /* Initialize the Receive Control Register
262 * FIXME:
263 * The promiscuous bit set because I could not receive ARP reply
264 * packets from the server when I send a ARP request. It only works
265 * when I set the promiscuous bit
266 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500267 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
wdenkaa603362003-05-12 21:50:16 +0000268}
269
270/*
271 * Function: smc_shutdown
272 * Purpose: closes down the SMC91xxx chip.
273 * Method:
274 * 1. zero the interrupt mask
275 * 2. clear the enable receive flag
276 * 3. clear the enable xmit flags
277 *
278 * TODO:
279 * (1) maybe utilize power down mode.
280 * Why not yet? Because while the chip will go into power down mode,
281 * the manual says that it will wake up in response to any I/O requests
282 * in the register space. Empirical results do not show this working.
283 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500284static void smc_shutdown(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000285{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500286 PRINTK2("%s:smc_shutdown\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000287
288 /* no more interrupts for me */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500289 SMC_SELECT_BANK(dev, 2);
290 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenkaa603362003-05-12 21:50:16 +0000291
292 /* and tell the card to stay away from that nasty outside world */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500293 SMC_SELECT_BANK(dev, 0);
294 SMC_outb(dev, 0, LAN91C96_RCR);
295 SMC_outb(dev, 0, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000296}
297
298
299/*
300 * Function: smc_hardware_send_packet(struct net_device * )
301 * Purpose:
302 * This sends the actual packet to the SMC9xxx chip.
303 *
304 * Algorithm:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100305 * First, see if a saved_skb is available.
wdenkaa603362003-05-12 21:50:16 +0000306 * ( this should NOT be called if there is no 'saved_skb'
307 * Now, find the packet number that the chip allocated
308 * Point the data pointers at it in memory
309 * Set the length word in the chip's memory
310 * Dump the packet to chip memory
311 * Check if a last byte is needed ( odd length packet )
312 * if so, set the control flag right
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100313 * Tell the card to send it
wdenkaa603362003-05-12 21:50:16 +0000314 * Enable the transmit interrupt, so I know if it failed
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100315 * Free the kernel data if I actually sent it.
wdenkaa603362003-05-12 21:50:16 +0000316 */
Joe Hershbergerc78248f2012-05-21 14:45:30 +0000317static int smc_send_packet(struct eth_device *dev, void *packet,
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500318 int packet_length)
wdenkaa603362003-05-12 21:50:16 +0000319{
320 byte packet_no;
wdenkaa603362003-05-12 21:50:16 +0000321 byte *buf;
322 int length;
323 int numPages;
324 int try = 0;
325 int time_out;
326 byte status;
327
328
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500329 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
wdenkaa603362003-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 Menonf95b93b2009-10-16 00:06:35 -0500349 printf("%s: Far too big packet error. \n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000350 return 0;
351 }
352
353 /* now, try to allocate the memory */
354
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500355 SMC_SELECT_BANK(dev, 2);
356 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000357
358 again:
359 try++;
360 time_out = MEMORY_WAIT_TIME;
361 do {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500362 status = SMC_inb(dev, LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000363 if (status & LAN91C96_IST_ALLOC_INT) {
364
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500365 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
366 LAN91C96_INT_STATS);
wdenkaa603362003-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 Menonf95b93b2009-10-16 00:06:35 -0500373 dev->name, try);
wdenkaa603362003-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 Menonf95b93b2009-10-16 00:06:35 -0500381 dev->name, try);
wdenkaa603362003-05-12 21:50:16 +0000382
383 /* I can send the packet now.. */
wdenkaa603362003-05-12 21:50:16 +0000384 buf = (byte *) packet;
385
386 /* If I get here, I _know_ there is a packet slot waiting for me */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500387 packet_no = SMC_inb(dev, LAN91C96_ARR);
wdenkaa603362003-05-12 21:50:16 +0000388 if (packet_no & LAN91C96_ARR_FAILED) {
389 /* or isn't there? BAD CHIP! */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500390 printf("%s: Memory allocation failed. \n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000391 return 0;
392 }
393
394 /* we have a packet address, so tell the card to use it */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500395 SMC_outb(dev, packet_no, LAN91C96_PNR);
wdenkaa603362003-05-12 21:50:16 +0000396
397 /* point to the beginning of the packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500398 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
wdenkaa603362003-05-12 21:50:16 +0000399
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500400 PRINTK3("%s: Trying to xmit packet of length %x\n",
401 dev->name, length);
wdenkaa603362003-05-12 21:50:16 +0000402
403#if SMC_DEBUG > 2
404 printf ("Transmitting Packet\n");
405 print_packet (buf, length);
406#endif
407
408 /* send the packet length ( +6 for status, length and ctl byte )
409 and the status word ( set to zeros ) */
410#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500411 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000412#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500413 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000414 /* send the packet length ( +6 for status words, length, and ctl */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500415 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000416#endif /* USE_32_BIT */
417
418 /* send the actual data
419 * I _think_ it's faster to send the longs first, and then
420 * mop up by sending the last word. It depends heavily
421 * on alignment, at least on the 486. Maybe it would be
422 * a good idea to check which is optimal? But that could take
423 * almost as much time as is saved?
424 */
425#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500426 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
wdenkaa603362003-05-12 21:50:16 +0000427 if (length & 0x2)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500428 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
wdenkaa603362003-05-12 21:50:16 +0000429 LAN91C96_DATA_HIGH);
430#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500431 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
wdenkaa603362003-05-12 21:50:16 +0000432#endif /* USE_32_BIT */
433
434 /* Send the last byte, if there is one. */
435 if ((length & 1) == 0) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500436 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000437 } else {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500438 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000439 }
440
441 /* and let the chipset deal with it */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500442 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000443
444 /* poll for TX INT */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500445 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
wdenkaa603362003-05-12 21:50:16 +0000446 /* sending failed */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500447 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000448
449 /* release packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500450 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000451
452 /* wait for MMU getting ready (low) */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500453 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000454 udelay (10);
wdenkaa603362003-05-12 21:50:16 +0000455
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500456 PRINTK2("MMU ready\n");
wdenkaa603362003-05-12 21:50:16 +0000457
458
459 return 0;
460 } else {
461 /* ack. int */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500462 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000463
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500464 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
wdenkaa603362003-05-12 21:50:16 +0000465
466 /* release packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500467 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000468
469 /* wait for MMU getting ready (low) */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500470 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000471 udelay (10);
wdenkaa603362003-05-12 21:50:16 +0000472
473 PRINTK2 ("MMU ready\n");
474 }
475
476 return length;
477}
478
wdenkaa603362003-05-12 21:50:16 +0000479
480/*
481 * Open and Initialize the board
482 *
483 * Set up everything, reset the card, etc ..
484 *
485 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500486static int smc_open(bd_t *bd, struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000487{
wdenk6b1aa8d2004-06-06 22:11:41 +0000488 int i, err; /* used to set hw ethernet address */
wdenkaa603362003-05-12 21:50:16 +0000489
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500490 PRINTK2("%s:smc_open\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000491
492 /* reset the hardware */
493
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500494 smc_reset(dev);
495 smc_enable(dev);
wdenkaa603362003-05-12 21:50:16 +0000496
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500497 SMC_SELECT_BANK(dev, 1);
498 /* set smc_mac_addr, and sync it with u-boot globals */
499 err = smc_get_ethaddr(bd, dev);
Mike Frysinger719b6752009-02-11 19:09:54 -0500500 if (err < 0)
501 return -1;
wdenk6b1aa8d2004-06-06 22:11:41 +0000502#ifdef USE_32_BIT
wdenkaa603362003-05-12 21:50:16 +0000503 for (i = 0; i < 6; i += 2) {
504 word address;
505
506 address = smc_mac_addr[i + 1] << 8;
507 address |= smc_mac_addr[i];
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500508 SMC_outw(dev, address, LAN91C96_IA0 + i);
wdenkaa603362003-05-12 21:50:16 +0000509 }
wdenke3a06802004-06-06 23:13:55 +0000510#else
wdenk6b1aa8d2004-06-06 22:11:41 +0000511 for (i = 0; i < 6; i++)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500512 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
wdenk6b1aa8d2004-06-06 22:11:41 +0000513#endif
wdenkaa603362003-05-12 21:50:16 +0000514 return 0;
515}
516
517/*-------------------------------------------------------------
518 *
519 * smc_rcv - receive a packet from the card
520 *
521 * There is ( at least ) a packet waiting to be read from
522 * chip-memory.
523 *
524 * o Read the status
525 * o If an error, record it
526 * o otherwise, read in the packet
527 *-------------------------------------------------------------
528 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500529static int smc_rcv(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000530{
531 int packet_number;
532 word status;
533 word packet_length;
534 int is_error = 0;
535
536#ifdef USE_32_BIT
537 dword stat_len;
538#endif
539
540
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500541 SMC_SELECT_BANK(dev, 2);
542 packet_number = SMC_inw(dev, LAN91C96_FIFO);
wdenkaa603362003-05-12 21:50:16 +0000543
544 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
545 return 0;
546 }
547
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500548 PRINTK3("%s:smc_rcv\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000549 /* start reading from the start of the packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500550 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
wdenkaa603362003-05-12 21:50:16 +0000551 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
552
553 /* First two words are status and packet_length */
554#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500555 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000556 status = stat_len & 0xffff;
557 packet_length = stat_len >> 16;
558#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500559 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
560 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000561#endif
562
563 packet_length &= 0x07ff; /* mask off top bits */
564
565 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
566
567 if (!(status & FRAME_FILTER)) {
568 /* Adjust for having already read the first two words */
569 packet_length -= 4; /*4; */
570
571
wdenkaa603362003-05-12 21:50:16 +0000572 /* set odd length for bug in LAN91C111, */
573 /* which never sets RS_ODDFRAME */
574 /* TODO ? */
575
576
577#ifdef USE_32_BIT
578 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
579 packet_length >> 2, packet_length & 3);
580 /* QUESTION: Like in the TX routine, do I want
581 to send the DWORDs or the bytes first, or some
582 mixture. A mixture might improve already slow PIO
583 performance */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500584 SMC_insl(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
585 packet_length >> 2);
wdenkaa603362003-05-12 21:50:16 +0000586 /* read the left over bytes */
587 if (packet_length & 3) {
588 int i;
589
590 byte *tail = (byte *) (NetRxPackets[0] + (packet_length & ~3));
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500591 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000592
593 for (i = 0; i < (packet_length & 3); i++)
594 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
595 }
596#else
597 PRINTK3 (" Reading %d words and %d byte(s) \n",
598 (packet_length >> 1), packet_length & 1);
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500599 SMC_insw(dev, LAN91C96_DATA_HIGH, NetRxPackets[0],
600 packet_length >> 1);
wdenkaa603362003-05-12 21:50:16 +0000601
602#endif /* USE_32_BIT */
603
604#if SMC_DEBUG > 2
605 printf ("Receiving Packet\n");
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500606 print_packet((byte *)NetRxPackets[0], packet_length);
wdenkaa603362003-05-12 21:50:16 +0000607#endif
608 } else {
609 /* error ... */
610 /* TODO ? */
611 is_error = 1;
612 }
613
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500614 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000615 udelay (1); /* Wait until not busy */
616
617 /* error or good, tell the card to get rid of this packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500618 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000619
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500620 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000621 udelay (1); /* Wait until not busy */
622
623 if (!is_error) {
624 /* Pass the packet up to the protocol layers. */
625 NetReceive (NetRxPackets[0], packet_length);
626 return packet_length;
627 } else {
628 return 0;
629 }
630
631}
632
633/*----------------------------------------------------
634 * smc_close
635 *
636 * this makes the board clean up everything that it can
637 * and not talk to the outside world. Caused by
638 * an 'ifconfig ethX down'
639 *
640 -----------------------------------------------------*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500641static int smc_close(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000642{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500643 PRINTK2("%s:smc_close\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000644
645 /* clear everything */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500646 smc_shutdown(dev);
wdenkaa603362003-05-12 21:50:16 +0000647
648 return 0;
649}
650
651#if SMC_DEBUG > 2
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500652static void print_packet(byte *buf, int length)
wdenkaa603362003-05-12 21:50:16 +0000653{
654#if 0
655 int i;
656 int remainder;
657 int lines;
658
659 printf ("Packet of length %d \n", length);
660
661 lines = length / 16;
662 remainder = length % 16;
663
664 for (i = 0; i < lines; i++) {
665 int cur;
666
667 for (cur = 0; cur < 8; cur++) {
668 byte a, b;
669
670 a = *(buf++);
671 b = *(buf++);
672 printf ("%02x%02x ", a, b);
673 }
674 printf ("\n");
675 }
676 for (i = 0; i < remainder / 2; i++) {
677 byte a, b;
678
679 a = *(buf++);
680 b = *(buf++);
681 printf ("%02x%02x ", a, b);
682 }
683 printf ("\n");
684#endif /* 0 */
685}
686#endif /* SMC_DEBUG > 2 */
687
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500688static int lan91c96_init(struct eth_device *dev, bd_t *bd)
wdenkaa603362003-05-12 21:50:16 +0000689{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500690 return smc_open(bd, dev);
wdenkaa603362003-05-12 21:50:16 +0000691}
692
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500693static void lan91c96_halt(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000694{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500695 smc_close(dev);
wdenkaa603362003-05-12 21:50:16 +0000696}
697
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500698static int lan91c96_recv(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000699{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500700 return smc_rcv(dev);
wdenkaa603362003-05-12 21:50:16 +0000701}
702
Joe Hershbergerc78248f2012-05-21 14:45:30 +0000703static int lan91c96_send(struct eth_device *dev, void *packet,
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500704 int length)
wdenkaa603362003-05-12 21:50:16 +0000705{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500706 return smc_send_packet(dev, packet, length);
wdenkaa603362003-05-12 21:50:16 +0000707}
708
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500709/* smc_get_ethaddr
wdenk6b1aa8d2004-06-06 22:11:41 +0000710 *
711 * This checks both the environment and the ROM for an ethernet address. If
712 * found, the environment takes precedence.
713 */
714
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500715static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
wdenk6b1aa8d2004-06-06 22:11:41 +0000716{
Mike Frysinger719b6752009-02-11 19:09:54 -0500717 uchar v_mac[6];
wdenk6b1aa8d2004-06-06 22:11:41 +0000718
Mike Frysinger719b6752009-02-11 19:09:54 -0500719 if (!eth_getenv_enetaddr("ethaddr", v_mac)) {
720 /* get ROM mac value if any */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500721 if (!get_rom_mac(dev, v_mac)) {
Mike Frysinger719b6752009-02-11 19:09:54 -0500722 printf("\n*** ERROR: ethaddr is NOT set !!\n");
723 return -1;
wdenk6b1aa8d2004-06-06 22:11:41 +0000724 }
Mike Frysinger719b6752009-02-11 19:09:54 -0500725 eth_setenv_enetaddr("ethaddr", v_mac);
wdenk6b1aa8d2004-06-06 22:11:41 +0000726 }
727
Mike Frysinger719b6752009-02-11 19:09:54 -0500728 smc_set_mac_addr(v_mac); /* use old function to update smc default */
729 PRINTK("Using MAC Address %pM\n", v_mac);
730 return 0;
wdenk6b1aa8d2004-06-06 22:11:41 +0000731}
732
wdenke3a06802004-06-06 23:13:55 +0000733/*
wdenk6b1aa8d2004-06-06 22:11:41 +0000734 * get_rom_mac()
735 * Note, this has omly been tested for the OMAP730 P2.
736 */
737
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500738static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
wdenk6b1aa8d2004-06-06 22:11:41 +0000739{
740#ifdef HARDCODE_MAC /* used for testing or to supress run time warnings */
741 char hw_mac_addr[] = { 0x02, 0x80, 0xad, 0x20, 0x31, 0xb8 };
742
743 memcpy (v_rom_mac, hw_mac_addr, 6);
744 return (1);
745#else
746 int i;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500747 SMC_SELECT_BANK(dev, 1);
wdenk6b1aa8d2004-06-06 22:11:41 +0000748 for (i=0; i<6; i++)
749 {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500750 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
wdenk6b1aa8d2004-06-06 22:11:41 +0000751 }
752 return (1);
753#endif
754}
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500755
756/* Structure to detect the device IDs */
757struct id_type {
758 u8 id;
759 char *name;
760};
761static struct id_type supported_chips[] = {
762 {0, ""}, /* Dummy entry to prevent id check failure */
763 {9, "LAN91C110"},
764 {8, "LAN91C100FD"},
765 {7, "LAN91C100"},
766 {5, "LAN91C95"},
Yanjun Yangf52a06c2010-12-26 10:40:54 +0800767 {4, "LAN91C94/96"},
768 {3, "LAN91C90/92"},
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500769};
770/* lan91c96_detect_chip
771 * See:
772 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
773 * page 71 - that is the closest we get to detect this device
774 */
775static int lan91c96_detect_chip(struct eth_device *dev)
776{
777 u8 chip_id;
778 int r;
779 SMC_SELECT_BANK(dev, 3);
Yanjun Yangd99ecfec2010-12-28 16:08:25 +0800780 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500781 SMC_SELECT_BANK(dev, 0);
782 for (r = 0; r < sizeof(supported_chips) / sizeof(struct id_type); r++)
783 if (chip_id == supported_chips[r].id)
784 return r;
785 return 0;
786}
787
788int lan91c96_initialize(u8 dev_num, int base_addr)
789{
790 struct eth_device *dev;
791 int r = 0;
792
793 dev = malloc(sizeof(*dev));
794 if (!dev) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500795 return 0;
796 }
797 memset(dev, 0, sizeof(*dev));
798
799 dev->iobase = base_addr;
800
801 /* Try to detect chip. Will fail if not present. */
802 r = lan91c96_detect_chip(dev);
803 if (!r) {
804 free(dev);
805 return 0;
806 }
807 get_rom_mac(dev, dev->enetaddr);
808
809 dev->init = lan91c96_init;
810 dev->halt = lan91c96_halt;
811 dev->send = lan91c96_send;
812 dev->recv = lan91c96_recv;
813 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
814
815 eth_register(dev);
816 return 0;
817}