blob: 6dccd59bda66a1a1403567afc81f1b21345e6b5f [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk1df49e22002-09-17 21:37:55 +00002/*
3 * (C) Copyright 2002
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
wdenk1df49e22002-09-17 21:37:55 +00005 */
6
7#include <common.h>
8#include <malloc.h>
9#include <net.h>
Ben Warren052a5ea2008-08-31 20:37:00 -070010#include <netdev.h>
wdenk1df49e22002-09-17 21:37:55 +000011#include <asm/io.h>
12#include <pci.h>
Marian Balakowiczaab8c492005-10-28 22:30:33 +020013#include <miiphy.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
wdenk1df49e22002-09-17 21:37:55 +000015
Marek Vasutdc83bfe2020-05-23 12:49:16 +020016/* Ethernet chip registers. */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020017#define SCBStatus 0 /* Rx/Command Unit Status *Word* */
18#define SCBIntAckByte 1 /* Rx/Command Unit STAT/ACK byte */
19#define SCBCmd 2 /* Rx/Command Unit Command *Word* */
20#define SCBIntrCtlByte 3 /* Rx/Command Unit Intr.Control Byte */
21#define SCBPointer 4 /* General purpose pointer. */
22#define SCBPort 8 /* Misc. commands and operands. */
23#define SCBflash 12 /* Flash memory control. */
24#define SCBeeprom 14 /* EEPROM memory control. */
25#define SCBCtrlMDI 16 /* MDI interface control. */
26#define SCBEarlyRx 20 /* Early receive byte count. */
27#define SCBGenControl 28 /* 82559 General Control Register */
28#define SCBGenStatus 29 /* 82559 General Status register */
wdenk1df49e22002-09-17 21:37:55 +000029
Marek Vasutdc83bfe2020-05-23 12:49:16 +020030/* 82559 SCB status word defnitions */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020031#define SCB_STATUS_CX 0x8000 /* CU finished command (transmit) */
32#define SCB_STATUS_FR 0x4000 /* frame received */
33#define SCB_STATUS_CNA 0x2000 /* CU left active state */
34#define SCB_STATUS_RNR 0x1000 /* receiver left ready state */
35#define SCB_STATUS_MDI 0x0800 /* MDI read/write cycle done */
36#define SCB_STATUS_SWI 0x0400 /* software generated interrupt */
37#define SCB_STATUS_FCP 0x0100 /* flow control pause interrupt */
wdenk1df49e22002-09-17 21:37:55 +000038
Wolfgang Denk4dc11462005-09-26 01:06:33 +020039#define SCB_INTACK_MASK 0xFD00 /* all the above */
wdenk1df49e22002-09-17 21:37:55 +000040
Wolfgang Denk4dc11462005-09-26 01:06:33 +020041#define SCB_INTACK_TX (SCB_STATUS_CX | SCB_STATUS_CNA)
42#define SCB_INTACK_RX (SCB_STATUS_FR | SCB_STATUS_RNR)
wdenk1df49e22002-09-17 21:37:55 +000043
Marek Vasutdc83bfe2020-05-23 12:49:16 +020044/* System control block commands */
wdenk1df49e22002-09-17 21:37:55 +000045/* CU Commands */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020046#define CU_NOP 0x0000
47#define CU_START 0x0010
48#define CU_RESUME 0x0020
49#define CU_STATSADDR 0x0040 /* Load Dump Statistics ctrs addr */
50#define CU_SHOWSTATS 0x0050 /* Dump statistics counters. */
51#define CU_ADDR_LOAD 0x0060 /* Base address to add to CU commands */
52#define CU_DUMPSTATS 0x0070 /* Dump then reset stats counters. */
wdenk1df49e22002-09-17 21:37:55 +000053
54/* RUC Commands */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020055#define RUC_NOP 0x0000
56#define RUC_START 0x0001
57#define RUC_RESUME 0x0002
58#define RUC_ABORT 0x0004
59#define RUC_ADDR_LOAD 0x0006 /* (seems not to clear on acceptance) */
60#define RUC_RESUMENR 0x0007
wdenk1df49e22002-09-17 21:37:55 +000061
Wolfgang Denk4dc11462005-09-26 01:06:33 +020062#define CU_CMD_MASK 0x00f0
63#define RU_CMD_MASK 0x0007
wdenk1df49e22002-09-17 21:37:55 +000064
Wolfgang Denk4dc11462005-09-26 01:06:33 +020065#define SCB_M 0x0100 /* 0 = enable interrupt, 1 = disable */
66#define SCB_SWI 0x0200 /* 1 - cause device to interrupt */
wdenk1df49e22002-09-17 21:37:55 +000067
Wolfgang Denk4dc11462005-09-26 01:06:33 +020068#define CU_STATUS_MASK 0x00C0
69#define RU_STATUS_MASK 0x003C
wdenk1df49e22002-09-17 21:37:55 +000070
Marek Vasute4211ed2020-05-23 13:17:03 +020071#define RU_STATUS_IDLE (0 << 2)
72#define RU_STATUS_SUS (1 << 2)
73#define RU_STATUS_NORES (2 << 2)
74#define RU_STATUS_READY (4 << 2)
75#define RU_STATUS_NO_RBDS_SUS ((1 << 2) | (8 << 2))
76#define RU_STATUS_NO_RBDS_NORES ((2 << 2) | (8 << 2))
77#define RU_STATUS_NO_RBDS_READY ((4 << 2) | (8 << 2))
wdenk1df49e22002-09-17 21:37:55 +000078
Marek Vasutdc83bfe2020-05-23 12:49:16 +020079/* 82559 Port interface commands. */
wdenk1df49e22002-09-17 21:37:55 +000080#define I82559_RESET 0x00000000 /* Software reset */
81#define I82559_SELFTEST 0x00000001 /* 82559 Selftest command */
82#define I82559_SELECTIVE_RESET 0x00000002
83#define I82559_DUMP 0x00000003
84#define I82559_DUMP_WAKEUP 0x00000007
85
Marek Vasutdc83bfe2020-05-23 12:49:16 +020086/* 82559 Eeprom interface. */
wdenk1df49e22002-09-17 21:37:55 +000087#define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
88#define EE_CS 0x02 /* EEPROM chip select. */
89#define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
90#define EE_WRITE_0 0x01
91#define EE_WRITE_1 0x05
92#define EE_DATA_READ 0x08 /* EEPROM chip data out. */
93#define EE_ENB (0x4800 | EE_CS)
94#define EE_CMD_BITS 3
95#define EE_DATA_BITS 16
96
Marek Vasutdc83bfe2020-05-23 12:49:16 +020097/* The EEPROM commands include the alway-set leading bit. */
wdenk1df49e22002-09-17 21:37:55 +000098#define EE_EWENB_CMD (4 << addr_len)
99#define EE_WRITE_CMD (5 << addr_len)
100#define EE_READ_CMD (6 << addr_len)
101#define EE_ERASE_CMD (7 << addr_len)
102
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200103/* Receive frame descriptors. */
wdenk1df49e22002-09-17 21:37:55 +0000104struct RxFD {
105 volatile u16 status;
106 volatile u16 control;
107 volatile u32 link; /* struct RxFD * */
108 volatile u32 rx_buf_addr; /* void * */
109 volatile u32 count;
110
111 volatile u8 data[PKTSIZE_ALIGN];
112};
113
114#define RFD_STATUS_C 0x8000 /* completion of received frame */
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200115#define RFD_STATUS_OK 0x2000 /* frame received with no errors */
wdenk1df49e22002-09-17 21:37:55 +0000116
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200117#define RFD_CONTROL_EL 0x8000 /* 1=last RFD in RFA */
118#define RFD_CONTROL_S 0x4000 /* 1=suspend RU after receiving frame */
119#define RFD_CONTROL_H 0x0010 /* 1=RFD is a header RFD */
120#define RFD_CONTROL_SF 0x0008 /* 0=simplified, 1=flexible mode */
wdenk1df49e22002-09-17 21:37:55 +0000121
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200122#define RFD_COUNT_MASK 0x3fff
123#define RFD_COUNT_F 0x4000
124#define RFD_COUNT_EOF 0x8000
wdenk1df49e22002-09-17 21:37:55 +0000125
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200126#define RFD_RX_CRC 0x0800 /* crc error */
127#define RFD_RX_ALIGNMENT 0x0400 /* alignment error */
128#define RFD_RX_RESOURCE 0x0200 /* out of space, no resources */
129#define RFD_RX_DMA_OVER 0x0100 /* DMA overrun */
130#define RFD_RX_SHORT 0x0080 /* short frame error */
131#define RFD_RX_LENGTH 0x0020
132#define RFD_RX_ERROR 0x0010 /* receive error */
133#define RFD_RX_NO_ADR_MATCH 0x0004 /* no address match */
134#define RFD_RX_IA_MATCH 0x0002 /* individual address does not match */
135#define RFD_RX_TCO 0x0001 /* TCO indication */
wdenk1df49e22002-09-17 21:37:55 +0000136
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200137/* Transmit frame descriptors */
wdenk1df49e22002-09-17 21:37:55 +0000138struct TxFD { /* Transmit frame descriptor set. */
139 volatile u16 status;
140 volatile u16 command;
141 volatile u32 link; /* void * */
142 volatile u32 tx_desc_addr; /* Always points to the tx_buf_addr element. */
143 volatile s32 count;
144
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200145 volatile u32 tx_buf_addr0; /* void *, frame to be transmitted. */
wdenk1df49e22002-09-17 21:37:55 +0000146 volatile s32 tx_buf_size0; /* Length of Tx frame. */
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200147 volatile u32 tx_buf_addr1; /* void *, frame to be transmitted. */
wdenk1df49e22002-09-17 21:37:55 +0000148 volatile s32 tx_buf_size1; /* Length of Tx frame. */
149};
150
151#define TxCB_CMD_TRANSMIT 0x0004 /* transmit command */
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200152#define TxCB_CMD_SF 0x0008 /* 0=simplified, 1=flexible mode */
153#define TxCB_CMD_NC 0x0010 /* 0=CRC insert by controller */
154#define TxCB_CMD_I 0x2000 /* generate interrupt on completion */
155#define TxCB_CMD_S 0x4000 /* suspend on completion */
156#define TxCB_CMD_EL 0x8000 /* last command block in CBL */
wdenk1df49e22002-09-17 21:37:55 +0000157
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200158#define TxCB_COUNT_MASK 0x3fff
159#define TxCB_COUNT_EOF 0x8000
wdenk1df49e22002-09-17 21:37:55 +0000160
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200161/* The Speedo3 Rx and Tx frame/buffer descriptors. */
wdenk1df49e22002-09-17 21:37:55 +0000162struct descriptor { /* A generic descriptor. */
163 volatile u16 status;
164 volatile u16 command;
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200165 volatile u32 link; /* struct descriptor * */
wdenk1df49e22002-09-17 21:37:55 +0000166
167 unsigned char params[0];
168};
169
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200170#define CONFIG_SYS_CMD_EL 0x8000
171#define CONFIG_SYS_CMD_SUSPEND 0x4000
172#define CONFIG_SYS_CMD_INT 0x2000
173#define CONFIG_SYS_CMD_IAS 0x0001 /* individual address setup */
174#define CONFIG_SYS_CMD_CONFIGURE 0x0002 /* configure */
wdenk1df49e22002-09-17 21:37:55 +0000175
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200176#define CONFIG_SYS_STATUS_C 0x8000
177#define CONFIG_SYS_STATUS_OK 0x2000
wdenk1df49e22002-09-17 21:37:55 +0000178
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200179/* Misc. */
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200180#define NUM_RX_DESC PKTBUFSRX
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200181#define NUM_TX_DESC 1 /* Number of TX descriptors */
wdenk1df49e22002-09-17 21:37:55 +0000182
183#define TOUT_LOOP 1000000
184
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200185static struct RxFD rx_ring[NUM_RX_DESC]; /* RX descriptor ring */
186static struct TxFD tx_ring[NUM_TX_DESC]; /* TX descriptor ring */
wdenk1df49e22002-09-17 21:37:55 +0000187static int rx_next; /* RX descriptor ring pointer */
188static int tx_next; /* TX descriptor ring pointer */
189static int tx_threshold;
190
191/*
192 * The parameters for a CmdConfigure operation.
193 * There are so many options that it would be difficult to document
194 * each bit. We mostly use the default or recommended settings.
195 */
wdenk1df49e22002-09-17 21:37:55 +0000196static const char i82558_config_cmd[] = {
197 22, 0x08, 0, 1, 0, 0, 0x22, 0x03, 1, /* 1=Use MII 0=Use AUI */
198 0, 0x2E, 0, 0x60, 0x08, 0x88,
199 0x68, 0, 0x40, 0xf2, 0x84, /* Disable FC */
200 0x31, 0x05,
201};
202
Marek Vasute4211ed2020-05-23 13:17:03 +0200203static void init_rx_ring(struct eth_device *dev);
204static void purge_tx_ring(struct eth_device *dev);
wdenk1df49e22002-09-17 21:37:55 +0000205
Marek Vasute4211ed2020-05-23 13:17:03 +0200206static void read_hw_addr(struct eth_device *dev, bd_t * bis);
wdenk1df49e22002-09-17 21:37:55 +0000207
Marek Vasute4211ed2020-05-23 13:17:03 +0200208static int eepro100_init(struct eth_device *dev, bd_t * bis);
Joe Hershbergerc5a889a2012-05-21 14:45:25 +0000209static int eepro100_send(struct eth_device *dev, void *packet, int length);
Marek Vasute4211ed2020-05-23 13:17:03 +0200210static int eepro100_recv(struct eth_device *dev);
211static void eepro100_halt(struct eth_device *dev);
wdenk1df49e22002-09-17 21:37:55 +0000212
Wolfgang Denk2ced0702014-10-21 15:23:32 +0200213#if defined(CONFIG_E500)
wdenk9c53f402003-10-15 23:53:47 +0000214#define bus_to_phys(a) (a)
215#define phys_to_bus(a) (a)
216#else
wdenk1df49e22002-09-17 21:37:55 +0000217#define bus_to_phys(a) pci_mem_to_phys((pci_dev_t)dev->priv, a)
218#define phys_to_bus(a) pci_phys_to_mem((pci_dev_t)dev->priv, a)
wdenk9c53f402003-10-15 23:53:47 +0000219#endif
wdenk1df49e22002-09-17 21:37:55 +0000220
Marek Vasute4211ed2020-05-23 13:17:03 +0200221static inline int INW(struct eth_device *dev, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000222{
Bin Meng8e0a8cb2016-01-25 01:26:26 -0800223 return le16_to_cpu(*(volatile u16 *)(addr + (u_long)dev->iobase));
wdenk1df49e22002-09-17 21:37:55 +0000224}
225
Marek Vasute4211ed2020-05-23 13:17:03 +0200226static inline void OUTW(struct eth_device *dev, int command, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000227{
Bin Meng8e0a8cb2016-01-25 01:26:26 -0800228 *(volatile u16 *)((addr + (u_long)dev->iobase)) = cpu_to_le16(command);
wdenk1df49e22002-09-17 21:37:55 +0000229}
230
Marek Vasute4211ed2020-05-23 13:17:03 +0200231static inline void OUTL(struct eth_device *dev, int command, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000232{
Bin Meng8e0a8cb2016-01-25 01:26:26 -0800233 *(volatile u32 *)((addr + (u_long)dev->iobase)) = cpu_to_le32(command);
wdenk1df49e22002-09-17 21:37:55 +0000234}
235
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500236#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marek Vasute4211ed2020-05-23 13:17:03 +0200237static inline int INL(struct eth_device *dev, u_long addr)
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200238{
Bin Meng8e0a8cb2016-01-25 01:26:26 -0800239 return le32_to_cpu(*(volatile u32 *)(addr + (u_long)dev->iobase));
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200240}
241
Marek Vasute4211ed2020-05-23 13:17:03 +0200242static int get_phyreg(struct eth_device *dev, unsigned char addr,
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200243 unsigned char reg, unsigned short *value)
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200244{
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200245 int cmd;
246 int timeout = 50;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200247
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200248 /* read requested data */
249 cmd = (2 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
Marek Vasute4211ed2020-05-23 13:17:03 +0200250 OUTL(dev, cmd, SCBCtrlMDI);
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200251
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200252 do {
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200253 udelay(1000);
Marek Vasute4211ed2020-05-23 13:17:03 +0200254 cmd = INL(dev, SCBCtrlMDI);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200255 } while (!(cmd & (1 << 28)) && (--timeout));
256
257 if (timeout == 0)
258 return -1;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200259
Marek Vasute4211ed2020-05-23 13:17:03 +0200260 *value = (unsigned short)(cmd & 0xffff);
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200261
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200262 return 0;
263}
264
Marek Vasute4211ed2020-05-23 13:17:03 +0200265static int set_phyreg(struct eth_device *dev, unsigned char addr,
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200266 unsigned char reg, unsigned short value)
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200267{
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200268 int cmd;
269 int timeout = 50;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200270
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200271 /* write requested data */
272 cmd = (1 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
Marek Vasute4211ed2020-05-23 13:17:03 +0200273 OUTL(dev, cmd | value, SCBCtrlMDI);
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200274
Marek Vasute4211ed2020-05-23 13:17:03 +0200275 while (!(INL(dev, SCBCtrlMDI) & (1 << 28)) && (--timeout))
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200276 udelay(1000);
277
278 if (timeout == 0)
279 return -1;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200280
281 return 0;
282}
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200283
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200284/*
285 * Check if given phyaddr is valid, i.e. there is a PHY connected.
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200286 * Do this by checking model value field from ID2 register.
287 */
Marek Vasute4211ed2020-05-23 13:17:03 +0200288static struct eth_device* verify_phyaddr(const char *devname,
Ben Warren97824d62010-07-29 12:56:11 -0700289 unsigned char addr)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200290{
291 struct eth_device *dev;
292 unsigned short value;
293 unsigned char model;
294
295 dev = eth_get_dev_by_name(devname);
296 if (dev == NULL) {
297 printf("%s: no such device\n", devname);
298 return NULL;
299 }
300
301 /* read id2 register */
Mike Frysingerd63ee712010-12-23 15:40:12 -0500302 if (get_phyreg(dev, addr, MII_PHYSID2, &value) != 0) {
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200303 printf("%s: mii read timeout!\n", devname);
304 return NULL;
305 }
306
307 /* get model */
308 model = (unsigned char)((value >> 4) & 0x003f);
309
310 if (model == 0) {
311 printf("%s: no PHY at address %d\n", devname, addr);
312 return NULL;
313 }
314
315 return dev;
316}
317
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500318static int eepro100_miiphy_read(struct mii_dev *bus, int addr, int devad,
319 int reg)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200320{
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500321 unsigned short value = 0;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200322 struct eth_device *dev;
323
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500324 dev = verify_phyaddr(bus->name, addr);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200325 if (dev == NULL)
326 return -1;
327
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500328 if (get_phyreg(dev, addr, reg, &value) != 0) {
329 printf("%s: mii read timeout!\n", bus->name);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200330 return -1;
331 }
332
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500333 return value;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200334}
335
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500336static int eepro100_miiphy_write(struct mii_dev *bus, int addr, int devad,
337 int reg, u16 value)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200338{
339 struct eth_device *dev;
340
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500341 dev = verify_phyaddr(bus->name, addr);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200342 if (dev == NULL)
343 return -1;
344
345 if (set_phyreg(dev, addr, reg, value) != 0) {
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500346 printf("%s: mii write timeout!\n", bus->name);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200347 return -1;
348 }
349
350 return 0;
351}
352
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500353#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200354
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200355/* Wait for the chip get the command. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200356static int wait_for_eepro100(struct eth_device *dev)
wdenk1df49e22002-09-17 21:37:55 +0000357{
358 int i;
359
Marek Vasute4211ed2020-05-23 13:17:03 +0200360 for (i = 0; INW(dev, SCBCmd) & (CU_CMD_MASK | RU_CMD_MASK); i++) {
Marek Vasut323b64b2020-05-23 13:20:14 +0200361 if (i >= TOUT_LOOP)
wdenk1df49e22002-09-17 21:37:55 +0000362 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000363 }
364
365 return 1;
366}
367
368static struct pci_device_id supported[] = {
369 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82557},
370 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559},
371 {PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559ER},
372 {}
373};
374
Marek Vasute4211ed2020-05-23 13:17:03 +0200375int eepro100_initialize(bd_t * bis)
wdenk1df49e22002-09-17 21:37:55 +0000376{
377 pci_dev_t devno;
378 int card_number = 0;
379 struct eth_device *dev;
380 u32 iobase, status;
381 int idx = 0;
382
383 while (1) {
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200384 /* Find PCI device */
Marek Vasut323b64b2020-05-23 13:20:14 +0200385 devno = pci_find_devices(supported, idx++);
386 if (devno < 0)
wdenk1df49e22002-09-17 21:37:55 +0000387 break;
wdenk1df49e22002-09-17 21:37:55 +0000388
Marek Vasute4211ed2020-05-23 13:17:03 +0200389 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &iobase);
wdenk1df49e22002-09-17 21:37:55 +0000390 iobase &= ~0xf;
391
Marek Vasut66599202020-05-23 13:11:48 +0200392 debug("eepro100: Intel i82559 PCI EtherExpressPro @0x%x\n",
393 iobase);
wdenk1df49e22002-09-17 21:37:55 +0000394
Marek Vasute4211ed2020-05-23 13:17:03 +0200395 pci_write_config_dword(devno,
wdenk1df49e22002-09-17 21:37:55 +0000396 PCI_COMMAND,
397 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
398
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200399 /* Check if I/O accesses and Bus Mastering are enabled. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200400 pci_read_config_dword(devno, PCI_COMMAND, &status);
wdenk1df49e22002-09-17 21:37:55 +0000401 if (!(status & PCI_COMMAND_MEMORY)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200402 printf("Error: Can not enable MEM access.\n");
wdenk1df49e22002-09-17 21:37:55 +0000403 continue;
404 }
405
406 if (!(status & PCI_COMMAND_MASTER)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200407 printf("Error: Can not enable Bus Mastering.\n");
wdenk1df49e22002-09-17 21:37:55 +0000408 continue;
409 }
410
Marek Vasute4211ed2020-05-23 13:17:03 +0200411 dev = (struct eth_device *)malloc(sizeof *dev);
Nobuhiro Iwamatsuf414b3e2010-10-19 14:03:41 +0900412 if (!dev) {
413 printf("eepro100: Can not allocate memory\n");
414 break;
415 }
416 memset(dev, 0, sizeof(*dev));
wdenk1df49e22002-09-17 21:37:55 +0000417
Marek Vasute4211ed2020-05-23 13:17:03 +0200418 sprintf(dev->name, "i82559#%d", card_number);
419 dev->priv = (void *)devno; /* this have to come before bus_to_phys() */
420 dev->iobase = bus_to_phys(iobase);
wdenk1df49e22002-09-17 21:37:55 +0000421 dev->init = eepro100_init;
422 dev->halt = eepro100_halt;
423 dev->send = eepro100_send;
424 dev->recv = eepro100_recv;
425
Marek Vasute4211ed2020-05-23 13:17:03 +0200426 eth_register(dev);
wdenk1df49e22002-09-17 21:37:55 +0000427
Marek Vasute4211ed2020-05-23 13:17:03 +0200428#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200429 /* register mii command access routines */
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500430 int retval;
431 struct mii_dev *mdiodev = mdio_alloc();
432 if (!mdiodev)
433 return -ENOMEM;
434 strncpy(mdiodev->name, dev->name, MDIO_NAME_LEN);
435 mdiodev->read = eepro100_miiphy_read;
436 mdiodev->write = eepro100_miiphy_write;
437
438 retval = mdio_register(mdiodev);
439 if (retval < 0)
440 return retval;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200441#endif
442
wdenk1df49e22002-09-17 21:37:55 +0000443 card_number++;
444
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200445 /* Set the latency timer for value. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200446 pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x20);
wdenk1df49e22002-09-17 21:37:55 +0000447
Simon Glass0db4b942020-05-10 11:40:10 -0600448 udelay(10 * 1000);
wdenk1df49e22002-09-17 21:37:55 +0000449
Marek Vasute4211ed2020-05-23 13:17:03 +0200450 read_hw_addr(dev, bis);
wdenk1df49e22002-09-17 21:37:55 +0000451 }
452
453 return card_number;
454}
455
456
Marek Vasute4211ed2020-05-23 13:17:03 +0200457static int eepro100_init(struct eth_device *dev, bd_t * bis)
wdenk1df49e22002-09-17 21:37:55 +0000458{
Ben Warrende9fcb52008-01-09 18:15:53 -0500459 int i, status = -1;
wdenk1df49e22002-09-17 21:37:55 +0000460 int tx_cur;
461 struct descriptor *ias_cmd, *cfg_cmd;
462
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200463 /* Reset the ethernet controller */
Marek Vasute4211ed2020-05-23 13:17:03 +0200464 OUTL(dev, I82559_SELECTIVE_RESET, SCBPort);
Simon Glass0db4b942020-05-10 11:40:10 -0600465 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000466
Marek Vasute4211ed2020-05-23 13:17:03 +0200467 OUTL(dev, I82559_RESET, SCBPort);
Simon Glass0db4b942020-05-10 11:40:10 -0600468 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000469
Marek Vasute4211ed2020-05-23 13:17:03 +0200470 if (!wait_for_eepro100(dev)) {
471 printf("Error: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000472 goto Done;
473 }
Marek Vasute4211ed2020-05-23 13:17:03 +0200474 OUTL(dev, 0, SCBPointer);
475 OUTW(dev, SCB_M | RUC_ADDR_LOAD, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000476
Marek Vasute4211ed2020-05-23 13:17:03 +0200477 if (!wait_for_eepro100(dev)) {
478 printf("Error: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000479 goto Done;
480 }
Marek Vasute4211ed2020-05-23 13:17:03 +0200481 OUTL(dev, 0, SCBPointer);
482 OUTW(dev, SCB_M | CU_ADDR_LOAD, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000483
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200484 /* Initialize Rx and Tx rings. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200485 init_rx_ring(dev);
486 purge_tx_ring(dev);
wdenk1df49e22002-09-17 21:37:55 +0000487
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200488 /* Tell the adapter where the RX ring is located. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200489 if (!wait_for_eepro100(dev)) {
490 printf("Error: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000491 goto Done;
492 }
493
Marek Vasute4211ed2020-05-23 13:17:03 +0200494 OUTL(dev, phys_to_bus((u32)&rx_ring[rx_next]), SCBPointer);
495 OUTW(dev, SCB_M | RUC_START, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000496
497 /* Send the Configure frame */
498 tx_cur = tx_next;
499 tx_next = ((tx_next + 1) % NUM_TX_DESC);
500
Marek Vasute4211ed2020-05-23 13:17:03 +0200501 cfg_cmd = (struct descriptor *)&tx_ring[tx_cur];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200502 cfg_cmd->command = cpu_to_le16 ((CONFIG_SYS_CMD_SUSPEND | CONFIG_SYS_CMD_CONFIGURE));
wdenk1df49e22002-09-17 21:37:55 +0000503 cfg_cmd->status = 0;
Marek Vasute4211ed2020-05-23 13:17:03 +0200504 cfg_cmd->link = cpu_to_le32 (phys_to_bus((u32)&tx_ring[tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000505
Marek Vasute4211ed2020-05-23 13:17:03 +0200506 memcpy(cfg_cmd->params, i82558_config_cmd,
507 sizeof(i82558_config_cmd));
wdenk1df49e22002-09-17 21:37:55 +0000508
Marek Vasute4211ed2020-05-23 13:17:03 +0200509 if (!wait_for_eepro100(dev)) {
510 printf("Error---CONFIG_SYS_CMD_CONFIGURE: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000511 goto Done;
512 }
513
Marek Vasute4211ed2020-05-23 13:17:03 +0200514 OUTL(dev, phys_to_bus((u32)&tx_ring[tx_cur]), SCBPointer);
515 OUTW(dev, SCB_M | CU_START, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000516
517 for (i = 0;
Marek Vasute4211ed2020-05-23 13:17:03 +0200518 !(le16_to_cpu(tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C);
wdenk1df49e22002-09-17 21:37:55 +0000519 i++) {
520 if (i >= TOUT_LOOP) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200521 printf("%s: Tx error buffer not ready\n", dev->name);
wdenk1df49e22002-09-17 21:37:55 +0000522 goto Done;
523 }
524 }
525
Marek Vasute4211ed2020-05-23 13:17:03 +0200526 if (!(le16_to_cpu(tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) {
527 printf("TX error status = 0x%08X\n",
528 le16_to_cpu(tx_ring[tx_cur].status));
wdenk1df49e22002-09-17 21:37:55 +0000529 goto Done;
530 }
531
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200532 /* Send the Individual Address Setup frame */
wdenk1df49e22002-09-17 21:37:55 +0000533 tx_cur = tx_next;
534 tx_next = ((tx_next + 1) % NUM_TX_DESC);
535
Marek Vasute4211ed2020-05-23 13:17:03 +0200536 ias_cmd = (struct descriptor *)&tx_ring[tx_cur];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200537 ias_cmd->command = cpu_to_le16 ((CONFIG_SYS_CMD_SUSPEND | CONFIG_SYS_CMD_IAS));
wdenk1df49e22002-09-17 21:37:55 +0000538 ias_cmd->status = 0;
Marek Vasute4211ed2020-05-23 13:17:03 +0200539 ias_cmd->link = cpu_to_le32 (phys_to_bus((u32)&tx_ring[tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000540
Marek Vasute4211ed2020-05-23 13:17:03 +0200541 memcpy(ias_cmd->params, dev->enetaddr, 6);
wdenk1df49e22002-09-17 21:37:55 +0000542
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200543 /* Tell the adapter where the TX ring is located. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200544 if (!wait_for_eepro100(dev)) {
545 printf("Error: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000546 goto Done;
547 }
548
Marek Vasute4211ed2020-05-23 13:17:03 +0200549 OUTL(dev, phys_to_bus((u32)&tx_ring[tx_cur]), SCBPointer);
550 OUTW(dev, SCB_M | CU_START, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000551
Marek Vasute4211ed2020-05-23 13:17:03 +0200552 for (i = 0; !(le16_to_cpu(tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C);
wdenk1df49e22002-09-17 21:37:55 +0000553 i++) {
554 if (i >= TOUT_LOOP) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200555 printf("%s: Tx error buffer not ready\n",
wdenk1df49e22002-09-17 21:37:55 +0000556 dev->name);
557 goto Done;
558 }
559 }
560
Marek Vasute4211ed2020-05-23 13:17:03 +0200561 if (!(le16_to_cpu(tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) {
562 printf("TX error status = 0x%08X\n",
563 le16_to_cpu(tx_ring[tx_cur].status));
wdenk1df49e22002-09-17 21:37:55 +0000564 goto Done;
565 }
566
Ben Warrende9fcb52008-01-09 18:15:53 -0500567 status = 0;
wdenk1df49e22002-09-17 21:37:55 +0000568
569 Done:
570 return status;
571}
572
Joe Hershbergerc5a889a2012-05-21 14:45:25 +0000573static int eepro100_send(struct eth_device *dev, void *packet, int length)
wdenk1df49e22002-09-17 21:37:55 +0000574{
575 int i, status = -1;
576 int tx_cur;
577
578 if (length <= 0) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200579 printf("%s: bad packet size: %d\n", dev->name, length);
wdenk1df49e22002-09-17 21:37:55 +0000580 goto Done;
581 }
582
583 tx_cur = tx_next;
584 tx_next = (tx_next + 1) % NUM_TX_DESC;
585
Marek Vasute4211ed2020-05-23 13:17:03 +0200586 tx_ring[tx_cur].command = cpu_to_le16 (TxCB_CMD_TRANSMIT |
wdenk1df49e22002-09-17 21:37:55 +0000587 TxCB_CMD_SF |
588 TxCB_CMD_S |
Marek Vasute4211ed2020-05-23 13:17:03 +0200589 TxCB_CMD_EL);
wdenk1df49e22002-09-17 21:37:55 +0000590 tx_ring[tx_cur].status = 0;
591 tx_ring[tx_cur].count = cpu_to_le32 (tx_threshold);
592 tx_ring[tx_cur].link =
Marek Vasute4211ed2020-05-23 13:17:03 +0200593 cpu_to_le32 (phys_to_bus((u32)&tx_ring[tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000594 tx_ring[tx_cur].tx_desc_addr =
Marek Vasute4211ed2020-05-23 13:17:03 +0200595 cpu_to_le32 (phys_to_bus((u32)&tx_ring[tx_cur].tx_buf_addr0));
wdenk1df49e22002-09-17 21:37:55 +0000596 tx_ring[tx_cur].tx_buf_addr0 =
Marek Vasute4211ed2020-05-23 13:17:03 +0200597 cpu_to_le32 (phys_to_bus((u_long)packet));
wdenk1df49e22002-09-17 21:37:55 +0000598 tx_ring[tx_cur].tx_buf_size0 = cpu_to_le32 (length);
599
Marek Vasute4211ed2020-05-23 13:17:03 +0200600 if (!wait_for_eepro100(dev)) {
601 printf("%s: Tx error ethernet controller not ready.\n",
wdenk1df49e22002-09-17 21:37:55 +0000602 dev->name);
603 goto Done;
604 }
605
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200606 /* Send the packet. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200607 OUTL(dev, phys_to_bus((u32)&tx_ring[tx_cur]), SCBPointer);
608 OUTW(dev, SCB_M | CU_START, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000609
Marek Vasute4211ed2020-05-23 13:17:03 +0200610 for (i = 0; !(le16_to_cpu(tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_C);
wdenk1df49e22002-09-17 21:37:55 +0000611 i++) {
612 if (i >= TOUT_LOOP) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200613 printf("%s: Tx error buffer not ready\n", dev->name);
wdenk1df49e22002-09-17 21:37:55 +0000614 goto Done;
615 }
616 }
617
Marek Vasute4211ed2020-05-23 13:17:03 +0200618 if (!(le16_to_cpu(tx_ring[tx_cur].status) & CONFIG_SYS_STATUS_OK)) {
619 printf("TX error status = 0x%08X\n",
620 le16_to_cpu(tx_ring[tx_cur].status));
wdenk1df49e22002-09-17 21:37:55 +0000621 goto Done;
622 }
623
624 status = length;
625
626 Done:
627 return status;
628}
629
Marek Vasute4211ed2020-05-23 13:17:03 +0200630static int eepro100_recv(struct eth_device *dev)
wdenk1df49e22002-09-17 21:37:55 +0000631{
632 u16 status, stat;
633 int rx_prev, length = 0;
634
Marek Vasute4211ed2020-05-23 13:17:03 +0200635 stat = INW(dev, SCBStatus);
636 OUTW(dev, stat & SCB_STATUS_RNR, SCBStatus);
wdenk1df49e22002-09-17 21:37:55 +0000637
638 for (;;) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200639 status = le16_to_cpu(rx_ring[rx_next].status);
wdenk1df49e22002-09-17 21:37:55 +0000640
Marek Vasut323b64b2020-05-23 13:20:14 +0200641 if (!(status & RFD_STATUS_C))
wdenk1df49e22002-09-17 21:37:55 +0000642 break;
wdenk1df49e22002-09-17 21:37:55 +0000643
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200644 /* Valid frame status. */
wdenk1df49e22002-09-17 21:37:55 +0000645 if ((status & RFD_STATUS_OK)) {
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200646 /* A valid frame received. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200647 length = le32_to_cpu(rx_ring[rx_next].count) & 0x3fff;
wdenk1df49e22002-09-17 21:37:55 +0000648
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200649 /* Pass the packet up to the protocol layers. */
Joe Hershberger9f09a362015-04-08 01:41:06 -0500650 net_process_received_packet((u8 *)rx_ring[rx_next].data,
651 length);
wdenk1df49e22002-09-17 21:37:55 +0000652 } else {
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200653 /* There was an error. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200654 printf("RX error status = 0x%08X\n", status);
wdenk1df49e22002-09-17 21:37:55 +0000655 }
656
657 rx_ring[rx_next].control = cpu_to_le16 (RFD_CONTROL_S);
658 rx_ring[rx_next].status = 0;
659 rx_ring[rx_next].count = cpu_to_le32 (PKTSIZE_ALIGN << 16);
660
661 rx_prev = (rx_next + NUM_RX_DESC - 1) % NUM_RX_DESC;
662 rx_ring[rx_prev].control = 0;
663
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200664 /* Update entry information. */
wdenk1df49e22002-09-17 21:37:55 +0000665 rx_next = (rx_next + 1) % NUM_RX_DESC;
666 }
667
668 if (stat & SCB_STATUS_RNR) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200669 printf("%s: Receiver is not ready, restart it !\n", dev->name);
wdenk1df49e22002-09-17 21:37:55 +0000670
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200671 /* Reinitialize Rx ring. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200672 init_rx_ring(dev);
wdenk1df49e22002-09-17 21:37:55 +0000673
Marek Vasute4211ed2020-05-23 13:17:03 +0200674 if (!wait_for_eepro100(dev)) {
675 printf("Error: Can not restart ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000676 goto Done;
677 }
678
Marek Vasute4211ed2020-05-23 13:17:03 +0200679 OUTL(dev, phys_to_bus((u32)&rx_ring[rx_next]), SCBPointer);
680 OUTW(dev, SCB_M | RUC_START, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000681 }
682
683 Done:
684 return length;
685}
686
Marek Vasute4211ed2020-05-23 13:17:03 +0200687static void eepro100_halt(struct eth_device *dev)
wdenk1df49e22002-09-17 21:37:55 +0000688{
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200689 /* Reset the ethernet controller */
Marek Vasute4211ed2020-05-23 13:17:03 +0200690 OUTL(dev, I82559_SELECTIVE_RESET, SCBPort);
Simon Glass0db4b942020-05-10 11:40:10 -0600691 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000692
Marek Vasute4211ed2020-05-23 13:17:03 +0200693 OUTL(dev, I82559_RESET, SCBPort);
Simon Glass0db4b942020-05-10 11:40:10 -0600694 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000695
Marek Vasute4211ed2020-05-23 13:17:03 +0200696 if (!wait_for_eepro100(dev)) {
697 printf("Error: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000698 goto Done;
699 }
Marek Vasute4211ed2020-05-23 13:17:03 +0200700 OUTL(dev, 0, SCBPointer);
701 OUTW(dev, SCB_M | RUC_ADDR_LOAD, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000702
Marek Vasute4211ed2020-05-23 13:17:03 +0200703 if (!wait_for_eepro100(dev)) {
704 printf("Error: Can not reset ethernet controller.\n");
wdenk1df49e22002-09-17 21:37:55 +0000705 goto Done;
706 }
Marek Vasute4211ed2020-05-23 13:17:03 +0200707 OUTL(dev, 0, SCBPointer);
708 OUTW(dev, SCB_M | CU_ADDR_LOAD, SCBCmd);
wdenk1df49e22002-09-17 21:37:55 +0000709
710 Done:
711 return;
712}
713
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200714/* SROM Read. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200715static int read_eeprom(struct eth_device *dev, int location, int addr_len)
wdenk1df49e22002-09-17 21:37:55 +0000716{
717 unsigned short retval = 0;
718 int read_cmd = location | EE_READ_CMD;
719 int i;
720
Marek Vasute4211ed2020-05-23 13:17:03 +0200721 OUTW(dev, EE_ENB & ~EE_CS, SCBeeprom);
722 OUTW(dev, EE_ENB, SCBeeprom);
wdenk1df49e22002-09-17 21:37:55 +0000723
724 /* Shift the read command bits out. */
725 for (i = 12; i >= 0; i--) {
726 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
727
Marek Vasute4211ed2020-05-23 13:17:03 +0200728 OUTW(dev, EE_ENB | dataval, SCBeeprom);
Simon Glass0db4b942020-05-10 11:40:10 -0600729 udelay(1);
Marek Vasute4211ed2020-05-23 13:17:03 +0200730 OUTW(dev, EE_ENB | dataval | EE_SHIFT_CLK, SCBeeprom);
Simon Glass0db4b942020-05-10 11:40:10 -0600731 udelay(1);
wdenk1df49e22002-09-17 21:37:55 +0000732 }
Marek Vasute4211ed2020-05-23 13:17:03 +0200733 OUTW(dev, EE_ENB, SCBeeprom);
wdenk1df49e22002-09-17 21:37:55 +0000734
735 for (i = 15; i >= 0; i--) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200736 OUTW(dev, EE_ENB | EE_SHIFT_CLK, SCBeeprom);
Simon Glass0db4b942020-05-10 11:40:10 -0600737 udelay(1);
wdenk1df49e22002-09-17 21:37:55 +0000738 retval = (retval << 1) |
Marek Vasute4211ed2020-05-23 13:17:03 +0200739 ((INW(dev, SCBeeprom) & EE_DATA_READ) ? 1 : 0);
740 OUTW(dev, EE_ENB, SCBeeprom);
Simon Glass0db4b942020-05-10 11:40:10 -0600741 udelay(1);
wdenk1df49e22002-09-17 21:37:55 +0000742 }
743
744 /* Terminate the EEPROM access. */
Marek Vasute4211ed2020-05-23 13:17:03 +0200745 OUTW(dev, EE_ENB & ~EE_CS, SCBeeprom);
wdenk1df49e22002-09-17 21:37:55 +0000746 return retval;
747}
748
Marek Vasute4211ed2020-05-23 13:17:03 +0200749static void init_rx_ring(struct eth_device *dev)
wdenk1df49e22002-09-17 21:37:55 +0000750{
751 int i;
752
753 for (i = 0; i < NUM_RX_DESC; i++) {
754 rx_ring[i].status = 0;
755 rx_ring[i].control =
756 (i == NUM_RX_DESC - 1) ? cpu_to_le16 (RFD_CONTROL_S) : 0;
757 rx_ring[i].link =
758 cpu_to_le32 (phys_to_bus
Marek Vasute4211ed2020-05-23 13:17:03 +0200759 ((u32)&rx_ring[(i + 1) % NUM_RX_DESC]));
wdenk1df49e22002-09-17 21:37:55 +0000760 rx_ring[i].rx_buf_addr = 0xffffffff;
761 rx_ring[i].count = cpu_to_le32 (PKTSIZE_ALIGN << 16);
762 }
763
764 rx_next = 0;
765}
766
Marek Vasute4211ed2020-05-23 13:17:03 +0200767static void purge_tx_ring(struct eth_device *dev)
wdenk1df49e22002-09-17 21:37:55 +0000768{
769 int i;
770
771 tx_next = 0;
772 tx_threshold = 0x01208000;
773
774 for (i = 0; i < NUM_TX_DESC; i++) {
775 tx_ring[i].status = 0;
776 tx_ring[i].command = 0;
777 tx_ring[i].link = 0;
778 tx_ring[i].tx_desc_addr = 0;
779 tx_ring[i].count = 0;
780
781 tx_ring[i].tx_buf_addr0 = 0;
782 tx_ring[i].tx_buf_size0 = 0;
783 tx_ring[i].tx_buf_addr1 = 0;
784 tx_ring[i].tx_buf_size1 = 0;
785 }
786}
787
Marek Vasute4211ed2020-05-23 13:17:03 +0200788static void read_hw_addr(struct eth_device *dev, bd_t * bis)
wdenk1df49e22002-09-17 21:37:55 +0000789{
wdenk1df49e22002-09-17 21:37:55 +0000790 u16 sum = 0;
791 int i, j;
Marek Vasute4211ed2020-05-23 13:17:03 +0200792 int addr_len = read_eeprom(dev, 0, 6) == 0xffff ? 8 : 6;
wdenk1df49e22002-09-17 21:37:55 +0000793
794 for (j = 0, i = 0; i < 0x40; i++) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200795 u16 value = read_eeprom(dev, i, addr_len);
wdenk1df49e22002-09-17 21:37:55 +0000796
wdenk1df49e22002-09-17 21:37:55 +0000797 sum += value;
798 if (i < 3) {
799 dev->enetaddr[j++] = value;
800 dev->enetaddr[j++] = value >> 8;
801 }
802 }
803
804 if (sum != 0xBABA) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200805 memset(dev->enetaddr, 0, ETH_ALEN);
Marek Vasut66599202020-05-23 13:11:48 +0200806 debug("%s: Invalid EEPROM checksum %#4.4x, check settings before activating this device!\n",
807 dev->name, sum);
wdenk1df49e22002-09-17 21:37:55 +0000808 }
809}