blob: f2489aaf82655c42ff049371f8b0327be777dae1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkaa603362003-05-12 21:50:16 +00002/*------------------------------------------------------------------------
3 * lan91c96.c
4 * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
Bin Meng75574052016-02-05 19:30:11 -08005 * on the SMC91111 driver from U-Boot.
wdenkaa603362003-05-12 21:50:16 +00006 *
7 * (C) Copyright 2002
8 * Sysgo Real-Time Solutions, GmbH <www.elinos.com>
9 * Rolf Offermanns <rof@sysgo.de>
10 *
11 * Copyright (C) 2001 Standard Microsystems Corporation (SMSC)
12 * Developed by Simple Network Magic Corporation (SNMC)
13 * Copyright (C) 1996 by Erik Stahlman (ES)
14 *
wdenkaa603362003-05-12 21:50:16 +000015 * Information contained in this file was obtained from the LAN91C96
16 * manual from SMC. To get a copy, if you really want one, you can find
17 * information under www.smsc.com.
18 *
wdenkaa603362003-05-12 21:50:16 +000019 * "Features" of the SMC chip:
20 * 6144 byte packet memory. ( for the 91C96 )
21 * EEPROM for configuration
22 * AUI/TP selection ( mine has 10Base2/10BaseT select )
23 *
24 * Arguments:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +010025 * io = for the base address
wdenkaa603362003-05-12 21:50:16 +000026 * irq = for the IRQ
27 *
28 * author:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +010029 * Erik Stahlman ( erik@vt.edu )
30 * Daris A Nevil ( dnevil@snmc.com )
wdenkaa603362003-05-12 21:50:16 +000031 *
32 *
33 * Hardware multicast code from Peter Cammaert ( pc@denkart.be )
34 *
35 * Sources:
36 * o SMSC LAN91C96 databook (www.smsc.com)
37 * o smc91111.c (u-boot driver)
38 * o smc9194.c (linux kernel driver)
39 * o lan91c96.c (Intel Diagnostic Manager driver)
40 *
41 * History:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +010042 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
wdenkaa603362003-05-12 21:50:16 +000043 * for lan91c96
44 *---------------------------------------------------------------------------
45 */
46
47#include <common.h>
48#include <command.h>
Nishanth Menonf95b93b2009-10-16 00:06:35 -050049#include <malloc.h>
wdenkaa603362003-05-12 21:50:16 +000050#include "lan91c96.h"
51#include <net.h>
Anatolij Gustschind1d92d72011-11-19 13:12:12 +000052#include <linux/compiler.h>
wdenkaa603362003-05-12 21:50:16 +000053
wdenkaa603362003-05-12 21:50:16 +000054/*------------------------------------------------------------------------
55 *
56 * Configuration options, for the experienced user to change.
57 *
58 -------------------------------------------------------------------------*/
59
60/* Use power-down feature of the chip */
61#define POWER_DOWN 0
62
63/*
64 * Wait time for memory to be free. This probably shouldn't be
65 * tuned that much, as waiting for this means nothing else happens
66 * in the system
67*/
68#define MEMORY_WAIT_TIME 16
69
70#define SMC_DEBUG 0
71
72#if (SMC_DEBUG > 2 )
73#define PRINTK3(args...) printf(args)
74#else
75#define PRINTK3(args...)
76#endif
77
78#if SMC_DEBUG > 1
79#define PRINTK2(args...) printf(args)
80#else
81#define PRINTK2(args...)
82#endif
83
84#ifdef SMC_DEBUG
85#define PRINTK(args...) printf(args)
86#else
87#define PRINTK(args...)
88#endif
89
90
91/*------------------------------------------------------------------------
92 *
93 * The internal workings of the driver. If you are changing anything
94 * here with the SMC stuff, you should have the datasheet and know
95 * what you are doing.
96 *
97 *------------------------------------------------------------------------
98 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -050099#define DRIVER_NAME "LAN91C96"
wdenkaa603362003-05-12 21:50:16 +0000100#define SMC_ALLOC_MAX_TRY 5
101#define SMC_TX_TIMEOUT 30
102
103#define ETH_ZLEN 60
104
105#ifdef CONFIG_LAN91C96_USE_32_BIT
106#define USE_32_BIT 1
107#else
108#undef USE_32_BIT
109#endif
110
wdenk6b1aa8d2004-06-06 22:11:41 +0000111/* See if a MAC address is defined in the current environment. If so use it. If not
112 . print a warning and set the environment and other globals with the default.
113 . If an EEPROM is present it really should be consulted.
114*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500115static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev);
116static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
wdenkaa603362003-05-12 21:50:16 +0000117
wdenkaa603362003-05-12 21:50:16 +0000118/* ------------------------------------------------------------
119 * Internal routines
120 * ------------------------------------------------------------
121 */
122
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100123static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
wdenkaa603362003-05-12 21:50:16 +0000124
125/*
126 * This function must be called before smc_open() if you want to override
127 * the default mac address.
128 */
129
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500130static void smc_set_mac_addr(const unsigned char *addr)
wdenkaa603362003-05-12 21:50:16 +0000131{
132 int i;
133
134 for (i = 0; i < sizeof (smc_mac_addr); i++) {
135 smc_mac_addr[i] = addr[i];
136 }
137}
wdenkaa603362003-05-12 21:50:16 +0000138
139/***********************************************
140 * Show available memory *
141 ***********************************************/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500142void dump_memory_info(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000143{
Anatolij Gustschind1d92d72011-11-19 13:12:12 +0000144 __maybe_unused word mem_info;
wdenkaa603362003-05-12 21:50:16 +0000145 word old_bank;
146
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500147 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
wdenkaa603362003-05-12 21:50:16 +0000148
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500149 SMC_SELECT_BANK(dev, 0);
150 mem_info = SMC_inw(dev, LAN91C96_MIR);
wdenkaa603362003-05-12 21:50:16 +0000151 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
152
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500153 SMC_SELECT_BANK(dev, old_bank);
wdenkaa603362003-05-12 21:50:16 +0000154}
155
156/*
157 * A rather simple routine to print out a packet for debugging purposes.
158 */
159#if SMC_DEBUG > 2
160static void print_packet (byte *, int);
161#endif
162
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500163static int poll4int (struct eth_device *dev, byte mask, int timeout)
wdenkaa603362003-05-12 21:50:16 +0000164{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200165 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
wdenkaa603362003-05-12 21:50:16 +0000166 int is_timeout = 0;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500167 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
wdenkaa603362003-05-12 21:50:16 +0000168
169 PRINTK2 ("Polling...\n");
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500170 SMC_SELECT_BANK(dev, 2);
171 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
wdenkaa603362003-05-12 21:50:16 +0000172 if (get_timer (0) >= tmo) {
173 is_timeout = 1;
174 break;
175 }
176 }
177
178 /* restore old bank selection */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500179 SMC_SELECT_BANK(dev, old_bank);
wdenkaa603362003-05-12 21:50:16 +0000180
181 if (is_timeout)
182 return 1;
183 else
184 return 0;
185}
186
187/*
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500188 * Function: smc_reset
wdenkaa603362003-05-12 21:50:16 +0000189 * Purpose:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100190 * This sets the SMC91111 chip to its normal state, hopefully from whatever
191 * mess that any other DOS driver has put it in.
wdenkaa603362003-05-12 21:50:16 +0000192 *
193 * Maybe I should reset more registers to defaults in here? SOFTRST should
194 * do that for me.
195 *
196 * Method:
197 * 1. send a SOFT RESET
198 * 2. wait for it to finish
199 * 3. enable autorelease mode
200 * 4. reset the memory management unit
201 * 5. clear all interrupts
202 *
203*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500204static void smc_reset(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000205{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500206 PRINTK2("%s:smc_reset\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000207
208 /* This resets the registers mostly to defaults, but doesn't
209 affect EEPROM. That seems unnecessary */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500210 SMC_SELECT_BANK(dev, 0);
211 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
wdenkaa603362003-05-12 21:50:16 +0000212
213 udelay (10);
214
215 /* Disable transmit and receive functionality */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500216 SMC_outw(dev, 0, LAN91C96_RCR);
217 SMC_outw(dev, 0, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000218
219 /* set the control register */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500220 SMC_SELECT_BANK(dev, 1);
221 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
wdenkaa603362003-05-12 21:50:16 +0000222 LAN91C96_CONTROL);
223
224 /* Disable all interrupts */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500225 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenkaa603362003-05-12 21:50:16 +0000226}
227
228/*
229 * Function: smc_enable
230 * Purpose: let the chip talk to the outside work
231 * Method:
232 * 1. Initialize the Memory Configuration Register
233 * 2. Enable the transmitter
234 * 3. Enable the receiver
235*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500236static void smc_enable(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000237{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500238 PRINTK2("%s:smc_enable\n", dev->name);
239 SMC_SELECT_BANK(dev, 0);
wdenkaa603362003-05-12 21:50:16 +0000240
241 /* Initialize the Memory Configuration Register. See page
242 49 of the LAN91C96 data sheet for details. */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500243 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
wdenkaa603362003-05-12 21:50:16 +0000244
245 /* Initialize the Transmit Control Register */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500246 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000247 /* Initialize the Receive Control Register
248 * FIXME:
249 * The promiscuous bit set because I could not receive ARP reply
250 * packets from the server when I send a ARP request. It only works
251 * when I set the promiscuous bit
252 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500253 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
wdenkaa603362003-05-12 21:50:16 +0000254}
255
256/*
257 * Function: smc_shutdown
258 * Purpose: closes down the SMC91xxx chip.
259 * Method:
260 * 1. zero the interrupt mask
261 * 2. clear the enable receive flag
262 * 3. clear the enable xmit flags
263 *
264 * TODO:
265 * (1) maybe utilize power down mode.
266 * Why not yet? Because while the chip will go into power down mode,
267 * the manual says that it will wake up in response to any I/O requests
268 * in the register space. Empirical results do not show this working.
269 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500270static void smc_shutdown(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000271{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500272 PRINTK2("%s:smc_shutdown\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000273
274 /* no more interrupts for me */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500275 SMC_SELECT_BANK(dev, 2);
276 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenkaa603362003-05-12 21:50:16 +0000277
278 /* and tell the card to stay away from that nasty outside world */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500279 SMC_SELECT_BANK(dev, 0);
280 SMC_outb(dev, 0, LAN91C96_RCR);
281 SMC_outb(dev, 0, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000282}
283
284
285/*
286 * Function: smc_hardware_send_packet(struct net_device * )
287 * Purpose:
288 * This sends the actual packet to the SMC9xxx chip.
289 *
290 * Algorithm:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100291 * First, see if a saved_skb is available.
wdenkaa603362003-05-12 21:50:16 +0000292 * ( this should NOT be called if there is no 'saved_skb'
293 * Now, find the packet number that the chip allocated
294 * Point the data pointers at it in memory
295 * Set the length word in the chip's memory
296 * Dump the packet to chip memory
297 * Check if a last byte is needed ( odd length packet )
298 * if so, set the control flag right
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100299 * Tell the card to send it
wdenkaa603362003-05-12 21:50:16 +0000300 * Enable the transmit interrupt, so I know if it failed
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100301 * Free the kernel data if I actually sent it.
wdenkaa603362003-05-12 21:50:16 +0000302 */
Joe Hershbergerc78248f2012-05-21 14:45:30 +0000303static int smc_send_packet(struct eth_device *dev, void *packet,
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500304 int packet_length)
wdenkaa603362003-05-12 21:50:16 +0000305{
306 byte packet_no;
wdenkaa603362003-05-12 21:50:16 +0000307 byte *buf;
308 int length;
309 int numPages;
310 int try = 0;
311 int time_out;
312 byte status;
313
314
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500315 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000316
317 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
318
319 /* allocate memory
320 ** The MMU wants the number of pages to be the number of 256 bytes
321 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
322 **
323 ** The 91C111 ignores the size bits, but the code is left intact
324 ** for backwards and future compatibility.
325 **
326 ** Pkt size for allocating is data length +6 (for additional status
327 ** words, length and ctl!)
328 **
329 ** If odd size then last byte is included in this header.
330 */
331 numPages = ((length & 0xfffe) + 6);
332 numPages >>= 8; /* Divide by 256 */
333
334 if (numPages > 7) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500335 printf("%s: Far too big packet error. \n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000336 return 0;
337 }
338
339 /* now, try to allocate the memory */
340
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500341 SMC_SELECT_BANK(dev, 2);
342 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000343
344 again:
345 try++;
346 time_out = MEMORY_WAIT_TIME;
347 do {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500348 status = SMC_inb(dev, LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000349 if (status & LAN91C96_IST_ALLOC_INT) {
350
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500351 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
352 LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000353 break;
354 }
355 } while (--time_out);
356
357 if (!time_out) {
358 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500359 dev->name, try);
wdenkaa603362003-05-12 21:50:16 +0000360 if (try < SMC_ALLOC_MAX_TRY)
361 goto again;
362 else
363 return 0;
364 }
365
366 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500367 dev->name, try);
wdenkaa603362003-05-12 21:50:16 +0000368
369 /* I can send the packet now.. */
wdenkaa603362003-05-12 21:50:16 +0000370 buf = (byte *) packet;
371
372 /* If I get here, I _know_ there is a packet slot waiting for me */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500373 packet_no = SMC_inb(dev, LAN91C96_ARR);
wdenkaa603362003-05-12 21:50:16 +0000374 if (packet_no & LAN91C96_ARR_FAILED) {
375 /* or isn't there? BAD CHIP! */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500376 printf("%s: Memory allocation failed. \n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000377 return 0;
378 }
379
380 /* we have a packet address, so tell the card to use it */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500381 SMC_outb(dev, packet_no, LAN91C96_PNR);
wdenkaa603362003-05-12 21:50:16 +0000382
383 /* point to the beginning of the packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500384 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
wdenkaa603362003-05-12 21:50:16 +0000385
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500386 PRINTK3("%s: Trying to xmit packet of length %x\n",
387 dev->name, length);
wdenkaa603362003-05-12 21:50:16 +0000388
389#if SMC_DEBUG > 2
390 printf ("Transmitting Packet\n");
391 print_packet (buf, length);
392#endif
393
394 /* send the packet length ( +6 for status, length and ctl byte )
395 and the status word ( set to zeros ) */
396#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500397 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000398#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500399 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000400 /* send the packet length ( +6 for status words, length, and ctl */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500401 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000402#endif /* USE_32_BIT */
403
404 /* send the actual data
405 * I _think_ it's faster to send the longs first, and then
406 * mop up by sending the last word. It depends heavily
407 * on alignment, at least on the 486. Maybe it would be
408 * a good idea to check which is optimal? But that could take
409 * almost as much time as is saved?
410 */
411#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500412 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
wdenkaa603362003-05-12 21:50:16 +0000413 if (length & 0x2)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500414 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
wdenkaa603362003-05-12 21:50:16 +0000415 LAN91C96_DATA_HIGH);
416#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500417 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
wdenkaa603362003-05-12 21:50:16 +0000418#endif /* USE_32_BIT */
419
420 /* Send the last byte, if there is one. */
421 if ((length & 1) == 0) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500422 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000423 } else {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500424 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000425 }
426
427 /* and let the chipset deal with it */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500428 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000429
430 /* poll for TX INT */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500431 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
wdenkaa603362003-05-12 21:50:16 +0000432 /* sending failed */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500433 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000434
435 /* release packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500436 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000437
438 /* wait for MMU getting ready (low) */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500439 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000440 udelay (10);
wdenkaa603362003-05-12 21:50:16 +0000441
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500442 PRINTK2("MMU ready\n");
wdenkaa603362003-05-12 21:50:16 +0000443
444
445 return 0;
446 } else {
447 /* ack. int */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500448 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000449
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500450 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
wdenkaa603362003-05-12 21:50:16 +0000451
452 /* release packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500453 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000454
455 /* wait for MMU getting ready (low) */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500456 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000457 udelay (10);
wdenkaa603362003-05-12 21:50:16 +0000458
459 PRINTK2 ("MMU ready\n");
460 }
461
462 return length;
463}
464
wdenkaa603362003-05-12 21:50:16 +0000465
466/*
467 * Open and Initialize the board
468 *
469 * Set up everything, reset the card, etc ..
470 *
471 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500472static int smc_open(bd_t *bd, struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000473{
wdenk6b1aa8d2004-06-06 22:11:41 +0000474 int i, err; /* used to set hw ethernet address */
wdenkaa603362003-05-12 21:50:16 +0000475
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500476 PRINTK2("%s:smc_open\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000477
478 /* reset the hardware */
479
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500480 smc_reset(dev);
481 smc_enable(dev);
wdenkaa603362003-05-12 21:50:16 +0000482
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500483 SMC_SELECT_BANK(dev, 1);
484 /* set smc_mac_addr, and sync it with u-boot globals */
485 err = smc_get_ethaddr(bd, dev);
Mike Frysinger719b6752009-02-11 19:09:54 -0500486 if (err < 0)
487 return -1;
wdenk6b1aa8d2004-06-06 22:11:41 +0000488#ifdef USE_32_BIT
wdenkaa603362003-05-12 21:50:16 +0000489 for (i = 0; i < 6; i += 2) {
490 word address;
491
492 address = smc_mac_addr[i + 1] << 8;
493 address |= smc_mac_addr[i];
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500494 SMC_outw(dev, address, LAN91C96_IA0 + i);
wdenkaa603362003-05-12 21:50:16 +0000495 }
wdenke3a06802004-06-06 23:13:55 +0000496#else
wdenk6b1aa8d2004-06-06 22:11:41 +0000497 for (i = 0; i < 6; i++)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500498 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
wdenk6b1aa8d2004-06-06 22:11:41 +0000499#endif
wdenkaa603362003-05-12 21:50:16 +0000500 return 0;
501}
502
503/*-------------------------------------------------------------
504 *
505 * smc_rcv - receive a packet from the card
506 *
507 * There is ( at least ) a packet waiting to be read from
508 * chip-memory.
509 *
510 * o Read the status
511 * o If an error, record it
512 * o otherwise, read in the packet
513 *-------------------------------------------------------------
514 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500515static int smc_rcv(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000516{
517 int packet_number;
518 word status;
519 word packet_length;
520 int is_error = 0;
521
522#ifdef USE_32_BIT
523 dword stat_len;
524#endif
525
526
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500527 SMC_SELECT_BANK(dev, 2);
528 packet_number = SMC_inw(dev, LAN91C96_FIFO);
wdenkaa603362003-05-12 21:50:16 +0000529
530 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
531 return 0;
532 }
533
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500534 PRINTK3("%s:smc_rcv\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000535 /* start reading from the start of the packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500536 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
wdenkaa603362003-05-12 21:50:16 +0000537 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
538
539 /* First two words are status and packet_length */
540#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500541 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000542 status = stat_len & 0xffff;
543 packet_length = stat_len >> 16;
544#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500545 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
546 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000547#endif
548
549 packet_length &= 0x07ff; /* mask off top bits */
550
551 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
552
553 if (!(status & FRAME_FILTER)) {
554 /* Adjust for having already read the first two words */
555 packet_length -= 4; /*4; */
556
557
wdenkaa603362003-05-12 21:50:16 +0000558 /* set odd length for bug in LAN91C111, */
559 /* which never sets RS_ODDFRAME */
560 /* TODO ? */
561
562
563#ifdef USE_32_BIT
564 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
565 packet_length >> 2, packet_length & 3);
566 /* QUESTION: Like in the TX routine, do I want
567 to send the DWORDs or the bytes first, or some
568 mixture. A mixture might improve already slow PIO
569 performance */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500570 SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
571 packet_length >> 2);
wdenkaa603362003-05-12 21:50:16 +0000572 /* read the left over bytes */
573 if (packet_length & 3) {
574 int i;
575
Joe Hershberger9f09a362015-04-08 01:41:06 -0500576 byte *tail = (byte *)(net_rx_packets[0] +
577 (packet_length & ~3));
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500578 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000579
580 for (i = 0; i < (packet_length & 3); i++)
581 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
582 }
583#else
Joe Hershberger9f09a362015-04-08 01:41:06 -0500584 PRINTK3(" Reading %d words and %d byte(s)\n",
585 (packet_length >> 1), packet_length & 1);
586 SMC_insw(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
587 packet_length >> 1);
wdenkaa603362003-05-12 21:50:16 +0000588
589#endif /* USE_32_BIT */
590
591#if SMC_DEBUG > 2
592 printf ("Receiving Packet\n");
Joe Hershberger9f09a362015-04-08 01:41:06 -0500593 print_packet((byte *)net_rx_packets[0], packet_length);
wdenkaa603362003-05-12 21:50:16 +0000594#endif
595 } else {
596 /* error ... */
597 /* TODO ? */
598 is_error = 1;
599 }
600
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500601 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000602 udelay (1); /* Wait until not busy */
603
604 /* error or good, tell the card to get rid of this packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500605 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000606
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500607 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
wdenkaa603362003-05-12 21:50:16 +0000608 udelay (1); /* Wait until not busy */
609
610 if (!is_error) {
611 /* Pass the packet up to the protocol layers. */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500612 net_process_received_packet(net_rx_packets[0], packet_length);
wdenkaa603362003-05-12 21:50:16 +0000613 return packet_length;
614 } else {
615 return 0;
616 }
617
618}
619
620/*----------------------------------------------------
621 * smc_close
622 *
623 * this makes the board clean up everything that it can
624 * and not talk to the outside world. Caused by
625 * an 'ifconfig ethX down'
626 *
627 -----------------------------------------------------*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500628static int smc_close(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000629{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500630 PRINTK2("%s:smc_close\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000631
632 /* clear everything */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500633 smc_shutdown(dev);
wdenkaa603362003-05-12 21:50:16 +0000634
635 return 0;
636}
637
638#if SMC_DEBUG > 2
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500639static void print_packet(byte *buf, int length)
wdenkaa603362003-05-12 21:50:16 +0000640{
641#if 0
642 int i;
643 int remainder;
644 int lines;
645
646 printf ("Packet of length %d \n", length);
647
648 lines = length / 16;
649 remainder = length % 16;
650
651 for (i = 0; i < lines; i++) {
652 int cur;
653
654 for (cur = 0; cur < 8; cur++) {
655 byte a, b;
656
657 a = *(buf++);
658 b = *(buf++);
659 printf ("%02x%02x ", a, b);
660 }
661 printf ("\n");
662 }
663 for (i = 0; i < remainder / 2; i++) {
664 byte a, b;
665
666 a = *(buf++);
667 b = *(buf++);
668 printf ("%02x%02x ", a, b);
669 }
670 printf ("\n");
671#endif /* 0 */
672}
673#endif /* SMC_DEBUG > 2 */
674
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500675static int lan91c96_init(struct eth_device *dev, bd_t *bd)
wdenkaa603362003-05-12 21:50:16 +0000676{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500677 return smc_open(bd, dev);
wdenkaa603362003-05-12 21:50:16 +0000678}
679
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500680static void lan91c96_halt(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000681{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500682 smc_close(dev);
wdenkaa603362003-05-12 21:50:16 +0000683}
684
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500685static int lan91c96_recv(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000686{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500687 return smc_rcv(dev);
wdenkaa603362003-05-12 21:50:16 +0000688}
689
Joe Hershbergerc78248f2012-05-21 14:45:30 +0000690static int lan91c96_send(struct eth_device *dev, void *packet,
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500691 int length)
wdenkaa603362003-05-12 21:50:16 +0000692{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500693 return smc_send_packet(dev, packet, length);
wdenkaa603362003-05-12 21:50:16 +0000694}
695
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500696/* smc_get_ethaddr
wdenk6b1aa8d2004-06-06 22:11:41 +0000697 *
698 * This checks both the environment and the ROM for an ethernet address. If
699 * found, the environment takes precedence.
700 */
701
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500702static int smc_get_ethaddr(bd_t *bd, struct eth_device *dev)
wdenk6b1aa8d2004-06-06 22:11:41 +0000703{
Mike Frysinger719b6752009-02-11 19:09:54 -0500704 uchar v_mac[6];
wdenk6b1aa8d2004-06-06 22:11:41 +0000705
Simon Glass399a9ce2017-08-03 12:22:14 -0600706 if (!eth_env_get_enetaddr("ethaddr", v_mac)) {
Mike Frysinger719b6752009-02-11 19:09:54 -0500707 /* get ROM mac value if any */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500708 if (!get_rom_mac(dev, v_mac)) {
Mike Frysinger719b6752009-02-11 19:09:54 -0500709 printf("\n*** ERROR: ethaddr is NOT set !!\n");
710 return -1;
wdenk6b1aa8d2004-06-06 22:11:41 +0000711 }
Simon Glass8551d552017-08-03 12:22:11 -0600712 eth_env_set_enetaddr("ethaddr", v_mac);
wdenk6b1aa8d2004-06-06 22:11:41 +0000713 }
714
Mike Frysinger719b6752009-02-11 19:09:54 -0500715 smc_set_mac_addr(v_mac); /* use old function to update smc default */
716 PRINTK("Using MAC Address %pM\n", v_mac);
717 return 0;
wdenk6b1aa8d2004-06-06 22:11:41 +0000718}
719
wdenke3a06802004-06-06 23:13:55 +0000720/*
wdenk6b1aa8d2004-06-06 22:11:41 +0000721 * get_rom_mac()
722 * Note, this has omly been tested for the OMAP730 P2.
723 */
724
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500725static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
wdenk6b1aa8d2004-06-06 22:11:41 +0000726{
wdenk6b1aa8d2004-06-06 22:11:41 +0000727 int i;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500728 SMC_SELECT_BANK(dev, 1);
wdenk6b1aa8d2004-06-06 22:11:41 +0000729 for (i=0; i<6; i++)
730 {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500731 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
wdenk6b1aa8d2004-06-06 22:11:41 +0000732 }
733 return (1);
wdenk6b1aa8d2004-06-06 22:11:41 +0000734}
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500735
736/* Structure to detect the device IDs */
737struct id_type {
738 u8 id;
739 char *name;
740};
741static struct id_type supported_chips[] = {
742 {0, ""}, /* Dummy entry to prevent id check failure */
743 {9, "LAN91C110"},
744 {8, "LAN91C100FD"},
745 {7, "LAN91C100"},
746 {5, "LAN91C95"},
Yanjun Yangf52a06c2010-12-26 10:40:54 +0800747 {4, "LAN91C94/96"},
748 {3, "LAN91C90/92"},
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500749};
750/* lan91c96_detect_chip
751 * See:
752 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
753 * page 71 - that is the closest we get to detect this device
754 */
755static int lan91c96_detect_chip(struct eth_device *dev)
756{
757 u8 chip_id;
758 int r;
759 SMC_SELECT_BANK(dev, 3);
Yanjun Yangd99ecfec2010-12-28 16:08:25 +0800760 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500761 SMC_SELECT_BANK(dev, 0);
Axel Lin10a72b62013-07-03 11:24:18 +0800762 for (r = 0; r < ARRAY_SIZE(supported_chips); r++)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500763 if (chip_id == supported_chips[r].id)
764 return r;
765 return 0;
766}
767
768int lan91c96_initialize(u8 dev_num, int base_addr)
769{
770 struct eth_device *dev;
771 int r = 0;
772
773 dev = malloc(sizeof(*dev));
774 if (!dev) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500775 return 0;
776 }
777 memset(dev, 0, sizeof(*dev));
778
779 dev->iobase = base_addr;
780
781 /* Try to detect chip. Will fail if not present. */
782 r = lan91c96_detect_chip(dev);
783 if (!r) {
784 free(dev);
785 return 0;
786 }
787 get_rom_mac(dev, dev->enetaddr);
788
789 dev->init = lan91c96_init;
790 dev->halt = lan91c96_halt;
791 dev->send = lan91c96_send;
792 dev->recv = lan91c96_recv;
793 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
794
795 eth_register(dev);
796 return 0;
797}