blob: f47483255246335d2c29031ba06c1c00c0128caa [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>
Marek Vasut2110c652020-05-23 15:07:30 +02008#include <asm/io.h>
Marek Vasut7efcae42020-05-23 14:55:26 +02009#include <cpu_func.h>
wdenk1df49e22002-09-17 21:37:55 +000010#include <malloc.h>
Marek Vasut2110c652020-05-23 15:07:30 +020011#include <miiphy.h>
wdenk1df49e22002-09-17 21:37:55 +000012#include <net.h>
Ben Warren052a5ea2008-08-31 20:37:00 -070013#include <netdev.h>
wdenk1df49e22002-09-17 21:37:55 +000014#include <pci.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
wdenk1df49e22002-09-17 21:37:55 +000016
Marek Vasutdc83bfe2020-05-23 12:49:16 +020017/* Ethernet chip registers. */
Marek Vasut447271b2020-05-23 13:52:50 +020018#define SCB_STATUS 0 /* Rx/Command Unit Status *Word* */
19#define SCB_INT_ACK_BYTE 1 /* Rx/Command Unit STAT/ACK byte */
20#define SCB_CMD 2 /* Rx/Command Unit Command *Word* */
21#define SCB_INTR_CTL_BYTE 3 /* Rx/Command Unit Intr.Control Byte */
22#define SCB_POINTER 4 /* General purpose pointer. */
23#define SCB_PORT 8 /* Misc. commands and operands. */
24#define SCB_FLASH 12 /* Flash memory control. */
25#define SCB_EEPROM 14 /* EEPROM memory control. */
26#define SCB_CTRL_MDI 16 /* MDI interface control. */
27#define SCB_EARLY_RX 20 /* Early receive byte count. */
28#define SCB_GEN_CONTROL 28 /* 82559 General Control Register */
29#define SCB_GEN_STATUS 29 /* 82559 General Status register */
wdenk1df49e22002-09-17 21:37:55 +000030
Marek Vasutdc83bfe2020-05-23 12:49:16 +020031/* 82559 SCB status word defnitions */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020032#define SCB_STATUS_CX 0x8000 /* CU finished command (transmit) */
33#define SCB_STATUS_FR 0x4000 /* frame received */
34#define SCB_STATUS_CNA 0x2000 /* CU left active state */
35#define SCB_STATUS_RNR 0x1000 /* receiver left ready state */
36#define SCB_STATUS_MDI 0x0800 /* MDI read/write cycle done */
37#define SCB_STATUS_SWI 0x0400 /* software generated interrupt */
38#define SCB_STATUS_FCP 0x0100 /* flow control pause interrupt */
wdenk1df49e22002-09-17 21:37:55 +000039
Wolfgang Denk4dc11462005-09-26 01:06:33 +020040#define SCB_INTACK_MASK 0xFD00 /* all the above */
wdenk1df49e22002-09-17 21:37:55 +000041
Wolfgang Denk4dc11462005-09-26 01:06:33 +020042#define SCB_INTACK_TX (SCB_STATUS_CX | SCB_STATUS_CNA)
43#define SCB_INTACK_RX (SCB_STATUS_FR | SCB_STATUS_RNR)
wdenk1df49e22002-09-17 21:37:55 +000044
Marek Vasutdc83bfe2020-05-23 12:49:16 +020045/* System control block commands */
wdenk1df49e22002-09-17 21:37:55 +000046/* CU Commands */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020047#define CU_NOP 0x0000
48#define CU_START 0x0010
49#define CU_RESUME 0x0020
50#define CU_STATSADDR 0x0040 /* Load Dump Statistics ctrs addr */
51#define CU_SHOWSTATS 0x0050 /* Dump statistics counters. */
52#define CU_ADDR_LOAD 0x0060 /* Base address to add to CU commands */
53#define CU_DUMPSTATS 0x0070 /* Dump then reset stats counters. */
wdenk1df49e22002-09-17 21:37:55 +000054
55/* RUC Commands */
Wolfgang Denk4dc11462005-09-26 01:06:33 +020056#define RUC_NOP 0x0000
57#define RUC_START 0x0001
58#define RUC_RESUME 0x0002
59#define RUC_ABORT 0x0004
60#define RUC_ADDR_LOAD 0x0006 /* (seems not to clear on acceptance) */
61#define RUC_RESUMENR 0x0007
wdenk1df49e22002-09-17 21:37:55 +000062
Wolfgang Denk4dc11462005-09-26 01:06:33 +020063#define CU_CMD_MASK 0x00f0
64#define RU_CMD_MASK 0x0007
wdenk1df49e22002-09-17 21:37:55 +000065
Wolfgang Denk4dc11462005-09-26 01:06:33 +020066#define SCB_M 0x0100 /* 0 = enable interrupt, 1 = disable */
67#define SCB_SWI 0x0200 /* 1 - cause device to interrupt */
wdenk1df49e22002-09-17 21:37:55 +000068
Wolfgang Denk4dc11462005-09-26 01:06:33 +020069#define CU_STATUS_MASK 0x00C0
70#define RU_STATUS_MASK 0x003C
wdenk1df49e22002-09-17 21:37:55 +000071
Marek Vasute4211ed2020-05-23 13:17:03 +020072#define RU_STATUS_IDLE (0 << 2)
73#define RU_STATUS_SUS (1 << 2)
74#define RU_STATUS_NORES (2 << 2)
75#define RU_STATUS_READY (4 << 2)
76#define RU_STATUS_NO_RBDS_SUS ((1 << 2) | (8 << 2))
77#define RU_STATUS_NO_RBDS_NORES ((2 << 2) | (8 << 2))
78#define RU_STATUS_NO_RBDS_READY ((4 << 2) | (8 << 2))
wdenk1df49e22002-09-17 21:37:55 +000079
Marek Vasutdc83bfe2020-05-23 12:49:16 +020080/* 82559 Port interface commands. */
wdenk1df49e22002-09-17 21:37:55 +000081#define I82559_RESET 0x00000000 /* Software reset */
82#define I82559_SELFTEST 0x00000001 /* 82559 Selftest command */
83#define I82559_SELECTIVE_RESET 0x00000002
84#define I82559_DUMP 0x00000003
85#define I82559_DUMP_WAKEUP 0x00000007
86
Marek Vasutdc83bfe2020-05-23 12:49:16 +020087/* 82559 Eeprom interface. */
wdenk1df49e22002-09-17 21:37:55 +000088#define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
89#define EE_CS 0x02 /* EEPROM chip select. */
90#define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
91#define EE_WRITE_0 0x01
92#define EE_WRITE_1 0x05
93#define EE_DATA_READ 0x08 /* EEPROM chip data out. */
94#define EE_ENB (0x4800 | EE_CS)
95#define EE_CMD_BITS 3
96#define EE_DATA_BITS 16
97
Marek Vasutdc83bfe2020-05-23 12:49:16 +020098/* The EEPROM commands include the alway-set leading bit. */
Marek Vasutf9cc66a2020-05-23 16:23:28 +020099#define EE_EWENB_CMD(addr_len) (4 << (addr_len))
100#define EE_WRITE_CMD(addr_len) (5 << (addr_len))
101#define EE_READ_CMD(addr_len) (6 << (addr_len))
102#define EE_ERASE_CMD(addr_len) (7 << (addr_len))
wdenk1df49e22002-09-17 21:37:55 +0000103
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200104/* Receive frame descriptors. */
Marek Vasut447271b2020-05-23 13:52:50 +0200105struct eepro100_rxfd {
Marek Vasut7ad665f2020-05-23 15:02:47 +0200106 u16 status;
107 u16 control;
108 u32 link; /* struct eepro100_rxfd * */
109 u32 rx_buf_addr; /* void * */
110 u32 count;
wdenk1df49e22002-09-17 21:37:55 +0000111
Marek Vasut7ad665f2020-05-23 15:02:47 +0200112 u8 data[PKTSIZE_ALIGN];
wdenk1df49e22002-09-17 21:37:55 +0000113};
114
115#define RFD_STATUS_C 0x8000 /* completion of received frame */
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200116#define RFD_STATUS_OK 0x2000 /* frame received with no errors */
wdenk1df49e22002-09-17 21:37:55 +0000117
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200118#define RFD_CONTROL_EL 0x8000 /* 1=last RFD in RFA */
119#define RFD_CONTROL_S 0x4000 /* 1=suspend RU after receiving frame */
120#define RFD_CONTROL_H 0x0010 /* 1=RFD is a header RFD */
121#define RFD_CONTROL_SF 0x0008 /* 0=simplified, 1=flexible mode */
wdenk1df49e22002-09-17 21:37:55 +0000122
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200123#define RFD_COUNT_MASK 0x3fff
124#define RFD_COUNT_F 0x4000
125#define RFD_COUNT_EOF 0x8000
wdenk1df49e22002-09-17 21:37:55 +0000126
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200127#define RFD_RX_CRC 0x0800 /* crc error */
128#define RFD_RX_ALIGNMENT 0x0400 /* alignment error */
129#define RFD_RX_RESOURCE 0x0200 /* out of space, no resources */
130#define RFD_RX_DMA_OVER 0x0100 /* DMA overrun */
131#define RFD_RX_SHORT 0x0080 /* short frame error */
132#define RFD_RX_LENGTH 0x0020
133#define RFD_RX_ERROR 0x0010 /* receive error */
134#define RFD_RX_NO_ADR_MATCH 0x0004 /* no address match */
135#define RFD_RX_IA_MATCH 0x0002 /* individual address does not match */
136#define RFD_RX_TCO 0x0001 /* TCO indication */
wdenk1df49e22002-09-17 21:37:55 +0000137
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200138/* Transmit frame descriptors */
Marek Vasut7ad665f2020-05-23 15:02:47 +0200139struct eepro100_txfd { /* Transmit frame descriptor set. */
140 u16 status;
141 u16 command;
142 u32 link; /* void * */
143 u32 tx_desc_addr; /* Always points to the tx_buf_addr element. */
144 s32 count;
wdenk1df49e22002-09-17 21:37:55 +0000145
Marek Vasut7ad665f2020-05-23 15:02:47 +0200146 u32 tx_buf_addr0; /* void *, frame to be transmitted. */
147 s32 tx_buf_size0; /* Length of Tx frame. */
148 u32 tx_buf_addr1; /* void *, frame to be transmitted. */
149 s32 tx_buf_size1; /* Length of Tx frame. */
wdenk1df49e22002-09-17 21:37:55 +0000150};
151
Marek Vasut447271b2020-05-23 13:52:50 +0200152#define TXCB_CMD_TRANSMIT 0x0004 /* transmit command */
153#define TXCB_CMD_SF 0x0008 /* 0=simplified, 1=flexible mode */
154#define TXCB_CMD_NC 0x0010 /* 0=CRC insert by controller */
155#define TXCB_CMD_I 0x2000 /* generate interrupt on completion */
156#define TXCB_CMD_S 0x4000 /* suspend on completion */
157#define TXCB_CMD_EL 0x8000 /* last command block in CBL */
wdenk1df49e22002-09-17 21:37:55 +0000158
Marek Vasut447271b2020-05-23 13:52:50 +0200159#define TXCB_COUNT_MASK 0x3fff
160#define TXCB_COUNT_EOF 0x8000
wdenk1df49e22002-09-17 21:37:55 +0000161
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200162/* The Speedo3 Rx and Tx frame/buffer descriptors. */
Marek Vasut7ad665f2020-05-23 15:02:47 +0200163struct descriptor { /* A generic descriptor. */
164 u16 status;
165 u16 command;
166 u32 link; /* struct descriptor * */
wdenk1df49e22002-09-17 21:37:55 +0000167
168 unsigned char params[0];
169};
170
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200171#define CONFIG_SYS_CMD_EL 0x8000
172#define CONFIG_SYS_CMD_SUSPEND 0x4000
173#define CONFIG_SYS_CMD_INT 0x2000
174#define CONFIG_SYS_CMD_IAS 0x0001 /* individual address setup */
175#define CONFIG_SYS_CMD_CONFIGURE 0x0002 /* configure */
wdenk1df49e22002-09-17 21:37:55 +0000176
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200177#define CONFIG_SYS_STATUS_C 0x8000
178#define CONFIG_SYS_STATUS_OK 0x2000
wdenk1df49e22002-09-17 21:37:55 +0000179
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200180/* Misc. */
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200181#define NUM_RX_DESC PKTBUFSRX
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200182#define NUM_TX_DESC 1 /* Number of TX descriptors */
wdenk1df49e22002-09-17 21:37:55 +0000183
184#define TOUT_LOOP 1000000
185
wdenk1df49e22002-09-17 21:37:55 +0000186/*
187 * The parameters for a CmdConfigure operation.
188 * There are so many options that it would be difficult to document
189 * each bit. We mostly use the default or recommended settings.
190 */
wdenk1df49e22002-09-17 21:37:55 +0000191static const char i82558_config_cmd[] = {
192 22, 0x08, 0, 1, 0, 0, 0x22, 0x03, 1, /* 1=Use MII 0=Use AUI */
193 0, 0x2E, 0, 0x60, 0x08, 0x88,
194 0x68, 0, 0x40, 0xf2, 0x84, /* Disable FC */
195 0x31, 0x05,
196};
197
Marek Vasut13beaa82020-05-23 16:49:07 +0200198struct eepro100_priv {
Marek Vasutd443d2d2020-05-23 17:13:26 +0200199 /* RX descriptor ring */
200 struct eepro100_rxfd rx_ring[NUM_RX_DESC];
201 /* TX descriptor ring */
202 struct eepro100_txfd tx_ring[NUM_TX_DESC];
203 /* RX descriptor ring pointer */
204 int rx_next;
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200205 u16 rx_stat;
Marek Vasutd443d2d2020-05-23 17:13:26 +0200206 /* TX descriptor ring pointer */
207 int tx_next;
208 int tx_threshold;
Marek Vasut13beaa82020-05-23 16:49:07 +0200209 struct eth_device dev;
Marek Vasut33346692020-05-23 17:10:03 +0200210 pci_dev_t devno;
211 char *name;
212 void __iomem *iobase;
213 u8 *enetaddr;
Marek Vasut13beaa82020-05-23 16:49:07 +0200214};
215
Wolfgang Denk2ced0702014-10-21 15:23:32 +0200216#if defined(CONFIG_E500)
Marek Vasutc62e0242020-05-23 16:38:41 +0200217#define bus_to_phys(dev, a) (a)
218#define phys_to_bus(dev, a) (a)
wdenk9c53f402003-10-15 23:53:47 +0000219#else
Marek Vasutc62e0242020-05-23 16:38:41 +0200220#define bus_to_phys(dev, a) pci_mem_to_phys((dev), (a))
221#define phys_to_bus(dev, a) pci_phys_to_mem((dev), (a))
wdenk9c53f402003-10-15 23:53:47 +0000222#endif
wdenk1df49e22002-09-17 21:37:55 +0000223
Marek Vasut33346692020-05-23 17:10:03 +0200224static int INW(struct eepro100_priv *priv, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000225{
Marek Vasut33346692020-05-23 17:10:03 +0200226 return le16_to_cpu(readw(addr + priv->iobase));
wdenk1df49e22002-09-17 21:37:55 +0000227}
228
Marek Vasut33346692020-05-23 17:10:03 +0200229static void OUTW(struct eepro100_priv *priv, int command, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000230{
Marek Vasut33346692020-05-23 17:10:03 +0200231 writew(cpu_to_le16(command), addr + priv->iobase);
wdenk1df49e22002-09-17 21:37:55 +0000232}
233
Marek Vasut33346692020-05-23 17:10:03 +0200234static void OUTL(struct eepro100_priv *priv, int command, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000235{
Marek Vasut33346692020-05-23 17:10:03 +0200236 writel(cpu_to_le32(command), addr + priv->iobase);
wdenk1df49e22002-09-17 21:37:55 +0000237}
238
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500239#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marek Vasut33346692020-05-23 17:10:03 +0200240static int INL(struct eepro100_priv *priv, u_long addr)
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200241{
Marek Vasut33346692020-05-23 17:10:03 +0200242 return le32_to_cpu(readl(addr + priv->iobase));
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200243}
244
Marek Vasut33346692020-05-23 17:10:03 +0200245static int get_phyreg(struct eepro100_priv *priv, unsigned char addr,
Marek Vasut60560d02020-05-23 13:21:43 +0200246 unsigned char reg, unsigned short *value)
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200247{
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200248 int timeout = 50;
Marek Vasut33346692020-05-23 17:10:03 +0200249 int cmd;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200250
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200251 /* read requested data */
252 cmd = (2 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
Marek Vasut33346692020-05-23 17:10:03 +0200253 OUTL(priv, cmd, SCB_CTRL_MDI);
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200254
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200255 do {
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200256 udelay(1000);
Marek Vasut33346692020-05-23 17:10:03 +0200257 cmd = INL(priv, SCB_CTRL_MDI);
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200258 } while (!(cmd & (1 << 28)) && (--timeout));
259
260 if (timeout == 0)
261 return -1;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200262
Marek Vasute4211ed2020-05-23 13:17:03 +0200263 *value = (unsigned short)(cmd & 0xffff);
Wolfgang Denk4dc11462005-09-26 01:06:33 +0200264
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200265 return 0;
266}
267
Marek Vasut33346692020-05-23 17:10:03 +0200268static int set_phyreg(struct eepro100_priv *priv, unsigned char addr,
Marek Vasut60560d02020-05-23 13:21:43 +0200269 unsigned char reg, unsigned short value)
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200270{
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200271 int timeout = 50;
Marek Vasut33346692020-05-23 17:10:03 +0200272 int cmd;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200273
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200274 /* write requested data */
275 cmd = (1 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
Marek Vasut33346692020-05-23 17:10:03 +0200276 OUTL(priv, cmd | value, SCB_CTRL_MDI);
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200277
Marek Vasut33346692020-05-23 17:10:03 +0200278 while (!(INL(priv, SCB_CTRL_MDI) & (1 << 28)) && (--timeout))
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200279 udelay(1000);
280
281 if (timeout == 0)
282 return -1;
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200283
284 return 0;
285}
Wolfgang Denkb8d1f512005-09-26 00:39:59 +0200286
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200287/*
288 * Check if given phyaddr is valid, i.e. there is a PHY connected.
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200289 * Do this by checking model value field from ID2 register.
290 */
Marek Vasut33346692020-05-23 17:10:03 +0200291static int verify_phyaddr(struct eepro100_priv *priv, unsigned char addr)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200292{
Marek Vasut33346692020-05-23 17:10:03 +0200293 unsigned short value, model;
294 int ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200295
296 /* read id2 register */
Marek Vasut33346692020-05-23 17:10:03 +0200297 ret = get_phyreg(priv, addr, MII_PHYSID2, &value);
298 if (ret) {
299 printf("%s: mii read timeout!\n", priv->name);
300 return ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200301 }
302
303 /* get model */
Marek Vasut33346692020-05-23 17:10:03 +0200304 model = (value >> 4) & 0x003f;
305 if (!model) {
306 printf("%s: no PHY at address %d\n", priv->name, addr);
307 return -EINVAL;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200308 }
309
Marek Vasut33346692020-05-23 17:10:03 +0200310 return 0;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200311}
312
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500313static int eepro100_miiphy_read(struct mii_dev *bus, int addr, int devad,
314 int reg)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200315{
Marek Vasut4448e602020-05-23 17:55:50 +0200316 struct eepro100_priv *priv = bus->priv;
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500317 unsigned short value = 0;
Marek Vasut33346692020-05-23 17:10:03 +0200318 int ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200319
Marek Vasut33346692020-05-23 17:10:03 +0200320 ret = verify_phyaddr(priv, addr);
321 if (ret)
322 return ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200323
Marek Vasut33346692020-05-23 17:10:03 +0200324 ret = get_phyreg(priv, addr, reg, &value);
325 if (ret) {
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500326 printf("%s: mii read timeout!\n", bus->name);
Marek Vasut33346692020-05-23 17:10:03 +0200327 return ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200328 }
329
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500330 return value;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200331}
332
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500333static int eepro100_miiphy_write(struct mii_dev *bus, int addr, int devad,
334 int reg, u16 value)
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200335{
Marek Vasut4448e602020-05-23 17:55:50 +0200336 struct eepro100_priv *priv = bus->priv;
Marek Vasut33346692020-05-23 17:10:03 +0200337 int ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200338
Marek Vasut33346692020-05-23 17:10:03 +0200339 ret = verify_phyaddr(priv, addr);
340 if (ret)
341 return ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200342
Marek Vasut33346692020-05-23 17:10:03 +0200343 ret = set_phyreg(priv, addr, reg, value);
344 if (ret) {
Joe Hershberger1fbcbed2016-08-08 11:28:38 -0500345 printf("%s: mii write timeout!\n", bus->name);
Marek Vasut33346692020-05-23 17:10:03 +0200346 return ret;
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200347 }
348
349 return 0;
350}
Jon Loeligerb1d408a2007-07-09 17:30:01 -0500351#endif
Marian Balakowiczaab8c492005-10-28 22:30:33 +0200352
Marek Vasut33346692020-05-23 17:10:03 +0200353static void init_rx_ring(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000354{
Marek Vasutd443d2d2020-05-23 17:13:26 +0200355 struct eepro100_rxfd *rx_ring = priv->rx_ring;
wdenk1df49e22002-09-17 21:37:55 +0000356 int i;
357
Marek Vasut2110c652020-05-23 15:07:30 +0200358 for (i = 0; i < NUM_RX_DESC; i++) {
359 rx_ring[i].status = 0;
360 rx_ring[i].control = (i == NUM_RX_DESC - 1) ?
361 cpu_to_le16 (RFD_CONTROL_S) : 0;
362 rx_ring[i].link =
Marek Vasut33346692020-05-23 17:10:03 +0200363 cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutc62e0242020-05-23 16:38:41 +0200364 (u32)&rx_ring[(i + 1) %
Marek Vasut2110c652020-05-23 15:07:30 +0200365 NUM_RX_DESC]));
366 rx_ring[i].rx_buf_addr = 0xffffffff;
367 rx_ring[i].count = cpu_to_le32(PKTSIZE_ALIGN << 16);
wdenk1df49e22002-09-17 21:37:55 +0000368 }
369
Marek Vasut2110c652020-05-23 15:07:30 +0200370 flush_dcache_range((unsigned long)rx_ring,
371 (unsigned long)rx_ring +
372 (sizeof(*rx_ring) * NUM_RX_DESC));
wdenk1df49e22002-09-17 21:37:55 +0000373
Marek Vasutd443d2d2020-05-23 17:13:26 +0200374 priv->rx_next = 0;
Marek Vasut2110c652020-05-23 15:07:30 +0200375}
wdenk1df49e22002-09-17 21:37:55 +0000376
Marek Vasut33346692020-05-23 17:10:03 +0200377static void purge_tx_ring(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000378{
Marek Vasutd443d2d2020-05-23 17:13:26 +0200379 struct eepro100_txfd *tx_ring = priv->tx_ring;
380
381 priv->tx_next = 0;
382 priv->tx_threshold = 0x01208000;
Marek Vasut2110c652020-05-23 15:07:30 +0200383 memset(tx_ring, 0, sizeof(*tx_ring) * NUM_TX_DESC);
wdenk1df49e22002-09-17 21:37:55 +0000384
Marek Vasut2110c652020-05-23 15:07:30 +0200385 flush_dcache_range((unsigned long)tx_ring,
386 (unsigned long)tx_ring +
387 (sizeof(*tx_ring) * NUM_TX_DESC));
388}
wdenk1df49e22002-09-17 21:37:55 +0000389
Marek Vasut2110c652020-05-23 15:07:30 +0200390/* Wait for the chip get the command. */
Marek Vasut33346692020-05-23 17:10:03 +0200391static int wait_for_eepro100(struct eepro100_priv *priv)
Marek Vasut2110c652020-05-23 15:07:30 +0200392{
393 int i;
wdenk1df49e22002-09-17 21:37:55 +0000394
Marek Vasut33346692020-05-23 17:10:03 +0200395 for (i = 0; INW(priv, SCB_CMD) & (CU_CMD_MASK | RU_CMD_MASK); i++) {
Marek Vasut2110c652020-05-23 15:07:30 +0200396 if (i >= TOUT_LOOP)
397 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000398 }
399
Marek Vasut2110c652020-05-23 15:07:30 +0200400 return 1;
wdenk1df49e22002-09-17 21:37:55 +0000401}
402
Marek Vasut33346692020-05-23 17:10:03 +0200403static int eepro100_txcmd_send(struct eepro100_priv *priv,
Marek Vasutd2139bb2020-05-23 14:30:31 +0200404 struct eepro100_txfd *desc)
405{
406 u16 rstat;
407 int i = 0;
408
Marek Vasut7efcae42020-05-23 14:55:26 +0200409 flush_dcache_range((unsigned long)desc,
410 (unsigned long)desc + sizeof(*desc));
411
Marek Vasut33346692020-05-23 17:10:03 +0200412 if (!wait_for_eepro100(priv))
Marek Vasutd2139bb2020-05-23 14:30:31 +0200413 return -ETIMEDOUT;
414
Marek Vasut33346692020-05-23 17:10:03 +0200415 OUTL(priv, phys_to_bus(priv->devno, (u32)desc), SCB_POINTER);
416 OUTW(priv, SCB_M | CU_START, SCB_CMD);
Marek Vasutd2139bb2020-05-23 14:30:31 +0200417
418 while (true) {
Marek Vasut7efcae42020-05-23 14:55:26 +0200419 invalidate_dcache_range((unsigned long)desc,
420 (unsigned long)desc + sizeof(*desc));
Marek Vasutd2139bb2020-05-23 14:30:31 +0200421 rstat = le16_to_cpu(desc->status);
422 if (rstat & CONFIG_SYS_STATUS_C)
423 break;
424
425 if (i++ >= TOUT_LOOP) {
Marek Vasut33346692020-05-23 17:10:03 +0200426 printf("%s: Tx error buffer not ready\n", priv->name);
Marek Vasutd2139bb2020-05-23 14:30:31 +0200427 return -EINVAL;
428 }
429 }
430
Marek Vasut7efcae42020-05-23 14:55:26 +0200431 invalidate_dcache_range((unsigned long)desc,
432 (unsigned long)desc + sizeof(*desc));
Marek Vasutd2139bb2020-05-23 14:30:31 +0200433 rstat = le16_to_cpu(desc->status);
434
435 if (!(rstat & CONFIG_SYS_STATUS_OK)) {
436 printf("TX error status = 0x%08X\n", rstat);
437 return -EIO;
438 }
439
440 return 0;
441}
442
Marek Vasut2110c652020-05-23 15:07:30 +0200443/* SROM Read. */
Marek Vasut33346692020-05-23 17:10:03 +0200444static int read_eeprom(struct eepro100_priv *priv, int location, int addr_len)
Marek Vasut2110c652020-05-23 15:07:30 +0200445{
446 unsigned short retval = 0;
Marek Vasutf9cc66a2020-05-23 16:23:28 +0200447 int read_cmd = location | EE_READ_CMD(addr_len);
Marek Vasut2110c652020-05-23 15:07:30 +0200448 int i;
449
Marek Vasut33346692020-05-23 17:10:03 +0200450 OUTW(priv, EE_ENB & ~EE_CS, SCB_EEPROM);
451 OUTW(priv, EE_ENB, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200452
453 /* Shift the read command bits out. */
454 for (i = 12; i >= 0; i--) {
455 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
456
Marek Vasut33346692020-05-23 17:10:03 +0200457 OUTW(priv, EE_ENB | dataval, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200458 udelay(1);
Marek Vasut33346692020-05-23 17:10:03 +0200459 OUTW(priv, EE_ENB | dataval | EE_SHIFT_CLK, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200460 udelay(1);
461 }
Marek Vasut33346692020-05-23 17:10:03 +0200462 OUTW(priv, EE_ENB, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200463
464 for (i = 15; i >= 0; i--) {
Marek Vasut33346692020-05-23 17:10:03 +0200465 OUTW(priv, EE_ENB | EE_SHIFT_CLK, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200466 udelay(1);
467 retval = (retval << 1) |
Marek Vasut33346692020-05-23 17:10:03 +0200468 !!(INW(priv, SCB_EEPROM) & EE_DATA_READ);
469 OUTW(priv, EE_ENB, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200470 udelay(1);
471 }
472
473 /* Terminate the EEPROM access. */
Marek Vasut33346692020-05-23 17:10:03 +0200474 OUTW(priv, EE_ENB & ~EE_CS, SCB_EEPROM);
Marek Vasut2110c652020-05-23 15:07:30 +0200475 return retval;
476}
477
Marek Vasutd68d2722020-05-23 16:20:25 +0200478#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marek Vasut33346692020-05-23 17:10:03 +0200479static int eepro100_initialize_mii(struct eepro100_priv *priv)
Marek Vasutd68d2722020-05-23 16:20:25 +0200480{
481 /* register mii command access routines */
482 struct mii_dev *mdiodev;
483 int ret;
484
485 mdiodev = mdio_alloc();
486 if (!mdiodev)
487 return -ENOMEM;
488
Marek Vasut33346692020-05-23 17:10:03 +0200489 strncpy(mdiodev->name, priv->name, MDIO_NAME_LEN);
Marek Vasutd68d2722020-05-23 16:20:25 +0200490 mdiodev->read = eepro100_miiphy_read;
491 mdiodev->write = eepro100_miiphy_write;
Marek Vasut4448e602020-05-23 17:55:50 +0200492 mdiodev->priv = priv;
Marek Vasutd68d2722020-05-23 16:20:25 +0200493
494 ret = mdio_register(mdiodev);
495 if (ret < 0) {
496 mdio_free(mdiodev);
497 return ret;
498 }
499
500 return 0;
501}
502#else
Marek Vasut33346692020-05-23 17:10:03 +0200503static int eepro100_initialize_mii(struct eepro100_priv *priv)
Marek Vasutd68d2722020-05-23 16:20:25 +0200504{
505 return 0;
506}
507#endif
508
Marek Vasut2110c652020-05-23 15:07:30 +0200509static struct pci_device_id supported[] = {
Marek Vasutf7fee912020-05-23 15:11:30 +0200510 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82557) },
511 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559) },
512 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559ER) },
513 { }
Marek Vasut2110c652020-05-23 15:07:30 +0200514};
515
Marek Vasutaddde612020-05-23 17:20:39 +0200516static void eepro100_get_hwaddr(struct eepro100_priv *priv)
Marek Vasut2110c652020-05-23 15:07:30 +0200517{
518 u16 sum = 0;
519 int i, j;
Marek Vasut33346692020-05-23 17:10:03 +0200520 int addr_len = read_eeprom(priv, 0, 6) == 0xffff ? 8 : 6;
Marek Vasut2110c652020-05-23 15:07:30 +0200521
522 for (j = 0, i = 0; i < 0x40; i++) {
Marek Vasut33346692020-05-23 17:10:03 +0200523 u16 value = read_eeprom(priv, i, addr_len);
Marek Vasut2110c652020-05-23 15:07:30 +0200524
525 sum += value;
526 if (i < 3) {
Marek Vasut33346692020-05-23 17:10:03 +0200527 priv->enetaddr[j++] = value;
528 priv->enetaddr[j++] = value >> 8;
Marek Vasut2110c652020-05-23 15:07:30 +0200529 }
530 }
531
532 if (sum != 0xBABA) {
Marek Vasut33346692020-05-23 17:10:03 +0200533 memset(priv->enetaddr, 0, ETH_ALEN);
Marek Vasut2110c652020-05-23 15:07:30 +0200534 debug("%s: Invalid EEPROM checksum %#4.4x, check settings before activating this device!\n",
Marek Vasut33346692020-05-23 17:10:03 +0200535 priv->name, sum);
Marek Vasut2110c652020-05-23 15:07:30 +0200536 }
537}
538
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200539static int eepro100_init_common(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000540{
Marek Vasutd443d2d2020-05-23 17:13:26 +0200541 struct eepro100_rxfd *rx_ring = priv->rx_ring;
542 struct eepro100_txfd *tx_ring = priv->tx_ring;
Marek Vasutd2139bb2020-05-23 14:30:31 +0200543 struct eepro100_txfd *ias_cmd, *cfg_cmd;
544 int ret, status = -1;
wdenk1df49e22002-09-17 21:37:55 +0000545 int tx_cur;
wdenk1df49e22002-09-17 21:37:55 +0000546
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200547 /* Reset the ethernet controller */
Marek Vasut33346692020-05-23 17:10:03 +0200548 OUTL(priv, I82559_SELECTIVE_RESET, SCB_PORT);
Simon Glass0db4b942020-05-10 11:40:10 -0600549 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000550
Marek Vasut33346692020-05-23 17:10:03 +0200551 OUTL(priv, I82559_RESET, SCB_PORT);
Simon Glass0db4b942020-05-10 11:40:10 -0600552 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000553
Marek Vasut33346692020-05-23 17:10:03 +0200554 if (!wait_for_eepro100(priv)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200555 printf("Error: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200556 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000557 }
Marek Vasut33346692020-05-23 17:10:03 +0200558 OUTL(priv, 0, SCB_POINTER);
559 OUTW(priv, SCB_M | RUC_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000560
Marek Vasut33346692020-05-23 17:10:03 +0200561 if (!wait_for_eepro100(priv)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200562 printf("Error: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200563 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000564 }
Marek Vasut33346692020-05-23 17:10:03 +0200565 OUTL(priv, 0, SCB_POINTER);
566 OUTW(priv, SCB_M | CU_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000567
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200568 /* Initialize Rx and Tx rings. */
Marek Vasut33346692020-05-23 17:10:03 +0200569 init_rx_ring(priv);
570 purge_tx_ring(priv);
wdenk1df49e22002-09-17 21:37:55 +0000571
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200572 /* Tell the adapter where the RX ring is located. */
Marek Vasut33346692020-05-23 17:10:03 +0200573 if (!wait_for_eepro100(priv)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200574 printf("Error: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200575 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000576 }
577
Marek Vasut7efcae42020-05-23 14:55:26 +0200578 /* RX ring cache was already flushed in init_rx_ring() */
Marek Vasutd443d2d2020-05-23 17:13:26 +0200579 OUTL(priv, phys_to_bus(priv->devno, (u32)&rx_ring[priv->rx_next]),
Marek Vasutc62e0242020-05-23 16:38:41 +0200580 SCB_POINTER);
Marek Vasut33346692020-05-23 17:10:03 +0200581 OUTW(priv, SCB_M | RUC_START, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000582
583 /* Send the Configure frame */
Marek Vasutd443d2d2020-05-23 17:13:26 +0200584 tx_cur = priv->tx_next;
585 priv->tx_next = ((priv->tx_next + 1) % NUM_TX_DESC);
wdenk1df49e22002-09-17 21:37:55 +0000586
Marek Vasutd2139bb2020-05-23 14:30:31 +0200587 cfg_cmd = &tx_ring[tx_cur];
Marek Vasutc0359102020-05-23 13:45:41 +0200588 cfg_cmd->command = cpu_to_le16(CONFIG_SYS_CMD_SUSPEND |
589 CONFIG_SYS_CMD_CONFIGURE);
wdenk1df49e22002-09-17 21:37:55 +0000590 cfg_cmd->status = 0;
Marek Vasut33346692020-05-23 17:10:03 +0200591 cfg_cmd->link = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutd443d2d2020-05-23 17:13:26 +0200592 (u32)&tx_ring[priv->tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000593
Marek Vasutd2139bb2020-05-23 14:30:31 +0200594 memcpy(((struct descriptor *)cfg_cmd)->params, i82558_config_cmd,
Marek Vasut60560d02020-05-23 13:21:43 +0200595 sizeof(i82558_config_cmd));
wdenk1df49e22002-09-17 21:37:55 +0000596
Marek Vasut33346692020-05-23 17:10:03 +0200597 ret = eepro100_txcmd_send(priv, cfg_cmd);
Marek Vasutd2139bb2020-05-23 14:30:31 +0200598 if (ret) {
599 if (ret == -ETIMEDOUT)
600 printf("Error---CONFIG_SYS_CMD_CONFIGURE: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200601 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000602 }
603
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200604 /* Send the Individual Address Setup frame */
Marek Vasutd443d2d2020-05-23 17:13:26 +0200605 tx_cur = priv->tx_next;
606 priv->tx_next = ((priv->tx_next + 1) % NUM_TX_DESC);
wdenk1df49e22002-09-17 21:37:55 +0000607
Marek Vasutd2139bb2020-05-23 14:30:31 +0200608 ias_cmd = &tx_ring[tx_cur];
Marek Vasutc0359102020-05-23 13:45:41 +0200609 ias_cmd->command = cpu_to_le16(CONFIG_SYS_CMD_SUSPEND |
610 CONFIG_SYS_CMD_IAS);
wdenk1df49e22002-09-17 21:37:55 +0000611 ias_cmd->status = 0;
Marek Vasut33346692020-05-23 17:10:03 +0200612 ias_cmd->link = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutd443d2d2020-05-23 17:13:26 +0200613 (u32)&tx_ring[priv->tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000614
Marek Vasut33346692020-05-23 17:10:03 +0200615 memcpy(((struct descriptor *)ias_cmd)->params, priv->enetaddr, 6);
wdenk1df49e22002-09-17 21:37:55 +0000616
Marek Vasut33346692020-05-23 17:10:03 +0200617 ret = eepro100_txcmd_send(priv, ias_cmd);
Marek Vasutd2139bb2020-05-23 14:30:31 +0200618 if (ret) {
619 if (ret == -ETIMEDOUT)
620 printf("Error: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200621 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000622 }
623
Ben Warrende9fcb52008-01-09 18:15:53 -0500624 status = 0;
wdenk1df49e22002-09-17 21:37:55 +0000625
Marek Vasut447271b2020-05-23 13:52:50 +0200626done:
wdenk1df49e22002-09-17 21:37:55 +0000627 return status;
628}
629
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200630static int eepro100_send_common(struct eepro100_priv *priv,
631 void *packet, int length)
wdenk1df49e22002-09-17 21:37:55 +0000632{
Marek Vasutd443d2d2020-05-23 17:13:26 +0200633 struct eepro100_txfd *tx_ring = priv->tx_ring;
Marek Vasut7efcae42020-05-23 14:55:26 +0200634 struct eepro100_txfd *desc;
Marek Vasutd2139bb2020-05-23 14:30:31 +0200635 int ret, status = -1;
wdenk1df49e22002-09-17 21:37:55 +0000636 int tx_cur;
637
638 if (length <= 0) {
Marek Vasut33346692020-05-23 17:10:03 +0200639 printf("%s: bad packet size: %d\n", priv->name, length);
Marek Vasut447271b2020-05-23 13:52:50 +0200640 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000641 }
642
Marek Vasutd443d2d2020-05-23 17:13:26 +0200643 tx_cur = priv->tx_next;
644 priv->tx_next = (priv->tx_next + 1) % NUM_TX_DESC;
wdenk1df49e22002-09-17 21:37:55 +0000645
Marek Vasut7efcae42020-05-23 14:55:26 +0200646 desc = &tx_ring[tx_cur];
647 desc->command = cpu_to_le16(TXCB_CMD_TRANSMIT | TXCB_CMD_SF |
648 TXCB_CMD_S | TXCB_CMD_EL);
649 desc->status = 0;
Marek Vasutd443d2d2020-05-23 17:13:26 +0200650 desc->count = cpu_to_le32(priv->tx_threshold);
Marek Vasut33346692020-05-23 17:10:03 +0200651 desc->link = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutd443d2d2020-05-23 17:13:26 +0200652 (u32)&tx_ring[priv->tx_next]));
Marek Vasut33346692020-05-23 17:10:03 +0200653 desc->tx_desc_addr = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutc62e0242020-05-23 16:38:41 +0200654 (u32)&desc->tx_buf_addr0));
Marek Vasut33346692020-05-23 17:10:03 +0200655 desc->tx_buf_addr0 = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutc62e0242020-05-23 16:38:41 +0200656 (u_long)packet));
Marek Vasut7efcae42020-05-23 14:55:26 +0200657 desc->tx_buf_size0 = cpu_to_le32(length);
wdenk1df49e22002-09-17 21:37:55 +0000658
Marek Vasut33346692020-05-23 17:10:03 +0200659 ret = eepro100_txcmd_send(priv, &tx_ring[tx_cur]);
Marek Vasutd2139bb2020-05-23 14:30:31 +0200660 if (ret) {
661 if (ret == -ETIMEDOUT)
662 printf("%s: Tx error ethernet controller not ready.\n",
Marek Vasut33346692020-05-23 17:10:03 +0200663 priv->name);
Marek Vasut447271b2020-05-23 13:52:50 +0200664 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000665 }
666
667 status = length;
668
Marek Vasut447271b2020-05-23 13:52:50 +0200669done:
wdenk1df49e22002-09-17 21:37:55 +0000670 return status;
671}
672
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200673static int eepro100_recv_common(struct eepro100_priv *priv, uchar **packetp)
wdenk1df49e22002-09-17 21:37:55 +0000674{
Marek Vasutd443d2d2020-05-23 17:13:26 +0200675 struct eepro100_rxfd *rx_ring = priv->rx_ring;
Marek Vasut7efcae42020-05-23 14:55:26 +0200676 struct eepro100_rxfd *desc;
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200677 int length;
678 u16 status;
wdenk1df49e22002-09-17 21:37:55 +0000679
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200680 priv->rx_stat = INW(priv, SCB_STATUS);
681 OUTW(priv, priv->rx_stat & SCB_STATUS_RNR, SCB_STATUS);
wdenk1df49e22002-09-17 21:37:55 +0000682
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200683 desc = &rx_ring[priv->rx_next];
684 invalidate_dcache_range((unsigned long)desc,
685 (unsigned long)desc + sizeof(*desc));
686 status = le16_to_cpu(desc->status);
wdenk1df49e22002-09-17 21:37:55 +0000687
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200688 if (!(status & RFD_STATUS_C))
689 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000690
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200691 /* Valid frame status. */
692 if (status & RFD_STATUS_OK) {
693 /* A valid frame received. */
694 length = le32_to_cpu(desc->count) & 0x3fff;
695 /* Pass the packet up to the protocol layers. */
696 *packetp = desc->data;
697 return length;
698 }
wdenk1df49e22002-09-17 21:37:55 +0000699
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200700 /* There was an error. */
701 printf("RX error status = 0x%08X\n", status);
702 return -EINVAL;
703}
704
705static void eepro100_free_pkt_common(struct eepro100_priv *priv)
706{
707 struct eepro100_rxfd *rx_ring = priv->rx_ring;
708 struct eepro100_rxfd *desc;
709 int rx_prev;
wdenk1df49e22002-09-17 21:37:55 +0000710
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200711 desc = &rx_ring[priv->rx_next];
wdenk1df49e22002-09-17 21:37:55 +0000712
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200713 desc->control = cpu_to_le16(RFD_CONTROL_S);
714 desc->status = 0;
715 desc->count = cpu_to_le32(PKTSIZE_ALIGN << 16);
716 flush_dcache_range((unsigned long)desc,
717 (unsigned long)desc + sizeof(*desc));
wdenk1df49e22002-09-17 21:37:55 +0000718
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200719 rx_prev = (priv->rx_next + NUM_RX_DESC - 1) % NUM_RX_DESC;
720 desc = &rx_ring[rx_prev];
721 desc->control = 0;
722 flush_dcache_range((unsigned long)desc,
723 (unsigned long)desc + sizeof(*desc));
wdenk1df49e22002-09-17 21:37:55 +0000724
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200725 /* Update entry information. */
726 priv->rx_next = (priv->rx_next + 1) % NUM_RX_DESC;
wdenk1df49e22002-09-17 21:37:55 +0000727
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200728 if (!(priv->rx_stat & SCB_STATUS_RNR))
729 return;
wdenk1df49e22002-09-17 21:37:55 +0000730
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200731 printf("%s: Receiver is not ready, restart it !\n", priv->name);
732
733 /* Reinitialize Rx ring. */
734 init_rx_ring(priv);
wdenk1df49e22002-09-17 21:37:55 +0000735
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200736 if (!wait_for_eepro100(priv)) {
737 printf("Error: Can not restart ethernet controller.\n");
738 return;
wdenk1df49e22002-09-17 21:37:55 +0000739 }
740
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200741 /* RX ring cache was already flushed in init_rx_ring() */
742 OUTL(priv, phys_to_bus(priv->devno, (u32)&rx_ring[priv->rx_next]),
743 SCB_POINTER);
744 OUTW(priv, SCB_M | RUC_START, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000745}
746
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200747static void eepro100_halt_common(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000748{
Marek Vasutdc83bfe2020-05-23 12:49:16 +0200749 /* Reset the ethernet controller */
Marek Vasut33346692020-05-23 17:10:03 +0200750 OUTL(priv, I82559_SELECTIVE_RESET, SCB_PORT);
Simon Glass0db4b942020-05-10 11:40:10 -0600751 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000752
Marek Vasut33346692020-05-23 17:10:03 +0200753 OUTL(priv, I82559_RESET, SCB_PORT);
Simon Glass0db4b942020-05-10 11:40:10 -0600754 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000755
Marek Vasut33346692020-05-23 17:10:03 +0200756 if (!wait_for_eepro100(priv)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200757 printf("Error: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200758 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000759 }
Marek Vasut33346692020-05-23 17:10:03 +0200760 OUTL(priv, 0, SCB_POINTER);
761 OUTW(priv, SCB_M | RUC_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000762
Marek Vasut33346692020-05-23 17:10:03 +0200763 if (!wait_for_eepro100(priv)) {
Marek Vasute4211ed2020-05-23 13:17:03 +0200764 printf("Error: Can not reset ethernet controller.\n");
Marek Vasut447271b2020-05-23 13:52:50 +0200765 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000766 }
Marek Vasut33346692020-05-23 17:10:03 +0200767 OUTL(priv, 0, SCB_POINTER);
768 OUTW(priv, SCB_M | CU_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000769
Marek Vasut447271b2020-05-23 13:52:50 +0200770done:
wdenk1df49e22002-09-17 21:37:55 +0000771 return;
772}
773
Marek Vasut0ea22ba2020-05-23 17:28:20 +0200774static int eepro100_init(struct eth_device *dev, bd_t *bis)
775{
776 struct eepro100_priv *priv =
777 container_of(dev, struct eepro100_priv, dev);
778
779 return eepro100_init_common(priv);
780}
781
782static void eepro100_halt(struct eth_device *dev)
783{
784 struct eepro100_priv *priv =
785 container_of(dev, struct eepro100_priv, dev);
786
787 eepro100_halt_common(priv);
788}
789
790static int eepro100_send(struct eth_device *dev, void *packet, int length)
791{
792 struct eepro100_priv *priv =
793 container_of(dev, struct eepro100_priv, dev);
794
795 return eepro100_send_common(priv, packet, length);
796}
797
798static int eepro100_recv(struct eth_device *dev)
799{
800 struct eepro100_priv *priv =
801 container_of(dev, struct eepro100_priv, dev);
802 uchar *packet;
803 int ret;
804
805 ret = eepro100_recv_common(priv, &packet);
806 if (ret > 0)
807 net_process_received_packet(packet, ret);
808 if (ret)
809 eepro100_free_pkt_common(priv);
810
811 return ret;
812}
813
Marek Vasut2110c652020-05-23 15:07:30 +0200814int eepro100_initialize(bd_t *bis)
wdenk1df49e22002-09-17 21:37:55 +0000815{
Marek Vasut13beaa82020-05-23 16:49:07 +0200816 struct eepro100_priv *priv;
Marek Vasut2110c652020-05-23 15:07:30 +0200817 struct eth_device *dev;
Marek Vasutd68d2722020-05-23 16:20:25 +0200818 int card_number = 0;
Marek Vasut2110c652020-05-23 15:07:30 +0200819 u32 iobase, status;
Marek Vasutd68d2722020-05-23 16:20:25 +0200820 pci_dev_t devno;
Marek Vasut2110c652020-05-23 15:07:30 +0200821 int idx = 0;
Marek Vasutd68d2722020-05-23 16:20:25 +0200822 int ret;
wdenk1df49e22002-09-17 21:37:55 +0000823
Marek Vasut2110c652020-05-23 15:07:30 +0200824 while (1) {
825 /* Find PCI device */
826 devno = pci_find_devices(supported, idx++);
827 if (devno < 0)
828 break;
wdenk1df49e22002-09-17 21:37:55 +0000829
Marek Vasut2110c652020-05-23 15:07:30 +0200830 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &iobase);
831 iobase &= ~0xf;
wdenk1df49e22002-09-17 21:37:55 +0000832
Marek Vasut2110c652020-05-23 15:07:30 +0200833 debug("eepro100: Intel i82559 PCI EtherExpressPro @0x%x\n",
834 iobase);
wdenk1df49e22002-09-17 21:37:55 +0000835
Marek Vasut2110c652020-05-23 15:07:30 +0200836 pci_write_config_dword(devno, PCI_COMMAND,
837 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
wdenk1df49e22002-09-17 21:37:55 +0000838
Marek Vasut2110c652020-05-23 15:07:30 +0200839 /* Check if I/O accesses and Bus Mastering are enabled. */
840 pci_read_config_dword(devno, PCI_COMMAND, &status);
841 if (!(status & PCI_COMMAND_MEMORY)) {
842 printf("Error: Can not enable MEM access.\n");
843 continue;
844 }
wdenk1df49e22002-09-17 21:37:55 +0000845
Marek Vasut2110c652020-05-23 15:07:30 +0200846 if (!(status & PCI_COMMAND_MASTER)) {
847 printf("Error: Can not enable Bus Mastering.\n");
848 continue;
849 }
wdenk1df49e22002-09-17 21:37:55 +0000850
Marek Vasut13beaa82020-05-23 16:49:07 +0200851 priv = calloc(1, sizeof(*priv));
852 if (!priv) {
Marek Vasut2110c652020-05-23 15:07:30 +0200853 printf("eepro100: Can not allocate memory\n");
854 break;
855 }
Marek Vasut13beaa82020-05-23 16:49:07 +0200856 dev = &priv->dev;
wdenk1df49e22002-09-17 21:37:55 +0000857
Marek Vasut2110c652020-05-23 15:07:30 +0200858 sprintf(dev->name, "i82559#%d", card_number);
Marek Vasut33346692020-05-23 17:10:03 +0200859 priv->name = dev->name;
860 /* this have to come before bus_to_phys() */
861 priv->devno = devno;
862 priv->iobase = (void __iomem *)bus_to_phys(devno, iobase);
863 priv->enetaddr = dev->enetaddr;
864
Marek Vasut2110c652020-05-23 15:07:30 +0200865 dev->init = eepro100_init;
866 dev->halt = eepro100_halt;
867 dev->send = eepro100_send;
868 dev->recv = eepro100_recv;
Marek Vasut7efcae42020-05-23 14:55:26 +0200869
Marek Vasut2110c652020-05-23 15:07:30 +0200870 eth_register(dev);
wdenk1df49e22002-09-17 21:37:55 +0000871
Marek Vasut33346692020-05-23 17:10:03 +0200872 ret = eepro100_initialize_mii(priv);
Marek Vasutd68d2722020-05-23 16:20:25 +0200873 if (ret) {
874 eth_unregister(dev);
Marek Vasut13beaa82020-05-23 16:49:07 +0200875 free(priv);
Marek Vasutd68d2722020-05-23 16:20:25 +0200876 return ret;
877 }
wdenk1df49e22002-09-17 21:37:55 +0000878
Marek Vasut2110c652020-05-23 15:07:30 +0200879 card_number++;
wdenk1df49e22002-09-17 21:37:55 +0000880
Marek Vasut2110c652020-05-23 15:07:30 +0200881 /* Set the latency timer for value. */
882 pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x20);
wdenk1df49e22002-09-17 21:37:55 +0000883
Marek Vasut2110c652020-05-23 15:07:30 +0200884 udelay(10 * 1000);
885
Marek Vasutaddde612020-05-23 17:20:39 +0200886 eepro100_get_hwaddr(priv);
wdenk1df49e22002-09-17 21:37:55 +0000887 }
Marek Vasut2110c652020-05-23 15:07:30 +0200888
889 return card_number;
wdenk1df49e22002-09-17 21:37:55 +0000890}