blob: feba5327da4d3f02b9acbeb782aa2397606e9a7e [file] [log] [blame]
Tom Rini83d290c2018-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 Vasut047a8002020-05-23 15:07:30 +02008#include <asm/io.h>
Marek Vasut5116aae2020-05-23 14:55:26 +02009#include <cpu_func.h>
wdenk1df49e22002-09-17 21:37:55 +000010#include <malloc.h>
Marek Vasut047a8002020-05-23 15:07:30 +020011#include <miiphy.h>
wdenk1df49e22002-09-17 21:37:55 +000012#include <net.h>
Ben Warren10efa022008-08-31 20:37:00 -070013#include <netdev.h>
wdenk1df49e22002-09-17 21:37:55 +000014#include <pci.h>
Simon Glassc05ed002020-05-10 11:40:11 -060015#include <linux/delay.h>
wdenk1df49e22002-09-17 21:37:55 +000016
Marek Vasutaba283d2020-05-23 12:49:16 +020017/* Ethernet chip registers. */
Marek Vasutf3878f52020-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 Vasutaba283d2020-05-23 12:49:16 +020031/* 82559 SCB status word defnitions */
Wolfgang Denkc570b2f2005-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 Denkc570b2f2005-09-26 01:06:33 +020040#define SCB_INTACK_MASK 0xFD00 /* all the above */
wdenk1df49e22002-09-17 21:37:55 +000041
Wolfgang Denkc570b2f2005-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 Vasutaba283d2020-05-23 12:49:16 +020045/* System control block commands */
wdenk1df49e22002-09-17 21:37:55 +000046/* CU Commands */
Wolfgang Denkc570b2f2005-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 Denkc570b2f2005-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 Denkc570b2f2005-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 Denkc570b2f2005-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 Denkc570b2f2005-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 Vasutdb9f1812020-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 Vasutaba283d2020-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 Vasutaba283d2020-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 Vasutaba283d2020-05-23 12:49:16 +020098/* The EEPROM commands include the alway-set leading bit. */
Marek Vasuta6c06ec2020-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 Vasutaba283d2020-05-23 12:49:16 +0200104/* Receive frame descriptors. */
Marek Vasutf3878f52020-05-23 13:52:50 +0200105struct eepro100_rxfd {
Marek Vasutd47cf872020-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 Vasutd47cf872020-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 Denkc570b2f2005-09-26 01:06:33 +0200116#define RFD_STATUS_OK 0x2000 /* frame received with no errors */
wdenk1df49e22002-09-17 21:37:55 +0000117
Wolfgang Denkc570b2f2005-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 Denkc570b2f2005-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 Denkc570b2f2005-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 Vasutaba283d2020-05-23 12:49:16 +0200138/* Transmit frame descriptors */
Marek Vasutd47cf872020-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 Vasutd47cf872020-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 Vasutf3878f52020-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 Vasutf3878f52020-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 Vasutaba283d2020-05-23 12:49:16 +0200162/* The Speedo3 Rx and Tx frame/buffer descriptors. */
Marek Vasutd47cf872020-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-VILLARD6d0f6bc2008-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-VILLARD6d0f6bc2008-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 Vasutaba283d2020-05-23 12:49:16 +0200180/* Misc. */
Wolfgang Denkc570b2f2005-09-26 01:06:33 +0200181#define NUM_RX_DESC PKTBUFSRX
Marek Vasutaba283d2020-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 Vasutbd159c62020-05-23 16:49:07 +0200198struct eepro100_priv {
Marek Vasutfb8307e2020-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 Vasut88351032020-05-23 17:28:20 +0200205 u16 rx_stat;
Marek Vasutfb8307e2020-05-23 17:13:26 +0200206 /* TX descriptor ring pointer */
207 int tx_next;
208 int tx_threshold;
Marek Vasutaf8ecdf2020-05-23 16:26:20 +0200209#ifdef CONFIG_DM_ETH
210 struct udevice *devno;
211#else
Marek Vasutbd159c62020-05-23 16:49:07 +0200212 struct eth_device dev;
Marek Vasut389da972020-05-23 17:10:03 +0200213 pci_dev_t devno;
Marek Vasutaf8ecdf2020-05-23 16:26:20 +0200214#endif
Marek Vasut389da972020-05-23 17:10:03 +0200215 char *name;
216 void __iomem *iobase;
217 u8 *enetaddr;
Marek Vasutbd159c62020-05-23 16:49:07 +0200218};
219
Marek Vasutaf8ecdf2020-05-23 16:26:20 +0200220#if defined(CONFIG_DM_ETH)
221#define bus_to_phys(dev, a) dm_pci_mem_to_phys((dev), (a))
222#define phys_to_bus(dev, a) dm_pci_phys_to_mem((dev), (a))
223#elif defined(CONFIG_E500)
Marek Vasutfa9e1212020-05-23 16:38:41 +0200224#define bus_to_phys(dev, a) (a)
225#define phys_to_bus(dev, a) (a)
wdenk42d1f032003-10-15 23:53:47 +0000226#else
Marek Vasutfa9e1212020-05-23 16:38:41 +0200227#define bus_to_phys(dev, a) pci_mem_to_phys((dev), (a))
228#define phys_to_bus(dev, a) pci_phys_to_mem((dev), (a))
wdenk42d1f032003-10-15 23:53:47 +0000229#endif
wdenk1df49e22002-09-17 21:37:55 +0000230
Marek Vasut389da972020-05-23 17:10:03 +0200231static int INW(struct eepro100_priv *priv, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000232{
Marek Vasut389da972020-05-23 17:10:03 +0200233 return le16_to_cpu(readw(addr + priv->iobase));
wdenk1df49e22002-09-17 21:37:55 +0000234}
235
Marek Vasut389da972020-05-23 17:10:03 +0200236static void OUTW(struct eepro100_priv *priv, int command, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000237{
Marek Vasut389da972020-05-23 17:10:03 +0200238 writew(cpu_to_le16(command), addr + priv->iobase);
wdenk1df49e22002-09-17 21:37:55 +0000239}
240
Marek Vasut389da972020-05-23 17:10:03 +0200241static void OUTL(struct eepro100_priv *priv, int command, u_long addr)
wdenk1df49e22002-09-17 21:37:55 +0000242{
Marek Vasut389da972020-05-23 17:10:03 +0200243 writel(cpu_to_le32(command), addr + priv->iobase);
wdenk1df49e22002-09-17 21:37:55 +0000244}
245
Jon Loeliger07d38a12007-07-09 17:30:01 -0500246#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marek Vasut389da972020-05-23 17:10:03 +0200247static int INL(struct eepro100_priv *priv, u_long addr)
Wolfgang Denka9127332005-09-26 00:39:59 +0200248{
Marek Vasut389da972020-05-23 17:10:03 +0200249 return le32_to_cpu(readl(addr + priv->iobase));
Wolfgang Denka9127332005-09-26 00:39:59 +0200250}
251
Marek Vasut389da972020-05-23 17:10:03 +0200252static int get_phyreg(struct eepro100_priv *priv, unsigned char addr,
Marek Vasut773af832020-05-23 13:21:43 +0200253 unsigned char reg, unsigned short *value)
Wolfgang Denka9127332005-09-26 00:39:59 +0200254{
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200255 int timeout = 50;
Marek Vasut389da972020-05-23 17:10:03 +0200256 int cmd;
Wolfgang Denka9127332005-09-26 00:39:59 +0200257
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200258 /* read requested data */
259 cmd = (2 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
Marek Vasut389da972020-05-23 17:10:03 +0200260 OUTL(priv, cmd, SCB_CTRL_MDI);
Wolfgang Denkc570b2f2005-09-26 01:06:33 +0200261
Wolfgang Denka9127332005-09-26 00:39:59 +0200262 do {
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200263 udelay(1000);
Marek Vasut389da972020-05-23 17:10:03 +0200264 cmd = INL(priv, SCB_CTRL_MDI);
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200265 } while (!(cmd & (1 << 28)) && (--timeout));
266
267 if (timeout == 0)
268 return -1;
Wolfgang Denka9127332005-09-26 00:39:59 +0200269
Marek Vasutdb9f1812020-05-23 13:17:03 +0200270 *value = (unsigned short)(cmd & 0xffff);
Wolfgang Denkc570b2f2005-09-26 01:06:33 +0200271
Wolfgang Denka9127332005-09-26 00:39:59 +0200272 return 0;
273}
274
Marek Vasut389da972020-05-23 17:10:03 +0200275static int set_phyreg(struct eepro100_priv *priv, unsigned char addr,
Marek Vasut773af832020-05-23 13:21:43 +0200276 unsigned char reg, unsigned short value)
Wolfgang Denka9127332005-09-26 00:39:59 +0200277{
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200278 int timeout = 50;
Marek Vasut389da972020-05-23 17:10:03 +0200279 int cmd;
Wolfgang Denka9127332005-09-26 00:39:59 +0200280
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200281 /* write requested data */
282 cmd = (1 << 26) | ((addr & 0x1f) << 21) | ((reg & 0x1f) << 16);
Marek Vasut389da972020-05-23 17:10:03 +0200283 OUTL(priv, cmd | value, SCB_CTRL_MDI);
Wolfgang Denka9127332005-09-26 00:39:59 +0200284
Marek Vasut389da972020-05-23 17:10:03 +0200285 while (!(INL(priv, SCB_CTRL_MDI) & (1 << 28)) && (--timeout))
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200286 udelay(1000);
287
288 if (timeout == 0)
289 return -1;
Wolfgang Denka9127332005-09-26 00:39:59 +0200290
291 return 0;
292}
Wolfgang Denka9127332005-09-26 00:39:59 +0200293
Marek Vasutaba283d2020-05-23 12:49:16 +0200294/*
295 * Check if given phyaddr is valid, i.e. there is a PHY connected.
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200296 * Do this by checking model value field from ID2 register.
297 */
Marek Vasut389da972020-05-23 17:10:03 +0200298static int verify_phyaddr(struct eepro100_priv *priv, unsigned char addr)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200299{
Marek Vasut389da972020-05-23 17:10:03 +0200300 unsigned short value, model;
301 int ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200302
303 /* read id2 register */
Marek Vasut389da972020-05-23 17:10:03 +0200304 ret = get_phyreg(priv, addr, MII_PHYSID2, &value);
305 if (ret) {
306 printf("%s: mii read timeout!\n", priv->name);
307 return ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200308 }
309
310 /* get model */
Marek Vasut389da972020-05-23 17:10:03 +0200311 model = (value >> 4) & 0x003f;
312 if (!model) {
313 printf("%s: no PHY at address %d\n", priv->name, addr);
314 return -EINVAL;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200315 }
316
Marek Vasut389da972020-05-23 17:10:03 +0200317 return 0;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200318}
319
Joe Hershberger5a49f172016-08-08 11:28:38 -0500320static int eepro100_miiphy_read(struct mii_dev *bus, int addr, int devad,
321 int reg)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200322{
Marek Vasut39daab22020-05-23 17:55:50 +0200323 struct eepro100_priv *priv = bus->priv;
Joe Hershberger5a49f172016-08-08 11:28:38 -0500324 unsigned short value = 0;
Marek Vasut389da972020-05-23 17:10:03 +0200325 int ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200326
Marek Vasut389da972020-05-23 17:10:03 +0200327 ret = verify_phyaddr(priv, addr);
328 if (ret)
329 return ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200330
Marek Vasut389da972020-05-23 17:10:03 +0200331 ret = get_phyreg(priv, addr, reg, &value);
332 if (ret) {
Joe Hershberger5a49f172016-08-08 11:28:38 -0500333 printf("%s: mii read timeout!\n", bus->name);
Marek Vasut389da972020-05-23 17:10:03 +0200334 return ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200335 }
336
Joe Hershberger5a49f172016-08-08 11:28:38 -0500337 return value;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200338}
339
Joe Hershberger5a49f172016-08-08 11:28:38 -0500340static int eepro100_miiphy_write(struct mii_dev *bus, int addr, int devad,
341 int reg, u16 value)
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200342{
Marek Vasut39daab22020-05-23 17:55:50 +0200343 struct eepro100_priv *priv = bus->priv;
Marek Vasut389da972020-05-23 17:10:03 +0200344 int ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200345
Marek Vasut389da972020-05-23 17:10:03 +0200346 ret = verify_phyaddr(priv, addr);
347 if (ret)
348 return ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200349
Marek Vasut389da972020-05-23 17:10:03 +0200350 ret = set_phyreg(priv, addr, reg, value);
351 if (ret) {
Joe Hershberger5a49f172016-08-08 11:28:38 -0500352 printf("%s: mii write timeout!\n", bus->name);
Marek Vasut389da972020-05-23 17:10:03 +0200353 return ret;
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200354 }
355
356 return 0;
357}
Jon Loeliger07d38a12007-07-09 17:30:01 -0500358#endif
Marian Balakowicz63ff0042005-10-28 22:30:33 +0200359
Marek Vasut389da972020-05-23 17:10:03 +0200360static void init_rx_ring(struct eepro100_priv *priv)
Marek Vasut047a8002020-05-23 15:07:30 +0200361{
Marek Vasutfb8307e2020-05-23 17:13:26 +0200362 struct eepro100_rxfd *rx_ring = priv->rx_ring;
Marek Vasut047a8002020-05-23 15:07:30 +0200363 int i;
364
365 for (i = 0; i < NUM_RX_DESC; i++) {
366 rx_ring[i].status = 0;
367 rx_ring[i].control = (i == NUM_RX_DESC - 1) ?
368 cpu_to_le16 (RFD_CONTROL_S) : 0;
369 rx_ring[i].link =
Marek Vasut389da972020-05-23 17:10:03 +0200370 cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutfa9e1212020-05-23 16:38:41 +0200371 (u32)&rx_ring[(i + 1) %
Marek Vasut047a8002020-05-23 15:07:30 +0200372 NUM_RX_DESC]));
373 rx_ring[i].rx_buf_addr = 0xffffffff;
374 rx_ring[i].count = cpu_to_le32(PKTSIZE_ALIGN << 16);
375 }
376
377 flush_dcache_range((unsigned long)rx_ring,
378 (unsigned long)rx_ring +
379 (sizeof(*rx_ring) * NUM_RX_DESC));
380
Marek Vasutfb8307e2020-05-23 17:13:26 +0200381 priv->rx_next = 0;
Marek Vasut047a8002020-05-23 15:07:30 +0200382}
383
Marek Vasut389da972020-05-23 17:10:03 +0200384static void purge_tx_ring(struct eepro100_priv *priv)
Marek Vasut047a8002020-05-23 15:07:30 +0200385{
Marek Vasutfb8307e2020-05-23 17:13:26 +0200386 struct eepro100_txfd *tx_ring = priv->tx_ring;
387
388 priv->tx_next = 0;
389 priv->tx_threshold = 0x01208000;
Marek Vasut047a8002020-05-23 15:07:30 +0200390 memset(tx_ring, 0, sizeof(*tx_ring) * NUM_TX_DESC);
391
392 flush_dcache_range((unsigned long)tx_ring,
393 (unsigned long)tx_ring +
394 (sizeof(*tx_ring) * NUM_TX_DESC));
395}
396
Marek Vasutaba283d2020-05-23 12:49:16 +0200397/* Wait for the chip get the command. */
Marek Vasut389da972020-05-23 17:10:03 +0200398static int wait_for_eepro100(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000399{
400 int i;
401
Marek Vasut389da972020-05-23 17:10:03 +0200402 for (i = 0; INW(priv, SCB_CMD) & (CU_CMD_MASK | RU_CMD_MASK); i++) {
Marek Vasut9b12ff92020-05-23 13:20:14 +0200403 if (i >= TOUT_LOOP)
wdenk1df49e22002-09-17 21:37:55 +0000404 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000405 }
406
407 return 1;
408}
409
Marek Vasut389da972020-05-23 17:10:03 +0200410static int eepro100_txcmd_send(struct eepro100_priv *priv,
Marek Vasut95655b92020-05-23 14:30:31 +0200411 struct eepro100_txfd *desc)
412{
413 u16 rstat;
414 int i = 0;
415
Marek Vasut5116aae2020-05-23 14:55:26 +0200416 flush_dcache_range((unsigned long)desc,
417 (unsigned long)desc + sizeof(*desc));
418
Marek Vasut389da972020-05-23 17:10:03 +0200419 if (!wait_for_eepro100(priv))
Marek Vasut95655b92020-05-23 14:30:31 +0200420 return -ETIMEDOUT;
421
Marek Vasut389da972020-05-23 17:10:03 +0200422 OUTL(priv, phys_to_bus(priv->devno, (u32)desc), SCB_POINTER);
423 OUTW(priv, SCB_M | CU_START, SCB_CMD);
Marek Vasut95655b92020-05-23 14:30:31 +0200424
425 while (true) {
Marek Vasut5116aae2020-05-23 14:55:26 +0200426 invalidate_dcache_range((unsigned long)desc,
427 (unsigned long)desc + sizeof(*desc));
Marek Vasut95655b92020-05-23 14:30:31 +0200428 rstat = le16_to_cpu(desc->status);
429 if (rstat & CONFIG_SYS_STATUS_C)
430 break;
431
432 if (i++ >= TOUT_LOOP) {
Marek Vasut389da972020-05-23 17:10:03 +0200433 printf("%s: Tx error buffer not ready\n", priv->name);
Marek Vasut95655b92020-05-23 14:30:31 +0200434 return -EINVAL;
435 }
436 }
437
Marek Vasut5116aae2020-05-23 14:55:26 +0200438 invalidate_dcache_range((unsigned long)desc,
439 (unsigned long)desc + sizeof(*desc));
Marek Vasut95655b92020-05-23 14:30:31 +0200440 rstat = le16_to_cpu(desc->status);
441
442 if (!(rstat & CONFIG_SYS_STATUS_OK)) {
443 printf("TX error status = 0x%08X\n", rstat);
444 return -EIO;
445 }
446
447 return 0;
448}
449
Marek Vasut047a8002020-05-23 15:07:30 +0200450/* SROM Read. */
Marek Vasut389da972020-05-23 17:10:03 +0200451static int read_eeprom(struct eepro100_priv *priv, int location, int addr_len)
Marek Vasut047a8002020-05-23 15:07:30 +0200452{
453 unsigned short retval = 0;
Marek Vasuta6c06ec2020-05-23 16:23:28 +0200454 int read_cmd = location | EE_READ_CMD(addr_len);
Marek Vasut047a8002020-05-23 15:07:30 +0200455 int i;
456
Marek Vasut389da972020-05-23 17:10:03 +0200457 OUTW(priv, EE_ENB & ~EE_CS, SCB_EEPROM);
458 OUTW(priv, EE_ENB, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200459
460 /* Shift the read command bits out. */
461 for (i = 12; i >= 0; i--) {
462 short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
463
Marek Vasut389da972020-05-23 17:10:03 +0200464 OUTW(priv, EE_ENB | dataval, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200465 udelay(1);
Marek Vasut389da972020-05-23 17:10:03 +0200466 OUTW(priv, EE_ENB | dataval | EE_SHIFT_CLK, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200467 udelay(1);
468 }
Marek Vasut389da972020-05-23 17:10:03 +0200469 OUTW(priv, EE_ENB, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200470
471 for (i = 15; i >= 0; i--) {
Marek Vasut389da972020-05-23 17:10:03 +0200472 OUTW(priv, EE_ENB | EE_SHIFT_CLK, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200473 udelay(1);
474 retval = (retval << 1) |
Marek Vasut389da972020-05-23 17:10:03 +0200475 !!(INW(priv, SCB_EEPROM) & EE_DATA_READ);
476 OUTW(priv, EE_ENB, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200477 udelay(1);
478 }
479
480 /* Terminate the EEPROM access. */
Marek Vasut389da972020-05-23 17:10:03 +0200481 OUTW(priv, EE_ENB & ~EE_CS, SCB_EEPROM);
Marek Vasut047a8002020-05-23 15:07:30 +0200482 return retval;
483}
484
Marek Vasut66fed732020-05-23 16:20:25 +0200485#if defined(CONFIG_MII) || defined(CONFIG_CMD_MII)
Marek Vasut389da972020-05-23 17:10:03 +0200486static int eepro100_initialize_mii(struct eepro100_priv *priv)
Marek Vasut66fed732020-05-23 16:20:25 +0200487{
488 /* register mii command access routines */
489 struct mii_dev *mdiodev;
490 int ret;
491
492 mdiodev = mdio_alloc();
493 if (!mdiodev)
494 return -ENOMEM;
495
Marek Vasut389da972020-05-23 17:10:03 +0200496 strncpy(mdiodev->name, priv->name, MDIO_NAME_LEN);
Marek Vasut66fed732020-05-23 16:20:25 +0200497 mdiodev->read = eepro100_miiphy_read;
498 mdiodev->write = eepro100_miiphy_write;
Marek Vasut39daab22020-05-23 17:55:50 +0200499 mdiodev->priv = priv;
Marek Vasut66fed732020-05-23 16:20:25 +0200500
501 ret = mdio_register(mdiodev);
502 if (ret < 0) {
503 mdio_free(mdiodev);
504 return ret;
505 }
506
507 return 0;
508}
509#else
Marek Vasut389da972020-05-23 17:10:03 +0200510static int eepro100_initialize_mii(struct eepro100_priv *priv)
Marek Vasut66fed732020-05-23 16:20:25 +0200511{
512 return 0;
513}
514#endif
515
Marek Vasut047a8002020-05-23 15:07:30 +0200516static struct pci_device_id supported[] = {
Marek Vasut3a156842020-05-23 15:11:30 +0200517 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82557) },
518 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559) },
519 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_INTEL_82559ER) },
520 { }
Marek Vasut047a8002020-05-23 15:07:30 +0200521};
522
Marek Vasut43b73832020-05-23 17:20:39 +0200523static void eepro100_get_hwaddr(struct eepro100_priv *priv)
Marek Vasut047a8002020-05-23 15:07:30 +0200524{
525 u16 sum = 0;
526 int i, j;
Marek Vasut389da972020-05-23 17:10:03 +0200527 int addr_len = read_eeprom(priv, 0, 6) == 0xffff ? 8 : 6;
Marek Vasut047a8002020-05-23 15:07:30 +0200528
529 for (j = 0, i = 0; i < 0x40; i++) {
Marek Vasut389da972020-05-23 17:10:03 +0200530 u16 value = read_eeprom(priv, i, addr_len);
Marek Vasut047a8002020-05-23 15:07:30 +0200531
532 sum += value;
533 if (i < 3) {
Marek Vasut389da972020-05-23 17:10:03 +0200534 priv->enetaddr[j++] = value;
535 priv->enetaddr[j++] = value >> 8;
Marek Vasut047a8002020-05-23 15:07:30 +0200536 }
537 }
538
539 if (sum != 0xBABA) {
Marek Vasut389da972020-05-23 17:10:03 +0200540 memset(priv->enetaddr, 0, ETH_ALEN);
Marek Vasut047a8002020-05-23 15:07:30 +0200541 debug("%s: Invalid EEPROM checksum %#4.4x, check settings before activating this device!\n",
Marek Vasut389da972020-05-23 17:10:03 +0200542 priv->name, sum);
Marek Vasut047a8002020-05-23 15:07:30 +0200543 }
544}
545
Marek Vasut88351032020-05-23 17:28:20 +0200546static int eepro100_init_common(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000547{
Marek Vasutfb8307e2020-05-23 17:13:26 +0200548 struct eepro100_rxfd *rx_ring = priv->rx_ring;
549 struct eepro100_txfd *tx_ring = priv->tx_ring;
Marek Vasut95655b92020-05-23 14:30:31 +0200550 struct eepro100_txfd *ias_cmd, *cfg_cmd;
551 int ret, status = -1;
wdenk1df49e22002-09-17 21:37:55 +0000552 int tx_cur;
wdenk1df49e22002-09-17 21:37:55 +0000553
Marek Vasutaba283d2020-05-23 12:49:16 +0200554 /* Reset the ethernet controller */
Marek Vasut389da972020-05-23 17:10:03 +0200555 OUTL(priv, I82559_SELECTIVE_RESET, SCB_PORT);
Simon Glass07e11142020-05-10 11:40:10 -0600556 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000557
Marek Vasut389da972020-05-23 17:10:03 +0200558 OUTL(priv, I82559_RESET, SCB_PORT);
Simon Glass07e11142020-05-10 11:40:10 -0600559 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000560
Marek Vasut389da972020-05-23 17:10:03 +0200561 if (!wait_for_eepro100(priv)) {
Marek Vasutdb9f1812020-05-23 13:17:03 +0200562 printf("Error: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200563 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000564 }
Marek Vasut389da972020-05-23 17:10:03 +0200565 OUTL(priv, 0, SCB_POINTER);
566 OUTW(priv, SCB_M | RUC_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000567
Marek Vasut389da972020-05-23 17:10:03 +0200568 if (!wait_for_eepro100(priv)) {
Marek Vasutdb9f1812020-05-23 13:17:03 +0200569 printf("Error: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200570 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000571 }
Marek Vasut389da972020-05-23 17:10:03 +0200572 OUTL(priv, 0, SCB_POINTER);
573 OUTW(priv, SCB_M | CU_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000574
Marek Vasutaba283d2020-05-23 12:49:16 +0200575 /* Initialize Rx and Tx rings. */
Marek Vasut389da972020-05-23 17:10:03 +0200576 init_rx_ring(priv);
577 purge_tx_ring(priv);
wdenk1df49e22002-09-17 21:37:55 +0000578
Marek Vasutaba283d2020-05-23 12:49:16 +0200579 /* Tell the adapter where the RX ring is located. */
Marek Vasut389da972020-05-23 17:10:03 +0200580 if (!wait_for_eepro100(priv)) {
Marek Vasutdb9f1812020-05-23 13:17:03 +0200581 printf("Error: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200582 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000583 }
584
Marek Vasut5116aae2020-05-23 14:55:26 +0200585 /* RX ring cache was already flushed in init_rx_ring() */
Marek Vasutfb8307e2020-05-23 17:13:26 +0200586 OUTL(priv, phys_to_bus(priv->devno, (u32)&rx_ring[priv->rx_next]),
Marek Vasutfa9e1212020-05-23 16:38:41 +0200587 SCB_POINTER);
Marek Vasut389da972020-05-23 17:10:03 +0200588 OUTW(priv, SCB_M | RUC_START, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000589
590 /* Send the Configure frame */
Marek Vasutfb8307e2020-05-23 17:13:26 +0200591 tx_cur = priv->tx_next;
592 priv->tx_next = ((priv->tx_next + 1) % NUM_TX_DESC);
wdenk1df49e22002-09-17 21:37:55 +0000593
Marek Vasut95655b92020-05-23 14:30:31 +0200594 cfg_cmd = &tx_ring[tx_cur];
Marek Vasutb0131732020-05-23 13:45:41 +0200595 cfg_cmd->command = cpu_to_le16(CONFIG_SYS_CMD_SUSPEND |
596 CONFIG_SYS_CMD_CONFIGURE);
wdenk1df49e22002-09-17 21:37:55 +0000597 cfg_cmd->status = 0;
Marek Vasut389da972020-05-23 17:10:03 +0200598 cfg_cmd->link = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutfb8307e2020-05-23 17:13:26 +0200599 (u32)&tx_ring[priv->tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000600
Marek Vasut95655b92020-05-23 14:30:31 +0200601 memcpy(((struct descriptor *)cfg_cmd)->params, i82558_config_cmd,
Marek Vasut773af832020-05-23 13:21:43 +0200602 sizeof(i82558_config_cmd));
wdenk1df49e22002-09-17 21:37:55 +0000603
Marek Vasut389da972020-05-23 17:10:03 +0200604 ret = eepro100_txcmd_send(priv, cfg_cmd);
Marek Vasut95655b92020-05-23 14:30:31 +0200605 if (ret) {
606 if (ret == -ETIMEDOUT)
607 printf("Error---CONFIG_SYS_CMD_CONFIGURE: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200608 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000609 }
610
Marek Vasutaba283d2020-05-23 12:49:16 +0200611 /* Send the Individual Address Setup frame */
Marek Vasutfb8307e2020-05-23 17:13:26 +0200612 tx_cur = priv->tx_next;
613 priv->tx_next = ((priv->tx_next + 1) % NUM_TX_DESC);
wdenk1df49e22002-09-17 21:37:55 +0000614
Marek Vasut95655b92020-05-23 14:30:31 +0200615 ias_cmd = &tx_ring[tx_cur];
Marek Vasutb0131732020-05-23 13:45:41 +0200616 ias_cmd->command = cpu_to_le16(CONFIG_SYS_CMD_SUSPEND |
617 CONFIG_SYS_CMD_IAS);
wdenk1df49e22002-09-17 21:37:55 +0000618 ias_cmd->status = 0;
Marek Vasut389da972020-05-23 17:10:03 +0200619 ias_cmd->link = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutfb8307e2020-05-23 17:13:26 +0200620 (u32)&tx_ring[priv->tx_next]));
wdenk1df49e22002-09-17 21:37:55 +0000621
Marek Vasut389da972020-05-23 17:10:03 +0200622 memcpy(((struct descriptor *)ias_cmd)->params, priv->enetaddr, 6);
wdenk1df49e22002-09-17 21:37:55 +0000623
Marek Vasut389da972020-05-23 17:10:03 +0200624 ret = eepro100_txcmd_send(priv, ias_cmd);
Marek Vasut95655b92020-05-23 14:30:31 +0200625 if (ret) {
626 if (ret == -ETIMEDOUT)
627 printf("Error: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200628 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000629 }
630
Ben Warren422b1a02008-01-09 18:15:53 -0500631 status = 0;
wdenk1df49e22002-09-17 21:37:55 +0000632
Marek Vasutf3878f52020-05-23 13:52:50 +0200633done:
wdenk1df49e22002-09-17 21:37:55 +0000634 return status;
635}
636
Marek Vasut88351032020-05-23 17:28:20 +0200637static int eepro100_send_common(struct eepro100_priv *priv,
638 void *packet, int length)
wdenk1df49e22002-09-17 21:37:55 +0000639{
Marek Vasutfb8307e2020-05-23 17:13:26 +0200640 struct eepro100_txfd *tx_ring = priv->tx_ring;
Marek Vasut5116aae2020-05-23 14:55:26 +0200641 struct eepro100_txfd *desc;
Marek Vasut95655b92020-05-23 14:30:31 +0200642 int ret, status = -1;
wdenk1df49e22002-09-17 21:37:55 +0000643 int tx_cur;
644
645 if (length <= 0) {
Marek Vasut389da972020-05-23 17:10:03 +0200646 printf("%s: bad packet size: %d\n", priv->name, length);
Marek Vasutf3878f52020-05-23 13:52:50 +0200647 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000648 }
649
Marek Vasutfb8307e2020-05-23 17:13:26 +0200650 tx_cur = priv->tx_next;
651 priv->tx_next = (priv->tx_next + 1) % NUM_TX_DESC;
wdenk1df49e22002-09-17 21:37:55 +0000652
Marek Vasut5116aae2020-05-23 14:55:26 +0200653 desc = &tx_ring[tx_cur];
654 desc->command = cpu_to_le16(TXCB_CMD_TRANSMIT | TXCB_CMD_SF |
655 TXCB_CMD_S | TXCB_CMD_EL);
656 desc->status = 0;
Marek Vasutfb8307e2020-05-23 17:13:26 +0200657 desc->count = cpu_to_le32(priv->tx_threshold);
Marek Vasut389da972020-05-23 17:10:03 +0200658 desc->link = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutfb8307e2020-05-23 17:13:26 +0200659 (u32)&tx_ring[priv->tx_next]));
Marek Vasut389da972020-05-23 17:10:03 +0200660 desc->tx_desc_addr = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutfa9e1212020-05-23 16:38:41 +0200661 (u32)&desc->tx_buf_addr0));
Marek Vasut389da972020-05-23 17:10:03 +0200662 desc->tx_buf_addr0 = cpu_to_le32(phys_to_bus(priv->devno,
Marek Vasutfa9e1212020-05-23 16:38:41 +0200663 (u_long)packet));
Marek Vasut5116aae2020-05-23 14:55:26 +0200664 desc->tx_buf_size0 = cpu_to_le32(length);
wdenk1df49e22002-09-17 21:37:55 +0000665
Marek Vasut389da972020-05-23 17:10:03 +0200666 ret = eepro100_txcmd_send(priv, &tx_ring[tx_cur]);
Marek Vasut95655b92020-05-23 14:30:31 +0200667 if (ret) {
668 if (ret == -ETIMEDOUT)
669 printf("%s: Tx error ethernet controller not ready.\n",
Marek Vasut389da972020-05-23 17:10:03 +0200670 priv->name);
Marek Vasutf3878f52020-05-23 13:52:50 +0200671 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000672 }
673
674 status = length;
675
Marek Vasutf3878f52020-05-23 13:52:50 +0200676done:
wdenk1df49e22002-09-17 21:37:55 +0000677 return status;
678}
679
Marek Vasut88351032020-05-23 17:28:20 +0200680static int eepro100_recv_common(struct eepro100_priv *priv, uchar **packetp)
wdenk1df49e22002-09-17 21:37:55 +0000681{
Marek Vasutfb8307e2020-05-23 17:13:26 +0200682 struct eepro100_rxfd *rx_ring = priv->rx_ring;
Marek Vasut5116aae2020-05-23 14:55:26 +0200683 struct eepro100_rxfd *desc;
Marek Vasut88351032020-05-23 17:28:20 +0200684 int length;
685 u16 status;
wdenk1df49e22002-09-17 21:37:55 +0000686
Marek Vasut88351032020-05-23 17:28:20 +0200687 priv->rx_stat = INW(priv, SCB_STATUS);
688 OUTW(priv, priv->rx_stat & SCB_STATUS_RNR, SCB_STATUS);
wdenk1df49e22002-09-17 21:37:55 +0000689
Marek Vasut88351032020-05-23 17:28:20 +0200690 desc = &rx_ring[priv->rx_next];
691 invalidate_dcache_range((unsigned long)desc,
692 (unsigned long)desc + sizeof(*desc));
693 status = le16_to_cpu(desc->status);
wdenk1df49e22002-09-17 21:37:55 +0000694
Marek Vasut88351032020-05-23 17:28:20 +0200695 if (!(status & RFD_STATUS_C))
696 return 0;
wdenk1df49e22002-09-17 21:37:55 +0000697
Marek Vasut88351032020-05-23 17:28:20 +0200698 /* Valid frame status. */
699 if (status & RFD_STATUS_OK) {
700 /* A valid frame received. */
701 length = le32_to_cpu(desc->count) & 0x3fff;
702 /* Pass the packet up to the protocol layers. */
703 *packetp = desc->data;
704 return length;
wdenk1df49e22002-09-17 21:37:55 +0000705 }
706
Marek Vasut88351032020-05-23 17:28:20 +0200707 /* There was an error. */
708 printf("RX error status = 0x%08X\n", status);
709 return -EINVAL;
wdenk1df49e22002-09-17 21:37:55 +0000710}
711
Marek Vasut88351032020-05-23 17:28:20 +0200712static void eepro100_free_pkt_common(struct eepro100_priv *priv)
wdenk1df49e22002-09-17 21:37:55 +0000713{
Marek Vasut88351032020-05-23 17:28:20 +0200714 struct eepro100_rxfd *rx_ring = priv->rx_ring;
715 struct eepro100_rxfd *desc;
716 int rx_prev;
Marek Vasut389da972020-05-23 17:10:03 +0200717
Marek Vasut88351032020-05-23 17:28:20 +0200718 desc = &rx_ring[priv->rx_next];
719
720 desc->control = cpu_to_le16(RFD_CONTROL_S);
721 desc->status = 0;
722 desc->count = cpu_to_le32(PKTSIZE_ALIGN << 16);
723 flush_dcache_range((unsigned long)desc,
724 (unsigned long)desc + sizeof(*desc));
725
726 rx_prev = (priv->rx_next + NUM_RX_DESC - 1) % NUM_RX_DESC;
727 desc = &rx_ring[rx_prev];
728 desc->control = 0;
729 flush_dcache_range((unsigned long)desc,
730 (unsigned long)desc + sizeof(*desc));
731
732 /* Update entry information. */
733 priv->rx_next = (priv->rx_next + 1) % NUM_RX_DESC;
734
735 if (!(priv->rx_stat & SCB_STATUS_RNR))
736 return;
737
738 printf("%s: Receiver is not ready, restart it !\n", priv->name);
739
740 /* Reinitialize Rx ring. */
741 init_rx_ring(priv);
742
743 if (!wait_for_eepro100(priv)) {
744 printf("Error: Can not restart ethernet controller.\n");
745 return;
746 }
747
748 /* RX ring cache was already flushed in init_rx_ring() */
749 OUTL(priv, phys_to_bus(priv->devno, (u32)&rx_ring[priv->rx_next]),
750 SCB_POINTER);
751 OUTW(priv, SCB_M | RUC_START, SCB_CMD);
752}
753
754static void eepro100_halt_common(struct eepro100_priv *priv)
755{
Marek Vasutaba283d2020-05-23 12:49:16 +0200756 /* Reset the ethernet controller */
Marek Vasut389da972020-05-23 17:10:03 +0200757 OUTL(priv, I82559_SELECTIVE_RESET, SCB_PORT);
Simon Glass07e11142020-05-10 11:40:10 -0600758 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000759
Marek Vasut389da972020-05-23 17:10:03 +0200760 OUTL(priv, I82559_RESET, SCB_PORT);
Simon Glass07e11142020-05-10 11:40:10 -0600761 udelay(20);
wdenk1df49e22002-09-17 21:37:55 +0000762
Marek Vasut389da972020-05-23 17:10:03 +0200763 if (!wait_for_eepro100(priv)) {
Marek Vasutdb9f1812020-05-23 13:17:03 +0200764 printf("Error: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200765 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000766 }
Marek Vasut389da972020-05-23 17:10:03 +0200767 OUTL(priv, 0, SCB_POINTER);
768 OUTW(priv, SCB_M | RUC_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000769
Marek Vasut389da972020-05-23 17:10:03 +0200770 if (!wait_for_eepro100(priv)) {
Marek Vasutdb9f1812020-05-23 13:17:03 +0200771 printf("Error: Can not reset ethernet controller.\n");
Marek Vasutf3878f52020-05-23 13:52:50 +0200772 goto done;
wdenk1df49e22002-09-17 21:37:55 +0000773 }
Marek Vasut389da972020-05-23 17:10:03 +0200774 OUTL(priv, 0, SCB_POINTER);
775 OUTW(priv, SCB_M | CU_ADDR_LOAD, SCB_CMD);
wdenk1df49e22002-09-17 21:37:55 +0000776
Marek Vasutf3878f52020-05-23 13:52:50 +0200777done:
wdenk1df49e22002-09-17 21:37:55 +0000778 return;
779}
780
Marek Vasutaf8ecdf2020-05-23 16:26:20 +0200781#ifndef CONFIG_DM_ETH
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900782static int eepro100_init(struct eth_device *dev, struct bd_info *bis)
Marek Vasut88351032020-05-23 17:28:20 +0200783{
784 struct eepro100_priv *priv =
785 container_of(dev, struct eepro100_priv, dev);
786
787 return eepro100_init_common(priv);
788}
789
790static void eepro100_halt(struct eth_device *dev)
791{
792 struct eepro100_priv *priv =
793 container_of(dev, struct eepro100_priv, dev);
794
795 eepro100_halt_common(priv);
796}
797
798static int eepro100_send(struct eth_device *dev, void *packet, int length)
799{
800 struct eepro100_priv *priv =
801 container_of(dev, struct eepro100_priv, dev);
802
803 return eepro100_send_common(priv, packet, length);
804}
805
806static int eepro100_recv(struct eth_device *dev)
807{
808 struct eepro100_priv *priv =
809 container_of(dev, struct eepro100_priv, dev);
810 uchar *packet;
811 int ret;
812
813 ret = eepro100_recv_common(priv, &packet);
814 if (ret > 0)
815 net_process_received_packet(packet, ret);
816 if (ret)
817 eepro100_free_pkt_common(priv);
818
819 return ret;
820}
821
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900822int eepro100_initialize(struct bd_info *bis)
wdenk1df49e22002-09-17 21:37:55 +0000823{
Marek Vasutbd159c62020-05-23 16:49:07 +0200824 struct eepro100_priv *priv;
Marek Vasut047a8002020-05-23 15:07:30 +0200825 struct eth_device *dev;
Marek Vasut66fed732020-05-23 16:20:25 +0200826 int card_number = 0;
Marek Vasut047a8002020-05-23 15:07:30 +0200827 u32 iobase, status;
Marek Vasut66fed732020-05-23 16:20:25 +0200828 pci_dev_t devno;
Marek Vasut047a8002020-05-23 15:07:30 +0200829 int idx = 0;
Marek Vasut66fed732020-05-23 16:20:25 +0200830 int ret;
wdenk1df49e22002-09-17 21:37:55 +0000831
Marek Vasut047a8002020-05-23 15:07:30 +0200832 while (1) {
833 /* Find PCI device */
834 devno = pci_find_devices(supported, idx++);
835 if (devno < 0)
836 break;
wdenk1df49e22002-09-17 21:37:55 +0000837
Marek Vasut047a8002020-05-23 15:07:30 +0200838 pci_read_config_dword(devno, PCI_BASE_ADDRESS_0, &iobase);
839 iobase &= ~0xf;
wdenk1df49e22002-09-17 21:37:55 +0000840
Marek Vasut047a8002020-05-23 15:07:30 +0200841 debug("eepro100: Intel i82559 PCI EtherExpressPro @0x%x\n",
842 iobase);
wdenk1df49e22002-09-17 21:37:55 +0000843
Marek Vasut047a8002020-05-23 15:07:30 +0200844 pci_write_config_dword(devno, PCI_COMMAND,
845 PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER);
wdenk1df49e22002-09-17 21:37:55 +0000846
Marek Vasut047a8002020-05-23 15:07:30 +0200847 /* Check if I/O accesses and Bus Mastering are enabled. */
848 pci_read_config_dword(devno, PCI_COMMAND, &status);
849 if (!(status & PCI_COMMAND_MEMORY)) {
850 printf("Error: Can not enable MEM access.\n");
851 continue;
wdenk1df49e22002-09-17 21:37:55 +0000852 }
Marek Vasut047a8002020-05-23 15:07:30 +0200853
854 if (!(status & PCI_COMMAND_MASTER)) {
855 printf("Error: Can not enable Bus Mastering.\n");
856 continue;
857 }
858
Marek Vasutbd159c62020-05-23 16:49:07 +0200859 priv = calloc(1, sizeof(*priv));
860 if (!priv) {
Marek Vasut047a8002020-05-23 15:07:30 +0200861 printf("eepro100: Can not allocate memory\n");
862 break;
863 }
Marek Vasutbd159c62020-05-23 16:49:07 +0200864 dev = &priv->dev;
Marek Vasut047a8002020-05-23 15:07:30 +0200865
866 sprintf(dev->name, "i82559#%d", card_number);
Marek Vasut389da972020-05-23 17:10:03 +0200867 priv->name = dev->name;
868 /* this have to come before bus_to_phys() */
869 priv->devno = devno;
870 priv->iobase = (void __iomem *)bus_to_phys(devno, iobase);
871 priv->enetaddr = dev->enetaddr;
872
Marek Vasut047a8002020-05-23 15:07:30 +0200873 dev->init = eepro100_init;
874 dev->halt = eepro100_halt;
875 dev->send = eepro100_send;
876 dev->recv = eepro100_recv;
877
878 eth_register(dev);
879
Marek Vasut389da972020-05-23 17:10:03 +0200880 ret = eepro100_initialize_mii(priv);
Marek Vasut66fed732020-05-23 16:20:25 +0200881 if (ret) {
882 eth_unregister(dev);
Marek Vasutbd159c62020-05-23 16:49:07 +0200883 free(priv);
Marek Vasut66fed732020-05-23 16:20:25 +0200884 return ret;
885 }
Marek Vasut047a8002020-05-23 15:07:30 +0200886
887 card_number++;
888
889 /* Set the latency timer for value. */
890 pci_write_config_byte(devno, PCI_LATENCY_TIMER, 0x20);
891
892 udelay(10 * 1000);
893
Marek Vasut43b73832020-05-23 17:20:39 +0200894 eepro100_get_hwaddr(priv);
wdenk1df49e22002-09-17 21:37:55 +0000895 }
896
Marek Vasut047a8002020-05-23 15:07:30 +0200897 return card_number;
wdenk1df49e22002-09-17 21:37:55 +0000898}
Marek Vasutaf8ecdf2020-05-23 16:26:20 +0200899
900#else /* DM_ETH */
901static int eepro100_start(struct udevice *dev)
902{
903 struct eth_pdata *plat = dev_get_platdata(dev);
904 struct eepro100_priv *priv = dev_get_priv(dev);
905
906 memcpy(priv->enetaddr, plat->enetaddr, sizeof(plat->enetaddr));
907
908 return eepro100_init_common(priv);
909}
910
911static void eepro100_stop(struct udevice *dev)
912{
913 struct eepro100_priv *priv = dev_get_priv(dev);
914
915 eepro100_halt_common(priv);
916}
917
918static int eepro100_send(struct udevice *dev, void *packet, int length)
919{
920 struct eepro100_priv *priv = dev_get_priv(dev);
921 int ret;
922
923 ret = eepro100_send_common(priv, packet, length);
924
925 return ret ? 0 : -ETIMEDOUT;
926}
927
928static int eepro100_recv(struct udevice *dev, int flags, uchar **packetp)
929{
930 struct eepro100_priv *priv = dev_get_priv(dev);
931
932 return eepro100_recv_common(priv, packetp);
933}
934
935static int eepro100_free_pkt(struct udevice *dev, uchar *packet, int length)
936{
937 struct eepro100_priv *priv = dev_get_priv(dev);
938
939 eepro100_free_pkt_common(priv);
940
941 return 0;
942}
943
944static int eepro100_read_rom_hwaddr(struct udevice *dev)
945{
946 struct eepro100_priv *priv = dev_get_priv(dev);
947
948 eepro100_get_hwaddr(priv);
949
950 return 0;
951}
952
953static int eepro100_bind(struct udevice *dev)
954{
955 static int card_number;
956 char name[16];
957
958 sprintf(name, "eepro100#%u", card_number++);
959
960 return device_set_name(dev, name);
961}
962
963static int eepro100_probe(struct udevice *dev)
964{
965 struct eth_pdata *plat = dev_get_platdata(dev);
966 struct eepro100_priv *priv = dev_get_priv(dev);
967 u16 command, status;
968 u32 iobase;
969 int ret;
970
971 dm_pci_read_config32(dev, PCI_BASE_ADDRESS_0, &iobase);
972 iobase &= ~0xf;
973
974 debug("eepro100: Intel i82559 PCI EtherExpressPro @0x%x\n", iobase);
975
976 priv->devno = dev;
977 priv->enetaddr = plat->enetaddr;
978 priv->iobase = (void __iomem *)bus_to_phys(dev, iobase);
979
980 command = PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
981 dm_pci_write_config16(dev, PCI_COMMAND, command);
982 dm_pci_read_config16(dev, PCI_COMMAND, &status);
983 if ((status & command) != command) {
984 printf("eepro100: Couldn't enable IO access or Bus Mastering\n");
985 return -EINVAL;
986 }
987
988 ret = eepro100_initialize_mii(priv);
989 if (ret)
990 return ret;
991
992 dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x20);
993
994 return 0;
995}
996
997static const struct eth_ops eepro100_ops = {
998 .start = eepro100_start,
999 .send = eepro100_send,
1000 .recv = eepro100_recv,
1001 .stop = eepro100_stop,
1002 .free_pkt = eepro100_free_pkt,
1003 .read_rom_hwaddr = eepro100_read_rom_hwaddr,
1004};
1005
1006U_BOOT_DRIVER(eth_eepro100) = {
1007 .name = "eth_eepro100",
1008 .id = UCLASS_ETH,
1009 .bind = eepro100_bind,
1010 .probe = eepro100_probe,
1011 .ops = &eepro100_ops,
1012 .priv_auto_alloc_size = sizeof(struct eepro100_priv),
1013 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
1014};
1015
1016U_BOOT_PCI_DEVICE(eth_eepro100, supported);
1017#endif