blob: c2f611144db6cf4702dadd2b133af2753c52a311 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk45219c42003-05-12 21:50:16 +00002/*------------------------------------------------------------------------
3 * lan91c96.c
4 * This is a driver for SMSC's LAN91C96 single-chip Ethernet device, based
Bin Menga1875592016-02-05 19:30:11 -08005 * on the SMC91111 driver from U-Boot.
wdenk45219c42003-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 *
wdenk45219c42003-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 *
wdenk45219c42003-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-VILLARD1b769882008-01-25 07:54:47 +010025 * io = for the base address
wdenk45219c42003-05-12 21:50:16 +000026 * irq = for the IRQ
27 *
28 * author:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +010029 * Erik Stahlman ( erik@vt.edu )
30 * Daris A Nevil ( dnevil@snmc.com )
wdenk45219c42003-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-VILLARD1b769882008-01-25 07:54:47 +010042 * 04/30/03 Mathijs Haarman Modified smc91111.c (u-boot version)
wdenk45219c42003-05-12 21:50:16 +000043 * for lan91c96
44 *---------------------------------------------------------------------------
45 */
46
47#include <common.h>
48#include <command.h>
Simon Glass9fb625c2019-08-01 09:46:51 -060049#include <env.h>
Nishanth Menonb7ad4102009-10-16 00:06:35 -050050#include <malloc.h>
Simon Glassc05ed002020-05-10 11:40:11 -060051#include <linux/delay.h>
wdenk45219c42003-05-12 21:50:16 +000052#include "lan91c96.h"
53#include <net.h>
Anatolij Gustschindffc0ae2011-11-19 13:12:12 +000054#include <linux/compiler.h>
wdenk45219c42003-05-12 21:50:16 +000055
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500101#define DRIVER_NAME "LAN91C96"
wdenk45219c42003-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
wdenk5ca26792004-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 Yamadab75d8dc2020-06-26 15:13:33 +0900117static int smc_get_ethaddr(struct bd_info *bd, struct eth_device *dev);
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500118static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac);
wdenk45219c42003-05-12 21:50:16 +0000119
wdenk45219c42003-05-12 21:50:16 +0000120/* ------------------------------------------------------------
121 * Internal routines
122 * ------------------------------------------------------------
123 */
124
Wolfgang Denkd52fb7e2006-03-11 22:53:33 +0100125static unsigned char smc_mac_addr[] = { 0xc0, 0x00, 0x00, 0x1b, 0x62, 0x9c };
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500132static void smc_set_mac_addr(const unsigned char *addr)
wdenk45219c42003-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}
140
wdenk45219c42003-05-12 21:50:16 +0000141/***********************************************
142 * Show available memory *
143 ***********************************************/
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500144void dump_memory_info(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000145{
Anatolij Gustschindffc0ae2011-11-19 13:12:12 +0000146 __maybe_unused word mem_info;
wdenk45219c42003-05-12 21:50:16 +0000147 word old_bank;
148
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500149 old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT) & 0xF;
wdenk45219c42003-05-12 21:50:16 +0000150
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500151 SMC_SELECT_BANK(dev, 0);
152 mem_info = SMC_inw(dev, LAN91C96_MIR);
wdenk45219c42003-05-12 21:50:16 +0000153 PRINTK2 ("Memory: %4d available\n", (mem_info >> 8) * 2048);
154
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500155 SMC_SELECT_BANK(dev, old_bank);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500165static int poll4int (struct eth_device *dev, byte mask, int timeout)
wdenk45219c42003-05-12 21:50:16 +0000166{
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200167 int tmo = get_timer (0) + timeout * CONFIG_SYS_HZ;
wdenk45219c42003-05-12 21:50:16 +0000168 int is_timeout = 0;
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500169 word old_bank = SMC_inw(dev, LAN91C96_BANK_SELECT);
wdenk45219c42003-05-12 21:50:16 +0000170
171 PRINTK2 ("Polling...\n");
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500172 SMC_SELECT_BANK(dev, 2);
173 while ((SMC_inw(dev, LAN91C96_INT_STATS) & mask) == 0) {
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500181 SMC_SELECT_BANK(dev, old_bank);
wdenk45219c42003-05-12 21:50:16 +0000182
183 if (is_timeout)
184 return 1;
185 else
186 return 0;
187}
188
189/*
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500190 * Function: smc_reset
wdenk45219c42003-05-12 21:50:16 +0000191 * Purpose:
Jean-Christophe PLAGNIOL-VILLARD1b769882008-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.
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500206static void smc_reset(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000207{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500208 PRINTK2("%s:smc_reset\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000209
210 /* This resets the registers mostly to defaults, but doesn't
211 affect EEPROM. That seems unnecessary */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500212 SMC_SELECT_BANK(dev, 0);
213 SMC_outw(dev, LAN91C96_RCR_SOFT_RST, LAN91C96_RCR);
wdenk45219c42003-05-12 21:50:16 +0000214
Simon Glass07e11142020-05-10 11:40:10 -0600215 udelay(10);
wdenk45219c42003-05-12 21:50:16 +0000216
217 /* Disable transmit and receive functionality */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500218 SMC_outw(dev, 0, LAN91C96_RCR);
219 SMC_outw(dev, 0, LAN91C96_TCR);
wdenk45219c42003-05-12 21:50:16 +0000220
221 /* set the control register */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500222 SMC_SELECT_BANK(dev, 1);
223 SMC_outw(dev, SMC_inw(dev, LAN91C96_CONTROL) | LAN91C96_CTR_BIT_8,
wdenk45219c42003-05-12 21:50:16 +0000224 LAN91C96_CONTROL);
225
226 /* Disable all interrupts */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500227 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500238static void smc_enable(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000239{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500240 PRINTK2("%s:smc_enable\n", dev->name);
241 SMC_SELECT_BANK(dev, 0);
wdenk45219c42003-05-12 21:50:16 +0000242
243 /* Initialize the Memory Configuration Register. See page
244 49 of the LAN91C96 data sheet for details. */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500245 SMC_outw(dev, LAN91C96_MCR_TRANSMIT_PAGES, LAN91C96_MCR);
wdenk45219c42003-05-12 21:50:16 +0000246
247 /* Initialize the Transmit Control Register */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500248 SMC_outw(dev, LAN91C96_TCR_TXENA, LAN91C96_TCR);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500255 SMC_outw(dev, LAN91C96_RCR_RXEN | LAN91C96_RCR_PRMS, LAN91C96_RCR);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500272static void smc_shutdown(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000273{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500274 PRINTK2("%s:smc_shutdown\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000275
276 /* no more interrupts for me */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500277 SMC_SELECT_BANK(dev, 2);
278 SMC_outb(dev, 0, LAN91C96_INT_MASK);
wdenk45219c42003-05-12 21:50:16 +0000279
280 /* and tell the card to stay away from that nasty outside world */
Nishanth Menonb7ad4102009-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);
wdenk45219c42003-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-VILLARD1b769882008-01-25 07:54:47 +0100293 * First, see if a saved_skb is available.
wdenk45219c42003-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-VILLARD1b769882008-01-25 07:54:47 +0100301 * Tell the card to send it
wdenk45219c42003-05-12 21:50:16 +0000302 * Enable the transmit interrupt, so I know if it failed
Jean-Christophe PLAGNIOL-VILLARD1b769882008-01-25 07:54:47 +0100303 * Free the kernel data if I actually sent it.
wdenk45219c42003-05-12 21:50:16 +0000304 */
Joe Hershberger30934b32012-05-21 14:45:30 +0000305static int smc_send_packet(struct eth_device *dev, void *packet,
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500306 int packet_length)
wdenk45219c42003-05-12 21:50:16 +0000307{
308 byte packet_no;
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500317 PRINTK3("%s:smc_hardware_send_packet\n", dev->name);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500337 printf("%s: Far too big packet error. \n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000338 return 0;
339 }
340
341 /* now, try to allocate the memory */
342
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500343 SMC_SELECT_BANK(dev, 2);
344 SMC_outw(dev, LAN91C96_MMUCR_ALLOC_TX | numPages, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000345
346 again:
347 try++;
348 time_out = MEMORY_WAIT_TIME;
349 do {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500350 status = SMC_inb(dev, LAN91C96_INT_STATS);
wdenk45219c42003-05-12 21:50:16 +0000351 if (status & LAN91C96_IST_ALLOC_INT) {
352
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500353 SMC_outb(dev, LAN91C96_IST_ALLOC_INT,
354 LAN91C96_INT_STATS);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500361 dev->name, try);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500369 dev->name, try);
wdenk45219c42003-05-12 21:50:16 +0000370
371 /* I can send the packet now.. */
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500375 packet_no = SMC_inb(dev, LAN91C96_ARR);
wdenk45219c42003-05-12 21:50:16 +0000376 if (packet_no & LAN91C96_ARR_FAILED) {
377 /* or isn't there? BAD CHIP! */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500378 printf("%s: Memory allocation failed. \n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000379 return 0;
380 }
381
382 /* we have a packet address, so tell the card to use it */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500383 SMC_outb(dev, packet_no, LAN91C96_PNR);
wdenk45219c42003-05-12 21:50:16 +0000384
385 /* point to the beginning of the packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500386 SMC_outw(dev, LAN91C96_PTR_AUTO_INCR, LAN91C96_POINTER);
wdenk45219c42003-05-12 21:50:16 +0000387
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500388 PRINTK3("%s: Trying to xmit packet of length %x\n",
389 dev->name, length);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500399 SMC_outl(dev, (length + 6) << 16, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000400#else
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500401 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000402 /* send the packet length ( +6 for status words, length, and ctl */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500403 SMC_outw(dev, (length + 6), LAN91C96_DATA_HIGH);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500414 SMC_outsl(dev, LAN91C96_DATA_HIGH, buf, length >> 2);
wdenk45219c42003-05-12 21:50:16 +0000415 if (length & 0x2)
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500416 SMC_outw(dev, *((word *) (buf + (length & 0xFFFFFFFC))),
wdenk45219c42003-05-12 21:50:16 +0000417 LAN91C96_DATA_HIGH);
418#else
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500419 SMC_outsw(dev, LAN91C96_DATA_HIGH, buf, (length) >> 1);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500424 SMC_outw(dev, 0, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000425 } else {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500426 SMC_outw(dev, buf[length - 1] | 0x2000, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000427 }
428
429 /* and let the chipset deal with it */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500430 SMC_outw(dev, LAN91C96_MMUCR_ENQUEUE, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000431
432 /* poll for TX INT */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500433 if (poll4int (dev, LAN91C96_MSK_TX_INT, SMC_TX_TIMEOUT)) {
wdenk45219c42003-05-12 21:50:16 +0000434 /* sending failed */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500435 PRINTK2("%s: TX timeout, sending failed...\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000436
437 /* release packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500438 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000439
440 /* wait for MMU getting ready (low) */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500441 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass07e11142020-05-10 11:40:10 -0600442 udelay(10);
wdenk45219c42003-05-12 21:50:16 +0000443
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500444 PRINTK2("MMU ready\n");
wdenk45219c42003-05-12 21:50:16 +0000445
446
447 return 0;
448 } else {
449 /* ack. int */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500450 SMC_outw(dev, LAN91C96_IST_TX_INT, LAN91C96_INT_STATS);
wdenk45219c42003-05-12 21:50:16 +0000451
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500452 PRINTK2("%s: Sent packet of length %d \n", dev->name, length);
wdenk45219c42003-05-12 21:50:16 +0000453
454 /* release packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500455 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_TX, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000456
457 /* wait for MMU getting ready (low) */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500458 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass07e11142020-05-10 11:40:10 -0600459 udelay(10);
wdenk45219c42003-05-12 21:50:16 +0000460
461 PRINTK2 ("MMU ready\n");
462 }
463
464 return length;
465}
466
wdenk45219c42003-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 Yamadab75d8dc2020-06-26 15:13:33 +0900474static int smc_open(struct bd_info *bd, struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000475{
wdenk5ca26792004-06-06 22:11:41 +0000476 int i, err; /* used to set hw ethernet address */
wdenk45219c42003-05-12 21:50:16 +0000477
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500478 PRINTK2("%s:smc_open\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000479
480 /* reset the hardware */
481
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500482 smc_reset(dev);
483 smc_enable(dev);
wdenk45219c42003-05-12 21:50:16 +0000484
Nishanth Menonb7ad4102009-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 Frysinger03f3d8d2009-02-11 19:09:54 -0500488 if (err < 0)
489 return -1;
wdenk5ca26792004-06-06 22:11:41 +0000490#ifdef USE_32_BIT
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500496 SMC_outw(dev, address, LAN91C96_IA0 + i);
wdenk45219c42003-05-12 21:50:16 +0000497 }
wdenka56bd922004-06-06 23:13:55 +0000498#else
wdenk5ca26792004-06-06 22:11:41 +0000499 for (i = 0; i < 6; i++)
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500500 SMC_outb(dev, smc_mac_addr[i], LAN91C96_IA0 + i);
wdenk5ca26792004-06-06 22:11:41 +0000501#endif
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500517static int smc_rcv(struct eth_device *dev)
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500529 SMC_SELECT_BANK(dev, 2);
530 packet_number = SMC_inw(dev, LAN91C96_FIFO);
wdenk45219c42003-05-12 21:50:16 +0000531
532 if (packet_number & LAN91C96_FIFO_RXEMPTY) {
533 return 0;
534 }
535
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500536 PRINTK3("%s:smc_rcv\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000537 /* start reading from the start of the packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500538 SMC_outw(dev, LAN91C96_PTR_READ | LAN91C96_PTR_RCV |
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500543 stat_len = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenk45219c42003-05-12 21:50:16 +0000544 status = stat_len & 0xffff;
545 packet_length = stat_len >> 16;
546#else
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500547 status = SMC_inw(dev, LAN91C96_DATA_HIGH);
548 packet_length = SMC_inw(dev, LAN91C96_DATA_HIGH);
wdenk45219c42003-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
wdenk45219c42003-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 Hershberger1fd92db2015-04-08 01:41:06 -0500572 SMC_insl(dev, LAN91C96_DATA_HIGH, net_rx_packets[0],
573 packet_length >> 2);
wdenk45219c42003-05-12 21:50:16 +0000574 /* read the left over bytes */
575 if (packet_length & 3) {
576 int i;
577
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500578 byte *tail = (byte *)(net_rx_packets[0] +
579 (packet_length & ~3));
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500580 dword leftover = SMC_inl(dev, LAN91C96_DATA_HIGH);
wdenk45219c42003-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 Hershberger1fd92db2015-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);
wdenk45219c42003-05-12 21:50:16 +0000590
591#endif /* USE_32_BIT */
592
593#if SMC_DEBUG > 2
594 printf ("Receiving Packet\n");
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500595 print_packet((byte *)net_rx_packets[0], packet_length);
wdenk45219c42003-05-12 21:50:16 +0000596#endif
597 } else {
598 /* error ... */
599 /* TODO ? */
600 is_error = 1;
601 }
602
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500603 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass07e11142020-05-10 11:40:10 -0600604 udelay(1); /* Wait until not busy */
wdenk45219c42003-05-12 21:50:16 +0000605
606 /* error or good, tell the card to get rid of this packet */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500607 SMC_outw(dev, LAN91C96_MMUCR_RELEASE_RX, LAN91C96_MMU);
wdenk45219c42003-05-12 21:50:16 +0000608
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500609 while (SMC_inw(dev, LAN91C96_MMU) & LAN91C96_MMUCR_NO_BUSY)
Simon Glass07e11142020-05-10 11:40:10 -0600610 udelay(1); /* Wait until not busy */
wdenk45219c42003-05-12 21:50:16 +0000611
612 if (!is_error) {
613 /* Pass the packet up to the protocol layers. */
Joe Hershberger1fd92db2015-04-08 01:41:06 -0500614 net_process_received_packet(net_rx_packets[0], packet_length);
wdenk45219c42003-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 Menonb7ad4102009-10-16 00:06:35 -0500630static int smc_close(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000631{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500632 PRINTK2("%s:smc_close\n", dev->name);
wdenk45219c42003-05-12 21:50:16 +0000633
634 /* clear everything */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500635 smc_shutdown(dev);
wdenk45219c42003-05-12 21:50:16 +0000636
637 return 0;
638}
639
640#if SMC_DEBUG > 2
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500641static void print_packet(byte *buf, int length)
wdenk45219c42003-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 Yamadab75d8dc2020-06-26 15:13:33 +0900677static int lan91c96_init(struct eth_device *dev, struct bd_info *bd)
wdenk45219c42003-05-12 21:50:16 +0000678{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500679 return smc_open(bd, dev);
wdenk45219c42003-05-12 21:50:16 +0000680}
681
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500682static void lan91c96_halt(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000683{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500684 smc_close(dev);
wdenk45219c42003-05-12 21:50:16 +0000685}
686
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500687static int lan91c96_recv(struct eth_device *dev)
wdenk45219c42003-05-12 21:50:16 +0000688{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500689 return smc_rcv(dev);
wdenk45219c42003-05-12 21:50:16 +0000690}
691
Joe Hershberger30934b32012-05-21 14:45:30 +0000692static int lan91c96_send(struct eth_device *dev, void *packet,
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500693 int length)
wdenk45219c42003-05-12 21:50:16 +0000694{
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500695 return smc_send_packet(dev, packet, length);
wdenk45219c42003-05-12 21:50:16 +0000696}
697
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500698/* smc_get_ethaddr
wdenk5ca26792004-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 Yamadab75d8dc2020-06-26 15:13:33 +0900704static int smc_get_ethaddr(struct bd_info *bd, struct eth_device *dev)
wdenk5ca26792004-06-06 22:11:41 +0000705{
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500706 uchar v_mac[6];
wdenk5ca26792004-06-06 22:11:41 +0000707
Simon Glass35affd72017-08-03 12:22:14 -0600708 if (!eth_env_get_enetaddr("ethaddr", v_mac)) {
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500709 /* get ROM mac value if any */
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500710 if (!get_rom_mac(dev, v_mac)) {
Mike Frysinger03f3d8d2009-02-11 19:09:54 -0500711 printf("\n*** ERROR: ethaddr is NOT set !!\n");
712 return -1;
wdenk5ca26792004-06-06 22:11:41 +0000713 }
Simon Glassfd1e9592017-08-03 12:22:11 -0600714 eth_env_set_enetaddr("ethaddr", v_mac);
wdenk5ca26792004-06-06 22:11:41 +0000715 }
716
Mike Frysinger03f3d8d2009-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;
wdenk5ca26792004-06-06 22:11:41 +0000720}
721
wdenka56bd922004-06-06 23:13:55 +0000722/*
wdenk5ca26792004-06-06 22:11:41 +0000723 * get_rom_mac()
724 * Note, this has omly been tested for the OMAP730 P2.
725 */
726
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500727static int get_rom_mac(struct eth_device *dev, unsigned char *v_rom_mac)
wdenk5ca26792004-06-06 22:11:41 +0000728{
wdenk5ca26792004-06-06 22:11:41 +0000729 int i;
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500730 SMC_SELECT_BANK(dev, 1);
wdenk5ca26792004-06-06 22:11:41 +0000731 for (i=0; i<6; i++)
732 {
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500733 v_rom_mac[i] = SMC_inb(dev, LAN91C96_IA0 + i);
wdenk5ca26792004-06-06 22:11:41 +0000734 }
735 return (1);
wdenk5ca26792004-06-06 22:11:41 +0000736}
Nishanth Menonb7ad4102009-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 Yang0e7790d2010-12-26 10:40:54 +0800749 {4, "LAN91C94/96"},
750 {3, "LAN91C90/92"},
Nishanth Menonb7ad4102009-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 Yang16721712010-12-28 16:08:25 +0800762 chip_id = (SMC_inw(dev, 0xA) & LAN91C96_REV_CHIPID) >> 4;
Nishanth Menonb7ad4102009-10-16 00:06:35 -0500763 SMC_SELECT_BANK(dev, 0);
Axel Lina62cd292013-07-03 11:24:18 +0800764 for (r = 0; r < ARRAY_SIZE(supported_chips); r++)
Nishanth Menonb7ad4102009-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 Menonb7ad4102009-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}