blob: c2f611144db6cf4702dadd2b133af2753c52a311 [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>
Simon Glass5e6201b2019-08-01 09:46:51 -060049#include <env.h>
Nishanth Menonf95b93b2009-10-16 00:06:35 -050050#include <malloc.h>
Simon Glassdbd79542020-05-10 11:40:11 -060051#include <linux/delay.h>
wdenkaa603362003-05-12 21:50:16 +000052#include "lan91c96.h"
53#include <net.h>
Anatolij Gustschind1d92d72011-11-19 13:12:12 +000054#include <linux/compiler.h>
wdenkaa603362003-05-12 21:50:16 +000055
wdenkaa603362003-05-12 21:50:16 +000056/*------------------------------------------------------------------------
57 *
58 * Configuration options, for the experienced user to change.
59 *
60 -------------------------------------------------------------------------*/
61
62/* Use power-down feature of the chip */
63#define POWER_DOWN 0
64
65/*
66 * Wait time for memory to be free. This probably shouldn't be
67 * tuned that much, as waiting for this means nothing else happens
68 * in the system
69*/
70#define MEMORY_WAIT_TIME 16
71
72#define SMC_DEBUG 0
73
74#if (SMC_DEBUG > 2 )
75#define PRINTK3(args...) printf(args)
76#else
77#define PRINTK3(args...)
78#endif
79
80#if SMC_DEBUG > 1
81#define PRINTK2(args...) printf(args)
82#else
83#define PRINTK2(args...)
84#endif
85
86#ifdef SMC_DEBUG
87#define PRINTK(args...) printf(args)
88#else
89#define PRINTK(args...)
90#endif
91
92
93/*------------------------------------------------------------------------
94 *
95 * The internal workings of the driver. If you are changing anything
96 * here with the SMC stuff, you should have the datasheet and know
97 * what you are doing.
98 *
99 *------------------------------------------------------------------------
100 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500101#define DRIVER_NAME "LAN91C96"
wdenkaa603362003-05-12 21:50:16 +0000102#define SMC_ALLOC_MAX_TRY 5
103#define SMC_TX_TIMEOUT 30
104
105#define ETH_ZLEN 60
106
107#ifdef CONFIG_LAN91C96_USE_32_BIT
108#define USE_32_BIT 1
109#else
110#undef USE_32_BIT
111#endif
112
wdenk6b1aa8d2004-06-06 22:11:41 +0000113/* See if a MAC address is defined in the current environment. If so use it. If not
114 . print a warning and set the environment and other globals with the default.
115 . If an EEPROM is present it really should be consulted.
116*/
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900117static int smc_get_ethaddr(struct bd_info *bd, struct eth_device *dev);
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500118static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
wdenkaa603362003-05-12 21:50:16 +0000119
wdenkaa603362003-05-12 21:50:16 +0000120/* ------------------------------------------------------------
121 * Internal routines
122 * ------------------------------------------------------------
123 */
124
Wolfgang Denk7fa6e902006-03-11 22:53:33 +0100125static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
wdenkaa603362003-05-12 21:50:16 +0000126
127/*
128 * This function must be called before smc_open() if you want to override
129 * the default mac address.
130 */
131
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500132static void smc_set_mac_addr(const unsigned char *addr)
wdenkaa603362003-05-12 21:50:16 +0000133{
134 int i;
135
136 for (i = 0; i < sizeof (smc_mac_addr); i++) {
137 smc_mac_addr[i] = addr[i];
138 }
139}
wdenkaa603362003-05-12 21:50:16 +0000140
141/***********************************************
142 * Show available memory *
143 ***********************************************/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500144void dump_memory_info(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000145{
Anatolij Gustschind1d92d72011-11-19 13:12:12 +0000146 __maybe_unused word mem_info;
wdenkaa603362003-05-12 21:50:16 +0000147 word old_bank;
148
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500149 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
wdenkaa603362003-05-12 21:50:16 +0000150
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500151 SMC_SELECT_BANK(dev, 0);
152 mem_info = SMC_inw(dev, LAN91C96_MIR);
wdenkaa603362003-05-12 21:50:16 +0000153 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
154
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500155 SMC_SELECT_BANK(dev, old_bank);
wdenkaa603362003-05-12 21:50:16 +0000156}
157
158/*
159 * A rather simple routine to print out a packet for debugging purposes.
160 */
161#if SMC_DEBUG > 2
162static void print_packet (byte *, int);
163#endif
164
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500165static int poll4int (struct eth_device *dev, byte mask, int timeout)
wdenkaa603362003-05-12 21:50:16 +0000166{
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200167 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
wdenkaa603362003-05-12 21:50:16 +0000168 int is_timeout = 0;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500169 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
wdenkaa603362003-05-12 21:50:16 +0000170
171 PRINTK2 ("Polling...\n");
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500172 SMC_SELECT_BANK(dev, 2);
173 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
wdenkaa603362003-05-12 21:50:16 +0000174 if (get_timer (0) >= tmo) {
175 is_timeout = 1;
176 break;
177 }
178 }
179
180 /* restore old bank selection */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500181 SMC_SELECT_BANK(dev, old_bank);
wdenkaa603362003-05-12 21:50:16 +0000182
183 if (is_timeout)
184 return 1;
185 else
186 return 0;
187}
188
189/*
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500190 * Function: smc_reset
wdenkaa603362003-05-12 21:50:16 +0000191 * Purpose:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100192 * This sets the SMC91111 chip to its normal state, hopefully from whatever
193 * mess that any other DOS driver has put it in.
wdenkaa603362003-05-12 21:50:16 +0000194 *
195 * Maybe I should reset more registers to defaults in here? SOFTRST should
196 * do that for me.
197 *
198 * Method:
199 * 1. send a SOFT RESET
200 * 2. wait for it to finish
201 * 3. enable autorelease mode
202 * 4. reset the memory management unit
203 * 5. clear all interrupts
204 *
205*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500206static void smc_reset(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000207{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500208 PRINTK2("%s:smc_reset\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000209
210 /* This resets the registers mostly to defaults, but doesn't
211 affect EEPROM. That seems unnecessary */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500212 SMC_SELECT_BANK(dev, 0);
213 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
wdenkaa603362003-05-12 21:50:16 +0000214
Simon Glass0db4b942020-05-10 11:40:10 -0600215 udelay(10);
wdenkaa603362003-05-12 21:50:16 +0000216
217 /* Disable transmit and receive functionality */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500218 SMC_outw(dev, 0, LAN91C96_RCR);
219 SMC_outw(dev, 0, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000220
221 /* set the control register */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500222 SMC_SELECT_BANK(dev, 1);
223 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
wdenkaa603362003-05-12 21:50:16 +0000224 LAN91C96_CONTROL);
225
226 /* Disable all interrupts */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500227 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenkaa603362003-05-12 21:50:16 +0000228}
229
230/*
231 * Function: smc_enable
232 * Purpose: let the chip talk to the outside work
233 * Method:
234 * 1. Initialize the Memory Configuration Register
235 * 2. Enable the transmitter
236 * 3. Enable the receiver
237*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500238static void smc_enable(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000239{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500240 PRINTK2("%s:smc_enable\n", dev->name);
241 SMC_SELECT_BANK(dev, 0);
wdenkaa603362003-05-12 21:50:16 +0000242
243 /* Initialize the Memory Configuration Register. See page
244 49 of the LAN91C96 data sheet for details. */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500245 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
wdenkaa603362003-05-12 21:50:16 +0000246
247 /* Initialize the Transmit Control Register */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500248 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000249 /* Initialize the Receive Control Register
250 * FIXME:
251 * The promiscuous bit set because I could not receive ARP reply
252 * packets from the server when I send a ARP request. It only works
253 * when I set the promiscuous bit
254 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500255 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
wdenkaa603362003-05-12 21:50:16 +0000256}
257
258/*
259 * Function: smc_shutdown
260 * Purpose: closes down the SMC91xxx chip.
261 * Method:
262 * 1. zero the interrupt mask
263 * 2. clear the enable receive flag
264 * 3. clear the enable xmit flags
265 *
266 * TODO:
267 * (1) maybe utilize power down mode.
268 * Why not yet? Because while the chip will go into power down mode,
269 * the manual says that it will wake up in response to any I/O requests
270 * in the register space. Empirical results do not show this working.
271 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500272static void smc_shutdown(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000273{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500274 PRINTK2("%s:smc_shutdown\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000275
276 /* no more interrupts for me */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500277 SMC_SELECT_BANK(dev, 2);
278 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenkaa603362003-05-12 21:50:16 +0000279
280 /* and tell the card to stay away from that nasty outside world */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500281 SMC_SELECT_BANK(dev, 0);
282 SMC_outb(dev, 0, LAN91C96_RCR);
283 SMC_outb(dev, 0, LAN91C96_TCR);
wdenkaa603362003-05-12 21:50:16 +0000284}
285
286
287/*
288 * Function: smc_hardware_send_packet(struct net_device * )
289 * Purpose:
290 * This sends the actual packet to the SMC9xxx chip.
291 *
292 * Algorithm:
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100293 * First, see if a saved_skb is available.
wdenkaa603362003-05-12 21:50:16 +0000294 * ( this should NOT be called if there is no 'saved_skb'
295 * Now, find the packet number that the chip allocated
296 * Point the data pointers at it in memory
297 * Set the length word in the chip's memory
298 * Dump the packet to chip memory
299 * Check if a last byte is needed ( odd length packet )
300 * if so, set the control flag right
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100301 * Tell the card to send it
wdenkaa603362003-05-12 21:50:16 +0000302 * Enable the transmit interrupt, so I know if it failed
Jean-Christophe PLAGNIOL-VILLARD41348722008-01-25 07:54:47 +0100303 * Free the kernel data if I actually sent it.
wdenkaa603362003-05-12 21:50:16 +0000304 */
Joe Hershbergerc78248f2012-05-21 14:45:30 +0000305static int smc_send_packet(struct eth_device *dev, void *packet,
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500306 int packet_length)
wdenkaa603362003-05-12 21:50:16 +0000307{
308 byte packet_no;
wdenkaa603362003-05-12 21:50:16 +0000309 byte *buf;
310 int length;
311 int numPages;
312 int try = 0;
313 int time_out;
314 byte status;
315
316
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500317 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000318
319 length = ETH_ZLEN < packet_length ? packet_length : ETH_ZLEN;
320
321 /* allocate memory
322 ** The MMU wants the number of pages to be the number of 256 bytes
323 ** 'pages', minus 1 ( since a packet can't ever have 0 pages :) )
324 **
325 ** The 91C111 ignores the size bits, but the code is left intact
326 ** for backwards and future compatibility.
327 **
328 ** Pkt size for allocating is data length +6 (for additional status
329 ** words, length and ctl!)
330 **
331 ** If odd size then last byte is included in this header.
332 */
333 numPages = ((length & 0xfffe) + 6);
334 numPages >>= 8; /* Divide by 256 */
335
336 if (numPages > 7) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500337 printf("%s: Far too big packet error. \n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000338 return 0;
339 }
340
341 /* now, try to allocate the memory */
342
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500343 SMC_SELECT_BANK(dev, 2);
344 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000345
346 again:
347 try++;
348 time_out = MEMORY_WAIT_TIME;
349 do {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500350 status = SMC_inb(dev, LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000351 if (status & LAN91C96_IST_ALLOC_INT) {
352
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500353 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
354 LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000355 break;
356 }
357 } while (--time_out);
358
359 if (!time_out) {
360 PRINTK2 ("%s: memory allocation, try %d failed ...\n",
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500361 dev->name, try);
wdenkaa603362003-05-12 21:50:16 +0000362 if (try < SMC_ALLOC_MAX_TRY)
363 goto again;
364 else
365 return 0;
366 }
367
368 PRINTK2 ("%s: memory allocation, try %d succeeded ...\n",
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500369 dev->name, try);
wdenkaa603362003-05-12 21:50:16 +0000370
371 /* I can send the packet now.. */
wdenkaa603362003-05-12 21:50:16 +0000372 buf = (byte *) packet;
373
374 /* If I get here, I _know_ there is a packet slot waiting for me */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500375 packet_no = SMC_inb(dev, LAN91C96_ARR);
wdenkaa603362003-05-12 21:50:16 +0000376 if (packet_no & LAN91C96_ARR_FAILED) {
377 /* or isn't there? BAD CHIP! */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500378 printf("%s: Memory allocation failed. \n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000379 return 0;
380 }
381
382 /* we have a packet address, so tell the card to use it */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500383 SMC_outb(dev, packet_no, LAN91C96_PNR);
wdenkaa603362003-05-12 21:50:16 +0000384
385 /* point to the beginning of the packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500386 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
wdenkaa603362003-05-12 21:50:16 +0000387
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500388 PRINTK3("%s: Trying to xmit packet of length %x\n",
389 dev->name, length);
wdenkaa603362003-05-12 21:50:16 +0000390
391#if SMC_DEBUG > 2
392 printf ("Transmitting Packet\n");
393 print_packet (buf, length);
394#endif
395
396 /* send the packet length ( +6 for status, length and ctl byte )
397 and the status word ( set to zeros ) */
398#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500399 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000400#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500401 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000402 /* send the packet length ( +6 for status words, length, and ctl */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500403 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000404#endif /* USE_32_BIT */
405
406 /* send the actual data
407 * I _think_ it's faster to send the longs first, and then
408 * mop up by sending the last word. It depends heavily
409 * on alignment, at least on the 486. Maybe it would be
410 * a good idea to check which is optimal? But that could take
411 * almost as much time as is saved?
412 */
413#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500414 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
wdenkaa603362003-05-12 21:50:16 +0000415 if (length & 0x2)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500416 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
wdenkaa603362003-05-12 21:50:16 +0000417 LAN91C96_DATA_HIGH);
418#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500419 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
wdenkaa603362003-05-12 21:50:16 +0000420#endif /* USE_32_BIT */
421
422 /* Send the last byte, if there is one. */
423 if ((length & 1) == 0) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500424 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000425 } else {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500426 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000427 }
428
429 /* and let the chipset deal with it */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500430 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000431
432 /* poll for TX INT */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500433 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
wdenkaa603362003-05-12 21:50:16 +0000434 /* sending failed */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500435 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000436
437 /* release packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500438 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000439
440 /* wait for MMU getting ready (low) */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500441 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass0db4b942020-05-10 11:40:10 -0600442 udelay(10);
wdenkaa603362003-05-12 21:50:16 +0000443
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500444 PRINTK2("MMU ready\n");
wdenkaa603362003-05-12 21:50:16 +0000445
446
447 return 0;
448 } else {
449 /* ack. int */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500450 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
wdenkaa603362003-05-12 21:50:16 +0000451
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500452 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
wdenkaa603362003-05-12 21:50:16 +0000453
454 /* release packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500455 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000456
457 /* wait for MMU getting ready (low) */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500458 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass0db4b942020-05-10 11:40:10 -0600459 udelay(10);
wdenkaa603362003-05-12 21:50:16 +0000460
461 PRINTK2 ("MMU ready\n");
462 }
463
464 return length;
465}
466
wdenkaa603362003-05-12 21:50:16 +0000467
468/*
469 * Open and Initialize the board
470 *
471 * Set up everything, reset the card, etc ..
472 *
473 */
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900474static int smc_open(struct bd_info *bd, struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000475{
wdenk6b1aa8d2004-06-06 22:11:41 +0000476 int i, err; /* used to set hw ethernet address */
wdenkaa603362003-05-12 21:50:16 +0000477
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500478 PRINTK2("%s:smc_open\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000479
480 /* reset the hardware */
481
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500482 smc_reset(dev);
483 smc_enable(dev);
wdenkaa603362003-05-12 21:50:16 +0000484
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500485 SMC_SELECT_BANK(dev, 1);
486 /* set smc_mac_addr, and sync it with u-boot globals */
487 err = smc_get_ethaddr(bd, dev);
Mike Frysinger719b6752009-02-11 19:09:54 -0500488 if (err < 0)
489 return -1;
wdenk6b1aa8d2004-06-06 22:11:41 +0000490#ifdef USE_32_BIT
wdenkaa603362003-05-12 21:50:16 +0000491 for (i = 0; i < 6; i += 2) {
492 word address;
493
494 address = smc_mac_addr[i + 1] << 8;
495 address |= smc_mac_addr[i];
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500496 SMC_outw(dev, address, LAN91C96_IA0 + i);
wdenkaa603362003-05-12 21:50:16 +0000497 }
wdenke3a06802004-06-06 23:13:55 +0000498#else
wdenk6b1aa8d2004-06-06 22:11:41 +0000499 for (i = 0; i < 6; i++)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500500 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
wdenk6b1aa8d2004-06-06 22:11:41 +0000501#endif
wdenkaa603362003-05-12 21:50:16 +0000502 return 0;
503}
504
505/*-------------------------------------------------------------
506 *
507 * smc_rcv - receive a packet from the card
508 *
509 * There is ( at least ) a packet waiting to be read from
510 * chip-memory.
511 *
512 * o Read the status
513 * o If an error, record it
514 * o otherwise, read in the packet
515 *-------------------------------------------------------------
516 */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500517static int smc_rcv(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000518{
519 int packet_number;
520 word status;
521 word packet_length;
522 int is_error = 0;
523
524#ifdef USE_32_BIT
525 dword stat_len;
526#endif
527
528
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500529 SMC_SELECT_BANK(dev, 2);
530 packet_number = SMC_inw(dev, LAN91C96_FIFO);
wdenkaa603362003-05-12 21:50:16 +0000531
532 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
533 return 0;
534 }
535
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500536 PRINTK3("%s:smc_rcv\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000537 /* start reading from the start of the packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500538 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
wdenkaa603362003-05-12 21:50:16 +0000539 LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
540
541 /* First two words are status and packet_length */
542#ifdef USE_32_BIT
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500543 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000544 status = stat_len & 0xffff;
545 packet_length = stat_len >> 16;
546#else
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500547 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
548 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000549#endif
550
551 packet_length &= 0x07ff; /* mask off top bits */
552
553 PRINTK2 ("RCV: STATUS %4x LENGTH %4x\n", status, packet_length);
554
555 if (!(status & FRAME_FILTER)) {
556 /* Adjust for having already read the first two words */
557 packet_length -= 4; /*4; */
558
559
wdenkaa603362003-05-12 21:50:16 +0000560 /* set odd length for bug in LAN91C111, */
561 /* which never sets RS_ODDFRAME */
562 /* TODO ? */
563
564
565#ifdef USE_32_BIT
566 PRINTK3 (" Reading %d dwords (and %d bytes) \n",
567 packet_length >> 2, packet_length & 3);
568 /* QUESTION: Like in the TX routine, do I want
569 to send the DWORDs or the bytes first, or some
570 mixture. A mixture might improve already slow PIO
571 performance */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500572 SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
573 packet_length >> 2);
wdenkaa603362003-05-12 21:50:16 +0000574 /* read the left over bytes */
575 if (packet_length & 3) {
576 int i;
577
Joe Hershberger9f09a362015-04-08 01:41:06 -0500578 byte *tail = (byte *)(net_rx_packets[0] +
579 (packet_length & ~3));
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500580 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenkaa603362003-05-12 21:50:16 +0000581
582 for (i = 0; i < (packet_length & 3); i++)
583 *tail++ = (byte) (leftover >> (8 * i)) & 0xff;
584 }
585#else
Joe Hershberger9f09a362015-04-08 01:41:06 -0500586 PRINTK3(" Reading %d words and %d byte(s)\n",
587 (packet_length >> 1), packet_length & 1);
588 SMC_insw(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
589 packet_length >> 1);
wdenkaa603362003-05-12 21:50:16 +0000590
591#endif /* USE_32_BIT */
592
593#if SMC_DEBUG > 2
594 printf ("Receiving Packet\n");
Joe Hershberger9f09a362015-04-08 01:41:06 -0500595 print_packet((byte *)net_rx_packets[0], packet_length);
wdenkaa603362003-05-12 21:50:16 +0000596#endif
597 } else {
598 /* error ... */
599 /* TODO ? */
600 is_error = 1;
601 }
602
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500603 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass0db4b942020-05-10 11:40:10 -0600604 udelay(1); /* Wait until not busy */
wdenkaa603362003-05-12 21:50:16 +0000605
606 /* error or good, tell the card to get rid of this packet */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500607 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
wdenkaa603362003-05-12 21:50:16 +0000608
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500609 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass0db4b942020-05-10 11:40:10 -0600610 udelay(1); /* Wait until not busy */
wdenkaa603362003-05-12 21:50:16 +0000611
612 if (!is_error) {
613 /* Pass the packet up to the protocol layers. */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500614 net_process_received_packet(net_rx_packets[0], packet_length);
wdenkaa603362003-05-12 21:50:16 +0000615 return packet_length;
616 } else {
617 return 0;
618 }
619
620}
621
622/*----------------------------------------------------
623 * smc_close
624 *
625 * this makes the board clean up everything that it can
626 * and not talk to the outside world. Caused by
627 * an 'ifconfig ethX down'
628 *
629 -----------------------------------------------------*/
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500630static int smc_close(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000631{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500632 PRINTK2("%s:smc_close\n", dev->name);
wdenkaa603362003-05-12 21:50:16 +0000633
634 /* clear everything */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500635 smc_shutdown(dev);
wdenkaa603362003-05-12 21:50:16 +0000636
637 return 0;
638}
639
640#if SMC_DEBUG > 2
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500641static void print_packet(byte *buf, int length)
wdenkaa603362003-05-12 21:50:16 +0000642{
643#if 0
644 int i;
645 int remainder;
646 int lines;
647
648 printf ("Packet of length %d \n", length);
649
650 lines = length / 16;
651 remainder = length % 16;
652
653 for (i = 0; i < lines; i++) {
654 int cur;
655
656 for (cur = 0; cur < 8; cur++) {
657 byte a, b;
658
659 a = *(buf++);
660 b = *(buf++);
661 printf ("%02x%02x ", a, b);
662 }
663 printf ("\n");
664 }
665 for (i = 0; i < remainder / 2; i++) {
666 byte a, b;
667
668 a = *(buf++);
669 b = *(buf++);
670 printf ("%02x%02x ", a, b);
671 }
672 printf ("\n");
673#endif /* 0 */
674}
675#endif /* SMC_DEBUG > 2 */
676
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900677static int lan91c96_init(struct eth_device *dev, struct bd_info *bd)
wdenkaa603362003-05-12 21:50:16 +0000678{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500679 return smc_open(bd, dev);
wdenkaa603362003-05-12 21:50:16 +0000680}
681
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500682static void lan91c96_halt(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000683{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500684 smc_close(dev);
wdenkaa603362003-05-12 21:50:16 +0000685}
686
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500687static int lan91c96_recv(struct eth_device *dev)
wdenkaa603362003-05-12 21:50:16 +0000688{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500689 return smc_rcv(dev);
wdenkaa603362003-05-12 21:50:16 +0000690}
691
Joe Hershbergerc78248f2012-05-21 14:45:30 +0000692static int lan91c96_send(struct eth_device *dev, void *packet,
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500693 int length)
wdenkaa603362003-05-12 21:50:16 +0000694{
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500695 return smc_send_packet(dev, packet, length);
wdenkaa603362003-05-12 21:50:16 +0000696}
697
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500698/* smc_get_ethaddr
wdenk6b1aa8d2004-06-06 22:11:41 +0000699 *
700 * This checks both the environment and the ROM for an ethernet address. If
701 * found, the environment takes precedence.
702 */
703
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900704static int smc_get_ethaddr(struct bd_info *bd, struct eth_device *dev)
wdenk6b1aa8d2004-06-06 22:11:41 +0000705{
Mike Frysinger719b6752009-02-11 19:09:54 -0500706 uchar v_mac[6];
wdenk6b1aa8d2004-06-06 22:11:41 +0000707
Simon Glass399a9ce2017-08-03 12:22:14 -0600708 if (!eth_env_get_enetaddr("ethaddr", v_mac)) {
Mike Frysinger719b6752009-02-11 19:09:54 -0500709 /* get ROM mac value if any */
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500710 if (!get_rom_mac(dev, v_mac)) {
Mike Frysinger719b6752009-02-11 19:09:54 -0500711 printf("\n*** ERROR: ethaddr is NOT set !!\n");
712 return -1;
wdenk6b1aa8d2004-06-06 22:11:41 +0000713 }
Simon Glass8551d552017-08-03 12:22:11 -0600714 eth_env_set_enetaddr("ethaddr", v_mac);
wdenk6b1aa8d2004-06-06 22:11:41 +0000715 }
716
Mike Frysinger719b6752009-02-11 19:09:54 -0500717 smc_set_mac_addr(v_mac); /* use old function to update smc default */
718 PRINTK("Using MAC Address %pM\n", v_mac);
719 return 0;
wdenk6b1aa8d2004-06-06 22:11:41 +0000720}
721
wdenke3a06802004-06-06 23:13:55 +0000722/*
wdenk6b1aa8d2004-06-06 22:11:41 +0000723 * get_rom_mac()
724 * Note, this has omly been tested for the OMAP730 P2.
725 */
726
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500727static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
wdenk6b1aa8d2004-06-06 22:11:41 +0000728{
wdenk6b1aa8d2004-06-06 22:11:41 +0000729 int i;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500730 SMC_SELECT_BANK(dev, 1);
wdenk6b1aa8d2004-06-06 22:11:41 +0000731 for (i=0; i<6; i++)
732 {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500733 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
wdenk6b1aa8d2004-06-06 22:11:41 +0000734 }
735 return (1);
wdenk6b1aa8d2004-06-06 22:11:41 +0000736}
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500737
738/* Structure to detect the device IDs */
739struct id_type {
740 u8 id;
741 char *name;
742};
743static struct id_type supported_chips[] = {
744 {0, ""}, /* Dummy entry to prevent id check failure */
745 {9, "LAN91C110"},
746 {8, "LAN91C100FD"},
747 {7, "LAN91C100"},
748 {5, "LAN91C95"},
Yanjun Yangf52a06c2010-12-26 10:40:54 +0800749 {4, "LAN91C94/96"},
750 {3, "LAN91C90/92"},
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500751};
752/* lan91c96_detect_chip
753 * See:
754 * http://www.embeddedsys.com/subpages/resources/images/documents/LAN91C96_datasheet.pdf
755 * page 71 - that is the closest we get to detect this device
756 */
757static int lan91c96_detect_chip(struct eth_device *dev)
758{
759 u8 chip_id;
760 int r;
761 SMC_SELECT_BANK(dev, 3);
Yanjun Yangd99ecfec2010-12-28 16:08:25 +0800762 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500763 SMC_SELECT_BANK(dev, 0);
Axel Lin10a72b62013-07-03 11:24:18 +0800764 for (r = 0; r < ARRAY_SIZE(supported_chips); r++)
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500765 if (chip_id == supported_chips[r].id)
766 return r;
767 return 0;
768}
769
770int lan91c96_initialize(u8 dev_num, int base_addr)
771{
772 struct eth_device *dev;
773 int r = 0;
774
775 dev = malloc(sizeof(*dev));
776 if (!dev) {
Nishanth Menonf95b93b2009-10-16 00:06:35 -0500777 return 0;
778 }
779 memset(dev, 0, sizeof(*dev));
780
781 dev->iobase = base_addr;
782
783 /* Try to detect chip. Will fail if not present. */
784 r = lan91c96_detect_chip(dev);
785 if (!r) {
786 free(dev);
787 return 0;
788 }
789 get_rom_mac(dev, dev->enetaddr);
790
791 dev->init = lan91c96_init;
792 dev->halt = lan91c96_halt;
793 dev->send = lan91c96_send;
794 dev->recv = lan91c96_recv;
795 sprintf(dev->name, "%s-%hu", supported_chips[r].name, dev_num);
796
797 eth_register(dev);
798 return 0;
799}